Skip to content

Commit cf4694b

Browse files
pgondasean-jc
authored andcommitted
tools: Add atomic_test_and_set_bit()
Add x86 and generic implementations of atomic_test_and_set_bit() to allow KVM selftests to atomically manage bitmaps. Note, the generic version is taken from arch_test_and_set_bit() as of commit 415d832 ("locking/atomic: Make test_and_*_bit() ordered on failure"). Signed-off-by: Peter Gonda <[email protected]> Co-developed-by: Sean Christopherson <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent dc88244 commit cf4694b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#define LOCK_PREFIX "\n\tlock; "
1010

11+
#include <asm/asm.h>
1112
#include <asm/cmpxchg.h>
1213

1314
/*
@@ -70,4 +71,10 @@ static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
7071
return cmpxchg(&v->counter, old, new);
7172
}
7273

74+
static inline int atomic_test_and_set_bit(long nr, unsigned long *addr)
75+
{
76+
GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, "Ir", nr, "%0", "c");
77+
78+
}
79+
7380
#endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <linux/compiler.h>
66
#include <linux/types.h>
7+
#include <linux/bitops.h>
78

89
/*
910
* Atomic operations that C can't guarantee us. Useful for
@@ -69,4 +70,15 @@ static inline int atomic_cmpxchg(atomic_t *v, int oldval, int newval)
6970
return cmpxchg(&(v)->counter, oldval, newval);
7071
}
7172

73+
static inline int atomic_test_and_set_bit(long nr, unsigned long *addr)
74+
{
75+
unsigned long mask = BIT_MASK(nr);
76+
long old;
77+
78+
addr += BIT_WORD(nr);
79+
80+
old = __sync_fetch_and_or(addr, mask);
81+
return !!(old & mask);
82+
}
83+
7284
#endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */

0 commit comments

Comments
 (0)