Skip to contentSkip to conversation

x86/amd: Fix DE_CFG races and CPUID rescan issues

PATCH In review Andrew Cooper 2 participants 20 messages Nov 26, 2025 – Dec 1, 2025

lore
Share & tools
lore
#0 lore raw

cover x86/amd: Fix DE_CFG races and CPUID rescan issues

This is sufficient to fix the CPUID recan issues found by XenServer's testing, and a DE_CFG bug I found along the way.

There are other CPUID rescan issues which will take further work to fix.

Andrew Cooper (3):
x86/amd: Use setup_force_cpu_cap() for BTC_NO x86/amd: Stop updating the Zenbleed mitigation dynamically x86/amd: Fix race editing DE_CFG

 xen/arch/x86/cpu/amd.c               | 232 +++++++++++++--------------
 xen/arch/x86/cpu/cpu.h               |   3 +-
 xen/arch/x86/cpu/hygon.c             |   6 +-
 xen/arch/x86/cpu/microcode/amd.c     |   2 -
 xen/arch/x86/include/asm/processor.h |   2 -
 5 files changed, 121 insertions(+), 124 deletions(-)

base-commit: 08c787f66cced18f0d0afafd86a040341adbd031

Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/3 x86/amd: Use setup_force_cpu_cap() for BTC_NO

When re-scanning features, forced caps are taken into account but unforced such as this are not. This causes BTC_NO to go missing, and for the system to appear to have lost features.

The practical consequence of this observation is that all after-the-fact adjustments to CPUID must be forced.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/cpu/amd.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
xen/arch/x86/cpu/amd.c +3 -2
index 9b02e1ba675c..8f468aaf0921 100644
@@ -1225,8 +1225,9 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 		 * Branch Type Confusion, but predate the allocation of the
 		 * BTC_NO bit.  Fill it back in if we're not virtualised.
 		 */
-		if (!cpu_has_hypervisor && !cpu_has(c, X86_FEATURE_BTC_NO))
-			__set_bit(X86_FEATURE_BTC_NO, c->x86_capability);
+		if (c == &boot_cpu_data && !cpu_has_hypervisor &&
+		    !cpu_has(c, X86_FEATURE_BTC_NO))
+			setup_force_cpu_cap(X86_FEATURE_BTC_NO);
 		break;
 	}
 
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 1/3] x86/amd: Use setup_force_cpu_cap() for BTC_NO

Quoted 1 line · Andrew Cooper

When re-scanning features,

What exactly do you mean with this, outside of XenServer (i.e. upstream)? The only thing I can think of is recheck_cpu_features(), which calls identify_cpu() and hence init_amd(). Thus ...

Quoted 3 lines

forced caps are taken into account but unforced
such as this are not. This causes BTC_NO to go missing, and for the system to appear to have lost features.

... I don't really follow where features might be lost.

Jan

#3 lore raw

Re: [PATCH 1/3] x86/amd: Use setup_force_cpu_cap() for BTC_NO

Quoted 10 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

When re-scanning features,

What exactly do you mean with this, outside of XenServer (i.e. upstream)? The only thing I can think of is recheck_cpu_features(), which calls identify_cpu() and hence init_amd(). Thus ...

Quoted 3 lines

forced caps are taken into account but unforced
such as this are not. This causes BTC_NO to go missing, and for the system to appear to have lost features.

... I don't really follow where features might be lost.

Well - it's a feature that we started upstreaming and I still hope to finish in some copious free time.

Already upstream, we rescan the Raw CPU policy after microcode load. That has had fixes such as dis-engaging CPUID Masking/Overriding so the Raw policy comes out accurate.

The next step (not upstream yet) is to regenerate the Host and Guest policies.  I recently fixed a bug in XenServer's testing and noticed that the underlying logic had bit-rotted quite a bit, hence this series.

The purpose is to be able to activate new features added by a late microcode load, such as new speculative defences, or simply provide new FOO_NO bits.

~Andrew

#4 lore raw

Re: [PATCH 1/3] x86/amd: Use setup_force_cpu_cap() for BTC_NO

Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

When re-scanning features,

What exactly do you mean with this, outside of XenServer (i.e. upstream)? The only thing I can think of is recheck_cpu_features(), which calls identify_cpu() and hence init_amd(). Thus ...

Quoted 3 lines

forced caps are taken into account but unforced
such as this are not. This causes BTC_NO to go missing, and for the system to appear to have lost features.

Quoted 8 lines
Quoted 1 line

... I don't really follow where features might be lost.

Well - it's a feature that we started upstreaming and I still hope to finish in some copious free time.

Already upstream, we rescan the Raw CPU policy after microcode load. That has had fixes such as dis-engaging CPUID Masking/Overriding so the Raw policy comes out accurate.

Yet that doesn't take forced features into account afaics. So at the very least this needs to come with a description which more accurately describes what (if anything) is actually being fixed / altered upstream.

Quoted 7 lines

The next step (not upstream yet) is to regenerate the Host and Guest policies.  I recently fixed a bug in XenServer's testing and noticed that the underlying logic had bit-rotted quite a bit, hence this series.

The purpose is to be able to activate new features added by a late microcode load, such as new speculative defences, or simply provide new FOO_NO bits.

Yes, that's a good goal.

Jan

#5 lore raw

Re: [PATCH 1/3] x86/amd: Use setup_force_cpu_cap() for BTC_NO

Quoted 15 lines · Jan Beulich
Quoted 14 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

When re-scanning features,

What exactly do you mean with this, outside of XenServer (i.e. upstream)? The only thing I can think of is recheck_cpu_features(), which calls identify_cpu() and hence init_amd(). Thus ...

Quoted 3 lines

forced caps are taken into account but unforced
such as this are not. This causes BTC_NO to go missing, and for the system to appear to have lost features.

... I don't really follow where features might be lost.

Well - it's a feature that we started upstreaming and I still hope to finish in some copious free time.

Quoted 6 lines
Quoted 3 lines

Already upstream, we rescan the Raw CPU policy after microcode load. That has had fixes such as dis-engaging CPUID Masking/Overriding so the Raw policy comes out accurate.

Yet that doesn't take forced features into account afaics. So at the very least this needs to come with a description which more accurately describes what (if anything) is actually being fixed / altered upstream.

I don't know what more you want me to say.  It's not a problem per say in upstream, but it does come about because BTC_NO is handled inconsistently to the other FOO_NO bits.

recheck_cpu_features() papers over the issue by re-invoking identify_cpu().  It's necessary for S3 resume because all of init_$VENDOR() really is needed, but it looks bogus in smp_store_cpu_info() because it's repeating work done immediately prior in start_secondary().

~Andrew

#6 lore raw

Re: [PATCH 1/3] x86/amd: Use setup_force_cpu_cap() for BTC_NO

Quoted 19 lines · Andrew Cooper
Quoted 18 lines · Jan Beulich
Quoted 17 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

When re-scanning features,

What exactly do you mean with this, outside of XenServer (i.e. upstream)? The only thing I can think of is recheck_cpu_features(), which calls identify_cpu() and hence init_amd(). Thus ...

Quoted 3 lines

forced caps are taken into account but unforced
such as this are not. This causes BTC_NO to go missing, and for the system to appear to have lost features.

... I don't really follow where features might be lost.

Well - it's a feature that we started upstreaming and I still hope to finish in some copious free time.

Already upstream, we rescan the Raw CPU policy after microcode load. That has had fixes such as dis-engaging CPUID Masking/Overriding so the Raw policy comes out accurate.

Quoted 7 lines
Quoted 3 lines

Yet that doesn't take forced features into account afaics. So at the very least this needs to come with a description which more accurately describes what (if anything) is actually being fixed / altered upstream.

I don't know what more you want me to say.  It's not a problem per say in upstream, but it does come about because BTC_NO is handled inconsistently to the other FOO_NO bits.

First, it still is unclear to me how "When re-scanning features, forced caps are taken into account" applies to upstream. As a a result, "This causes BTC_NO to go missing" is unclear as well. Both aspects need to be clear from the description alone.

Further, while indeed BTC_NO is the only *_NO one set by means of __set_bit(), an (apparently) similar issue exists for SRSO_US_NO, in it being cleared by __clear_bit(). How that goes together with the feature elsewhere being forced on I can't immediately tell.

Quoted 2 lines

recheck_cpu_features() papers over the issue by re-invoking identify_cpu().

This isn't papering over anything. We do the normal identification, to then compare its results with the ones obtained during boot.

Quoted 4 lines

  It's necessary for S3 resume because all of
init_$VENDOR() really is needed, but it looks bogus in smp_store_cpu_info() because it's repeating work done immediately prior in start_secondary().

What is it that is done there "immediately prior"? I see smp_callin() calling smp_store_cpu_info() alone, which in turn calls either identify_cpu() or recheck_cpu_features(). Nothing being repeated afaics. There is an earlier call to cpu_init(), but that doesn't collect any feature info. (Perhaps we have a latent ordering problem there, as in principle feature dependent setup would better come after determining what features are available. The only feature dependent operations there look to be PKRU clearing and skinit_enable_intr(), so hopefully not an active issue; certainly not as long as the system is symmetric.)

Jan

#7 lore raw

PATCH 1/3 x86/amd: Use setup_force_cpu_cap() for BTC_NO

In reply to Andrew Cooper

A XenServer feature in the process of being upstreamed is to be able to re-caculate the guest CPU Policies at runtime, e.g. after a microcode load and/or livepatch to expose new functionality. Right now, upstream Xen only rescans the Raw CPU Policy on microcode load.

One complication with recalculating the guest policies is that BTC_NO is handled differently to other $FOO_NO bits, by using __set_bit() rather than setup_force_cpu_cap().

For consistency, switch it to using setup_force_cpu_cap(). This doesn't matter for upstream Xen right now, but it will ease upstream the feature.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* Rewrite the commit message.

---
 xen/arch/x86/cpu/amd.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
xen/arch/x86/cpu/amd.c +3 -2
index 805a8189e6cd..b3e12b084c56 100644
@@ -1115,8 +1115,9 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 		 * Branch Type Confusion, but predate the allocation of the
 		 * BTC_NO bit.  Fill it back in if we're not virtualised.
 		 */
-		if (!cpu_has_hypervisor && !cpu_has(c, X86_FEATURE_BTC_NO))
-			__set_bit(X86_FEATURE_BTC_NO, c->x86_capability);
+		if (c == &boot_cpu_data && !cpu_has_hypervisor &&
+		    !cpu_has(c, X86_FEATURE_BTC_NO))
+			setup_force_cpu_cap(X86_FEATURE_BTC_NO);
 		break;
 	}
 
Signature · 1 line
2.39.5
Acked-by on v1 1/3
#8 lore raw

Re: [PATCH v1.9 1/3] x86/amd: Use setup_force_cpu_cap() for BTC_NO

Quoted 5 lines · Andrew Cooper

A XenServer feature in the process of being upstreamed is to be able to re-caculate the guest CPU Policies at runtime, e.g. after a microcode load and/or livepatch to expose new functionality. Right now, upstream Xen only rescans the Raw CPU Policy on microcode load.

Quoted 8 lines

One complication with recalculating the guest policies is that BTC_NO is handled differently to other $FOO_NO bits, by using __set_bit() rather than setup_force_cpu_cap().

For consistency, switch it to using setup_force_cpu_cap(). This doesn't matter for upstream Xen right now, but it will ease upstream the feature.

#9 lore raw

PATCH 2/3 x86/amd: Stop updating the Zenbleed mitigation dynamically

In reply to Andrew Cooper

This was potentially helpful when the chickenbit was the only mitigation and microcode had not been released, but that was two years ago.

Zenbleed microcode has been avaialble since December 2023, and the subsequent Entrysign signature vulnerability means that firmware updates block OS-loading and more OS-loadable microcode will be produced for Zen2.

i.e. the Zenbleed fix is not going to appear at runtime these days.

No practical change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/cpu/amd.c               | 2 +-
 xen/arch/x86/cpu/microcode/amd.c     | 2 --
 xen/arch/x86/include/asm/processor.h | 2 --
 3 files changed, 1 insertion(+), 5 deletions(-)
xen/arch/x86/cpu/amd.c +1 -1
index 8f468aaf0921..7953261895ac 100644
@@ -979,7 +979,7 @@ void __init detect_zen2_null_seg_behaviour(void)
 
 }
 
-void amd_check_zenbleed(void)
+static void amd_check_zenbleed(void)
 {
 	const struct cpu_signature *sig = &this_cpu(cpu_sig);
 	unsigned int good_rev;
xen/arch/x86/cpu/microcode/amd.c +0 -2
index 4a7573c885e5..e7ae1e802353 100644
@@ -380,8 +380,6 @@ static int cf_check apply_microcode(const struct microcode_patch *patch,
            "microcode: CPU%u updated from revision %#x to %#x, date = %04x-%02x-%02x\n",
            cpu, old_rev, rev, patch->year, patch->month, patch->day);
 
-    amd_check_zenbleed();
-
     return 0;
 }
 
xen/arch/x86/include/asm/processor.h +0 -2
index 1342241742ac..2e087c625770 100644
@@ -435,8 +435,6 @@ enum ap_boot_method {
 };
 extern enum ap_boot_method ap_boot_method;
 
-void amd_check_zenbleed(void);
-
 #endif /* !__ASSEMBLY__ */
 
 #endif /* __ASM_X86_PROCESSOR_H */
Signature · 1 line
2.39.5
Acked-by on v1 2/3
#10 lore raw

Re: [PATCH 2/3] x86/amd: Stop updating the Zenbleed mitigation dynamically

Quoted 7 lines · Andrew Cooper

This was potentially helpful when the chickenbit was the only mitigation and microcode had not been released, but that was two years ago.

Zenbleed microcode has been avaialble since December 2023, and the subsequent Entrysign signature vulnerability means that firmware updates block OS-loading and more OS-loadable microcode will be produced for Zen2.

Quoted 5 lines

i.e. the Zenbleed fix is not going to appear at runtime these days.

No practical change.

on the basis that people unwilling to update their firmware already accept being vulnerable. To them this might be a perceived regression, i.e. not exactly "No practical change", but we kind of accept that possibility.

Jan

#11 lore raw

Re: [PATCH 2/3] x86/amd: Stop updating the Zenbleed mitigation dynamically

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

This was potentially helpful when the chickenbit was the only mitigation and microcode had not been released, but that was two years ago.

Zenbleed microcode has been avaialble since December 2023, and the subsequent Entrysign signature vulnerability means that firmware updates block OS-loading and more OS-loadable microcode will be produced for Zen2.

Quoted 6 lines
Quoted 5 lines

i.e. the Zenbleed fix is not going to appear at runtime these days.

No practical change.

Thanks.

Quoted 3 lines

on the basis that people unwilling to update their firmware already accept being vulnerable. To them this might be a perceived regression, i.e. not exactly "No practical change", but we kind of accept that possibility.

It's not quite that easy.  There are plenty of Zen2 systems without a firmware update.

But, a user who cares about their security will have a more up-to-date microcode than 2 years old, and will get the Zenbleed fix at boot time.

What I'm trying to say is that the "old ucode at boot, new at runtime" case doesn't sensibly exist any more.

~Andrew

#12 lore raw

PATCH 3/3 x86/amd: Fix race editing DE_CFG

In reply to Andrew Cooper

We have two different functions explaining that DE_CFG is Core-scoped and that writes are racy but happen to be safe. This is only true when there's one of them.

Introduce amd_init_de_cfg() to be the singular function which writes to DE_CFG, modelled after the logic we already have for BP_CFG.

While reworking amd_check_zenbleed() into a simple predicate used by amd_init_de_cfg(), fix the microcode table. The 'good_rev' was specific to an individual stepping and not valid to be matched by model, let alone a range. The only CPUs incorrectly matched that I can locate appear to be pre-production, and probably didn't get Zenbleed microcode.

Rework amd_init_lfence() to be amd_init_lfence_dispatch() with only the purpose of configuring X86_FEATURE_LFENCE_DISPATCH in the case that it needs synthesising. Run it on the BSP only and use setup_force_cpu_cap() to prevent the bit disappearing on a subseuqent CPUID rescan.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

I've submitted a matching fix to Linux's Zenbleed table.

---
 xen/arch/x86/cpu/amd.c   | 227 +++++++++++++++++++--------------------
 xen/arch/x86/cpu/cpu.h   |   3 +-
 xen/arch/x86/cpu/hygon.c |   6 +-
 3 files changed, 118 insertions(+), 118 deletions(-)
xen/arch/x86/cpu/amd.c +111 -116
index 7953261895ac..c9d0b55c8c8d 100644
@@ -747,45 +747,6 @@ void amd_log_freq(const struct cpuinfo_x86 *c)
 		printk("CPU%u: %u MHz\n", smp_processor_id(), low_mhz);
 }
 
-void amd_init_lfence(struct cpuinfo_x86 *c)
-{
-	uint64_t value;
-
-	/*
-	 * Some hardware has LFENCE dispatch serialising always enabled,
-	 * nothing to do on that case.
-	 */
-	if (test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability))
-		return;
-
-	/*
-	 * Attempt to set lfence to be Dispatch Serialising.  This MSR almost
-	 * certainly isn't virtualised (and Xen at least will leak the real
-	 * value in but silently discard writes), as well as being per-core
-	 * rather than per-thread, so do a full safe read/write/readback cycle
-	 * in the worst case.
-	 */
-	if (rdmsr_safe(MSR_AMD64_DE_CFG, &value))
-		/* Unable to read.  Assume the safer default. */
-		__clear_bit(X86_FEATURE_LFENCE_DISPATCH,
-			    c->x86_capability);
-	else if (value & AMD64_DE_CFG_LFENCE_SERIALISE)
-		/* Already dispatch serialising. */
-		__set_bit(X86_FEATURE_LFENCE_DISPATCH,
-			  c->x86_capability);
-	else if (wrmsr_safe(MSR_AMD64_DE_CFG,
-			    value | AMD64_DE_CFG_LFENCE_SERIALISE) ||
-		 rdmsr_safe(MSR_AMD64_DE_CFG, &value) ||
-		 !(value & AMD64_DE_CFG_LFENCE_SERIALISE))
-		/* Attempt to set failed.  Assume the safer default. */
-		__clear_bit(X86_FEATURE_LFENCE_DISPATCH,
-			    c->x86_capability);
-	else
-		/* Successfully enabled! */
-		__set_bit(X86_FEATURE_LFENCE_DISPATCH,
-			  c->x86_capability);
-}
-
 /*
  * Refer to the AMD Speculative Store Bypass whitepaper:
  * https://developer.amd.com/wp-content/resources/124441_AMD64_SpeculativeStoreBypassDisable_Whitepaper_final.pdf
@@ -979,76 +940,6 @@ void __init detect_zen2_null_seg_behaviour(void)
 
 }
 
-static void amd_check_zenbleed(void)
-{
-	const struct cpu_signature *sig = &this_cpu(cpu_sig);
-	unsigned int good_rev;
-	uint64_t val, old_val, chickenbit = (1 << 9);
-
-	/*
-	 * If we're virtualised, we can't do family/model checks safely, and
-	 * we likely wouldn't have access to DE_CFG even if we could see a
-	 * microcode revision.
-	 *
-	 * A hypervisor may hide AVX as a stopgap mitigation.  We're not in a
-	 * position to care either way.  An admin doesn't want to be disabling
-	 * AVX as a mitigation on any build of Xen with this logic present.
-	 */
-	if (cpu_has_hypervisor || boot_cpu_data.x86 != 0x17)
-		return;
-
-	switch (boot_cpu_data.x86_model) {
-	case 0x30 ... 0x3f: good_rev = 0x0830107a; break;
-	case 0x60 ... 0x67: good_rev = 0x0860010b; break;
-	case 0x68 ... 0x6f: good_rev = 0x08608105; break;
-	case 0x70 ... 0x7f: good_rev = 0x08701032; break;
-	case 0xa0 ... 0xaf: good_rev = 0x08a00008; break;
-	default:
-		/*
-		 * With the Fam17h check above, most parts getting here are
-		 * Zen1.  They're not affected.  Assume Zen2 ones making it
-		 * here are affected regardless of microcode version.
-		 */
-		if (is_zen1_uarch())
-			return;
-		good_rev = ~0U;
-		break;
-	}
-
-	rdmsrl(MSR_AMD64_DE_CFG, val);
-	old_val = val;
-
-	/*
-	 * Microcode is the preferred mitigation, in terms of performance.
-	 * However, without microcode, this chickenbit (specific to the Zen2
-	 * uarch) disables Floating Point Mov-Elimination to mitigate the
-	 * issue.
-	 */
-	val &= ~chickenbit;
-	if (sig->rev < good_rev)
-		val |= chickenbit;
-
-	if (val == old_val)
-		/* Nothing to change. */
-		return;
-
-	/*
-	 * DE_CFG is a Core-scoped MSR, and this write is racy during late
-	 * microcode load.  However, both threads calculate the new value from
-	 * state which is shared, and unrelated to the old value, so the
-	 * result should be consistent.
-	 */
-	wrmsrl(MSR_AMD64_DE_CFG, val);
-
-	/*
-	 * Inform the admin that we changed something, but don't spam,
-	 * especially during a late microcode load.
-	 */
-	if (smp_processor_id() == 0)
-		printk(XENLOG_INFO "Zenbleed mitigation - using %s\n",
-		       val & chickenbit ? "chickenbit" : "microcode");
-}
-
 static void cf_check fam17_disable_c6(void *arg)
 {
 	/* Disable C6 by clearing the CCR{0,1,2}_CC6EN bits. */
@@ -1075,6 +966,112 @@ static void cf_check fam17_disable_c6(void *arg)
 	wrmsrl(MSR_AMD_CSTATE_CFG, val & mask);
 }
 
+static bool zenbleed_use_chickenbit(void)
+{
+    unsigned int curr_rev;
+    uint8_t fixed_rev;
+
+    /*
+     * If we're virtualised, we can't do family/model checks safely, and
+     * we likely wouldn't have access to DE_CFG even if we could see a
+     * microcode revision.
+     *
+     * A hypervisor may hide AVX as a stopgap mitigation.  We're not in a
+     * position to care either way.  An admin doesn't want to be disabling
+     * AVX as a mitigation on any build of Xen with this logic present.
+     */
+    if ( cpu_has_hypervisor || boot_cpu_data.family != 0x17 )
+        return false;
+
+    curr_rev = this_cpu(cpu_sig).rev;
+    switch ( curr_rev >> 8 )
+    {
+    case 0x083010: fixed_rev = 0x7a; break;
+    case 0x086001: fixed_rev = 0x0b; break;
+    case 0x086081: fixed_rev = 0x05; break;
+    case 0x087010: fixed_rev = 0x32; break;
+    case 0x08a000: fixed_rev = 0x08; break;
+    default:
+        /*
+         * With the Fam17h check above, most parts getting here are Zen1.
+         * They're not affected.  Assume Zen2 ones making it here are affected
+         * regardless of microcode version.
+         */
+        return is_zen2_uarch();
+    }
+
+    return (uint8_t)curr_rev >= fixed_rev;
+}
+
+void amd_init_de_cfg(const struct cpuinfo_x86 *c)
+{
+    uint64_t val, new = 0;
+
+    /* The MSR doesn't exist on Fam 0xf/0x11. */
+    if ( c->family != 0xf && c->family != 0x11 )
+        return;
+
+    /*
+     * On Zen3 (Fam 0x19) and later CPUs, LFENCE is unconditionally dispatch
+     * serialising, and is enumerated in CPUID.  Hypervisors may also
+     * enumerate it when the setting is in place and MSR_AMD64_DE_CFG isn't
+     * available.
+     */
+    if ( !test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        new |= AMD64_DE_CFG_LFENCE_SERIALISE;
+
+    /*
+     * If vulnerable to Zenbleed and not mitigated in microcode, use the
+     * bigger hammer.
+     */
+    if ( zenbleed_use_chickenbit() )
+        new |= (1 << 9);
+
+    if ( !new )
+        return;
+
+    if ( rdmsr_safe(MSR_AMD64_DE_CFG, &val) ||
+         (val & new) == new )
+        return;
+
+    /*
+     * DE_CFG is a Core-scoped MSR, and this write is racy.  However, both
+     * threads calculate the new value from state which expected to be
+     * consistent across CPUs and unrelated to the old value, so the result
+     * should be consistent.
+     */
+    wrmsr_safe(MSR_AMD64_DE_CFG, val | new);
+}
+
+void __init amd_init_lfence_dispatch(void)
+{
+    struct cpuinfo_x86 *c = &boot_cpu_data;
+    uint64_t val;
+
+    if ( test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        /* LFENCE is forced dispatch serialising and we can't control it. */
+        return;
+
+    if ( c->family == 0xf || c->family == 0x11 )
+        /* MSR doesn't exist.  LFENCE is dispatch serialising on this hardare. */
+        goto set;
+
+    if ( rdmsr_safe(MSR_AMD64_DE_CFG, &val) )
+        /* Unable to read.  Assume the safer default. */
+        goto clear;
+
+    if ( val & AMD64_DE_CFG_LFENCE_SERIALISE )
+        /* Already dispatch serialising. */
+        goto set;
+
+ clear:
+    setup_clear_cpu_cap(X86_FEATURE_LFENCE_DISPATCH);
+    return;
+
+ set:
+    setup_force_cpu_cap(X86_FEATURE_LFENCE_DISPATCH);
+}
+
 static void amd_check_bp_cfg(void)
 {
 	uint64_t val, new = 0;
@@ -1118,6 +1115,11 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 	u32 l, h;
 	uint64_t value;
 
+        amd_init_de_cfg(c);
+
+        if (c == &boot_cpu_data)
+            amd_init_lfence_dispatch(); /* Needs amd_init_de_cfg() */
+
 	/* Disable TLB flush filter by setting HWCR.FFDIS on K8
 	 * bit 6 of msr C001_0015
 	 *
@@ -1156,12 +1158,6 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 	if (c == &boot_cpu_data && !cpu_has(c, X86_FEATURE_RSTR_FP_ERR_PTRS))
 		setup_force_cpu_cap(X86_BUG_FPU_PTRS);
 
-	if (c->x86 == 0x0f || c->x86 == 0x11)
-		/* Always dispatch serialising on this hardare. */
-		__set_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability);
-	else /* Implicily "== 0x10 || >= 0x12" by being 64bit. */
-		amd_init_lfence(c);
-
 	amd_init_ssbd(c);
 
 	if (c->x86 == 0x17)
@@ -1379,7 +1375,6 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 	if ((smp_processor_id() == 1) && !cpu_has(c, X86_FEATURE_ITSC))
 		disable_c1_ramping();
 
-	amd_check_zenbleed();
 	amd_check_bp_cfg();
 
 	if (fam17_c6_disabled)
xen/arch/x86/cpu/cpu.h +2 -1
index cbb434f3a23d..8bed3f52490f 100644
@@ -24,7 +24,8 @@ extern bool detect_extended_topology(struct cpuinfo_x86 *c);
 
 void cf_check early_init_amd(struct cpuinfo_x86 *c);
 void amd_log_freq(const struct cpuinfo_x86 *c);
-void amd_init_lfence(struct cpuinfo_x86 *c);
+void amd_init_de_cfg(const struct cpuinfo_x86 *c);
+void amd_init_lfence_dispatch(void);
 void amd_init_ssbd(const struct cpuinfo_x86 *c);
 void amd_init_spectral_chicken(void);
 void detect_zen2_null_seg_behaviour(void);
xen/arch/x86/cpu/hygon.c +5 -1
index f7508cc8fcb9..68e98651cb79 100644
@@ -31,7 +31,11 @@ static void cf_check init_hygon(struct cpuinfo_x86 *c)
 {
 	unsigned long long value;
 
-	amd_init_lfence(c);
+        amd_init_de_cfg(c);
+
+        if (c == &boot_cpu_data)
+            amd_init_lfence_dispatch(); /* Needs amd_init_de_cfg() */
+
 	amd_init_ssbd(c);
 
 	/* Probe for NSCB on Zen2 CPUs when not virtualised */
Signature · 1 line
2.39.5
#13 lore raw

Re: [PATCH 3/3] x86/amd: Fix race editing DE_CFG

Quoted 38 lines · Andrew Cooper
xen/arch/x86/cpu/amd.c +34 -0
@@ -1075,6 +966,112 @@ static void cf_check fam17_disable_c6(void *arg)
 	wrmsrl(MSR_AMD_CSTATE_CFG, val & mask);
 }
 
+static bool zenbleed_use_chickenbit(void)
+{
+    unsigned int curr_rev;
+    uint8_t fixed_rev;
+
+    /*
+     * If we're virtualised, we can't do family/model checks safely, and
+     * we likely wouldn't have access to DE_CFG even if we could see a
+     * microcode revision.
+     *
+     * A hypervisor may hide AVX as a stopgap mitigation.  We're not in a
+     * position to care either way.  An admin doesn't want to be disabling
+     * AVX as a mitigation on any build of Xen with this logic present.
+     */
+    if ( cpu_has_hypervisor || boot_cpu_data.family != 0x17 )
+        return false;
+
+    curr_rev = this_cpu(cpu_sig).rev;
+    switch ( curr_rev >> 8 )
+    {
+    case 0x083010: fixed_rev = 0x7a; break;
+    case 0x086001: fixed_rev = 0x0b; break;
+    case 0x086081: fixed_rev = 0x05; break;
+    case 0x087010: fixed_rev = 0x32; break;
+    case 0x08a000: fixed_rev = 0x08; break;
+    default:
+        /*
+         * With the Fam17h check above, most parts getting here are Zen1.
+         * They're not affected.  Assume Zen2 ones making it here are affected
+         * regardless of microcode version.
+         */
+        return is_zen2_uarch();
+    }
+

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1012

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+    return (uint8_t)curr_rev >= fixed_rev;
+}
+
+void amd_init_de_cfg(const struct cpuinfo_x86 *c)
+{
+    uint64_t val, new = 0;
+
+    /* The MSR doesn't exist on Fam 0xf/0x11. */
+    if ( c->family != 0xf && c->family != 0x11 )
+        return;

Comment and code don't match. Did you mean

    if ( c->family == 0xf || c->family == 0x11 )
        return;

(along the lines of what you have in amd_init_lfence_dispatch())?

Quoted 20 lines
xen/arch/x86/cpu/amd.c +20 -0
+    /*
+     * On Zen3 (Fam 0x19) and later CPUs, LFENCE is unconditionally dispatch
+     * serialising, and is enumerated in CPUID.  Hypervisors may also
+     * enumerate it when the setting is in place and MSR_AMD64_DE_CFG isn't
+     * available.
+     */
+    if ( !test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        new |= AMD64_DE_CFG_LFENCE_SERIALISE;
+
+    /*
+     * If vulnerable to Zenbleed and not mitigated in microcode, use the
+     * bigger hammer.
+     */
+    if ( zenbleed_use_chickenbit() )
+        new |= (1 << 9);
+
+    if ( !new )
+        return;
+
+    if ( rdmsr_safe(MSR_AMD64_DE_CFG, &val) ||

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1043

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+         (val & new) == new )
+        return;
+
+    /*
+     * DE_CFG is a Core-scoped MSR, and this write is racy.  However, both
+     * threads calculate the new value from state which expected to be
+     * consistent across CPUs and unrelated to the old value, so the result
+     * should be consistent.
+     */
+    wrmsr_safe(MSR_AMD64_DE_CFG, val | new);

Either of the bits may be the cause of #GP. In that case we wouldn't set the other bit, even if it may be possible to set it.

Quoted 12 lines
xen/arch/x86/cpu/amd.c +12 -0
+}
+
+void __init amd_init_lfence_dispatch(void)
+{
+    struct cpuinfo_x86 *c = &boot_cpu_data;
+    uint64_t val;
+
+    if ( test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        /* LFENCE is forced dispatch serialising and we can't control it. */
+        return;
+
+    if ( c->family == 0xf || c->family == 0x11 )

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1065

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+        /* MSR doesn't exist.  LFENCE is dispatch serialising on this hardare. */
+        goto set;
+
+    if ( rdmsr_safe(MSR_AMD64_DE_CFG, &val) )
+        /* Unable to read.  Assume the safer default. */
+        goto clear;
+
+    if ( val & AMD64_DE_CFG_LFENCE_SERIALISE )
+        /* Already dispatch serialising. */
+        goto set;

Why "already", when this is now after we did the adjustment?

Quoted 13 lines
xen/arch/x86/cpu/amd.c +8 -0
+ clear:
+    setup_clear_cpu_cap(X86_FEATURE_LFENCE_DISPATCH);
+    return;
+
+ set:
+    setup_force_cpu_cap(X86_FEATURE_LFENCE_DISPATCH);
+}
+
 static void amd_check_bp_cfg(void)
 {
 	uint64_t val, new = 0;
@@ -1118,6 +1115,11 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 	u32 l, h;

xen/arch/x86/cpu/amd.c static void cf_check init_amd(struct cpuinfo_x86 *c) · line 1125

Quoted 10 lines
xen/arch/x86/cpu/amd.c +5 -0
 	uint64_t value;
 
+        amd_init_de_cfg(c);
+
+        if (c == &boot_cpu_data)
+            amd_init_lfence_dispatch(); /* Needs amd_init_de_cfg() */
+
 	/* Disable TLB flush filter by setting HWCR.FFDIS on K8
 	 * bit 6 of msr C001_0015
 	 *

Nit: I don't think we want to mix indentation style within a function.

Quoted 5 lines
xen/arch/x86/cpu/hygon.c +0 -0
@@ -31,7 +31,11 @@ static void cf_check init_hygon(struct cpuinfo_x86 *c)
 {
 	unsigned long long value;

xen/arch/x86/cpu/hygon.c static void cf_check init_hygon(struct cpuinfo_x86 *c) · line 41

Quoted 10 lines
xen/arch/x86/cpu/hygon.c +5 -1
 
-	amd_init_lfence(c);
+        amd_init_de_cfg(c);
+
+        if (c == &boot_cpu_data)
+            amd_init_lfence_dispatch(); /* Needs amd_init_de_cfg() */
+
 	amd_init_ssbd(c);
 
 	/* Probe for NSCB on Zen2 CPUs when not virtualised */

Same here then.

Jan

#14 lore raw

Re: [PATCH 3/3] x86/amd: Fix race editing DE_CFG

Quoted 51 lines · Jan Beulich
Quoted 38 lines · Andrew Cooper
xen/arch/x86/cpu/amd.c +34 -0
@@ -1075,6 +966,112 @@ static void cf_check fam17_disable_c6(void *arg)
 	wrmsrl(MSR_AMD_CSTATE_CFG, val & mask);
 }
 
+static bool zenbleed_use_chickenbit(void)
+{
+    unsigned int curr_rev;
+    uint8_t fixed_rev;
+
+    /*
+     * If we're virtualised, we can't do family/model checks safely, and
+     * we likely wouldn't have access to DE_CFG even if we could see a
+     * microcode revision.
+     *
+     * A hypervisor may hide AVX as a stopgap mitigation.  We're not in a
+     * position to care either way.  An admin doesn't want to be disabling
+     * AVX as a mitigation on any build of Xen with this logic present.
+     */
+    if ( cpu_has_hypervisor || boot_cpu_data.family != 0x17 )
+        return false;
+
+    curr_rev = this_cpu(cpu_sig).rev;
+    switch ( curr_rev >> 8 )
+    {
+    case 0x083010: fixed_rev = 0x7a; break;
+    case 0x086001: fixed_rev = 0x0b; break;
+    case 0x086081: fixed_rev = 0x05; break;
+    case 0x087010: fixed_rev = 0x32; break;
+    case 0x08a000: fixed_rev = 0x08; break;
+    default:
+        /*
+         * With the Fam17h check above, most parts getting here are Zen1.
+         * They're not affected.  Assume Zen2 ones making it here are affected
+         * regardless of microcode version.
+         */
+        return is_zen2_uarch();
+    }
+

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1012

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+    return (uint8_t)curr_rev >= fixed_rev;
+}
+
+void amd_init_de_cfg(const struct cpuinfo_x86 *c)
+{
+    uint64_t val, new = 0;
+
+    /* The MSR doesn't exist on Fam 0xf/0x11. */
+    if ( c->family != 0xf && c->family != 0x11 )
+        return;

Comment and code don't match. Did you mean

Quoted 4 lines
    if ( c->family == 0xf || c->family == 0x11 )
        return;

(along the lines of what you have in amd_init_lfence_dispatch())?

Oh - that was a last minute refactor which I didn't do quite correctly. Yes, it should match amd_init_lfence_dispatch().

Quoted 25 lines
Quoted 25 lines
xen/arch/x86/cpu/amd.c +24 -0
+    /*
+     * On Zen3 (Fam 0x19) and later CPUs, LFENCE is unconditionally dispatch
+     * serialising, and is enumerated in CPUID.  Hypervisors may also
+     * enumerate it when the setting is in place and MSR_AMD64_DE_CFG isn't
+     * available.
+     */
+    if ( !test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        new |= AMD64_DE_CFG_LFENCE_SERIALISE;
+
+    /*
+     * If vulnerable to Zenbleed and not mitigated in microcode, use the
+     * bigger hammer.
+     */
+    if ( zenbleed_use_chickenbit() )
+        new |= (1 << 9);
+
+    if ( !new )
+        return;
+
+    if ( rdmsr_safe(MSR_AMD64_DE_CFG, &val) ||
+         (val & new) == new )
+        return;
+
+    /*
Quoted 8 lines

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1043

Quoted 6 lines
xen/arch/x86/cpu/amd.c +6 -0
+     * DE_CFG is a Core-scoped MSR, and this write is racy.  However, both
+     * threads calculate the new value from state which expected to be
+     * consistent across CPUs and unrelated to the old value, so the result
+     * should be consistent.
+     */
+    wrmsr_safe(MSR_AMD64_DE_CFG, val | new);

Either of the bits may be the cause of #GP. In that case we wouldn't set the other bit, even if it may be possible to set it.

This MSR does not #GP on real hardware.

Also, both of these bits come from instructions AMD have provided, saying "set $X in case $Y", which we have honoured as part of the conditions for setting up new, which I consider to be a reasonable guarantee that no #GP will ensue.

This wrmsr_safe() is covering the virt case, because older Xen and Byhive used to disallow writes to it, and OpenBSD would explode as a consequence.  Xen's fix was 4175fd3ccd17.

I toyed with the idea of having a tristate de_cfg_writeable, but that got very ugly very quickly

The other option would be to ignore DE_CFG entirely under virt.  That's what we do for BP_CFG already, and no hypervisor is going to really let us have access to it, and it would downgrade to non-safe variants.

Quoted 15 lines
Quoted 15 lines
xen/arch/x86/cpu/amd.c +15 -0
+}
+
+void __init amd_init_lfence_dispatch(void)
+{
+    struct cpuinfo_x86 *c = &boot_cpu_data;
+    uint64_t val;
+
+    if ( test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        /* LFENCE is forced dispatch serialising and we can't control it. */
+        return;
+
+    if ( c->family == 0xf || c->family == 0x11 )
+        /* MSR doesn't exist.  LFENCE is dispatch serialising on this hardare. */
+        goto set;
+
Quoted 8 lines

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1065

Quoted 7 lines
xen/arch/x86/cpu/amd.c +7 -0
+    if ( rdmsr_safe(MSR_AMD64_DE_CFG, &val) )
+        /* Unable to read.  Assume the safer default. */
+        goto clear;
+
+    if ( val & AMD64_DE_CFG_LFENCE_SERIALISE )
+        /* Already dispatch serialising. */
+        goto set;

Why "already", when this is now after we did the adjustment?

The old logic read it back to see if the write had stuck.  Again that was the virt case.

With the new logic, I can probably just drop this particular comment. It's not terribly useful given the new layout.

~Andrew

#15 lore raw

Re: [PATCH 3/3] x86/amd: Fix race editing DE_CFG

Quoted 102 lines · Andrew Cooper
Quoted 51 lines · Jan Beulich
Quoted 38 lines · Andrew Cooper
xen/arch/x86/cpu/amd.c +34 -0
@@ -1075,6 +966,112 @@ static void cf_check fam17_disable_c6(void *arg)
 	wrmsrl(MSR_AMD_CSTATE_CFG, val & mask);
 }
 
+static bool zenbleed_use_chickenbit(void)
+{
+    unsigned int curr_rev;
+    uint8_t fixed_rev;
+
+    /*
+     * If we're virtualised, we can't do family/model checks safely, and
+     * we likely wouldn't have access to DE_CFG even if we could see a
+     * microcode revision.
+     *
+     * A hypervisor may hide AVX as a stopgap mitigation.  We're not in a
+     * position to care either way.  An admin doesn't want to be disabling
+     * AVX as a mitigation on any build of Xen with this logic present.
+     */
+    if ( cpu_has_hypervisor || boot_cpu_data.family != 0x17 )
+        return false;
+
+    curr_rev = this_cpu(cpu_sig).rev;
+    switch ( curr_rev >> 8 )
+    {
+    case 0x083010: fixed_rev = 0x7a; break;
+    case 0x086001: fixed_rev = 0x0b; break;
+    case 0x086081: fixed_rev = 0x05; break;
+    case 0x087010: fixed_rev = 0x32; break;
+    case 0x08a000: fixed_rev = 0x08; break;
+    default:
+        /*
+         * With the Fam17h check above, most parts getting here are Zen1.
+         * They're not affected.  Assume Zen2 ones making it here are affected
+         * regardless of microcode version.
+         */
+        return is_zen2_uarch();
+    }
+

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1012

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+    return (uint8_t)curr_rev >= fixed_rev;
+}
+
+void amd_init_de_cfg(const struct cpuinfo_x86 *c)
+{
+    uint64_t val, new = 0;
+
+    /* The MSR doesn't exist on Fam 0xf/0x11. */
+    if ( c->family != 0xf && c->family != 0x11 )
+        return;

Comment and code don't match. Did you mean

Quoted 4 lines
    if ( c->family == 0xf || c->family == 0x11 )
        return;

(along the lines of what you have in amd_init_lfence_dispatch())?

Oh - that was a last minute refactor which I didn't do quite correctly. Yes, it should match amd_init_lfence_dispatch().

Quoted 24 lines
Quoted 24 lines
xen/arch/x86/cpu/amd.c +24 -0
+    /*
+     * On Zen3 (Fam 0x19) and later CPUs, LFENCE is unconditionally dispatch
+     * serialising, and is enumerated in CPUID.  Hypervisors may also
+     * enumerate it when the setting is in place and MSR_AMD64_DE_CFG isn't
+     * available.
+     */
+    if ( !test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        new |= AMD64_DE_CFG_LFENCE_SERIALISE;
+
+    /*
+     * If vulnerable to Zenbleed and not mitigated in microcode, use the
+     * bigger hammer.
+     */
+    if ( zenbleed_use_chickenbit() )
+        new |= (1 << 9);
+
+    if ( !new )
+        return;
+
+    if ( rdmsr_safe(MSR_AMD64_DE_CFG, &val) ||
+         (val & new) == new )
+        return;
+
+    /*
Quoted 8 lines

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1043

Quoted 6 lines
xen/arch/x86/cpu/amd.c +6 -0
+     * DE_CFG is a Core-scoped MSR, and this write is racy.  However, both
+     * threads calculate the new value from state which expected to be
+     * consistent across CPUs and unrelated to the old value, so the result
+     * should be consistent.
+     */
+    wrmsr_safe(MSR_AMD64_DE_CFG, val | new);

Either of the bits may be the cause of #GP. In that case we wouldn't set the other bit, even if it may be possible to set it.

This MSR does not #GP on real hardware.

Also, both of these bits come from instructions AMD have provided, saying "set $X in case $Y", which we have honoured as part of the conditions for setting up new, which I consider to be a reasonable guarantee that no #GP will ensue.

This wrmsr_safe() is covering the virt case, because older Xen and Byhive used to disallow writes to it, and OpenBSD would explode as a consequence.  Xen's fix was 4175fd3ccd17.

Quoted 6 lines

I toyed with the idea of having a tristate de_cfg_writeable, but that got very ugly very quickly

The other option would be to ignore DE_CFG entirely under virt.  That's what we do for BP_CFG already, and no hypervisor is going to really let us have access to it, and it would downgrade to non-safe variants.

In fact, ignoring the virt case for DE_CFG makes this generally nicer.

~Andrew

#16 lore raw

Re: [PATCH 3/3] x86/amd: Fix race editing DE_CFG

Quoted 92 lines · Andrew Cooper
Quoted 91 lines · Andrew Cooper
Quoted 51 lines · Jan Beulich
Quoted 38 lines · Andrew Cooper
xen/arch/x86/cpu/amd.c +34 -0
@@ -1075,6 +966,112 @@ static void cf_check fam17_disable_c6(void *arg)
 	wrmsrl(MSR_AMD_CSTATE_CFG, val & mask);
 }
 
+static bool zenbleed_use_chickenbit(void)
+{
+    unsigned int curr_rev;
+    uint8_t fixed_rev;
+
+    /*
+     * If we're virtualised, we can't do family/model checks safely, and
+     * we likely wouldn't have access to DE_CFG even if we could see a
+     * microcode revision.
+     *
+     * A hypervisor may hide AVX as a stopgap mitigation.  We're not in a
+     * position to care either way.  An admin doesn't want to be disabling
+     * AVX as a mitigation on any build of Xen with this logic present.
+     */
+    if ( cpu_has_hypervisor || boot_cpu_data.family != 0x17 )
+        return false;
+
+    curr_rev = this_cpu(cpu_sig).rev;
+    switch ( curr_rev >> 8 )
+    {
+    case 0x083010: fixed_rev = 0x7a; break;
+    case 0x086001: fixed_rev = 0x0b; break;
+    case 0x086081: fixed_rev = 0x05; break;
+    case 0x087010: fixed_rev = 0x32; break;
+    case 0x08a000: fixed_rev = 0x08; break;
+    default:
+        /*
+         * With the Fam17h check above, most parts getting here are Zen1.
+         * They're not affected.  Assume Zen2 ones making it here are affected
+         * regardless of microcode version.
+         */
+        return is_zen2_uarch();
+    }
+

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1012

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+    return (uint8_t)curr_rev >= fixed_rev;
+}
+
+void amd_init_de_cfg(const struct cpuinfo_x86 *c)
+{
+    uint64_t val, new = 0;
+
+    /* The MSR doesn't exist on Fam 0xf/0x11. */
+    if ( c->family != 0xf && c->family != 0x11 )
+        return;

Comment and code don't match. Did you mean

Quoted 4 lines
    if ( c->family == 0xf || c->family == 0x11 )
        return;

(along the lines of what you have in amd_init_lfence_dispatch())?

Oh - that was a last minute refactor which I didn't do quite correctly. Yes, it should match amd_init_lfence_dispatch().

Quoted 32 lines
Quoted 20 lines
xen/arch/x86/cpu/amd.c +20 -0
+    /*
+     * On Zen3 (Fam 0x19) and later CPUs, LFENCE is unconditionally dispatch
+     * serialising, and is enumerated in CPUID.  Hypervisors may also
+     * enumerate it when the setting is in place and MSR_AMD64_DE_CFG isn't
+     * available.
+     */
+    if ( !test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        new |= AMD64_DE_CFG_LFENCE_SERIALISE;
+
+    /*
+     * If vulnerable to Zenbleed and not mitigated in microcode, use the
+     * bigger hammer.
+     */
+    if ( zenbleed_use_chickenbit() )
+        new |= (1 << 9);
+
+    if ( !new )
+        return;
+
+    if ( rdmsr_safe(MSR_AMD64_DE_CFG, &val) ||

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1043

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+         (val & new) == new )
+        return;
+
+    /*
+     * DE_CFG is a Core-scoped MSR, and this write is racy.  However, both
+     * threads calculate the new value from state which expected to be
+     * consistent across CPUs and unrelated to the old value, so the result
+     * should be consistent.
+     */
+    wrmsr_safe(MSR_AMD64_DE_CFG, val | new);

Either of the bits may be the cause of #GP. In that case we wouldn't set the other bit, even if it may be possible to set it.

Quoted 1 line
Quoted 1 line

This MSR does not #GP on real hardware.

I consider this unexpected / inconsistent, at least as long as some of the bits would be documented as reserved. "Would be" because the particular Fam17 and Fam19 PPRs I'm looking at don't even mention DE_CFG (or BP_CFG, for that matter).

Quoted 4 lines
Quoted 4 lines

Also, both of these bits come from instructions AMD have provided, saying "set $X in case $Y", which we have honoured as part of the conditions for setting up new, which I consider to be a reasonable guarantee that no #GP will ensue.

The AMD instructions are for particular models, aren't they? While that may mean the bits are fine to blindly (try to) set on other models, pretty likely this can't be extended to other families. (While zenbleed_use_chickenbit() is family-specific, the LFENCE bit is tried without regard to family.)

Quoted 4 lines
Quoted 4 lines

This wrmsr_safe() is covering the virt case, because older Xen and Byhive used to disallow writes to it, and OpenBSD would explode as a consequence.  Xen's fix was 4175fd3ccd17.

Quoted 8 lines
Quoted 6 lines

I toyed with the idea of having a tristate de_cfg_writeable, but that got very ugly very quickly

The other option would be to ignore DE_CFG entirely under virt.  That's what we do for BP_CFG already, and no hypervisor is going to really let us have access to it, and it would downgrade to non-safe variants.

In fact, ignoring the virt case for DE_CFG makes this generally nicer.

And being consistent with what we do with BP_CFG looks desirable to me as well.

Jan

#17 lore raw

Re: [PATCH 3/3] x86/amd: Fix race editing DE_CFG

Quoted 93 lines · Jan Beulich
Quoted 92 lines · Andrew Cooper
Quoted 91 lines · Andrew Cooper
Quoted 51 lines · Jan Beulich
Quoted 38 lines · Andrew Cooper
xen/arch/x86/cpu/amd.c +34 -0
@@ -1075,6 +966,112 @@ static void cf_check fam17_disable_c6(void *arg)
 	wrmsrl(MSR_AMD_CSTATE_CFG, val & mask);
 }
 
+static bool zenbleed_use_chickenbit(void)
+{
+    unsigned int curr_rev;
+    uint8_t fixed_rev;
+
+    /*
+     * If we're virtualised, we can't do family/model checks safely, and
+     * we likely wouldn't have access to DE_CFG even if we could see a
+     * microcode revision.
+     *
+     * A hypervisor may hide AVX as a stopgap mitigation.  We're not in a
+     * position to care either way.  An admin doesn't want to be disabling
+     * AVX as a mitigation on any build of Xen with this logic present.
+     */
+    if ( cpu_has_hypervisor || boot_cpu_data.family != 0x17 )
+        return false;
+
+    curr_rev = this_cpu(cpu_sig).rev;
+    switch ( curr_rev >> 8 )
+    {
+    case 0x083010: fixed_rev = 0x7a; break;
+    case 0x086001: fixed_rev = 0x0b; break;
+    case 0x086081: fixed_rev = 0x05; break;
+    case 0x087010: fixed_rev = 0x32; break;
+    case 0x08a000: fixed_rev = 0x08; break;
+    default:
+        /*
+         * With the Fam17h check above, most parts getting here are Zen1.
+         * They're not affected.  Assume Zen2 ones making it here are affected
+         * regardless of microcode version.
+         */
+        return is_zen2_uarch();
+    }
+

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1012

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+    return (uint8_t)curr_rev >= fixed_rev;
+}
+
+void amd_init_de_cfg(const struct cpuinfo_x86 *c)
+{
+    uint64_t val, new = 0;
+
+    /* The MSR doesn't exist on Fam 0xf/0x11. */
+    if ( c->family != 0xf && c->family != 0x11 )
+        return;

Comment and code don't match. Did you mean

Quoted 4 lines
    if ( c->family == 0xf || c->family == 0x11 )
        return;

(along the lines of what you have in amd_init_lfence_dispatch())?

Oh - that was a last minute refactor which I didn't do quite correctly. Yes, it should match amd_init_lfence_dispatch().

Quoted 32 lines
Quoted 20 lines
xen/arch/x86/cpu/amd.c +20 -0
+    /*
+     * On Zen3 (Fam 0x19) and later CPUs, LFENCE is unconditionally dispatch
+     * serialising, and is enumerated in CPUID.  Hypervisors may also
+     * enumerate it when the setting is in place and MSR_AMD64_DE_CFG isn't
+     * available.
+     */
+    if ( !test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        new |= AMD64_DE_CFG_LFENCE_SERIALISE;
+
+    /*
+     * If vulnerable to Zenbleed and not mitigated in microcode, use the
+     * bigger hammer.
+     */
+    if ( zenbleed_use_chickenbit() )
+        new |= (1 << 9);
+
+    if ( !new )
+        return;
+
+    if ( rdmsr_safe(MSR_AMD64_DE_CFG, &val) ||

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1043

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+         (val & new) == new )
+        return;
+
+    /*
+     * DE_CFG is a Core-scoped MSR, and this write is racy.  However, both
+     * threads calculate the new value from state which expected to be
+     * consistent across CPUs and unrelated to the old value, so the result
+     * should be consistent.
+     */
+    wrmsr_safe(MSR_AMD64_DE_CFG, val | new);

Either of the bits may be the cause of #GP. In that case we wouldn't set the other bit, even if it may be possible to set it.

Quoted 5 lines
Quoted 1 line
Quoted 1 line

This MSR does not #GP on real hardware.

I consider this unexpected / inconsistent, at least as long as some of the bits would be documented as reserved. "Would be" because the particular Fam17 and Fam19 PPRs I'm looking at don't even mention DE_CFG (or BP_CFG, for that matter).

You need the even-more-NDA manual to find those details.

Reserved doesn't mean #GP. It means "don't rely on the behaviour".

Quoted 9 lines
Quoted 4 lines
Quoted 4 lines

Also, both of these bits come from instructions AMD have provided, saying "set $X in case $Y", which we have honoured as part of the conditions for setting up new, which I consider to be a reasonable guarantee that no #GP will ensue.

The AMD instructions are for particular models, aren't they? While that may mean the bits are fine to blindly (try to) set on other models, pretty likely this can't be extended to other families. (While zenbleed_use_chickenbit() is family-specific, the LFENCE bit is tried without regard to family.)

The Managing Speciation whitepaper says "set bit 1 on Fam 10, 12, 14, 15-17".

It also says that AMD will treat the MSR and bit 1 as architectural moving forwards.  In reality, on Zen3 (post-dating the whitepaper) and later, it's write-discard, read-as-1, and this is the behaviour we provide to all VMs.

The Zenbleed instruction say "set bit 9 on Zen2".

So, the logic in this patch following AMD's written instructions.

~Andrew

#18 lore raw

PATCH 3/3 x86/amd: Fix race editing DE_CFG

In reply to Andrew Cooper

We have two different functions explaining that DE_CFG is Core-scoped and that writes are racy but happen to be safe. This is only true when there's one of them.

Introduce amd_init_de_cfg() to be the singular function which writes to DE_CFG, modelled after the logic we already have for BP_CFG.

While reworking amd_check_zenbleed() into a simple predicate used by amd_init_de_cfg(), fix the microcode table. The 'good_rev' was specific to an individual stepping and not valid to be matched by model, let alone a range. The only CPUs incorrectly matched that I can locate appear to be pre-production, and probably didn't get Zenbleed microcode.

Rework amd_init_lfence() to be amd_init_lfence_dispatch() with only the purpose of configuring X86_FEATURE_LFENCE_DISPATCH in the case that it needs synthesising. Run it on the BSP only and use setup_force_cpu_cap() to prevent the bit disappearing on a subseuqent CPUID rescan.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* Fix FamF/11h check in amd_init_de_cfg(), and exclude the virt case.
* Fix tabs/spaces in init_{amd,hygon}()
* Drop comment in amd_init_lfence_dispatch()

---
 xen/arch/x86/cpu/amd.c   | 223 +++++++++++++++++++--------------------
 xen/arch/x86/cpu/cpu.h   |   3 +-
 xen/arch/x86/cpu/hygon.c |   6 +-
 3 files changed, 114 insertions(+), 118 deletions(-)
xen/arch/x86/cpu/amd.c +107 -116
index 7953261895ac..82b02df9a9d5 100644
@@ -747,45 +747,6 @@ void amd_log_freq(const struct cpuinfo_x86 *c)
 		printk("CPU%u: %u MHz\n", smp_processor_id(), low_mhz);
 }
 
-void amd_init_lfence(struct cpuinfo_x86 *c)
-{
-	uint64_t value;
-
-	/*
-	 * Some hardware has LFENCE dispatch serialising always enabled,
-	 * nothing to do on that case.
-	 */
-	if (test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability))
-		return;
-
-	/*
-	 * Attempt to set lfence to be Dispatch Serialising.  This MSR almost
-	 * certainly isn't virtualised (and Xen at least will leak the real
-	 * value in but silently discard writes), as well as being per-core
-	 * rather than per-thread, so do a full safe read/write/readback cycle
-	 * in the worst case.
-	 */
-	if (rdmsr_safe(MSR_AMD64_DE_CFG, &value))
-		/* Unable to read.  Assume the safer default. */
-		__clear_bit(X86_FEATURE_LFENCE_DISPATCH,
-			    c->x86_capability);
-	else if (value & AMD64_DE_CFG_LFENCE_SERIALISE)
-		/* Already dispatch serialising. */
-		__set_bit(X86_FEATURE_LFENCE_DISPATCH,
-			  c->x86_capability);
-	else if (wrmsr_safe(MSR_AMD64_DE_CFG,
-			    value | AMD64_DE_CFG_LFENCE_SERIALISE) ||
-		 rdmsr_safe(MSR_AMD64_DE_CFG, &value) ||
-		 !(value & AMD64_DE_CFG_LFENCE_SERIALISE))
-		/* Attempt to set failed.  Assume the safer default. */
-		__clear_bit(X86_FEATURE_LFENCE_DISPATCH,
-			    c->x86_capability);
-	else
-		/* Successfully enabled! */
-		__set_bit(X86_FEATURE_LFENCE_DISPATCH,
-			  c->x86_capability);
-}
-
 /*
  * Refer to the AMD Speculative Store Bypass whitepaper:
  * https://developer.amd.com/wp-content/resources/124441_AMD64_SpeculativeStoreBypassDisable_Whitepaper_final.pdf
@@ -979,76 +940,6 @@ void __init detect_zen2_null_seg_behaviour(void)
 
 }
 
-static void amd_check_zenbleed(void)
-{
-	const struct cpu_signature *sig = &this_cpu(cpu_sig);
-	unsigned int good_rev;
-	uint64_t val, old_val, chickenbit = (1 << 9);
-
-	/*
-	 * If we're virtualised, we can't do family/model checks safely, and
-	 * we likely wouldn't have access to DE_CFG even if we could see a
-	 * microcode revision.
-	 *
-	 * A hypervisor may hide AVX as a stopgap mitigation.  We're not in a
-	 * position to care either way.  An admin doesn't want to be disabling
-	 * AVX as a mitigation on any build of Xen with this logic present.
-	 */
-	if (cpu_has_hypervisor || boot_cpu_data.x86 != 0x17)
-		return;
-
-	switch (boot_cpu_data.x86_model) {
-	case 0x30 ... 0x3f: good_rev = 0x0830107a; break;
-	case 0x60 ... 0x67: good_rev = 0x0860010b; break;
-	case 0x68 ... 0x6f: good_rev = 0x08608105; break;
-	case 0x70 ... 0x7f: good_rev = 0x08701032; break;
-	case 0xa0 ... 0xaf: good_rev = 0x08a00008; break;
-	default:
-		/*
-		 * With the Fam17h check above, most parts getting here are
-		 * Zen1.  They're not affected.  Assume Zen2 ones making it
-		 * here are affected regardless of microcode version.
-		 */
-		if (is_zen1_uarch())
-			return;
-		good_rev = ~0U;
-		break;
-	}
-
-	rdmsrl(MSR_AMD64_DE_CFG, val);
-	old_val = val;
-
-	/*
-	 * Microcode is the preferred mitigation, in terms of performance.
-	 * However, without microcode, this chickenbit (specific to the Zen2
-	 * uarch) disables Floating Point Mov-Elimination to mitigate the
-	 * issue.
-	 */
-	val &= ~chickenbit;
-	if (sig->rev < good_rev)
-		val |= chickenbit;
-
-	if (val == old_val)
-		/* Nothing to change. */
-		return;
-
-	/*
-	 * DE_CFG is a Core-scoped MSR, and this write is racy during late
-	 * microcode load.  However, both threads calculate the new value from
-	 * state which is shared, and unrelated to the old value, so the
-	 * result should be consistent.
-	 */
-	wrmsrl(MSR_AMD64_DE_CFG, val);
-
-	/*
-	 * Inform the admin that we changed something, but don't spam,
-	 * especially during a late microcode load.
-	 */
-	if (smp_processor_id() == 0)
-		printk(XENLOG_INFO "Zenbleed mitigation - using %s\n",
-		       val & chickenbit ? "chickenbit" : "microcode");
-}
-
 static void cf_check fam17_disable_c6(void *arg)
 {
 	/* Disable C6 by clearing the CCR{0,1,2}_CC6EN bits. */
@@ -1075,6 +966,108 @@ static void cf_check fam17_disable_c6(void *arg)
 	wrmsrl(MSR_AMD_CSTATE_CFG, val & mask);
 }
 
+static bool zenbleed_use_chickenbit(void)
+{
+    unsigned int curr_rev;
+    uint8_t fixed_rev;
+
+    /* Zenbleed only affects Zen2.  Nothing to do on non-Fam17h systems. */
+    if ( boot_cpu_data.family != 0x17 )
+        return false;
+
+    curr_rev = this_cpu(cpu_sig).rev;
+    switch ( curr_rev >> 8 )
+    {
+    case 0x083010: fixed_rev = 0x7a; break;
+    case 0x086001: fixed_rev = 0x0b; break;
+    case 0x086081: fixed_rev = 0x05; break;
+    case 0x087010: fixed_rev = 0x32; break;
+    case 0x08a000: fixed_rev = 0x08; break;
+    default:
+        /*
+         * With the Fam17h check above, most parts getting here are Zen1.
+         * They're not affected.  Assume Zen2 ones making it here are affected
+         * regardless of microcode version.
+         */
+        return is_zen2_uarch();
+    }
+
+    return (uint8_t)curr_rev >= fixed_rev;
+}
+
+void amd_init_de_cfg(const struct cpuinfo_x86 *c)
+{
+    uint64_t val, new = 0;
+
+    /*
+     * The MSR doesn't exist on Fam 0xf/0x11.  If virtualised, we won't have
+     * mutable access even if we can read it.
+     */
+    if ( c->family == 0xf || c->family == 0x11 || cpu_has_hypervisor )
+        return;
+
+    /*
+     * On Zen3 (Fam 0x19) and later CPUs, LFENCE is unconditionally dispatch
+     * serialising, and is enumerated in CPUID.
+     *
+     * On older systems, LFENCE is unconditionally dispatch serialising (when
+     * the MSR doesn't exist), or can be made so by setting this bit.
+     */
+    if ( !test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        new |= AMD64_DE_CFG_LFENCE_SERIALISE;
+
+    /*
+     * If vulnerable to Zenbleed and not mitigated in microcode, use the
+     * bigger hammer.
+     */
+    if ( zenbleed_use_chickenbit() )
+        new |= (1 << 9);
+
+    if ( !new )
+        return;
+
+    val = rdmsr(MSR_AMD64_DE_CFG);
+
+    if ( (val & new) == new )
+        return;
+
+    /*
+     * DE_CFG is a Core-scoped MSR, and this write is racy.  However, both
+     * threads calculate the new value from state which expected to be
+     * consistent across CPUs and unrelated to the old value, so the result
+     * should be consistent.
+     */
+    wrmsr(MSR_AMD64_DE_CFG, val | new);
+}
+
+void __init amd_init_lfence_dispatch(void)
+{
+    struct cpuinfo_x86 *c = &boot_cpu_data;
+    uint64_t val;
+
+    if ( test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        /* LFENCE is forced dispatch serialising and we can't control it. */
+        return;
+
+    if ( c->family == 0xf || c->family == 0x11 )
+        /* MSR doesn't exist.  LFENCE is dispatch serialising on this hardare. */
+        goto set;
+
+    if ( rdmsr_safe(MSR_AMD64_DE_CFG, &val) )
+        /* Unable to read.  Assume the safer default. */
+        goto clear;
+
+    if ( val & AMD64_DE_CFG_LFENCE_SERIALISE )
+        goto set;
+
+ clear:
+    setup_clear_cpu_cap(X86_FEATURE_LFENCE_DISPATCH);
+    return;
+
+ set:
+    setup_force_cpu_cap(X86_FEATURE_LFENCE_DISPATCH);
+}
+
 static void amd_check_bp_cfg(void)
 {
 	uint64_t val, new = 0;
@@ -1118,6 +1111,11 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 	u32 l, h;
 	uint64_t value;
 
+	amd_init_de_cfg(c);
+
+	if (c == &boot_cpu_data)
+		amd_init_lfence_dispatch(); /* Needs amd_init_de_cfg() */
+
 	/* Disable TLB flush filter by setting HWCR.FFDIS on K8
 	 * bit 6 of msr C001_0015
 	 *
@@ -1156,12 +1154,6 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 	if (c == &boot_cpu_data && !cpu_has(c, X86_FEATURE_RSTR_FP_ERR_PTRS))
 		setup_force_cpu_cap(X86_BUG_FPU_PTRS);
 
-	if (c->x86 == 0x0f || c->x86 == 0x11)
-		/* Always dispatch serialising on this hardare. */
-		__set_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability);
-	else /* Implicily "== 0x10 || >= 0x12" by being 64bit. */
-		amd_init_lfence(c);
-
 	amd_init_ssbd(c);
 
 	if (c->x86 == 0x17)
@@ -1379,7 +1371,6 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 	if ((smp_processor_id() == 1) && !cpu_has(c, X86_FEATURE_ITSC))
 		disable_c1_ramping();
 
-	amd_check_zenbleed();
 	amd_check_bp_cfg();
 
 	if (fam17_c6_disabled)
xen/arch/x86/cpu/cpu.h +2 -1
index cbb434f3a23d..8bed3f52490f 100644
@@ -24,7 +24,8 @@ extern bool detect_extended_topology(struct cpuinfo_x86 *c);
 
 void cf_check early_init_amd(struct cpuinfo_x86 *c);
 void amd_log_freq(const struct cpuinfo_x86 *c);
-void amd_init_lfence(struct cpuinfo_x86 *c);
+void amd_init_de_cfg(const struct cpuinfo_x86 *c);
+void amd_init_lfence_dispatch(void);
 void amd_init_ssbd(const struct cpuinfo_x86 *c);
 void amd_init_spectral_chicken(void);
 void detect_zen2_null_seg_behaviour(void);
xen/arch/x86/cpu/hygon.c +5 -1
index f7508cc8fcb9..c3faabbdb764 100644
@@ -31,7 +31,11 @@ static void cf_check init_hygon(struct cpuinfo_x86 *c)
 {
 	unsigned long long value;
 
-	amd_init_lfence(c);
+	amd_init_de_cfg(c);
+
+	if (c == &boot_cpu_data)
+		amd_init_lfence_dispatch(); /* Needs amd_init_de_cfg() */
+
 	amd_init_ssbd(c);
 
 	/* Probe for NSCB on Zen2 CPUs when not virtualised */

base-commit: fb0e37df71a31318c61e0715ffed3e149ca8a4aa
prerequisite-patch-id: 3598b7168c11c3dd68f825c75b77edc552fc231f
prerequisite-patch-id: e57b8bc06e86a4470b5bcf8e412d248e1f8f3331

Signature · 1 line
2.39.5
Reviewed-by on v1 3/3
#19 lore raw

Re: [PATCH v1.9 3/3] x86/amd: Fix race editing DE_CFG

Quoted 13 lines · Andrew Cooper

We have two different functions explaining that DE_CFG is Core-scoped and that writes are racy but happen to be safe. This is only true when there's one of them.

Introduce amd_init_de_cfg() to be the singular function which writes to DE_CFG, modelled after the logic we already have for BP_CFG.

While reworking amd_check_zenbleed() into a simple predicate used by amd_init_de_cfg(), fix the microcode table. The 'good_rev' was specific to an individual stepping and not valid to be matched by model, let alone a range. The only CPUs incorrectly matched that I can locate appear to be pre-production, and probably didn't get Zenbleed microcode.

Quoted 6 lines

Rework amd_init_lfence() to be amd_init_lfence_dispatch() with only the purpose of configuring X86_FEATURE_LFENCE_DISPATCH in the case that it needs synthesising. Run it on the BSP only and use setup_force_cpu_cap() to prevent the bit disappearing on a subseuqent CPUID rescan.

with one more request towards commentary:

Quoted 33 lines
xen/arch/x86/cpu/amd.c +33 -0
+void amd_init_de_cfg(const struct cpuinfo_x86 *c)
+{
+    uint64_t val, new = 0;
+
+    /*
+     * The MSR doesn't exist on Fam 0xf/0x11.  If virtualised, we won't have
+     * mutable access even if we can read it.
+     */
+    if ( c->family == 0xf || c->family == 0x11 || cpu_has_hypervisor )
+        return;
+
+    /*
+     * On Zen3 (Fam 0x19) and later CPUs, LFENCE is unconditionally dispatch
+     * serialising, and is enumerated in CPUID.
+     *
+     * On older systems, LFENCE is unconditionally dispatch serialising (when
+     * the MSR doesn't exist), or can be made so by setting this bit.
+     */
+    if ( !test_bit(X86_FEATURE_LFENCE_DISPATCH, c->x86_capability) )
+        new |= AMD64_DE_CFG_LFENCE_SERIALISE;
+
+    /*
+     * If vulnerable to Zenbleed and not mitigated in microcode, use the
+     * bigger hammer.
+     */
+    if ( zenbleed_use_chickenbit() )
+        new |= (1 << 9);
+
+    if ( !new )
+        return;
+
+    val = rdmsr(MSR_AMD64_DE_CFG);
+

xen/arch/x86/cpu/amd.c static void cf_check fam17_disable_c6(void *arg) · line 1040

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+    if ( (val & new) == new )
+        return;
+
+    /*
+     * DE_CFG is a Core-scoped MSR, and this write is racy.  However, both
+     * threads calculate the new value from state which expected to be
+     * consistent across CPUs and unrelated to the old value, so the result
+     * should be consistent.
+     */
+    wrmsr(MSR_AMD64_DE_CFG, val | new);

The reason this isn't at risk of faulting when potentially trying to set a reserved bit would better be at least briefly mentioned here, I think. Yes, logic above tries to eliminate all cases where either bit may be reserved, but we're still on somewhat thin ice here.

Jan