block: fix 'savevm' hang with -object iothread
v2 cover block: fix 'savevm' hang with -object iothread
v2:
* New patch to use bdrv_drain_all_begin/end() in savevm/loadvm [Kevin] (All other patches unchanged)
The 'savevm' command hangs when -object iothread is used. See patches for details, but basically the vmstate read/write code didn't conform to the latest block layer locking rules.
Stefan Hajnoczi (4):
block: count bdrv_co_rw_vmstate() requests
block: use BDRV_POLL_WHILE() in bdrv_rw_vmstate()
migration: avoid recursive AioContext locking in save_vmstate()
migration: use bdrv_drain_all_begin/end() instead bdrv_drain_all()
block/io.c | 21 +++++++++++++-------- migration/savevm.c | 21 +++++++++++++++++++-- 2 files changed, 32 insertions(+), 10 deletions(-)
Signature · 1 line
2.9.3
PATCH v2 1/4 block: count bdrv_co_rw_vmstate() requests
Call bdrv_inc/dec_in_flight() for vmstate reads/writes. This seems unnecessary at first glance because vmstate reads/writes are done synchronously while the guest is stopped. But we need the bdrv_wakeup() in bdrv_dec_in_flight() so the main loop sees request completion. Besides, it's cleaner to count vmstate reads/writes like ordinary read/write requests.
The bdrv_wakeup() partially fixes a 'savevm' hang with -object iothread.
--- block/io.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
block/io.c +12 -5
@@ -1988,17 +1988,24 @@ bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos, bool is_read) {BlockDriver *drv = bs->drv; +int ret = -ENOTSUP ; + +bdrv_inc_in_flight (bs);if (!drv) { -return -ENOMEDIUM ; + ret = -ENOMEDIUM ; }else if (drv->bdrv_load_vmstate) { -return is_read ? drv->bdrv_load_vmstate (bs, qiov, pos) - : drv->bdrv_save_vmstate (bs, qiov, pos); +if (is_read) { + ret = drv->bdrv_load_vmstate (bs, qiov, pos); + }else { + ret = drv->bdrv_save_vmstate (bs, qiov, pos); + } }else if (bs->file) { -return bdrv_co_rw_vmstate (bs->file->bs, qiov, pos, is_read); + ret =bdrv_co_rw_vmstate (bs->file->bs, qiov, pos, is_read); } -return -ENOTSUP ; +bdrv_dec_in_flight (bs); +return ret; } static void coroutine_fn bdrv_co_rw_vmstate_entry(void *opaque)
Signature · 1 line
2.9.3
PATCH v2 2/4 block: use BDRV_POLL_WHILE() in bdrv_rw_vmstate()
In reply to Stefan HajnocziCalling aio_poll() directly may have been fine previously, but this is the future, man! The difference between an aio_poll() loop and BDRV_POLL_WHILE() is that BDRV_POLL_WHILE() releases the AioContext around aio_poll().
This allows the IOThread to run fd handlers or BHs to complete the request. Failure to release the AioContext causes deadlocks.
Using BDRV_POLL_WHILE() partially fixes a 'savevm' hang with -object iothread.
--- block/io.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
block/io.c +1 -3
@@ -2031,9 +2031,7 @@ bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,Coroutine *co =qemu_coroutine_create (bdrv_co_rw_vmstate_entry, &data);bdrv_coroutine_enter (bs, co); -while (data.ret == -EINPROGRESS ) { -aio_poll (bdrv_get_aio_context (bs), true); - } +BDRV_POLL_WHILE (bs, data.ret == -EINPROGRESS );return data.ret; } }
Signature · 1 line
2.9.3
PATCH v2 3/4 migration: avoid recursive AioContext locking in save_vmstate()
In reply to Stefan HajnocziAioContext was designed to allow nested acquire/release calls. It uses a recursive mutex so callers don't need to worry about nesting...or so we thought.
BDRV_POLL_WHILE() is used to wait for block I/O requests. It releases the AioContext temporarily around aio_poll(). This gives IOThreads a chance to acquire the AioContext to process I/O completions.
It turns out that recursive locking and BDRV_POLL_WHILE() don't mix. BDRV_POLL_WHILE() only releases the AioContext once, so the IOThread will not be able to acquire the AioContext if it was acquired multiple times.
Instead of trying to release AioContext n times in BDRV_POLL_WHILE(), this patch simply avoids nested locking in save_vmstate(). It's the simplest fix and we should step back to consider the big picture with all the recent changes to block layer threading.
This patch is the final fix to solve 'savevm' hanging with -object iothread.
--- migration/savevm.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
migration/savevm.c +11 -1
@@ -2150,6 +2150,14 @@ int save_vmstate(const char *name, Error **errp) goto the_end; } +/* The bdrv_all_create_snapshot() call that follows acquires the AioContext +* for itself. BDRV_POLL_WHILE() does not support nested locking because +* it only releases the lock once. Therefore synchronous I/O will deadlock +* unless we release the AioContext before bdrv_all_create_snapshot(). +*/ +aio_context_release (aio_context); + aio_context =NULL ; + ret =bdrv_all_create_snapshot (sn, bs, vm_state_size, &bs);if (ret <0 ) { error_setg(errp, "Error while creating snapshot on '%s'",
@@ -2160,7 +2168,9 @@ int save_vmstate(const char *name, Error **errp) ret =0 ; the_end: -aio_context_release (aio_context); +if (aio_context) { +aio_context_release (aio_context); + }if (saved_vm_running) {vm_start (); }
Signature · 1 line
2.9.3
PATCH v2 4/4 migration: use bdrv_drain_all_begin/end() instead bdrv_drain_all()
In reply to Stefan Hajnocziblk/bdrv_drain_all() only takes effect for a single instant and then resumes block jobs, guest devices, and other external clients like the NBD server. This can be handy when performing a synchronous drain before terminating the program, for example.
Monitor commands usually need to quiesce I/O across an entire code region so blk/bdrv_drain_all() is not suitable. They must use bdrv_drain_all_begin/end() to mark the region. This prevents new I/O requests from slipping in or worse - block jobs completing and modifying the graph.
I audited other blk/bdrv_drain_all() callers but did not find anything that needs a similar fix. This patch fixes the savevm/loadvm commands. Although I haven't encountered a read world issue this makes the code safer.
--- migration/savevm.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
migration/savevm.c +8 -1
@@ -2113,6 +2113,8 @@ int save_vmstate(const char *name, Error **errp) }vm_stop (RUN_STATE_SAVE_VM ); +bdrv_drain_all_begin (); +aio_context_acquire (aio_context);memset (sn,0 ,sizeof (*sn));
@@ -2171,6 +2173,9 @@ int save_vmstate(const char *name, Error **errp)if (aio_context) {aio_context_release (aio_context); } + +bdrv_drain_all_end (); +if (saved_vm_running) {vm_start (); }
@@ -2279,7 +2284,7 @@ int load_vmstate(const char *name, Error **errp) }/* Flush all IO requests so they don't interfere with the new state. */ -bdrv_drain_all (); +bdrv_drain_all_begin (); ret =bdrv_all_goto_snapshot (name, &bs);if (ret <0 ) {
@@ -2303,6 +2308,8 @@ int load_vmstate(const char *name, Error **errp)qemu_fclose (f);aio_context_release (aio_context); +bdrv_drain_all_end (); +migration_incoming_state_destroy ();if (ret <0 ) {error_setg (errp,"Error %d while loading VM state" , ret);
Signature · 1 line
2.9.3
Re: [Qemu-devel] [PATCH v2 4/4] migration: use bdrv_drain_all_begin/end() instead bdrv_drain_all()
Am 19.05.2017 um 12:32 hat Stefan Hajnoczi geschrieben:
Quoted 29 lines
blk/bdrv_drain_all() only takes effect for a single instant and then resumes block jobs, guest devices, and other external clients like the NBD server. This can be handy when performing a synchronous drain before terminating the program, for example.
Monitor commands usually need to quiesce I/O across an entire code region so blk/bdrv_drain_all() is not suitable. They must use bdrv_drain_all_begin/end() to mark the region. This prevents new I/O requests from slipping in or worse - block jobs completing and modifying the graph.
I audited other blk/bdrv_drain_all() callers but did not find anything that needs a similar fix. This patch fixes the savevm/loadvm commands. Although I haven't encountered a read world issue this makes the code safer.
Suggested-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>migration/savevm.c +1
-1@@ -2279,7 +2284,7 @@ int load_vmstate(const char *name, Error **errp) }/* Flush all IO requests so they don't interfere with the new state. */ -bdrv_drain_all (); +bdrv_drain_all_begin (); ret =bdrv_all_goto_snapshot (name, &bs);if (ret <0 ) {
migration/savevm.c int load_vmstate(const char *name, Error **errp) · line 2315
Quoted 8 lines
migration/savevm.c +2
-0@@ -2303,6 +2308,8 @@ int load_vmstate(const char *name, Error **errp)qemu_fclose (f);aio_context_release (aio_context); +bdrv_drain_all_end (); +migration_incoming_state_destroy ();if (ret <0 ) {error_setg (errp,"Error %d while loading VM state" , ret);
There are a few error returns between these two places where a bdrv_drain_all_end() is missing.
Kevin
Re: [Qemu-devel] [PATCH v2 4/4] migration: use bdrv_drain_all_begin/end() instead bdrv_drain_all()
Quoted 33 lines · Kevin Wolf
Am 19.05.2017 um 12:32 hat Stefan Hajnoczi geschrieben:
Quoted 32 lines
blk/bdrv_drain_all() only takes effect for a single instant and then resumes block jobs, guest devices, and other external clients like the NBD server. This can be handy when performing a synchronous drain before terminating the program, for example.
Monitor commands usually need to quiesce I/O across an entire code region so blk/bdrv_drain_all() is not suitable. They must use bdrv_drain_all_begin/end() to mark the region. This prevents new I/O requests from slipping in or worse - block jobs completing and modifying the graph.
I audited other blk/bdrv_drain_all() callers but did not find anything that needs a similar fix. This patch fixes the savevm/loadvm commands. Although I haven't encountered a read world issue this makes the code safer.
Suggested-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>migration/savevm.c +1
-1@@ -2279,7 +2284,7 @@ int load_vmstate(const char *name, Error **errp) }/* Flush all IO requests so they don't interfere with the new state. */ -bdrv_drain_all (); +bdrv_drain_all_begin (); ret =bdrv_all_goto_snapshot (name, &bs);if (ret <0 ) {@@ -2303,6 +2308,8 @@ int load_vmstate(const char *name, Error **errp)qemu_fclose (f);aio_context_release (aio_context);
Quoted 8 lines
migration/savevm.c int load_vmstate(const char *name, Error **errp) · line 2315
Quoted 5 lines
migration/savevm.c +2
-0+bdrv_drain_all_end (); +migration_incoming_state_destroy ();if (ret <0 ) {error_setg (errp,"Error %d while loading VM state" , ret);There are a few error returns between these two places where a
bdrv_drain_all_end()is missing.
Thank you, will fix in the next revision.
Stefan