Skip to contentSkip to conversation

rqspinlock: Fix order in raw_res_spin_(un)lock_irq to allow schedule

PATCH Applied Gabriele Monaco 5 participants 5 messages Jun 10 – Jun 13

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rqspinlock: Fix order in raw_res_spin_(un)lock_irq to allow schedule

raw_res_spin_unlock_irqrestore() calls raw_res_spin_unlock() and then restores interrupts, this means preemption is enabled when interrupts are still disabled (as part of raw_res_spin_unlock()) so this cannot trigger an actual preemption.
This is inconsistent with other spinlock implementations (raw_spin_unlock_irqrestore() and bpf_res_spin_unlock_irqrestore() itself).

Adjust the macro to ensure interrupts are enabled before enabling preemption, allowing to schedule at that point. Make the same modification in the error path of raw_res_spin_lock_irqsave().

---
New submission of [1]

[1] - https://lore.kernel.org/lkml/[email protected]

---
 include/asm-generic/rqspinlock.h | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
include/asm-generic/rqspinlock.h +11 -3
index 151d267a49..4d46643f46 100644
@@ -243,12 +243,20 @@ static __always_inline void res_spin_unlock(rqspinlock_t *lock)
 	({                                        \
 		int __ret;                        \
 		local_irq_save(flags);            \
-		__ret = raw_res_spin_lock(lock);  \
-		if (__ret)                        \
+		preempt_disable();                \
+		__ret = res_spin_lock(lock);      \
+		if (__ret) {                      \
 			local_irq_restore(flags); \
+			preempt_enable();         \
+		}                                 \
 		__ret;                            \
 	})
 
-#define raw_res_spin_unlock_irqrestore(lock, flags) ({ raw_res_spin_unlock(lock); local_irq_restore(flags); })
+#define raw_res_spin_unlock_irqrestore(lock, flags) \
+	({                                          \
+		res_spin_unlock(lock);              \
+		local_irq_restore(flags);           \
+		preempt_enable();                   \
+	})
 
 #endif /* __ASM_GENERIC_RQSPINLOCK_H */

base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8

Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] kernel/locking/locktorture.c ignores the return value of raw_res_spin_lock and raw_res_spin_lock_irqsave, proceeding to modify shared state without holding the lock on failure. --

commit 92f88363aff605830b5600d6bc2a3ec351170c8e
Author: Gabriele Monaco <[email protected]>

rqspinlock: Fix order in raw_res_spin_(un)lock_irq to allow schedule

This commit fixes the preemption enabling order in the resilient spinlock unlock macros. It ensures interrupts are enabled before preemption to allow scheduling, maintaining consistency with other spinlock implementations.

Quoted 16 lines
include/asm-generic/rqspinlock.h +5 -2
index 151d267a496b3..4d46643f46cbc 100644
@@ -243,12 +243,20 @@ static __always_inline void res_spin_unlock(rqspinlock_t *lock)
 	({                                        \
 		int __ret;                        \
 		local_irq_save(flags);            \
-		__ret = raw_res_spin_lock(lock);  \
-		if (__ret)                        \
+		preempt_disable();                \
+		__ret = res_spin_lock(lock);      \
+		if (__ret) {                      \
 			local_irq_restore(flags); \
+			preempt_enable();         \
+		}                                 \

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

Quoted 10 lines
include/asm-generic/rqspinlock.h +6 -1
 		__ret;                            \
 	})
 
-#define raw_res_spin_unlock_irqrestore(lock, flags) ({ raw_res_spin_unlock(lock); local_irq_restore(flags); })
+#define raw_res_spin_unlock_irqrestore(lock, flags) \
+	({                                          \
+		res_spin_unlock(lock);              \
+		local_irq_restore(flags);           \
+		preempt_enable();                   \
+	})

[Severity: High]
This isn't a bug introduced by this patch, but while reviewing the resilient spinlock changes, I noticed a pre-existing issue in the locktorture module.

Does locktorture correctly handle the return values from the resilient spinlock API?

In kernel/locking/locktorture.c:torture_raw_res_spin_write_lock(), the return value is discarded:

    raw_res_spin_lock(&rqspinlock);
    return 0;

Similarly, in torture_raw_res_spin_write_lock_irq():

    raw_res_spin_lock_irqsave(&rqspinlock, flags);
    cxt.cur_ops->flags = flags;
    return 0;

Since raw_res_spin_lock and raw_res_spin_lock_irqsave can fail and return an error code like -EDEADLK or -ETIMEDOUT, ignoring the return value allows the caller to proceed unlocked into the critical section.

When the torture thread later calls raw_res_spin_unlock(), it will cause an unbalanced preempt_enable() because the failed lock acquisition path already restored preemption.

Furthermore, it forcibly clears the lock state, which could unlock a lock legitimately held by another thread, potentially causing lock state corruption and panics.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 65 lines · sashiko-bot

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] kernel/locking/locktorture.c ignores the return value of raw_res_spin_lock and raw_res_spin_lock_irqsave, proceeding to modify shared state without holding the lock on failure. --

commit 92f88363aff605830b5600d6bc2a3ec351170c8e
Author: Gabriele Monaco <[email protected]>

rqspinlock: Fix order in raw_res_spin_(un)lock_irq to allow schedule

This commit fixes the preemption enabling order in the resilient spinlock unlock macros. It ensures interrupts are enabled before preemption to allow scheduling, maintaining consistency with other spinlock implementations.

Quoted 16 lines
include/asm-generic/rqspinlock.h +5 -2
index 151d267a496b3..4d46643f46cbc 100644
@@ -243,12 +243,20 @@ static __always_inline void res_spin_unlock(rqspinlock_t *lock)
 	({                                        \
 		int __ret;                        \
 		local_irq_save(flags);            \
-		__ret = raw_res_spin_lock(lock);  \
-		if (__ret)                        \
+		preempt_disable();                \
+		__ret = res_spin_lock(lock);      \
+		if (__ret) {                      \
 			local_irq_restore(flags); \
+			preempt_enable();         \
+		}                                 \

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

Quoted 10 lines
include/asm-generic/rqspinlock.h +6 -1
 		__ret;                            \
 	})
 
-#define raw_res_spin_unlock_irqrestore(lock, flags) ({ raw_res_spin_unlock(lock); local_irq_restore(flags); })
+#define raw_res_spin_unlock_irqrestore(lock, flags) \
+	({                                          \
+		res_spin_unlock(lock);              \
+		local_irq_restore(flags);           \
+		preempt_enable();                   \
+	})

[Severity: High]
This isn't a bug introduced by this patch, but while reviewing the resilient spinlock changes, I noticed a pre-existing issue in the locktorture module.

Does locktorture correctly handle the return values from the resilient spinlock API?

In kernel/locking/locktorture.c:torture_raw_res_spin_write_lock(), the return value is discarded:

    raw_res_spin_lock(&rqspinlock);
    return 0;

Similarly, in torture_raw_res_spin_write_lock_irq():

    raw_res_spin_lock_irqsave(&rqspinlock, flags);
    cxt.cur_ops->flags = flags;
    return 0;

Since raw_res_spin_lock and raw_res_spin_lock_irqsave can fail and return an error code like -EDEADLK or -ETIMEDOUT, ignoring the return value allows the caller to proceed unlocked into the critical section.

Quoted 7 lines

When the torture thread later calls raw_res_spin_unlock(), it will cause an unbalanced preempt_enable() because the failed lock acquisition path already restored preemption.

Furthermore, it forcibly clears the lock state, which could unlock a lock legitimately held by another thread, potentially causing lock state corruption and panics.

In case of locktorture, error checking is skipped because it does not exercise deadlocks, it's only for locking scalability and correctness checks.

#3 lore raw
In reply to Gabriele Monaco
Related issue in kernel/locking/locktorture.c
Quoted 18 lines
include/asm-generic/rqspinlock.h +5 -2
index 151d267a496b..4d46643f46cb 100644
@@ -243,12 +243,20 @@ static __always_inline void res_spin_unlock(rqspinlock_t *lock)
 	({                                        \
 		int __ret;                        \
 		local_irq_save(flags);            \
-		__ret = raw_res_spin_lock(lock);  \
-		if (__ret)                        \
+		preempt_disable();                \
+		__ret = res_spin_lock(lock);      \
+		if (__ret) {                      \
 			local_irq_restore(flags); \
+			preempt_enable();         \
+		}                                 \
 		__ret;                            \
 	})

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

Quoted 10 lines
include/asm-generic/rqspinlock.h +6 -1
 
-#define raw_res_spin_unlock_irqrestore(lock, flags) ({ raw_res_spin_unlock(lock); local_irq_restore(flags); })
+#define raw_res_spin_unlock_irqrestore(lock, flags) \
+	({                                          \
+		res_spin_unlock(lock);              \
+		local_irq_restore(flags);           \
+		preempt_enable();                   \
+	})
 
 #endif /* __ASM_GENERIC_RQSPINLOCK_H */

Related issue in kernel/locking/locktorture.c

During the v2 review discussion, the Sashiko AI bot identified a pre-existing bug in the locktorture module where return values from raw_res_spin_lock() and raw_res_spin_lock_irqsave() are ignored in torture_raw_res_spin_write_lock():

    static int torture_raw_res_spin_write_lock(int tid __maybe_unused)
    {
        raw_res_spin_lock(&rqspinlock);
        return 0;
    }

Since these functions can fail with -EDEADLK or -ETIMEDOUT, ignoring the return value allows the caller to proceed into the critical section without holding the lock. When the torture thread later calls raw_res_spin_unlock(), it will cause an unbalanced preempt_enable() because the failed lock acquisition path already restored preemption.

Furthermore, forcibly clearing the lock state could unlock a lock legitimately held by another thread, potentially causing lock state corruption.

The same issue exists in torture_raw_res_spin_write_lock_irq() which also ignores the return value from raw_res_spin_lock_irqsave().

Should these torture functions check the return values and handle lock acquisition failures appropriately?

Reference: https://lore.kernel.org/all/[email protected]/

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27265934925

#4 lore raw
In reply to Gabriele Monaco
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Wed, 10 Jun 2026 11:04:29 +0200 you wrote:

raw_res_spin_unlock_irqrestore() calls raw_res_spin_unlock() and then restores interrupts, this means preemption is enabled when interrupts are still disabled (as part of raw_res_spin_unlock()) so this cannot trigger an actual preemption.
This is inconsistent with other spinlock implementations (raw_spin_unlock_irqrestore() and bpf_res_spin_unlock_irqrestore() itself).

[...]

Here is the summary with links:
- [bpf-next] rqspinlock: Fix order in raw_res_spin_(un)lock_irq to allow schedule

    https://git.kernel.org/bpf/bpf-next/c/b48bd16eb9fc

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html