mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Chang S. Bae" <chang.seok.bae@intel.com>
To: Guixiong Wei <weiguixiong@bytedance.com>, <x86@kernel.org>
Cc: <tglx@kernel.org>, <mingo@redhat.com>, <bp@alien8.de>,
	<dave.hansen@linux.intel.com>, <hpa@zytor.com>,
	<shuah@kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-kselftest@vger.kernel.org>, <stable@vger.kernel.org>
Subject: Re: [PATCH v3 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec()
Date: Thu, 29 Oct 2026 13:16:04 -0700	[thread overview]
Message-ID: <eac354da-21f1-4663-84b0-74dab1459fa9@intel.com> (raw)
In-Reply-To: <f6c7690f8288ad9e95996e0cb37fc4c385df9ee1.1793072271.git.weiguixiong@bytedance.com>

On 10/26/2026 8:44 PM, Guixiong Wei wrote:
>   
>   void fpstate_reset(struct fpu *fpu)
>   {
> +	/* Free dynamic fpstate (if any) before resetting pointer: */
> +	fpstate_free(fpu);
> +

Did you check Sashiko review there?
https://sashiko.dev/#/patchset/cover.1793072271.git.weiguixiong%40bytedance.com

The point looks valid to me.

I think one way to fix it is to set TIF_NEED_FPU_LOAD in
fpu_flush_thread() before the reset. Maybe something like:

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index d1aeecd57f5e..f474fc357281 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -592,8 +592,15 @@ static void __fpstate_reset(struct fpstate *fpstate)
         fpstate->user_xfeatures = fpu_user_cfg.default_features;
  }

+/*
+ * Callers resetting the current task must ensure that its FPU register 
state
+ * isn't saved on context switch as the buffer may be freed here.
+ */
  void fpstate_reset(struct fpu *fpu)
  {
+       /* Free dynamic fpstate (if any) before resetting pointer: */
+       fpstate_free(fpu);
+
         /* Set the fpstate pointer to the default fpstate */
         fpu->fpstate = &fpu->__fpstate;
         __fpstate_reset(fpu->fpstate);
@@ -670,6 +677,8 @@ int fpu_clone(struct task_struct *dst, u64 
clone_flags, bool minimal,
         /* The new task's FPU state cannot be valid in the hardware. */
         dst_fpu->last_cpu = -1;

+       /* Break the link to the other FPU's fpstate: */
+       dst_fpu->fpstate = NULL;
         fpstate_reset(dst_fpu);

         if (!cpu_feature_enabled(X86_FEATURE_FPU))
@@ -866,6 +875,9 @@ void fpu__clear_user_states(struct fpu *fpu)

  void fpu_flush_thread(void)
  {
+       /* Prevent the hardware state from being saved on context switch */
+       set_thread_flag(TIF_NEED_FPU_LOAD);
+
         fpstate_reset(x86_task_fpu(current));
         fpu_reset_fpstate_regs();

Thanks,
Chang

  parent reply	other threads:[~2026-10-29 20:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27  3:44 [PATCH v3 0/2] x86/fpu: Fix dynamic fpstate leak on exec() Guixiong Wei
2026-10-27  3:44 ` [PATCH v3 1/2] x86/fpu: Fix memory leak with dynamic fpstate and exec() Guixiong Wei
2026-10-27  3:45   ` Guixiong Wei
2026-10-29 20:16   ` Chang S. Bae [this message]
2026-10-27  3:44 ` [PATCH v3 2/2] selftests/x86/amx: Test dynamic fpstate cleanup across exec() Guixiong Wei
2026-10-27  3:45   ` Guixiong Wei
2026-10-27  3:45 ` [PATCH v3 0/2] x86/fpu: Fix dynamic fpstate leak on exec() Guixiong Wei
2026-10-29 20:15 ` Chang S. Bae

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=eac354da-21f1-4663-84b0-74dab1459fa9@intel.com \
    --to=chang.seok.bae@intel.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=shuah@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@kernel.org \
    --cc=weiguixiong@bytedance.com \
    --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®