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 96B0BEDEBE6 for ; Tue, 3 Mar 2026 19:25:14 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vxVMa-00022o-SQ; Tue, 03 Mar 2026 14:25:01 -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 1vxVMT-00020w-TH; Tue, 03 Mar 2026 14:24:53 -0500 Received: from mailcow.dlhnet.de ([84.39.110.98]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vxVMQ-00028V-MI; Tue, 03 Mar 2026 14:24:53 -0500 Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 76B2C46ABA1; Tue, 3 Mar 2026 20:24:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dlhnet.de; s=dkim; t=1772565878; h=from:subject:date:message-id:to:cc:mime-version:content-type: in-reply-to:references; bh=DAXz+BQldji3Qh2rNeq3LjL1Cibg+4m7wJdGotbezQo=; b=osa0wNhkM/2zAHz+iGZ4ZGpxjWOGzGxtxEKt9IAe+LQoVF7UKFlZaqe9bXhi/Xqn5ZfYRb DQX3WEDgVugALd9+rXVmaXuT006OsoP93zd1htp6URGGlwHTuddhjKR/HPYB/wtarwyidB cqNSykXP90V3S+nzMM4M+TI1ZpC3ts5HLFMsUhzKJDkM0sFpsUMwUx48hEiRB/K9EcdVja IUIBPFHP/79dkHOeilfkca/iCVIqwEDKAYPufnofWWpU6XQUcH3kGU8J/avTmjb4c1McZx 7RQHj28GgBLnnJ4F+rbmD31SPtjER5FaJ1NWQMDl4v82rmoLkshm3zbfcJAycw== From: Peter Lieven Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_8055D373-A018-4AE9-BEF3-574B5AE8600E" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81.1.4\)) Subject: =?utf-8?Q?Re=3A_=5BPATCH_v2_04/19=5D_nfs=3A_Run_co_BH_CB_in_the_c?= =?utf-8?Q?oroutine=E2=80=99s_AioContext?= Date: Tue, 3 Mar 2026 20:24:25 +0100 In-Reply-To: <20251110154854.151484-5-hreitz@redhat.com> Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org, Kevin Wolf , Stefan Hajnoczi , Paolo Bonzini , "Richard W . M . Jones" , Ilya Dryomov , =?utf-8?Q?Philippe_Mathieu-Daud=C3=A9?= , =?utf-8?Q?Alex_Benn=C3=A9e?= , Fam Zheng , Ronnie Sahlberg , Peter Maydell To: Hanna Czenczek References: <20251110154854.151484-1-hreitz@redhat.com> <20251110154854.151484-5-hreitz@redhat.com> X-Mailer: Apple Mail (2.3826.700.81.1.4) X-Last-TLS-Session-Version: TLSv1.2 Received-SPF: pass client-ip=84.39.110.98; envelope-from=pl@dlhnet.de; helo=mailcow.dlhnet.de X-Spam_score_int: -5 X-Spam_score: -0.6 X-Spam_bar: / X-Spam_report: (-0.6 / 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, HTML_MESSAGE=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.322, RCVD_IN_VALIDITY_SAFE_BLOCKED=1.141, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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 --Apple-Mail=_8055D373-A018-4AE9-BEF3-574B5AE8600E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Am 10.11.2025 um 16:48 schrieb Hanna Czenczek : >=20 > Like in =E2=80=9Crbd: Run co BH CB in the coroutine=E2=80=99s = AioContext=E2=80=9D, drop the > completion flag, yield exactly once, and run the BH in the = coroutine=E2=80=99s > AioContext. >=20 > (Can be reproduced with multiqueue by adding a usleep(100000) before = the > `while (!task.complete)` loops.) >=20 > Like in =E2=80=9Ciscsi: Run co BH CB in the coroutine=E2=80=99s = AioContext=E2=80=9D, this makes > nfs_co_generic_bh_cb() trivial, so we can drop it in favor of just > calling aio_co_wake() directly. >=20 > Cc: qemu-stable@nongnu.org > Signed-off-by: Hanna Czenczek > --- > block/nfs.c | 41 ++++++++++++++++------------------------- > 1 file changed, 16 insertions(+), 25 deletions(-) >=20 > diff --git a/block/nfs.c b/block/nfs.c > index 0a7d38db09..1d3a34a30c 100644 > --- a/block/nfs.c > +++ b/block/nfs.c > @@ -69,7 +69,6 @@ typedef struct NFSClient { > typedef struct NFSRPC { > BlockDriverState *bs; > int ret; > - int complete; > QEMUIOVector *iov; > struct stat *st; > Coroutine *co; > @@ -230,14 +229,6 @@ static void coroutine_fn = nfs_co_init_task(BlockDriverState *bs, NFSRPC *task) > }; > } >=20 > -static void nfs_co_generic_bh_cb(void *opaque) > -{ > - NFSRPC *task =3D opaque; > - > - task->complete =3D 1; > - aio_co_wake(task->co); > -} > - > /* Called (via nfs_service) with QemuMutex held. */ > static void > nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data, > @@ -256,8 +247,16 @@ nfs_co_generic_cb(int ret, struct nfs_context = *nfs, void *data, > if (task->ret < 0) { > error_report("NFS Error: %s", nfs_get_error(nfs)); > } > - replay_bh_schedule_oneshot_event(task->client->aio_context, > - nfs_co_generic_bh_cb, task); > + > + /* > + * Safe to call: nfs_service(), which called us, is only run from = the FD > + * handlers, never from the request coroutine. The request = coroutine in > + * turn will yield unconditionally. > + * No need to release the lock, even if we directly enter the = coroutine, as > + * the lock is never re-taken after yielding. (Note: If we do = enter the > + * coroutine, @task will probably be dangling once aio_co_wake() = returns.) > + */ > + aio_co_wake(task->co); > } >=20 > static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, int64_t = offset, > @@ -278,9 +277,7 @@ static int coroutine_fn = nfs_co_preadv(BlockDriverState *bs, int64_t offset, >=20 > nfs_set_events(client); > } > - while (!task.complete) { > - qemu_coroutine_yield(); > - } > + qemu_coroutine_yield(); >=20 > if (task.ret < 0) { > return task.ret; > @@ -328,9 +325,7 @@ static int coroutine_fn = nfs_co_pwritev(BlockDriverState *bs, int64_t offset, >=20 > nfs_set_events(client); > } > - while (!task.complete) { > - qemu_coroutine_yield(); > - } > + qemu_coroutine_yield(); >=20 > if (my_buffer) { > g_free(buf); > @@ -358,9 +353,7 @@ static int coroutine_fn = nfs_co_flush(BlockDriverState *bs) >=20 > nfs_set_events(client); > } > - while (!task.complete) { > - qemu_coroutine_yield(); > - } > + qemu_coroutine_yield(); >=20 > return task.ret; > } > @@ -723,8 +716,8 @@ nfs_get_allocated_file_size_cb(int ret, struct = nfs_context *nfs, void *data, > if (task->ret < 0) { > error_report("NFS Error: %s", nfs_get_error(nfs)); > } > - replay_bh_schedule_oneshot_event(task->client->aio_context, > - nfs_co_generic_bh_cb, task); > + /* Safe to call, see nfs_co_generic_cb() */ > + aio_co_wake(task->co); > } >=20 > static int64_t coroutine_fn = nfs_co_get_allocated_file_size(BlockDriverState *bs) > @@ -748,9 +741,7 @@ static int64_t coroutine_fn = nfs_co_get_allocated_file_size(BlockDriverState *bs) >=20 > nfs_set_events(client); > } > - while (!task.complete) { > - qemu_coroutine_yield(); > - } > + qemu_coroutine_yield(); >=20 > return (task.ret < 0 ? task.ret : st.st_blocks * 512); > } > --=20 > 2.51.1 >=20 Hallo Hanna, it has been a long time where I was inactive in Qemu development and I = should have checked this earlier, but it seems that this patch = accidentally broke libnfs usage with at least qemu cmdline tools like = qemu-img. Again, sorry for not testing this earlier, but I have only = recently entered the Qemu show again. I found this glitch while working on a patch to include libnfs v6 = support in qemu to avoid dropping libnfs support. A simple call like: qemu-img create -f qcow2 nfs://nfsserver/image.qcow2 10G hangs forever in the second write request. I have not confirmed, but this might also hold true for libiscsi which = internally is quite identical (at least it was 3 years ago ;-)) You are right, the coroutine can only yield once, but it seems that the = bottom half logic is still needed. Maybe you have an idea why? I remember that we had this differently = first and implemented it after Stefan pointed out that it might be = needed. I will try to find the thread about this discussion. This patch here restores functionality for me: diff --git a/block/nfs.c b/block/nfs.c index 1d3a34a30c..45731ea3da 100644 --- a/block/nfs.c +++ b/block/nfs.c @@ -75,6 +75,12 @@ typedef struct NFSRPC { NFSClient *client; } NFSRPC; =20 +static void nfs_co_generic_bh_cb(void *opaque) +{ + NFSRPC *task =3D opaque; + aio_co_wake(task->co); +} + static int nfs_parse_uri(const char *filename, QDict *options, Error = **errp) { g_autoptr(GUri) uri =3D g_uri_parse(filename, G_URI_FLAGS_NONE, = NULL); @@ -248,15 +254,8 @@ nfs_co_generic_cb(int ret, struct nfs_context *nfs, = void *data, error_report("NFS Error: %s", nfs_get_error(nfs)); } =20 - /* - * Safe to call: nfs_service(), which called us, is only run from = the FD - * handlers, never from the request coroutine. The request = coroutine in - * turn will yield unconditionally. - * No need to release the lock, even if we directly enter the = coroutine, as - * the lock is never re-taken after yielding. (Note: If we do = enter the - * coroutine, @task will probably be dangling once aio_co_wake() = returns.) - */ - aio_co_wake(task->co); + replay_bh_schedule_oneshot_event(task->client->aio_context, + nfs_co_generic_bh_cb, task); } =20 static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, int64_t = offset, @@ -716,8 +715,8 @@ nfs_get_allocated_file_size_cb(int ret, struct = nfs_context *nfs, void *data, if (task->ret < 0) { error_report("NFS Error: %s", nfs_get_error(nfs)); } - /* Safe to call, see nfs_co_generic_cb() */ - aio_co_wake(task->co); + replay_bh_schedule_oneshot_event(task->client->aio_context, + nfs_co_generic_bh_cb, task); } =20 static int64_t coroutine_fn = nfs_co_get_allocated_file_size(BlockDriverState *bs) Best Peter --Apple-Mail=_8055D373-A018-4AE9-BEF3-574B5AE8600E Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

Am 10.11.2025 um 16:48 schrieb Hanna Czenczek = <hreitz@redhat.com>:

Like in =E2=80=9Crbd: Run = co BH CB in the coroutine=E2=80=99s AioContext=E2=80=9D, drop = the
completion flag, yield exactly once, and run the BH in the = coroutine=E2=80=99s
AioContext.

(Can be reproduced with = multiqueue by adding a usleep(100000) before the
`while = (!task.complete)` loops.)

Like in =E2=80=9Ciscsi: Run co BH CB in = the coroutine=E2=80=99s AioContext=E2=80=9D, this = makes
nfs_co_generic_bh_cb() trivial, so we can drop it in favor of = just
calling aio_co_wake() directly.

Cc: = qemu-stable@nongnu.org
Signed-off-by: Hanna Czenczek = <hreitz@redhat.com>
---
block/nfs.c | 41 = ++++++++++++++++-------------------------
1 file changed, 16 = insertions(+), 25 deletions(-)

diff --git a/block/nfs.c = b/block/nfs.c
index 0a7d38db09..1d3a34a30c 100644
--- = a/block/nfs.c
+++ b/block/nfs.c
@@ -69,7 +69,6 @@ typedef struct = NFSClient {
typedef struct NFSRPC {
=     BlockDriverState *bs;
=     int ret;
-    int = complete;
    QEMUIOVector *iov;
=     struct stat *st;
=     Coroutine *co;
@@ -230,14 +229,6 @@ static = void coroutine_fn nfs_co_init_task(BlockDriverState *bs, NFSRPC = *task)
    };
}

-static void = nfs_co_generic_bh_cb(void *opaque)
-{
-    NFSRPC = *task =3D opaque;
-
-    task->complete =3D = 1;
-    aio_co_wake(task->co);
-}
-
/* = Called (via nfs_service) with QemuMutex held.  */
static = void
nfs_co_generic_cb(int ret, struct nfs_context *nfs, void = *data,
@@ -256,8 +247,16 @@ nfs_co_generic_cb(int ret, struct = nfs_context *nfs, void *data,
    if = (task->ret < 0) {
=         error_report("NFS Error: = %s", nfs_get_error(nfs));
    }
- =    replay_bh_schedule_oneshot_event(task->client->aio= _context,
- =             &n= bsp;           &nbs= p;           nfs_co= _generic_bh_cb, task);
+
+    /*
+ =     * Safe to call: nfs_service(), which called us, = is only run from the FD
+     * handlers, never = from the request coroutine.  The request coroutine in
+ =     * turn will yield unconditionally.
+ =     * No need to release the lock, even if we = directly enter the coroutine, as
+     * the lock = is never re-taken after yielding.  (Note: If we do enter the
+ =     * coroutine, @task will probably be dangling = once aio_co_wake() returns.)
+     */
+ =    aio_co_wake(task->co);
}

static int = coroutine_fn nfs_co_preadv(BlockDriverState *bs, int64_t offset,
@@ = -278,9 +277,7 @@ static int coroutine_fn nfs_co_preadv(BlockDriverState = *bs, int64_t offset,

=         nfs_set_events(client);     }
-    while = (!task.complete) {
- =        qemu_coroutine_yield();
- =    }
+ =    qemu_coroutine_yield();

=     if (task.ret < 0) {
=         return task.ret;
@@ = -328,9 +325,7 @@ static int coroutine_fn nfs_co_pwritev(BlockDriverState = *bs, int64_t offset,

=         nfs_set_events(client);     }
-    while = (!task.complete) {
- =        qemu_coroutine_yield();
- =    }
+ =    qemu_coroutine_yield();

=     if (my_buffer) {
=         g_free(buf);
@@ = -358,9 +353,7 @@ static int coroutine_fn nfs_co_flush(BlockDriverState = *bs)

=         nfs_set_events(client);     }
-    while = (!task.complete) {
- =        qemu_coroutine_yield();
- =    }
+ =    qemu_coroutine_yield();

=     return task.ret;
}
@@ -723,8 +716,8 @@ = nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void = *data,
    if (task->ret < 0) {
=         error_report("NFS Error: = %s", nfs_get_error(nfs));
    }
- =    replay_bh_schedule_oneshot_event(task->client->aio= _context,
- =             &n= bsp;           &nbs= p;           nfs_co= _generic_bh_cb, task);
+    /* Safe to call, see = nfs_co_generic_cb() */
+ =    aio_co_wake(task->co);
}

static int64_t = coroutine_fn nfs_co_get_allocated_file_size(BlockDriverState *bs)
@@ = -748,9 +741,7 @@ static int64_t coroutine_fn = nfs_co_get_allocated_file_size(BlockDriverState *bs)

=         nfs_set_events(client);     }
-    while = (!task.complete) {
- =        qemu_coroutine_yield();
- =    }
+ =    qemu_coroutine_yield();

=     return (task.ret < 0 ? task.ret : = st.st_blocks * 512);
}
-- =
2.51.1


Hallo = Hanna,

it has been a long time where I was = inactive in Qemu development and I should have checked this earlier, but = it seems that this patch accidentally broke libnfs usage with at least = qemu cmdline tools like qemu-img. Again, sorry for not testing this = earlier, but I have only recently entered the Qemu show = again.

I found this glitch while working on a = patch to include libnfs v6 support in qemu to avoid dropping libnfs = support.

A simple call = like:

qemu-img create -f qcow2 nfs://nfsserver/image.qcow2 = 10G

hangs forever in the second write = request.

I have not confirmed, but this might = also hold true for libiscsi which internally is quite identical (at = least it was 3 years ago ;-))

You are right, = the coroutine can only yield once, but it seems that the bottom half = logic is still needed.
Maybe you have an idea why? I remember = that we had this differently first and implemented it after Stefan = pointed out that it might be needed.
I will try to find the = thread about this discussion.

This patch here = restores functionality for me:

diff --git = a/block/nfs.c b/block/nfs.c

index = 1d3a34a30c..45731ea3da 100644

--- = a/block/nfs.c

+++ = b/block/nfs.c

@@ = -75,6 +75,12 @@ typedef struct NFSRPC {

   =   NFSClient *client;

 } = NFSRPC;

 

+static void = nfs_co_generic_bh_cb(void *opaque)

+{

+    = NFSRPC *task =3D opaque;

+    = aio_co_wake(task->co);

+}

+

 static int = nfs_parse_uri(const char *filename, QDict *options, Error = **errp)

 {

   =   g_autoptr(GUri) uri =3D g_uri_parse(filename, G_URI_FLAGS_NONE, = NULL);

@@ = -248,15 +254,8 @@ nfs_co_generic_cb(int ret, struct nfs_context = *nfs, void *data,

   =       error_report("NFS Error: %s", = nfs_get_error(nfs));

   =   }

 

-    = /*

-     * = Safe to call: nfs_service(), which called us, is only run from the = FD

-     * = handlers, never from the request coroutine.  The request coroutine = in

-     * = turn will yield unconditionally.

-     * = No need to release the lock, even if we directly enter the coroutine, = as

-     * = the lock is never re-taken after yielding.  (Note: If we do enter = the

-     * = coroutine, @task will probably be dangling once aio_co_wake() = returns.)

-     = */

-    = aio_co_wake(task->co);

+    = replay_bh_schedule_oneshot_event(task->client->aio_context,

+     =                     =             nfs_co_generic_bh_cb, = task);

 }

 

 static int = coroutine_fn nfs_co_preadv(BlockDriverState *bs, int64_t = offset,

@@ = -716,8 +715,8 @@ nfs_get_allocated_file_size_cb(int ret, struct = nfs_context *nfs, void *data,

   =   if (task->ret < 0) {

   =       error_report("NFS Error: %s", = nfs_get_error(nfs));

   =   }

-    /* = Safe to call, see nfs_co_generic_cb() */

-    = aio_co_wake(task->co);

+    = replay_bh_schedule_oneshot_event(task->client->aio_context,

+     =                     =             nfs_co_generic_bh_cb, = task);

 }

 

 static = int64_t coroutine_fn nfs_co_get_allocated_file_size(BlockDriverState = *bs)




Best

Peter


= --Apple-Mail=_8055D373-A018-4AE9-BEF3-574B5AE8600E--