mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Chang S. Bae" <chang.seok.bae@intel.com>
To: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Cc: x86@kernel.org, tglx@linutronix.de, mingo@redhat.com,
	bp@alien8.de, dave.hansen@linux.intel.com, shuah@kernel.org,
	chang.seok.bae@intel.com
Subject: [PATCH 6/6] selftests/mm: Add coredump test for x86
Date: Tue, 10 Nov 2026 03:16:03 +0000	[thread overview]
Message-ID: <20261110031603.698635-7-chang.seok.bae@intel.com> (raw)
In-Reply-To: <20261110031603.698635-1-chang.seok.bae@intel.com>

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@intel.com>
---
Note: The test fails right now. So the fix [*] should get in first.

[*] https://lore.kernel.org/lkml/20261015210545.48208-1-chang.seok.bae@intel.com/
---
 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


      parent reply	other threads:[~2026-11-10  3:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10  3:15 [PATCH 0/6] selftests/x86/xstate: more test cases for coredump and ptrace Chang S. Bae
2026-11-10  3:15 ` [PATCH 1/6] selftests/x86/xstate: Track testing status by kselftest API Chang S. Bae
2026-11-10  3:15 ` [PATCH 2/6] selftests/x86/xstate: Pre-allocate ptracee XSAVE buffer Chang S. Bae
2026-11-10  3:16 ` [PATCH 3/6] selftests/x86/xstate: Validate XSTATE_BV in ptrace test Chang S. Bae
2026-11-10  3:16 ` [PATCH 4/6] selftests/x86/xstate: Validate state reset " Chang S. Bae
2026-11-10  3:16 ` [PATCH 5/6] selftests/x86/xstate: Add coredump test Chang S. Bae
2026-11-10  3:16 ` Chang S. Bae [this message]

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=20261110031603.698635-7-chang.seok.bae@intel.com \
    --to=chang.seok.bae@intel.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=shuah@kernel.org \
    --cc=tglx@linutronix.de \
    --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®