From patchwork Tue Jul 19 10:31:11 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 988602 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.4) with ESMTP id p6JAeOKL026270 for ; Tue, 19 Jul 2011 10:40:24 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751283Ab1GSKkV (ORCPT ); Tue, 19 Jul 2011 06:40:21 -0400 Received: from mail-wy0-f174.google.com ([74.125.82.174]:56932 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751080Ab1GSKkV (ORCPT ); Tue, 19 Jul 2011 06:40:21 -0400 Received: by wyg8 with SMTP id 8so2677175wyg.19 for ; Tue, 19 Jul 2011 03:40:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; bh=oABDTyqDfJFl+6aR1lr79SMCDTFfGvlblym8NJeGB8U=; b=dLPiWTQFgvW51lqoPVGTggH0e3Aw0eyWr/qCNB1p00lwDMuzxkXm2zs9/8UFkAZZ8u TeSqGbvWwVJ8YFULJyJTAv5fwB0bgb+B0Rls3jZrx6daTBWMH9u9dw7NGrmojwIMR//4 gM+5u3G21rk+NpBODYK4Tv9HRqAAKFIz3qryk= Received: by 10.217.7.66 with SMTP id z44mr886448wes.100.1311072019608; Tue, 19 Jul 2011 03:40:19 -0700 (PDT) Received: from localhost.localdomain (bzq-79-180-198-19.red.bezeqint.net [79.180.198.19]) by mx.google.com with ESMTPS id g55sm2937871wee.11.2011.07.19.03.40.18 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 19 Jul 2011 03:40:19 -0700 (PDT) From: Sasha Levin To: kvm@vger.kernel.org Cc: Sasha Levin , Avi Kivity , Marcelo Tosatti Subject: [PATCH v2] MMIO: Make coalesced mmio use a device per zone Date: Tue, 19 Jul 2011 13:31:11 +0300 Message-Id: <1311071471-15546-1-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.6 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Tue, 19 Jul 2011 10:40:24 +0000 (UTC) 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. Cc: Avi Kivity Cc: Marcelo Tosatti Suggested-by: Avi Kivity Signed-off-by: Sasha Levin --- include/linux/kvm_host.h | 7 ++- virt/kvm/coalesced_mmio.c | 114 ++++++++++++++++---------------------------- virt/kvm/coalesced_mmio.h | 7 +-- 3 files changed, 50 insertions(+), 78 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index eabb21a..b1d7f2d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -63,7 +63,7 @@ extern struct kmem_cache *kvm_vcpu_cache; */ struct kvm_io_bus { int dev_count; -#define NR_IOBUS_DEVS 200 +#define NR_IOBUS_DEVS 300 struct kvm_io_device *devs[NR_IOBUS_DEVS]; }; @@ -256,8 +256,11 @@ struct kvm { struct kvm_arch arch; atomic_t users_count; #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET - struct kvm_coalesced_mmio_dev *coalesced_mmio_dev; struct kvm_coalesced_mmio_ring *coalesced_mmio_ring; + struct { + spinlock_t lock; + struct list_head items; + } coalesced_zones; #endif struct mutex irq_lock; diff --git a/virt/kvm/coalesced_mmio.c b/virt/kvm/coalesced_mmio.c index ae075dc..01049a5 100644 --- a/virt/kvm/coalesced_mmio.c +++ b/virt/kvm/coalesced_mmio.c @@ -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, gpa_t addr, int len) { - struct kvm_coalesced_mmio_zone *zone; - int i; - - /* 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) - */ + /* is it in a batchable area ? + * (addr,len) is fully included in + * (zone->addr, zone->size) + */ - if (zone->addr <= addr && - addr + len <= zone->addr + zone->size) - return 1; - } - return 0; + return (dev->zone.addr <= addr && + addr + len <= dev->zone.addr + dev->zone.size); } 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, if (!coalesced_mmio_in_range(dev, addr, len)) return -EOPNOTSUPP; - spin_lock(&dev->lock); + spin_lock(&dev->kvm->coalesced_zones.lock); if (!coalesced_mmio_has_room(dev)) { - spin_unlock(&dev->lock); + spin_unlock(&dev->kvm->coalesced_zones.lock); return -EOPNOTSUPP; } @@ -87,7 +77,7 @@ static int coalesced_mmio_write(struct kvm_io_device *this, memcpy(ring->coalesced_mmio[ring->last].data, val, len); smp_wmb(); ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX; - spin_unlock(&dev->lock); + spin_unlock(&dev->kvm->coalesced_zones.lock); return 0; } @@ -95,6 +85,8 @@ static void coalesced_mmio_destructor(struct kvm_io_device *this) { struct kvm_coalesced_mmio_dev *dev = to_mmio(this); + list_del(&dev->list); + kfree(dev); } @@ -105,7 +97,6 @@ static const struct kvm_io_device_ops coalesced_mmio_ops = { int kvm_coalesced_mmio_init(struct kvm *kvm) { - struct kvm_coalesced_mmio_dev *dev; struct page *page; int ret; @@ -113,31 +104,13 @@ int kvm_coalesced_mmio_init(struct kvm *kvm) page = alloc_page(GFP_KERNEL | __GFP_ZERO); if (!page) goto out_err; - kvm->coalesced_mmio_ring = page_address(page); - ret = -ENOMEM; - dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); - 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; + ret = 0; + kvm->coalesced_mmio_ring = page_address(page); - 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; + spin_lock_init(&kvm->coalesced_zones.lock); + INIT_LIST_HEAD(&kvm->coalesced_zones.items); - return ret; - -out_free_dev: - kvm->coalesced_mmio_dev = NULL; - kfree(dev); -out_free_page: - kvm->coalesced_mmio_ring = NULL; - __free_page(page); out_err: return ret; } @@ -151,51 +124,48 @@ void kvm_coalesced_mmio_free(struct kvm *kvm) int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm, 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) - return -ENXIO; + dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); + if (!dev) + return -ENOMEM; + + kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops); + dev->kvm = kvm; + dev->zone = *zone; mutex_lock(&kvm->slots_lock); - if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) { - mutex_unlock(&kvm->slots_lock); - return -ENOBUFS; - } + ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev); + mutex_unlock(&kvm->slots_lock); + if (ret < 0) + goto out_free_dev; - dev->zone[dev->nb_zones] = *zone; - dev->nb_zones++; + list_add_tail(&dev->list, &kvm->coalesced_zones.items); + + return ret; + +out_free_dev: + kfree(dev); + + if (dev == NULL) + return -ENXIO; - mutex_unlock(&kvm->slots_lock); return 0; } int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm, struct kvm_coalesced_mmio_zone *zone) { - int i; - struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev; - struct kvm_coalesced_mmio_zone *z; - - if (dev == NULL) - return -ENXIO; + struct kvm_coalesced_mmio_dev *dev; mutex_lock(&kvm->slots_lock); - i = dev->nb_zones; - while (i) { - z = &dev->zone[i - 1]; - - /* 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]; + list_for_each_entry(dev, &kvm->coalesced_zones.items, list) + if (coalesced_mmio_in_range(dev, zone->addr, zone->size)) { + kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &dev->dev); + kvm_iodevice_destructor(&dev->dev); } - i--; - } mutex_unlock(&kvm->slots_lock); diff --git a/virt/kvm/coalesced_mmio.h b/virt/kvm/coalesced_mmio.h index 8a5959e..b280c20 100644 --- a/virt/kvm/coalesced_mmio.h +++ b/virt/kvm/coalesced_mmio.h @@ -12,14 +12,13 @@ #ifdef CONFIG_KVM_MMIO -#define KVM_COALESCED_MMIO_ZONE_MAX 100 +#include struct kvm_coalesced_mmio_dev { + struct list_head list; struct kvm_io_device dev; struct kvm *kvm; - spinlock_t lock; - int nb_zones; - struct kvm_coalesced_mmio_zone zone[KVM_COALESCED_MMIO_ZONE_MAX]; + struct kvm_coalesced_mmio_zone zone; }; int kvm_coalesced_mmio_init(struct kvm *kvm);