Files
linux/rust/kernel/jump_label.rs
FUJITA Tomonori dea1bf3814 rust: jump_label: skip arch-specific asm in testlib builds
Running `make rusttest` with `ARCH=` set to an architecture other than
the host's may fail in the future, e.g. `ARCH=arm64` on an x86_64 host:

error: alignment must be a power of 2
  --> rust/kernel/jump_label.rs:51:13
   |
51 |     include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_static_branch_asm.rs"));
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: instantiated into assembly here
  --> <inline asm>:3:10
   |
 3 |     .align        3
   |                   ^

The reason is that `rusttest` builds the kernel crate as a host
library: it passes the `CONFIG_*` cfgs of the configured architecture,
but not `--target`, so code generation happens for the
host. `arch_static_branch!` then selects the arch-specific inline asm
arm based on CONFIG_*, and the host assembler rejects it.

This does not happen with the current master because
`arch_static_branch!` has no user inside the kernel crate itself yet,
but fix it now to avoid surprises later.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Link: https://patch.msgid.link/20260809134858.1219036-1-tomo@flapping.org
[ Reworded slightly to clarify it "may fail in the future". - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2026-08-23 21:27:16 +02:00

87 lines
2.5 KiB
Rust

// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2024 Google LLC.
//! Logic for static keys.
//!
//! C header: [`include/linux/jump_label.h`](srctree/include/linux/jump_label.h).
/// Branch based on a static key.
///
/// Takes three arguments:
///
/// * `key` - the path to the static variable containing the `static_key`.
/// * `keytyp` - the type of `key`.
/// * `field` - the name of the field of `key` that contains the `static_key`.
///
/// # Safety
///
/// The macro must be used with a real static key defined by C.
#[macro_export]
macro_rules! static_branch_unlikely {
($key:path, $keytyp:ty, $field:ident) => {{
let _key: *const $keytyp = ::core::ptr::addr_of!($key);
let _key: *const $crate::bindings::static_key_false = ::core::ptr::addr_of!((*_key).$field);
let _key: *const $crate::bindings::static_key = _key.cast();
#[cfg(not(CONFIG_JUMP_LABEL))]
{
$crate::bindings::static_key_count(_key.cast_mut()) > 0
}
#[cfg(CONFIG_JUMP_LABEL)]
$crate::jump_label::arch_static_branch! { $key, $keytyp, $field, false }
}};
}
pub use static_branch_unlikely;
/// Assert that the assembly block evaluates to a string literal.
#[cfg(CONFIG_JUMP_LABEL)]
const _: &str = include!(concat!(
env!("OBJTREE"),
"/rust/kernel/generated_arch_static_branch_asm.rs"
));
#[macro_export]
#[doc(hidden)]
#[cfg(not(testlib))]
#[cfg(CONFIG_JUMP_LABEL)]
macro_rules! arch_static_branch {
($key:path, $keytyp:ty, $field:ident, $branch:expr) => {'my_label: {
$crate::asm!(
include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch_static_branch_asm.rs"));
l_yes = label {
break 'my_label true;
},
symb = sym $key,
off = const ::core::mem::offset_of!($keytyp, $field),
branch = const $crate::jump_label::bool_to_int($branch),
);
break 'my_label false;
}};
}
#[macro_export]
#[doc(hidden)]
#[cfg(testlib)]
#[cfg(CONFIG_JUMP_LABEL)]
macro_rules! arch_static_branch {
($key:path, $keytyp:ty, $field:ident, $branch:expr) => {
// The asm falls through until patched, which never happens on the host.
false
};
}
#[cfg(CONFIG_JUMP_LABEL)]
pub use arch_static_branch;
/// A helper used by inline assembly to pass a boolean to as a `const` parameter.
///
/// Using this function instead of a cast lets you assert that the input is a boolean, and not some
/// other type that can also be cast to an integer.
#[doc(hidden)]
pub const fn bool_to_int(b: bool) -> i32 {
b as i32
}