[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH for-4.17 v3 1/2] amd/virt_ssbd: set SSBD at vCPU context switch


  • To: Andrew Cooper <Andrew.Cooper3@xxxxxxxxxx>
  • From: Roger Pau Monné <roger.pau@xxxxxxxxxx>
  • Date: Tue, 15 Nov 2022 10:33:40 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=U4KnyjQJsRd5YmQNLDBBEZ5PEaDR0ErchjihpQI52bQ=; b=airLqyd3n9FHFe37qVmYR7vSSelTXd+i5Q9elNKf0KcZ9h+QyTPaxY1M4vC1pIbinBhFXjBpjfyxnoZ2ZBAe2E7YprZ3cQV7qnngSgdPFYhasbeP0+b8674Xpt/nosfvuLXLWl1s1aOYN1Qh5/Z33da7W0ElZ2F+LS4Vaj3Rnb0/auZ6l1gIBmg9v/gDD4I1LYQNYHKxfmsltd9I9/ua4XgCtuiwN4wDEGa/K6LbpKJR/1EcDePU+hZMvfbtPNHfxK0uLeoU0cEJhrxHH2ZAmJMlcVhSMRDlxV2VjpBc0Lrjj3qJeCue0gnJtXs0/ih8ZYxoxrmkO0WV0xsfl4N30Q==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KmlJ0hjNJSipsC28jErdgmlv3WUSw+v7fgzxJ57E5iT3+FrBT4bT1vDB7at9rqVpnSvePZ6s8atQEzkNFftqwhhLu4Xtf1piiY4JLuOqYAudv7uiNKqwRn8oHTQQEZliU4nAXd0+6QVaOoOFNN8TZnXhNGuQ+f7fIf0yd98B1TL0tPFnc6SlFsPaKFc1mW4MwkfE3yMx4vXOdjgQYp3JiAAEDgvCzJvaL1r4ch0mMCyThRYMwJMPoWSXzudmHBNGXI9VJTMCmyoHL3roPB/0lG6jdUBQeXjxKVJL/FBF8qMn/2Mi80MffEiGX+dPmrUhe8hEZB4g28vyXTpDoOrtTA==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>, "Henry.Wang@xxxxxxx" <Henry.Wang@xxxxxxx>, George Dunlap <George.Dunlap@xxxxxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Julien Grall <julien@xxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>
  • Delivery-date: Tue, 15 Nov 2022 09:34:31 +0000
  • Ironport-data: A9a23:W8RYkK4PRC33weGQfw7L8AxRtOPGchMFZxGqfqrLsTDasY5as4F+v jQbUGuEMv7ZY2PxL9p1PNngoxgBvMfcn9VhQFE+/HxjHi5G8cbLO4+Ufxz6V8+wwm8vb2o8t plDNYOQRCwQZiWBzvt4GuG59RGQ7YnRGvynTraBYnoqLeNdYH9JoQp5nOIkiZJfj9G8Agec0 fv/uMSaM1K+s9JOGjt8B5mr9VU+4pwehBtC5gZkPKkS4QeH/5UoJMl3yZ+ZfiOQrrZ8RoZWd 86bpJml82XQ+QsaC9/Nut4XpWVTH9Y+lSDX4pZnc/DKbipq/0Te4Y5iXBYoUm9Fii3hojxE4 I4lWapc6+seFvakdOw1C3G0GszlVEFM0OevzXOX6aR/w6BaGpdFLjoH4EweZOUlFuhL7W5m9 6AfGB8sUEy/2uPq+LK7csRmg9ghFZy+VG8fkikIITDxK98DGMiGb4CUoNhS0XE3m9xEGuvYa 4wBcz1zYR/cYhpJfFAKFJY5m+TujX76G9FagAvN+exrvC6OnEooiOCF3Nn9I7RmQe1PmUm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Zr6sTYXBvT8LZyrnPDsqZpWAgjue0uy6IGsgG107X2A7yvkNDnW9DuA5eoOQoYewrEcw g+tX7IVYh90nw=
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On Mon, Nov 14, 2022 at 09:39:50PM +0000, Andrew Cooper wrote:
> On 03/11/2022 17:02, Roger Pau Monne wrote:
> > The current logic for AMD SSBD context switches it on every
> > vm{entry,exit} if the Xen and guest selections don't match.  This is
> > expensive when not using SPEC_CTRL, and hence should be avoided as
> > much as possible.
> >
> > When SSBD is not being set from SPEC_CTRL on AMD don't context switch
> > at vm{entry,exit} and instead only context switch SSBD when switching
> > vCPUs.  This has the side effect of running Xen code with the guest
> > selection of SSBD, the documentation is updated to note this behavior.
> > Also note that then when `ssbd` is selected on the command line guest
> > SSBD selection will not have an effect, and the hypervisor will run
> > with SSBD unconditionally enabled when not using SPEC_CTRL itself.
> >
> > This fixes an issue with running C code in a GIF=0 region, that's
> > problematic when using UBSAN or other instrumentation techniques.
> 
> This paragraph needs to be at the top, because it's the reason why this
> is a blocker bug for 4.17.  Everything else is discussing why we take
> the approach we take.

Done.

> (and to be clear, it's slow even with MSR_SPEC_CTRL.  It's just that its
> a whole lot less slow than with the LS_CFG MSR.)
> 
> >
> > As a result of no longer running the code to set SSBD in a GIF=0
> > region the locking of amd_set_legacy_ssbd() can be done using normal
> > spinlocks, and some more checks can be added to assure it works as
> > intended.
> >
> > Finally it's also worth noticing that since the guest SSBD selection
> > is no longer set on vmentry the VIRT_SPEC_MSR handling needs to
> > propagate the value to the hardware as part of handling the wrmsr.
> >
> > Signed-off-by: Roger Pau Monné <roger.pau@xxxxxxxxxx>
> > ---
> > Changes since v2:
> >  - Fix calling set_reg unconditionally.
> >  - Fix comment.
> >  - Call amd_set_ssbd() from guest_wrmsr().
> >
> > Changes since v1:
> >  - Just check virt_spec_ctrl value != 0 on context switch.
> >  - Remove stray asm newline.
> >  - Use val in svm_set_reg().
> >  - Fix style in amd.c.
> >  - Do not clear ssbd
> > ---
> >  docs/misc/xen-command-line.pandoc | 10 +++---
> >  xen/arch/x86/cpu/amd.c            | 55 +++++++++++++++++--------------
> >  xen/arch/x86/hvm/svm/entry.S      |  6 ----
> >  xen/arch/x86/hvm/svm/svm.c        | 45 ++++++++++---------------
> >  xen/arch/x86/include/asm/amd.h    |  2 +-
> >  xen/arch/x86/msr.c                |  9 +++++
> 
> Need to patch msr.h now that the semantics of virt_spec_ctrl have changed.

Sure, will adjust the comment there.

> 
> > diff --git a/xen/arch/x86/cpu/amd.c b/xen/arch/x86/cpu/amd.c
> > index 98c52d0686..05d72c6501 100644
> > --- a/xen/arch/x86/cpu/amd.c
> > +++ b/xen/arch/x86/cpu/amd.c
> > <snip>
> > +void amd_set_ssbd(bool enable)
> > +{
> > +   if (opt_ssbd)
> > +           /*
> > +            * Ignore attempts to turn off SSBD, it's hardcoded on the
> > +            * command line.
> > +            */
> > +           return;
> > +
> > +   if (cpu_has_virt_ssbd)
> > +           wrmsr(MSR_VIRT_SPEC_CTRL, enable ? SPEC_CTRL_SSBD : 0, 0);
> > +   else if (amd_legacy_ssbd)
> > +           core_set_legacy_ssbd(enable);
> > +   else
> > +           ASSERT_UNREACHABLE();
> 
> This assert is reachable on Fam14 and older, I think.

Hm, I'm unsure how.  Calls to this function are gated on the vCPU
having virt_ssbd set in the CPUID policy, and that can only happen if
there's a way to set SSBD.

Can you elaborate on the path that you think can trigger this?

As I would think that's the path that needs fixing, rather than
removing the assert here.

> > diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c
> > index 1aeaabcb13..8b101d4f27 100644
> > --- a/xen/arch/x86/hvm/svm/svm.c
> > +++ b/xen/arch/x86/hvm/svm/svm.c
> > @@ -973,6 +973,16 @@ static void cf_check svm_ctxt_switch_from(struct vcpu 
> > *v)
> >  
> >      /* Resume use of ISTs now that the host TR is reinstated. */
> >      enable_each_ist(idt_tables[cpu]);
> > +
> > +    /*
> > +     * Clear previous guest selection of SSBD if set.  Note that 
> > SPEC_CTRL.SSBD
> > +     * is already cleared by svm_vmexit_spec_ctrl.
> > +     */
> > +    if ( v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
> > +    {
> > +        ASSERT(v->domain->arch.cpuid->extd.virt_ssbd);
> > +        amd_set_ssbd(false);
> > +    }
> >  }
> >  
> >  static void cf_check svm_ctxt_switch_to(struct vcpu *v)
> > @@ -1000,6 +1010,13 @@ static void cf_check svm_ctxt_switch_to(struct vcpu 
> > *v)
> >  
> >      if ( cpu_has_msr_tsc_aux )
> >          wrmsr_tsc_aux(v->arch.msrs->tsc_aux);
> > +
> > +    /* Load SSBD if set by the guest. */
> > +    if ( v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
> > +    {
> > +        ASSERT(v->domain->arch.cpuid->extd.virt_ssbd);
> > +        amd_set_ssbd(true);
> > +    }
> 
> While this functions, it's still a perf problem.  You now flip the bit
> twice when switching between vcpus with legacy SSBD.
> 
> This wouldn't be so bad if you'd also fixed the inner function to not do
> a read/modify/write on the very slow MSR, because then we'd only be
> touching it twice, not 4 times.
> 
> This isn't critical to fix for 4.17, but will need addressing in due course.

Indeed.  I know about this, but didn't consider it critical enough to
fix in the current release status.

> However, as the patch does need a respin, amd_set_ssbd() is too
> generic.  It's previous name, amd_set_legacy_ssbd(), was more
> appropriate, as it clearly highlights the fact that it's the
> non-MSR_SPEC_CTRL path.

Can adjust the function name, not a problem.

Thanks, Roger.



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.