Re: [PATCH v4 2/2] selftests/x86/amx: Test dynamic fpstate cleanup across exec()

From: Chang S. Bae

Date: Fri Nov 06 2026 - 11:45:24 EST


On 11/2/26 02:30, Guixiong Wei wrote:
---
tools/testing/selftests/x86/amx.c | 251 +++++++++++++++++++++++++++++-
1 file changed, 249 insertions(+), 2 deletions(-)


The code looks an overkill to me. I also considered using kmemleak, but this vmallocinfo looks to be efficient.

The other way would be adding a script on top of a small change to amx.c file. See the attachment.

Can you please try running this on your side and see if it works?

PS: I sent this yesterday but it didn't go through. I switched *.sh to *.txt in the attachment.

Thanks,
Chang#!/usr/bin/env bash

NUM_ITERATION=10

if [ "$EUID" -ne 0 ]; then
echo "Error: Please run as root (sudo) to access /proc/vmallocinfo."
exit 1
fi

TMP_BEFORE=$(mktemp)
TMP_AFTER=$(mktemp)

# Function to parse and aggregate vmallocinfo by caller/module
parse_vmalloc() {
# Typical format: 0xffffc90000000000-0xffffc90000002000 8192 pcpu_alloc_alloc+0x3b/0x240 vmalloc
# We 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
}

parse_vmalloc > "$TMP_BEFORE"
./amx_64 --exec-test $NUM_ITERATION
parse_vmalloc > "$TMP_AFTER"


# Join both files on the caller name and compute the differences
LEAKED=$(awk -F, '
NR == FNR {
before[$1] = $2
next
}
{
after[$1] = $2
diff = $2 - before[$1]
if ($1 ~ /__xfd_enable_feature/ && diff > 1024*4*3*$NUM_ITERATION) {
found=1
#print "%-40s %+15d\n", $1, diff
}
delete before[$1] # Mark as processed
} END {
print found+0
}' "$TMP_BEFORE" "$TMP_AFTER")

if [ "$LEAKED" == "1" ]; then
echo "[FAIL] memory leak"
else
echo "[OK] no memory leak"
fi

rm -f "$TMP_BEFORE" "$TMP_AFTER"
diff --git a/tools/testing/selftests/x86/amx.c b/tools/testing/selftests/x86/amx.c
index 40769c16de1b..58bbeac3a023 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,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_CHILD_ARG "--exec-test-child"
+
struct xstate_info xtiledata;

/* The helpers for managing XSAVE buffer and tile states: */
@@ -478,11 +482,44 @@ static void test_fork(void)
_exit(0);
}

-int main(void)
+static int test_exec(int argc, char **argv)
+{
+ int i, times = atoi(argv[2]);
+
+ req_xtiledata_perm();
+
+ 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");
+ }
+
+ return 0;
+}
+
+int main(int argc, char **argv)
{
unsigned long features;
long rc;

+ if (argc == 2 && !strcmp(argv[1], EXEC_TEST_CHILD_ARG))
+ 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");
@@ -496,6 +533,10 @@ int main(void)
}

init_stashed_xsave();
+
+ if (argc == 3 && !strcmp(argv[1], EXEC_TEST_ARG))
+ return test_exec(argc, argv);
+
sethandler(SIGILL, handle_noperm, 0);

test_dynamic_state();