mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-28 13:22:57 +00:00
KVM: destruct kvm_io_device while unregistering it from kvm_io_bus
Current usage of kvm_io_device requires users to destruct it with an extra call of kvm_iodevice_destructor after the device gets unregistered from kvm_io_bus. This is not necessary and can cause errors if a user forgot to make the extra call. Simplify the usage by combining kvm_iodevice_destructor into kvm_io_bus_unregister_dev. This reduces LOCs a bit for users and can avoid the leakage of destructing the device explicitly. Signed-off-by: Wei Wang <wei.w.wang@intel.com> Reviewed-by: Sean Christopherson <seanjc@google.com> Link: https://lore.kernel.org/r/20230207123713.3905-2-wei.w.wang@intel.com Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
parent
5f643e460a
commit
5ea5ca3c2b
4 changed files with 17 additions and 22 deletions
|
@ -55,10 +55,4 @@ static inline int kvm_iodevice_write(struct kvm_vcpu *vcpu,
|
||||||
: -EOPNOTSUPP;
|
: -EOPNOTSUPP;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
|
|
||||||
{
|
|
||||||
if (dev->ops->destructor)
|
|
||||||
dev->ops->destructor(dev);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* __KVM_IODEV_H__ */
|
#endif /* __KVM_IODEV_H__ */
|
||||||
|
|
|
@ -186,15 +186,10 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
|
||||||
coalesced_mmio_in_range(dev, zone->addr, zone->size)) {
|
coalesced_mmio_in_range(dev, zone->addr, zone->size)) {
|
||||||
r = kvm_io_bus_unregister_dev(kvm,
|
r = kvm_io_bus_unregister_dev(kvm,
|
||||||
zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS, &dev->dev);
|
zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS, &dev->dev);
|
||||||
|
|
||||||
kvm_iodevice_destructor(&dev->dev);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* On failure, unregister destroys all devices on the
|
* On failure, unregister destroys all devices on the
|
||||||
* bus _except_ the target device, i.e. coalesced_zones
|
* bus, including the target device. There's no need
|
||||||
* has been modified. Bail after destroying the target
|
* to restart the walk as there aren't any zones left.
|
||||||
* device, there's no need to restart the walk as there
|
|
||||||
* aren't any zones left.
|
|
||||||
*/
|
*/
|
||||||
if (r)
|
if (r)
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -931,7 +931,6 @@ kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx,
|
||||||
bus = kvm_get_bus(kvm, bus_idx);
|
bus = kvm_get_bus(kvm, bus_idx);
|
||||||
if (bus)
|
if (bus)
|
||||||
bus->ioeventfd_count--;
|
bus->ioeventfd_count--;
|
||||||
ioeventfd_release(p);
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5297,6 +5297,12 @@ static void hardware_disable_all(void)
|
||||||
}
|
}
|
||||||
#endif /* CONFIG_KVM_GENERIC_HARDWARE_ENABLING */
|
#endif /* CONFIG_KVM_GENERIC_HARDWARE_ENABLING */
|
||||||
|
|
||||||
|
static void kvm_iodevice_destructor(struct kvm_io_device *dev)
|
||||||
|
{
|
||||||
|
if (dev->ops->destructor)
|
||||||
|
dev->ops->destructor(dev);
|
||||||
|
}
|
||||||
|
|
||||||
static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
|
static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -5520,7 +5526,7 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
|
||||||
int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
|
int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
|
||||||
struct kvm_io_device *dev)
|
struct kvm_io_device *dev)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i;
|
||||||
struct kvm_io_bus *new_bus, *bus;
|
struct kvm_io_bus *new_bus, *bus;
|
||||||
|
|
||||||
lockdep_assert_held(&kvm->slots_lock);
|
lockdep_assert_held(&kvm->slots_lock);
|
||||||
|
@ -5550,18 +5556,19 @@ int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
|
||||||
rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
|
rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
|
||||||
synchronize_srcu_expedited(&kvm->srcu);
|
synchronize_srcu_expedited(&kvm->srcu);
|
||||||
|
|
||||||
/* Destroy the old bus _after_ installing the (null) bus. */
|
/*
|
||||||
|
* If NULL bus is installed, destroy the old bus, including all the
|
||||||
|
* attached devices. Otherwise, destroy the caller's device only.
|
||||||
|
*/
|
||||||
if (!new_bus) {
|
if (!new_bus) {
|
||||||
pr_err("kvm: failed to shrink bus, removing it completely\n");
|
pr_err("kvm: failed to shrink bus, removing it completely\n");
|
||||||
for (j = 0; j < bus->dev_count; j++) {
|
kvm_io_bus_destroy(bus);
|
||||||
if (j == i)
|
return -ENOMEM;
|
||||||
continue;
|
|
||||||
kvm_iodevice_destructor(bus->range[j].dev);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kvm_iodevice_destructor(dev);
|
||||||
kfree(bus);
|
kfree(bus);
|
||||||
return new_bus ? 0 : -ENOMEM;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
|
struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
|
||||||
|
|
Loading…
Reference in a new issue