Skip to contentSkip to conversation

x86: direct APIC vector adjustments

PATCH In review Jan Beulich 2 participants 6 messages Nov 24, 2025

lore
Share & tools
lore
#0 lore raw

v2 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). The first patch also is a pretty much unrelated change to tidy things up first.

1: vPMU: move APIC ack past the handling of the interrupt 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 v2 1/4 x86/vPMU: move APIC ack past the handling of the interrupt

While benign as long as it's using a direct-APIC-vector (handlers for which are called with IRQs off), follow the more usual pattern of ack-ing the IRQ only after handling it.

---
v2: New.

xen/arch/x86/apic.c +1 -1
@@ -1319,8 +1319,8 @@ static void cf_check error_interrupt(voi
 
 static void cf_check pmu_interrupt(void)
 {
-    ack_APIC_irq();
     vpmu_do_interrupt();
+    ack_APIC_irq();
 }
 
 void __init apic_intr_init(void)
#3 lore raw

PATCH v2 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);
 
#4 lore raw

PATCH v2 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.

With alloc_direct_apic_vector() now being __init, it's meaningless to use a lock, and the indirection is also pretty pointless. Drop both while at it.

---
v2: Re-work alloc_direct_apic_vector().

xen/arch/x86/cpu/mcheck/mce_intel.c +2 -2
@@ -161,7 +161,7 @@ static void intel_init_thermal(const str
     }
 
     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 cpuin
     }
 
     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 +1 -1
@@ -293,7 +293,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 +2 -2
@@ -3181,10 +3181,10 @@ const struct hvm_function_table * __init
 
     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 +1 -1
@@ -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 +9 -12
@@ -970,31 +970,28 @@ 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))
+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. */
#5 lore raw

PATCH v2 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 functions also don't need to be scattered 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.
---
v2: Re-base over patch moving the APIC ack and over change to
alloc_direct_apic_vector().

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)
-{
-    vpmu_do_interrupt();
-    ack_APIC_irq();
-}
-
 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 +14 -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 vpmu_do_interrupt(void)
 {
     struct vcpu *sampled = current, *sampling;
     struct vpmu_struct *vpmu;
@@ -322,6 +324,12 @@ void vpmu_do_interrupt(void)
 #endif
 }
 
+static void cf_check vpmu_interrupt(void)
+{
+    vpmu_do_interrupt();
+    ack_APIC_irq();
+}
+
 #ifdef CONFIG_MEM_SHARING
 int vpmu_allocate_context(struct vcpu *v)
 {
@@ -369,7 +377,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 +440,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 +868,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");
+
+        pmu_apic_vector = alloc_direct_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);