Skip to contentSkip to conversation

vhost-user-input: use free(elem) instead of g_free(elem)

PATCH In review Stefan Hajnoczi 6 participants 7 messages Nov 19, 2019 – Nov 21, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost-user-input: use free(elem) instead of g_free(elem)

The virtqueue element returned by vu_queue_pop() is allocated using malloc(3) by virtqueue_alloc_element(). Use the matching free(3) function instead of glib's g_free().

---
 contrib/vhost-user-input/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
contrib/vhost-user-input/main.c +2 -2
index 449fd2171a..ef4b7769f2 100644
@@ -77,7 +77,7 @@ static void vi_input_send(VuInput *vi, struct virtio_input_event *event)
         len = iov_from_buf(elem->in_sg, elem->in_num,
                            0, &vi->queue[i].event, sizeof(virtio_input_event));
         vu_queue_push(dev, vq, elem, len);
-        g_free(elem);
+        free(elem);
     }
 
     vu_queue_notify(&vi->dev.parent, vq);
@@ -153,7 +153,7 @@ static void vi_handle_sts(VuDev *dev, int qidx)
                          0, &event, sizeof(event));
         vi_handle_status(vi, &event);
         vu_queue_push(dev, vq, elem, len);
-        g_free(elem);
+        free(elem);
     }
 
     vu_queue_notify(&vi->dev.parent, vq);
Signature · 1 line
2.23.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Stefan Hajnoczi

The virtqueue element returned by vu_queue_pop() is allocated using malloc(3) by virtqueue_alloc_element(). Use the matching free(3) function instead of glib's g_free().

Quoted 30 lines
---
 contrib/vhost-user-input/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
contrib/vhost-user-input/main.c +2 -3
index 449fd2171a..ef4b7769f2 100644
@@ -77,7 +77,7 @@ static void vi_input_send(VuInput *vi, struct virtio_input_event *event)
         len = iov_from_buf(elem->in_sg, elem->in_num,
                            0, &vi->queue[i].event, sizeof(virtio_input_event));
         vu_queue_push(dev, vq, elem, len);
-        g_free(elem);
+        free(elem);
     }
 
     vu_queue_notify(&vi->dev.parent, vq);
@@ -153,7 +153,7 @@ static void vi_handle_sts(VuDev *dev, int qidx)
                          0, &event, sizeof(event));
         vi_handle_status(vi, &event);
         vu_queue_push(dev, vq, elem, len);
-        g_free(elem);
+        free(elem);
     }
 
     vu_queue_notify(&vi->dev.parent, vq);
--

2.23.0

Signature · 1 line
Marc-André Lureau
Reviewed-by on v1 1/1
#2 lore raw
In reply to Stefan Hajnoczi
Quoted 5 lines · Stefan Hajnoczi

The virtqueue element returned by vu_queue_pop() is allocated using malloc(3) by virtqueue_alloc_element(). Use the matching free(3) function instead of glib's g_free().

Fixes 06914c97d3a ?
Quoted 27 lines
---
  contrib/vhost-user-input/main.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
contrib/vhost-user-input/main.c +2 -2
index 449fd2171a..ef4b7769f2 100644
@@ -77,7 +77,7 @@ static void vi_input_send(VuInput *vi, struct virtio_input_event *event)
          len = iov_from_buf(elem->in_sg, elem->in_num,
                             0, &vi->queue[i].event, sizeof(virtio_input_event));
          vu_queue_push(dev, vq, elem, len);
-        g_free(elem);
+        free(elem);
      }
 
      vu_queue_notify(&vi->dev.parent, vq);
@@ -153,7 +153,7 @@ static void vi_handle_sts(VuDev *dev, int qidx)
                           0, &event, sizeof(event));
          vi_handle_status(vi, &event);
          vu_queue_push(dev, vq, elem, len);
-        g_free(elem);
+        free(elem);
      }
 
      vu_queue_notify(&vi->dev.parent, vq);
#3 lore raw
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Stefan Hajnoczi

The virtqueue element returned by vu_queue_pop() is allocated using malloc(3) by virtqueue_alloc_element(). Use the matching free(3) function instead of glib's g_free().

Fixes 06914c97d3a ?

Good idea, I should have included that.

Stefan

#4 lore raw
Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Stefan Hajnoczi

The virtqueue element returned by vu_queue_pop() is allocated using malloc(3) by virtqueue_alloc_element(). Use the matching free(3) function instead of glib's g_free().

Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Fixes 06914c97d3a ?

Good idea, I should have included that.

Stefan

I'd prefer keeping the Fixes tag to bugfixes as opposed to cleanups. I.e. it should be a reasonable heuristic for people asking "do I need this patch" to be able to answer based on whether they have the linked patch.

Signature · 1 line
MST
#5 lore raw
In reply to Stefan Hajnoczi
Quoted 3 lines · Stefan Hajnoczi

The virtqueue element returned by vu_queue_pop() is allocated using malloc(3) by virtqueue_alloc_element(). Use the matching free(3) function instead of glib's g_free().

Just as an FYI, since glib 2.46 g_malloc is hardcoded to use the system allocator, so it is now guaranteed that g_malloc/malloc and g_free/free are safely interchangable. I recently got this clarified in the glib docs:

https://gitlab.gnome.org/GNOME/glib/merge_requests/1099//diffs

QEMU mandates 2.48 so we are now safe in that regard

For readability/sanity sake I'd still suggest matching functions but it is not a functional danger any more. Even when it was a risk, that risk only arose if you called GLib's API for installing a custom allocator callback which QEMU never did, so it was always a non-issue.

Where this is most helpful is in exchanging allocated data with 3rd party libraries that don't use glib. We no longer have to worry about dup'ing memory going in/out libraries not using glib's allocators.

Quoted 32 lines
---
 contrib/vhost-user-input/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
contrib/vhost-user-input/main.c +2 -2
index 449fd2171a..ef4b7769f2 100644
@@ -77,7 +77,7 @@ static void vi_input_send(VuInput *vi, struct virtio_input_event *event)
         len = iov_from_buf(elem->in_sg, elem->in_num,
                            0, &vi->queue[i].event, sizeof(virtio_input_event));
         vu_queue_push(dev, vq, elem, len);
-        g_free(elem);
+        free(elem);
     }
 
     vu_queue_notify(&vi->dev.parent, vq);
@@ -153,7 +153,7 @@ static void vi_handle_sts(VuDev *dev, int qidx)
                          0, &event, sizeof(event));
         vi_handle_status(vi, &event);
         vu_queue_push(dev, vq, elem, len);
-        g_free(elem);
+        free(elem);
     }
 
     vu_queue_notify(&vi->dev.parent, vq);
Signature · 1 line
2.23.0

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw
Quoted 12 lines · Daniel P. Berrangé
Quoted 3 lines · Stefan Hajnoczi

The virtqueue element returned by vu_queue_pop() is allocated using malloc(3) by virtqueue_alloc_element(). Use the matching free(3) function instead of glib's g_free().

Just as an FYI, since glib 2.46 g_malloc is hardcoded to use the system allocator, so it is now guaranteed that g_malloc/malloc and g_free/free are safely interchangable. I recently got this clarified in the glib docs:

https://gitlab.gnome.org/GNOME/glib/merge_requests/1099//diffs

Quoted 7 lines

QEMU mandates 2.48 so we are now safe in that regard

For readability/sanity sake I'd still suggest matching functions but it is not a functional danger any more. Even when it was a risk, that risk only arose if you called GLib's API for installing a custom allocator callback which QEMU never did, so it was always a non-issue.

You are right, although QEMU did use g_mem_set_vtable(). The custom functions still used malloc() underneath though so it would be safe anyway:

commit 98cf48f60aa4999f5b2808569a193a401a390e6a
Author: Paolo Bonzini <[email protected]>
Date: Wed Sep 16 17:38:44 2015 +0200

trace: remove malloc tracing