RFC [PATCH] Make bdrv_flush synchronous only and update callers
This patch makes bdrv_flush a synchronous function and updates any callers from a coroutine context to use bdrv_co_flush instead.
The motivation for this patch comes from the GSoC Continuation-Passing C project. When coroutines were introduced, synchronous functions in the block layer were converted to use asynchronous methods by dynamically detecting if they were being run from a coroutine context by calling qemu_in_coroutine(), and yielding if so. If they were not, they would spawn a new coroutine and poll until the asynchronous counterpart finished.
However this approach does not work with CPC as the CPC translator converts all functions annotated coroutine_fn to a different (continuation based) calling convention. This means that coroutine_fn annotated functions cannot be called from a non-coroutine context.
This patch is a Request For Comments on the approach of splitting these "dynamic" functions into synchronous and asynchronous versions. This is easy for bdrv_flush as it already has an asynchronous counterpart - bdrv_co_flush. The only caller of bdrv_flush from a coroutine context is mirror_drain in block/mirror.c - this should be annotated as a coroutine_fn as it calls qemu_coroutine_yield().
If this approach meets with approval I will develop a patchset splitting the other "dynamic" functions in the block layer. This will allow all coroutine functions to have a coroutine_fn annotation that can be statically checked (CPC can be used to verify annotations).
I have audited the other callers of bdrv_flush, they are included below:
block.c: bdrv_reopen_prepare, bdrv_close, bdrv_commit, bdrv_pwrite_sync block/qcow2-cache.c: qcow2_cache_entry_flush, qcow2_cache_flush block/qcow2-refcount.c: qcow2_update_snapshot_refcount block/qcow2-snapshot.c: qcow2_write_snapshotsblock/qcow2.c: qcow2_mark_dirty, qcow2_mark_clean block/qed-check.c: qed_check_mark_cleanblock/qed.c: bdrv_qed_open, bdrv_qed_closeblockdev.c: external_snapshot_prepare, do_drive_delcpus.c: do_vm_stophw/block/nvme.c: nvme_clear_ctrlqemu-io-cmds.c: flush_fsavevm.c: bdrv_fclose
--- block.c | 13 ++++--------- block/mirror.c | 4 ++-- 2 files changed, 6 insertions(+), 11 deletions(-)
block.c +4 -9
@@ -4110,15 +4110,10 @@ int bdrv_flush(BlockDriverState *bs) .ret = NOT_DONE, }; -if (qemu_in_coroutine ()) { -/* Fast-path if already in coroutine context */ -bdrv_flush_co_entry (&rwco); - }else { - co =qemu_coroutine_create (bdrv_flush_co_entry); -qemu_coroutine_enter (co, &rwco); -while (rwco.ret ==NOT_DONE ) { -qemu_aio_wait (); - } + co =qemu_coroutine_create (bdrv_flush_co_entry); +qemu_coroutine_enter (co, &rwco); +while (rwco.ret ==NOT_DONE ) { +qemu_aio_wait (); }return rwco.ret;
block/mirror.c +2 -2
@@ -282,7 +282,7 @@ static void mirror_free_init(MirrorBlockJob *s) } } -static void mirror_drain (MirrorBlockJob *s) +static void coroutine_fn mirror_drain(MirrorBlockJob *s) {while (s->in_flight >0 ) {qemu_coroutine_yield ();
@@ -390,7 +390,7 @@ static void coroutine_fn mirror_run(void *opaque) should_complete = false;if (s->in_flight ==0 && cnt ==0 ) {trace_mirror_before_flush (s); - ret =bdrv_flush (s->target); + ret =bdrv_co_flush (s->target);if (ret <0 ) {if (mirror_error_action (s, false, -ret) ==BDRV_ACTION_REPORT ) { goto immediate_exit;
Signature · 1 line
1.8.3.2
Quoted 22 lines · Charlie Shepherd
This patch makes
bdrv_flusha synchronous function and updates any callers from a coroutine context to usebdrv_co_flushinstead.The motivation for this patch comes from the GSoC Continuation-Passing C project. When coroutines were introduced, synchronous functions in the block layer were converted to use asynchronous methods by dynamically detecting if they were being run from a coroutine context by calling
qemu_in_coroutine(), and yielding if so. If they were not, they would spawn a new coroutine and poll until the asynchronous counterpart finished.However this approach does not work with CPC as the CPC translator converts all functions annotated
coroutine_fnto a different (continuation based) calling convention. This means thatcoroutine_fnannotated functions cannot be called from a non-coroutine context.This patch is a Request For Comments on the approach of splitting these "dynamic" functions into synchronous and asynchronous versions. This is easy for
bdrv_flushas it already has an asynchronous counterpart -bdrv_co_flush. The only caller ofbdrv_flushfrom a coroutine context ismirror_draininblock/mirror.c- this should be annotated as acoroutine_fnas it callsqemu_coroutine_yield().
Quoted 8 lines
If this approach meets with approval I will develop a patchset splitting the other "dynamic" functions in the block layer. This will allow all coroutine functions to have a
coroutine_fnannotation that can be statically checked (CPC can be used to verify annotations).I have audited the other callers of
bdrv_flush, they are included below:
block.c:bdrv_reopen_prepare,bdrv_close,bdrv_commit,bdrv_pwrite_sync
bdrv_pwrite_sync() is a dynamic function. If called from coroutine context it will yield (indirectly from bdrv_pwrite() or bdrv_flush()).
Quoted 4 lines
block/qcow2-cache.c:qcow2_cache_entry_flush,qcow2_cache_flushblock/qcow2-refcount.c:qcow2_update_snapshot_refcountblock/qcow2-snapshot.c:qcow2_write_snapshotsblock/qcow2.c:qcow2_mark_dirty,qcow2_mark_clean
qcow2 runs in coroutine context, the coroutine_fn annotations are just missing. See block/qcow2.c:bdrv_qcow2 for the entry points like qcow2_co_readv.
Quoted 7 lines
block/qed-check.c:qed_check_mark_cleanblock/qed.c:bdrv_qed_open,bdrv_qed_closeblockdev.c:external_snapshot_prepare,do_drive_delcpus.c:do_vm_stophw/block/nvme.c:nvme_clear_ctrlqemu-io-cmds.c:flush_fsavevm.c:bdrv_fclose
I think the rest are fine and your patch looks good.
Am 19.07.2013 um 07:27 hat Stefan Hajnoczi geschrieben:
Quoted 35 lines · Charlie Shepherd
Quoted 22 lines
This patch makes
bdrv_flusha synchronous function and updates any callers from a coroutine context to usebdrv_co_flushinstead.The motivation for this patch comes from the GSoC Continuation-Passing C project. When coroutines were introduced, synchronous functions in the block layer were converted to use asynchronous methods by dynamically detecting if they were being run from a coroutine context by calling
qemu_in_coroutine(), and yielding if so. If they were not, they would spawn a new coroutine and poll until the asynchronous counterpart finished.However this approach does not work with CPC as the CPC translator converts all functions annotated
coroutine_fnto a different (continuation based) calling convention. This means thatcoroutine_fnannotated functions cannot be called from a non-coroutine context.This patch is a Request For Comments on the approach of splitting these "dynamic" functions into synchronous and asynchronous versions. This is easy for
bdrv_flushas it already has an asynchronous counterpart -bdrv_co_flush. The only caller ofbdrv_flushfrom a coroutine context ismirror_draininblock/mirror.c- this should be annotated as acoroutine_fnas it callsqemu_coroutine_yield().Quoted 8 lines
If this approach meets with approval I will develop a patchset splitting the other "dynamic" functions in the block layer. This will allow all coroutine functions to have a
coroutine_fnannotation that can be statically checked (CPC can be used to verify annotations).I have audited the other callers of
bdrv_flush, they are included below:
block.c:bdrv_reopen_prepare,bdrv_close,bdrv_commit,bdrv_pwrite_sync
bdrv_pwrite_sync()is a dynamic function. If called from coroutine context it will yield (indirectly frombdrv_pwrite()orbdrv_flush()).
Quoted 8 lines
Quoted 4 lines
block/qcow2-cache.c:qcow2_cache_entry_flush,qcow2_cache_flushblock/qcow2-refcount.c:qcow2_update_snapshot_refcountblock/qcow2-snapshot.c:qcow2_write_snapshotsblock/qcow2.c:qcow2_mark_dirty,qcow2_mark_cleanqcow2 runs in coroutine context, the
coroutine_fnannotations are just missing. See block/qcow2.c:bdrv_qcow2 for the entry points likeqcow2_co_readv.
Yes, you can't rely on coroutine_fn, it's missing in many places where it should be there. But that was still the optimistic view.
The truth is that the greatest part of the qcow2 functions can be called from eiher coroutine or non-coroutine context. You get coroutine context for read/write/discard/flush, but anything else like doing snapshots, resizing, preallocating the image, writing compressed data also accesses the same metadata management functions outside coroutines.
It's only getting worse for function like bdrv_pwrite().
Kevin
Quoted 48 lines · Kevin Wolf
Am 19.07.2013 um 07:27 hat Stefan Hajnoczi geschrieben:
Quoted 35 lines · Charlie Shepherd
Quoted 22 lines
This patch makes
bdrv_flusha synchronous function and updates any callers from a coroutine context to usebdrv_co_flushinstead.The motivation for this patch comes from the GSoC Continuation-Passing C project. When coroutines were introduced, synchronous functions in the block layer were converted to use asynchronous methods by dynamically detecting if they were being run from a coroutine context by calling
qemu_in_coroutine(), and yielding if so. If they were not, they would spawn a new coroutine and poll until the asynchronous counterpart finished.However this approach does not work with CPC as the CPC translator converts all functions annotated
coroutine_fnto a different (continuation based) calling convention. This means thatcoroutine_fnannotated functions cannot be called from a non-coroutine context.This patch is a Request For Comments on the approach of splitting these "dynamic" functions into synchronous and asynchronous versions. This is easy for
bdrv_flushas it already has an asynchronous counterpart -bdrv_co_flush. The only caller ofbdrv_flushfrom a coroutine context ismirror_draininblock/mirror.c- this should be annotated as acoroutine_fnas it callsqemu_coroutine_yield().Quoted 8 lines
If this approach meets with approval I will develop a patchset splitting the other "dynamic" functions in the block layer. This will allow all coroutine functions to have a
coroutine_fnannotation that can be statically checked (CPC can be used to verify annotations).I have audited the other callers of
bdrv_flush, they are included below:
block.c:bdrv_reopen_prepare,bdrv_close,bdrv_commit,bdrv_pwrite_sync
bdrv_pwrite_sync()is a dynamic function. If called from coroutine context it will yield (indirectly frombdrv_pwrite()orbdrv_flush()).Quoted 8 lines
Quoted 4 lines
block/qcow2-cache.c:qcow2_cache_entry_flush,qcow2_cache_flushblock/qcow2-refcount.c:qcow2_update_snapshot_refcountblock/qcow2-snapshot.c:qcow2_write_snapshotsblock/qcow2.c:qcow2_mark_dirty,qcow2_mark_cleanqcow2 runs in coroutine context, the
coroutine_fnannotations are just missing. See block/qcow2.c:bdrv_qcow2 for the entry points likeqcow2_co_readv.Yes, you can't rely on
coroutine_fn, it's missing in many places where it should be there. But that was still the optimistic view.
Quoted 7 lines
The truth is that the greatest part of the qcow2 functions can be called from eiher coroutine or non-coroutine context. You get coroutine context for read/write/discard/flush, but anything else like doing snapshots, resizing, preallocating the image, writing compressed data also accesses the same metadata management functions outside coroutines.
It's only getting worse for function like
bdrv_pwrite().
A built-time check for coroutine_fn would be valuable if we ever hope to get disciplined with this annotation.
The check can detect when a coroutine_fn is invoked outside coroutine context. I wonder if Coccinelle can detect this, although I never figured out how to use it as a grep-like tool instead of just a patch-like tool.
Stefan
Quoted 7 lines · Stefan Hajnoczi
A built-time check for
coroutine_fnwould be valuable if we ever hope to get disciplined with this annotation.The check can detect when a
coroutine_fnis invoked outside coroutine context. I wonder if Coccinelle can detect this, although I never figured out how to use it as a grep-like tool instead of just a patch-like tool.
The recent "cps-inference" branch of CPC enables precisely that kind of check. Charlie is using it to drive his modifications to QEMU and has already suggested several improvements that I have implemented. Hopefully we should reach something fully covering QEMU by the end his GSoC.
If there is interest, I can post a script showing how to build it and use it to check QEMU annotations (it does not require any modifications to QEMU, only a couple of configure switches).
Best regards,
Signature · 1 line
Gabriel
Quoted 9 lines · Gabriel Kerneis
Quoted 7 lines · Stefan Hajnoczi
A built-time check for
coroutine_fnwould be valuable if we ever hope to get disciplined with this annotation.The check can detect when a
coroutine_fnis invoked outside coroutine context. I wonder if Coccinelle can detect this, although I never figured out how to use it as a grep-like tool instead of just a patch-like tool.
Quoted 8 lines
The recent "cps-inference" branch of CPC enables precisely that kind of check. Charlie is using it to drive his modifications to QEMU and has already suggested several improvements that I have implemented. Hopefully we should reach something fully covering QEMU by the end his GSoC.
If there is interest, I can post a script showing how to build it and use it to check QEMU annotations (it does not require any modifications to QEMU, only a couple of configure switches).
What is the status of CPC packaging in distros?
Stefan
Quoted 15 lines · Stefan Hajnoczi
Quoted 14 lines · Gabriel Kerneis
Quoted 7 lines · Stefan Hajnoczi
A built-time check for
coroutine_fnwould be valuable if we ever hope to get disciplined with this annotation.The check can detect when a
coroutine_fnis invoked outside coroutine context. I wonder if Coccinelle can detect this, although I never figured out how to use it as a grep-like tool instead of just a patch-like tool.The recent "cps-inference" branch of CPC enables precisely that kind of check. Charlie is using it to drive his modifications to QEMU and has already suggested several improvements that I have implemented. Hopefully we should reach something fully covering QEMU by the end his GSoC.
Quoted 5 lines
Quoted 3 lines
If there is interest, I can post a script showing how to build it and use it to check QEMU annotations (it does not require any modifications to QEMU, only a couple of configure switches).
What is the status of CPC packaging in distros?
There will be an opam [1] package for it before the end of the week (based on the CIL release planned for tomorrow). I've also been working on a Debian package but it's not ready yet unfortunately.
Signature · 1 line
Gabriel
Wenchao Xia
Reviewed-by I am glad to have an accurate sync bdrv_flush(). Code looks fine.
Quoted 94 lines
This patch makes
bdrv_flusha synchronous function and updates any callers from a coroutine context to usebdrv_co_flushinstead.The motivation for this patch comes from the GSoC Continuation-Passing C project. When coroutines were introduced, synchronous functions in the block layer were converted to use asynchronous methods by dynamically detecting if they were being run from a coroutine context by calling
qemu_in_coroutine(), and yielding if so. If they were not, they would spawn a new coroutine and poll until the asynchronous counterpart finished.However this approach does not work with CPC as the CPC translator converts all functions annotated
coroutine_fnto a different (continuation based) calling convention. This means thatcoroutine_fnannotated functions cannot be called from a non-coroutine context.This patch is a Request For Comments on the approach of splitting these "dynamic" functions into synchronous and asynchronous versions. This is easy for
bdrv_flushas it already has an asynchronous counterpart -bdrv_co_flush. The only caller ofbdrv_flushfrom a coroutine context ismirror_draininblock/mirror.c- this should be annotated as acoroutine_fnas it callsqemu_coroutine_yield().If this approach meets with approval I will develop a patchset splitting the other "dynamic" functions in the block layer. This will allow all coroutine functions to have a
coroutine_fnannotation that can be statically checked (CPC can be used to verify annotations).I have audited the other callers of
bdrv_flush, they are included below:
block.c:bdrv_reopen_prepare,bdrv_close,bdrv_commit,bdrv_pwrite_syncblock/qcow2-cache.c:qcow2_cache_entry_flush,qcow2_cache_flushblock/qcow2-refcount.c:qcow2_update_snapshot_refcountblock/qcow2-snapshot.c:qcow2_write_snapshotsblock/qcow2.c:qcow2_mark_dirty,qcow2_mark_cleanblock/qed-check.c:qed_check_mark_cleanblock/qed.c:bdrv_qed_open,bdrv_qed_closeblockdev.c:external_snapshot_prepare,do_drive_delcpus.c:do_vm_stophw/block/nvme.c:nvme_clear_ctrlqemu-io-cmds.c:flush_fsavevm.c:bdrv_fclose--- block.c | 13 ++++--------- block/mirror.c | 4 ++-- 2 files changed, 6 insertions(+), 11 deletions(-)block.c +4
-9@@ -4110,15 +4110,10 @@ int bdrv_flush(BlockDriverState *bs) .ret = NOT_DONE, }; -if (qemu_in_coroutine ()) { -/* Fast-path if already in coroutine context */ -bdrv_flush_co_entry (&rwco); - }else { - co =qemu_coroutine_create (bdrv_flush_co_entry); -qemu_coroutine_enter (co, &rwco); -while (rwco.ret ==NOT_DONE ) { -qemu_aio_wait (); - } + co =qemu_coroutine_create (bdrv_flush_co_entry); +qemu_coroutine_enter (co, &rwco); +while (rwco.ret ==NOT_DONE ) { +qemu_aio_wait (); }return rwco.ret;block/mirror.c +2
-2@@ -282,7 +282,7 @@ static void mirror_free_init(MirrorBlockJob *s) } } -static void mirror_drain (MirrorBlockJob *s) +static void coroutine_fn mirror_drain(MirrorBlockJob *s) {while (s->in_flight >0 ) {qemu_coroutine_yield ();@@ -390,7 +390,7 @@ static void coroutine_fn mirror_run(void *opaque) should_complete = false;if (s->in_flight ==0 && cnt ==0 ) {trace_mirror_before_flush (s); - ret =bdrv_flush (s->target); + ret =bdrv_co_flush (s->target);if (ret <0 ) {if (mirror_error_action (s, false, -ret) ==BDRV_ACTION_REPORT ) { goto immediate_exit;
Signature · 3 lines
Best Regards Wenchao Xia