mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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,
	syzbot+58cbd7974a103b8b8916@syzkaller.appspotmail.com,
	syzbot+b07ec23d35be305855be@syzkaller.appspotmail.com,
	stable@vger.kernel.org
Subject: [PATCH 3/6] x86/fpu: Reset the fpstate on exec() under fpregs_lock()
Date: Mon, 30 Nov 2026 20:51:39 +0000	[thread overview]
Message-ID: <20261130205143.1258369-4-chang.seok.bae@intel.com> (raw)
In-Reply-To: <20261130205143.1258369-1-chang.seok.bae@intel.com>

fpregs_lock() should be held when either FPU registers or storage is
updated. The fpstate metadata is also relevant so fpstate_reset() has to
be under the lock. Currently fpu_flush_thread() has fpstate_reset()
without the lock.

A couple of sysbot reports [1,2] both indicated a race condition to a
task that previously loaded dynamic states.

To address this issue, first prepare the helper by renaming to
fpu_reset_fpstate() and providing the storage reset option to accommodate
the fpu_clone() path. Then, move fpstate_reset() under the lock.

Fixes: e61d6310a0f8 ("x86/fpu: Reset permission and fpstate on exec()")
Closes: https://syzkaller.appspot.com/bug?extid=58cbd7974a103b8b8916 [1]
Closes: https://syzkaller.appspot.com/bug?extid=b07ec23d35be305855be [2]
Reported-by: syzbot+58cbd7974a103b8b8916@syzkaller.appspotmail.com
Reported-by: syzbot+b07ec23d35be305855be@syzkaller.appspotmail.com
Suggested-by: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Cc: Ömer Mete Kaya <omermetekaya0@gmail.com>
Cc: stable@vger.kernel.org
---
 arch/x86/kernel/fpu/core.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 80847a08ff3bb..de746eedfe397 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -613,6 +613,9 @@ static void __fpstate_reset(struct fpstate *fpstate)
 	fpstate->user_xfeatures	= fpu_user_cfg.default_features;
 }
 
+/*
+ * fpregs_lock() must be held when fpstate belongs to the current task
+ */
 void fpstate_reset(struct fpu *fpu)
 {
 	/* Set the fpstate pointer to the default fpstate */
@@ -674,15 +677,21 @@ static int update_fpu_shstk(struct task_struct *dst, unsigned long ssp)
 }
 
 /*
- * Reset FPU register storage to the init values from:
+ * Reset fpstate metadata and 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)
+static void fpu_reset_fpstate(struct task_struct *tsk, struct fpu *fpu, bool reset_regs)
 {
+	/* Current must get its register storage reset. */
+	WARN_ON_ONCE(tsk == current && !reset_regs);
+
 	if (tsk == current)
 		fpregs_lock();
 
+	/* Reset the fpstate metadata: */
+	fpstate_reset(fpu);
+
 	/*
 	 * This does not change the actual hardware registers. It just
 	 * resets the memory image. Do not use fpstate_init() here. Just copy
@@ -691,8 +700,17 @@ static void fpu_reset_fpstate_regs(struct task_struct *tsk, struct fpu *fpu)
 	 * PKRU handling does not rely on the xstate when restoring for
 	 * user space as PKRU is eagerly written in switch_to() and
 	 * flush_thread().
+	 *
+	 * When skipping the reset, from fpu_clone(), it means the new task
+	 * inherits the current register context instead.
+	 */
+	if (reset_regs)
+		memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size());
+
+	/*
+	 * Even if !reset_regs, new task's FPU state cannot be valid in the
+	 * hardware
 	 */
-	memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size());
 	__fpu_invalidate_fpregs_state(fpu);
 
 	/*
@@ -720,10 +738,11 @@ int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal,
 
 	BUILD_BUG_ON(sizeof(*dst) % SMP_CACHE_BYTES != 0);
 
-	fpstate_reset(dst_fpu);
-
-	/* Fill the new task FPU storage with init-state */
-	fpu_reset_fpstate_regs(dst, dst_fpu);
+	/*
+	 * Reset the fpstate metadata and fill the new task FPU storage
+	 * with init-state when needed.
+	 */
+	fpu_reset_fpstate(dst, dst_fpu, minimal);
 
 	/*
 	 * No FPU state inheritance for kernel threads and IO
@@ -874,8 +893,7 @@ void fpu__clear_user_states(struct fpu *fpu)
 
 void fpu_flush_thread(void)
 {
-	fpstate_reset(x86_task_fpu(current));
-	fpu_reset_fpstate_regs(current, x86_task_fpu(current));
+	fpu_reset_fpstate(current, x86_task_fpu(current), true);
 }
 /*
  * Load FPU context before returning to userspace.
-- 
2.53.0


  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 ` [PATCH 2/6] x86/fpu: Refactor fpstate storage reset sequence Chang S. Bae
2026-11-30 20:51 ` Chang S. Bae [this message]
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-4-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=syzbot+58cbd7974a103b8b8916@syzkaller.appspotmail.com \
    --cc=syzbot+b07ec23d35be305855be@syzkaller.appspotmail.com \
    --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®