mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/6] x86/fpu: Fix issues around resetting fpstate
@ 2026-11-30 20:51 Chang S. Bae
  2026-11-30 20:51 ` [PATCH 1/6] x86/fpu: Remove remaining fpstate reset for init_task Chang S. Bae
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Chang S. Bae @ 2026-11-30 20:51 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, weiguixiong, omermetekaya0,
	chang.seok.bae

Hi all,

This series addresses a couple of issues in fpu_flush_thread():

  Guixiong first reported and posted a patch [1] to address the memory
  leak on the exec() path. When TILE_DATA is active, the fpstate storage
  is expanded to record the large state. Then an exec() will reset the
  current fpstate entirely. But the issue there is not free up the
  reallocated fpstate memory - addressed in patch5 and test case in patch6.

  While working on this, syzbot reported another issue [2,3] on this path
  for resetting fpstate without fpregs_lock(). There is a race condition.
  Patch3 has the fix.

Then a couple of follow-up on that:

  Right now fpu_reset_fpstate_regs() is called on the exec() path only
  but it holds the lock just for the register storage. Including the
  metadata reset, as per Dave's suggestion, makes the same code sequence
  as in fpu_clone(). Similarily fpstate free code could be restructured.
  So, the series ends up including some refactoring (patch2/4).

  Finally, while looking at the fpstate_reset() sites, a removable one
  was also found, so include the minor fix as well (patch1).

Cc'ed stable@ since those refactoring if bought will change the base
where the fixes are applied.

[1]: https://lore.kernel.org/20260929151013.81562-2-weiguixiong@bytedance.com
[2]: https://syzkaller.appspot.com/bug?extid=58cbd7974a103b8b8916
[3]: https://syzkaller.appspot.com/bug?extid=b07ec23d35be305855be

Thanks,
Chang

Chang S. Bae (6):
  x86/fpu: Remove remaining fpstate reset for init_task
  x86/fpu: Refactor fpstate storage reset sequence
  x86/fpu: Reset the fpstate on exec() under fpregs_lock()
  x86/fpu: Refactor fpstate memory free helpers
  x86/fpu: Fix memory leak with dynamic fpstate and exec()
  selftests/x86/amx: Test fpstate memory cleanup on exec()

 arch/x86/include/asm/fpu/api.h          |   6 +-
 arch/x86/kernel/fpu/core.c              | 111 +++++++++++++++---------
 arch/x86/kernel/fpu/xstate.c            |  19 ++--
 arch/x86/kernel/process.c               |   2 +-
 tools/testing/selftests/x86/Makefile    |   1 +
 tools/testing/selftests/x86/amx.c       |  51 ++++++++++-
 tools/testing/selftests/x86/amx_exec.sh |  87 +++++++++++++++++++
 7 files changed, 223 insertions(+), 54 deletions(-)
 create mode 100755 tools/testing/selftests/x86/amx_exec.sh


base-commit: 75b8262a97abcb972ef897dad5f94d425157b635
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/6] x86/fpu: Remove remaining fpstate reset for init_task
  2026-11-30 20:51 [PATCH 0/6] x86/fpu: Fix issues around resetting fpstate Chang S. Bae
@ 2026-11-30 20:51 ` Chang S. Bae
  2026-11-30 20:51 ` [PATCH 2/6] x86/fpu: Refactor fpstate storage reset sequence Chang S. Bae
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Chang S. Bae @ 2026-11-30 20:51 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, weiguixiong, omermetekaya0,
	chang.seok.bae, stable

The init task has no FPU storage, but the fallback path that disables
XSTATE still invokes fpstate_reset(). Remove it.

Fixes: 22aafe3bcb67 ("x86/fpu: Remove init_task FPU state dependencies, add debugging warning for PF_KTHREAD tasks")
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Cc: stable@vger.kernel.org
---
 arch/x86/kernel/fpu/xstate.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index da303714379cd..f69eb0a596f44 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -778,8 +778,6 @@ static void __init fpu__init_disable_system_xstate(unsigned int legacy_size)
 	 * XFD MSR.
 	 */
 	init_fpstate.xfd = 0;
-
-	fpstate_reset(x86_task_fpu(current));
 }
 
 static u64 __init host_default_mask(void)
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/6] x86/fpu: Refactor fpstate storage reset sequence
  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
  2026-11-30 20:51 ` [PATCH 3/6] x86/fpu: Reset the fpstate on exec() under fpregs_lock() Chang S. Bae
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Chang S. Bae @ 2026-11-30 20:51 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, weiguixiong, omermetekaya0,
	chang.seok.bae, stable

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 3/6] x86/fpu: Reset the fpstate on exec() under fpregs_lock()
  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
  2026-11-30 20:51 ` [PATCH 4/6] x86/fpu: Refactor fpstate memory free helpers Chang S. Bae
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Chang S. Bae @ 2026-11-30 20:51 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, weiguixiong, omermetekaya0,
	chang.seok.bae, syzbot+58cbd7974a103b8b8916,
	syzbot+b07ec23d35be305855be, stable

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 4/6] x86/fpu: Refactor fpstate memory free helpers
  2026-11-30 20:51 [PATCH 0/6] x86/fpu: Fix issues around resetting fpstate Chang S. Bae
                   ` (2 preceding siblings ...)
  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 ` 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
  5 siblings, 0 replies; 8+ messages in thread
From: Chang S. Bae @ 2026-11-30 20:51 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, weiguixiong, omermetekaya0,
	chang.seok.bae, stable

Upcoming changes to free up vmalloc'ed fpstate memory will have the same
code under fpstate_realloc(). Refactor the code into a helper in advance.

Rename the existing fpstate_free() to fpu_free_fpstate() since it takes a
struct fpu pointer. Then convert fpstate_free() to free a vmalloc'ed
fpstate and use it where it can.

No functional change.

Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Cc: stable@vger.kernel.org
---
Note for backporting, this prepares:
  Fixes: 500afbf645a0 ("x86/fpu/xstate: Add fpstate_realloc()/free()")
---
 arch/x86/include/asm/fpu/api.h |  6 ++++--
 arch/x86/kernel/fpu/xstate.c   | 17 +++++++++++------
 arch/x86/kernel/process.c      |  2 +-
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/arch/x86/include/asm/fpu/api.h b/arch/x86/include/asm/fpu/api.h
index 90c63fe19c0fb..d3f5536f092ad 100644
--- a/arch/x86/include/asm/fpu/api.h
+++ b/arch/x86/include/asm/fpu/api.h
@@ -125,9 +125,11 @@ DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
 
 /* Process cleanup */
 #ifdef CONFIG_X86_64
-extern void fpstate_free(struct fpu *fpu);
+extern void fpu_free_fpstate(struct fpu *fpu);
+extern void fpstate_free(struct fpstate *fpstate);
 #else
-static inline void fpstate_free(struct fpu *fpu) { }
+static inline void fpu_free_fpstate(struct fpu *fpu) { }
+static inline void fpstate_free(struct fpstate *fpstate) { }
 #endif
 
 /* fpstate-related functions which are exported to KVM */
diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index f69eb0a596f44..22ddc00689a6b 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -1594,10 +1594,17 @@ static int __init xfd_update_static_branch(void)
 }
 arch_initcall(xfd_update_static_branch)
 
-void fpstate_free(struct fpu *fpu)
+void fpstate_free(struct fpstate *fpstate)
 {
-	if (fpu->fpstate && fpu->fpstate != &fpu->__fpstate)
-		vfree(fpu->fpstate);
+	/* Only free valloc'ed state */
+	if (fpstate && fpstate->is_valloc)
+		vfree(fpstate);
+}
+
+void fpu_free_fpstate(struct fpu *fpu)
+{
+	if (fpu->fpstate != &fpu->__fpstate)
+		fpstate_free(fpu->fpstate);
 }
 
 /**
@@ -1678,9 +1685,7 @@ static int fpstate_realloc(u64 xfeatures, unsigned int ksize,
 		xfd_update_state(fpu->fpstate);
 	fpregs_unlock();
 
-	/* Only free valloc'ed state */
-	if (curfps && curfps->is_valloc)
-		vfree(curfps);
+	fpstate_free(curfps);
 
 	return 0;
 }
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index 85e6b076ef17b..b4fd62c1921d8 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -118,7 +118,7 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 void arch_release_task_struct(struct task_struct *tsk)
 {
 	if (fpu_state_size_dynamic() && !(tsk->flags & (PF_KTHREAD | PF_USER_WORKER)))
-		fpstate_free(x86_task_fpu(tsk));
+		fpu_free_fpstate(x86_task_fpu(tsk));
 }
 #endif
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 5/6] x86/fpu: Fix memory leak with dynamic fpstate and exec()
  2026-11-30 20:51 [PATCH 0/6] x86/fpu: Fix issues around resetting fpstate Chang S. Bae
                   ` (3 preceding siblings ...)
  2026-11-30 20:51 ` [PATCH 4/6] x86/fpu: Refactor fpstate memory free helpers Chang S. Bae
@ 2026-11-30 20:51 ` Chang S. Bae
  2026-11-30 20:51 ` [PATCH 6/6] selftests/x86/amx: Test fpstate memory cleanup on exec() Chang S. Bae
  5 siblings, 0 replies; 8+ messages in thread
From: Chang S. Bae @ 2026-11-30 20:51 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, weiguixiong, omermetekaya0,
	chang.seok.bae, stable

Using an XFD-controlled xfeature can allocate a larger fpstate. On
exec(), fpstate_reset() replaces fpu->fpstate with the embedded buffer
without freeing the dynamic allocation. Once the pointer is overwritten,
the allocation cannot be freed on task exit.

Free vmalloc'ed area from fpu_reset_fpstate().

Fixes: 500afbf645a0 ("x86/fpu/xstate: Add fpstate_realloc()/free()")
Closes: https://lore.kernel.org/20260929151013.81562-2-weiguixiong@bytedance.com
Reported-by: Guixiong Wei <weiguixiong@bytedance.com>
Originally-by: Guixiong Wei <weiguixiong@bytedance.com>
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Cc: stable@vger.kernel.org
---
 arch/x86/kernel/fpu/core.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index de746eedfe397..f088b1f8518ea 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -680,16 +680,28 @@ static int update_fpu_shstk(struct task_struct *dst, unsigned long ssp)
  * Reset fpstate metadata and register storage to the init values from:
  *  - fpu_clone() for new task's FPU state
  *  - fpu_flush_thread() for current
+ *
+ * Also, free up the expanded storage.
  */
 static void fpu_reset_fpstate(struct task_struct *tsk, struct fpu *fpu, bool reset_regs)
 {
+	struct fpstate *oldfps = NULL;
+
 	/* Current must get its register storage reset. */
 	WARN_ON_ONCE(tsk == current && !reset_regs);
 
-	if (tsk == current)
+	/* New tasks must have a null pointer from arch_dup_task_struct() */
+	WARN_ON_ONCE(tsk != current && fpu->fpstate != NULL);
+
+	if (tsk == current) {
+		oldfps = fpu->fpstate;
 		fpregs_lock();
+	}
 
-	/* Reset the fpstate metadata: */
+	/*
+	 * Reset the fpstate metadata, including fpu->fpstate being
+	 * reset to __fpstate.
+	 */
 	fpstate_reset(fpu);
 
 	/*
@@ -719,8 +731,10 @@ static void fpu_reset_fpstate(struct task_struct *tsk, struct fpu *fpu, bool res
 	 */
 	set_tsk_thread_flag(tsk, TIF_NEED_FPU_LOAD);
 
-	if (tsk == current)
+	if (tsk == current) {
 		fpregs_unlock();
+		fpstate_free(oldfps);
+	}
 }
 
 /* Clone current's FPU state on fork */
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 6/6] selftests/x86/amx: Test fpstate memory cleanup on exec()
  2026-11-30 20:51 [PATCH 0/6] x86/fpu: Fix issues around resetting fpstate Chang S. Bae
                   ` (4 preceding siblings ...)
  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 ` Chang S. Bae
  2026-11-30 21:33   ` Chang S. Bae
  5 siblings, 1 reply; 8+ messages in thread
From: Chang S. Bae @ 2026-11-30 20:51 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, weiguixiong, omermetekaya0,
	chang.seok.bae, Shuah Khan, kselftest

Add a test to ensure no memory leak on exec(). If the task's fpstate is
expanded to support AMX large state, but not properly freed up, then it
fails.

The shell script makes the test simple, and relying on a specific symbol
is inevitable for the testing.

Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Cc: Guixiong Wei <weiguixiong@bytedance.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: kselftest@vger.kernel.org
---
 tools/testing/selftests/x86/Makefile    |  1 +
 tools/testing/selftests/x86/amx.c       | 51 ++++++++++++++-
 tools/testing/selftests/x86/amx_exec.sh | 87 +++++++++++++++++++++++++
 3 files changed, 138 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/x86/amx_exec.sh

diff --git a/tools/testing/selftests/x86/Makefile b/tools/testing/selftests/x86/Makefile
index 7565d2cf6a3c9..43619ba2dec13 100644
--- a/tools/testing/selftests/x86/Makefile
+++ b/tools/testing/selftests/x86/Makefile
@@ -71,6 +71,7 @@ endif
 ifeq ($(CAN_BUILD_X86_64),1)
 all: all_64
 TEST_PROGS += $(BINARIES_64)
+TEST_PROGS += amx_exec.sh
 EXTRA_CFLAGS += -DCAN_BUILD_64
 $(foreach t,$(TARGETS_C_64BIT_ALL),$(eval $(call gen-target-rule-64,$(t))))
 endif
diff --git a/tools/testing/selftests/x86/amx.c b/tools/testing/selftests/x86/amx.c
index 40769c16de1bb..0e618825c23ab 100644
--- a/tools/testing/selftests/x86/amx.c
+++ b/tools/testing/selftests/x86/amx.c
@@ -5,6 +5,7 @@
 #include <errno.h>
 #include <setjmp.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
 #include <unistd.h>
@@ -30,6 +31,10 @@
 #define XFEATURE_MASK_XTILEDATA	(1 << XFEATURE_XTILEDATA)
 #define XFEATURE_MASK_XTILE	(XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA)
 
+#define EXEC_TEST_ARG		"--exec-test"
+#define EXEC_TEST_CHILD_ARG	"--exec-test-child"
+#define EXEC_TEST_XSAVESZ_ARG	"--exec-test-xsave-size"
+
 struct xstate_info xtiledata;
 
 /* The helpers for managing XSAVE buffer and tile states: */
@@ -478,11 +483,48 @@ static void test_fork(void)
 	_exit(0);
 }
 
-int main(void)
+static void test_exec(char **argv)
+{
+	int i, times = atoi(argv[2]);
+
+	for (i = 0; i < times; i++) {
+		pid_t child;
+		int status;
+
+		child = fork();
+		if (child < 0)
+			fatal_error("fork");
+		if (!child) {
+			char *args[] = {argv[0], EXEC_TEST_CHILD_ARG, NULL};
+
+			if (!load_rand_tiledata(stashed_xsave))
+				fatal_error("failed to load tiledata");
+
+			execv(args[0], args);
+			fatal_error("exec");
+		}
+
+		if (waitpid(child, &status, 0) != child)
+			fatal_error("waitpid");
+
+		if (!WIFEXITED(status) || WEXITSTATUS(status))
+			fatal_error("exec test child");
+	}
+}
+
+int main(int argc, char **argv)
 {
 	unsigned long features;
 	long rc;
 
+	if (argc == 2 && !strcmp(argv[1], EXEC_TEST_CHILD_ARG))
+		return 0;
+
+	if (argc == 2 && !strcmp(argv[1], EXEC_TEST_XSAVESZ_ARG)) {
+		printf("%u\n", get_xbuf_size());
+		return 0;
+	}
+
 	rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_SUPP, &features);
 	if (rc || (features & XFEATURE_MASK_XTILE) != XFEATURE_MASK_XTILE) {
 		ksft_print_msg("no AMX support\n");
@@ -498,6 +540,12 @@ int main(void)
 	init_stashed_xsave();
 	sethandler(SIGILL, handle_noperm, 0);
 
+	if (argc == 3 && !strcmp(argv[1], EXEC_TEST_ARG)) {
+		req_xtiledata_perm();
+		test_exec(argv);
+		goto out;
+	}
+
 	test_dynamic_state();
 
 	/* Request permission for the following tests */
@@ -511,6 +559,7 @@ int main(void)
 	 */
 	test_xstate(XFEATURE_XTILEDATA);
 
+out:
 	clearhandler(SIGILL);
 	free_stashed_xsave();
 
diff --git a/tools/testing/selftests/x86/amx_exec.sh b/tools/testing/selftests/x86/amx_exec.sh
new file mode 100755
index 0000000000000..2168460d6c0b9
--- /dev/null
+++ b/tools/testing/selftests/x86/amx_exec.sh
@@ -0,0 +1,87 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+KSFT_PASS=0
+KSFT_FAIL=1
+KSFT_SKIP=4
+
+NUM_ITERATION=10
+AMX_TEST="./amx_64"
+
+if [[ ! -x "$AMX_TEST" ]]; then
+    echo "# SKIP: $AMX_TEST does not exist or is not executable."
+    exit $KSFT_SKIP
+fi
+
+# While the kernel may have the compacted size, but it has additional metadata
+XSAVE_SIZE=$("$AMX_TEST" --exec-test-xsave-size)
+
+if [ "$EUID" -ne 0 ]; then
+    echo "# SKIP: Please run as root (sudo) to access /proc/vmallocinfo."
+    exit $KSFT_SKIP
+fi
+
+TMP_BEFORE=$(mktemp) || exit $KSFT_FAIL
+TMP_AFTER=$(mktemp) || {
+    rm -f "$TMP_BEFORE"
+    exit $KSFT_FAIL
+}
+
+parse_vmalloc() {
+    # Typical format (e.g.): 0xffffc90000000000-0xffffc90000002000    8192 pcpu_alloc_alloc+0x3b/0x240 vmalloc
+    # Aggregate the size (column 2) grouped by the caller function (column 3)
+    awk '
+    {
+        # Remove offset details like +0x3b/0x240 to group by base function name
+        split($3, caller, "+")
+        # Keep track of caller type if present (e.g. vmalloc, ioremap)
+        type = $4 ? " (" $4 ")" : ""
+        key = caller[1] type
+        sum[key] += $2
+    }
+    END {
+        for (k in sum) {
+            print k "," sum[k]
+        }
+    }' /proc/vmallocinfo
+}
+
+echo -e "[RUN]\tCheck memory leak on exec()"
+parse_vmalloc > "$TMP_BEFORE"
+"$AMX_TEST" --exec-test "$NUM_ITERATION" || {
+    # Let $AMX_TEST figure out AMX capabilities
+    rc=$?
+    echo "# error on running $AMX_TEST (exit code: $rc)"
+    rm -f "$TMP_BEFORE" "$TMP_AFTER"
+    exit "$rc"
+}
+parse_vmalloc > "$TMP_AFTER"
+
+# Join both files on the caller name and compute the differences
+LEAKED=$(awk -v iterations=$NUM_ITERATION -v memory_increase=$XSAVE_SIZE -F, '
+NR == FNR {
+    before[$1] = $2
+    next
+}
+{
+    after[$1] = $2
+    diff = $2 - before[$1]
+    # Search for relevant symbols as many to relax inline sensitivities
+    if ($1 ~ /(fpstate_realloc|xfd_enable_feature)/ && diff > memory_increase * iterations) {
+        leftover=1
+    }
+    # Mark as processed
+    delete before[$1]
+} END {
+    print leftover+0
+}' "$TMP_BEFORE" "$TMP_AFTER")
+
+rm -f "$TMP_BEFORE" "$TMP_AFTER"
+
+if [ "$LEAKED" == "1" ]; then
+    echo -e "[FAIL]\tMemory leak"
+    exit $KSFT_FAIL
+else
+    echo -e "[OK]\tNo memory leak"
+    exit $KSFT_PASS
+fi
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 6/6] selftests/x86/amx: Test fpstate memory cleanup on exec()
  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
  0 siblings, 0 replies; 8+ messages in thread
From: Chang S. Bae @ 2026-11-30 21:33 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, tglx, mingo, bp, dave.hansen, weiguixiong, omermetekaya0,
	Shuah Khan, linux-kselftest

On 11/30/2026 12:51 PM, Chang S. Bae wrote:
> 
> Cc: kselftest@vger.kernel.org
Sorry for the typo -- adding linux-kselftest@vger.kernel.org.

The original posting is this:
  
https://lore.kernel.org/lkml/20261130205143.1258369-7-chang.seok.bae@intel.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-30 21:33 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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

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®