From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: <qemu-block@nongnu.org>, Kevin Wolf <kwolf@redhat.com>,
eesposit@redhat.com, Peter Lieven <pl@kamp.de>,
Hanna Reitz <hreitz@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>
Subject: [PATCH] block/nfs: avoid BDRV_POLL_WHILE() in nfs_co_get_allocated_file_size()
Date: Mon, 3 Apr 2023 19:30:33 -0400 [thread overview]
Message-ID: <20230403233033.408120-1-stefanha@redhat.com> (raw)
Commit 82618d7bc341 ("block: Convert bdrv_get_allocated_file_size() to
co_wrapper") made nfs_get_allocated_file_size() a coroutine. The
coroutine still uses BDRV_POLL_WHILE() to wait for the NFS RPC to
complete.
Take it a step further and yield the coroutine until the RPC completes.
This avoids the blocking, nested event loop and unifies
nfs_co_get_allocated_file_size() with the other coroutine functions that
send RPCs:
- Use nfs_co_init_task() to set up a coroutine NFSRPC task.
- Take client->mutex to protect fd handler state since we're in IO_CODE.
- Use nfs_co_generic_cb() instead of a specialized callback function.
- Yield until the task completes.
Getting rid of BDRV_POLL_WHILE() helps with the multi-queue block layer
effort where we don't want to take the AioContext lock.
This commit passes qemu-iotests/check -nfs, except inactivate-failure,
which also fails before this commit.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
block/nfs.c | 45 +++++++++++++++++----------------------------
1 file changed, 17 insertions(+), 28 deletions(-)
diff --git a/block/nfs.c b/block/nfs.c
index 351dc6ec8d..71062c9b47 100644
--- a/block/nfs.c
+++ b/block/nfs.c
@@ -248,14 +248,18 @@ nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
{
NFSRPC *task = private_data;
task->ret = ret;
- assert(!task->st);
if (task->ret > 0 && task->iov) {
+ assert(!task->st);
if (task->ret <= task->iov->size) {
qemu_iovec_from_buf(task->iov, 0, data, task->ret);
} else {
task->ret = -EIO;
}
}
+ if (task->ret == 0 && task->st) {
+ assert(!task->iov);
+ memcpy(task->st, data, sizeof(struct stat));
+ }
if (task->ret < 0) {
error_report("NFS Error: %s", nfs_get_error(nfs));
}
@@ -713,29 +717,10 @@ static int nfs_has_zero_init(BlockDriverState *bs)
}
#if !defined(_WIN32)
-/* Called (via nfs_service) with QemuMutex held. */
-static void
-nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
- void *private_data)
-{
- NFSRPC *task = private_data;
- task->ret = ret;
- if (task->ret == 0) {
- memcpy(task->st, data, sizeof(struct stat));
- }
- if (task->ret < 0) {
- error_report("NFS Error: %s", nfs_get_error(nfs));
- }
-
- /* Set task->complete before reading bs->wakeup. */
- qatomic_mb_set(&task->complete, 1);
- bdrv_wakeup(task->bs);
-}
-
static int64_t coroutine_fn nfs_co_get_allocated_file_size(BlockDriverState *bs)
{
NFSClient *client = bs->opaque;
- NFSRPC task = {0};
+ NFSRPC task;
struct stat st;
if (bdrv_is_read_only(bs) &&
@@ -743,15 +728,19 @@ static int64_t coroutine_fn nfs_co_get_allocated_file_size(BlockDriverState *bs)
return client->st_blocks * 512;
}
- task.bs = bs;
+ nfs_co_init_task(bs, &task);
task.st = &st;
- if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
- &task) != 0) {
- return -ENOMEM;
- }
+ WITH_QEMU_LOCK_GUARD(&client->mutex) {
+ if (nfs_fstat_async(client->context, client->fh, nfs_co_generic_cb,
+ &task) != 0) {
+ return -ENOMEM;
+ }
- nfs_set_events(client);
- BDRV_POLL_WHILE(bs, !task.complete);
+ nfs_set_events(client);
+ }
+ while (!task.complete) {
+ qemu_coroutine_yield();
+ }
return (task.ret < 0 ? task.ret : st.st_blocks * 512);
}
--
2.39.2
next reply other threads:[~2023-04-03 23:32 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-03 23:30 Stefan Hajnoczi [this message]
2023-04-27 12:54 ` [PATCH] block/nfs: avoid BDRV_POLL_WHILE() in nfs_co_get_allocated_file_size() Kevin Wolf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230403233033.408120-1-stefanha@redhat.com \
--to=stefanha@redhat.com \
--cc=eesposit@redhat.com \
--cc=hreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=pl@kamp.de \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).