Commit cf4694be authored by Peter Gonda's avatar Peter Gonda Committed by Sean Christopherson
Browse files

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

Signed-off-by: default avatarPeter Gonda <pgonda@google.com>
Co-developed-by: default avatarSean Christopherson <seanjc@google.com>
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/20221006003409.649993-5-seanjc@google.com
parent dc88244b
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -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 */
+12 −0
Original line number Diff line number Diff line
@@ -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 */