Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp549451ybk; Sat, 9 May 2020 10:39:11 -0700 (PDT) X-Google-Smtp-Source: APiQypK9F1EonGc/17wp3DeBRrY2T8rByvRN5bdYAvy2gpseYLZJL21i3hpu3tDRAm3yAS1YypEm X-Received: by 2002:a17:906:9450:: with SMTP id z16mr6729795ejx.166.1589045951072; Sat, 09 May 2020 10:39:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589045951; cv=none; d=google.com; s=arc-20160816; b=pidi0WsYndOn8AvGREywljkyszWrfNGD0SD9BvIbcAFjZ711lmcPZ6f269Bv5mNmao ST0rCbhLm9M7eqDnmlTD3nZn0yzLKbAADNzIAfiLMRKhFyWHv2+9NOLY/tkeaQJGbGFo sE7IrrHTJlCDObMoqI2zxgCFEknRw/zlTYmkHM7Yiv/JPlf1LxL5Y+Rp5u91WimpC7mC iuYbIp39aYrmiCujqtnQVoWb00qkLtZHBlJ07C1Yqrua0smQvZR1t9MA2P8NMssqUF7c GeyOIU28u/Lf9FY+8TjgsNoJkXvF9GWiOYBbmvgrLEU1zVB6SIFWvVakWr5sCdXf9jEP huLQ== 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 :dkim-signature; bh=gWYq4lmVWwpAUdSfaRInq2nL2gakTxrotMVf+tlcxZ8=; b=AuQBqE7k0UQOlyFbcQAJzQ2GMG1+QErvsd1ZqS2RvZWnwlRFVdktnRoDrZoaqaaFBN QmHDUrcGZoQJ0q1CLm7Wxl3LEpmMugGJAaeI9bdevMRffWjvjpdZ/XFQwky78TEly+Bp zGlc4WK3QHyXkrW2D3n0hDGDvYa/qoOPF8PHVarYkeTwgMNcrv8vZJuEFJzITD09mkuI 6ow3J9/FbKsql/PfaQNGlhptO+B2b63kH9jftCN/vDq3mC9BQdtGUUwbYLoEcJ3MrbDR gECHHRA4Gwc/lngFd451CVA98AdGCxkPZstoF9E030+dQjic6DS2j8AkTO83uYuGcoVa 5eSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=hxzhRnhX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s8si2861032edw.484.2020.05.09.10.38.48; Sat, 09 May 2020 10:39:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=hxzhRnhX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728620AbgEIRhT (ORCPT + 99 others); Sat, 9 May 2020 13:37:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:54586 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728579AbgEIRhQ (ORCPT ); Sat, 9 May 2020 13:37:16 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4749420A8B; Sat, 9 May 2020 17:37:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589045835; bh=/Y6JDEC90w0GBqbdeWrZTksp0NgWDLpS8ypWQddiY5s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hxzhRnhXhtQM6aIIKoTpf3aoCQ6MsdQvBTCvJKlppbU3SoSYsKh/EUeg0F2RnHl8p df7jC2YJaZjYrEkvQvcIyyXI+M/Ue4xTB1WeoKli7/ANYUQ4HGNGUaVohQysRtg2jn fwCjwZCKyz1e+1xIdXFVj7kmjXsLC0Yb+wdiNGfY= From: Sasha Levin 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, Sasha Levin Subject: [PATCH v11 11/18] x86/fsgsbase/64: Use FSGSBASE in switch_to() if available Date: Sat, 9 May 2020 13:36:48 -0400 Message-Id: <20200509173655.13977-12-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200509173655.13977-1-sashal@kernel.org> References: <20200509173655.13977-1-sashal@kernel.org> 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, 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 Signed-off-by: Sasha Levin 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 --- 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 aaa65f284b9b9..e066750be89a0 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -199,8 +199,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) @@ -279,10 +289,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.20.1