xref: /linux/include/linux/instrumented.h (revision 48dea9a700c8728cc31a1dd44588b97578de86ee)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 /*
4  * This header provides generic wrappers for memory access instrumentation that
5  * the compiler cannot emit for: KASAN, KCSAN.
6  */
7 #ifndef _LINUX_INSTRUMENTED_H
8 #define _LINUX_INSTRUMENTED_H
9 
10 #include <linux/compiler.h>
11 #include <linux/kasan-checks.h>
12 #include <linux/kcsan-checks.h>
13 #include <linux/types.h>
14 
15 /**
16  * instrument_read - instrument regular read access
17  *
18  * Instrument a regular read access. The instrumentation should be inserted
19  * before the actual read happens.
20  *
21  * @ptr address of access
22  * @size size of access
23  */
24 static __always_inline void instrument_read(const volatile void *v, size_t size)
25 {
26 	kasan_check_read(v, size);
27 	kcsan_check_read(v, size);
28 }
29 
30 /**
31  * instrument_write - instrument regular write access
32  *
33  * Instrument a regular write access. The instrumentation should be inserted
34  * before the actual write happens.
35  *
36  * @ptr address of access
37  * @size size of access
38  */
39 static __always_inline void instrument_write(const volatile void *v, size_t size)
40 {
41 	kasan_check_write(v, size);
42 	kcsan_check_write(v, size);
43 }
44 
45 /**
46  * instrument_atomic_read - instrument atomic read access
47  *
48  * Instrument an atomic read access. The instrumentation should be inserted
49  * before the actual read happens.
50  *
51  * @ptr address of access
52  * @size size of access
53  */
54 static __always_inline void instrument_atomic_read(const volatile void *v, size_t size)
55 {
56 	kasan_check_read(v, size);
57 	kcsan_check_atomic_read(v, size);
58 }
59 
60 /**
61  * instrument_atomic_write - instrument atomic write access
62  *
63  * Instrument an atomic write access. The instrumentation should be inserted
64  * before the actual write happens.
65  *
66  * @ptr address of access
67  * @size size of access
68  */
69 static __always_inline void instrument_atomic_write(const volatile void *v, size_t size)
70 {
71 	kasan_check_write(v, size);
72 	kcsan_check_atomic_write(v, size);
73 }
74 
75 /**
76  * instrument_copy_to_user - instrument reads of copy_to_user
77  *
78  * Instrument reads from kernel memory, that are due to copy_to_user (and
79  * variants). The instrumentation must be inserted before the accesses.
80  *
81  * @to destination address
82  * @from source address
83  * @n number of bytes to copy
84  */
85 static __always_inline void
86 instrument_copy_to_user(void __user *to, const void *from, unsigned long n)
87 {
88 	kasan_check_read(from, n);
89 	kcsan_check_read(from, n);
90 }
91 
92 /**
93  * instrument_copy_from_user - instrument writes of copy_from_user
94  *
95  * Instrument writes to kernel memory, that are due to copy_from_user (and
96  * variants). The instrumentation should be inserted before the accesses.
97  *
98  * @to destination address
99  * @from source address
100  * @n number of bytes to copy
101  */
102 static __always_inline void
103 instrument_copy_from_user(const void *to, const void __user *from, unsigned long n)
104 {
105 	kasan_check_write(to, n);
106 	kcsan_check_write(to, n);
107 }
108 
109 #endif /* _LINUX_INSTRUMENTED_H */
110