Skip to content

Commit fbe4a7e

Browse files
up2wingsean-jc
authored andcommitted
KVM: Setup empty IRQ routing when creating a VM
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>
1 parent f2362c0 commit fbe4a7e

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

include/linux/kvm_host.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,6 +2094,7 @@ int kvm_set_irq_routing(struct kvm *kvm,
20942094
const struct kvm_irq_routing_entry *entries,
20952095
unsigned nr,
20962096
unsigned flags);
2097+
int kvm_init_irq_routing(struct kvm *kvm);
20972098
int kvm_set_routing_entry(struct kvm *kvm,
20982099
struct kvm_kernel_irq_routing_entry *e,
20992100
const struct kvm_irq_routing_entry *ue);
@@ -2103,6 +2104,11 @@ void kvm_free_irq_routing(struct kvm *kvm);
21032104

21042105
static inline void kvm_free_irq_routing(struct kvm *kvm) {}
21052106

2107+
static inline int kvm_init_irq_routing(struct kvm *kvm)
2108+
{
2109+
return 0;
2110+
}
2111+
21062112
#endif
21072113

21082114
int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);

virt/kvm/irqchip.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,27 @@ int kvm_set_irq_routing(struct kvm *kvm,
237237

238238
return r;
239239
}
240+
241+
/*
242+
* Allocate empty IRQ routing by default so that additional setup isn't needed
243+
* when userspace-driven IRQ routing is activated, and so that kvm->irq_routing
244+
* is guaranteed to be non-NULL.
245+
*/
246+
int kvm_init_irq_routing(struct kvm *kvm)
247+
{
248+
struct kvm_irq_routing_table *new;
249+
int chip_size;
250+
251+
new = kzalloc(struct_size(new, map, 1), GFP_KERNEL_ACCOUNT);
252+
if (!new)
253+
return -ENOMEM;
254+
255+
new->nr_rt_entries = 1;
256+
257+
chip_size = sizeof(int) * KVM_NR_IRQCHIPS * KVM_IRQCHIP_NUM_PINS;
258+
memset(new->chip, -1, chip_size);
259+
260+
RCU_INIT_POINTER(kvm->irq_routing, new);
261+
262+
return 0;
263+
}

virt/kvm/kvm_main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,12 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
11861186
if (init_srcu_struct(&kvm->irq_srcu))
11871187
goto out_err_no_irq_srcu;
11881188

1189+
r = kvm_init_irq_routing(kvm);
1190+
if (r)
1191+
goto out_err_no_irq_routing;
1192+
11891193
refcount_set(&kvm->users_count, 1);
1194+
11901195
for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
11911196
for (j = 0; j < 2; j++) {
11921197
slots = &kvm->__memslots[i][j];
@@ -1265,6 +1270,8 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
12651270
WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
12661271
for (i = 0; i < KVM_NR_BUSES; i++)
12671272
kfree(kvm_get_bus(kvm, i));
1273+
kvm_free_irq_routing(kvm);
1274+
out_err_no_irq_routing:
12681275
cleanup_srcu_struct(&kvm->irq_srcu);
12691276
out_err_no_irq_srcu:
12701277
cleanup_srcu_struct(&kvm->srcu);

0 commit comments

Comments
 (0)