From e7e5a051c8212020482f718764dd9a22d26cfb05 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 5 Feb 2025 10:54:50 +0000 Subject: [PATCH] FROMGIT: 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 Reviewed-by: Fiona Behrens Signed-off-by: Boqun Feng Link: https://lore.kernel.org/r/20250205-guard-get-lock-v2-1-ba32a8c1d5b7@google.com Bug: 388786466 (cherry picked from commit 84f788fe93eb1311824d6853595baa50f3ccd175 git://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git lockdep-for-tip) Change-Id: Ib67020b20ed1ac071e0aeaa0f702553dc915aed0 Signed-off-by: Alice Ryhl --- rust/kernel/sync/lock.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 62d226ae020e..b41360623bc2 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -169,7 +169,12 @@ pub struct Guard<'a, T: ?Sized, B: Backend> { // SAFETY: `Guard` is sync when the data protected by the lock is also sync. unsafe impl Sync for Guard<'_, T, B> {} -impl Guard<'_, T, B> { +impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> { + /// Returns the lock that this guard originates from. + pub fn lock_ref(&self) -> &'a Lock { + self.lock + } + pub(crate) fn do_unlocked(&mut self, cb: impl FnOnce() -> U) -> U { // SAFETY: The caller owns the lock, so it is safe to unlock it. unsafe { B::unlock(self.lock.state.get(), &self.state) };