Skip to contentSkip to conversation

x86/apic: Fix function typechecking in TSC Deadline errata check

PATCH In review Andrew Cooper 2 participants 3 messages Mar 21, 2022 – Apr 2, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/apic: Fix function typechecking in TSC Deadline errata check

Sadly, cf_check typechecking doesn't work through casts. Introduce an ad-hoc typecheck and fix *_readline_rev() checks to be cf_check.

This is a latent bug. The affected models don't have CET-IBT, so won't actually explode from lacking endbr64 instructions.

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

---
 xen/arch/x86/apic.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
xen/arch/x86/apic.c +11 -6
index 96d73a744964..794bbc21ae2c 100644
@@ -1092,12 +1092,17 @@ static void setup_APIC_timer(void)
     local_irq_restore(flags);
 }
 
+#define DEADLINE_MODEL_FUNCT(m, fn) \
+    { .vendor = X86_VENDOR_INTEL, .family = 6, .model = (m), \
+      .feature = X86_FEATURE_TSC_DEADLINE, \
+      .driver_data = fn + (0 * sizeof(fn == ((unsigned int (*)(void))NULL))) }
+
 #define DEADLINE_MODEL_MATCH(m, fr) \
     { .vendor = X86_VENDOR_INTEL, .family = 6, .model = (m), \
       .feature = X86_FEATURE_TSC_DEADLINE, \
       .driver_data = (void *)(unsigned long)(fr) }
 
-static unsigned int __init hsx_deadline_rev(void)
+static unsigned int __init cf_check hsx_deadline_rev(void)
 {
     switch ( boot_cpu_data.x86_mask )
     {
@@ -1108,7 +1113,7 @@ static unsigned int __init hsx_deadline_rev(void)
     return ~0U;
 }
 
-static unsigned int __init bdx_deadline_rev(void)
+static unsigned int __init cf_check bdx_deadline_rev(void)
 {
     switch ( boot_cpu_data.x86_mask )
     {
@@ -1121,7 +1126,7 @@ static unsigned int __init bdx_deadline_rev(void)
     return ~0U;
 }
 
-static unsigned int __init skx_deadline_rev(void)
+static unsigned int __init cf_check skx_deadline_rev(void)
 {
     switch ( boot_cpu_data.x86_mask )
     {
@@ -1135,17 +1140,17 @@ static unsigned int __init skx_deadline_rev(void)
 
 static const struct x86_cpu_id __initconstrel deadline_match[] = {
     DEADLINE_MODEL_MATCH(0x3c, 0x22),             /* Haswell */
-    DEADLINE_MODEL_MATCH(0x3f, hsx_deadline_rev), /* Haswell EP/EX */
+    DEADLINE_MODEL_FUNCT(0x3f, hsx_deadline_rev), /* Haswell EP/EX */
     DEADLINE_MODEL_MATCH(0x45, 0x20),             /* Haswell D */
     DEADLINE_MODEL_MATCH(0x46, 0x17),             /* Haswell H */
 
     DEADLINE_MODEL_MATCH(0x3d, 0x25),             /* Broadwell */
     DEADLINE_MODEL_MATCH(0x47, 0x17),             /* Broadwell H */
     DEADLINE_MODEL_MATCH(0x4f, 0x0b000020),       /* Broadwell EP/EX */
-    DEADLINE_MODEL_MATCH(0x56, bdx_deadline_rev), /* Broadwell D */
+    DEADLINE_MODEL_FUNCT(0x56, bdx_deadline_rev), /* Broadwell D */
 
     DEADLINE_MODEL_MATCH(0x4e, 0xb2),             /* Skylake M */
-    DEADLINE_MODEL_MATCH(0x55, skx_deadline_rev), /* Skylake X */
+    DEADLINE_MODEL_FUNCT(0x55, skx_deadline_rev), /* Skylake X */
     DEADLINE_MODEL_MATCH(0x5e, 0xb2),             /* Skylake D */
 
     DEADLINE_MODEL_MATCH(0x8e, 0x52),             /* Kabylake M */
Signature · 1 line
2.11.0
#1 lore raw

xen/arch/x86/apic.c static void setup_APIC_timer(void) · line 1098

Quoted 10 lines · Andrew Cooper
xen/arch/x86/apic.c +4 -0
@@ -1092,12 +1092,17 @@ static void setup_APIC_timer(void)
     local_irq_restore(flags);
 }
 
+#define DEADLINE_MODEL_FUNCT(m, fn) \
+    { .vendor = X86_VENDOR_INTEL, .family = 6, .model = (m), \
+      .feature = X86_FEATURE_TSC_DEADLINE, \
+      .driver_data = fn + (0 * sizeof(fn == ((unsigned int (*)(void))NULL))) }

Are you sure all compiler versions we support are happy about + of a function pointer and a constant? Even if that constant is zero, this is not legal as per the plain C spec.

Also strictly speaking you would want to parenthesize both uses of fn.

xen/arch/x86/apic.c static void setup_APIC_timer(void) · line 1103

Quoted 4 lines
xen/arch/x86/apic.c +0 -0
 #define DEADLINE_MODEL_MATCH(m, fr) \
     { .vendor = X86_VENDOR_INTEL, .family = 6, .model = (m), \
       .feature = X86_FEATURE_TSC_DEADLINE, \
       .driver_data = (void *)(unsigned long)(fr) }

As long as we leave this in place, there's a (small) risk of the wrong macro being used again if another hook would need adding here. We might be safer if driver_data became "unsigned long" and the void * cast was dropped from here (with an "unsigned long" cast added in the new macro, which at the same time would address my other concern above).

Jan

Reviewed-by
#2 lore raw
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/x86/apic.c +0 -0
@@ -1092,12 +1092,17 @@ static void setup_APIC_timer(void)
     local_irq_restore(flags);
 }
 
Quoted 8 lines

xen/arch/x86/apic.c static void setup_APIC_timer(void) · line 1098

Quoted 4 lines
xen/arch/x86/apic.c +4 -0
+#define DEADLINE_MODEL_FUNCT(m, fn) \
+    { .vendor = X86_VENDOR_INTEL, .family = 6, .model = (m), \
+      .feature = X86_FEATURE_TSC_DEADLINE, \
+      .driver_data = fn + (0 * sizeof(fn == ((unsigned int (*)(void))NULL))) }

Are you sure all compiler versions we support are happy about + of a function pointer and a constant? Even if that constant is zero, this is not legal as per the plain C spec.

Thanks for the pointer to
https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html - this is indeed fine then, with the assumption that this is also only meaningful with the non-upstream -fcf-check-attribute= patch in place.

Hence with ...

Quoted 2 lines

Also strictly speaking you would want to parenthesize both uses of fn.

... this taken care of (also to please Misra)

IOW ...

Quoted 11 lines

xen/arch/x86/apic.c static void setup_APIC_timer(void) · line 1103

Quoted 4 lines
xen/arch/x86/apic.c +0 -0
 #define DEADLINE_MODEL_MATCH(m, fr) \
     { .vendor = X86_VENDOR_INTEL, .family = 6, .model = (m), \
       .feature = X86_FEATURE_TSC_DEADLINE, \
       .driver_data = (void *)(unsigned long)(fr) }

As long as we leave this in place, there's a (small) risk of the wrong macro being used again if another hook would need adding here. We might be safer if driver_data became "unsigned long" and the void * cast was dropped from here (with an "unsigned long" cast added in the new macro, which at the same time would address my other concern above).

... while I continue to be concerned here, we can as well deal with that if and when a new such hook appeared.

Jan