Skip to contentSkip to conversation

rqspinlock: Fix TAS fallback lock entry creation

PATCH In review Kumar Kartikeya Dwivedi 3 participants 3 messages Jan 22 – Jan 23

Subsystems (2)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

PATCH 1/1 rqspinlock: Fix TAS fallback lock entry creation

The TAS fallback can be invoked directly when queued spin locks are disabled, and through the slow path when paravirt is enabled for queued spin locks. In the latter case, the res_spin_lock macro will attempt the fast path and already hold the entry when entering the slow path. This will lead to creation of extraneous entries that are not released, which may cause false positives for deadlock detection.

Fix this by always preceding invocation of the TAS fallback in every case with the grabbing of the held lock entry, and add a comment to make note of this.

Fixes c9102a68c070 ("rqspinlock: Add a test-and-set fallback")
---
 include/asm-generic/rqspinlock.h | 2 +-
 kernel/bpf/rqspinlock.c          | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
include/asm-generic/rqspinlock.h +1 -1
index 0f2dcbbfee2f..5c5cf2f7fc39 100644
@@ -191,7 +191,7 @@ static __always_inline int res_spin_lock(rqspinlock_t *lock)
 
 #else
 
-#define res_spin_lock(lock) resilient_tas_spin_lock(lock)
+#define res_spin_lock(lock) ({ grab_held_lock_entry(lock); resilient_tas_spin_lock(lock); })
 
 #endif /* CONFIG_QUEUED_SPINLOCKS */
 
kernel/bpf/rqspinlock.c +4 -3
index f7d0c8d4644e..2fdfa828e3d3 100644
@@ -265,10 +265,11 @@ int __lockfunc resilient_tas_spin_lock(rqspinlock_t *lock)
 
 	RES_INIT_TIMEOUT(ts);
 	/*
-	 * The fast path is not invoked for the TAS fallback, so we must grab
-	 * the deadlock detection entry here.
+	 * We are either called directly from res_spin_lock after grabbing the
+	 * deadlock detection entry when queued spinlocks are disabled, or from
+	 * resilient_queued_spin_lock_slowpath after grabbing the deadlock
+	 * detection entry. No need to obtain it here.
 	 */
-	grab_held_lock_entry(lock);
 
 	/*
 	 * Since the waiting loop's time is dependent on the amount of

--
2.47.3

Tested-by on v1 1/1
#1 lore raw
Quoted 53 lines · Kumar Kartikeya Dwivedi

The TAS fallback can be invoked directly when queued spin locks are disabled, and through the slow path when paravirt is enabled for queued spin locks. In the latter case, the res_spin_lock macro will attempt the fast path and already hold the entry when entering the slow path. This will lead to creation of extraneous entries that are not released, which may cause false positives for deadlock detection.

Fix this by always preceding invocation of the TAS fallback in every case with the grabbing of the held lock entry, and add a comment to make note of this.

Fixes c9102a68c070 ("rqspinlock: Add a test-and-set fallback")
---
 include/asm-generic/rqspinlock.h | 2 +-
 kernel/bpf/rqspinlock.c          | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
include/asm-generic/rqspinlock.h +1 -1
index 0f2dcbbfee2f..5c5cf2f7fc39 100644
@@ -191,7 +191,7 @@ static __always_inline int res_spin_lock(rqspinlock_t *lock)
 
 #else
 
-#define res_spin_lock(lock) resilient_tas_spin_lock(lock)
+#define res_spin_lock(lock) ({ grab_held_lock_entry(lock); resilient_tas_spin_lock(lock); })
 
 #endif /* CONFIG_QUEUED_SPINLOCKS */
 
kernel/bpf/rqspinlock.c +4 -4
index f7d0c8d4644e..2fdfa828e3d3 100644
@@ -265,10 +265,11 @@ int __lockfunc resilient_tas_spin_lock(rqspinlock_t *lock)
 
        RES_INIT_TIMEOUT(ts);
        /*
-        * The fast path is not invoked for the TAS fallback, so we must grab
-        * the deadlock detection entry here.
+        * We are either called directly from res_spin_lock after grabbing the
+        * deadlock detection entry when queued spinlocks are disabled, or from
+        * resilient_queued_spin_lock_slowpath after grabbing the deadlock
+        * detection entry. No need to obtain it here.
         */
-       grab_held_lock_entry(lock);
 
        /*
         * Since the waiting loop's time is dependent on the amount of
--
Quoted 2 lines

2.47.3

Make sense to me. Thanks for fixing the false positive.

#2 lore raw
Quoted 11 lines · Amery Hung
Quoted 8 lines · Kumar Kartikeya Dwivedi

The TAS fallback can be invoked directly when queued spin locks are disabled, and through the slow path when paravirt is enabled for queued spin locks. In the latter case, the res_spin_lock macro will attempt the fast path and already hold the entry when entering the slow path. This will lead to creation of extraneous entries that are not released, which may cause false positives for deadlock detection.

Quoted 7 lines
Quoted 7 lines

Fix this by always preceding invocation of the TAS fallback in every case with the grabbing of the held lock entry, and add a comment to make note of this.

Fixes c9102a68c070 ("rqspinlock: Add a test-and-set fallback")

pw bot either slow or asleep.
Applied to bpf-next. I don't think it's that urgent.