From: Gleb Pesin riscv_v_is_on() tests only the standard sstatus.VS field. Since commit d863910eabaf ("riscv: vector: Support xtheadvector save/restore"), riscv_v_enable() and riscv_v_disable() set and clear SR_VS_THEAD instead on cores with xtheadvector, so riscv_v_is_on() returns false there even while the vector unit is enabled. __switch_to_vector() relies on riscv_v_is_on() to recognise a task that is switched out inside a preemptible kernel-mode vector section. On xtheadvector cores that check never succeeds: the outgoing task neither disables the vector unit nor sets RISCV_PREEMPT_V_IN_SCHEDULE, and riscv_v_enable() is skipped when the task is switched back in. Test the status field that the running core actually uses. Fixes: d863910eabaf ("riscv: vector: Support xtheadvector save/restore") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Gleb Pesin --- arch/riscv/include/asm/vector.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index fffe72a77..910313dd0 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -128,7 +128,9 @@ static __always_inline void riscv_v_disable(void) static __always_inline bool riscv_v_is_on(void) { - return !!(csr_read(CSR_SSTATUS) & SR_VS); + unsigned long mask = has_xtheadvector() ? SR_VS_THEAD : SR_VS; + + return !!(csr_read(CSR_SSTATUS) & mask); } static __always_inline void __vstate_csr_save(struct __riscv_v_ext_state *dest) -- 2.53.0 From: Gleb Pesin and provide the constants that ALTERNATIVE() needs for a vendor extension (RISCV_VENDOR_EXT_ALTERNATIVES_BASE and RISCV_ISA_VENDOR_EXT_*), but they also pull in C headers and declarations, so assembly files cannot include them. Keep the constants visible to assembly and guard the rest with __ASSEMBLY__. No functional change. This is needed by the next patch, which uses the xtheadvector alternative in entry.S. Cc: stable@vger.kernel.org # 6.14+: needed by the next patch Assisted-by: LLM Signed-off-by: Gleb Pesin --- arch/riscv/include/asm/vendor_extensions.h | 28 ++++++++++++++---------- arch/riscv/include/asm/vendor_extensions/thead.h | 8 +++++-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/arch/riscv/include/asm/vendor_extensions.h b/arch/riscv/include/asm/vendor_extensions.h index 7437304a7..c9fca8591 100644 --- a/arch/riscv/include/asm/vendor_extensions.h +++ b/arch/riscv/include/asm/vendor_extensions.h @@ -6,16 +6,25 @@ #ifndef _ASM_VENDOR_EXTENSIONS_H #define _ASM_VENDOR_EXTENSIONS_H -#include - -#include -#include - /* * The extension keys of each vendor must be strictly less than this value. */ #define RISCV_ISA_VENDOR_EXT_MAX 32 +/* + * 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 __ASSEMBLY__ + +#include + +#include +#include + struct riscv_isavendorinfo { DECLARE_BITMAP(isa, RISCV_ISA_VENDOR_EXT_MAX); }; @@ -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); @@ -101,4 +103,6 @@ static __always_inline bool riscv_cpu_has_vendor_extension_unlikely(const unsign return __riscv_isa_vendor_extension_available(cpu, vendor, ext); } +#endif /* __ASSEMBLY__ */ + #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 e85c75b3b..625f44175 100644 --- a/arch/riscv/include/asm/vendor_extensions/thead.h +++ b/arch/riscv/include/asm/vendor_extensions/thead.h @@ -4,13 +4,15 @@ #include -#include - /* * Extension keys must be strictly less than RISCV_ISA_VENDOR_EXT_MAX. */ #define RISCV_ISA_VENDOR_EXT_XTHEADVECTOR 0 +#ifndef __ASSEMBLY__ + +#include + extern struct riscv_isa_vendor_ext_data_list riscv_isa_vendor_ext_list_thead; #ifdef CONFIG_RISCV_ISA_VENDOR_EXT_THEAD @@ -19,6 +21,8 @@ void disable_xtheadvector(void); static inline void disable_xtheadvector(void) { } #endif +#endif /* __ASSEMBLY__ */ + /* Extension specific helpers */ /* -- 2.53.0 From: Gleb Pesin handle_exception clears SR_FS_VS on entry so that stray floating-point or vector instructions in the kernel raise an illegal-instruction exception. xtheadvector keeps its vector status in SR_VS_THEAD (sstatus bits 24:23), which that mask does not cover. On 64-bit kernels bit 23 happens to be cleared as SR_SPELP, so the T-Head field is only partly reset: DIRTY becomes CLEAN, and the vector unit stays enabled in the kernel after a trap from a context that used it. Clear the whole SR_VS_THEAD field on cores with xtheadvector. The status saved in pt_regs still holds the interrupted value, so the vector state is restored on return exactly as before. Other cores keep the current mask, because bit 24 is not part of the vector status there. Fixes: d863910eabaf ("riscv: vector: Support xtheadvector save/restore") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Gleb Pesin --- arch/riscv/kernel/entry.S | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index d799c4e56..aeb99c20b 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include .section .irqentry.text, "ax" @@ -176,9 +178,14 @@ SYM_CODE_START(handle_exception) * actual user copy routines. * * Disable the FPU/Vector to detect illegal usage of floating point - * or vector in kernel space. + * or vector in kernel space. xtheadvector keeps its vector status in + * a different sstatus field; those bits are reserved elsewhere. */ - li t0, SR_SUM | SR_FS_VS + ALTERNATIVE(__stringify(li t0, SR_SUM | SR_FS_VS), + __stringify(li t0, SR_SUM | SR_FS_VS | SR_VS_THEAD), + THEAD_VENDOR_ID, + RISCV_VENDOR_EXT_ALTERNATIVES_BASE + RISCV_ISA_VENDOR_EXT_XTHEADVECTOR, + CONFIG_RISCV_ISA_XTHEADVECTOR) #ifdef CONFIG_64BIT li t1, SR_ELP or t0, t0, t1 -- 2.53.0