From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 90F26CCFA18 for ; Tue, 11 Nov 2025 15:49:30 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vIqc4-000111-5R; Tue, 11 Nov 2025 10:48:56 -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 1vIqbS-0000L5-8Y for qemu-devel@nongnu.org; Tue, 11 Nov 2025 10:48:27 -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 1vIqbP-0002Lm-Hc for qemu-devel@nongnu.org; Tue, 11 Nov 2025 10:48:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1762876094; 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=FzUH5L5f9GW5ZhlF3o0tjrC3GU9JY/7tPQwUg1WEewI=; b=C8BeLwKxnA9lRCgIkvciP1yLFXZ8W7H4Y2H3dON8+sGepGrB3WyO3J+qdoyXoHUcuCc6Ox AozsfkjwlsCAIROWdjG4zvMk3alWW4WBN3Mxu7sJ0d77MPW7kjWM7uY0qIii4z1kXiGV+u fExNgRVmcCjiLSPuQw7d9vs7l9mwSKI= Received: from mx-prod-mc-01.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-574-Owv2cLXHP3mFJYYNWwJWMA-1; Tue, 11 Nov 2025 10:48:10 -0500 X-MC-Unique: Owv2cLXHP3mFJYYNWwJWMA-1 X-Mimecast-MFC-AGG-ID: Owv2cLXHP3mFJYYNWwJWMA_1762876090 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BDA61195608F; Tue, 11 Nov 2025 15:48:09 +0000 (UTC) Received: from redhat.com (unknown [10.45.225.214]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6916D19560B7; Tue, 11 Nov 2025 15:48:07 +0000 (UTC) Date: Tue, 11 Nov 2025 16:48:04 +0100 From: Kevin Wolf To: Daniel =?iso-8859-1?Q?P=2E_Berrang=E9?= Cc: Eric Blake , qemu-devel@nongnu.org, qemu-block@nongnu.org, stefanha@redhat.com Subject: Re: [PATCH 6/8] qio: Hoist ref of listener outside loop Message-ID: References: <20251103202849.3687643-10-eblake@redhat.com> <20251103202849.3687643-16-eblake@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Received-SPF: pass client-ip=170.10.133.124; envelope-from=kwolf@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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Am 11.11.2025 um 15:43 hat Daniel P. Berrangé geschrieben: > On Wed, Nov 05, 2025 at 03:57:29PM -0600, Eric Blake wrote: > > On Tue, Nov 04, 2025 at 11:13:48AM +0000, Daniel P. Berrangé wrote: > > > On Mon, Nov 03, 2025 at 02:10:57PM -0600, Eric Blake wrote: > > > > 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. > > > > > > It isn't quite this simple. Glib reference counts the callback > > > func / data, holding a reference when dispatching the callback. > > > > > > IOW, even if the GSource is unrefed, the callback 'notify' > > > function won't be called if the main loop is in the process > > > of dispatching. > > > > I'm not sure I follow your argument. Glib holds a reference on the > > GSource object, not on the opaque data that is handed to the GSource. > > It is possible to use g_source_set_callback_indirect() where GSource > > can learn how to use the same reference counting on data as external > > code, by the use of function pointers for ref and unref, but QIO uses > > merely g_source_set_callback(). > > https://gitlab.gnome.org/GNOME/glib/-/blob/main/glib/gmain.c#L1844 > > shows that glib then wraps that opaque pointer into an internal > > GSourceCallback object which itself is reference counted, so that the > > notify function is not called until the GSource is finalized, but that > > is reference counting on the container, not on the opaque object > > itself (which in this patch is the QIONetListener). > > > > > > > > With this change, the reference on 'listener' can now be > > > released even if the callback is currently dispatching. > > > > So if I'm understanding your concern, you're worried that the unwatch > > code can finish looping through the g_source_destroy and then reach > > the point where it unrefs listener, but that a late-breaking client > > connection can trigger a callback that can still be executing in > > another thread/coroutine after the listener is unref'ed but before the > > GSource has been finalized? If so, would squashing this in fix the > > problem you are seeing? > > Consider the following scenario, where we have two threads, one > is calling QIONetListener APIs, and one is the event thread. > > In the current code, when we unref() the GSource for the socket > watch, the destroy-notify does not get called, because the > event thread is in the middle of a dispatch callback for the > I/O event. When the dispatch callback returns control to the > event loop, the GSourceCallback is unrefed, and this triggers > the destroy-notify call, which unrefs the listener. > > The flow looks like this: > > Thread 1: > qio_net_listener_set_client_func(lstnr, f, ...); > => foreach sock: socket > => object_ref(lstnr) > => sock_src = qio_channel_socket_add_watch_source(sock, ...., lstnr, object_unref); > > Thread 2: > poll() > => event POLLIN on socket > => ref(GSourceCallback) > => call dispatch(sock) > ...do stuff.. > > Thread 1: > qio_net_listener_set_client_func(lstnr, NULL, ...); > => foreach sock: socket > => g_source_unref(sock_src) > unref(lstnr) (the final reference) > => finalize(lstnr) > > Thread 2: > => return dispatch(sock) > => unref(GSourceCallback) > => destroy-notify > => object_unref > > > > > diff --git i/io/net-listener.c w/io/net-listener.c > > index 9f4e3c0be0c..1fcbbeb7a76 100644 > > --- i/io/net-listener.c > > +++ w/io/net-listener.c > > @@ -67,8 +67,10 @@ static void qio_net_listener_aio_func(void *opaque) > > { > > QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(opaque); > > > > + object_ref(OBJECT(sioc->listener)); > > qio_net_listener_channel_func(QIO_CHANNEL(sioc), G_IO_IN, > > sioc->listener); > > + object_unref(OBJECT(sioc->listener)); > > } > > Now consider this patch, plus this extra hunk... > > Thread 1: > qio_net_listener_set_client_func(lstnr, f, ...); > => object_ref(listener) > => foreach sock: socket > => sock_src = qio_channel_socket_add_watch_source(sock, ...., lstnr, NULL); > > Thread 2: > poll() > => event POLLIN on socket > => call dispatch(sock) > => ref(lstnr) > ...do stuff.. > > Thread 1: > qio_net_listener_set_client_func(lstnr, NULL, ...); > => foreach sock: socket > => g_source_unref(sock_src) > => object_unref(listener) > unref(lstnr) (still 1 reference left) > > Thread 2: > => unref(lstnr) (the final reference) > => finalize(lstnr) > => return dispatch(sock) > => unref(GSourceCallback) > > > That appears to work ok, however, there's still a race window that is > not solved. Between the time thread 2 sees POLLIN, and when it calls > the dispatch(sock) function, it is possible that thread 1 will drop > the last reference: > > > > Thread 1: > qio_net_listener_set_client_func(lstnr, f, ...); > => object_ref(listener) > => foreach sock: socket > => sock_src = qio_channel_socket_add_watch_source(sock, ...., lstnr, NULL); > > Thread 2: > poll() > => event POLLIN on socket > > Thread 1: > qio_net_listener_set_client_func(lstnr, NULL, ...); > => foreach sock: socket > => g_source_unref(sock_src) > => object_unref(listener) > unref(lstnr) (still 1 reference left) Is what you're worried about that there is still a reference left in the opaque pointer of an fd handler, but it's not in the refcount and therefore this already frees the listener while thread 2 will still access it? > > Thread 2: > => call dispatch(sock) > => ref(lstnr) > ...do stuff.. > => unref(lstnr) (the final reference) > => finalize(lstnr) > => return dispatch(sock) > => unref(GSourceCallback) > > > I don't see a way to solve this without synchronization with the event > loop for releasing the reference on the opaque data for the dispatcher > callback. That's what the current code does, but I'm seeing no way for > the AioContext event loop callbacks to have anything equivalent. This > feels like a gap in the AioContext design. I think the way you would normally do this is schedule a BH in thread 2 to do the critical work. If you delete the fd handler and unref the listener in thread 2, then there is no race. But maybe adding a callback for node deletion in AioHandler wouldn't hurt because the opaque pointer pretty much always references something and doing an unref when deleting the AioHandler should be a pretty common pattern. > This is admittedly an incredibly hard to trigger race condition. It would > need a client to be calling a QMP command that tears down the NBD server, > at the exact same time as a new NBD client was incoming. Or the same kind > of scenario for other pieces of QEMU code using QIONetListener. This still > makes me worried though, as rare races have a habit of hitting QEMU > eventually. Aren't both QMP and incoming NBD connections always handled in the main thread? I'm not sure if I know a case where we would actually get this pattern with two different threads today. Of course, that doesn't mean that we couldn't get it in the future. Kevin