1 /* SPDX-License-Identifier: GPL-2.0 */
3 * KCSAN access checks and modifiers. These can be used to explicitly check
4 * uninstrumented accesses, or change KCSAN checking behaviour of accesses.
6 * Copyright (C) 2019, Google LLC.
9 #ifndef _LINUX_KCSAN_CHECKS_H
10 #define _LINUX_KCSAN_CHECKS_H
12 /* Note: Only include what is already included by compiler.h. */
13 #include <linux/compiler_attributes.h>
14 #include <linux/types.h>
16 /* Access types -- if KCSAN_ACCESS_WRITE is not set, the access is a read. */
17 #define KCSAN_ACCESS_WRITE (1 << 0) /* Access is a write. */
18 #define KCSAN_ACCESS_COMPOUND (1 << 1) /* Compounded read-write instrumentation. */
19 #define KCSAN_ACCESS_ATOMIC (1 << 2) /* Access is atomic. */
20 /* The following are special, and never due to compiler instrumentation. */
21 #define KCSAN_ACCESS_ASSERT (1 << 3) /* Access is an assertion. */
22 #define KCSAN_ACCESS_SCOPED (1 << 4) /* Access is a scoped access. */
25 * __kcsan_*: Always calls into the runtime when KCSAN is enabled. This may be used
26 * even in compilation units that selectively disable KCSAN, but must use KCSAN
27 * to validate access to an address. Never use these in header files!
31 * __kcsan_check_access - check generic access for races
33 * @ptr: address of access
34 * @size: size of access
35 * @type: access type modifier
37 void __kcsan_check_access(const volatile void *ptr, size_t size, int type);
40 * kcsan_disable_current - disable KCSAN for the current context
44 void kcsan_disable_current(void);
47 * kcsan_enable_current - re-enable KCSAN for the current context
51 void kcsan_enable_current(void);
52 void kcsan_enable_current_nowarn(void); /* Safe in uaccess regions. */
55 * kcsan_nestable_atomic_begin - begin nestable atomic region
57 * Accesses within the atomic region may appear to race with other accesses but
58 * should be considered atomic.
60 void kcsan_nestable_atomic_begin(void);
63 * kcsan_nestable_atomic_end - end nestable atomic region
65 void kcsan_nestable_atomic_end(void);
68 * kcsan_flat_atomic_begin - begin flat atomic region
70 * Accesses within the atomic region may appear to race with other accesses but
71 * should be considered atomic.
73 void kcsan_flat_atomic_begin(void);
76 * kcsan_flat_atomic_end - end flat atomic region
78 void kcsan_flat_atomic_end(void);
81 * kcsan_atomic_next - consider following accesses as atomic
83 * Force treating the next n memory accesses for the current context as atomic
86 * @n: number of following memory accesses to treat as atomic.
88 void kcsan_atomic_next(int n);
91 * kcsan_set_access_mask - set access mask
93 * Set the access mask for all accesses for the current context if non-zero.
94 * Only value changes to bits set in the mask will be reported.
98 void kcsan_set_access_mask(unsigned long mask);
100 /* Scoped access information. */
101 struct kcsan_scoped_access {
102 struct list_head list;
103 const volatile void *ptr;
108 * Automatically call kcsan_end_scoped_access() when kcsan_scoped_access goes
109 * out of scope; relies on attribute "cleanup", which is supported by all
110 * compilers that support KCSAN.
112 #define __kcsan_cleanup_scoped \
113 __maybe_unused __attribute__((__cleanup__(kcsan_end_scoped_access)))
116 * kcsan_begin_scoped_access - begin scoped access
118 * Begin scoped access and initialize @sa, which will cause KCSAN to
119 * continuously check the memory range in the current thread until
120 * kcsan_end_scoped_access() is called for @sa.
122 * Scoped accesses are implemented by appending @sa to an internal list for the
123 * current execution context, and then checked on every call into the KCSAN
126 * @ptr: address of access
127 * @size: size of access
128 * @type: access type modifier
129 * @sa: struct kcsan_scoped_access to use for the scope of the access
131 struct kcsan_scoped_access *
132 kcsan_begin_scoped_access(const volatile void *ptr, size_t size, int type,
133 struct kcsan_scoped_access *sa);
136 * kcsan_end_scoped_access - end scoped access
138 * End a scoped access, which will stop KCSAN checking the memory range.
139 * Requires that kcsan_begin_scoped_access() was previously called once for @sa.
141 * @sa: a previously initialized struct kcsan_scoped_access
143 void kcsan_end_scoped_access(struct kcsan_scoped_access *sa);
146 #else /* CONFIG_KCSAN */
148 static inline void __kcsan_check_access(const volatile void *ptr, size_t size,
151 static inline void kcsan_disable_current(void) { }
152 static inline void kcsan_enable_current(void) { }
153 static inline void kcsan_enable_current_nowarn(void) { }
154 static inline void kcsan_nestable_atomic_begin(void) { }
155 static inline void kcsan_nestable_atomic_end(void) { }
156 static inline void kcsan_flat_atomic_begin(void) { }
157 static inline void kcsan_flat_atomic_end(void) { }
158 static inline void kcsan_atomic_next(int n) { }
159 static inline void kcsan_set_access_mask(unsigned long mask) { }
161 struct kcsan_scoped_access { };
162 #define __kcsan_cleanup_scoped __maybe_unused
163 static inline struct kcsan_scoped_access *
164 kcsan_begin_scoped_access(const volatile void *ptr, size_t size, int type,
165 struct kcsan_scoped_access *sa) { return sa; }
166 static inline void kcsan_end_scoped_access(struct kcsan_scoped_access *sa) { }
168 #endif /* CONFIG_KCSAN */
170 #ifdef __SANITIZE_THREAD__
172 * Only calls into the runtime when the particular compilation unit has KCSAN
173 * instrumentation enabled. May be used in header files.
175 #define kcsan_check_access __kcsan_check_access
178 * Only use these to disable KCSAN for accesses in the current compilation unit;
179 * calls into libraries may still perform KCSAN checks.
181 #define __kcsan_disable_current kcsan_disable_current
182 #define __kcsan_enable_current kcsan_enable_current_nowarn
184 static inline void kcsan_check_access(const volatile void *ptr, size_t size,
186 static inline void __kcsan_enable_current(void) { }
187 static inline void __kcsan_disable_current(void) { }
191 * __kcsan_check_read - check regular read access for races
193 * @ptr: address of access
194 * @size: size of access
196 #define __kcsan_check_read(ptr, size) __kcsan_check_access(ptr, size, 0)
199 * __kcsan_check_write - check regular write access for races
201 * @ptr: address of access
202 * @size: size of access
204 #define __kcsan_check_write(ptr, size) \
205 __kcsan_check_access(ptr, size, KCSAN_ACCESS_WRITE)
208 * __kcsan_check_read_write - check regular read-write access for races
210 * @ptr: address of access
211 * @size: size of access
213 #define __kcsan_check_read_write(ptr, size) \
214 __kcsan_check_access(ptr, size, KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE)
217 * kcsan_check_read - check regular read access for races
219 * @ptr: address of access
220 * @size: size of access
222 #define kcsan_check_read(ptr, size) kcsan_check_access(ptr, size, 0)
225 * kcsan_check_write - check regular write access for races
227 * @ptr: address of access
228 * @size: size of access
230 #define kcsan_check_write(ptr, size) \
231 kcsan_check_access(ptr, size, KCSAN_ACCESS_WRITE)
234 * kcsan_check_read_write - check regular read-write access for races
236 * @ptr: address of access
237 * @size: size of access
239 #define kcsan_check_read_write(ptr, size) \
240 kcsan_check_access(ptr, size, KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE)
243 * Check for atomic accesses: if atomic accesses are not ignored, this simply
244 * aliases to kcsan_check_access(), otherwise becomes a no-op.
246 #ifdef CONFIG_KCSAN_IGNORE_ATOMICS
247 #define kcsan_check_atomic_read(...) do { } while (0)
248 #define kcsan_check_atomic_write(...) do { } while (0)
249 #define kcsan_check_atomic_read_write(...) do { } while (0)
251 #define kcsan_check_atomic_read(ptr, size) \
252 kcsan_check_access(ptr, size, KCSAN_ACCESS_ATOMIC)
253 #define kcsan_check_atomic_write(ptr, size) \
254 kcsan_check_access(ptr, size, KCSAN_ACCESS_ATOMIC | KCSAN_ACCESS_WRITE)
255 #define kcsan_check_atomic_read_write(ptr, size) \
256 kcsan_check_access(ptr, size, KCSAN_ACCESS_ATOMIC | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_COMPOUND)
260 * ASSERT_EXCLUSIVE_WRITER - assert no concurrent writes to @var
262 * Assert that there are no concurrent writes to @var; other readers are
263 * allowed. This assertion can be used to specify properties of concurrent code,
264 * where violation cannot be detected as a normal data race.
266 * For example, if we only have a single writer, but multiple concurrent
267 * readers, to avoid data races, all these accesses must be marked; even
268 * concurrent marked writes racing with the single writer are bugs.
269 * Unfortunately, due to being marked, they are no longer data races. For cases
270 * like these, we can use the macro as follows:
274 * void writer(void) {
275 * spin_lock(&update_foo_lock);
276 * ASSERT_EXCLUSIVE_WRITER(shared_foo);
277 * WRITE_ONCE(shared_foo, ...);
278 * spin_unlock(&update_foo_lock);
280 * void reader(void) {
281 * // update_foo_lock does not need to be held!
282 * ... = READ_ONCE(shared_foo);
285 * Note: ASSERT_EXCLUSIVE_WRITER_SCOPED(), if applicable, performs more thorough
286 * checking if a clear scope where no concurrent writes are expected exists.
288 * @var: variable to assert on
290 #define ASSERT_EXCLUSIVE_WRITER(var) \
291 __kcsan_check_access(&(var), sizeof(var), KCSAN_ACCESS_ASSERT)
294 * Helper macros for implementation of for ASSERT_EXCLUSIVE_*_SCOPED(). @id is
295 * expected to be unique for the scope in which instances of kcsan_scoped_access
298 #define __kcsan_scoped_name(c, suffix) __kcsan_scoped_##c##suffix
299 #define __ASSERT_EXCLUSIVE_SCOPED(var, type, id) \
300 struct kcsan_scoped_access __kcsan_scoped_name(id, _) \
301 __kcsan_cleanup_scoped; \
302 struct kcsan_scoped_access *__kcsan_scoped_name(id, _dummy_p) \
303 __maybe_unused = kcsan_begin_scoped_access( \
304 &(var), sizeof(var), KCSAN_ACCESS_SCOPED | (type), \
305 &__kcsan_scoped_name(id, _))
308 * ASSERT_EXCLUSIVE_WRITER_SCOPED - assert no concurrent writes to @var in scope
310 * Scoped variant of ASSERT_EXCLUSIVE_WRITER().
312 * Assert that there are no concurrent writes to @var for the duration of the
313 * scope in which it is introduced. This provides a better way to fully cover
314 * the enclosing scope, compared to multiple ASSERT_EXCLUSIVE_WRITER(), and
315 * increases the likelihood for KCSAN to detect racing accesses.
317 * For example, it allows finding race-condition bugs that only occur due to
318 * state changes within the scope itself:
322 * void writer(void) {
323 * spin_lock(&update_foo_lock);
325 * ASSERT_EXCLUSIVE_WRITER_SCOPED(shared_foo);
326 * WRITE_ONCE(shared_foo, 42);
328 * // shared_foo should still be 42 here!
330 * spin_unlock(&update_foo_lock);
333 * if (READ_ONCE(shared_foo) == 42)
334 * WRITE_ONCE(shared_foo, 1); // bug!
337 * @var: variable to assert on
339 #define ASSERT_EXCLUSIVE_WRITER_SCOPED(var) \
340 __ASSERT_EXCLUSIVE_SCOPED(var, KCSAN_ACCESS_ASSERT, __COUNTER__)
343 * ASSERT_EXCLUSIVE_ACCESS - assert no concurrent accesses to @var
345 * Assert that there are no concurrent accesses to @var (no readers nor
346 * writers). This assertion can be used to specify properties of concurrent
347 * code, where violation cannot be detected as a normal data race.
349 * For example, where exclusive access is expected after determining no other
350 * users of an object are left, but the object is not actually freed. We can
351 * check that this property actually holds as follows:
355 * if (refcount_dec_and_test(&obj->refcnt)) {
356 * ASSERT_EXCLUSIVE_ACCESS(*obj);
357 * do_some_cleanup(obj);
358 * release_for_reuse(obj);
363 * 1. ASSERT_EXCLUSIVE_ACCESS_SCOPED(), if applicable, performs more thorough
364 * checking if a clear scope where no concurrent accesses are expected exists.
366 * 2. For cases where the object is freed, `KASAN <kasan.html>`_ is a better
367 * fit to detect use-after-free bugs.
369 * @var: variable to assert on
371 #define ASSERT_EXCLUSIVE_ACCESS(var) \
372 __kcsan_check_access(&(var), sizeof(var), KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT)
375 * ASSERT_EXCLUSIVE_ACCESS_SCOPED - assert no concurrent accesses to @var in scope
377 * Scoped variant of ASSERT_EXCLUSIVE_ACCESS().
379 * Assert that there are no concurrent accesses to @var (no readers nor writers)
380 * for the entire duration of the scope in which it is introduced. This provides
381 * a better way to fully cover the enclosing scope, compared to multiple
382 * ASSERT_EXCLUSIVE_ACCESS(), and increases the likelihood for KCSAN to detect
385 * @var: variable to assert on
387 #define ASSERT_EXCLUSIVE_ACCESS_SCOPED(var) \
388 __ASSERT_EXCLUSIVE_SCOPED(var, KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT, __COUNTER__)
391 * ASSERT_EXCLUSIVE_BITS - assert no concurrent writes to subset of bits in @var
393 * Bit-granular variant of ASSERT_EXCLUSIVE_WRITER().
395 * Assert that there are no concurrent writes to a subset of bits in @var;
396 * concurrent readers are permitted. This assertion captures more detailed
397 * bit-level properties, compared to the other (word granularity) assertions.
398 * Only the bits set in @mask are checked for concurrent modifications, while
399 * ignoring the remaining bits, i.e. concurrent writes (or reads) to ~mask bits
402 * Use this for variables, where some bits must not be modified concurrently,
403 * yet other bits are expected to be modified concurrently.
405 * For example, variables where, after initialization, some bits are read-only,
406 * but other bits may still be modified concurrently. A reader may wish to
407 * assert that this is true as follows:
411 * ASSERT_EXCLUSIVE_BITS(flags, READ_ONLY_MASK);
412 * foo = (READ_ONCE(flags) & READ_ONLY_MASK) >> READ_ONLY_SHIFT;
414 * Note: The access that immediately follows ASSERT_EXCLUSIVE_BITS() is assumed
415 * to access the masked bits only, and KCSAN optimistically assumes it is
416 * therefore safe, even in the presence of data races, and marking it with
417 * READ_ONCE() is optional from KCSAN's point-of-view. We caution, however, that
418 * it may still be advisable to do so, since we cannot reason about all compiler
419 * optimizations when it comes to bit manipulations (on the reader and writer
420 * side). If you are sure nothing can go wrong, we can write the above simply
425 * ASSERT_EXCLUSIVE_BITS(flags, READ_ONLY_MASK);
426 * foo = (flags & READ_ONLY_MASK) >> READ_ONLY_SHIFT;
428 * Another example, where this may be used, is when certain bits of @var may
429 * only be modified when holding the appropriate lock, but other bits may still
430 * be modified concurrently. Writers, where other bits may change concurrently,
431 * could use the assertion as follows:
435 * spin_lock(&foo_lock);
436 * ASSERT_EXCLUSIVE_BITS(flags, FOO_MASK);
438 * new_flags = (old_flags & ~FOO_MASK) | (new_foo << FOO_SHIFT);
439 * if (cmpxchg(&flags, old_flags, new_flags) != old_flags) { ... }
440 * spin_unlock(&foo_lock);
442 * @var: variable to assert on
443 * @mask: only check for modifications to bits set in @mask
445 #define ASSERT_EXCLUSIVE_BITS(var, mask) \
447 kcsan_set_access_mask(mask); \
448 __kcsan_check_access(&(var), sizeof(var), KCSAN_ACCESS_ASSERT);\
449 kcsan_set_access_mask(0); \
450 kcsan_atomic_next(1); \
453 #endif /* _LINUX_KCSAN_CHECKS_H */