]> www.infradead.org Git - linux.git/commitdiff
KVM: Setup empty IRQ routing when creating a VM
authorYi Wang <foxywang@tencent.com>
Mon, 6 May 2024 10:17:49 +0000 (18:17 +0800)
committerSean Christopherson <seanjc@google.com>
Tue, 11 Jun 2024 21:18:34 +0000 (14:18 -0700)
Setup empty IRQ routing during VM creation so that x86 and s390 don't need
to set empty/dummy IRQ routing during KVM_CREATE_IRQCHIP (in future
patches).  Initializing IRQ routing before there are any potential readers
allows KVM to avoid the synchronize_srcu() in kvm_set_irq_routing(), which
can introduces 20+ milliseconds of latency in the VM creation path.

Ensuring that all VMs have non-NULL IRQ routing also hardens KVM against
misbehaving userspace VMMs, e.g. RISC-V dynamically instantiates its
interrupt controller, but doesn't override kvm_arch_intc_initialized() or
kvm_arch_irqfd_allowed(), and so can likely reach kvm_irq_map_gsi()
without fully initialized IRQ routing.

Signed-off-by: Yi Wang <foxywang@tencent.com>
Acked-by: Christian Borntraeger <borntraeger@linux.ibm.com>
Link: https://lore.kernel.org/r/20240506101751.3145407-2-foxywang@tencent.com
[sean: init refcount after IRQ routing, fix stub, massage changelog]
Signed-off-by: Sean Christopherson <seanjc@google.com>
include/linux/kvm_host.h
virt/kvm/irqchip.c
virt/kvm/kvm_main.c

index c80fe03a1fa4c290d1ea99a363321c1b3f355695..eaa70e9b1218ba3182f73d55b96462426c036189 100644 (file)
@@ -2094,6 +2094,7 @@ int kvm_set_irq_routing(struct kvm *kvm,
                        const struct kvm_irq_routing_entry *entries,
                        unsigned nr,
                        unsigned flags);
+int kvm_init_irq_routing(struct kvm *kvm);
 int kvm_set_routing_entry(struct kvm *kvm,
                          struct kvm_kernel_irq_routing_entry *e,
                          const struct kvm_irq_routing_entry *ue);
@@ -2103,6 +2104,11 @@ void kvm_free_irq_routing(struct kvm *kvm);
 
 static inline void kvm_free_irq_routing(struct kvm *kvm) {}
 
+static inline int kvm_init_irq_routing(struct kvm *kvm)
+{
+       return 0;
+}
+
 #endif
 
 int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);
index 1e567d1f6d3dfc4f911dba543ae4f3ea603d4d9c..162d8ed889f215b0735ea90a53abff7d40bcfb8a 100644 (file)
@@ -237,3 +237,27 @@ out:
 
        return r;
 }
+
+/*
+ * Allocate empty IRQ routing by default so that additional setup isn't needed
+ * when userspace-driven IRQ routing is activated, and so that kvm->irq_routing
+ * is guaranteed to be non-NULL.
+ */
+int kvm_init_irq_routing(struct kvm *kvm)
+{
+       struct kvm_irq_routing_table *new;
+       int chip_size;
+
+       new = kzalloc(struct_size(new, map, 1), GFP_KERNEL_ACCOUNT);
+       if (!new)
+               return -ENOMEM;
+
+       new->nr_rt_entries = 1;
+
+       chip_size = sizeof(int) * KVM_NR_IRQCHIPS * KVM_IRQCHIP_NUM_PINS;
+       memset(new->chip, -1, chip_size);
+
+       RCU_INIT_POINTER(kvm->irq_routing, new);
+
+       return 0;
+}
index f4f5a435b2250f9f7d301857613020686fdea77f..7bf1ef0fc10b7cacf7f9d59dca11f3c5732d62ef 100644 (file)
@@ -1186,7 +1186,12 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
        if (init_srcu_struct(&kvm->irq_srcu))
                goto out_err_no_irq_srcu;
 
+       r = kvm_init_irq_routing(kvm);
+       if (r)
+               goto out_err_no_irq_routing;
+
        refcount_set(&kvm->users_count, 1);
+
        for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
                for (j = 0; j < 2; j++) {
                        slots = &kvm->__memslots[i][j];
@@ -1265,6 +1270,8 @@ out_err_no_arch_destroy_vm:
        WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
        for (i = 0; i < KVM_NR_BUSES; i++)
                kfree(kvm_get_bus(kvm, i));
+       kvm_free_irq_routing(kvm);
+out_err_no_irq_routing:
        cleanup_srcu_struct(&kvm->irq_srcu);
 out_err_no_irq_srcu:
        cleanup_srcu_struct(&kvm->srcu);