Skip to content

Commit 77e1b83

Browse files
committed
KVM: x86: Decouple device assignment from IRQ bypass
Use a dedicated counter to track the number of IRQs that can utilize IRQ bypass instead of piggybacking the assigned device count. As evidenced by commit 2edd9cb ("kvm: detect assigned device via irqbypass manager"), it's possible for a device to be able to post IRQs to a vCPU without said device being assigned to a VM. Leave the calls to kvm_arch_{start,end}_assignment() alone for the moment to avoid regressing the MMIO stale data mitigation. KVM is abusing the assigned device count when applying mmio_stale_data_clear, and it's not at all clear if vDPA devices rely on this behavior. This will hopefully be cleaned up in the future, as the number of assigned devices is a terrible heuristic for detecting if a VM has access to host MMIO. Link: https://lore.kernel.org/r/20250611224604.313496-55-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 99836eb commit 77e1b83

File tree

7 files changed

+25
-13
lines changed

7 files changed

+25
-13
lines changed

arch/x86/include/asm/kvm-x86-ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ KVM_X86_OP_OPTIONAL(update_cpu_dirty_logging)
112112
KVM_X86_OP_OPTIONAL(vcpu_blocking)
113113
KVM_X86_OP_OPTIONAL(vcpu_unblocking)
114114
KVM_X86_OP_OPTIONAL(pi_update_irte)
115-
KVM_X86_OP_OPTIONAL(pi_start_assignment)
115+
KVM_X86_OP_OPTIONAL(pi_start_bypass)
116116
KVM_X86_OP_OPTIONAL(apicv_pre_state_restore)
117117
KVM_X86_OP_OPTIONAL(apicv_post_state_restore)
118118
KVM_X86_OP_OPTIONAL_RET0(dy_apicv_has_pending_interrupt)

arch/x86/include/asm/kvm_host.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,8 @@ struct kvm_arch {
13831383
atomic_t noncoherent_dma_count;
13841384
#define __KVM_HAVE_ARCH_ASSIGNED_DEVICE
13851385
atomic_t assigned_device_count;
1386+
unsigned long nr_possible_bypass_irqs;
1387+
13861388
#ifdef CONFIG_KVM_IOAPIC
13871389
struct kvm_pic *vpic;
13881390
struct kvm_ioapic *vioapic;
@@ -1856,7 +1858,7 @@ struct kvm_x86_ops {
18561858
int (*pi_update_irte)(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
18571859
unsigned int host_irq, uint32_t guest_irq,
18581860
struct kvm_vcpu *vcpu, u32 vector);
1859-
void (*pi_start_assignment)(struct kvm *kvm);
1861+
void (*pi_start_bypass)(struct kvm *kvm);
18601862
void (*apicv_pre_state_restore)(struct kvm_vcpu *vcpu);
18611863
void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);
18621864
bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu);

arch/x86/kvm/irq.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,15 @@ int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
570570
spin_lock_irq(&kvm->irqfds.lock);
571571
irqfd->producer = prod;
572572

573+
if (!kvm->arch.nr_possible_bypass_irqs++)
574+
kvm_x86_call(pi_start_bypass)(kvm);
575+
573576
if (irqfd->irq_entry.type == KVM_IRQ_ROUTING_MSI) {
574577
ret = kvm_pi_update_irte(irqfd, &irqfd->irq_entry);
575-
if (ret)
578+
if (ret) {
579+
kvm->arch.nr_possible_bypass_irqs--;
576580
kvm_arch_end_assignment(irqfd->kvm);
581+
}
577582
}
578583
spin_unlock_irq(&kvm->irqfds.lock);
579584

@@ -606,6 +611,8 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
606611
}
607612
irqfd->producer = NULL;
608613

614+
kvm->arch.nr_possible_bypass_irqs--;
615+
609616
spin_unlock_irq(&kvm->irqfds.lock);
610617

611618

arch/x86/kvm/vmx/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
10141014
.nested_ops = &vmx_nested_ops,
10151015

10161016
.pi_update_irte = vmx_pi_update_irte,
1017-
.pi_start_assignment = vmx_pi_start_assignment,
1017+
.pi_start_bypass = vmx_pi_start_bypass,
10181018

10191019
#ifdef CONFIG_X86_64
10201020
.set_hv_timer = vt_op(set_hv_timer),

arch/x86/kvm/vmx/posted_intr.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,13 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
146146

147147
static bool vmx_can_use_vtd_pi(struct kvm *kvm)
148148
{
149+
/*
150+
* Note, reading the number of possible bypass IRQs can race with a
151+
* bypass IRQ being attached to the VM. vmx_pi_start_bypass() ensures
152+
* blockng vCPUs will see an elevated count or get KVM_REQ_UNBLOCK.
153+
*/
149154
return irqchip_in_kernel(kvm) && kvm_arch_has_irq_bypass() &&
150-
kvm_arch_has_assigned_device(kvm);
155+
READ_ONCE(kvm->arch.nr_possible_bypass_irqs);
151156
}
152157

153158
/*
@@ -285,12 +290,11 @@ bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu)
285290

286291

287292
/*
288-
* Bail out of the block loop if the VM has an assigned
289-
* device, but the blocking vCPU didn't reconfigure the
290-
* PI.NV to the wakeup vector, i.e. the assigned device
291-
* came along after the initial check in vmx_vcpu_pi_put().
293+
* Kick all vCPUs when the first possible bypass IRQ is attached to a VM, as
294+
* blocking vCPUs may scheduled out without reconfiguring PID.NV to the wakeup
295+
* vector, i.e. if the bypass IRQ came along after vmx_vcpu_pi_put().
292296
*/
293-
void vmx_pi_start_assignment(struct kvm *kvm)
297+
void vmx_pi_start_bypass(struct kvm *kvm)
294298
{
295299
if (!kvm_arch_has_irq_bypass())
296300
return;

arch/x86/kvm/vmx/posted_intr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ 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,
1919
struct kvm_vcpu *vcpu, u32 vector);
20-
void vmx_pi_start_assignment(struct kvm *kvm);
20+
void vmx_pi_start_bypass(struct kvm *kvm);
2121

2222
static inline int pi_find_highest_vector(struct pi_desc *pi_desc)
2323
{

arch/x86/kvm/x86.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13440,8 +13440,7 @@ bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
1344013440

1344113441
void kvm_arch_start_assignment(struct kvm *kvm)
1344213442
{
13443-
if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1)
13444-
kvm_x86_call(pi_start_assignment)(kvm);
13443+
atomic_inc(&kvm->arch.assigned_device_count);
1344513444
}
1344613445
EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
1344713446

0 commit comments

Comments
 (0)