Return-Path: <dave.hansen@linux.intel.com>
X-Original-To: dave@localhost
Delivered-To: dave@localhost
Received: from ray2 (localhost [127.0.0.1])
	by ray2.sr71.net (Postfix) with ESMTP id CA16CB5D
	for <dave@localhost>; Thu, 19 Nov 2026 13:07:34 -0800 (PST)
X-Original-To: dave.hansen@linux.intel.com
Delivered-To: dave.hansen@linux.intel.com
Received: from linux.intel.com [10.54.29.200]
	by ray2 with IMAP (fetchmail-6.4.38 polling linux.intel.com account dave.hansen folder Inbox)
	for <dave@localhost> (single-drop); Thu, 19 Nov 2026 13:07:34 -0800 (PST)
Received: from orviesa002.jf.intel.com (ORVIESA002.jf.intel.com [10.64.159.142])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by linux.intel.com (Postfix) with ESMTPS id CB86220B8CD1
	for <dave.hansen@linux.intel.com>; Thu, 19 Nov 2026 13:07:33 -0800 (PST)
X-CSE-ConnectionGUID: rKqoRxW0SM2BR4FBPh9MRw==
X-CSE-MsgGUID: l+FYn7xzSW2WCwk6ILf7+Q==
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="6.27,221,1787036400"; 
   d="scan'208";a="10554561"
Received: from davehans-spike.ostc.intel.com (HELO localhost.localdomain) ([10.165.164.11])
  by orviesa002.jf.intel.com with ESMTP; 19 Nov 2026 13:07:34 -0800
Subject: [PATCH 3/4] x86/fpu: Reset the fpstate on exec() under fpregs_lock()
To: dave.hansen@intel.com
Cc: Dave Hansen <dave.hansen@linux.intel.com>
From: Dave Hansen <dave.hansen@linux.intel.com>
Date: Thu, 19 Nov 2026 13:07:33 -0800
References: <20261119210730.2D32F7F8@davehans-spike.ostc.intel.com>
In-Reply-To: <20261119210730.2D32F7F8@davehans-spike.ostc.intel.com>
Message-Id: <20261119210733.7A3C75E4@davehans-spike.ostc.intel.com>


Written by Claude, Anthropic's AI model, at Dave Hansen's direction.

switch_fpu() and kernel_fpu_begin() save live registers through
fpu->fpstate, using its xfeatures as the XSAVES mask, whenever
TIF_NEED_FPU_LOAD is clear. Code that changes the fpstate pointer or
its contents therefore does so under fpregs_lock(), which disables
preemption and softirqs and makes irq_fpu_usable() refuse hardirq FPU
use.

fpu_flush_thread() does not. It calls fpstate_reset() before taking
fpregs_lock() and before setting TIF_NEED_FPU_LOAD. At exec() the
registers are normally still live, so a preemption, softirq or hardirq
kernel_fpu_begin() in that window saves them through a pointer and
mask that fpstate_reset() is in the middle of writing. KCSAN reports
this as a data race between __fpstate_reset() and os_xsave().

It is harmless today. The embedded fpstate is only ever written with
the same default values, so the racing stores change nothing. With a
dynamically allocated fpstate the save lands in the embedded one using
its default mask, which is correctly sized, and exec() is about to
discard the state anyway. The worst outcome is a WARN from
xfd_validate_state() under CONFIG_X86_DEBUG_FPU because the hardware
XFD no longer matches the new fpstate's xfd.

Move the fpstate_reset() call into fpu_reset_fpstate_regs(), inside
its existing fpregs_lock() region. The vfree() of the old fpstate stays
outside the locked region since it may sleep.

No functional change intended on non-debug kernels. Compile tested
only.

---

 b/arch/x86/kernel/fpu/core.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff -puN arch/x86/kernel/fpu/core.c~fpu-flush-thread-fpregs-lock arch/x86/kernel/fpu/core.c
--- a/arch/x86/kernel/fpu/core.c~fpu-flush-thread-fpregs-lock	2026-11-19 12:52:10.079375387 -0800
+++ b/arch/x86/kernel/fpu/core.c	2026-11-19 13:03:51.610146974 -0800
@@ -824,6 +824,12 @@ static void fpu_reset_fpstate_regs(void)
 	fpregs_lock();
 	__fpu_invalidate_fpregs_state(fpu);
 	/*
+	 * Resetting the fpstate is only safe with preemption disabled.
+	 * Otherwise a context switch could save the registers into it
+	 * halfway through. Do it here, under the lock, not in the caller.
+	 */
+	fpstate_reset(fpu);
+	/*
 	 * This does not change the actual hardware registers. It just
 	 * resets the memory image and sets TIF_NEED_FPU_LOAD so a
 	 * subsequent return to usermode will reload the registers from the
@@ -882,7 +888,6 @@ void fpu_flush_thread(void)
 	struct fpu *fpu = x86_task_fpu(current);
 	struct fpstate *old_fps = fpu->fpstate;
 
-	fpstate_reset(fpu);
 	fpu_reset_fpstate_regs();
 
 	/*
_
