From: "Chang S. Bae" <chang.seok.bae@intel.com>
To: linux-kernel@vger.kernel.org
Cc: x86@kernel.org, tglx@kernel.org, mingo@redhat.com, bp@alien8.de,
dave.hansen@linux.intel.com, weiguixiong@bytedance.com,
omermetekaya0@gmail.com, chang.seok.bae@intel.com,
stable@vger.kernel.org
Subject: [PATCH 2/6] x86/fpu: Refactor fpstate storage reset sequence
Date: Mon, 30 Nov 2026 20:51:38 +0000 [thread overview]
Message-ID: <20261130205143.1258369-3-chang.seok.bae@intel.com> (raw)
In-Reply-To: <20261130205143.1258369-1-chang.seok.bae@intel.com>
fpu_clone() and fpu_flush_thread() both reset the fpstate register
storage with the same sequence. Move it into one helper.
fpu_clone() doesn't need the storage reset always but for now prepare for
the next change, that moves the fpstate metadata reset to there and that
will also make the storage reset optional.
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Cc: stable@vger.kernel.org
---
Note for backporting, this prepares:
Fixes: e61d6310a0f8 ("x86/fpu:Reset permission and fpstate on exec()")
---
arch/x86/kernel/fpu/core.c | 77 ++++++++++++++++++--------------------
1 file changed, 36 insertions(+), 41 deletions(-)
diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index bbe881cf44923..80847a08ff3bb 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -673,6 +673,38 @@ static int update_fpu_shstk(struct task_struct *dst, unsigned long ssp)
return 0;
}
+/*
+ * Reset FPU register storage to the init values from:
+ * - fpu_clone() for new task's FPU state
+ * - fpu_flush_thread() for current
+ */
+static void fpu_reset_fpstate_regs(struct task_struct *tsk, struct fpu *fpu)
+{
+ if (tsk == current)
+ fpregs_lock();
+
+ /*
+ * This does not change the actual hardware registers. It just
+ * resets the memory image. Do not use fpstate_init() here. Just copy
+ * init_fpstate which has the correct content already except for PKRU.
+ *
+ * PKRU handling does not rely on the xstate when restoring for
+ * user space as PKRU is eagerly written in switch_to() and
+ * flush_thread().
+ */
+ memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size());
+ __fpu_invalidate_fpregs_state(fpu);
+
+ /*
+ * Subsequently enforce reload for user space tasks and prevent kernel
+ * threads from trying to save the FPU registers on context switch.
+ */
+ set_tsk_thread_flag(tsk, TIF_NEED_FPU_LOAD);
+
+ if (tsk == current)
+ fpregs_unlock();
+}
+
/* Clone current's FPU state on fork */
int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal,
unsigned long ssp)
@@ -688,27 +720,17 @@ int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal,
BUILD_BUG_ON(sizeof(*dst) % SMP_CACHE_BYTES != 0);
- /* The new task's FPU state cannot be valid in the hardware. */
- dst_fpu->last_cpu = -1;
-
fpstate_reset(dst_fpu);
- /*
- * Enforce reload for user space tasks and prevent kernel threads
- * from trying to save the FPU registers on context switch.
- */
- set_tsk_thread_flag(dst, TIF_NEED_FPU_LOAD);
+ /* Fill the new task FPU storage with init-state */
+ fpu_reset_fpstate_regs(dst, dst_fpu);
/*
* No FPU state inheritance for kernel threads and IO
* worker threads.
*/
- if (minimal) {
- /* Clear out the minimal state */
- memcpy(&dst_fpu->fpstate->regs, &init_fpstate.regs,
- init_fpstate_copy_size());
+ if (minimal)
return 0;
- }
/*
* If a new feature is added, ensure all dynamic features are
@@ -814,33 +836,6 @@ static inline void restore_fpregs_from_init_fpstate(u64 features_mask)
pkru_write_default();
}
-/*
- * Reset current->fpu memory state to the init values.
- */
-static void fpu_reset_fpstate_regs(void)
-{
- struct fpu *fpu = x86_task_fpu(current);
-
- fpregs_lock();
- __fpu_invalidate_fpregs_state(fpu);
- /*
- * This does not change the actual hardware registers. It just
- * resets the memory image and sets TIF_NEED_FPU_LOAD so a
- * subsequent return to usermode will reload the registers from the
- * task's memory image.
- *
- * Do not use fpstate_init() here. Just copy init_fpstate which has
- * the correct content already except for PKRU.
- *
- * PKRU handling does not rely on the xstate when restoring for
- * user space as PKRU is eagerly written in switch_to() and
- * flush_thread().
- */
- memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size());
- set_thread_flag(TIF_NEED_FPU_LOAD);
- fpregs_unlock();
-}
-
/*
* Reset current's user FPU states to the init states. current's
* supervisor states, if any, are not modified by this function. The
@@ -880,7 +875,7 @@ void fpu__clear_user_states(struct fpu *fpu)
void fpu_flush_thread(void)
{
fpstate_reset(x86_task_fpu(current));
- fpu_reset_fpstate_regs();
+ fpu_reset_fpstate_regs(current, x86_task_fpu(current));
}
/*
* Load FPU context before returning to userspace.
--
2.53.0
next prev parent reply other threads:[~2026-11-30 21:19 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 20:51 [PATCH 0/6] x86/fpu: Fix issues around resetting fpstate Chang S. Bae
2026-11-30 20:51 ` [PATCH 1/6] x86/fpu: Remove remaining fpstate reset for init_task Chang S. Bae
2026-11-30 20:51 ` Chang S. Bae [this message]
2026-11-30 20:51 ` [PATCH 3/6] x86/fpu: Reset the fpstate on exec() under fpregs_lock() Chang S. Bae
2026-11-30 20:51 ` [PATCH 4/6] x86/fpu: Refactor fpstate memory free helpers Chang S. Bae
2026-11-30 20:51 ` [PATCH 5/6] x86/fpu: Fix memory leak with dynamic fpstate and exec() Chang S. Bae
2026-11-30 20:51 ` [PATCH 6/6] selftests/x86/amx: Test fpstate memory cleanup on exec() Chang S. Bae
2026-11-30 21:33 ` Chang S. Bae
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130205143.1258369-3-chang.seok.bae@intel.com \
--to=chang.seok.bae@intel.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=omermetekaya0@gmail.com \
--cc=stable@vger.kernel.org \
--cc=tglx@kernel.org \
--cc=weiguixiong@bytedance.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®