Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2345685ybz; Thu, 23 Apr 2020 16:26:50 -0700 (PDT) X-Google-Smtp-Source: APiQypJfPc77qQR7yNwwsAq4sJLlraF0Gxr+eIr0/WWcs3ZZjOmlDXyo9uhbFk7zjkOQmIKMEcFQ X-Received: by 2002:a50:a0c7:: with SMTP id 65mr5081426edo.7.1587684410058; Thu, 23 Apr 2020 16:26:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587684410; cv=none; d=google.com; s=arc-20160816; b=J+coKXgbs+PlCPscVheir8qJOfozzgkc/R6mlmbCROs6n4g1nLU+ony+rLiMHj2vTf ZR+93tNPxX2xuPijQDKD+W8LHpiUPySEGp4kfqoEWr+4IzWrv/dqIj0fEnt0s4i0GE3c X+HJBWUzmt59xg/O3jVXvOJciVzCh93oYh16vWxlKUZzDXxw3O/dkGUsUjhM16eKBByj VPDCBRbRwQvuCQQn2pJ39Il7L9xf8/hznyWCmb1JHbfZwl28YYsMEdCmxCXc6TLmpE8P 4GZ6KrhBB3TSF6dNl36r3EYfxewoeCl1CKkkxRaipKltIKvg6k1vijhQXg9WiXux/rwY cPIw== 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=bFAFgXPuqOfuFK+gUVJqxTDl1K3HmQYHBGUY8wevKMY=; b=A/A4jGo+JRhQG010ucSpSmnBHc7MnxLmyOEdyP5jlZvdfmoRWE2T9d2FvXtM7g79fm ThSnu9VFgluDPe3Ng6c71pHoFnFWq7knWgOu1Lo5X0jYO96SJ3IQeHifnaeoFZBlxsuI 8eVXKsolp6SCq+lC4N/XhahXqNZgrCBrT/jzhasv3/1KcGUhSQEdJI8zBacAbA/Xan6J Tj1zyQrFfFnuLgBKe/+u29BVXtWMrmchwePF4Xo+PwMpnmz4Hnnpqn2SxSgHjZzXcW52 IotUTEqU5mP4Zx60JKw1komOtfTneKszo+8l6tCqisz/HbBTj+DYvELWvcTiKwAqpRtO 81jA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oNG+PUaZ; 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 f3si2067341edw.598.2020.04.23.16.26.24; Thu, 23 Apr 2020 16:26:50 -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=oNG+PUaZ; 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 S1728230AbgDWXXF (ORCPT + 99 others); Thu, 23 Apr 2020 19:23:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:59130 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729181AbgDWXW0 (ORCPT ); Thu, 23 Apr 2020 19:22:26 -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 57BA2214AF; Thu, 23 Apr 2020 23:22:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587684145; bh=g/GBnDliB//bhWBmJ6kKV3RtUiv2G0d9s8tpHD4NzUE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oNG+PUaZ9tAdUDABL/7C/hAbwbceqab1B0HVyXf616/jYns+37OxeeCXPpHDOTFy3 fvdScaXvsiQ1lhrMShs4AfZr2BnGRNUHmuks6ZQ2FQ1Jzakc2107y7qW7RedzLuBVm JDT8ITYSkdNrRIL/qkX9HrcAUTmXsYWZEttirM/U= From: Sasha Levin To: linux-kernel@vger.kernel.org, tglx@linutronix.de, bp@alien8.de, luto@kernel.org Cc: hpa@zytor.com, dave.hansen@intel.com, tony.luck@intel.com, ak@linux.intel.com, ravi.v.shankar@intel.com, chang.seok.bae@intel.com, Andrew Cooper , Sasha Levin Subject: [PATCH v10 10/18] x86/fsgsbase/64: Enable FSGSBASE instructions in helper functions Date: Thu, 23 Apr 2020 19:21:59 -0400 Message-Id: <20200423232207.5797-11-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200423232207.5797-1-sashal@kernel.org> References: <20200423232207.5797-1-sashal@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Chang S. Bae" Add CPU feature conditional FS/GS base access to the relevant helper functions. That allows accelerating certain FS/GS base operations in subsequent changes. Note, that while possible, the user space entry/exit GS base operations are not going to use the new FSGSBASE instructions. The reason is that it would require additional storage for the user space value which adds more complexity to the low level code and experiments have shown marginal benefit. This may be revisited later but for now the SWAPGS based handling in the entry code is preserved except for the paranoid entry/exit code. Suggested-by: Tony Luck Signed-off-by: Chang S. Bae Reviewed-by: Tony Luck Cc: Thomas Gleixner Cc: Borislav Petkov Cc: Andy Lutomirski Cc: H. Peter Anvin Cc: Dave Hansen Cc: Tony Luck Cc: Andi Kleen Cc: Andrew Cooper Signed-off-by: Sasha Levin --- arch/x86/include/asm/fsgsbase.h | 27 +++++++-------- arch/x86/kernel/process_64.c | 58 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 15 deletions(-) diff --git a/arch/x86/include/asm/fsgsbase.h b/arch/x86/include/asm/fsgsbase.h index fdd1177499b40..aefd53767a5d4 100644 --- a/arch/x86/include/asm/fsgsbase.h +++ b/arch/x86/include/asm/fsgsbase.h @@ -49,35 +49,32 @@ static __always_inline void wrgsbase(unsigned long gsbase) asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory"); } +#include + /* Helper functions for reading/writing FS/GS base */ static inline unsigned long x86_fsbase_read_cpu(void) { unsigned long fsbase; - rdmsrl(MSR_FS_BASE, fsbase); + if (static_cpu_has(X86_FEATURE_FSGSBASE)) + fsbase = rdfsbase(); + else + rdmsrl(MSR_FS_BASE, fsbase); return fsbase; } -static inline unsigned long x86_gsbase_read_cpu_inactive(void) -{ - unsigned long gsbase; - - rdmsrl(MSR_KERNEL_GS_BASE, gsbase); - - return gsbase; -} - static inline void x86_fsbase_write_cpu(unsigned long fsbase) { - wrmsrl(MSR_FS_BASE, fsbase); + if (static_cpu_has(X86_FEATURE_FSGSBASE)) + wrfsbase(fsbase); + else + wrmsrl(MSR_FS_BASE, fsbase); } -static inline void x86_gsbase_write_cpu_inactive(unsigned long gsbase) -{ - wrmsrl(MSR_KERNEL_GS_BASE, gsbase); -} +extern unsigned long x86_gsbase_read_cpu_inactive(void); +extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase); #endif /* CONFIG_X86_64 */ diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 5ef9d8f25b0e8..aaa65f284b9b9 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -328,6 +328,64 @@ static unsigned long x86_fsgsbase_read_task(struct task_struct *task, return base; } +unsigned long x86_gsbase_read_cpu_inactive(void) +{ + unsigned long gsbase; + + if (static_cpu_has(X86_FEATURE_FSGSBASE)) { + bool need_restore = false; + unsigned long flags; + + /* + * We read the inactive GS base value by swapping + * to make it the active one. But we cannot allow + * an interrupt while we switch to and from. + */ + if (!irqs_disabled()) { + local_irq_save(flags); + need_restore = true; + } + + native_swapgs(); + gsbase = rdgsbase(); + native_swapgs(); + + if (need_restore) + local_irq_restore(flags); + } else { + rdmsrl(MSR_KERNEL_GS_BASE, gsbase); + } + + return gsbase; +} + +void x86_gsbase_write_cpu_inactive(unsigned long gsbase) +{ + if (static_cpu_has(X86_FEATURE_FSGSBASE)) { + bool need_restore = false; + unsigned long flags; + + /* + * We write the inactive GS base value by swapping + * to make it the active one. But we cannot allow + * an interrupt while we switch to and from. + */ + if (!irqs_disabled()) { + local_irq_save(flags); + need_restore = true; + } + + native_swapgs(); + wrgsbase(gsbase); + native_swapgs(); + + if (need_restore) + local_irq_restore(flags); + } else { + wrmsrl(MSR_KERNEL_GS_BASE, gsbase); + } +} + unsigned long x86_fsbase_read_task(struct task_struct *task) { unsigned long fsbase; -- 2.20.1