Skip to contentSkip to conversation

riscv: signal: protect regs->status RMW from concurrent preemption

PATCH Superseded Guobin Zhang 3 participants 6 messages Aug 7 – Oct 20 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: signal: protect regs->status RMW from concurrent preemption

__fstate_clean() performs a non-atomic read-modify-write (RMW) on task_pt_regs(current)->status to clear the FS bits. This RMW is unprotected - it runs with preemption enabled and IRQs on during signal delivery (setup_rt_frame -> fstate_save) and sigreturn (restore_fp_state -> fstate_restore).

If a reschedule IPI or timer interrupt triggers preemption between the load and store of this RMW, __switch_to_vector() calls riscv_v_vstate_set_restore() which modifies the VS bits of the same regs->status. When the preempted task resumes, it stores back the stale value captured before preemption, overwriting the VS update and restoring VS=DIRTY while TIF_RISCV_V_DEFER_RESTORE remains set - a combination that should never occur and leads to vector state corruption.

The race window:

__fstate_clean (signal path) set_restore (schedule path)
----------------------------- -----------------------------
ld a0, regs->status // VS=DIRTY, FS=DIRTY
<- preempted (reschedule IPI) regs->status VS = INITIAL set TIF_RISCV_V_DEFER_RESTORE <- resumed
andi a0, ~FS
ori a0, FS_CLEAN // stale a0 still has VS=DIRTY
sd a0, regs->status // overwrites VS=INITIAL with VS=DIRTY
-> result: VS=DIRTY + DEFER -> ANOMALY

Fix by adding preempt_disable/enable around the RMW in __fstate_clean() and fstate_off(). riscv_v_vstate_set_restore() has the identical hazard: it is called from __restore_v_state() (sigreturn path) with preemption enabled, and it can race the same way against the scheduler's own call to riscv_v_vstate_set_restore() for the same task in __switch_to_vector(). Wrap that call site with preempt_disable/enable as well.

Add WARN_ON_ONCE(preemptible()) to riscv_v_vstate_restore() to catch any future unprotected callers at development time.

---
Race: regs->status is shared between the FPU (SR_FS) and vector (SR_VS) state machines. Three call sites do a non-atomic RMW on it while preemptible: __fstate_clean()/fstate_off() in switch_to.h, and riscv_v_vstate_set_restore() as called from signal.c's __restore_v_state() (sigreturn path).

Cause: if the task is preempted between the load and the store, the scheduler's own switch_to() writes the other half of the same register word when switching this task back in. The task then resumes and stores its stale value, clobbering that write.

Fix: wrap each RMW with preempt_disable()/preempt_enable(). This is sufficient because the conflicting writer only runs via an actual context switch of this task, which preempt_disable() prevents. local_irq_disable() is not needed: no IRQ handler touches regs->status, so masking IRQs would add latency without closing any extra race.

Reproduced on Spacemit K1 hardware; not reproducible under QEMU/TCG (see commit message for the full race-window trace).

---
 arch/riscv/include/asm/switch_to.h | 4 ++++
 arch/riscv/include/asm/vector.h    | 2 ++
 arch/riscv/kernel/signal.c         | 2 ++
 3 files changed, 8 insertions(+)
arch/riscv/include/asm/switch_to.h +4 -0
index 0e71eb82f920..fd0ceebd1c23 100644
@@ -21,13 +21,17 @@ extern void __fstate_restore(struct task_struct *restore_from);
 
 static inline void __fstate_clean(struct pt_regs *regs)
 {
+	preempt_disable();
 	regs->status = (regs->status & ~SR_FS) | SR_FS_CLEAN;
+	preempt_enable();
 }
 
 static inline void fstate_off(struct task_struct *task,
 			      struct pt_regs *regs)
 {
+	preempt_disable();
 	regs->status = (regs->status & ~SR_FS) | SR_FS_OFF;
+	preempt_enable();
 }
 
 static inline void fstate_save(struct task_struct *task,
arch/riscv/include/asm/vector.h +2 -0
index 00cb9c0982b1..da1559bcee36 100644
@@ -315,6 +315,8 @@ static inline void riscv_v_vstate_save(struct __riscv_v_ext_state *vstate,
 static inline void riscv_v_vstate_restore(struct __riscv_v_ext_state *vstate,
 					  struct pt_regs *regs)
 {
+	WARN_ON_ONCE(preemptible());
+
 	if (riscv_v_vstate_query(regs)) {
 		__riscv_v_vstate_restore(vstate, vstate->datap);
 		__riscv_v_vstate_clean(regs);
arch/riscv/kernel/signal.c +2 -0
index 59784dc117e4..6f6f3315bc09 100644
@@ -123,7 +123,9 @@ static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec)
 	 * to avoid getting the vstate incorrectly clobbered by the
 	 *  discarded vector state.
 	 */
+	preempt_disable();
 	riscv_v_vstate_set_restore(current, regs);
+	preempt_enable();
 
 	/* Copy everything of __sc_riscv_v_state except datap. */
 	err = __copy_from_user(&current->thread.vstate, &state->v_state,

---
base-commit: 075b74841bd0065a3bda3440873c747938e69b68
change-id: 20260807-vector_fpu_regs_status_rmw_fix-b51fa4a453d8

Best regards,

Signature · 1 line
Guobin Zhang <[email protected]>
#1 lore raw

Hi Guobin,

Quoted 60 lines · Guobin Zhang

__fstate_clean() performs a non-atomic read-modify-write (RMW) on task_pt_regs(current)->status to clear the FS bits. This RMW is unprotected - it runs with preemption enabled and IRQs on during signal delivery (setup_rt_frame -> fstate_save) and sigreturn (restore_fp_state -> fstate_restore).

If a reschedule IPI or timer interrupt triggers preemption between the load and store of this RMW, __switch_to_vector() calls riscv_v_vstate_set_restore() which modifies the VS bits of the same regs->status. When the preempted task resumes, it stores back the stale value captured before preemption, overwriting the VS update and restoring VS=DIRTY while TIF_RISCV_V_DEFER_RESTORE remains set - a combination that should never occur and leads to vector state corruption.

The race window:

__fstate_clean (signal path) set_restore (schedule path)
----------------------------- -----------------------------
ld a0, regs->status // VS=DIRTY, FS=DIRTY
<- preempted (reschedule IPI) regs->status VS = INITIAL set TIF_RISCV_V_DEFER_RESTORE <- resumed
andi a0, ~FS
ori a0, FS_CLEAN // stale a0 still has VS=DIRTY
sd a0, regs->status // overwrites VS=INITIAL with VS=DIRTY
-> result: VS=DIRTY + DEFER -> ANOMALY

Fix by adding preempt_disable/enable around the RMW in __fstate_clean() and fstate_off(). riscv_v_vstate_set_restore() has the identical hazard: it is called from __restore_v_state() (sigreturn path) with preemption enabled, and it can race the same way against the scheduler's own call to riscv_v_vstate_set_restore() for the same task in __switch_to_vector(). Wrap that call site with preempt_disable/enable as well.

Add WARN_ON_ONCE(preemptible()) to riscv_v_vstate_restore() to catch any future unprotected callers at development time.

---
Race: regs->status is shared between the FPU (SR_FS) and vector (SR_VS) state machines. Three call sites do a non-atomic RMW on it while preemptible: __fstate_clean()/fstate_off() in switch_to.h, and riscv_v_vstate_set_restore() as called from signal.c's __restore_v_state() (sigreturn path).

Cause: if the task is preempted between the load and the store, the scheduler's own switch_to() writes the other half of the same register word when switching this task back in. The task then resumes and stores its stale value, clobbering that write.

Fix: wrap each RMW with preempt_disable()/preempt_enable(). This is sufficient because the conflicting writer only runs via an actual context switch of this task, which preempt_disable() prevents. local_irq_disable() is not needed: no IRQ handler touches regs->status, so masking IRQs would add latency without closing any extra race.

Quoted 7 lines

Reproduced on Spacemit K1 hardware; not reproducible under QEMU/TCG (see commit message for the full race-window trace).

---
 arch/riscv/include/asm/switch_to.h | 4 ++++
 arch/riscv/include/asm/vector.h    | 2 ++
 arch/riscv/kernel/signal.c         | 2 ++
 3 files changed, 8 insertions(+)

Han Gao pointed me to this patch as a possible fix to the issue I encountered on the SpacemiT K1 [1]. So far, my first tests show that it either fixes the issue or at the bare minimum hides it.

As you also mention that you reproduced the issue on the Spacemit K1 hardware, can you please tell me if the bug appeared with similar symptoms?

Thanks
Aurelien

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

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                     http://aurel32.net
#2 lore raw
Quoted 3 lines · Aurelien Jarno

As you also mention that you reproduced the issue on the Spacemit K1 hardware, can you please tell me if the bug appeared with similar symptoms?

In my case, the issue manifested as a SIGILL coredump. The signal information was:

si_signo = 4 (SIGILL)
si_code = 1 (ILL_ILLOPC)

So the observed symptom was SIGILL/ILL_ILLOPC on SpacemiT K1.

Thanks,
Guobin

#3 lore raw
In reply to Aurelien Jarno

Hi Aurelien,

Quoted 71 lines · Aurelien Jarno

Hi Guobin,

Quoted 60 lines · Guobin Zhang

__fstate_clean() performs a non-atomic read-modify-write (RMW) on task_pt_regs(current)->status to clear the FS bits. This RMW is unprotected - it runs with preemption enabled and IRQs on during signal delivery (setup_rt_frame -> fstate_save) and sigreturn (restore_fp_state -> fstate_restore).

If a reschedule IPI or timer interrupt triggers preemption between the load and store of this RMW, __switch_to_vector() calls riscv_v_vstate_set_restore() which modifies the VS bits of the same regs->status. When the preempted task resumes, it stores back the stale value captured before preemption, overwriting the VS update and restoring VS=DIRTY while TIF_RISCV_V_DEFER_RESTORE remains set - a combination that should never occur and leads to vector state corruption.

The race window:

__fstate_clean (signal path) set_restore (schedule path)
----------------------------- -----------------------------
ld a0, regs->status // VS=DIRTY, FS=DIRTY
<- preempted (reschedule IPI) regs->status VS = INITIAL set TIF_RISCV_V_DEFER_RESTORE <- resumed
andi a0, ~FS
ori a0, FS_CLEAN // stale a0 still has VS=DIRTY
sd a0, regs->status // overwrites VS=INITIAL with VS=DIRTY
-> result: VS=DIRTY + DEFER -> ANOMALY

Fix by adding preempt_disable/enable around the RMW in __fstate_clean() and fstate_off(). riscv_v_vstate_set_restore() has the identical hazard: it is called from __restore_v_state() (sigreturn path) with preemption enabled, and it can race the same way against the scheduler's own call to riscv_v_vstate_set_restore() for the same task in __switch_to_vector(). Wrap that call site with preempt_disable/enable as well.

Add WARN_ON_ONCE(preemptible()) to riscv_v_vstate_restore() to catch any future unprotected callers at development time.

---
Race: regs->status is shared between the FPU (SR_FS) and vector (SR_VS) state machines. Three call sites do a non-atomic RMW on it while preemptible: __fstate_clean()/fstate_off() in switch_to.h, and riscv_v_vstate_set_restore() as called from signal.c's __restore_v_state() (sigreturn path).

Cause: if the task is preempted between the load and the store, the scheduler's own switch_to() writes the other half of the same register word when switching this task back in. The task then resumes and stores its stale value, clobbering that write.

Fix: wrap each RMW with preempt_disable()/preempt_enable(). This is sufficient because the conflicting writer only runs via an actual context switch of this task, which preempt_disable() prevents. local_irq_disable() is not needed: no IRQ handler touches regs->status, so masking IRQs would add latency without closing any extra race.

Quoted 7 lines

Reproduced on Spacemit K1 hardware; not reproducible under QEMU/TCG (see commit message for the full race-window trace).

---
 arch/riscv/include/asm/switch_to.h | 4 ++++
 arch/riscv/include/asm/vector.h    | 2 ++
 arch/riscv/kernel/signal.c         | 2 ++
 3 files changed, 8 insertions(+)
Quoted 8 lines

Han Gao pointed me to this patch as a possible fix to the issue I encountered on the SpacemiT K1 [1]. So far, my first tests show that it either fixes the issue or at the bare minimum hides it.

As you also mention that you reproduced the issue on the Spacemit K1 hardware, can you please tell me if the bug appeared with similar symptoms?

I have a reproducer on this bug and can reliablely catch it on K3. The code runs in a loop that does the following things:

1. dirtify FS by executing a fp instruction
2. configure vstate as e8 m1
3. load bytes into v5
4. trigger a signal handler by executing ebreak
5. vector save using the loaded v5

The signal handler verifies if it comes from the ebreak, otherwise it is the vstate corruption. Run multiple threads per core to increase the context switch rate.

This should hit the problem pretty soon, as corrupting lmul to m8 would cause the vector save from v5 trap immediately. With the fix the problem no longer occurs.

Cheers,
Andy

#4 lore raw
In reply to Guobin Zhang

Hi Guobin,

Quoted 29 lines · Guobin Zhang

__fstate_clean() performs a non-atomic read-modify-write (RMW) on task_pt_regs(current)->status to clear the FS bits. This RMW is unprotected - it runs with preemption enabled and IRQs on during signal delivery (setup_rt_frame -> fstate_save) and sigreturn (restore_fp_state -> fstate_restore).

If a reschedule IPI or timer interrupt triggers preemption between the load and store of this RMW, __switch_to_vector() calls riscv_v_vstate_set_restore() which modifies the VS bits of the same regs->status. When the preempted task resumes, it stores back the stale value captured before preemption, overwriting the VS update and restoring VS=DIRTY while TIF_RISCV_V_DEFER_RESTORE remains set - a combination that should never occur and leads to vector state corruption.

The race window:

__fstate_clean (signal path) set_restore (schedule path)
----------------------------- -----------------------------
ld a0, regs->status // VS=DIRTY, FS=DIRTY
<- preempted (reschedule IPI) regs->status VS = INITIAL set TIF_RISCV_V_DEFER_RESTORE <- resumed
andi a0, ~FS
ori a0, FS_CLEAN // stale a0 still has VS=DIRTY
sd a0, regs->status // overwrites VS=INITIAL with VS=DIRTY
-> result: VS=DIRTY + DEFER -> ANOMALY

Quoted 7 lines

Fix by adding preempt_disable/enable around the RMW in __fstate_clean() and fstate_off(). riscv_v_vstate_set_restore() has the identical hazard: it is called from __restore_v_state() (sigreturn path) with preemption enabled, and it can race the same way against the scheduler's own call to riscv_v_vstate_set_restore() for the same task in __switch_to_vector(). Wrap that call site with preempt_disable/enable as well.

Thanks for finding this. Indeed, pt_regs->status updates are not safe in preemptible context, since it is not atomic and can race with the nesting context. This can result in vtype corruption since marking the VS as DIRTY again will write the vtype used by the context saving code into process's vstate when setting up the signal frame.

Quoted 42 lines

Add WARN_ON_ONCE(preemptible()) to riscv_v_vstate_restore() to catch any future unprotected callers at development time.

---
Race: regs->status is shared between the FPU (SR_FS) and vector (SR_VS) state machines. Three call sites do a non-atomic RMW on it while preemptible: __fstate_clean()/fstate_off() in switch_to.h, and riscv_v_vstate_set_restore() as called from signal.c's __restore_v_state() (sigreturn path).

Cause: if the task is preempted between the load and the store, the scheduler's own switch_to() writes the other half of the same register word when switching this task back in. The task then resumes and stores its stale value, clobbering that write.

Fix: wrap each RMW with preempt_disable()/preempt_enable(). This is sufficient because the conflicting writer only runs via an actual context switch of this task, which preempt_disable() prevents. local_irq_disable() is not needed: no IRQ handler touches regs->status, so masking IRQs would add latency without closing any extra race.

Reproduced on Spacemit K1 hardware; not reproducible under QEMU/TCG (see commit message for the full race-window trace).

---
 arch/riscv/include/asm/switch_to.h | 4 ++++
 arch/riscv/include/asm/vector.h    | 2 ++
 arch/riscv/kernel/signal.c         | 2 ++
 3 files changed, 8 insertions(+)
arch/riscv/include/asm/switch_to.h +1 -0
index 0e71eb82f920..fd0ceebd1c23 100644
@@ -21,13 +21,17 @@ extern void __fstate_restore(struct task_struct *restore_from);
 
 static inline void __fstate_clean(struct pt_regs *regs)
 {
+	preempt_disable();
 	regs->status = (regs->status & ~SR_FS) | SR_FS_CLEAN;

arch/riscv/include/asm/switch_to.h extern void __fstate_restore(struct task_struct *restore_from); · line 35

Quoted 10 lines
arch/riscv/include/asm/switch_to.h +3 -0
+	preempt_enable();
 }
 
 static inline void fstate_off(struct task_struct *task,
 			      struct pt_regs *regs)
 {
+	preempt_disable();
 	regs->status = (regs->status & ~SR_FS) | SR_FS_OFF;
+	preempt_enable();
 }

Ideally, we should guard the preemption at fstate_save and fstate_restore level. This works because saving and restoring floating registers are reentrant safe (not for the vector as we will get the corrupted vtype).

Instead, I would prefer enclosing premption off region there, and have fstate_{save,restore}_preempt_off() for code that is clearly run with preemption off (the scheduler code and kernel_mode_fpu).

Something like:

+static inline void fstate_save_preempt_off(struct task_struct *task,
struct pt_regs *regs)

 {
        if ((regs->status & SR_FS) == SR_FS_DIRTY) {
@@ -44,7 +40,7 @@ static inline void fstate_save(struct task_struct *task,
        }

}

-static inline void fstate_restore(struct task_struct *task, +static inline void fstate_restore_preempt_off(struct task_struct *task,
struct pt_regs *regs)

 {
        if ((regs->status & SR_FS) != SR_FS_OFF) {
@@ -53,14 +49,30 @@ static inline void fstate_restore(struct task_struct *task,
        }

}

+static inline void fstate_save(struct task_struct *task,
+ struct pt_regs *regs)
+{

+       preempt_disable();
+       fstate_save_preempt_off(task, regs);
+       preempt_enable();

+}
+
+static inline void fstate_restore(struct task_struct *task,
+ struct pt_regs *regs)
+{

+       preempt_disable();
+       fstate_restore_preempt_off(task, regs);
+       preempt_enable();

+}
+
static inline void __switch_to_fpu(struct task_struct *prev,
struct task_struct *next)

 {
        struct pt_regs *regs;

        regs = task_pt_regs(prev);
-       fstate_save(prev, regs);
-       fstate_restore(next, task_pt_regs(next));
+       fstate_save_preempt_off(prev, regs);
+       fstate_restore_preempt_off(next, task_pt_regs(next));

}

static __always_inline bool has_fpu(void)

Thanks again for finding this and please let me know if you would like to send a revision for this.

Cheers,
Andy

#5 lore raw

Hi Andy,

Thanks for the review and the suggested approach. I'll send a v2 that disables preemption at the fstate_save()/fstate_restore() level and adds the fstate_{save,restore}_preempt_off() variants for the scheduler and kernel_mode_fpu.

Thanks,
Guobin