From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f201.google.com (mail-oi1-f201.google.com [209.85.167.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 340703B47CF for ; Mon, 15 Jun 2026 19:37:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781552266; cv=none; b=SsMc1EjEBVNkn9bqruQbYrpXOQq6edkKkI78Js1WDqn63GykavjTqcRFSsyBm+QgtqzkH0MewY8i7pAK7vuCQpje4LMuftUl7egS8mUD1279ZzMekcO8dHqJC3bBkaNl4SwEGOFB0awh9Bo2igf44ctM7euLF5nfTB7zX7JR1EU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781552266; c=relaxed/simple; bh=uzBfL06+0Mojb5eY6VNCrss0cPFTjSrWhL6ZXGes1H8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pZABe7kCdx2MzxUj375vydWPVJ9cyhpDUqkFKIryW4s6xPOLz/SZsx0OMwAKdU8sXFk7TEYZhBbYj0blqOCdhJBwNxBVgEWV18mfBwR6zHX8Gxfc+sZOT+iEaGULXfmeWSuw4MiY8qZkEUwct7oNd7GWqEiZDDk/oucHU3HxMKE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--avagin.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=IqRkNVY/; arc=none smtp.client-ip=209.85.167.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--avagin.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IqRkNVY/" Received: by mail-oi1-f201.google.com with SMTP id 5614622812f47-486055c1c6fso3779805b6e.1 for ; Mon, 15 Jun 2026 12:37:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781552258; x=1782157058; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=l4cPk0x/vPFsbfrrd3SG7nc7RLOqgzITpmKFfEuQCLY=; b=IqRkNVY/JAoZdeLOWm+jpZdNnji7j2/hZdNS7G2vkqbHdQyJpnmOiTvQ7h0Pw88HhP OsY3eGRbCKDr0Z+/1if7eXrfv6sHmIx0k9YVa+r9mKQXp2fvnKOfIKKMaH+DRSoFgX6J 5/4GNCtjceGdE+lk1Tj82TNSPqbXcbr8PkRg331bSOXnrReC6PZSIyMcfsj/EcpG9cjw NfBqPUHQ48P/43WDAZD1M/hs9TtwrgO/YbF0zX4ZOZse5ygvRZhFKnxo/0WqL+Y9ML7H YGwOtCSZ5wi4s2VVS7//02emW4iGRREjU2unEWN2CT1jImRqYObDBCsfK257eWpItQ7u GDdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781552258; x=1782157058; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=l4cPk0x/vPFsbfrrd3SG7nc7RLOqgzITpmKFfEuQCLY=; b=cGlOde7mDKeAfYRy8xAs1m1neKJhSD05yvXof4PKY0vqPyHXMmsn/52xbNqH4W3Ddj na7OTZxuqBbXDod8NUbvzv7QPCYbGJMy3pkvZ40fJAGURiH/iXOnJy6vFZSr0zutwl+j NJACoti1MlX6hhRvL8SiWatsvLmqhfeWJkT9pEBten61SFMBYgQSUeAKU1YCkeKRR/5z Ov/o5OOn81Yj01jkFyC9CCO+rPOBG3i3uvN5xwmUceyhd0BPWmPoG+TjKg/ND0NkshiP 5Bav66Sh2MuRrifii+1iDqyqObIrdE4kONb+JjikrdFLINDf7txFxqf8jC1oA+0fWIPE ivNg== X-Forwarded-Encrypted: i=1; AFNElJ8i686kytWKKYCZzJPEw+WCRng/dl6IsXQdkAaicjGzYnZqm+6gTH1oypGMWyo6hdD9dmri@lists.linux.dev X-Gm-Message-State: AOJu0YwGoQzECHg1qwOf0k4BfQElwVearqfFrW9Jrfa2G4bl9mkK0ZlX ZXVjo3SXD6+V5RLza+B4cGx8ZW7in1P5boz8GcK90sBIvLMREClbc3A8MM2+P5PBQpVWJXnAhrM oo/2WOQ== X-Received: from ilbdx6-n2.prod.google.com ([2002:a05:6e02:4206:20b0:502:269d:3313]) (user=avagin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6809:303:20b0:487:467e:6eea with SMTP id 5614622812f47-487467e7126mr4586317b6e.35.1781552258190; Mon, 15 Jun 2026 12:37:38 -0700 (PDT) Date: Mon, 15 Jun 2026 19:37:16 +0000 In-Reply-To: <20260615193716.1843340-1-avagin@google.com> Precedence: bulk X-Mailing-List: criu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260615193716.1843340-1-avagin@google.com> X-Mailer: git-send-email 2.54.0.1189.g8c84645362-goog Message-ID: <20260615193716.1843340-11-avagin@google.com> Subject: [PATCH 10/10] selftests/x86: Check restoring FPU state with larger xstate_size From: Andrei Vagin To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "Chang S. Bae" Cc: linux-kernel@vger.kernel.org, criu@lists.linux.dev, Dave Hansen , x86@kernel.org, Andrei Vagin , "H. Peter Anvin" Content-Type: text/plain; charset="UTF-8" Add two new test cases to sigframe_fpu_portability.c: The first test case (test_larger_xstate_size) verifies that the kernel can restore FPU state from a signal frame that has xstate_size larger than the current task's fpstate->user_size, but the buffer doesn't contain states of any unsupported features. This test case emulates a case when a process is migrated from a newer cpu to an older cpu, but the process doesn't use any unsupported features. The second test case (test_unsupported_xfeatures) verifies that the kernel correctly rejects restoring FPU state from a signal frame if it contains states of any unsupported features. Signed-off-by: Andrei Vagin --- .../selftests/x86/sigframe_fpu_portability.c | 129 +++++++++++++++++- 1 file changed, 128 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/x86/sigframe_fpu_portability.c b/tools/testing/selftests/x86/sigframe_fpu_portability.c index 462219905303..b349efcf05c3 100644 --- a/tools/testing/selftests/x86/sigframe_fpu_portability.c +++ b/tools/testing/selftests/x86/sigframe_fpu_portability.c @@ -27,6 +27,14 @@ * - test_insufficient_xstate_size: * Verifies that the kernel rejects a frame if xstate_size is too small for * the features enabled in xfeatures. + * + * - test_larger_xstate_size: + * Verifies that the kernel restores state from a frame with xstate_size + * larger than the current task's size, if no unsupported features are active. + * + * - test_unsupported_xfeatures: + * Verifies that the kernel rejects a frame if it contains unsupported + * features in the xsave header. */ #define SIGFRAME_XSTATE_HDR_OFFSET 512 @@ -203,15 +211,134 @@ static void test_insufficient_xstate_size(void) clearhandler(SIGSEGV); } +static char fpu_buffer[8192] __attribute__((aligned(64))); +#define UNSUPPORTED_XFEATURE (1ULL<<62) + +static void __handle_larger_xstate_size(int sig, siginfo_t *si, void *ucp, bool mod_xhdr) +{ + ucontext_t *uc = ucp; + void *fp = uc->uc_mcontext.fpregs; + struct _fpx_sw_bytes *sw = get_fpx_sw_bytes(fp); + size_t copy_size; + uint64_t *ymmh_p, xfeatures; + struct xsave_buffer *xbuf; + + if (sw->magic1 != FP_XSTATE_MAGIC1) { + sig_print("magic1 is not valid\n"); + return; + } + + copy_size = sw->xstate_size; + if (copy_size > sizeof(fpu_buffer)) { + sig_print("fpu_buffer is too small\n"); + return; + } + + memset(fpu_buffer, 0, sizeof(fpu_buffer)); + memcpy(fpu_buffer, fp, copy_size); + + xbuf = (struct xsave_buffer *)fpu_buffer; + sw = get_fpx_sw_bytes(fpu_buffer); + + sw->xstate_size += 64; + sw->extended_size += 64; + xfeatures = get_fpx_sw_bytes_features(xbuf); + set_fpx_sw_bytes_features(fpu_buffer, xfeatures | UNSUPPORTED_XFEATURE); + + *(uint32_t *)((char *)fpu_buffer + sw->xstate_size) = FP_XSTATE_MAGIC2; + + if (mod_xhdr) { + xfeatures = get_xstatebv(xbuf); + set_xstatebv(xbuf, xfeatures | UNSUPPORTED_XFEATURE); + } + + ymmh_p = (uint64_t *)(fpu_buffer + ymm_offset); + ymmh_p[0] = TEST_YMMH_VAL; + ymmh_p[1] = TEST_YMMH_VAL + 1; + + /* Update fpregs to point to the new buffer */ + uc->uc_mcontext.fpregs = (fpregset_t)fpu_buffer; +} + +static void handle_larger_xstate_size(int sig, siginfo_t *si, void *ucp) +{ + __handle_larger_xstate_size(sig, si, ucp, false); +} + +static void test_larger_xstate_size(void) +{ + uint64_t v[4] = {0, 0, 0, 0}; + + sig_err_buf[0] = 0; + sethandler(SIGUSR1, handle_larger_xstate_size, 0); + + v[0] = 0x1111111111111111ULL; + v[1] = 0x2222222222222222ULL; + v[2] = 0x3333333333333333ULL; + v[3] = 0x4444444444444444ULL; + write_ymm0(v); + + raise(SIGUSR1); + v[0] = v[1] = v[2] = v[3] = 0; + read_ymm0(v); + + if (sig_err_buf[0]) + ksft_test_result_fail("%s\n", sig_err_buf); + else if (v[2] == TEST_YMMH_VAL && v[3] == TEST_YMMH_VAL + 1) + ksft_test_result_pass("YMM state restored correctly with larger xstate_size\n"); + else + ksft_test_result_fail( + "Got upper bits: 0x%lx 0x%lx (expected %lx %lx)\n", + v[2], v[3], TEST_YMMH_VAL, TEST_YMMH_VAL + 1); + + clearhandler(SIGUSR1); +} + +static void handle_unsupported_xfeatures(int sig, siginfo_t *si, void *ucp) +{ + __handle_larger_xstate_size(sig, si, ucp, true); +} + +static void test_unsupported_xfeatures(void) +{ + uint64_t v[4] = {0, 0, 0, 0}; + + sig_err_buf[0] = 0; + + sethandler(SIGUSR1, handle_unsupported_xfeatures, 0); + sethandler(SIGSEGV, handle_segv, 0); + + v[0] = 0x1111111111111111ULL; + v[1] = 0x2222222222222222ULL; + v[2] = 0x3333333333333333ULL; + v[3] = 0x4444444444444444ULL; + write_ymm0(v); + + if (sigsetjmp(segv_jmpbuf, 1) == 0) { + raise(SIGUSR1); + sig_print("raise(SIGUSR1) returned (expected SIGSEGV)\n"); + } + + clearhandler(SIGUSR1); + clearhandler(SIGSEGV); + + if (sig_err_buf[0]) + ksft_test_result_fail("%s\n", sig_err_buf); + else + ksft_test_result_pass("Unsupported feature in xsave header triggered SIGSEGV\n"); +} + int main(void) { ksft_print_header(); - ksft_set_plan(2); + ksft_set_plan(4); check_avx_support(); test_shrunk_xstate_size(); test_insufficient_xstate_size(); + test_larger_xstate_size(); + test_unsupported_xfeatures(); ksft_finished(); return 0; -- 2.54.0.1189.g8c84645362-goog