Skip to content

Commit 05c5e23

Browse files
committed
KVM: SVM: Track per-vCPU IRTEs using kvm_kernel_irqfd structure
Track the IRTEs that are posting to an SVM vCPU via the associated irqfd structure and GSI routing instead of dynamically allocating a separate data structure. In addition to eliminating an atomic allocation, this will allow hoisting much of the IRTE update logic to common x86. Cc: Sairaj Kodilkar <sarunkod@amd.com> Link: https://lore.kernel.org/r/20250611224604.313496-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent cb21073 commit 05c5e23

File tree

3 files changed

+36
-48
lines changed

3 files changed

+36
-48
lines changed

arch/x86/kvm/svm/avic.c

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,6 @@ static bool next_vm_id_wrapped = 0;
7676
static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
7777
bool x2avic_enabled;
7878

79-
/*
80-
* This is a wrapper of struct amd_iommu_ir_data.
81-
*/
82-
struct amd_svm_iommu_ir {
83-
struct list_head node; /* Used by SVM for per-vcpu ir_list */
84-
void *data; /* Storing pointer to struct amd_ir_data */
85-
};
86-
8779
static void avic_activate_vmcb(struct vcpu_svm *svm)
8880
{
8981
struct vmcb *vmcb = svm->vmcb01.ptr;
@@ -747,8 +739,8 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate)
747739
{
748740
int ret = 0;
749741
unsigned long flags;
750-
struct amd_svm_iommu_ir *ir;
751742
struct vcpu_svm *svm = to_svm(vcpu);
743+
struct kvm_kernel_irqfd *irqfd;
752744

753745
if (!kvm_arch_has_assigned_device(vcpu->kvm))
754746
return 0;
@@ -762,11 +754,11 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate)
762754
if (list_empty(&svm->ir_list))
763755
goto out;
764756

765-
list_for_each_entry(ir, &svm->ir_list, node) {
757+
list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) {
766758
if (activate)
767-
ret = amd_iommu_activate_guest_mode(ir->data);
759+
ret = amd_iommu_activate_guest_mode(irqfd->irq_bypass_data);
768760
else
769-
ret = amd_iommu_deactivate_guest_mode(ir->data);
761+
ret = amd_iommu_deactivate_guest_mode(irqfd->irq_bypass_data);
770762
if (ret)
771763
break;
772764
}
@@ -775,27 +767,30 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate)
775767
return ret;
776768
}
777769

778-
static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
770+
static void svm_ir_list_del(struct vcpu_svm *svm,
771+
struct kvm_kernel_irqfd *irqfd,
772+
struct amd_iommu_pi_data *pi)
779773
{
780774
unsigned long flags;
781-
struct amd_svm_iommu_ir *cur;
775+
struct kvm_kernel_irqfd *cur;
782776

783777
spin_lock_irqsave(&svm->ir_list_lock, flags);
784-
list_for_each_entry(cur, &svm->ir_list, node) {
785-
if (cur->data != pi->ir_data)
778+
list_for_each_entry(cur, &svm->ir_list, vcpu_list) {
779+
if (cur->irq_bypass_data != pi->ir_data)
780+
continue;
781+
if (WARN_ON_ONCE(cur != irqfd))
786782
continue;
787-
list_del(&cur->node);
788-
kfree(cur);
783+
list_del(&irqfd->vcpu_list);
789784
break;
790785
}
791786
spin_unlock_irqrestore(&svm->ir_list_lock, flags);
792787
}
793788

794-
static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
789+
static int svm_ir_list_add(struct vcpu_svm *svm,
790+
struct kvm_kernel_irqfd *irqfd,
791+
struct amd_iommu_pi_data *pi)
795792
{
796-
int ret = 0;
797793
unsigned long flags;
798-
struct amd_svm_iommu_ir *ir;
799794
u64 entry;
800795

801796
if (WARN_ON_ONCE(!pi->ir_data))
@@ -812,25 +807,14 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
812807
struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
813808
struct vcpu_svm *prev_svm;
814809

815-
if (!prev_vcpu) {
816-
ret = -EINVAL;
817-
goto out;
818-
}
810+
if (!prev_vcpu)
811+
return -EINVAL;
819812

820813
prev_svm = to_svm(prev_vcpu);
821-
svm_ir_list_del(prev_svm, pi);
814+
svm_ir_list_del(prev_svm, irqfd, pi);
822815
}
823816

824-
/**
825-
* Allocating new amd_iommu_pi_data, which will get
826-
* add to the per-vcpu ir_list.
827-
*/
828-
ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_ATOMIC | __GFP_ACCOUNT);
829-
if (!ir) {
830-
ret = -ENOMEM;
831-
goto out;
832-
}
833-
ir->data = pi->ir_data;
817+
irqfd->irq_bypass_data = pi->ir_data;
834818

835819
spin_lock_irqsave(&svm->ir_list_lock, flags);
836820

@@ -845,10 +829,9 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
845829
amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK,
846830
true, pi->ir_data);
847831

848-
list_add(&ir->node, &svm->ir_list);
832+
list_add(&irqfd->vcpu_list, &svm->ir_list);
849833
spin_unlock_irqrestore(&svm->ir_list_lock, flags);
850-
out:
851-
return ret;
834+
return 0;
852835
}
853836

854837
/*
@@ -952,7 +935,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
952935
* scheduling information in IOMMU irte.
953936
*/
954937
if (!ret && pi.is_guest_mode)
955-
svm_ir_list_add(svm, &pi);
938+
svm_ir_list_add(svm, irqfd, &pi);
956939
}
957940

958941
if (!ret && svm) {
@@ -993,7 +976,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
993976

994977
vcpu = kvm_get_vcpu_by_id(kvm, id);
995978
if (vcpu)
996-
svm_ir_list_del(to_svm(vcpu), &pi);
979+
svm_ir_list_del(to_svm(vcpu), irqfd, &pi);
997980
}
998981
}
999982
out:
@@ -1005,8 +988,8 @@ static inline int
1005988
avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
1006989
{
1007990
int ret = 0;
1008-
struct amd_svm_iommu_ir *ir;
1009991
struct vcpu_svm *svm = to_svm(vcpu);
992+
struct kvm_kernel_irqfd *irqfd;
1010993

1011994
lockdep_assert_held(&svm->ir_list_lock);
1012995

@@ -1020,8 +1003,8 @@ avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
10201003
if (list_empty(&svm->ir_list))
10211004
return 0;
10221005

1023-
list_for_each_entry(ir, &svm->ir_list, node) {
1024-
ret = amd_iommu_update_ga(cpu, r, ir->data);
1006+
list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) {
1007+
ret = amd_iommu_update_ga(cpu, r, irqfd->irq_bypass_data);
10251008
if (ret)
10261009
return ret;
10271010
}

arch/x86/kvm/svm/svm.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,12 @@ struct vcpu_svm {
310310
u64 *avic_physical_id_cache;
311311

312312
/*
313-
* Per-vcpu list of struct amd_svm_iommu_ir:
314-
* This is used mainly to store interrupt remapping information used
315-
* when update the vcpu affinity. This avoids the need to scan for
316-
* IRTE and try to match ga_tag in the IOMMU driver.
313+
* Per-vCPU list of irqfds that are eligible to post IRQs directly to
314+
* the vCPU (a.k.a. device posted IRQs, a.k.a. IRQ bypass). The list
315+
* is used to reconfigure IRTEs when the vCPU is loaded/put (to set the
316+
* target pCPU), when AVIC is toggled on/off (to (de)activate bypass),
317+
* and if the irqfd becomes ineligible for posting (to put the IRTE
318+
* back into remapped mode).
317319
*/
318320
struct list_head ir_list;
319321
spinlock_t ir_list_lock;

include/linux/kvm_irqfd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ struct kvm_kernel_irqfd {
5959
struct work_struct shutdown;
6060
struct irq_bypass_consumer consumer;
6161
struct irq_bypass_producer *producer;
62+
63+
struct list_head vcpu_list;
64+
void *irq_bypass_data;
6265
};
6366

6467
#endif /* __LINUX_KVM_IRQFD_H */

0 commit comments

Comments
 (0)