Re: [PATCH 7/7] selftests/x86: Add tests for signal frame FPU portability

From: Chang S. Bae

Date: Mon Sep 14 2026 - 13:36:07 EST


On 9/7/2026 9:34 PM, Andrei Vagin wrote:
Add a new selftest tools/testing/selftests/x86/sigframe_fpu_portability.c
to verify signal frame portability and consistency when the xstate size
is shrunk:

- test_valid_shrunk_xstate_size: Verifies that the kernel correctly
restores the xstate context from a signal frame where xstate_size has
been manually shrunk to only cover active features, as long as the
FP_XSTATE_MAGIC2 marker is correctly placed. This simulates migrating
a process created on a host with fewer xstate features to a host with
more features.

- test_invalid_shrunk_xstate_size: Verifies that the kernel rejects
(via SIGSEGV) a signal frame where xstate_size is smaller than required
by the enabled features in the xfeatures mask.

Reviewed-by: Alexander Mikhalitsyn <alexander@xxxxxxxxxxxxx>
Signed-off-by: Andrei Vagin <avagin@xxxxxxxxxx>

...


+static void sig_print(const char *msg)
+{
+ int left = SIGNAL_BUF_LEN - strlen(sig_err_buf) - 1;
+
+ strncat(sig_err_buf, msg, left);
+}
+
+

Nit: extra empty line


+static void __handle_shrunk_xstate_size(int sig, siginfo_t *si, void *ucp, bool valid_size)
+{
+ ucontext_t *uc = ucp;
+ void *fp = uc->uc_mcontext.fpregs;
+ struct _fpx_sw_bytes *sw;
+ struct xsave_buffer *xbuf;
+ uint64_t xfeatures, *ymmh_p;
+
+ if (!fp) {
+ sig_print("fpregs is NULL\n");
+ return;
+ }
+
+ sw = get_fpx_sw_bytes(fp);
+ if (sw->magic1 != FP_XSTATE_MAGIC1) {
+ sig_print("magic1 is not valid\n");
+ return;
+ }
+
+ xbuf = (struct xsave_buffer *)fp;
+
+ /*
+ * Both test cases shrink the frame to contain only AVX (FP + SSE + YMM).
+ * If valid_size is true, set xstate_size to match the enabled features.
+ * If valid_size is false, set xstate_size too small (SSE only), which
+ * the kernel must reject.
+ */
+ if (valid_size)
+ sw->xstate_size = xstate_size_ymm;
+ else
+ sw->xstate_size = XSTATE_SSE_ONLY_SIZE;
Yeah, I think this note explains the distinction between the two test cases and the point of testing:

Reviewed-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>

Thanks,
Chang