Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1149135rdb; Fri, 9 Feb 2024 11:44:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IHNTOvLT/zXYBZh/ZESTH1Zj1aCtU0m8iJ8XAksKVTgRPZvpSNvWm9On0ZyKsJzEmqaq8SF X-Received: by 2002:a25:ae07:0:b0:dc7:4318:8c24 with SMTP id a7-20020a25ae07000000b00dc743188c24mr129681ybj.52.1707507898043; Fri, 09 Feb 2024 11:44:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707507898; cv=pass; d=google.com; s=arc-20160816; b=kwvnX+fKN3Mb3v8GeBFuwKVhRCjVxtWE62dXBxJPkaIZURHwamBRc0Pm3qvFDKjrcR vi8qihw13m0gP8mZ41J/uBx0RhluKDMvlFdMha1MeM0O2t+Tamjj44327E9MjY+5KVW+ u5PUHJ5HCkdsRN6UgTTcNgNGd5WT2JzgNEKOe+rEMXl2/3Vvs1D+r7byDoS9cyEIVxYl P7gcexhaI5nGOIVRDATNsCYJC8KMGMyCv2QDDRT0n3/HDkhWMITw7TIcEA5PIzIYtACe QA0KOK8nsYxS4URFeewh6b3cR5szvbmX8aG5KERIDFFnuQy32Atxyi57Gdw/E9EonWk9 WeJw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:feedback-id :dkim-signature:dkim-signature; bh=Uq0f/q/RzaK4737YBI6RkZe/+xPjtPBgR+88IW4uEMw=; fh=direS0pFmhUhjy7gJRtT2aVUyTsDmuLWusmWPncnFI4=; b=XuXrmEFHaEn9Vy2eMeTqor6eaxyTNkXJAcsCywwDPoGBHdt8NIYLLRhThpUmmbtNS2 M8jSI2oX0lQlUN38SghCzAzj9NYbvVKrLlB1OoRdbqURbSz4TdLS08x9ovNsbYzgU7sv epolmRzzGYApLn4/15sUmO+HHo0DsvQYIbrGyOlcX0OzcuLkEGMyNhfQnJMQBXkbmpF0 cnbfUnW0g4kcU1+fL1XPVAeizpmqCwvS03cBKoHeZaJcHTMKUvLuVzkGu3V241RvsM4P SSyHtdowxysGLTqN1+ay/cCjY0uKmgrnPmvFUjNMMrpASyoKHzf6lSGQFtb9Hm4tzgri TtMQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ryhl.io header.s=fm3 header.b=NjmM4v4B; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=ei+3VEb5; arc=pass (i=1 spf=pass spfdomain=ryhl.io dkim=pass dkdomain=ryhl.io dkim=pass dkdomain=messagingengine.com dmarc=pass fromdomain=ryhl.io); spf=pass (google.com: domain of linux-kernel+bounces-59901-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59901-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ryhl.io X-Forwarded-Encrypted: i=2; AJvYcCVMmqpbMhWh1HJYCSPPW5iEOQF4jVI8V5mUI3GoPXTyb8JDWbKoO6dNen8x75Vp4OhIUDXi00W3PzsO00DXyI1d8e/hdA9ZRLr/78GLIA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id c4-20020ad45ae4000000b0068cd144e023si2807120qvh.329.2024.02.09.11.44.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 11:44:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59901-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ryhl.io header.s=fm3 header.b=NjmM4v4B; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=ei+3VEb5; arc=pass (i=1 spf=pass spfdomain=ryhl.io dkim=pass dkdomain=ryhl.io dkim=pass dkdomain=messagingengine.com dmarc=pass fromdomain=ryhl.io); spf=pass (google.com: domain of linux-kernel+bounces-59901-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59901-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ryhl.io 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 C73321C22093 for ; Fri, 9 Feb 2024 19:44:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 68A4F1272CA; Fri, 9 Feb 2024 19:44:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ryhl.io header.i=@ryhl.io header.b="NjmM4v4B"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="ei+3VEb5" Received: from fout4-smtp.messagingengine.com (fout4-smtp.messagingengine.com [103.168.172.147]) (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 52E581272B3; Fri, 9 Feb 2024 19:44:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.147 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707507858; cv=none; b=qBL5Q3/omytDiid9a6pIkSrl9bNAteoQggYok6B6GQitISWlZiYQ8TAF440ZaawWHYDhUA+CmP/5gqluyE+i6jh77cORsBSTZVSrDQjvrskGc+j9v2qNmv7hq9LyXzklqrwhgc3wz/rorNcdZ0VYkVPZulkrrXYiSO4L5DaUlTc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707507858; c=relaxed/simple; bh=aUjVqz9weRqk3nI2+rU6qNFUPRqw/bc0Q2Em0cvvgxM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=fIi/RcArOFvn2ZsU9RJzlLjA7NOwPbCVQKdOZpiw/V7K2HuhUuYn+WHaiXUQeia9a0f0XzbEChShqvklqUHXppXhS1gryT+wKqNfZFzAsQTnAocjR+Rp71ucrUw/sVrDTVv/VTNCyKhCxWo6ekDplE906aDVW6yWjXMDuScxaIg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ryhl.io; spf=pass smtp.mailfrom=ryhl.io; dkim=pass (2048-bit key) header.d=ryhl.io header.i=@ryhl.io header.b=NjmM4v4B; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=ei+3VEb5; arc=none smtp.client-ip=103.168.172.147 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ryhl.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ryhl.io Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.nyi.internal (Postfix) with ESMTP id DC8CD13800BA; Fri, 9 Feb 2024 14:44:14 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 09 Feb 2024 14:44:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ryhl.io; h=cc:cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1707507854; x=1707594254; bh=Uq0f/q/RzaK4737YBI6RkZe/+xPjtPBgR+88IW4uEMw=; b= NjmM4v4BA4Nq5stu7QlEEwKslSyZfdseQr2QrM0ItcP6WzCH4J4KC3CSc7Jl+uK9 2xNL/AhiB9Mpuq6ukEbHKDiRR855Pr3NFRtVgS+r/qOLIdjd8aRDQ0Ecgb/t9+kG kQs5auoaVUrSfDCt7PgKzou2Wum7kNWKBuGrDG4TuMDQk1q4rsf/7bLPZb6j8RdE YsX8jyXIEVbgmjtX0BMy/cWtD6mgWA1BLS2S3aqG+hZVuXC93oja1lqDWvSMf4e0 e99LtWTY5ybp1bxydfyJioEOfSUTR1/9hzS1sU0DieXKoE1KLEwa7gy7CtNlpJWV 2Jxa25tRWTXBT0xJLusMbA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1707507854; x= 1707594254; bh=Uq0f/q/RzaK4737YBI6RkZe/+xPjtPBgR+88IW4uEMw=; b=e i+3VEb5Vt+RYAONVpOYUMzTWlAeDL3tcZ1EbU1wM0DywzNhDSCUUx/vUaON56Web 5yZbwhiKuLwp9Flfeggg9g1VqGrQPBXtqlfrlT2nL4q9w32Z2cZ4zN06MskwX40E DkKWvV3DrSd8f92xS6Ca3hjXFrrOTEUctFWTG3FyRzaAEE0O3jPdEicbVL9kVRF2 O7qm0tT+JyhVvTxPr9zEB6yshuXUmz2b9KkCH2EE/dbunQic1hmj4QrgfVSResa6 NuBtVAQP4mC2K9ofgIrCOjqgB9RFbqiSgUS4ploIbQQw+Eiw+HwOVBjT8l8/Iv2T szCC87Ds70q/RYTQPx+Fg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrtdeigdduvdeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtvdejnecuhfhrohhmpeetlhhi tggvucfthihhlhcuoegrlhhitggvsehrhihhlhdrihhoqeenucggtffrrghtthgvrhhnpe efgfeugfdtgefhueeiudffjefhveeuheeuheekvdetfefgheehtdffuddtgfdujeenucev lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhitggvse hrhihhlhdrihho X-ME-Proxy: Feedback-ID: i56684263:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 9 Feb 2024 14:44:12 -0500 (EST) Message-ID: Date: Fri, 9 Feb 2024 20:44:11 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] rust: locks: Add `get_mut` method to `Lock` Content-Language: en-US-large To: mathys35.gasnier@gmail.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl References: <20240209-rust-locks-get-mut-v1-1-ce351fc3de47@gmail.com> From: Alice Ryhl In-Reply-To: <20240209-rust-locks-get-mut-v1-1-ce351fc3de47@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2/9/24 17:22, 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. > > Signed-off-by: Mathys-Gasnier > --- > rust/kernel/sync/lock.rs | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs > index f12a684bc957..7711c6313e14 100644 > --- a/rust/kernel/sync/lock.rs > +++ b/rust/kernel/sync/lock.rs > @@ -121,6 +121,11 @@ 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 > + pub fn get_mut(&mut self) -> &mut T { > + self.data.get_mut() > + } You can never have a mutable reference to a Linux mutex because we pin our locks. At most, you can have a Pin<&mut Self>. Alice