1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Special rules for ignoring entire classes of data-racy memory accesses. None
4 * of the rules here imply that such data races are generally safe!
6 * All rules in this file can be configured via CONFIG_KCSAN_PERMISSIVE. Keep
7 * them separate from core code to make it easier to audit.
9 * Copyright (C) 2019, Google LLC.
12 #ifndef _KERNEL_KCSAN_PERMISSIVE_H
13 #define _KERNEL_KCSAN_PERMISSIVE_H
15 #include <linux/bitops.h>
16 #include <linux/sched.h>
17 #include <linux/types.h>
20 * Access ignore rules based on address.
22 static __always_inline bool kcsan_ignore_address(const volatile void *ptr)
24 if (!IS_ENABLED(CONFIG_KCSAN_PERMISSIVE))
28 * Data-racy bitops on current->flags are too common, ignore completely
31 return ptr == ¤t->flags;
35 * Data race ignore rules based on access type and value change patterns.
38 kcsan_ignore_data_race(size_t size, int type, u64 old, u64 new, u64 diff)
40 if (!IS_ENABLED(CONFIG_KCSAN_PERMISSIVE))
44 * Rules here are only for plain read accesses, so that we still report
45 * data races between plain read-write accesses.
47 if (type || size > sizeof(long))
51 * A common pattern is checking/setting just 1 bit in a variable; for
54 * if (flags & SOME_FLAG) { ... }
56 * and elsewhere flags is updated concurrently:
58 * flags |= SOME_OTHER_FLAG; // just 1 bit
60 * While it is still recommended that such accesses be marked
61 * appropriately, in many cases these types of data races are so common
62 * that marking them all is often unrealistic and left to maintainer
65 * The assumption in all cases is that with all known compiler
66 * optimizations (including those that tear accesses), because no more
67 * than 1 bit changed, the plain accesses are safe despite the presence
70 * The rules here will ignore the data races if we observe no more than
73 * Of course many operations can effecively change just 1 bit, but the
74 * general assuption that data races involving 1-bit changes can be
75 * tolerated still applies.
77 * And in case a true bug is missed, the bug likely manifests as a
78 * reportable data race elsewhere.
80 if (hweight64(diff) == 1) {
82 * Exception: Report data races where the values look like
83 * ordinary booleans (one of them was 0 and the 0th bit was
84 * changed) More often than not, they come with interesting
85 * memory ordering requirements, so let's report them.
87 if (!((!old || !new) && diff == 1))
94 #endif /* _KERNEL_KCSAN_PERMISSIVE_H */