@@ -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
0 commit comments