[PATCH 6/6] selftests/x86/amx: Test fpstate memory cleanup on exec()
From: Chang S. Bae
Date: Mon Nov 30 2026 - 16:19:39 EST
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@xxxxxxxxx>
Cc: Guixiong Wei <weiguixiong@xxxxxxxxxxxxx>
Cc: Shuah Khan <shuah@xxxxxxxxxx>
Cc: kselftest@xxxxxxxxxxxxxxx
---
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