From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796359533; b=CLlw5UwOsnKllAPvMMH1S2lZFm1c2seV9/N9jn6qUm60UllO9Lm8YFf8G7NTbWg3Qf6T +qCRSkMOgnLzXuxUFcdavAg77F72Sf0C7o9aIhc2vRPQ3EVztbwjpsvwFD+oclhb1NYAp FGq6T9eHGp6j+uisiGq4tNK4du24eIbsgrrChkoD7CcRwDuVOd7DOOemFf5txxpCwQX1Q E+4WdBog0E8SOBhAVe8TtR9h2PraHtDW00Da4ISJvXxr+z3GPq0saZ+P5yFBgESFNlVoG 34MmMHmT4c08XE5fgZHJG8oUKBxjttFiBWNl0e3EkuJbCLUHJDszyXWp1eFIPj8ESRw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796359533; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=vC+iXnkn3+1qHVGG/egK9cudToZb/NRo1uIpUrWQx3U=; b=kaUO5LnkXcRYyjw6CA/Zgr8W08FHIunq3+CK2ImLM7zmVSdZEJLZzu4Bzl4w8Is+OU+X I3CnEYoYT4F3/teiA7xiy6AnILsqjkBz5iar0pXNwvWyut3ZVTa8vQRJPZLvtc4yoY4Dd f59ZJazAKGWFoa+MUtRDOxFq3DAUCxgQal46wQPUHZ2a/YUqeNPcArTcJSu9+/mGT0xM8 qxIyLQYANR01uQw0dKFgirodVatPg23vGGXsBMbQOM+Sr2ZyDtoD50Kj0je3teeSXcbe4 WGPVSPVAVUh1qZ1QyXHZSifwHqs+7GxWsoFNVjTXDhRIUWxGipCdfJhbFa6yoyn8euw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lhTSSLNT; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48490606A322 for ; Fri, 4 Dec 2026 04:45:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lhTSSLNT Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3352F1F000CE; Fri, 4 Dec 2026 04:45:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796359532; bh=vC+iXnkn3+1qHVGG/egK9cudToZb/NRo1uIpUrWQx3U=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lhTSSLNTmnYjF48y1slcHwIQjLY9vtdaMtFLGly4RWGYt0uh1zTus7mQAGS/dm2c7 lj5+8TT3nYfqhj5c4uIjcbeiNPBsiamUK9LEzdxPz091LYU3sYU6KHOUuE5S9LACof VFbfQQlMvX+eMW/mflE9qWSzkVavVyUbpC0LA9mUcPy4wm0KAgHCv2UPUJlx8EFOgv y6wUiWM/Tu7WjD4F96B9E+n4stqWUbHJy3r65StU81z1zlvSA2aTEqWME7esxJsmQo HIgMFvGMR7OfPhDD8LGCvaigCtN/42DQdDl2UPTqJ2jUkNo0s/LLWANnftT4USlKom iz//LdcZyjaNQ== From: Sasha Levin To: stable@vger.kernel.org Cc: Guobin Zhang , Andy Chiu , Paul Walmsley , Sasha Levin Subject: [PATCH 6.12.y] riscv: signal: protect regs->status RMW from concurrent preemption Date: Thu, 3 Dec 2026 23:45:30 -0500 Message-ID: <20261204044509.2651058-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120218-ongoing-squeamish-e2f5@gregkh> References: <2026120218-ongoing-squeamish-e2f5@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Guobin Zhang [ Upstream commit 018fe0700c569b50465b27d68deee7e9157e8e5f ] __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 in fstate_save() and fstate_restore(), which covers the RMW in __fstate_clean(), and around the RMW in fstate_off(). Add fstate_{save,restore}_preempt_off() for code that already runs with preemption disabled (the scheduler and kernel_mode_fpu). 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. Suggested-by: Andy Chiu Signed-off-by: Guobin Zhang Reviewed-by: Andy Chiu Link: https://patch.msgid.link/20261020-vector_fpu_regs_status_rmw_fix-v2-1-474a59155e96@intel.com Cc: stable@vger.kernel.org Signed-off-by: Paul Walmsley [ sashal: Kept the existing open-coded (regs->status & SR_VS) != SR_VS_OFF condition in riscv_v_vstate_restore() and added only the upstream WARN_ON_ONCE(preemptible()); before it, since the context there differs. ] Signed-off-by: Sasha Levin --- arch/riscv/include/asm/switch_to.h | 30 +++++++++++++++++++++++------ arch/riscv/include/asm/vector.h | 2 ++ arch/riscv/kernel/kernel_mode_fpu.c | 4 ++-- arch/riscv/kernel/signal.c | 2 ++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 9685cd85e57cc..919d873046e4e 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -27,11 +27,13 @@ static inline void __fstate_clean(struct pt_regs *regs) 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, - struct pt_regs *regs) +static inline void fstate_save_preempt_off(struct task_struct *task, + struct pt_regs *regs) { if ((regs->status & SR_FS) == SR_FS_DIRTY) { __fstate_save(task); @@ -39,8 +41,8 @@ static inline void fstate_save(struct task_struct *task, } } -static inline void fstate_restore(struct task_struct *task, - struct pt_regs *regs) +static inline void fstate_restore_preempt_off(struct task_struct *task, + struct pt_regs *regs) { if ((regs->status & SR_FS) != SR_FS_OFF) { __fstate_restore(task); @@ -48,14 +50,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) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index be7d309cca8a7..4c7989e415ef6 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -186,6 +186,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 ((regs->status & SR_VS) != SR_VS_OFF) { __riscv_v_vstate_restore(vstate, vstate->datap); __riscv_v_vstate_clean(regs); diff --git a/arch/riscv/kernel/kernel_mode_fpu.c b/arch/riscv/kernel/kernel_mode_fpu.c index 0ac8348876c49..03d847fbfcf68 100644 --- a/arch/riscv/kernel/kernel_mode_fpu.c +++ b/arch/riscv/kernel/kernel_mode_fpu.c @@ -14,7 +14,7 @@ void kernel_fpu_begin(void) { preempt_disable(); - fstate_save(current, task_pt_regs(current)); + fstate_save_preempt_off(current, task_pt_regs(current)); csr_set(CSR_SSTATUS, SR_FS); } EXPORT_SYMBOL_GPL(kernel_fpu_begin); @@ -22,7 +22,7 @@ EXPORT_SYMBOL_GPL(kernel_fpu_begin); void kernel_fpu_end(void) { csr_clear(CSR_SSTATUS, SR_FS); - fstate_restore(current, task_pt_regs(current)); + fstate_restore_preempt_off(current, task_pt_regs(current)); preempt_enable(); } EXPORT_SYMBOL_GPL(kernel_fpu_end); diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index c3c517b9eee55..5bcc7931fe9dc 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -124,7 +124,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(¤t->thread.vstate, &state->v_state, -- 2.53.0