Use the new automagic lock class code to remove the lock class and name parameters from Lock::new() and Lock::pin_init(). The old functions are renamed to new_with_class() and pin_init_with_class() respectively. The new approach uses the caller tracking machinery in Rust, which means it can be trivially wrapped by adding #[track_caller] to any functions that should bubble up lock class creation to their caller. This, for example, allows a type using multiple Mutexes to create separate lock classes for every user of the type, simply by adding that attribute to the mutex creation code paths. Signed-off-by: Asahi Lina <lina@xxxxxxxxxxxxx> --- rust/kernel/sync/lock.rs | 42 +++++++++++++++++++++++++++++++++++---- rust/kernel/sync/lock/mutex.rs | 4 ++-- rust/kernel/sync/lock/spinlock.rs | 2 +- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 8e71e7aa2cc1..8849741c1d9a 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -5,7 +5,7 @@ //! It contains a generic Rust lock and guard that allow for different backends (e.g., mutexes, //! spinlocks, raw spinlocks) to be provided with minimal effort. -use super::LockClassKey; +use super::{lockdep::caller_lock_class, LockClassKey}; use crate::{ bindings, init::PinInit, pin_init, str::CStr, try_pin_init, types::Opaque, types::ScopeGuard, }; @@ -103,7 +103,40 @@ unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {} impl<T, B: Backend> Lock<T, B> { /// Constructs a new lock initialiser. #[allow(clippy::new_ret_no_self)] - pub fn new(t: T, name: &'static CStr, key: LockClassKey) -> impl PinInit<Self> { + #[track_caller] + pub fn new(t: T) -> impl PinInit<Self> { + let (key, name) = caller_lock_class(); + Self::new_with_key(t, name, key) + } + + /// Constructs a new lock initialiser taking an initialiser/ + pub fn pin_init<E>(t: impl PinInit<T, E>) -> impl PinInit<Self, E> + where + E: core::convert::From<core::convert::Infallible>, + { + let (key, name) = caller_lock_class(); + Self::pin_init_with_key(t, name, key) + } + + /// Constructs a new lock initialiser. + #[allow(clippy::new_ret_no_self)] + #[track_caller] + pub fn new_named(t: T, name: &'static CStr) -> impl PinInit<Self> { + let (key, _) = caller_lock_class(); + Self::new_with_key(t, name, key) + } + + /// Constructs a new lock initialiser taking an initialiser/ + pub fn pin_init_named<E>(t: impl PinInit<T, E>, name: &'static CStr) -> impl PinInit<Self, E> + where + E: core::convert::From<core::convert::Infallible>, + { + let (key, _) = caller_lock_class(); + Self::pin_init_with_key(t, name, key) + } + + /// Constructs a new lock initialiser given a particular name and lock class key. + pub fn new_with_key(t: T, name: &'static CStr, key: LockClassKey) -> impl PinInit<Self> { pin_init!(Self { data: UnsafeCell::new(t), _pin: PhantomPinned, @@ -115,8 +148,9 @@ pub fn new(t: T, name: &'static CStr, key: LockClassKey) -> impl PinInit<Self> { }) } - /// Constructs a new lock initialiser taking an initialiser. - pub fn pin_init<E>( + /// Constructs a new lock initialiser taking an initialiser given a particular + /// name and lock class key. + pub fn pin_init_with_key<E>( t: impl PinInit<T, E>, name: &'static CStr, key: LockClassKey, diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs index 06fe685501b4..15ea70fa3933 100644 --- a/rust/kernel/sync/lock/mutex.rs +++ b/rust/kernel/sync/lock/mutex.rs @@ -13,7 +13,7 @@ #[macro_export] macro_rules! new_mutex { ($inner:expr $(, $name:literal)? $(,)?) => { - $crate::sync::Mutex::new( + $crate::sync::Mutex::new_with_key( $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!()) }; } @@ -26,7 +26,7 @@ macro_rules! new_mutex { #[macro_export] macro_rules! new_mutex_pinned { ($inner:expr $(, $name:literal)? $(,)?) => { - $crate::sync::Mutex::pin_init( + $crate::sync::Mutex::pin_init_with_key( $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!()) }; } diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs index 979b56464a4e..9f6137f047ee 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -13,7 +13,7 @@ #[macro_export] macro_rules! new_spinlock { ($inner:expr $(, $name:literal)? $(,)?) => { - $crate::sync::SpinLock::new( + $crate::sync::SpinLock::new_with_class( $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!()) }; } -- 2.40.1