Skip to content

Commit

Permalink
tools: Add atomic_test_and_set_bit()
Browse files Browse the repository at this point in the history
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 <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
  • Loading branch information
Peter Gonda authored and Sean Christopherson committed Nov 17, 2022
1 parent dc88244 commit cf4694b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tools/arch/x86/include/asm/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#define LOCK_PREFIX "\n\tlock; "

#include <asm/asm.h>
#include <asm/cmpxchg.h>

/*
Expand Down Expand Up @@ -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 */
12 changes: 12 additions & 0 deletions tools/include/asm-generic/atomic-gcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */

0 comments on commit cf4694b

Please sign in to comment.