[PATCH 6/6] selftests/mm: Add coredump test for x86
From: Chang S. Bae
Date: Mon Nov 09 2026 - 22:44:41 EST
There is currently no test covering the coredump path for PKRU. Add one
by reusing the helpers from the x86 selftests.
Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
---
Note: The test fails right now. So the fix [*] should get in first.
[*] https://lore.kernel.org/lkml/20261015210545.48208-1-chang.seok.bae@xxxxxxxxx/
---
tools/testing/selftests/mm/Makefile | 2 +-
tools/testing/selftests/mm/pkey-x86.h | 1 +
.../selftests/mm/pkey_coredump_tests.c | 89 +++++++++++++++++++
tools/testing/selftests/mm/run_vmtests.sh | 11 +++
4 files changed, 102 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/mm/pkey_coredump_tests.c
diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index 0ea72e997934..d687936c8144 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -116,7 +116,7 @@ CAN_BUILD_X86_64 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_64bit_pr
CAN_BUILD_WITH_NOPIE := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_program.c -no-pie)
VMTARGETS := protection_keys
-VMTARGETS += pkey_sighandler_tests
+VMTARGETS += pkey_sighandler_tests pkey_coredump_tests
BINARIES_32 := $(VMTARGETS:%=%_32)
BINARIES_64 := $(VMTARGETS:%=%_64)
diff --git a/tools/testing/selftests/mm/pkey-x86.h b/tools/testing/selftests/mm/pkey-x86.h
index f7ecd335df1e..5b16e9ebcb2d 100644
--- a/tools/testing/selftests/mm/pkey-x86.h
+++ b/tools/testing/selftests/mm/pkey-x86.h
@@ -112,6 +112,7 @@ static inline u32 pkey_bit_position(int pkey)
#define XSTATE_PKEY_BIT (9)
#define XSTATE_PKEY 0x200
#define XSTATE_BV_OFFSET 512
+#define XSTATE_SW_BYTES_OFFSET 464
static inline int pkey_reg_xstate_offset(void)
{
diff --git a/tools/testing/selftests/mm/pkey_coredump_tests.c b/tools/testing/selftests/mm/pkey_coredump_tests.c
new file mode 100644
index 000000000000..1afb40b71800
--- /dev/null
+++ b/tools/testing/selftests/mm/pkey_coredump_tests.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <sys/wait.h>
+
+#include "pkey-helpers.h"
+
+#if defined(__i386__) || defined(__x86_64__)
+#include "../x86/coredump.h"
+
+static bool validate_corefile_pkru(u64 *xsave, u32 pkru)
+{
+ bool ret = false;
+
+ if (!(xsave[XSTATE_SW_BYTES_OFFSET / sizeof(u64)] & XSTATE_PKEY))
+ goto out;
+
+ if (!(xsave[XSTATE_BV_OFFSET / sizeof(u64)] & XSTATE_PKEY))
+ goto out;
+
+ if (*(u32 *)(xsave + pkey_reg_xstate_offset() / sizeof(u64)) != pkru)
+ goto out;
+
+ ret = true;
+out:
+ return ret;
+}
+
+static void test_x86_coredump(void)
+{
+ u64 *corefile_xsave;
+ u32 new_pkru;
+ pid_t child;
+ int status;
+
+ if (!coredump_prepare())
+ return;
+
+ new_pkru = ~__read_pkey_reg();
+ new_pkru &= ~3;
+
+ child = fork();
+ if (child < 0) {
+ coredump_exit_fail_msg("fork() failed.");
+ } else if (!child) {
+ if (chdir(core_dir)) {
+ printf("\tchdir() failed.\n");
+ _exit(1);
+ }
+ __write_pkey_reg(new_pkru);
+ coredump_trigger();
+ _exit(1);
+ }
+
+ if (waitpid(child, &status, 0) != child ||
+ !WIFSIGNALED(status) || WTERMSIG(status) != SIGSEGV)
+ coredump_exit_fail_msg("child didn't SIGSEGV successfully.");
+
+ corefile_map(child);
+ corefile_xsave = (u64 *)retrieve_corefile_xbuf();
+
+ if (validate_corefile_pkru(corefile_xsave, new_pkru))
+ ksft_test_result_pass("PKRU was correctly core-dumped\n");
+ else
+ ksft_test_result_fail("PKRU was not correctly core-dumped\n");
+
+ coredump_cleanup();
+}
+#endif
+
+static void (*pkey_tests[])(void) = {
+#if defined(__i386__) || defined(__x86_64__)
+ test_x86_coredump,
+#endif
+};
+
+int main(int argc, char *argv[])
+{
+ ksft_print_header();
+
+ if (!is_pkeys_supported())
+ ksft_exit_skip("pkeys not supported\n");
+
+ ksft_set_plan(ARRAY_SIZE(pkey_tests));
+
+ for (test_nr = 0; test_nr < ARRAY_SIZE(pkey_tests); test_nr++)
+ (*pkey_tests[test_nr])();
+
+ ksft_finished();
+}
diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh
index a1b45a3dedae..bf7c8aa0b098 100755
--- a/tools/testing/selftests/mm/run_vmtests.sh
+++ b/tools/testing/selftests/mm/run_vmtests.sh
@@ -373,6 +373,17 @@ then
CATEGORY="pkey" run_test ./protection_keys_64
fi
+# pkey_coredump tests
+if [ -x ./pkey_coredump_tests_32 ]
+then
+ CATEGORY="pkey" run_test ./pkey_coredump_tests_32
+fi
+
+if [ -x ./pkey_coredump_tests_64 ]
+then
+ CATEGORY="pkey" run_test ./pkey_coredump_tests_64
+fi
+
CATEGORY="soft_dirty" run_test ./soft-dirty
CATEGORY="pagemap" run_test ./pagemap_ioctl
--
2.53.0