Re: [v5 1/3] KVM: setup empty irq routing when create vm

From: Christian Borntraeger

Date: Fri Apr 17 2026 - 06:27:42 EST



Am 17.04.26 um 12:19 schrieb Paolo Bonzini:
On 4/17/26 11:36, Christian Borntraeger wrote:


         irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm- >irq_srcu,
                                         lockdep_is_held(&kvm- >irq_lock));
         if (irq_rt && gsi < irq_rt->nr_rt_entries) {  <---------

Hmm, I guess I misread the code and the problem is likely not this.
Let me have another look.

It makes sense anyway, together with a similar extra element in
kvm_set_irq_routing():

Yes, it should certainly avoid some cycles in some cases.
Let me try to understand what exactly triggered the increases system time after
the original patch and then I will send some patches.



diff --git a/virt/kvm/irqchip.c b/virt/kvm/irqchip.c
index 462c70621247..c3e4fbbfed94 100644
--- a/virt/kvm/irqchip.c
+++ b/virt/kvm/irqchip.c
@@ -178,11 +178,9 @@ int kvm_set_irq_routing(struct kvm *kvm,
     for (i = 0; i < nr; ++i) {
         if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
             return -EINVAL;
-        nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
+        nr_rt_entries = max(nr_rt_entries + 1, ue[i].gsi);
     }

-    nr_rt_entries += 1;
-
     new = kzalloc_flex(*new, map, nr_rt_entries, GFP_KERNEL_ACCOUNT);
     if (!new)
         return -ENOMEM;
@@ -246,11 +244,11 @@ int kvm_init_irq_routing(struct kvm *kvm)
     struct kvm_irq_routing_table *new;
     int chip_size;

-    new = kzalloc_flex(*new, map, 1, GFP_KERNEL_ACCOUNT);
+    new = kzalloc_flex(*new, map, 0, GFP_KERNEL_ACCOUNT);
     if (!new)
         return -ENOMEM;

-    new->nr_rt_entries = 1;
+    new->nr_rt_entries = 0;

     chip_size = sizeof(int) * KVM_NR_IRQCHIPS * KVM_IRQCHIP_NUM_PINS;
     memset(new->chip, -1, chip_size);

Paolo