Skip to contentSkip to conversation

x86: direct APIC vector adjustments

PATCH Superseded Jan Beulich 2 participants 18 messages Nov 19, 2025 – Nov 21, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86: direct APIC vector adjustments

The setting up of such vectors really is a boot-time-only thing. vPMU also doesn't need any vector when it's no in use anyway (which is the default).

1: guest: move allocation of Xen upcall vector to init code 2: MCE: restrict allocation of thermal and CMCI vector to BSP 3: IRQ: direct-APIC-vector setting is now init-only 4: vPMU: don't statically reserve the interrupt vector

Jan

#1 lore raw

PATCH 1/4 x86/guest: move allocation of Xen upcall vector to init code

There's no need to do this every time init_evtchn() is called. Just do it once when setting up CPU0. Drop the assertion as well, as alloc_hipriority_vector() (called by alloc_direct_apic_vector()) uses more restrictive BUG_ON() anyway. Then evtchn_upcall_vector can also validly become ro-after-init, just that it needs to move out of init_evtchn().

xen/arch/x86/guest/xen/xen.c +4 -6
@@ -233,16 +233,12 @@ static void cf_check xen_evtchn_upcall(v
     ack_APIC_irq();
 }
 
+static uint8_t __ro_after_init evtchn_upcall_vector;
+
 static int init_evtchn(void)
 {
-    static uint8_t evtchn_upcall_vector;
     int rc;
 
-    if ( !evtchn_upcall_vector )
-        alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall);
-
-    ASSERT(evtchn_upcall_vector);
-
     rc = xen_hypercall_set_evtchn_upcall_vector(this_cpu(vcpu_id),
                                                 evtchn_upcall_vector);
     if ( rc )
@@ -293,6 +289,8 @@ static void __init cf_check setup(void)
                XEN_LEGACY_MAX_VCPUS);
     }
 
+    alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall);
+
     BUG_ON(init_evtchn());
 }
 
#2 lore raw

Re: [PATCH 1/4] x86/guest: move allocation of Xen upcall vector to init code

Quoted 31 lines · Jan Beulich

There's no need to do this every time init_evtchn() is called. Just do it once when setting up CPU0. Drop the assertion as well, as alloc_hipriority_vector() (called by alloc_direct_apic_vector()) uses more restrictive BUG_ON() anyway. Then evtchn_upcall_vector can also validly become ro-after-init, just that it needs to move out of init_evtchn().

xen/arch/x86/guest/xen/xen.c +2 -6
@@ -233,16 +233,12 @@ static void cf_check xen_evtchn_upcall(v
     ack_APIC_irq();
 }
 
+static uint8_t __ro_after_init evtchn_upcall_vector;
+
 static int init_evtchn(void)
 {
-    static uint8_t evtchn_upcall_vector;
     int rc;
 
-    if ( !evtchn_upcall_vector )
-        alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall);
-
-    ASSERT(evtchn_upcall_vector);
-
     rc = xen_hypercall_set_evtchn_upcall_vector(this_cpu(vcpu_id),
                                                 evtchn_upcall_vector);
     if ( rc )

xen/arch/x86/guest/xen/xen.c static void __init cf_check setup(void) · line 296

Quoted 8 lines
xen/arch/x86/guest/xen/xen.c +2 -0
@@ -293,6 +289,8 @@ static void __init cf_check setup(void)
                XEN_LEGACY_MAX_VCPUS);
     }
 
+    alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall);
+
     BUG_ON(init_evtchn());
 }
 

This patch is fine, but it would be nicer to split init_evtchn() into bsp_init_evtchn() and percpu_init_evtchn().

Just out of context in init_evtchn(), there's a check for CPU0 that also ought to move into bsp_init_evtchn() (and therefore into __init), at which point the percpu simplifies to a single hypercall, and we keep subsystem specifics out of setup().

~Andrew

#3 lore raw

Re: [PATCH 1/4] x86/guest: move allocation of Xen upcall vector to init code

Quoted 43 lines · Andrew Cooper
Quoted 31 lines · Jan Beulich

There's no need to do this every time init_evtchn() is called. Just do it once when setting up CPU0. Drop the assertion as well, as alloc_hipriority_vector() (called by alloc_direct_apic_vector()) uses more restrictive BUG_ON() anyway. Then evtchn_upcall_vector can also validly become ro-after-init, just that it needs to move out of init_evtchn().

xen/arch/x86/guest/xen/xen.c +2 -6
@@ -233,16 +233,12 @@ static void cf_check xen_evtchn_upcall(v
     ack_APIC_irq();
 }
 
+static uint8_t __ro_after_init evtchn_upcall_vector;
+
 static int init_evtchn(void)
 {
-    static uint8_t evtchn_upcall_vector;
     int rc;
 
-    if ( !evtchn_upcall_vector )
-        alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall);
-
-    ASSERT(evtchn_upcall_vector);
-
     rc = xen_hypercall_set_evtchn_upcall_vector(this_cpu(vcpu_id),
                                                 evtchn_upcall_vector);
     if ( rc )

xen/arch/x86/guest/xen/xen.c static void __init cf_check setup(void) · line 296

Quoted 8 lines
xen/arch/x86/guest/xen/xen.c +2 -0
@@ -293,6 +289,8 @@ static void __init cf_check setup(void)
                XEN_LEGACY_MAX_VCPUS);
     }
 
+    alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall);
+
     BUG_ON(init_evtchn());
 }
 

This patch is fine, but it would be nicer to split init_evtchn() into bsp_init_evtchn() and percpu_init_evtchn().

Quoted 4 lines

Just out of context in init_evtchn(), there's a check for CPU0 that also ought to move into bsp_init_evtchn() (and therefore into __init), at which point the percpu simplifies to a single hypercall, and we keep subsystem specifics out of setup().

No, scratch that.  HVM_PARAM_CALLBACK_IRQ is not in the list of HVM Params that migration moves on migrate (see write_hvm_params() in xg_sr_save_x86_hvm.c).

Everything is awful.

Could you include a comment such as /* HVM_PARAM_CALLBACK_IRQ is not moved on migrate, so has to be set up again on resume. */ to make it clear why that piece of logic needs to stay in a non-init function?

~Andrew

#4 lore raw

Re: [PATCH 1/4] x86/guest: move allocation of Xen upcall vector to init code

Quoted 53 lines · Andrew Cooper
Quoted 43 lines · Andrew Cooper
Quoted 31 lines · Jan Beulich

There's no need to do this every time init_evtchn() is called. Just do it once when setting up CPU0. Drop the assertion as well, as alloc_hipriority_vector() (called by alloc_direct_apic_vector()) uses more restrictive BUG_ON() anyway. Then evtchn_upcall_vector can also validly become ro-after-init, just that it needs to move out of init_evtchn().

xen/arch/x86/guest/xen/xen.c +2 -6
@@ -233,16 +233,12 @@ static void cf_check xen_evtchn_upcall(v
     ack_APIC_irq();
 }
 
+static uint8_t __ro_after_init evtchn_upcall_vector;
+
 static int init_evtchn(void)
 {
-    static uint8_t evtchn_upcall_vector;
     int rc;
 
-    if ( !evtchn_upcall_vector )
-        alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall);
-
-    ASSERT(evtchn_upcall_vector);
-
     rc = xen_hypercall_set_evtchn_upcall_vector(this_cpu(vcpu_id),
                                                 evtchn_upcall_vector);
     if ( rc )

xen/arch/x86/guest/xen/xen.c static void __init cf_check setup(void) · line 296

Quoted 8 lines
xen/arch/x86/guest/xen/xen.c +2 -0
@@ -293,6 +289,8 @@ static void __init cf_check setup(void)
                XEN_LEGACY_MAX_VCPUS);
     }
 
+    alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall);
+
     BUG_ON(init_evtchn());
 }
 

This patch is fine, but it would be nicer to split init_evtchn() into bsp_init_evtchn() and percpu_init_evtchn().

Quoted 4 lines

Just out of context in init_evtchn(), there's a check for CPU0 that also ought to move into bsp_init_evtchn() (and therefore into __init), at which point the percpu simplifies to a single hypercall, and we keep subsystem specifics out of setup().

No, scratch that.  HVM_PARAM_CALLBACK_IRQ is not in the list of HVM Params that migration moves on migrate (see write_hvm_params() in xg_sr_save_x86_hvm.c).

Quoted 5 lines

Everything is awful.

Could you include a comment such as /* HVM_PARAM_CALLBACK_IRQ is not moved on migrate, so has to be set up again on resume. */ to make it clear why that piece of logic needs to stay in a non-init function?

It's pretty much unrelated to the change here, but yes, sure, I can add such a comment while touching the function.

Jan

#5 lore raw

Re: [PATCH 1/4] x86/guest: move allocation of Xen upcall vector to init code

Quoted 53 lines · Jan Beulich
Quoted 52 lines · Andrew Cooper
Quoted 43 lines · Andrew Cooper
Quoted 31 lines · Jan Beulich

There's no need to do this every time init_evtchn() is called. Just do it once when setting up CPU0. Drop the assertion as well, as alloc_hipriority_vector() (called by alloc_direct_apic_vector()) uses more restrictive BUG_ON() anyway. Then evtchn_upcall_vector can also validly become ro-after-init, just that it needs to move out of init_evtchn().

xen/arch/x86/guest/xen/xen.c +2 -6
@@ -233,16 +233,12 @@ static void cf_check xen_evtchn_upcall(v
     ack_APIC_irq();
 }
 
+static uint8_t __ro_after_init evtchn_upcall_vector;
+
 static int init_evtchn(void)
 {
-    static uint8_t evtchn_upcall_vector;
     int rc;
 
-    if ( !evtchn_upcall_vector )
-        alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall);
-
-    ASSERT(evtchn_upcall_vector);
-
     rc = xen_hypercall_set_evtchn_upcall_vector(this_cpu(vcpu_id),
                                                 evtchn_upcall_vector);
     if ( rc )

xen/arch/x86/guest/xen/xen.c static void __init cf_check setup(void) · line 296

Quoted 8 lines
xen/arch/x86/guest/xen/xen.c +2 -0
@@ -293,6 +289,8 @@ static void __init cf_check setup(void)
                XEN_LEGACY_MAX_VCPUS);
     }
 
+    alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall);
+
     BUG_ON(init_evtchn());
 }
 

This patch is fine, but it would be nicer to split init_evtchn() into bsp_init_evtchn() and percpu_init_evtchn().

Quoted 4 lines

Just out of context in init_evtchn(), there's a check for CPU0 that also ought to move into bsp_init_evtchn() (and therefore into __init), at which point the percpu simplifies to a single hypercall, and we keep subsystem specifics out of setup().

No, scratch that.  HVM_PARAM_CALLBACK_IRQ is not in the list of HVM Params that migration moves on migrate (see write_hvm_params() in xg_sr_save_x86_hvm.c).

Quoted 7 lines
Quoted 5 lines

Everything is awful.

Could you include a comment such as /* HVM_PARAM_CALLBACK_IRQ is not moved on migrate, so has to be set up again on resume. */ to make it clear why that piece of logic needs to stay in a non-init function?

It's pretty much unrelated to the change here, but yes, sure, I can add such a comment while touching the function.

Yes please.  It's relevant to judging which code should move out of init_evtchn().

With that done, Reviewed-by: Andrew Cooper <[email protected]>

#6 lore raw

PATCH 2/4 x86/MCE: restrict allocation of thermal and CMCI vector to BSP

In reply to Jan Beulich

There's no need to do this for every AP; just do it once when setting up the BSP. Then both vector variables can also validly become ro-after-init.

While touching intel_init_thermal(), constify its 1st parameter, which in turn requires touching intel_thermal_supported() as well.

---
This contextually (but not functionally) collides with "x86/MCE: adjust S3 resume handling".

xen/arch/x86/cpu/mcheck/mce.c +1 -1
@@ -37,7 +37,7 @@ bool is_mc_panic;
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, nr_mce_banks);
 unsigned int __read_mostly firstbank;
 unsigned int __read_mostly ppin_msr;
-uint8_t __read_mostly cmci_apic_vector;
+uint8_t __ro_after_init cmci_apic_vector;
 bool __read_mostly cmci_support;
 
 /* If mce_force_broadcast == 1, lmce_support will be disabled forcibly. */
xen/arch/x86/cpu/mcheck/mce_intel.c +10 -8
@@ -87,7 +87,7 @@ static void cf_check intel_thermal_inter
 }
 
 /* Thermal monitoring depends on APIC, ACPI and clock modulation */
-static bool intel_thermal_supported(struct cpuinfo_x86 *c)
+static bool intel_thermal_supported(const struct cpuinfo_x86 *c)
 {
     if ( !cpu_has_apic )
         return false;
@@ -110,13 +110,13 @@ static void __init mcheck_intel_therm_in
 }
 
 /* P4/Xeon Thermal regulation detect and init */
-static void intel_init_thermal(struct cpuinfo_x86 *c)
+static void intel_init_thermal(const struct cpuinfo_x86 *c, bool bsp)
 {
     uint64_t msr_content;
     uint32_t val;
     int tm2 = 0;
     unsigned int cpu = smp_processor_id();
-    static uint8_t thermal_apic_vector;
+    static uint8_t __ro_after_init thermal_apic_vector;
 
     if ( !intel_thermal_supported(c) )
         return; /* -ENODEV */
@@ -160,7 +160,8 @@ static void intel_init_thermal(struct cp
         return; /* -EBUSY */
     }
 
-    alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt);
+    if ( bsp )
+        alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt);
 
     /* The temperature transition interrupt handler setup */
     val = thermal_apic_vector;    /* our delivery vector */
@@ -667,7 +668,7 @@ static void cf_check cmci_interrupt(void
         mctelem_dismiss(mctc);
 }
 
-static void intel_init_cmci(struct cpuinfo_x86 *c)
+static void intel_init_cmci(struct cpuinfo_x86 *c, bool bsp)
 {
     u32 l, apic;
     int cpu = smp_processor_id();
@@ -687,7 +688,8 @@ static void intel_init_cmci(struct cpuin
         return;
     }
 
-    alloc_direct_apic_vector(&cmci_apic_vector, cmci_interrupt);
+    if ( bsp )
+        alloc_direct_apic_vector(&cmci_apic_vector, cmci_interrupt);
 
     apic = cmci_apic_vector;
     apic |= (APIC_DM_FIXED | APIC_LVT_MASKED);
@@ -993,9 +995,9 @@ enum mcheck_type intel_mcheck_init(struc
 
     intel_init_mce(bsp);
 
-    intel_init_cmci(c);
+    intel_init_cmci(c, bsp);
 
-    intel_init_thermal(c);
+    intel_init_thermal(c, bsp);
 
     intel_init_ppin(c);
 
#7 lore raw

Re: [PATCH 2/4] x86/MCE: restrict allocation of thermal and CMCI vector to BSP

Quoted 19 lines · Jan Beulich
xen/arch/x86/cpu/mcheck/mce.c +2 -2
@@ -110,13 +110,13 @@ static void __init mcheck_intel_therm_in
 }
 
 /* P4/Xeon Thermal regulation detect and init */
-static void intel_init_thermal(struct cpuinfo_x86 *c)
+static void intel_init_thermal(const struct cpuinfo_x86 *c, bool bsp)
 {
     uint64_t msr_content;
     uint32_t val;
     int tm2 = 0;
     unsigned int cpu = smp_processor_id();
-    static uint8_t thermal_apic_vector;
+    static uint8_t __ro_after_init thermal_apic_vector;
 
     if ( !intel_thermal_supported(c) )
         return; /* -ENODEV */

xen/arch/x86/cpu/mcheck/mce.c static void intel_init_thermal(struct cp · line 164

Quoted 6 lines
xen/arch/x86/cpu/mcheck/mce.c +2 -1
@@ -160,7 +160,8 @@ static void intel_init_thermal(struct cp
         return; /* -EBUSY */
     }
 
-    alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt);
+    if ( bsp )
+        alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt);

We really don't want both c and bsp passed in.  That can only go wrong.

Furthermore, this function has 2 other examples generating bsp locally.

The function is in desperate need of cleanup (MSRs, variable and constant names), but right now this makes it worse.

Please either use c == &boot_cpu_data, and I'll do some cleanup later, or generate bsp = c == &boot_cpu_data and fix up all users in the function.

~Andrew

#8 lore raw

Re: [PATCH 2/4] x86/MCE: restrict allocation of thermal and CMCI vector to BSP

Quoted 29 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
xen/arch/x86/cpu/mcheck/mce.c +2 -2
@@ -110,13 +110,13 @@ static void __init mcheck_intel_therm_in
 }
 
 /* P4/Xeon Thermal regulation detect and init */
-static void intel_init_thermal(struct cpuinfo_x86 *c)
+static void intel_init_thermal(const struct cpuinfo_x86 *c, bool bsp)
 {
     uint64_t msr_content;
     uint32_t val;
     int tm2 = 0;
     unsigned int cpu = smp_processor_id();
-    static uint8_t thermal_apic_vector;
+    static uint8_t __ro_after_init thermal_apic_vector;
 
     if ( !intel_thermal_supported(c) )
         return; /* -ENODEV */

xen/arch/x86/cpu/mcheck/mce.c static void intel_init_thermal(struct cp · line 164

Quoted 6 lines
xen/arch/x86/cpu/mcheck/mce.c +2 -1
@@ -160,7 +160,8 @@ static void intel_init_thermal(struct cp
         return; /* -EBUSY */
     }
 
-    alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt);
+    if ( bsp )
+        alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt);

We really don't want both c and bsp passed in.  That can only go wrong.

Quoted 7 lines

Furthermore, this function has 2 other examples generating bsp locally.

The function is in desperate need of cleanup (MSRs, variable and constant names), but right now this makes it worse.

Please either use c == &boot_cpu_data, and I'll do some cleanup later, or generate bsp = c == &boot_cpu_data and fix up all users in the function.

No, throughout mce/ this won't work as long as acpi/power.c:enter_state() has

    mcheck_init(&boot_cpu_data, false);

Jan

#9 lore raw

Re: [PATCH 2/4] x86/MCE: restrict allocation of thermal and CMCI vector to BSP

Quoted 31 lines · Jan Beulich
Quoted 30 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
xen/arch/x86/cpu/mcheck/mce.c +2 -2
@@ -110,13 +110,13 @@ static void __init mcheck_intel_therm_in
 }
 
 /* P4/Xeon Thermal regulation detect and init */
-static void intel_init_thermal(struct cpuinfo_x86 *c)
+static void intel_init_thermal(const struct cpuinfo_x86 *c, bool bsp)
 {
     uint64_t msr_content;
     uint32_t val;
     int tm2 = 0;
     unsigned int cpu = smp_processor_id();
-    static uint8_t thermal_apic_vector;
+    static uint8_t __ro_after_init thermal_apic_vector;
 
     if ( !intel_thermal_supported(c) )
         return; /* -ENODEV */

xen/arch/x86/cpu/mcheck/mce.c static void intel_init_thermal(struct cp · line 164

Quoted 6 lines
xen/arch/x86/cpu/mcheck/mce.c +2 -1
@@ -160,7 +160,8 @@ static void intel_init_thermal(struct cp
         return; /* -EBUSY */
     }
 
-    alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt);
+    if ( bsp )
+        alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt);

We really don't want both c and bsp passed in.  That can only go wrong.

Furthermore, this function has 2 other examples generating bsp locally.

Quoted 8 lines
Quoted 5 lines

The function is in desperate need of cleanup (MSRs, variable and constant names), but right now this makes it worse.

Please either use c == &boot_cpu_data, and I'll do some cleanup later, or generate bsp = c == &boot_cpu_data and fix up all users in the function.

No, throughout mce/ this won't work as long as acpi/power.c:enter_state() has

    mcheck_init(&boot_cpu_data, false);

How's not not already broken then?  As said, intel_init_thermal() is already using c == &boot_cpu_data.

This patch introduces a conflicting idea of bsp in this function, and that's what I really want to avoid.

This looks like it wants splitting properly as {bsp,percpu}_init_$FOO() like we have elsewhere.

~Andrew

#10 lore raw

Re: [PATCH 2/4] x86/MCE: restrict allocation of thermal and CMCI vector to BSP

Quoted 35 lines · Andrew Cooper
Quoted 34 lines · Jan Beulich
Quoted 33 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
xen/arch/x86/cpu/mcheck/mce.c +2 -2
@@ -110,13 +110,13 @@ static void __init mcheck_intel_therm_in
 }
 
 /* P4/Xeon Thermal regulation detect and init */
-static void intel_init_thermal(struct cpuinfo_x86 *c)
+static void intel_init_thermal(const struct cpuinfo_x86 *c, bool bsp)
 {
     uint64_t msr_content;
     uint32_t val;
     int tm2 = 0;
     unsigned int cpu = smp_processor_id();
-    static uint8_t thermal_apic_vector;
+    static uint8_t __ro_after_init thermal_apic_vector;
 
     if ( !intel_thermal_supported(c) )
         return; /* -ENODEV */

xen/arch/x86/cpu/mcheck/mce.c static void intel_init_thermal(struct cp · line 164

Quoted 6 lines
xen/arch/x86/cpu/mcheck/mce.c +2 -1
@@ -160,7 +160,8 @@ static void intel_init_thermal(struct cp
         return; /* -EBUSY */
     }
 
-    alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt);
+    if ( bsp )
+        alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt);

We really don't want both c and bsp passed in.  That can only go wrong.

Furthermore, this function has 2 other examples generating bsp locally.

The function is in desperate need of cleanup (MSRs, variable and constant names), but right now this makes it worse.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Please either use c == &boot_cpu_data, and I'll do some cleanup later, or generate bsp = c == &boot_cpu_data and fix up all users in the function.

No, throughout mce/ this won't work as long as acpi/power.c:enter_state() has

    mcheck_init(&boot_cpu_data, false);

How's not not already broken then?  As said, intel_init_thermal() is already using c == &boot_cpu_data.

That's two printk()s, so not overly severe a bug. And being fixed by "x86/MCE:
adjust S3 resume handling" posted months ago. There I'm actually putting up the question whether one of the two could/should stay as is.

Quoted 5 lines

This patch introduces a conflicting idea of bsp in this function, and that's what I really want to avoid.

This looks like it wants splitting properly as {bsp,percpu}_init_$FOO() like we have elsewhere.

Without detailed checking, I'm not sure that would properly cover things. Right now we have three modes: BSP (boot), BSP (resume), and AP.

More importantly, though: This would be more than enough content for another series, i.e. shouldn't block the work here (which really moves things to a more consistent state, within cpu/mcheck/, just like that other patch also aims at doing).

Jan

#11 lore raw

PATCH 3/4 x86/IRQ: direct-APIC-vector setting is now init-only

In reply to Jan Beulich

With all callers of alloc_direct_apic_vector() now being limited to BSP setup, it and its helpers (whose other callers have already been init- only) can become __init. As a result data items can be adjusted, too.

---
Question is whether with this the "if (*vector == 0)" in alloc_direct_apic_vector() is of much use anymore.

xen/arch/x86/irq.c +5 -5
@@ -970,22 +970,22 @@ void pirq_set_affinity(struct domain *d,
 DEFINE_PER_CPU(unsigned int, irq_count);
 static DEFINE_PER_CPU(bool, check_eoi_deferral);
 
-uint8_t alloc_hipriority_vector(void)
+uint8_t __init alloc_hipriority_vector(void)
 {
-    static uint8_t next = FIRST_HIPRIORITY_VECTOR;
+    static uint8_t __initdata next = FIRST_HIPRIORITY_VECTOR;
     BUG_ON(next < FIRST_HIPRIORITY_VECTOR);
     BUG_ON(next > LAST_HIPRIORITY_VECTOR);
     return next++;
 }
 
-static void (*direct_apic_vector[X86_IDT_VECTORS])(void);
-void set_direct_apic_vector(uint8_t vector, void (*handler)(void))
+static void (*__ro_after_init direct_apic_vector[X86_IDT_VECTORS])(void);
+void __init set_direct_apic_vector(uint8_t vector, void (*handler)(void))
 {
     BUG_ON(direct_apic_vector[vector] != NULL);
     direct_apic_vector[vector] = handler;
 }
 
-void alloc_direct_apic_vector(uint8_t *vector, void (*handler)(void))
+void __init alloc_direct_apic_vector(uint8_t *vector, void (*handler)(void))
 {
     static DEFINE_SPINLOCK(lock);
 
#12 lore raw

Re: [PATCH 3/4] x86/IRQ: direct-APIC-vector setting is now init-only

Quoted 30 lines · Jan Beulich

With all callers of alloc_direct_apic_vector() now being limited to BSP setup, it and its helpers (whose other callers have already been init- only) can become __init. As a result data items can be adjusted, too.

---
Question is whether with this the "if (*vector == 0)" in alloc_direct_apic_vector() is of much use anymore.

xen/arch/x86/irq.c +4 -4
@@ -970,22 +970,22 @@ void pirq_set_affinity(struct domain *d,
 DEFINE_PER_CPU(unsigned int, irq_count);
 static DEFINE_PER_CPU(bool, check_eoi_deferral);
 
-uint8_t alloc_hipriority_vector(void)
+uint8_t __init alloc_hipriority_vector(void)
 {
-    static uint8_t next = FIRST_HIPRIORITY_VECTOR;
+    static uint8_t __initdata next = FIRST_HIPRIORITY_VECTOR;
     BUG_ON(next < FIRST_HIPRIORITY_VECTOR);
     BUG_ON(next > LAST_HIPRIORITY_VECTOR);
     return next++;
 }
 
-static void (*direct_apic_vector[X86_IDT_VECTORS])(void);
-void set_direct_apic_vector(uint8_t vector, void (*handler)(void))
+static void (*__ro_after_init direct_apic_vector[X86_IDT_VECTORS])(void);
+void __init set_direct_apic_vector(uint8_t vector, void (*handler)(void))

xen/arch/x86/irq.c void pirq_set_affinity(struct domain *d, · line 991

Quoted 10 lines
xen/arch/x86/irq.c +1 -1
 {
     BUG_ON(direct_apic_vector[vector] != NULL);
     direct_apic_vector[vector] = handler;
 }
 
-void alloc_direct_apic_vector(uint8_t *vector, void (*handler)(void))
+void __init alloc_direct_apic_vector(uint8_t *vector, void (*handler)(void))
 {
     static DEFINE_SPINLOCK(lock);
 

This function, being __init now, cannot possibly need the spinlock.

To your question checking vector for 0, that always wrong in my opinion, stemming from this being a bad API.

It should become:

xen/arch/x86/cpu/mcheck/mce_intel.c +0 -0
index bb3c9f9e5234..9ae0bece1af7 100644
@@ -161,7 +161,7 @@ static void intel_init_thermal(const struct cpuinfo_x86 *c, bool bsp)

     }

     if ( bsp )
-        alloc_direct_apic_vector(&thermal_apic_vector, intel_thermal_interrupt); +        thermal_apic_vector = alloc_direct_apic_vector(intel_thermal_interrupt);
     /* The temperature transition interrupt handler setup */
     val = thermal_apic_vector;    /* our delivery vector */
@@ -689,7 +689,7 @@ static void intel_init_cmci(struct cpuinfo_x86 *c, bool bsp) }

     if ( bsp )
-        alloc_direct_apic_vector(&cmci_apic_vector, cmci_interrupt); +        cmci_apic_vector = alloc_direct_apic_vector(cmci_interrupt);
     apic = cmci_apic_vector;
     apic |= (APIC_DM_FIXED | APIC_LVT_MASKED);

xen/arch/x86/guest/xen/xen.c +0 -0
index 640872efdd2a..a473296ba94f 100644
@@ -289,7 +289,7 @@ static void __init cf_check setup(void)

                XEN_LEGACY_MAX_VCPUS);
     }

-    alloc_direct_apic_vector(&evtchn_upcall_vector, xen_evtchn_upcall); +    evtchn_upcall_vector = alloc_direct_apic_vector(xen_evtchn_upcall);
     BUG_ON(init_evtchn());
 }

xen/arch/x86/hvm/vmx/vmx.c +0 -0
index e2b5077654ef..0919edd73312 100644
@@ -3118,10 +3118,10 @@ const struct hvm_function_table * __init start_vmx(void)


     if ( cpu_has_vmx_posted_intr_processing )
     {
-        alloc_direct_apic_vector(&posted_intr_vector, pi_notification_interrupt); +        posted_intr_vector = alloc_direct_apic_vector(pi_notification_interrupt); if ( iommu_intpost )
         {
-            alloc_direct_apic_vector(&pi_wakeup_vector, pi_wakeup_interrupt); +            pi_wakeup_vector = alloc_direct_apic_vector(pi_wakeup_interrupt);
             vmx_function_table.pi_update_irte = vmx_pi_update_irte; }

xen/arch/x86/include/asm/irq.h +0 -0
index 7315150b66b4..0cf47a883722 100644
@@ -119,7 +119,7 @@ uint8_t alloc_hipriority_vector(void);

 void free_lopriority_vector(uint8_t vector);

 void set_direct_apic_vector(uint8_t vector, void (*handler)(void)); -void alloc_direct_apic_vector(uint8_t *vector, void (*handler)(void)); +uint8_t alloc_direct_apic_vector(void (*handler)(void));

 void do_IRQ(struct cpu_user_regs *regs);

xen/arch/x86/irq.c +0 -0
index c740035927c2..f50b5b5d5037 100644
@@ -985,16 +985,13 @@ void __init set_direct_apic_vector(uint8_t vector, void (*handler)(void))

     direct_apic_vector[vector] = handler;
 }

-void __init alloc_direct_apic_vector(uint8_t *vector, void (*handler)(void)) +uint8_t __init alloc_direct_apic_vector(void (*handler)(void))
 {
-    static DEFINE_SPINLOCK(lock);
+    uint8_t vec = alloc_hipriority_vector();

-    spin_lock(&lock);
-    if (*vector == 0) {
-        *vector = alloc_hipriority_vector();
-        set_direct_apic_vector(*vector, handler);
-    }
-    spin_unlock(&lock);
+    set_direct_apic_vector(vec, handler);
+
+    return vec;
 }

 /* This could free any vectors, but is needed only for low-prio ones. */

which makes it a far more normal looking API, improves code generation, and gets rid of the pointless lock.

Feel free to fold this in, or I can submit it separately, but I think both parts want to go in together.

~Andrew

#13 lore raw

PATCH 4/4 x86/vPMU: don't statically reserve the interrupt vector

In reply to Jan Beulich

Move the setup to vPMU code, doing the allocation of a vector only when one is actually going to be needed. With that the handler function also doesn't need to be split across two places anymore.

Add the freed up vector to the dynamically allocatable range.

---
In case down the road we also want to have a build mode with vPMU code excluded, this may also simplify things a little there.

xen/arch/x86/apic.c +0 -13
@@ -1313,16 +1313,6 @@ static void cf_check error_interrupt(voi
            entries[3], entries[2], entries[1], entries[0]);
 }
 
-/*
- * This interrupt handles performance counters interrupt
- */
-
-static void cf_check pmu_interrupt(void)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt();
-}
-
 void __init apic_intr_init(void)
 {
     smp_intr_init();
@@ -1333,9 +1323,6 @@ void __init apic_intr_init(void)
     /* IPI vectors for APIC spurious and error interrupts */
     set_direct_apic_vector(SPURIOUS_APIC_VECTOR, spurious_interrupt);
     set_direct_apic_vector(ERROR_APIC_VECTOR, error_interrupt);
-
-    /* Performance Counters Interrupt */
-    set_direct_apic_vector(PMU_APIC_VECTOR, pmu_interrupt);
 }
 
 /*
xen/arch/x86/cpu/vpmu.c +10 -4
@@ -40,6 +40,8 @@ static struct arch_vpmu_ops __initdata v
 static DEFINE_SPINLOCK(vpmu_lock);
 static unsigned vpmu_count;
 
+static uint8_t __ro_after_init pmu_apic_vector;
+
 static DEFINE_PER_CPU(struct vcpu *, last_vcpu);
 
 static int __init cf_check parse_vpmu_params(const char *s)
@@ -94,7 +96,7 @@ void vpmu_lvtpc_update(uint32_t val)
 
     vpmu = vcpu_vpmu(curr);
 
-    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | (val & APIC_LVT_MASKED);
+    vpmu->hw_lapic_lvtpc = pmu_apic_vector | (val & APIC_LVT_MASKED);
 
     /* Postpone APIC updates for PV(H) guests if PMU interrupt is pending */
     if ( has_vlapic(curr->domain) || !vpmu->xenpmu_data ||
@@ -160,7 +162,7 @@ static inline struct vcpu *choose_hwdom_
     return hardware_domain->vcpu[idx];
 }
 
-void vpmu_do_interrupt(void)
+static void cf_check vpmu_interrupt(void)
 {
     struct vcpu *sampled = current, *sampling;
     struct vpmu_struct *vpmu;
@@ -169,6 +171,8 @@ void vpmu_do_interrupt(void)
     uint32_t vlapic_lvtpc;
 #endif
 
+    ack_APIC_irq();
+
     /*
      * dom0 will handle interrupt for special domains (e.g. idle domain) or,
      * in XENPMU_MODE_ALL, for everyone.
@@ -369,7 +373,7 @@ void vpmu_save(struct vcpu *v)
 
     vpmu_reset(vpmu, VPMU_CONTEXT_SAVE);
 
-    apic_write(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
+    apic_write(APIC_LVTPC, pmu_apic_vector | APIC_LVT_MASKED);
 }
 
 int vpmu_load(struct vcpu *v, bool from_guest)
@@ -432,7 +436,7 @@ static int vpmu_arch_initialise(struct v
         return ret;
     }
 
-    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+    vpmu->hw_lapic_lvtpc = pmu_apic_vector | APIC_LVT_MASKED;
     vpmu_set(vpmu, VPMU_INITIALIZED);
 
     return 0;
@@ -860,6 +864,8 @@ static int __init cf_check vpmu_init(voi
         register_cpu_notifier(&cpu_nfb);
         printk(XENLOG_INFO "VPMU: version " __stringify(XENPMU_VER_MAJ) "."
                __stringify(XENPMU_VER_MIN) "\n");
+
+        alloc_direct_apic_vector(&pmu_apic_vector, vpmu_interrupt);
     }
     else
     {
xen/arch/x86/include/asm/irq-vectors.h +1 -2
@@ -8,13 +8,12 @@
 #define EVENT_CHECK_VECTOR	0xfc
 #define CALL_FUNCTION_VECTOR	0xfb
 #define LOCAL_TIMER_VECTOR	0xfa
-#define PMU_APIC_VECTOR 	0xf9
 /*
  * High-priority dynamically-allocated vectors. For interrupts that
  * must be higher priority than any guest-bound interrupt.
  */
 #define FIRST_HIPRIORITY_VECTOR	0xf1
-#define LAST_HIPRIORITY_VECTOR  0xf8
+#define LAST_HIPRIORITY_VECTOR  0xf9
 /* IRQ0 (timer) is statically allocated but must be high priority. */
 #define IRQ0_VECTOR             0xf0
 
xen/arch/x86/include/asm/vpmu.h +0 -1
@@ -99,7 +99,6 @@ static inline bool vpmu_are_all_set(cons
 
 void vpmu_lvtpc_update(uint32_t val);
 int vpmu_do_msr(unsigned int msr, uint64_t *msr_content, bool is_write);
-void vpmu_do_interrupt(void);
 void vpmu_initialise(struct vcpu *v);
 void vpmu_destroy(struct vcpu *v);
 void vpmu_save(struct vcpu *v);
#14 lore raw

Re: [PATCH 4/4] x86/vPMU: don't statically reserve the interrupt vector

Quoted 17 lines · Jan Beulich

Move the setup to vPMU code, doing the allocation of a vector only when one is actually going to be needed. With that the handler function also doesn't need to be split across two places anymore.

Add the freed up vector to the dynamically allocatable range.

---
In case down the road we also want to have a build mode with vPMU code excluded, this may also simplify things a little there.

xen/arch/x86/apic.c +0 -0
@@ -1313,16 +1313,6 @@ static void cf_check error_interrupt(voi
            entries[3], entries[2], entries[1], entries[0]);
 }
 

xen/arch/x86/apic.c static void cf_check error_interrupt(voi · line 1325

Quoted 10 lines
xen/arch/x86/apic.c +0 -10
-/*
- * This interrupt handles performance counters interrupt
- */
-
-static void cf_check pmu_interrupt(void)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt();
-}
-

I know you're only moving this, but it's likely-buggy before and after. ack_APIC_irq() needs to be last, and Xen's habit for acking early is why we have reentrancy problems.

I think there wants to be a patch ahead of this one swapping the order so the ack is at the end, so that this patch can retain that property when merging the functions.

Or, if you're absolutely certain it doesn't need backporting as a bugfix, then merging into this patch is probably ok as long as it's called out clearly in the commit message.

Quoted 12 lines
xen/arch/x86/cpu/vpmu.c +1 -1
@@ -369,7 +373,7 @@ void vpmu_save(struct vcpu *v)
 
     vpmu_reset(vpmu, VPMU_CONTEXT_SAVE);
 
-    apic_write(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
+    apic_write(APIC_LVTPC, pmu_apic_vector | APIC_LVT_MASKED);
 }
 
 int vpmu_load(struct vcpu *v, bool from_guest)

xen/arch/x86/cpu/vpmu.c static int vpmu_arch_initialise(struct v · line 439

Quoted 5 lines
xen/arch/x86/cpu/vpmu.c +1 -1
@@ -432,7 +436,7 @@ static int vpmu_arch_initialise(struct v
         return ret;
     }
 
-    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+    vpmu->hw_lapic_lvtpc = pmu_apic_vector | APIC_LVT_MASKED;

Taking a step back, I'm confused as to why we have pmu_apic_vector at all.

LVTPC needs programming with NMIs in order to provide coherent information.

I think this might go a long way to explaining some of the complaints we've had in the past about junk showing up.

~Andrew

#15 lore raw

Re: [PATCH 4/4] x86/vPMU: don't statically reserve the interrupt vector

Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
xen/arch/x86/apic.c +0 -0
@@ -1313,16 +1313,6 @@ static void cf_check error_interrupt(voi
            entries[3], entries[2], entries[1], entries[0]);
 }
 
Quoted 14 lines

xen/arch/x86/apic.c static void cf_check error_interrupt(voi · line 1325

Quoted 10 lines
xen/arch/x86/apic.c +0 -10
-/*
- * This interrupt handles performance counters interrupt
- */
-
-static void cf_check pmu_interrupt(void)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt();
-}
-

I know you're only moving this, but it's likely-buggy before and after. ack_APIC_irq() needs to be last, and Xen's habit for acking early is why we have reentrancy problems.

I was wondering, but was vaguely (but apparently wrongly) remembering that the PMU interrupt is self-disabling (i.e. requires re-enabling before it can fire again). Should have checked vpmu_do_interrupt() a little more closely, where from the various plain "return" it's pretty clear that isn't the case.

Quoted 7 lines

I think there wants to be a patch ahead of this one swapping the order so the ack is at the end, so that this patch can retain that property when merging the functions.

Or, if you're absolutely certain it doesn't need backporting as a bugfix, then merging into this patch is probably ok as long as it's called out clearly in the commit message.

No, I'll make this a separate, prereq patch.

Jan

#16 lore raw

Re: [PATCH 4/4] x86/vPMU: don't statically reserve the interrupt vector

Quoted 21 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
xen/arch/x86/apic.c +0 -0
@@ -1313,16 +1313,6 @@ static void cf_check error_interrupt(voi
            entries[3], entries[2], entries[1], entries[0]);
 }
 
Quoted 13 lines

xen/arch/x86/apic.c static void cf_check error_interrupt(voi · line 1325

Quoted 10 lines
xen/arch/x86/apic.c +0 -10
-/*
- * This interrupt handles performance counters interrupt
- */
-
-static void cf_check pmu_interrupt(void)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt();
-}
-

I know you're only moving this, but it's likely-buggy before and after. ack_APIC_irq() needs to be last, and Xen's habit for acking early is why we have reentrancy problems.

Quoted 5 lines

I was wondering, but was vaguely (but apparently wrongly) remembering that the PMU interrupt is self-disabling (i.e. requires re-enabling before it can fire again). Should have checked vpmu_do_interrupt() a little more closely, where from the various plain "return" it's pretty clear that isn't the case.

It can be configured to be self-disabling. IA32_DEBUGCTL.Freeze_PerfMon_On_PMI, and variations on this theme depending on the arch perfmon revision.

I'm not aware of AMD having a similar capability.

Quoted 9 lines
Quoted 8 lines

I think there wants to be a patch ahead of this one swapping the order so the ack is at the end, so that this patch can retain that property when merging the functions.

Or, if you're absolutely certain it doesn't need backporting as a bugfix, then merging into this patch is probably ok as long as it's called out clearly in the commit message.

No, I'll make this a separate, prereq patch.

Ok, and with this rebased on top, Acked-by: Andrew Cooper <[email protected]>

#17 lore raw

Re: [PATCH 4/4] x86/vPMU: don't statically reserve the interrupt vector

In reply to Jan Beulich
Quoted 33 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
xen/arch/x86/apic.c +0 -0
@@ -1313,16 +1313,6 @@ static void cf_check error_interrupt(voi
            entries[3], entries[2], entries[1], entries[0]);
 }
 
Quoted 14 lines

xen/arch/x86/apic.c static void cf_check error_interrupt(voi · line 1325

Quoted 10 lines
xen/arch/x86/apic.c +0 -10
-/*
- * This interrupt handles performance counters interrupt
- */
-
-static void cf_check pmu_interrupt(void)
-{
-    ack_APIC_irq();
-    vpmu_do_interrupt();
-}
-

I know you're only moving this, but it's likely-buggy before and after. ack_APIC_irq() needs to be last, and Xen's habit for acking early is why we have reentrancy problems.

I was wondering, but was vaguely (but apparently wrongly) remembering that the PMU interrupt is self-disabling (i.e. requires re-enabling before it can fire again). Should have checked vpmu_do_interrupt() a little more closely, where from the various plain "return" it's pretty clear that isn't the case.

Quoted 4 lines

I think there wants to be a patch ahead of this one swapping the order so the ack is at the end, so that this patch can retain that property when merging the functions.

Quoted 5 lines
Quoted 3 lines

Or, if you're absolutely certain it doesn't need backporting as a bugfix, then merging into this patch is probably ok as long as it's called out clearly in the commit message.

No, I'll make this a separate, prereq patch.

It won't really need backporting, though: Direct-APIC-vector handlers are called with IRQs off, and hence when to ack is benign as long as IRQs aren't transiently turned on while handling. Nevertheless it probably makes sense to switch things around, so I'll add that extra patch anyway.

Jan