Re: [PATCH v3 1/2] rust: add static_key_false

Boqun Feng
2024-06-25T16:18:41Z

Hi Alice,

On Fri, Jun 21, 2024 at 10:35:26AM +0000, Alice Ryhl wrote:
> Add just enough support for static key so that we can use it from
> tracepoints. Tracepoints rely on `static_key_false` even though it is
> deprecated, so we add the same functionality to Rust.
>
> It is not possible to use the existing C implementation of
> arch_static_branch because it passes the argument `key` to inline
> assembly as an 'i' parameter, so 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.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

[Add linux-arch, and related arch maintainers Cced]

Since inline asms are touched here, please consider copying linux-arch
and arch maintainers next time ;-)

> ---
> rust/kernel/lib.rs | 1 +
> rust/kernel/static_key.rs | 143 ++++++++++++++++++++++++++++++++++++++++++++++
> scripts/Makefile.build | 2 +-
> 3 files changed, 145 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index fbd91a48ff8b..a0be9544996d 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -38,6 +38,7 @@
> pub mod prelude;
> pub mod print;
> mod static_assert;
> +pub mod static_key;
> #[doc(hidden)]
> pub mod std_vendor;
> pub mod str;
> diff --git a/rust/kernel/static_key.rs b/rust/kernel/static_key.rs
> new file mode 100644
> index 000000000000..9c844fe3e3a3
> --- /dev/null
> +++ b/rust/kernel/static_key.rs
> @@ -0,0 +1,143 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +// Copyright (C) 2024 Google LLC.
> +
> +//! Logic for static keys.
> +
> +use crate::bindings::*;
> +
> +#[doc(hidden)]
> +#[macro_export]
> +#[cfg(target_arch = "x86_64")]
> +macro_rules! _static_key_false {
> + ($key:path, $keytyp:ty, $field:ident) => {'my_label: {
> + core::arch::asm!(
> + r#"
> + 1: .byte 0x0f,0x1f,0x44,0x00,0x00
> +
> + .pushsection __jump_table, "aw"
> + .balign 8
> + .long 1b - .
> + .long {0} - .
> + .quad {1} + {2} - .
> + .popsection
> + "#,
> + label {
> + break 'my_label true;
> + },
> + sym $key,
> + const ::core::mem::offset_of!($keytyp, $field),
> + );
> +
> + break 'my_label false;
> + }};
> +}
> +
> +#[doc(hidden)]
> +#[macro_export]
> +#[cfg(target_arch = "aarch64")]
> +macro_rules! _static_key_false {
> + ($key:path, $keytyp:ty, $field:ident) => {'my_label: {
> + core::arch::asm!(
> + r#"
> + 1: nop
> +
> + .pushsection __jump_table, "aw"
> + .align 3
> + .long 1b - ., {0} - .
> + .quad {1} + {2} - .
> + .popsection
> + "#,
> + label {
> + break 'my_label true;
> + },
> + sym $key,
> + const ::core::mem::offset_of!($keytyp, $field),
> + );
> +
> + break 'my_label false;
> + }};
> +}
> +

For x86_64 and arm64 bits:

Acked-by: Boqun Feng <boqun.feng@gmail.com>

One thing though, we should split the arch-specific impls into different
files, for example: rust/kernel/arch/arm64.rs or rust/arch/arm64.rs.
That'll be easier for arch maintainers to watch the Rust changes related
to a particular architecture.

Another thought is that, could you implement an arch_static_branch!()
(instead of _static_key_false!()) and use it for static_key_false!()
similar to what we have in C? The benefit is that at least for myself
it'll be easier to compare the implementation between C and Rust.

Regards,
Boqun

> +#[doc(hidden)]
> +#[macro_export]
> +#[cfg(target_arch = "loongarch64")]
> +macro_rules! _static_key_false {
> + ($key:path, $keytyp:ty, $field:ident) => {'my_label: {
> + core::arch::asm!(
> + r#"
> + 1: nop
> +
> + .pushsection __jump_table, "aw"
> + .align 3
> + .long 1b - ., {0} - .
> + .quad {1} + {2} - .
> + .popsection
> + "#,
> + label {
> + break 'my_label true;
> + },
> + sym $key,
> + const ::core::mem::offset_of!($keytyp, $field),
> + );
> +
> + break 'my_label false;
> + }};
> +}
> +
> +#[doc(hidden)]
> +#[macro_export]
> +#[cfg(target_arch = "riscv64")]
> +macro_rules! _static_key_false {
> + ($key:path, $keytyp:ty, $field:ident) => {'my_label: {
> + core::arch::asm!(
> + r#"
> + .align 2
> + .option push
> + .option norelax
> + .option norvc
> + 1: nop
> + .option pop
> + .pushsection __jump_table, "aw"
> + .align 3
> + .long 1b - ., {0} - .
> + .dword {1} + {2} - .
> + .popsection
> + "#,
> + label {
> + break 'my_label true;
> + },
> + sym $key,
> + const ::core::mem::offset_of!($keytyp, $field),
> + );
> +
> + break 'my_label false;
> + }};
> +}
> +
> +/// 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`.
> +#[macro_export]
> +macro_rules! static_key_false {
> + // Forward to the real implementation. Separated like this so that we don't have to duplicate
> + // the documentation.
> + ($key:path, $keytyp:ty, $field:ident) => {{
> + // Assert that `$key` has type `$keytyp` and that `$key.$field` has type `static_key`.
> + //
> + // SAFETY: We know that `$key` is a static because otherwise the inline assembly will not
> + // compile. The raw pointers created in this block are in-bounds of `$key`.
> + static _TY_ASSERT: () = unsafe {
> + let key: *const $keytyp = ::core::ptr::addr_of!($key);
> + let _: *const $crate::bindings::static_key = ::core::ptr::addr_of!((*key).$field);
> + };
> +
> + $crate::_static_key_false! { $key, $keytyp, $field }
> + }};
> +}
> +
> +pub use static_key_false;
> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index efacca63c897..60197c1c063f 100644
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -263,7 +263,7 @@ $(obj)/%.lst: $(obj)/%.c FORCE
> # Compile Rust sources (.rs)
> # ---------------------------------------------------------------------------
>
> -rust_allowed_features := new_uninit
> +rust_allowed_features := asm_const,asm_goto,new_uninit
>
> # `--out-dir` is required to avoid temporaries being created by `rustc` in the
> # current working directory, which may be not accessible in the out-of-tree
>
> --
> 2.45.2.741.gdbec12cfda-goog
>