Received: by 2002:a05:7208:13ce:b0:7f:395a:35b6 with SMTP id r14csp31064rbe; Wed, 28 Feb 2024 11:03:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWzQNL/xWisTWt4gFT38uYWuEp+8af6/YZdXKUkcKBaVzm9tXMeBvQpOVufGUygkYY+go8D3V/8agkSqACdToVGumkPRmhRkxR8bwC0yw== X-Google-Smtp-Source: AGHT+IFwqB5anSXLeoqyRqaw8LaNO+CoDUyUwPTwL/RcClVeSDKhMaxx7wqY9kUDUMaKtD/0iFs9 X-Received: by 2002:a17:90a:db54:b0:29a:1708:9878 with SMTP id u20-20020a17090adb5400b0029a17089878mr46525pjx.38.1709147020507; Wed, 28 Feb 2024 11:03:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709147020; cv=pass; d=google.com; s=arc-20160816; b=iCKYchM8DYyK3taEtsJlHUY+fUBTgjWFmLALR8ZxvD7DYNMUHxoOoJvQ3UUz2s6Tum 3Gk5WJkGPlG3wqEnBReUf1kyaWpNXsU95BDISYOYbD9xqYc+J0HLFp93IYM6Fvb4iv3i uttkz+enfyEpYm409/BeFvwG85IM0EafqNswrbebIbhGTP7a1HbdBPZ/yYQqQcrU8hw/ HMboBMJPTnDKFCwmagUmMXV3fXd9x9+TPTbuW3wtiDgWdgUYFNywhpfBxADIklwyFM7v oZ1p4RfMdFg/qZ1K2Ns7poOZbC0NZsU9h0gddaemN6moK5sgq9iM6BSwEOE+39zzBE3+ /vXw== 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=7rfc86T7k5b0i963LoD1bGQIFebMJk/S7CVsLQAk4jg=; fh=QKqi2N+22UKh/AYqHtmJZLEvLwkGz8i9Fuq2ONT0O8c=; b=dMZ6nvW0ip7GtaFGcrQEUyrjwKKy9vTaLfylILWVJvdqNO4epIDpTlzL27T+Uw8hiZ G58jefBpUcSJ4tZC65kztbs/qjcmUAmt1ZtOwQN4/8QepZAv3yRaoIdv6upc7LqIHOUk M0C4HDHMdj0u6VBV3E3TTBZJCuaAeSDL6bROXfHVGfhFOE2d2gjfwCEvN1GRAfHbEE3H zFka0j8P5D0q+ymJ+ETc0NjgJu7t3jxhcWCrIfDaEDN1vFcLuSfYi6TtdtDZsh3M8wAp /fzgns3mBp2gG1OTi9JyuAV+BVHeTFZ4hwvOUDfeCxYWTPQHKh0L4mAo9iqXTQHHPDu2 tKKw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VQyV4vf4; 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-85560-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85560-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id u38-20020a634566000000b005d8e401b4a2si94351pgk.801.2024.02.28.11.03.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 11:03:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85560-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VQyV4vf4; 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-85560-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85560-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 30A79B26F63 for ; Wed, 28 Feb 2024 18:52:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 00D607440C; Wed, 28 Feb 2024 18:51:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VQyV4vf4" Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) (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 7C7AD71EB3; Wed, 28 Feb 2024 18:51:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709146313; cv=none; b=hiJdAt397LS0CSHzsiVoWvi3nT2EyAY2hdx2O3ieTEVeJIttZYnGiXkpfIZTV3+KV6Ntxorfwxoaa5NnsJK1Iwb58mGGXHNHTiCzZv899TF6VH04e/gjv9oII4M89aBz8WgyJKOlFzSXjtF3UX1SCk2cM7VAoUSlfP4/3CO8hzU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709146313; c=relaxed/simple; bh=PcSdRbgwcOcuK3W1kpU42Akdd3/7NahCewrPceYtuUg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=tq7yRPx2meU9Km9lFSemlAX+uG6LXvBPdvjtTi4wfQl9pL4OkHWMnPAsWLB7IUXEZ1Jq4ASU6w3vJRJqgQnOkjxNqqFHJ3+u/CKo/DzrVpxthm5XuiZFBIgRFEq6GnujB46FJqR03yW/Ca7bdXQ9aP21OonoIPO5H1nw+AgmtFg= 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=VQyV4vf4; arc=none smtp.client-ip=209.85.219.171 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-yb1-f171.google.com with SMTP id 3f1490d57ef6-dc745927098so93130276.3; Wed, 28 Feb 2024 10:51:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709146310; x=1709751110; 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=7rfc86T7k5b0i963LoD1bGQIFebMJk/S7CVsLQAk4jg=; b=VQyV4vf49GoK7prnqdYclBeSiJJb9fJmEXIp54/jVxXzyqsDAWaOrvzvDt2J3Xn6wU /p2mIdlA5TuvQLwpvg5s5hXd79qP1Hf5aeO3fc8nMoDIL0qMy9kdvjI9qsqcNJg5JOU6 oAmjJYNItKZwp63aaBK4X5+h+QAw8+uN2kxdCMi1IBGYD7wIdz6Rj2dD0uNeYTp51J5U ff/gxZQUguKiK/RAcEUig5cItvnHkVe1A35SUDViol6uFLRahrnzRQ77P853tI5XWp7n kidRDCnwopr8dgNBKyGQ5KNwHMbhtHqGjlzB1RNBpDN7FxWGMOH2OEXe4klLLUDEHDOM zsoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709146310; x=1709751110; 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=7rfc86T7k5b0i963LoD1bGQIFebMJk/S7CVsLQAk4jg=; b=fPqr6coCpQH6xn4+FulcK3i8owge4cafOePPNNIpjWdoQhyfN3bEH0d44mUENtUh5U 86ecvdv7K8L0dUPqIOEeLwIFkHiLZZdF+jP5qs8ONtJGXyUwsoGrJW/p4eDGOPuNHDCl Q2IaGuASc4nuj+n5DBl/G20VEjgrpzSIKGHJuAebRn8GMwLHOomKl8JPu6/Rxe04csPn cd25OClDt4Yksz89WwNYLARtgaya7kbe3QPxRzE6YWxYfydzZyg/chLn4lPbmeceWAa2 bOcMa8fkZZERde4mUz+xzVfYOvQPul+i0IHAP/nSAFB1OeK17sdHpH2ytcYHIRZL1/XM C5iQ== X-Forwarded-Encrypted: i=1; AJvYcCWMrVod8e+EJuMIQFgAVV/WGCrisUn6YvHzBj+tT9FzQ/FMOdi9Fh6sMzX6exmfIRM/YyzY5CWIloKDGEHqvR3HOwDokfjAc7pnNwHAgN6MZR9A4LH2CYMAuuMiuWoCKktlAxxcoKt0JaeqmEo= X-Gm-Message-State: AOJu0YxT168FSMF3voXpLW+9XyoID9fJxbNjPWE5kISEPlMnliSCYxJi jU7I8yJ92M1pdKKuxtlte1enZToR12FsKKFJnHJe4ZH9lzymKmpk6szC7ZFG X-Received: by 2002:a5b:602:0:b0:dcc:383b:e48d with SMTP id d2-20020a5b0602000000b00dcc383be48dmr69471ybq.26.1709146310402; Wed, 28 Feb 2024 10:51:50 -0800 (PST) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id x4-20020ac87a84000000b0042e390c9804sm25987qtr.6.2024.02.28.10.51.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 10:51:49 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailauth.nyi.internal (Postfix) with ESMTP id 743A827C005B; Wed, 28 Feb 2024 13:51:49 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Wed, 28 Feb 2024 13:51:49 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrgeejgdduudefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpefhtedvgfdtueekvdekieetieetjeeihedvteehuddujedvkedtkeefgedv vdehtdenucffohhmrghinhepkhgvrhhnvghlrdhorhhgnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhh phgvrhhsohhnrghlihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunh drfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 28 Feb 2024 13:51:48 -0500 (EST) Date: Wed, 28 Feb 2024 10:51:06 -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 v4] rust: locks: Add `get_mut` method to `Lock` Message-ID: References: <20240226-rust-locks-get-mut-v4-1-24abf57707a8@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: <20240226-rust-locks-get-mut-v4-1-24abf57707a8@gmail.com> On Mon, Feb 26, 2024 at 02:55:47PM +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 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 > Signed-off-by: Mathys-Gasnier > --- > 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 | 17 ++++++++++++++++- > 1 file changed, 16 insertions(+), 1 deletion(-) > > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs > index f12a684bc957..a563991bf851 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,17 @@ 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. This looks good! But I forgot to mention this last time: it'll be good to add an "# Example" session which would act as both a document and a test. Do you mind to send a follow up patch? Anyway, Reviewed-by: Boqun Feng Regards, Boqun > + 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 >