From patchwork Wed May 26 19:50:38 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 102486 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o4QJuF3M012439 for ; Wed, 26 May 2010 19:56:16 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757122Ab0EZTz3 (ORCPT ); Wed, 26 May 2010 15:55:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:7951 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756890Ab0EZTz2 (ORCPT ); Wed, 26 May 2010 15:55:28 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o4QJsrsG028137 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 26 May 2010 15:54:54 -0400 Received: from redhat.com (vpn-6-108.tlv.redhat.com [10.35.6.108]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id o4QJspg5030751; Wed, 26 May 2010 15:54:52 -0400 Date: Wed, 26 May 2010 22:50:38 +0300 From: "Michael S. Tsirkin" To: Rusty Russell , linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, qemu-devel@nongnu.org Subject: [PATCHv2-RFC 1/2] virtio: support layout with avail ring before idx Message-ID: <8a6f96c73a72f04ed7c142e631ca59ee45b4b23b.1274903330.git.mst@redhat.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mutt-Fcc: =sent User-Agent: Mutt/1.5.19 (2009-01-05) X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 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.3 (demeter.kernel.org [140.211.167.41]); Wed, 26 May 2010 19:56:16 +0000 (UTC) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 1ca8890..2241342 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -224,7 +224,7 @@ add_head: /* Put entry in available array (but don't update avail->idx until they * do sync). FIXME: avoid modulus here? */ avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num; - vq->vring.avail->ring[avail] = head; + vq->vring.avail_ring[avail] = head; pr_debug("Added buffer head %i to %p\n", head, vq); END_USE(vq); @@ -425,7 +425,7 @@ struct virtqueue *vring_new_virtqueue(unsigned int num, if (!vq) return NULL; - vring_init(&vq->vring, num, pages, vring_align); + vring_init(&vq->vring, num, pages, vring_align, false); vq->vq.callback = callback; vq->vq.vdev = vdev; vq->vq.name = name; diff --git a/include/linux/virtio_ring.h b/include/linux/virtio_ring.h index e4d144b..c5f3ee7 100644 --- a/include/linux/virtio_ring.h +++ b/include/linux/virtio_ring.h @@ -47,6 +47,11 @@ struct vring_avail { __u16 ring[]; }; +struct vring_avail_ctrl { + __u16 flags; + __u16 idx; +}; + /* u32 is used here for ids for padding reasons. */ struct vring_used_elem { /* Index of start of used descriptor chain. */ @@ -66,7 +71,9 @@ struct vring { struct vring_desc *desc; - struct vring_avail *avail; + struct vring_avail_ctrl *avail; + + __u16 *avail_ring; struct vring_used *used; }; @@ -79,11 +86,18 @@ struct vring { * // The actual descriptors (16 bytes each) * struct vring_desc desc[num]; * - * // A ring of available descriptor heads with free-running index. + * // A ring of available descriptor heads with a control structure + * // including a free-running index. + * // The ring can come either after (legacy) or before the control. * __u16 avail_flags; * __u16 avail_idx; * __u16 available[num]; * + * or + * + * __u16 available[num]; + * __u16 avail_flags; + * __u16 avail_idx; * // Padding to the next align boundary. * char pad[]; * @@ -94,13 +108,17 @@ struct vring { * }; */ static inline void vring_init(struct vring *vr, unsigned int num, void *p, - unsigned long align) + unsigned long align, bool avail_ring_first) { + struct vring_avail *avail = p + num * sizeof(struct vring_desc); vr->num = num; vr->desc = p; - vr->avail = p + num*sizeof(struct vring_desc); - vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + align-1) - & ~(align - 1)); + vr->avail_ring = avail_ring_first ? (void*)avail : &avail->ring; + vr->avail = avail_ring_first ? (void *)&vr->avail_ring[num] : p; + vr->used = (void *)ALIGN((unsigned long)&avail->ring[num], align); + /* Verify that avail fits before used. */ + BUG_ON((unsigned long)(vr->avail + 1) > (unsigned long)vr->used); + BUG_ON((unsigned long)(&vr->avail_ring[num]) > (unsigned long)vr->used); } static inline unsigned vring_size(unsigned int num, unsigned long align)