Skip to content

Commit 8f65291

Browse files
Darksonningomolnar
authored andcommitted
rust: sync: Add accessor for the lock behind a given guard
In order to assert a particular `Guard` is associated with a particular `Lock`, add an accessor to obtain a reference to the underlying `Lock` of a `Guard`. Binder needs this assertion to ensure unsafe list operations are done with the correct lock held. [Boqun: Capitalize the title and reword the commit log] Signed-off-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Fiona Behrens <me@kloenk.dev> Link: https://lore.kernel.org/r/20250205-guard-get-lock-v2-1-ba32a8c1d5b7@google.com Link: https://lore.kernel.org/r/20250307232717.1759087-8-boqun.feng@gmail.com
1 parent de4b59d commit 8f65291

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

rust/kernel/sync/lock.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,12 @@ pub struct Guard<'a, T: ?Sized, B: Backend> {
199199
// SAFETY: `Guard` is sync when the data protected by the lock is also sync.
200200
unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
201201

202-
impl<T: ?Sized, B: Backend> Guard<'_, T, B> {
202+
impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
203+
/// Returns the lock that this guard originates from.
204+
pub fn lock_ref(&self) -> &'a Lock<T, B> {
205+
self.lock
206+
}
207+
203208
pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
204209
// SAFETY: The caller owns the lock, so it is safe to unlock it.
205210
unsafe { B::unlock(self.lock.state.get(), &self.state) };

0 commit comments

Comments
 (0)