Message ID | 20240222-rust-locks-get-mut-v3-1-d38a6f4bde3d@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-76877-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp52834dyb; Thu, 22 Feb 2024 08:27:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU0oiYedz+sunlg6WqbwycvMGsC4yRNY1XFCuE4vQghd5PCxqMgNgtLo1wek1mfYmtNyYg+KFQmcienKL8JpWWYkXNeAg== X-Google-Smtp-Source: AGHT+IFZY0LR910GrDMYab8z2E/WgakPBRWWUmxG5AedQlxt2TntthFaxsQp0Adw1J+p4XiMpnQI X-Received: by 2002:ac8:7f48:0:b0:42e:5a82:d448 with SMTP id g8-20020ac87f48000000b0042e5a82d448mr809629qtk.25.1708619224618; Thu, 22 Feb 2024 08:27:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708619224; cv=pass; d=google.com; s=arc-20160816; b=0opglX2NE7rk1vwMBvz4FYZavszVnN2cGqIzbs8Ezrikb9u0xKhWJWItlz86ge9yLa KArI1vWtvdEA9+Dvs5Da1ylPgptfX3rpVZo9GB67HumFtJaHK/T7EBgpWQT4b/w5RGkr JUV0cunRz8rQ9UcoO257p/eRu80GuIIflDan/GdirbnhETJIHtBb9ZvD1QjFQJZhRLOy UbjMhRTHA3bsJjglqLin6ZPdPATXFXT+Lsm6YG5LP6YdaGOqusTYMChmrh/0e6KTuKDd XvAygYvksAfxpV897cLFAso1jyVIBsgzizX4dAdkku9qpA95vK2u/Kz3ob2lXgm7XK4Q e9zA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=reply-to:cc:to:message-id:content-transfer-encoding:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:subject:date :from:dkim-signature; bh=ptWW47VZHpZsuYFXnfeEhiT+DXIONM8JnI8BnqUNK9c=; fh=qwJeMkdNujxVMHeH7Cj+tJvO7drDDKA/K4Q6lkr51cU=; b=bbseDE4Jkfybv8qmMz1nE02tKiHlsyKdNbWivT1A9K2QMPlC4ZpsBO9M6jWOQ/pPTy yyo3zAFSBFzdiYen49h4uQsY0NBdVOWAvu1ABJi/Q+s4lg5e6vDvHwHliYvN7q2W0Opq Q654QI4408SGw32lQIYb+ywTzO9k52P6bAU8Ax5L+i5HvSSUUXVCUUzSxwI7eFum1rGC SXr1sOauvzh9SEhWgW7EIXMvNiC1A943xbQq/mP9uLyUWfArtVD631Jx6YbYS8HzkuFa 31t/1ZLSRgX9M8lN3XR0thvv7A02Yy4iz03HJEBy/wiBAUm36+VPyhHXlH32xy8+YTVz j+Cw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WLO5vQhX; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-76877-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76877-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id v3-20020ac87483000000b0042dc70f7e51si12914550qtq.679.2024.02.22.08.27.04 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 08:27:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-76877-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WLO5vQhX; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-76877-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76877-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 650C01C225B6 for <ouuuleilei@gmail.com>; Thu, 22 Feb 2024 16:27:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1F407151CED; Thu, 22 Feb 2024 16:26:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WLO5vQhX" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E6BD1509B5; Thu, 22 Feb 2024 16:26:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708619205; cv=none; b=nd74QlfFj3pE2VOPzGWSBt26blx1FZ8qd+Tmx7c79HrNvS5PK9tLVoPZAAraMgqGdKCvbDI5MTZkTUfuOkfbt6IFNWTH6lL/0zl7U69C2FOXUjCE1etF3pbT0X6Rbah4ZVZOaHd5qGlfEmJCFWQAnN4y8I5RnTHI8iR/qt28DKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708619205; c=relaxed/simple; bh=Sg+7uBjGXjQ1pdFX/E/hnLOflmTEI1B/auJ2nJ5yZc0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=sOf8XR0EmEIvNeNP1lGbQhB9Dj3+RwrkHStbAu1vev3yomZudj5JRc+Z6c5Nfh3Gmm1yXpq4OJRAvZInoCeTkgLgT+EQOtK5FwZTkrTBxS34ofB2RMlTkPq9wMHxpOrqPWi7v32kKwdsqBBXf6GgBnFyzEt5C1bGANNbrrKYDUw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WLO5vQhX; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPS id C2867C433F1; Thu, 22 Feb 2024 16:26:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708619204; bh=Sg+7uBjGXjQ1pdFX/E/hnLOflmTEI1B/auJ2nJ5yZc0=; h=From:Date:Subject:To:Cc:Reply-To:From; b=WLO5vQhXIjUKiQW8MTNAerCcpBsPpFOzeIdgsi8kk7cuLH0455SdSy2LdfaS8pz+z szQinCZUxI5/ERaAYyqew38pmwKsmbUFRx1B1AUpebU9Jd/IPBOelz00AjHfKTcSDE 5cVg2krcsICfsyr9EjXNaPf5JZvHkb9R7BDVz+ZjnAnkHqsuavU3//GOC6+mMk6H1B 8/2MmVRZ5oWWc59NVvtCDEhMCQDpBMRVotzu5xsc9CopupQialp/GadulIhhOgt0bn Yv8FV9pQ/lY/UWTXj7xM16MwdJT0uGWV++70IiQpvuK8cGxVdlelRvj0aQlAr4TiJk cNL7YxC/U3URg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id A36E4C54791; Thu, 22 Feb 2024 16:26:44 +0000 (UTC) From: Mathys-Gasnier via B4 Relay <devnull+mathys35.gasnier.gmail.com@kernel.org> Date: Thu, 22 Feb 2024 17:26:44 +0100 Subject: [PATCH v3] rust: locks: Add `get_mut` method to `Lock` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240222-rust-locks-get-mut-v3-1-d38a6f4bde3d@gmail.com> X-B4-Tracking: v=1; b=H4sIAMN112UC/23NQQ6CMBCF4auQrh3TGUqqrryHcQFDgUagpgWiI dzdghsWLP+XzDezCMZbE8QtmYU3kw3W9THSUyK4yfvagC1jC5KkJOIF/BgGaB2/AtRmgG4cgBV JTSix1LmIh29vKvvZ0MczdmPD4Px3+zHhuv45ktcjbkJAYJNmWHFaGqXvdZfb9syuEys30Y5AO iQoEhlzmSqmQstiTyzL8gNvQQPR9wAAAA== To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Wedson Almeida Filho <wedsonaf@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, Benno Lossin <benno.lossin@proton.me>, Andreas Hindborg <a.hindborg@samsung.com>, Alice Ryhl <aliceryhl@google.com> Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Mathys-Gasnier <mathys35.gasnier@gmail.com> X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1708619203; l=2400; i=mathys35.gasnier@gmail.com; s=20240118; h=from:subject:message-id; bh=ky/U3Y/MfgLzPcLAna8JPEAOaEfDevCIfhcVRsmkGug=; b=+QNhsp6z7HFvXcgU9rqBsgzmYXhbx8BMMQKZVVl3iF27daLZJUjw+acCHkIHotOIKO9yiW8qw t1cr1passtyA4d6GfP0rDl+TbeGP4C0ecOarsbaGF95e1sfZ2j6smYK X-Developer-Key: i=mathys35.gasnier@gmail.com; a=ed25519; pk=C5tqKAA3Ua7li5s3a+q2aDelT2j98/yjGg2nEVGArXE= X-Endpoint-Received: by B4 Relay for mathys35.gasnier@gmail.com/20240118 with auth_id=129 X-Original-From: Mathys-Gasnier <mathys35.gasnier@gmail.com> Reply-To: <mathys35.gasnier@gmail.com> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791617112487647698 X-GMAIL-MSGID: 1791617112487647698 |
Series |
[v3] rust: locks: Add `get_mut` method to `Lock`
|
|
Commit Message
Mathys-Gasnier via B4 Relay
Feb. 22, 2024, 4:26 p.m. UTC
From: Mathys-Gasnier <mathys35.gasnier@gmail.com> Having a mutable reference guarantees that no other threads have access to the lock, so we can take advantage of that to grant callers access to the protected data without the the cost of acquiring and releasing the locks. Since the lifetime of the data is tied to the mutable reference, the borrow checker guarantees that the usage is safe. Signed-off-by: Mathys-Gasnier <mathys35.gasnier@gmail.com> --- Changes in v3: - Changing the function to take a `Pin<&mut self>` instead of a `&mut self` - Removed reviewed-by's since big changes were made. Please take another look. - Link to v2: https://lore.kernel.org/r/20240212-rust-locks-get-mut-v2-1-5ccd34c2b70b@gmail.com Changes in v2: - Improved doc comment. - Link to v1: https://lore.kernel.org/r/20240209-rust-locks-get-mut-v1-1-ce351fc3de47@gmail.com --- rust/kernel/sync/lock.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) --- base-commit: 711cbfc717650532624ca9f56fbaf191bed56e67 change-id: 20240118-rust-locks-get-mut-c42072101d7a Best regards,
Comments
On 2/22/24 13:26, Mathys-Gasnier via B4 Relay wrote: > From: Mathys-Gasnier <mathys35.gasnier@gmail.com> > > Having a mutable reference guarantees that no other threads have > access to the lock, so we can take advantage of that to grant callers > access to the protected data without the the cost of acquiring and > releasing the locks. Since the lifetime of the data is tied to the > mutable reference, the borrow checker guarantees that the usage is safe. > > Signed-off-by: Mathys-Gasnier <mathys35.gasnier@gmail.com> > --- > [...] This looks magnificent as is. Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Hi, Thanks for the patch! Please see a few comments below. On Thu, Feb 22, 2024 at 05:26:44PM +0100, Mathys-Gasnier via B4 Relay wrote: > From: Mathys-Gasnier <mathys35.gasnier@gmail.com> > > Having a mutable reference guarantees that no other threads have > access to the lock, so we can take advantage of that to grant callers > access to the protected data without the the cost of acquiring and > releasing the locks. Since the lifetime of the data is tied to the > mutable reference, the borrow checker guarantees that the usage is safe. > > Signed-off-by: Mathys-Gasnier <mathys35.gasnier@gmail.com> > --- > Changes in v3: > - Changing the function to take a `Pin<&mut self>` instead of a `&mut self` > - Removed reviewed-by's since big changes were made. Please take another > look. > - Link to v2: https://lore.kernel.org/r/20240212-rust-locks-get-mut-v2-1-5ccd34c2b70b@gmail.com > > Changes in v2: > - Improved doc comment. > - Link to v1: https://lore.kernel.org/r/20240209-rust-locks-get-mut-v1-1-ce351fc3de47@gmail.com > --- > rust/kernel/sync/lock.rs | 16 +++++++++++++++- > 1 file changed, 15 insertions(+), 1 deletion(-) > > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs > index f12a684bc957..0c8faf36d654 100644 > --- a/rust/kernel/sync/lock.rs > +++ b/rust/kernel/sync/lock.rs > @@ -7,7 +7,11 @@ > > use super::LockClassKey; > use crate::{bindings, init::PinInit, pin_init, str::CStr, types::Opaque, types::ScopeGuard}; > -use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned}; > +use core::{ > + cell::UnsafeCell, > + marker::{PhantomData, PhantomPinned}, > + pin::Pin, > +}; > use macros::pin_data; > > pub mod mutex; > @@ -121,6 +125,16 @@ pub fn lock(&self) -> Guard<'_, T, B> { > // SAFETY: The lock was just acquired. > unsafe { Guard::new(self, state) } > } > + > + /// Gets the data contained in the lock This above line could use a period and a new line. > + /// Having a mutable reference to the lock guarantees that no other threads have access to the lock. > + /// Making it safe to get a mutable reference to the lock content. > + pub fn get_mut(self: Pin<&mut Self>) -> &mut T { > + // SAFETY: Since the data is not pinned (No structural pinning for data). > + // It is safe to get a mutable reference to the data and we never move the state. Compare to "never move the state", a more accurate safety guarantee is "the `&mut Self` is only used to get the reference of the `data` field, therefore `self` won't get moved", I think. BTW, while we are at it, I think we should document the "structural/non-structural pinning" design decisions somewhere, for example in the struct definition: #[pin_data] pub struct Lock<T: ?Sized, B: Backend> { ... /// The data protected by the lock. /// This field is non-structural pinned. pub(crate) data: UnsafeCell<T>, } Thoughts? Or do we think "non-structural pinned" should be the default case so no need to document it? I want to have a clear document for each field to avoid the accidental "everyone forgets what's the decision here" ;-) Regards, Boqun > + let lock = unsafe { self.get_unchecked_mut() }; > + lock.data.get_mut() > + } > } > > /// A lock guard. > > --- > base-commit: 711cbfc717650532624ca9f56fbaf191bed56e67 > change-id: 20240118-rust-locks-get-mut-c42072101d7a > > Best regards, > -- > Mathys-Gasnier <mathys35.gasnier@gmail.com> >
On Fri, Feb 23, 2024 at 3:52 AM Boqun Feng <boqun.feng@gmail.com> wrote: > BTW, while we are at it, I think we should document the > "structural/non-structural pinning" design decisions somewhere, for > example in the struct definition: > > #[pin_data] > pub struct Lock<T: ?Sized, B: Backend> { > ... > /// The data protected by the lock. > /// This field is non-structural pinned. > pub(crate) data: UnsafeCell<T>, > } > > Thoughts? Or do we think "non-structural pinned" should be the default > case so no need to document it? I want to have a clear document for each > field to avoid the accidental "everyone forgets what's the decision > here" ;-) I would normally assume that "field is not marked #[pin]" implies that it's not structurally pinned. But it could still be worth to call out here. I prefer the wording "not structurally pinnned" over "non-structural pinned". Alice
On Sun, Feb 25, 2024 at 10:21:23AM +0100, Mathys Gasnier wrote: > Should i include this comment in this patch ? > My suggestion is 1) in the comment of the `get_mut()` function, mention that "`data` is not structurally pinned, so return a `&mut T` here" and 2) in the function body of `get_mut()`, at the safety comments, you only need to put the reasoning explaining that `self` wouldn't get moved via the return value of `self.get_unchecked_mut()`. With these (along with the period and newline added), it'll be good to me. Regards, Boqun > Le ven. 23 févr. 2024 à 11:49, Alice Ryhl <aliceryhl@google.com> a écrit : > > > On Fri, Feb 23, 2024 at 3:52 AM Boqun Feng <boqun.feng@gmail.com> wrote: > > > BTW, while we are at it, I think we should document the > > > "structural/non-structural pinning" design decisions somewhere, for > > > example in the struct definition: > > > > > > #[pin_data] > > > pub struct Lock<T: ?Sized, B: Backend> { > > > ... > > > /// The data protected by the lock. > > > /// This field is non-structural pinned. > > > pub(crate) data: UnsafeCell<T>, > > > } > > > > > > Thoughts? Or do we think "non-structural pinned" should be the default > > > case so no need to document it? I want to have a clear document for each > > > field to avoid the accidental "everyone forgets what's the decision > > > here" ;-) > > > > I would normally assume that "field is not marked #[pin]" implies that > > it's not structurally pinned. But it could still be worth to call out > > here. > > > > I prefer the wording "not structurally pinnned" over "non-structural > > pinned". > > > > Alice > >
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index f12a684bc957..0c8faf36d654 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -7,7 +7,11 @@ use super::LockClassKey; use crate::{bindings, init::PinInit, pin_init, str::CStr, types::Opaque, types::ScopeGuard}; -use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned}; +use core::{ + cell::UnsafeCell, + marker::{PhantomData, PhantomPinned}, + pin::Pin, +}; use macros::pin_data; pub mod mutex; @@ -121,6 +125,16 @@ pub fn lock(&self) -> Guard<'_, T, B> { // SAFETY: The lock was just acquired. unsafe { Guard::new(self, state) } } + + /// Gets the data contained in the lock + /// Having a mutable reference to the lock guarantees that no other threads have access to the lock. + /// Making it safe to get a mutable reference to the lock content. + pub fn get_mut(self: Pin<&mut Self>) -> &mut T { + // SAFETY: Since the data is not pinned (No structural pinning for data). + // It is safe to get a mutable reference to the data and we never move the state. + let lock = unsafe { self.get_unchecked_mut() }; + lock.data.get_mut() + } } /// A lock guard.