Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp881541imm; Thu, 31 May 2018 11:02:10 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLzvXjEcgf4xhvkD9LAUDQw9mzJGCCEdIXruPOALUaXcTlGVq+mQg0F1uW/z7F2EFUZw6nR X-Received: by 2002:a17:902:28ab:: with SMTP id f40-v6mr7965258plb.208.1527789730928; Thu, 31 May 2018 11:02:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527789730; cv=none; d=google.com; s=arc-20160816; b=Tos01nZHgUqCTanPv27+nmYMP3X6vyoUXQ6Lsh3YGLF0K1ovz0We1p59fGd9QoJr9b lGlIfoplnS+5LYBSYBQgpZRcQV7Obe9y0MaESSsRMqA8nEODFGzMoikdBxBWGkJzozK9 gOHCwQLWa0QyOqdfe3b0DeyjAwjTnGMMD9/PNhTiHtbTnpxGhYt6R9vsEMzXqRl76fya uHCHLfows5L4DxA8lQPum0uEcGhim+lmPpXbxc8p236Gj6zGyr2jU0edt00QC8W/fPwo pf+iP5/lK3fSpVanyp1c+4gt/VrHgT3PGpSvgSwj6WzA4nF66SKalRU4+yZd3ZAJnfI1 JvNg== 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:arc-authentication-results; bh=bQVm8Q/DiPAdLVGsJIU14l3c0+USVSwHotLkZpf+c4c=; b=z/gFbkhKTZ6KvPBhOumUpTmbq2Tg02dOd79HFscTnqeQk9uYFloa/g8XiqxIYosqvz MniWKMoSLLDKRYPTzXyFkyPg/Vu9qMdWi09IhJKkxp4EmeupildOLpuElAxOBOLY9ys9 /+uM+vWS3SC0e4YafebcyqaPnM1NmjnnrCMUt2moCQXC13DLVfd0J3lG30aEehbtBKN8 jxcTk61WBn4xTEpfyL9iOTTmaL+sj9B93sCbXvwX5idmHczBRhoa+QOIFvKNgwvj1hd/ tvlcE9DlsQ04XFmEyDJyR9OnwMt6dvbtblqNFmHOAAkl1A3nLrHcbFLK4Q+gBJH4TaAq r4uA== 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 r63-v6si36785547plb.366.2018.05.31.11.01.56; Thu, 31 May 2018 11:02:10 -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 S1755972AbeEaSAy (ORCPT + 99 others); Thu, 31 May 2018 14:00:54 -0400 Received: from mga01.intel.com ([192.55.52.88]:18552 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755974AbeEaR7E (ORCPT ); Thu, 31 May 2018 13:59:04 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 May 2018 10:58:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,463,1520924400"; d="scan'208";a="60725533" Received: from chang-linux-2.sc.intel.com ([10.3.52.139]) by orsmga001.jf.intel.com with ESMTP; 31 May 2018 10:58:58 -0700 From: "Chang S. Bae" To: Andy Lutomirski , "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar Cc: Andi Kleen , Dave Hansen , Markus T Metzger , "Ravi V . Shankar" , "Chang S . Bae" , linux-kernel@vger.kernel.org Subject: [PATCH V2 10/15] x86/fsgsbase/64: Preserve FS/GS state in __switch_to if FSGSBASE is on Date: Thu, 31 May 2018 10:58:40 -0700 Message-Id: <1527789525-8857-11-git-send-email-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1527789525-8857-1-git-send-email-chang.seok.bae@intel.com> References: <1527789525-8857-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 FS and GS bases 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. Signed-off-by: Andy Lutomirski [chang: 5~10% performance improvement on context switch micro- benchmark, when FS/GS base is actively switched.] Signed-off-by: Chang S. Bae Reviewed-by: Andi Kleen Cc: H. Peter Anvin Cc: Dave Hansen 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 8ba947f..55af719 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -234,8 +234,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) @@ -314,10 +324,22 @@ static __always_inline void load_seg_legacy(unsigned short prev_index, static __always_inline void load_fsgs(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 task_seg_base(struct task_struct *task, -- 2.7.4