From nobody Wed Nov 12 00:12:53 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1566910212; cv=none; d=zoho.com; s=zohoarc; b=YZTzfh+IfBlmwzguCO8B3zBiVZH5xKFOTPfIeYaxWWSOH8JEJO6sKqC5vwe7QzsEMpcuGqW0PIt8pphj0YzxD7oPgG6toq+iPilq6nCgfOHo5x6357NMZigoWhBNef88RVROsnJmuNKUqeMbzI4v9VBCNPmBGs8DqL5TCzQfY18= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566910212; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=+aK606t9LAk6Lz2XUg4rbwX8IaUktKmMAgSyYHbJjv4=; b=c5Jvjylz760erQ0zH3eKR5aeRDub5WAt+ggmHZiT0keI9XkmBz82qtjPzQaHSyo8RSU0H8tFEAYCUgAV8iwE3x2YmnYzP0rI2BydrQARaGf2vu3NEGRBrUJfa+EKlj4pCEzSGz+Bt6R6C4wUwSDFRTysHyoa2zk+ns/zh5Ze+2s= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1566910212422557.5391176811106; Tue, 27 Aug 2019 05:50:12 -0700 (PDT) Received: from localhost ([::1]:51174 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i2avF-0001Rn-D5 for importer@patchew.org; Tue, 27 Aug 2019 08:50:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53957) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i2auU-0000wc-5A for qemu-devel@nongnu.org; Tue, 27 Aug 2019 08:49:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i2auS-00016u-Ts for qemu-devel@nongnu.org; Tue, 27 Aug 2019 08:49:18 -0400 Received: from s3.sipsolutions.net ([2a01:4f8:191:4433::2]:45912 helo=sipsolutions.net) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i2auS-000163-NH for qemu-devel@nongnu.org; Tue, 27 Aug 2019 08:49:16 -0400 Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1i2auN-0008R3-Gk; Tue, 27 Aug 2019 14:49:11 +0200 From: Johannes Berg To: qemu-devel@nongnu.org Date: Tue, 27 Aug 2019 15:49:09 +0300 Message-Id: <20190827124909.16824-1-johannes@sipsolutions.net> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a01:4f8:191:4433::2 Subject: [Qemu-devel] [PATCH v3] libvhost-user-glib: fix VugDev main fd cleanup X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Johannes Berg Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Johannes Berg 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. Fixes: 8bb7ddb78a1c ("libvhost-user: add glib source helper") Signed-off-by: Johannes Berg --- 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(-) diff --git a/contrib/libvhost-user/libvhost-user-glib.c b/contrib/libvhost-= user/libvhost-user-glib.c index 99edd2f3de45..eaf88b8aa749 100644 --- a/contrib/libvhost-user/libvhost-user-glib.c +++ b/contrib/libvhost-user/libvhost-user-glib.c @@ -91,7 +91,6 @@ vug_source_new(VugDev *gdev, int fd, GIOCondition cond, g_source_add_poll(gsrc, &src->gfd); id =3D g_source_attach(gsrc, NULL); g_assert(id); - g_source_unref(gsrc); =20 return gsrc; } @@ -131,6 +130,16 @@ static void vug_watch(VuDev *dev, int condition, void = *data) } } =20 +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, } =20 dev->fdmap =3D g_hash_table_new_full(NULL, NULL, NULL, - (GDestroyNotify) g_source_destroy); + (GDestroyNotify) vug_source_destroy= ); =20 dev->src =3D vug_source_new(dev, socket, G_IO_IN, vug_watch, NULL); =20 @@ -157,5 +166,5 @@ vug_deinit(VugDev *dev) g_assert(dev); =20 g_hash_table_unref(dev->fdmap); - g_source_unref(dev->src); + vug_source_destroy(dev->src); } diff --git a/contrib/libvhost-user/libvhost-user-glib.h b/contrib/libvhost-= user/libvhost-user-glib.h index 64d539d93aba..1a79a4916ef2 100644 --- a/contrib/libvhost-user/libvhost-user-glib.h +++ b/contrib/libvhost-user/libvhost-user-glib.h @@ -31,5 +31,6 @@ void vug_deinit(VugDev *dev); =20 GSource *vug_source_new(VugDev *dev, int fd, GIOCondition cond, vu_watch_cb vu_cb, gpointer data); +void vug_source_destroy(GSource *src); =20 #endif /* LIBVHOST_USER_GLIB_H */ diff --git a/contrib/vhost-user-input/main.c b/contrib/vhost-user-input/mai= n.c index 449fd2171a5a..7d6b0f9d80cc 100644 --- a/contrib/vhost-user-input/main.c +++ b/contrib/vhost-user-input/main.c @@ -187,7 +187,7 @@ vi_queue_set_started(VuDev *dev, int qidx, bool started) } =20 if (!started && vi->evsrc) { - g_source_destroy(vi->evsrc); + vug_source_destroy(vi->evsrc); vi->evsrc =3D NULL; } } @@ -401,9 +401,7 @@ main(int argc, char *argv[]) =20 vug_deinit(&vi.dev); =20 - 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; --=20 2.23.0