Skip to content

Commit 6e59bcc

Browse files
Darksonnrostedt
authored andcommitted
rust: add static_branch_unlikely for static_key_false
Add just enough support for static key so that we can use it from tracepoints. Tracepoints rely on `static_branch_unlikely` with a `struct static_key_false`, so we add the same functionality to Rust. This patch only provides a generic implementation without code patching (matching the one used when CONFIG_JUMP_LABEL is disabled). Later patches add support for inline asm implementations that use runtime patching. When CONFIG_JUMP_LABEL is unset, `static_key_count` is a static inline function, so a Rust helper is defined for `static_key_count` in this case. If Rust is compiled with LTO, this call should get inlined. The helper can be eliminated once we have the necessary inline asm to make atomic operations from Rust. Cc: Masami Hiramatsu <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Jason Baron <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: Miguel Ojeda <[email protected]> Cc: Alex Gaynor <[email protected]> Cc: Wedson Almeida Filho <[email protected]> Cc: " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " <[email protected]> Cc: Benno Lossin <[email protected]> Cc: Andreas Hindborg <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Dave Hansen <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Sean Christopherson <[email protected]> Cc: Uros Bizjak <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Will Deacon <[email protected]> Cc: Marc Zyngier <[email protected]> Cc: Oliver Upton <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Ryan Roberts <[email protected]> Cc: Fuad Tabba <[email protected]> Cc: Paul Walmsley <[email protected]> Cc: Palmer Dabbelt <[email protected]> Cc: Albert Ou <[email protected]> Cc: Anup Patel <[email protected]> Cc: Andrew Jones <[email protected]> Cc: Alexandre Ghiti <[email protected]> Cc: Conor Dooley <[email protected]> Cc: Samuel Holland <[email protected]> Cc: Huacai Chen <[email protected]> Cc: WANG Xuerui <[email protected]> Cc: Bibo Mao <[email protected]> Cc: Tiezhu Yang <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Tianrui Zhao <[email protected]> Link: https://lore.kernel.org/[email protected] Reviewed-by: Boqun Feng <[email protected]> Reviewed-by: Gary Guo <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent eb887c4 commit 6e59bcc

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/ethtool.h>
1515
#include <linux/firmware.h>
1616
#include <linux/jiffies.h>
17+
#include <linux/jump_label.h>
1718
#include <linux/mdio.h>
1819
#include <linux/phy.h>
1920
#include <linux/refcount.h>

rust/helpers/helpers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "build_assert.c"
1313
#include "build_bug.c"
1414
#include "err.c"
15+
#include "jump_label.c"
1516
#include "kunit.c"
1617
#include "mutex.c"
1718
#include "page.c"

rust/helpers/jump_label.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Copyright (C) 2024 Google LLC.
5+
*/
6+
7+
#include <linux/jump_label.h>
8+
9+
#ifndef CONFIG_JUMP_LABEL
10+
int rust_helper_static_key_count(struct static_key *key)
11+
{
12+
return static_key_count(key);
13+
}
14+
#endif

rust/kernel/jump_label.rs

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

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub mod error;
3636
pub mod firmware;
3737
pub mod init;
3838
pub mod ioctl;
39+
pub mod jump_label;
3940
#[cfg(CONFIG_KUNIT)]
4041
pub mod kunit;
4142
pub mod list;

0 commit comments

Comments
 (0)