Skip to contentSkip to conversation

bcachefs: six locks: Fix lost wakeup

PATCH New Kent Overstreet 1 participant 1 message Nov 14, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcachefs: six locks: Fix lost wakeup

In percpu reader mode, trylock() for read had a lost wakeup: on failure to get the lock, we may have caused a writer to fail to get the lock, because we temporarily elevated the reader count.

We need to check for waiters after decrementing the read count - not before.

---
 fs/bcachefs/six.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/bcachefs/six.c +5 -2
index d22826cabee0..f37d5ad2c6ff 100644
@@ -163,8 +163,11 @@ static int __do_six_trylock(struct six_lock *lock, enum six_lock_type type,
 		this_cpu_sub(*lock->readers, !ret);
 		preempt_enable();
 
-		if (!ret && (old & SIX_LOCK_WAITING_write))
-			ret = -1 - SIX_LOCK_write;
+		if (!ret) {
+			smp_mb();
+			if (atomic_read(&lock->state) & SIX_LOCK_WAITING_write)
+				ret = -1 - SIX_LOCK_write;
+		}
 	} else if (type == SIX_LOCK_write && lock->readers) {
 		if (try) {
 			atomic_add(SIX_LOCK_HELD_write, &lock->state);
Signature · 1 line
2.42.0