On Fri, May 29, 2026 at 01:30:41PM -0400, Lyude Paul wrote: > These two functions are inspired by the Rust stdlib equivalent: > > https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.get_mut() > > The idea here is very simple - if the user has access to a Pin<&mut > Mutex<…>>, we can guarantee that no one else can look at the data protected > by the lock. Thus in such situations, locking the mutex isn't necessary to > access its contents. This can be useful in situations like `Drop` > implementations, where we may want to access the contents of a Mutex within > a struct before dropping it. > > So to do this, we add `get_mut_pinned()` to `Lock` - which provides a > function to access the inner contents of a Mutex provided a Pin<&mut …>. > > Signed-off-by: Lyude Paul <[email protected]>
Reviewed-by: Alice Ryhl <[email protected]> > rust/kernel/sync/lock.rs | 11 +++++++++++ > 1 file changed, 11 insertions(+) > > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs > index 10b6b5e9b024f..5ca36baed34f5 100644 > --- a/rust/kernel/sync/lock.rs > +++ b/rust/kernel/sync/lock.rs > @@ -190,6 +190,17 @@ pub fn try_lock(&self) -> Option<Guard<'_, T, B>> { > // that `init` was called. > unsafe { B::try_lock(self.state.get()).map(|state| Guard::new(self, > state)) } > } > + > + /// Returns a pinned mutable reference to the underlying data. > + /// > + /// Because this borrows the lock mutably, no actual locking needs to > take place - as the > + /// mutable borrow statically guarantees no new locks can be acquired > while this reference > + /// exists. > + #[inline(always)] Normal #[inline] is sufficient. > + pub fn get_mut_pinned(self: Pin<&mut Self>) -> Pin<&mut T> { > + // SAFETY: We return a pinned T, ensuring we don't move T. > + unsafe { self.map_unchecked_mut(|data| data.data.get_mut()) } > + } > } > > /// A lock guard. > -- > 2.54.0 >
