From: lyude@redhat.com
To: "Gary Guo" <gary@garyguo.net>, "Boqun Feng" <boqun@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
"Waiman Long" <longman@redhat.com>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH RFC 1/2] rust: sync: introduce a way to create lock class from caller
Date: Tue, 07 Jul 2026 17:25:35 -0400 [thread overview]
Message-ID: <1df84ed4095c28f1b636ef914ad43b80e00a38ab.camel@redhat.com> (raw)
In-Reply-To: <20260703-rust_lockdep-v1-1-1c21c62d0341@garyguo.net>
Reviewed-by: Lyude Paul <lyude@redhat.com>
On Fri, 2026-07-03 at 14:47 +0100, Gary Guo wrote:
> Rust provides a `#[track_caller]` mechanism that can be used to
> identify
> callers from callees. This also works across multiple level of calls.
> As lockdep only needs the address for static key classes, the address
> of
> these `&'static Location` can be used as unique lock class keys.
> This allows a more implicit way of creating lock classes without
> having to
> use macros.
>
> Create `LockClassKey::from_caller`, and use it for mutex and
> spinlocks.
>
> Signed-off-by: Gary Guo <gary@garyguo.net>
> ---
> rust/kernel/sync.rs | 37
> +++++++++++++++++++++++++++++++++----
> rust/kernel/sync/lock.rs | 13 +++++++++++--
> rust/kernel/sync/lock/mutex.rs | 3 +--
> rust/kernel/sync/lock/spinlock.rs | 3 +--
> 4 files changed, 46 insertions(+), 10 deletions(-)
>
> diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs
> index 993dbf2caa0e..cf76fb37c460 100644
> --- a/rust/kernel/sync.rs
> +++ b/rust/kernel/sync.rs
> @@ -5,8 +5,12 @@
> //! This module contains the kernel APIs related to synchronisation
> that have been ported or
> //! wrapped for usage by Rust code in the kernel.
>
> -use crate::prelude::*;
> -use crate::types::Opaque;
> +use core::panic::Location;
> +
> +use crate::{
> + prelude::*,
> + types::Opaque, //
> +};
> use pin_init;
>
> mod arc;
> @@ -66,6 +70,31 @@ impl LockClassKey {
> }
> }
>
> + /// Obtain a statically allocated lock class key identified by
> the caller's location.
> + ///
> + /// Different caller locations will be guaranteed to have
> different lock class keys; however for
> + /// the same caller location, this may return different keys,
> e.g. if the caller is instantiated
> + /// in different object files.
> + #[inline]
> + #[track_caller]
> + pub const fn from_caller() -> Pin<&'static Self> {
> + static_assert!(size_of::<Location<'_>>() >=
> size_of::<LockClassKey>());
> +
> + #[cfg(CONFIG_LOCKDEP)]
> + {
> + let caller = Location::caller();
> + // SAFETY: For static objects, lockdep does not touch
> the underlying memory, and only
> + // the address matters. `Location::caller()` is in
> rodata so it meets the requirement.
> + // The size check above makes sure that it does not
> overlap with other lock class keys.
> + unsafe {
> Pin::new_unchecked(&*core::ptr::from_ref(caller).cast::<Self>()) }
> + }
> +
> + // A separate version if lockdep is disabled to avoid
> unnecessary `Location` being
> + // generated.
> + #[cfg(not(CONFIG_LOCKDEP))]
> + static_lock_class!()
> + }
> +
> /// Initializes a dynamically allocated lock class key.
> ///
> /// In the common case of using a statically allocated lock
> class key, the
> @@ -83,7 +112,7 @@ impl LockClassKey {
> /// let key_ptr = key.into_foreign();
> ///
> /// {
> - /// stack_pin_init!(let num: SpinLock<u32> = SpinLock::new(
> + /// stack_pin_init!(let num: SpinLock<u32> =
> SpinLock::new_with_lock_class(
> /// 0,
> /// c"my_spinlock",
> /// // SAFETY: `key_ptr` is returned by the above
> `into_foreign()`, whose
> @@ -129,7 +158,7 @@ fn drop(self: Pin<&mut Self>) {
> /// use kernel::sync::{static_lock_class, Arc, SpinLock};
> ///
> /// fn new_locked_int() -> Result<Arc<SpinLock<u32>>> {
> -/// Arc::pin_init(SpinLock::new(
> +/// Arc::pin_init(SpinLock::new_with_lock_class(
> /// 42,
> /// c"new_locked_int",
> /// static_lock_class!(),
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index 10b6b5e9b024..447fec291cdf 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -127,8 +127,17 @@ unsafe impl<T: ?Sized + Send, B: Backend> Send
> for Lock<T, B> {}
> unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
>
> impl<T, B: Backend> Lock<T, B> {
> - /// Constructs a new lock initialiser.
> - pub fn new(
> + /// Constructs a new lock initialiser with a custom name.
> + #[inline]
> + #[track_caller]
> + pub fn new_with_name(t: impl PinInit<T>, name: &'static CStr) ->
> impl PinInit<Self> {
> + let key = LockClassKey::from_caller();
> + Self::new_with_lock_class(t, name, key)
> + }
> +
> + /// Constructs a new lock initialiser with a custom name and
> lock class key.
> + #[inline]
> + pub fn new_with_lock_class(
> t: impl PinInit<T>,
> name: &'static CStr,
> key: Pin<&'static LockClassKey>,
> diff --git a/rust/kernel/sync/lock/mutex.rs
> b/rust/kernel/sync/lock/mutex.rs
> index cda0203efefb..3675ce244e08 100644
> --- a/rust/kernel/sync/lock/mutex.rs
> +++ b/rust/kernel/sync/lock/mutex.rs
> @@ -11,8 +11,7 @@
> #[macro_export]
> macro_rules! new_mutex {
> ($inner:expr $(, $name:literal)? $(,)?) => {
> - $crate::sync::Mutex::new(
> - $inner, $crate::optional_name!($($name)?),
> $crate::static_lock_class!())
> + $crate::sync::Mutex::new_with_name($inner,
> $crate::optional_name!($($name)?))
> };
> }
> pub use new_mutex;
> diff --git a/rust/kernel/sync/lock/spinlock.rs
> b/rust/kernel/sync/lock/spinlock.rs
> index ef76fa07ca3a..091167ceda66 100644
> --- a/rust/kernel/sync/lock/spinlock.rs
> +++ b/rust/kernel/sync/lock/spinlock.rs
> @@ -11,8 +11,7 @@
> #[macro_export]
> macro_rules! new_spinlock {
> ($inner:expr $(, $name:literal)? $(,)?) => {
> - $crate::sync::SpinLock::new(
> - $inner, $crate::optional_name!($($name)?),
> $crate::static_lock_class!())
> + $crate::sync::SpinLock::new_with_name($inner,
> $crate::optional_name!($($name)?))
> };
> }
> pub use new_spinlock;
next prev parent reply other threads:[~2026-07-07 21:25 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-03 13:47 [PATCH RFC 0/2] rust: sync: create lock class using `#[track_caller]` Gary Guo
2026-07-03 13:47 ` [PATCH RFC 1/2] rust: sync: introduce a way to create lock class from caller Gary Guo
2026-07-07 21:25 ` lyude [this message]
2026-07-03 13:47 ` [PATCH RFC 2/2] lockdep: delegate Rust lock class printing to Rust code Gary Guo
2026-07-07 21:24 ` lyude
2026-07-03 14:01 ` [PATCH RFC 0/2] rust: sync: create lock class using `#[track_caller]` Peter Zijlstra
2026-07-03 14:24 ` Gary Guo
2026-07-03 22:32 ` Peter Zijlstra
2026-07-04 16:52 ` Gary Guo
2026-07-04 17:10 ` Peter Zijlstra
2026-07-04 17:46 ` Gary Guo
2026-07-07 21:05 ` lyude
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=1df84ed4095c28f1b636ef914ad43b80e00a38ab.camel@redhat.com \
--to=lyude@redhat.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=lossin@kernel.org \
--cc=mingo@redhat.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=will@kernel.org \
--cc=work@onurozkan.dev \
/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