All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrei Vagin <avagin@google.com>
To: "Chang S. Bae" <chang.seok.bae@intel.com>
Cc: Daniel Wade <danjwade95@gmail.com>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] x86/fpu: Put non-enabled dynamic features into init state on sigreturn
Date: Thu, 19 Nov 2026 07:50:45 +0000	[thread overview]
Message-ID: <av6qVXvdezQgH7BW@google.com> (raw)
In-Reply-To: <av5TI7uMazFmWYid@google.com>

On Thu, Nov 19, 2026 at 01:38:43AM +0000, Andrei Vagin wrote:
> On Wed, Nov 18, 2026 at 12:19:02PM -0800, Chang S. Bae wrote:
> > On 11/17/2026 3:26 AM, Daniel Wade wrote:
> > > On sigreturn restore_fpregs_from_user() restores the features the signal
> > > frame carries and inits the rest, but both masks are limited to
> > > fpstate::user_xfeatures. If the task lost the registers earlier in the
> > > syscall, a dynamic feature it has not enabled yet -- XTILE_DATA before
> > > its first AMX use -- still holds the previous task state, and
> > 
> > How could that happen?
> 
> The code relies on this: while a task owns the FPU registers, a dynamic
> feature it has not enabled is in its init state.
> restore_fpregs_from_fpstate() maintains that by restoring with
> fpu_kernel_cfg.max_features (the comment there describes exactly this
> case), and fpu__clear_user_states() does the same on signal delivery.
> 
> The 64-bit sigreturn path doesn't. restore_fpregs_from_user() restores
> from the user buffer with RFBM and init_bv both limited to
> fpstate->user_xfeatures, then calls fpregs_mark_activate(). That clears
> TIF_NEED_FPU_LOAD, so the full restore on exit to user, which would have
> put XTILE_DATA into init state, never runs.
> 
> If the registers still belonged to current, that's fine. But the task
> can lose them inside rt_sigreturn: the first XRSTOR from the frame
> faults and fault_in_readable() sleeps, or the task is simply preempted.
> Switching out only saves the registers. It doesn't clear them, so the
> next AMX user leaves its tiles there:
> 
>   CPU0
>   A: rt_sigreturn()            A has AMX permission, XTILE_DATA not enabled
>        sleeps / preempted      A's state saved, TIF_NEED_FPU_LOAD set
>   B: runs, uses AMX            B's tiles are now in the registers
>   A: back in rt_sigreturn()
>        restore_fpregs_from_user()
>          xfd_update_state()    XFD[18] set: B's tiles hidden, not cleared
>          XRSTOR(frame, RFBM)   bit 18 not in RFBM or init_bv: untouched
>          fpregs_mark_activate()
>   A: exit to user              no reload, B's tiles still in the registers
> 
> 
> Here is the selftest to rerpoduce the issue:
> 
> Subject: [PATCH] selftests/x86/amx: Check that sigreturn does not expose stale
>  XTILEDATA
>

The next patch should be applied on top of the previous patch:

diff --git a/tools/testing/selftests/x86/amx.c b/tools/testing/selftests/x86/amx.c
index 31268193c4cd..17fc113eda5c 100644
--- a/tools/testing/selftests/x86/amx.c
+++ b/tools/testing/selftests/x86/amx.c
@@ -895,8 +895,13 @@ static void test_sigreturn_stale_tiledata(void)
 		attacker = fork();
 		if (attacker < 0)
 			fatal_error("fork");
-		if (attacker == 0)
-			_exit(stale_tiledata_attacker(cpu, helper_cpu));
+		if (attacker == 0) {
+			int ret = stale_tiledata_attacker(cpu, helper_cpu);
+
+			/* _exit() does not flush stdio buffers. */
+			fflush(stdout);
+			_exit(ret);
+		}
 
 		if (waitpid(attacker, &status, 0) != attacker)
 			fatal_error("waitpid");
diff --git a/tools/testing/selftests/x86/xstate.h b/tools/testing/selftests/x86/xstate.h
index eedf0cab7ccb..7229f3e5c6d4 100644
--- a/tools/testing/selftests/x86/xstate.h
+++ b/tools/testing/selftests/x86/xstate.h
@@ -93,7 +93,8 @@ static inline void xrstor(struct xsave_buffer *xbuf, uint64_t rfbm)
 	uint32_t rfbm_lo = rfbm;
 
 	asm volatile("xrstor (%%rdi)"
-		     : : "D" (xbuf), "a" (rfbm_lo), "d" (rfbm_hi));
+		     : : "D" (xbuf), "a" (rfbm_lo), "d" (rfbm_hi)
+		     : "memory");
 }
 
 static inline uint64_t xgetbv(uint32_t index)

--
I pushed all changed in https://github.com/avagin/linux-task-diag/commits/wip/fpu-sigreturn-init-xtiledata/.

Thanks,
Andrei

      reply	other threads:[~2026-11-19  7:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 11:26 [PATCH] x86/fpu: Put non-enabled dynamic features into init state on sigreturn Daniel Wade
2026-11-18 20:19 ` Chang S. Bae
2026-11-19  1:38   ` Andrei Vagin
2026-11-19  7:50     ` Andrei Vagin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=av6qVXvdezQgH7BW@google.com \
    --to=avagin@google.com \
    --cc=bp@alien8.de \
    --cc=chang.seok.bae@intel.com \
    --cc=danjwade95@gmail.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.