From nobody Sun Nov 16 01:08:08 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1762201910; cv=none; d=zohomail.com; s=zohoarc; b=gQVLrD5s3q9L2JylmAYbFoGNRKtfIWxro0yKWzeZX9SFE6S/xxkkNpy7EHxsyontmxXemX8OsmALpluUqmh4LNqWcEUGP3rrNzJAlf6iLg0O/W5UHTiv1xIgsZFDPMht7XlwtPa+i5BK1o6MpVWZ8oAsSZYWuw6pTAGhdeTdIMI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1762201910; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=8sKETWfJjHc4CXcUbADX9wTUC00+MGTXyf9La3ddVEY=; b=U4T/3VNFpzT30mOqgF+3Yfyft8K7ij+Bu3+QAHF95c+2Ni1Dw40aOtZiZPHVyIQqrYJcojziV0P3JR4rs5lYdCxqfAM1r6dt8nHJSZWIrJ/sEo4qUMQHAHVrwp8nvDmJXhtmt9GbCsLibvwEPc/+DZfKbW/aYyP+tIDXE/zvDCg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1762201910719587.7197308907056; Mon, 3 Nov 2025 12:31:50 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vG1B5-0003tY-Ju; Mon, 03 Nov 2025 15:29:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1As-0003gi-BU for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:10 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1Ak-0007e0-5L for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:09 -0500 Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-209-g6zC2akXNiKPQ2F4Z54xrA-1; Mon, 03 Nov 2025 15:28:57 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 78D871956063; Mon, 3 Nov 2025 20:28:56 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.131]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6224030001A1; Mon, 3 Nov 2025 20:28:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1762201738; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8sKETWfJjHc4CXcUbADX9wTUC00+MGTXyf9La3ddVEY=; b=gRhyk4OH+RH4rhBsc+jeW5y9sXugwzSzz3FUWQodNmRAAgxSYkgMl2aDNsSumWBjQXMgcB RSx9IxXVnANIWehTXpeOaG5rBCrLH1lSi5v2zHL9NgxkPzUhAsEMTPn/dXKATze61Mkq+Y KRCaTYcKzHdI/IaGPqPgsdvFrDGpO4Y= X-MC-Unique: g6zC2akXNiKPQ2F4Z54xrA-1 X-Mimecast-MFC-AGG-ID: g6zC2akXNiKPQ2F4Z54xrA_1762201736 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, berrange@redhat.com, kwolf@redhat.com Subject: [PATCH 1/8] qio: Add trace points to net_listener Date: Mon, 3 Nov 2025 14:10:52 -0600 Message-ID: <20251103202849.3687643-11-eblake@redhat.com> In-Reply-To: <20251103202849.3687643-10-eblake@redhat.com> References: <20251103202849.3687643-10-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1762201922007158500 Content-Type: text/plain; charset="utf-8" Upcoming patches will adjust how net_listener watches for new client connections; adding trace points now makes it easier to debug that the changes work as intended. For example, adding --trace=3D'qio_net_listener*' to the qemu-storage-daemon command line before --nbd-server will track when the server first starts listening for clients. Signed-off-by: Eric Blake Reviewed-by: Daniel P. Berrang=C3=A9 --- io/net-listener.c | 17 +++++++++++++++++ io/trace-events | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/io/net-listener.c b/io/net-listener.c index 47405965a66..0adbc409cf2 100644 --- a/io/net-listener.c +++ b/io/net-listener.c @@ -23,6 +23,7 @@ #include "io/dns-resolver.h" #include "qapi/error.h" #include "qemu/module.h" +#include "trace.h" QIONetListener *qio_net_listener_new(void) { @@ -50,6 +51,7 @@ static gboolean qio_net_listener_channel_func(QIOChannel = *ioc, return TRUE; } + trace_qio_net_listener_callback(listener, listener->io_func); if (listener->io_func) { listener->io_func(listener, sioc, listener->io_data); } @@ -124,6 +126,8 @@ void qio_net_listener_add(QIONetListener *listener, listener->connected =3D true; if (listener->io_func !=3D NULL) { + trace_qio_net_listener_watch_enabled(listener, listener->io_func, + "add"); object_ref(OBJECT(listener)); listener->io_source[listener->nsioc] =3D qio_channel_add_watch_sou= rce( QIO_CHANNEL(listener->sioc[listener->nsioc]), G_IO_IN, @@ -143,6 +147,9 @@ void qio_net_listener_set_client_func_full(QIONetListen= er *listener, { size_t i; + if (listener->io_func) { + trace_qio_net_listener_watch_disabled(listener, "set_client_func"); + } if (listener->io_notify) { listener->io_notify(listener->io_data); } @@ -159,6 +166,8 @@ void qio_net_listener_set_client_func_full(QIONetListen= er *listener, } if (listener->io_func !=3D NULL) { + trace_qio_net_listener_watch_enabled(listener, listener->io_func, + "set_client_func"); for (i =3D 0; i < listener->nsioc; i++) { object_ref(OBJECT(listener)); listener->io_source[i] =3D qio_channel_add_watch_source( @@ -218,6 +227,9 @@ QIOChannelSocket *qio_net_listener_wait_client(QIONetLi= stener *listener) }; size_t i; + if (listener->io_func) { + trace_qio_net_listener_watch_disabled(listener, "wait_client"); + } for (i =3D 0; i < listener->nsioc; i++) { if (listener->io_source[i]) { g_source_destroy(listener->io_source[i]); @@ -248,6 +260,8 @@ QIOChannelSocket *qio_net_listener_wait_client(QIONetLi= stener *listener) g_main_context_unref(ctxt); if (listener->io_func !=3D NULL) { + trace_qio_net_listener_watch_enabled(listener, listener->io_func, + "wait_client"); for (i =3D 0; i < listener->nsioc; i++) { object_ref(OBJECT(listener)); listener->io_source[i] =3D qio_channel_add_watch_source( @@ -268,6 +282,9 @@ void qio_net_listener_disconnect(QIONetListener *listen= er) return; } + if (listener->io_func) { + trace_qio_net_listener_watch_disabled(listener, "disconnect"); + } for (i =3D 0; i < listener->nsioc; i++) { if (listener->io_source[i]) { g_source_destroy(listener->io_source[i]); diff --git a/io/trace-events b/io/trace-events index dc3a63ba1fb..8cc4cae3a5d 100644 --- a/io/trace-events +++ b/io/trace-events @@ -72,3 +72,8 @@ qio_channel_command_new_pid(void *ioc, int writefd, int r= eadfd, int pid) "Comman qio_channel_command_new_spawn(void *ioc, const char *binary, int flags) "C= ommand new spawn ioc=3D%p binary=3D%s flags=3D%d" qio_channel_command_abort(void *ioc, int pid) "Command abort ioc=3D%p pid= =3D%d" qio_channel_command_wait(void *ioc, int pid, int ret, int status) "Command= abort ioc=3D%p pid=3D%d ret=3D%d status=3D%d" + +# net-listener.c +qio_net_listener_watch_enabled(void *listener, void *func, const char *ext= ra) "Net listener=3D%p watch enabled func=3D%p by %s" +qio_net_listener_watch_disabled(void *listener, const char *extra) "Net li= stener=3D%p watch disabled by %s" +qio_net_listener_callback(void *listener, void *func) "Net listener=3D%p c= allback forwarding to func=3D%p" --=20 2.51.1 From nobody Sun Nov 16 01:08:08 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1762201770; cv=none; d=zohomail.com; s=zohoarc; b=ND+ImLOumWDKm4gKnsexvV3q0nZHbXExRHoOXpnY7hCun3qA+LcJxaQUVbbs9jztT1+LLTgX5O9ydnZouzfmmsgcO6Jk6oXMLiLE6/yMmBDlRiFsuZo4QW92/1aYT/GK62a3DVJepE2sSd32WZMfkgWQZ0BEJb+vDDq454EvlxI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1762201770; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=HWzTGc3AvYUES9HKYfYuHSoNeIyyzbhENNJcxTClcds=; b=XBMWsrjohpGnE8JGP/ghGvfJIFG0oPWvspEwmo0a7IvRpAT+z8LiLO8NBdsg1muX4KvKElBmywD5uiqQTasXJSFcX8ZOnwGPf9e9Zwo0G9NJO/GOxSoZHRC3yRlR5gfk42B4ttqIu8d1vK4iw4tgRFk8RfWGihurL78jkdRSB2U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 176220177056285.40322343251614; Mon, 3 Nov 2025 12:29:30 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vG1At-0003iS-FE; Mon, 03 Nov 2025 15:29:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1An-0003ZR-0O for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:05 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1Ak-0007eP-Bx for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:04 -0500 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-381-_ZorKTfSNQKk-sl1pHfREg-1; Mon, 03 Nov 2025 15:28:59 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5EC8C180057A; Mon, 3 Nov 2025 20:28:58 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.131]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 19F5E30001A1; Mon, 3 Nov 2025 20:28:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1762201740; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HWzTGc3AvYUES9HKYfYuHSoNeIyyzbhENNJcxTClcds=; b=TUWNPyBFrcRDZ/UOhqcutwdfSQGzFyqPxkc7vxkIrorbp/30JcjsbI17omWko7UFmfC7Y3 E0EkXpKUDR/jOSKLhvyVR9vDwpEMZxv5z6SJs7WvZeRYsK+NO+0BJoNN2/bStXEm9zaYAW lhP7w+9qB3VHMIG0nSVou3gYnobtZJo= X-MC-Unique: _ZorKTfSNQKk-sl1pHfREg-1 X-Mimecast-MFC-AGG-ID: _ZorKTfSNQKk-sl1pHfREg_1762201738 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, berrange@redhat.com, kwolf@redhat.com Subject: [PATCH 2/8] qio: Minor optimization when callback function is unchanged Date: Mon, 3 Nov 2025 14:10:53 -0600 Message-ID: <20251103202849.3687643-12-eblake@redhat.com> In-Reply-To: <20251103202849.3687643-10-eblake@redhat.com> References: <20251103202849.3687643-10-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1762201776804158500 Content-Type: text/plain; charset="utf-8" In qemu-nbd and other NBD server setups where parallel clients are supported, it is common that the caller will re-register the same callback function as long as it has not reached its limit on simultaneous clients. In that case, there is no need to tear down and reinstall GSource watches in the GMainContext. Signed-off-by: Eric Blake Reviewed-by: Daniel P. Berrang=C3=A9 --- io/net-listener.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/io/net-listener.c b/io/net-listener.c index 0adbc409cf2..e89286ea63c 100644 --- a/io/net-listener.c +++ b/io/net-listener.c @@ -147,6 +147,10 @@ void qio_net_listener_set_client_func_full(QIONetListe= ner *listener, { size_t i; + if (listener->io_func =3D=3D func && listener->io_data =3D=3D data) { + return; + } + if (listener->io_func) { trace_qio_net_listener_watch_disabled(listener, "set_client_func"); } --=20 2.51.1 From nobody Sun Nov 16 01:08:08 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1762201865; cv=none; d=zohomail.com; s=zohoarc; b=VSU6a5LUrgi5jygdQ+gWeJu6M96LTIyJj/lUXha8wEaqIww1xKNbOa5R1cVjEVkP8X6RxUi+8/9c400HBuI7z91JMxxphMoJF2+suHbCgwCnKPaEveW+scsY03C5zoOQYvGb15yz9adSBLORXfl1L6C0mcgSVbDq0TU4raYEgYc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1762201865; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=1HL5ifWhqGLNT78hocwYD/4hVKCIU/hgFQH4mWeG4lg=; b=iTLvY6KPrVgYyEgG5NvafnDBnZvrLLiGQyjF8WhGZn7pv3f0x66trvVZOVNJ1nAnG791IO9hNO7GYa0B797bz/XnPraAutBlrQEfeudTq6f4EUFbywdxzInDwfA27q7xV+vchTPWvEff3YcLSwLFghEDBp6na7x0BTYmDKeRzcI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1762201865179316.7108308507044; Mon, 3 Nov 2025 12:31:05 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vG1Ax-0003lO-8H; Mon, 03 Nov 2025 15:29:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1Ar-0003ei-Oi for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:09 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1Am-0007er-RY for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:09 -0500 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-467-AucUEJcvOHGC_2DQxR1q2A-1; Mon, 03 Nov 2025 15:29:00 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1772218002E4; Mon, 3 Nov 2025 20:29:00 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.131]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C883F30001A1; Mon, 3 Nov 2025 20:28:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1762201742; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1HL5ifWhqGLNT78hocwYD/4hVKCIU/hgFQH4mWeG4lg=; b=f9TdfEVsiCOOab8cYVE9JwuKRQGA6o2Z6IrJ12inuvRlD0V2lIDOXc1Pi4dGOavJhrPQJt b1Yzk7R9zqJwlGTJlxYclPeQeaS97QrIMKMtFmWU277LIbaO7hLHjC/npOjCcIzYDYhXTO rWPKujq4oHxnjBuWNVYYWZ7f3JJ3aOQ= X-MC-Unique: AucUEJcvOHGC_2DQxR1q2A-1 X-Mimecast-MFC-AGG-ID: AucUEJcvOHGC_2DQxR1q2A_1762201740 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, berrange@redhat.com, kwolf@redhat.com Subject: [PATCH 3/8] qio: Remember context of qio_net_listener_set_client_func_full Date: Mon, 3 Nov 2025 14:10:54 -0600 Message-ID: <20251103202849.3687643-13-eblake@redhat.com> In-Reply-To: <20251103202849.3687643-10-eblake@redhat.com> References: <20251103202849.3687643-10-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1762201867392154100 Content-Type: text/plain; charset="utf-8" io/net-listener.c has two modes of use: asynchronous (the user calls qio_net_listener_set_client_func to wake up the callback via the global GMainContext, or qio_net_listener_set_client_func_full to wake up the callback via the caller's own alternative GMainContext), and synchronous (the user calls qio_net_listener_wait_client which creates its own GMainContext and waits for the first client connection before returning, with no need for a user's callback). But commit 938c8b79 ("qio: store gsources for net listeners", v2.12.0) has a latent logic flaw: when qio_net_listener_wait_client finishes on its temporary context, it reverts all of the siocs back to the global GMainContext rather than the potentially non-NULL context they might have been originally registered with. Similarly, if the user creates a net-listener, adds initial addresses, registers an async callback with a non-default context (which ties to all siocs for the initial addresses), then adds more addresses with qio_net_listener_add, the siocs for later addresses are blindly placed in the global context, rather than sharing the context of the earlier ones. In practice, I don't think this has caused issues. As pointed out by the original commit, all async callers prior to that commit were already okay with the NULL default context; and the typical usage pattern is to first add ALL the addresses the listener will pay attention to before ever setting the async callback. Likewise, if a file uses only qio_net_listener_set_client_func instead of qio_net_listener_set_client_func_full, then it is never using a custom context, so later assignments of async callbacks will still be to the same global context as earlier ones. Meanwhile, any callers that want to do the sync operation to grab the first client are unlikely to register an async callback; altogether bypassing the question of whether later assignments of a GSource are being tied to a different context over time. I do note that chardev/char-socket.c is the only file that calls both qio_net_listener_wait_client (sync for a single client in tcp_chr_accept_server_sync), and qio_net_listener_set_client_func_full (several places, all with chr->gcontext, but sometimes with a NULL callback function during teardown). But as far as I can tell, the two uses are mutually exclusive, based on the is_waitconnect parameter to qmp_chardev_open_socket_server. That said, it is more robust to remember when a callback function is tied to a non-default context, and have both the sync wait and any late address additions honor that same context. That way, the code will be robust even if a later user performs a sync wait for a specific client in the middle of servicing a longer-lived QIONetListener that has an async callback for all other clients. Signed-off-by: Eric Blake Reviewed-by: Daniel P. Berrang=C3=A9 --- include/io/net-listener.h | 1 + io/net-listener.c | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/io/net-listener.h b/include/io/net-listener.h index ab9f291ed62..42fbfab5467 100644 --- a/include/io/net-listener.h +++ b/include/io/net-listener.h @@ -50,6 +50,7 @@ struct QIONetListener { QIOChannelSocket **sioc; GSource **io_source; size_t nsioc; + GMainContext *context; bool connected; diff --git a/io/net-listener.c b/io/net-listener.c index e89286ea63c..15df673fb6e 100644 --- a/io/net-listener.c +++ b/io/net-listener.c @@ -132,7 +132,7 @@ void qio_net_listener_add(QIONetListener *listener, listener->io_source[listener->nsioc] =3D qio_channel_add_watch_sou= rce( QIO_CHANNEL(listener->sioc[listener->nsioc]), G_IO_IN, qio_net_listener_channel_func, - listener, (GDestroyNotify)object_unref, NULL); + listener, (GDestroyNotify)object_unref, listener->context); } listener->nsioc++; @@ -160,6 +160,7 @@ void qio_net_listener_set_client_func_full(QIONetListen= er *listener, listener->io_func =3D func; listener->io_data =3D data; listener->io_notify =3D notify; + listener->context =3D context; for (i =3D 0; i < listener->nsioc; i++) { if (listener->io_source[i]) { @@ -271,7 +272,7 @@ QIOChannelSocket *qio_net_listener_wait_client(QIONetLi= stener *listener) listener->io_source[i] =3D qio_channel_add_watch_source( QIO_CHANNEL(listener->sioc[i]), G_IO_IN, qio_net_listener_channel_func, - listener, (GDestroyNotify)object_unref, NULL); + listener, (GDestroyNotify)object_unref, listener->context); } } --=20 2.51.1 From nobody Sun Nov 16 01:08:08 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1762201815; cv=none; d=zohomail.com; s=zohoarc; b=XWP2av5PHZzpTgOJrO0H3e2XEwSPrZ9mV68XgkbGh0yZQLBpVFOxOgpazdWcv2jZARTO+Z0s59xoghQ0KhkeQXbTW9uV8l3ZzfJwyxatYmA5KigpXRMQlIav6gZbULdL3ddTUPNQgtdwi8yRlICk8kbabNdFuHAx32p8Ua1PGiA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1762201815; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=UvxQwy5WZQVUM812c4udxrousOO9CAupjtj9AVJC3SI=; b=kGMbge8mMVqOGJuazn7GAei1+jX3jkcQZTHx2iWTu5yRUs3cROLR8zyHSHcS/qUH5v1SfXCrccqjEY/VsB4Rn/41TgsxNIXFznZvyiqm1uoQVnl152nnnU+m18z1ZDey/pH9O/eJvfTr6tjWu99sqF/5b7elJX4c5WNAAeGiG0I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1762201815980384.7363833388704; Mon, 3 Nov 2025 12:30:15 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vG1Av-0003ja-UI; Mon, 03 Nov 2025 15:29:13 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1Ar-0003ej-RW for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:09 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1Ap-0007fZ-Dq for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:09 -0500 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-493-XynZli2uOgyAURo-5v_UfA-1; Mon, 03 Nov 2025 15:29:03 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7A7231805C33; Mon, 3 Nov 2025 20:29:02 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.131]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7F70030001A1; Mon, 3 Nov 2025 20:29:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1762201744; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UvxQwy5WZQVUM812c4udxrousOO9CAupjtj9AVJC3SI=; b=K+LeAlrvLa/FsXMb9T0CHYjW7jOaM8BrCURXYy0lyKZMH3apcGDK0VTVIONRPs75/7xmhr 6E2IfNFZRNaC+HCbx+kbz2Sr1UEVvpBlb/s6lynNuuz4MuDXF28pdK742ZX24K3lpgJbTn yu+HJ+TgkIUn0c6bTnBJqcJtfWbA29M= X-MC-Unique: XynZli2uOgyAURo-5v_UfA-1 X-Mimecast-MFC-AGG-ID: XynZli2uOgyAURo-5v_UfA_1762201742 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, berrange@redhat.com, kwolf@redhat.com Subject: [PATCH 4/8] qio: Factor out helpers qio_net_listener_[un]watch Date: Mon, 3 Nov 2025 14:10:55 -0600 Message-ID: <20251103202849.3687643-14-eblake@redhat.com> In-Reply-To: <20251103202849.3687643-10-eblake@redhat.com> References: <20251103202849.3687643-10-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1762201827122158500 Content-Type: text/plain; charset="utf-8" The code had three similar repetitions of an iteration over one or all of nsiocs to set up a GSource, and likewise for teardown. Since an upcoming patch wants to tweak whether GSource or AioContext is used, its better to consolidate that into one helper function for fewer places to edit later. Signed-off-by: Eric Blake Reviewed-by: Daniel P. Berrang=C3=A9 --- io/net-listener.c | 109 +++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 64 deletions(-) diff --git a/io/net-listener.c b/io/net-listener.c index 15df673fb6e..e1378b9a612 100644 --- a/io/net-listener.c +++ b/io/net-listener.c @@ -106,6 +106,45 @@ int qio_net_listener_open_sync(QIONetListener *listene= r, } } +/* + * i =3D=3D 0 to set watch on entire array, non-zero to only set watch on + * recent additions when earlier entries are already watched. + */ +static void +qio_net_listener_watch(QIONetListener *listener, size_t i, const char *cal= ler) +{ + if (!listener->io_func) { + return; + } + + trace_qio_net_listener_watch_enabled(listener, listener->io_func, call= er); + for ( ; i < listener->nsioc; i++) { + object_ref(OBJECT(listener)); + listener->io_source[i] =3D qio_channel_add_watch_source( + QIO_CHANNEL(listener->sioc[i]), G_IO_IN, + qio_net_listener_channel_func, + listener, (GDestroyNotify)object_unref, listener->context); + } +} + +static void +qio_net_listener_unwatch(QIONetListener *listener, const char *caller) +{ + size_t i; + + if (!listener->io_func) { + return; + } + + trace_qio_net_listener_watch_disabled(listener, caller); + for (i =3D 0; i < listener->nsioc; i++) { + if (listener->io_source[i]) { + g_source_destroy(listener->io_source[i]); + g_source_unref(listener->io_source[i]); + listener->io_source[i] =3D NULL; + } + } +} void qio_net_listener_add(QIONetListener *listener, QIOChannelSocket *sioc) @@ -125,17 +164,7 @@ void qio_net_listener_add(QIONetListener *listener, object_ref(OBJECT(sioc)); listener->connected =3D true; - if (listener->io_func !=3D NULL) { - trace_qio_net_listener_watch_enabled(listener, listener->io_func, - "add"); - object_ref(OBJECT(listener)); - listener->io_source[listener->nsioc] =3D qio_channel_add_watch_sou= rce( - QIO_CHANNEL(listener->sioc[listener->nsioc]), G_IO_IN, - qio_net_listener_channel_func, - listener, (GDestroyNotify)object_unref, listener->context); - } - - listener->nsioc++; + qio_net_listener_watch(listener, listener->nsioc++, "add"); } @@ -145,15 +174,11 @@ void qio_net_listener_set_client_func_full(QIONetList= ener *listener, GDestroyNotify notify, GMainContext *context) { - size_t i; - if (listener->io_func =3D=3D func && listener->io_data =3D=3D data) { return; } - if (listener->io_func) { - trace_qio_net_listener_watch_disabled(listener, "set_client_func"); - } + qio_net_listener_unwatch(listener, "set_client_func"); if (listener->io_notify) { listener->io_notify(listener->io_data); } @@ -162,25 +187,7 @@ void qio_net_listener_set_client_func_full(QIONetListe= ner *listener, listener->io_notify =3D notify; listener->context =3D context; - for (i =3D 0; i < listener->nsioc; i++) { - if (listener->io_source[i]) { - g_source_destroy(listener->io_source[i]); - g_source_unref(listener->io_source[i]); - listener->io_source[i] =3D NULL; - } - } - - if (listener->io_func !=3D NULL) { - trace_qio_net_listener_watch_enabled(listener, listener->io_func, - "set_client_func"); - for (i =3D 0; i < listener->nsioc; i++) { - object_ref(OBJECT(listener)); - listener->io_source[i] =3D qio_channel_add_watch_source( - QIO_CHANNEL(listener->sioc[i]), G_IO_IN, - qio_net_listener_channel_func, - listener, (GDestroyNotify)object_unref, context); - } - } + qio_net_listener_watch(listener, 0, "set_client_func"); } void qio_net_listener_set_client_func(QIONetListener *listener, @@ -232,16 +239,7 @@ QIOChannelSocket *qio_net_listener_wait_client(QIONetL= istener *listener) }; size_t i; - if (listener->io_func) { - trace_qio_net_listener_watch_disabled(listener, "wait_client"); - } - for (i =3D 0; i < listener->nsioc; i++) { - if (listener->io_source[i]) { - g_source_destroy(listener->io_source[i]); - g_source_unref(listener->io_source[i]); - listener->io_source[i] =3D NULL; - } - } + qio_net_listener_unwatch(listener, "wait_client"); sources =3D g_new0(GSource *, listener->nsioc); for (i =3D 0; i < listener->nsioc; i++) { @@ -264,17 +262,7 @@ QIOChannelSocket *qio_net_listener_wait_client(QIONetL= istener *listener) g_main_loop_unref(loop); g_main_context_unref(ctxt); - if (listener->io_func !=3D NULL) { - trace_qio_net_listener_watch_enabled(listener, listener->io_func, - "wait_client"); - for (i =3D 0; i < listener->nsioc; i++) { - object_ref(OBJECT(listener)); - listener->io_source[i] =3D qio_channel_add_watch_source( - QIO_CHANNEL(listener->sioc[i]), G_IO_IN, - qio_net_listener_channel_func, - listener, (GDestroyNotify)object_unref, listener->context); - } - } + qio_net_listener_watch(listener, 0, "wait_client"); return data.sioc; } @@ -287,15 +275,8 @@ void qio_net_listener_disconnect(QIONetListener *liste= ner) return; } - if (listener->io_func) { - trace_qio_net_listener_watch_disabled(listener, "disconnect"); - } + qio_net_listener_unwatch(listener, "disconnect"); for (i =3D 0; i < listener->nsioc; i++) { - if (listener->io_source[i]) { - g_source_destroy(listener->io_source[i]); - g_source_unref(listener->io_source[i]); - listener->io_source[i] =3D NULL; - } qio_channel_close(QIO_CHANNEL(listener->sioc[i]), NULL); } listener->connected =3D false; --=20 2.51.1 From nobody Sun Nov 16 01:08:08 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1762201827; cv=none; d=zohomail.com; s=zohoarc; b=diVu5pCmFrlOCFCrM+ec488slWfZjXl9S1gzsiCjyRyEau8x3Pq2uXJJcdoazaIYrh8PNKg5ECKqM3CiG/TBIwZA4yoxHylMc/JDqTMNINem/9ndo2vYwA54bnA4mOcxHH4V0bqYErHdmiSg1NTD22HlmGmI3p9uQurVPdLuGt4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1762201827; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NNHZkYLghkWJKpV9IcEk5Jr77yUchR1dS2rgF6gG7fU=; b=IsJcm6Y0/CQTBpjgIe/JCt1bD2tyKvbt8lsNVI5PnekchT1te2am+X0DttE4LJ78GB3Ag/qUxUJrZVwXCnogcP9b8BuVS2iUYRz+U08imTjQvRTr/yp4ooHaM/IbdkajPSDGY+GrgKj8mHUFfEOsHbN4b3DPubCUTdgiNr1uXMA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1762201827362386.8208700570668; Mon, 3 Nov 2025 12:30:27 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vG1B6-0003tg-TJ; Mon, 03 Nov 2025 15:29:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1Aw-0003lP-WC for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:15 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1Au-0007hQ-Nc for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:14 -0500 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-518--WRJh1mON3evFDGx2Xd5yg-1; Mon, 03 Nov 2025 15:29:07 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id ED626180121B; Mon, 3 Nov 2025 20:29:03 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.131]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 686F330002C3; Mon, 3 Nov 2025 20:29:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1762201750; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NNHZkYLghkWJKpV9IcEk5Jr77yUchR1dS2rgF6gG7fU=; b=MhSfa2jPuQmetfr4R/gMZpCfmYKmqH9sfIL9JWeO3SKxQpQ/yS7CW70gb44ak9IKvW54Nk 2RCHGp6b416nLj8n2R7ojJxmesP5ykWsSteHUHZt3wY/7tF1Lep2oZTSTC8c5T3pTglj1A sS0CS4kXMGUDleE5JCtak4QjE41MH+w= X-MC-Unique: -WRJh1mON3evFDGx2Xd5yg-1 X-Mimecast-MFC-AGG-ID: -WRJh1mON3evFDGx2Xd5yg_1762201745 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, berrange@redhat.com, kwolf@redhat.com Subject: [PATCH 5/8] qio: Let listening sockets remember their owning QIONetListener Date: Mon, 3 Nov 2025 14:10:56 -0600 Message-ID: <20251103202849.3687643-15-eblake@redhat.com> In-Reply-To: <20251103202849.3687643-10-eblake@redhat.com> References: <20251103202849.3687643-10-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1762201829055158500 Content-Type: text/plain; charset="utf-8" Make it easier to get from the sioc listening to a single address on behalf of a NetListener back to its owning object, which will be beneficial in an upcoming patch that teaches NetListener how to interact with AioContext. Signed-off-by: Eric Blake --- include/io/channel-socket.h | 1 + io/channel-socket.c | 1 + io/net-listener.c | 1 + 3 files changed, 3 insertions(+) diff --git a/include/io/channel-socket.h b/include/io/channel-socket.h index a88cf8b3a9f..eee686f3b4d 100644 --- a/include/io/channel-socket.h +++ b/include/io/channel-socket.h @@ -49,6 +49,7 @@ struct QIOChannelSocket { socklen_t remoteAddrLen; ssize_t zero_copy_queued; ssize_t zero_copy_sent; + struct QIONetListener *listener; }; diff --git a/io/channel-socket.c b/io/channel-socket.c index 712b793eaf2..59e929f97c3 100644 --- a/io/channel-socket.c +++ b/io/channel-socket.c @@ -65,6 +65,7 @@ qio_channel_socket_new(void) sioc->fd =3D -1; sioc->zero_copy_queued =3D 0; sioc->zero_copy_sent =3D 0; + sioc->listener =3D NULL; ioc =3D QIO_CHANNEL(sioc); qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN); diff --git a/io/net-listener.c b/io/net-listener.c index e1378b9a612..afdacdd5ff4 100644 --- a/io/net-listener.c +++ b/io/net-listener.c @@ -152,6 +152,7 @@ void qio_net_listener_add(QIONetListener *listener, if (listener->name) { qio_channel_set_name(QIO_CHANNEL(sioc), listener->name); } + sioc->listener =3D listener; listener->sioc =3D g_renew(QIOChannelSocket *, listener->sioc, listener->nsioc + 1); --=20 2.51.1 From nobody Sun Nov 16 01:08:08 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1762201816; cv=none; d=zohomail.com; s=zohoarc; b=G/lvFHRqD+tZYg1cd6ld+WA6vWqmP04hDKr5/vZO0LTPIUaAiffVmCJYUnCSLz9HVYsGhredPRVjBPKp/h7N0SIim2MU6dZs/ri3rJTx2Noh18+u1H+DpOH7tT2LjWhgNlZC6bgypuSAqJMt5B/0uyq3bOi5lI4bVaGUs+yq+TY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1762201816; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=3dCZJR2rhySQFOC9g1lQvWQEzMYUr96iWq8xrKN5xFU=; b=Jc8Kj9n/6WbhNdDXhGkNtsWZqspbR5Gzi6fTd3kVUBDdpFHHQU2wAur0XRIu0xnRxc9CDXgasF8LvdxWtX8yS7gzhc2cNnG8sJ8DMccEBIqtd15ELFAZDVum3aIiZKYk3U7P4zEd+L94ur6e+DBaMitzBCVUrgX4baFJrCuT9Pg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1762201816489420.38818799325054; Mon, 3 Nov 2025 12:30:16 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vG1B9-00040k-5l; Mon, 03 Nov 2025 15:29:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1B1-0003om-Be for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:19 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1Ax-0007id-Ik for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:18 -0500 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-294-jUV4eXSHOGW-3S-xPM_YyQ-1; Mon, 03 Nov 2025 15:29:08 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E4CA91855168; Mon, 3 Nov 2025 20:29:05 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.131]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 65CDD3000233; Mon, 3 Nov 2025 20:29:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1762201754; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3dCZJR2rhySQFOC9g1lQvWQEzMYUr96iWq8xrKN5xFU=; b=ga6nuqiqVz1zvNIteVKi6pe8MZXRTbSmM5qQ592C0OfzPDMyPlhXkWiv4CETcfBKcgcKyS oQyHQVrOE82ejHsEACutF7kZXunBqCANcjvP9q55Rqs0ZQUWi0iOq9i2Pi/Wk5Ijx2zzTn 006wyqnPR7uTJFSjwI4FTmZ2T8hu16I= X-MC-Unique: jUV4eXSHOGW-3S-xPM_YyQ-1 X-Mimecast-MFC-AGG-ID: jUV4eXSHOGW-3S-xPM_YyQ_1762201747 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, berrange@redhat.com, kwolf@redhat.com Subject: [PATCH 6/8] qio: Hoist ref of listener outside loop Date: Mon, 3 Nov 2025 14:10:57 -0600 Message-ID: <20251103202849.3687643-16-eblake@redhat.com> In-Reply-To: <20251103202849.3687643-10-eblake@redhat.com> References: <20251103202849.3687643-10-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1762201825226158500 Content-Type: text/plain; charset="utf-8" The point of QIONetListener is to allow a server to listen to more than one socket address at a time, and respond to clients in a first-come-first-serve order across any of those addresses. While some servers (like NBD) really do want to serve multiple simultaneous clients, many other servers only care about the first client to connect, and will immediately deregister the callback, possibly by dropping their reference to the QIONetListener. The existing code ensures that all other pending callbacks remain safe once the first callback drops the listener, by adding an extra reference to the listener for each GSource created, where those references pair to the eventual teardown of each GSource after a given callbacks has been serviced or aborted. But it is equally acceptable to hoist the reference to the listener outside the loop - as long as there is a callback function registered, it is sufficient to have a single reference live for the entire array of sioc, rather than one reference per sioc in the array. Hoisting the reference like this will make it easier for an upcoming patch to still ensure the listener cannot be prematurely garbage collected during the user's callback, even when the callback no longer uses a per-sioc GSource. Signed-off-by: Eric Blake --- io/net-listener.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/io/net-listener.c b/io/net-listener.c index afdacdd5ff4..ce29bf3c993 100644 --- a/io/net-listener.c +++ b/io/net-listener.c @@ -118,12 +118,14 @@ qio_net_listener_watch(QIONetListener *listener, size= _t i, const char *caller) } trace_qio_net_listener_watch_enabled(listener, listener->io_func, call= er); - for ( ; i < listener->nsioc; i++) { + if (i =3D=3D 0) { object_ref(OBJECT(listener)); + } + for ( ; i < listener->nsioc; i++) { listener->io_source[i] =3D qio_channel_add_watch_source( QIO_CHANNEL(listener->sioc[i]), G_IO_IN, qio_net_listener_channel_func, - listener, (GDestroyNotify)object_unref, listener->context); + listener, NULL, listener->context); } } @@ -144,6 +146,7 @@ qio_net_listener_unwatch(QIONetListener *listener, cons= t char *caller) listener->io_source[i] =3D NULL; } } + object_unref(OBJECT(listener)); } void qio_net_listener_add(QIONetListener *listener, --=20 2.51.1 From nobody Sun Nov 16 01:08:08 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1762201915; cv=none; d=zohomail.com; s=zohoarc; b=g+UosxKrIlvRSCujQLZbpUHIisDOZEx6hleHnuXTMPoTmqa+ajUDFaBoBl2qCRuGdSEuB2kfR9F0UgSwDRxgaWnb4KlLVdCT75q6Vl/k5yZTd2NvKADFkPhxN8ruAajYiwRObIHlEaRZ++AeyX50bhCZnpRoujLRu1FlYo8dT08= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1762201915; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=N3665WET8n0BkmnE742sRFIUJjlw+28NAK/J1/BwZ0Q=; b=oHUupTlY2Wu+aA4wNHraMxFDO+cfVZr3Zysjwy11yvO79XfPYF2yJIpZYSGSgM5gJGBDJYmZhBsUHYHCiJeJRz7MGE0BiOldZa6qf5orvtLZ5KinS86ufQ/WqJnfwklUpbg+QOCh6ziuUDzvgFFnTaLVGnPd9fot6JwWZfr9TSI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1762201915286731.6788510013735; Mon, 3 Nov 2025 12:31:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vG1BD-00045x-Hi; Mon, 03 Nov 2025 15:29:31 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1B1-0003pY-H8 for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:20 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1Ay-0007im-7O for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:19 -0500 Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-693-VfCG1JpzOAKahxO5yO9Sdg-1; Mon, 03 Nov 2025 15:29:12 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7CC2E1944D3A; Mon, 3 Nov 2025 20:29:07 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.131]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 53B6130001A1; Mon, 3 Nov 2025 20:29:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1762201753; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N3665WET8n0BkmnE742sRFIUJjlw+28NAK/J1/BwZ0Q=; b=GTO1pb3/5sSHTHcwJXFKZ+uua6TljUiHAtHbMGvQ2ZiEmkG6zP4ECCI82H5KfzDftjrzBZ H2SLVqsZA7D0qTpzTw7pEEQliLNNCAscB2Y49Ssy2UtvYSyzeCvLBENhfWCgZkzyEqHlhG /HSXyn5Y2Ojvz7vkxGfTIC5CtLUp744= X-MC-Unique: VfCG1JpzOAKahxO5yO9Sdg-1 X-Mimecast-MFC-AGG-ID: VfCG1JpzOAKahxO5yO9Sdg_1762201751 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, berrange@redhat.com, kwolf@redhat.com Subject: [PATCH 7/8] qio: Use AioContext for default-context QIONetListener Date: Mon, 3 Nov 2025 14:10:58 -0600 Message-ID: <20251103202849.3687643-17-eblake@redhat.com> In-Reply-To: <20251103202849.3687643-10-eblake@redhat.com> References: <20251103202849.3687643-10-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1762201917517154100 Content-Type: text/plain; charset="utf-8" The user "John Doe" reported a deadlock when attempting to use qemu-storage-daemon to serve both a base file over NBD, and a qcow2 file with that NBD export as its backing file, from the same process, even though it worked just fine when there were two q-s-d processes. The bulk of the NBD server code properly uses coroutines to make progress in an event-driven manner, but the code for spawning a new coroutine at the point when listen(2) detects a new client was hard-coded to use the global GMainContext; in other words, the callback that triggers nbd_client_new to let the server start the negotiation sequence with the client requires the main loop to be making progress. However, the code for bdrv_open of a qcow2 image with an NBD backing file uses an AIO_WAIT_WHILE nested event loop to ensure that the entire qcow2 backing chain is either fully loaded or rejected, without any side effects from the main loop causing unwanted changes to the disk being loaded (in short, an AioContext represents the set of actions that are known to be safe while handling block layer I/O, while excluding any other pending actions in the global main loop with potentially larger risk of unwanted side effects). This creates a classic case of deadlock: the server can't progress to the point of accept(2)ing the client to write to the NBD socket because the main loop is being starved until the AIO_WAIT_WHILE completes the bdrv_open, but the AIO_WAIT_WHILE can't progress because it is blocked on the client coroutine stuck in a read() of the expected magic number from the server side of the socket. Fortunately, the way that AioContext is set up, any callback that is registered to the global AioContext will also be serviced by the main loop. So the fix for the deadlock is to alter QIONetListener so that if it is not being used in an explicit alternative GMainContext, then it should perform its polling via the global AioContext (which indirectly still progresses in the default GMainContext) rather than directly in the default GMainContext. This has no change in behavior to any prior use that did not starve the main loop, but has the additional benefit that in the bdrv_open case of a nested AioContext loop, the server's listen/accept handler is no longer starved because it is now part of the same AioContext loop. From there, since NBD already uses coroutines for both server and client code, the nested AioContext loop finishes quickly and opening the qcow2 backing chain no longer deadlocks. The next patch will add a unit test (kept separate to make it easier to rearrange the series to demonstrate the deadlock without this patch). Fixes: https://gitlab.com/qemu-project/qemu/-/issues/3169 Signed-off-by: Eric Blake --- io/net-listener.c | 53 ++++++++++++++++++++++++++++++++++++++--------- io/trace-events | 4 ++-- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/io/net-listener.c b/io/net-listener.c index ce29bf3c993..9f4e3c0be0c 100644 --- a/io/net-listener.c +++ b/io/net-listener.c @@ -23,6 +23,7 @@ #include "io/dns-resolver.h" #include "qapi/error.h" #include "qemu/module.h" +#include "qemu/main-loop.h" #include "trace.h" QIONetListener *qio_net_listener_new(void) @@ -62,6 +63,15 @@ static gboolean qio_net_listener_channel_func(QIOChannel= *ioc, } +static void qio_net_listener_aio_func(void *opaque) +{ + QIOChannelSocket *sioc =3D QIO_CHANNEL_SOCKET(opaque); + + qio_net_listener_channel_func(QIO_CHANNEL(sioc), G_IO_IN, + sioc->listener); +} + + int qio_net_listener_open_sync(QIONetListener *listener, SocketAddress *addr, int num, @@ -117,15 +127,33 @@ qio_net_listener_watch(QIONetListener *listener, size= _t i, const char *caller) return; } - trace_qio_net_listener_watch_enabled(listener, listener->io_func, call= er); + trace_qio_net_listener_watch_enabled(listener, listener->io_func, + listener->context, caller); if (i =3D=3D 0) { object_ref(OBJECT(listener)); } for ( ; i < listener->nsioc; i++) { - listener->io_source[i] =3D qio_channel_add_watch_source( - QIO_CHANNEL(listener->sioc[i]), G_IO_IN, - qio_net_listener_channel_func, - listener, NULL, listener->context); + if (listener->context) { + /* + * The user passed a GMainContext with the async callback; + * they plan on running their own g_main_loop. + */ + listener->io_source[i] =3D qio_channel_add_watch_source( + QIO_CHANNEL(listener->sioc[i]), G_IO_IN, + qio_net_listener_channel_func, + listener, NULL, listener->context); + } else { + /* + * The user is fine with the default context. But by doing + * it in the main thread's AioContext rather than + * specifically in a GMainContext, we can remain + * responsive even if another AioContext depends on + * connecting to this server. + */ + aio_set_fd_handler(qemu_get_aio_context(), listener->sioc[i]->= fd, + qio_net_listener_aio_func, NULL, NULL, NULL, + listener->sioc[i]); + } } } @@ -138,12 +166,17 @@ qio_net_listener_unwatch(QIONetListener *listener, co= nst char *caller) return; } - trace_qio_net_listener_watch_disabled(listener, caller); + trace_qio_net_listener_watch_disabled(listener, listener->context, cal= ler); for (i =3D 0; i < listener->nsioc; i++) { - if (listener->io_source[i]) { - g_source_destroy(listener->io_source[i]); - g_source_unref(listener->io_source[i]); - listener->io_source[i] =3D NULL; + if (listener->context) { + if (listener->io_source[i]) { + g_source_destroy(listener->io_source[i]); + g_source_unref(listener->io_source[i]); + listener->io_source[i] =3D NULL; + } + } else { + aio_set_fd_handler(qemu_get_aio_context(), listener->sioc[i]->= fd, + NULL, NULL, NULL, NULL, NULL); } } object_unref(OBJECT(listener)); diff --git a/io/trace-events b/io/trace-events index 8cc4cae3a5d..1b01b2d51e6 100644 --- a/io/trace-events +++ b/io/trace-events @@ -74,6 +74,6 @@ qio_channel_command_abort(void *ioc, int pid) "Command ab= ort ioc=3D%p pid=3D%d" qio_channel_command_wait(void *ioc, int pid, int ret, int status) "Command= abort ioc=3D%p pid=3D%d ret=3D%d status=3D%d" # net-listener.c -qio_net_listener_watch_enabled(void *listener, void *func, const char *ext= ra) "Net listener=3D%p watch enabled func=3D%p by %s" -qio_net_listener_watch_disabled(void *listener, const char *extra) "Net li= stener=3D%p watch disabled by %s" +qio_net_listener_watch_enabled(void *listener, void *func, void *ctx, cons= t char *extra) "Net listener=3D%p watch enabled func=3D%p ctx=3D%p by %s" +qio_net_listener_watch_disabled(void *listener, void *ctx, const char *ext= ra) "Net listener=3D%p watch disabled ctx=3D%p by %s" qio_net_listener_callback(void *listener, void *func) "Net listener=3D%p c= allback forwarding to func=3D%p" --=20 2.51.1 From nobody Sun Nov 16 01:08:08 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1762201927; cv=none; d=zohomail.com; s=zohoarc; b=jgvT8mIKt8OLaeg8xrSXAdzNTwzoDGIJImhy6lxMIUToXYy1a9xEhwUqJM2vLDoa8vGK+XNiWmcoIJjPKcQeEI+28VUfE/rybhmxMo3ykAqJhMPgGW3rxMQzHUWavUy7bhjIW4ixL3jjbr3KDmRQlwPBD/D4hfUTZIdsLlTPRsc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1762201927; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0AzqrP4Q2MbNGpljBETQ9h1CiGOVkyDv1lMoTk5T+co=; b=JCgLWEE/FbaEUrHCsU8NqPSW+y5GO1x8VIRKoVrgYx//t7P6s4DZt4GlVYfE3cscOXpm8QwyQlKFVnZxC5kjcXcaO9V3f0gmMLuLErX/hYxSgDmfrLjh8JplDXHrGOYvubQa/LVeBlaSB417tKmHvjvBO3zMNbOBfRLkJgLkUXM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1762201927099268.6950386133959; Mon, 3 Nov 2025 12:32:07 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vG1B7-0003yX-Qz; Mon, 03 Nov 2025 15:29:26 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1B5-0003tH-47 for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:23 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vG1B1-0007k9-WD for qemu-devel@nongnu.org; Mon, 03 Nov 2025 15:29:22 -0500 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-650-9E_zEs8JM4CsLTDqU-ECZg-1; Mon, 03 Nov 2025 15:29:16 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4133A1865FDC; Mon, 3 Nov 2025 20:29:10 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.131]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D680830001A8; Mon, 3 Nov 2025 20:29:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1762201758; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0AzqrP4Q2MbNGpljBETQ9h1CiGOVkyDv1lMoTk5T+co=; b=LaJ6r7DCgXYc3H9V7qk7zbOSGNL/GdGMa3xpIo0Z/H/oHKqr5KBBZBmB6Q3sW1BMki6a5p 8hXbwrqprRksueRNwK4B0nWdVk9EqNXLPWt1JM9N8mYWUZ2gk0S7CSXXvAKuzshB2MUZtT WgvHHSEktRFvdaxf25oR9vM15bn1Vfs= X-MC-Unique: 9E_zEs8JM4CsLTDqU-ECZg-1 X-Mimecast-MFC-AGG-ID: 9E_zEs8JM4CsLTDqU-ECZg_1762201755 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, berrange@redhat.com, kwolf@redhat.com, Vladimir Sementsov-Ogievskiy , Hanna Reitz Subject: [PATCH 8/8] iotests: Add coverage of recent NBD qio deadlock fix Date: Mon, 3 Nov 2025 14:10:59 -0600 Message-ID: <20251103202849.3687643-18-eblake@redhat.com> In-Reply-To: <20251103202849.3687643-10-eblake@redhat.com> References: <20251103202849.3687643-10-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1762201929957154100 Content-Type: text/plain; charset="utf-8" Test that all images in a qcow2 chain using an NBD backing file can be served by the same process. Prior to the recent QIONetListener fixes, this test would demonstrate deadlock. The test borrows heavily from the original formula by "John Doe" in the gitlab bug, but uses a Unix socket rather than TCP to avoid port contention, and uses a full-blown QEMU rather than qemu-storage-daemon since both programs were impacted. [While preparing this patch by making the new test executable, I noticed vvfat.out does not need execute permissions] Fixes: https://gitlab.com/qemu-project/qemu/-/issues/3169 Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- tests/qemu-iotests/tests/nbd-in-qcow2-chain | 84 +++++++++++++++++++ .../qemu-iotests/tests/nbd-in-qcow2-chain.out | 56 +++++++++++++ tests/qemu-iotests/tests/vvfat.out | 0 3 files changed, 140 insertions(+) create mode 100755 tests/qemu-iotests/tests/nbd-in-qcow2-chain create mode 100644 tests/qemu-iotests/tests/nbd-in-qcow2-chain.out mode change 100755 =3D> 100644 tests/qemu-iotests/tests/vvfat.out diff --git a/tests/qemu-iotests/tests/nbd-in-qcow2-chain b/tests/qemu-iotes= ts/tests/nbd-in-qcow2-chain new file mode 100755 index 00000000000..b89f74d4552 --- /dev/null +++ b/tests/qemu-iotests/tests/nbd-in-qcow2-chain @@ -0,0 +1,84 @@ +#!/usr/bin/env bash +# group: rw quick +# +# Test of opening both server and client NBD in a qcow2 backing chain +# +# Copyright (C) Red Hat, Inc. +# +# SPDX-License-Identifier: GPL-2.0-or-later + +# creator +owner=3Deblake@redhat.com + +seq=3D`basename $0` +echo "QA output created by $seq" + +status=3D1 # failure is the default! + +_cleanup() +{ + _cleanup_qemu + _cleanup_test_img + rm -f "$SOCK_DIR/nbd" +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +cd .. +. ./common.rc +. ./common.filter +. ./common.qemu +. ./common.nbd + +_supported_fmt qcow2 # Hardcoded to qcow2 command line and QMP below +_supported_proto file + +size=3D100M + +echo +echo "=3D=3D=3D Preparing base image =3D=3D=3D" + +TEST_IMG=3D"$TEST_IMG.base" _make_test_img $size + +echo +echo "=3D=3D=3D Starting QEMU and exposing base image =3D=3D=3D" + +_launch_qemu -machine q35 +h1=3D$QEMU_HANDLE +_send_qemu_cmd $QEMU_HANDLE '{"execute": "qmp_capabilities"}' 'return' +_send_qemu_cmd $QEMU_HANDLE '{"execute": "blockdev-add", + "arguments": {"node-name":"base", "driver":"qcow2", + "file":{"driver":"file", "filename":"'"$TEST_IMG.base"'"}}}' 'return' +_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-start", + "arguments": {"addr":{"type":"unix", + "data":{"path":"'"$SOCK_DIR/nbd"'"}}}}' 'return' +_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", + "arguments": {"device":"base","name":"base"}}' 'return' + +echo +echo "=3D=3D=3D Creating wrapper image =3D=3D=3D" + +_make_test_img -F raw -b "nbd+unix:///base?socket=3D$SOCK_DIR/nbd" $size + +echo +echo "=3D=3D=3D Adding wrapper image =3D=3D=3D" + +_send_qemu_cmd $QEMU_HANDLE '{"execute": "blockdev-add", + "arguments": {"node-name":"wrap", "driver":"qcow2", + "file":{"driver":"file", "filename":"'"$TEST_IMG"'"}}}' 'return' +_send_qemu_cmd $QEMU_HANDLE '{"execute":"nbd-server-add", + "arguments": {"device":"wrap","name":"wrap"}}' 'return' + +echo +echo "=3D=3D=3D Checking NBD server =3D=3D=3D" + +$QEMU_NBD --list -k $SOCK_DIR/nbd + +echo +echo "=3D=3D=3D Cleaning up =3D=3D=3D" + +_send_qemu_cmd $QEMU_HANDLE '{"execute":"quit"}' '' + +echo "*** done" +rm -f $seq.full +status=3D0 diff --git a/tests/qemu-iotests/tests/nbd-in-qcow2-chain.out b/tests/qemu-i= otests/tests/nbd-in-qcow2-chain.out new file mode 100644 index 00000000000..5f1d31ae2e0 --- /dev/null +++ b/tests/qemu-iotests/tests/nbd-in-qcow2-chain.out @@ -0,0 +1,56 @@ +QA output created by nbd-in-qcow2-chain + +=3D=3D=3D Preparing base image =3D=3D=3D +Formatting 'TEST_DIR/t.IMGFMT.base', fmt=3DIMGFMT size=3D104857600 + +=3D=3D=3D Starting QEMU and exposing base image =3D=3D=3D +{"execute": "qmp_capabilities"} +{"return": {}} +{"execute": "blockdev-add", + "arguments": {"node-name":"base", "driver":"IMGFMT", + "file":{"driver":"file", "filename":"TEST_DIR/t.IMGFMT.base"}}} +{"return": {}} +{"execute":"nbd-server-start", + "arguments": {"addr":{"type":"unix", + "data":{"path":"SOCK_DIR/nbd"}}}} +{"return": {}} +{"execute":"nbd-server-add", + "arguments": {"device":"base","name":"base"}} +{"return": {}} + +=3D=3D=3D Creating wrapper image =3D=3D=3D +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D104857600 backing_file= =3Dnbd+unix:///base?socket=3DSOCK_DIR/nbd backing_fmt=3Draw + +=3D=3D=3D Adding wrapper image =3D=3D=3D +{"execute": "blockdev-add", + "arguments": {"node-name":"wrap", "driver":"IMGFMT", + "file":{"driver":"file", "filename":"TEST_DIR/t.IMGFMT"}}} +{"return": {}} +{"execute":"nbd-server-add", + "arguments": {"device":"wrap","name":"wrap"}} +{"return": {}} + +=3D=3D=3D Checking NBD server =3D=3D=3D +exports available: 2 + export: 'base' + size: 104857600 + flags: 0x158f ( readonly flush fua df multi cache block-status-payload ) + min block: 1 + opt block: 4096 + max block: 33554432 + transaction size: 64-bit + available meta contexts: 1 + base:allocation + export: 'wrap' + size: 104857600 + flags: 0x158f ( readonly flush fua df multi cache block-status-payload ) + min block: 1 + opt block: 4096 + max block: 33554432 + transaction size: 64-bit + available meta contexts: 1 + base:allocation + +=3D=3D=3D Cleaning up =3D=3D=3D +{"execute":"quit"} +*** done diff --git a/tests/qemu-iotests/tests/vvfat.out b/tests/qemu-iotests/tests/= vvfat.out old mode 100755 new mode 100644 --=20 2.51.1