Skip to content

Commit cf04ec3

Browse files
committed
KVM: x86: Dedup AVIC vs. PI code for identifying target vCPU
Hoist the logic for identifying the target vCPU for a posted interrupt into common x86. The code is functionally identical between Intel and AMD. Tested-by: Sairaj Kodilkar <sarunkod@amd.com> Link: https://lore.kernel.org/r/20250611224604.313496-30-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9517aed commit cf04ec3

File tree

6 files changed

+75
-113
lines changed

6 files changed

+75
-113
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ struct kvm_x86_ops {
18551855

18561856
int (*pi_update_irte)(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
18571857
unsigned int host_irq, uint32_t guest_irq,
1858-
struct kvm_kernel_irq_routing_entry *new);
1858+
struct kvm_vcpu *vcpu, u32 vector);
18591859
void (*pi_start_assignment)(struct kvm *kvm);
18601860
void (*apicv_pre_state_restore)(struct kvm_vcpu *vcpu);
18611861
void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);

arch/x86/kvm/irq.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,42 @@ void kvm_arch_irq_routing_update(struct kvm *kvm)
508508
kvm_make_scan_ioapic_request(kvm);
509509
}
510510

511+
static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd,
512+
struct kvm_kernel_irq_routing_entry *entry)
513+
{
514+
struct kvm *kvm = irqfd->kvm;
515+
struct kvm_vcpu *vcpu = NULL;
516+
struct kvm_lapic_irq irq;
517+
518+
if (!irqchip_in_kernel(kvm) ||
519+
!kvm_arch_has_irq_bypass() ||
520+
!kvm_arch_has_assigned_device(kvm))
521+
return 0;
522+
523+
if (entry && entry->type == KVM_IRQ_ROUTING_MSI) {
524+
kvm_set_msi_irq(kvm, entry, &irq);
525+
526+
/*
527+
* Force remapped mode if hardware doesn't support posting the
528+
* virtual interrupt to a vCPU. Only IRQs are postable (NMIs,
529+
* SMIs, etc. are not), and neither AMD nor Intel IOMMUs support
530+
* posting multicast/broadcast IRQs. If the interrupt can't be
531+
* posted, the device MSI needs to be routed to the host so that
532+
* the guest's desired interrupt can be synthesized by KVM.
533+
*
534+
* This means that KVM can only post lowest-priority interrupts
535+
* if they have a single CPU as the destination, e.g. only if
536+
* the guest has affined the interrupt to a single vCPU.
537+
*/
538+
if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
539+
!kvm_irq_is_postable(&irq))
540+
vcpu = NULL;
541+
}
542+
543+
return kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, irqfd->producer->irq,
544+
irqfd->gsi, vcpu, irq.vector);
545+
}
546+
511547
int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
512548
struct irq_bypass_producer *prod)
513549
{
@@ -522,8 +558,7 @@ int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
522558
irqfd->producer = prod;
523559

524560
if (irqfd->irq_entry.type == KVM_IRQ_ROUTING_MSI) {
525-
ret = kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq,
526-
irqfd->gsi, &irqfd->irq_entry);
561+
ret = kvm_pi_update_irte(irqfd, &irqfd->irq_entry);
527562
if (ret)
528563
kvm_arch_end_assignment(irqfd->kvm);
529564
}
@@ -551,8 +586,7 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
551586
spin_lock_irq(&kvm->irqfds.lock);
552587

553588
if (irqfd->irq_entry.type == KVM_IRQ_ROUTING_MSI) {
554-
ret = kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq,
555-
irqfd->gsi, NULL);
589+
ret = kvm_pi_update_irte(irqfd, NULL);
556590
if (ret)
557591
pr_info("irq bypass consumer (eventfd %p) unregistration fails: %d\n",
558592
irqfd->consumer.eventfd, ret);
@@ -568,8 +602,7 @@ int kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd,
568602
struct kvm_kernel_irq_routing_entry *old,
569603
struct kvm_kernel_irq_routing_entry *new)
570604
{
571-
return kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, irqfd->producer->irq,
572-
irqfd->gsi, new);
605+
return kvm_pi_update_irte(irqfd, new);
573606
}
574607

575608
bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old,

arch/x86/kvm/svm/avic.c

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -804,60 +804,20 @@ static int svm_ir_list_add(struct vcpu_svm *svm,
804804
return 0;
805805
}
806806

807-
/*
808-
* Note:
809-
* The HW cannot support posting multicast/broadcast
810-
* interrupts to a vCPU. So, we still use legacy interrupt
811-
* remapping for these kind of interrupts.
812-
*
813-
* For lowest-priority interrupts, we only support
814-
* those with single CPU as the destination, e.g. user
815-
* configures the interrupts via /proc/irq or uses
816-
* irqbalance to make the interrupts single-CPU.
817-
*/
818-
static int
819-
get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
820-
struct vcpu_data *vcpu_info, struct kvm_vcpu **vcpu)
821-
{
822-
struct kvm_lapic_irq irq;
823-
*vcpu = NULL;
824-
825-
kvm_set_msi_irq(kvm, e, &irq);
826-
827-
if (!kvm_intr_is_single_vcpu(kvm, &irq, vcpu) ||
828-
!kvm_irq_is_postable(&irq)) {
829-
pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
830-
__func__, irq.vector);
831-
return -1;
832-
}
833-
834-
pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
835-
irq.vector);
836-
vcpu_info->vector = irq.vector;
837-
838-
return 0;
839-
}
840-
841807
int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
842808
unsigned int host_irq, uint32_t guest_irq,
843-
struct kvm_kernel_irq_routing_entry *new)
809+
struct kvm_vcpu *vcpu, u32 vector)
844810
{
845-
bool enable_remapped_mode = true;
846-
struct vcpu_data vcpu_info;
847-
struct kvm_vcpu *vcpu = NULL;
848811
int ret = 0;
849812

850-
if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass())
851-
return 0;
852-
853813
/*
854814
* If the IRQ was affined to a different vCPU, remove the IRTE metadata
855815
* from the *previous* vCPU's list.
856816
*/
857817
svm_ir_list_del(irqfd);
858818

859819
pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
860-
__func__, host_irq, guest_irq, !!new);
820+
__func__, host_irq, guest_irq, !!vcpu);
861821

862822
/**
863823
* Here, we setup with legacy mode in the following cases:
@@ -866,23 +826,23 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
866826
* 3. APIC virtualization is disabled for the vcpu.
867827
* 4. IRQ has incompatible delivery mode (SMI, INIT, etc)
868828
*/
869-
if (new && new->type == KVM_IRQ_ROUTING_MSI &&
870-
!get_pi_vcpu_info(kvm, new, &vcpu_info, &vcpu) &&
871-
kvm_vcpu_apicv_active(vcpu)) {
872-
struct amd_iommu_pi_data pi;
873-
874-
enable_remapped_mode = false;
875-
876-
vcpu_info.pi_desc_addr = avic_get_backing_page_address(to_svm(vcpu));
877-
829+
if (vcpu && kvm_vcpu_apicv_active(vcpu)) {
878830
/*
879831
* Try to enable guest_mode in IRTE. Note, the address
880832
* of the vCPU's AVIC backing page is passed to the
881833
* IOMMU via vcpu_info->pi_desc_addr.
882834
*/
883-
pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id);
884-
pi.is_guest_mode = true;
885-
pi.vcpu_data = &vcpu_info;
835+
struct vcpu_data vcpu_info = {
836+
.pi_desc_addr = avic_get_backing_page_address(to_svm(vcpu)),
837+
.vector = vector,
838+
};
839+
840+
struct amd_iommu_pi_data pi = {
841+
.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id),
842+
.is_guest_mode = true,
843+
.vcpu_data = &vcpu_info,
844+
};
845+
886846
ret = irq_set_vcpu_affinity(host_irq, &pi);
887847

888848
/**
@@ -894,23 +854,19 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
894854
*/
895855
if (!ret)
896856
ret = svm_ir_list_add(to_svm(vcpu), irqfd, &pi);
897-
}
898857

899-
if (!ret && vcpu) {
900-
trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id,
901-
guest_irq, vcpu_info.vector,
902-
vcpu_info.pi_desc_addr, !!new);
858+
trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, guest_irq,
859+
vector, vcpu_info.pi_desc_addr, true);
860+
} else {
861+
ret = irq_set_vcpu_affinity(host_irq, NULL);
903862
}
904863

905864
if (ret < 0) {
906865
pr_err("%s: failed to update PI IRTE\n", __func__);
907866
goto out;
908867
}
909868

910-
if (enable_remapped_mode)
911-
ret = irq_set_vcpu_affinity(host_irq, NULL);
912-
else
913-
ret = 0;
869+
ret = 0;
914870
out:
915871
return ret;
916872
}

arch/x86/kvm/svm/svm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu);
747747
void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu);
748748
int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
749749
unsigned int host_irq, uint32_t guest_irq,
750-
struct kvm_kernel_irq_routing_entry *new);
750+
struct kvm_vcpu *vcpu, u32 vector);
751751
void avic_vcpu_blocking(struct kvm_vcpu *vcpu);
752752
void avic_vcpu_unblocking(struct kvm_vcpu *vcpu);
753753
void avic_ring_doorbell(struct kvm_vcpu *vcpu);

arch/x86/kvm/vmx/posted_intr.c

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -300,46 +300,19 @@ void vmx_pi_start_assignment(struct kvm *kvm)
300300

301301
int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
302302
unsigned int host_irq, uint32_t guest_irq,
303-
struct kvm_kernel_irq_routing_entry *new)
303+
struct kvm_vcpu *vcpu, u32 vector)
304304
{
305-
struct kvm_lapic_irq irq;
306-
struct kvm_vcpu *vcpu;
307-
struct vcpu_data vcpu_info;
308-
309-
if (!vmx_can_use_vtd_pi(kvm))
310-
return 0;
311-
312-
/*
313-
* VT-d PI cannot support posting multicast/broadcast
314-
* interrupts to a vCPU, we still use interrupt remapping
315-
* for these kind of interrupts.
316-
*
317-
* For lowest-priority interrupts, we only support
318-
* those with single CPU as the destination, e.g. user
319-
* configures the interrupts via /proc/irq or uses
320-
* irqbalance to make the interrupts single-CPU.
321-
*
322-
* We will support full lowest-priority interrupt later.
323-
*
324-
* In addition, we can only inject generic interrupts using
325-
* the PI mechanism, refuse to route others through it.
326-
*/
327-
if (!new || new->type != KVM_IRQ_ROUTING_MSI)
328-
goto do_remapping;
329-
330-
kvm_set_msi_irq(kvm, new, &irq);
331-
332-
if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
333-
!kvm_irq_is_postable(&irq))
334-
goto do_remapping;
335-
336-
vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
337-
vcpu_info.vector = irq.vector;
338-
339-
trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, guest_irq,
340-
vcpu_info.vector, vcpu_info.pi_desc_addr, true);
341-
342-
return irq_set_vcpu_affinity(host_irq, &vcpu_info);
343-
do_remapping:
344-
return irq_set_vcpu_affinity(host_irq, NULL);
305+
if (vcpu) {
306+
struct vcpu_data vcpu_info = {
307+
.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)),
308+
.vector = vector,
309+
};
310+
311+
trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, guest_irq,
312+
vcpu_info.vector, vcpu_info.pi_desc_addr, true);
313+
314+
return irq_set_vcpu_affinity(host_irq, &vcpu_info);
315+
} else {
316+
return irq_set_vcpu_affinity(host_irq, NULL);
317+
}
345318
}

arch/x86/kvm/vmx/posted_intr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void pi_apicv_pre_state_restore(struct kvm_vcpu *vcpu);
1616
bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu);
1717
int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
1818
unsigned int host_irq, uint32_t guest_irq,
19-
struct kvm_kernel_irq_routing_entry *new);
19+
struct kvm_vcpu *vcpu, u32 vector);
2020
void vmx_pi_start_assignment(struct kvm *kvm);
2121

2222
static inline int pi_find_highest_vector(struct pi_desc *pi_desc)

0 commit comments

Comments
 (0)