[PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status
Andy Chiu
tchiu at tenstorrent.com
Mon Nov 30 14:01:57 PST 2026
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 at intel.com>
Fixes: 956895b9d8f7 ("riscv: vector: make Vector always available for softirq context")
Signed-off-by: Andy Chiu <tchiu at 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
More information about the linux-riscv
mailing list