From nobody Fri Feb 13 00:14:48 2026 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 36DE13BB24 for ; Tue, 4 Jun 2024 20:40:08 +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=1717533609; cv=none; b=VoRwJHtoWV+WU/6N198JBA8n/wmJUc0KIG48qg0VO7sAead0GmxtpxNR6PUqbgcPesi8EzCsKbSV5ENty3lAobMd4cUBVtimhLcVMYphdk7gmo9WgB3JQzAnI7bYeUpKFslo/57rWr/Ke5sxR7K2DPiTWnYNsyUhQMj9VnYfDCY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717533609; c=relaxed/simple; bh=OA4jXIR7HzNnpaEbG8qQ2TpYGmLYNKQZaYWLgI4fz70=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AjrEkKe+ZXWe1WNkR7GgY60cEmyZC0NrLT7uZUcxssXl1V4i5NiJr4EhVg9GRLQlyJQZiA7WcYigUh2rGxhMtYQ/LrnRabh+IG0h/OnGZp65xPCb/vr5xIFLMQe0jBx+upaY0SgdVut8Nk3RNLXo6EqrFHDVCg0dgZlx23iqzMw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iQuK7mO8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iQuK7mO8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5893C2BBFC; Tue, 4 Jun 2024 20:40:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717533608; bh=OA4jXIR7HzNnpaEbG8qQ2TpYGmLYNKQZaYWLgI4fz70=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iQuK7mO8wi/Niq/PkspASgP/nPjYY3fsO3u+c0iQ72Y+DdLzwdW5KhZx9S1D90eF5 H5zIDaBAQkbsSDTRfhShL+zOdJ5gUIdTgKx8xy++sd8dntdLIr8LRIgNkPiwC/qB+Y 1IO0oO9SYbRYojZHcFodL+pK2d35OT5g4lgrsSAvULWdc+O0ubFaKETiRai07Nuwoa lyUJdv49tW37eVsQAY+IKuPFongImcl5sGRj+bCLgra8GaXTFFMuZRffYCI0xfs642 gzTxpdwkarELkZS7vB4/8dEktBGZeUMyjI/xDdAtFgHqIQbAlaet5xhSW1Y0pOgMLp MCMWsJuD2dyYw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 77B2CCE3ED6; Tue, 4 Jun 2024 13:40:08 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, kernel-team@meta.com, mingo@kernel.org Cc: elver@google.com, andreyknvl@google.com, glider@google.com, dvyukov@google.com, cai@lca.pw, boqun.feng@gmail.com, "Paul E. McKenney" , Bart Van Assche , Breno Leitao , Jens Axboe Subject: [PATCH kcsan 1/2] kcsan: Add example to data_race() kerneldoc header Date: Tue, 4 Jun 2024 13:40:05 -0700 Message-Id: <20240604204006.2367440-1-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: 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" Although the data_race() kerneldoc header accurately states what it does, some of the implications and usage patterns are non-obvious. Therefore, add a brief locking example and also state how to have KCSAN ignore accesses while also preventing the compiler from folding, spindling, or otherwise mutilating the access. [ paulmck: Apply Bart Van Assche feedback. ] [ paulmck: Apply feedback from Marco Elver. ] Reported-by: Bart Van Assche Signed-off-by: Paul E. McKenney Cc: Marco Elver Cc: Breno Leitao Cc: Jens Axboe --- include/linux/compiler.h | 10 +++++++- .../Documentation/access-marking.txt | 24 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 8c252e073bd81..68a24a3a69799 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -194,9 +194,17 @@ void ftrace_likely_update(struct ftrace_likely_data *f= , int val, * This data_race() macro is useful for situations in which data races * should be forgiven. One example is diagnostic code that accesses * shared variables but is not a part of the core synchronization design. + * For example, if accesses to a given variable are protected by a lock, + * except for diagnostic code, then the accesses under the lock should + * be plain C-language accesses and those in the diagnostic code should + * use data_race(). This way, KCSAN will complain if buggy lockless + * accesses to that variable are introduced, even if the buggy accesses + * are protected by READ_ONCE() or WRITE_ONCE(). * * This macro *does not* affect normal code generation, but is a hint - * to tooling that data races here are to be ignored. + * to tooling that data races here are to be ignored. If the access must + * be atomic *and* KCSAN should ignore the access, use both data_race() + * and READ_ONCE(), for example, data_race(READ_ONCE(x)). */ #define data_race(expr) \ ({ \ diff --git a/tools/memory-model/Documentation/access-marking.txt b/tools/me= mory-model/Documentation/access-marking.txt index 65778222183e3..3377d01bb512c 100644 --- a/tools/memory-model/Documentation/access-marking.txt +++ b/tools/memory-model/Documentation/access-marking.txt @@ -24,6 +24,11 @@ The Linux kernel provides the following access-marking o= ptions: 4. WRITE_ONCE(), for example, "WRITE_ONCE(a, b);" The various forms of atomic_set() also fit in here. =20 +5. __data_racy, for example "int __data_racy a;" + +6. KCSAN's negative-marking assertions, ASSERT_EXCLUSIVE_ACCESS() + and ASSERT_EXCLUSIVE_WRITER(), are described in the + "ACCESS-DOCUMENTATION OPTIONS" section below. =20 These may be used in combination, as shown in this admittedly improbable example: @@ -205,6 +210,23 @@ because doing otherwise prevents KCSAN from detecting = violations of your code's synchronization rules. =20 =20 +Use of __data_racy +------------------ + +Adding the __data_racy type qualifier to the declaration of a variable +causes KCSAN to treat all accesses to that variable as if they were +enclosed by data_race(). However, __data_racy does not affect the +compiler, though one could imagine hardened kernel builds treating the +__data_racy type qualifier as if it was the volatile keyword. + +Note well that __data_racy is subject to the same pointer-declaration +rules as are other type qualifiers such as const and volatile. +For example: + + int __data_racy *p; // Pointer to data-racy data. + int *__data_racy p; // Data-racy pointer to non-data-racy data. + + ACCESS-DOCUMENTATION OPTIONS =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D =20 @@ -342,7 +364,7 @@ as follows: =20 Because foo is read locklessly, all accesses are marked. The purpose of the ASSERT_EXCLUSIVE_WRITER() is to allow KCSAN to check for a buggy -concurrent lockless write. +concurrent write, whether marked or not. =20 =20 Lock-Protected Writes With Heuristic Lockless Reads --=20 2.40.1 From nobody Fri Feb 13 00:14:48 2026 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 36DAA17C96 for ; Tue, 4 Jun 2024 20:40:08 +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=1717533609; cv=none; b=MJrFi0CM9v+TyZ/pOjDEmVUfHDbJ4XHYCGH+grC8fWCW6nwj75Y6/FafLhbXKKLYWZ6R4bumWOHy04upcuFb7mNRBNdFjKJMTwRlnFcWaJVsHildYZPvXlpcgy5az8lA6JEv/GJnA8cUakFwAaYsfnCoEwgxg16rz0W0HWJQDDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717533609; c=relaxed/simple; bh=YGiYd+G/quHQJqKEqS1+8iAeP7kk1gfIeZPfJ7HaAxs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CwfdI/iY4ljAZaaXseyiWRbA+cocQ9azlzpmrgNHNKLO7/QsPm6ridfxtam8GX/2dF6Qisk8UjnA43XnfS27QRdYBtCQbKvNyo7WTktqS55ZPj552pRzIgCxk9dY4khYnrf0+gB4X8bqGHPES37TO9UrBPWkMRlCdzNSjK/xMDI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hyNtBL0+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hyNtBL0+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CEDA0C3277B; Tue, 4 Jun 2024 20:40:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717533608; bh=YGiYd+G/quHQJqKEqS1+8iAeP7kk1gfIeZPfJ7HaAxs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hyNtBL0+ZIdrbbg607Hd5kixC94aaljLboCpxsKU9lnc87K4mvL7iMpsr0i53mOff I8RlTTEtz2Oif43out4MY3TEK6Nbe4QOJN+UQ8xBxHryyw8aFulS0jH0Qw8mxyCHwZ bjbPOa41bCw0wOASz3VDP54WkkxlnJnX1mShwfU3GJfRWRYRYDKfsPF5NCHLT+DFSA mvJRwv3FRmrngDiGnz46qEGuzd3zP4c+MWUMeeUgwnakmgGK41+dkuoVo06HFcY47U EBVlUfujUPrqokXdmbPGF9cSgL04nBcgJu2b5iMIk5hQtaDa7FuLCnUBXi9Pnf11Ia Rc2afeQIqYQmw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 7ABB9CE3F0F; Tue, 4 Jun 2024 13:40:08 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, kernel-team@meta.com, mingo@kernel.org Cc: elver@google.com, andreyknvl@google.com, glider@google.com, dvyukov@google.com, cai@lca.pw, boqun.feng@gmail.com, Jeff Johnson , "Paul E . McKenney" Subject: [PATCH kcsan 2/2] kcsan: test: add missing MODULE_DESCRIPTION() macro Date: Tue, 4 Jun 2024 13:40:06 -0700 Message-Id: <20240604204006.2367440-2-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: 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: Jeff Johnson Fix the warning reported by 'make C=3D1 W=3D1': WARNING: modpost: missing MODULE_DESCRIPTION() in kernel/kcsan/kcsan_test.o Signed-off-by: Jeff Johnson Reviewed-by: Marco Elver Signed-off-by: Paul E. McKenney --- kernel/kcsan/kcsan_test.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/kcsan/kcsan_test.c b/kernel/kcsan/kcsan_test.c index 0c17b4c83e1ca..117d9d4d3c3bd 100644 --- a/kernel/kcsan/kcsan_test.c +++ b/kernel/kcsan/kcsan_test.c @@ -1620,5 +1620,6 @@ static struct kunit_suite kcsan_test_suite =3D { =20 kunit_test_suites(&kcsan_test_suite); =20 +MODULE_DESCRIPTION("KCSAN test suite"); MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("Marco Elver "); --=20 2.40.1