From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 395B0C433DF for ; Tue, 16 Jun 2020 09:53:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D1AFF20767 for ; Tue, 16 Jun 2020 09:53:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="FEaeSamp" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727953AbgFPJxt (ORCPT ); Tue, 16 Jun 2020 05:53:49 -0400 Received: from mail.skyhub.de ([5.9.137.197]:55442 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725911AbgFPJxt (ORCPT ); Tue, 16 Jun 2020 05:53:49 -0400 Received: from zn.tnic (p200300ec2f0f4c0030193e5e894dac2c.dip0.t-ipconnect.de [IPv6:2003:ec:2f0f:4c00:3019:3e5e:894d:ac2c]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 2DC321EC0284; Tue, 16 Jun 2020 11:53:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1592301228; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:in-reply-to:references; bh=rOV5dWY1hwX8A8h8h/o4gakUbHaT5FDjfbK9K8CtYAY=; b=FEaeSampGLvuc8K2hfxQPo9ZgKhy4pl/pjRca6n5d5/Aag6fc/HYoDA/NzTeUu3JGFE5xT sJuNVYd7Vgg2ty1POhzUPlRaZO6jduNkIaBuhHXLfzmDCDOjIDyLdeZaKD8Yy61/J1EOdm /AWKVT+edRBsTOTp3JAi1T6G7M1fRts= Date: Tue, 16 Jun 2020 11:53:40 +0200 From: Borislav Petkov To: x86-ml , jpa@kernelbug.mail.kapsi.fi Cc: Dave Hansen , "H. Peter Anvin" , Sebastian Andrzej Siewior , lkml Subject: [PATCH] x86/fpu: Reset MXCSR to default in kernel_fpu_begin() Message-ID: <20200616095340.GD13515@zn.tnic> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Ok, here's the fix first so that it goes in. I'll hammer on the test case later. --- From: Petteri Aimonen 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. ] Signed-off-by: Petteri Aimonen Signed-off-by: Borislav Petkov Link: https://bugzilla.kernel.org/show_bug.cgi?id=207979 --- arch/x86/include/asm/fpu/internal.h | 5 +++++ arch/x86/kernel/fpu/core.c | 3 +++ 2 files changed, 8 insertions(+) diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h index 42159f45bf9c..845e7481ab77 100644 --- a/arch/x86/include/asm/fpu/internal.h +++ b/arch/x86/include/asm/fpu/internal.h @@ -623,6 +623,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 06c818967bb6..f398fedc590a 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -101,6 +101,9 @@ void kernel_fpu_begin(void) copy_fpregs_to_fpstate(¤t->thread.fpu); } __cpu_invalidate_fpregs_state(); + + if (boot_cpu_has(X86_FEATURE_XMM)) + ldmxcsr(MXCSR_DEFAULT); } EXPORT_SYMBOL_GPL(kernel_fpu_begin); -- 2.21.0 -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette