Skip to content

Commit cc22988

Browse files
mstsirkindavem330
authored andcommitted
virtio: support unlocked queue poll
This adds a way to check ring empty state after enable_cb outside any locks. Will be used by virtio_net. Note: there's room for more optimization: caller is likely to have a memory barrier already, which means we might be able to get rid of a barrier here. Deferring this optimization until we do some benchmarking. Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 01276ed commit cc22988

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

drivers/virtio/virtio_ring.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -607,19 +607,21 @@ void virtqueue_disable_cb(struct virtqueue *_vq)
607607
EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
608608

609609
/**
610-
* virtqueue_enable_cb - restart callbacks after disable_cb.
610+
* virtqueue_enable_cb_prepare - restart callbacks after disable_cb
611611
* @vq: the struct virtqueue we're talking about.
612612
*
613-
* This re-enables callbacks; it returns "false" if there are pending
614-
* buffers in the queue, to detect a possible race between the driver
615-
* checking for more work, and enabling callbacks.
613+
* This re-enables callbacks; it returns current queue state
614+
* in an opaque unsigned value. This value should be later tested by
615+
* virtqueue_poll, to detect a possible race between the driver checking for
616+
* more work, and enabling callbacks.
616617
*
617618
* Caller must ensure we don't call this with other virtqueue
618619
* operations at the same time (except where noted).
619620
*/
620-
bool virtqueue_enable_cb(struct virtqueue *_vq)
621+
unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
621622
{
622623
struct vring_virtqueue *vq = to_vvq(_vq);
624+
u16 last_used_idx;
623625

624626
START_USE(vq);
625627

@@ -629,15 +631,45 @@ bool virtqueue_enable_cb(struct virtqueue *_vq)
629631
* either clear the flags bit or point the event index at the next
630632
* entry. Always do both to keep code simple. */
631633
vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
632-
vring_used_event(&vq->vring) = vq->last_used_idx;
634+
vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
635+
END_USE(vq);
636+
return last_used_idx;
637+
}
638+
EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
639+
640+
/**
641+
* virtqueue_poll - query pending used buffers
642+
* @vq: the struct virtqueue we're talking about.
643+
* @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
644+
*
645+
* Returns "true" if there are pending used buffers in the queue.
646+
*
647+
* This does not need to be serialized.
648+
*/
649+
bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
650+
{
651+
struct vring_virtqueue *vq = to_vvq(_vq);
652+
633653
virtio_mb(vq->weak_barriers);
634-
if (unlikely(more_used(vq))) {
635-
END_USE(vq);
636-
return false;
637-
}
654+
return (u16)last_used_idx != vq->vring.used->idx;
655+
}
656+
EXPORT_SYMBOL_GPL(virtqueue_poll);
638657

639-
END_USE(vq);
640-
return true;
658+
/**
659+
* virtqueue_enable_cb - restart callbacks after disable_cb.
660+
* @vq: the struct virtqueue we're talking about.
661+
*
662+
* This re-enables callbacks; it returns "false" if there are pending
663+
* buffers in the queue, to detect a possible race between the driver
664+
* checking for more work, and enabling callbacks.
665+
*
666+
* Caller must ensure we don't call this with other virtqueue
667+
* operations at the same time (except where noted).
668+
*/
669+
bool virtqueue_enable_cb(struct virtqueue *_vq)
670+
{
671+
unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
672+
return !virtqueue_poll(_vq, last_used_idx);
641673
}
642674
EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
643675

include/linux/virtio.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ void virtqueue_disable_cb(struct virtqueue *vq);
7070

7171
bool virtqueue_enable_cb(struct virtqueue *vq);
7272

73+
unsigned virtqueue_enable_cb_prepare(struct virtqueue *vq);
74+
75+
bool virtqueue_poll(struct virtqueue *vq, unsigned);
76+
7377
bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
7478

7579
void *virtqueue_detach_unused_buf(struct virtqueue *vq);

0 commit comments

Comments
 (0)