FROMLIST: KVM: Avoid synchronize_srcu() in kvm_io_bus_register_dev()

Device MMIO registration may happen quite frequently during VM boot,
and the SRCU synchronization each time has a measurable effect
on VM startup time. In our experiments it can account for around 25%
of a VM's startup time.

Replace the synchronization with a deferred free of the old kvm_io_bus
structure.

Bug: 395485007
Bug: 357781595
Link: https://lore.kernel.org/all/20250624092256.1105524-4-keirf@google.com/

Change-Id: I52af8db54259f4423e3dcdfa298573944796d88d
Signed-off-by: Keir Fraser <keirf@google.com>
This commit is contained in:
Keir Fraser
2025-06-30 09:39:33 +00:00
parent 4be05c6524
commit da662aecc8
2 changed files with 9 additions and 2 deletions

View File

@@ -211,6 +211,7 @@ struct kvm_io_range {
struct kvm_io_bus {
int dev_count;
int ioeventfd_count;
struct rcu_head rcu;
struct kvm_io_range range[];
};

View File

@@ -5985,6 +5985,13 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
return r < 0 ? r : 0;
}
static void __free_bus(struct rcu_head *rcu)
{
struct kvm_io_bus *bus = container_of(rcu, struct kvm_io_bus, rcu);
kfree(bus);
}
int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
int len, struct kvm_io_device *dev)
{
@@ -6023,8 +6030,7 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
memcpy(new_bus->range + i + 1, bus->range + i,
(bus->dev_count - i) * sizeof(struct kvm_io_range));
rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
synchronize_srcu_expedited(&kvm->srcu);
kfree(bus);
call_srcu(&kvm->srcu, &bus->rcu, __free_bus);
return 0;
}