block: Make raw_co_get_allocated_file_size asynchronous
cover block: Make raw_co_get_allocated_file_size asynchronous
As discussed in another thread [1], here's an RFC addressing a VCPU softlockup encountered when issuing QMP commands that target a disk placed on NFS.
Since QMP commands happen with the qemu_global_mutex locked, any command that takes too long to finish will block other threads waiting to take the global mutex. One such thread could be a VCPU thread going out of the guest to handle IO.
This is the case when issuing the QMP command query-block, which eventually calls raw_co_get_allocated_file_size(). This function makes an 'fstat' call that has been observed to take a long time (seconds) over NFS.
NFS latency issues aside, we can improve the situation by not blocking VCPU threads while the command is running.
Move the 'fstat' call into the thread-pool and make the necessary adaptations to ensure raw_co_get_allocated_file_size runs in a coroutine in the block driver aio_context.
1- Question about QMP and BQL
https://lists.gnu.org/archive/html/qemu-devel/2023-05/msg03141.html
CI run: https://gitlab.com/farosas/qemu/-/pipelines/876583685
Fabiano Rosas (3):
block: Remove bdrv_query_block_node_info
block: Mark bdrv_co_get_allocated_file_size() as mixed
block: Allow bdrv_get_allocated_file_size to run in bdrv context
João Silva (1):
block: Add a thread-pool version of fstat
Lin Ma (2):
Convert query-block/info_block to coroutine
Convert query-block/info_block to coroutine
block/file-posix.c | 40 ++++++++++++++++++++++++-- block/monitor/block-hmp-cmds.c | 2 +- block/qapi.c | 51 +++++++++++++++------------------- blockdev.c | 6 ++-- hmp-commands-info.hx | 1 + include/block/block-hmp-cmds.h | 2 +- include/block/block-io.h | 2 +- include/block/qapi.h | 3 -- include/block/raw-aio.h | 4 ++- qapi/block-core.json | 5 ++-- 10 files changed, 72 insertions(+), 44 deletions(-)
Signature · 1 line
2.35.3
RFC PATCH 1/6 block: Remove bdrv_query_block_node_info
The last call site of this function has been removed by commit c04d0ab026 ("qemu-img: Let info print block graph").
--- block/qapi.c | 27 --------------------------- include/block/qapi.h | 3 --- 2 files changed, 30 deletions(-)
block/qapi.c +0 -27
@@ -309,33 +309,6 @@ out:aio_context_release (bdrv_get_aio_context (bs)); } -/** -* bdrv_query_block_node_info: -* @bs: block node to examine -* @p_info: location to store node information -* @errp: location to store error information -* -* Store image information about @bs in @p_info. -* -* @p_info will be set only on success. On error, store error in @errp. -*/ -void bdrv_query_block_node_info (BlockDriverState *bs, -BlockNodeInfo **p_info, -Error **errp) -{ -BlockNodeInfo *info; -ERRP_GUARD (); - - info =g_new0 (BlockNodeInfo,1 ); -bdrv_do_query_node_info (bs, info, errp); -if (*errp) { -qapi_free_BlockNodeInfo (info); -return ; - } - - *p_info = info; -} -/** * bdrv_query_image_info: * @bs: block node to examine
include/block/qapi.h +0 -3
@@ -36,9 +36,6 @@ BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,int bdrv_query_snapshot_info_list (BlockDriverState *bs,SnapshotInfoList **p_list,Error **errp); -void bdrv_query_block_node_info (BlockDriverState *bs, -BlockNodeInfo **p_info, -Error **errp); void bdrv_query_image_info(BlockDriverState *bs, ImageInfo **p_info, bool flat,
Signature · 1 line
2.35.3
Re: [RFC PATCH 1/6] block: Remove bdrv_query_block_node_info
Quoted 52 lines · Fabiano Rosas
The last call site of this function has been removed by commit
c04d0ab026("qemu-img: Let info print block graph").Signed-off-by Fabiano Rosas <[email protected]>--- block/qapi.c | 27 --------------------------- include/block/qapi.h | 3 --- 2 files changed, 30 deletions(-)block/qapi.c +0
-27@@ -309,33 +309,6 @@ out:aio_context_release (bdrv_get_aio_context (bs)); } -/** -* bdrv_query_block_node_info: -* @bs: block node to examine -* @p_info: location to store node information -* @errp: location to store error information -* -* Store image information about @bs in @p_info. -* -* @p_info will be set only on success. On error, store error in @errp. -*/ -void bdrv_query_block_node_info (BlockDriverState *bs, -BlockNodeInfo **p_info, -Error **errp) -{ -BlockNodeInfo *info; -ERRP_GUARD (); - - info =g_new0 (BlockNodeInfo,1 ); -bdrv_do_query_node_info (bs, info, errp); -if (*errp) { -qapi_free_BlockNodeInfo (info); -return ; - } - - *p_info = info; -} -/** * bdrv_query_image_info: * @bs: block node to examine include/block/qapi.h +0
-0
include/block/qapi.h BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, · line 41
Quoted 9 lines
include/block/qapi.h +0
-3@@ -36,9 +36,6 @@ BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,int bdrv_query_snapshot_info_list (BlockDriverState *bs,SnapshotInfoList **p_list,Error **errp); -void bdrv_query_block_node_info (BlockDriverState *bs, -BlockNodeInfo **p_info, -Error **errp); void bdrv_query_image_info(BlockDriverState *bs, ImageInfo **p_info, bool flat,
RFC PATCH 2/6 block: Mark bdrv_co_get_allocated_file_size() as mixed
In reply to Fabiano RosasSome callers of this function are about to be converted to use coroutines, so allow it to be executed both inside and outside a coroutine.
--- include/block/block-io.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/block/block-io.h +1 -1
@@ -87,7 +87,7 @@ int64_t co_wrapper_mixed_bdrv_rdlock bdrv_getlength(BlockDriverState *bs); int64_t coroutine_fn GRAPH_RDLOCKbdrv_co_get_allocated_file_size (BlockDriverState *bs); -int64_t co_wrapper_bdrv_rdlock +int64_t co_wrapper_mixed_bdrv_rdlockbdrv_get_allocated_file_size (BlockDriverState *bs); BlockMeasureInfo *bdrv_measure(BlockDriver *drv, QemuOpts *opts,
Signature · 1 line
2.35.3
Re: [RFC PATCH 2/6] block: Mark bdrv_co_get_allocated_file_size() as mixed
Quoted 8 lines · Fabiano Rosas
Some callers of this function are about to be converted to use coroutines, so allow it to be executed both inside and outside a coroutine.
Signed-off-by Fabiano Rosas <[email protected]>--- include/block/block-io.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
include/block/block-io.h +1
-1@@ -87,7 +87,7 @@ int64_t co_wrapper_mixed_bdrv_rdlock bdrv_getlength(BlockDriverState *bs); int64_t coroutine_fn GRAPH_RDLOCKbdrv_co_get_allocated_file_size (BlockDriverState *bs); -int64_t co_wrapper_bdrv_rdlock +int64_t co_wrapper_mixed_bdrv_rdlockbdrv_get_allocated_file_size (BlockDriverState *bs); BlockMeasureInfo *bdrv_measure(BlockDriver *drv, QemuOpts *opts,Signature · 1 line
2.35.3
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [RFC PATCH 2/6] block: Mark bdrv_co_get_allocated_file_size() as mixed
In reply to Fabiano RosasAm 23.05.2023 um 23:38 hat Fabiano Rosas geschrieben:
Quoted 5 lines
Some callers of this function are about to be converted to use coroutines, so allow it to be executed both inside and outside a coroutine.
Signed-off-by Fabiano Rosas <[email protected]>
This is not a sufficient justification for introducing a new mixed function (we want to get rid of them, not add new ones).
You need to explain why the new coroutine callers can't directly call bdrv_co_get_allocated_file_size() instead of going through the wrapper. This is usually only the case if you have a function that doesn't know whether it runs in coroutine context or not. Functions that you explicitly convert to coroutine_fn know for sure.
Quoted 8 lines
include/block/block-io.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/block/block-io.h +0
-0
include/block/block-io.h int64_t co_wrapper_mixed_bdrv_rdlock bdrv_getlength(BlockDriverState *bs); · line 91
Quoted 6 lines
include/block/block-io.h +1
-1@@ -87,7 +87,7 @@ int64_t co_wrapper_mixed_bdrv_rdlock bdrv_getlength(BlockDriverState *bs); int64_t coroutine_fn GRAPH_RDLOCKbdrv_co_get_allocated_file_size (BlockDriverState *bs); -int64_t co_wrapper_bdrv_rdlock +int64_t co_wrapper_mixed_bdrv_rdlockbdrv_get_allocated_file_size (BlockDriverState *bs);
You're changing bdrv_get_allocated_file_size() (which is the function you really mean), but the subject line talks about bdrv_co_get_allocated_file_size().
Kevin
RFC PATCH 3/6 Convert query-block/info_block to coroutine
In reply to Fabiano RosasFrom: Lin Ma <[email protected]>
Sometimes the query-block performs time-consuming I/O(say waiting for the fstat of NFS complete), So let's make this QMP handler runs in a coroutine.
The following patch moves the fstat() into a thread pool.
--- block/monitor/block-hmp-cmds.c | 2 +- block/qapi.c | 2 +- hmp-commands-info.hx | 1 + include/block/block-hmp-cmds.h | 2 +- qapi/block-core.json | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-)
block/monitor/block-hmp-cmds.c +1 -1
@@ -738,7 +738,7 @@ static void print_block_info(Monitor *mon, BlockInfo *info, } } -void hmp_info_block (Monitor *mon,const QDict *qdict) +void coroutine_fn hmp_info_block(Monitor *mon, const QDict *qdict) {BlockInfoList *block_list, *info;BlockDeviceInfoList *blockdev_list, *blockdev;
block/qapi.c +1 -1
@@ -667,7 +667,7 @@ bdrv_query_bds_stats(BlockDriverState *bs, bool blk_level)return s; } -BlockInfoList *qmp_query_block (Error **errp) +BlockInfoList *coroutine_fn qmp_query_block(Error **errp) {BlockInfoList *head =NULL , **p_next = &head;BlockBackend *blk;
hmp-commands-info.hx +1 -0
@@ -65,6 +65,7 @@ ERST .help = "show info of one block device or all block devices " "(-n: show named nodes; -v: show details)", .cmd = hmp_info_block, + .coroutine = true, }, SRST
include/block/block-hmp-cmds.h +1 -1
@@ -48,7 +48,7 @@ void hmp_eject(Monitor *mon, const QDict *qdict);void hmp_qemu_io (Monitor *mon,const QDict *qdict); -void hmp_info_block (Monitor *mon,const QDict *qdict); +void coroutine_fn hmp_info_block(Monitor *mon, const QDict *qdict);void hmp_info_blockstats (Monitor *mon,const QDict *qdict);void hmp_info_block_jobs (Monitor *mon,const QDict *qdict);void hmp_info_snapshots (Monitor *mon,const QDict *qdict);
qapi/block-core.json +1 -1
@@ -838,7 +838,7 @@ # } ## { 'command': 'query-block', 'returns': ['BlockInfo'], - 'allow-preconfig': true } + 'allow-preconfig': true, 'coroutine': true } ## # @BlockDeviceTimedStats:
Signature · 1 line
2.35.3
Re: [RFC PATCH 3/6] Convert query-block/info_block to coroutine
Quoted 7 lines · Fabiano Rosas
From: Lin Ma <[email protected]>
Sometimes the query-block performs time-consuming I/O(say waiting for the fstat of NFS complete), So let's make this QMP handler runs in a coroutine.
The following patch moves the
fstat()into a thread pool.
Hi, this message talks about QMP and query-block only and the subject talks about info_block as well. We need to clarify this.
Also, lets make it clear that one is a QMP command, and the other is an HMP command.
In any case I would say:
"Convert QMP command query-block to use a coroutine", and in case we also need info_block, an additional patch could take care of the HMP command with a subject like:
"Convert HMP command info_block to use a coroutine".
Ciao,
Claudio
Quoted 75 lines
Signed-off-by Lin Ma <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>--- block/monitor/block-hmp-cmds.c | 2 +- block/qapi.c | 2 +- hmp-commands-info.hx | 1 + include/block/block-hmp-cmds.h | 2 +- qapi/block-core.json | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-)block/monitor/block-hmp-cmds.c +1
-1@@ -738,7 +738,7 @@ static void print_block_info(Monitor *mon, BlockInfo *info, } } -void hmp_info_block (Monitor *mon,const QDict *qdict) +void coroutine_fn hmp_info_block(Monitor *mon, const QDict *qdict) {BlockInfoList *block_list, *info;BlockDeviceInfoList *blockdev_list, *blockdev;block/qapi.c +1
-1@@ -667,7 +667,7 @@ bdrv_query_bds_stats(BlockDriverState *bs, bool blk_level)return s; } -BlockInfoList *qmp_query_block (Error **errp) +BlockInfoList *coroutine_fn qmp_query_block(Error **errp) {BlockInfoList *head =NULL , **p_next = &head;BlockBackend *blk;hmp-commands-info.hx +1
-0@@ -65,6 +65,7 @@ ERST .help = "show info of one block device or all block devices " "(-n: show named nodes; -v: show details)", .cmd = hmp_info_block, + .coroutine = true, }, SRSTinclude/block/block-hmp-cmds.h +1
-1@@ -48,7 +48,7 @@ void hmp_eject(Monitor *mon, const QDict *qdict);void hmp_qemu_io (Monitor *mon,const QDict *qdict); -void hmp_info_block (Monitor *mon,const QDict *qdict); +void coroutine_fn hmp_info_block(Monitor *mon, const QDict *qdict);void hmp_info_blockstats (Monitor *mon,const QDict *qdict);void hmp_info_block_jobs (Monitor *mon,const QDict *qdict);void hmp_info_snapshots (Monitor *mon,const QDict *qdict);qapi/block-core.json +1
-1@@ -838,7 +838,7 @@ # } ## { 'command': 'query-block', 'returns': ['BlockInfo'], - 'allow-preconfig': true } + 'allow-preconfig': true, 'coroutine': true } ## # @BlockDeviceTimedStats:
Re: [RFC PATCH 3/6] Convert query-block/info_block to coroutine
In reply to Fabiano RosasQuoted 5 lines · Fabiano Rosas
From: Lin Ma <[email protected]>
Sometimes the query-block performs time-consuming I/O(say waiting for the fstat of NFS complete), So let's make this QMP handler runs in a coroutine.
Grammar suggestions:
Sometimes the query-block command performs time-consuming I/O (say waiting for the fstat of NFS to complete), so let's make this QMP handler run in a coroutine.
Quoted 9 lines
The following patch moves the
fstat()into a thread pool.Signed-off-by Lin Ma <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>---
+++b/qapi/block-core.json
qapi/block-core.json line 841
Quoted 5 lines
qapi/block-core.json +1
-1@@ -838,7 +838,7 @@ # } ## { 'command': 'query-block', 'returns': ['BlockInfo'], - 'allow-preconfig': true } + 'allow-preconfig': true, 'coroutine': true }
Rereading docs/devel/qapi-code-gen.rst:
| Coroutine safety can be hard to prove, similar to thread safety. Common | pitfalls are: | | - The global mutex isn't held across ``qemu_coroutine_yield()``, so | operations that used to assume that they execute atomically may have | to be more careful to protect against changes in the global state. | | - Nested event loops (``AIO_WAIT_WHILE()`` etc.) are problematic in | coroutine context and can easily lead to deadlocks. They should be | replaced by yielding and reentering the coroutine when the condition | becomes false. | | Since the command handler may assume coroutine context, any callers | other than the QMP dispatcher must also call it in coroutine context. | In particular, HMP commands calling such a QMP command handler must be | marked ``.coroutine = true`` in hmp-commands.hx.
I agree that you also need to change the HMP handler, but the commit message didn't mention that other than in the subject line. The commit message could also do a better job at explaining how you have audited that merely adding the coroutine marker is safe (ie. that all of the calls made by query_block are already coroutine safe). While the intent behind this patch is on the right track, I'm not sure if I have enough information to say whether it is safe, or if there are other lurking problems we will need to fix first.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [RFC PATCH 3/6] Convert query-block/info_block to coroutine
Quoted 7 lines · Eric Blake
Quoted 5 lines · Fabiano Rosas
From: Lin Ma <[email protected]>
Sometimes the query-block performs time-consuming I/O(say waiting for the fstat of NFS complete), So let's make this QMP handler runs in a coroutine.
Quoted 6 lines
Grammar suggestions:
Sometimes the query-block command performs time-consuming I/O (say waiting for the fstat of NFS to complete), so let's make this QMP handler run in a coroutine.
Thanks!
Quoted 34 lines
Quoted 9 lines
The following patch moves the
fstat()into a thread pool.Signed-off-by Lin Ma <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>---
+++b/qapi/block-core.jsonqapi/block-core.json line 841
Quoted 5 lines
qapi/block-core.json +1
-1@@ -838,7 +838,7 @@ # } ## { 'command': 'query-block', 'returns': ['BlockInfo'], - 'allow-preconfig': true } + 'allow-preconfig': true, 'coroutine': true }Rereading
docs/devel/qapi-code-gen.rst:| Coroutine safety can be hard to prove, similar to thread safety. Common | pitfalls are: | | - The global mutex isn't held across ``qemu_coroutine_yield()``, so | operations that used to assume that they execute atomically may have | to be more careful to protect against changes in the global state. | | - Nested event loops (``AIO_WAIT_WHILE()`` etc.) are problematic in | coroutine context and can easily lead to deadlocks. They should be | replaced by yielding and reentering the coroutine when the condition | becomes false. | | Since the command handler may assume coroutine context, any callers | other than the QMP dispatcher must also call it in coroutine context. | In particular, HMP commands calling such a QMP command handler must be | marked ``.coroutine = true`` in hmp-commands.hx.
Quoted 2 lines
I agree that you also need to change the HMP handler, but the commit message didn't mention that other than in the subject line. The
Ok, I'll update the message for v2.
Quoted 6 lines
commit message could also do a better job at explaining how you have audited that merely adding the coroutine marker is safe (ie. that all of the calls made by
query_blockare already coroutine safe). While the intent behind this patch is on the right track, I'm not sure if I have enough information to say whether it is safe, or if there are other lurking problems we will need to fix first.
Fair point, I've been trusting the tests too much. A closer code audit is necessary indeed.
RFC PATCH 4/6 Convert query-block/info_block to coroutine
In reply to Fabiano RosasFrom: Lin Ma <[email protected]>
Sometimes the query-block performs time-consuming I/O(say waiting for the fstat of NFS complete), So let's make this QMP handler runs in a coroutine.
The following patch moves the fstat() into a thread pool.
--- blockdev.c | 6 +++--- qapi/block-core.json | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-)
blockdev.c +3 -3
@@ -2804,9 +2804,9 @@ void qmp_drive_backup(DriveBackup *backup, Error **errp)blockdev_do_action (&action, errp); } -BlockDeviceInfoList *qmp_query_named_block_nodes (bool has_flat, -bool flat, -Error **errp) +BlockDeviceInfoList *coroutine_fn qmp_query_named_block_nodes(bool has_flat, + bool flat, + Error **errp) {bool return_flat = has_flat && flat;
qapi/block-core.json +2 -1
@@ -1972,7 +1972,8 @@ { 'command': 'query-named-block-nodes', 'returns': [ 'BlockDeviceInfo' ], 'data': { '*flat': 'bool' }, - 'allow-preconfig': true } + 'allow-preconfig': true, + 'coroutine': true} ## # @XDbgBlockGraphNodeType:
Signature · 1 line
2.35.3
Re: [RFC PATCH 4/6] Convert query-block/info_block to coroutine
The commit title/message are duplicated to previous one, Here should use "query-named-block-nodes" instead.
Lin
________________________________________
From: Fabiano Rosas <[email protected]>
Sent: Wednesday, May 24, 2023 5:39 AM
To: [email protected]
Cc: [email protected]; Kevin Wolf; Hanna Reitz; Markus Armbruster; João Silva; Lin Ma; Claudio Fontana; Dario Faggioli; Eric Blake
Subject: [RFC PATCH 4/6] Convert query-block/info_block to coroutine
From: Lin Ma <[email protected]>
Sometimes the query-block performs time-consuming I/O(say waiting for the fstat of NFS complete), So let's make this QMP handler runs in a coroutine.
The following patch moves the fstat() into a thread pool.
--- blockdev.c | 6 +++--- qapi/block-core.json | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-)
blockdev.c +3 -3
@@ -2804,9 +2804,9 @@ void qmp_drive_backup(DriveBackup *backup, Error **errp)blockdev_do_action (&action, errp); } -BlockDeviceInfoList *qmp_query_named_block_nodes (bool has_flat, -bool flat, -Error **errp) +BlockDeviceInfoList *coroutine_fn qmp_query_named_block_nodes(bool has_flat, + bool flat, + Error **errp) {bool return_flat = has_flat && flat;
qapi/block-core.json +2 -1
@@ -1972,7 +1972,8 @@ { 'command': 'query-named-block-nodes', 'returns': [ 'BlockDeviceInfo' ], 'data': { '*flat': 'bool' }, - 'allow-preconfig': true } + 'allow-preconfig': true, + 'coroutine': true} ## # @XDbgBlockGraphNodeType:
--
2.35.3
Re: [RFC PATCH 4/6] Convert query-block/info_block to coroutine
Quoted 5 lines · Lin Ma
The commit title/message are duplicated to previous one, Here should use "query-named-block-nodes" instead.
Lin
Ugh, what a blunder, they're even nicely aligned in the git log. I'll fix it in the next version.
Thanks!
Re: [RFC PATCH 4/6] Convert query-block/info_block to coroutine
In reply to Fabiano RosasQuoted 10 lines · Fabiano Rosas
From: Lin Ma <[email protected]>
Sometimes the query-block performs time-consuming I/O(say waiting for the fstat of NFS complete), So let's make this QMP handler runs in a coroutine.
The following patch moves the
fstat()into a thread pool.Signed-off-by Lin Ma <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>
Apart from the wrong subject,
why is this change not including the update to:
hmp-commands-info.hx
like the previous one?
Thanks,
C
Quoted 36 lines
--- blockdev.c | 6 +++--- qapi/block-core.json | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-)blockdev.c +3
-3@@ -2804,9 +2804,9 @@ void qmp_drive_backup(DriveBackup *backup, Error **errp)blockdev_do_action (&action, errp); } -BlockDeviceInfoList *qmp_query_named_block_nodes (bool has_flat, -bool flat, -Error **errp) +BlockDeviceInfoList *coroutine_fn qmp_query_named_block_nodes(bool has_flat, + bool flat, + Error **errp) {bool return_flat = has_flat && flat;qapi/block-core.json +2
-1@@ -1972,7 +1972,8 @@ { 'command': 'query-named-block-nodes', 'returns': [ 'BlockDeviceInfo' ], 'data': { '*flat': 'bool' }, - 'allow-preconfig': true } + 'allow-preconfig': true, + 'coroutine': true} ## # @XDbgBlockGraphNodeType:
Re: [RFC PATCH 4/6] Convert query-block/info_block to coroutine
The query-named-block-nodes is only availabe for qmp, not support hmp yet.
Lin
________________________________________
From: Claudio Fontana <[email protected]>
Sent: Wednesday, May 24, 2023 4:49 PM
To: Fabiano Rosas; [email protected]
Cc: [email protected]; Kevin Wolf; Hanna Reitz; Markus Armbruster; João Silva; Lin Ma; Dario Faggioli; Eric Blake
Subject: Re: [RFC PATCH 4/6] Convert query-block/info_block to coroutine
Quoted 10 lines · Fabiano Rosas
From: Lin Ma <[email protected]>
Sometimes the query-block performs time-consuming I/O(say waiting for the fstat of NFS complete), So let's make this QMP handler runs in a coroutine.
The following patch moves the
fstat()into a thread pool.Signed-off-by Lin Ma <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>
Apart from the wrong subject,
why is this change not including the update to:
hmp-commands-info.hx
like the previous one?
Thanks,
C
Quoted 36 lines
--- blockdev.c | 6 +++--- qapi/block-core.json | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-)blockdev.c +3
-3@@ -2804,9 +2804,9 @@ void qmp_drive_backup(DriveBackup *backup, Error **errp)blockdev_do_action (&action, errp); } -BlockDeviceInfoList *qmp_query_named_block_nodes (bool has_flat, -bool flat, -Error **errp) +BlockDeviceInfoList *coroutine_fn qmp_query_named_block_nodes(bool has_flat, + bool flat, + Error **errp) {bool return_flat = has_flat && flat;qapi/block-core.json +2
-1@@ -1972,7 +1972,8 @@ { 'command': 'query-named-block-nodes', 'returns': [ 'BlockDeviceInfo' ], 'data': { '*flat': 'bool' }, - 'allow-preconfig': true } + 'allow-preconfig': true, + 'coroutine': true} ## # @XDbgBlockGraphNodeType:
Re: [RFC PATCH 4/6] Convert query-block/info_block to coroutine
Quoted 3 lines · Lin Ma
The query-named-block-nodes is only availabe for qmp, not support hmp yet.
Lin
Ok, makes sense.
RFC PATCH 5/6 block: Allow bdrv_get_allocated_file_size to run in bdrv context
In reply to Fabiano RosasWe're about to move calls to 'fstat' into the thread-pool to avoid blocking VCPU threads should the system call take too long.
To achieve that we first need to make sure none of its callers is holding the aio_context lock, otherwise yielding before scheduling the aiocb handler would result in a deadlock when the qemu_global_mutex is released and another thread tries to acquire the aio_context.
--- block/qapi.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
block/qapi.c +21 -1
@@ -222,6 +222,26 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,return 0 ; } +static int64_t bdrv_get_actual_size (BlockDriverState *bs) +{ +int64_t size; +AioContext *old_ctx =NULL ; + +if (qemu_in_coroutine ()) { +aio_context_release (bdrv_get_aio_context (bs)); + old_ctx =bdrv_co_enter (bs); + } + + size =bdrv_get_allocated_file_size (bs); + +if (qemu_in_coroutine () && old_ctx) { +bdrv_co_leave (bs, old_ctx); +aio_context_acquire (bdrv_get_aio_context (bs)); + } + +return size; +} +/** * Helper function for other query info functions. Store information about @bs * in @info, setting @errp on error.
@@ -250,7 +270,7 @@ static void bdrv_do_query_node_info(BlockDriverState *bs, info->filename =g_strdup (bs->filename); info->format =g_strdup (bdrv_get_format_name (bs)); info->virtual_size = size; - info->actual_size =bdrv_get_allocated_file_size (bs); + info->actual_size =bdrv_get_actual_size (bs); info->has_actual_size = info->actual_size >=0 ;if (bs->encrypted) { info->encrypted = true;
Signature · 1 line
2.35.3
Re: [RFC PATCH 5/6] block: Allow bdrv_get_allocated_file_size to run in bdrv context
Am 23.05.2023 um 23:39 hat Fabiano Rosas geschrieben:
Quoted 18 lines
We're about to move calls to 'fstat' into the thread-pool to avoid blocking VCPU threads should the system call take too long.
To achieve that we first need to make sure none of its callers is holding the
aio_contextlock, otherwise yielding before scheduling the aiocb handler would result in a deadlock when theqemu_global_mutexis released and another thread tries to acquire theaio_context.Signed-off-by Fabiano Rosas <[email protected]>--- block/qapi.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)block/qapi.c +0
-0
block/qapi.c int bdrv_query_snapshot_info_list(BlockDriverState *bs, · line 230
Quoted 9 lines
block/qapi.c +6
-0@@ -222,6 +222,26 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,return 0 ; } +static int64_t bdrv_get_actual_size (BlockDriverState *bs) +{ +int64_t size; +AioContext *old_ctx =NULL ; + +if (qemu_in_coroutine ()) {
Hm. Why can't we make sure that it always runs in a coroutine?
Callers:
* bdrv_query_block_node_info(). This functions seems to be completely unused, we can remove it.
* bdrv_query_image_info(). Called by bdrv_block_device_info() and itself. bdrv_block_device_info() could become a co_wrapper after swapping the first two parameters so that it runs in the AioContext of @bs.
* bdrv_query_block_graph_info(). Only called by qemu-img. Could become a co_wrapper_bdrv_rdlock.
block/qapi.c int bdrv_query_snapshot_info_list(BlockDriverState *bs, · line 232
Quoted 2 lines
block/qapi.c +2
-0+aio_context_release (bdrv_get_aio_context (bs)); + old_ctx =bdrv_co_enter (bs);
I think this is the wrong function to do this. The caller should already make sure that it's in the right AioContext.
block/qapi.c int bdrv_query_snapshot_info_list(BlockDriverState *bs, · line 243
Quoted 11 lines
block/qapi.c +11
-0+ } + + size =bdrv_get_allocated_file_size (bs); + +if (qemu_in_coroutine () && old_ctx) { +bdrv_co_leave (bs, old_ctx); +aio_context_acquire (bdrv_get_aio_context (bs)); + } + +return size; +}
Kevin
Re: [RFC PATCH 5/6] block: Allow bdrv_get_allocated_file_size to run in bdrv context
Quoted 31 lines · Kevin Wolf
Am 23.05.2023 um 23:39 hat Fabiano Rosas geschrieben:
Quoted 18 lines
We're about to move calls to 'fstat' into the thread-pool to avoid blocking VCPU threads should the system call take too long.
To achieve that we first need to make sure none of its callers is holding the
aio_contextlock, otherwise yielding before scheduling the aiocb handler would result in a deadlock when theqemu_global_mutexis released and another thread tries to acquire theaio_context.Signed-off-by Fabiano Rosas <[email protected]>--- block/qapi.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)block/qapi.c +0
-0block/qapi.c int bdrv_query_snapshot_info_list(BlockDriverState *bs, · line 230
Quoted 9 lines
block/qapi.c +6
-0@@ -222,6 +222,26 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,return 0 ; } +static int64_t bdrv_get_actual_size (BlockDriverState *bs) +{ +int64_t size; +AioContext *old_ctx =NULL ; + +if (qemu_in_coroutine ()) {Hm. Why can't we make sure that it always runs in a coroutine?
Quoted 5 lines
Callers:
*
bdrv_query_block_node_info(). This functions seems to be completely unused, we can remove it.
Ok, I'm already removing it in patch 1.
Quoted 4 lines
*
bdrv_query_image_info(). Called bybdrv_block_device_info()and itself.bdrv_block_device_info()could become aco_wrapperafter swapping the first two parameters so that it runs in the AioContext of @bs.
We cannot have bdrv_block_device_info() as co_wrapper because that would create its own coroutine and yielding from that would merely have us waiting at bdrv_poll_co. So it doesn't solve the blocking issue.
What would work is to make bdrv_block_device_info() a coroutine_fn (without a wrapper). Its two callers, qmp_query_block() and qmp_query_named_block_nodes() are already being moved into the handler coroutine in this series, so it would be mostly a matter of adding the type annotation.
Quoted 3 lines
*
bdrv_query_block_graph_info(). Only called by qemu-img. Could become aco_wrapper_bdrv_rdlock.
This one works ok.
Quoted 6 lines
block/qapi.c int bdrv_query_snapshot_info_list(BlockDriverState *bs, · line 232
Quoted 2 lines
block/qapi.c +2
-0+aio_context_release (bdrv_get_aio_context (bs)); + old_ctx =bdrv_co_enter (bs);I think this is the wrong function to do this. The caller should already make sure that it's in the right AioContext.
The issue here is that bdrv_do_query_node_info() calls bdrv_co_get_allocated_file_size(), which is the function we want to make async and therefore needs to run outside of aio_context_acquire/release. However, bdrv_do_query_node_info() also calls bdrv_refresh_filename(), which is GLOBAL_STATE_CODE and therefore wants to be in the main thread context. So entering the bs AioContext at the caller doesn't work when giving the device its own iothread.
Quoted 13 lines
block/qapi.c int bdrv_query_snapshot_info_list(BlockDriverState *bs, · line 243
Quoted 11 lines
block/qapi.c +11
-0+ } + + size =bdrv_get_allocated_file_size (bs); + +if (qemu_in_coroutine () && old_ctx) { +bdrv_co_leave (bs, old_ctx); +aio_context_acquire (bdrv_get_aio_context (bs)); + } + +return size; +}Kevin
RFC PATCH 6/6 block: Add a thread-pool version of fstat
In reply to Fabiano RosasFrom: João Silva <[email protected]>
The fstat call can take a long time to finish when running over NFS. Add a version of it that runs in the thread pool.
Adapt one of its users, raw_co_get_allocated_file size to use the new version. That function is called via QMP under the qemu_global_mutex so it has a large chance of blocking VCPU threads in case it takes too long to finish.
--- block/file-posix.c | 40 +++++++++++++++++++++++++++++++++++++--- include/block/raw-aio.h | 4 +++- 2 files changed, 40 insertions(+), 4 deletions(-)
block/file-posix.c +37 -3
@@ -227,6 +227,9 @@ typedef struct RawPosixAIOData {struct {unsigned long op; } zone_mgmt; +struct { +struct stat *st; + } fstat; }; } RawPosixAIOData;
@@ -2644,6 +2647,34 @@ static void raw_close(BlockDriverState *bs) } } +static int handle_aiocb_fstat (void *opaque) +{ +RawPosixAIOData *aiocb = opaque; + +if (fstat (aiocb->aio_fildes, aiocb->fstat.st) <0 ) { +return -errno; + } + +return 0 ; +} + +static int coroutine_fn raw_co_fstat(BlockDriverState *bs, struct stat *st) +{ +BDRVRawState *s = bs->opaque; +RawPosixAIOData acb; + + acb = (RawPosixAIOData ) { + .bs = bs, + .aio_fildes = s->fd, + .aio_type =QEMU_AIO_FSTAT , + .fstat = { + .st = st, + }, + }; + +return raw_thread_pool_submit (handle_aiocb_fstat, &acb); +} +/** * Truncates the given regular file @fd to @offset and, when growing, fills the * new space according to @prealloc.
@@ -2883,11 +2914,14 @@ static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs) static int64_t coroutine_fn raw_co_get_allocated_file_size(BlockDriverState *bs) {struct stat st; -BDRVRawState *s = bs->opaque; +int ret; -if (fstat (s->fd, &st) <0 ) { -return -errno; + ret =raw_co_fstat (bs, &st); + +if (ret) { +return ret; } +return (int64_t )st.st_blocks *512 ; }
include/block/raw-aio.h +3 -1
@@ -31,6 +31,7 @@#define QEMU_AIO_ZONE_REPORT 0x0100#define QEMU_AIO_ZONE_MGMT 0x0200#define QEMU_AIO_ZONE_APPEND 0x0400 +#define QEMU_AIO_FSTAT 0x0800#define QEMU_AIO_TYPE_MASK \ (QEMU_AIO_READ | \ QEMU_AIO_WRITE | \
@@ -42,7 +43,8 @@ QEMU_AIO_TRUNCATE | \ QEMU_AIO_ZONE_REPORT | \ QEMU_AIO_ZONE_MGMT | \ - QEMU_AIO_ZONE_APPEND) + QEMU_AIO_ZONE_APPEND | \ + QEMU_AIO_FSTAT)/* AIO flags */ #define QEMU_AIO_MISALIGNED 0x1000
Signature · 1 line
2.35.3
Re: [RFC PATCH 6/6] block: Add a thread-pool version of fstat
Quoted 97 lines · Fabiano Rosas
From: João Silva <[email protected]>
The fstat call can take a long time to finish when running over NFS. Add a version of it that runs in the thread pool.
Adapt one of its users,
raw_co_get_allocated_filesize to use the new version. That function is called via QMP under theqemu_global_mutexso it has a large chance of blocking VCPU threads in case it takes too long to finish.Signed-off-by João Silva <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>--- block/file-posix.c | 40 +++++++++++++++++++++++++++++++++++++--- include/block/raw-aio.h | 4 +++- 2 files changed, 40 insertions(+), 4 deletions(-)block/file-posix.c +37
-3@@ -227,6 +227,9 @@ typedef struct RawPosixAIOData {struct {unsigned long op; } zone_mgmt; +struct { +struct stat *st; + } fstat; }; } RawPosixAIOData;@@ -2644,6 +2647,34 @@ static void raw_close(BlockDriverState *bs) } } +static int handle_aiocb_fstat (void *opaque) +{ +RawPosixAIOData *aiocb = opaque; + +if (fstat (aiocb->aio_fildes, aiocb->fstat.st) <0 ) { +return -errno; + } + +return 0 ; +} + +static int coroutine_fn raw_co_fstat(BlockDriverState *bs, struct stat *st) +{ +BDRVRawState *s = bs->opaque; +RawPosixAIOData acb; + + acb = (RawPosixAIOData ) { + .bs = bs, + .aio_fildes = s->fd, + .aio_type =QEMU_AIO_FSTAT , + .fstat = { + .st = st, + }, + }; + +return raw_thread_pool_submit (handle_aiocb_fstat, &acb); +} +/** * Truncates the given regular file @fd to @offset and, when growing, fills the * new space according to @prealloc. @@ -2883,11 +2914,14 @@ static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs) static int64_t coroutine_fn raw_co_get_allocated_file_size(BlockDriverState *bs) {struct stat st; -BDRVRawState *s = bs->opaque; +int ret; -if (fstat (s->fd, &st) <0 ) { -return -errno; + ret =raw_co_fstat (bs, &st); + +if (ret) { +return ret; } +return (int64_t )st.st_blocks *512 ; }include/block/raw-aio.h +1
-0@@ -31,6 +31,7 @@#define QEMU_AIO_ZONE_REPORT 0x0100#define QEMU_AIO_ZONE_MGMT 0x0200#define QEMU_AIO_ZONE_APPEND 0x0400 +#define QEMU_AIO_FSTAT 0x0800#define QEMU_AIO_TYPE_MASK \ (QEMU_AIO_READ | \ QEMU_AIO_WRITE | \
include/block/raw-aio.h line 50
Quoted 9 lines
include/block/raw-aio.h +2
-1@@ -42,7 +43,8 @@ QEMU_AIO_TRUNCATE | \ QEMU_AIO_ZONE_REPORT | \ QEMU_AIO_ZONE_MGMT | \ - QEMU_AIO_ZONE_APPEND) + QEMU_AIO_ZONE_APPEND | \ + QEMU_AIO_FSTAT)/* AIO flags */ #define QEMU_AIO_MISALIGNED 0x1000
Re: [RFC PATCH 6/6] block: Add a thread-pool version of fstat
In reply to Fabiano RosasQuoted 10 lines · Fabiano Rosas
From: João Silva <[email protected]>
The fstat call can take a long time to finish when running over NFS. Add a version of it that runs in the thread pool.
Adapt one of its users,
raw_co_get_allocated_filesize to use the new version. That function is called via QMP under theqemu_global_mutexso it has a large chance of blocking VCPU threads in case it takes too long to finish.
Quoted 6 lines
Signed-off-by João Silva <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>--- block/file-posix.c | 40 +++++++++++++++++++++++++++++++++++++--- include/block/raw-aio.h | 4 +++- 2 files changed, 40 insertions(+), 4 deletions(-)
Should this change occur earlier in the series, before calling commands are marked with QAPI coroutine flags? Otherwise, you have a bisection bug, where something marked coroutine can end up hanging when it calls a blocking syscall in the wrong context without the help of this patch offloading the syscall into a helper thread.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [RFC PATCH 6/6] block: Add a thread-pool version of fstat
Quoted 18 lines · Eric Blake
Quoted 10 lines · Fabiano Rosas
From: João Silva <[email protected]>
The fstat call can take a long time to finish when running over NFS. Add a version of it that runs in the thread pool.
Adapt one of its users,
raw_co_get_allocated_filesize to use the new version. That function is called via QMP under theqemu_global_mutexso it has a large chance of blocking VCPU threads in case it takes too long to finish.Quoted 6 lines
Signed-off-by João Silva <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>--- block/file-posix.c | 40 +++++++++++++++++++++++++++++++++++++--- include/block/raw-aio.h | 4 +++- 2 files changed, 40 insertions(+), 4 deletions(-)
Quoted 5 lines
Should this change occur earlier in the series, before calling commands are marked with QAPI coroutine flags? Otherwise, you have a bisection bug, where something marked coroutine can end up hanging when it calls a blocking syscall in the wrong context without the help of this patch offloading the syscall into a helper thread.
Hmm, I'm not sure. To submit the work to the thread pool we need to be in a coroutine already. If the syscall blocks for too long we'd be trading blocking the coroutine vs. blocking a vcpu thread anyway.
I have tested each patch to avoid bisection issues, but maybe it would be warranted to merge both parts into a single patch. Or arrange them in some other way... I'll experiment with it.