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 7/9] selftests/x86/xstate: Consolidate test invocations into a single entry
Date: Tue, 25 Feb 2025 17:07:27 -0800	[thread overview]
Message-ID: <20250226010731.2456-8-chang.seok.bae@intel.com> (raw)
In-Reply-To: <20250226010731.2456-1-chang.seok.bae@intel.com>

Currently, each of the three xstate tests runs as a separate invocation,
requiring the xstate number to be passed and state information to be
reconstructed repeatedly. This approach arose from their individual and
isolated development, but now it makes sense to unify them.

Introduce a wrapper function that first verifies feature availability
from the kernel and constructs the necessary state information once. The
wrapper then sequentially invokes all tests to ensure consistent
execution.

Update the AMX test to use this unified invocation.

Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
---
 tools/testing/selftests/x86/amx.c    | 11 ++++----
 tools/testing/selftests/x86/xstate.c | 38 ++++++++++++++++++++--------
 tools/testing/selftests/x86/xstate.h |  5 ++--
 3 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/tools/testing/selftests/x86/amx.c b/tools/testing/selftests/x86/amx.c
index 9cb691d67ef4..40769c16de1b 100644
--- a/tools/testing/selftests/x86/amx.c
+++ b/tools/testing/selftests/x86/amx.c
@@ -480,7 +480,6 @@ static void test_fork(void)
 
 int main(void)
 {
-	const unsigned int ctxtsw_num_threads = 5, ctxtsw_iterations = 10;
 	unsigned long features;
 	long rc;
 
@@ -506,11 +505,11 @@ int main(void)
 
 	test_fork();
 
-	test_context_switch(XFEATURE_XTILEDATA, ctxtsw_num_threads, ctxtsw_iterations);
-
-	test_ptrace(XFEATURE_XTILEDATA);
-
-	test_signal(XFEATURE_XTILEDATA);
+	/*
+	 * Perform generic xstate tests for context switching, ptrace,
+	 * and signal.
+	 */
+	test_xstate(XFEATURE_XTILEDATA);
 
 	clearhandler(SIGILL);
 	free_stashed_xsave();
diff --git a/tools/testing/selftests/x86/xstate.c b/tools/testing/selftests/x86/xstate.c
index b5600f492632..fd8451e55f3f 100644
--- a/tools/testing/selftests/x86/xstate.c
+++ b/tools/testing/selftests/x86/xstate.c
@@ -6,7 +6,9 @@
 #include <pthread.h>
 #include <stdbool.h>
 
+#include <asm/prctl.h>
 #include <sys/ptrace.h>
+#include <sys/syscall.h>
 #include <sys/uio.h>
 #include <sys/wait.h>
 
@@ -189,15 +191,13 @@ static void affinitize_cpu0(void)
 		ksft_exit_fail_msg("sched_setaffinity to CPU 0 failed\n");
 }
 
-void test_context_switch(uint32_t feature_num, uint32_t num_threads, uint32_t iterations)
+static void test_context_switch(uint32_t num_threads, uint32_t iterations)
 {
 	struct futex_info *finfo;
 
 	/* Affinitize to one CPU to force context switches */
 	affinitize_cpu0();
 
-	xstate = get_xstate_info(feature_num);
-
 	printf("[RUN]\t%s: check context switches, %d iterations, %d threads.\n",
 	       xstate.name, iterations, num_threads);
 
@@ -299,13 +299,11 @@ static void ptracer_inject_xstate(pid_t target)
 	free(xbuf2);
 }
 
-void test_ptrace(uint32_t feature_num)
+static void test_ptrace(void)
 {
 	pid_t child;
 	int status;
 
-	xstate = get_xstate_info(feature_num);
-
 	child = fork();
 	if (child < 0) {
 		ksft_exit_fail_msg("fork() failed\n");
@@ -392,17 +390,14 @@ static void validate_sigfpstate(int sig, siginfo_t *si, void *ctx_void)
 	copy_xstate(stashed_xbuf, xbuf);
 }
 
-void test_signal(uint32_t feature_num)
+static void test_signal(void)
 {
 	bool valid_xstate;
 
-	xstate = get_xstate_info(feature_num);
-
 	/*
 	 * The signal handler will access this to verify xstate context
 	 * preservation.
 	 */
-
 	stashed_xbuf = alloc_xbuf();
 	if (!stashed_xbuf)
 		ksft_exit_fail_msg("unable to allocate XSAVE buffer\n");
@@ -433,3 +428,26 @@ void test_signal(uint32_t feature_num)
 	clearhandler(SIGUSR1);
 	free(stashed_xbuf);
 }
+
+void test_xstate(uint32_t feature_num)
+{
+	const unsigned int ctxtsw_num_threads = 5, ctxtsw_iterations = 10;
+	unsigned long features;
+	long rc;
+
+	rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_SUPP, &features);
+	if (rc || !(features & (1 << feature_num))) {
+		ksft_print_msg("The kernel does not support feature number: %u\n", feature_num);
+		return;
+	}
+
+	xstate = get_xstate_info(feature_num);
+	if (!xstate.size || !xstate.xbuf_offset) {
+		ksft_exit_fail_msg("invalid state size/offset (%d/%d)\n",
+				   xstate.size, xstate.xbuf_offset);
+	}
+
+	test_context_switch(ctxtsw_num_threads, ctxtsw_iterations);
+	test_ptrace();
+	test_signal();
+}
diff --git a/tools/testing/selftests/x86/xstate.h b/tools/testing/selftests/x86/xstate.h
index 4d0ffe9609f8..42af36ec852f 100644
--- a/tools/testing/selftests/x86/xstate.h
+++ b/tools/testing/selftests/x86/xstate.h
@@ -189,8 +189,7 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffer
 		*ptr = data;
 }
 
-void test_context_switch(uint32_t feature_num, uint32_t num_threads, uint32_t iterations);
-void test_ptrace(uint32_t feature_num);
-void test_signal(uint32_t feature_num);
+/* Testing kernel's context switching and ABI support for the xstate. */
+void test_xstate(uint32_t feature_num);
 
 #endif /* __SELFTESTS_X86_XSTATE_H */
-- 
2.45.2


  parent reply	other threads:[~2025-02-26  1:07 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-26  1:07 [PATCH 0/9] selftests/x86/xstate: Introduce common code for testing extended states Chang S. Bae
2025-02-26  1:07 ` [PATCH 1/9] selftests/x86: Consolidate redundant signal helper functions Chang S. Bae
2025-02-26 12:15   ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26  1:07 ` [PATCH 2/9] selftests/x86/xstate: Refactor XSAVE helpers for general use Chang S. Bae
2025-02-26 12:15   ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26  1:07 ` [PATCH 3/9] selftests/x86/xstate: Enumerate and name xstate components Chang S. Bae
2025-02-26 12:15   ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26  1:07 ` [PATCH 4/9] selftests/x86/xstate: Refactor context switching test Chang S. Bae
2025-02-26 12:15   ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26  1:07 ` [PATCH 5/9] selftests/x86/xstate: Refactor ptrace ABI test Chang S. Bae
2025-02-26 12:15   ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26  1:07 ` [PATCH 6/9] selftests/x86/xstate: Introduce signal " Chang S. Bae
2025-02-26 12:15   ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26  1:07 ` Chang S. Bae [this message]
2025-02-26 12:15   ` [tip: x86/fpu] selftests/x86/xstate: Consolidate test invocations into a single entry tip-bot2 for Chang S. Bae
2025-02-26  1:07 ` [PATCH 8/9] selftests/x86/xstate: Clarify supported xstates Chang S. Bae
2025-02-26 12:15   ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26  1:07 ` [PATCH 9/9] selftests/x86/avx: Add AVX test Chang S. Bae
2025-02-26 12:15   ` [tip: x86/fpu] selftests/x86/avx: Add AVX tests tip-bot2 for Chang S. Bae
2025-02-26 12:07 ` [PATCH 0/9] selftests/x86/xstate: Introduce common code for testing extended states Ingo Molnar

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=20250226010731.2456-8-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®