From nobody Mon Feb 9 20:35:06 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=1579767523; cv=none; d=zohomail.com; s=zohoarc; b=dXI7FsqrqCP2u+jiUaDJMb2RNaUu7ecBcvahQfbR+/3WKSBNhJLGjfH3s3yaIBLOKIpQST5T9/u68UW4B5HK8cnvhaLnaoBgP6z73nVtKnjzlU0MBxF8PBEgKwILS+WNv7KioQFWSifAqbOFvWLCjc2ZJCQ10SwQ4l+vLuDAxhc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1579767523; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FKhys9QfeqMlX6fJTd37kfsD5zoYjdjs45z5CM3B2Oc=; b=ELcSiet+diKttF3EELYAmxLLC27sngWxc0FLju+EUBCsdNQj9kdRd9/zCvFb+wqgdPd6ekZiGpob+hBJ90sToMI1Wjic5Ex6hs1u2iDxqoGmRtJRhJ56ectjZdPi79tjd1JPLTey3gQGhGeyVwM7NP9Y0TEvY78qH0Gbh/Z7Gh4= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 1579767523716944.1658396286866; Thu, 23 Jan 2020 00:18:43 -0800 (PST) Received: from localhost ([::1]:52728 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iuXhK-0003Vh-EL for importer@patchew.org; Thu, 23 Jan 2020 03:18:42 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:53329) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iuXfw-00020b-TB for qemu-devel@nongnu.org; Thu, 23 Jan 2020 03:17:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iuXfv-0005l8-Jn for qemu-devel@nongnu.org; Thu, 23 Jan 2020 03:17:16 -0500 Received: from s3.sipsolutions.net ([2a01:4f8:191:4433::2]:35496 helo=sipsolutions.net) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iuXfv-0005im-CQ for qemu-devel@nongnu.org; Thu, 23 Jan 2020 03:17:15 -0500 Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) (envelope-from ) id 1iuXft-00FGF5-01; Thu, 23 Jan 2020 09:17:13 +0100 From: Johannes Berg To: qemu-devel@nongnu.org Subject: [PATCH v5 2/6] libvhost-user-glib: fix VugDev main fd cleanup Date: Thu, 23 Jan 2020 09:17:04 +0100 Message-Id: <20200123081708.7817-3-johannes@sipsolutions.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200123081708.7817-1-johannes@sipsolutions.net> References: <20200123081708.7817-1-johannes@sipsolutions.net> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 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 , mst@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" 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") Reviewed-by: Marc-Andr=C3=A9 Lureau 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..824c7780de61 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_destroy(src); + g_source_unref(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 ef4b7769f285..6020c6f33a46 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); - } + vug_source_destroy(vi.evsrc); g_array_free(vi.config, TRUE); g_free(vi.queue); return 0; --=20 2.24.1