Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp592461lqg; Fri, 1 Mar 2024 14:54:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVyCuZo3Uc9ALYJnll0sNHNZQd+INuA3QvTLidmuOZ1YxGHWHtKS4nEl9+vamhAU1qtexmuOPjgKiGuGuDYVA3rseZgg2KvtBwVnLSNVg== X-Google-Smtp-Source: AGHT+IHzHKT7YoEYI/kAS5ETFFms1sGc9qIcGsDsseqeoOhX5STrsZd23QRypyRo7h+aznguFMXA X-Received: by 2002:a19:8c48:0:b0:513:b8:fd51 with SMTP id i8-20020a198c48000000b0051300b8fd51mr2237116lfj.45.1709333648158; Fri, 01 Mar 2024 14:54:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709333648; cv=pass; d=google.com; s=arc-20160816; b=pqY05dv5f98PgG4bzNqNysg2qq7GP37IYrjvC9uS615Fng4ccwDus0G139B4i/ceuR yXqbKqRB5T8JCri82IJfwGlISBYgm+ZIpfn1mcrHkKepVas6//QVHKLffwv8pEwZzGHL NK5wqzBprpVi1ars0FQJM5/IUmkNzud3boGzK0Z1djp5/mdY7hCFMlVe+Fo07yEflbyN HEk9UoVrSq0oiH4ic2CO3bGgZpynldoSbLt4uEOrsY3axTxTTc6r3NkH33gjG/YnijAB a99NFkoDNz/cZP9ig3UzQO97GXhpE1EJVXLBQvb9jsRA3Sd3p/4U1+wPBRwOWDTj5jza SwUw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=dpIP2vZ3D1DN449Uvv4oePTKmqkMaAo4lO0G7OfiEqk=; fh=xROjBKCcwpOH7dVYAinjrtk3Tgselmt+BzCsB/Iu19c=; b=S+z+R11f865ze0jscbqVCLJGMrk5hAIKB5TvDohmCaJF1rhXnWGN50NTNCHHoLRVyZ 3ml7/AdhnKYjm6CIh83izBz+qdkP3OY+DjzEtHYgRau+jtl5BqlBZn4qppu5We74Dnrx /mFMU2EWXVuM3aOerYWMiWDPgm7W+WImv/NzUs++C2N/HB/BRdKISPD3hQI69ZQRSbJ4 6eAlbyKAXt5j7Qzo6VnXULSo5BYStNzaZBdYNxPuHoxe0Sy2Q7iMJNgWjLPs4LOY0fVy pnLYfg0npQ087TV2+Dx59LvCVdRLXhEj3l3VJ8pCMIfSy8Yjl4WYOgyvStxF/6alYjOj xm8Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VLyuAiXb; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-89209-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89209-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id m20-20020a056402511400b00563948cbff6si1843255edd.348.2024.03.01.14.54.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 14:54:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-89209-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VLyuAiXb; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-89209-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89209-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 9BE481F246B4 for ; Fri, 1 Mar 2024 22:54:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 65D695EE7E; Fri, 1 Mar 2024 22:53:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VLyuAiXb" Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) (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 C0C185EE64; Fri, 1 Mar 2024 22:53:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709333636; cv=none; b=fLRIe6f64ouck171ElVQh6+xr5nTeGxDmVQ1LiY78C+KTsk2AHIMgHo8mdDhGmPtJZM6Eazzhkol7wwWEsWBYfArJ3Tmti3yvdLApW/5zl3zpWwTigACILaXGGu69fKQRgE2ATdKJxOrWvFFEC38lQmLoIs8JYRisQYvmKg+CBQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709333636; c=relaxed/simple; bh=VsZMdjYHywpaXWli5LwKAYdq01wqqIHubkBZDVKzvis=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=L4sZIIGjJHAH+aCbJnU2Fy0YYFG6kxmT+tExm3yMnBZc3Eujlp9ucnjgr2PdcLelzBVZJD39TO+7X9JoMPT34P1uvjV5iUumU6+f4i0LQp7eGXYCxeC80+m/5QhMEdCrAfHrXCb2kVoBmUbo5VX9wdMd/9I9jfynxJhBQSMjMYQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VLyuAiXb; arc=none smtp.client-ip=209.85.219.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qv1-f50.google.com with SMTP id 6a1803df08f44-6901114742bso9115406d6.2; Fri, 01 Mar 2024 14:53:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709333634; x=1709938434; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=dpIP2vZ3D1DN449Uvv4oePTKmqkMaAo4lO0G7OfiEqk=; b=VLyuAiXbRVFqUxFCstT26D6ZpWFV5JV1vUl2IP8BbfYwtnOXtDVWG3/t6PPTWAzyFH aDbh98d/lgWSB7PV943TyWOwaeLPoA6C3lufPnaAV6TPuF4ba40O5k44wxJORIki8g0D ztCH+GT4U3YUIEOQkQxRN0MbiWGCTfx1mqHCftSn623tqS7UfYbPtHUVqizzlyKUEylM pQEjqHJGMhV3cH1lRnl7CYhTYW0OUftRABU0qyHQNP96UTyBeH72FBKAV9SnZFIFPmaZ JzAmWYZI2juykBzp0DxktXIBrC0AEm+ed/U4kPnc2FVid5QZle1N67XG+OA7WWbxw3Oh Kddg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709333634; x=1709938434; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dpIP2vZ3D1DN449Uvv4oePTKmqkMaAo4lO0G7OfiEqk=; b=APHNFSjIbx16U60a0wvJnyrW0yJsVOYhLRbPvRLjErcOrLyIDHpxwBiHa0tmoY18aR /oOaIGhAaO/T/KHiRMkb/Rp0oiwF5gm65okh0kQOEc4ep+Z2a5qBJYQWoh+fQIkS9YNY zu53tueF4HdGlqJJeGWXyBSWrnD2blLHFhWv9FxWnMXlqKBIqQc5wzwkCHMMLucsOMe4 JDop7l0QNSuAheD9nmPd73JXHUmR7d1OurZGd61mhNc5vtC0OAkUra/J5p/0H6PTjNHN k17NYDmIv4W0YLfUnxNYnBqPOmYcYhSOqqqpb9zLAJHiv4iZj+CYT/dUmgaqTgOWP87M Vi4A== X-Forwarded-Encrypted: i=1; AJvYcCXZeyNxrxxKFu10xL8DijSuKaCj2Xjgc5SOs/EeDEaSf66OipO7i8LIsI1K4m8FZirL5GCe58qS33g+Y6UAm2C+SgL20QNYyaIdbxyqOHmYPw6/E8EKkca6W1DPhhWjRY/gnX9nlJq4bRbF1qY= X-Gm-Message-State: AOJu0YwPZW/iKrSqzKFTTX0iHisWipqZk50U5oHhL+6n5/+vopflZUJA +OxlVWI0T2cBx0FlPDbu5qHZ6TZOFJ3PGXn7tZ5Zsl2koaq4qJcc X-Received: by 2002:a0c:ef50:0:b0:690:49a9:15d4 with SMTP id t16-20020a0cef50000000b0069049a915d4mr3001314qvs.7.1709333633736; Fri, 01 Mar 2024 14:53:53 -0800 (PST) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id ma6-20020a0562145b0600b0068ff983299fsm2343200qvb.45.2024.03.01.14.53.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 14:53:53 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfauth.nyi.internal (Postfix) with ESMTP id 8635C1200043; Fri, 1 Mar 2024 17:53:52 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Fri, 01 Mar 2024 17:53:52 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrhedvgddtfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhquhhn ucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrth htvghrnhepkeehfeeltdfgkeelleeukeduiedukeffveelhfekleeuieethfegiefgfeej kefgnecuffhomhgrihhnpehkvghrnhgvlhdrohhrghdprhhushhtqdhlrghnghdrohhrgh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhq uhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedqud ejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgv rdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Mar 2024 17:53:51 -0500 (EST) Date: Fri, 1 Mar 2024 14:53:02 -0800 From: Boqun Feng To: mathys35.gasnier@gmail.com Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Martin Rodriguez Reboredo Subject: Re: [PATCH v5] rust: locks: Add `get_mut` method to `Lock` Message-ID: References: <20240301-rust-locks-get-mut-v5-1-c5131dbbd3c4@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240301-rust-locks-get-mut-v5-1-c5131dbbd3c4@gmail.com> On Fri, Mar 01, 2024 at 06:33:23PM +0100, Mathys-Gasnier via B4 Relay wrote: > From: Mathys-Gasnier > > 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 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. > > Reviewed-by: Martin Rodriguez Reboredo > Reviewed-by: Alice Ryhl > Reviewed-by: Boqun Feng > Signed-off-by: Mathys-Gasnier > --- > Changes in v5: > - Adding example > - Link to v4: https://lore.kernel.org/r/20240226-rust-locks-get-mut-v4-1-24abf57707a8@gmail.com > > Changes in v4: > - Improved documentation > - Link to v3: https://lore.kernel.org/r/20240222-rust-locks-get-mut-v3-1-d38a6f4bde3d@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 | 38 +++++++++++++++++++++++++++++++++++++- > 1 file changed, 37 insertions(+), 1 deletion(-) > > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs > index f12a684bc957..345ca7be9d9f 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,38 @@ 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. And because `data` is not structurally pinned, it is safe to get a mutable reference > + /// to the lock content. > + /// > + /// # Example > + /// Thanks! But please see below: > + /// Using `get_mut` with a mutex. > + /// > + /// ``` The example looks good, however, I was thinking about something like: /// ``` /// use kernel::sync::{new_mutex, Mutex}; /// /// let mut m = Box::pin_init(new_mutex!(None))?; /// /// assert_eq!(*(m.lock()), None); /// /// Mutex::get_mut(m.as_mut()).replace(42i32); /// /// assert_eq!(*(m.lock()), Some(42)); /// /// # Ok::<(), Error>(()) /// ``` because, this will also run something instead of just compiling a function. > + /// use kernel::sync::Mutex; > + /// > + /// struct Example { > + /// a: u32, > + /// b: u32, > + /// } > + /// > + /// fn example(m: Pin<&mut Mutex>) { > + /// // Calling from Mutex to avoid conflict with Pin::get_mut(). > + /// let mut data = Mutex::get_mut(m); The other thing I notice when I try to make the above example work is: `Pin` also has a `get_mut`[1] function, so seems we have to use `Mutex::get_mut` to invoke the correct function, I personally want the following just works: m.as_mut().get_mut().replace(42i32); and looks to me the simplest way is to change the function's name (for example `get_data_mut`), and we can do: m.as_mut().get_data_mut().replace(42i32); Thoughts? Regards, Boqun [1]: https://doc.rust-lang.org/core/pin/struct.Pin.html#method.get_mut > + /// data.a += 10; > + /// data.b += 20; > + /// } > + /// ``` > + pub fn get_mut(self: Pin<&mut Self>) -> &mut T { > + // SAFETY: The lock will only be used to get a reference to the data, therefore self won't > + // get moved. > + 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 > >