Rust for Linux List
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Lyude Paul <lyude@redhat.com>
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	 rust-for-linux@vger.kernel.org, Gary Guo <gary@garyguo.net>,
	 Ingo Molnar <mingo@redhat.com>, Miguel Ojeda <ojeda@kernel.org>,
	 Tamir Duberstein <tamird@kernel.org>,
	Boqun Feng <boqun@kernel.org>,
	 Peter Zijlstra <peterz@infradead.org>,
	Benno Lossin <lossin@kernel.org>, Will Deacon <will@kernel.org>
Subject: Re: [PATCH v2 1/2] rust: sync: lock: Add Lock::get_mut_pinned()
Date: Mon, 1 Jun 2026 08:28:20 +0000	[thread overview]
Message-ID: <ah1CpIJaWBiUrNNr@google.com> (raw)
In-Reply-To: <20260529173137.303717-2-lyude@redhat.com>

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 <lyude@redhat.com>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

>  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
> 

  reply	other threads:[~2026-06-01  8:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-29 17:30 [PATCH v2 0/2] rust: sync: Introduce LazyInit Lyude Paul
2026-05-29 17:30 ` [PATCH v2 1/2] rust: sync: lock: Add Lock::get_mut_pinned() Lyude Paul
2026-06-01  8:28   ` Alice Ryhl [this message]
2026-05-29 17:30 ` [PATCH v2 2/2] rust: sync: Introduce LazyInit Lyude Paul
2026-06-01  8:27   ` Alice Ryhl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ah1CpIJaWBiUrNNr@google.com \
    --to=aliceryhl@google.com \
    --cc=boqun@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=mingo@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox