[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20241024-arm64-fp-sig-flush-v1-1-05148eb92a84@kernel.org>
Date: Thu, 24 Oct 2024 12:06:42 +0100
From: Mark Brown <broonie@...nel.org>
To: Catalin Marinas <catalin.marinas@....com>,
Will Deacon <will@...nel.org>
Cc: Mark Rutland <mark.rutland@....com>, Dave Martin <Dave.Martin@....com>,
linux-arm-kernel@...ts.infradead.org, linux-kernel@...r.kernel.org,
Mark Brown <broonie@...nel.org>
Subject: [PATCH] arm64/signal: Consistently invalidate the in register FP
state in restore
When restoring the SVE and SME specific floating point register states we
flush the task floating point state, marking the hardware state as stale so
that preemption does not result in us saving register state from the signal
handler on top of the restored context and forcing a reload from memory.
For the plain FPSIMD state we don't do this, we just copy the state from
userspace and then force an immediate reload of the register state.
This isn't racy against context switch since we copy the incoming data
onto the stack rather than directly into the task struct but it's still
messy and inconsistent.
Simplify things and avoid a potential source of error by moving the
invalidation of the CPU state to the main restore_sigframe() and
reworking the restore of the FPSIMD state to update the task struct and
rely on loading as part of the general do_notify_resume() handling for
return to user like we do for the SVE and SME state.
Signed-off-by: Mark Brown <broonie@...nel.org>
---
arch/arm64/kernel/signal.c | 70 +++++++++++++++-------------------------------
1 file changed, 22 insertions(+), 48 deletions(-)
diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index 5619869475304776fc005fe24a385bf86bfdd253..a536d65b1f11957fa0f84094df542175d050e00a 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -210,7 +210,7 @@ static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
static int restore_fpsimd_context(struct user_ctxs *user)
{
- struct user_fpsimd_state fpsimd;
+ struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state;
int err = 0;
/* check the size information */
@@ -218,18 +218,14 @@ static int restore_fpsimd_context(struct user_ctxs *user)
return -EINVAL;
/* copy the FP and status/control registers */
- err = __copy_from_user(fpsimd.vregs, &(user->fpsimd->vregs),
- sizeof(fpsimd.vregs));
- __get_user_error(fpsimd.fpsr, &(user->fpsimd->fpsr), err);
- __get_user_error(fpsimd.fpcr, &(user->fpsimd->fpcr), err);
+ err = __copy_from_user(fpsimd->vregs, &(user->fpsimd->vregs),
+ sizeof(fpsimd->vregs));
+ __get_user_error(fpsimd->fpsr, &(user->fpsimd->fpsr), err);
+ __get_user_error(fpsimd->fpcr, &(user->fpsimd->fpcr), err);
clear_thread_flag(TIF_SVE);
current->thread.fp_type = FP_STATE_FPSIMD;
- /* load the hardware registers from the fpsimd_state structure */
- if (!err)
- fpsimd_update_current_state(&fpsimd);
-
return err ? -EFAULT : 0;
}
@@ -333,7 +329,7 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
{
int err = 0;
unsigned int vl, vq;
- struct user_fpsimd_state fpsimd;
+ struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state;
u16 user_vl, flags;
if (user->sve_size < sizeof(*user->sve))
@@ -376,16 +372,6 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
if (user->sve_size < SVE_SIG_CONTEXT_SIZE(vq))
return -EINVAL;
- /*
- * Careful: we are about __copy_from_user() directly into
- * thread.sve_state with preemption enabled, so protection is
- * needed to prevent a racing context switch from writing stale
- * registers back over the new data.
- */
-
- fpsimd_flush_task_state(current);
- /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
-
sve_alloc(current, true);
if (!current->thread.sve_state) {
clear_thread_flag(TIF_SVE);
@@ -408,14 +394,10 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
fpsimd_only:
/* copy the FP and status/control registers */
/* restore_sigframe() already checked that user->fpsimd != NULL. */
- err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs,
- sizeof(fpsimd.vregs));
- __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err);
- __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err);
-
- /* load the hardware registers from the fpsimd_state structure */
- if (!err)
- fpsimd_update_current_state(&fpsimd);
+ err = __copy_from_user(fpsimd->vregs, user->fpsimd->vregs,
+ sizeof(fpsimd->vregs));
+ __get_user_error(fpsimd->fpsr, &user->fpsimd->fpsr, err);
+ __get_user_error(fpsimd->fpcr, &user->fpsimd->fpcr, err);
return err ? -EFAULT : 0;
}
@@ -524,16 +506,6 @@ static int restore_za_context(struct user_ctxs *user)
if (user->za_size < ZA_SIG_CONTEXT_SIZE(vq))
return -EINVAL;
- /*
- * Careful: we are about __copy_from_user() directly into
- * thread.sme_state with preemption enabled, so protection is
- * needed to prevent a racing context switch from writing stale
- * registers back over the new data.
- */
-
- fpsimd_flush_task_state(current);
- /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
-
sme_alloc(current, true);
if (!current->thread.sme_state) {
current->thread.svcr &= ~SVCR_ZA_MASK;
@@ -601,16 +573,6 @@ static int restore_zt_context(struct user_ctxs *user)
if (nregs != 1)
return -EINVAL;
- /*
- * Careful: we are about __copy_from_user() directly into
- * thread.zt_state with preemption enabled, so protection is
- * needed to prevent a racing context switch from writing stale
- * registers back over the new data.
- */
-
- fpsimd_flush_task_state(current);
- /* From now, fpsimd_thread_switch() won't touch ZT in thread state */
-
err = __copy_from_user(thread_zt_state(¤t->thread),
(char __user const *)user->zt +
ZT_SIG_REGS_OFFSET,
@@ -876,6 +838,18 @@ static int restore_sigframe(struct pt_regs *regs,
if (err == 0)
err = parse_user_sigframe(&user, sf);
+ /*
+ * Careful: we are about __copy_from_user() directly into
+ * thread floating point state with preemption enabled, so
+ * protection is needed to prevent a racing context switch
+ * from writing stale registers back over the new data. Mark
+ * the register floating point state as invalid and unbind the
+ * task from the CPU to force a reload before we return to
+ * userspace. fpsimd_flush_task_state() has a check for FP
+ * support.
+ */
+ fpsimd_flush_task_state(current);
+
if (err == 0 && system_supports_fpsimd()) {
if (!user.fpsimd)
return -EINVAL;
---
base-commit: 8e929cb546ee42c9a61d24fae60605e9e3192354
change-id: 20241023-arm64-fp-sig-flush-e41dc9c5abe9
Best regards,
--
Mark Brown <broonie@...nel.org>
Powered by blists - more mailing lists