kvm_io_bus devices are used for ioevent, pit, pic, ioapic,
coalesced_mmio.

Currently Qemu only emulates one PCI bus, it contains 32 slots,
one slot contains 8 functions, maximum of supported PCI devices:
 1 * 32 * 8 = 256. The maximum of coalesced mmio zone is 100,
each zone has an iobus devices. 300 io_bus devices is not enough.

This patch makes the kvm_io_range array can be resized dynamically.

Changes from v1:
- fix typo: kvm_io_bus_range -> kvm_io_range

Changes from v2:
- unregister device only when it exists

Signed-off-by: Amos Kong <ak...@redhat.com>
Reviewed-by: Jason Wang <jasow...@redhat.com>
CC: Alex Williamson <alex.william...@redhat.com>
---
 include/linux/kvm_host.h |    3 +--
 virt/kvm/kvm_main.c      |   41 +++++++++++++++++++++--------------------
 2 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 355e445..0e6d9d2 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -69,8 +69,7 @@ struct kvm_io_range {
 
 struct kvm_io_bus {
        int                   dev_count;
-#define NR_IOBUS_DEVS 300
-       struct kvm_io_range range[NR_IOBUS_DEVS];
+       struct kvm_io_range range[];
 };
 
 enum kvm_bus {
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index e4431ad..1275979 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2389,9 +2389,6 @@ int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
 int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
                          gpa_t addr, int len)
 {
-       if (bus->dev_count == NR_IOBUS_DEVS)
-               return -ENOSPC;
-
        bus->range[bus->dev_count++] = (struct kvm_io_range) {
                .addr = addr,
                .len = len,
@@ -2491,10 +2488,12 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum 
kvm_bus bus_idx, gpa_t addr,
        struct kvm_io_bus *new_bus, *bus;
 
        bus = kvm->buses[bus_idx];
-       if (bus->dev_count > NR_IOBUS_DEVS-1)
-               return -ENOSPC;
 
-       new_bus = kmemdup(bus, sizeof(struct kvm_io_bus), GFP_KERNEL);
+       new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count + 1) *
+                         sizeof(struct kvm_io_range)), GFP_KERNEL);
+       if (new_bus)
+               memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
+                      sizeof(struct kvm_io_range)));
        if (!new_bus)
                return -ENOMEM;
        kvm_io_bus_insert_dev(new_bus, dev, addr, len);
@@ -2513,26 +2512,28 @@ int kvm_io_bus_unregister_dev(struct kvm *kvm, enum 
kvm_bus bus_idx,
        struct kvm_io_bus *new_bus, *bus;
 
        bus = kvm->buses[bus_idx];
-
-       new_bus = kmemdup(bus, sizeof(*bus), GFP_KERNEL);
-       if (!new_bus)
-               return -ENOMEM;
-
        r = -ENOENT;
-       for (i = 0; i < new_bus->dev_count; i++)
-               if (new_bus->range[i].dev == dev) {
+       for (i = 0; i < bus->dev_count; i++)
+               if (bus->range[i].dev == dev) {
                        r = 0;
-                       new_bus->dev_count--;
-                       new_bus->range[i] = new_bus->range[new_bus->dev_count];
-                       sort(new_bus->range, new_bus->dev_count,
-                            sizeof(struct kvm_io_range),
-                            kvm_io_bus_sort_cmp, NULL);
                        break;
                }
 
-       if (r) {
-               kfree(new_bus);
+       if (r)
                return r;
+
+       new_bus = kmemdup(bus, sizeof(*bus) + ((bus->dev_count - 1) *
+                         sizeof(struct kvm_io_range)), GFP_KERNEL);
+       if (!new_bus)
+               return -ENOMEM;
+
+       new_bus->dev_count--;
+       /* copy last entry of bus->range to deleted entry spot if
+          deleted entry isn't the last entry of bus->range */
+       if (i != bus->dev_count - 1) {
+               new_bus->range[i] = bus->range[bus->dev_count - 1];
+               sort(new_bus->range, new_bus->dev_count,
+                    sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp, NULL);
        }
 
        rcu_assign_pointer(kvm->buses[bus_idx], new_bus);

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to