xref: /linux-6.15/include/linux/kcsan-checks.h (revision dfd402a4)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef _LINUX_KCSAN_CHECKS_H
4 #define _LINUX_KCSAN_CHECKS_H
5 
6 #include <linux/types.h>
7 
8 /*
9  * Access type modifiers.
10  */
11 #define KCSAN_ACCESS_WRITE 0x1
12 #define KCSAN_ACCESS_ATOMIC 0x2
13 
14 /*
15  * __kcsan_*: Always calls into runtime when KCSAN is enabled. This may be used
16  * even in compilation units that selectively disable KCSAN, but must use KCSAN
17  * to validate access to an address.   Never use these in header files!
18  */
19 #ifdef CONFIG_KCSAN
20 /**
21  * __kcsan_check_access - check generic access for data race
22  *
23  * @ptr address of access
24  * @size size of access
25  * @type access type modifier
26  */
27 void __kcsan_check_access(const volatile void *ptr, size_t size, int type);
28 
29 #else
30 static inline void __kcsan_check_access(const volatile void *ptr, size_t size,
31 					int type) { }
32 #endif
33 
34 /*
35  * kcsan_*: Only calls into runtime when the particular compilation unit has
36  * KCSAN instrumentation enabled. May be used in header files.
37  */
38 #ifdef __SANITIZE_THREAD__
39 #define kcsan_check_access __kcsan_check_access
40 #else
41 static inline void kcsan_check_access(const volatile void *ptr, size_t size,
42 				      int type) { }
43 #endif
44 
45 /**
46  * __kcsan_check_read - check regular read access for data races
47  *
48  * @ptr address of access
49  * @size size of access
50  */
51 #define __kcsan_check_read(ptr, size) __kcsan_check_access(ptr, size, 0)
52 
53 /**
54  * __kcsan_check_write - check regular write access for data races
55  *
56  * @ptr address of access
57  * @size size of access
58  */
59 #define __kcsan_check_write(ptr, size)                                         \
60 	__kcsan_check_access(ptr, size, KCSAN_ACCESS_WRITE)
61 
62 /**
63  * kcsan_check_read - check regular read access for data races
64  *
65  * @ptr address of access
66  * @size size of access
67  */
68 #define kcsan_check_read(ptr, size) kcsan_check_access(ptr, size, 0)
69 
70 /**
71  * kcsan_check_write - check regular write access for data races
72  *
73  * @ptr address of access
74  * @size size of access
75  */
76 #define kcsan_check_write(ptr, size)                                           \
77 	kcsan_check_access(ptr, size, KCSAN_ACCESS_WRITE)
78 
79 /*
80  * Check for atomic accesses: if atomic access are not ignored, this simply
81  * aliases to kcsan_check_access, otherwise becomes a no-op.
82  */
83 #ifdef CONFIG_KCSAN_IGNORE_ATOMICS
84 #define kcsan_check_atomic_read(...)                                           \
85 	do {                                                                   \
86 	} while (0)
87 #define kcsan_check_atomic_write(...)                                          \
88 	do {                                                                   \
89 	} while (0)
90 #else
91 #define kcsan_check_atomic_read(ptr, size)                                     \
92 	kcsan_check_access(ptr, size, KCSAN_ACCESS_ATOMIC)
93 #define kcsan_check_atomic_write(ptr, size)                                    \
94 	kcsan_check_access(ptr, size, KCSAN_ACCESS_ATOMIC | KCSAN_ACCESS_WRITE)
95 #endif
96 
97 #endif /* _LINUX_KCSAN_CHECKS_H */
98