[PATCH v2 4/6] selftests/x86/xstate: Validate state reset in ptrace test

From: Chang S. Bae

Date: Wed Dec 02 2026 - 16:52:17 EST


Clearing a feature bit in XSTATE_BV should cause the state component to
be initialized when the ptracer retrieves the state. Validate this
behavior.

Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
Cc: Jim Mattson <jmattson@xxxxxxxxxx>
---
V1 -> V2: Highlight subtleness with the init-tracking
---
tools/testing/selftests/x86/xstate.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/x86/xstate.c b/tools/testing/selftests/x86/xstate.c
index 61cb3327fc4f4..e0a8c9550c729 100644
--- a/tools/testing/selftests/x86/xstate.c
+++ b/tools/testing/selftests/x86/xstate.c
@@ -335,6 +335,22 @@ static void ptracer_inject_xstate(pid_t target)
else
ksft_test_result_fail("%s for %s\n", __func__, xstate.name);

+ ksft_print_msg("[RUN]\t%s: reset xstate via ptrace().\n", xstate.name);
+
+ /* Reset the feature bit in XSTATE_BV only, expecting init-state back */
+ set_xstatebv(xbuf1, get_xstatebv(xbuf1) & ~xstate.mask);
+
+ /* Prepare reference data */
+ load_init_xstate(&xstate, xbuf2);
+ xsave(xbuf2, xstate.mask);
+ /* Not every XSAVE implementation resets XSTATE_BV when in init-state */
+ set_xstatebv(xbuf2, get_xstatebv(xbuf2) & ~xstate.mask);
+
+ if (validate_ptracer_xstate(target, iov, xbuf1, xbuf2))
+ ksft_test_result_pass("%s (reset) for %s\n", __func__, xstate.name);
+ else
+ ksft_test_result_fail("%s (reset) for %s\n", __func__, xstate.name);
+
free(xbuf1);
free(xbuf2);
}
@@ -496,7 +512,7 @@ static struct test_info {
unsigned int nr;
} tests[] = {
{ .nr = 1, .fn = test_context_switch, },
- { .nr = 1, .fn = test_ptrace, },
+ { .nr = 2, .fn = test_ptrace, },
{ .nr = 2, .fn = test_signal, },
};

--
2.53.0