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,
Shuah Khan <shuah@kernel.org>,
kselftest@vger.kernel.org
Subject: [PATCH 6/6] selftests/x86/amx: Test fpstate memory cleanup on exec()
Date: Mon, 30 Nov 2026 20:51:42 +0000 [thread overview]
Message-ID: <20261130205143.1258369-7-chang.seok.bae@intel.com> (raw)
In-Reply-To: <20261130205143.1258369-1-chang.seok.bae@intel.com>
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
next prev 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 ` [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 ` Chang S. Bae [this message]
2026-11-30 21:33 ` [PATCH 6/6] selftests/x86/amx: Test fpstate memory cleanup on exec() 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-7-chang.seok.bae@intel.com \
--to=chang.seok.bae@intel.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=kselftest@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=omermetekaya0@gmail.com \
--cc=shuah@kernel.org \
--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®