[PATCH v3 34/62] KVM: x86: Track irq_bypass_vcpu in common x86 code

Sean Christopherson
2025-06-11T22:47:54Z

Track the vCPU that is being targeted for IRQ bypass, a.k.a. for a posted
IRQ, in common x86 code. This will allow for additional consolidation of
the SVM and VMX code.

Tested-by: Sairaj Kodilkar <sarunkod@amd.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/irq.c | 7 ++++++-
arch/x86/kvm/svm/avic.c | 4 ----
2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c
index 48134aebb541..6447ea518d01 100644
--- a/arch/x86/kvm/irq.c
+++ b/arch/x86/kvm/irq.c
@@ -544,8 +544,13 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd,

r = kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, host_irq, irqfd->gsi,
vcpu, irq.vector);
- if (r)
+ if (r) {
+ WARN_ON_ONCE(irqfd->irq_bypass_vcpu && !vcpu);
+ irqfd->irq_bypass_vcpu = NULL;
return r;
+ }
+
+ irqfd->irq_bypass_vcpu = vcpu;

trace_kvm_pi_irte_update(host_irq, vcpu, irqfd->gsi, irq.vector, !!vcpu);
return 0;
diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c
index 6085a629c5e6..97b747e82012 100644
--- a/arch/x86/kvm/svm/avic.c
+++ b/arch/x86/kvm/svm/avic.c
@@ -767,22 +767,18 @@ static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd)
spin_lock_irqsave(&to_svm(vcpu)->ir_list_lock, flags);
list_del(&irqfd->vcpu_list);
spin_unlock_irqrestore(&to_svm(vcpu)->ir_list_lock, flags);
-
- irqfd->irq_bypass_vcpu = NULL;
}

static int svm_ir_list_add(struct vcpu_svm *svm,
struct kvm_kernel_irqfd *irqfd,
struct amd_iommu_pi_data *pi)
{
- struct kvm_vcpu *vcpu = &svm->vcpu;
unsigned long flags;
u64 entry;

if (WARN_ON_ONCE(!pi->ir_data))
return -EINVAL;

- irqfd->irq_bypass_vcpu = vcpu;
irqfd->irq_bypass_data = pi->ir_data;

spin_lock_irqsave(&svm->ir_list_lock, flags);
--
2.50.0.rc1.591.g9c95f17f64-goog