Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp664396ybt; Wed, 8 Jul 2020 08:45:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxax9WiVMBVqEZ2r/k00I1VpluD6nMaqQUYPERiu2JIKdYDbtskZVMuy/wE+VA6MFqzQiDY X-Received: by 2002:aa7:cfc4:: with SMTP id r4mr32798031edy.22.1594223155823; Wed, 08 Jul 2020 08:45:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594223155; cv=none; d=google.com; s=arc-20160816; b=gbyANC8eCkDpFw/9SCSBQfzPGvWkan9tMIN6HwWYlQNJSMGO273TJGU/j6YH0ToFVJ MtLguo03WsHfQR3rN0Kfn3U1/p/kv3cajGW1uJcx9bdnLYVuZhPmg6ZZJ0XeUNXCin29 zxLyjiVMqn47O9c71teSUXbAkov+JsLvCHFD85WhpgkZsOqgSZ+1IiBH9Q01Rs8r2awc Cfs/zC/mouRaQ1QIVNxof2QFN8aVepUJCIqEiC5Tr0y0GkiBwCqgsLynfg7yBLyJFb6a UzQaaMDqP3hb0VeboIN0H4QTTLjSNyfPML5hgQ6UfN5gvtzTpO/LeYoMgsGET0I6HHo0 jrRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tLDx3ghBUHSJf++OKR1k21U0Sr3x1mdNXzt6QjV4nuk=; b=0ud4mrSRxW6MkleQQ4kKvwWqOVtLtjb7RX1tSKpAETVyTH2TsILpXNsQneWoRafOkk Tm7p/gDdmNk5Ti4gQXDCRoCbtQ/hZYx2k/ZvNK+95ullK0VyIfiLbU4P0WU8T2Dcb2RH gILjz6HK9g9kkuNCfpRTxDIhovrocmzrFxYCyST3CILkQTh3gthI6lXP54KLUkB7zsXo 4RD4VUwOAHZYaC4qk5k9DeGzQvODFUBHPwM6oa7FuMiV7h4kxI51g7eZ3aTdHPuhpuTy 8gBX5MW/ZGptleL66WujDUyHp3yVHXPrKg5ELwvqPbbbMVP1MHY8g9DTIt8JT9fkGxrL epoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ELAHhkgG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cb10si212573edb.420.2020.07.08.08.45.32; Wed, 08 Jul 2020 08:45:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ELAHhkgG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730637AbgGHPoX (ORCPT + 99 others); Wed, 8 Jul 2020 11:44:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:47842 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730403AbgGHPlZ (ORCPT ); Wed, 8 Jul 2020 11:41:25 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AA3FC20720; Wed, 8 Jul 2020 15:41:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594222885; bh=GEJJCDdVm8pUbVjvqKgHBBMZkXtSqzUnyt6nkVra9C4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ELAHhkgGJptcVpMd7kX0vSvpXs78iTXGMe1SuQJW5NUP30mn8+zduqdESZzIwAzEx MtzNTJ3j4mc5FGXWZo1Bj/+TGJ+TQf+GjIUgoh3wGZ9fps4asY5CiZCitf/uA62ARO kQO+YYdm7X6yd8sTXM1PdWmuyp/d7Dy0BKJPmH5k= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Petteri Aimonen , Borislav Petkov , Sasha Levin 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 Message-Id: <20200708154116.3199728-6-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200708154116.3199728-1-sashal@kernel.org> References: <20200708154116.3199728-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Petteri Aimonen [ 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 Signed-off-by: Borislav Petkov 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 --- 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(¤t->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