From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f202.google.com (mail-oi1-f202.google.com [209.85.167.202]) (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 5E6FF3B47D3 for ; Mon, 15 Jun 2026 19:37:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781552265; cv=none; b=WXh5ZlI6zZoFM7iiRdwM0+6fjMfFGyU4hNOr2ka4W+YnvWRl5RxvtgJsC9NxxvW8ULEzVAIwx6f4m1fUvoBC/02HO6VtP4YA4GR3uEz21jQKyH0jGE0JrC9dZ1vlkIVNjgszGkK2H93fMHisMpcT7T+gtLTIbSsArGDFVToGuMc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781552265; c=relaxed/simple; bh=uzBfL06+0Mojb5eY6VNCrss0cPFTjSrWhL6ZXGes1H8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dJLrtA5qEgJ1MqRoLyWRjUzRpyGo2644qe+M0hEC4MsBQwfos7ELgEHiTXZNl/cZZMPyv59SabdovXjV4TmkFpUIpKWc58ZHf8E+amjUz6eOCFOz24fzi+OsJKG841AkzRAGrrAm7qxDilpvaXVt4agJONlSy3ZxkjjLSB9PgNA= 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=Xfp3JVsg; arc=none smtp.client-ip=209.85.167.202 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="Xfp3JVsg" Received: by mail-oi1-f202.google.com with SMTP id 5614622812f47-48661b2ef8eso3961409b6e.0 for ; Mon, 15 Jun 2026 12:37:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781552258; x=1782157058; darn=vger.kernel.org; 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=Xfp3JVsgTIa6IKS2FTGW1ge6pB959tz8JgYjaGEEAJVFu4KMX89qE4QCPw8HcT+a8n J9HVBQDSjVfBnEqn4CoCPEAvW4lr9YxW4oAdETj0T7jmdIC9g5G0iEnq5Njq9hRxg6iW mqQRpc68qpz+VgEk7QuWi6MPfmMBcdXB158ONjTx8SFtYgjDBs1TM2mWNlctw2c3ArU9 AbROV1iJvSXHod48Z00Kv2lXcmg7QFVFctLDfS0ieBV4M1ldZANS+ai8B57Koqa78G34 ksI5opI6pBeII9FMCcM5RlME0xQ1Bxq7J50taPdcpc5fKFTSOAzYkiULeA2K1avHdIvv XNDA== 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=bjYx6Wm1l6W6FqPvQx1R/Sy3jy7r1qyv+zctB2vqnQpLcpoJZRDYndO33V6Z/b5KdV 2k8H+7sefMqqtGDo7nST4KyVOEtvjceIOo6fsJZEzvEuoiqrRf1CkiKPCdQDDMjLD91H mhv/oPBn+zW3KSmfsZg4DtG9PU1iS8Hegcsy4tQM9Yq6s+r9Mf5pawH3xC6z7lKl0Bgm E/iAomRu/9v7q+SSJM7yLGqvpaIaIuAxSzZhoIc/WiVESSVvKDHmR87HPK30wcS2x6Oc BYgkE2seuhv4ZgU5im1fHojm44MuE4hxKEzP+Bj6Cyt5C/SvQHiI9c2Vk23nMHlA3VdK t31Q== X-Gm-Message-State: AOJu0Yz4+wCxkCxmZs7jJus4CuQ1MsB7Q5yT5qoca5QHXtdhyngSI8Rs bdsB3JKXN7wJR0SQfH49eDz6874gEOUv0WM7T727FW+P9f+qcJy2B9tjAqYVObxtdU5vb9hwFym YAQKvDg== 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: linux-kernel@vger.kernel.org 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