1 /* SPDX-License-Identifier: GPL-2.0 */
4 * This file provides wrappers with sanitizer instrumentation for bit
7 * To use this functionality, an arch's bitops.h file needs to define each of
8 * the below bit operations with an arch_ prefix (e.g. arch_set_bit(),
9 * arch___set_bit(), etc.).
11 #ifndef _ASM_GENERIC_BITOPS_INSTRUMENTED_H
12 #define _ASM_GENERIC_BITOPS_INSTRUMENTED_H
14 #include <linux/kasan-checks.h>
17 * set_bit - Atomically set a bit in memory
19 * @addr: the address to start counting from
21 * This is a relaxed atomic operation (no implied memory barriers).
23 * Note that @nr may be almost arbitrarily large; this function is not
24 * restricted to acting on a single-word quantity.
26 static inline void set_bit(long nr, volatile unsigned long *addr)
28 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
29 arch_set_bit(nr, addr);
33 * __set_bit - Set a bit in memory
35 * @addr: the address to start counting from
37 * Unlike set_bit(), this function is non-atomic. If it is called on the same
38 * region of memory concurrently, the effect may be that only one operation
41 static inline void __set_bit(long nr, volatile unsigned long *addr)
43 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
44 arch___set_bit(nr, addr);
48 * clear_bit - Clears a bit in memory
50 * @addr: Address to start counting from
52 * This is a relaxed atomic operation (no implied memory barriers).
54 static inline void clear_bit(long nr, volatile unsigned long *addr)
56 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
57 arch_clear_bit(nr, addr);
61 * __clear_bit - Clears a bit in memory
62 * @nr: the bit to clear
63 * @addr: the address to start counting from
65 * Unlike clear_bit(), this function is non-atomic. If it is called on the same
66 * region of memory concurrently, the effect may be that only one operation
69 static inline void __clear_bit(long nr, volatile unsigned long *addr)
71 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
72 arch___clear_bit(nr, addr);
76 * clear_bit_unlock - Clear a bit in memory, for unlock
78 * @addr: the address to start counting from
80 * This operation is atomic and provides release barrier semantics.
82 static inline void clear_bit_unlock(long nr, volatile unsigned long *addr)
84 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
85 arch_clear_bit_unlock(nr, addr);
89 * __clear_bit_unlock - Clears a bit in memory
91 * @addr: Address to start counting from
93 * This is a non-atomic operation but implies a release barrier before the
94 * memory operation. It can be used for an unlock if no other CPUs can
95 * concurrently modify other bits in the word.
97 static inline void __clear_bit_unlock(long nr, volatile unsigned long *addr)
99 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
100 arch___clear_bit_unlock(nr, addr);
104 * change_bit - Toggle a bit in memory
106 * @addr: Address to start counting from
108 * This is a relaxed atomic operation (no implied memory barriers).
110 * Note that @nr may be almost arbitrarily large; this function is not
111 * restricted to acting on a single-word quantity.
113 static inline void change_bit(long nr, volatile unsigned long *addr)
115 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
116 arch_change_bit(nr, addr);
120 * __change_bit - Toggle a bit in memory
121 * @nr: the bit to change
122 * @addr: the address to start counting from
124 * Unlike change_bit(), this function is non-atomic. If it is called on the same
125 * region of memory concurrently, the effect may be that only one operation
128 static inline void __change_bit(long nr, volatile unsigned long *addr)
130 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
131 arch___change_bit(nr, addr);
135 * test_and_set_bit - Set a bit and return its old value
137 * @addr: Address to count from
139 * This is an atomic fully-ordered operation (implied full memory barrier).
141 static inline bool test_and_set_bit(long nr, volatile unsigned long *addr)
143 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
144 return arch_test_and_set_bit(nr, addr);
148 * __test_and_set_bit - Set a bit and return its old value
150 * @addr: Address to count from
152 * This operation is non-atomic. If two instances of this operation race, one
153 * can appear to succeed but actually fail.
155 static inline bool __test_and_set_bit(long nr, volatile unsigned long *addr)
157 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
158 return arch___test_and_set_bit(nr, addr);
162 * test_and_set_bit_lock - Set a bit and return its old value, for lock
164 * @addr: Address to count from
166 * This operation is atomic and provides acquire barrier semantics if
167 * the returned value is 0.
168 * It can be used to implement bit locks.
170 static inline bool test_and_set_bit_lock(long nr, volatile unsigned long *addr)
172 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
173 return arch_test_and_set_bit_lock(nr, addr);
177 * test_and_clear_bit - Clear a bit and return its old value
179 * @addr: Address to count from
181 * This is an atomic fully-ordered operation (implied full memory barrier).
183 static inline bool test_and_clear_bit(long nr, volatile unsigned long *addr)
185 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
186 return arch_test_and_clear_bit(nr, addr);
190 * __test_and_clear_bit - Clear a bit and return its old value
192 * @addr: Address to count from
194 * This operation is non-atomic. If two instances of this operation race, one
195 * can appear to succeed but actually fail.
197 static inline bool __test_and_clear_bit(long nr, volatile unsigned long *addr)
199 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
200 return arch___test_and_clear_bit(nr, addr);
204 * test_and_change_bit - Change a bit and return its old value
206 * @addr: Address to count from
208 * This is an atomic fully-ordered operation (implied full memory barrier).
210 static inline bool test_and_change_bit(long nr, volatile unsigned long *addr)
212 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
213 return arch_test_and_change_bit(nr, addr);
217 * __test_and_change_bit - Change a bit and return its old value
219 * @addr: Address to count from
221 * This operation is non-atomic. If two instances of this operation race, one
222 * can appear to succeed but actually fail.
224 static inline bool __test_and_change_bit(long nr, volatile unsigned long *addr)
226 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
227 return arch___test_and_change_bit(nr, addr);
231 * test_bit - Determine whether a bit is set
232 * @nr: bit number to test
233 * @addr: Address to start counting from
235 static inline bool test_bit(long nr, const volatile unsigned long *addr)
237 kasan_check_read(addr + BIT_WORD(nr), sizeof(long));
238 return arch_test_bit(nr, addr);
241 #if defined(arch_clear_bit_unlock_is_negative_byte)
243 * clear_bit_unlock_is_negative_byte - Clear a bit in memory and test if bottom
244 * byte is negative, for unlock.
245 * @nr: the bit to clear
246 * @addr: the address to start counting from
248 * This operation is atomic and provides release barrier semantics.
250 * This is a bit of a one-trick-pony for the filemap code, which clears
251 * PG_locked and tests PG_waiters,
254 clear_bit_unlock_is_negative_byte(long nr, volatile unsigned long *addr)
256 kasan_check_write(addr + BIT_WORD(nr), sizeof(long));
257 return arch_clear_bit_unlock_is_negative_byte(nr, addr);
259 /* Let everybody know we have it. */
260 #define clear_bit_unlock_is_negative_byte clear_bit_unlock_is_negative_byte
263 #endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_H */