Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status
@ 2026-11-30 22:01 Andy Chiu
  2026-12-01 18:10 ` Frédéric Mahé
  2026-12-02  7:58 ` Charlie Jenkins
  0 siblings, 2 replies; 5+ messages in thread
From: Andy Chiu @ 2026-11-30 22:01 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
	Oleg Nesterov, Andy Chiu, Eric Biggers, linux-riscv
  Cc: Andy Chiu, Guobin Zhang, dfustini, greentime.hu, Anup Patel,
	Charlie Jenkins, Kaiwen Xue, Deepak Gupta, Sergey Matyukevich,
	Drew Fustini, Yicong Yang, Ivy Lopez, Aurelien Jarno,
	Troy Mitchell, Jisheng Zhang, Conor Dooley, Zong Li, Nam Cao,
	Samuel Holland, Vivian Wang, Yunhui Cui, Yong-Xuan Wang,
	Andrew Jones, Zishun Yi

Before this patch, preempt_disable is not enough to protect the SR_FS
update in signal handling. As kernel-mode vector, which also update
regs->status, is allowed in softirq context and it can come right after
serving an interrupt.

Move SR_FS and SR_VS into its own separate field in pt_regs so that
RMW of SR_FS does not need to carry the same synchronization primitive
as non-atomic update of SR_VS. Also, export vendor's alternative macros
so that they can be reused in assembly code.

CC: Guobin Zhang <guobin.zhang@intel.com>
Fixes: 956895b9d8f7 ("riscv: vector: make Vector always available for softirq context")
Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
---
 arch/riscv/include/asm/csr.h                  |  9 +++
 arch/riscv/include/asm/ptrace.h               |  2 +
 arch/riscv/include/asm/switch_to.h            | 10 ++--
 arch/riscv/include/asm/vector.h               | 30 ++--------
 arch/riscv/include/asm/vendor_extensions.h    | 18 +++---
 .../include/asm/vendor_extensions/thead.h     | 10 +++-
 arch/riscv/kernel/asm-offsets.c               |  2 +
 arch/riscv/kernel/entry.S                     | 57 ++++++++++++++++++-
 arch/riscv/kernel/kernel_mode_vector.c        |  2 +-
 arch/riscv/kernel/process.c                   |  1 +
 10 files changed, 99 insertions(+), 42 deletions(-)

diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h
index 621f84d1898f..71c7fd9d8f26 100644
--- a/arch/riscv/include/asm/csr.h
+++ b/arch/riscv/include/asm/csr.h
@@ -27,6 +27,14 @@
 #define SR_ELP		SR_SPELP
 #endif
 
+#define SR_OFF		_AC(0x00000000, UL)
+#define SR_INITIAL	_AC(0x00000001, UL)
+#define SR_CLEAN	_AC(0x00000002, UL)
+#define SR_DIRTY	_AC(0x00000003, UL)
+#define SR_VS_THEAD_SHIFT		23
+#define SR_VS_SHIFT			9
+#define SR_FS_SHIFT			13
+
 #define SR_FS		_AC(0x00006000, UL) /* Floating-point Status */
 #define SR_FS_OFF	_AC(0x00000000, UL)
 #define SR_FS_INITIAL	_AC(0x00002000, UL)
@@ -52,6 +60,7 @@
 #define SR_XS_DIRTY	_AC(0x00018000, UL)
 
 #define SR_FS_VS	(SR_FS | SR_VS) /* Vector and Floating-Point Unit */
+#define SR_FS_VS_THEAD	(SR_FS | SR_VS_THEAD)
 
 #ifndef CONFIG_64BIT
 #define SR_SD		_AC(0x80000000, UL) /* FS/VS/XS dirty */
diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
index addc8188152f..a56498055218 100644
--- a/arch/riscv/include/asm/ptrace.h
+++ b/arch/riscv/include/asm/ptrace.h
@@ -49,6 +49,8 @@ struct pt_regs {
 	unsigned long t6;
 	/* Supervisor/Machine CSRs */
 	unsigned long status;
+	u8 sr_vs;
+	u8 sr_fs;
 	unsigned long badaddr;
 	unsigned long cause;
 	/* a0 value before the syscall */
diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
index 970f89935f0e..61588f7b03cf 100644
--- a/arch/riscv/include/asm/switch_to.h
+++ b/arch/riscv/include/asm/switch_to.h
@@ -22,21 +22,19 @@ extern void __fstate_restore(struct task_struct *restore_from);
 
 static inline void __fstate_clean(struct pt_regs *regs)
 {
-	regs->status = (regs->status & ~SR_FS) | SR_FS_CLEAN;
+	regs->sr_fs = SR_CLEAN;
 }
 
 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();
+	regs->sr_fs = SR_FS_OFF;
 }
 
 static inline void fstate_save_preempt_off(struct task_struct *task,
 					   struct pt_regs *regs)
 {
-	if ((regs->status & SR_FS) == SR_FS_DIRTY) {
+	if (regs->sr_fs == SR_DIRTY) {
 		__fstate_save(task);
 		__fstate_clean(regs);
 	}
@@ -45,7 +43,7 @@ static inline void fstate_save_preempt_off(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) {
+	if (regs->sr_fs != SR_OFF) {
 		__fstate_restore(task);
 		__fstate_clean(regs);
 	}
diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h
index 3381a535abb4..5ac50d138da2 100644
--- a/arch/riscv/include/asm/vector.h
+++ b/arch/riscv/include/asm/vector.h
@@ -22,24 +22,6 @@
 #include <asm/vendor_extensions.h>
 #include <asm/vendor_extensions/thead.h>
 
-#define __riscv_v_vstate_or(_val, TYPE) ({				\
-	typeof(_val) _res = _val;					\
-	if (has_xtheadvector()) \
-		_res = (_res & ~SR_VS_THEAD) | SR_VS_##TYPE##_THEAD;	\
-	else								\
-		_res = (_res & ~SR_VS) | SR_VS_##TYPE;			\
-	_res;								\
-})
-
-#define __riscv_v_vstate_check(_val, TYPE) ({				\
-	bool _res;							\
-	if (has_xtheadvector()) \
-		_res = ((_val) & SR_VS_THEAD) == SR_VS_##TYPE##_THEAD;	\
-	else								\
-		_res = ((_val) & SR_VS) == SR_VS_##TYPE;		\
-	_res;								\
-})
-
 extern unsigned long riscv_v_vsize;
 int riscv_v_setup_vsize(void);
 bool insn_is_vector(u32 insn_buf);
@@ -87,27 +69,27 @@ static __always_inline bool has_xtheadvector(void)
 
 static inline void __riscv_v_vstate_clean(struct pt_regs *regs)
 {
-	regs->status = __riscv_v_vstate_or(regs->status, CLEAN);
+	regs->sr_vs = SR_CLEAN;
 }
 
 static inline void __riscv_v_vstate_dirty(struct pt_regs *regs)
 {
-	regs->status = __riscv_v_vstate_or(regs->status, DIRTY);
+	regs->sr_vs = SR_DIRTY;
 }
 
 static inline void riscv_v_vstate_off(struct pt_regs *regs)
 {
-	regs->status = __riscv_v_vstate_or(regs->status, OFF);
+	regs->sr_vs = SR_OFF;
 }
 
 static inline void riscv_v_vstate_on(struct pt_regs *regs)
 {
-	regs->status = __riscv_v_vstate_or(regs->status, INITIAL);
+	regs->sr_vs = SR_INITIAL;
 }
 
 static inline bool riscv_v_vstate_query(struct pt_regs *regs)
 {
-	return !__riscv_v_vstate_check(regs->status, OFF);
+	return !!(regs->sr_vs);
 }
 
 static __always_inline void riscv_v_enable(void)
@@ -311,7 +293,7 @@ static inline void riscv_v_vstate_discard(struct pt_regs *regs)
 static inline void riscv_v_vstate_save(struct __riscv_v_ext_state *vstate,
 				       struct pt_regs *regs)
 {
-	if (__riscv_v_vstate_check(regs->status, DIRTY)) {
+	if (regs->sr_vs == SR_DIRTY) {
 		__riscv_v_vstate_save(vstate, vstate->datap);
 		__riscv_v_vstate_clean(regs);
 	}
diff --git a/arch/riscv/include/asm/vendor_extensions.h b/arch/riscv/include/asm/vendor_extensions.h
index b00149be2627..ff8815e87013 100644
--- a/arch/riscv/include/asm/vendor_extensions.h
+++ b/arch/riscv/include/asm/vendor_extensions.h
@@ -6,6 +6,15 @@
 #ifndef _ASM_VENDOR_EXTENSIONS_H
 #define _ASM_VENDOR_EXTENSIONS_H
 
+/*
+ * The alternatives need some way of distinguishing between vendor extensions
+ * and errata. Incrementing all of the vendor extension keys so they are at
+ * least 0x8000 accomplishes that.
+ */
+#define RISCV_VENDOR_EXT_ALTERNATIVES_BASE	0x8000
+
+#ifndef __ASSEMBLER__
+
 #include <asm/cpufeature.h>
 
 #include <linux/array_size.h>
@@ -32,13 +41,6 @@ extern struct riscv_isa_vendor_ext_data_list *riscv_isa_vendor_ext_list[];
 
 extern const size_t riscv_isa_vendor_ext_list_size;
 
-/*
- * The alternatives need some way of distinguishing between vendor extensions
- * and errata. Incrementing all of the vendor extension keys so they are at
- * least 0x8000 accomplishes that.
- */
-#define RISCV_VENDOR_EXT_ALTERNATIVES_BASE	0x8000
-
 #define VENDOR_EXT_ALL_CPUS			-1
 
 bool __riscv_isa_vendor_extension_available(int cpu, unsigned long vendor, unsigned int bit);
@@ -95,4 +97,6 @@ static __always_inline bool riscv_cpu_has_vendor_extension_unlikely(const unsign
 	return __riscv_isa_vendor_extension_available(cpu, vendor, ext);
 }
 
+#endif /* !__ASSEMBLER__ */
+
 #endif /* _ASM_VENDOR_EXTENSIONS_H */
diff --git a/arch/riscv/include/asm/vendor_extensions/thead.h b/arch/riscv/include/asm/vendor_extensions/thead.h
index e85c75b3b340..bab7f952f651 100644
--- a/arch/riscv/include/asm/vendor_extensions/thead.h
+++ b/arch/riscv/include/asm/vendor_extensions/thead.h
@@ -4,13 +4,15 @@
 
 #include <asm/vendor_extensions.h>
 
-#include <linux/types.h>
-
 /*
  * Extension keys must be strictly less than RISCV_ISA_VENDOR_EXT_MAX.
  */
 #define RISCV_ISA_VENDOR_EXT_XTHEADVECTOR		0
 
+#ifndef __ASSEMBLER__
+
+#include <linux/types.h>
+
 extern struct riscv_isa_vendor_ext_data_list riscv_isa_vendor_ext_list_thead;
 
 #ifdef CONFIG_RISCV_ISA_VENDOR_EXT_THEAD
@@ -44,4 +46,6 @@ static inline void disable_xtheadvector(void) { }
 #define THEAD_VLB_V_V16T0		".long	0x012028807\n\t"
 #define THEAD_VLB_V_V24T0		".long	0x012028c07\n\t"
 
-#endif
+#endif /* !__ASSEMBLER__ */
+
+#endif /* _ASM_RISCV_VENDOR_EXTENSIONS_THEAD_H */
diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c
index a75f0cfea1e9..0cb413569538 100644
--- a/arch/riscv/kernel/asm-offsets.c
+++ b/arch/riscv/kernel/asm-offsets.c
@@ -128,6 +128,8 @@ void asm_offsets(void)
 	OFFSET(PT_GP, pt_regs, gp);
 	OFFSET(PT_ORIG_A0, pt_regs, orig_a0);
 	OFFSET(PT_STATUS, pt_regs, status);
+	OFFSET(PT_SR_VS, pt_regs, sr_vs);
+	OFFSET(PT_SR_FS, pt_regs, sr_fs);
 	OFFSET(PT_BADADDR, pt_regs, badaddr);
 	OFFSET(PT_CAUSE, pt_regs, cause);
 
diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index 628b85e3915a..8da33dc16278 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -16,6 +16,9 @@
 #include <asm/thread_info.h>
 #include <asm/asm-offsets.h>
 #include <asm/errata_list.h>
+#include <asm/vendorid_list.h>
+#include <asm/vendor_extensions.h>
+#include <asm/vendor_extensions/thead.h>
 #include <linux/sizes.h>
 
 	.section .irqentry.text, "ax"
@@ -125,6 +128,42 @@
 		CONFIG_RISCV_USER_CFI)
 .endm
 
+/*
+ * xtheadvector keeps its VS field at a different position in sstatus than
+ * standard RVV. The helpers below pick the right encoding at boot.
+ */
+.macro li_sr_sum_fs_vs reg
+	ALTERNATIVE(__stringify(li \reg, SR_SUM | SR_FS_VS),
+		__stringify(li \reg, SR_SUM | SR_FS_VS_THEAD; nop),
+		THEAD_VENDOR_ID,
+		RISCV_VENDOR_EXT_ALTERNATIVES_BASE + RISCV_ISA_VENDOR_EXT_XTHEADVECTOR,
+		CONFIG_RISCV_ISA_V)
+.endm
+
+.macro li_sr_fs_vs reg
+	ALTERNATIVE(__stringify(li \reg, SR_FS_VS),
+		__stringify(li \reg, SR_FS_VS_THEAD; nop),
+		THEAD_VENDOR_ID,
+		RISCV_VENDOR_EXT_ALTERNATIVES_BASE + RISCV_ISA_VENDOR_EXT_XTHEADVECTOR,
+		CONFIG_RISCV_ISA_V)
+.endm
+
+.macro srli_sr_vs rd, rs
+	ALTERNATIVE(__stringify(srli \rd, \rs, SR_VS_SHIFT),
+		__stringify(srli \rd, \rs, SR_VS_THEAD_SHIFT),
+		THEAD_VENDOR_ID,
+		RISCV_VENDOR_EXT_ALTERNATIVES_BASE + RISCV_ISA_VENDOR_EXT_XTHEADVECTOR,
+		CONFIG_RISCV_ISA_V)
+.endm
+
+.macro slli_sr_vs rd, rs
+	ALTERNATIVE(__stringify(slli \rd, \rs, SR_VS_SHIFT),
+		__stringify(slli \rd, \rs, SR_VS_THEAD_SHIFT),
+		THEAD_VENDOR_ID,
+		RISCV_VENDOR_EXT_ALTERNATIVES_BASE + RISCV_ISA_VENDOR_EXT_XTHEADVECTOR,
+		CONFIG_RISCV_ISA_V)
+.endm
+
 SYM_CODE_START(handle_exception)
 	/*
 	 * If coming from userspace, preserve the user thread pointer and load
@@ -178,7 +217,7 @@ SYM_CODE_START(handle_exception)
 	 * Disable the FPU/Vector to detect illegal usage of floating point
 	 * or vector in kernel space.
 	 */
-	li t0, SR_SUM | SR_FS_VS
+	li_sr_sum_fs_vs t0
 #ifdef CONFIG_64BIT
 	li t1, SR_ELP
 	or t0, t0, t1
@@ -187,6 +226,9 @@ SYM_CODE_START(handle_exception)
 	REG_L s0, TASK_TI_USER_SP(tp)
 	csrrc s1, CSR_STATUS, t0
 	save_userssp s2, s1
+	li_sr_fs_vs t0
+	and t2, t0, s1
+	xor s1, t2, s1
 	csrr s2, CSR_EPC
 	csrr s3, CSR_TVAL
 	csrr s4, CSR_CAUSE
@@ -197,6 +239,12 @@ SYM_CODE_START(handle_exception)
 	REG_S s3, PT_BADADDR(sp)
 	REG_S s4, PT_CAUSE(sp)
 	REG_S s5, PT_TP(sp)
+	srli_sr_vs t3, t2
+	srli t2, t2, SR_FS_SHIFT
+	andi  t2, t2, SR_DIRTY
+	andi  t3, t3, SR_DIRTY
+	sb t3, PT_SR_VS(sp)
+	sb t2, PT_SR_FS(sp)
 
 	/*
 	 * Set the scratch register to 0, so that if a recursive exception
@@ -303,6 +351,13 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
 	REG_L  a2, PT_EPC(sp)
 	REG_SC x0, a2, PT_EPC(sp)
 
+	lb t0, PT_SR_VS(sp)
+	lb t1, PT_SR_FS(sp)
+	slli_sr_vs t0, t0
+	slli t1, t1, SR_FS_SHIFT
+	or a0, a0, t0
+	or a0, a0, t1
+
 	csrw CSR_STATUS, a0
 	csrw CSR_EPC, a2
 
diff --git a/arch/riscv/kernel/kernel_mode_vector.c b/arch/riscv/kernel/kernel_mode_vector.c
index b9481a3e40e3..506384ff1de5 100644
--- a/arch/riscv/kernel/kernel_mode_vector.c
+++ b/arch/riscv/kernel/kernel_mode_vector.c
@@ -195,7 +195,7 @@ asmlinkage void riscv_v_context_nesting_start(struct pt_regs *regs)
 		return;
 
 	depth = riscv_v_ctx_get_depth();
-	if (depth == 0 && __riscv_v_vstate_check(regs->status, DIRTY))
+	if (depth == 0 && regs->sr_vs == SR_DIRTY)
 		riscv_preempt_v_set_dirty();
 
 	riscv_v_ctx_depth_inc();
diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c
index 34c1d498d1ca..f7297b9ffd9e 100644
--- a/arch/riscv/kernel/process.c
+++ b/arch/riscv/kernel/process.c
@@ -98,6 +98,7 @@ void __show_regs(struct pt_regs *regs)
 
 	pr_cont("status: " REG_FMT " badaddr: " REG_FMT " cause: " REG_FMT "\n",
 		regs->status, regs->badaddr, regs->cause);
+	pr_cont("status.vs=%02x\tstatus.fs=%02x\n", regs->sr_vs, regs->sr_fs);
 }
 void show_regs(struct pt_regs *regs)
 {

base-commit: d1a8d411533d6be16ac79796c02a9bfc7d7958f1
-- 
2.43.0


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status
  2026-11-30 22:01 [PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status Andy Chiu
@ 2026-12-01 18:10 ` Frédéric Mahé
  2026-12-02  7:58 ` Charlie Jenkins
  1 sibling, 0 replies; 5+ messages in thread
From: Frédéric Mahé @ 2026-12-01 18:10 UTC (permalink / raw)
  To: Andy Chiu
  Cc: Frédéric Mahé, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Alexandre Ghiti, Oleg Nesterov, Andy Chiu,
	Eric Biggers, Aurelien Jarno, linux-riscv

Hi Andy,

Tested on a Banana Pi BPI-F3 (SpacemiT K1, VLEN 256), v7.4-rc5. The
riscv kselftests pass (except pointer_masking: no Ssnpm on K1). A stress
test with 8 threads holding FP and vector values across syscalls while
a 200 us SIGALRM handler clobbers f/v registers, under disk load, ran
600 s (2.1M signals) with no corruption. The new status.vs/fs line in
show_regs looks fine.
Testing done with the help of Claude (AI assistant).

Tested-by: Frédéric Mahé <frederic.mahe@cirad.fr> # BPI-F3

Frédéric

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status
  2026-11-30 22:01 [PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status Andy Chiu
  2026-12-01 18:10 ` Frédéric Mahé
@ 2026-12-02  7:58 ` Charlie Jenkins
  2026-12-02 23:33   ` [EXT] " Andy Chiu
  1 sibling, 1 reply; 5+ messages in thread
From: Charlie Jenkins @ 2026-12-02  7:58 UTC (permalink / raw)
  To: Andy Chiu
  Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
	Oleg Nesterov, Andy Chiu, Eric Biggers, linux-riscv, Guobin Zhang,
	dfustini, greentime.hu, Anup Patel, Charlie Jenkins, Kaiwen Xue,
	Deepak Gupta, Sergey Matyukevich, Drew Fustini, Yicong Yang,
	Ivy Lopez, Aurelien Jarno, Troy Mitchell, Jisheng Zhang,
	Conor Dooley, Zong Li, Nam Cao, Samuel Holland, Vivian Wang,
	Yunhui Cui, Yong-Xuan Wang, Andrew Jones, Zishun Yi

> Before this patch, preempt_disable is not enough to protect the SR_FS
> update in signal handling. As kernel-mode vector, which also update
> regs->status, is allowed in softirq context and it can come right after
> serving an interrupt.
> 
> Move SR_FS and SR_VS into its own separate field in pt_regs so that
> RMW of SR_FS does not need to carry the same synchronization primitive
> as non-atomic update of SR_VS. Also, export vendor's alternative macros
> so that they can be reused in assembly code.
> 
> CC: Guobin Zhang <guobin.zhang@intel.com>
> Fixes: 956895b9d8f7 ("riscv: vector: make Vector always available for softirq context")
> Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
>
> diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h
> index 621f84d1898f..71c7fd9d8f26 100644
> --- a/arch/riscv/include/asm/csr.h
> +++ b/arch/riscv/include/asm/csr.h
> @@ -27,6 +27,14 @@
>  #define SR_ELP		SR_SPELP
>  #endif
>  
> +#define SR_OFF		_AC(0x00000000, UL)
> +#define SR_INITIAL	_AC(0x00000001, UL)
> +#define SR_CLEAN	_AC(0x00000002, UL)
> +#define SR_DIRTY	_AC(0x00000003, UL)
> +#define SR_VS_THEAD_SHIFT		23
> +#define SR_VS_SHIFT			9
> +#define SR_FS_SHIFT			13
> +
>  #define SR_FS		_AC(0x00006000, UL) /* Floating-point Status */
>  #define SR_FS_OFF	_AC(0x00000000, UL)
>  #define SR_FS_INITIAL	_AC(0x00002000, UL)
> @@ -52,6 +60,7 @@
>  #define SR_XS_DIRTY	_AC(0x00018000, UL)
>  
>  #define SR_FS_VS	(SR_FS | SR_VS) /* Vector and Floating-Point Unit */
> +#define SR_FS_VS_THEAD	(SR_FS | SR_VS_THEAD)
>  
>  #ifndef CONFIG_64BIT
>  #define SR_SD		_AC(0x80000000, UL) /* FS/VS/XS dirty */
> diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
> index addc8188152f..a56498055218 100644
> --- a/arch/riscv/include/asm/ptrace.h
> +++ b/arch/riscv/include/asm/ptrace.h
> @@ -49,6 +49,8 @@ struct pt_regs {
>  	unsigned long t6;
>  	/* Supervisor/Machine CSRs */
>  	unsigned long status;
> +	u8 sr_vs;
> +	u8 sr_fs;
>  	unsigned long badaddr;
>  	unsigned long cause;
>  	/* a0 value before the syscall */
> diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
> index 970f89935f0e..61588f7b03cf 100644
> --- a/arch/riscv/include/asm/switch_to.h
> +++ b/arch/riscv/include/asm/switch_to.h
> @@ -22,21 +22,19 @@ extern void __fstate_restore(struct task_struct *restore_from);
>  
>  static inline void __fstate_clean(struct pt_regs *regs)
>  {
> -	regs->status = (regs->status & ~SR_FS) | SR_FS_CLEAN;
> +	regs->sr_fs = SR_CLEAN;
>  }
>  
>  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();
> +	regs->sr_fs = SR_FS_OFF;
I think these operations might be able to be done atomically and then we
don't need to disable preemption or split these away from sstatus for
clean/off.

__fstate_clean is only ever entered when initial, clean, or dirty.

There is a single entry point when regs->status=initial and that is
start_thread(), which immediately goes on to call fstate_restore() which
sets regs->status to clean. So as it stands, I don't think there is any
functional difference between initial and clean. We could just have all
threads start at clean and get rid of setting it to initial in the first
place. The semantics of having a thread start off as "initial" only to
immediately have registers restored seems off to me anyway, I would
expect "initial" to be some default fp values instead of parent fp
values as it is being used here.

When __fstate_clean is entered with regs->status=clean/dirty a simple
atomic and of ~0x2000 can be performed to arrive at clean again.

__fstate_off previously has the somewhat silly operation of
"(regs->status & ~SR_FS) | SR_FS_OFF" which is the same as "regs->status
& ~SR_FS" since SR_FS_OFF is 0. An atomic and can represent this as
well.

The same idea should be true for vector.

Let me know if I am missing some condition here :)

-- 
- Charlie


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [EXT] Re: [PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status
  2026-12-02  7:58 ` Charlie Jenkins
@ 2026-12-02 23:33   ` Andy Chiu
  2026-12-03  9:08     ` Charlie Jenkins
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Chiu @ 2026-12-02 23:33 UTC (permalink / raw)
  To: Charlie Jenkins
  Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
	Oleg Nesterov, Andy Chiu, Eric Biggers, linux-riscv, Guobin Zhang,
	dfustini, greentime.hu, Anup Patel, Kaiwen Xue, Deepak Gupta,
	Sergey Matyukevich, Drew Fustini, Yicong Yang, Ivy Lopez,
	Aurelien Jarno, Troy Mitchell, Jisheng Zhang, Conor Dooley,
	Zong Li, Nam Cao, Samuel Holland, Vivian Wang, Yunhui Cui,
	Yong-Xuan Wang, Andrew Jones, Zishun Yi

Hi Charlie,

On Wed, Dec 2, 2026 at 1:58 AM Charlie Jenkins
<thecharlesjenkins@gmail.com> wrote:
>
> > Before this patch, preempt_disable is not enough to protect the SR_FS
> > update in signal handling. As kernel-mode vector, which also update
> > regs->status, is allowed in softirq context and it can come right after
> > serving an interrupt.
> >
> > Move SR_FS and SR_VS into its own separate field in pt_regs so that
> > RMW of SR_FS does not need to carry the same synchronization primitive
> > as non-atomic update of SR_VS. Also, export vendor's alternative macros
> > so that they can be reused in assembly code.
> >
> > CC: Guobin Zhang <guobin.zhang@intel.com>
> > Fixes: 956895b9d8f7 ("riscv: vector: make Vector always available for softirq context")
> > Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
> >
> > diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h
> > index 621f84d1898f..71c7fd9d8f26 100644
> > --- a/arch/riscv/include/asm/csr.h
> > +++ b/arch/riscv/include/asm/csr.h
> > @@ -27,6 +27,14 @@
> >  #define SR_ELP               SR_SPELP
> >  #endif
> >
> > +#define SR_OFF               _AC(0x00000000, UL)
> > +#define SR_INITIAL   _AC(0x00000001, UL)
> > +#define SR_CLEAN     _AC(0x00000002, UL)
> > +#define SR_DIRTY     _AC(0x00000003, UL)
> > +#define SR_VS_THEAD_SHIFT            23
> > +#define SR_VS_SHIFT                  9
> > +#define SR_FS_SHIFT                  13
> > +
> >  #define SR_FS                _AC(0x00006000, UL) /* Floating-point Status */
> >  #define SR_FS_OFF    _AC(0x00000000, UL)
> >  #define SR_FS_INITIAL        _AC(0x00002000, UL)
> > @@ -52,6 +60,7 @@
> >  #define SR_XS_DIRTY  _AC(0x00018000, UL)
> >
> >  #define SR_FS_VS     (SR_FS | SR_VS) /* Vector and Floating-Point Unit */
> > +#define SR_FS_VS_THEAD       (SR_FS | SR_VS_THEAD)
> >
> >  #ifndef CONFIG_64BIT
> >  #define SR_SD                _AC(0x80000000, UL) /* FS/VS/XS dirty */
> > diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
> > index addc8188152f..a56498055218 100644
> > --- a/arch/riscv/include/asm/ptrace.h
> > +++ b/arch/riscv/include/asm/ptrace.h
> > @@ -49,6 +49,8 @@ struct pt_regs {
> >       unsigned long t6;
> >       /* Supervisor/Machine CSRs */
> >       unsigned long status;
> > +     u8 sr_vs;
> > +     u8 sr_fs;
> >       unsigned long badaddr;
> >       unsigned long cause;
> >       /* a0 value before the syscall */
> > diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
> > index 970f89935f0e..61588f7b03cf 100644
> > --- a/arch/riscv/include/asm/switch_to.h
> > +++ b/arch/riscv/include/asm/switch_to.h
> > @@ -22,21 +22,19 @@ extern void __fstate_restore(struct task_struct *restore_from);
> >
> >  static inline void __fstate_clean(struct pt_regs *regs)
> >  {
> > -     regs->status = (regs->status & ~SR_FS) | SR_FS_CLEAN;
> > +     regs->sr_fs = SR_CLEAN;
> >  }
> >
> >  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();
> > +     regs->sr_fs = SR_FS_OFF;
> I think these operations might be able to be done atomically and then we
> don't need to disable preemption or split these away from sstatus for
> clean/off.
>
> __fstate_clean is only ever entered when initial, clean, or dirty.
>
> There is a single entry point when regs->status=initial and that is
> start_thread(), which immediately goes on to call fstate_restore() which
> sets regs->status to clean. So as it stands, I don't think there is any
> functional difference between initial and clean. We could just have all
> threads start at clean and get rid of setting it to initial in the first
> place. The semantics of having a thread start off as "initial" only to
> immediately have registers restored seems off to me anyway, I would
> expect "initial" to be some default fp values instead of parent fp
> values as it is being used here.
>
> When __fstate_clean is entered with regs->status=clean/dirty a simple
> atomic and of ~0x2000 can be performed to arrive at clean again.
>

Thanks for the feedback! The idea works for SR_FS as it should really
be swinging between "dirty" and "clean". Using amo operations on SR_VS
does not help as an onging series is making "initial" as a valid state
[1], e.g. to optimize context tracking on the syscall path. Most SR_VS
bit ops are properly guarded, one thing that is missing on the current
code is at __restore_v_state() in sigreturn. Therefore, to fully adopt
this approach, we can do something like:
- Use AMO for SR_FS cleaning
- Use the legacy way to operate on SR_VS
- Guard the vstate clearing op at sigreturn with
get/put_cpu_vector_context(), and find any leftover code that does not
properly guard it

AMO store on some systems may not be as fast as a regular store, that
could be something worth benchmarking

[1]: https://lore.kernel.org/all/20261111184359.1239821-1-tchiu@tenstorrent.com/

Please let me know if you have any preference, thanks!

Cheers,
Andy

> __fstate_off previously has the somewhat silly operation of
> "(regs->status & ~SR_FS) | SR_FS_OFF" which is the same as "regs->status
> & ~SR_FS" since SR_FS_OFF is 0. An atomic and can represent this as
> well.
>
> The same idea should be true for vector.
>
> Let me know if I am missing some condition here :)
>
> --
> - Charlie
>

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [EXT] Re: [PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status
  2026-12-02 23:33   ` [EXT] " Andy Chiu
@ 2026-12-03  9:08     ` Charlie Jenkins
  0 siblings, 0 replies; 5+ messages in thread
From: Charlie Jenkins @ 2026-12-03  9:08 UTC (permalink / raw)
  To: Andy Chiu
  Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
	Oleg Nesterov, Andy Chiu, Eric Biggers, linux-riscv, Guobin Zhang,
	dfustini, greentime.hu, Anup Patel, Kaiwen Xue, Deepak Gupta,
	Sergey Matyukevich, Drew Fustini, Yicong Yang, Ivy Lopez,
	Aurelien Jarno, Troy Mitchell, Jisheng Zhang, Conor Dooley,
	Zong Li, Nam Cao, Samuel Holland, Vivian Wang, Yunhui Cui,
	Yong-Xuan Wang, Andrew Jones, Zishun Yi

On Wed, Dec 02, 2026 at 05:33:23PM -0600, Andy Chiu wrote:
> Hi Charlie,
> 
> On Wed, Dec 2, 2026 at 1:58 AM Charlie Jenkins
> <thecharlesjenkins@gmail.com> wrote:
> >
> > > Before this patch, preempt_disable is not enough to protect the SR_FS
> > > update in signal handling. As kernel-mode vector, which also update
> > > regs->status, is allowed in softirq context and it can come right after
> > > serving an interrupt.
> > >
> > > Move SR_FS and SR_VS into its own separate field in pt_regs so that
> > > RMW of SR_FS does not need to carry the same synchronization primitive
> > > as non-atomic update of SR_VS. Also, export vendor's alternative macros
> > > so that they can be reused in assembly code.
> > >
> > > CC: Guobin Zhang <guobin.zhang@intel.com>
> > > Fixes: 956895b9d8f7 ("riscv: vector: make Vector always available for softirq context")
> > > Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
> > >
> > > diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h
> > > index 621f84d1898f..71c7fd9d8f26 100644
> > > --- a/arch/riscv/include/asm/csr.h
> > > +++ b/arch/riscv/include/asm/csr.h
> > > @@ -27,6 +27,14 @@
> > >  #define SR_ELP               SR_SPELP
> > >  #endif
> > >
> > > +#define SR_OFF               _AC(0x00000000, UL)
> > > +#define SR_INITIAL   _AC(0x00000001, UL)
> > > +#define SR_CLEAN     _AC(0x00000002, UL)
> > > +#define SR_DIRTY     _AC(0x00000003, UL)
> > > +#define SR_VS_THEAD_SHIFT            23
> > > +#define SR_VS_SHIFT                  9
> > > +#define SR_FS_SHIFT                  13
> > > +
> > >  #define SR_FS                _AC(0x00006000, UL) /* Floating-point Status */
> > >  #define SR_FS_OFF    _AC(0x00000000, UL)
> > >  #define SR_FS_INITIAL        _AC(0x00002000, UL)
> > > @@ -52,6 +60,7 @@
> > >  #define SR_XS_DIRTY  _AC(0x00018000, UL)
> > >
> > >  #define SR_FS_VS     (SR_FS | SR_VS) /* Vector and Floating-Point Unit */
> > > +#define SR_FS_VS_THEAD       (SR_FS | SR_VS_THEAD)
> > >
> > >  #ifndef CONFIG_64BIT
> > >  #define SR_SD                _AC(0x80000000, UL) /* FS/VS/XS dirty */
> > > diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
> > > index addc8188152f..a56498055218 100644
> > > --- a/arch/riscv/include/asm/ptrace.h
> > > +++ b/arch/riscv/include/asm/ptrace.h
> > > @@ -49,6 +49,8 @@ struct pt_regs {
> > >       unsigned long t6;
> > >       /* Supervisor/Machine CSRs */
> > >       unsigned long status;
> > > +     u8 sr_vs;
> > > +     u8 sr_fs;
> > >       unsigned long badaddr;
> > >       unsigned long cause;
> > >       /* a0 value before the syscall */
> > > diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
> > > index 970f89935f0e..61588f7b03cf 100644
> > > --- a/arch/riscv/include/asm/switch_to.h
> > > +++ b/arch/riscv/include/asm/switch_to.h
> > > @@ -22,21 +22,19 @@ extern void __fstate_restore(struct task_struct *restore_from);
> > >
> > >  static inline void __fstate_clean(struct pt_regs *regs)
> > >  {
> > > -     regs->status = (regs->status & ~SR_FS) | SR_FS_CLEAN;
> > > +     regs->sr_fs = SR_CLEAN;
> > >  }
> > >
> > >  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();
> > > +     regs->sr_fs = SR_FS_OFF;
> > I think these operations might be able to be done atomically and then we
> > don't need to disable preemption or split these away from sstatus for
> > clean/off.
> >
> > __fstate_clean is only ever entered when initial, clean, or dirty.
> >
> > There is a single entry point when regs->status=initial and that is
> > start_thread(), which immediately goes on to call fstate_restore() which
> > sets regs->status to clean. So as it stands, I don't think there is any
> > functional difference between initial and clean. We could just have all
> > threads start at clean and get rid of setting it to initial in the first
> > place. The semantics of having a thread start off as "initial" only to
> > immediately have registers restored seems off to me anyway, I would
> > expect "initial" to be some default fp values instead of parent fp
> > values as it is being used here.
> >
> > When __fstate_clean is entered with regs->status=clean/dirty a simple
> > atomic and of ~0x2000 can be performed to arrive at clean again.
> >
> 
> Thanks for the feedback! The idea works for SR_FS as it should really
> be swinging between "dirty" and "clean". Using amo operations on SR_VS
> does not help as an onging series is making "initial" as a valid state
> [1], e.g. to optimize context tracking on the syscall path.

Aww okay I didn't realize that patch was doing that! I did just look at
that series and I think it's possible to make these ideas not conflict.
You are using INITIAL will the goal of avoiding unecessary save/restores
which ties perfectly since the save/restore paths that are setting the
VS bits can be bypassed so we avoid the issue of needing to transition
from INITIAL->CLEAN. I guess my point is that I don't think it makes
semantic sense to be able to transition from INITIAL->CLEAN. INITIAL
should stay INITIAL until there is state that needs to be restored and
then it can transition to DIRTY before transitioning to CLEAN. It looks
like how you are currently using it, INITIAL will be persisted across
the syscall path and not transition at all since it will be discarded at
the end anyways which makes sense.

> Most SR_VS
> bit ops are properly guarded, one thing that is missing on the current
> code is at __restore_v_state() in sigreturn. Therefore, to fully adopt
> this approach, we can do something like:
> - Use AMO for SR_FS cleaning
> - Use the legacy way to operate on SR_VS
> - Guard the vstate clearing op at sigreturn with
> get/put_cpu_vector_context(), and find any leftover code that does not
> properly guard it
> 
> AMO store on some systems may not be as fast as a regular store, that
> could be something worth benchmarking

Thinking about this more, this does not need to be atomic. To make that
claim I feel the need to give a potentially over thought explanation.
The condition we care about is that we are not missing a restore. Our
goal is to guarantee that everytime there is a save, that a restore
follows. It would be functionally acceptable if there are multiple
duplicated saves. But this is not a race that we are concerned about
because the two cases that we see when we are changing the FS/VS bits
are 1) save/restore happens on the same core so ordering will be correct
2) save happens on one core, gets migrated to another core, restore
happens but during the migration flushing is done so no concern there
either. The actual race we are seeing is that between reading the status
bit and doing the OR operation and then writing back, that the
underlying status register changes and we overwrite intermediate
changes. We can avoid this tearing by using WRITE_ONCE/READ_ONCE and we
do not need atomics.

> 
> [1]: https://lore.kernel.org/all/20261111184359.1239821-1-tchiu@tenstorrent.com/
> 
> Please let me know if you have any preference, thanks!

My hesitancy around adding to pt_regs is that it does end up eating up
some kernel stack space which is precious and I am not confident it is
necessary, and I would like to add to pt_regs only if it is necessary.

- Charlie

> 
> Cheers,
> Andy
> 
> > __fstate_off previously has the somewhat silly operation of
> > "(regs->status & ~SR_FS) | SR_FS_OFF" which is the same as "regs->status
> > & ~SR_FS" since SR_FS_OFF is 0. An atomic and can represent this as
> > well.
> >
> > The same idea should be true for vector.
> >
> > Let me know if I am missing some condition here :)
> >
> > --
> > - Charlie
> >

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-03  9:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 22:01 [PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status Andy Chiu
2026-12-01 18:10 ` Frédéric Mahé
2026-12-02  7:58 ` Charlie Jenkins
2026-12-02 23:33   ` [EXT] " Andy Chiu
2026-12-03  9:08     ` Charlie Jenkins

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox