Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 169484a

Browse files
Darksonnrostedt
authored andcommitted
rust: add arch_static_branch
To allow the Rust implementation of static_key_false to use runtime code patching instead of the generic implementation, pull in the relevant inline assembly from the jump_label.h header by running the C preprocessor on a .rs.S file. Build rules are added for .rs.S files. Since the relevant inline asm has been adjusted to export the inline asm via the ARCH_STATIC_BRANCH_ASM macro in a consistent way, the Rust side does not need architecture specific code to pull in the asm. It is not possible to use the existing C implementation of arch_static_branch via a Rust helper because it passes the argument `key` to inline assembly as an 'i' parameter. Any attempt to add a C helper for this function will fail to compile because the value of `key` must be known at compile-time. Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Josh Poimboeuf <jpoimboe@kernel.org> Cc: Jason Baron <jbaron@akamai.com> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Alex Gaynor <alex.gaynor@gmail.com> Cc: Wedson Almeida Filho <wedsonaf@gmail.com> Cc: Gary Guo <gary@garyguo.net> Cc: " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " <bjorn3_gh@protonmail.com> Cc: Benno Lossin <benno.lossin@proton.me> Cc: Andreas Hindborg <a.hindborg@kernel.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@redhat.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Sean Christopherson <seanjc@google.com> Cc: Uros Bizjak <ubizjak@gmail.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will@kernel.org> Cc: Marc Zyngier <maz@kernel.org> Cc: Oliver Upton <oliver.upton@linux.dev> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Fuad Tabba <tabba@google.com> Cc: Paul Walmsley <paul.walmsley@sifive.com> Cc: Palmer Dabbelt <palmer@dabbelt.com> Cc: Albert Ou <aou@eecs.berkeley.edu> Cc: Anup Patel <apatel@ventanamicro.com> Cc: Andrew Jones <ajones@ventanamicro.com> Cc: Alexandre Ghiti <alexghiti@rivosinc.com> Cc: Conor Dooley <conor.dooley@microchip.com> Cc: Samuel Holland <samuel.holland@sifive.com> Cc: Huacai Chen <chenhuacai@kernel.org> Cc: WANG Xuerui <kernel@xen0n.name> Cc: Bibo Mao <maobibo@loongson.cn> Cc: Tiezhu Yang <yangtiezhu@loongson.cn> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Tianrui Zhao <zhaotianrui@loongson.cn> Link: https://lore.kernel.org/20241030-tracepoint-v12-5-eec7f0f8ad22@google.com Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org> Co-developed-by: Miguel Ojeda <ojeda@kernel.org> Signed-off-by: Miguel Ojeda <ojeda@kernel.org> Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent aecaf18 commit 169484a

File tree

6 files changed

+104
-2
lines changed

6 files changed

+104
-2
lines changed

rust/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
3636
obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
3737
obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
3838

39+
always-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/arch_static_branch_asm.rs
40+
3941
# Avoids running `$(RUSTC)` for the sysroot when it may not be available.
4042
ifdef CONFIG_RUST
4143

@@ -424,4 +426,8 @@ $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
424426
$(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
425427
+$(call if_changed_rule,rustc_library)
426428

429+
ifdef CONFIG_JUMP_LABEL
430+
$(obj)/kernel.o: $(obj)/kernel/arch_static_branch_asm.rs
431+
endif
432+
427433
endif # CONFIG_RUST

rust/kernel/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
/arch_static_branch_asm.rs
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#include <linux/jump_label.h>
4+
5+
// Cut here.
6+
7+
::kernel::concat_literals!(ARCH_STATIC_BRANCH_ASM("{symb} + {off} + {branch}", "{l_yes}"))

rust/kernel/jump_label.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,51 @@ macro_rules! static_branch_unlikely {
2424
let _key: *const $crate::bindings::static_key_false = ::core::ptr::addr_of!((*_key).$field);
2525
let _key: *const $crate::bindings::static_key = _key.cast();
2626

27-
$crate::bindings::static_key_count(_key.cast_mut()) > 0
27+
#[cfg(not(CONFIG_JUMP_LABEL))]
28+
{
29+
$crate::bindings::static_key_count(_key) > 0
30+
}
31+
32+
#[cfg(CONFIG_JUMP_LABEL)]
33+
$crate::jump_label::arch_static_branch! { $key, $keytyp, $field, false }
2834
}};
2935
}
3036
pub use static_branch_unlikely;
37+
38+
/// Assert that the assembly block evaluates to a string literal.
39+
#[cfg(CONFIG_JUMP_LABEL)]
40+
const _: &str = include!(concat!(
41+
env!("OBJTREE"),
42+
"/rust/kernel/arch_static_branch_asm.rs"
43+
));
44+
45+
#[macro_export]
46+
#[doc(hidden)]
47+
#[cfg(CONFIG_JUMP_LABEL)]
48+
macro_rules! arch_static_branch {
49+
($key:path, $keytyp:ty, $field:ident, $branch:expr) => {'my_label: {
50+
$crate::asm!(
51+
include!(concat!(env!("OBJTREE"), "/rust/kernel/arch_static_branch_asm.rs"));
52+
l_yes = label {
53+
break 'my_label true;
54+
},
55+
symb = sym $key,
56+
off = const ::core::mem::offset_of!($keytyp, $field),
57+
branch = const $crate::jump_label::bool_to_int($branch),
58+
);
59+
60+
break 'my_label false;
61+
}};
62+
}
63+
64+
#[cfg(CONFIG_JUMP_LABEL)]
65+
pub use arch_static_branch;
66+
67+
/// A helper used by inline assembly to pass a boolean to as a `const` parameter.
68+
///
69+
/// Using this function instead of a cast lets you assert that the input is a boolean, and not some
70+
/// other type that can also be cast to an integer.
71+
#[doc(hidden)]
72+
pub const fn bool_to_int(b: bool) -> i32 {
73+
b as i32
74+
}

rust/kernel/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,38 @@ macro_rules! container_of {
148148
ptr.sub(offset) as *const $type
149149
}}
150150
}
151+
152+
/// Helper for `.rs.S` files.
153+
#[doc(hidden)]
154+
#[macro_export]
155+
macro_rules! concat_literals {
156+
($( $asm:literal )* ) => {
157+
::core::concat!($($asm),*)
158+
};
159+
}
160+
161+
/// Wrapper around `asm!` configured for use in the kernel.
162+
///
163+
/// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!`
164+
/// syntax.
165+
// For x86, `asm!` uses intel syntax by default, but we want to use at&t syntax in the kernel.
166+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
167+
#[macro_export]
168+
macro_rules! asm {
169+
($($asm:expr),* ; $($rest:tt)*) => {
170+
::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
171+
};
172+
}
173+
174+
/// Wrapper around `asm!` configured for use in the kernel.
175+
///
176+
/// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!`
177+
/// syntax.
178+
// For non-x86 arches we just pass through to `asm!`.
179+
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
180+
#[macro_export]
181+
macro_rules! asm {
182+
($($asm:expr),* ; $($rest:tt)*) => {
183+
::core::arch::asm!( $($asm)*, $($rest)* )
184+
};
185+
}

scripts/Makefile.build

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,13 @@ $(obj)/%.lst: $(obj)/%.c FORCE
248248
# Compile Rust sources (.rs)
249249
# ---------------------------------------------------------------------------
250250

251-
rust_allowed_features := new_uninit
251+
rust_allowed_features := asm_const,asm_goto,new_uninit
252252

253253
# `--out-dir` is required to avoid temporaries being created by `rustc` in the
254254
# current working directory, which may be not accessible in the out-of-tree
255255
# modules case.
256256
rust_common_cmd = \
257+
OBJTREE=$(abspath $(objtree)) \
257258
RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
258259
-Zallow-features=$(rust_allowed_features) \
259260
-Zcrate-attr=no_std \
@@ -303,6 +304,12 @@ quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
303304
$(obj)/%.ll: $(obj)/%.rs FORCE
304305
+$(call if_changed_dep,rustc_ll_rs)
305306

307+
quiet_cmd_rustc_rs_rs_S = RSCPP $(quiet_modtag) $@
308+
cmd_rustc_rs_rs_S = $(CPP) $(c_flags) -xc -C -P $< | sed '1,/^\/\/ Cut here.$$/d' >$@
309+
310+
$(obj)/%.rs: $(obj)/%.rs.S FORCE
311+
+$(call if_changed_dep,rustc_rs_rs_S)
312+
306313
# Compile assembler sources (.S)
307314
# ---------------------------------------------------------------------------
308315

0 commit comments

Comments
 (0)