All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com,
	stefanha@redhat.com, qemu-devel@nongnu.org
Subject: [PATCH v8 14/14] block: Convert 'block_resize' to coroutine
Date: Mon,  5 Oct 2020 17:58:55 +0200	[thread overview]
Message-ID: <20201005155855.256490-15-kwolf@redhat.com> (raw)
In-Reply-To: <20201005155855.256490-1-kwolf@redhat.com>

block_resize performs some I/O that could potentially take quite some
time, so use it as an example for the new 'coroutine': true annotation
in the QAPI schema.

bdrv_truncate() requires that we're already in the right AioContext for
the BlockDriverState if called in coroutine context. So instead of just
taking the AioContext lock, move the QMP handler coroutine to the
context.

Call blk_unref() only after switching back because blk_unref() may only
be called in the main thread.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 qapi/block-core.json |  3 ++-
 blockdev.c           | 16 ++++++++--------
 hmp-commands.hx      |  1 +
 3 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/qapi/block-core.json b/qapi/block-core.json
index 12a24772b5..90383b0148 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -1304,7 +1304,8 @@
 { 'command': 'block_resize',
   'data': { '*device': 'str',
             '*node-name': 'str',
-            'size': 'int' } }
+            'size': 'int' },
+  'coroutine': true }
 
 ##
 # @NewImageMode:
diff --git a/blockdev.c b/blockdev.c
index bebd3ba1c3..e331f81034 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -2439,14 +2439,14 @@ BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
     return ret;
 }
 
-void qmp_block_resize(bool has_device, const char *device,
-                      bool has_node_name, const char *node_name,
-                      int64_t size, Error **errp)
+void coroutine_fn qmp_block_resize(bool has_device, const char *device,
+                                   bool has_node_name, const char *node_name,
+                                   int64_t size, Error **errp)
 {
     Error *local_err = NULL;
     BlockBackend *blk = NULL;
     BlockDriverState *bs;
-    AioContext *aio_context;
+    AioContext *old_ctx;
 
     bs = bdrv_lookup_bs(has_device ? device : NULL,
                         has_node_name ? node_name : NULL,
@@ -2456,9 +2456,6 @@ void qmp_block_resize(bool has_device, const char *device,
         return;
     }
 
-    aio_context = bdrv_get_aio_context(bs);
-    aio_context_acquire(aio_context);
-
     if (size < 0) {
         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
         goto out;
@@ -2475,12 +2472,15 @@ void qmp_block_resize(bool has_device, const char *device,
     }
 
     bdrv_drained_begin(bs);
+    old_ctx = bdrv_co_enter(bs);
     blk_truncate(blk, size, false, PREALLOC_MODE_OFF, 0, errp);
+    bdrv_co_leave(bs, old_ctx);
     bdrv_drained_end(bs);
 
 out:
+    bdrv_co_lock(bs);
     blk_unref(blk);
-    aio_context_release(aio_context);
+    bdrv_co_unlock(bs);
 }
 
 void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
diff --git a/hmp-commands.hx b/hmp-commands.hx
index 1088d64503..de83a5cc61 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -76,6 +76,7 @@ ERST
         .params     = "device size",
         .help       = "resize a block image",
         .cmd        = hmp_block_resize,
+        .coroutine  = true,
     },
 
 SRST
-- 
2.25.4



  parent reply	other threads:[~2020-10-05 16:34 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-05 15:58 [PATCH v8 00/14] monitor: Optionally run handlers in coroutines Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 01/14] monitor: Add Monitor parameter to monitor_set_cpu() Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 02/14] monitor: Add Monitor parameter to monitor_get_cpu_index() Kevin Wolf
2020-10-05 16:34   ` Eric Blake
2020-10-05 15:58 ` [PATCH v8 03/14] monitor: Use getter/setter functions for cur_mon Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 04/14] hmp: Update current monitor only in handle_hmp_command() Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 05/14] qmp: Assert that no other monitor is active Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 06/14] qmp: Call monitor_set_cur() only in qmp_dispatch() Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 07/14] monitor: Make current monitor a per-coroutine property Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 08/14] qapi: Add a 'coroutine' flag for commands Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 09/14] qmp: Move dispatcher to a coroutine Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 10/14] hmp: Add support for coroutine command handlers Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 11/14] util/async: Add aio_co_reschedule_self() Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 12/14] block: Add bdrv_co_enter()/leave() Kevin Wolf
2020-10-05 15:58 ` [PATCH v8 13/14] block: Add bdrv_lock()/unlock() Kevin Wolf
2020-10-05 15:58 ` Kevin Wolf [this message]
2020-10-06  6:29 ` [PATCH v8 00/14] monitor: Optionally run handlers in coroutines Markus Armbruster
2020-10-08 15:53 ` Stefan Hajnoczi

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=20201005155855.256490-15-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=armbru@redhat.com \
    --cc=marcandre.lureau@gmail.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.