xref: /OK3568_Linux_fs/kernel/include/asm-generic/bitops/instrumented-non-atomic.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /*
4*4882a593Smuzhiyun  * This file provides wrappers with sanitizer instrumentation for non-atomic
5*4882a593Smuzhiyun  * bit operations.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * To use this functionality, an arch's bitops.h file needs to define each of
8*4882a593Smuzhiyun  * the below bit operations with an arch_ prefix (e.g. arch_set_bit(),
9*4882a593Smuzhiyun  * arch___set_bit(), etc.).
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun #ifndef _ASM_GENERIC_BITOPS_INSTRUMENTED_NON_ATOMIC_H
12*4882a593Smuzhiyun #define _ASM_GENERIC_BITOPS_INSTRUMENTED_NON_ATOMIC_H
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/instrumented.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /**
17*4882a593Smuzhiyun  * __set_bit - Set a bit in memory
18*4882a593Smuzhiyun  * @nr: the bit to set
19*4882a593Smuzhiyun  * @addr: the address to start counting from
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * Unlike set_bit(), this function is non-atomic. If it is called on the same
22*4882a593Smuzhiyun  * region of memory concurrently, the effect may be that only one operation
23*4882a593Smuzhiyun  * succeeds.
24*4882a593Smuzhiyun  */
__set_bit(long nr,volatile unsigned long * addr)25*4882a593Smuzhiyun static inline void __set_bit(long nr, volatile unsigned long *addr)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	instrument_write(addr + BIT_WORD(nr), sizeof(long));
28*4882a593Smuzhiyun 	arch___set_bit(nr, addr);
29*4882a593Smuzhiyun }
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /**
32*4882a593Smuzhiyun  * __clear_bit - Clears a bit in memory
33*4882a593Smuzhiyun  * @nr: the bit to clear
34*4882a593Smuzhiyun  * @addr: the address to start counting from
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * Unlike clear_bit(), this function is non-atomic. If it is called on the same
37*4882a593Smuzhiyun  * region of memory concurrently, the effect may be that only one operation
38*4882a593Smuzhiyun  * succeeds.
39*4882a593Smuzhiyun  */
__clear_bit(long nr,volatile unsigned long * addr)40*4882a593Smuzhiyun static inline void __clear_bit(long nr, volatile unsigned long *addr)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	instrument_write(addr + BIT_WORD(nr), sizeof(long));
43*4882a593Smuzhiyun 	arch___clear_bit(nr, addr);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /**
47*4882a593Smuzhiyun  * __change_bit - Toggle a bit in memory
48*4882a593Smuzhiyun  * @nr: the bit to change
49*4882a593Smuzhiyun  * @addr: the address to start counting from
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * Unlike change_bit(), this function is non-atomic. If it is called on the same
52*4882a593Smuzhiyun  * region of memory concurrently, the effect may be that only one operation
53*4882a593Smuzhiyun  * succeeds.
54*4882a593Smuzhiyun  */
__change_bit(long nr,volatile unsigned long * addr)55*4882a593Smuzhiyun static inline void __change_bit(long nr, volatile unsigned long *addr)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	instrument_write(addr + BIT_WORD(nr), sizeof(long));
58*4882a593Smuzhiyun 	arch___change_bit(nr, addr);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
__instrument_read_write_bitop(long nr,volatile unsigned long * addr)61*4882a593Smuzhiyun static inline void __instrument_read_write_bitop(long nr, volatile unsigned long *addr)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC)) {
64*4882a593Smuzhiyun 		/*
65*4882a593Smuzhiyun 		 * We treat non-atomic read-write bitops a little more special.
66*4882a593Smuzhiyun 		 * Given the operations here only modify a single bit, assuming
67*4882a593Smuzhiyun 		 * non-atomicity of the writer is sufficient may be reasonable
68*4882a593Smuzhiyun 		 * for certain usage (and follows the permissible nature of the
69*4882a593Smuzhiyun 		 * assume-plain-writes-atomic rule):
70*4882a593Smuzhiyun 		 * 1. report read-modify-write races -> check read;
71*4882a593Smuzhiyun 		 * 2. do not report races with marked readers, but do report
72*4882a593Smuzhiyun 		 *    races with unmarked readers -> check "atomic" write.
73*4882a593Smuzhiyun 		 */
74*4882a593Smuzhiyun 		kcsan_check_read(addr + BIT_WORD(nr), sizeof(long));
75*4882a593Smuzhiyun 		/*
76*4882a593Smuzhiyun 		 * Use generic write instrumentation, in case other sanitizers
77*4882a593Smuzhiyun 		 * or tools are enabled alongside KCSAN.
78*4882a593Smuzhiyun 		 */
79*4882a593Smuzhiyun 		instrument_write(addr + BIT_WORD(nr), sizeof(long));
80*4882a593Smuzhiyun 	} else {
81*4882a593Smuzhiyun 		instrument_read_write(addr + BIT_WORD(nr), sizeof(long));
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /**
86*4882a593Smuzhiyun  * __test_and_set_bit - Set a bit and return its old value
87*4882a593Smuzhiyun  * @nr: Bit to set
88*4882a593Smuzhiyun  * @addr: Address to count from
89*4882a593Smuzhiyun  *
90*4882a593Smuzhiyun  * This operation is non-atomic. If two instances of this operation race, one
91*4882a593Smuzhiyun  * can appear to succeed but actually fail.
92*4882a593Smuzhiyun  */
__test_and_set_bit(long nr,volatile unsigned long * addr)93*4882a593Smuzhiyun static inline bool __test_and_set_bit(long nr, volatile unsigned long *addr)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun 	__instrument_read_write_bitop(nr, addr);
96*4882a593Smuzhiyun 	return arch___test_and_set_bit(nr, addr);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /**
100*4882a593Smuzhiyun  * __test_and_clear_bit - Clear a bit and return its old value
101*4882a593Smuzhiyun  * @nr: Bit to clear
102*4882a593Smuzhiyun  * @addr: Address to count from
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * This operation is non-atomic. If two instances of this operation race, one
105*4882a593Smuzhiyun  * can appear to succeed but actually fail.
106*4882a593Smuzhiyun  */
__test_and_clear_bit(long nr,volatile unsigned long * addr)107*4882a593Smuzhiyun static inline bool __test_and_clear_bit(long nr, volatile unsigned long *addr)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	__instrument_read_write_bitop(nr, addr);
110*4882a593Smuzhiyun 	return arch___test_and_clear_bit(nr, addr);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /**
114*4882a593Smuzhiyun  * __test_and_change_bit - Change a bit and return its old value
115*4882a593Smuzhiyun  * @nr: Bit to change
116*4882a593Smuzhiyun  * @addr: Address to count from
117*4882a593Smuzhiyun  *
118*4882a593Smuzhiyun  * This operation is non-atomic. If two instances of this operation race, one
119*4882a593Smuzhiyun  * can appear to succeed but actually fail.
120*4882a593Smuzhiyun  */
__test_and_change_bit(long nr,volatile unsigned long * addr)121*4882a593Smuzhiyun static inline bool __test_and_change_bit(long nr, volatile unsigned long *addr)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	__instrument_read_write_bitop(nr, addr);
124*4882a593Smuzhiyun 	return arch___test_and_change_bit(nr, addr);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /**
128*4882a593Smuzhiyun  * test_bit - Determine whether a bit is set
129*4882a593Smuzhiyun  * @nr: bit number to test
130*4882a593Smuzhiyun  * @addr: Address to start counting from
131*4882a593Smuzhiyun  */
test_bit(long nr,const volatile unsigned long * addr)132*4882a593Smuzhiyun static inline bool test_bit(long nr, const volatile unsigned long *addr)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	instrument_atomic_read(addr + BIT_WORD(nr), sizeof(long));
135*4882a593Smuzhiyun 	return arch_test_bit(nr, addr);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun #endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_NON_ATOMIC_H */
139