* [PATCH v2] block: Factor out bdrv_run_co()
@ 2020-05-19 17:56 Vladimir Sementsov-Ogievskiy
2020-05-19 18:28 ` Eric Blake
2020-05-20 14:05 ` Kevin Wolf
0 siblings, 2 replies; 5+ messages in thread
From: Vladimir Sementsov-Ogievskiy @ 2020-05-19 17:56 UTC (permalink / raw)
To: qemu-block; +Cc: kwolf, fam, vsementsov, qemu-devel, mreitz, stefanha, den
We have a few bdrv_*() functions that can either spawn a new coroutine
and wait for it with BDRV_POLL_WHILE() or use a fastpath if they are
alreeady running in a coroutine. All of them duplicate basically the
same code.
Factor the common code into a new function bdrv_run_co().
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
[Factor out bdrv_run_co_entry too]
---
Hi!
I'm a bit lost on rebasing "block/io: safer inc/dec in_flight sections"
(is it needed or not?), so, I decided to send just this one patch:
I suggest to go a bit further, and refactor that bdrv_run_co don't need
additional *ret argument neither NOT_DONE logic.
block/io.c | 191 ++++++++++++++++++++---------------------------------
1 file changed, 70 insertions(+), 121 deletions(-)
diff --git a/block/io.c b/block/io.c
index 121ce17a49..794eebbd0c 100644
--- a/block/io.c
+++ b/block/io.c
@@ -35,8 +35,6 @@
#include "qemu/main-loop.h"
#include "sysemu/replay.h"
-#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
-
/* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */
#define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
@@ -891,29 +889,61 @@ static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
return 0;
}
+typedef int coroutine_fn BdrvRequestEntry(void *opaque);
+typedef struct BdrvRunCo {
+ BdrvRequestEntry *entry;
+ void *opaque;
+ int ret;
+ bool done;
+} BdrvRunCo;
+
+static void coroutine_fn bdrv_run_co_entry(void *opaque)
+{
+ BdrvRunCo *arg = opaque;
+
+ arg->ret = arg->entry(arg->opaque);
+ arg->done = true;
+ aio_wait_kick();
+}
+
+static int bdrv_run_co(BlockDriverState *bs, BdrvRequestEntry *entry,
+ void *opaque)
+{
+ if (qemu_in_coroutine()) {
+ /* Fast-path if already in coroutine context */
+ return entry(opaque);
+ } else {
+ BdrvRunCo s = { .entry = entry, .opaque = opaque };
+
+ bdrv_coroutine_enter(bs, qemu_coroutine_create(bdrv_run_co_entry, &s));
+
+ BDRV_POLL_WHILE(bs, !s.done);
+
+ return s.ret;
+ }
+}
+
typedef struct RwCo {
BdrvChild *child;
int64_t offset;
QEMUIOVector *qiov;
bool is_write;
- int ret;
BdrvRequestFlags flags;
} RwCo;
-static void coroutine_fn bdrv_rw_co_entry(void *opaque)
+static int coroutine_fn bdrv_rw_co_entry(void *opaque)
{
RwCo *rwco = opaque;
if (!rwco->is_write) {
- rwco->ret = bdrv_co_preadv(rwco->child, rwco->offset,
- rwco->qiov->size, rwco->qiov,
- rwco->flags);
+ return bdrv_co_preadv(rwco->child, rwco->offset,
+ rwco->qiov->size, rwco->qiov,
+ rwco->flags);
} else {
- rwco->ret = bdrv_co_pwritev(rwco->child, rwco->offset,
- rwco->qiov->size, rwco->qiov,
- rwco->flags);
+ return bdrv_co_pwritev(rwco->child, rwco->offset,
+ rwco->qiov->size, rwco->qiov,
+ rwco->flags);
}
- aio_wait_kick();
}
/*
@@ -923,25 +953,15 @@ static int bdrv_prwv_co(BdrvChild *child, int64_t offset,
QEMUIOVector *qiov, bool is_write,
BdrvRequestFlags flags)
{
- Coroutine *co;
RwCo rwco = {
.child = child,
.offset = offset,
.qiov = qiov,
.is_write = is_write,
- .ret = NOT_DONE,
.flags = flags,
};
- if (qemu_in_coroutine()) {
- /* Fast-path if already in coroutine context */
- bdrv_rw_co_entry(&rwco);
- } else {
- co = qemu_coroutine_create(bdrv_rw_co_entry, &rwco);
- bdrv_coroutine_enter(child->bs, co);
- BDRV_POLL_WHILE(child->bs, rwco.ret == NOT_DONE);
- }
- return rwco.ret;
+ return bdrv_run_co(child->bs, bdrv_rw_co_entry, &rwco);
}
int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
@@ -2229,8 +2249,6 @@ typedef struct BdrvCoBlockStatusData {
int64_t *pnum;
int64_t *map;
BlockDriverState **file;
- int ret;
- bool done;
} BdrvCoBlockStatusData;
int coroutine_fn bdrv_co_block_status_from_file(BlockDriverState *bs,
@@ -2484,16 +2502,14 @@ static int coroutine_fn bdrv_co_block_status_above(BlockDriverState *bs,
}
/* Coroutine wrapper for bdrv_block_status_above() */
-static void coroutine_fn bdrv_block_status_above_co_entry(void *opaque)
+static int coroutine_fn bdrv_block_status_above_co_entry(void *opaque)
{
BdrvCoBlockStatusData *data = opaque;
- data->ret = bdrv_co_block_status_above(data->bs, data->base,
- data->want_zero,
- data->offset, data->bytes,
- data->pnum, data->map, data->file);
- data->done = true;
- aio_wait_kick();
+ return bdrv_co_block_status_above(data->bs, data->base,
+ data->want_zero,
+ data->offset, data->bytes,
+ data->pnum, data->map, data->file);
}
/*
@@ -2508,7 +2524,6 @@ static int bdrv_common_block_status_above(BlockDriverState *bs,
int64_t *map,
BlockDriverState **file)
{
- Coroutine *co;
BdrvCoBlockStatusData data = {
.bs = bs,
.base = base,
@@ -2518,18 +2533,9 @@ static int bdrv_common_block_status_above(BlockDriverState *bs,
.pnum = pnum,
.map = map,
.file = file,
- .done = false,
};
- if (qemu_in_coroutine()) {
- /* Fast-path if already in coroutine context */
- bdrv_block_status_above_co_entry(&data);
- } else {
- co = qemu_coroutine_create(bdrv_block_status_above_co_entry, &data);
- bdrv_coroutine_enter(bs, co);
- BDRV_POLL_WHILE(bs, !data.done);
- }
- return data.ret;
+ return bdrv_run_co(bs, bdrv_block_status_above_co_entry, &data);
}
int bdrv_block_status_above(BlockDriverState *bs, BlockDriverState *base,
@@ -2630,7 +2636,6 @@ typedef struct BdrvVmstateCo {
QEMUIOVector *qiov;
int64_t pos;
bool is_read;
- int ret;
} BdrvVmstateCo;
static int coroutine_fn
@@ -2658,33 +2663,25 @@ bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
return ret;
}
-static void coroutine_fn bdrv_co_rw_vmstate_entry(void *opaque)
+static int coroutine_fn bdrv_co_rw_vmstate_entry(void *opaque)
{
BdrvVmstateCo *co = opaque;
- co->ret = bdrv_co_rw_vmstate(co->bs, co->qiov, co->pos, co->is_read);
- aio_wait_kick();
+
+ return bdrv_co_rw_vmstate(co->bs, co->qiov, co->pos, co->is_read);
}
static inline int
bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
bool is_read)
{
- if (qemu_in_coroutine()) {
- return bdrv_co_rw_vmstate(bs, qiov, pos, is_read);
- } else {
- BdrvVmstateCo data = {
- .bs = bs,
- .qiov = qiov,
- .pos = pos,
- .is_read = is_read,
- .ret = -EINPROGRESS,
- };
- Coroutine *co = qemu_coroutine_create(bdrv_co_rw_vmstate_entry, &data);
+ BdrvVmstateCo data = {
+ .bs = bs,
+ .qiov = qiov,
+ .pos = pos,
+ .is_read = is_read,
+ };
- bdrv_coroutine_enter(bs, co);
- BDRV_POLL_WHILE(bs, data.ret == -EINPROGRESS);
- return data.ret;
- }
+ return bdrv_run_co(bs, bdrv_co_rw_vmstate_entry, &data);
}
int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
@@ -2762,18 +2759,9 @@ void bdrv_aio_cancel_async(BlockAIOCB *acb)
/**************************************************************/
/* Coroutine block device emulation */
-typedef struct FlushCo {
- BlockDriverState *bs;
- int ret;
-} FlushCo;
-
-
-static void coroutine_fn bdrv_flush_co_entry(void *opaque)
+static int coroutine_fn bdrv_flush_co_entry(void *opaque)
{
- FlushCo *rwco = opaque;
-
- rwco->ret = bdrv_co_flush(rwco->bs);
- aio_wait_kick();
+ return bdrv_co_flush(opaque);
}
int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
@@ -2890,36 +2878,20 @@ early_exit:
int bdrv_flush(BlockDriverState *bs)
{
- Coroutine *co;
- FlushCo flush_co = {
- .bs = bs,
- .ret = NOT_DONE,
- };
-
- if (qemu_in_coroutine()) {
- /* Fast-path if already in coroutine context */
- bdrv_flush_co_entry(&flush_co);
- } else {
- co = qemu_coroutine_create(bdrv_flush_co_entry, &flush_co);
- bdrv_coroutine_enter(bs, co);
- BDRV_POLL_WHILE(bs, flush_co.ret == NOT_DONE);
- }
-
- return flush_co.ret;
+ return bdrv_run_co(bs, bdrv_flush_co_entry, bs);
}
typedef struct DiscardCo {
BdrvChild *child;
int64_t offset;
int64_t bytes;
- int ret;
} DiscardCo;
-static void coroutine_fn bdrv_pdiscard_co_entry(void *opaque)
+
+static int coroutine_fn bdrv_pdiscard_co_entry(void *opaque)
{
DiscardCo *rwco = opaque;
- rwco->ret = bdrv_co_pdiscard(rwco->child, rwco->offset, rwco->bytes);
- aio_wait_kick();
+ return bdrv_co_pdiscard(rwco->child, rwco->offset, rwco->bytes);
}
int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
@@ -3038,24 +3010,13 @@ out:
int bdrv_pdiscard(BdrvChild *child, int64_t offset, int64_t bytes)
{
- Coroutine *co;
DiscardCo rwco = {
.child = child,
.offset = offset,
.bytes = bytes,
- .ret = NOT_DONE,
};
- if (qemu_in_coroutine()) {
- /* Fast-path if already in coroutine context */
- bdrv_pdiscard_co_entry(&rwco);
- } else {
- co = qemu_coroutine_create(bdrv_pdiscard_co_entry, &rwco);
- bdrv_coroutine_enter(child->bs, co);
- BDRV_POLL_WHILE(child->bs, rwco.ret == NOT_DONE);
- }
-
- return rwco.ret;
+ return bdrv_run_co(child->bs, bdrv_pdiscard_co_entry, &rwco);
}
int bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf)
@@ -3463,21 +3424,19 @@ typedef struct TruncateCo {
PreallocMode prealloc;
BdrvRequestFlags flags;
Error **errp;
- int ret;
} TruncateCo;
-static void coroutine_fn bdrv_truncate_co_entry(void *opaque)
+static int coroutine_fn bdrv_truncate_co_entry(void *opaque)
{
TruncateCo *tco = opaque;
- tco->ret = bdrv_co_truncate(tco->child, tco->offset, tco->exact,
- tco->prealloc, tco->flags, tco->errp);
- aio_wait_kick();
+
+ return bdrv_co_truncate(tco->child, tco->offset, tco->exact,
+ tco->prealloc, tco->flags, tco->errp);
}
int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
PreallocMode prealloc, BdrvRequestFlags flags, Error **errp)
{
- Coroutine *co;
TruncateCo tco = {
.child = child,
.offset = offset,
@@ -3485,17 +3444,7 @@ int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
.prealloc = prealloc,
.flags = flags,
.errp = errp,
- .ret = NOT_DONE,
};
- if (qemu_in_coroutine()) {
- /* Fast-path if already in coroutine context */
- bdrv_truncate_co_entry(&tco);
- } else {
- co = qemu_coroutine_create(bdrv_truncate_co_entry, &tco);
- bdrv_coroutine_enter(child->bs, co);
- BDRV_POLL_WHILE(child->bs, tco.ret == NOT_DONE);
- }
-
- return tco.ret;
+ return bdrv_run_co(child->bs, bdrv_truncate_co_entry, &tco);
}
--
2.21.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] block: Factor out bdrv_run_co()
2020-05-19 17:56 [PATCH v2] block: Factor out bdrv_run_co() Vladimir Sementsov-Ogievskiy
@ 2020-05-19 18:28 ` Eric Blake
2020-05-20 14:05 ` Kevin Wolf
1 sibling, 0 replies; 5+ messages in thread
From: Eric Blake @ 2020-05-19 18:28 UTC (permalink / raw)
To: Vladimir Sementsov-Ogievskiy, qemu-block
Cc: kwolf, fam, qemu-devel, mreitz, stefanha, den
On 5/19/20 12:56 PM, Vladimir Sementsov-Ogievskiy wrote:
> We have a few bdrv_*() functions that can either spawn a new coroutine
> and wait for it with BDRV_POLL_WHILE() or use a fastpath if they are
> alreeady running in a coroutine. All of them duplicate basically the
already
> same code.
>
> Factor the common code into a new function bdrv_run_co().
>
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> [Factor out bdrv_run_co_entry too]
> ---
>
> Hi!
>
> I'm a bit lost on rebasing "block/io: safer inc/dec in_flight sections"
> (is it needed or not?), so, I decided to send just this one patch:
>
> I suggest to go a bit further, and refactor that bdrv_run_co don't need
> additional *ret argument neither NOT_DONE logic.
Yes, and this approach was easier to review than v1.
>
> block/io.c | 191 ++++++++++++++++++++---------------------------------
> 1 file changed, 70 insertions(+), 121 deletions(-)
>
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] block: Factor out bdrv_run_co()
2020-05-19 17:56 [PATCH v2] block: Factor out bdrv_run_co() Vladimir Sementsov-Ogievskiy
2020-05-19 18:28 ` Eric Blake
@ 2020-05-20 14:05 ` Kevin Wolf
2020-05-20 14:49 ` Kevin Wolf
1 sibling, 1 reply; 5+ messages in thread
From: Kevin Wolf @ 2020-05-20 14:05 UTC (permalink / raw)
To: Vladimir Sementsov-Ogievskiy
Cc: fam, qemu-block, qemu-devel, mreitz, stefanha, den
Am 19.05.2020 um 19:56 hat Vladimir Sementsov-Ogievskiy geschrieben:
> We have a few bdrv_*() functions that can either spawn a new coroutine
> and wait for it with BDRV_POLL_WHILE() or use a fastpath if they are
> alreeady running in a coroutine. All of them duplicate basically the
> same code.
>
> Factor the common code into a new function bdrv_run_co().
>
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> [Factor out bdrv_run_co_entry too]
> ---
>
> Hi!
>
> I'm a bit lost on rebasing "block/io: safer inc/dec in_flight sections"
> (is it needed or not?), so, I decided to send just this one patch:
>
> I suggest to go a bit further, and refactor that bdrv_run_co don't need
> additional *ret argument neither NOT_DONE logic.
Hm, this approach adds another indirection and bdrv_pread/pwrite still
seems to be on some hot paths. But maybe this is just the right
motivation to clean up qcow2 a bit and use explicit bdrv_co_*() where it
is possible. I might take a look later.
> block/io.c | 191 ++++++++++++++++++++---------------------------------
> 1 file changed, 70 insertions(+), 121 deletions(-)
>
> diff --git a/block/io.c b/block/io.c
> index 121ce17a49..794eebbd0c 100644
> --- a/block/io.c
> +++ b/block/io.c
> @@ -35,8 +35,6 @@
> #include "qemu/main-loop.h"
> #include "sysemu/replay.h"
>
> -#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
> -
> /* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */
> #define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS)
>
> @@ -891,29 +889,61 @@ static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
> return 0;
> }
>
> +typedef int coroutine_fn BdrvRequestEntry(void *opaque);
> +typedef struct BdrvRunCo {
> + BdrvRequestEntry *entry;
> + void *opaque;
> + int ret;
> + bool done;
> +} BdrvRunCo;
> +
> +static void coroutine_fn bdrv_run_co_entry(void *opaque)
> +{
> + BdrvRunCo *arg = opaque;
> +
> + arg->ret = arg->entry(arg->opaque);
> + arg->done = true;
> + aio_wait_kick();
> +}
> +
> +static int bdrv_run_co(BlockDriverState *bs, BdrvRequestEntry *entry,
> + void *opaque)
> +{
> + if (qemu_in_coroutine()) {
> + /* Fast-path if already in coroutine context */
> + return entry(opaque);
> + } else {
> + BdrvRunCo s = { .entry = entry, .opaque = opaque };
> +
> + bdrv_coroutine_enter(bs, qemu_coroutine_create(bdrv_run_co_entry, &s));
Let's keep the coroutine in a separate variable, maybe inside BdrvRunCo.
It's important for debugging BDRV_POLL_WHILE() hangs in gdb.
> +
> + BDRV_POLL_WHILE(bs, !s.done);
> +
> + return s.ret;
> + }
> +}
Kevin
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] block: Factor out bdrv_run_co()
2020-05-20 14:05 ` Kevin Wolf
@ 2020-05-20 14:49 ` Kevin Wolf
2020-05-20 15:01 ` Vladimir Sementsov-Ogievskiy
0 siblings, 1 reply; 5+ messages in thread
From: Kevin Wolf @ 2020-05-20 14:49 UTC (permalink / raw)
To: Vladimir Sementsov-Ogievskiy
Cc: fam, qemu-block, qemu-devel, mreitz, stefanha, den
Am 20.05.2020 um 16:05 hat Kevin Wolf geschrieben:
> Am 19.05.2020 um 19:56 hat Vladimir Sementsov-Ogievskiy geschrieben:
> > We have a few bdrv_*() functions that can either spawn a new coroutine
> > and wait for it with BDRV_POLL_WHILE() or use a fastpath if they are
> > alreeady running in a coroutine. All of them duplicate basically the
> > same code.
> >
> > Factor the common code into a new function bdrv_run_co().
> >
> > Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> > Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> > [Factor out bdrv_run_co_entry too]
> > ---
> >
> > Hi!
> >
> > I'm a bit lost on rebasing "block/io: safer inc/dec in_flight sections"
> > (is it needed or not?), so, I decided to send just this one patch:
> >
> > I suggest to go a bit further, and refactor that bdrv_run_co don't need
> > additional *ret argument neither NOT_DONE logic.
>
> Hm, this approach adds another indirection and bdrv_pread/pwrite still
> seems to be on some hot paths. But maybe this is just the right
> motivation to clean up qcow2 a bit and use explicit bdrv_co_*() where it
> is possible. I might take a look later.
Still not easily possible it seems. We can add a few coroutine_fn
markers here and there (and probably should do that), but the
interesting I/O is in the Qcow2Cache, which is used from basically
everywhere.
Kevin
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] block: Factor out bdrv_run_co()
2020-05-20 14:49 ` Kevin Wolf
@ 2020-05-20 15:01 ` Vladimir Sementsov-Ogievskiy
0 siblings, 0 replies; 5+ messages in thread
From: Vladimir Sementsov-Ogievskiy @ 2020-05-20 15:01 UTC (permalink / raw)
To: Kevin Wolf; +Cc: fam, qemu-block, qemu-devel, mreitz, stefanha, den
20.05.2020 17:49, Kevin Wolf wrote:
> Am 20.05.2020 um 16:05 hat Kevin Wolf geschrieben:
>> Am 19.05.2020 um 19:56 hat Vladimir Sementsov-Ogievskiy geschrieben:
>>> We have a few bdrv_*() functions that can either spawn a new coroutine
>>> and wait for it with BDRV_POLL_WHILE() or use a fastpath if they are
>>> alreeady running in a coroutine. All of them duplicate basically the
>>> same code.
>>>
>>> Factor the common code into a new function bdrv_run_co().
>>>
>>> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
>>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
>>> [Factor out bdrv_run_co_entry too]
>>> ---
>>>
>>> Hi!
>>>
>>> I'm a bit lost on rebasing "block/io: safer inc/dec in_flight sections"
>>> (is it needed or not?), so, I decided to send just this one patch:
>>>
>>> I suggest to go a bit further, and refactor that bdrv_run_co don't need
>>> additional *ret argument neither NOT_DONE logic.
>>
>> Hm, this approach adds another indirection and bdrv_pread/pwrite still
>> seems to be on some hot paths. But maybe this is just the right
>> motivation to clean up qcow2 a bit and use explicit bdrv_co_*() where it
>> is possible. I might take a look later.
>
> Still not easily possible it seems. We can add a few coroutine_fn
> markers here and there (and probably should do that), but the
> interesting I/O is in the Qcow2Cache, which is used from basically
> everywhere.
>
Hmm. Calling *_entry on in-coroutine path is also an extra inderection.
I've posted another solution for this think: auto generation of coroutine wrappers code, which produce no extra inderection.
RFC was here, a year ago: https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg05322.html
May be, it's time to resend it, as your point gives it a point.
--
Best regards,
Vladimir
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-05-20 15:03 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-19 17:56 [PATCH v2] block: Factor out bdrv_run_co() Vladimir Sementsov-Ogievskiy
2020-05-19 18:28 ` Eric Blake
2020-05-20 14:05 ` Kevin Wolf
2020-05-20 14:49 ` Kevin Wolf
2020-05-20 15:01 ` Vladimir Sementsov-Ogievskiy
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.