Skip to contentSkip to conversation

amd/virt_ssbd: refactoring and fixes

PATCH Superseded Roger Pau Monne 4 participants 9 messages Nov 3, 2022 – Nov 15, 2022

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover amd/virt_ssbd: refactoring and fixes

Hello,

Just two patches remaining, and the last one is already Acked.

First patch deals with moving the switching of SSBD from guest vm{entry,exit} to vCPU context switch, and lets Xen run with the guest SSBD selection under some circumstances by default.

Thanks, Roger.

Roger Pau Monne (2):
amd/virt_ssbd: set SSBD at vCPU context switch
amd: remove VIRT_SC_MSR_HVM synthetic feature

 docs/misc/xen-command-line.pandoc      | 10 +++--
 xen/arch/x86/cpu/amd.c                 | 56 ++++++++++++++------------
 xen/arch/x86/cpuid.c                   |  9 +++--
 xen/arch/x86/hvm/svm/entry.S           |  6 ---
 xen/arch/x86/hvm/svm/svm.c             | 45 ++++++++-------------
 xen/arch/x86/include/asm/amd.h         |  3 +-
 xen/arch/x86/include/asm/cpufeatures.h |  2 +-
 xen/arch/x86/msr.c                     |  9 +++++
 xen/arch/x86/spec_ctrl.c               |  8 ++--
 9 files changed, 75 insertions(+), 73 deletions(-)
Signature · 1 line
2.37.3
#1 lore raw

PATCH v3 1/2 amd/virt_ssbd: set SSBD at vCPU context switch

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.

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.

---
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 +++++
 6 files changed, 63 insertions(+), 64 deletions(-)
docs/misc/xen-command-line.pandoc +6 -4
index 0fbdcb574f..424b12cfb2 100644
@@ -2372,10 +2372,12 @@ By default, Xen will use STIBP when IBRS is in use (IBRS implies STIBP), and
 when hardware hints recommend using it as a blanket setting.
 
 On hardware supporting SSBD (Speculative Store Bypass Disable), the `ssbd=`
-option can be used to force or prevent Xen using the feature itself.  On AMD
-hardware, this is a global option applied at boot, and not virtualised for
-guest use.  On Intel hardware, the feature is virtualised for guests,
-independently of Xen's choice of setting.
+option can be used to force or prevent Xen using the feature itself.  The
+feature is virtualised for guests, independently of Xen's choice of setting.
+On AMD hardware, disabling Xen SSBD usage on the command line (`ssbd=0` which
+is the default value) can lead to Xen running with the guest SSBD selection
+depending on hardware support, on the same hardware setting `ssbd=1` will
+result in SSBD always being enabled, regardless of guest choice.
 
 On hardware supporting PSFD (Predictive Store Forwarding Disable), the `psfd=`
 option can be used to force or prevent Xen using the feature itself.  By
xen/arch/x86/cpu/amd.c +30 -25

90 lines not on this page · raw message

xen/arch/x86/hvm/svm/entry.S +0 -6
index a26589aa9a..981cd82e7c 100644
@@ -59,9 +59,6 @@ __UNLIKELY_END(nsvm_hap)
 
         clgi
 
-        ALTERNATIVE "", STR(call vmentry_virt_spec_ctrl), \
-                        X86_FEATURE_VIRT_SC_MSR_HVM
-
         /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */
         /* SPEC_CTRL_EXIT_TO_SVM       Req: b=curr %rsp=regs/cpuinfo, Clob: acd */
         .macro svm_vmentry_spec_ctrl
@@ -131,9 +128,6 @@ __UNLIKELY_END(nsvm_hap)
         ALTERNATIVE "", svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
 
-        ALTERNATIVE "", STR(call vmexit_virt_spec_ctrl), \
-                        X86_FEATURE_VIRT_SC_MSR_HVM
-
         /*
          * STGI is executed unconditionally, and is sufficiently serialising
          * to safely resolve any Spectre-v1 concerns in the above logic.
xen/arch/x86/hvm/svm/svm.c +17 -28

63 lines not on this page · raw message

xen/arch/x86/include/asm/amd.h +1 -1
index 6a42f68542..81ed71710f 100644
@@ -153,6 +153,6 @@ void amd_check_disable_c1e(unsigned int port, u8 value);
 
 extern bool amd_legacy_ssbd;
 bool amd_setup_legacy_ssbd(void);
-void amd_set_legacy_ssbd(bool enable);
+void amd_set_ssbd(bool enable);
 
 #endif /* __AMD_H__ */
xen/arch/x86/msr.c +9 -0
index 95416995a5..5609b71e99 100644
@@ -24,6 +24,7 @@
 #include <xen/nospec.h>
 #include <xen/sched.h>
 
+#include <asm/amd.h>
 #include <asm/debugreg.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/hvm/viridian.h>
@@ -697,7 +698,15 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
                 msrs->spec_ctrl.raw &= ~SPEC_CTRL_SSBD;
         }
         else
+        {
             msrs->virt_spec_ctrl.raw = val & SPEC_CTRL_SSBD;
+            if ( v == curr )
+                /*
+                 * Propagate the value to hardware, as it won't be set on guest
+                 * resume path.
+                 */
+                amd_set_ssbd(val & SPEC_CTRL_SSBD);
+        }
         break;
 
     case MSR_AMD64_DE_CFG:
Signature · 1 line
2.37.3
#2 lore raw

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

Hi Roger,

Quoted 26 lines

-----Original Message-----
From: Roger Pau Monne <[email protected]>
Subject: [PATCH for-4.17 v3 1/2] amd/virt_ssbd: set SSBD at vCPU context switch

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.

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.

Quoted 5 lines

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.

Thanks for respinning the patch!

Kind regards,
Henry

Reviewed-by on v3 1/2
#3 lore raw

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

In reply to Roger Pau Monne
Quoted 21 lines · Roger Pau Monne

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.

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.

Quoted 5 lines

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.

with one further remark:

Quoted 7 lines
xen/arch/x86/hvm/svm/svm.c +1 -0
@@ -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]);
+

xen/arch/x86/hvm/svm/svm.c static void cf_check svm_ctxt_switch_from(struct vcpu *v) · line 986

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +9 -0
+    /*
+     * 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);
+    }
 }

Is "cleared" in the comment correct when "spec-ctrl=ssbd"? I think "suitably set" or "cleared/set" or some such would be wanted. This could certainly be adjusted while committing (if you agree), but I will want to give Andrew some time anyway before putting it in, to avoid there again being objections after a change in this area has gone in.

Jan

#4 lore raw

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

Quoted 46 lines · Jan Beulich
Quoted 21 lines · Roger Pau Monne

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.

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.

Quoted 5 lines

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.

with one further remark:

Quoted 15 lines
xen/arch/x86/hvm/svm/svm.c +9 -0
@@ -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);
Quoted 8 lines

xen/arch/x86/hvm/svm/svm.c static void cf_check svm_ctxt_switch_from(struct vcpu *v) · line 986

Quoted 2 lines
xen/arch/x86/hvm/svm/svm.c +1 -0
+    }
 }

Is "cleared" in the comment correct when "spec-ctrl=ssbd"? I think "suitably set" or "cleared/set" or some such would be wanted. This could certainly be adjusted while committing (if you agree), but I will want to give Andrew some time anyway before putting it in, to avoid there again being objections after a change in this area has gone in.

Hm, indeed, maybe "already handled" to avoid getting into the set/clear nomenclature.

Thanks, Roger.

#5 lore raw

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

In reply to Jan Beulich

Hi Andrew,

Quoted 4 lines

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

Quoted 1 line
Quoted 8 lines

with one further remark:

Is "cleared" in the comment correct when "spec-ctrl=ssbd"? I think "suitably set" or "cleared/set" or some such would be wanted. This could certainly be adjusted while committing (if you agree), but I will want to give Andrew some time anyway before putting it in, to avoid there again being objections after a change in this area has gone in.

Also this one please :) Any feedback would be appreciated.

Kind regards,
Henry

Quoted 2 lines

Jan

#6 lore raw

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

In reply to Roger Pau Monne
Quoted 13 lines · Roger Pau Monne

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.

Quoted 2 lines

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.

(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.)

Quoted 23 lines

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.

---
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

Quoted 7 lines
---
 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.

Quoted 10 lines
xen/arch/x86/cpu/amd.c +5 -0
index 98c52d0686..05d72c6501 100644
<snip>
+void amd_set_ssbd(bool enable)
+{
+	if (opt_ssbd)
+		/*
+		 * Ignore attempts to turn off SSBD, it's hardcoded on the

xen/arch/x86/cpu/amd.c bool __init amd_setup_legacy_ssbd(void) · line 823

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+		 * 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.

Quoted 22 lines
xen/arch/x86/hvm/svm/svm.c +10 -0
index 1aeaabcb13..8b101d4f27 100644
@@ -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)

xen/arch/x86/hvm/svm/svm.c static void cf_check svm_ctxt_switch_to(struct vcpu *v) · line 1019

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +7 -0
@@ -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.

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.

~Andrew

#7 lore raw

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

Quoted 14 lines · Andrew Cooper
Quoted 13 lines · Roger Pau Monne

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.

Quoted 6 lines
Quoted 2 lines

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.

Quoted 34 lines

(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.)

Quoted 23 lines

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.

---
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

Quoted 7 lines
---
 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 +++++
Quoted 1 line

Need to patch msr.h now that the semantics of virt_spec_ctrl have changed.

Sure, will adjust the comment there.

Quoted 15 lines
Quoted 15 lines
xen/arch/x86/cpu/amd.c +9 -0
index 98c52d0686..05d72c6501 100644
<snip>
+void amd_set_ssbd(bool enable)
+{
+	if (opt_ssbd)
+		/*
+		 * Ignore attempts to turn off SSBD, it's hardcoded on the
+		 * command line.
+		 */
+		return;
+
Quoted 8 lines

xen/arch/x86/cpu/amd.c bool __init amd_setup_legacy_ssbd(void) · line 823

Quoted 6 lines
xen/arch/x86/cpu/amd.c +6 -0
+	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.

Quoted 36 lines
Quoted 22 lines
xen/arch/x86/hvm/svm/svm.c +10 -0
index 1aeaabcb13..8b101d4f27 100644
@@ -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)

xen/arch/x86/hvm/svm/svm.c static void cf_check svm_ctxt_switch_to(struct vcpu *v) · line 1019

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +7 -0
@@ -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.

Quoted 5 lines

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.

Quoted 4 lines

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.

#8 lore raw

PATCH v3 2/2 amd: remove VIRT_SC_MSR_HVM synthetic feature

In reply to Roger Pau Monne

Since the VIRT_SPEC_CTRL.SSBD selection is no longer context switched on vm{entry,exit} there's no need to use a synthetic feature bit for it anymore.

Remove the bit and instead use a global variable.

No functional change intended.

---
 xen/arch/x86/cpu/amd.c                 | 1 +
 xen/arch/x86/cpuid.c                   | 9 +++++----
 xen/arch/x86/include/asm/amd.h         | 1 +
 xen/arch/x86/include/asm/cpufeatures.h | 2 +-
 xen/arch/x86/spec_ctrl.c               | 8 ++++----
 5 files changed, 12 insertions(+), 9 deletions(-)
xen/arch/x86/cpu/amd.c +1 -0
index 05d72c6501..11f8e1d359 100644
@@ -49,6 +49,7 @@ boolean_param("allow_unsafe", opt_allow_unsafe);
 /* Signal whether the ACPI C1E quirk is required. */
 bool __read_mostly amd_acpi_c1e_quirk;
 bool __ro_after_init amd_legacy_ssbd;
+bool __ro_after_init amd_virt_spec_ctrl;
 
 static inline int rdmsr_amd_safe(unsigned int msr, unsigned int *lo,
 				 unsigned int *hi)
xen/arch/x86/cpuid.c +5 -4
index 822f9ace10..acc2f606ce 100644
@@ -3,6 +3,7 @@
 #include <xen/param.h>
 #include <xen/sched.h>
 #include <xen/nospec.h>
+#include <asm/amd.h>
 #include <asm/cpuid.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/nestedhvm.h>
@@ -543,9 +544,9 @@ static void __init calculate_hvm_max_policy(void)
 
     /*
      * VIRT_SSBD is exposed in the default policy as a result of
-     * VIRT_SC_MSR_HVM being set, it also needs exposing in the max policy.
+     * amd_virt_spec_ctrl being set, it also needs exposing in the max policy.
      */
-    if ( boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM) )
+    if ( amd_virt_spec_ctrl )
         __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset);
 
     /*
@@ -606,9 +607,9 @@ static void __init calculate_hvm_def_policy(void)
 
     /*
      * Only expose VIRT_SSBD if AMD_SSBD is not available, and thus
-     * VIRT_SC_MSR_HVM is set.
+     * amd_virt_spec_ctrl is set.
      */
-    if ( boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM) )
+    if ( amd_virt_spec_ctrl )
         __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset);
 
     sanitise_featureset(hvm_featureset);
xen/arch/x86/include/asm/amd.h +1 -0
index 81ed71710f..5c100784dd 100644
@@ -152,6 +152,7 @@ extern bool amd_acpi_c1e_quirk;
 void amd_check_disable_c1e(unsigned int port, u8 value);
 
 extern bool amd_legacy_ssbd;
+extern bool amd_virt_spec_ctrl;
 bool amd_setup_legacy_ssbd(void);
 void amd_set_ssbd(bool enable);
 
xen/arch/x86/include/asm/cpufeatures.h +1 -1
index 3895de4faf..efd3a667ef 100644
@@ -24,7 +24,7 @@ XEN_CPUFEATURE(APERFMPERF,        X86_SYNTH( 8)) /* APERFMPERF */
 XEN_CPUFEATURE(MFENCE_RDTSC,      X86_SYNTH( 9)) /* MFENCE synchronizes RDTSC */
 XEN_CPUFEATURE(XEN_SMEP,          X86_SYNTH(10)) /* SMEP gets used by Xen itself */
 XEN_CPUFEATURE(XEN_SMAP,          X86_SYNTH(11)) /* SMAP gets used by Xen itself */
-XEN_CPUFEATURE(VIRT_SC_MSR_HVM,   X86_SYNTH(12)) /* MSR_VIRT_SPEC_CTRL exposed to HVM */
+/* Bit 12 unused. */
 XEN_CPUFEATURE(IND_THUNK_LFENCE,  X86_SYNTH(13)) /* Use IND_THUNK_LFENCE */
 XEN_CPUFEATURE(IND_THUNK_JMP,     X86_SYNTH(14)) /* Use IND_THUNK_JMP */
 XEN_CPUFEATURE(SC_NO_BRANCH_HARDEN, X86_SYNTH(15)) /* (Disable) Conditional branch hardening */
xen/arch/x86/spec_ctrl.c +4 -4
index 4e53056624..0b94af6b86 100644
@@ -514,12 +514,12 @@ static void __init print_details(enum ind_thunk thunk, uint64_t caps)
            (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ||
             boot_cpu_has(X86_FEATURE_SC_RSB_HVM) ||
             boot_cpu_has(X86_FEATURE_IBPB_ENTRY_HVM) ||
-            boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM) ||
+            amd_virt_spec_ctrl ||
             opt_eager_fpu || opt_md_clear_hvm)       ? ""               : " None",
            boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_SPEC_CTRL" : "",
            (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ||
-            boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM)) ? " MSR_VIRT_SPEC_CTRL"
-                                                       : "",
+            amd_virt_spec_ctrl)                      ? " MSR_VIRT_SPEC_CTRL"
+                                                     : "",
            boot_cpu_has(X86_FEATURE_SC_RSB_HVM)      ? " RSB"           : "",
            opt_eager_fpu                             ? " EAGER_FPU"     : "",
            opt_md_clear_hvm                          ? " MD_CLEAR"      : "",
@@ -1247,7 +1247,7 @@ void __init init_speculation_mitigations(void)
     /* Support VIRT_SPEC_CTRL.SSBD if AMD_SSBD is not available. */
     if ( opt_msr_sc_hvm && !cpu_has_amd_ssbd &&
          (cpu_has_virt_ssbd || (amd_legacy_ssbd && amd_setup_legacy_ssbd())) )
-        setup_force_cpu_cap(X86_FEATURE_VIRT_SC_MSR_HVM);
+        amd_virt_spec_ctrl = true;
 
     /* Figure out default_xen_spec_ctrl. */
     if ( has_spec_ctrl && ibrs )
Signature · 1 line
2.37.3