1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun /*
4*4882a593Smuzhiyun * This file provides wrappers with sanitizer instrumentation for bit
5*4882a593Smuzhiyun * locking 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_LOCK_H
12*4882a593Smuzhiyun #define _ASM_GENERIC_BITOPS_INSTRUMENTED_LOCK_H
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/instrumented.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /**
17*4882a593Smuzhiyun * clear_bit_unlock - Clear a bit in memory, for unlock
18*4882a593Smuzhiyun * @nr: the bit to set
19*4882a593Smuzhiyun * @addr: the address to start counting from
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * This operation is atomic and provides release barrier semantics.
22*4882a593Smuzhiyun */
clear_bit_unlock(long nr,volatile unsigned long * addr)23*4882a593Smuzhiyun static inline void clear_bit_unlock(long nr, volatile unsigned long *addr)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun instrument_atomic_write(addr + BIT_WORD(nr), sizeof(long));
26*4882a593Smuzhiyun arch_clear_bit_unlock(nr, addr);
27*4882a593Smuzhiyun }
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun * __clear_bit_unlock - Clears a bit in memory
31*4882a593Smuzhiyun * @nr: Bit to clear
32*4882a593Smuzhiyun * @addr: Address to start counting from
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * This is a non-atomic operation but implies a release barrier before the
35*4882a593Smuzhiyun * memory operation. It can be used for an unlock if no other CPUs can
36*4882a593Smuzhiyun * concurrently modify other bits in the word.
37*4882a593Smuzhiyun */
__clear_bit_unlock(long nr,volatile unsigned long * addr)38*4882a593Smuzhiyun static inline void __clear_bit_unlock(long nr, volatile unsigned long *addr)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun instrument_write(addr + BIT_WORD(nr), sizeof(long));
41*4882a593Smuzhiyun arch___clear_bit_unlock(nr, addr);
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /**
45*4882a593Smuzhiyun * test_and_set_bit_lock - Set a bit and return its old value, for lock
46*4882a593Smuzhiyun * @nr: Bit to set
47*4882a593Smuzhiyun * @addr: Address to count from
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * This operation is atomic and provides acquire barrier semantics if
50*4882a593Smuzhiyun * the returned value is 0.
51*4882a593Smuzhiyun * It can be used to implement bit locks.
52*4882a593Smuzhiyun */
test_and_set_bit_lock(long nr,volatile unsigned long * addr)53*4882a593Smuzhiyun static inline bool test_and_set_bit_lock(long nr, volatile unsigned long *addr)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun instrument_atomic_read_write(addr + BIT_WORD(nr), sizeof(long));
56*4882a593Smuzhiyun return arch_test_and_set_bit_lock(nr, addr);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun #if defined(arch_clear_bit_unlock_is_negative_byte)
60*4882a593Smuzhiyun /**
61*4882a593Smuzhiyun * clear_bit_unlock_is_negative_byte - Clear a bit in memory and test if bottom
62*4882a593Smuzhiyun * byte is negative, for unlock.
63*4882a593Smuzhiyun * @nr: the bit to clear
64*4882a593Smuzhiyun * @addr: the address to start counting from
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * This operation is atomic and provides release barrier semantics.
67*4882a593Smuzhiyun *
68*4882a593Smuzhiyun * This is a bit of a one-trick-pony for the filemap code, which clears
69*4882a593Smuzhiyun * PG_locked and tests PG_waiters,
70*4882a593Smuzhiyun */
71*4882a593Smuzhiyun static inline bool
clear_bit_unlock_is_negative_byte(long nr,volatile unsigned long * addr)72*4882a593Smuzhiyun clear_bit_unlock_is_negative_byte(long nr, volatile unsigned long *addr)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun instrument_atomic_write(addr + BIT_WORD(nr), sizeof(long));
75*4882a593Smuzhiyun return arch_clear_bit_unlock_is_negative_byte(nr, addr);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun /* Let everybody know we have it. */
78*4882a593Smuzhiyun #define clear_bit_unlock_is_negative_byte clear_bit_unlock_is_negative_byte
79*4882a593Smuzhiyun #endif
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun #endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_LOCK_H */
82