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 415d832497 ("locking/atomic: Make test_and_*_bit() ordered on
failure").

Signed-off-by: Peter Gonda <pgonda@google.com>
Co-developed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/20221006003409.649993-5-seanjc@google.com
This commit is contained in:
Peter Gonda 2022-10-06 00:34:06 +00:00 committed by Sean Christopherson
parent dc88244bf5
commit cf4694be2b
2 changed files with 19 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#define LOCK_PREFIX "\n\tlock; "
#include <asm/asm.h>
#include <asm/cmpxchg.h>
/*
@ -70,4 +71,10 @@ static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
return cmpxchg(&v->counter, old, new);
}
static inline int atomic_test_and_set_bit(long nr, unsigned long *addr)
{
GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, "Ir", nr, "%0", "c");
}
#endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */

View File

@ -4,6 +4,7 @@
#include <linux/compiler.h>
#include <linux/types.h>
#include <linux/bitops.h>
/*
* 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)
return cmpxchg(&(v)->counter, oldval, newval);
}
static inline int atomic_test_and_set_bit(long nr, unsigned long *addr)
{
unsigned long mask = BIT_MASK(nr);
long old;
addr += BIT_WORD(nr);
old = __sync_fetch_and_or(addr, mask);
return !!(old & mask);
}
#endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */