Received: by 2002:ac0:a874:0:0:0:0:0 with SMTP id c49csp744429ima; Fri, 15 Mar 2019 13:09:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqw7o2AzM58n+dJ5kjSPRSi1KTrb61NuZgP2uG1kXEsatpfTS8z3YwySVIAButZ9b9mVdk9M X-Received: by 2002:a63:c04a:: with SMTP id z10mr5287698pgi.115.1552680579031; Fri, 15 Mar 2019 13:09:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552680579; cv=none; d=google.com; s=arc-20160816; b=B99+TMkJgIhgITCk1XJPl1CEQmzkzwmwbgggWWOs9iA9F/YJChjNWBuCrm6hHRJ0ow 59c5tnGQG3zVstGEPLUmZxyGFhexYzXK/WaIw8GK22Kelo9QYJS4DRIQonGA3cAMXeQC UpI6MokRq3AEqYH2z5l1qmmpA/6+F8XYOFxekx/osUQBa7PQC+FbAl1j/wGGfYeft2OQ 1xI+HijPiv/EngJ/ZNtjv5cn7mfWjc3ykRF4Q9q1XDqMLJI4A1wLdCsknAlvD6g5Z5tx YwfYuOzc5BpoB7UZXisexxfhNcEpwiwLXzZDKfmuaHFWDC9u7KojqqKS/pxC1y90dQ/8 Dm7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=eGly8mJ6Rji6YQaZ4DV3IR4S2asG4uvq7uG3cyJCym4=; b=UDucuEmH+/eBMyE0Pp8RL4v+8GEmMLeMENxleGVb7NONCrY66f7PxvAbSo3ja0kDhG lOEInWCGIGtCNlzVkEIvazoPLuoeKCTo9qI68WT6qPOSnxH16DeKl1c98tKGj/WvSSf1 NQlB8nfsmeMoh1M2kBH2G/Z0mqbYXUSVvKtQuffEt8tNRvCfTJz6w2DAVnjWLah+Yh/y sD9vSplHRmZsYU4PavTGI2nCoIbCRx/e0lrqZhu0hyrmhvdcFIf8EBQ2l2pA97kKGGT4 KzK1ylC3vZn9Km2DgaQ/heHTgsuUtIRZqGuGqi5p6D4ZQwVIHLnufivkxhTlPohCV6np YPrQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l63si2670227pfj.53.2019.03.15.13.09.23; Fri, 15 Mar 2019 13:09:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726720AbfCOUHG (ORCPT + 99 others); Fri, 15 Mar 2019 16:07:06 -0400 Received: from mga01.intel.com ([192.55.52.88]:22898 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726551AbfCOUHD (ORCPT ); Fri, 15 Mar 2019 16:07:03 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Mar 2019 13:07:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,483,1544515200"; d="scan'208";a="307617704" Received: from chang-linux-3.sc.intel.com ([143.183.85.65]) by orsmga005.jf.intel.com with ESMTP; 15 Mar 2019 13:07:01 -0700 From: "Chang S. Bae" To: Thomas Gleixner , Ingo Molnar , Andy Lutomirski , "H . Peter Anvin" , Andi Kleen Cc: Ravi Shankar , "Chang S . Bae" , LKML Subject: [RESEND PATCH v6 05/12] x86/fsgsbase/64: Preserve FS/GS state in __switch_to() if FSGSBASE is on Date: Fri, 15 Mar 2019 13:06:38 -0700 Message-Id: <1552680405-5265-6-git-send-email-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1552680405-5265-1-git-send-email-chang.seok.bae@intel.com> References: <1552680405-5265-1-git-send-email-chang.seok.bae@intel.com> 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, we can efficiently read and write the FSBASE and GSBASE 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 my Skylake laptop. [ chang: 5~10% performance improvements were seen by a context switch benchmark that ran threads with different FS/GSBASE values (to the baseline 4.16). Minor edit on the changelog. ] Signed-off-by: Andy Lutomirski Signed-off-by: Chang S. Bae Reviewed-by: Andi Kleen Cc: H. Peter Anvin Cc: Thomas Gleixner Cc: Ingo Molnar --- 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 87b5ffce2a47..d3abe4748d8b 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -245,8 +245,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) @@ -325,10 +335,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.19.1