From patchwork Wed May 20 18:48:43 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 25072 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n4KIt986026875 for ; Wed, 20 May 2009 18:55:09 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755483AbZETSzA (ORCPT ); Wed, 20 May 2009 14:55:00 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1755921AbZETSzA (ORCPT ); Wed, 20 May 2009 14:55:00 -0400 Received: from mx2.redhat.com ([66.187.237.31]:50266 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755483AbZETSy6 (ORCPT ); Wed, 20 May 2009 14:54:58 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n4KIswLX009807; Wed, 20 May 2009 14:54:58 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n4KIsvFH019073; Wed, 20 May 2009 14:54:57 -0400 Received: from amt.cnet (vpn-10-28.str.redhat.com [10.32.10.28]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n4KIstUl021850; Wed, 20 May 2009 14:54:56 -0400 Received: from amt.cnet (amt.cnet [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id 38F7F274E5E; Wed, 20 May 2009 15:54:22 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.3/8.14.3/Submit) id n4KIsKHO028785; Wed, 20 May 2009 15:54:20 -0300 Message-Id: <20090520185134.623968425@localhost.localdomain> References: <20090520184841.954066003@localhost.localdomain> User-Agent: quilt/0.46-1 Date: Wed, 20 May 2009 15:48:43 -0300 From: Marcelo Tosatti To: Avi Kivity , kvm@vger.kernel.org Cc: Gregory Haskins , Marcelo Tosatti Subject: [patch 2/4] KVM: move coalesced_mmio locking to its own device Content-Disposition: inline; filename=coalesced-mmio-lock X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Get rid of kvm->lock dependency on coalesced_mmio methods. Use an atomic variable instead to guarantee only one vcpu is batching data into the ring at a given time. Signed-off-by: Marcelo Tosatti Index: kvm-irqlock/virt/kvm/coalesced_mmio.c =================================================================== --- kvm-irqlock.orig/virt/kvm/coalesced_mmio.c +++ kvm-irqlock/virt/kvm/coalesced_mmio.c @@ -26,9 +26,12 @@ static int coalesced_mmio_in_range(struc if (!is_write) return 0; - /* kvm->lock is taken by the caller and must be not released before - * dev.read/write - */ + /* + * Some other vcpu might be batching data into the ring, + * fallback to userspace. Ordering not our problem. + */ + if (!atomic_add_unless(&dev->in_use, 1, 1)) + return 0; /* Are we able to batch it ? */ @@ -41,7 +44,7 @@ static int coalesced_mmio_in_range(struc KVM_COALESCED_MMIO_MAX; if (next == dev->kvm->coalesced_mmio_ring->first) { /* full */ - return 0; + goto out_denied; } /* is it in a batchable area ? */ @@ -57,6 +60,8 @@ static int coalesced_mmio_in_range(struc addr + len <= zone->addr + zone->size) return 1; } +out_denied: + atomic_set(&dev->in_use, 0); return 0; } @@ -67,15 +72,14 @@ static void coalesced_mmio_write(struct (struct kvm_coalesced_mmio_dev*)this->private; struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring; - /* kvm->lock must be taken by caller before call to in_range()*/ - /* copy data in first free entry of the ring */ ring->coalesced_mmio[ring->last].phys_addr = addr; ring->coalesced_mmio[ring->last].len = len; memcpy(ring->coalesced_mmio[ring->last].data, val, len); - smp_wmb(); ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX; + smp_wmb(); + atomic_set(&dev->in_use, 0); } static void coalesced_mmio_destructor(struct kvm_io_device *this) @@ -90,6 +94,8 @@ int kvm_coalesced_mmio_init(struct kvm * dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); if (!dev) return -ENOMEM; + atomic_set(&dev->in_use, 0); + dev->dev.write = coalesced_mmio_write; dev->dev.in_range = coalesced_mmio_in_range; dev->dev.destructor = coalesced_mmio_destructor; Index: kvm-irqlock/virt/kvm/coalesced_mmio.h =================================================================== --- kvm-irqlock.orig/virt/kvm/coalesced_mmio.h +++ kvm-irqlock/virt/kvm/coalesced_mmio.h @@ -12,6 +12,7 @@ struct kvm_coalesced_mmio_dev { struct kvm_io_device dev; struct kvm *kvm; + atomic_t in_use; int nb_zones; struct kvm_coalesced_mmio_zone zone[KVM_COALESCED_MMIO_ZONE_MAX]; };