From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57ABA38AC7E for ; Mon, 1 Jun 2026 08:28:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780302506; cv=none; b=PDAJaIvvOty0K/Ow27Oyorjl4lubglHvcgdNzTq9eOzuw5qzR5vugN8Cjf7S/Fmezw07XbMJtAuM4H28/N8h5UuMgwVbdQlnqXN7MBojTI0UzKw3bDt4j7CRhmII8iH+j8T6eYgwupHUBBbz8QuSZKgvxKtRtUPKjrkv2XVFhLc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780302506; c=relaxed/simple; bh=otexNk9OHuD/6rio1mOTZPg7DkxgYhCuRlwbkbOk/EU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oiAE5DvUua7TcVt0jkyrgYs/bBdLKu/c1X0SLEEV09th73Li7nqgPbjTjPEwXG+ynFPK/fWMDiIHTsHGWgGG3Wp2JJ4IUDzuhzpIU0m6oIjKLbJSrWvjB8OxuPMpJY+q7rW58s01Ps6Qbp0V3wgNP2pSpj+ACUmtiQkoHPZ+EDI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tRHrRFFv; arc=none smtp.client-ip=209.85.208.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tRHrRFFv" Received: by mail-ed1-f74.google.com with SMTP id 4fb4d7f45d1cf-68b5941a81bso15955a12.1 for ; Mon, 01 Jun 2026 01:28:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780302503; x=1780907303; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=FNCDtya95lHSsHMXITL21ruuRscfBvAweyPJ3bnEneE=; b=tRHrRFFv3ZU50gAkL84N/6T8HIiVJHifl2RhT7wKpYvM3yxP09NK6JE8pJk1d0Fcke VmNAzMWC5AiYRzATUJYDu8TbAZxzGf47yFa1Hp/5uc3pKIIvTQB3YHN8prglV96bvE6U v5VmJHaxBJ9bmYvryknmmkclow3E1FY58zlCWxVdQGEqGy7+P6f+pN7T8r452CzALqG8 c7f/xrTCtNPoYcZhGO5blpOlO4T/ICyM8Auj0MC+zkTRtr4VI1iytQ9Co/HBWkr8+H6+ ulHsAfUsjIyqL1Om2sTZ9hOudmUDbmbUxnB3TzHyVDfAGx2oXQQOz1CLUNkHhKfSJYJJ 5J1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780302503; x=1780907303; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=FNCDtya95lHSsHMXITL21ruuRscfBvAweyPJ3bnEneE=; b=Fp3QTXkL8wgm9gKAJ66k/F5UddTrydxCh08aJcyEtMQXIO7CvsBZyWS3MogMAixpVZ FJUtGKdTqAyFLRVNJ3ZQug9rd8XtcDRZhBsLUGoH15MgPPVmzYM4gm0ERGc1XoL7q6nj 1iiaEAelnRjj+31rLlZhaytsTNDIrNnv0TUR6eP/UVUb43hOh5OJis2TpZINYV1cL3k3 JJCzQ6NxNq4eoClA0Df4Tk0a2EFug6U29D30nPjdBn8GlCFmlEv4rolrhze/umw3j3dL xb278Fe3qcmVSaRwOAub0uVXz9q9IhqwcXqsazMkbf5gGzOYSaaIH+CAQ4M7TDB3VHX4 +xxA== X-Forwarded-Encrypted: i=1; AFNElJ8RhYSPlwDnw9YaqA0of95gsTXM/yK7uT+GvBO7e30JZDf0hDJTXfgb6VoZyK3z391wggTAkkPS0nmADXU=@vger.kernel.org X-Gm-Message-State: AOJu0YwhoiCgLcw3h29if0pwrZMa7sDPGvEbhZNffLSMN6AIlmByNed8 CKuSElYKa8cQH41RH6hrTltLMuQEXHEmpSlEpEIZ4MiJYSpvLd+b37F10uwVmEUux+5A98Q25B7 3r65qAQp16Q7Y3D2IPw== X-Received: from edbdk3.prod.google.com ([2002:a05:6402:1d83:b0:68b:767b:1828]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:d1d3:0:b0:671:9dec:ba3 with SMTP id 4fb4d7f45d1cf-68c07b60fc3mr4435388a12.13.1780302502114; Mon, 01 Jun 2026 01:28:22 -0700 (PDT) Date: Mon, 1 Jun 2026 08:28:20 +0000 In-Reply-To: <20260529173137.303717-2-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529173137.303717-1-lyude@redhat.com> <20260529173137.303717-2-lyude@redhat.com> Message-ID: Subject: Re: [PATCH v2 1/2] rust: sync: lock: Add Lock::get_mut_pinned() From: Alice Ryhl To: Lyude Paul Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Gary Guo , Ingo Molnar , Miguel Ojeda , Tamir Duberstein , Boqun Feng , Peter Zijlstra , Benno Lossin , Will Deacon Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Fri, May 29, 2026 at 01:30:41PM -0400, Lyude Paul wrote: > These two functions are inspired by the Rust stdlib equivalent: >=20 > https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.get_mut() >=20 > The idea here is very simple - if the user has access to a Pin<&mut > Mutex<=E2=80=A6>>, 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 t= o > access its contents. This can be useful in situations like `Drop` > implementations, where we may want to access the contents of a Mutex with= in > a struct before dropping it. >=20 > 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 =E2= =80=A6>. >=20 > Signed-off-by: Lyude Paul Reviewed-by: Alice Ryhl > rust/kernel/sync/lock.rs | 11 +++++++++++ > 1 file changed, 11 insertions(+) >=20 > 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> { > // that `init` was called. > unsafe { B::try_lock(self.state.get()).map(|state| Guard::new(se= lf, state)) } > } > + > + /// Returns a pinned mutable reference to the underlying data. > + /// > + /// Because this borrows the lock mutably, no actual locking needs t= o take place - as the > + /// mutable borrow statically guarantees no new locks can be acquire= d 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()) } > + } > } > =20 > /// A lock guard. > --=20 > 2.54.0 >=20