Skip to content

Commit 168e82f

Browse files
sean-jcgregkh
authored andcommitted
KVM: Stop looking for coalesced MMIO zones if the bus is destroyed
commit 5d3c4c7 upstream. Abort the walk of coalesced MMIO zones if kvm_io_bus_unregister_dev() fails to allocate memory for the new instance of the bus. If it can't instantiate a new bus, unregister_dev() destroys all devices _except_ the target device. But, it doesn't tell the caller that it obliterated the bus and invoked the destructor for all devices that were on the bus. In the coalesced MMIO case, this can result in a deleted list entry dereference due to attempting to continue iterating on coalesced_zones after future entries (in the walk) have been deleted. Opportunistically add curly braces to the for-loop, which encompasses many lines but sneaks by without braces due to the guts being a single if statement. Fixes: f658866 ("KVM: fix memory leak in kvm_io_bus_unregister_dev()") Cc: stable@vger.kernel.org Reported-by: Hao Sun <sunhao.th@gmail.com> Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20210412222050.876100-3-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4e899ca commit 168e82f

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

‎include/linux/kvm_host.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
191191
int len, void *val);
192192
int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
193193
int len, struct kvm_io_device *dev);
194-
void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
195-
struct kvm_io_device *dev);
194+
int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
195+
struct kvm_io_device *dev);
196196
struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
197197
gpa_t addr);
198198

‎virt/kvm/coalesced_mmio.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,36 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
174174
struct kvm_coalesced_mmio_zone *zone)
175175
{
176176
struct kvm_coalesced_mmio_dev *dev, *tmp;
177+
int r;
177178

178179
if (zone->pio != 1 && zone->pio != 0)
179180
return -EINVAL;
180181

181182
mutex_lock(&kvm->slots_lock);
182183

183-
list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list)
184+
list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list) {
184185
if (zone->pio == dev->zone.pio &&
185186
coalesced_mmio_in_range(dev, zone->addr, zone->size)) {
186-
kvm_io_bus_unregister_dev(kvm,
187+
r = kvm_io_bus_unregister_dev(kvm,
187188
zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS, &dev->dev);
188189
kvm_iodevice_destructor(&dev->dev);
190+
191+
/*
192+
* On failure, unregister destroys all devices on the
193+
* bus _except_ the target device, i.e. coalesced_zones
194+
* has been modified. No need to restart the walk as
195+
* there aren't any zones left.
196+
*/
197+
if (r)
198+
break;
189199
}
200+
}
190201

191202
mutex_unlock(&kvm->slots_lock);
192203

204+
/*
205+
* Ignore the result of kvm_io_bus_unregister_dev(), from userspace's
206+
* perspective, the coalesced MMIO is most definitely unregistered.
207+
*/
193208
return 0;
194209
}

‎virt/kvm/kvm_main.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4462,23 +4462,23 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
44624462
}
44634463

44644464
/* Caller must hold slots_lock. */
4465-
void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
4466-
struct kvm_io_device *dev)
4465+
int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
4466+
struct kvm_io_device *dev)
44674467
{
44684468
int i, j;
44694469
struct kvm_io_bus *new_bus, *bus;
44704470

44714471
bus = kvm_get_bus(kvm, bus_idx);
44724472
if (!bus)
4473-
return;
4473+
return 0;
44744474

44754475
for (i = 0; i < bus->dev_count; i++)
44764476
if (bus->range[i].dev == dev) {
44774477
break;
44784478
}
44794479

44804480
if (i == bus->dev_count)
4481-
return;
4481+
return 0;
44824482

44834483
new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1),
44844484
GFP_KERNEL_ACCOUNT);
@@ -4503,7 +4503,7 @@ void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
45034503
}
45044504

45054505
kfree(bus);
4506-
return;
4506+
return new_bus ? 0 : -ENOMEM;
45074507
}
45084508

45094509
struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,

0 commit comments

Comments
 (0)