Skip to content

Commit d8949ef

Browse files
melverpaulmckrcu
authored andcommitted
kcsan: Introduce scoped ASSERT_EXCLUSIVE macros
Introduce ASSERT_EXCLUSIVE_*_SCOPED(), which provide an intuitive interface to use the scoped-access feature, without having to explicitly mark the start and end of the desired scope. Basing duration of the checks on scope avoids accidental misuse and resulting false positives, which may be hard to debug. See added comments for usage. The macros are implemented using __attribute__((__cleanup__(func))), which is supported by all compilers that currently support KCSAN. Suggested-by: Boqun Feng <boqun.feng@gmail.com> Suggested-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Marco Elver <elver@google.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 parent 9967683 commit d8949ef

3 files changed

Lines changed: 89 additions & 3 deletions

File tree

‎Documentation/dev-tools/kcsan.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ are defined at the C-language level. The following macros can be used to check
238238
properties of concurrent code where bugs would not manifest as data races.
239239

240240
.. kernel-doc:: include/linux/kcsan-checks.h
241-
:functions: ASSERT_EXCLUSIVE_WRITER ASSERT_EXCLUSIVE_ACCESS
241+
:functions: ASSERT_EXCLUSIVE_WRITER ASSERT_EXCLUSIVE_WRITER_SCOPED
242+
ASSERT_EXCLUSIVE_ACCESS ASSERT_EXCLUSIVE_ACCESS_SCOPED
242243
ASSERT_EXCLUSIVE_BITS
243244

244245
Implementation Details

‎include/linux/kcsan-checks.h‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,63 @@ static inline void kcsan_check_access(const volatile void *ptr, size_t size,
234234
* ... = READ_ONCE(shared_foo);
235235
* }
236236
*
237+
* Note: ASSERT_EXCLUSIVE_WRITER_SCOPED(), if applicable, performs more thorough
238+
* checking if a clear scope where no concurrent writes are expected exists.
239+
*
237240
* @var: variable to assert on
238241
*/
239242
#define ASSERT_EXCLUSIVE_WRITER(var) \
240243
__kcsan_check_access(&(var), sizeof(var), KCSAN_ACCESS_ASSERT)
241244

245+
/*
246+
* Helper macros for implementation of for ASSERT_EXCLUSIVE_*_SCOPED(). @id is
247+
* expected to be unique for the scope in which instances of kcsan_scoped_access
248+
* are declared.
249+
*/
250+
#define __kcsan_scoped_name(c, suffix) __kcsan_scoped_##c##suffix
251+
#define __ASSERT_EXCLUSIVE_SCOPED(var, type, id) \
252+
struct kcsan_scoped_access __kcsan_scoped_name(id, _) \
253+
__kcsan_cleanup_scoped; \
254+
struct kcsan_scoped_access *__kcsan_scoped_name(id, _dummy_p) \
255+
__maybe_unused = kcsan_begin_scoped_access( \
256+
&(var), sizeof(var), KCSAN_ACCESS_SCOPED | (type), \
257+
&__kcsan_scoped_name(id, _))
258+
259+
/**
260+
* ASSERT_EXCLUSIVE_WRITER_SCOPED - assert no concurrent writes to @var in scope
261+
*
262+
* Scoped variant of ASSERT_EXCLUSIVE_WRITER().
263+
*
264+
* Assert that there are no concurrent writes to @var for the duration of the
265+
* scope in which it is introduced. This provides a better way to fully cover
266+
* the enclosing scope, compared to multiple ASSERT_EXCLUSIVE_WRITER(), and
267+
* increases the likelihood for KCSAN to detect racing accesses.
268+
*
269+
* For example, it allows finding race-condition bugs that only occur due to
270+
* state changes within the scope itself:
271+
*
272+
* .. code-block:: c
273+
*
274+
* void writer(void) {
275+
* spin_lock(&update_foo_lock);
276+
* {
277+
* ASSERT_EXCLUSIVE_WRITER_SCOPED(shared_foo);
278+
* WRITE_ONCE(shared_foo, 42);
279+
* ...
280+
* // shared_foo should still be 42 here!
281+
* }
282+
* spin_unlock(&update_foo_lock);
283+
* }
284+
* void buggy(void) {
285+
* if (READ_ONCE(shared_foo) == 42)
286+
* WRITE_ONCE(shared_foo, 1); // bug!
287+
* }
288+
*
289+
* @var: variable to assert on
290+
*/
291+
#define ASSERT_EXCLUSIVE_WRITER_SCOPED(var) \
292+
__ASSERT_EXCLUSIVE_SCOPED(var, KCSAN_ACCESS_ASSERT, __COUNTER__)
293+
242294
/**
243295
* ASSERT_EXCLUSIVE_ACCESS - assert no concurrent accesses to @var
244296
*
@@ -258,6 +310,9 @@ static inline void kcsan_check_access(const volatile void *ptr, size_t size,
258310
* release_for_reuse(obj);
259311
* }
260312
*
313+
* Note: ASSERT_EXCLUSIVE_ACCESS_SCOPED(), if applicable, performs more thorough
314+
* checking if a clear scope where no concurrent accesses are expected exists.
315+
*
261316
* Note: For cases where the object is freed, `KASAN <kasan.html>`_ is a better
262317
* fit to detect use-after-free bugs.
263318
*
@@ -266,10 +321,26 @@ static inline void kcsan_check_access(const volatile void *ptr, size_t size,
266321
#define ASSERT_EXCLUSIVE_ACCESS(var) \
267322
__kcsan_check_access(&(var), sizeof(var), KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT)
268323

324+
/**
325+
* ASSERT_EXCLUSIVE_ACCESS_SCOPED - assert no concurrent accesses to @var in scope
326+
*
327+
* Scoped variant of ASSERT_EXCLUSIVE_ACCESS().
328+
*
329+
* Assert that there are no concurrent accesses to @var (no readers nor writers)
330+
* for the entire duration of the scope in which it is introduced. This provides
331+
* a better way to fully cover the enclosing scope, compared to multiple
332+
* ASSERT_EXCLUSIVE_ACCESS(), and increases the likelihood for KCSAN to detect
333+
* racing accesses.
334+
*
335+
* @var: variable to assert on
336+
*/
337+
#define ASSERT_EXCLUSIVE_ACCESS_SCOPED(var) \
338+
__ASSERT_EXCLUSIVE_SCOPED(var, KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT, __COUNTER__)
339+
269340
/**
270341
* ASSERT_EXCLUSIVE_BITS - assert no concurrent writes to subset of bits in @var
271342
*
272-
* Bit-granular variant of ASSERT_EXCLUSIVE_WRITER(var).
343+
* Bit-granular variant of ASSERT_EXCLUSIVE_WRITER().
273344
*
274345
* Assert that there are no concurrent writes to a subset of bits in @var;
275346
* concurrent readers are permitted. This assertion captures more detailed

‎kernel/kcsan/debugfs.c‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ static noinline void microbenchmark(unsigned long iters)
110110
*/
111111
static long test_dummy;
112112
static long test_flags;
113+
static long test_scoped;
113114
static noinline void test_thread(unsigned long iters)
114115
{
115116
const long CHANGE_BITS = 0xff00ff00ff00ff00L;
@@ -120,7 +121,8 @@ static noinline void test_thread(unsigned long iters)
120121
memset(&current->kcsan_ctx, 0, sizeof(current->kcsan_ctx));
121122

122123
pr_info("KCSAN: %s begin | iters: %lu\n", __func__, iters);
123-
pr_info("test_dummy@%px, test_flags@%px\n", &test_dummy, &test_flags);
124+
pr_info("test_dummy@%px, test_flags@%px, test_scoped@%px,\n",
125+
&test_dummy, &test_flags, &test_scoped);
124126

125127
cycles = get_cycles();
126128
while (iters--) {
@@ -141,6 +143,18 @@ static noinline void test_thread(unsigned long iters)
141143

142144
test_flags ^= CHANGE_BITS; /* generate value-change */
143145
__kcsan_check_write(&test_flags, sizeof(test_flags));
146+
147+
BUG_ON(current->kcsan_ctx.scoped_accesses.prev);
148+
{
149+
/* Should generate reports anywhere in this block. */
150+
ASSERT_EXCLUSIVE_WRITER_SCOPED(test_scoped);
151+
ASSERT_EXCLUSIVE_ACCESS_SCOPED(test_scoped);
152+
BUG_ON(!current->kcsan_ctx.scoped_accesses.prev);
153+
/* Unrelated accesses. */
154+
__kcsan_check_access(&cycles, sizeof(cycles), 0);
155+
__kcsan_check_access(&cycles, sizeof(cycles), KCSAN_ACCESS_ATOMIC);
156+
}
157+
BUG_ON(current->kcsan_ctx.scoped_accesses.prev);
144158
}
145159
cycles = get_cycles() - cycles;
146160

0 commit comments

Comments
 (0)