From patchwork Mon Feb 12 14:13:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathys-Gasnier via B4 Relay X-Patchwork-Id: 199841 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp2454604dyd; Mon, 12 Feb 2024 06:16:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IFvMjq+1SuCato7G2lkRwSYbTJn9qNYGSb6zQ8Z7MboOWmuv0rzKtrbOkiaZD8akhIW+onx X-Received: by 2002:a05:6a20:d817:b0:19e:aa84:7486 with SMTP id iv23-20020a056a20d81700b0019eaa847486mr9160786pzb.19.1707747396351; Mon, 12 Feb 2024 06:16:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707747396; cv=pass; d=google.com; s=arc-20160816; b=FUBJIQVa1tjUuQccTPn7YqrqM+vC4xcL9Fad+Iux48mDBhYFsAa3WfU5O7VeL+CcYq tkgL159YVhXfLe0oizn2h+tqvKmkTD/vzT25bBfhy061KBAuMTsSORHZQReHo0QcowXQ uim4ApK0wmQoe62KydXiyGQ0ELQkQ4xak/0cQz2DAAVspZYhZjobfBMtbCl9Vq9IQE93 Y+iqrbrtXT+lKSpIk0UYTnYzzK5/xr9oURFqTyVfy3HeQKzj3WUkgBgWXLh+D5rRHxhV wBy1JLifSwU4C0p6RObE7s2iSi5k8lK19ms8ZW3ILQyM+zZsCiCzIiS8Apcwi9bownm2 rYIg== 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=jaHhWDVacBlsPWpMka5AmC9S9lBuhjz/kZtdO+LyF5g=; fh=my8R4Bl800Ceh/syuP1vd0J7o4/uUFA0mv1/IBRAwyE=; b=OJCgYeNB3B9vM0qdHmi3LkLMuAJsGmIzja9lmOHmMTxbpa3ojLKH8f+0vEL3JxCG1i RyWC4BLxnwNzCO35k+IE9BVbKKg5FCUoKYENRGnhfmKm2aK4rc63I/vq/SorolNeT0sA D9Qq5rwK+mlIQ+8R28v+17K+V+r0kWMA8pzqXlAL419JKdKu2zlRAMVPmpctqH/a0vnp BPAyUyXRegSbP5Tk8fFqLmCkGmNbrcNLwt1dwB5pn8bdEjSzpY6EUd735EP1ULjZeJUl A11WxldUveoYkhObcW8rGCkaZmzpfAISQQXZc3AhyviXV3cluF0efaffnB9wDhavAe0J lMcA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QXjPeDXT; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-61808-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61808-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCUvupL4hUpYUoodloo9t/W6LQm7iC2X64j6AwTzTt+bPL2wOqiC4r1El8cMybRm7I36fpLcp3OvOr0B28SHu2YipTUPoQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id e22-20020a637456000000b005d7957d53e6si291612pgn.678.2024.02.12.06.16.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 06:16:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61808-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QXjPeDXT; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-61808-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61808-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 03E73287EEE for ; Mon, 12 Feb 2024 14:15:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B71A33A8DD; Mon, 12 Feb 2024 14:13:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QXjPeDXT" 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 124D725740; Mon, 12 Feb 2024 14:13:22 +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=1707747203; cv=none; b=BqTR5AdOKtqw0zagNxjq26utXqo4bQhHwAgtz0CxpNCkXPOtW2ps9qPwqvSlEv+ZRnOhOevtSuAsTMgU9yYChnrj54rXN6gtpUhXmbOpP3FbySFGEhupdCR0WYDtpNRjiJQ51FSd6DMvjyfuYv3W7jpSSGFpasos0T0YI4s8R5Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707747203; c=relaxed/simple; bh=MRoovGwQKiJetb5e8HWBFCiVQj6gfEzlk51Wmz0W+y4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=E9uyfrYgVkHaha4kV7ZVwkmdMq+2NHO86+5POTFhjJxjrIbC2gV7J2IMuPf8qWp6+D+6YDbxu5zjoY78mH5Z6hHVMEtGBhwE/EJYQx1nUlRHrWvAbl8COyoLz4tQrU8+T9H2nL1q8Yt9tueo5G/N3raWJ2nhD010AMNep/RS+UQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QXjPeDXT; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPS id 6FE8FC433F1; Mon, 12 Feb 2024 14:13:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707747202; bh=MRoovGwQKiJetb5e8HWBFCiVQj6gfEzlk51Wmz0W+y4=; h=From:Date:Subject:To:Cc:Reply-To:From; b=QXjPeDXT5ZZSDYX55tztYDpV5rZooJplFHdHa0UOVPBwQJlDvuy3VAi+Jcvf9Hsy2 xvz2GmZKdWhJ80Wbm+Wk6iO0p2toyQbIiIufcvQ23RgJLG6SnmzhJ417caBIJXqGEW s7xegs6IZ+Y8tdlGl2VoizIdhfipW6iwc0sseWVFAlyBrHmBfBuOlRfCsYoFuNZ49W qWTKVo6g4+e0YN0AI2tVxLVjngOG/pjG97JrEzN54iylXD5XfUDCQg62QoKENcGVSj Dry8pii2TMAKWr8RcZdOe5zdRG8LA5qJ91cGLSVRj/tz3sXl2AIZHvZZ9vWK2m7aqO e26Vt6V6203oQ== 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 5B3FBC48297; Mon, 12 Feb 2024 14:13:22 +0000 (UTC) From: Mathys-Gasnier via B4 Relay Date: Mon, 12 Feb 2024 15:13:22 +0100 Subject: [PATCH v2] rust: locks: Add `get_mut` method to `Lock` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240212-rust-locks-get-mut-v2-1-5ccd34c2b70b@gmail.com> X-B4-Tracking: v=1; b=H4sIAIEnymUC/22NQQ6CMBBFr0Jm7ZhOqUFdeQ/DgpQBJgI1bWk0h LtbcevyveS/v0JgLxzgWqzgOUkQN2fQhwLs0Mw9o7SZQSttFNEZ/RIijs4+AvYccVoiWqNVpUl RWzWQh0/Pnbz26L3OPEiIzr/3j0Rf+8tpdfmXS4SElssTdbZs2VS3fmpkPFo3Qb1t2wc70JFNt AAAAA== To: 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 Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Martin Rodriguez Reboredo , Mathys-Gasnier X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707747202; l=1549; i=mathys35.gasnier@gmail.com; s=20240118; h=from:subject:message-id; bh=4WHIEGc6TIVbfvh7PeNOQmNjpDZmYDavVMoHaIlodHI=; b=q7QD3UdUQRQXztcJOdSgVPNrRpVwVF4EV+NxuHzQ0fNNhCQfFLrtc7vL1HeBf97d+i2Pm7BTo b6bOFj/3Ch8DHtpCfnUFM/cB2BPCMOXl2I+OUl/daVwpCYgt/NeZfrO 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 Reply-To: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790702933763151934 X-GMAIL-MSGID: 1790702933763151934 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 Reviewed-by: Wedson Almeida Filho --- 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 | 7 +++++++ 1 file changed, 7 insertions(+) --- base-commit: 711cbfc717650532624ca9f56fbaf191bed56e67 change-id: 20240118-rust-locks-get-mut-c42072101d7a Best regards, diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index f12a684bc957..d15af6625d01 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -121,6 +121,13 @@ 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(&mut self) -> &mut T { + self.data.get_mut() + } } /// A lock guard.