From patchwork Wed May 4 20:51:47 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 755332 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p44Kv0e6000472 for ; Wed, 4 May 2011 20:57:01 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755914Ab1EDU4b (ORCPT ); Wed, 4 May 2011 16:56:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37117 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755827Ab1EDUwP (ORCPT ); Wed, 4 May 2011 16:52:15 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p44Kq5Je028451 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 4 May 2011 16:52:05 -0400 Received: from redhat.com (vpn-201-69.tlv.redhat.com [10.35.201.69]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id p44Kpv7i002640; Wed, 4 May 2011 16:51:58 -0400 Date: Wed, 4 May 2011 23:51:47 +0300 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: Rusty Russell , Carsten Otte , Christian Borntraeger , linux390@de.ibm.com, Martin Schwidefsky , Heiko Carstens , Shirley Ma , lguest@lists.ozlabs.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-s390@vger.kernel.org, kvm@vger.kernel.org, Krishna Kumar , Tom Lendacky , steved@us.ibm.com, habanero@linux.vnet.ibm.com Subject: [PATCH 09/18] virtio: use avail_event index Message-ID: <8bba6a0a8eee17e741c5155b04ff1b1c9f34bf94.1304541919.git.mst@redhat.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mutt-Fcc: =sent User-Agent: Mutt/1.5.21 (2010-09-15) X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 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 (demeter1.kernel.org [140.211.167.41]); Wed, 04 May 2011 20:57:01 +0000 (UTC) Use the new avail_event feature to reduce the number of exits from the guest. Signed-off-by: Michael S. Tsirkin --- drivers/virtio/virtio_ring.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 files changed, 38 insertions(+), 1 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 3a3ed75..262dfe6 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -82,6 +82,15 @@ struct vring_virtqueue /* Host supports indirect buffers */ bool indirect; + /* Host publishes avail event idx */ + bool event; + + /* Is kicked_avail below valid? */ + bool kicked_avail_valid; + + /* avail idx value we already kicked. */ + u16 kicked_avail; + /* Number of free buffers */ unsigned int num_free; /* Head of free buffer list. */ @@ -228,6 +237,12 @@ add_head: * new available array entries. */ virtio_wmb(); vq->vring.avail->idx++; + /* If the driver never bothers to kick in a very long while, + * avail index might wrap around. If that happens, invalidate + * kicked_avail index we stored. TODO: make sure all drivers + * kick at least once in 2^16 and remove this. */ + if (unlikely(vq->vring.avail->idx == vq->kicked_avail)) + vq->kicked_avail_valid = true; pr_debug("Added buffer head %i to %p\n", head, vq); END_USE(vq); @@ -236,6 +251,23 @@ add_head: } EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp); + +static bool vring_notify(struct vring_virtqueue *vq) +{ + u16 old, new; + bool v; + if (!vq->event) + return !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY); + + v = vq->kicked_avail_valid; + old = vq->kicked_avail; + new = vq->kicked_avail = vq->vring.avail->idx; + vq->kicked_avail_valid = true; + if (unlikely(!v)) + return true; + return vring_need_event(vring_avail_event(&vq->vring), new, old); +} + void virtqueue_kick(struct virtqueue *_vq) { struct vring_virtqueue *vq = to_vvq(_vq); @@ -244,7 +276,7 @@ void virtqueue_kick(struct virtqueue *_vq) /* Need to update avail index before checking if we should notify */ virtio_mb(); - if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY)) + if (vring_notify(vq)) /* Prod other side to tell it about changes. */ vq->notify(&vq->vq); @@ -437,6 +469,8 @@ struct virtqueue *vring_new_virtqueue(unsigned int num, vq->vq.name = name; vq->notify = notify; vq->broken = false; + vq->kicked_avail_valid = false; + vq->kicked_avail = 0; vq->last_used_idx = 0; list_add_tail(&vq->vq.list, &vdev->vqs); #ifdef DEBUG @@ -444,6 +478,7 @@ struct virtqueue *vring_new_virtqueue(unsigned int num, #endif vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC); + vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_AVAIL_EVENT_IDX); /* No callback? Tell other side not to bother us. */ if (!callback) @@ -482,6 +517,8 @@ void vring_transport_features(struct virtio_device *vdev) break; case VIRTIO_RING_F_USED_EVENT_IDX: break; + case VIRTIO_RING_F_AVAIL_EVENT_IDX: + break; default: /* We don't understand this bit. */ clear_bit(i, vdev->features);