Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp717410ybm; Thu, 28 May 2020 13:19:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJys1aunHqnlewfribWLam7pDWzvyTKqAJr7DsfzbapZ3SmSpgONLaRMYdJcc4FXiowNtCft X-Received: by 2002:a17:906:7813:: with SMTP id u19mr4491810ejm.199.1590697163219; Thu, 28 May 2020 13:19:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590697163; cv=none; d=google.com; s=arc-20160816; b=g3ujRSAwf4Sj8+rlvpW3HArT4391+DUsZlzT292phekeoqJ4OZinID0EpS7d9CjnF8 GkACXGNmPfnYUhiQDHkc3fyGffPhyokWEGnugWQIYDZH1BZontF0E2+N/oSX2fqtAeiL pQRxKZZSrHLVm0GhgMAaCfOugnQV/+BipSowObRvpnJU79i9Q1XNQhywI2qqCx19dgBY UV94/W0T1uqTpN7fOA0xBY4Dn1vgQnJcM3cr3sJJ/usJdTHuGB1wxg1Th/3XAWgW8zfO qakYWlcb4bvLHqgLW/IFbLRLayYJ8JvcfMgJIwQe24fX51MqdmJPXM7RqsL6y8lSPmU9 qLJQ== 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=Z350rGSHVsOen+6jUbnYuAW/fl0okCRZKqCYekXkayA=; b=OFP6Dq8it1v1SZBjrhsPqIA4QgxXaXS0zolsIEtZU1l+gCDcesu8C0LvHEfVRWr4v8 XNgrri6U93rTZoZmWIqdmtAE3qWrgQhY3EAUeqlLHbYToFF5kJSdyPykPddK/njZbsXn EY3tNnGaZMoMi0sW9lp/y9/RQ3Vhbby0h7wWZ8KvVlQzyeh2CxgiuIVfDFIdYoMfE2lf G4Kkyx9pTa7ybe5xYHmXQWN0wVXsUh10feyDv8kX2E8HMvOLLZfCJkiHHNscTm2QGEtH rOdvBXdcpolz4sn8cp1UxvIJjrS6pibg3ImSkdce872Uw42Q49TsITpGIO0UAEI3dQXy V/8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="f8/4xF0M"; 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 fi8si3556088ejb.641.2020.05.28.13.18.58; Thu, 28 May 2020 13:19:23 -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="f8/4xF0M"; 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 S2407055AbgE1UOm (ORCPT + 99 others); Thu, 28 May 2020 16:14:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:41758 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2407011AbgE1UOW (ORCPT ); Thu, 28 May 2020 16:14:22 -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 186C2214F1; Thu, 28 May 2020 20:14:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590696861; bh=TPKI62BqBvZhsuUBfCwO33Stb+N1jFBOmDUnrHw33C0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f8/4xF0MgTlsClMU1iKlEI5LTrwokkIgX40gbSxCSGtBYjocm+sM/rRNlkZk5Vdco PWfgYjS0IqC3QDYErVCE3SqFl1A3uwMZ8fxjKEYseLjjerhqVPnQH/mDdLRm91EK7S XUJ7T7yyBHJJX8ZdH3jXLWnatzXMXgKZBGlvON1A= From: Sasha Levin To: tglx@linutronix.de, luto@kernel.org, ak@linux.intel.com Cc: corbet@lwn.net, mingo@redhat.com, bp@alien8.de, x86@kernel.org, shuah@kernel.org, gregkh@linuxfoundation.org, tony.luck@intel.com, chang.seok.bae@intel.com, dave.hansen@linux.intel.com, peterz@infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, jarkko.sakkinen@linux.intel.com, Ravi Shankar , "H . Peter Anvin" , Sasha Levin Subject: [PATCH v13 05/16] x86/process/64: Use FSBSBASE in switch_to() if available Date: Thu, 28 May 2020 16:13:51 -0400 Message-Id: <20200528201402.1708239-6-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200528201402.1708239-1-sashal@kernel.org> References: <20200528201402.1708239-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: Andy Lutomirski With the new FSGSBASE instructions, FS and GSABSE can be efficiently read and writen in __switch_to(). Use that capability to preserve the full state. This will enable user code to do whatever it wants with the new instructions without any kernel-induced gotchas. (There can still be architectural gotchas: movl %gs,%eax; movl %eax,%gs may change GSBASE if WRGSBASE was used, but users are expected to read the CPU manual before doing things like that.) This is a considerable speedup. It seems to save about 100 cycles per context switch compared to the baseline 4.6-rc1 behavior on a Skylake laptop. [ chang: 5~10% performance improvements were seen with a context switch benchmark that ran threads with different FS/GSBASE values (to the baseline 4.16). Minor edit on the changelog. ] [ tglx: Masaage changelog ] Signed-off-by: Andy Lutomirski Signed-off-by: Chang S. Bae Signed-off-by: Thomas Gleixner Reviewed-by: Andi Kleen Cc: Ravi Shankar Cc: H. Peter Anvin Link: https://lkml.kernel.org/r/1557309753-24073-8-git-send-email-chang.seok.bae@intel.com Signed-off-by: Sasha Levin --- arch/x86/kernel/process_64.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 0624689825a3..85c7f9cabde2 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -233,8 +233,18 @@ static __always_inline void save_fsgs(struct task_struct *task) { savesegment(fs, task->thread.fsindex); savesegment(gs, task->thread.gsindex); - save_base_legacy(task, task->thread.fsindex, FS); - save_base_legacy(task, task->thread.gsindex, GS); + if (static_cpu_has(X86_FEATURE_FSGSBASE)) { + /* + * If FSGSBASE is enabled, we can't make any useful guesses + * about the base, and user code expects us to save the current + * value. Fortunately, reading the base directly is efficient. + */ + task->thread.fsbase = rdfsbase(); + task->thread.gsbase = __rdgsbase_inactive(); + } else { + save_base_legacy(task, task->thread.fsindex, FS); + save_base_legacy(task, task->thread.gsindex, GS); + } } #if IS_ENABLED(CONFIG_KVM) @@ -313,10 +323,22 @@ static __always_inline void load_seg_legacy(unsigned short prev_index, static __always_inline void x86_fsgsbase_load(struct thread_struct *prev, struct thread_struct *next) { - load_seg_legacy(prev->fsindex, prev->fsbase, - next->fsindex, next->fsbase, FS); - load_seg_legacy(prev->gsindex, prev->gsbase, - next->gsindex, next->gsbase, GS); + if (static_cpu_has(X86_FEATURE_FSGSBASE)) { + /* Update the FS and GS selectors if they could have changed. */ + if (unlikely(prev->fsindex || next->fsindex)) + loadseg(FS, next->fsindex); + if (unlikely(prev->gsindex || next->gsindex)) + loadseg(GS, next->gsindex); + + /* Update the bases. */ + wrfsbase(next->fsbase); + __wrgsbase_inactive(next->gsbase); + } else { + load_seg_legacy(prev->fsindex, prev->fsbase, + next->fsindex, next->fsbase, FS); + load_seg_legacy(prev->gsindex, prev->gsbase, + next->gsindex, next->gsbase, GS); + } } static unsigned long x86_fsgsbase_read_task(struct task_struct *task, -- 2.25.1