]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
x86/irq: KVM: Track PIR bitmap as an "unsigned long" array
authorSean Christopherson <seanjc@google.com>
Tue, 1 Apr 2025 16:34:43 +0000 (09:34 -0700)
committerSean Christopherson <seanjc@google.com>
Thu, 24 Apr 2025 18:19:38 +0000 (11:19 -0700)
Track the PIR bitmap in posted interrupt descriptor structures as an array
of unsigned longs instead of using unionized arrays for KVM (u32s) versus
IRQ management (u64s).  In practice, because the non-KVM usage is (sanely)
restricted to 64-bit kernels, all existing usage of the u64 variant is
already working with unsigned longs.

Using "unsigned long" for the array will allow reworking KVM's processing
of the bitmap to read/write in 64-bit chunks on 64-bit kernels, i.e. will
allow optimizing KVM by reducing the number of atomic accesses to PIR.

Opportunstically replace the open coded literals in the posted MSIs code
with the appropriate macro.  Deliberately don't use ARRAY_SIZE() in the
for-loops, even though it would be cleaner from a certain perspective, in
anticipation of decoupling the processing from the array declaration.

No functional change intended.

Link: https://lore.kernel.org/r/20250401163447.846608-5-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/include/asm/posted_intr.h
arch/x86/kernel/irq.c
arch/x86/kvm/lapic.c
arch/x86/kvm/lapic.h
arch/x86/kvm/vmx/posted_intr.h

index bb107ebbe7131b3bc917b265e5bc1af9da534acd..d86aa4badceef9a8c20aa9a08fbcff85da54841b 100644 (file)
@@ -8,12 +8,12 @@
 
 #define PID_TABLE_ENTRY_VALID 1
 
+#define NR_PIR_VECTORS 256
+#define NR_PIR_WORDS   (NR_PIR_VECTORS / BITS_PER_LONG)
+
 /* Posted-Interrupt Descriptor */
 struct pi_desc {
-       union {
-               u32 pir[8];     /* Posted interrupt requested */
-               u64 pir64[4];
-       };
+       unsigned long pir[NR_PIR_WORDS];     /* Posted interrupt requested */
        union {
                struct {
                        u16     notifications; /* Suppress and outstanding bits */
@@ -43,12 +43,12 @@ static inline bool pi_test_and_clear_sn(struct pi_desc *pi_desc)
 
 static inline bool pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
 {
-       return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
+       return test_and_set_bit(vector, pi_desc->pir);
 }
 
 static inline bool pi_is_pir_empty(struct pi_desc *pi_desc)
 {
-       return bitmap_empty((unsigned long *)pi_desc->pir, NR_VECTORS);
+       return bitmap_empty(pi_desc->pir, NR_VECTORS);
 }
 
 static inline void pi_set_sn(struct pi_desc *pi_desc)
@@ -110,7 +110,7 @@ static inline bool pi_pending_this_cpu(unsigned int vector)
        if (WARN_ON_ONCE(vector > NR_VECTORS || vector < FIRST_EXTERNAL_VECTOR))
                return false;
 
-       return test_bit(vector, (unsigned long *)pid->pir);
+       return test_bit(vector, pid->pir);
 }
 
 extern void intel_posted_msi_init(void);
index 6d2c8894877e86a089d8119040e48e9973defb09..c8cf7c61fc57eccf16a06d3f9209689141b2368c 100644 (file)
@@ -412,12 +412,12 @@ void intel_posted_msi_init(void)
  * instead of:
  *             read, xchg, read, xchg, read, xchg, read, xchg
  */
-static __always_inline bool handle_pending_pir(u64 *pir, struct pt_regs *regs)
+static __always_inline bool handle_pending_pir(unsigned long *pir, struct pt_regs *regs)
 {
-       unsigned long pir_copy[4], pending = 0;
+       unsigned long pir_copy[NR_PIR_WORDS], pending = 0;
        int i, vec = FIRST_EXTERNAL_VECTOR;
 
-       for (i = 0; i < 4; i++) {
+       for (i = 0; i < NR_PIR_WORDS; i++) {
                pir_copy[i] = READ_ONCE(pir[i]);
                pending |= pir_copy[i];
        }
@@ -425,7 +425,7 @@ static __always_inline bool handle_pending_pir(u64 *pir, struct pt_regs *regs)
        if (!pending)
                return false;
 
-       for (i = 0; i < 4; i++) {
+       for (i = 0; i < NR_PIR_WORDS; i++) {
                if (!pir_copy[i])
                        continue;
 
@@ -465,7 +465,7 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_posted_msi_notification)
         * MAX_POSTED_MSI_COALESCING_LOOP - 1 loops are executed here.
         */
        while (++i < MAX_POSTED_MSI_COALESCING_LOOP) {
-               if (!handle_pending_pir(pid->pir64, regs))
+               if (!handle_pending_pir(pid->pir, regs))
                        break;
        }
 
@@ -480,7 +480,7 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_posted_msi_notification)
         * process PIR bits one last time such that handling the new interrupts
         * are not delayed until the next IRQ.
         */
-       handle_pending_pir(pid->pir64, regs);
+       handle_pending_pir(pid->pir, regs);
 
        apic_eoi();
        irq_exit();
index 38d793a96686253b31464f38433e02168f930f73..bd72701256bc1dc14e183177825e1ca382443649 100644 (file)
@@ -655,8 +655,9 @@ static u8 count_vectors(void *bitmap)
        return count;
 }
 
-bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
+bool __kvm_apic_update_irr(unsigned long *pir, void *regs, int *max_irr)
 {
+       u32 *__pir = (void *)pir;
        u32 i, vec;
        u32 pir_val, irr_val, prev_irr_val;
        int max_updated_irr;
@@ -668,10 +669,10 @@ bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
                u32 *p_irr = (u32 *)(regs + APIC_IRR + i * 0x10);
 
                irr_val = READ_ONCE(*p_irr);
-               pir_val = READ_ONCE(pir[i]);
+               pir_val = READ_ONCE(__pir[i]);
 
                if (pir_val) {
-                       pir_val = xchg(&pir[i], 0);
+                       pir_val = xchg(&__pir[i], 0);
 
                        prev_irr_val = irr_val;
                        do {
@@ -691,7 +692,7 @@ bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
 }
 EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
 
-bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
+bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, unsigned long *pir, int *max_irr)
 {
        struct kvm_lapic *apic = vcpu->arch.apic;
        bool irr_updated = __kvm_apic_update_irr(pir, apic->regs, max_irr);
index e33c969439f72a8be39927b0740b5fc9a21cba8b..4ce30db6582814a52f3ef2812683e3ff8ce42cd4 100644 (file)
@@ -103,8 +103,8 @@ bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
                           int shorthand, unsigned int dest, int dest_mode);
 int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2);
 void kvm_apic_clear_irr(struct kvm_vcpu *vcpu, int vec);
-bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr);
-bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr);
+bool __kvm_apic_update_irr(unsigned long *pir, void *regs, int *max_irr);
+bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, unsigned long *pir, int *max_irr);
 void kvm_apic_update_ppr(struct kvm_vcpu *vcpu);
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
                     struct dest_map *dest_map);
index 68605ca7ef689fbb7894984c463d7bbbaad01649..8d183983cd915bce9167428a550935d7004f2609 100644 (file)
@@ -20,7 +20,7 @@ static inline int pi_find_highest_vector(struct pi_desc *pi_desc)
 {
        int vec;
 
-       vec = find_last_bit((unsigned long *)pi_desc->pir, 256);
+       vec = find_last_bit(pi_desc->pir, 256);
        return vec < 256 ? vec : -1;
 }