Skip to content

Commit bb056c0

Browse files
sean-jcbonzini
authored andcommitted
tools: KVM: selftests: Convert clear/set_bit() to actual atomics
Convert {clear,set}_bit() to atomics as KVM's ucall implementation relies on clear_bit() being atomic, they are defined in atomic.h, and the same helpers in the kernel proper are atomic. KVM's ucall infrastructure is the only user of clear_bit() in tools/, and there are no true set_bit() users. tools/testing/nvdimm/ does make heavy use of set_bit(), but that code builds into a kernel module of sorts, i.e. pulls in all of the kernel's header and so is already getting the kernel's atomic set_bit(). Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 3629335 commit bb056c0

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

tools/arch/x86/include/asm/atomic.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ static inline int test_and_set_bit(long nr, unsigned long *addr)
7676
GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, "Ir", nr, "%0", "c");
7777
}
7878

79+
static inline int test_and_clear_bit(long nr, unsigned long *addr)
80+
{
81+
GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(btc), *addr, "Ir", nr, "%0", "c");
82+
}
83+
7984
#endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */

tools/include/asm-generic/atomic-gcc.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ static inline int test_and_set_bit(long nr, unsigned long *addr)
8181
return !!(old & mask);
8282
}
8383

84+
static inline int test_and_clear_bit(long nr, unsigned long *addr)
85+
{
86+
unsigned long mask = BIT_MASK(nr);
87+
long old;
88+
89+
addr += BIT_WORD(nr);
90+
91+
old = __sync_fetch_and_and(addr, ~mask);
92+
return !!(old & mask);
93+
}
94+
8495
#endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */

tools/include/asm-generic/bitops/atomic.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
#include <asm/types.h>
66
#include <asm/bitsperlong.h>
77

8-
static inline void set_bit(unsigned long nr, unsigned long *addr)
9-
{
10-
addr[nr / __BITS_PER_LONG] |= 1UL << (nr % __BITS_PER_LONG);
11-
}
12-
13-
static inline void clear_bit(unsigned long nr, unsigned long *addr)
14-
{
15-
addr[nr / __BITS_PER_LONG] &= ~(1UL << (nr % __BITS_PER_LONG));
16-
}
8+
/*
9+
* Just alias the test versions, all of the compiler built-in atomics "fetch",
10+
* and optimizing compile-time constants on x86 isn't worth the complexity.
11+
*/
12+
#define set_bit test_and_set_bit
13+
#define clear_bit test_and_clear_bit
1714

1815
#endif /* _TOOLS_LINUX_ASM_GENERIC_BITOPS_ATOMIC_H_ */

0 commit comments

Comments
 (0)