Rusty Russell
2011-Nov-03 07:42 UTC
[PATCH 5 of 5] virtio: expose added descriptors immediately
A virtio driver does virtqueue_add_buf() multiple times before finally calling virtqueue_kick(); previously we only exposed the added buffers in the virtqueue_kick() call. This means we don't need a memory barrier in virtqueue_add_buf(), but it reduces concurrency as the device (ie. host) can't see the buffers until the kick. Signed-off-by: Rusty Russell <rusty at rustcorp.com.au> --- drivers/virtio/virtio_ring.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -227,9 +227,15 @@ add_head: /* Put entry in available array (but don't update avail->idx until they * do sync). */ - avail = ((vq->vring.avail->idx + vq->num_added++) & (vq->vring.num-1)); + avail = (vq->vring.avail->idx & (vq->vring.num-1)); vq->vring.avail->ring[avail] = head; + /* Descriptors and available array need to be set before we expose the + * new available array entries. */ + virtio_wmb(); + vq->vring.avail->idx++; + vq->num_added++; + pr_debug("Added buffer head %i to %p\n", head, vq); END_USE(vq); @@ -248,13 +254,10 @@ bool virtqueue_kick_prepare(struct virtq * new available array entries. */ virtio_wmb(); - old = vq->vring.avail->idx; - new = vq->vring.avail->idx = old + vq->num_added; + old = vq->vring.avail->idx - vq->num_added; + new = vq->vring.avail->idx; vq->num_added = 0; - /* Need to update avail index before checking if we should notify */ - virtio_mb(); - if (vq->event) { needs_kick = vring_need_event(vring_avail_event(&vq->vring), new, old);
Michael S. Tsirkin
2011-Nov-13 21:03 UTC
[PATCH 5 of 5] virtio: expose added descriptors immediately
On Thu, Nov 03, 2011 at 06:12:53PM +1030, Rusty Russell wrote:> A virtio driver does virtqueue_add_buf() multiple times before finally > calling virtqueue_kick(); previously we only exposed the added buffers > in the virtqueue_kick() call. This means we don't need a memory > barrier in virtqueue_add_buf(), but it reduces concurrency as the > device (ie. host) can't see the buffers until the kick. > > Signed-off-by: Rusty Russell <rusty at rustcorp.com.au>In the past I played with a patch like this, but I didn't see a performance gain either way. Do you see any gain? I'm a bit concerned that with this patch, a buggy driver that adds more than 2^16 descriptors without a kick would seem to work sometimes. Let's add WARN_ON(vq->num_added > (1 << 16))?> --- > drivers/virtio/virtio_ring.c | 37 ++++++++++++++++++++++--------------- > 1 file changed, 22 insertions(+), 15 deletions(-) > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c > --- a/drivers/virtio/virtio_ring.c > +++ b/drivers/virtio/virtio_ring.c > @@ -227,9 +227,15 @@ add_head: > > /* Put entry in available array (but don't update avail->idx until they > * do sync). */ > - avail = ((vq->vring.avail->idx + vq->num_added++) & (vq->vring.num-1)); > + avail = (vq->vring.avail->idx & (vq->vring.num-1)); > vq->vring.avail->ring[avail] = head; > > + /* Descriptors and available array need to be set before we expose the > + * new available array entries. */ > + virtio_wmb(); > + vq->vring.avail->idx++; > + vq->num_added++; > + > pr_debug("Added buffer head %i to %p\n", head, vq); > END_USE(vq); > > @@ -248,13 +254,10 @@ bool virtqueue_kick_prepare(struct virtq > * new available array entries. */ > virtio_wmb(); > > - old = vq->vring.avail->idx; > - new = vq->vring.avail->idx = old + vq->num_added; > + old = vq->vring.avail->idx - vq->num_added; > + new = vq->vring.avail->idx; > vq->num_added = 0; > > - /* Need to update avail index before checking if we should notify */ > - virtio_mb(); > - > if (vq->event) { > needs_kick = vring_need_event(vring_avail_event(&vq->vring), > new, old); > > > _______________________________________________ > Virtualization mailing list > Virtualization at lists.linux-foundation.org > https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Michael S. Tsirkin
2012-Jul-01 09:20 UTC
RFD: virtio balloon API use (was Re: [PATCH 5 of 5] virtio: expose added descriptors immediately)
On Thu, Nov 03, 2011 at 06:12:53PM +1030, Rusty Russell wrote:> A virtio driver does virtqueue_add_buf() multiple times before finally > calling virtqueue_kick(); previously we only exposed the added buffers > in the virtqueue_kick() call. This means we don't need a memory > barrier in virtqueue_add_buf(), but it reduces concurrency as the > device (ie. host) can't see the buffers until the kick. > > Signed-off-by: Rusty Russell <rusty at rustcorp.com.au>Looking at recent mm compaction patches made me look at locking in balloon closely. And I noticed the referenced patch (commit ee7cd8981e15bcb365fc762afe3fc47b8242f630 upstream) interacts strangely with virtio balloon; balloon currently does: static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq) { struct scatterlist sg; sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns); init_completion(&vb->acked); /* We should always be able to add one buffer to an empty queue. */ if (virtqueue_add_buf(vq, &sg, 1, 0, vb, GFP_KERNEL) < 0) BUG(); virtqueue_kick(vq); /* When host has read buffer, this completes via balloon_ack */ wait_for_completion(&vb->acked); } While vq callback does: static void balloon_ack(struct virtqueue *vq) { struct virtio_balloon *vb; unsigned int len; vb = virtqueue_get_buf(vq, &len); if (vb) complete(&vb->acked); } So virtqueue_get_buf might now run concurrently with virtqueue_kick. I audited both and this seems safe in practice but I think we need to either declare this legal at the API level or add locking in driver. Further, is there a guarantee that we never get spurious callbacks? We currently check ring not empty but esp for non shared MSI this might not be needed. If a spurious callback triggers, virtqueue_get_buf can run concurrently with virtqueue_add_buf which is known to be racy. Again I think this is currently safe as no spurious callbacks in practice but should we guarantee no spurious callbacks at the API level or add locking in driver? -- MST
Apparently Analagous Threads
- [PATCH 5 of 5] virtio: expose added descriptors immediately
- [PATCH] virtio: Avoid possible kernel panic if DEBUG is enabled.
- [PATCH] virtio: Avoid possible kernel panic if DEBUG is enabled.
- [PATCH 0/9] virtio: new API for addition of buffers, scatterlist changes
- [PATCH 0/9] virtio: new API for addition of buffers, scatterlist changes