Skip to content

Commit 2a20592

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 03c6ccc commit 2a20592

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
@@ -190,8 +190,8 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
190190
int len, void *val);
191191
int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
192192
int len, struct kvm_io_device *dev);
193-
void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
194-
struct kvm_io_device *dev);
193+
int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
194+
struct kvm_io_device *dev);
195195
struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
196196
gpa_t addr);
197197

‎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
@@ -4342,23 +4342,23 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
43424342
}
43434343

43444344
/* Caller must hold slots_lock. */
4345-
void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
4346-
struct kvm_io_device *dev)
4345+
int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
4346+
struct kvm_io_device *dev)
43474347
{
43484348
int i, j;
43494349
struct kvm_io_bus *new_bus, *bus;
43504350

43514351
bus = kvm_get_bus(kvm, bus_idx);
43524352
if (!bus)
4353-
return;
4353+
return 0;
43544354

43554355
for (i = 0; i < bus->dev_count; i++)
43564356
if (bus->range[i].dev == dev) {
43574357
break;
43584358
}
43594359

43604360
if (i == bus->dev_count)
4361-
return;
4361+
return 0;
43624362

43634363
new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1),
43644364
GFP_KERNEL_ACCOUNT);
@@ -4383,7 +4383,7 @@ void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
43834383
}
43844384

43854385
kfree(bus);
4386-
return;
4386+
return new_bus ? 0 : -ENOMEM;
43874387
}
43884388

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

0 commit comments

Comments
 (0)