Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2476265rdb; Mon, 12 Feb 2024 06:16:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IEJQOmn18gU/QxRxl15VL1zZB8tn1NWiPtK/7vtNlXKWJh+OQXtUxMMXOm9mcOg0w7zghDN X-Received: by 2002:a05:6a20:d386:b0:19e:5e80:2802 with SMTP id iq6-20020a056a20d38600b0019e5e802802mr7783263pzb.9.1707747391949; Mon, 12 Feb 2024 06:16:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707747391; cv=pass; d=google.com; s=arc-20160816; b=CWvQ+wDc6aZ6Z/zFirw4S6HdleVIpA2h80952UCQCdfZs2y6zxhSK1zSEb9tPECmYl 6Jns0Ax5RUP4ozu5EnRUPpla1/kdXIIghwaZnh/B9EuRMWPFbW/hNEF5Qd6V9qti97GE Cqy8xLKuyvnDWmr99ppPDNoKpkR3KQv1Ermi6VLUfs0ML7ErakaevrcIXdOy82Hriled DY+HbCIbH3SaXNQJO3BBRq0gYHZBuwKwNPAKTxb73aqPQjUkmoB/qZHA58Wb7fXcMseL 7Ty/pWTsONSkxgqVOMP+1kAbd33sajfLlFBhXAAaHOp7/p/U450l7b/XSNaF+DTdWhuW cnEA== 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=hLLqmLLAs2vbzQYzRG9khO5rs2v/BLHIomoEGuwMkhQ=; b=0aQSGwuoWlNaF4Ogv4Hs5XZ7TxR9EyCuLPF8pwBQe5akIkeH5uDIb6lb5K/FvUDC9W QqnAL6f1W3zrVqna5fCIX/S3cUODZcMH+NRTHpJaIpGN2CSnnd6EKfDsa2YfV9nXmlOb jqfrAXOA3lMevO8TTGPqRhpcgdNxS32o+2KQo3+qUKQYUrru64V6xNC5oHrFc46+M8Xv K8YLqza6knYQxp+DKWJ1As5l0C/91x1ZLg85+HRUF+njqLnI1RGjcKegLi0/1DTrVaJP jqeZmERGlipVoKxiHt+1lwANyD7WlAAWaX7tSZ16oyR8FkIWqXtaoWbbrqra+EinZm+e ZvNQ==; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61808-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCWSNREcyzdSNeGXssvNbEfZmYAxp5a17r1eKJ6CGOjF5keFg4NYmYCxQ3JBcFZMGoVsWneq64cXr/uglnoyCgZ6jG+uAP3mbXyQQvDnJw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id lo17-20020a056a003d1100b006e0335e3eb2si5071159pfb.227.2024.02.12.06.16.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 06:16:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61808-linux.lists.archive=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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61808-linux.lists.archive=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 94C0828773E for ; Mon, 12 Feb 2024 14:15:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E0BAF3A8E0; Mon, 12 Feb 2024 14:13:23 +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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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: 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 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(+) 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. --- base-commit: 711cbfc717650532624ca9f56fbaf191bed56e67 change-id: 20240118-rust-locks-get-mut-c42072101d7a Best regards, -- Mathys-Gasnier