rust: sync: Add lock::Backend::assert_is_held()
Subsystems (3)
Lyude Paul
reviewerPATCH v2 1/1 rust: sync: Add lock::Backend::assert_is_held()
Since we've exposed Lock::from_raw() and Guard::new() publically, we want to be able to make sure that we assert that a lock is actually held when constructing a Guard for it to handle instances of unsafe Guard::new() calls outside of our lock module.
So, let's do this by adding a new method assert_is_held to Backend, which uses lockdep to check whether or not a lock has been acquired. When lockdep is disabled, this has no overhead.
---
V2
* Use lockdep instead of is_locked() functions
* Drop is_locked, replace with assert_is_held()
--- rust/helpers/mutex.c | 5 +++++ rust/helpers/spinlock.c | 5 +++++ rust/kernel/sync/lock.rs | 10 ++++++++++ rust/kernel/sync/lock/mutex.rs | 7 +++++++ rust/kernel/sync/lock/spinlock.rs | 7 +++++++ 5 files changed, 34 insertions(+)
rust/helpers/mutex.c +5 -0
@@ -12,3 +12,8 @@ void rust_helper___mutex_init(struct mutex *mutex, const char *name, {__mutex_init (mutex, name, key); } + +void rust_helper_mutex_assert_is_held (struct mutex *mutex) +{ +lockdep_assert_held (mutex); +}
rust/helpers/spinlock.c +5 -0
@@ -26,3 +26,8 @@ int rust_helper_spin_trylock(spinlock_t *lock) {return spin_trylock (lock); } + +void rust_helper_spin_assert_is_held (spinlock_t *lock) +{ +lockdep_assert_held (lock); +}
rust/kernel/sync/lock.rs +10 -0
@@ -85,6 +85,13 @@ unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) {// SAFETY: The safety requirements ensure that the lock is initialised. *guard_state =unsafe {Self ::lock (ptr) }; } + +/// Asserts that the lock is held using lockdep. +/// +/// # Safety +/// +/// Callers must ensure that [`Backend::init`] has been previously called. +unsafe fn assert_is_held (ptr: *mut Self ::State ); }/// A mutual exclusion primitive.
@@ -207,6 +214,9 @@ impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {/// /// The caller must ensure that it owns the lock. pub (crate )unsafe fn new (lock: &'aLock <T ,B >, state:B ::GuardState ) ->Self { +// SAFETY: The caller can only hold the lock if `Backend::init` has already been called. +unsafe {B ::assert_is_held (lock.state.get ()) }; +Self { lock, state,
rust/kernel/sync/lock/mutex.rs +7 -0
@@ -126,4 +126,11 @@ unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {None } } + +unsafe fn assert_is_held (ptr: *mut Self ::State ) { +// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. +unsafe { + bindings::mutex_assert_is_held (ptr); + } + } }
rust/kernel/sync/lock/spinlock.rs +7 -0
@@ -125,4 +125,11 @@ unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {None } } + +unsafe fn assert_is_held (ptr: *mut Self ::State ) { +// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. +unsafe { + bindings::spin_assert_is_held (ptr); + } + } }
base-commit: b7ed2b6f4e8d7f64649795e76ee9db67300de8eb
Signature · 1 line
2.47.0
Quoted 11 lines · Lyude Paul
Since we've exposed
Lock::from_raw()andGuard::new()publically, we want to be able to make sure that we assert that a lock is actually held when constructing a Guard for it to handle instances of unsafeGuard::new()calls outside of our lock module.So, let's do this by adding a new method
assert_is_heldto Backend, which uses lockdep to check whether or not a lock has been acquired. When lockdep is disabled, this has no overhead.Signed-off-by Lyude Paul <[email protected]>
Queued for more testing and reviews, thanks!
Regards,
Boqun
Quoted 104 lines
---
V2
* Use lockdep instead ofis_locked()functions
* Dropis_locked, replace withassert_is_held()--- rust/helpers/mutex.c | 5 +++++ rust/helpers/spinlock.c | 5 +++++ rust/kernel/sync/lock.rs | 10 ++++++++++ rust/kernel/sync/lock/mutex.rs | 7 +++++++ rust/kernel/sync/lock/spinlock.rs | 7 +++++++ 5 files changed, 34 insertions(+)rust/helpers/mutex.c +5
-0@@ -12,3 +12,8 @@ void rust_helper___mutex_init(struct mutex *mutex, const char *name, {__mutex_init (mutex, name, key); } + +void rust_helper_mutex_assert_is_held (struct mutex *mutex) +{ +lockdep_assert_held (mutex); +}rust/helpers/spinlock.c +5
-0@@ -26,3 +26,8 @@ int rust_helper_spin_trylock(spinlock_t *lock) {return spin_trylock (lock); } + +void rust_helper_spin_assert_is_held (spinlock_t *lock) +{ +lockdep_assert_held (lock); +}rust/kernel/sync/lock.rs +10
-0@@ -85,6 +85,13 @@ unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) {// SAFETY: The safety requirements ensure that the lock is initialised. *guard_state =unsafe {Self ::lock (ptr) }; } + +/// Asserts that the lock is held using lockdep. +/// +/// # Safety +/// +/// Callers must ensure that [`Backend::init`] has been previously called. +unsafe fn assert_is_held (ptr: *mut Self ::State ); }/// A mutual exclusion primitive. @@ -207,6 +214,9 @@ impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {/// /// The caller must ensure that it owns the lock. pub (crate )unsafe fn new (lock: &'aLock <T ,B >, state:B ::GuardState ) ->Self { +// SAFETY: The caller can only hold the lock if `Backend::init` has already been called. +unsafe {B ::assert_is_held (lock.state.get ()) }; +Self { lock, state,rust/kernel/sync/lock/mutex.rs +7
-0@@ -126,4 +126,11 @@ unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {None } } + +unsafe fn assert_is_held (ptr: *mut Self ::State ) { +// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. +unsafe { + bindings::mutex_assert_is_held (ptr); + } + } }rust/kernel/sync/lock/spinlock.rs +7
-0@@ -125,4 +125,11 @@ unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {None } } + +unsafe fn assert_is_held (ptr: *mut Self ::State ) { +// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. +unsafe { + bindings::spin_assert_is_held (ptr); + } + } }base-commit:
b7ed2b6f4e8d7f64649795e76ee9db67300de8ebSignature · 1 line
2.47.0
The following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: fbd7a5a0359bc770e898d918d84977ea61163aad Gitweb: https://git.kernel.org/tip/fbd7a5a0359bc770e898d918d84977ea61163aad Author: Lyude Paul <[email protected]> AuthorDate: Mon, 25 Nov 2024 15:40:58 -05:00 Committer: Boqun Feng <[email protected]> CommitterDate: Thu, 19 Dec 2024 14:04:42 -08:00
rust: sync: Add lock::Backend::assert_is_held()
Since we've exposed Lock::from_raw() and Guard::new() publically, we want to be able to make sure that we assert that a lock is actually held when constructing a Guard for it to handle instances of unsafe Guard::new() calls outside of our lock module.
Hence add a new method assert_is_held() to Backend, which uses lockdep to check whether or not a lock has been acquired. When lockdep is disabled, this has no overhead.
[Boqun: Resolve the conflicts with exposing Guard::new(), reword the
commit log a bit and format "unsafe { <statement>; }" into "unsafe { <statement> }" for the consistency. ]
--- rust/helpers/mutex.c | 5 +++++ rust/helpers/spinlock.c | 5 +++++ rust/kernel/sync/lock.rs | 10 ++++++++++ rust/kernel/sync/lock/mutex.rs | 5 +++++ rust/kernel/sync/lock/spinlock.rs | 5 +++++ 5 files changed, 30 insertions(+)
rust/helpers/mutex.c +5 -0
@@ -12,3 +12,8 @@ void rust_helper___mutex_init(struct mutex *mutex, const char *name, {__mutex_init (mutex, name, key); } + +void rust_helper_mutex_assert_is_held (struct mutex *mutex) +{ +lockdep_assert_held (mutex); +}
rust/helpers/spinlock.c +5 -0
@@ -30,3 +30,8 @@ int rust_helper_spin_trylock(spinlock_t *lock) {return spin_trylock (lock); } + +void rust_helper_spin_assert_is_held (spinlock_t *lock) +{ +lockdep_assert_held (lock); +}
rust/kernel/sync/lock.rs +10 -0
@@ -90,6 +90,13 @@ pub unsafe trait Backend {// SAFETY: The safety requirements ensure that the lock is initialised. *guard_state =unsafe {Self ::lock (ptr) }; } + +/// Asserts that the lock is held using lockdep. +/// +/// # Safety +/// +/// Callers must ensure that [`Backend::init`] has been previously called. +unsafe fn assert_is_held (ptr: *mut Self ::State ); }/// A mutual exclusion primitive.
@@ -235,6 +242,9 @@ impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {/// /// The caller must ensure that it owns the lock. pub unsafe fn new (lock: &'aLock <T ,B >, state:B ::GuardState ) ->Self { +// SAFETY: The caller can only hold the lock if `Backend::init` has already been called. +unsafe {B ::assert_is_held (lock.state.get ()) }; +Self { lock, state,
rust/kernel/sync/lock/mutex.rs +5 -0
@@ -134,4 +134,9 @@ unsafe impl super::Backend for MutexBackend {None } } + +unsafe fn assert_is_held (ptr: *mut Self ::State ) { +// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. +unsafe { bindings::mutex_assert_is_held (ptr) } + } }
rust/kernel/sync/lock/spinlock.rs +5 -0
@@ -133,4 +133,9 @@ unsafe impl super::Backend for SpinLockBackend {None } } + +unsafe fn assert_is_held (ptr: *mut Self ::State ) { +// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. +unsafe { bindings::spin_assert_is_held (ptr) } + } }