From: Alice Ryhl <aliceryhl@google.com>
To: "Steven Rostedt" <rostedt@goodmis.org>,
"Masami Hiramatsu" <mhiramat@kernel.org>,
"Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Josh Poimboeuf" <jpoimboe@kernel.org>,
"Jason Baron" <jbaron@akamai.com>,
"Ard Biesheuvel" <ardb@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@samsung.com>
Cc: linux-trace-kernel@vger.kernel.org,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
Alice Ryhl <aliceryhl@google.com>
Subject: [PATCH 3/3] rust: add tracepoint support
Date: Thu, 06 Jun 2024 15:05:26 +0000 [thread overview]
Message-ID: <20240606-tracepoint-v1-3-6551627bf51b@google.com> (raw)
In-Reply-To: <20240606-tracepoint-v1-0-6551627bf51b@google.com>
Make it possible to have Rust code call into tracepoints defined by C
code. It is still required that the tracepoint is declared in a C
header, and that this header is included in the input to bindgen.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
rust/bindings/bindings_helper.h | 1 +
rust/bindings/lib.rs | 15 +++++++
rust/helpers.c | 24 +++++++++++
rust/kernel/lib.rs | 1 +
rust/kernel/tracepoint.rs | 92 +++++++++++++++++++++++++++++++++++++++++
5 files changed, 133 insertions(+)
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index ddb5644d4fd9..d442f9ccfc2c 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -15,6 +15,7 @@
#include <linux/refcount.h>
#include <linux/sched.h>
#include <linux/slab.h>
+#include <linux/tracepoint.h>
#include <linux/wait.h>
#include <linux/workqueue.h>
diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs
index 40ddaee50d8b..48856761d682 100644
--- a/rust/bindings/lib.rs
+++ b/rust/bindings/lib.rs
@@ -48,3 +48,18 @@ mod bindings_helper {
}
pub use bindings_raw::*;
+
+/// Rust version of the C macro `rcu_dereference_raw`.
+///
+/// The rust helper only works with void pointers, but this wrapper method makes it work with any
+/// pointer type using pointer casts.
+///
+/// # Safety
+///
+/// This method has the same safety requirements as the C macro of the same name.
+#[inline(always)]
+pub unsafe fn rcu_dereference_raw<T>(p: *const *mut T) -> *mut T {
+ // SAFETY: This helper calls into the C macro, so the caller promises to uphold the safety
+ // requirements.
+ unsafe { __rcu_dereference_raw(p as *mut *mut _) as *mut T }
+}
diff --git a/rust/helpers.c b/rust/helpers.c
index 2c37a0f5d7a8..0560cc2a512a 100644
--- a/rust/helpers.c
+++ b/rust/helpers.c
@@ -165,6 +165,30 @@ rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
}
EXPORT_SYMBOL_GPL(rust_helper_krealloc);
+void rust_helper_preempt_enable_notrace(void)
+{
+ preempt_enable_notrace();
+}
+EXPORT_SYMBOL_GPL(rust_helper_preempt_enable_notrace);
+
+void rust_helper_preempt_disable_notrace(void)
+{
+ preempt_disable_notrace();
+}
+EXPORT_SYMBOL_GPL(rust_helper_preempt_disable_notrace);
+
+bool rust_helper_current_cpu_online(void)
+{
+ return cpu_online(raw_smp_processor_id());
+}
+EXPORT_SYMBOL_GPL(rust_helper_current_cpu_online);
+
+void *rust_helper___rcu_dereference_raw(void **p)
+{
+ return rcu_dereference_raw(p);
+}
+EXPORT_SYMBOL_GPL(rust_helper___rcu_dereference_raw);
+
/*
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
* use it in contexts where Rust expects a `usize` like slice (array) indices.
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 22e1fedd0774..3f3b280bb437 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -46,6 +46,7 @@
pub mod sync;
pub mod task;
pub mod time;
+pub mod tracepoint;
pub mod types;
pub mod workqueue;
diff --git a/rust/kernel/tracepoint.rs b/rust/kernel/tracepoint.rs
new file mode 100644
index 000000000000..d628ae71fc58
--- /dev/null
+++ b/rust/kernel/tracepoint.rs
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Copyright (C) 2024 Google LLC.
+
+//! Logic for tracepoints.
+
+/// Declare the Rust entry point for a tracepoint.
+#[macro_export]
+macro_rules! declare_trace {
+ ($($(#[$attr:meta])* $pub:vis fn $name:ident($($argname:ident : $argtyp:ty),* $(,)?);)*) => {$(
+ $( #[$attr] )*
+ #[inline(always)]
+ $pub unsafe fn $name($($argname : $argtyp),*) {
+ #[cfg(CONFIG_TRACEPOINTS)]
+ {
+ use $crate::bindings::*;
+
+ // SAFETY: This macro only compiles if $name is a real tracepoint, and if it is a
+ // real tracepoint, then it is okay to query the static key.
+ let should_trace = unsafe {
+ $crate::macros::paste! {
+ $crate::static_key::static_key_false!(
+ [< __tracepoint_ $name >],
+ $crate::bindings::tracepoint,
+ key
+ )
+ }
+ };
+
+ if should_trace {
+ $crate::tracepoint::do_trace!($name($($argname : $argtyp),*), cond);
+ }
+ }
+
+ #[cfg(not(CONFIG_TRACEPOINTS))]
+ {
+ // If tracepoints are disabled, insert a trivial use of each argument
+ // to avoid unused argument warnings.
+ $( let _unused = $argname; )*
+ }
+ }
+ )*}
+}
+
+#[doc(hidden)]
+#[macro_export]
+macro_rules! do_trace {
+ ($name:ident($($argname:ident : $argtyp:ty),* $(,)?), $cond:expr) => {{
+ if !$crate::bindings::current_cpu_online() {
+ return;
+ }
+
+ // SAFETY: This call is balanced with the call below.
+ unsafe { $crate::bindings::preempt_disable_notrace() };
+
+ // SAFETY: This calls the tracepoint with the provided arguments. The caller of the Rust
+ // wrapper guarantees that this is okay.
+ #[cfg(CONFIG_HAVE_STATIC_CALL)]
+ unsafe {
+ let it_func_ptr: *mut $crate::bindings::tracepoint_func =
+ $crate::bindings::rcu_dereference_raw(
+ ::core::ptr::addr_of!(
+ $crate::macros::concat_idents!(__tracepoint_, $name).funcs
+ )
+ );
+
+ if !it_func_ptr.is_null() {
+ let __data = (*it_func_ptr).data;
+ $crate::macros::paste! {
+ $crate::static_call::static_call! {
+ [< tp_func_ $name >] (__data, $($argname),*)
+ };
+ }
+ }
+ }
+
+ // SAFETY: This calls the tracepoint with the provided arguments. The caller of the Rust
+ // wrapper guarantees that this is okay.
+ #[cfg(not(CONFIG_HAVE_STATIC_CALL))]
+ unsafe {
+ $crate::macros::concat_idents!(__traceiter_, $name)(
+ ::core::ptr::null_mut(),
+ $($argname),*
+ );
+ }
+
+ // SAFETY: This call is balanced with the call above.
+ unsafe { $crate::bindings::preempt_enable_notrace() };
+ }}
+}
+
+pub use {declare_trace, do_trace};
--
2.45.2.505.gda0bf45e8d-goog
next prev parent reply other threads:[~2024-06-06 15:06 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-06 15:05 [PATCH 0/3] Tracepoints and static branch/call in Rust Alice Ryhl
2024-06-06 15:05 ` [PATCH 1/3] rust: add static_call support Alice Ryhl
2024-06-06 17:18 ` Peter Zijlstra
2024-06-06 19:09 ` Miguel Ojeda
2024-06-06 19:33 ` Peter Zijlstra
2024-06-07 9:43 ` Alice Ryhl
2024-06-07 10:52 ` Peter Zijlstra
2024-06-07 11:08 ` Alice Ryhl
2024-06-07 11:46 ` Miguel Ojeda
2024-06-06 15:05 ` [PATCH 2/3] rust: add static_key_false Alice Ryhl
2024-06-06 15:38 ` Mathieu Desnoyers
2024-06-06 16:19 ` Alice Ryhl
2024-06-06 17:23 ` Peter Zijlstra
2024-06-06 15:05 ` Alice Ryhl [this message]
2024-06-06 15:30 ` [PATCH 3/3] rust: add tracepoint support Mathieu Desnoyers
2024-06-06 15:49 ` Boqun Feng
2024-06-06 16:18 ` Mathieu Desnoyers
2024-06-06 17:35 ` Peter Zijlstra
2024-06-06 19:00 ` Boqun Feng
2024-06-06 19:29 ` Peter Zijlstra
2024-06-06 23:50 ` Boqun Feng
2024-06-06 16:16 ` Alice Ryhl
2024-06-06 16:21 ` Mathieu Desnoyers
2024-06-06 17:26 ` Peter Zijlstra
2024-06-06 15:25 ` [PATCH 0/3] Tracepoints and static branch/call in Rust Mathieu Desnoyers
2024-06-06 15:46 ` Alice Ryhl
2024-06-06 16:17 ` Mathieu Desnoyers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240606-tracepoint-v1-3-6551627bf51b@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=ardb@kernel.org \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=jbaron@akamai.com \
--cc=jpoimboe@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.