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 1/3] rust: add static_call support
Date: Thu, 06 Jun 2024 15:05:24 +0000 [thread overview]
Message-ID: <20240606-tracepoint-v1-1-6551627bf51b@google.com> (raw)
In-Reply-To: <20240606-tracepoint-v1-0-6551627bf51b@google.com>
Add static_call support by mirroring how C does. When the platform does
not support static calls (right now, that means that it is not x86),
then the function pointer is loaded from a global and called. Otherwise,
we generate a call to a trampoline function, and objtool is used to make
these calls patchable at runtime.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
rust/kernel/lib.rs | 1 +
rust/kernel/static_call.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 93 insertions(+)
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index fbd91a48ff8b..d534b1178955 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -38,6 +38,7 @@
pub mod prelude;
pub mod print;
mod static_assert;
+pub mod static_call;
#[doc(hidden)]
pub mod std_vendor;
pub mod str;
diff --git a/rust/kernel/static_call.rs b/rust/kernel/static_call.rs
new file mode 100644
index 000000000000..f7b8ba7bf1fb
--- /dev/null
+++ b/rust/kernel/static_call.rs
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Copyright (C) 2024 Google LLC.
+
+//! Logic for static calls.
+
+#[macro_export]
+#[doc(hidden)]
+macro_rules! ty_underscore_for {
+ ($arg:expr) => {
+ _
+ };
+}
+
+#[doc(hidden)]
+#[repr(transparent)]
+pub struct AddressableStaticCallKey {
+ _ptr: *const bindings::static_call_key,
+}
+unsafe impl Sync for AddressableStaticCallKey {}
+impl AddressableStaticCallKey {
+ pub const fn new(ptr: *const bindings::static_call_key) -> Self {
+ Self { _ptr: ptr }
+ }
+}
+
+#[cfg(CONFIG_HAVE_STATIC_CALL)]
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _static_call {
+ ($name:ident($($args:expr),* $(,)?)) => {{
+ // Symbol mangling will give this symbol a unique name.
+ #[cfg(CONFIG_HAVE_STATIC_CALL_INLINE)]
+ #[link_section = ".discard.addressable"]
+ #[used]
+ static __ADDRESSABLE: $crate::static_call::AddressableStaticCallKey = unsafe {
+ $crate::static_call::AddressableStaticCallKey::new(::core::ptr::addr_of!(
+ $crate::macros::paste! { $crate::bindings:: [<__SCK__ $name >]; }
+ ))
+ };
+
+ let fn_ptr: unsafe extern "C" fn($($crate::static_call::ty_underscore_for!($args)),*) -> _ =
+ $crate::macros::paste! { $crate::bindings:: [<__SCT__ $name >]; };
+ (fn_ptr)($($args),*)
+ }};
+}
+
+#[cfg(not(CONFIG_HAVE_STATIC_CALL))]
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _static_call {
+ ($name:ident($($args:expr),* $(,)?)) => {{
+ let void_ptr_fn: *mut ::core::ffi::c_void =
+ $crate::macros::paste! { $crate::bindings:: [<__SCK__ $name >]; }.func;
+
+ let fn_ptr: unsafe extern "C" fn($($crate::static_call::ty_underscore_for!($args)),*) -> _ =
+ if true {
+ ::core::mem::transmute(void_ptr_fn)
+ } else {
+ // This is dead code, but it influences type inference on `fn_ptr` so that we
+ // transmute the function pointer to the right type.
+ $crate::macros::paste! { $crate::bindings:: [<__SCT__ $name >]; }
+ };
+
+ (fn_ptr)($($args),*)
+ }};
+}
+
+/// Statically call a global function.
+///
+/// # Safety
+///
+/// This macro will call the provided function. It is up to the caller to uphold the safety
+/// guarantees of the function.
+///
+/// # Examples
+///
+/// ```ignore
+/// fn call_static() {
+/// unsafe {
+/// static_call! { your_static_call() };
+/// }
+/// }
+/// ```
+#[macro_export]
+macro_rules! static_call {
+ // Forward to the real implementation. Separated like this so that we don't have to duplicate
+ // the documentation.
+ ($($args:tt)*) => { $crate::static_call::_static_call! { $($args)* } };
+}
+
+pub use {_static_call, static_call, ty_underscore_for};
--
2.45.2.505.gda0bf45e8d-goog
next prev parent reply other threads:[~2024-06-06 15:05 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 ` Alice Ryhl [this message]
2024-06-06 17:18 ` [PATCH 1/3] rust: add static_call support 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 ` [PATCH 3/3] rust: add tracepoint support Alice Ryhl
2024-06-06 15:30 ` 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-1-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).