dataplane: use more of the generic virtio data structures, drop hostmem
cover dataplane: use more of the generic virtio data structures, drop hostmem
Now that the memory API is thread-safe, we can use it in virtio-blk-dataplane and replace hostmem.[ch]. This series does this, and also changes the vring API to use VirtQueueElement (with an eye towards migration). With this change, virtio-blk-dataplane is also safe against memory hot-unplug.
The next step would be to replace memory_region_find with address_space_{map,unmap}, which handle dirtying of memory correctly. However these APIs are not thread-safe yet, and neither is the handling of dirty memory (Juan's patches may be a start here).
Also, the usage of iov_discard_{front,back} may cause some complication when we use address_space_{map,unmap}. We may have to change a bit the logic in virtio-blk-dataplane to switch to address_space_{map,unmap}.
If we do not want to do this intermediate step, the first three patches can be applied separately from the fourth.
Paolo Bonzini (4):
vring: create a common function to parse descriptors
vring: factor common code for error exits
dataplane: change vring API to use VirtQueueElement
dataplane: replace hostmem with memory_region_find
hw/block/dataplane/virtio-blk.c | 86 +++++------- hw/virtio/dataplane/Makefile.objs | 2 +- hw/virtio/dataplane/hostmem.c | 183 ------------------------- hw/virtio/dataplane/vring.c | 244 +++++++++++++++++++++------------- include/hw/virtio/dataplane/hostmem.h | 58 -------- include/hw/virtio/dataplane/vring.h | 9 +- 6 files changed, 193 insertions(+), 389 deletions(-) delete mode 100644 hw/virtio/dataplane/hostmem.c delete mode 100644 include/hw/virtio/dataplane/hostmem.h
Signature · 1 line
1.8.3.1
PATCH 1/4 vring: create a common function to parse descriptors
--- hw/virtio/dataplane/vring.c | 113 ++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 62 deletions(-)
hw/virtio/dataplane/vring.c +51 -62
@@ -110,6 +110,47 @@ bool vring_should_notify(VirtIODevice *vdev, Vring *vring)return vring_need_event (vring_used_event (&vring->vr), new, old); } + +static int get_desc (Vring *vring, +struct iovec iov[],struct iovec *iov_end, +unsigned int *out_num,unsigned int *in_num, +struct vring_desc *desc) +{ +unsigned *num; + +if (desc->flags &VRING_DESC_F_WRITE ) { + num = in_num; + }else { + num = out_num; + +/* If it's an output descriptor, they're all supposed +* to come before any input descriptors. */ +if (unlikely (*in_num)) { +error_report ("Descriptor has out after in" ); +return -EFAULT ; + } + } + +/* Stop for now if there are not enough iovecs available. */ + iov += *in_num + *out_num; +if (iov >= iov_end) { +return -ENOBUFS ; + } + +/* TODO handle non-contiguous memory across region boundaries */ + iov->iov_base =hostmem_lookup (&vring->hostmem, desc->addr, desc->len, + desc->flags &VRING_DESC_F_WRITE ); +if (!iov->iov_base) { +error_report ("Failed to map descriptor addr %#" PRIx64" len %u" , + (uint64_t )desc->addr, desc->len); +return -EFAULT ; + } + + iov->iov_len = desc->len; + *num +=1 ; +return 0 ; +} +/* This is stolen from linux/drivers/vhost/vhost.c. */ static int get_indirect(Vring *vring, struct iovec iov[], struct iovec *iov_end,
@@ -118,6 +159,7 @@ static int get_indirect(Vring *vring, {struct vring_desc desc;unsigned int i =0 , count, found =0 ; +int ret;/* Sanity check */ if (unlikely (indirect->len %sizeof (desc))) {
@@ -170,36 +212,10 @@ static int get_indirect(Vring *vring,return -EFAULT ; } -/* Stop for now if there are not enough iovecs available. */ -if (iov >= iov_end) { -return -ENOBUFS ; - } - - iov->iov_base =hostmem_lookup (&vring->hostmem, desc.addr, desc.len, - desc.flags &VRING_DESC_F_WRITE ); -if (!iov->iov_base) { -error_report ("Failed to map indirect descriptor" -"addr %#" PRIx64" len %u" , - (uint64_t )desc.addr, desc.len); - vring->broken = true; -return -EFAULT ; - } - iov->iov_len = desc.len; - iov++; - -/* If this is an input descriptor, increment that count. */ -if (desc.flags &VRING_DESC_F_WRITE ) { - *in_num +=1 ; - }else { -/* If it's an output descriptor, they're all supposed -* to come before any input descriptors. */ -if (unlikely (*in_num)) { -error_report ("Indirect descriptor " -"has out after in: idx %u" , i); - vring->broken = true; -return -EFAULT ; - } - *out_num +=1 ; + ret =get_desc (vring, iov, iov_end, out_num, in_num, &desc); +if (ret <0 ) { + vring->broken |= (ret == -EFAULT ); +return ret; } i = desc.next; }while (desc.flags &VRING_DESC_F_NEXT );
@@ -224,6 +240,7 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,struct vring_desc desc;unsigned int i, head, found =0 , num = vring->vr.num;uint16_t avail_idx, last_avail_idx; +int ret;/* If there was a fatal error then refuse operation */ if (vring->broken) {
@@ -294,40 +311,12 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,continue ; } -/* If there are not enough iovecs left, stop for now. The caller -* should check if there are more descs available once they have dealt -* with the current set. -*/ -if (iov >= iov_end) { -return -ENOBUFS ; + ret =get_desc (vring, iov, iov_end, out_num, in_num, &desc); +if (ret <0 ) { + vring->broken |= (ret == -EFAULT ); +return ret; } -/* TODO handle non-contiguous memory across region boundaries */ - iov->iov_base =hostmem_lookup (&vring->hostmem, desc.addr, desc.len, - desc.flags &VRING_DESC_F_WRITE ); -if (!iov->iov_base) { -error_report ("Failed to map vring desc addr %#" PRIx64" len %u" , - (uint64_t )desc.addr, desc.len); - vring->broken = true; -return -EFAULT ; - } - iov->iov_len = desc.len; - iov++; - -if (desc.flags &VRING_DESC_F_WRITE ) { -/* If this is an input descriptor, -* increment that count. */ - *in_num +=1 ; - }else { -/* If it's an output descriptor, they're all supposed -* to come before any input descriptors. */ -if (unlikely (*in_num)) { -error_report ("Descriptor has out after in: idx %d" , i); - vring->broken = true; -return -EFAULT ; - } - *out_num +=1 ; - } i = desc.next; }while (desc.flags &VRING_DESC_F_NEXT );
Signature · 1 line
1.8.3.1
PATCH 2/4 vring: factor common code for error exits
In reply to Paolo Bonzini--- hw/block/dataplane/virtio-blk.c | 1 + hw/virtio/dataplane/vring.c | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 13 deletions(-)
hw/block/dataplane/virtio-blk.c +1 -0
@@ -308,6 +308,7 @@ static void handle_notify(EventNotifier *e)if (process_request (&s->ioqueue, iov, out_num, in_num, head) <0 ) {vring_set_broken (&s->vring); + ret = -EFAULT ;break ; } iov += out_num + in_num;
hw/virtio/dataplane/vring.c +21 -13
@@ -244,7 +244,8 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,/* If there was a fatal error then refuse operation */ if (vring->broken) { -return -EFAULT ; + ret = -EFAULT ; + goto out; }/* Check it isn't doing very strange things with descriptor numbers. */
@@ -255,13 +256,14 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,if (unlikely ((uint16_t )(avail_idx - last_avail_idx) > num)) {error_report ("Guest moved used index from %u to %u" , last_avail_idx, avail_idx); - vring->broken = true; -return -EFAULT ; + ret = -EFAULT ; + goto out; }/* If there's nothing new since last we looked. */ if (avail_idx == last_avail_idx) { -return -EAGAIN ; + ret = -EAGAIN ; + goto out; }/* Only get avail ring entries after they have been exposed by guest. */
@@ -274,8 +276,8 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,/* If their number is silly, that's an error. */ if (unlikely (head >= num)) {error_report ("Guest says index %u > %u is available" , head, num); - vring->broken = true; -return -EFAULT ; + ret = -EFAULT ; + goto out; }if (vdev->guest_features & (1 <<VIRTIO_RING_F_EVENT_IDX )) {
@@ -289,14 +291,14 @@ int vring_pop(VirtIODevice *vdev, Vring *vring, do { if (unlikely(i >= num)) { error_report("Desc index is %u > %u, head = %u", i, num, head); - vring->broken = true; - return -EFAULT; + ret = -EFAULT; + goto out; } if (unlikely(++found > num)) { error_report("Loop detected: last one at %u vq size %u head %u", i, num, head); - vring->broken = true; - return -EFAULT; + ret = -EFAULT; + goto out; } desc = vring->vr.desc[i];
@@ -306,15 +308,14 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,if (desc.flags &VRING_DESC_F_INDIRECT ) {int ret =get_indirect (vring, iov, iov_end, out_num, in_num, &desc);if (ret <0 ) { -return ret; + goto out; }continue ; } ret =get_desc (vring, iov, iov_end, out_num, in_num, &desc);if (ret <0 ) { - vring->broken |= (ret == -EFAULT ); -return ret; + goto out; } i = desc.next;
@@ -323,6 +324,13 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,/* On success, increment avail index. */ vring->last_avail_idx++;return head; + +out: +assert (ret <0 ); +if (ret == -EFAULT ) { + vring->broken = true; + } +return ret; }/* After we've used one of their buffers, we tell them about it.
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 2/4] vring: factor common code for error exits
hw/virtio/dataplane/vring.c int vring_pop(VirtIODevice *vdev, Vring *vring, · line 333
Quoted 8 lines · Paolo Bonzini
return head;hw/virtio/dataplane/vring.c +7
-0+ +out: +assert (ret <0 ); +if (ret == -EFAULT ) { + vring->broken = true; + } +return ret;
If this is only the error path, can we call the
label something other than "out"?
r~
Re: [Qemu-devel] [PATCH 2/4] vring: factor common code for error exits
Il 10/10/2013 22:53, Richard Henderson ha scritto:
Quoted 12 lines · Paolo Bonzini
hw/virtio/dataplane/vring.c int vring_pop(VirtIODevice *vdev, Vring *vring, · line 333
Quoted 8 lines
return head;hw/virtio/dataplane/vring.c +7
-0+ +out: +assert (ret <0 ); +if (ret == -EFAULT ) { + vring->broken = true; + } +return ret;If this is only the error path, can we call the
label something other than "out"?
Yes. Though I think it does not matter if the function returns a zero or positive result after the next patch, so it can become a general exit label too.
Paolo
PATCH 3/4 dataplane: change vring API to use VirtQueueElement
In reply to Paolo Bonzini--- hw/block/dataplane/virtio-blk.c | 85 ++++++++++++++----------------------- hw/virtio/dataplane/vring.c | 51 +++++++++++++--------- include/hw/virtio/dataplane/vring.h | 6 +-- 3 files changed, 66 insertions(+), 76 deletions(-)
hw/block/dataplane/virtio-blk.c +33 -52
@@ -35,7 +35,7 @@ enum {typedef struct {struct iocb iocb;/* Linux AIO control block */ QEMUIOVector *inhdr;/* iovecs for virtio_blk_inhdr */ -unsigned int head;/* vring descriptor index */ +VirtQueueElement *elem;/* saved data from the virtqueue */ struct iovec *bounce_iov;/* used if guest buffers are unaligned */ QEMUIOVector *read_qiov;/* for read completion /w bounce buffer */ }VirtIOBlockRequest ;
@@ -96,7 +96,7 @@ static void complete_request(struct iocb *iocb, ssize_t ret, void *opaque) len =0 ; } -trace_virtio_blk_data_plane_complete_request (s, req->head, ret); +trace_virtio_blk_data_plane_complete_request (s, req->elem->index, ret);if (req->read_qiov) {assert (req->bounce_iov);
@@ -118,12 +118,12 @@ static void complete_request(struct iocb *iocb, ssize_t ret, void *opaque)* written to, but for virtio-blk it seems to be the number of bytes * transferred plus the status bytes. */ -vring_push (&s->vring, req->head, len +sizeof (hdr)); - +vring_push (&s->vring, req->elem, len +sizeof (hdr)); + req->elem =NULL ; s->num_reqs--; } -static void complete_request_early (VirtIOBlockDataPlane *s,unsigned int head, +static void complete_request_early (VirtIOBlockDataPlane *s,VirtQueueElement *elem,QEMUIOVector *inhdr,unsigned char status) {struct virtio_blk_inhdr hdr = {
@@ -134,26 +134,26 @@ static void complete_request_early(VirtIOBlockDataPlane *s, unsigned int head,qemu_iovec_destroy (inhdr);g_slice_free (QEMUIOVector, inhdr); -vring_push (&s->vring, head,sizeof (hdr)); +vring_push (&s->vring, elem,sizeof (hdr));notify_guest (s); }/* Get disk serial number */ static void do_get_id_cmd (VirtIOBlockDataPlane *s,struct iovec *iov,unsigned int iov_cnt, -unsigned int head,QEMUIOVector *inhdr) +VirtQueueElement *elem,QEMUIOVector *inhdr) {char id[VIRTIO_BLK_ID_BYTES ];/* Serial number not NUL-terminated when shorter than buffer */ strncpy (id, s->blk->serial ? s->blk->serial :"" ,sizeof (id));iov_from_buf (iov, iov_cnt,0 , id,sizeof (id)); -complete_request_early (s, head, inhdr,VIRTIO_BLK_S_OK ); +complete_request_early (s, elem, inhdr,VIRTIO_BLK_S_OK ); }static int do_rdwr_cmd (VirtIOBlockDataPlane *s,bool read, -struct iovec *iov,unsigned int iov_cnt, -long long offset,unsigned int head, +struct iovec *iov,unsigned iov_cnt, +long long offset,VirtQueueElement *elem,QEMUIOVector *inhdr) {struct iocb *iocb;
@@ -186,19 +186,20 @@ static int do_rdwr_cmd(VirtIOBlockDataPlane *s, bool read,/* Fill in virtio block metadata needed for completion */ VirtIOBlockRequest *req =container_of (iocb, VirtIOBlockRequest, iocb); - req->head = head; + req->elem = elem; req->inhdr = inhdr; req->bounce_iov = bounce_iov; req->read_qiov = read_qiov;return 0 ; } -static int process_request (IOQueue *ioq,struct iovec iov[], -unsigned int out_num,unsigned int in_num, -unsigned int head) +static int process_request (IOQueue *ioq,VirtQueueElement *elem) {VirtIOBlockDataPlane *s =container_of (ioq, VirtIOBlockDataPlane, ioqueue); -struct iovec *in_iov = &iov[out_num]; +struct iovec *iov = elem->out_sg; +struct iovec *in_iov = elem->in_sg; +unsigned out_num = elem->out_num; +unsigned in_num = elem->in_num;struct virtio_blk_outhdr outhdr;QEMUIOVector *inhdr;size_t in_size;
@@ -229,29 +230,29 @@ static int process_request(IOQueue *ioq, struct iovec iov[],switch (outhdr.type) {case VIRTIO_BLK_T_IN : -do_rdwr_cmd (s, true, in_iov, in_num, outhdr.sector *512 , head, inhdr); +do_rdwr_cmd (s, true, in_iov, in_num, outhdr.sector *512 , elem, inhdr);return 0 ;case VIRTIO_BLK_T_OUT : -do_rdwr_cmd (s, false, iov, out_num, outhdr.sector *512 , head, inhdr); +do_rdwr_cmd (s, false, iov, out_num, outhdr.sector *512 , elem, inhdr);return 0 ;case VIRTIO_BLK_T_SCSI_CMD :/* TODO support SCSI commands */ -complete_request_early (s, head, inhdr,VIRTIO_BLK_S_UNSUPP ); +complete_request_early (s, elem, inhdr,VIRTIO_BLK_S_UNSUPP );return 0 ;case VIRTIO_BLK_T_FLUSH :/* TODO fdsync not supported by Linux AIO, do it synchronously here! */ if (qemu_fdatasync (s->fd) <0 ) { -complete_request_early (s, head, inhdr,VIRTIO_BLK_S_IOERR ); +complete_request_early (s, elem, inhdr,VIRTIO_BLK_S_IOERR ); }else { -complete_request_early (s, head, inhdr,VIRTIO_BLK_S_OK ); +complete_request_early (s, elem, inhdr,VIRTIO_BLK_S_OK ); }return 0 ;case VIRTIO_BLK_T_GET_ID : -do_get_id_cmd (s, in_iov, in_num, head, inhdr); +do_get_id_cmd (s, in_iov, in_num, elem, inhdr);return 0 ;default :
@@ -267,29 +268,8 @@ static void handle_notify(EventNotifier *e)VirtIOBlockDataPlane *s =container_of (e, VirtIOBlockDataPlane, host_notifier); -/* There is one array of iovecs into which all new requests are extracted -* from the vring. Requests are read from the vring and the translated -* descriptors are written to the iovecs array. The iovecs do not have to -* persist across handle_notify() calls because the kernel copies the -* iovecs on io_submit(). -* -* Handling io_submit() EAGAIN may require storing the requests across -* handle_notify() calls until the kernel has sufficient resources to -* accept more I/O. This is not implemented yet. -*/ -struct iovec iovec[VRING_MAX ]; -struct iovec *end = &iovec[VRING_MAX ]; -struct iovec *iov = iovec; - -/* When a request is read from the vring, the index of the first descriptor -* (aka head) is returned so that the completed request can be pushed onto -* the vring later. -* -* The number of hypervisor read-only iovecs is out_num. The number of -* hypervisor write-only iovecs is in_num. -*/ -int head; -unsigned int out_num =0 , in_num =0 ; +VirtQueueElement *elem; +int ret;unsigned int num_queued;event_notifier_test_and_clear (&s->host_notifier);
@@ -298,30 +278,31 @@ static void handle_notify(EventNotifier *e)vring_disable_notification (s->vdev, &s->vring);for (;;) { - head =vring_pop (s->vdev, &s->vring, iov, end, &out_num, &in_num); -if (head <0 ) { + ret =vring_pop (s->vdev, &s->vring, &elem); +if (ret <0 ) { +assert (elem ==NULL );break ;/* no more requests */ } -trace_virtio_blk_data_plane_process_request (s, out_num, in_num, - head); +trace_virtio_blk_data_plane_process_request (s, elem->out_num, + elem->in_num, elem->index); -if (process_request (&s->ioqueue, iov, out_num, in_num, head) <0 ) { +if (process_request (&s->ioqueue, elem) <0 ) {vring_set_broken (&s->vring); +vring_push (&s->vring, elem,0 ); ret = -EFAULT ;break ; } - iov += out_num + in_num; } -if (likely (head == -EAGAIN )) {/* vring emptied */ +if (likely (ret == -EAGAIN )) {/* vring emptied */ /* Re-enable guest->host notifies and stop processing the vring. * But if the guest has snuck in more descriptors, keep processing. */ if (vring_enable_notification (s->vdev, &s->vring)) {break ; } - }else {/* head == -ENOBUFS or fatal error, iovecs[] is depleted */ + }else {/* ret == -ENOBUFS or fatal error, iovecs[] is depleted */ /* Since there are no iovecs[] left, stop processing for now. Do * not re-enable guest->host notifies since the I/O completion * handler knows to check for more vring descriptors anyway.
hw/virtio/dataplane/vring.c +31 -20
145 lines not on this page · raw message
include/hw/virtio/dataplane/vring.h +2 -4
@@ -54,9 +54,7 @@ void vring_teardown(Vring *vring, VirtIODevice *vdev, int n);void vring_disable_notification (VirtIODevice *vdev,Vring *vring);bool vring_enable_notification (VirtIODevice *vdev,Vring *vring);bool vring_should_notify (VirtIODevice *vdev,Vring *vring); -int vring_pop (VirtIODevice *vdev,Vring *vring, -struct iovec iov[],struct iovec *iov_end, -unsigned int *out_num,unsigned int *in_num); -void vring_push (Vring *vring,unsigned int head,int len); +int vring_pop (VirtIODevice *vdev,Vring *vring,VirtQueueElement **elem); +void vring_push (Vring *vring,VirtQueueElement *elem,int len);#endif /* VRING_H */
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 3/4] dataplane: change vring API to use VirtQueueElement
Quoted 11 lines · Paolo Bonzini
hw/block/dataplane/virtio-blk.c +3
-2@@ -298,30 +278,31 @@ static void handle_notify(EventNotifier *e)vring_disable_notification (s->vdev, &s->vring);for (;;) { - head =vring_pop (s->vdev, &s->vring, iov, end, &out_num, &in_num); -if (head <0 ) { + ret =vring_pop (s->vdev, &s->vring, &elem); +if (ret <0 ) { +assert (elem ==NULL );break ;/* no more requests */ }
hw/block/dataplane/virtio-blk.c static void handle_notify(EventNotifier *e) · line 292
Quoted 10 lines
hw/block/dataplane/virtio-blk.c +4
-3-trace_virtio_blk_data_plane_process_request (s, out_num, in_num, - head); +trace_virtio_blk_data_plane_process_request (s, elem->out_num, + elem->in_num, elem->index); -if (process_request (&s->ioqueue, iov, out_num, in_num, head) <0 ) { +if (process_request (&s->ioqueue, elem) <0 ) {vring_set_broken (&s->vring); +vring_push (&s->vring, elem,0 );
If we give up on the vring I don't think we should push the element back. It may cause the guest to panic.
I guess what we really need here is to unmap scatter-gather buffers and delete elem.
Stefan
Re: [Qemu-devel] [PATCH 3/4] dataplane: change vring API to use VirtQueueElement
Il 04/12/2013 15:06, Stefan Hajnoczi ha scritto:
Quoted 26 lines · Paolo Bonzini
Quoted 11 lines
hw/block/dataplane/virtio-blk.c +3
-2@@ -298,30 +278,31 @@ static void handle_notify(EventNotifier *e)vring_disable_notification (s->vdev, &s->vring);for (;;) { - head =vring_pop (s->vdev, &s->vring, iov, end, &out_num, &in_num); -if (head <0 ) { + ret =vring_pop (s->vdev, &s->vring, &elem); +if (ret <0 ) { +assert (elem ==NULL );break ;/* no more requests */ }hw/block/dataplane/virtio-blk.c static void handle_notify(EventNotifier *e) · line 292
Quoted 10 lines
hw/block/dataplane/virtio-blk.c +4
-3-trace_virtio_blk_data_plane_process_request (s, out_num, in_num, - head); +trace_virtio_blk_data_plane_process_request (s, elem->out_num, + elem->in_num, elem->index); -if (process_request (&s->ioqueue, iov, out_num, in_num, head) <0 ) { +if (process_request (&s->ioqueue, elem) <0 ) {vring_set_broken (&s->vring); +vring_push (&s->vring, elem,0 );If we give up on the vring I don't think we should push the element back. It may cause the guest to panic.
Quoted 2 lines
I guess what we really need here is to unmap scatter-gather buffers and delete elem.
That's what already happens actually. vring_push has
+ g_slice_free(VirtQueueElement, elem);
+
/* Don't touch vring if a fatal error occurred */
if (vring->broken) {
return;in this patch and
+ for (i = 0; i < elem->out_num; i++) {
+ vring_unmap(elem->out_sg[i].iov_base, false);
+ }
+
+ for (i = 0; i < elem->in_num; i++) {
+ vring_unmap(elem->in_sg[i].iov_base, true);
+ }
g_slice_free(VirtQueueElement, elem);
in the next one.
Though I admit vring_push isn't such a great name and API. I can add instead a vring_free_element function. Do you think vring_push should call it, or should the caller do that?
Paolo
Re: [Qemu-devel] [PATCH 3/4] dataplane: change vring API to use VirtQueueElement
Quoted 51 lines · Paolo Bonzini
Il 04/12/2013 15:06, Stefan Hajnoczi ha scritto:
Quoted 26 lines · Paolo Bonzini
Quoted 11 lines
hw/block/dataplane/virtio-blk.c +3
-2@@ -298,30 +278,31 @@ static void handle_notify(EventNotifier *e)vring_disable_notification (s->vdev, &s->vring);for (;;) { - head =vring_pop (s->vdev, &s->vring, iov, end, &out_num, &in_num); -if (head <0 ) { + ret =vring_pop (s->vdev, &s->vring, &elem); +if (ret <0 ) { +assert (elem ==NULL );break ;/* no more requests */ }hw/block/dataplane/virtio-blk.c static void handle_notify(EventNotifier *e) · line 292
Quoted 10 lines
hw/block/dataplane/virtio-blk.c +4
-3-trace_virtio_blk_data_plane_process_request (s, out_num, in_num, - head); +trace_virtio_blk_data_plane_process_request (s, elem->out_num, + elem->in_num, elem->index); -if (process_request (&s->ioqueue, iov, out_num, in_num, head) <0 ) { +if (process_request (&s->ioqueue, elem) <0 ) {vring_set_broken (&s->vring); +vring_push (&s->vring, elem,0 );If we give up on the vring I don't think we should push the element back. It may cause the guest to panic.
Quoted 2 lines
I guess what we really need here is to unmap scatter-gather buffers and delete elem.
That's what already happens actually.
vring_pushhashw/virtio/dataplane/vring.c +2
-0+g_slice_free (VirtQueueElement, elem); +/* Don't touch vring if a fatal error occurred */ if (vring->broken) {return ;in this patch and
(quoted diff) +7
-0+ for (i = 0; i < elem->out_num; i++) { + vring_unmap(elem->out_sg[i].iov_base, false); + } + + for (i = 0; i < elem->in_num; i++) { + vring_unmap(elem->in_sg[i].iov_base, true); + } g_slice_free(VirtQueueElement, elem);
Quoted 5 lines
in the next one.
Though I admit
vring_pushisn't such a great name and API. I can add instead avring_free_elementfunction. Do you thinkvring_pushshould call it, or should the caller do that?
I think vring_push() should free the VirtQueueElement.
We just need to expose vring_free_element() so that handle_notify() can call it without pushing bogus buffers back to the guest.
Stefan
Re: [Qemu-devel] [PATCH 3/4] dataplane: change vring API to use VirtQueueElement
Il 05/12/2013 10:24, Stefan Hajnoczi ha scritto:
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
That's what already happens actually.
vring_pushhashw/virtio/dataplane/vring.c +2
-0+g_slice_free (VirtQueueElement, elem); +/* Don't touch vring if a fatal error occurred */ if (vring->broken) {return ;in this patch and
(quoted diff) +7
-0+ for (i = 0; i < elem->out_num; i++) { + vring_unmap(elem->out_sg[i].iov_base, false); + } + + for (i = 0; i < elem->in_num; i++) { + vring_unmap(elem->in_sg[i].iov_base, true); + } g_slice_free(VirtQueueElement, elem);in the next one.
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Though I admit
vring_pushisn't such a great name and API. I can add instead avring_free_elementfunction. Do you thinkvring_pushshould call it, or should the caller do that?I think
vring_push()should free the VirtQueueElement.We just need to expose
vring_free_element()so thathandle_notify()can call it without pushing bogus buffers back to the guest.
It's not pushing back bogus buffer, see the "if (vring->broken)" above.
But if you prefer handle_notify() to call vring_free_element(), I can
of course do that.
Paolo
Re: [Qemu-devel] [PATCH 3/4] dataplane: change vring API to use VirtQueueElement
Quoted 30 lines · Paolo Bonzini
Il 05/12/2013 10:24, Stefan Hajnoczi ha scritto:
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
That's what already happens actually.
vring_pushhashw/virtio/dataplane/vring.c +2
-0+g_slice_free (VirtQueueElement, elem); +/* Don't touch vring if a fatal error occurred */ if (vring->broken) {return ;in this patch and
(quoted diff) +7
-0+ for (i = 0; i < elem->out_num; i++) { + vring_unmap(elem->out_sg[i].iov_base, false); + } + + for (i = 0; i < elem->in_num; i++) { + vring_unmap(elem->in_sg[i].iov_base, true); + }Quoted 7 lines
Quoted 7 lines
g_slice_free(VirtQueueElement, elem);in the next one.
Though I admit
vring_pushisn't such a great name and API. I can add instead avring_free_elementfunction. Do you thinkvring_pushshould call it, or should the caller do that?I think
vring_push()should free the VirtQueueElement.
Quoted 6 lines
Quoted 2 lines
We just need to expose
vring_free_element()so thathandle_notify()can call it without pushing bogus buffers back to the guest.It's not pushing back bogus buffer, see the "if (
vring->broken)" above.
But if you preferhandle_notify()to callvring_free_element(), I can
of course do that.
Ah, I missed that :). It would be clearer to call vring_free_element() explicitly.
Stefan
PATCH 4/4 dataplane: replace hostmem with memory_region_find
In reply to Paolo Bonzini--- hw/virtio/dataplane/Makefile.objs | 2 +- hw/virtio/dataplane/hostmem.c | 183 ---------------------------------- hw/virtio/dataplane/vring.c | 74 ++++++++++++-- include/hw/virtio/dataplane/hostmem.h | 58 ----------- include/hw/virtio/dataplane/vring.h | 3 +- 5 files changed, 68 insertions(+), 252 deletions(-) delete mode 100644 hw/virtio/dataplane/hostmem.c delete mode 100644 include/hw/virtio/dataplane/hostmem.h
hw/virtio/dataplane/Makefile.objs +1 -1
@@ -1 +1 @@ -common-obj-y += hostmem.o vring.o +common-obj-y += vring.o
hw/virtio/dataplane/hostmem.c deleted +0 -183
183 lines not on this page · raw message
hw/virtio/dataplane/vring.c +66 -8
133 lines not on this page · raw message
include/hw/virtio/dataplane/hostmem.h deleted +0 -58
58 lines not on this page · raw message
include/hw/virtio/dataplane/vring.h +1 -2
@@ -19,11 +19,10 @@#include <linux/virtio_ring.h> #include "qemu-common.h" -#include "hostmem.h" #include "hw/virtio/virtio.h" typedef struct { - HostMem hostmem; /* guest memory mapper */ + MemoryRegion *mr; /* memory region containing the vring */ struct vring vr; /* virtqueue vring mapped to host memory */ uint16_t last_avail_idx; /* last processed avail ring index */ uint16_t last_used_idx; /* last processed used ring index */
Signature · 1 line
1.8.3.1
Quoted 24 lines · Paolo Bonzini
Now that the memory API is thread-safe, we can use it in virtio-blk-dataplane and replace hostmem.[ch]. This series does this, and also changes the vring API to use VirtQueueElement (with an eye towards migration). With this change, virtio-blk-dataplane is also safe against memory hot-unplug.
The next step would be to replace
memory_region_findwith address_space_{map,unmap}, which handle dirtying of memory correctly. However these APIs are not thread-safe yet, and neither is the handling of dirty memory (Juan's patches may be a start here).Also, the usage of iov_discard_{front,back} may cause some complication when we use address_space_{map,unmap}. We may have to change a bit the logic in virtio-blk-dataplane to switch to address_space_{map,unmap}.
If we do not want to do this intermediate step, the first three patches can be applied separately from the fourth.
Paolo Bonzini (4):
vring: create a common function to parse descriptors
vring: factor common code for error exits
dataplane: change vring API to use VirtQueueElement
dataplane: replace hostmem withmemory_region_find
Quoted 9 lines
hw/block/dataplane/virtio-blk.c | 86 +++++------- hw/virtio/dataplane/Makefile.objs | 2 +- hw/virtio/dataplane/hostmem.c | 183 ------------------------- hw/virtio/dataplane/vring.c | 244 +++++++++++++++++++++------------- include/hw/virtio/dataplane/hostmem.h | 58 -------- include/hw/virtio/dataplane/vring.h | 9 +- 6 files changed, 193 insertions(+), 389 deletions(-) delete mode 100644 hw/virtio/dataplane/hostmem.c delete mode 100644 include/hw/virtio/dataplane/hostmem.h
Finally looked into this series. I think we should merge all patches so we start exercising the thread-safe memory API as soon as possible in the QEMU 2.0 release cycle.
Before merging I wanted to discuss the vring->broken case which I've left a comment on.
Stefan