x86/fpu: Fix dynamic fpstate leak on exec()
Subsystems (3)
v2 cover x86/fpu: Fix dynamic fpstate leak on exec()
An exec() after using an XFD-controlled xfeature resets fpu->fpstate to its embedded storage without freeing the dynamically allocated state. Fix the leak in fpstate_reset() and add an AMX regression selftest.
The fix preserves the old pointer, installs and initializes the embedded fpstate, and then frees the detached allocation. Since fpstate_reset() now owns cleanup, fpu_clone() initializes dst_fpu->fpstate to NULL before invoking it.
The selftest performs ten XTILEDATA request, XRSTOR and self-exec cycles in the same task and checks the associated /proc/vmallocinfo entries. It fails with 10 leaked allocations on the unfixed kernel and passes with zero on the fixed kernel.
Changes since v1:
- Rename the fix and describe the memory leak explicitly.
- Move cleanup into fpstate_reset().
- Detach the old fpstate before freeing it.
- Initialize dst_fpu->fpstate to NULL before resetting it.
- Add the requested AMX regression selftest as a separate patch.
v1: https://lore.kernel.org/r/[email protected]
Guixiong Wei (2):
x86/fpu: Fix memory leak with dynamic fpstate and exec() selftests/x86/amx: Test dynamic fpstate cleanup across exec()
arch/x86/include/asm/fpu/api.h | 6 +- arch/x86/kernel/fpu/core.c | 5 ++ arch/x86/kernel/fpu/xstate.c | 10 +-- arch/x86/kernel/process.c | 2 +- tools/testing/selftests/x86/amx.c | 144 +++++++++++++++++++++++++++++- 5 files changed, 156 insertions(+), 11 deletions(-)
base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 1/2 x86/fpu: Fix memory leak with dynamic fpstate and exec()
The fpstate embedded in struct fpu only accommodates the default xfeatures. After a task obtains permission for an XFD-controlled xfeature, its first use raises #NM. The handler calls __xfd_enable_feature(), which uses fpstate_realloc() to install a larger fpstate allocated with vzalloc().
On exec(), fpu_flush_thread() calls fpstate_reset(), which overwrites fpu->fpstate with the embedded fpstate. This leaks the dynamic allocation because arch_release_task_struct() cannot free it when the task exits.
Make fpstate_reset() own the complete lifetime transition. Preserve the old pointer, install and initialize the embedded fpstate, and then free the old allocation. This detaches the allocation from fpu->fpstate before freeing it and makes future reset callers release dynamic state automatically.
Since fpstate_reset() now releases the old state, initialize dst_fpu->fpstate to NULL before invoking it from fpu_clone().
Make fpstate_free() operate on the fpstate pointer itself so it can free the detached allocation. Use it for the existing reallocation and task release paths as well.
On an AMX-capable KVM guest, 1000 iterations left 1000 dynamic fpstate allocations on an unfixed kernel and none with this change.
--- arch/x86/include/asm/fpu/api.h | 6 +++--- arch/x86/kernel/fpu/core.c | 5 +++++ arch/x86/kernel/fpu/xstate.c | 10 ++++------ arch/x86/kernel/process.c | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-)
arch/x86/include/asm/fpu/api.h +3 -3
@@ -123,11 +123,11 @@ extern void fpu__resume_cpu(void);DECLARE_PER_CPU (bool, kernel_fpu_allowed); DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx); -/* Process cleanup */ +/* Dynamic fpstate cleanup */ #ifdef CONFIG_X86_64 -extern void fpstate_free (struct fpu *fpu); +extern void fpstate_free (struct fpstate *fpstate);#else -static inline void fpstate_free (struct fpu *fpu) { } +static inline void fpstate_free (struct fpstate *fpstate) { }#endif /* fpstate-related functions which are exported to KVM */
arch/x86/kernel/fpu/core.c +5 -0
@@ -594,6 +594,8 @@ static void __fpstate_reset(struct fpstate *fpstate)void fpstate_reset (struct fpu *fpu) { +struct fpstate *oldfpstate = fpu->fpstate; +/* Set the fpstate pointer to the default fpstate */ fpu->fpstate = &fpu->__fpstate;__fpstate_reset (fpu->fpstate);
@@ -610,6 +612,8 @@ void fpstate_reset(struct fpu *fpu)* guest FPUs, which allows for common guest and userspace ABI. */ fpu->guest_perm.__user_state_size = fpu_user_cfg.default_size; + +fpstate_free (oldfpstate); }static inline void fpu_inherit_perms (struct fpu *dst_fpu)
@@ -670,6 +674,7 @@ int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal,/* The new task's FPU state cannot be valid in the hardware. */ dst_fpu->last_cpu =-1 ; + dst_fpu->fpstate =NULL ;fpstate_reset (dst_fpu);if (!cpu_feature_enabled (X86_FEATURE_FPU ))
arch/x86/kernel/fpu/xstate.c +4 -6
@@ -1556,10 +1556,10 @@ 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); +if (fpstate && fpstate->is_valloc) +vfree (fpstate); }/**
@@ -1640,9 +1640,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 ; }
arch/x86/kernel/process.c +1 -1
@@ -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)); +fpstate_free (x86_task_fpu (tsk)->fpstate); }#endif
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v2 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
Quoted 10 lines · Guixiong Wei
The fpstate embedded in struct fpu only accommodates the default xfeatures. After a task obtains permission for an XFD-controlled xfeature, its first use raises #NM. The handler calls
__xfd_enable_feature(), which usesfpstate_realloc()to install a larger fpstate allocated withvzalloc().On
exec(),fpu_flush_thread()callsfpstate_reset(), which overwritesfpu->fpstatewith the embedded fpstate. This leaks the dynamic allocation becausearch_release_task_struct()cannot free it when the task exits.
Hmm, I'd start with Dave's wording here, or tweak it slightly.
Quoted 5 lines
Make
fpstate_reset()own the complete lifetime transition. Preserve the old pointer, install and initialize the embedded fpstate, and then free the old allocation. This detaches the allocation fromfpu->fpstatebefore freeing it and makes future reset callers release dynamic state automatically.
I'm not sure what the intended argument is here. Does this mean the old allocation cannot or should not be freed at the beginning of fpstate_reset()? If so, please explain functional issues. Otherwise, I'd simply follow Dave earlier suggestion.
Quoted 2 lines
On an AMX-capable KVM guest, 1000 iterations left 1000 dynamic fpstate allocations on an unfixed kernel and none with this change.
Since the selftest can reproduce the issue without KVM, I'm not sure why
the KVM guest is relevant here. In any case, the entire sentence could be dropped if the problem description is clear enough.
arch/x86/kernel/fpu/core.c static void __fpstate_reset(struct fpstate *fpstate) · line 598
Quoted 5 lines
void fpstate_reset(struct fpu *fpu) {arch/x86/kernel/fpu/core.c +2
-0+struct fpstate *oldfpstate = fpu->fpstate; +
Hmm, again, why not just call fpstate_free(fpu->fpstate) here?
I see Dave also noted this comment along the lines:
/* Free dynamic fpstate (if any) before resetting pointer: */
Quoted 13 lines
/* Set the fpstate pointer to the default fpstate */ fpu->fpstate = &fpu->__fpstate; __fpstate_reset(fpu->fpstate);arch/x86/kernel/fpu/core.c +2
-0@@ -610,6 +612,8 @@ void fpstate_reset(struct fpu *fpu)* guest FPUs, which allows for common guest and userspace ABI. */ fpu->guest_perm.__user_state_size = fpu_user_cfg.default_size; + +fpstate_free (oldfpstate); }
arch/x86/kernel/fpu/core.c int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal, · line 678
Quoted 5 lines
arch/x86/kernel/fpu/core.c +1
-0@@ -670,6 +674,7 @@ int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal,/* The new task's FPU state cannot be valid in the hardware. */ dst_fpu->last_cpu =-1 ; + dst_fpu->fpstate =NULL ;fpstate_reset (dst_fpu);
Dave also suggested adding a comment here:
/* Break the link to the other FPU's fpstate: */
dst_fpu->fpstate = NULL;Quoted 8 lines
arch/x86/kernel/fpu/core.c +0
-0if (!cpu_feature_enabled (X86_FEATURE_FPU ))arch/x86/kernel/fpu/xstate.c +0
-0@@ -1556,10 +1556,10 @@ static int __init xfd_update_static_branch(void) }
arch/x86/kernel/fpu/xstate.c static int __init xfd_update_static_branch(void) · line 1563
Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +3
-3arch_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); +if (fpstate && fpstate->is_valloc) +vfree (fpstate); }
Shouldn't is_valloc be cleared here, or when fpstate_reset() replaces fpu->fpstate with the embedded fpstate?
Thanks,
Chang
Re: [PATCH v2 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
Quoted 12 lines · Chang S. Bae
Quoted 9 lines · Guixiong Wei
arch/x86/kernel/fpu/xstate.c +1
-1-void fpstate_free (struct fpu *fpu) +void fpstate_free (struct fpstate *fpstate){
- if (fpu->fpstate&&fpu->fpstate!=&fpu->__fpstate) -vfree(fpu->fpstate);
+ if (fpstate &&fpstate->is_valloc)
+ vfree(fpstate);
}Shouldn't
is_vallocbe cleared here, or whenfpstate_reset()replacesfpu->fpstatewith the embedded fpstate?
No, is_valloc is pat of the vmalloc'ed memory itself.
That said, removing the original check does not seem reassuring either. Maybe, another option would be adding an internal helper:
void __fpstate_reset(struct fpstate *fpstate)
{
if (fpstate && fpstate->is_valloc)
vfree(fpstate);
}Then, refactor fpstate_realloc() and replace the vfree() in fpstate_free() with it, keeping the fpstate_free() prototype unchanged.
Thanks,
Chang
Re: [PATCH v2 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
Quoted 20 lines
From: "Chang S. Bae"<[email protected]>
Date: Tue, Oct 20, 2026, 01:53
Subject: Re: [PATCH v2 1/2] x86/fpu: Fix memory leak with dynamic fpstate andexec()
To: "Guixiong Wei"<[email protected]>, <[email protected]>
Cc: "Thomas Gleixner"<[email protected]>, "Ingo Molnar"<[email protected]>, "Borislav Petkov"<[email protected]>, "Dave Hansen"<[email protected]>, "H . Peter Anvin"<[email protected]>, <[email protected]>, <[email protected]>Quoted 13 lines · Chang S. Bae
Quoted 9 lines · Guixiong Wei
arch/x86/kernel/fpu/xstate.c +1
-1-void fpstate_free (struct fpu *fpu) +void fpstate_free (struct fpstate *fpstate){
- if (fpu->fpstate&&fpu->fpstate!=&fpu->__fpstate) -vfree(fpu->fpstate);
+ if (fpstate &&fpstate->is_valloc)
+ vfree(fpstate);
}
Shouldn'tis_vallocbe cleared here, or whenfpstate_reset()replacesfpu->fpstatewith the embedded fpstate?
Quoted 15 lines
No,is_vallocis pat of the vmalloc'ed memory itself.
That said, removing the original check does not seem reassuring either. Maybe, another option would be adding an internal helper:
void__fpstate_reset(struct fpstate *fpstate)
{
if (fpstate &&fpstate->is_valloc)
vfree(fpstate);
}
Then, refactorfpstate_realloc()and replace thevfree()infpstate_free()with it, keeping thefpstate_free()prototype unchanged.
Good suggestion, adopted in v3 with one small naming divergence worth flagging: core.c already has a static __fpstate_reset(struct fpstate *) that initializes fpstate fields, so I named the new helper __fpstate_free() to preserve the "reset vs. free" split and avoid shadowing. Final shape:
static void __fpstate_free(struct fpstate *fpstate) {
if (fpstate && fpstate->is_valloc) vfree(fpstate);
}
void fpstate_free(struct fpu *fpu)
{
__fpstate_free(fpu->fpstate);
}
fpstate_realloc() ends with __fpstate_free(curfps) on its detached pointer.
Quoted 3 lines
Thanks,
Chang
Thanks,
Guixiong
Re: [PATCH v2 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
Quoted 5 lines · Guixiong Wei
void
fpstate_free(struct fpu *fpu)
{
__fpstate_free(fpu->fpstate);
}
Hmm, just curious why the original check has to be removed. I thought this sanity check from the struct fpu context was reasonable, no?
void fpstate_free(struct fpu *fpu)
{
if (fpu->fpstate && fpu->fpstate != &fpu->__fpstate)
__fpstate_free(fpu->fpstate);
}Thanks,
Chang
Re: [PATCH v2 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
Quoted 12 lines
From: "Chang S. Bae"<[email protected]>
Date: Wed, Oct 21, 2026, 02:21
Subject: Re: [PATCH v2 1/2] x86/fpu: Fix memory leak with dynamic fpstate andexec()
To: "Guixiong Wei"<[email protected]>
Cc: <[email protected]>, "Thomas Gleixner"<[email protected]>, "Ingo Molnar"<[email protected]>, "Borislav Petkov"<[email protected]>, "Dave Hansen"<[email protected]>, "H . Peter Anvin"<[email protected]>, <[email protected]>, <[email protected]>Quoted 5 lines · Guixiong Wei
voidfpstate_free(struct fpu *fpu)
{
__fpstate_free(fpu->fpstate);
}
Quoted 13 lines
Hmm, just curious why the original check has to be removed. I thought this sanity check from the struct fpu context was reasonable, no?
voidfpstate_free(struct fpu *fpu)
{
if (fpu->fpstate&&fpu->fpstate!=&fpu->__fpstate)__fpstate_free(fpu->fpstate);
}
Thanks,
Chang
Right, will restore it in v3.
With that check back, __fpstate_free() ends up with fpstate_realloc() as its only real caller, so I'd prefer to drop the helper and keep both free sites inline — same shape as before v2:
void fpstate_free(struct fpu *fpu)
{
if (fpu->fpstate && fpu->fpstate != &fpu->__fpstate) vfree(fpu->fpstate);
}
/* fpstate_realloc(), unchanged: */
if (curfps && curfps->is_valloc)
vfree(curfps);
The fix then collapses to two changes in core.c: free the dynamic fpstate at the start of fpstate_reset(), and clear dst_fpu->fpstate to NULL in fpu_clone() before the reset. xstate.c, api.h and process.c stay untouched.
OK with you?
Thanks,
Guixiong
Re: [PATCH v2 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
Quoted 7 lines · Guixiong Wei
The fix then collapses to two changes in
core.c: free the dynamic fpstate at the start offpstate_reset(), and cleardst_fpu->fpstateto NULL infpu_clone()before the reset.xstate.c,api.handprocess.cstay untouched.OK with you?
Yea, I think that could be an option.
Thanks,
Chang
Re: [PATCH v2 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
In reply to Chang S. BaeQuoted 17 lines
From: "Chang S. Bae"<[email protected]>
Date: Thu, Oct 15, 2026, 08:19
Subject: Re: [PATCH v2 1/2] x86/fpu: Fix memory leak with dynamic fpstate andexec()
To: "Guixiong Wei"<[email protected]>, <[email protected]>
Cc: "Thomas Gleixner"<[email protected]>, "Ingo Molnar"<[email protected]>, "Borislav Petkov"<[email protected]>, "Dave Hansen"<[email protected]>, "H . Peter Anvin"<[email protected]>, <[email protected]>, <[email protected]>Quoted 10 lines · Guixiong Wei
The fpstate embedded in struct fpu only accommodates the default xfeatures. After a task obtains permission for an XFD-controlled xfeature, its first use raises #NM. The handler calls
__xfd_enable_feature(), which usesfpstate_realloc()to install a larger fpstate allocated withvzalloc().
Onexec(),fpu_flush_thread()callsfpstate_reset(), which overwritesfpu->fpstatewith the embedded fpstate. This leaks the dynamic allocation becausearch_release_task_struct()cannot free it when the task exits.
Quoted 2 lines
Hmm, I'd start with Dave's wording here, or tweak it slightly.
Will do. v3 will picks up Dave's phrasing for the opening paragraphs describing how the dynamic fpstate is allocated and leaked.
Quoted 11 lines
Quoted 5 lines
Make
fpstate_reset()own the complete lifetime transition. Preserve the old pointer, install and initialize the embedded fpstate, and then free the old allocation. This detaches the allocation fromfpu->fpstatebefore freeing it and makes future reset callers release dynamic state automatically.
I'm not sure what the intended argument is here. Does this mean the old allocation cannot or should not be freed at the beginning offpstate_reset()? If so, please explain functional issues. Otherwise, I'd simply follow Dave earlier suggestion.
Agreed, no functional reason. Will free at the beginning of fpstate_reset() in v3.
Quoted 8 lines
Quoted 2 lines
On an AMX-capable KVM guest, 1000 iterations left 1000 dynamic fpstate allocations on an unfixed kernel and none with this change.
Since the selftest can reproduce the issue without KVM, I'm not sure why the KVM guest is relevant here. In any case, the entire sentence could be dropped if the problem description is clear enough.
Will do. Dropped in v3;
Quoted 11 lines
Quoted 5 lines
voidfpstate_reset(struct fpu *fpu)
{
+ struct fpstate *oldfpstate =fpu->fpstate; +
Hmm, again, why not just callfpstate_free(fpu->fpstate)here?
I see Dave also noted this comment along the lines:
/* Free dynamic fpstate (if any) before resetting pointer: */
Will do.
Quoted 12 lines
arch/x86/kernel/fpu/core.c void fpstate_reset(struct fpu *fpu) · line 617
Quoted 10 lines
/* Set the fpstate pointer to the default fpstate */
fpu->fpstate=&fpu->__fpstate;__fpstate_reset(fpu->fpstate);arch/x86/kernel/fpu/core.c +2
-0@@ -610,6 +612,8 @@ void fpstate_reset(struct fpu *fpu)* guest FPUs, which allows for common guest and userspace ABI. */ fpu->guest_perm.__user_state_size = fpu_user_cfg.default_size; + + fpstate_free(oldfpstate); }
Quoted 12 lines
Quoted 6 lines
arch/x86/kernel/fpu/core.c +0
-0@@ -670,6 +674,7 @@ int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal, /* The new task's FPU state cannot be valid in the hardware. */ dst_fpu->last_cpu = -1;
+ dst_fpu->fpstate = NULL;
fpstate_reset(dst_fpu);
Dave also suggested adding a comment here:
/* Break the link to the other FPU's fpstate: */dst_fpu->fpstate= NULL;
Will do.
Quoted 14 lines
Quoted 14 lines
if (!cpu_feature_enabled(X86_FEATURE_FPU))arch/x86/kernel/fpu/xstate.c +0
-0@@ -1556,10 +1556,10 @@ static int __init xfd_update_static_branch(void) } arch_initcall(xfd_update_static_branch)arch/x86/kernel/fpu/xstate.c +1
-1-void fpstate_free (struct fpu *fpu) +void fpstate_free (struct fpstate *fpstate){
- if (fpu->fpstate&&fpu->fpstate!=&fpu->__fpstate)
Quoted 8 lines
Quoted 4 lines
- vfree(fpu->fpstate);
+ if (fpstate &&fpstate->is_valloc)
+ vfree(fpstate);
}
Shouldn'tis_vallocbe cleared here, or whenfpstate_reset()replacesfpu->fpstatewith the embedded fpstate?
No need: is_valloc lives inside the vmalloc'ed buffer itself, so vfree() releases the flag together with the storage.
Quoted 3 lines
Thanks,
Chang
PATCH v2 2/2 selftests/x86/amx: Test dynamic fpstate cleanup across exec()
In reply to Guixiong WeiAn AMX task gets a dynamically allocated fpstate after requesting XTILEDATA permission and first loading tile data. Test that exec() releases this allocation instead of making it unreachable.
Run ten request, XRSTOR and self-exec cycles in the same task. Count the matching allocations in /proc/vmallocinfo before the test, while the first allocation is live and after the task exits. Skip the test when /proc/vmallocinfo is unavailable or when concurrent global vmalloc changes prevent an isolated measurement.
The test observed 10 leaked allocations on an unfixed kernel and zero with the fix applied.
--- tools/testing/selftests/x86/amx.c | 144 +++++++++++++++++++++++++++++- 1 file changed, 143 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/amx.c +143 -1
@@ -3,8 +3,10 @@#define _GNU_SOURCE#include <err.h> #include <errno.h> +#include <limits.h> #include <setjmp.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <stdbool.h> #include <unistd.h>
@@ -30,6 +32,9 @@#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_ITERS 10 +struct xstate_info xtiledata;/* The helpers for managing XSAVE buffer and tile states: */
@@ -478,11 +483,144 @@ static void test_fork(void)_exit (0 ); } -int main (void ) +static int count_dynamic_fpstates (void ) +{ +char *line =NULL ; +size_t line_size =0 ; +FILE *fp; +int count =0 ; + + fp =fopen ("/proc/vmallocinfo" ,"r" ); +if (!fp) +return -errno; + +while (getline (&line, &line_size, fp) >=0 ) { +if (strstr (line,"__xfd_enable_feature" ) || +strstr (line,"fpstate_realloc" )) + count++; + } + +if (ferror (fp)) + count = -EIO ; + +free (line); +fclose (fp); +return count; +} + +static int parse_exec_arg (const char *arg) +{ +char *end; +long value; + + errno =0 ; + value =strtol (arg, &end,10 ); +if (errno || *end || value <0 || value >INT_MAX ) +fatal_error ("invalid exec test argument: %s" , arg); + +return value; +} + +static int run_exec_test (int iterations,int baseline) +{ +char iterations_arg[16 ]; +char baseline_arg[16 ]; +int allocated; + +if (!iterations) +return 0 ; + +req_xtiledata_perm (); +if (!load_rand_tiledata (stashed_xsave)) +fatal_error ("failed to load tiledata before exec()" ); + +if (iterations ==EXEC_TEST_ITERS ) { + allocated =count_dynamic_fpstates (); +if (allocated != baseline +1 ) +return KSFT_SKIP ; + } + +snprintf (iterations_arg,sizeof (iterations_arg),"%d" , iterations -1 ); +snprintf (baseline_arg,sizeof (baseline_arg),"%d" , baseline); +execl ("/proc/self/exe" ,"amx" ,EXEC_TEST_ARG , iterations_arg, + baseline_arg,NULL ); +fatal_error ("exec" ); +} + +static void test_exec (void ) +{ +char iterations_arg[16 ]; +char baseline_arg[16 ]; +int before, after, delta; +pid_t child; +int status; + +printf ("[RUN]\tCheck dynamic fpstate cleanup across exec().\n" ); + + before =count_dynamic_fpstates (); +if (before <0 ) { +printf ("[SKIP]\tCannot read /proc/vmallocinfo: %s\n" , +strerror (-before)); +return ; + } + + child =fork (); +if (child <0 ) +fatal_error ("fork" ); +if (!child) { +snprintf (iterations_arg,sizeof (iterations_arg),"%d" , +EXEC_TEST_ITERS ); +snprintf (baseline_arg,sizeof (baseline_arg),"%d" , before); +execl ("/proc/self/exe" ,"amx" ,EXEC_TEST_ARG , iterations_arg, + baseline_arg,NULL ); +fatal_error ("exec" ); + } + +if (waitpid (child, &status,0 ) != child) +fatal_error ("waitpid" ); +if (WIFEXITED (status) &&WEXITSTATUS (status) ==KSFT_SKIP ) { +printf ("[SKIP]\tDynamic fpstate allocation was not isolated.\n" ); +return ; + } +if (!WIFEXITED (status) ||WEXITSTATUS (status)) +fatal_error ("exec test child" ); + + after =count_dynamic_fpstates (); +if (after <0 ) { +printf ("[SKIP]\tCannot read /proc/vmallocinfo after exec(): %s\n" , +strerror (-after)); +return ; + } + + delta = after - before; +if (!delta) { +printf ("[OK]\tDynamic fpstate allocations were freed across exec().\n" ); +return ; + } + +if (delta ==EXEC_TEST_ITERS ) +errx (1 ,"[FAIL]\texec() leaked %d dynamic fpstate allocations" , + delta); + +printf ("[SKIP]\tDynamic fpstate vmalloc usage changed concurrently.\n" ); +} + +int main (int argc,char **argv) {unsigned long features; +int iterations =0 ; +int baseline =0 ; +bool exec_test;long rc; + exec_test = argc ==4 && !strcmp (argv[1 ],EXEC_TEST_ARG ); +if (exec_test) { + iterations =parse_exec_arg (argv[2 ]); + baseline =parse_exec_arg (argv[3 ]); +if (!iterations) +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 +636,10 @@ int main(void)init_stashed_xsave ();sethandler (SIGILL , handle_noperm,0 ); +if (exec_test) +return run_exec_test (iterations, baseline); + +test_exec ();test_dynamic_state ();/* Request permission for the following tests */
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v2 2/2] selftests/x86/amx: Test dynamic fpstate cleanup across exec()
tools/testing/selftests/x86/amx.c static void test_fork(void) · line 495
Quoted 12 lines · Guixiong Wei
tools/testing/selftests/x86/amx.c +10
-1-int main (void ) +static int count_dynamic_fpstates (void ) +{ +char *line =NULL ; +size_t line_size =0 ; +FILE *fp; +int count =0 ; + + fp =fopen ("/proc/vmallocinfo" ,"r" ); +if (!fp) +return -errno;
The call site doesn't interprete the error code. Instead, you can simply call fatal_error() here and below on an IO error.
Quoted 33 lines
tools/testing/selftests/x86/amx.c +32
-0+ +while (getline (&line, &line_size, fp) >=0 ) { +if (strstr (line,"__xfd_enable_feature" ) || +strstr (line,"fpstate_realloc" )) + count++; + } + +if (ferror (fp)) + count = -EIO ; + +free (line); +fclose (fp); +return count; +} +static void test_exec (void ) +{ +char iterations_arg[16 ]; +char baseline_arg[16 ]; +int before, after, delta; +pid_t child; +int status; + +printf ("[RUN]\tCheck dynamic fpstate cleanup across exec().\n" ); + + before =count_dynamic_fpstates (); +if (before <0 ) { +printf ("[SKIP]\tCannot read /proc/vmallocinfo: %s\n" , +strerror (-before)); +return ; + } + + child =fork ();
tools/testing/selftests/x86/amx.c static void test_fork(void) · line 577
Quoted 10 lines
tools/testing/selftests/x86/amx.c +10
-0+if (child <0 ) +fatal_error ("fork" ); +if (!child) { +snprintf (iterations_arg,sizeof (iterations_arg),"%d" , +EXEC_TEST_ITERS ); +snprintf (baseline_arg,sizeof (baseline_arg),"%d" , before); +execl ("/proc/self/exe" ,"amx" ,EXEC_TEST_ARG , iterations_arg, + baseline_arg,NULL ); +fatal_error ("exec" ); + }
This nested exec() with passing numbers via arguments looks more complicate than necessary.
Instead, you could put the fork()/exec() sequence in a loop. Before each exec(), call load_rand_tiledata(), and adjust main() to return immediately from the exec test. I found this also reproduces the increase in /proc/vmallocinfo.
Quoted 16 lines
tools/testing/selftests/x86/amx.c +16
-0+ +if (waitpid (child, &status,0 ) != child) +fatal_error ("waitpid" ); +if (WIFEXITED (status) &&WEXITSTATUS (status) ==KSFT_SKIP ) { +printf ("[SKIP]\tDynamic fpstate allocation was not isolated.\n" ); +return ; + } +if (!WIFEXITED (status) ||WEXITSTATUS (status)) +fatal_error ("exec test child" ); + + after =count_dynamic_fpstates (); +if (after <0 ) { +printf ("[SKIP]\tCannot read /proc/vmallocinfo after exec(): %s\n" , +strerror (-after)); +return ; + }
tools/testing/selftests/x86/amx.c static void test_fork(void) · line 603
Quoted 10 lines
tools/testing/selftests/x86/amx.c +10
-0+ + delta = after - before; +if (!delta) { +printf ("[OK]\tDynamic fpstate allocations were freed across exec().\n" ); +return ; + } + +if (delta ==EXEC_TEST_ITERS ) +errx (1 ,"[FAIL]\texec() leaked %d dynamic fpstate allocations" , + delta);
errx() terminates the entire test, preventing subsequent tests from running. Unless this failure makes it unsafe or meaningless to continue, why not report the failure with printf() and let the remaining tests run?
tools/testing/selftests/x86/amx.c static void test_fork(void) · line 606
Quoted 3 lines
tools/testing/selftests/x86/amx.c +3
-0+ +printf ("[SKIP]\tDynamic fpstate vmalloc usage changed concurrently.\n" ); +}
Thanks,
Chang
Re: [PATCH v2 2/2] selftests/x86/amx: Test dynamic fpstate cleanup across exec()
Quoted 7 lines
From: "Chang S. Bae"<[email protected]>
Date: Thu, Oct 15, 2026, 08:19
Subject: Re: [PATCH v2 2/2] selftests/x86/amx: Test dynamic fpstate cleanup acrossexec()
To: "Guixiong Wei"<[email protected]>, <[email protected]>
Cc: "Thomas Gleixner"<[email protected]>, "Ingo Molnar"<[email protected]>, "Borislav Petkov"<[email protected]>, "Dave Hansen"<[email protected]>, "H . Peter Anvin"<[email protected]>, "Shuah Khan"<[email protected]>, <[email protected]>, <[email protected]> On 10/7/2026 10:59 PM, Guixiong Wei wrote:
Quoted 16 lines
tools/testing/selftests/x86/amx.c static void test_fork(void) · line 495
Quoted 12 lines
tools/testing/selftests/x86/amx.c +10
-1-int main (void ) +static int count_dynamic_fpstates (void ) +{ + char *line = NULL; + size_t line_size = 0; + FILE *fp; + int count = 0; + + fp = fopen("/proc/vmallocinfo", "r"); + if (!fp) + return -errno;
The call site doesn't interprete the error code. Instead, you can simply callfatal_error()here and below on an IO error.
Will do. v3 drops the -errno plumbing and calls fatal_error() on fopen()/getline()/ferror() failures
Quoted 44 lines
Quoted 4 lines
tools/testing/selftests/x86/amx.c +4
-0+ + while (getline(&line, &line_size, fp) >= 0) { + if (strstr(line, "__xfd_enable_feature") || + strstr(line, "fpstate_realloc"))tools/testing/selftests/x86/amx.c static void test_fork(void) · line 509
Quoted 10 lines
tools/testing/selftests/x86/amx.c +10
-0+ count++; + } + + if (ferror(fp)) + count = -EIO; + + free(line); + fclose(fp); + return count; +}Quoted 18 lines
tools/testing/selftests/x86/amx.c +18
-0+static void test_exec (void ) +{ + char iterations_arg[16]; + char baseline_arg[16]; + int before, after, delta; + pid_t child; + int status; + + printf("[RUN]\tCheck dynamic fpstate cleanup across exec().\n"); + + before = count_dynamic_fpstates(); + if (before < 0) { + printf("[SKIP]\tCannot read /proc/vmallocinfo: %s\n", + strerror(-before)); + return; + } + + child = fork();tools/testing/selftests/x86/amx.c static void test_fork(void) · line 577
Quoted 10 lines
tools/testing/selftests/x86/amx.c +10
-0+ if (child < 0) + fatal_error("fork"); + if (!child) { + snprintf(iterations_arg, sizeof(iterations_arg), "%d", + EXEC_TEST_ITERS); + snprintf(baseline_arg, sizeof(baseline_arg), "%d", before); + execl("/proc/self/exe", "amx", EXEC_TEST_ARG, iterations_arg, + baseline_arg, NULL); + fatal_error("exec"); + }
Quoted 8 lines
This nested
exec()with passing numbers via arguments looks more complicate than necessary.
Instead, you could put the fork()/exec() sequence in a loop. Before eachexec(), callload_rand_tiledata(), and adjustmain()to return immediately from the exec test. I found this also reproduces the increase in/proc/vmallocinfo.
Agreed, much simpler. v3 restructures test_exec() as:
for (i = 0; i < EXEC_TEST_ITERS; i++) { child = fork();
if (child < 0) fatal_error("fork");
if (!child) { req_xtiledata_perm(); load_rand_tiledata(&stashed_tiles); execl("/proc/self/exe", "amx", EXEC_TEST_ARG, NULL); fatal_error("exec");
}
if (waitpid(child, &status, 0) != child) fatal_error("waitpid");
...
}
main() short-circuits when argv[1] == EXEC_TEST_ARG and exits 0, so the exec'd child does nothing beyond returning. The baseline/final counts are taken by the parent around the loop. Confirmed on an AMX
host: unfixed kernel reports EXEC_TEST_ITERS leaked allocations, fixed kernel reports 0.
Quoted 23 lines
Quoted 23 lines
tools/testing/selftests/x86/amx.c +23
-0+ + if (waitpid(child, &status, 0) != child) + fatal_error("waitpid"); + if (WIFEXITED(status) && WEXITSTATUS(status) == KSFT_SKIP) { + printf("[SKIP]\tDynamic fpstate allocation was not isolated.\n"); + return; + } + if (!WIFEXITED(status) || WEXITSTATUS(status)) + fatal_error("exec test child"); + + after = count_dynamic_fpstates(); + if (after < 0) { + printf("[SKIP]\tCannot read /proc/vmallocinfo after exec(): %s\n", + strerror(-after)); + return; + } + + delta = after - before; + if (!delta) { + printf("[OK]\tDynamic fpstate allocations were freed across exec().\n"); + return; + } +
Quoted 8 lines
Quoted 3 lines
+ if (delta ==
EXEC_TEST_ITERS)
+ errx(1, "[FAIL]\texec() leaked %d dynamic fpstate allocations",
+ delta);
errx()terminates the entire test, preventing subsequent tests from running. Unless this failure makes it unsafe or meaningless to continue, why not report the failure withprintf()and let the remaining tests run?
Will do. v3 replaces the errx() with a printf("[FAIL]\t...") and returns from test_exec() so later subtests still execute. The failure is still visible in kselftest output.
Quoted 7 lines
tools/testing/selftests/x86/amx.c static void test_fork(void) · line 606
Quoted 3 lines
tools/testing/selftests/x86/amx.c +3
-0+ + printf("[SKIP]\tDynamic fpstate vmalloc usage changed concurrently.\n"); +}
Thanks,
Chang
Thanks,
Guixiong