Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp3816344ima; Tue, 23 Oct 2018 11:45:07 -0700 (PDT) X-Google-Smtp-Source: ACcGV62uFnH5r8IFBZMOj1iOoWuigpalfsz1MczeTXMcyvb8ZuBJSgkawIvsFgl873NuyfTrUDRM X-Received: by 2002:a65:4cc3:: with SMTP id n3-v6mr48929039pgt.257.1540320307668; Tue, 23 Oct 2018 11:45:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540320307; cv=none; d=google.com; s=arc-20160816; b=bOnP4Mnc06aPEkf+r5pkSjOGDK/aC56S16PvMuUhLQt5WQ2CWNA+oSz6ffdDNHNOXq 81ScqMBGUHUJ2OuXmy3i51jZvztLxfsVGVX5xrZNPtwoEcKoAjyGYcC8Ge70DBXyU5AI 6e/CJqDZhC7V2yao6oGOHDEqvkMNfo4onWPzvDM2sgmpNJuRYZ5yGvDDZyYGO852+7yl DMy5k3FyqXsIYbsWBExxhLIOq57tPTR6276tA2k2KpIqcXHlrWd60dNngFwR9mneXLs6 BCjAKE47weUV4Y4MmqJkDIQF8Y48JlY7y4CYweAYk4hrwATtlWz2kCTUgIM6u9LPPHlP 1mDg== 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; bh=YKt4gXniAparZWfsOAthXfd3RDySyoSRsCj5P7OYSyo=; b=Kq2+98xotwtySOiUhmfOW9A+7TDHbR0ZbtfBATcIrkuT7PEC4Lz8eyKzHro6RUIDgE tzRQH31PfjR9dM82rAL6PrO5qvZkKUp17mSfxH1k0BcbWFwzVao7WOO9sE9K/ZpSbHxU Z91OylJsxCR2P13hx3uvc6ENR4H6Dh2f8BTaA0aYNQSTA//omapHRQnoAvc76dTB/QvL mmKdLXGyCXApmt7kaDRHGa8egB7lLAb9v4qOBvZl0sizF9WlR5fnWPDqrSLlHA3Cw6C3 J1P3j7wXDU4AOhPMSYE6Mfi4j/k9ApCK7ZsFRRzYWOWIOVrjnOwnvLavh07j0RI3WMzE H1tQ== 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 e7-v6si2187014pgn.82.2018.10.23.11.44.52; Tue, 23 Oct 2018 11:45:07 -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 S1728875AbeJXDIB (ORCPT + 99 others); Tue, 23 Oct 2018 23:08:01 -0400 Received: from mga05.intel.com ([192.55.52.43]:55080 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728862AbeJXDIB (ORCPT ); Tue, 23 Oct 2018 23:08:01 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Oct 2018 11:43:26 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,417,1534834800"; d="scan'208";a="243726580" Received: from chang-linux-2.sc.intel.com ([10.3.52.139]) by orsmga004.jf.intel.com with ESMTP; 23 Oct 2018 11:43:26 -0700 From: "Chang S. Bae" To: Ingo Molnar , Thomas Gleixner , Andy Lutomirski , "H . Peter Anvin" Cc: Andi Kleen , Dave Hansen , Markus T Metzger , Ravi Shankar , "Chang S . Bae" , LKML Subject: [v3 05/12] x86/fsgsbase/64: Preserve FS/GS state in __switch_to() if FSGSBASE is on Date: Tue, 23 Oct 2018 11:42:27 -0700 Message-Id: <20181023184234.14025-6-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181023184234.14025-1-chang.seok.bae@intel.com> References: <20181023184234.14025-1-chang.seok.bae@intel.com> 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, 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. 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 Cc: Dave Hansen --- 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 fcf18046c3d6..1d975cadc256 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -238,8 +238,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 = rd_inactive_gsbase(); + } else { + save_base_legacy(task, task->thread.fsindex, FS); + save_base_legacy(task, task->thread.gsindex, GS); + } } #if IS_ENABLED(CONFIG_KVM) @@ -318,10 +328,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); + wr_inactive_gsbase(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