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 <mhiramat@kernel.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Josh Poimboeuf <jpoimboe@kernel.org> Cc: Jason Baron <jbaron@akamai.com> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Alex Gaynor <alex.gaynor@gmail.com> Cc: Wedson Almeida Filho <wedsonaf@gmail.com> Cc: " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " <bjorn3_gh@protonmail.com> Cc: Benno Lossin <benno.lossin@proton.me> Cc: Andreas Hindborg <a.hindborg@kernel.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@redhat.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Sean Christopherson <seanjc@google.com> Cc: Uros Bizjak <ubizjak@gmail.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will@kernel.org> Cc: Marc Zyngier <maz@kernel.org> Cc: Oliver Upton <oliver.upton@linux.dev> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Fuad Tabba <tabba@google.com> Cc: Paul Walmsley <paul.walmsley@sifive.com> Cc: Palmer Dabbelt <palmer@dabbelt.com> Cc: Albert Ou <aou@eecs.berkeley.edu> Cc: Anup Patel <apatel@ventanamicro.com> Cc: Andrew Jones <ajones@ventanamicro.com> Cc: Alexandre Ghiti <alexghiti@rivosinc.com> Cc: Conor Dooley <conor.dooley@microchip.com> Cc: Samuel Holland <samuel.holland@sifive.com> Cc: Huacai Chen <chenhuacai@kernel.org> Cc: WANG Xuerui <kernel@xen0n.name> Cc: Bibo Mao <maobibo@loongson.cn> Cc: Tiezhu Yang <yangtiezhu@loongson.cn> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Tianrui Zhao <zhaotianrui@loongson.cn> Link: https://lore.kernel.org/20241030-tracepoint-v12-1-eec7f0f8ad22@google.com Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
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)