]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
bit_spinlocks now use futexes
authorKent Overstreet <kent.overstreet@gmail.com>
Mon, 26 Apr 2021 23:40:09 +0000 (19:40 -0400)
committerKent Overstreet <kent.overstreet@gmail.com>
Tue, 27 Apr 2021 00:18:47 +0000 (20:18 -0400)
Spinlocks aren't a good idea in userspace, where we can't actually
disable preemption.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
include/linux/bit_spinlock.h
include/linux/rhashtable.h

index 0e88820ad04f684a131893ce534cbc37e3e75512..ed47cc6371782924b162c6f8eec56dafb48abe08 100644 (file)
@@ -3,38 +3,40 @@
 
 #include <linux/kernel.h>
 #include <linux/preempt.h>
-#include <linux/atomic.h>
-#include <linux/bug.h>
+#include <linux/futex.h>
 
-static inline void bit_spin_lock(int bitnum, unsigned long *addr)
+static inline void bit_spin_lock(int nr, unsigned long *_addr)
 {
-       while (unlikely(test_and_set_bit_lock(bitnum, addr))) {
-               do {
-                       cpu_relax();
-               } while (test_bit(bitnum, addr));
-       }
-}
+       u32 mask, *addr = ((u32 *) _addr) + (nr / 32), v;
 
-static inline int bit_spin_trylock(int bitnum, unsigned long *addr)
-{
-       return !test_and_set_bit_lock(bitnum, addr);
-}
+       nr &= 31;
+       mask = 1U << nr;
 
-static inline void bit_spin_unlock(int bitnum, unsigned long *addr)
-{
-       BUG_ON(!test_bit(bitnum, addr));
+       while (1) {
+               v = __atomic_fetch_or(addr, mask, __ATOMIC_ACQUIRE);
+               if (!(v & mask))
+                       break;
 
-       clear_bit_unlock(bitnum, addr);
+               futex(addr, FUTEX_WAIT|FUTEX_PRIVATE_FLAG, v, NULL, NULL, 0);
+       }
 }
 
-static inline void __bit_spin_unlock(int bitnum, unsigned long *addr)
+static inline void bit_spin_wake(int nr, unsigned long *_addr)
 {
-       bit_spin_unlock(bitnum, addr);
+       u32 *addr = ((u32 *) _addr) + (nr / 32);
+
+       futex(addr, FUTEX_WAKE|FUTEX_PRIVATE_FLAG, INT_MAX, NULL, NULL, 0);
 }
 
-static inline int bit_spin_is_locked(int bitnum, unsigned long *addr)
+static inline void bit_spin_unlock(int nr, unsigned long *_addr)
 {
-       return test_bit(bitnum, addr);
+       u32 mask, *addr = ((u32 *) _addr) + (nr / 32);
+
+       nr &= 31;
+       mask = 1U << nr;
+
+       __atomic_and_fetch(addr, ~mask, __ATOMIC_RELEASE);
+       futex(addr, FUTEX_WAKE|FUTEX_PRIVATE_FLAG, INT_MAX, NULL, NULL, 0);
 }
 
 #endif /* __LINUX_BIT_SPINLOCK_H */
index 6cf8c2571160cc8019aead4af8b8dff5f2f11914..c5e717bf937fda86e697f48f375732341a7dad79 100644 (file)
@@ -395,6 +395,7 @@ static inline void rht_assign_unlock(struct bucket_table *tbl,
        rcu_assign_pointer(*bkt, (void *)obj);
        preempt_enable();
        __release(bitlock);
+       bit_spin_wake(0, (unsigned long *) bkt);
 }
 
 /**