Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp984054ima; Wed, 24 Oct 2018 12:17:39 -0700 (PDT) X-Google-Smtp-Source: AJdET5euTBfGFEqXbbU9VWo0SZuGUd4OyMiVkMk/FGXa8Vv6iOqUqmsNRcHkCUf9yWMDAt4DMy/9 X-Received: by 2002:a63:f357:: with SMTP id t23-v6mr3611967pgj.402.1540408659489; Wed, 24 Oct 2018 12:17:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540408659; cv=none; d=google.com; s=arc-20160816; b=s78sjEYzk5zKy0kHsWBALTaXsV7biRYYYRsZBaH/eMap5OHmtcZi5XkgL4+Oe22tRa owTR/4fSsuY/WrL9zUhTa7rzbtD0TNXjoRUWNu4xm5CQkVw/zVY3PBGqC8HfQ2QVUv3f tWhGglF4BXHSLdHIgECovUpBgzS690E6ua2XPb7dw3m1BDL9CJ3/CzUGOHkxAiLYSX6O nV/o/ZY9XyWkbYZ5v/Fwkzc8pnsUnZTU8wK8fGWBDxL9rOw9sjBSDP5mXAEmg0j8PbrV wtuClW3K9fJ9jM0ZpMxjJ+zUbaoLCnq3as6AMDWGh6oLS/iA4BnrUn6PHnod8MSR06Ko lGRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=aUWvIrrKwRmAr/YduAVUiqwHNeNzRcpKO6VGmWfzolo=; b=pKrcJhFmdvcJTRRxzF2FG0kuo/GN72RAVQ2i0I4z8eVGiy3uESsUZUU3opSVdQ9WK/ cAAWCIY9jHhTBYc3Adsxq4XZWQMYnwQoDOp+dz7yW+ce5iIMGtgqoh94dNd8Bk3bAKCf URZxR+GBnd8qJoYI7aWRWQgFKVlozrD7666NAVUQjQsvtC5ElxZt5fYyOvfBY/dSwTT6 W3QaG9ozh0pY477vBaZK3xBsOhY/F9bGYkqZkhLVyAOJWienmPv/g7PU3zQiADlKg/mi KalkJTB8uASLoWhs7mfHG8MZPXskCFzNUPrjZw9m5eET8lpV7kdrhuvdxXSvXrEAuG/a dj4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="f1Q/3yNr"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z1-v6si6037726pfc.11.2018.10.24.12.17.23; Wed, 24 Oct 2018 12:17:39 -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; dkim=pass header.i=@kernel.org header.s=default header.b="f1Q/3yNr"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726839AbeJYDqM (ORCPT + 99 others); Wed, 24 Oct 2018 23:46:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:51090 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726433AbeJYDqM (ORCPT ); Wed, 24 Oct 2018 23:46:12 -0400 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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 2BD5B2084A for ; Wed, 24 Oct 2018 19:16:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1540408614; bh=6Jj0roqAEQx2ASP0o35natrHoQiitq11zKCVsJwB8JA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=f1Q/3yNr31lbZsnbGPGQFbD9Fig7yXhLzvxHtMZP33c57AAttwUmbAoeI3dEs0W9Y YVz/dBxNeRMGYTTQAc1/54i8PwDC2xshnJlkLTCN3EJvfdXTItV455SLhbje1WFptx ZSfRtSDzd7jJDu1KlPMBlV2qLFuDrsyGVVGjBO38= Received: by mail-wr1-f41.google.com with SMTP id q6-v6so6782512wrw.11 for ; Wed, 24 Oct 2018 12:16:54 -0700 (PDT) X-Gm-Message-State: AGRZ1gJgkEku9vOyPloul5QKpSGMoLwikEPFuyLxPplxd1BzDTyPwlIV w6qhrwPyVTaSF4miaSNgD1mf+4Xsictkgps6KaNFFw== X-Received: by 2002:adf:b1db:: with SMTP id r27-v6mr1049320wra.95.1540408612532; Wed, 24 Oct 2018 12:16:52 -0700 (PDT) MIME-Version: 1.0 References: <20181023184234.14025-1-chang.seok.bae@intel.com> <20181023184234.14025-5-chang.seok.bae@intel.com> In-Reply-To: <20181023184234.14025-5-chang.seok.bae@intel.com> From: Andy Lutomirski Date: Wed, 24 Oct 2018 12:16:40 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [v3 04/12] x86/fsgsbase/64: Enable FSGSBASE instructions in the helper functions To: "Bae, Chang Seok" , Boris Ostrovsky , Juergen Gross , xen-devel Cc: Ingo Molnar , Thomas Gleixner , Andrew Lutomirski , "H. Peter Anvin" , Andi Kleen , Dave Hansen , "Metzger, Markus T" , "Ravi V. Shankar" , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 23, 2018 at 11:43 AM Chang S. Bae wrote: > > The helper functions will switch on faster accesses to FSBASE and GSBASE > when the FSGSBASE feature is enabled. > > Accessing user GSBASE needs a couple of SWAPGS operations. It is avoidable > if the user GSBASE is saved at kernel entry, being updated as changes, and > restored back at kernel exit. However, it seems to spend more cycles for > savings and restorations. Little or no benefit was measured from > experiments. > > Signed-off-by: Chang S. Bae > Reviewed-by: Andi Kleen > Cc: Any Lutomirski > Cc: H. Peter Anvin > Cc: Thomas Gleixner > Cc: Ingo Molnar > Cc: Dave Hansen > --- > arch/x86/include/asm/fsgsbase.h | 17 +++---- > arch/x86/kernel/process_64.c | 82 +++++++++++++++++++++++++++------ > 2 files changed, 75 insertions(+), 24 deletions(-) > > diff --git a/arch/x86/include/asm/fsgsbase.h b/arch/x86/include/asm/fsgsbase.h > index b4d4509b786c..e500d771155f 100644 > --- a/arch/x86/include/asm/fsgsbase.h > +++ b/arch/x86/include/asm/fsgsbase.h > @@ -57,26 +57,23 @@ static __always_inline void wrgsbase(unsigned long gsbase) > : "memory"); > } > > +#include > + > /* Helper functions for reading/writing FS/GS base */ > > static inline unsigned long x86_fsbase_read_cpu(void) > { > unsigned long fsbase; > > - rdmsrl(MSR_FS_BASE, fsbase); > + if (static_cpu_has(X86_FEATURE_FSGSBASE)) > + fsbase = rdfsbase(); > + else > + rdmsrl(MSR_FS_BASE, fsbase); > > return fsbase; > } > > -static inline unsigned long x86_gsbase_read_cpu_inactive(void) > -{ > - unsigned long gsbase; > - > - rdmsrl(MSR_KERNEL_GS_BASE, gsbase); > - > - return gsbase; > -} > - > +extern unsigned long x86_gsbase_read_cpu_inactive(void); > extern void x86_fsbase_write_cpu(unsigned long fsbase); > extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase); > > diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c > index 31b4755369f0..fcf18046c3d6 100644 > --- a/arch/x86/kernel/process_64.c > +++ b/arch/x86/kernel/process_64.c > @@ -159,6 +159,36 @@ enum which_selector { > GS > }; > > +/* > + * Interrupts are disabled here. Out of line to be protected from kprobes. > + */ > +static noinline __kprobes unsigned long rd_inactive_gsbase(void) > +{ > + unsigned long gsbase, flags; > + > + local_irq_save(flags); > + native_swapgs(); > + gsbase = rdgsbase(); > + native_swapgs(); > + local_irq_restore(flags); > + > + return gsbase; > +} Please fold this into its only caller and make *that* noinline. Also, this function, and its "write" equivalent, will access the *active* gsbase. So it either needs to be fixed for Xen PV or some clear comment and careful auditing needs to be added to ensure that it's not used on Xen PV. Or it needs to be renamed native_x86_fsgsbase_... and add paravirt hooks, since Xen PV allows a very efficient but different implementation, I think. The latter is probably the right solution. (Hi Xen people -- how does CR4.FSGSBASE work on Xen? Is it always set? Never set? Set only if the guest tries to set it?) > void x86_fsbase_write_cpu(unsigned long fsbase) > { > - /* > - * Set the selector to 0 as a notion, that the segment base is > - * overwritten, which will be checked for skipping the segment load > - * during context switch. > - */ > - loadseg(FS, 0); > - wrmsrl(MSR_FS_BASE, fsbase); > + if (static_cpu_has(X86_FEATURE_FSGSBASE)) { > + wrfsbase(fsbase); > + } else { > + /* > + * Set the selector to 0 as a notion, that the segment base is > + * overwritten, which will be checked for skipping the segment load > + * during context switch. > + */ > + loadseg(FS, 0); > + wrmsrl(MSR_FS_BASE, fsbase); > + } > } > > void x86_gsbase_write_cpu_inactive(unsigned long gsbase) > { > - /* Set the selector to 0 for the same reason as %fs above. */ > - loadseg(GS, 0); > - wrmsrl(MSR_KERNEL_GS_BASE, gsbase); > + if (static_cpu_has(X86_FEATURE_FSGSBASE)) { > + wr_inactive_gsbase(gsbase); > + } else { > + /* Set the selector to 0 for the same reason as %fs above. */ > + loadseg(GS, 0); > + wrmsrl(MSR_KERNEL_GS_BASE, gsbase); I still don't get what this code is trying to do. See other email. I think it will straight up crash the kernel on some CPUs, since writing 0 to %%gs will zero out the *active* base on some CPUs. I think that, if you really want some fancy optimization for the non-FSGSBASE case, you need to pull that out into the callers of these helpers.