xref: /linux-6.15/rust/kernel/jump_label.rs (revision 6e59bcc9)
1*6e59bcc9SAlice Ryhl // SPDX-License-Identifier: GPL-2.0
2*6e59bcc9SAlice Ryhl 
3*6e59bcc9SAlice Ryhl // Copyright (C) 2024 Google LLC.
4*6e59bcc9SAlice Ryhl 
5*6e59bcc9SAlice Ryhl //! Logic for static keys.
6*6e59bcc9SAlice Ryhl //!
7*6e59bcc9SAlice Ryhl //! C header: [`include/linux/jump_label.h`](srctree/include/linux/jump_label.h).
8*6e59bcc9SAlice Ryhl 
9*6e59bcc9SAlice Ryhl /// Branch based on a static key.
10*6e59bcc9SAlice Ryhl ///
11*6e59bcc9SAlice Ryhl /// Takes three arguments:
12*6e59bcc9SAlice Ryhl ///
13*6e59bcc9SAlice Ryhl /// * `key` - the path to the static variable containing the `static_key`.
14*6e59bcc9SAlice Ryhl /// * `keytyp` - the type of `key`.
15*6e59bcc9SAlice Ryhl /// * `field` - the name of the field of `key` that contains the `static_key`.
16*6e59bcc9SAlice Ryhl ///
17*6e59bcc9SAlice Ryhl /// # Safety
18*6e59bcc9SAlice Ryhl ///
19*6e59bcc9SAlice Ryhl /// The macro must be used with a real static key defined by C.
20*6e59bcc9SAlice Ryhl #[macro_export]
21*6e59bcc9SAlice Ryhl macro_rules! static_branch_unlikely {
22*6e59bcc9SAlice Ryhl     ($key:path, $keytyp:ty, $field:ident) => {{
23*6e59bcc9SAlice Ryhl         let _key: *const $keytyp = ::core::ptr::addr_of!($key);
24*6e59bcc9SAlice Ryhl         let _key: *const $crate::bindings::static_key_false = ::core::ptr::addr_of!((*_key).$field);
25*6e59bcc9SAlice Ryhl         let _key: *const $crate::bindings::static_key = _key.cast();
26*6e59bcc9SAlice Ryhl 
27*6e59bcc9SAlice Ryhl         $crate::bindings::static_key_count(_key.cast_mut()) > 0
28*6e59bcc9SAlice Ryhl     }};
29*6e59bcc9SAlice Ryhl }
30*6e59bcc9SAlice Ryhl pub use static_branch_unlikely;
31