mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dave Hansen <dave.hansen@intel.com>
To: Marco Elver <elver@google.com>,
	syzbot <syzbot+b07ec23d35be305855be@syzkaller.appspotmail.com>
Cc: arnd@arndb.de, binbin.wu@linux.intel.com, bp@alien8.de,
	chao.gao@intel.com, dapeng1.mi@linux.intel.com,
	dave.hansen@linux.intel.com, ebiggers@kernel.org, hpa@zytor.com,
	linux-kernel@vger.kernel.org, mingo@redhat.com,
	seanjc@google.com, syzkaller-bugs@googlegroups.com,
	tglx@kernel.org, x86@kernel.org, "Bae,
	Chang Seok" <chang.seok.bae@intel.com>
Subject: Re: [syzbot] [kernel?] KCSAN: data-race in fpu_flush_thread / skb_copy_bits
Date: Thu, 19 Nov 2026 13:11:05 -0800	[thread overview]
Message-ID: <cf3c512d-5b02-46fa-b1d3-a1a10a1f3a8d@intel.com> (raw)
In-Reply-To: <CANpmjNOBWLCJZsyc2WZoOaF8QOj8a+yE0Puf6skiLNbjUrANjA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 172 bytes --]

I'm not fond of the changelog or comments as-is, but the attached fix
looks sane-adjacent to me.

Chang, any chance you'd want to dig in and make sure this is a real issue?

[-- Attachment #2: [PATCH 3/4] x86/fpu: Reset the fpstate on exec() under fpregs_lock().eml --]
[-- Type: message/rfc822, Size: 4290 bytes --]

From: Dave Hansen <dave.hansen@linux.intel.com>
To: dave.hansen@intel.com
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Subject: [PATCH 3/4] x86/fpu: Reset the fpstate on exec() under fpregs_lock()
Date: Thu, 19 Nov 2026 13:07:33 -0800
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();
 
 	/*
_

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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 20:26 syzbot
2026-11-19 20:53 ` Marco Elver
2026-11-19 21:11   ` Dave Hansen [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=cf3c512d-5b02-46fa-b1d3-a1a10a1f3a8d@intel.com \
    --to=dave.hansen@intel.com \
    --cc=arnd@arndb.de \
    --cc=binbin.wu@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=chang.seok.bae@intel.com \
    --cc=chao.gao@intel.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=ebiggers@kernel.org \
    --cc=elver@google.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=seanjc@google.com \
    --cc=syzbot+b07ec23d35be305855be@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®