Skip to contentSkip to conversation

KVM: x86: Clean up lowest priority IRQ code

PATCH Applied Sean Christopherson 1 participant 5 messages Aug 21, 2025 – Sep 16, 2025

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: x86: Clean up lowest priority IRQ code

Move some local APIC specific code into lapic.c that has no business being
exposed outside of local APIC emulation. The lowest priority vector
hashing code in particular is *very* specific to lapic.c internals, but that's not at all obvious from the globally-visible symbols.

Sean Christopherson (3):
KVM: x86: Move kvm_irq_delivery_to_apic() from irq.c to lapic.c
KVM: x86: Make "lowest priority" helpers local to lapic.c
KVM: x86: Move vector_hashing into lapic.c

 arch/x86/kvm/irq.c   | 57 ------------------------------
 arch/x86/kvm/irq.h   |  4 ---
 arch/x86/kvm/lapic.c | 82 +++++++++++++++++++++++++++++++++++++++-----
 arch/x86/kvm/lapic.h | 12 ++-----
 arch/x86/kvm/x86.c   |  8 -----
 arch/x86/kvm/x86.h   |  1 -
 6 files changed, 77 insertions(+), 87 deletions(-)

base-commit: 9c7a1f8d8784e17bb6e0df4f616fbd8daae02919

Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
author maintainer
#1 lore raw

PATCH 1/3 KVM: x86: Move kvm_irq_delivery_to_apic() from irq.c to lapic.c

Move kvm_irq_delivery_to_apic() to lapic.c as it is specific to local APIC emulation. This will allow burying more local APIC code in lapic.c, e.g. the various "lowest priority" helpers.

No functional change intended.

---
 arch/x86/kvm/irq.c   | 57 --------------------------------------------
 arch/x86/kvm/irq.h   |  4 ----
 arch/x86/kvm/lapic.c | 57 ++++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/lapic.h |  3 +++
 4 files changed, 60 insertions(+), 61 deletions(-)
arch/x86/kvm/irq.c +0 -57
index 16da89259011..a6b122f732be 100644
@@ -195,63 +195,6 @@ bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
 	return irqchip_in_kernel(kvm);
 }
 
-int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
-			     struct kvm_lapic_irq *irq, struct dest_map *dest_map)
-{
-	int r = -1;
-	struct kvm_vcpu *vcpu, *lowest = NULL;
-	unsigned long i, dest_vcpu_bitmap[BITS_TO_LONGS(KVM_MAX_VCPUS)];
-	unsigned int dest_vcpus = 0;
-
-	if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map))
-		return r;
-
-	if (irq->dest_mode == APIC_DEST_PHYSICAL &&
-	    irq->dest_id == 0xff && kvm_lowest_prio_delivery(irq)) {
-		pr_info("apic: phys broadcast and lowest prio\n");
-		irq->delivery_mode = APIC_DM_FIXED;
-	}
-
-	memset(dest_vcpu_bitmap, 0, sizeof(dest_vcpu_bitmap));
-
-	kvm_for_each_vcpu(i, vcpu, kvm) {
-		if (!kvm_apic_present(vcpu))
-			continue;
-
-		if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
-					irq->dest_id, irq->dest_mode))
-			continue;
-
-		if (!kvm_lowest_prio_delivery(irq)) {
-			if (r < 0)
-				r = 0;
-			r += kvm_apic_set_irq(vcpu, irq, dest_map);
-		} else if (kvm_apic_sw_enabled(vcpu->arch.apic)) {
-			if (!kvm_vector_hashing_enabled()) {
-				if (!lowest)
-					lowest = vcpu;
-				else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
-					lowest = vcpu;
-			} else {
-				__set_bit(i, dest_vcpu_bitmap);
-				dest_vcpus++;
-			}
-		}
-	}
-
-	if (dest_vcpus != 0) {
-		int idx = kvm_vector_to_index(irq->vector, dest_vcpus,
-					dest_vcpu_bitmap, KVM_MAX_VCPUS);
-
-		lowest = kvm_get_vcpu(kvm, idx);
-	}
-
-	if (lowest)
-		r = kvm_apic_set_irq(lowest, irq, dest_map);
-
-	return r;
-}
-
 static void kvm_msi_to_lapic_irq(struct kvm *kvm,
 				 struct kvm_kernel_irq_routing_entry *e,
 				 struct kvm_lapic_irq *irq)
arch/x86/kvm/irq.h +0 -4
index 5e62c1f79ce6..34f4a78a7a01 100644
@@ -121,8 +121,4 @@ void __kvm_migrate_timers(struct kvm_vcpu *vcpu);
 
 int apic_has_pending_timer(struct kvm_vcpu *vcpu);
 
-int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
-			     struct kvm_lapic_irq *irq,
-			     struct dest_map *dest_map);
-
 #endif
arch/x86/kvm/lapic.c +57 -0

63 lines not on this page · raw message

arch/x86/kvm/lapic.h +3 -0
index 8b00e29741de..edfed763cf89 100644
@@ -119,6 +119,9 @@ void kvm_inhibit_apic_access_page(struct kvm_vcpu *vcpu);
 
 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
 		struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map);
+int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
+			     struct kvm_lapic_irq *irq,
+			     struct dest_map *dest_map);
 void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high);
 
 int kvm_apic_set_base(struct kvm_vcpu *vcpu, u64 value, bool host_initiated);
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
author maintainer
#2 lore raw

PATCH 2/3 KVM: x86: Make "lowest priority" helpers local to lapic.c

In reply to Sean Christopherson

Make various helpers for resolving lowest priority IRQs local to lapic.c now that kvm_irq_delivery_to_apic() lives in lapic.c as well.

No functional change intended.

---
 arch/x86/kvm/lapic.c | 19 ++++++++++++-------
 arch/x86/kvm/lapic.h |  9 ---------
 2 files changed, 12 insertions(+), 16 deletions(-)
arch/x86/kvm/lapic.c +12 -7
index 129ade22efca..1a8bc81973e3 100644
@@ -1063,8 +1063,8 @@ bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
 }
 EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
 
-int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
-		       const unsigned long *bitmap, u32 bitmap_size)
+static int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
+			       const unsigned long *bitmap, u32 bitmap_size)
 {
 	int idx = find_nth_bit(bitmap, bitmap_size, vector % dest_vcpus);
 
@@ -1099,6 +1099,16 @@ static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
 	return false;
 }
 
+static bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq)
+{
+	return (irq->delivery_mode == APIC_DM_LOWEST || irq->msi_redir_hint);
+}
+
+static int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
+{
+	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
+}
+
 /* Return true if the interrupt can be handled by using *bitmap as index mask
  * for valid destinations in *dst array.
  * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
@@ -1451,11 +1461,6 @@ void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq,
 	rcu_read_unlock();
 }
 
-int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
-{
-	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
-}
-
 static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
 {
 	return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
arch/x86/kvm/lapic.h +0 -9
index edfed763cf89..50123fe7f58f 100644
@@ -105,7 +105,6 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu);
 void kvm_apic_after_set_mcg_cap(struct kvm_vcpu *vcpu);
 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(unsigned long *pir, void *regs, int *max_irr);
 bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, unsigned long *pir, int *max_irr);
@@ -225,12 +224,6 @@ static inline bool kvm_apic_init_sipi_allowed(struct kvm_vcpu *vcpu)
 	       !kvm_x86_call(apic_init_signal_blocked)(vcpu);
 }
 
-static inline bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq)
-{
-	return (irq->delivery_mode == APIC_DM_LOWEST ||
-			irq->msi_redir_hint);
-}
-
 static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu)
 {
 	return lapic_in_kernel(vcpu) && test_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
@@ -245,8 +238,6 @@ void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq,
 
 bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
 			struct kvm_vcpu **dest_vcpu);
-int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
-			const unsigned long *bitmap, u32 bitmap_size);
 void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu);
 void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu);
 void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu);
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
author maintainer
#3 lore raw

PATCH 3/3 KVM: x86: Move vector_hashing into lapic.c

In reply to Sean Christopherson

Move the vector_hashing module param into lapic.c now that all usage is contained within the local APIC emulation code.

Opportunistically drop the accessor and append "_enabled" to the variable to help capture that it's a boolean module param.

No functional change intended.

---
 arch/x86/kvm/lapic.c | 8 ++++++--
 arch/x86/kvm/x86.c   | 8 --------
 arch/x86/kvm/x86.h   | 1 -
 3 files changed, 6 insertions(+), 11 deletions(-)
arch/x86/kvm/lapic.c +6 -2
index 1a8bc81973e3..6fac6fb86c19 100644
@@ -74,6 +74,10 @@ module_param(lapic_timer_advance, bool, 0444);
 #define LAPIC_TIMER_ADVANCE_NS_MAX     5000
 /* step-by-step approximation to mitigate fluctuation */
 #define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
+
+static bool __read_mostly vector_hashing_enabled = true;
+module_param_named(vector_hashing, vector_hashing_enabled, bool, 0444);
+
 static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data);
 static int kvm_lapic_msr_write(struct kvm_lapic *apic, u32 reg, u64 data);
 
@@ -1152,7 +1156,7 @@ static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
 	if (!kvm_lowest_prio_delivery(irq))
 		return true;
 
-	if (!kvm_vector_hashing_enabled()) {
+	if (!vector_hashing_enabled) {
 		lowest = -1;
 		for_each_set_bit(i, bitmap, 16) {
 			if (!(*dst)[i])
@@ -1293,7 +1297,7 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
 				r = 0;
 			r += kvm_apic_set_irq(vcpu, irq, dest_map);
 		} else if (kvm_apic_sw_enabled(vcpu->arch.apic)) {
-			if (!kvm_vector_hashing_enabled()) {
+			if (!vector_hashing_enabled) {
 				if (!lowest)
 					lowest = vcpu;
 				else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
arch/x86/kvm/x86.c +0 -8
index 7ba2cdfdac44..554b36de700c 100644
@@ -164,9 +164,6 @@ module_param(kvmclock_periodic_sync, bool, 0444);
 static u32 __read_mostly tsc_tolerance_ppm = 250;
 module_param(tsc_tolerance_ppm, uint, 0644);
 
-static bool __read_mostly vector_hashing = true;
-module_param(vector_hashing, bool, 0444);
-
 bool __read_mostly enable_vmware_backdoor = false;
 module_param(enable_vmware_backdoor, bool, 0444);
 EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
@@ -13552,11 +13549,6 @@ bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
 }
 EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
 
-bool kvm_vector_hashing_enabled(void)
-{
-	return vector_hashing;
-}
-
 bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
 {
 	return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
arch/x86/kvm/x86.h +0 -1
index eb3088684e8a..786e36fcd0fb 100644
@@ -431,7 +431,6 @@ void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu,
 
 int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
 int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
-bool kvm_vector_hashing_enabled(void);
 void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code);
 int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
 				    void *insn, int insn_len);
Signature · 1 line
2.51.0.261.g7ce5a0a67e-goog
author maintainer Applied
#4 lore raw
In reply to Sean Christopherson
Quoted 11 lines · Sean Christopherson

Move some local APIC specific code into lapic.c that has no business being
exposed outside of local APIC emulation. The lowest priority vector
hashing code in particular is *very* specific to lapic.c internals, but that's not at all obvious from the globally-visible symbols.

Sean Christopherson (3):
KVM: x86: Move kvm_irq_delivery_to_apic() from irq.c to lapic.c
KVM: x86: Make "lowest priority" helpers local to lapic.c
KVM: x86: Move vector_hashing into lapic.c

[...]

Applied to kvm-x86 misc, thanks!

[1/3] KVM: x86: Move kvm_irq_delivery_to_apic() from irq.c to lapic.c

      https://github.com/kvm-x86/linux/commit/cbf5d9457462
[2/3] KVM: x86: Make "lowest priority" helpers local to lapic.c
      https://github.com/kvm-x86/linux/commit/73473f31a4bf
[3/3] KVM: x86: Move vector_hashing into lapic.c
      https://github.com/kvm-x86/linux/commit/aac057dd6231

--
https://github.com/kvm-x86/linux/tree/next