virtio-net: do not leak cpu mappings
cover virtio-net: do not leak cpu mappings
Hi all,
this patch series fixes a cpu mapping leak in virtio-net.
The bug is caused by virtio_net_handle_ctrl: it maps the entire out_sg iov, but then modifies it and reduces it (iov_discard_front), and only unmap the reduced version of the iov.
This causes a crash when running on Xen, but the behaviour is obviously incorrect without Xen too.
The patch series fixes the issue by allowing virtio_net_handle_ctrl to unmap the original out_sg iov but still call virtqueue_fill and virtqueue_flush on the modified iov.
The first three patches do not introduce any functional changes.
Stefano Stabellini (4):
introduce virtqueue_unmap_sg
use virtqueue_unmap_sg in virtqueue_fill
move virtqueue_unmap_sg calls from virtqueue_fill to virtqueue_push
virtio-net: do not leak cpu mappings
hw/net/virtio-net.c | 9 ++++++++- hw/virtio/virtio.c | 43 ++++++++++++++++++++++++------------------- include/hw/virtio/virtio.h | 2 ++ 3 files changed, 34 insertions(+), 20 deletions(-)
PATCH 1/4 introduce virtqueue_unmap_sg
Introduce a function to unmap an sg previously mapped with virtqueue_map_sg.
--- hw/virtio/virtio.c | 22 ++++++++++++++++++++++ include/hw/virtio/virtio.h | 2 ++ 2 files changed, 24 insertions(+)
hw/virtio/virtio.c +22 -0
@@ -446,6 +446,28 @@ void virtqueue_map_sg(struct iovec *sg, hwaddr *addr, } } +void virtqueue_unmap_sg (const struct iovec *sg,size_t num_sg, +int is_write,unsigned int len) +{ +unsigned int i; +unsigned int offset; + +if (num_sg >VIRTQUEUE_MAX_SIZE ) { +error_report ("virtio: unmap attempt out of bounds: %zd > %d" , + num_sg,VIRTQUEUE_MAX_SIZE ); +exit (1 ); + } + + offset =0 ; +for (i =0 ; i < num_sg; i++) { +size_t size =MIN (len - offset, sg[i].iov_len); + +cpu_physical_memory_unmap (sg[i].iov_base, sg[i].iov_len, is_write, size); + + offset += size; + } +} +int virtqueue_pop (VirtQueue *vq,VirtQueueElement *elem) {unsigned int i, head, max;
include/hw/virtio/virtio.h +2 -0
@@ -173,6 +173,8 @@ void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,void virtqueue_map_sg (struct iovec *sg,hwaddr *addr,size_t num_sg,int is_write); +void virtqueue_unmap_sg (const struct iovec *sg,size_t num_sg, +int is_write,unsigned int len);int virtqueue_pop (VirtQueue *vq,VirtQueueElement *elem);int virtqueue_avail_bytes (VirtQueue *vq,unsigned int in_bytes,unsigned int out_bytes);
Signature · 1 line
1.7.10.4
Re: [Qemu-devel] [PATCH 1/4] introduce virtqueue_unmap_sg
Quoted 32 lines · Stefano Stabellini
Introduce a function to unmap an sg previously mapped with
virtqueue_map_sg.Signed-off-by Stefano Stabellini <[email protected]>--- hw/virtio/virtio.c | 22 ++++++++++++++++++++++ include/hw/virtio/virtio.h | 2 ++ 2 files changed, 24 insertions(+)hw/virtio/virtio.c +11
-0@@ -446,6 +446,28 @@ void virtqueue_map_sg(struct iovec *sg, hwaddr *addr, } } +void virtqueue_unmap_sg (const struct iovec *sg,size_t num_sg, +int is_write,unsigned int len) +{ +unsigned int i; +unsigned int offset; + +if (num_sg >VIRTQUEUE_MAX_SIZE ) { +error_report ("virtio: unmap attempt out of bounds: %zd > %d" , + num_sg,VIRTQUEUE_MAX_SIZE ); +exit (1 ); + }
hw/virtio/virtio.c void virtqueue_map_sg(struct iovec *sg, hwaddr *addr, · line 469
Quoted 10 lines
hw/virtio/virtio.c +10
-0+ + offset =0 ; +for (i =0 ; i < num_sg; i++) { +size_t size =MIN (len - offset, sg[i].iov_len); + +cpu_physical_memory_unmap (sg[i].iov_base, sg[i].iov_len, is_write, size); + + offset += size; + } +}
It seems rather odd that "size" and the iov_len fields in the iovec are size_t but 'len' and 'offset' are only unsigned int...
thanks
-- PMM
Re: [Qemu-devel] [PATCH 1/4] introduce virtqueue_unmap_sg
Quoted 40 lines · Peter Maydell
Quoted 38 lines · Stefano Stabellini
Introduce a function to unmap an sg previously mapped with
virtqueue_map_sg.Signed-off-by Stefano Stabellini <[email protected]>--- hw/virtio/virtio.c | 22 ++++++++++++++++++++++ include/hw/virtio/virtio.h | 2 ++ 2 files changed, 24 insertions(+)hw/virtio/virtio.c +17
-0@@ -446,6 +446,28 @@ void virtqueue_map_sg(struct iovec *sg, hwaddr *addr, } } +void virtqueue_unmap_sg (const struct iovec *sg,size_t num_sg, +int is_write,unsigned int len) +{ +unsigned int i; +unsigned int offset; + +if (num_sg >VIRTQUEUE_MAX_SIZE ) { +error_report ("virtio: unmap attempt out of bounds: %zd > %d" , + num_sg,VIRTQUEUE_MAX_SIZE ); +exit (1 ); + } + + offset =0 ; +for (i =0 ; i < num_sg; i++) { +size_t size =MIN (len - offset, sg[i].iov_len); + +cpu_physical_memory_unmap (sg[i].iov_base, sg[i].iov_len, is_write, size);
Quoted 8 lines
hw/virtio/virtio.c void virtqueue_map_sg(struct iovec *sg, hwaddr *addr, · line 469
Quoted 4 lines
hw/virtio/virtio.c +4
-0+ + offset += size; + } +}It seems rather odd that "size" and the
iov_lenfields in the iovec aresize_tbut 'len' and 'offset' are only unsigned int...
The code is taken from virtqueue_fill but that's a good point.
PATCH 2/4 use virtqueue_unmap_sg in virtqueue_fill
In reply to Stefano StabelliniUse virtqueue_unmap_sg to unmap in_sg and out_sg in virtqueue_fill.
No functional changes.
--- hw/virtio/virtio.c | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-)
hw/virtio/virtio.c +2 -18
@@ -238,26 +238,10 @@ int virtio_queue_empty(VirtQueue *vq)void virtqueue_fill (VirtQueue *vq,const VirtQueueElement *elem,unsigned int len,unsigned int idx) { -unsigned int offset; -int i; -trace_virtqueue_fill (vq, elem, len, idx); - offset =0 ; -for (i =0 ; i < elem->in_num; i++) { -size_t size =MIN (len - offset, elem->in_sg[i].iov_len); - -cpu_physical_memory_unmap (elem->in_sg[i].iov_base, - elem->in_sg[i].iov_len, -1 , size); - - offset += size; - } - -for (i =0 ; i < elem->out_num; i++) -cpu_physical_memory_unmap (elem->out_sg[i].iov_base, - elem->out_sg[i].iov_len, -0 , elem->out_sg[i].iov_len); +virtqueue_unmap_sg (elem->in_sg, elem->in_num,1 , len); +virtqueue_unmap_sg (elem->out_sg, elem->out_num,0 ,UINT_MAX ); idx = (idx +vring_used_idx (vq)) % vq->vring.num;
Signature · 1 line
1.7.10.4
PATCH 3/4 move virtqueue_unmap_sg calls from virtqueue_fill to virtqueue_push
In reply to Stefano StabelliniMove the two virtqueue_unmap_sg calls from virtqueue_fill tovirtqueue_push: most virtio drivers simply call virtqueue_push so they are unaffected. virtio-net calls virtqueue_fill directly, so we need to make the two virtqueue_unmap_sg calls explicitely there.
Also replace the virtqueue_push call from virtio_net_handle_ctrl with an open coded version it in place.
No functional changes.
--- hw/net/virtio-net.c | 7 ++++++- hw/virtio/virtio.c | 5 ++--- 2 files changed, 8 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +6 -1
@@ -803,7 +803,10 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) s =iov_from_buf (elem.in_sg, elem.in_num,0 , &status,sizeof (status));assert (s ==sizeof (status)); -virtqueue_push (vq, &elem,sizeof (status)); +virtqueue_unmap_sg (elem.in_sg, elem.in_num,1 ,sizeof (status)); +virtqueue_unmap_sg (elem.out_sg, elem.out_num,0 ,UINT_MAX ); +virtqueue_fill (vq, &elem,sizeof (status),0 ); +virtqueue_flush (vq,1 );virtio_notify (vdev, vq); } }
@@ -1052,6 +1055,8 @@ static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t }/* signal other side */ +virtqueue_unmap_sg (elem.in_sg, elem.in_num,1 , total); +virtqueue_unmap_sg (elem.out_sg, elem.out_num,0 ,UINT_MAX );virtqueue_fill (q->rx_vq, &elem, total, i++); }
hw/virtio/virtio.c +2 -3
@@ -240,9 +240,6 @@ void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, {trace_virtqueue_fill (vq, elem, len, idx); -virtqueue_unmap_sg (elem->in_sg, elem->in_num,1 , len); -virtqueue_unmap_sg (elem->out_sg, elem->out_num,0 ,UINT_MAX ); - idx = (idx +vring_used_idx (vq)) % vq->vring.num;/* Get a pointer to the next entry in the used ring. */
@@ -267,6 +264,8 @@ void virtqueue_flush(VirtQueue *vq, unsigned int count)void virtqueue_push (VirtQueue *vq,const VirtQueueElement *elem,unsigned int len) { +virtqueue_unmap_sg (elem->in_sg, elem->in_num,1 , len); +virtqueue_unmap_sg (elem->out_sg, elem->out_num,0 ,UINT_MAX );virtqueue_fill (vq, elem, len,0 );virtqueue_flush (vq,1 ); }
Signature · 1 line
1.7.10.4
PATCH 4/4 virtio-net: do not leak cpu mappings
In reply to Stefano StabelliniIn virtio_net_handle_ctrl unmap the previously mapped out_sg, not a subset of it.
This patch fixes an abort() when running on Xen.
--- hw/net/virtio-net.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +3 -1
@@ -773,6 +773,7 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)VirtQueueElement elem;size_t s;struct iovec *iov; +struct iovec iov_copy[VIRTQUEUE_MAX_SIZE ];unsigned int iov_cnt;while (virtqueue_pop (vq, &elem)) {
@@ -782,6 +783,7 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)exit (1 ); } +memcpy (iov_copy, elem.out_sg, elem.out_num*sizeof (struct iovec )); iov = elem.out_sg; iov_cnt = elem.out_num; s =iov_to_buf (iov, iov_cnt,0 , &ctrl,sizeof (ctrl));
@@ -804,7 +806,7 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)assert (s ==sizeof (status));virtqueue_unmap_sg (elem.in_sg, elem.in_num,1 ,sizeof (status)); -virtqueue_unmap_sg (elem.out_sg, elem.out_num,0 ,UINT_MAX ); +virtqueue_unmap_sg (iov_copy, elem.out_num,0 ,UINT_MAX );virtqueue_fill (vq, &elem,sizeof (status),0 );virtqueue_flush (vq,1 );virtio_notify (vdev, vq);
Signature · 1 line
1.7.10.4
Il 25/11/2014 15:42, Stefano Stabellini ha scritto:
Quoted 7 lines
Hi all,
this patch series fixes a cpu mapping leak in virtio-net.The bug is caused by
virtio_net_handle_ctrl: it maps the entireout_sgiov, but then modifies it and reduces it (iov_discard_front), and only unmap the reduced version of the iov.
Quoted 8 lines
This causes a crash when running on Xen, but the behaviour is obviously incorrect without Xen too.
The patch series fixes the issue by allowing
virtio_net_handle_ctrlto unmap the originalout_sgiov but still callvirtqueue_fillandvirtqueue_flushon the modified iov.The first three patches do not introduce any functional changes.
Thanks for these pathes, 1-2 years ago I tried to use virtio net and disks on xen unsuccessful and I was unable to solve it. When I'll have time I'll retry.
About virtio disks can have problem similar to this or should be ok?
About the other patches posted in link below are all not applied and should be only rebased or they must be fully revisioned/modified?
http://wiki.xen.org/wiki/Virtio_On_Xen
Thanks for any reply and sorry for my bad english.
Quoted 17 lines
Stefano Stabellini (4):
introducevirtqueue_unmap_sg
usevirtqueue_unmap_sginvirtqueue_fill
movevirtqueue_unmap_sgcalls fromvirtqueue_filltovirtqueue_push
virtio-net: do not leak cpu mappingshw/net/virtio-net.c | 9 ++++++++- hw/virtio/virtio.c | 43 ++++++++++++++++++++++++------------------- include/hw/virtio/virtio.h | 2 ++ 3 files changed, 34 insertions(+), 20 deletions(-)_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 17 lines · Fabio Fantoni
Il 25/11/2014 15:42, Stefano Stabellini ha scritto:
Quoted 7 lines
Hi all,
this patch series fixes a cpu mapping leak in virtio-net.The bug is caused by
virtio_net_handle_ctrl: it maps the entireout_sgiov, but then modifies it and reduces it (iov_discard_front), and only unmap the reduced version of the iov.Quoted 8 lines
This causes a crash when running on Xen, but the behaviour is obviously incorrect without Xen too.
The patch series fixes the issue by allowing
virtio_net_handle_ctrlto unmap the originalout_sgiov but still callvirtqueue_fillandvirtqueue_flushon the modified iov.The first three patches do not introduce any functional changes.
Quoted 8 lines
Thanks for these pathes, 1-2 years ago I tried to use virtio net and disks on xen unsuccessful and I was unable to solve it.
When I'll have time I'll retry.
About virtio disks can have problem similar to this or should be ok? About the other patches posted in link below are all not applied and should be only rebased or they must be fully revisioned/modified?
http://wiki.xen.org/wiki/Virtio_On_Xen
Don't use patches on that page. They are outdated.
I think Stefano's patch series intends to fix issue for HVM guest, which might well be what you're using. Well, I would be very surprised if you tell me you're using PV virtio! ;-)
Wei.