linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Petteri Aimonen <jpa@git.mail.kapsi.fi>,
	Borislav Petkov <bp@suse.de>, Sasha Levin <sashal@kernel.org>
Subject: [PATCH AUTOSEL 5.7 06/30] x86/fpu: Reset MXCSR to default in kernel_fpu_begin()
Date: Wed,  8 Jul 2020 11:40:52 -0400	[thread overview]
Message-ID: <20200708154116.3199728-6-sashal@kernel.org> (raw)
In-Reply-To: <20200708154116.3199728-1-sashal@kernel.org>

From: Petteri Aimonen <jpa@git.mail.kapsi.fi>

[ Upstream commit 7ad816762f9bf89e940e618ea40c43138b479e10 ]

Previously, kernel floating point code would run with the MXCSR control
register value last set by userland code by the thread that was active
on the CPU core just before kernel call. This could affect calculation
results if rounding mode was changed, or a crash if a FPU/SIMD exception
was unmasked.

Restore MXCSR to the kernel's default value.

 [ bp: Carve out from a bigger patch by Petteri, add feature check, add
   FNINIT call too (amluto). ]

Signed-off-by: Petteri Aimonen <jpa@git.mail.kapsi.fi>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://bugzilla.kernel.org/show_bug.cgi?id=207979
Link: https://lkml.kernel.org/r/20200624114646.28953-2-bp@alien8.de
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 arch/x86/include/asm/fpu/internal.h | 5 +++++
 arch/x86/kernel/fpu/core.c          | 6 ++++++
 2 files changed, 11 insertions(+)

diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h
index 44c48e34d7994..00eac7f1529b0 100644
--- a/arch/x86/include/asm/fpu/internal.h
+++ b/arch/x86/include/asm/fpu/internal.h
@@ -619,6 +619,11 @@ static inline void switch_fpu_finish(struct fpu *new_fpu)
  * MXCSR and XCR definitions:
  */
 
+static inline void ldmxcsr(u32 mxcsr)
+{
+	asm volatile("ldmxcsr %0" :: "m" (mxcsr));
+}
+
 extern unsigned int mxcsr_feature_mask;
 
 #define XCR_XFEATURE_ENABLED_MASK	0x00000000
diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 12c70840980e4..cd8839027f66d 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -101,6 +101,12 @@ void kernel_fpu_begin(void)
 		copy_fpregs_to_fpstate(&current->thread.fpu);
 	}
 	__cpu_invalidate_fpregs_state();
+
+	if (boot_cpu_has(X86_FEATURE_XMM))
+		ldmxcsr(MXCSR_DEFAULT);
+
+	if (boot_cpu_has(X86_FEATURE_FPU))
+		asm volatile ("fninit");
 }
 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
 
-- 
2.25.1


  parent reply	other threads:[~2020-07-08 15:44 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-08 15:40 [PATCH AUTOSEL 5.7 01/30] drm/msm: fix potential memleak in error branch Sasha Levin
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 02/30] drm/msm/dpu: allow initialization of encoder locks during encoder init Sasha Levin
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 03/30] ima: extend boot_aggregate with kernel measurements Sasha Levin
2020-07-08 16:13   ` Mimi Zohar
2020-07-09  1:27     ` Sasha Levin
2020-11-29 13:17       ` Mimi Zohar
2020-12-01  0:21         ` Sasha Levin
2020-12-01  3:13           ` Mimi Zohar
2020-12-02 23:53             ` Sasha Levin
2020-12-11  3:10         ` Tyler Hicks
2020-12-11 11:01           ` Mimi Zohar
2020-12-11 17:46             ` James Bottomley
2020-12-13  2:22               ` Mimi Zohar
2020-12-28 19:28                 ` Ken Goldman
2020-12-29  2:01                   ` Mimi Zohar
2020-12-14 16:42             ` Tyler Hicks
2021-01-12 15:35               ` Tyler Hicks
2021-01-12 16:56                 ` Mimi Zohar
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 04/30] drm/exynos: Properly propagate return value in drm_iommu_attach_device() Sasha Levin
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 05/30] drm/exynos: fix ref count leak in mic_pre_enable Sasha Levin
2020-07-08 15:40 ` Sasha Levin [this message]
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 07/30] exfat: Set the unused characters of FileName field to the value 0000h Sasha Levin
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 08/30] exfat: call sync_filesystem for read-only remount Sasha Levin
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 09/30] thermal/drivers: imx: Fix missing of_node_put() at probe time Sasha Levin
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 10/30] ACPI: DPTF: Add battery participant for TigerLake Sasha Levin
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 11/30] blk-mq-debugfs: update blk_queue_flag_name[] accordingly for new flags Sasha Levin
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 12/30] m68k: nommu: register start of the memory with memblock Sasha Levin
2020-07-08 15:40 ` [PATCH AUTOSEL 5.7 13/30] m68k: mm: fix node memblock init Sasha Levin

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=20200708154116.3199728-6-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=bp@suse.de \
    --cc=jpa@git.mail.kapsi.fi \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.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;
as well as URLs for NNTP newsgroup(s).