Skip to content

Commit e4b2b0b

Browse files
committed
Merge tag 'kcsan.2024.07.12a' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu
Pull KCSAN updates from Paul McKenney: - improve the documentation for the new __data_racy type qualifier to the data_race() macro's kernel-doc header and to the LKMM's access-marking documentation - add missing MODULE_DESCRIPTION * tag 'kcsan.2024.07.12a' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu: kcsan: Add missing MODULE_DESCRIPTION() macro kcsan: Add example to data_race() kerneldoc header
2 parents b176e21 + ddd7432 commit e4b2b0b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

include/linux/compiler.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,17 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
194194
* This data_race() macro is useful for situations in which data races
195195
* should be forgiven. One example is diagnostic code that accesses
196196
* shared variables but is not a part of the core synchronization design.
197+
* For example, if accesses to a given variable are protected by a lock,
198+
* except for diagnostic code, then the accesses under the lock should
199+
* be plain C-language accesses and those in the diagnostic code should
200+
* use data_race(). This way, KCSAN will complain if buggy lockless
201+
* accesses to that variable are introduced, even if the buggy accesses
202+
* are protected by READ_ONCE() or WRITE_ONCE().
197203
*
198204
* This macro *does not* affect normal code generation, but is a hint
199-
* to tooling that data races here are to be ignored.
205+
* to tooling that data races here are to be ignored. If the access must
206+
* be atomic *and* KCSAN should ignore the access, use both data_race()
207+
* and READ_ONCE(), for example, data_race(READ_ONCE(x)).
200208
*/
201209
#define data_race(expr) \
202210
({ \

kernel/kcsan/kcsan_test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,5 +1620,6 @@ static struct kunit_suite kcsan_test_suite = {
16201620

16211621
kunit_test_suites(&kcsan_test_suite);
16221622

1623+
MODULE_DESCRIPTION("KCSAN test suite");
16231624
MODULE_LICENSE("GPL v2");
16241625
MODULE_AUTHOR("Marco Elver <[email protected]>");

tools/memory-model/Documentation/access-marking.txt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ The Linux kernel provides the following access-marking options:
2525
4. WRITE_ONCE(), for example, "WRITE_ONCE(a, b);"
2626
The various forms of atomic_set() also fit in here.
2727

28+
5. __data_racy, for example "int __data_racy a;"
29+
30+
6. KCSAN's negative-marking assertions, ASSERT_EXCLUSIVE_ACCESS()
31+
and ASSERT_EXCLUSIVE_WRITER(), are described in the
32+
"ACCESS-DOCUMENTATION OPTIONS" section below.
2833

2934
These may be used in combination, as shown in this admittedly improbable
3035
example:
@@ -206,6 +211,23 @@ because doing otherwise prevents KCSAN from detecting violations of your
206211
code's synchronization rules.
207212

208213

214+
Use of __data_racy
215+
------------------
216+
217+
Adding the __data_racy type qualifier to the declaration of a variable
218+
causes KCSAN to treat all accesses to that variable as if they were
219+
enclosed by data_race(). However, __data_racy does not affect the
220+
compiler, though one could imagine hardened kernel builds treating the
221+
__data_racy type qualifier as if it was the volatile keyword.
222+
223+
Note well that __data_racy is subject to the same pointer-declaration
224+
rules as are other type qualifiers such as const and volatile.
225+
For example:
226+
227+
int __data_racy *p; // Pointer to data-racy data.
228+
int *__data_racy p; // Data-racy pointer to non-data-racy data.
229+
230+
209231
ACCESS-DOCUMENTATION OPTIONS
210232
============================
211233

@@ -343,7 +365,7 @@ as follows:
343365

344366
Because foo is read locklessly, all accesses are marked. The purpose
345367
of the ASSERT_EXCLUSIVE_WRITER() is to allow KCSAN to check for a buggy
346-
concurrent lockless write.
368+
concurrent write, whether marked or not.
347369

348370

349371
Lock-Protected Writes With Heuristic Lockless Reads

0 commit comments

Comments
 (0)