Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1228291ybp; Fri, 4 Oct 2019 11:21:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqyd2pAeizCjLG5DQTSC5wJwESF9amkTMuRe662BT4RKhKJL6aKNHikEEHXWem5mQkLvD65i X-Received: by 2002:a05:6402:2cb:: with SMTP id b11mr16990941edx.285.1570213305768; Fri, 04 Oct 2019 11:21:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570213305; cv=none; d=google.com; s=arc-20160816; b=WR6EmkRLNejmQHDjEvJtXuoICe1qYxfeaDiNx6JpycGfbNQSo5vkBjLGLCE5RsPabI FdlzhXt6zy/Nc3x+pOd9EdxgAHEYMDqzps6RXL5CPvyozQB8s6ZEcqPK2ZSVJhn0HU8c 6jNtBcchegprWdM2baD/JnBEfSX0jUzmgDMACL9tD+s4K6TNKJmPLZRU/UAecc0hCU0p B0a9hk7WhaoM1n21D3hEHuyUA78u6Zv9NHaCJzMvgvUSxv/fyKwTUicqaskEB7iYszE0 lF1BO4rp+rrNM+aFanp3RniFfqvHdkcDbmoeWsP5xsJF18IkD8tKlGOQo2+j0B387ni6 aOLQ== 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=4R0pXcibmNND94Gs5d7antH34TMzzkMIORlSnHGlXqc=; b=jyO9AoYIxgM+wyoCYk525EHpjw52pDVHiXgwcrwHA1F3wJPuj6qmPYGOcO4sAkEKSJ 81j0cxIUFLlFU4HPxB+8mRfffuG+1BOM2Q4In4TNubxd9xHiqQxPj1RO0qgv5QFeLsTR 23pA1m/msmCRmirE1iE+Dx2nvwAjSA6dGCQv0YJTj7wJ7LiCIocgnBNlvdqxIyAlBod1 5sKjQEcgxkk39PxJYcLinITQsP81J1hs7ysYUq+AenX+IemGRRT+7wHx3YT6LstBlAQ+ y0mmLnjk8UO7k9t3nQzqxpHOkvVLDZETVHf/TrLBdOWYoI/qu/BsDbKtidlkJ0WKm8XH zRAg== 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 e7si3727356edn.267.2019.10.04.11.21.21; Fri, 04 Oct 2019 11:21:45 -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 S2388811AbfJDSRM (ORCPT + 99 others); Fri, 4 Oct 2019 14:17:12 -0400 Received: from mga07.intel.com ([134.134.136.100]:15531 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388462AbfJDSRF (ORCPT ); Fri, 4 Oct 2019 14:17:05 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Oct 2019 11:17:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,257,1566889200"; d="scan'208";a="204394801" Received: from chang-linux-3.sc.intel.com ([172.25.66.185]) by orsmga002.jf.intel.com with ESMTP; 04 Oct 2019 11:17:04 -0700 From: "Chang S. Bae" 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 Subject: [PATCH v9 11/17] x86/fsgsbase/64: Use FSGSBASE in switch_to() if available Date: Fri, 4 Oct 2019 11:16:03 -0700 Message-Id: <1570212969-21888-12-git-send-email-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570212969-21888-1-git-send-email-chang.seok.bae@intel.com> References: <1570212969-21888-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, FS/GS base can be efficiently read and written 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 GS base 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 by a context switch benchmark that ran threads with different FS/GS base values (to the baseline 4.16). ] Signed-off-by: Andy Lutomirski 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 --- Changes from v8: * Rebased on the precedent helper changes; removed the interrupt condition check and IRQ disablement from save_fsgs() and x86_fsgsbase_load(). Changes from v7: * Used appropriate GS base read/write functions depending on interrupt conditions. This fixes the bug in v7. * Massaged changelog by Thomas * Used '[FS|GS] base' consistently, instead of '[FS|GS]BASE' --- 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 295aa0c..56c0e5b 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -200,8 +200,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 = x86_gsbase_read_cpu_inactive(); + } else { + save_base_legacy(task, task->thread.fsindex, FS); + save_base_legacy(task, task->thread.gsindex, GS); + } } #if IS_ENABLED(CONFIG_KVM) @@ -280,10 +290,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); + x86_gsbase_write_cpu_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.7.4