1 /* SPDX-License-Identifier: GPL-2.0 */
4 * This header provides generic wrappers for memory access instrumentation that
5 * the compiler cannot emit for: KASAN, KCSAN, KMSAN.
7 #ifndef _LINUX_INSTRUMENTED_H
8 #define _LINUX_INSTRUMENTED_H
10 #include <linux/compiler.h>
11 #include <linux/kasan-checks.h>
12 #include <linux/kcsan-checks.h>
13 #include <linux/kmsan-checks.h>
14 #include <linux/types.h>
17 * instrument_read - instrument regular read access
19 * Instrument a regular read access. The instrumentation should be inserted
20 * before the actual read happens.
22 * @ptr address of access
23 * @size size of access
25 static __always_inline void instrument_read(const volatile void *v, size_t size)
27 kasan_check_read(v, size);
28 kcsan_check_read(v, size);
32 * instrument_write - instrument regular write access
34 * Instrument a regular write access. The instrumentation should be inserted
35 * before the actual write happens.
37 * @ptr address of access
38 * @size size of access
40 static __always_inline void instrument_write(const volatile void *v, size_t size)
42 kasan_check_write(v, size);
43 kcsan_check_write(v, size);
47 * instrument_read_write - instrument regular read-write access
49 * Instrument a regular write access. The instrumentation should be inserted
50 * before the actual write happens.
52 * @ptr address of access
53 * @size size of access
55 static __always_inline void instrument_read_write(const volatile void *v, size_t size)
57 kasan_check_write(v, size);
58 kcsan_check_read_write(v, size);
62 * instrument_atomic_read - instrument atomic read access
64 * Instrument an atomic read access. The instrumentation should be inserted
65 * before the actual read happens.
67 * @ptr address of access
68 * @size size of access
70 static __always_inline void instrument_atomic_read(const volatile void *v, size_t size)
72 kasan_check_read(v, size);
73 kcsan_check_atomic_read(v, size);
77 * instrument_atomic_write - instrument atomic write access
79 * Instrument an atomic write access. The instrumentation should be inserted
80 * before the actual write happens.
82 * @ptr address of access
83 * @size size of access
85 static __always_inline void instrument_atomic_write(const volatile void *v, size_t size)
87 kasan_check_write(v, size);
88 kcsan_check_atomic_write(v, size);
92 * instrument_atomic_read_write - instrument atomic read-write access
94 * Instrument an atomic read-write access. The instrumentation should be
95 * inserted before the actual write happens.
97 * @ptr address of access
98 * @size size of access
100 static __always_inline void instrument_atomic_read_write(const volatile void *v, size_t size)
102 kasan_check_write(v, size);
103 kcsan_check_atomic_read_write(v, size);
107 * instrument_copy_to_user - instrument reads of copy_to_user
109 * Instrument reads from kernel memory, that are due to copy_to_user (and
110 * variants). The instrumentation must be inserted before the accesses.
112 * @to destination address
113 * @from source address
114 * @n number of bytes to copy
116 static __always_inline void
117 instrument_copy_to_user(void __user *to, const void *from, unsigned long n)
119 kasan_check_read(from, n);
120 kcsan_check_read(from, n);
121 kmsan_copy_to_user(to, from, n, 0);
125 * instrument_copy_from_user_before - add instrumentation before copy_from_user
127 * Instrument writes to kernel memory, that are due to copy_from_user (and
128 * variants). The instrumentation should be inserted before the accesses.
130 * @to destination address
131 * @from source address
132 * @n number of bytes to copy
134 static __always_inline void
135 instrument_copy_from_user_before(const void *to, const void __user *from, unsigned long n)
137 kasan_check_write(to, n);
138 kcsan_check_write(to, n);
142 * instrument_copy_from_user_after - add instrumentation after copy_from_user
144 * Instrument writes to kernel memory, that are due to copy_from_user (and
145 * variants). The instrumentation should be inserted after the accesses.
147 * @to destination address
148 * @from source address
149 * @n number of bytes to copy
150 * @left number of bytes not copied (as returned by copy_from_user)
152 static __always_inline void
153 instrument_copy_from_user_after(const void *to, const void __user *from,
154 unsigned long n, unsigned long left)
156 kmsan_unpoison_memory(to, n - left);
160 * instrument_get_user() - add instrumentation to get_user()-like macros
162 * get_user() and friends are fragile, so it may depend on the implementation
163 * whether the instrumentation happens before or after the data is copied from
166 * @to destination variable, may not be address-taken
168 #define instrument_get_user(to) \
170 u64 __tmp = (u64)(to); \
171 kmsan_unpoison_memory(&__tmp, sizeof(__tmp)); \
177 * instrument_put_user() - add instrumentation to put_user()-like macros
179 * put_user() and friends are fragile, so it may depend on the implementation
180 * whether the instrumentation happens before or after the data is copied from
183 * @from source address
184 * @ptr userspace pointer to copy to
185 * @size number of bytes to copy
187 #define instrument_put_user(from, ptr, size) \
189 kmsan_copy_to_user(ptr, &from, sizeof(from), 0); \
192 #endif /* _LINUX_INSTRUMENTED_H */