From nobody Thu Oct 2 07:46:26 2025 Received: from mail-qv1-f41.google.com (mail-qv1-f41.google.com [209.85.219.41]) (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 2D83227AC57 for ; Fri, 19 Sep 2025 09:13:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758273184; cv=none; b=iSRp8mpn/bAzUezH8JjOBFnC49NrZz4NPLlwmpnxf+kF3WGWM4jL9OodwHuNR5wS5IUF1K6Ltjpxu9oflAbsO0Os0PgXlxiOEysXQeOtyBNtQQNUv43j0cKQhtc4wNiHrAcKWIJaWKGdmiCQJec9aMz8ccAFO9/qbRz+OsNGoK0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758273184; c=relaxed/simple; bh=QweTAqxNpSrDrK2q0/RR+kdBUV2hZvkvOnc1TJn8Yck=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NP2J82Rkj0tO8lDehiJYvtzT3DAXlcaShoaozrGnQRSxv8qv92eai9TxAWVL6wyc3lc/421PHBt2Aoy7w+yjQAtSQuBGNZ0+nZTAg+zG+st7AthcfQwHKsd9ionNm/8nIBIym1UqHeGFjuVp8FSbFqAJ5IB+NQcLVEc/3odDmWo= 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=hVPmUjVX; arc=none smtp.client-ip=209.85.219.41 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hVPmUjVX" Received: by mail-qv1-f41.google.com with SMTP id 6a1803df08f44-78ea15d3489so18630476d6.3 for ; Fri, 19 Sep 2025 02:13:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758273182; x=1758877982; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=K70gfC2VGaXawlZrrSZ8hp0QF5eghAVmIXEzCCUkbN4=; b=hVPmUjVX8qD57Te8PtzQaXb7pzBCYUxYaT3AmPJBcIgvd1tDfMhYhZjEmMPWZiaLEN 91vtiIAhkGHfCFl0gV/2yFN/y23ek1ZjvYmsOIyib5wa461d8v6B7hIqdeRfNVMo+P23 yxeOnXtX2tf8EZY0MD3PPX1Fo8bSzHpE3FwLYB2xMWZtE6Lsxd/TDK0Gl89WBIgxs9Sn TWhY9zUmpV9cLSWB0mto1XtmyatUBIhboUcks0AaUhdIOCUI1tz5xI85IfAy0Gz+BN2J 7Emn7lx0c/fJHle5K9pQ8qnVkIrtQt/NLGZt4/Xw71tCkOLxetOZ7jxRC9sWaFRlwp+g /68A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758273182; x=1758877982; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=K70gfC2VGaXawlZrrSZ8hp0QF5eghAVmIXEzCCUkbN4=; b=A1x3nmVT99V4ADDC1SYTwY0VRVFsW0uWPErYZqJ59/Lj0fuZGTpSeR85RApJV4US+W cqydSkJ0T8SegTQ6a4/pm1uTsRr6BPOhKfmZjUfmFoNkdCU15f9HLyWcWtW8mepDb9YH alMBfpTMlwCRgiJJPk1WglndLp1ZdDh5u0UPtXmxPddd5eaOhlMG1Q9KqrRSs4+gfh9/ mqDNIwFVmacF7wiw4Jc/1kSDa5KjaVw/+gXyXoXYO4L2ES32uMAgaXDzOSyBcf9sHiPh o21fCYwgJ09mGAsFzQ+Hg57u1sMQgq3aAi3OwstjUKZHeFwJk7I7IlA+oWVZU4b2v8wr skQg== X-Forwarded-Encrypted: i=1; AJvYcCXEdWGJLjHw9kHW0Hvh1b07RKeAFga03u9WSYxqSpCE9S4WvzZ2TfXMYI1QUqdoi7pK8VQe2qFGqc2pC18=@vger.kernel.org X-Gm-Message-State: AOJu0YygZtUj/xRedROMpY8XQ+f6Hrh9KHwcIKkFQLOGp5y1lmAnsXn+ ojJjTxj45bAKdVSMrvUBkNla3kqpEvRDJBgmxJ+oc+GKxQoXkXd9loBO X-Gm-Gg: ASbGncsO4ctJ260FHe/lWDxva2UQJoJUJIjWNOylOICxABVY2eevfD915Gzs3LiWhDf EOLmrNNHj/CjkvMkst0dJ5Asx0739mG6kn5CMBFk87bD6UEa8tkVJ2fH2ot406x6QuTysOpzMK9 xWtkmzU4pn13IIsdU4F5ZcdwR/Ovb0Zm1TrPMo8sWW/SInHgNlh9eJdmeSLGzbLZC4T4IeXR92T G7T5be8Q+jXOHXYGETnklc2czksdb5LmqFzb1cNPUZmiEqUeYRikhRKJTnvM53+qOgzVmzj8DiY 4TuFDeoJSG/p3ZHJiGte24uLOKVqy5hFWZH64XJ8WmxBWMALMcEzrtpaBE5/9eKJG8M/NECEimt fiNeiThixWog6Gl290+Cn1jk4eHFmf3YuR9sNuy2w+4CmOWtfo19BfJSeCLRceBmCwhnTOshfdX ypVBY7uB7Yr0GZ X-Google-Smtp-Source: AGHT+IENO3y1I+WdQckOF9/GGnaAMQ+ul1aKo9ejx3/ulS4DRfGqgjIdtHE0clXgDs5HZit1Tjxilw== X-Received: by 2002:a05:6214:29e1:b0:794:6621:6d18 with SMTP id 6a1803df08f44-7991d549c82mr24827026d6.51.1758273182126; Fri, 19 Sep 2025 02:13:02 -0700 (PDT) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-793474ac8dasm26073756d6.30.2025.09.19.02.13.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 02:13:01 -0700 (PDT) Received: from phl-compute-12.internal (phl-compute-12.internal [10.202.2.52]) by mailfauth.phl.internal (Postfix) with ESMTP id 3316AF40066; Fri, 19 Sep 2025 05:13:01 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-12.internal (MEProxy); Fri, 19 Sep 2025 05:13:01 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdegkeekvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhquhhnucfh vghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrthhtvg hrnhepgffhffevhffhvdfgjefgkedvlefgkeegveeuheelhfeivdegffejgfetuefgheei necuffhomhgrihhnpehkvghrnhgvlhdrohhrghenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghr shhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvg hngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvgdpnhgspghrtghpthhtohep vdefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehpvghtvghriiesnhhfrhgrug gvrggurdhorhhgpdhrtghpthhtohepmhhinhhgoheskhgvrhhnvghlrdhorhhgpdhrtghp thhtohepthhglhigsehlihhnuhhtrhhonhhigidruggvpdhrtghpthhtoheprhhushhtqd hfohhrqdhlihhnuhigsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhi nhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplh hkmhhmsehlihhsthhsrdhlihhnuhigrdguvghvpdhrtghpthhtohepfihilhhlsehkvghr nhgvlhdrohhrghdprhgtphhtthhopehpvghtvghriiesihhnfhhrrgguvggrugdrohhrgh dprhgtphhtthhopehlohhnghhmrghnsehrvgguhhgrthdrtghomh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 19 Sep 2025 05:13:00 -0400 (EDT) From: Boqun Feng To: "Peter Zijlstra" , "Ingo Molnar" , "Thomas Gleixner" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, lkmm@lists.linux.dev, "Will Deacon" , "Peter Zijlstra" , Waiman Long , "Miguel Ojeda" , alex.gaynor@gmail.com, "Gary Guo" , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , "Benno Lossin" , "Alice Ryhl" , "Trevor Gross" , "Danilo Krummrich" , "Andreas Hindborg" , Alexander Sverdlin , stable@vger.kernel.org, Adrian Freihofer , "Paul E. McKenney" , Boqun Feng Subject: [PATCH 1/4] locking/spinlock/debug: Fix data-race in do_raw_write_lock Date: Fri, 19 Sep 2025 11:12:38 +0200 Message-ID: <20250919091241.32138-2-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250919091241.32138-1-boqun.feng@gmail.com> References: <20250919091241.32138-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Alexander Sverdlin KCSAN reports: BUG: KCSAN: data-race in do_raw_write_lock / do_raw_write_lock write (marked) to 0xffff800009cf504c of 4 bytes by task 1102 on cpu 1: do_raw_write_lock+0x120/0x204 _raw_write_lock_irq do_exit call_usermodehelper_exec_async ret_from_fork read to 0xffff800009cf504c of 4 bytes by task 1103 on cpu 0: do_raw_write_lock+0x88/0x204 _raw_write_lock_irq do_exit call_usermodehelper_exec_async ret_from_fork value changed: 0xffffffff -> 0x00000001 Reported by Kernel Concurrency Sanitizer on: CPU: 0 PID: 1103 Comm: kworker/u4:1 6.1.111 Commit 1a365e822372 ("locking/spinlock/debug: Fix various data races") has adressed most of these races, but seems to be not consistent/not complete. From do_raw_write_lock() only debug_write_lock_after() part has been converted to WRITE_ONCE(), but not debug_write_lock_before() part. Do it now. Cc: stable@vger.kernel.org Fixes: 1a365e822372 ("locking/spinlock/debug: Fix various data races") Reported-by: Adrian Freihofer Acked-by: Waiman Long Signed-off-by: Alexander Sverdlin Reviewed-by: Paul E. McKenney Signed-off-by: Boqun Feng --- Notes: SubmissionLink: https://lore.kernel.org/all/20250826102731.52507-1-alex= ander.sverdlin@siemens.com/ kernel/locking/spinlock_debug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/locking/spinlock_debug.c b/kernel/locking/spinlock_debu= g.c index 87b03d2e41db..2338b3adfb55 100644 --- a/kernel/locking/spinlock_debug.c +++ b/kernel/locking/spinlock_debug.c @@ -184,8 +184,8 @@ void do_raw_read_unlock(rwlock_t *lock) static inline void debug_write_lock_before(rwlock_t *lock) { RWLOCK_BUG_ON(lock->magic !=3D RWLOCK_MAGIC, lock, "bad magic"); - RWLOCK_BUG_ON(lock->owner =3D=3D current, lock, "recursion"); - RWLOCK_BUG_ON(lock->owner_cpu =3D=3D raw_smp_processor_id(), + RWLOCK_BUG_ON(READ_ONCE(lock->owner) =3D=3D current, lock, "recursion"); + RWLOCK_BUG_ON(READ_ONCE(lock->owner_cpu) =3D=3D raw_smp_processor_id(), lock, "cpu recursion"); } =20 --=20 2.51.0 From nobody Thu Oct 2 07:46:26 2025 Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.179]) (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 D2A012FDC4E for ; Fri, 19 Sep 2025 09:13:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758273190; cv=none; b=LTCGXcPoeGUvfxFirdcRVM20thS+2BeH1NKjjCQjQLkMFISgnBoMCGmZLG92SU9WY8n6nnge2h2hgMxO5jJ32Q27M5KCRhsnb1pRdk1TFT5awfAjFArrB9IyYbh7MYIMN4nJMMgSTaxMCCkH5Zt84YJ88jRQVF6Z2t4kkK7g7HA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758273190; c=relaxed/simple; bh=Lzovmfm5FiaEorEh9d/YkmbdKQ4FW9Q9fpLfGCU4K1Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TxX5MgdpZ0fy6mnGtLOk+MDjTx/TMXsZFr4z9thUAyG6T69d9OYOjJqVqBkdPJFymCc85tw0g2GXzyED6hJ3KPN3DgXC3eH7WupHwAJ7AvwNrD6Tdm7ADaul32VdG4n5OtuMu0rAvFNCHgTPFi3S6wdzIjF4XPzEwX6bNQMGQ/E= 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=HdbmJC25; arc=none smtp.client-ip=209.85.160.179 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HdbmJC25" Received: by mail-qt1-f179.google.com with SMTP id d75a77b69052e-4b548745253so30569341cf.0 for ; Fri, 19 Sep 2025 02:13:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758273187; x=1758877987; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=kIwF6rXqWhVto9ZPqUAqlWxtbpHvuz3MRQbuOi5HJu4=; b=HdbmJC25TshYjud28rUPQqyU3sfKngH067nsd6b7RiZRMLUyz3SUNGdBNBmDJBjS8V Cg4AMRH2zIr4qwoXRLUYxMxEnteJjFkSRKzgz5Th60x6uXmijHvo3/AnVj3kmXAyHtpA rhcEQ+eferXCJfTFM5Jxy2apoyitg0bYSXSRWMbqtvhbpH6BGZOuhZuVR/ggMWUmuf9F tNgymEjhu1/SyYqBulq9R7l0ZVcZyWcPtnq9dGRy8mVzRtQK3UKzYcg0iVGnlT2WQTXr t01PwTWiXMoQ36zdsMpwYl/0DZ23OpvraxlnEJh4qZ2Tqx4I2ToBhYESLdXGr7bVjBMy 0qtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758273187; x=1758877987; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=kIwF6rXqWhVto9ZPqUAqlWxtbpHvuz3MRQbuOi5HJu4=; b=u4eVuSMsaXoJFY0ReYR94vhzjU5qRx+4uvJoxkNi1B1eSJz7jL+crnZKzT2Sso4IY9 bNAqvzcLU+5zX+l1YxHyGNfMuDucGyijjsGT86bGUI4gRNmEBn6RlAe8reqro91hhnfT 5LHRBfWl6LBFTBV1KH89SWkYK+F4tpikVGrePjI5KUEaBwfF3IoyLfiICIs5U5fUeDFo 2lpS/C58zhEszOiz/nvlLTJT4FFBFFyD8PyD+SmdN4hyvUqDJoJlNMz0wngoAc2w/ma7 b6lRziiuQr4KzhJEjcYl8Xw4heXzBwljrkOXzBvViOsRekETDP/GEQ47NS4cJ9MPgQjA he9A== X-Forwarded-Encrypted: i=1; AJvYcCVPyysYBH3ygtcfid6WynPhJ2YA5biiWPQB4PWpjkpSL4zUbz+P602q13lvRwGBVWpuUyveL71//Hq0uF0=@vger.kernel.org X-Gm-Message-State: AOJu0YxEpKNtCXo8MXGEdkiewSIYzCKek2BbuGWgHMVpKkOicqJfo0XX 9HuKv3s0gpJiDe5jPTnPcwTgMtXVJy5eg9xKmJJGnTlGXhKgNbYhcfuK X-Gm-Gg: ASbGncumpDxDC1YLPiKL0T0klQ+ze63Afx5gebWi5QguS1HnEeHYp+L9z2MTfXlIC1i 3HBNFyssSBXGEDck2PDCopnBqr1I3U66Bt8SV0a/el92EkNdsJnKHa1tENg875G6oOH4WnreQAp 8X7skHHjtWkGqIozsK5d4fLP+PSu/yl+Kef8AwhKfOsytR75B5NiPT5aIZL51JcCZdVdz0nHik/ oA/+j8TGEXc+wtzf+Ad+rxJWob9FeMb8CRCFikcyfYTQtXyO/a9qRThB0r3PsXuJXl6BwzNeQY6 KIVnQH4KGaEXfV5otm3UytndkTJM9NBJ9rFogYWK0qWBdddztjnEJ6w/4oZOILD+r3td2fXqf9p 0WrlQWRkolLrrxnKkafYEC5L8gEiVRhkirmzcPH5rMBdnwsvu7feac90bhJpBVUYEV9qWcoGyYq 14osHM0Aews/Vr X-Google-Smtp-Source: AGHT+IGADpUlc4d6GuGOP+3nLljBkz9sqj/QHSoZhghqe3A6PffXI/lWFd81SOfDZ9Hd2vishRWIPA== X-Received: by 2002:ac8:5dd1:0:b0:4b4:96a8:f79c with SMTP id d75a77b69052e-4c073c99e35mr31025171cf.79.1758273186690; Fri, 19 Sep 2025 02:13:06 -0700 (PDT) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4bda2c6d0c5sm26674071cf.21.2025.09.19.02.13.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 02:13:06 -0700 (PDT) Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfauth.phl.internal (Postfix) with ESMTP id DBC8AF40066; Fri, 19 Sep 2025 05:13:05 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Fri, 19 Sep 2025 05:13:05 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdegkeekvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhquhhnucfh vghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrthhtvg hrnheptedtteegjeefffeileevheeguddvleekfeeigfevgffhfeetieekgfffhfeiledv necuffhomhgrihhnpehgihhthhhusgdrtghomhdpkhgvrhhnvghlrdhorhhgpdhlohgtkh druggrthgrnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhho mhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghe dtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhes fhhigihmvgdrnhgrmhgvpdhnsggprhgtphhtthhopedvtddpmhhouggvpehsmhhtphhouh htpdhrtghpthhtohepphgvthgvrhiisehnfhhrrgguvggrugdrohhrghdprhgtphhtthho pehmihhnghhosehkvghrnhgvlhdrohhrghdprhgtphhtthhopehtghhlgieslhhinhhuth hrohhnihigrdguvgdprhgtphhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghr rdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvg hrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlkhhmmheslhhishhtshdrlhhinhhu gidruggvvhdprhgtphhtthhopeifihhllheskhgvrhhnvghlrdhorhhgpdhrtghpthhtoh epphgvthgvrhiisehinhhfrhgruggvrggurdhorhhgpdhrtghpthhtoheplhhonhhgmhgr nhesrhgvughhrghtrdgtohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 19 Sep 2025 05:13:04 -0400 (EDT) From: Boqun Feng To: "Peter Zijlstra" , "Ingo Molnar" , "Thomas Gleixner" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, lkmm@lists.linux.dev, "Will Deacon" , "Peter Zijlstra" , Waiman Long , "Miguel Ojeda" , alex.gaynor@gmail.com, "Gary Guo" , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , "Benno Lossin" , "Alice Ryhl" , "Trevor Gross" , "Danilo Krummrich" , "Andreas Hindborg" , Daniel Almeida , Boqun Feng Subject: [PATCH 2/4] rust: lock: guard: Add T: Unpin bound to DerefMut Date: Fri, 19 Sep 2025 11:12:39 +0200 Message-ID: <20250919091241.32138-3-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250919091241.32138-1-boqun.feng@gmail.com> References: <20250919091241.32138-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Daniel Almeida A core property of pinned types is not handing a mutable reference to the inner data in safe code, as this trivially allows that data to be moved. Enforce this condition by adding a bound on lock::Guard's DerefMut implementation, so that it's only implemented for pinning-agnostic types. Link: https://github.com/Rust-for-Linux/linux/issues/1181 Suggested-by: Benno Lossin Suggested-by: Boqun Feng Reviewed-by: Benno Lossin Reviewed-by: Alice Ryhl Signed-off-by: Daniel Almeida Signed-off-by: Boqun Feng --- Notes: SubmissionLink: https://lore.kernel.org/all/20250828-lock-t-when-t-is-p= inned-v2-1-b067c4b93fd6@collabora.com/ rust/kernel/sync/lock.rs | 5 ++++- rust/kernel/sync/lock/global.rs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 27202beef90c..b482f34bf0ce 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -251,7 +251,10 @@ fn deref(&self) -> &Self::Target { } } =20 -impl core::ops::DerefMut for Guard<'_, T, B> { +impl core::ops::DerefMut for Guard<'_, T, B> +where + T: Unpin, +{ fn deref_mut(&mut self) -> &mut Self::Target { // SAFETY: The caller owns the lock, so it is safe to deref the pr= otected data. unsafe { &mut *self.lock.data.get() } diff --git a/rust/kernel/sync/lock/global.rs b/rust/kernel/sync/lock/global= .rs index d65f94b5caf2..38b448032799 100644 --- a/rust/kernel/sync/lock/global.rs +++ b/rust/kernel/sync/lock/global.rs @@ -106,7 +106,10 @@ fn deref(&self) -> &Self::Target { } } =20 -impl core::ops::DerefMut for GlobalGuard { +impl core::ops::DerefMut for GlobalGuard +where + B::Item: Unpin, +{ fn deref_mut(&mut self) -> &mut Self::Target { &mut self.inner } --=20 2.51.0 From nobody Thu Oct 2 07:46:26 2025 Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) (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 4AF36302154 for ; Fri, 19 Sep 2025 09:13:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758273192; cv=none; b=kvNobzbXqySgsYP20xdSJRj5rMHVSC8qBu+yhmApDyJmU3aCaRLPSTFLU+n1emC/B2dKomO3UEd1JZ5vbBRq/bqz1jcHzNTlxalqnh64KrULSvjvYcM7tRNyZYNx/5mnGA2kQlflKgPhb6r/k/URngO/SFF+8YQt67N+Cmu3wP8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758273192; c=relaxed/simple; bh=6iII7fOYlyuZ8eQyG3nIT1ClvaKOibtuYRtJwPDwqjw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uW7ayLwKcwchJzVJ4VeMgoDmkhLia9Nk7BEavEYWw+cEW+FTjYObtxTVQd5LrBsCPs9lCe39VyehX7bWqynxohWGYqCHR+PxSCSS7g7/2kyK/1q0QN8UvzjQvphuuEJ7Ae/cpWtRDQ3Vhht7bXV/JOHAZ7C5Td9I537rdb0wfPw= 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=foOFT6sQ; arc=none smtp.client-ip=209.85.219.52 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="foOFT6sQ" Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-77dd76f6964so29155276d6.1 for ; Fri, 19 Sep 2025 02:13:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758273190; x=1758877990; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=Tz+hRdDjBJIiUvT5QMJv4FOcC6A5SLAQN/E0smv/a6w=; b=foOFT6sQj+DqYplwDHoXOA6+ovMEpwod5lZoF3zjF+rG7AcnsMuD7kqnfPsr8Uxqfa xXMHRKxFdHQoUWC/jeXWMNEz5ZLnRpVwM7vAeRaYi/RA1INo1mEaEiRDZVg0Wh1oNhLa SsUbkeZivDQQi04wq2I1o3yhHzl4ig2dY+uC95g05B+wrA6QaJIqBj7uE5jppZH48w9H xgH8enZXAFCZzFUdoW21GoXtrMhkLwYyz8yVbtO5RfjjoetF1QRGXnbPre2K4BDt3mxb GAh1Rcayg1Sw76zj9ywf8yDyCemAaY+haDnNC3MnKqPtJzPFVmGRw7MS+LyZeKFllKHz cEug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758273190; x=1758877990; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Tz+hRdDjBJIiUvT5QMJv4FOcC6A5SLAQN/E0smv/a6w=; b=GOxWsMUYxjE7oPnCnu4dI6o9DOjh8m3upBwWzDVxJ7vQYHlBz+RpY1JMWawULinVEz G4dKj7rWJlKrjgcuEAVTJx5WwRew5QlTPsqTw/wTknvSSlRzrqNhC9dMr1/5gL1bf3/E k86UgrhbqzXd9bti9e7MmmENvyxzKfg8i62CxtXQd3OlB5XDFLPgTFQyyI4WhQ2OZ1WP FFhSNyR27aM6ufLLHYTnwB3PlWO2tYnokWZ6BPZxZi4oqpDlpAW90JriPGlzjXOJomR5 /Cqsjm7zayjVICYykv12E4A2Qi/ZvE6JuPBX9StmxQtsWZY6D6Qp22ALnrlsc1nAcP0C UkFg== X-Forwarded-Encrypted: i=1; AJvYcCWm7yglHP7+JdZcweAyWMaqSJv6g5FmdavOmqo05FKNfogk8Io5ay8SoP2L8wn3P7A3jPlcu6Qk8fQqbUA=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/591cyB0gW94WJIm4ta4EfVV1Y5oFhiLsR4582/K5bKb9M83I VwBkrZ41M+zTVUCBakhwE9dCRpEdC+yx4stvB07oGSQYYsnCoqhiW/vs X-Gm-Gg: ASbGncs4GRPrZTQgmSOeNaLG3T2cYR3TMl1djOrV38vh1y3APzuZJk23SJxoZLN+PJt lcpqObK4XIcGGOPDybmVezVfIhfigpr7LzA7DvbvQfm/bZezMqZd17cdk8AbTcsY5D9EXgqz5bQ Ne7oVl23Frgn3lPYyNoTdYrNmG7mfbXj8N75ItaqAXLHzqeljNZuJJz16ENzmghZd9sdH94+AkA 4b94PTCuIdm6rP9kYvdce5OB4eUyadby8JlZanINm9CZ7pocpgEnNIZS2DyLYMa5q6gkwttM462 N/fdOUYGTt2WRzXKR5Q4ISL9azhkBxVG5XzxDLJBz2fnbXH/b3JbPYomgRsvUKwBnBzANYSrZwI eNYVMd7OXAbNNspwnaBb6oqVGizthJXd+dNUBDHN2lOX1xYLAMi+H7qSOxpJeNer9mhXN2nrJqr Gh4WH6XDAv0xIZ X-Google-Smtp-Source: AGHT+IGHdHKieKTrB7753iPEOZIDAZM2fgg6Vs6HuQORCmBrQ4D7MJr31cHmfl/qBr2eF2H/N1qLqg== X-Received: by 2002:a05:6214:20ce:b0:783:f54f:418a with SMTP id 6a1803df08f44-798bc7bf0damr31065416d6.15.1758273190002; Fri, 19 Sep 2025 02:13:10 -0700 (PDT) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-793533ac961sm25881696d6.50.2025.09.19.02.13.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 02:13:09 -0700 (PDT) Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailfauth.phl.internal (Postfix) with ESMTP id 23359F40067; Fri, 19 Sep 2025 05:13:09 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Fri, 19 Sep 2025 05:13:09 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdegkeekvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhquhhnucfh vghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrthhtvg hrnhepkeetteejgeduieekudejfedvvddtiefgieffffeljeefieehiefffedvfeffgfet necuffhomhgrihhnpehgihhthhhusgdrtghomhdpkhgvrhhnvghlrdhorhhgnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgv shhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddujeejkeehhe ehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv pdhnsggprhgtphhtthhopedvtddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepph gvthgvrhiisehnfhhrrgguvggrugdrohhrghdprhgtphhtthhopehmihhnghhosehkvghr nhgvlhdrohhrghdprhgtphhtthhopehtghhlgieslhhinhhuthhrohhnihigrdguvgdprh gtphhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhr ghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdroh hrghdprhgtphhtthhopehlkhhmmheslhhishhtshdrlhhinhhugidruggvvhdprhgtphht thhopeifihhllheskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepphgvthgvrhiisehinh hfrhgruggvrggurdhorhhgpdhrtghpthhtoheplhhonhhgmhgrnhesrhgvughhrghtrdgt ohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 19 Sep 2025 05:13:08 -0400 (EDT) From: Boqun Feng To: "Peter Zijlstra" , "Ingo Molnar" , "Thomas Gleixner" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, lkmm@lists.linux.dev, "Will Deacon" , "Peter Zijlstra" , Waiman Long , "Miguel Ojeda" , alex.gaynor@gmail.com, "Gary Guo" , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , "Benno Lossin" , "Alice Ryhl" , "Trevor Gross" , "Danilo Krummrich" , "Andreas Hindborg" , Daniel Almeida , Boqun Feng Subject: [PATCH 3/4] rust: lock: Pin the inner data Date: Fri, 19 Sep 2025 11:12:40 +0200 Message-ID: <20250919091241.32138-4-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250919091241.32138-1-boqun.feng@gmail.com> References: <20250919091241.32138-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Daniel Almeida In preparation to support Lock where T is pinned, the first thing that needs to be done is to structurally pin the 'data' member. This switches the 't' parameter in Lock::new() to take in an impl PinInit instead of a plain T. This in turn uses the blanket implementation "impl PinInit for T". Subsequent patches will touch on Guard. Link: https://github.com/Rust-for-Linux/linux/issues/1181 Suggested-by: Benno Lossin Suggested-by: Boqun Feng Reviewed-by: Benno Lossin Reviewed-by: Alice Ryhl Signed-off-by: Daniel Almeida Signed-off-by: Boqun Feng --- Notes: SubmissionLink: https://lore.kernel.org/all/20250828-lock-t-when-t-is-p= inned-v2-2-b067c4b93fd6@collabora.com/ rust/kernel/sync/lock.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index b482f34bf0ce..9242790d15db 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -11,7 +11,7 @@ types::{NotThreadSafe, Opaque, ScopeGuard}, }; use core::{cell::UnsafeCell, marker::PhantomPinned, pin::Pin}; -use pin_init::{pin_data, pin_init, PinInit}; +use pin_init::{pin_data, pin_init, PinInit, Wrapper}; =20 pub mod mutex; pub mod spinlock; @@ -115,6 +115,7 @@ pub struct Lock { _pin: PhantomPinned, =20 /// The data protected by the lock. + #[pin] pub(crate) data: UnsafeCell, } =20 @@ -127,9 +128,13 @@ unsafe impl Sync for Loc= k {} =20 impl Lock { /// Constructs a new lock initialiser. - pub fn new(t: T, name: &'static CStr, key: Pin<&'static LockClassKey>)= -> impl PinInit { + pub fn new( + t: impl PinInit, + name: &'static CStr, + key: Pin<&'static LockClassKey>, + ) -> impl PinInit { pin_init!(Self { - data: UnsafeCell::new(t), + data <- UnsafeCell::pin_init(t), _pin: PhantomPinned, // SAFETY: `slot` is valid while the closure is called and bot= h `name` and `key` have // static lifetimes so they live indefinitely. --=20 2.51.0 From nobody Thu Oct 2 07:46:26 2025 Received: from mail-qv1-f48.google.com (mail-qv1-f48.google.com [209.85.219.48]) (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 09A4C2EF64F for ; Fri, 19 Sep 2025 09:13:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758273201; cv=none; b=UfItoyHXVaf8VcRIiqRPtKY4zYw5OXvaODpuAe1KQ+3+zfjRVo1+R09MTNr1HpREMkQmcWzPLz5pPCiwc9UAb/s4eDwkEFo9LH8Mb9bnV9j8TXFXuq1COUf5F0GtBCOR1xJWgzpFg5dQBh5zaP46Gvl62qQl4Rm1vkrQZ+iVUYc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758273201; c=relaxed/simple; bh=qiImjsEzOnaVKlSA0NmMABN9A4Q0NbEvUqksJ6TQ7Vg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y948XWeit2gCW1hI0GfsiS92LAiNe+rj2W12hKkeFtiG6SJ4A59AMnoam+4Ly71IAc1/8Vxhd1DGUvV050K9TnOAI0IJJXhFrj3cC2rgmOFQ+6UX0CW010E7FpmxqCd1TD5fqphzoRRiFzCwBMD5eWkFtqGyXDkHicojbcj8eos= 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=di82UDSw; arc=none smtp.client-ip=209.85.219.48 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="di82UDSw" Received: by mail-qv1-f48.google.com with SMTP id 6a1803df08f44-78f58f4230cso15782616d6.1 for ; Fri, 19 Sep 2025 02:13:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758273198; x=1758877998; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=HC7c63QfQ23In6I7fvHeR2uJH3BX6Ddk3KhkxkSWOaw=; b=di82UDSw1Ujorkjr5j1iFvqsxFzI6TpG8UvnCWoEYTV/yi4iSgNCZrePM1tJqYQgKn a/gOoUYZqGWMkRY5BbIStmEFAvhVV53M7XpKOQycfQkMY79RFdSOT8uY4EZk+ncik3YP 4pExHcCuLv0ZDf+W1lkTc0NxMq9706qW9wH1XY0f+9RlcDo5LoHVBC5Kj9X80fuJT2Rm lEW5TANsWIzcGemzDz/0NS5Ek7Yb4jO65988YvXWmlZtilQxT/Tz9asi5RPbZHI1r1U/ aHTLP8+TWRC+L9WL80ocJ2drlmrNCKiyeYu9gm3HDWtPw4TzbDv1Ip/jyu3dn7GWSSoW /iyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758273198; x=1758877998; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=HC7c63QfQ23In6I7fvHeR2uJH3BX6Ddk3KhkxkSWOaw=; b=fWMKO62BDg9YYCuexAG2LiVlvGH2cDomRB4soHPNGQPKBj+ytgLLYg3aAytDBp8dZP /Pg2/RGUp/NZcJQHs+3pQ0JFlwOMj3rfRDNmzNHggdA1QgvpzGj1F+7p0PJBw4IwVLsO MdNOD0Y2mmeat1D8FtzsGm6Xl3Kr4Av73/G6yCesent/2N9ZBBrK9MfAXNtL2QUi+o0o 0qPEr120dqNT4METM2DFQlKqihTRZu0u/0rj3SdGWrpZ7zAeRI0YCl1oLxsSCrM2Pilw pE/TgLpLOTUbBqA3SxSuakEsDlR0USnSBoUizsZ88RK/28xT64FayKiJshHtl142d59J MdvA== X-Forwarded-Encrypted: i=1; AJvYcCW2P3ZrnpKfDTC3U9JdZVkBXi0opl8h+Bwxio27kwP1SJpT9vZgcMEYDC6eYguid3BbaaG4rdwPYBFCk74=@vger.kernel.org X-Gm-Message-State: AOJu0YwsoQ32J3LcIsNUH2JKX9n2E9J/X4QXQhYdj3jpCV0OvCDNj4F/ 6vbCJwKla4TsbxUbS5xqJNOW2+yxwVHPQMBXV7j+klEgoPUIMKrAFCHp X-Gm-Gg: ASbGncsUWhc229nxRiuwCdKAcyMLeZsTQc3+NsdC/w+BjBiEYseIDTOpVThsnEtOhlQ 3AQW7MIfjN+8khE9z+DwY4J7RlU/7yRzPdt5UCRTmjhnCAXiK1YzW/YrBTCZ0M4ep+cS3z0fSyx YV6iEi3cjTqFjgDh2W3PcVXj257imyWVCom1K6ZWqJWKVq+ljb1ExBzyuBHMVQ05AHmauVoPSSC 41hpVTtS72RG7JJEZ9EuLM1qxouITY4Z56PyD4mt7ZF5tT1TQmR5BaPet8qzdDSNQkC2F9LRXKm NcAcVbLHzERhEqSLVv7aVEEkY1tDJ3uHV8IC6TIFmXtG/7PHuEnFrxbCcoRl0yBL1GiRlSkerji LrWiLHKvLG0ZokhQ9yl6bfJbKp7kPNG9jT2p4Ei0aqStgaATn3r69of3uWN3ifTx5CJ14FmHa5T Lvjpt4py4dh/ff X-Google-Smtp-Source: AGHT+IFZ8F2wXi48IVZenpQOrH5DZrRMNXs/JXKEj22vTrsl2NFuYo7YHquPhCVcBtbRl/WONq7N2A== X-Received: by 2002:ad4:5964:0:b0:768:d8cd:17e5 with SMTP id 6a1803df08f44-799197edf17mr24758906d6.40.1758273197932; Fri, 19 Sep 2025 02:13:17 -0700 (PDT) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-79351f71705sm25966476d6.39.2025.09.19.02.13.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 02:13:17 -0700 (PDT) Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfauth.phl.internal (Postfix) with ESMTP id 1E63BF40066; Fri, 19 Sep 2025 05:13:17 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Fri, 19 Sep 2025 05:13:17 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdegkeekvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhquhhnucfh vghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrthhtvg hrnheptedtteegjeefffeileevheeguddvleekfeeigfevgffhfeetieekgfffhfeiledv necuffhomhgrihhnpehgihhthhhusgdrtghomhdpkhgvrhhnvghlrdhorhhgpdhlohgtkh druggrthgrnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhho mhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghe dtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhes fhhigihmvgdrnhgrmhgvpdhnsggprhgtphhtthhopedvtddpmhhouggvpehsmhhtphhouh htpdhrtghpthhtohepphgvthgvrhiisehnfhhrrgguvggrugdrohhrghdprhgtphhtthho pehmihhnghhosehkvghrnhgvlhdrohhrghdprhgtphhtthhopehtghhlgieslhhinhhuth hrohhnihigrdguvgdprhgtphhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghr rdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvg hrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlkhhmmheslhhishhtshdrlhhinhhu gidruggvvhdprhgtphhtthhopeifihhllheskhgvrhhnvghlrdhorhhgpdhrtghpthhtoh epphgvthgvrhiisehinhhfrhgruggvrggurdhorhhgpdhrtghpthhtoheplhhonhhgmhgr nhesrhgvughhrghtrdgtohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 19 Sep 2025 05:13:16 -0400 (EDT) From: Boqun Feng To: "Peter Zijlstra" , "Ingo Molnar" , "Thomas Gleixner" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, lkmm@lists.linux.dev, "Will Deacon" , "Peter Zijlstra" , Waiman Long , "Miguel Ojeda" , alex.gaynor@gmail.com, "Gary Guo" , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , "Benno Lossin" , "Alice Ryhl" , "Trevor Gross" , "Danilo Krummrich" , "Andreas Hindborg" , Daniel Almeida , Boqun Feng Subject: [PATCH 4/4] rust: lock: Add a Pin<&mut T> accessor Date: Fri, 19 Sep 2025 11:12:41 +0200 Message-ID: <20250919091241.32138-5-boqun.feng@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250919091241.32138-1-boqun.feng@gmail.com> References: <20250919091241.32138-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Daniel Almeida In order for callers to be able to access the inner T safely if T: !Unpin, there needs to be a way to get a Pin<&mut T>. Add this accessor and a corresponding example to tell users how it works. This requires the pin projection functionality [1] for better ergonomic. Link: https://github.com/Rust-for-Linux/linux/issues/1181 Link: https://lore.kernel.org/rust-for-linux/20250912174148.373530-1-lossin= @kernel.org/ [1] Suggested-by: Benno Lossin Suggested-by: Boqun Feng Reviewed-by: Alice Ryhl Signed-off-by: Daniel Almeida Reviewed-by: Benno Lossin [boqun: Apply Daniel's fix to the code example, add the reference to pin projection patch and remove out-of-date part in the commit log] Signed-off-by: Boqun Feng --- Notes: SubmissionLink: https://lore.kernel.org/all/20250828-lock-t-when-t-is-p= inned-v2-3-b067c4b93fd6@collabora.com/ rust/kernel/sync/lock.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 9242790d15db..cb00fdb94ffd 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -245,6 +245,31 @@ pub(crate) fn do_unlocked(&mut self, cb: impl FnOnc= e() -> U) -> U { =20 cb() } + + /// Returns a pinned mutable reference to the protected data. + /// + /// The guard implements [`DerefMut`] when `T: Unpin`, so for [`Unpin`] + /// types [`DerefMut`] should be used instead of this function. + /// + /// [`DerefMut`]: core::ops::DerefMut + /// [`Unpin`]: core::marker::Unpin + /// + /// # Examples + /// + /// ``` + /// # use kernel::sync::{Mutex, MutexGuard}; + /// # use core::{pin::Pin, marker::PhantomPinned}; + /// struct Data(PhantomPinned); + /// + /// fn example(mutex: &Mutex) { + /// let mut data: MutexGuard<'_, Data> =3D mutex.lock(); + /// let mut data: Pin<&mut Data> =3D data.as_mut(); + /// } + /// ``` + pub fn as_mut(&mut self) -> Pin<&mut T> { + // SAFETY: `self.lock.data` is structurally pinned. + unsafe { Pin::new_unchecked(&mut *self.lock.data.get()) } + } } =20 impl core::ops::Deref for Guard<'_, T, B> { --=20 2.51.0