tools: KVM: selftests: Convert clear/set_bit() to actual atomics
authorSean Christopherson <seanjc@google.com>
Sat, 19 Nov 2022 01:34:50 +0000 (01:34 +0000)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 2 Dec 2022 18:22:35 +0000 (13:22 -0500)
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 <seanjc@google.com>
Message-Id: <20221119013450.2643007-10-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/arch/x86/include/asm/atomic.h
tools/include/asm-generic/atomic-gcc.h
tools/include/asm-generic/bitops/atomic.h

index a42733a..365cf18 100644 (file)
@@ -76,4 +76,9 @@ static inline int test_and_set_bit(long nr, unsigned long *addr)
        GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, "Ir", nr, "%0", "c");
 }
 
+static inline int test_and_clear_bit(long nr, unsigned long *addr)
+{
+       GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(btc), *addr, "Ir", nr, "%0", "c");
+}
+
 #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */
index 37ef522..9b3c528 100644 (file)
@@ -81,4 +81,15 @@ static inline int test_and_set_bit(long nr, unsigned long *addr)
        return !!(old & mask);
 }
 
+static inline int test_and_clear_bit(long nr, unsigned long *addr)
+{
+       unsigned long mask = BIT_MASK(nr);
+       long old;
+
+       addr += BIT_WORD(nr);
+
+       old = __sync_fetch_and_and(addr, ~mask);
+       return !!(old & mask);
+}
+
 #endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */
index f64b049..ab37a22 100644 (file)
@@ -5,14 +5,11 @@
 #include <asm/types.h>
 #include <asm/bitsperlong.h>
 
-static inline void set_bit(unsigned long nr, unsigned long *addr)
-{
-       addr[nr / __BITS_PER_LONG] |= 1UL << (nr % __BITS_PER_LONG);
-}
-
-static inline void clear_bit(unsigned long nr, unsigned long *addr)
-{
-       addr[nr / __BITS_PER_LONG] &= ~(1UL << (nr % __BITS_PER_LONG));
-}
+/*
+ * Just alias the test versions, all of the compiler built-in atomics "fetch",
+ * and optimizing compile-time constants on x86 isn't worth the complexity.
+ */
+#define set_bit test_and_set_bit
+#define clear_bit test_and_clear_bit
 
 #endif /* _TOOLS_LINUX_ASM_GENERIC_BITOPS_ATOMIC_H_ */