libvhost-user-glib: fix VugDev main fd cleanup
PATCH v3 1/1 libvhost-user-glib: fix VugDev main fd cleanup
From: Johannes Berg <[email protected]>
If you try to make a device implementation that can handle multiple connections and allow disconnections (which requires overriding the VHOST_USER_NONE handling), then glib will warn that we remove a src while it's still on the mainloop, and will poll() an FD that doesn't exist anymore.
Fix this by making vug_source_new() require pairing with the new vug_source_destroy() so we can keep the GSource referenced in the meantime.
Note that this requires calling the new API in vhost-user-input. vhost-user-gpu also uses vug_source_new(), but never seems to free the result at all, so I haven't changed anything there.
--- contrib/libvhost-user/libvhost-user-glib.c | 15 ++++++++++++--- contrib/libvhost-user/libvhost-user-glib.h | 1 + contrib/vhost-user-input/main.c | 6 ++---- 3 files changed, 15 insertions(+), 7 deletions(-)
contrib/libvhost-user/libvhost-user-glib.c +12 -3
@@ -91,7 +91,6 @@ vug_source_new(VugDev *gdev, int fd, GIOCondition cond,g_source_add_poll (gsrc, &src->gfd); id =g_source_attach (gsrc,NULL );g_assert (id); -g_source_unref (gsrc);return gsrc; }
@@ -131,6 +130,16 @@ static void vug_watch(VuDev *dev, int condition, void *data) } } +void vug_source_destroy (GSource *src) +{ +if (!src) { +return ; + } + +g_source_unref (src); +g_source_destroy (src); +} +bool vug_init (VugDev *dev,uint16_t max_queues,int socket,vu_panic_cb panic,const VuDevIface *iface)
@@ -144,7 +153,7 @@ vug_init(VugDev *dev, uint16_t max_queues, int socket, } dev->fdmap =g_hash_table_new_full (NULL ,NULL ,NULL , - (GDestroyNotify ) g_source_destroy); + (GDestroyNotify ) vug_source_destroy); dev->src =vug_source_new (dev, socket,G_IO_IN , vug_watch,NULL );
@@ -157,5 +166,5 @@ vug_deinit(VugDev *dev)g_assert (dev);g_hash_table_unref (dev->fdmap); -g_source_unref (dev->src); +vug_source_destroy (dev->src); }
contrib/libvhost-user/libvhost-user-glib.h +1 -0
@@ -31,5 +31,6 @@ void vug_deinit(VugDev *dev);GSource *vug_source_new (VugDev *dev,int fd,GIOCondition cond,vu_watch_cb vu_cb,gpointer data); +void vug_source_destroy (GSource *src);#endif /* LIBVHOST_USER_GLIB_H */
contrib/vhost-user-input/main.c +2 -4
@@ -187,7 +187,7 @@ vi_queue_set_started(VuDev *dev, int qidx, bool started) }if (!started && vi->evsrc) { -g_source_destroy (vi->evsrc); +vug_source_destroy (vi->evsrc); vi->evsrc =NULL ; } }
@@ -401,9 +401,7 @@ main(int argc, char *argv[])vug_deinit (&vi.dev); -if (vi.evsrc) { -g_source_unref (vi.evsrc); - } +vugg_source_destroy (vi.evsrc);g_array_free (vi.config, TRUE);g_free (vi.queue);return 0 ;
Signature · 1 line
2.23.0
Hi
Quoted 39 lines · Johannes Berg
From: Johannes Berg <[email protected]>
If you try to make a device implementation that can handle multiple connections and allow disconnections (which requires overriding the
VHOST_USER_NONEhandling), then glib will warn that we remove a src while it's still on the mainloop, and willpoll()an FD that doesn't exist anymore.Fix this by making
vug_source_new()require pairing with the newvug_source_destroy()so we can keep the GSource referenced in the meantime.Note that this requires calling the new API in vhost-user-input. vhost-user-gpu also uses
vug_source_new(), but never seems to free the result at all, so I haven't changed anything there.Fixes 8bb7ddb78a1c ("libvhost-user: add glib source helper")Signed-off-by Johannes Berg <[email protected]>--- contrib/libvhost-user/libvhost-user-glib.c | 15 ++++++++++++--- contrib/libvhost-user/libvhost-user-glib.h | 1 + contrib/vhost-user-input/main.c | 6 ++---- 3 files changed, 15 insertions(+), 7 deletions(-)contrib/libvhost-user/libvhost-user-glib.c +0
-1@@ -91,7 +91,6 @@ vug_source_new(VugDev *gdev, int fd, GIOCondition cond,g_source_add_poll (gsrc, &src->gfd); id =g_source_attach (gsrc,NULL );g_assert (id); -g_source_unref (gsrc);return gsrc; }@@ -131,6 +130,16 @@ static void vug_watch(VuDev *dev, int condition, void *data) }
contrib/libvhost-user/libvhost-user-glib.c static void vug_watch(VuDev *dev, int condition, void *data) · line 140
Quoted 10 lines
contrib/libvhost-user/libvhost-user-glib.c +8
-0} +void vug_source_destroy (GSource *src) +{ +if (!src) { +return ; + } + +g_source_unref (src); +g_source_destroy (src);
I would call destroy() (detach) before unref().
Quoted 46 lines
contrib/libvhost-user/libvhost-user-glib.c +4
-2+} +bool vug_init (VugDev *dev,uint16_t max_queues,int socket,vu_panic_cb panic,const VuDevIface *iface)@@ -144,7 +153,7 @@ vug_init(VugDev *dev, uint16_t max_queues, int socket, } dev->fdmap =g_hash_table_new_full (NULL ,NULL ,NULL , - (GDestroyNotify ) g_source_destroy); + (GDestroyNotify ) vug_source_destroy); dev->src =vug_source_new (dev, socket,G_IO_IN , vug_watch,NULL );@@ -157,5 +166,5 @@ vug_deinit(VugDev *dev)g_assert (dev);g_hash_table_unref (dev->fdmap); -g_source_unref (dev->src); +vug_source_destroy (dev->src); }contrib/libvhost-user/libvhost-user-glib.h +1
-0@@ -31,5 +31,6 @@ void vug_deinit(VugDev *dev);GSource *vug_source_new (VugDev *dev,int fd,GIOCondition cond,vu_watch_cb vu_cb,gpointer data); +void vug_source_destroy (GSource *src);#endif /* LIBVHOST_USER_GLIB_H */ contrib/vhost-user-input/main.c +1
-1@@ -187,7 +187,7 @@ vi_queue_set_started(VuDev *dev, int qidx, bool started) }if (!started && vi->evsrc) { -g_source_destroy (vi->evsrc); +vug_source_destroy (vi->evsrc); vi->evsrc =NULL ; } }
contrib/vhost-user-input/main.c main(int argc, char *argv[]) · line 404
Quoted 7 lines
contrib/vhost-user-input/main.c +1
-3@@ -401,9 +401,7 @@ main(int argc, char *argv[])vug_deinit (&vi.dev); -if (vi.evsrc) { -g_source_unref (vi.evsrc); - } +vugg_source_destroy (vi.evsrc);
typo
contrib/vhost-user-input/main.c main(int argc, char *argv[]) · line 407
Quoted 3 lines
contrib/vhost-user-input/main.c +0
-0g_array_free (vi.config, TRUE);g_free (vi.queue);return 0 ;
vhost-user-input calls g_source_destroy(), please replace it too with vug_source_destroy().
vhost-user-gpu "renderer_source" leaks, not this patch fault, but worth to mention in the commit. Feel free to add a second commit to fix that too.
Hi,
Quoted 4 lines
contrib/libvhost-user/libvhost-user-glib.c static void vug_watch(VuDev *dev, int condition, void *data) · line 140
Quoted 2 lines
contrib/libvhost-user/libvhost-user-glib.c +2
-0+g_source_unref (src); +g_source_destroy (src);I would call
destroy()(detach) beforeunref().
Fair enough. I think there's an internal reference, but it's probably safer that way.
Quoted 3 lines
Quoted 1 line
+
vugg_source_destroy(vi.evsrc);typo
Oops. I thought I compiled it, I guess I didn't, fixed.
Quoted 6 lines
Quoted 3 lines
g_array_free(vi.config, TRUE); g_free(vi.queue); return 0;vhost-user-input calls
g_source_destroy(), please replace it too withvug_source_destroy().
That's what the patch above was, apart from the typo :-)
Quoted 2 lines
vhost-user-gpu "
renderer_source" leaks, not this patch fault, but worth to mention in the commit.
I did:
Quoted 4 lines
Quoted 2 lines
vhost-user-gpu also uses
vug_source_new(), but never seems to free the result at all, so I haven't changed anything there.Feel free to add a second commit to fix that too.
I looked at it briefly but couldn't unwind the paths, sorry.
johannes