Skip to contentSkip to conversation

virtio,vhost-gpu: Release memory returned by malloc() with free()

PATCH NAK Philippe Mathieu-Daudé 5 participants 13 messages Mar 23, 2020 – Apr 15, 2020

lore
Share & tools
lore
#0 lore raw

cover virtio,vhost-gpu: Release memory returned by malloc() with free()

Coverity reported a ALLOC_FREE_MISMATCH in vg_handle_cursor(), because the memory returned by vu_queue_pop() is allocated with malloc(). Fix it.

Similar error occurs with virtio. Document and fix.

Philippe Mathieu-Daudé (3):
vhost-user-gpu: Release memory returned by vu_queue_pop() with free()
virtio: Document virtqueue_pop()
virtio-gpu: Release memory returned by virtqueue_pop() with free()

 include/hw/virtio/virtio.h              | 8 ++++++++
 contrib/vhost-user-gpu/vhost-user-gpu.c | 4 ++--
 contrib/vhost-user-gpu/virgl.c          | 2 +-
 hw/display/virtio-gpu-3d.c              | 2 +-
 hw/display/virtio-gpu.c                 | 8 ++++----
 5 files changed, 16 insertions(+), 8 deletions(-)
Signature · 1 line
2.21.1
#4 lore raw

PATCH 2/3 virtio: Document virtqueue_pop()

Document that virtqueue_pop() returned memory must be released with free().

---
 include/hw/virtio/virtio.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/hw/virtio/virtio.h +8 -0
index b69d517496..c6e3bfc500 100644
@@ -199,6 +199,14 @@ void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
                     unsigned int len, unsigned int idx);
 
 void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
+/**
+ * virtqueue_pop:
+ * @vq: a VirtQueue queue
+ * @sz: the size of struct to return (must be >= VirtQueueElement)
+ *
+ * Returns: a VirtQueueElement filled from the queue or NULL.
+ * The returned element must be free()-d by the caller.
+ */
 void *virtqueue_pop(VirtQueue *vq, size_t sz);
 unsigned int virtqueue_drop_all(VirtQueue *vq);
 void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
Signature · 1 line
2.21.1
#1 lore raw

PATCH 1/3 vhost-user-gpu: Release memory returned by vu_queue_pop() with free()

In reply to Philippe Mathieu-Daudé

vu_queue_pop() returns memory that must be freed with free().

Reported-by Coverity (CID 1421887 ALLOC_FREE_MISMATCH)
---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 4 ++--
 contrib/vhost-user-gpu/virgl.c          | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
contrib/vhost-user-gpu/vhost-user-gpu.c +2 -2
index b45d2019b4..a019d0a9ac 100644
@@ -848,7 +848,7 @@ vg_handle_ctrl(VuDev *dev, int qidx)
             QTAILQ_INSERT_TAIL(&vg->fenceq, cmd, next);
             vg->inflight++;
         } else {
-            g_free(cmd);
+            free(cmd);
         }
     }
 }
@@ -939,7 +939,7 @@ vg_handle_cursor(VuDev *dev, int qidx)
         }
         vu_queue_push(dev, vq, elem, 0);
         vu_queue_notify(dev, vq);
-        g_free(elem);
+        free(elem);
     }
 }
 
contrib/vhost-user-gpu/virgl.c +1 -1
index 43413e29df..b0bc22c3c1 100644
@@ -519,7 +519,7 @@ virgl_write_fence(void *opaque, uint32_t fence)
         g_debug("FENCE %" PRIu64, cmd->cmd_hdr.fence_id);
         vg_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
         QTAILQ_REMOVE(&g->fenceq, cmd, next);
-        g_free(cmd);
+        free(cmd);
         g->inflight--;
     }
 }
Signature · 1 line
2.21.1
#8 lore raw

PATCH 3/3 virtio-gpu: Release memory returned by virtqueue_pop() with free()

In reply to Philippe Mathieu-Daudé

As virtio_gpu_handle_ctrl() fills the cmdq calling virtqueue_pop(), we need to release it with free() in virtio_gpu_reset().

virtio_gpu_handle_ctrl() allocates memory calling virtqueue_pop(), release it in virtio_gpu_process_cmdq() with free().

virtio_gpu_process_cmdq() move commands from cmdq to fenceq, so we also have to release them with free().

---
 hw/display/virtio-gpu-3d.c | 2 +-
 hw/display/virtio-gpu.c    | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
hw/display/virtio-gpu-3d.c +1 -1
index 96621576c2..3a97d267e5 100644
@@ -506,7 +506,7 @@ static void virgl_write_fence(void *opaque, uint32_t fence)
         trace_virtio_gpu_fence_resp(cmd->cmd_hdr.fence_id);
         virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
         QTAILQ_REMOVE(&g->fenceq, cmd, next);
-        g_free(cmd);
+        free(cmd);
         g->inflight--;
         if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
             fprintf(stderr, "inflight: %3d (-)\r", g->inflight);
hw/display/virtio-gpu.c +4 -4
index 5f0dd7c150..f5fbb722ee 100644
@@ -835,7 +835,7 @@ void virtio_gpu_process_cmdq(VirtIOGPU *g)
                 fprintf(stderr, "inflight: %3d (+)\r", g->inflight);
             }
         } else {
-            g_free(cmd);
+            free(cmd);
         }
     }
 }
@@ -921,7 +921,7 @@ static void virtio_gpu_handle_cursor(VirtIODevice *vdev, VirtQueue *vq)
         }
         virtqueue_push(vq, elem, 0);
         virtio_notify(vdev, vq);
-        g_free(elem);
+        free(elem);
     }
 }
 
@@ -1157,14 +1157,14 @@ static void virtio_gpu_reset(VirtIODevice *vdev)
     while (!QTAILQ_EMPTY(&g->cmdq)) {
         cmd = QTAILQ_FIRST(&g->cmdq);
         QTAILQ_REMOVE(&g->cmdq, cmd, next);
-        g_free(cmd);
+        free(cmd);
     }
 
     while (!QTAILQ_EMPTY(&g->fenceq)) {
         cmd = QTAILQ_FIRST(&g->fenceq);
         QTAILQ_REMOVE(&g->fenceq, cmd, next);
         g->inflight--;
-        g_free(cmd);
+        free(cmd);
     }
 
 #ifdef CONFIG_VIRGL
Signature · 1 line
2.21.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH-for-5.0 1/3] vhost-user-gpu: Release memory returned by vu_queue_pop() with free()

In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

vu_queue_pop() returns memory that must be freed with free().

Reported-by Coverity (CID 1421887 ALLOC_FREE_MISMATCH)
Quoted 43 lines
---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 4 ++--
 contrib/vhost-user-gpu/virgl.c          | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
contrib/vhost-user-gpu/vhost-user-gpu.c +2 -2
index b45d2019b4..a019d0a9ac 100644
@@ -848,7 +848,7 @@ vg_handle_ctrl(VuDev *dev, int qidx)
             QTAILQ_INSERT_TAIL(&vg->fenceq, cmd, next);
             vg->inflight++;
         } else {
-            g_free(cmd);
+            free(cmd);
         }
     }
 }
@@ -939,7 +939,7 @@ vg_handle_cursor(VuDev *dev, int qidx)
         }
         vu_queue_push(dev, vq, elem, 0);
         vu_queue_notify(dev, vq);
-        g_free(elem);
+        free(elem);
     }
 }
 
contrib/vhost-user-gpu/virgl.c +1 -2
index 43413e29df..b0bc22c3c1 100644
@@ -519,7 +519,7 @@ virgl_write_fence(void *opaque, uint32_t fence)
         g_debug("FENCE %" PRIu64, cmd->cmd_hdr.fence_id);
         vg_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
         QTAILQ_REMOVE(&g->fenceq, cmd, next);
-        g_free(cmd);
+        free(cmd);
         g->inflight--;
     }
 }
--

2.21.1

Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH-for-5.0 2/3] virtio: Document virtqueue_pop()

In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

Document that virtqueue_pop() returned memory must be released with free().

Quoted 26 lines
---
 include/hw/virtio/virtio.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/hw/virtio/virtio.h +8 -1
index b69d517496..c6e3bfc500 100644
@@ -199,6 +199,14 @@ void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
                     unsigned int len, unsigned int idx);
 
 void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
+/**
+ * virtqueue_pop:
+ * @vq: a VirtQueue queue
+ * @sz: the size of struct to return (must be >= VirtQueueElement)
+ *
+ * Returns: a VirtQueueElement filled from the queue or NULL.
+ * The returned element must be free()-d by the caller.
+ */
 void *virtqueue_pop(VirtQueue *vq, size_t sz);
 unsigned int virtqueue_drop_all(VirtQueue *vq);
 void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
--

2.21.1

#10 lore raw

Re: [PATCH-for-5.0 0/3] virtio, vhost-gpu: Release memory returned by malloc() with free()

In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

Coverity reported a ALLOC_FREE_MISMATCH in vg_handle_cursor(), because the memory returned by vu_queue_pop() is allocated with malloc(). Fix it.

Similar error occurs with virtio. Document and fix.

I will queue this. Thanks!

Quoted 14 lines

Philippe Mathieu-Daudé (3):
vhost-user-gpu: Release memory returned by vu_queue_pop() with free()
virtio: Document virtqueue_pop()
virtio-gpu: Release memory returned by virtqueue_pop() with free()

 include/hw/virtio/virtio.h              | 8 ++++++++
 contrib/vhost-user-gpu/vhost-user-gpu.c | 4 ++--
 contrib/vhost-user-gpu/virgl.c          | 2 +-
 hw/display/virtio-gpu-3d.c              | 2 +-
 hw/display/virtio-gpu.c                 | 8 ++++----
 5 files changed, 16 insertions(+), 8 deletions(-)
Signature · 1 line
2.21.1
#9 lore raw

Re: [PATCH-for-5.0 3/3] virtio-gpu: Release memory returned by virtqueue_pop() with free()

In reply to Philippe Mathieu-Daudé

Hi

Quoted 11 lines · Philippe Mathieu-Daudé

As virtio_gpu_handle_ctrl() fills the cmdq calling virtqueue_pop(), we need to release it with free() in virtio_gpu_reset().

virtio_gpu_handle_ctrl() allocates memory calling virtqueue_pop(), release it in virtio_gpu_process_cmdq() with free().

virtio_gpu_process_cmdq() move commands from cmdq to fenceq, so we also have to release them with free().

nack, hw/virtio/virtio.c uses gmalloc (or I am missing something)

Quoted 61 lines
---
 hw/display/virtio-gpu-3d.c | 2 +-
 hw/display/virtio-gpu.c    | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
hw/display/virtio-gpu-3d.c +1 -1
index 96621576c2..3a97d267e5 100644
@@ -506,7 +506,7 @@ static void virgl_write_fence(void *opaque, uint32_t fence)
         trace_virtio_gpu_fence_resp(cmd->cmd_hdr.fence_id);
         virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
         QTAILQ_REMOVE(&g->fenceq, cmd, next);
-        g_free(cmd);
+        free(cmd);
         g->inflight--;
         if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
             fprintf(stderr, "inflight: %3d (-)\r", g->inflight);
hw/display/virtio-gpu.c +4 -5
index 5f0dd7c150..f5fbb722ee 100644
@@ -835,7 +835,7 @@ void virtio_gpu_process_cmdq(VirtIOGPU *g)
                 fprintf(stderr, "inflight: %3d (+)\r", g->inflight);
             }
         } else {
-            g_free(cmd);
+            free(cmd);
         }
     }
 }
@@ -921,7 +921,7 @@ static void virtio_gpu_handle_cursor(VirtIODevice *vdev, VirtQueue *vq)
         }
         virtqueue_push(vq, elem, 0);
         virtio_notify(vdev, vq);
-        g_free(elem);
+        free(elem);
     }
 }
 
@@ -1157,14 +1157,14 @@ static void virtio_gpu_reset(VirtIODevice *vdev)
     while (!QTAILQ_EMPTY(&g->cmdq)) {
         cmd = QTAILQ_FIRST(&g->cmdq);
         QTAILQ_REMOVE(&g->cmdq, cmd, next);
-        g_free(cmd);
+        free(cmd);
     }
 
     while (!QTAILQ_EMPTY(&g->fenceq)) {
         cmd = QTAILQ_FIRST(&g->fenceq);
         QTAILQ_REMOVE(&g->fenceq, cmd, next);
         g->inflight--;
-        g_free(cmd);
+        free(cmd);
     }
 
 #ifdef CONFIG_VIRGL
--

2.21.1

Signature · 1 line
Marc-André Lureau
#6 lore raw

Re: [PATCH-for-5.0 2/3] virtio: Document virtqueue_pop()

In reply to Marc-André Lureau
Quoted 10 lines · Marc-André Lureau
Quoted 5 lines · Philippe Mathieu-Daudé

Document that virtqueue_pop() returned memory must be released with free().

nack, hw/virtio/virtio.c uses g_malloc

Quoted 30 lines
Quoted 28 lines

---
 include/hw/virtio/virtio.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/hw/virtio/virtio.h +8 -1
index b69d517496..c6e3bfc500 100644
@@ -199,6 +199,14 @@ void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
                     unsigned int len, unsigned int idx);
 
 void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
+/**
+ * virtqueue_pop:
+ * @vq: a VirtQueue queue
+ * @sz: the size of struct to return (must be >= VirtQueueElement)
+ *
+ * Returns: a VirtQueueElement filled from the queue or NULL.
+ * The returned element must be free()-d by the caller.
+ */
 void *virtqueue_pop(VirtQueue *vq, size_t sz);
 unsigned int virtqueue_drop_all(VirtQueue *vq);
 void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
--

2.21.1

Signature · 1 line
Marc-André Lureau
#7 lore raw

Re: [PATCH-for-5.0 2/3] virtio: Document virtqueue_pop()

Quoted 6 lines · Marc-André Lureau
Quoted 4 lines · Marc-André Lureau
Quoted 1 line · Philippe Mathieu-Daudé
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Document that virtqueue_pop() returned memory must be released with free().

nack, hw/virtio/virtio.c uses g_malloc

Indeed... I opened hw/virtio/ and contrib/libvhost-user/ in the geany editor, and when I navigated from virtio.c to the virtqueue_alloc_element() definition I ended in libvhost-user.c:

static void *
virtqueue_alloc_element(size_t sz,
unsigned out_num, unsigned in_num)

{
    VuVirtqElement *elem;
    size_t in_sg_ofs = ALIGN_UP(sz, __alignof__(elem->in_sg[0]));
    size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
    size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);

    assert(sz >= sizeof(VuVirtqElement));
    elem = malloc(out_sg_end);
    elem->out_num = out_num;
    elem->in_num = in_num;
    elem->in_sg = (void *)elem + in_sg_ofs;
    elem->out_sg = (void *)elem + out_sg_ofs;
    return elem;
}
Quoted 33 lines
Quoted 31 lines
Quoted 29 lines


---
  include/hw/virtio/virtio.h | 8 ++++++++
  1 file changed, 8 insertions(+)
include/hw/virtio/virtio.h +8 -1
index b69d517496..c6e3bfc500 100644
@@ -199,6 +199,14 @@ void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
                      unsigned int len, unsigned int idx);
 
  void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
+/**
+ * virtqueue_pop:
+ * @vq: a VirtQueue queue
+ * @sz: the size of struct to return (must be >= VirtQueueElement)
+ *
+ * Returns: a VirtQueueElement filled from the queue or NULL.
+ * The returned element must be free()-d by the caller.
+ */
  void *virtqueue_pop(VirtQueue *vq, size_t sz);
  unsigned int virtqueue_drop_all(VirtQueue *vq);
  void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
--

2.21.1

#11 lore raw

Re: [PATCH-for-5.0 0/3] virtio, vhost-gpu: Release memory returned by malloc() with free()

In reply to Michael S. Tsirkin
Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Philippe Mathieu-Daudé

Coverity reported a ALLOC_FREE_MISMATCH in vg_handle_cursor(), because the memory returned by vu_queue_pop() is allocated with malloc(). Fix it.

Similar error occurs with virtio. Document and fix.

I will queue this. Thanks!

So what are we doing with this patchset? Marc-André reported issues - any plan to fix them up? Split up the patchset to 3 independent patches?

Quoted 14 lines
Quoted 14 lines

Philippe Mathieu-Daudé (3):
vhost-user-gpu: Release memory returned by vu_queue_pop() with free()
virtio: Document virtqueue_pop()
virtio-gpu: Release memory returned by virtqueue_pop() with free()

 include/hw/virtio/virtio.h              | 8 ++++++++
 contrib/vhost-user-gpu/vhost-user-gpu.c | 4 ++--
 contrib/vhost-user-gpu/virgl.c          | 2 +-
 hw/display/virtio-gpu-3d.c              | 2 +-
 hw/display/virtio-gpu.c                 | 8 ++++----
 5 files changed, 16 insertions(+), 8 deletions(-)
Signature · 1 line
2.21.1
#12 lore raw

Re: [PATCH-for-5.0 0/3] virtio, vhost-gpu: Release memory returned by malloc() with free()

Quoted 6 lines · Michael S. Tsirkin
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Philippe Mathieu-Daudé

Coverity reported a ALLOC_FREE_MISMATCH in vg_handle_cursor(), because the memory returned by vu_queue_pop() is allocated with malloc(). Fix it.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Similar error occurs with virtio. Document and fix.

I will queue this. Thanks!

So what are we doing with this patchset? Marc-André reported issues - any plan to fix them up? Split up the patchset to 3 independent patches?

As noted Marc-André, patches 2 & 3 are incorrect.

Patch 1 can be queued as it with no modification.

Quoted 16 lines
Quoted 15 lines
Quoted 15 lines

Philippe Mathieu-Daudé (3):
vhost-user-gpu: Release memory returned by vu_queue_pop() with free()
virtio: Document virtqueue_pop()
virtio-gpu: Release memory returned by virtqueue_pop() with free()

  include/hw/virtio/virtio.h              | 8 ++++++++
  contrib/vhost-user-gpu/vhost-user-gpu.c | 4 ++--
  contrib/vhost-user-gpu/virgl.c          | 2 +-
  hw/display/virtio-gpu-3d.c              | 2 +-
  hw/display/virtio-gpu.c                 | 8 ++++----
  5 files changed, 16 insertions(+), 8 deletions(-)
Signature · 1 line
2.21.1
#3 lore raw

Re: [PATCH-for-5.0 1/3] vhost-user-gpu: Release memory returned by vu_queue_pop() with free()

In reply to Philippe Mathieu-Daudé
Quoted 11 lines · Philippe Mathieu-Daudé

vu_queue_pop() returns memory that must be freed with free().

Reported-by Coverity (CID 1421887 ALLOC_FREE_MISMATCH)
---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 4 ++--
 contrib/vhost-user-gpu/virgl.c          | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

Applied this patch (and not any of the rest of the series) to master for rc3, thanks.

-- PMM