Skip to contentSkip to conversation

x86/ucode: Refresh raw CPU policy after microcode load

PATCH Superseded Andrew Cooper 3 participants 11 messages May 3, 2023 – May 4, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/ucode: Refresh raw CPU policy after microcode load

Loading microcode can cause new features to appear. This has happened routinely since Spectre/Meltdown, and even the presence of new status bits can mean the administrator has no further work to perform.

Refresh the raw CPU policy after late microcode load, so xen-cpuid can reflect the updated state of the system.

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

This is also the first step of being able to livepatch support for new functionality in microcode.

---
 xen/arch/x86/cpu-policy.c             | 6 +++---
 xen/arch/x86/cpu/microcode/core.c     | 4 ++++
 xen/arch/x86/include/asm/cpu-policy.h | 6 ++++++
 3 files changed, 13 insertions(+), 3 deletions(-)
xen/arch/x86/cpu-policy.c +3 -3
index a58bf6cad54e..ef6a2d0d180a 100644
@@ -15,7 +15,7 @@
 #include <asm/setup.h>
 #include <asm/xstate.h>
 
-struct cpu_policy __ro_after_init     raw_cpu_policy;
+struct cpu_policy __read_mostly       raw_cpu_policy;
 struct cpu_policy __ro_after_init    host_cpu_policy;
 #ifdef CONFIG_PV
 struct cpu_policy __ro_after_init  pv_max_cpu_policy;
@@ -343,7 +343,7 @@ static void recalculate_misc(struct cpu_policy *p)
     }
 }
 
-static void __init calculate_raw_policy(void)
+void calculate_raw_cpu_policy(void)
 {
     struct cpu_policy *p = &raw_cpu_policy;
 
@@ -655,7 +655,7 @@ static void __init calculate_hvm_def_policy(void)
 
 void __init init_guest_cpu_policies(void)
 {
-    calculate_raw_policy();
+    calculate_raw_cpu_policy();
     calculate_host_policy();
 
     if ( IS_ENABLED(CONFIG_PV) )
xen/arch/x86/cpu/microcode/core.c +4 -0
index 61cd36d601d6..cd456c476fbf 100644
@@ -34,6 +34,7 @@
 #include <xen/watchdog.h>
 
 #include <asm/apic.h>
+#include <asm/cpu-policy.h>
 #include <asm/delay.h>
 #include <asm/nmi.h>
 #include <asm/processor.h>
@@ -677,6 +678,9 @@ static long cf_check microcode_update_helper(void *data)
         spin_lock(&microcode_mutex);
         microcode_update_cache(patch);
         spin_unlock(&microcode_mutex);
+
+        /* Refresh the raw CPU policy, in case the features have changed. */
+        calculate_raw_cpu_policy();
     }
     else
         microcode_free_patch(patch);
xen/arch/x86/include/asm/cpu-policy.h +6 -0
index b361537a602b..99d5a8e67eeb 100644
@@ -24,4 +24,10 @@ void init_dom0_cpuid_policy(struct domain *d);
 /* Clamp the CPUID policy to reality. */
 void recalculate_cpuid_policy(struct domain *d);
 
+/*
+ * Collect the raw CPUID and MSR values.  Called during boot, and after late
+ * microcode loading.
+ */
+void calculate_raw_cpu_policy(void);
+
 #endif /* X86_CPU_POLICY_H */
Signature · 1 line
2.30.2
#1 lore raw
Quoted 1 line · Andrew Cooper

Loading microcode can cause new features to appear.

Or disappear (LWP)? While I don't think we want to panic() in this case (we do on the S3 resume path when recheck_cpu_features() fails on the BSP), it would seem to me that we want to go a step further than you do and at least warn when a feature went amiss. Or is that too much of a scope-creeping request right here?

xen/arch/x86/cpu/microcode/core.c static long cf_check microcode_update_helper(void *data) · line 683

Quoted 7 lines
xen/arch/x86/cpu/microcode/core.c +3 -0
@@ -677,6 +678,9 @@ static long cf_check microcode_update_helper(void *data)
         spin_lock(&microcode_mutex);
         microcode_update_cache(patch);
         spin_unlock(&microcode_mutex);
+
+        /* Refresh the raw CPU policy, in case the features have changed. */
+        calculate_raw_cpu_policy();

I understand this is in line with what we do during boot, but there and here I wonder whether this wouldn't better deal with possible asymmetries (e.g. in case ucode loading failed on one of the CPUs), along the lines of what we do near the end of identify_cpu() for APs. (Unlike the question higher up, this is definitely only a remark here, not something I'd consider dealing with right in this change.)

Jan

#2 lore raw
Quoted 7 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

Loading microcode can cause new features to appear.

Or disappear (LWP)? While I don't think we want to panic() in this case (we do on the S3 resume path when recheck_cpu_features() fails on the BSP), it would seem to me that we want to go a step further than you do and at least warn when a feature went amiss. Or is that too much of a scope-creeping request right here?

You're correct that I ought to discuss the disappear case.  But like livepatching, it's firmly in the realm of "the end user takes responsibility for trying this in their test system before running it in production".

For LWP specifically, we ought to explicitly permit its disappearance in recheck_cpu_features(), because this specific example is known to exist, and known safe as Xen never used or virtualised LWP functionality. Crashing on S3

Quoted 15 lines

xen/arch/x86/cpu/microcode/core.c static long cf_check microcode_update_helper(void *data) · line 683

Quoted 8 lines
xen/arch/x86/cpu/microcode/core.c +3 -0
@@ -677,6 +678,9 @@ static long cf_check microcode_update_helper(void *data)
         spin_lock(&microcode_mutex);
         microcode_update_cache(patch);
         spin_unlock(&microcode_mutex);
+
+        /* Refresh the raw CPU policy, in case the features have changed. */
+        calculate_raw_cpu_policy();

I understand this is in line with what we do during boot, but there and here I wonder whether this wouldn't better deal with possible asymmetries (e.g. in case ucode loading failed on one of the CPUs), along the lines of what we do near the end of identify_cpu() for APs. (Unlike the question higher up, this is definitely only a remark here, not something I'd consider dealing with right in this change.)

Asymmetry is an increasingly theoretical problem.  Yeah, it exists in principle, but Xen has no way of letting you explicitly get into that situation.

This too falls firmly into the "end user takes responsibility for testing it properly first" category.

We have explicit symmetric assumptions/requirements elsewhere (e.g. for a single system, there's 1 correct ucode blob).

We can acknowledge that asymmetry exists, but there is basically nothing Xen can do about it other than highlight that something is very wrong on the system.  Odds are that a system which gets into such a state won't survive much longer.

~Andrew

Reviewed-by
#3 lore raw
Quoted 9 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

Loading microcode can cause new features to appear.

Or disappear (LWP)? While I don't think we want to panic() in this case (we do on the S3 resume path when recheck_cpu_features() fails on the BSP), it would seem to me that we want to go a step further than you do and at least warn when a feature went amiss. Or is that too much of a scope-creeping request right here?

Quoted 4 lines

You're correct that I ought to discuss the disappear case.  But like livepatching, it's firmly in the realm of "the end user takes responsibility for trying this in their test system before running it in production".

Okay, with the case at least suitably mentioned

Quoted 3 lines

For LWP specifically, we ought to explicitly permit its disappearance in recheck_cpu_features(), because this specific example is known to exist, and known safe as Xen never used or virtualised LWP functionality.

Right, but iirc we did expose it to guests earlier on. And I've used it as a known example only anyway. Who knows what vendors decide to make disappear the next time round ...

Quoted 1 line

Crashing on S3

I may guess you meant to continue "... is bad anyway"?

Quoted 22 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/cpu/microcode/core.c +2 -0
@@ -677,6 +678,9 @@ static long cf_check microcode_update_helper(void *data)
         spin_lock(&microcode_mutex);
         microcode_update_cache(patch);
         spin_unlock(&microcode_mutex);
+
+        /* Refresh the raw CPU policy, in case the features have changed. */
Quoted 8 lines

Review static long cf_check microcode_update_helper(void *data) · line 683

Quoted 1 line
(quoted diff) +1 -0
+        calculate_raw_cpu_policy();

I understand this is in line with what we do during boot, but there and here I wonder whether this wouldn't better deal with possible asymmetries (e.g. in case ucode loading failed on one of the CPUs), along the lines of what we do near the end of identify_cpu() for APs. (Unlike the question higher up, this is definitely only a remark here, not something I'd consider dealing with right in this change.)

Asymmetry is an increasingly theoretical problem.  Yeah, it exists in principle, but Xen has no way of letting you explicitly get into that situation.

This too falls firmly into the "end user takes responsibility for testing it properly first" category.

Quoted 7 lines

We have explicit symmetric assumptions/requirements elsewhere (e.g. for a single system, there's 1 correct ucode blob).

We can acknowledge that asymmetry exists, but there is basically nothing Xen can do about it other than highlight that something is very wrong on the system.  Odds are that a system which gets into such a state won't survive much longer.

Indeed. Hence the desire to at least log the fact, such that investigation of the sudden death won't take long.

Jan

#4 lore raw
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

Loading microcode can cause new features to appear.

Or disappear (LWP)? While I don't think we want to panic() in this case (we do on the S3 resume path when recheck_cpu_features() fails on the BSP), it would seem to me that we want to go a step further than you do and at least warn when a feature went amiss. Or is that too much of a scope-creeping request right here?

Quoted 6 lines
Quoted 4 lines

You're correct that I ought to discuss the disappear case.  But like livepatching, it's firmly in the realm of "the end user takes responsibility for trying this in their test system before running it in production".

Okay, with the case at least suitably mentioned

Thanks,

Quoted 6 lines
Quoted 3 lines

For LWP specifically, we ought to explicitly permit its disappearance in recheck_cpu_features(), because this specific example is known to exist, and known safe as Xen never used or virtualised LWP functionality.

Right, but iirc we did expose it to guests earlier on. And I've used it as a known example only anyway. Who knows what vendors decide to make disappear the next time round ...

It's true that we used to expose the CPUID bit to guests, but IIRC we never virtualised it correctly which was my justification for hiding the feature bit when I was doing the original work to rationalise what guests saw.


Removing LWP was an extraordinary situation, and AMD didn't do it lightly.

What they did was sacrifice a fairly expensive LWP errata workaround to make space ucode space for IBPB instead.  They hid the CPUID bit (and specifically by modifying the CPUID mask MSR only) because there were no known production users of LWP, and a consequence of the patch, an errata got unfixed.

On real AMD systems which used to enumerate LWP, and subsequently ceased to, it was only "normal virt" levels of feature hiding.  The LWP CPUID leaf still has real data in it, and you can still use %xcr0.lwp, and the LWP MSRs still function.

Software which was genuinely using LWP, not tickling the errata case, and late loaded this patch wouldn't actually have anything malfunction underfoot.

Quoted 2 lines
Quoted 1 line

Crashing on S3

I may guess you meant to continue "... is bad anyway"?

Oops.  I was clearly in a rush for my morning meeting.  What I meant was "we probably shouldn't crash on S3 resume in this known-ok case".

~Andrew

#5 lore raw
In reply to Andrew Cooper
Quoted 14 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

Loading microcode can cause new features to appear.

Or disappear (LWP)? While I don't think we want to panic() in this case (we do on the S3 resume path when recheck_cpu_features() fails on the BSP), it would seem to me that we want to go a step further than you do and at least warn when a feature went amiss. Or is that too much of a scope-creeping request right here?

You're correct that I ought to discuss the disappear case.  But like livepatching, it's firmly in the realm of "the end user takes responsibility for trying this in their test system before running it in production".

Quoted 4 lines

For LWP specifically, we ought to explicitly permit its disappearance in recheck_cpu_features(), because this specific example is known to exist, and known safe as Xen never used or virtualised LWP functionality. Crashing on S3

Printing disappearing features might be a nice bonus, but could be done from the tool that loads the microcode itself, no need to do such work in the hypervisor IMO.

Quoted 23 lines
Quoted 15 lines

xen/arch/x86/cpu/microcode/core.c static long cf_check microcode_update_helper(void *data) · line 683

Quoted 8 lines
xen/arch/x86/cpu/microcode/core.c +3 -0
@@ -677,6 +678,9 @@ static long cf_check microcode_update_helper(void *data)
         spin_lock(&microcode_mutex);
         microcode_update_cache(patch);
         spin_unlock(&microcode_mutex);
+
+        /* Refresh the raw CPU policy, in case the features have changed. */
+        calculate_raw_cpu_policy();

I understand this is in line with what we do during boot, but there and here I wonder whether this wouldn't better deal with possible asymmetries (e.g. in case ucode loading failed on one of the CPUs), along the lines of what we do near the end of identify_cpu() for APs. (Unlike the question higher up, this is definitely only a remark here, not something I'd consider dealing with right in this change.)

Asymmetry is an increasingly theoretical problem.  Yeah, it exists in principle, but Xen has no way of letting you explicitly get into that situation.

This too falls firmly into the "end user takes responsibility for testing it properly first" category.

Quoted 7 lines

We have explicit symmetric assumptions/requirements elsewhere (e.g. for a single system, there's 1 correct ucode blob).

We can acknowledge that asymmetry exists, but there is basically nothing Xen can do about it other than highlight that something is very wrong on the system.  Odds are that a system which gets into such a state won't survive much longer.

Would it make sense to only update the CPU policy if updated == nr_cores?

Thanks, Roger.

#6 lore raw
Quoted 15 lines · Roger Pau Monné
Quoted 14 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

Loading microcode can cause new features to appear.

Or disappear (LWP)? While I don't think we want to panic() in this case (we do on the S3 resume path when recheck_cpu_features() fails on the BSP), it would seem to me that we want to go a step further than you do and at least warn when a feature went amiss. Or is that too much of a scope-creeping request right here?

You're correct that I ought to discuss the disappear case.  But like livepatching, it's firmly in the realm of "the end user takes responsibility for trying this in their test system before running it in production".

Quoted 8 lines
Quoted 4 lines

For LWP specifically, we ought to explicitly permit its disappearance in recheck_cpu_features(), because this specific example is known to exist, and known safe as Xen never used or virtualised LWP functionality. Crashing on S3

Printing disappearing features might be a nice bonus, but could be done from the tool that loads the microcode itself, no need to do such work in the hypervisor IMO.

Except that the system may not last long enough for the (or any) tool to actually make it to producing such output, let alone any human actually observing it (when that output isn't necessarily going to some remote system).

Jan

#7 lore raw
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Roger Pau Monné
Quoted 13 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

Loading microcode can cause new features to appear.

Or disappear (LWP)? While I don't think we want to panic() in this case (we do on the S3 resume path when recheck_cpu_features() fails on the BSP), it would seem to me that we want to go a step further than you do and at least warn when a feature went amiss. Or is that too much of a scope-creeping request right here?

You're correct that I ought to discuss the disappear case.  But like livepatching, it's firmly in the realm of "the end user takes responsibility for trying this in their test system before running it in production".

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

For LWP specifically, we ought to explicitly permit its disappearance in recheck_cpu_features(), because this specific example is known to exist, and known safe as Xen never used or virtualised LWP functionality. Crashing on S3

Printing disappearing features might be a nice bonus, but could be done from the tool that loads the microcode itself, no need to do such work in the hypervisor IMO.

Xen is really the only entity in a position to judge when stuff has gone away, so this can't really be deferred to another tool.

We have the X86_FEATURE_* names during boot for parsing the {dom0-}cpuid= command line options, but we don't keep this beyond init

Quoted 4 lines

Except that the system may not last long enough for the (or any) tool to actually make it to producing such output, let alone any human actually observing it (when that output isn't necessarily going to some remote system).

Yeah, xl dmesg/serial/whatever is the one place liable for anything like this to get noticed.

~Andrew

#8 lore raw

PATCH v2 1/1 x86/ucode: Refresh raw CPU policy after microcode load

In reply to Andrew Cooper

Loading microcode can cause new features to appear. This has happened routinely since Spectre/Meltdown, and even the presence of new status bits can sometimes mean the administrator has no further actions to perform.

Conversely, loading microcode can occasionally cause features to disappear. As with livepatching, it is very much the administrators responsibility to confirm that a late microcode load is safe on the intended system before rolling it out in production.

Refresh the raw CPU policy after late microcode load appears to have done something, so xen-cpuid can reflect the updated state of the system.

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

This is also the first step of being able to livepatch support for new functionality in microcode.

v2:
* Discuss disappearing features in the commit message

---
 xen/arch/x86/cpu-policy.c             | 6 +++---
 xen/arch/x86/cpu/microcode/core.c     | 4 ++++
 xen/arch/x86/include/asm/cpu-policy.h | 6 ++++++
 3 files changed, 13 insertions(+), 3 deletions(-)
xen/arch/x86/cpu-policy.c +3 -3
index a58bf6cad54e..ef6a2d0d180a 100644
@@ -15,7 +15,7 @@
 #include <asm/setup.h>
 #include <asm/xstate.h>
 
-struct cpu_policy __ro_after_init     raw_cpu_policy;
+struct cpu_policy __read_mostly       raw_cpu_policy;
 struct cpu_policy __ro_after_init    host_cpu_policy;
 #ifdef CONFIG_PV
 struct cpu_policy __ro_after_init  pv_max_cpu_policy;
@@ -343,7 +343,7 @@ static void recalculate_misc(struct cpu_policy *p)
     }
 }
 
-static void __init calculate_raw_policy(void)
+void calculate_raw_cpu_policy(void)
 {
     struct cpu_policy *p = &raw_cpu_policy;
 
@@ -655,7 +655,7 @@ static void __init calculate_hvm_def_policy(void)
 
 void __init init_guest_cpu_policies(void)
 {
-    calculate_raw_policy();
+    calculate_raw_cpu_policy();
     calculate_host_policy();
 
     if ( IS_ENABLED(CONFIG_PV) )
xen/arch/x86/cpu/microcode/core.c +4 -0
index 61cd36d601d6..cd456c476fbf 100644
@@ -34,6 +34,7 @@
 #include <xen/watchdog.h>
 
 #include <asm/apic.h>
+#include <asm/cpu-policy.h>
 #include <asm/delay.h>
 #include <asm/nmi.h>
 #include <asm/processor.h>
@@ -677,6 +678,9 @@ static long cf_check microcode_update_helper(void *data)
         spin_lock(&microcode_mutex);
         microcode_update_cache(patch);
         spin_unlock(&microcode_mutex);
+
+        /* Refresh the raw CPU policy, in case the features have changed. */
+        calculate_raw_cpu_policy();
     }
     else
         microcode_free_patch(patch);
xen/arch/x86/include/asm/cpu-policy.h +6 -0
index b361537a602b..99d5a8e67eeb 100644
@@ -24,4 +24,10 @@ void init_dom0_cpuid_policy(struct domain *d);
 /* Clamp the CPUID policy to reality. */
 void recalculate_cpuid_policy(struct domain *d);
 
+/*
+ * Collect the raw CPUID and MSR values.  Called during boot, and after late
+ * microcode loading.
+ */
+void calculate_raw_cpu_policy(void);
+
 #endif /* X86_CPU_POLICY_H */
Signature · 1 line
2.30.2
Acked-by on v2 1/1
#9 lore raw
Quoted 9 lines · Andrew Cooper

Loading microcode can cause new features to appear. This has happened routinely since Spectre/Meltdown, and even the presence of new status bits can sometimes mean the administrator has no further actions to perform.

Conversely, loading microcode can occasionally cause features to disappear. As with livepatching, it is very much the administrators responsibility to confirm that a late microcode load is safe on the intended system before rolling it out in production.

Quoted 5 lines

Refresh the raw CPU policy after late microcode load appears to have done something, so xen-cpuid can reflect the updated state of the system.

I'm not fully sure it's worth calling calculate_raw_cpu_policy() if updated != nr_cores, as it's possible the current CPU where the policy is regenerated hasn't been updated.

Thanks, Roger.

#10 lore raw
Quoted 10 lines · Roger Pau Monné
Quoted 9 lines · Andrew Cooper

Loading microcode can cause new features to appear. This has happened routinely since Spectre/Meltdown, and even the presence of new status bits can sometimes mean the administrator has no further actions to perform.

Conversely, loading microcode can occasionally cause features to disappear. As with livepatching, it is very much the administrators responsibility to confirm that a late microcode load is safe on the intended system before rolling it out in production.

Quoted 6 lines
Quoted 5 lines

Refresh the raw CPU policy after late microcode load appears to have done something, so xen-cpuid can reflect the updated state of the system.

Thanks.

Quoted 3 lines

I'm not fully sure it's worth calling calculate_raw_cpu_policy() if updated != nr_cores, as it's possible the current CPU where the policy is regenerated hasn't been updated.

As said previously, updated != nr_cores may exist in principle, but it is not a corner case we should care about.

If the system really is asymmetric after a late load, then it is probably not long for the world.  There is nothing Xen can do to recover.

It is awkward that we're not on the BSP when collecting.  I think that part of the logic didn't survive the rebase over switching to stop-machine.  I'll add it to the gitlab ticket of minor ucode work, because it's not the end of the world.

~Andrew