Skip to content

Commit 5334fc2

Browse files
committed
Merge patch series "Allow inlining C helpers into Rust when using LTO"
Alice Ryhl <aliceryhl@google.com> says: This patch series adds __rust_helper to every single rust helper. These changes were generated by adding __rust_helper and running ClangFormat. Unrelated formatting changes were removed manually. Why is __rust_helper needed? ============================ Currently, C helpers cannot be inlined into Rust even when using LTO because LLVM detects slightly different options on the codegen units. * LLVM doesn't want to inline functions compiled with `-fno-delete-null-pointer-checks` with code compiled without. The C CGUs all have this enabled and Rust CGUs don't. Inlining is okay since this is one of the hardening features that does not change the ABI, and we shouldn't have null pointer dereferences in these helpers. * LLVM doesn't want to inline functions with different list of builtins. C side has `-fno-builtin-wcslen`; `wcslen` is not a Rust builtin, so they should be compatible, but LLVM does not perform inlining due to attributes mismatch. * clang and Rust doesn't have the exact target string. Clang generates `+cmov,+cx8,+fxsr` but Rust doesn't enable them (in fact, Rust will complain if `-Ctarget-feature=+cmov,+cx8,+fxsr` is used). x86-64 always enable these features, so they are in fact the same target string, but LLVM doesn't understand this and so inlining is inhibited. This can be bypassed with `--ignore-tti-inline-compatible`, but this is a hidden option. (This analysis was written by Gary Guo.) How is this fixed? ================== To fix this we need to add __always_inline to all helpers when compiling with LTO. However, it should not be added when running bindgen as bindgen will ignore functions marked inline. To achieve this, we are using a #define called __rust_helper that is defined differently depending on whether bindgen is running or not. Note that __rust_helper is currently always #defined to nothing. Changing it to __always_inline will happen separately in another patch series. * patches from https://patch.msgid.link/20251202-define-rust-helper-v1-0-a2e13cbc17a6@google.com: rust: poll: add __rust_helper to helpers rust: pid_namespace: add __rust_helper to helpers rust: fs: add __rust_helper to helpers Link: https://patch.msgid.link/20251202-define-rust-helper-v1-0-a2e13cbc17a6@google.com Signed-off-by: Christian Brauner <brauner@kernel.org>
2 parents 8f0b4cc + 268eaa8 commit 5334fc2

3 files changed

Lines changed: 9 additions & 6 deletions

File tree

rust/helpers/fs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <linux/fs.h>
88

9-
struct file *rust_helper_get_file(struct file *f)
9+
__rust_helper struct file *rust_helper_get_file(struct file *f)
1010
{
1111
return get_file(f);
1212
}

rust/helpers/pid_namespace.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
#include <linux/pid_namespace.h>
44
#include <linux/cleanup.h>
55

6-
struct pid_namespace *rust_helper_get_pid_ns(struct pid_namespace *ns)
6+
__rust_helper struct pid_namespace *
7+
rust_helper_get_pid_ns(struct pid_namespace *ns)
78
{
89
return get_pid_ns(ns);
910
}
1011

11-
void rust_helper_put_pid_ns(struct pid_namespace *ns)
12+
__rust_helper void rust_helper_put_pid_ns(struct pid_namespace *ns)
1213
{
1314
put_pid_ns(ns);
1415
}
1516

1617
/* Get a reference on a task's pid namespace. */
17-
struct pid_namespace *rust_helper_task_get_pid_ns(struct task_struct *task)
18+
__rust_helper struct pid_namespace *
19+
rust_helper_task_get_pid_ns(struct task_struct *task)
1820
{
1921
struct pid_namespace *pid_ns;
2022

rust/helpers/poll.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
#include <linux/export.h>
44
#include <linux/poll.h>
55

6-
void rust_helper_poll_wait(struct file *filp, wait_queue_head_t *wait_address,
7-
poll_table *p)
6+
__rust_helper void rust_helper_poll_wait(struct file *filp,
7+
wait_queue_head_t *wait_address,
8+
poll_table *p)
89
{
910
poll_wait(filp, wait_address, p);
1011
}

0 commit comments

Comments
 (0)