[RFC PATCH 1/3] rust: static jump: add support for nested arguments

Andrew Ballance posted 3 patches 4 months ago
[RFC PATCH 1/3] rust: static jump: add support for nested arguments
Posted by Andrew Ballance 4 months ago
allows for nested arguments to be used with the static_branch macro.
e.g. `outer.inner.key` can now be accessed by the macro

Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
---
 rust/kernel/jump_label.rs | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/rust/kernel/jump_label.rs b/rust/kernel/jump_label.rs
index 4e974c768dbd..4ea3cbb340ff 100644
--- a/rust/kernel/jump_label.rs
+++ b/rust/kernel/jump_label.rs
@@ -19,9 +19,9 @@
 /// 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) => {{
+    ($key:path, $keytyp:ty, $field:ident $(.$field_cont: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_false = ::core::ptr::addr_of!((*_key).$field$(.$field_cont)*);
         let _key: *const $crate::bindings::static_key = _key.cast();
 
         #[cfg(not(CONFIG_JUMP_LABEL))]
@@ -30,7 +30,7 @@ macro_rules! static_branch_unlikely {
         }
 
         #[cfg(CONFIG_JUMP_LABEL)]
-        $crate::jump_label::arch_static_branch! { $key, $keytyp, $field, false }
+        $crate::jump_label::arch_static_branch! { $key, $keytyp, $field$(.$field_cont)*, false }
     }};
 }
 pub use static_branch_unlikely;
@@ -46,14 +46,14 @@ macro_rules! static_branch_unlikely {
 #[doc(hidden)]
 #[cfg(CONFIG_JUMP_LABEL)]
 macro_rules! arch_static_branch {
-    ($key:path, $keytyp:ty, $field:ident, $branch:expr) => {'my_label: {
+    ($key:path, $keytyp:ty, $field:ident $(.$field_cont: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),
+            off = const ::core::mem::offset_of!($keytyp, $field$(.$field_cont)*),
             branch = const $crate::jump_label::bool_to_int($branch),
         );
 
-- 
2.49.0
Re: [RFC PATCH 1/3] rust: static jump: add support for nested arguments
Posted by Alice Ryhl 4 months ago
On Wed, Jun 11, 2025 at 10:30 PM Andrew Ballance
<andrewjballance@gmail.com> wrote:
>
> allows for nested arguments to be used with the static_branch macro.
> e.g. `outer.inner.key` can now be accessed by the macro
>
> Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
> ---
>  rust/kernel/jump_label.rs | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/rust/kernel/jump_label.rs b/rust/kernel/jump_label.rs
> index 4e974c768dbd..4ea3cbb340ff 100644
> --- a/rust/kernel/jump_label.rs
> +++ b/rust/kernel/jump_label.rs
> @@ -19,9 +19,9 @@
>  /// 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) => {{
> +    ($key:path, $keytyp:ty, $field:ident $(.$field_cont:ident)*) => {{

I think this can be:
($key:path, $keytyp:ty, $($field:ident).+) => {{

this means "one or more identifiers separated by dots.

>          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_false = ::core::ptr::addr_of!((*_key).$field$(.$field_cont)*);
>          let _key: *const $crate::bindings::static_key = _key.cast();
>
>          #[cfg(not(CONFIG_JUMP_LABEL))]
> @@ -30,7 +30,7 @@ macro_rules! static_branch_unlikely {
>          }
>
>          #[cfg(CONFIG_JUMP_LABEL)]
> -        $crate::jump_label::arch_static_branch! { $key, $keytyp, $field, false }
> +        $crate::jump_label::arch_static_branch! { $key, $keytyp, $field$(.$field_cont)*, false }
>      }};
>  }
>  pub use static_branch_unlikely;
> @@ -46,14 +46,14 @@ macro_rules! static_branch_unlikely {
>  #[doc(hidden)]
>  #[cfg(CONFIG_JUMP_LABEL)]
>  macro_rules! arch_static_branch {
> -    ($key:path, $keytyp:ty, $field:ident, $branch:expr) => {'my_label: {
> +    ($key:path, $keytyp:ty, $field:ident $(.$field_cont: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),
> +            off = const ::core::mem::offset_of!($keytyp, $field$(.$field_cont)*),
>              branch = const $crate::jump_label::bool_to_int($branch),
>          );
>
> --
> 2.49.0
>