Skip to contentSkip to conversation

amd/virt_ssbd: refactoring and fixes

PATCH In review Roger Pau Monne 3 participants 11 messages Nov 15, 2022 – Nov 16, 2022

lore
Share & tools
lore
#0 lore raw

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

Andrew has expressed reservations to me privately with patch 2/2, but I'm still sending it so that comments can be made publicly (or the patch applied).

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         |  1 +
 xen/arch/x86/include/asm/cpufeatures.h |  2 +-
 xen/arch/x86/include/asm/msr.h         |  3 +-
 xen/arch/x86/msr.c                     |  9 +++++
 xen/arch/x86/spec_ctrl.c               |  8 ++--
 10 files changed, 75 insertions(+), 74 deletions(-)
Signature · 1 line
2.37.3
#1 lore raw

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

This fixes an issue with running C code in a GIF=0 region, that's problematic when using UBSAN or other instrumentation techniques.

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.

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 v3:
- Fix commit message order.
- Remove msr.h comment about context switching virt_spec_ctrl.
- s/amd_set_ssbd/amd_set_legacy_ssbd/.
- Adjust comment about clearing SSBD in svm_ctxt_switch_from().

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/msr.h    |  3 +-
 xen/arch/x86/msr.c                |  9 +++++
 6 files changed, 63 insertions(+), 65 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/msr.h +1 -2
index de18e90b2e..3eef1dfb98 100644
@@ -378,8 +378,7 @@ struct vcpu_msrs
     /*
      * 0xc001011f - MSR_VIRT_SPEC_CTRL (if !X86_FEATURE_AMD_SSBD)
      *
-     * AMD only. Guest selected value, context switched on guest VM
-     * entry/exit.
+     * AMD only. Guest selected value.
      */
     struct {
         uint32_t raw;
xen/arch/x86/msr.c +9 -0
index 95416995a5..cf46b18aa6 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_legacy_ssbd(val & SPEC_CTRL_SSBD);
+        }
         break;
 
     case MSR_AMD64_DE_CFG:
Signature · 1 line
2.37.3
#2 lore raw

PATCH v4 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 a332087604..9e3b9094d3 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 6a42f68542..a975d3de26 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_legacy_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
#3 lore raw

Re: [PATCH for-4.17 v4 2/2] amd: remove VIRT_SC_MSR_HVM synthetic feature

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

This is definitely not appropriate for 4.17, but it's a performance regression in general, hence my firm and repeated objection to this style of patch.

General synthetic bits have existed for several decades longer than alternatives.  It has never ever been a rule, or even a recommendation, to aggressively purge the non-alternative bits, because it's a provably bad thing to do.

You are attempting a micro-optimisation, that won't produce any improvement at all in centuries, while...

xen/arch/x86/cpu/amd.c boolean_param("allow_unsafe", opt_allow_unsafe); · line 52

Quoted 9 lines
xen/arch/x86/cpu/amd.c +1 -0
index a332087604..9e3b9094d3 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;

... actually expending .rodata with something 8 times less efficiently packed, and ...

xen/arch/x86/cpuid.c line 6

Quoted 12 lines
xen/arch/x86/cpu/amd.c +0 -0
 static inline int rdmsr_amd_safe(unsigned int msr, unsigned int *lo,
 				 unsigned int *hi)
xen/arch/x86/cpuid.c +1 -0
index 822f9ace10..acc2f606ce 100644
@@ -3,6 +3,7 @@
 #include <xen/param.h>
 #include <xen/sched.h>
 #include <xen/nospec.h>
+#include <asm/amd.h>

... (Specific to this instance) making life harder for the people trying to make CONFIG_AMD work, and ...

xen/arch/x86/spec_ctrl.c static void __init print_details(enum ind_thunk thunk, uint64_t caps) · line 517

Quoted 10 lines
xen/arch/x86/spec_ctrl.c +1 -1
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 ||

... breaking apart a single TEST instruction, which not only adds an extra conditional merge, but now hits an cold-ish cache line everywhere it's used.

Count how many synthetic feature bits it will actually take to change the per-cpu data size, and realise that, when it will take more than 200 years at the current rate of accumulation, any believe that this is an improvement to be had disappears.

Yes, it is only a micro regression, but you need a far better justification than "there is a gain of 64 bytes per CPU which will be non-theoretical in more than 200 years" when traded off vs the actual 512 bytes, plus extra code bloat bloat, plus reduced locality of data that this "improvement" genuinely costs today.

~Andrew

#4 lore raw

Re: [PATCH for-4.17 v4 2/2] amd: remove VIRT_SC_MSR_HVM synthetic feature

Quoted 13 lines · Andrew Cooper
Quoted 11 lines · 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.

Quoted 8 lines

This is definitely not appropriate for 4.17, but it's a performance regression in general, hence my firm and repeated objection to this style of patch.

General synthetic bits have existed for several decades longer than alternatives.  It has never ever been a rule, or even a recommendation, to aggressively purge the non-alternative bits, because it's a provably bad thing to do.

There we are again - you state something as bad without really saying why it is bad. My view is that synthetic bits were wrong to introduce when they don't stand a chance of being used in an alternative.

I agree though that there's no strong need for this to make 4.17. It may end up making backports slightly easier, as no such bit existed in 4.16.

Quoted 15 lines

You are attempting a micro-optimisation, that won't produce any improvement at all in centuries, while...

xen/arch/x86/cpu/amd.c boolean_param("allow_unsafe", opt_allow_unsafe); · line 52

Quoted 9 lines
xen/arch/x86/cpu/amd.c +1 -0
index a332087604..9e3b9094d3 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;

... actually expending .rodata with something 8 times less efficiently packed, and ...

... as long as you're talking of just a single CPU. The break-even is at 8 CPUs (8 bits used either way).

Quoted 18 lines

xen/arch/x86/spec_ctrl.c static void __init print_details(enum ind_thunk thunk, uint64_t caps) · line 517

Quoted 8 lines
xen/arch/x86/spec_ctrl.c +1 -1
@@ -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 ||

... breaking apart a single TEST instruction, which not only adds an extra conditional merge, but now hits an cold-ish cache line everywhere it's used.

Count how many synthetic feature bits it will actually take to change the per-cpu data size, and realise that, when it will take more than 200 years at the current rate of accumulation, any believe that this is an improvement to be had disappears.

Quoted 5 lines

Yes, it is only a micro regression, but you need a far better justification than "there is a gain of 64 bytes per CPU which will be non-theoretical in more than 200 years" when traded off vs the actual 512 bytes, plus extra code bloat bloat, plus reduced locality of data that this "improvement" genuinely costs today.

I don't see Roger stating anything like this.

I think we need to settle on at least halfway firm rules on when to use synthetic feature bits and when to use plain global booleans. Without that the tastes of the three of us are going to collide again every once in a while.

Jan

#5 lore raw

Re: [PATCH for-4.17 v4 2/2] amd: remove VIRT_SC_MSR_HVM synthetic feature

Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper
Quoted 11 lines · 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.

This is definitely not appropriate for 4.17, but it's a performance regression in general, hence my firm and repeated objection to this style of patch.

Quoted 6 lines
Quoted 4 lines

General synthetic bits have existed for several decades longer than alternatives.  It has never ever been a rule, or even a recommendation, to aggressively purge the non-alternative bits, because it's a provably bad thing to do.

There we are again - you state something as bad without really saying why it is bad.

You may not agree with the reasoning, but you are lying to yourself, if no-one else, by claiming that no justification was presented.

Quoted 2 lines

My view is that synthetic bits were wrong to introduce when they don't stand a chance of being used in an alternative.

Your view is incompatible with a linear interpretation of history, as has been pointed repeatedly before by the fact that 1/3 of Xen's synthetic features full predate the introduction of alternatives.

"I don't like using synthetic bits in this way" is a point of view, but is not something that counters technical reasoning about the tradeoff in question.

Quoted 4 lines

I agree though that there's no strong need for this to make 4.17. It may end up making backports slightly easier, as no such bit existed in 4.16.

*This* is a good justification to take the change.

Equally, Roger's subsequent observation that it can actually live in __initdata.

Quoted 14 lines
Quoted 14 lines

You are attempting a micro-optimisation, that won't produce any improvement at all in centuries, while...

xen/arch/x86/cpu/amd.c boolean_param("allow_unsafe", opt_allow_unsafe); · line 52

Quoted 9 lines
xen/arch/x86/cpu/amd.c +1 -0
index a332087604..9e3b9094d3 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;

... actually expending .rodata with something 8 times less efficiently packed, and ...

Quoted 2 lines

... as long as you're talking of just a single CPU. The break-even is at 8 CPUs (8 bits used either way).

And still irrelevant when the size of the per-cpu data area doesn't change for several centuries in the argued case.

Quoted 4 lines

I think we need to settle on at least halfway firm rules on when to use synthetic feature bits and when to use plain global booleans. Without that the tastes of the three of us are going to collide again every once in a while.

Its very easy.  All other things being equal, synthetic features are the most efficient option.

In most cases, things aren't all equal, and literally any technically-credible justification will do.

If a tradeoff doesn't plausibly work within a decade, then it's probably a waste of time raising, and definitely not a point to legitimately object with.  Especially as in the past, I've already given you an alternative course of action where the synthetic features aren't per-cpu...

~Andrew

#6 lore raw

Re: [PATCH for-4.17 v4 2/2] amd: remove VIRT_SC_MSR_HVM synthetic feature

Quoted 8 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

I think we need to settle on at least halfway firm rules on when to use synthetic feature bits and when to use plain global booleans. Without that the tastes of the three of us are going to collide again every once in a while.

Its very easy.  All other things being equal, synthetic features are the most efficient option.

See Roger's better wording of "why use a more complicated construct when a simple one will do". Yes, generated code may be better in certain cases, but no, we don't always judge by that aspect alone. Source simplicity is an important criteria, which at other times I recall you also weighing higher than the performance of resulting code (especially when dealing with performance aspects when they don't really matter at most/all use sites of whichever construct).

Jan

#7 lore raw

Re: [PATCH for-4.17 v4 2/2] amd: remove VIRT_SC_MSR_HVM synthetic feature

Quoted 10 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

I think we need to settle on at least halfway firm rules on when to use synthetic feature bits and when to use plain global booleans. Without that the tastes of the three of us are going to collide again every once in a while.

Its very easy.  All other things being equal, synthetic features are the most efficient option.

Quoted 7 lines

See Roger's better wording of "why use a more complicated construct when a simple one will do". Yes, generated code may be better in certain cases, but no, we don't always judge by that aspect alone. Source simplicity is an important criteria, which at other times I recall you also weighing higher than the performance of resulting code (especially when dealing with performance aspects when they don't really matter at most/all use sites of whichever construct).

I think it would be easier if we can discuss this in one of our x86 related meetings. It's still unclear to me why a synthetic feature would be preferred rather than a global variable in most cases (like the one here, even if the variable didn't end up having the __init attribute).

Thanks, Roger.

#8 lore raw

Re: [PATCH for-4.17 v4 2/2] amd: remove VIRT_SC_MSR_HVM synthetic feature

In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · 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.

Quoted 7 lines
Quoted 3 lines

This is definitely not appropriate for 4.17, but it's a performance regression in general, hence my firm and repeated objection to this style of patch.

While I don't have any objections in deferring this past 4.17, none of the modified paths are performance sensitive AFAICT.

Quoted 8 lines

General synthetic bits have existed for several decades longer than alternatives.  It has never ever been a rule, or even a recommendation, to aggressively purge the non-alternative bits, because it's a provably bad thing to do.

You are attempting a micro-optimisation, that won't produce any improvement at all in centuries, while...

Oh, I wasn't attempting any micro-optimizations TBH, just didn't see the need to keep this as a synthetic feature, and generally consider better to use a global variable because it's IMO easier to follow.

Quoted 14 lines

xen/arch/x86/cpu/amd.c boolean_param("allow_unsafe", opt_allow_unsafe); · line 52

Quoted 9 lines
xen/arch/x86/cpu/amd.c +1 -0
index a332087604..9e3b9094d3 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;

... actually expending .rodata with something 8 times less efficiently packed, and ...

Quoted 1 line
Quoted 14 lines

xen/arch/x86/cpuid.c line 6

Quoted 11 lines

static inline int rdmsr_amd_safe(unsigned int msr, unsigned int *lo,

xen/arch/x86/cpu/amd.c +0 -0
 				 unsigned int *hi)
xen/arch/x86/cpuid.c +1 -0
index 822f9ace10..acc2f606ce 100644
@@ -3,6 +3,7 @@
 #include <xen/param.h>
 #include <xen/sched.h>
 #include <xen/nospec.h>
+#include <asm/amd.h>

... (Specific to this instance) making life harder for the people trying to make CONFIG_AMD work, and ...

That's indeed a point, albeit I think adding a #define amd_virt_spec_ctrl false won't be the bigger of the problems when dealing with CONFIG_AMD, and will need to be done for other AMD specific variables anyway.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/spec_ctrl.c +0 -0
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) ||
Quoted 8 lines

xen/arch/x86/spec_ctrl.c static void __init print_details(enum ind_thunk thunk, uint64_t caps) · line 517

Quoted 4 lines
xen/arch/x86/spec_ctrl.c +1 -1
             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 ||

... breaking apart a single TEST instruction, which not only adds an extra conditional merge, but now hits an cold-ish cache line everywhere it's used.

Why does performance matter here? It's an init function that prints the speculation related settings to the screen, so that's likely to be many times slower that accessing a cold cache line.

Quoted 10 lines

Count how many synthetic feature bits it will actually take to change the per-cpu data size, and realise that, when it will take more than 200 years at the current rate of accumulation, any believe that this is an improvement to be had disappears.

Yes, it is only a micro regression, but you need a far better justification than "there is a gain of 64 bytes per CPU which will be non-theoretical in more than 200 years" when traded off vs the actual 512 bytes, plus extra code bloat bloat, plus reduced locality of data that this "improvement" genuinely costs today.

I wasn't considering any of the above when proposing the change, my only motivation was that global variables are clearer to use than synthetic features, and I didn't see a need for a synthetic feature in this case. If we agree the above possible performance regressions are worth it I'm fine keeping it as-is.

Now that I realize it amd_virt_spec_ctrl could even be plain __init.

Thanks, Roger.

#9 lore raw
In reply to Roger Pau Monne
Quoted 8 lines · Roger Pau Monne

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.

Quoted 5 lines

Andrew has expressed reservations to me privately with patch 2/2, but I'm still sending it so that comments can be made publicly (or the patch applied).

Thanks, Roger.

To avoid a further round trip, I've committed these patches with some minor non-functional adjustments.  In patch 1, an expanded context switching comment, and in patch 2, an adjusted commit message and movement into __initdata.

I've also closed out #90, which closes one of the release blockers.

All other bugs can wait until 4.18, including the totally bizarre one where using cpuid="host:virt-ssbd=1" in the xl.cfg file causes IBRS to become hidden.  I confirmed this was pre-existing behaviour, but haven't investigated further.

~Andrew

#10 lore raw
Quoted 13 lines · Andrew Cooper
Quoted 12 lines · Roger Pau Monne

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.

Andrew has expressed reservations to me privately with patch 2/2, but I'm still sending it so that comments can be made publicly (or the patch applied).

Quoted 6 lines
Quoted 1 line

Thanks, Roger.

To avoid a further round trip, I've committed these patches with some minor non-functional adjustments.  In patch 1, an expanded context switching comment, and in patch 2, an adjusted commit message and movement into __initdata.

FWIW, I didn't expand the comment in patch 1 because I though it was likely to get out of sync.

Quoted 6 lines

I've also closed out #90, which closes one of the release blockers.

All other bugs can wait until 4.18, including the totally bizarre one where using cpuid="host:virt-ssbd=1" in the xl.cfg file causes IBRS to become hidden.  I confirmed this was pre-existing behaviour, but haven't investigated further.

Hm, I don't seem to be able to use cpuid="host:virt-ssbd=1", I get:

# xl create -c tests/virt-spec/test-hvm32-virt-spec.cfg
Parsing config from tests/virt-spec/test-hvm32-virt-spec.cfg
while parsing CPUID string: "host:virt-ssbd=1":
  error: first word must be "host"

However using cpuid=["0x80000008:ebx=xxxxxx1xxxxxxxxxxxxxxxxxxxxxxxxx"] does work and I can see both virt-ssbd and ibrs present in the guest CPUID on an EPYC 7543P (Milan).

Regards, Roger.