Skip to content

Commit 08d9ccd

Browse files
committed
iommu/amd: KVM: SVM: Infer IsRun from validity of pCPU destination
Infer whether or not a vCPU should be marked running from the validity of the pCPU on which it is running. amd_iommu_update_ga() already skips the IRTE update if the pCPU is invalid, i.e. passing %true for is_run with an invalid pCPU would be a blatant and egregrious KVM bug. Tested-by: Sairaj Kodilkar <sarunkod@amd.com> Link: https://lore.kernel.org/r/20250611224604.313496-42-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 3be405e commit 08d9ccd

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

arch/x86/kvm/svm/avic.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
833833
entry = svm->avic_physical_id_entry;
834834
if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
835835
amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK,
836-
true, pi_data.ir_data);
836+
pi_data.ir_data);
837837

838838
irqfd->irq_bypass_data = pi_data.ir_data;
839839
list_add(&irqfd->vcpu_list, &svm->ir_list);
@@ -842,8 +842,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
842842
return irq_set_vcpu_affinity(host_irq, NULL);
843843
}
844844

845-
static inline int
846-
avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
845+
static inline int avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu)
847846
{
848847
int ret = 0;
849848
struct vcpu_svm *svm = to_svm(vcpu);
@@ -862,7 +861,7 @@ avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
862861
return 0;
863862

864863
list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) {
865-
ret = amd_iommu_update_ga(cpu, r, irqfd->irq_bypass_data);
864+
ret = amd_iommu_update_ga(cpu, irqfd->irq_bypass_data);
866865
if (ret)
867866
return ret;
868867
}
@@ -924,7 +923,7 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
924923

925924
WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry);
926925

927-
avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true);
926+
avic_update_iommu_vcpu_affinity(vcpu, h_physical_id);
928927

929928
spin_unlock_irqrestore(&svm->ir_list_lock, flags);
930929
}
@@ -964,7 +963,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu)
964963
*/
965964
spin_lock_irqsave(&svm->ir_list_lock, flags);
966965

967-
avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
966+
avic_update_iommu_vcpu_affinity(vcpu, -1);
968967

969968
entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
970969
svm->avic_physical_id_entry = entry;

drivers/iommu/amd/iommu.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3990,15 +3990,17 @@ int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
39903990
* Update the pCPU information for an IRTE that is configured to post IRQs to
39913991
* a vCPU, without issuing an IOMMU invalidation for the IRTE.
39923992
*
3993-
* This API is intended to be used when a vCPU is scheduled in/out (or stops
3994-
* running for any reason), to do a fast update of IsRun and (conditionally)
3995-
* Destination.
3993+
* If the vCPU is associated with a pCPU (@cpu >= 0), configure the Destination
3994+
* with the pCPU's APIC ID and set IsRun, else clear IsRun. I.e. treat vCPUs
3995+
* that are associated with a pCPU as running. This API is intended to be used
3996+
* when a vCPU is scheduled in/out (or stops running for any reason), to do a
3997+
* fast update of IsRun and (conditionally) Destination.
39963998
*
39973999
* Per the IOMMU spec, the Destination, IsRun, and GATag fields are not cached
39984000
* and thus don't require an invalidation to ensure the IOMMU consumes fresh
39994001
* information.
40004002
*/
4001-
int amd_iommu_update_ga(int cpu, bool is_run, void *data)
4003+
int amd_iommu_update_ga(int cpu, void *data)
40024004
{
40034005
struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
40044006
struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
@@ -4015,8 +4017,10 @@ int amd_iommu_update_ga(int cpu, bool is_run, void *data)
40154017
APICID_TO_IRTE_DEST_LO(cpu);
40164018
entry->hi.fields.destination =
40174019
APICID_TO_IRTE_DEST_HI(cpu);
4020+
entry->lo.fields_vapic.is_run = true;
4021+
} else {
4022+
entry->lo.fields_vapic.is_run = false;
40184023
}
4019-
entry->lo.fields_vapic.is_run = is_run;
40204024

40214025
return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
40224026
ir_data->irq_2_irte.index, entry);

include/linux/amd-iommu.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ static inline void amd_iommu_detect(void) { }
3030
/* IOMMU AVIC Function */
3131
extern int amd_iommu_register_ga_log_notifier(int (*notifier)(u32));
3232

33-
extern int
34-
amd_iommu_update_ga(int cpu, bool is_run, void *data);
33+
extern int amd_iommu_update_ga(int cpu, void *data);
3534

3635
extern int amd_iommu_activate_guest_mode(void *data);
3736
extern int amd_iommu_deactivate_guest_mode(void *data);
@@ -44,8 +43,7 @@ amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
4443
return 0;
4544
}
4645

47-
static inline int
48-
amd_iommu_update_ga(int cpu, bool is_run, void *data)
46+
static inline int amd_iommu_update_ga(int cpu, void *data)
4947
{
5048
return 0;
5149
}

0 commit comments

Comments
 (0)