Skip to contentSkip to conversation

rqspinlock: Introduce res_spin_trylock

PATCH In review Kumar Kartikeya Dwivedi 2 participants 2 messages Nov 25, 2025 – Nov 26, 2025

Subsystems (2)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

PATCH 1/1 rqspinlock: Introduce res_spin_trylock

A trylock variant for rqspinlock was missing owing to lack of users in the tree thus far, add one now as it would be needed in subsequent patches. Mark as __must_check and __always_inline.

This essentially copies queued_spin_trylock, but doesn't depend on it as rqspinlock compiles down to a TAS when CONFIG_QUEUED_SPINLOCKS=n.

---
 include/asm-generic/rqspinlock.h | 45 ++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
include/asm-generic/rqspinlock.h +45 -0
index 6d4244d643df3..a7f4b7c0fb78a 100644
@@ -217,12 +217,57 @@ static __always_inline void res_spin_unlock(rqspinlock_t *lock)
 	this_cpu_dec(rqspinlock_held_locks.cnt);
 }
 
+/**
+ * res_spin_trylock - try to acquire a queued spinlock
+ * @lock: Pointer to queued spinlock structure
+ *
+ * Attempts to acquire the lock without blocking. This function should be used
+ * in contexts where blocking is not allowed (e.g., NMI handlers).
+ *
+ * Return:
+ * * 1    - Lock was acquired successfully.
+ * * 0    - Lock acquisition failed.
+ */
+static __must_check __always_inline int res_spin_trylock(rqspinlock_t *lock)
+{
+	int val = atomic_read(&lock->val);
+	int ret;
+
+	if (unlikely(val))
+		return 0;
+
+	ret = likely(atomic_try_cmpxchg_acquire(&lock->val, &val, 1));
+	if (ret)
+		grab_held_lock_entry(lock);
+	return ret;
+}
+
 #ifdef CONFIG_QUEUED_SPINLOCKS
 #define raw_res_spin_lock_init(lock) ({ *(lock) = (rqspinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; })
 #else
 #define raw_res_spin_lock_init(lock) ({ *(lock) = (rqspinlock_t){0}; })
 #endif
 
+#define raw_res_spin_trylock(lock)              \
+	({                                      \
+		int __ret;                      \
+		preempt_disable();              \
+		__ret = res_spin_trylock(lock); \
+		if (!__ret)                     \
+			preempt_enable();       \
+		__ret;                          \
+	})
+
+#define raw_res_spin_trylock_irqsave(lock, flags)   \
+	({                                          \
+		int __ret;                          \
+		local_irq_save(flags);              \
+		__ret = raw_res_spin_trylock(lock); \
+		if (!__ret)                         \
+			local_irq_restore(flags);   \
+		__ret;                              \
+	})
+
 #define raw_res_spin_lock(lock)                    \
 	({                                         \
 		int __ret;                         \
#1 lore raw
Quoted 25 lines · Kumar Kartikeya Dwivedi

A trylock variant for rqspinlock was missing owing to lack of users in the tree thus far, add one now as it would be needed in subsequent patches. Mark as __must_check and __always_inline.

This essentially copies queued_spin_trylock, but doesn't depend on it as rqspinlock compiles down to a TAS when CONFIG_QUEUED_SPINLOCKS=n.

---
 include/asm-generic/rqspinlock.h | 45 ++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
include/asm-generic/rqspinlock.h +4 -0
index 6d4244d643df3..a7f4b7c0fb78a 100644
@@ -217,12 +217,57 @@ static __always_inline void res_spin_unlock(rqspinlock_t *lock)
        this_cpu_dec(rqspinlock_held_locks.cnt);
 }
 
+/**
+ * res_spin_trylock - try to acquire a queued spinlock
+ * @lock: Pointer to queued spinlock structure
+ *

include/asm-generic/rqspinlock.h static __always_inline void res_spin_unlock(rqspinlock_t *lock) · line 233

Quoted 10 lines
include/asm-generic/rqspinlock.h +10 -0
+ * Attempts to acquire the lock without blocking. This function should be used
+ * in contexts where blocking is not allowed (e.g., NMI handlers).
+ *
+ * Return:
+ * * 1    - Lock was acquired successfully.
+ * * 0    - Lock acquisition failed.
+ */
+static __must_check __always_inline int res_spin_trylock(rqspinlock_t *lock)
+{
+       int val = atomic_read(&lock->val);

This needs a comment why val = 0 like res_spin_lock() is doing is not enough here.

include/asm-generic/rqspinlock.h static __always_inline void res_spin_unlock(rqspinlock_t *lock) · line 241

Quoted 8 lines
include/asm-generic/rqspinlock.h +8 -0
+       int ret;
+
+       if (unlikely(val))
+               return 0;
+
+       ret = likely(atomic_try_cmpxchg_acquire(&lock->val, &val, 1));
+       if (ret)
+               grab_held_lock_entry(lock);

Same issue as with res_spin_lock()...

Overall it makes sense, but let's defer it for now, since without users somebody might send a patch to remove it as dead code.

pw-bot: cr