From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.19 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796073557; b=Xjj01txE16Q99h4aARrraa/eNGPSojIjvcq5bNlFX1xlWRlpCDXEJw4CC0MX1NBeS61O vb2bIUBtHJnOY7me5Sto2e3z3RD9AkNACCvfO06b5Bg++36VUhrxg3EXzuDmoA+bWyWUD +pwBxBJKwZlSfaWxJUGPVUhgQZDnuC0MZdpFA/9EdSv0iXELE5KidRlWSzPthGdK084jh N7db04VfcotBWrt0lQZZUIXvU1sVznGPrMe3HuBPEnZWiWZVJdnV488WdJ03ZDhGv9Be5 oFF62CRVyzZe/pDNglD3lbbq/kZokKk2XfjrPmeVgzq76giieKxXE24p2SYORmM10Hw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796073557; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=xRxNt9dW/X9nI0vt6lrAemkCNRKv606Ivl3yaaZSZag=; b=TfrvaZSSKPGBwCj2S5Yfsz1kLSG9cc0bkZNM/tecM39rnWJaGbg6o+tjX1GS6EI96qQs XdlcrmU6R4C8DNOT3NfG0wL0qNfhrfbd02b7MhlSW3d3M94F4Ktb4dy9FFdhLPpp3/aV2 b6TrAdMW96/AqtWyyQgC+ARDre+uXWA+/mx60+00bXmvE9ZsFEIznOx+aE9Lg91ufNlqU 2eI6j2DXga94Q81Pf3OieWXzYGvKJvXJbozUOtpsr01HzkRdHtvEw2QV7GXUI9yb3Rsuc HUC1UzRtW48R9zIzmGY0pCEKDrZqMrt1FoR+KPKcZg+9tcsmcR/sRLjiFZu0XTugRVA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=L4ZcPvI6; arc=none smtp.remote-ip=198.175.65.19 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 816AC606A32A; Mon, 30 Nov 2026 21:19:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 816AC606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=L4ZcPvI6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796073557; x=1827609557; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xRxNt9dW/X9nI0vt6lrAemkCNRKv606Ivl3yaaZSZag=; b=L4ZcPvI6S8cEUsawqRXf1RTLYcRhb2XcYkbWw/4Wv2FDanf3s7zjO70S eVIoQvXQqgJx4wHsnXdudhxbTByStzSKa7PUPmrLWzyusiIvP6fKVfoqS FKHdAc71tocFpAbPsff9LDbO3BftyMkqzB0a2QiSXsrVOgGMCRcpADEOb NJpGeFbwMR3BlSIc69tKn/3PBh5mSAY36qRKlcnBQ63bf3yf/Vahyb25N GOy4KW1muDjfOkXEzqG40mSgSwDOTezflPpJBEQA6psVLEQNVMFmjHvK0 yb3twt1gbpnDdBLW4J/E30wE2Tdk2PGurbkyEan2QlCeAyvCWpI7qldRy A==; X-CSE-ConnectionGUID: dJaSGyQ2SOqxWgiLJNCUCw== X-CSE-MsgGUID: lJjK0VmvT6WXY532g0X7GA== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4989752" X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="4989752" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 13:19:15 -0800 X-CSE-ConnectionGUID: sj1fAHf6TtuT5jfoVvMgTQ== X-CSE-MsgGUID: 2VLeErguSyeylgMTirht7A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="13166752" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.174]) by fmviesa005.fm.intel.com with ESMTP; 30 Nov 2026 13:19:14 -0800 From: "Chang S. Bae" To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, weiguixiong@bytedance.com, omermetekaya0@gmail.com, chang.seok.bae@intel.com, stable@vger.kernel.org Subject: [PATCH 2/6] x86/fpu: Refactor fpstate storage reset sequence Date: Mon, 30 Nov 2026 20:51:38 +0000 Message-ID: <20261130205143.1258369-3-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261130205143.1258369-1-chang.seok.bae@intel.com> References: <20261130205143.1258369-1-chang.seok.bae@intel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit fpu_clone() and fpu_flush_thread() both reset the fpstate register storage with the same sequence. Move it into one helper. fpu_clone() doesn't need the storage reset always but for now prepare for the next change, that moves the fpstate metadata reset to there and that will also make the storage reset optional. Signed-off-by: Chang S. Bae Cc: stable@vger.kernel.org --- Note for backporting, this prepares: Fixes: e61d6310a0f8 ("x86/fpu:Reset permission and fpstate on exec()") --- arch/x86/kernel/fpu/core.c | 77 ++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index bbe881cf44923..80847a08ff3bb 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -673,6 +673,38 @@ static int update_fpu_shstk(struct task_struct *dst, unsigned long ssp) return 0; } +/* + * Reset FPU register storage to the init values from: + * - fpu_clone() for new task's FPU state + * - fpu_flush_thread() for current + */ +static void fpu_reset_fpstate_regs(struct task_struct *tsk, struct fpu *fpu) +{ + if (tsk == current) + fpregs_lock(); + + /* + * This does not change the actual hardware registers. It just + * resets the memory image. Do not use fpstate_init() here. Just copy + * init_fpstate which has the correct content already except for PKRU. + * + * PKRU handling does not rely on the xstate when restoring for + * user space as PKRU is eagerly written in switch_to() and + * flush_thread(). + */ + memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size()); + __fpu_invalidate_fpregs_state(fpu); + + /* + * Subsequently enforce reload for user space tasks and prevent kernel + * threads from trying to save the FPU registers on context switch. + */ + set_tsk_thread_flag(tsk, TIF_NEED_FPU_LOAD); + + if (tsk == current) + fpregs_unlock(); +} + /* Clone current's FPU state on fork */ int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal, unsigned long ssp) @@ -688,27 +720,17 @@ int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal, BUILD_BUG_ON(sizeof(*dst) % SMP_CACHE_BYTES != 0); - /* The new task's FPU state cannot be valid in the hardware. */ - dst_fpu->last_cpu = -1; - fpstate_reset(dst_fpu); - /* - * Enforce reload for user space tasks and prevent kernel threads - * from trying to save the FPU registers on context switch. - */ - set_tsk_thread_flag(dst, TIF_NEED_FPU_LOAD); + /* Fill the new task FPU storage with init-state */ + fpu_reset_fpstate_regs(dst, dst_fpu); /* * No FPU state inheritance for kernel threads and IO * worker threads. */ - if (minimal) { - /* Clear out the minimal state */ - memcpy(&dst_fpu->fpstate->regs, &init_fpstate.regs, - init_fpstate_copy_size()); + if (minimal) return 0; - } /* * If a new feature is added, ensure all dynamic features are @@ -814,33 +836,6 @@ static inline void restore_fpregs_from_init_fpstate(u64 features_mask) pkru_write_default(); } -/* - * Reset current->fpu memory state to the init values. - */ -static void fpu_reset_fpstate_regs(void) -{ - struct fpu *fpu = x86_task_fpu(current); - - fpregs_lock(); - __fpu_invalidate_fpregs_state(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 - * task's memory image. - * - * Do not use fpstate_init() here. Just copy init_fpstate which has - * the correct content already except for PKRU. - * - * PKRU handling does not rely on the xstate when restoring for - * user space as PKRU is eagerly written in switch_to() and - * flush_thread(). - */ - memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size()); - set_thread_flag(TIF_NEED_FPU_LOAD); - fpregs_unlock(); -} - /* * Reset current's user FPU states to the init states. current's * supervisor states, if any, are not modified by this function. The @@ -880,7 +875,7 @@ void fpu__clear_user_states(struct fpu *fpu) void fpu_flush_thread(void) { fpstate_reset(x86_task_fpu(current)); - fpu_reset_fpstate_regs(); + fpu_reset_fpstate_regs(current, x86_task_fpu(current)); } /* * Load FPU context before returning to userspace. -- 2.53.0