Skip to content

Commit 10d8a20

Browse files
ChangSeokBaeIngo Molnar
authored andcommitted
selftests/x86/xstate: Consolidate test invocations into a single entry
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> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20250226010731.2456-8-chang.seok.bae@intel.com
1 parent e075d9f commit 10d8a20

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

tools/testing/selftests/x86/amx.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ static void test_fork(void)
480480

481481
int main(void)
482482
{
483-
const unsigned int ctxtsw_num_threads = 5, ctxtsw_iterations = 10;
484483
unsigned long features;
485484
long rc;
486485

@@ -506,11 +505,11 @@ int main(void)
506505

507506
test_fork();
508507

509-
test_context_switch(XFEATURE_XTILEDATA, ctxtsw_num_threads, ctxtsw_iterations);
510-
511-
test_ptrace(XFEATURE_XTILEDATA);
512-
513-
test_signal(XFEATURE_XTILEDATA);
508+
/*
509+
* Perform generic xstate tests for context switching, ptrace,
510+
* and signal.
511+
*/
512+
test_xstate(XFEATURE_XTILEDATA);
514513

515514
clearhandler(SIGILL);
516515
free_stashed_xsave();

tools/testing/selftests/x86/xstate.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
#include <pthread.h>
77
#include <stdbool.h>
88

9+
#include <asm/prctl.h>
910
#include <sys/ptrace.h>
11+
#include <sys/syscall.h>
1012
#include <sys/uio.h>
1113
#include <sys/wait.h>
1214

@@ -189,15 +191,13 @@ static void affinitize_cpu0(void)
189191
ksft_exit_fail_msg("sched_setaffinity to CPU 0 failed\n");
190192
}
191193

192-
void test_context_switch(uint32_t feature_num, uint32_t num_threads, uint32_t iterations)
194+
static void test_context_switch(uint32_t num_threads, uint32_t iterations)
193195
{
194196
struct futex_info *finfo;
195197

196198
/* Affinitize to one CPU to force context switches */
197199
affinitize_cpu0();
198200

199-
xstate = get_xstate_info(feature_num);
200-
201201
printf("[RUN]\t%s: check context switches, %d iterations, %d threads.\n",
202202
xstate.name, iterations, num_threads);
203203

@@ -299,13 +299,11 @@ static void ptracer_inject_xstate(pid_t target)
299299
free(xbuf2);
300300
}
301301

302-
void test_ptrace(uint32_t feature_num)
302+
static void test_ptrace(void)
303303
{
304304
pid_t child;
305305
int status;
306306

307-
xstate = get_xstate_info(feature_num);
308-
309307
child = fork();
310308
if (child < 0) {
311309
ksft_exit_fail_msg("fork() failed\n");
@@ -392,17 +390,14 @@ static void validate_sigfpstate(int sig, siginfo_t *si, void *ctx_void)
392390
copy_xstate(stashed_xbuf, xbuf);
393391
}
394392

395-
void test_signal(uint32_t feature_num)
393+
static void test_signal(void)
396394
{
397395
bool valid_xstate;
398396

399-
xstate = get_xstate_info(feature_num);
400-
401397
/*
402398
* The signal handler will access this to verify xstate context
403399
* preservation.
404400
*/
405-
406401
stashed_xbuf = alloc_xbuf();
407402
if (!stashed_xbuf)
408403
ksft_exit_fail_msg("unable to allocate XSAVE buffer\n");
@@ -433,3 +428,26 @@ void test_signal(uint32_t feature_num)
433428
clearhandler(SIGUSR1);
434429
free(stashed_xbuf);
435430
}
431+
432+
void test_xstate(uint32_t feature_num)
433+
{
434+
const unsigned int ctxtsw_num_threads = 5, ctxtsw_iterations = 10;
435+
unsigned long features;
436+
long rc;
437+
438+
rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_SUPP, &features);
439+
if (rc || !(features & (1 << feature_num))) {
440+
ksft_print_msg("The kernel does not support feature number: %u\n", feature_num);
441+
return;
442+
}
443+
444+
xstate = get_xstate_info(feature_num);
445+
if (!xstate.size || !xstate.xbuf_offset) {
446+
ksft_exit_fail_msg("invalid state size/offset (%d/%d)\n",
447+
xstate.size, xstate.xbuf_offset);
448+
}
449+
450+
test_context_switch(ctxtsw_num_threads, ctxtsw_iterations);
451+
test_ptrace();
452+
test_signal();
453+
}

tools/testing/selftests/x86/xstate.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffer
189189
*ptr = data;
190190
}
191191

192-
void test_context_switch(uint32_t feature_num, uint32_t num_threads, uint32_t iterations);
193-
void test_ptrace(uint32_t feature_num);
194-
void test_signal(uint32_t feature_num);
192+
/* Testing kernel's context switching and ABI support for the xstate. */
193+
void test_xstate(uint32_t feature_num);
195194

196195
#endif /* __SELFTESTS_X86_XSTATE_H */

0 commit comments

Comments
 (0)