xref: /linux-6.15/rust/kernel/jump_label.rs (revision 169484ab)
16e59bcc9SAlice Ryhl // SPDX-License-Identifier: GPL-2.0
26e59bcc9SAlice Ryhl 
36e59bcc9SAlice Ryhl // Copyright (C) 2024 Google LLC.
46e59bcc9SAlice Ryhl 
56e59bcc9SAlice Ryhl //! Logic for static keys.
66e59bcc9SAlice Ryhl //!
76e59bcc9SAlice Ryhl //! C header: [`include/linux/jump_label.h`](srctree/include/linux/jump_label.h).
86e59bcc9SAlice Ryhl 
96e59bcc9SAlice Ryhl /// Branch based on a static key.
106e59bcc9SAlice Ryhl ///
116e59bcc9SAlice Ryhl /// Takes three arguments:
126e59bcc9SAlice Ryhl ///
136e59bcc9SAlice Ryhl /// * `key` - the path to the static variable containing the `static_key`.
146e59bcc9SAlice Ryhl /// * `keytyp` - the type of `key`.
156e59bcc9SAlice Ryhl /// * `field` - the name of the field of `key` that contains the `static_key`.
166e59bcc9SAlice Ryhl ///
176e59bcc9SAlice Ryhl /// # Safety
186e59bcc9SAlice Ryhl ///
196e59bcc9SAlice Ryhl /// The macro must be used with a real static key defined by C.
206e59bcc9SAlice Ryhl #[macro_export]
216e59bcc9SAlice Ryhl macro_rules! static_branch_unlikely {
226e59bcc9SAlice Ryhl     ($key:path, $keytyp:ty, $field:ident) => {{
236e59bcc9SAlice Ryhl         let _key: *const $keytyp = ::core::ptr::addr_of!($key);
246e59bcc9SAlice Ryhl         let _key: *const $crate::bindings::static_key_false = ::core::ptr::addr_of!((*_key).$field);
256e59bcc9SAlice Ryhl         let _key: *const $crate::bindings::static_key = _key.cast();
266e59bcc9SAlice Ryhl 
27*169484abSAlice Ryhl         #[cfg(not(CONFIG_JUMP_LABEL))]
28*169484abSAlice Ryhl         {
29*169484abSAlice Ryhl             $crate::bindings::static_key_count(_key) > 0
30*169484abSAlice Ryhl         }
31*169484abSAlice Ryhl 
32*169484abSAlice Ryhl         #[cfg(CONFIG_JUMP_LABEL)]
33*169484abSAlice Ryhl         $crate::jump_label::arch_static_branch! { $key, $keytyp, $field, false }
346e59bcc9SAlice Ryhl     }};
356e59bcc9SAlice Ryhl }
366e59bcc9SAlice Ryhl pub use static_branch_unlikely;
37*169484abSAlice Ryhl 
38*169484abSAlice Ryhl /// Assert that the assembly block evaluates to a string literal.
39*169484abSAlice Ryhl #[cfg(CONFIG_JUMP_LABEL)]
40*169484abSAlice Ryhl const _: &str = include!(concat!(
41*169484abSAlice Ryhl     env!("OBJTREE"),
42*169484abSAlice Ryhl     "/rust/kernel/arch_static_branch_asm.rs"
43*169484abSAlice Ryhl ));
44*169484abSAlice Ryhl 
45*169484abSAlice Ryhl #[macro_export]
46*169484abSAlice Ryhl #[doc(hidden)]
47*169484abSAlice Ryhl #[cfg(CONFIG_JUMP_LABEL)]
48*169484abSAlice Ryhl macro_rules! arch_static_branch {
49*169484abSAlice Ryhl     ($key:path, $keytyp:ty, $field:ident, $branch:expr) => {'my_label: {
50*169484abSAlice Ryhl         $crate::asm!(
51*169484abSAlice Ryhl             include!(concat!(env!("OBJTREE"), "/rust/kernel/arch_static_branch_asm.rs"));
52*169484abSAlice Ryhl             l_yes = label {
53*169484abSAlice Ryhl                 break 'my_label true;
54*169484abSAlice Ryhl             },
55*169484abSAlice Ryhl             symb = sym $key,
56*169484abSAlice Ryhl             off = const ::core::mem::offset_of!($keytyp, $field),
57*169484abSAlice Ryhl             branch = const $crate::jump_label::bool_to_int($branch),
58*169484abSAlice Ryhl         );
59*169484abSAlice Ryhl 
60*169484abSAlice Ryhl         break 'my_label false;
61*169484abSAlice Ryhl     }};
62*169484abSAlice Ryhl }
63*169484abSAlice Ryhl 
64*169484abSAlice Ryhl #[cfg(CONFIG_JUMP_LABEL)]
65*169484abSAlice Ryhl pub use arch_static_branch;
66*169484abSAlice Ryhl 
67*169484abSAlice Ryhl /// A helper used by inline assembly to pass a boolean to as a `const` parameter.
68*169484abSAlice Ryhl ///
69*169484abSAlice Ryhl /// Using this function instead of a cast lets you assert that the input is a boolean, and not some
70*169484abSAlice Ryhl /// other type that can also be cast to an integer.
71*169484abSAlice Ryhl #[doc(hidden)]
72*169484abSAlice Ryhl pub const fn bool_to_int(b: bool) -> i32 {
73*169484abSAlice Ryhl     b as i32
74*169484abSAlice Ryhl }
75