Skip to contentSkip to conversation

virtio-net: do not leak cpu mappings

PATCH In review Stefano Stabellini 4 participants 9 messages Nov 25, 2014

lore
Share & tools
lore
#0 lore raw

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(-)
#1 lore raw

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
index 3e4b70c..2621ae6 100644
@@ -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
index 3e54e90..2325053 100644
@@ -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
#2 lore raw

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.

---
 hw/virtio/virtio.c         |   22 ++++++++++++++++++++++
 include/hw/virtio/virtio.h |    2 ++
 2 files changed, 24 insertions(+)
hw/virtio/virtio.c +11 -0
index 3e4b70c..2621ae6 100644
@@ -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

#3 lore raw

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.

---
 hw/virtio/virtio.c         |   22 ++++++++++++++++++++++
 include/hw/virtio/virtio.h |    2 ++
 2 files changed, 24 insertions(+)
hw/virtio/virtio.c +17 -0
index 3e4b70c..2621ae6 100644
@@ -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_len fields in the iovec are size_t but 'len' and 'offset' are only unsigned int...

The code is taken from virtqueue_fill but that's a good point.

#4 lore raw

PATCH 2/4 use virtqueue_unmap_sg in virtqueue_fill

In reply to Stefano Stabellini

Use 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
index 2621ae6..4af31d0 100644
@@ -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
#5 lore raw

PATCH 3/4 move virtqueue_unmap_sg calls from virtqueue_fill to virtqueue_push

In reply to Stefano Stabellini

Move the two virtqueue_unmap_sg calls from virtqueue_fill to
virtqueue_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
index 2ac6ce5..5035313 100644
@@ -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
index 4af31d0..0a6583a 100644
@@ -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
#6 lore raw

PATCH 4/4 virtio-net: do not leak cpu mappings

In reply to Stefano Stabellini

In 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
index 5035313..c9b82f3 100644
@@ -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
#7 lore raw
In reply to Stefano Stabellini

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 entire out_sg iov, 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_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.

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):
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(-)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#8 lore raw
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 entire out_sg iov, 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_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.

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.