Commit 2b3c246a authored by Sasha Levin's avatar Sasha Levin Committed by Avi Kivity

KVM: Make coalesced mmio use a device per zone

This patch changes coalesced mmio to create one mmio device per
zone instead of handling all zones in one device.

Doing so enables us to take advantage of existing locking and prevents
a race condition between coalesced mmio registration/unregistration
and lookups.
Suggested-by: default avatarAvi Kivity <avi@redhat.com>
Signed-off-by: default avatarSasha Levin <levinsasha928@gmail.com>
Signed-off-by: default avatarMarcelo Tosatti <mtosatti@redhat.com>
parent 8c3ba334
...@@ -63,7 +63,7 @@ extern struct kmem_cache *kvm_vcpu_cache; ...@@ -63,7 +63,7 @@ extern struct kmem_cache *kvm_vcpu_cache;
*/ */
struct kvm_io_bus { struct kvm_io_bus {
int dev_count; int dev_count;
#define NR_IOBUS_DEVS 200 #define NR_IOBUS_DEVS 300
struct kvm_io_device *devs[NR_IOBUS_DEVS]; struct kvm_io_device *devs[NR_IOBUS_DEVS];
}; };
...@@ -256,8 +256,9 @@ struct kvm { ...@@ -256,8 +256,9 @@ struct kvm {
struct kvm_arch arch; struct kvm_arch arch;
atomic_t users_count; atomic_t users_count;
#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
struct kvm_coalesced_mmio_dev *coalesced_mmio_dev;
struct kvm_coalesced_mmio_ring *coalesced_mmio_ring; struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
spinlock_t ring_lock;
struct list_head coalesced_zones;
#endif #endif
struct mutex irq_lock; struct mutex irq_lock;
......
...@@ -24,23 +24,13 @@ static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev) ...@@ -24,23 +24,13 @@ static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev, static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
gpa_t addr, int len) gpa_t addr, int len)
{ {
struct kvm_coalesced_mmio_zone *zone; /* is it in a batchable area ?
int i; * (addr,len) is fully included in
* (zone->addr, zone->size)
/* is it in a batchable area ? */ */
for (i = 0; i < dev->nb_zones; i++) {
zone = &dev->zone[i];
/* (addr,len) is fully included in
* (zone->addr, zone->size)
*/
if (zone->addr <= addr && return (dev->zone.addr <= addr &&
addr + len <= zone->addr + zone->size) addr + len <= dev->zone.addr + dev->zone.size);
return 1;
}
return 0;
} }
static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev) static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev)
...@@ -73,10 +63,10 @@ static int coalesced_mmio_write(struct kvm_io_device *this, ...@@ -73,10 +63,10 @@ static int coalesced_mmio_write(struct kvm_io_device *this,
if (!coalesced_mmio_in_range(dev, addr, len)) if (!coalesced_mmio_in_range(dev, addr, len))
return -EOPNOTSUPP; return -EOPNOTSUPP;
spin_lock(&dev->lock); spin_lock(&dev->kvm->ring_lock);
if (!coalesced_mmio_has_room(dev)) { if (!coalesced_mmio_has_room(dev)) {
spin_unlock(&dev->lock); spin_unlock(&dev->kvm->ring_lock);
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
...@@ -87,7 +77,7 @@ static int coalesced_mmio_write(struct kvm_io_device *this, ...@@ -87,7 +77,7 @@ static int coalesced_mmio_write(struct kvm_io_device *this,
memcpy(ring->coalesced_mmio[ring->last].data, val, len); memcpy(ring->coalesced_mmio[ring->last].data, val, len);
smp_wmb(); smp_wmb();
ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX; ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
spin_unlock(&dev->lock); spin_unlock(&dev->kvm->ring_lock);
return 0; return 0;
} }
...@@ -95,6 +85,8 @@ static void coalesced_mmio_destructor(struct kvm_io_device *this) ...@@ -95,6 +85,8 @@ static void coalesced_mmio_destructor(struct kvm_io_device *this)
{ {
struct kvm_coalesced_mmio_dev *dev = to_mmio(this); struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
list_del(&dev->list);
kfree(dev); kfree(dev);
} }
...@@ -105,7 +97,6 @@ static const struct kvm_io_device_ops coalesced_mmio_ops = { ...@@ -105,7 +97,6 @@ static const struct kvm_io_device_ops coalesced_mmio_ops = {
int kvm_coalesced_mmio_init(struct kvm *kvm) int kvm_coalesced_mmio_init(struct kvm *kvm)
{ {
struct kvm_coalesced_mmio_dev *dev;
struct page *page; struct page *page;
int ret; int ret;
...@@ -113,31 +104,18 @@ int kvm_coalesced_mmio_init(struct kvm *kvm) ...@@ -113,31 +104,18 @@ int kvm_coalesced_mmio_init(struct kvm *kvm)
page = alloc_page(GFP_KERNEL | __GFP_ZERO); page = alloc_page(GFP_KERNEL | __GFP_ZERO);
if (!page) if (!page)
goto out_err; goto out_err;
kvm->coalesced_mmio_ring = page_address(page);
ret = -ENOMEM; ret = 0;
dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); kvm->coalesced_mmio_ring = page_address(page);
if (!dev)
goto out_free_page;
spin_lock_init(&dev->lock);
kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
dev->kvm = kvm;
kvm->coalesced_mmio_dev = dev;
mutex_lock(&kvm->slots_lock);
ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev);
mutex_unlock(&kvm->slots_lock);
if (ret < 0)
goto out_free_dev;
return ret; /*
* We're using this spinlock to sync access to the coalesced ring.
* The list doesn't need it's own lock since device registration and
* unregistration should only happen when kvm->slots_lock is held.
*/
spin_lock_init(&kvm->ring_lock);
INIT_LIST_HEAD(&kvm->coalesced_zones);
out_free_dev:
kvm->coalesced_mmio_dev = NULL;
kfree(dev);
out_free_page:
kvm->coalesced_mmio_ring = NULL;
__free_page(page);
out_err: out_err:
return ret; return ret;
} }
...@@ -151,51 +129,49 @@ void kvm_coalesced_mmio_free(struct kvm *kvm) ...@@ -151,51 +129,49 @@ void kvm_coalesced_mmio_free(struct kvm *kvm)
int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm, int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
struct kvm_coalesced_mmio_zone *zone) struct kvm_coalesced_mmio_zone *zone)
{ {
struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev; int ret;
struct kvm_coalesced_mmio_dev *dev;
if (dev == NULL) dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
return -ENXIO; if (!dev)
return -ENOMEM;
kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
dev->kvm = kvm;
dev->zone = *zone;
mutex_lock(&kvm->slots_lock); mutex_lock(&kvm->slots_lock);
if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) { ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev);
mutex_unlock(&kvm->slots_lock); if (ret < 0)
return -ENOBUFS; goto out_free_dev;
} list_add_tail(&dev->list, &kvm->coalesced_zones);
mutex_unlock(&kvm->slots_lock);
dev->zone[dev->nb_zones] = *zone; return ret;
dev->nb_zones++;
out_free_dev:
mutex_unlock(&kvm->slots_lock); mutex_unlock(&kvm->slots_lock);
kfree(dev);
if (dev == NULL)
return -ENXIO;
return 0; return 0;
} }
int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm, int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
struct kvm_coalesced_mmio_zone *zone) struct kvm_coalesced_mmio_zone *zone)
{ {
int i; struct kvm_coalesced_mmio_dev *dev, *tmp;
struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
struct kvm_coalesced_mmio_zone *z;
if (dev == NULL)
return -ENXIO;
mutex_lock(&kvm->slots_lock); mutex_lock(&kvm->slots_lock);
i = dev->nb_zones; list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list)
while (i) { if (coalesced_mmio_in_range(dev, zone->addr, zone->size)) {
z = &dev->zone[i - 1]; kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &dev->dev);
kvm_iodevice_destructor(&dev->dev);
/* unregister all zones
* included in (zone->addr, zone->size)
*/
if (zone->addr <= z->addr &&
z->addr + z->size <= zone->addr + zone->size) {
dev->nb_zones--;
*z = dev->zone[dev->nb_zones];
} }
i--;
}
mutex_unlock(&kvm->slots_lock); mutex_unlock(&kvm->slots_lock);
......
...@@ -12,14 +12,13 @@ ...@@ -12,14 +12,13 @@
#ifdef CONFIG_KVM_MMIO #ifdef CONFIG_KVM_MMIO
#define KVM_COALESCED_MMIO_ZONE_MAX 100 #include <linux/list.h>
struct kvm_coalesced_mmio_dev { struct kvm_coalesced_mmio_dev {
struct list_head list;
struct kvm_io_device dev; struct kvm_io_device dev;
struct kvm *kvm; struct kvm *kvm;
spinlock_t lock; struct kvm_coalesced_mmio_zone zone;
int nb_zones;
struct kvm_coalesced_mmio_zone zone[KVM_COALESCED_MMIO_ZONE_MAX];
}; };
int kvm_coalesced_mmio_init(struct kvm *kvm); int kvm_coalesced_mmio_init(struct kvm *kvm);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment