Skip to contentSkip to conversation

x86/cpu-policy: set up host policy earlier

PATCH Superseded Jan Beulich 3 participants 5 messages Apr 7 – Jul 2

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/cpu-policy: set up host policy earlier

In order to use cpu_has_* expanding to host_cpu_policy.*, the host policy needs setting up alongside boot_cpu_data.x86_capability[]. Arrange for that towards the end of identify_cpu().

Fixes 894bd7617924 ("x86/Intel: use host CPU policy for ARAT checking")

---
In the longer run we may need to (also) update the host policy from setup_{force,clear}_cpu_cap().

This really was part of one of the XSTATE cleanup patches at the head of the AMX series. This explains the specific placement ahead of the call to xstate_init()). Likely anything from there downwards shouldn't really live in identify_cpu() anyway.

xen/arch/x86/cpu/common.c +2 -0
@@ -586,6 +586,8 @@ void identify_cpu(struct cpuinfo_x86 *c)
 	}
 
 	/* Now the feature flags better reflect actual CPU features! */
+	if (c == &boot_cpu_data)
+		calculate_host_policy();
 
 	xstate_init(c);
 
xen/arch/x86/cpu-policy.c +2 -1
@@ -359,7 +359,7 @@ void calculate_raw_cpu_policy(void)
     /* Was already added by probe_cpuid_faulting() */
 }
 
-static void __init calculate_host_policy(void)
+void __init calculate_host_policy(void)
 {
     struct cpu_policy *p = &host_cpu_policy;
     unsigned int max_extd_leaf;
@@ -904,6 +904,7 @@ static void __init calculate_hvm_def_pol
 
 void __init init_guest_cpu_policies(void)
 {
+    /* Do this a 2nd time to account for setup_{clear,force}_cpu_cap() uses. */
     calculate_host_policy();
 
     if ( IS_ENABLED(CONFIG_PV) )
xen/arch/x86/include/asm/cpu-policy.h +6 -0
@@ -30,4 +30,10 @@ void recalculate_cpuid_policy(struct dom
  */
 void calculate_raw_cpu_policy(void);
 
+/*
+ * Collect the host CPU policy.  Called after collecting enough CPUID output,
+ * and again after all feature overrides have been put in place.
+ */
+void calculate_host_policy(void);
+
 #endif /* X86_CPU_POLICY_H */
Tested-by on v1 1/1
#1 lore raw
Quoted 7 lines · Jan Beulich

In order to use cpu_has_* expanding to host_cpu_policy.*, the host policy needs setting up alongside boot_cpu_data.x86_capability[]. Arrange for that towards the end of identify_cpu().

Fixes 894bd7617924 ("x86/Intel: use host CPU policy for ARAT checking")
Quoted 52 lines

---
In the longer run we may need to (also) update the host policy from setup_{force,clear}_cpu_cap().

This really was part of one of the XSTATE cleanup patches at the head of the AMX series. This explains the specific placement ahead of the call to xstate_init()). Likely anything from there downwards shouldn't really live in identify_cpu() anyway.

xen/arch/x86/cpu/common.c +2 -0
@@ -586,6 +586,8 @@ void identify_cpu(struct cpuinfo_x86 *c)
 	}
 
 	/* Now the feature flags better reflect actual CPU features! */
+	if (c == &boot_cpu_data)
+		calculate_host_policy();
 
 	xstate_init(c);
 
xen/arch/x86/cpu-policy.c +2 -1
@@ -359,7 +359,7 @@ void calculate_raw_cpu_policy(void)
     /* Was already added by probe_cpuid_faulting() */
 }
 
-static void __init calculate_host_policy(void)
+void __init calculate_host_policy(void)
 {
     struct cpu_policy *p = &host_cpu_policy;
     unsigned int max_extd_leaf;
@@ -904,6 +904,7 @@ static void __init calculate_hvm_def_pol
 
 void __init init_guest_cpu_policies(void)
 {
+    /* Do this a 2nd time to account for setup_{clear,force}_cpu_cap() uses. */
     calculate_host_policy();
 
     if ( IS_ENABLED(CONFIG_PV) )
xen/arch/x86/include/asm/cpu-policy.h +6 -0
@@ -30,4 +30,10 @@ void recalculate_cpuid_policy(struct dom
  */
 void calculate_raw_cpu_policy(void);
 
+/*
+ * Collect the host CPU policy.  Called after collecting enough CPUID output,
+ * and again after all feature overrides have been put in place.
+ */
+void calculate_host_policy(void);
+
 #endif /* X86_CPU_POLICY_H */
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#2 lore raw
In reply to Jan Beulich
Quoted 19 lines · Jan Beulich

In order to use cpu_has_* expanding to host_cpu_policy.*, the host policy needs setting up alongside boot_cpu_data.x86_capability[]. Arrange for that towards the end of identify_cpu().

Fixes 894bd7617924 ("x86/Intel: use host CPU policy for ARAT checking")

---
In the longer run we may need to (also) update the host policy from setup_{force,clear}_cpu_cap().

This really was part of one of the XSTATE cleanup patches at the head of the AMX series. This explains the specific placement ahead of the call to xstate_init()). Likely anything from there downwards shouldn't really live in identify_cpu() anyway.

xen/arch/x86/cpu/common.c void identify_cpu(struct cpuinfo_x86 *c) · line 590

Quoted 5 lines
xen/arch/x86/cpu/common.c +2 -0
@@ -586,6 +586,8 @@ void identify_cpu(struct cpuinfo_x86 *c)
 	}
 
 	/* Now the feature flags better reflect actual CPU features! */
+	if (c == &boot_cpu_data)
+		calculate_host_policy();

calculate_host_cpu_policy() now it's not local to cpu-policy.c

Quoted 15 lines
xen/arch/x86/cpu/common.c +0 -0
 	xstate_init(c);
 
xen/arch/x86/cpu-policy.c +1 -1
@@ -359,7 +359,7 @@ void calculate_raw_cpu_policy(void)
     /* Was already added by probe_cpuid_faulting() */
 }
 
-static void __init calculate_host_policy(void)
+void __init calculate_host_policy(void)
 {
     struct cpu_policy *p = &host_cpu_policy;
     unsigned int max_extd_leaf;

xen/arch/x86/cpu-policy.c static void __init calculate_hvm_def_pol · line 908

Quoted 5 lines
xen/arch/x86/cpu-policy.c +1 -0
@@ -904,6 +904,7 @@ static void __init calculate_hvm_def_pol
 
 void __init init_guest_cpu_policies(void)
 {
+    /* Do this a 2nd time to account for setup_{clear,force}_cpu_cap() uses. */
     calculate_host_policy();

I'm sorry, but no.  The cleanup work to CPUID handling is in an attempt to purge this antipattern.

Double-initialising data structures *is* the entire reason we have order-of-boot problems in the first place.  Having a structure which is initialised but wrong is itself wrong.

Really, I should insist on a revert of switching cpu_has_* to use host_policy.  It was not adequately tested and is not compatible with how Xen starts up.

But as I expect you're going to object to that feedback, I'll tolerate setup_*_cpu_cap() keeping the host policy correct as they're called. The easy option is to call x86_cpu_featureset_to_policy() each time. The lower overhead option would be to have a table similar to cpu_user_regs_gpr_offsets[] and use that to locate the feature word in a policy object as it's being edited, although it's yet another thing to remember to keep in sync when adding new words.

~Andrew

#3 lore raw
Quoted 11 lines · Andrew Cooper

xen/arch/x86/cpu/common.c void identify_cpu(struct cpuinfo_x86 *c) · line 590

Quoted 8 lines · Jan Beulich
xen/arch/x86/cpu/common.c +2 -0
@@ -586,6 +586,8 @@ void identify_cpu(struct cpuinfo_x86 *c)
 	}
 
 	/* Now the feature flags better reflect actual CPU features! */
+	if (c == &boot_cpu_data)
+		calculate_host_policy();

calculate_host_cpu_policy() now it's not local to cpu-policy.c

Sure.

Quoted 13 lines
Quoted 13 lines
xen/arch/x86/cpu-policy.c +1 -1
@@ -359,7 +359,7 @@ void calculate_raw_cpu_policy(void)
     /* Was already added by probe_cpuid_faulting() */
 }
 
-static void __init calculate_host_policy(void)
+void __init calculate_host_policy(void)
 {
     struct cpu_policy *p = &host_cpu_policy;
     unsigned int max_extd_leaf;
@@ -904,6 +904,7 @@ static void __init calculate_hvm_def_pol
 
Quoted 7 lines

xen/arch/x86/cpu-policy.c static void __init calculate_hvm_def_pol · line 908

Quoted 4 lines
xen/arch/x86/cpu-policy.c +1 -0
 void __init init_guest_cpu_policies(void)
 {
+    /* Do this a 2nd time to account for setup_{clear,force}_cpu_cap() uses. */
     calculate_host_policy();

I'm sorry, but no.  The cleanup work to CPUID handling is in an attempt to purge this antipattern.

And it's not meant to stay here, once that cleanup work has further progressed. We did discuss this on one of the recent calls. I'm happy to ...

Quoted 3 lines

Double-initialising data structures *is* the entire reason we have order-of-boot problems in the first place.  Having a structure which is initialised but wrong is itself wrong.

... go further, just that I don't think what you suggest below will work.

Quoted 3 lines

Really, I should insist on a revert of switching cpu_has_* to use host_policy.  It was not adequately tested and is not compatible with how Xen starts up.

You realize that there was no "switching cpu_has_* to use host_policy"? There's no representation of leaf 6 in x86_capabilities[], and it seemed awkward to introduce it when we're really moving to using the policies.

Quoted 3 lines

But as I expect you're going to object to that feedback, I'll tolerate setup_*_cpu_cap() keeping the host policy correct as they're called. The easy option is to call x86_cpu_featureset_to_policy() each time.

Except that this isn't going to be enough. It may not need to be all of what calculate_host_policy() currently does, but at the very least recalculate_xstate() will imo need calling. Yet any cpu_has_* used there and changing value would also have a knock-on effect. Overall it then looks less error prone to me to call calculate_host_cpu_policy() from there (then very obviously eliminating the need for the call from init_guest_cpu_policies()), accepting the slight extra overhead.

Quoted 4 lines

The lower overhead option would be to have a table similar to cpu_user_regs_gpr_offsets[] and use that to locate the feature word in a policy object as it's being edited, although it's yet another thing to remember to keep in sync when adding new words.

As per above multiple feature words may be affected by a single change. Hence I think this isn't an option.

Jan

#4 lore raw
In reply to Andrew Cooper
Quoted 11 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich

In order to use cpu_has_* expanding to host_cpu_policy.*, the host policy needs setting up alongside boot_cpu_data.x86_capability[]. Arrange for that towards the end of identify_cpu().

Fixes 894bd7617924 ("x86/Intel: use host CPU policy for ARAT checking")

---
In the longer run we may need to (also) update the host policy from setup_{force,clear}_cpu_cap().

I'm noticing only while making the change that I did already suggest here what ...

Quoted 25 lines
Quoted 12 lines
xen/arch/x86/cpu-policy.c +1 -1
@@ -359,7 +359,7 @@ void calculate_raw_cpu_policy(void)
     /* Was already added by probe_cpuid_faulting() */
 }
 
-static void __init calculate_host_policy(void)
+void __init calculate_host_policy(void)
 {
     struct cpu_policy *p = &host_cpu_policy;
     unsigned int max_extd_leaf;

xen/arch/x86/cpu-policy.c static void __init calculate_hvm_def_pol · line 908

Quoted 5 lines
xen/arch/x86/cpu-policy.c +1 -0
@@ -904,6 +904,7 @@ static void __init calculate_hvm_def_pol
 
 void __init init_guest_cpu_policies(void)
 {
+    /* Do this a 2nd time to account for setup_{clear,force}_cpu_cap() uses. */
     calculate_host_policy();

I'm sorry, but no.  The cleanup work to CPUID handling is in an attempt to purge this antipattern.

Double-initialising data structures *is* the entire reason we have order-of-boot problems in the first place.  Having a structure which is initialised but wrong is itself wrong.

Quoted 7 lines

Really, I should insist on a revert of switching cpu_has_* to use host_policy.  It was not adequately tested and is not compatible with how Xen starts up.

But as I expect you're going to object to that feedback, I'll tolerate setup_*_cpu_cap() keeping the host policy correct as they're called. The easy option is to call x86_cpu_featureset_to_policy() each time.

... you're effectively (see my earlier reply) asking for here.

Jan