Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp910030ima; Fri, 1 Feb 2019 12:58:56 -0800 (PST) X-Google-Smtp-Source: AHgI3IY70UZ17hBiawKBPqgj48j8Vi5QFm5uzD/m2jAMAuZdAwmlmnZaAOdqaAwyq8cn1qY4AmzK X-Received: by 2002:a63:4611:: with SMTP id t17mr3733954pga.119.1549054736170; Fri, 01 Feb 2019 12:58:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549054736; cv=none; d=google.com; s=arc-20160816; b=EZiA4/DUz0ER2Z93QyINAV00cnrbLCILqTcaFn4L6yD6IbrYMrawbQtsSxp/IFg3ea aETg7qvXmk1iEnd0qrqyg21fVzd20B0z/EPHB9GW+XocuTdFVPDUWeWRwQiWDqjVMugj cEekNPzEkQx2Z0zrGAPtarxKFavZeX2UkXVhLyVDpCEMcpqmqZ7nI/HV7uJwserSIvo4 HbF+P93Wthea7ZQTo3K2KBfn3gYyCImnwGbTgqrerMQWZTs8k5BhBvC9liqk0BLBw1vD ixR/AgT1CA4heYAY9KSjqr4JugTvt2EPWmclMfztaRow47fUFXYtM9J+Uoq6Oh1ibf6w /OoA== 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=KpikGJmZGm2Url8tqINcQ67z/Cml/wmd5uzj3LlkRjU=; b=ZOVPq6nnAqJ/nHSxQbN/d5Tl2qDrS/eAidTBwF5+DkVwVzCfhBtkQHWuOoBlYQyIeK W8VZ1uJLOJe5NoL/WkvN2OeVehTk5ei6WtV/jzYojgc1UrY6Zq4UNIK23cDQFXdLzJqs ux1K2vLvYSObrvHkasxDABMSIG+F4qdDcYFmTwG/u+Le69eAXYozlKKwYBsOUcA+y6te X+QauexKDD+C9Jej6Tt96rGhKASG9t0Gjgph6DulFvOO6xc9bTu+0AOeQ5CQI2aaKmOY 7mBo18PCoNHEiIaR2aJjo8onHwPoMfg10lWoVyZc+s6Y40hq+mMj8RMcrI1aFEEn7nq4 Oj+Q== 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 a2si8552760pfb.166.2019.02.01.12.58.40; Fri, 01 Feb 2019 12:58:56 -0800 (PST) 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 S1728997AbfBAUzt (ORCPT + 99 others); Fri, 1 Feb 2019 15:55:49 -0500 Received: from mga07.intel.com ([134.134.136.100]:49739 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729764AbfBAUy5 (ORCPT ); Fri, 1 Feb 2019 15:54:57 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Feb 2019 12:54:57 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,549,1539673200"; d="scan'208";a="140866362" Received: from chang-linux-2.sc.intel.com ([10.3.52.165]) by fmsmga004.fm.intel.com with ESMTP; 01 Feb 2019 12:54:56 -0800 From: "Chang S. Bae" To: Andy Lutomirski , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Andi Kleen Cc: Markus T Metzger , Ravi Shankar , "Chang S . Bae" , LKML Subject: [PATCH v5 06/13] x86/fsgsbase/64: Preserve FS/GS state in __switch_to() if FSGSBASE is on Date: Fri, 1 Feb 2019 12:53:12 -0800 Message-Id: <20190201205319.15995-8-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190201205319.15995-1-chang.seok.bae@intel.com> References: <20190201205319.15995-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 (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 ebc55ed31fe7..d8ade9530fdb 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