Mirror block job fixes for 2.6.0-rc4
The following changes since commit ee1e0f8e5d3682c561edcdceccff72b9d9b16d8b:
util: align memory allocations to 2M on AArch64 (2016-04-22 12:26:01 +0100)
are available in the git repository at:
git://repo.or.cz/qemu/kevin.git tags/for-upstream
for you to fetch changes up to ab27c3b5e7408693dde0b565f050aa55c4a1bcef:
mirror: Workaround for unexpected iohandler events during completion (2016-04-22 16:44:09 +0200)
----------------------------------------------------------------
Mirror block job fixes for 2.6.0-rc4
----------------------------------------------------------------
Fam Zheng (5):
iohandler: Introduce iohandler_get_aio_context
event-notifier: Add "is_external" parameter
virtio: Mark host notifiers as external
aio-posix: Skip external nodes in aio_dispatch
mirror: Workaround for unexpected iohandler events during completion
aio-posix.c | 8 ++++++-- block/mirror.c | 9 +++++++++ hw/usb/ccid-card-emulated.c | 2 +- hw/virtio/virtio.c | 8 ++++---- include/qemu/event_notifier.h | 4 +++- include/qemu/main-loop.h | 1 + iohandler.c | 6 ++++++ stubs/Makefile.objs | 1 + stubs/iohandler.c | 8 ++++++++ stubs/set-fd-handler.c | 10 ++++++++++ target-i386/hyperv.c | 6 +++--- util/event_notifier-posix.c | 4 +++- util/event_notifier-win32.c | 1 + 13 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 stubs/iohandler.c
[Qemu-devel] [PULL 3/5] virtio: Mark host notifiers as external
From: Fam Zheng <[email protected]>
The effect of this change is the block layer drained section can work, for example when mirror job is being completed.
--- hw/virtio/virtio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/virtio/virtio.c +2 -2
@@ -1829,10 +1829,10 @@ void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign, bool set_handler) {if (assign && set_handler) { -event_notifier_set_handler (&vq->host_notifier, false, +event_notifier_set_handler (&vq->host_notifier, true, virtio_queue_host_notifier_read); }else { -event_notifier_set_handler (&vq->host_notifier, false,NULL ); +event_notifier_set_handler (&vq->host_notifier, true,NULL ); }if (!assign) {/* Test and clear notifier before after disabling event,
Signature · 1 line
1.8.3.1
[Qemu-devel] [PULL 4/5] aio-posix: Skip external nodes in aio_dispatch
In reply to Kevin WolfFrom: Fam Zheng <[email protected]>
aio_poll doesn't poll the external nodes so this should never be true, but aio_ctx_dispatch may get notified by the events from GSource. To make bdrv_drained_begin effective in main loop, we should check the is_external flag here too.
Also do the check in aio_pending so aio_dispatch is not called superfluously, when there is no events other than external ones.
--- aio-posix.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
aio-posix.c +6 -2
@@ -282,10 +282,12 @@ bool aio_pending(AioContext *ctx)int revents; revents = node->pfd.revents & node->pfd.events; -if (revents & (G_IO_IN |G_IO_HUP |G_IO_ERR ) && node->io_read) { +if (revents & (G_IO_IN |G_IO_HUP |G_IO_ERR ) && node->io_read && +aio_node_check (ctx, node->is_external)) {return true; } -if (revents & (G_IO_OUT |G_IO_ERR ) && node->io_write) { +if (revents & (G_IO_OUT |G_IO_ERR ) && node->io_write && +aio_node_check (ctx, node->is_external)) {return true; } }
@@ -323,6 +325,7 @@ bool aio_dispatch(AioContext *ctx)if (!node->deleted && (revents & (G_IO_IN |G_IO_HUP |G_IO_ERR )) && +aio_node_check (ctx, node->is_external) && node->io_read) { node->io_read (node->opaque);
@@ -333,6 +336,7 @@ bool aio_dispatch(AioContext *ctx) }if (!node->deleted && (revents & (G_IO_OUT |G_IO_ERR )) && +aio_node_check (ctx, node->is_external) && node->io_write) { node->io_write (node->opaque); progress = true;
Signature · 1 line
1.8.3.1
[Qemu-devel] [PULL 2/5] event-notifier: Add "is_external" parameter
In reply to Kevin WolfFrom: Fam Zheng <[email protected]>
All callers pass "false" keeping the old semantics. The windows implementation doesn't distinguish the flag yet. On posix, it is passed down to the underlying aio context.
--- hw/usb/ccid-card-emulated.c | 2 +- hw/virtio/virtio.c | 8 ++++---- include/qemu/event_notifier.h | 4 +++- stubs/set-fd-handler.c | 10 ++++++++++ target-i386/hyperv.c | 6 +++--- util/event_notifier-posix.c | 4 +++- util/event_notifier-win32.c | 1 + 7 files changed, 25 insertions(+), 10 deletions(-)
hw/usb/ccid-card-emulated.c +1 -1
@@ -407,7 +407,7 @@ static int init_event_notifier(EmulatedState *card)DPRINTF (card,2 ,"event notifier creation failed\n" );return -1 ; } -event_notifier_set_handler (&card->notifier, card_event_handler); +event_notifier_set_handler (&card->notifier, false, card_event_handler);return 0 ; }
hw/virtio/virtio.c +4 -4
@@ -1775,10 +1775,10 @@ void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign, bool with_irqfd) {if (assign && !with_irqfd) { -event_notifier_set_handler (&vq->guest_notifier, +event_notifier_set_handler (&vq->guest_notifier, false, virtio_queue_guest_notifier_read); }else { -event_notifier_set_handler (&vq->guest_notifier,NULL ); +event_notifier_set_handler (&vq->guest_notifier, false,NULL ); }if (!assign) {/* Test and clear notifier before closing it,
@@ -1829,10 +1829,10 @@ void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign, bool set_handler) {if (assign && set_handler) { -event_notifier_set_handler (&vq->host_notifier, +event_notifier_set_handler (&vq->host_notifier, false, virtio_queue_host_notifier_read); }else { -event_notifier_set_handler (&vq->host_notifier,NULL ); +event_notifier_set_handler (&vq->host_notifier, false,NULL ); }if (!assign) {/* Test and clear notifier before after disabling event,
include/qemu/event_notifier.h +3 -1
@@ -34,7 +34,9 @@ int event_notifier_init(EventNotifier *, int active);void event_notifier_cleanup (EventNotifier *);int event_notifier_set (EventNotifier *);int event_notifier_test_and_clear (EventNotifier *); -int event_notifier_set_handler (EventNotifier *,EventNotifierHandler *); +int event_notifier_set_handler (EventNotifier *, +bool is_external, +EventNotifierHandler *);#ifdef CONFIG_POSIX void event_notifier_init_fd (EventNotifier *,int fd);
stubs/set-fd-handler.c +10 -0
@@ -9,3 +9,13 @@ void qemu_set_fd_handler(int fd, {abort (); } + +void aio_set_fd_handler (AioContext *ctx, +int fd, +bool is_external, +IOHandler *io_read, +IOHandler *io_write, +void *opaque) +{ +abort (); +}
target-i386/hyperv.c +3 -3
@@ -88,7 +88,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint, goto err_sint_set_notifier; } -event_notifier_set_handler (&sint_route->sint_ack_notifier, +event_notifier_set_handler (&sint_route->sint_ack_notifier, false, kvm_hv_sint_ack_handler); gsi =kvm_irqchip_add_hv_sint_route (kvm_state, vcpu_id, sint);
@@ -112,7 +112,7 @@ HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint, err_irqfd:kvm_irqchip_release_virq (kvm_state, gsi); err_gsi: -event_notifier_set_handler (&sint_route->sint_ack_notifier,NULL ); +event_notifier_set_handler (&sint_route->sint_ack_notifier, false,NULL );event_notifier_cleanup (&sint_route->sint_ack_notifier); err_sint_set_notifier:event_notifier_cleanup (&sint_route->sint_set_notifier);
@@ -128,7 +128,7 @@ void kvm_hv_sint_route_destroy(HvSintRoute *sint_route) &sint_route->sint_set_notifier, sint_route->gsi);kvm_irqchip_release_virq (kvm_state, sint_route->gsi); -event_notifier_set_handler (&sint_route->sint_ack_notifier,NULL ); +event_notifier_set_handler (&sint_route->sint_ack_notifier, false,NULL );event_notifier_cleanup (&sint_route->sint_ack_notifier);event_notifier_cleanup (&sint_route->sint_set_notifier);g_free (sint_route);
util/event_notifier-posix.c +3 -1
@@ -91,9 +91,11 @@ int event_notifier_get_fd(const EventNotifier *e) }int event_notifier_set_handler (EventNotifier *e, +bool is_external,EventNotifierHandler *handler) { -qemu_set_fd_handler (e->rfd, (IOHandler *)handler,NULL , e); +aio_set_fd_handler (iohandler_get_aio_context (), e->rfd, is_external, + (IOHandler *)handler,NULL , e);return 0 ; }
util/event_notifier-win32.c +1 -0
@@ -33,6 +33,7 @@ HANDLE event_notifier_get_handle(EventNotifier *e) }int event_notifier_set_handler (EventNotifier *e, +bool is_external,EventNotifierHandler *handler) {if (handler) {
Signature · 1 line
1.8.3.1
[Qemu-devel] [PULL 5/5] mirror: Workaround for unexpected iohandler events during completion
In reply to Kevin WolfFrom: Fam Zheng <[email protected]>
Commit 5a7e7a0ba moved mirror_exit to a BH handler but didn't add any protection against new requests that could sneak in just before the BH is dispatched. For example (assuming a code base at that commit):
main_loop_wait # 1
os_host_main_loop_wait
g_main_context_dispatch
aio_ctx_dispatch
aio_dispatch
...
mirror_run
bdrv_drain
(a) block_job_defer_to_main_loop
qemu_iohandler_poll
virtio_queue_host_notifier_read
...
virtio_submit_multiwrite
(b) blk_aio_multiwrite
main_loop_wait # 2
<snip>
aio_dispatch
aio_bh_poll
(c) mirror_exitAt (a) we know the BDS has no pending request. However, the same main_loop_wait call is going to dispatch iohandlers (EventNotifier events), which may lead to a new I/O from guest. So the invariant is already broken at (c). Data loss.
Commit f3926945c8 made iohandler to use aio API. The order of virtio_queue_host_notifier_read and block_job_defer_to_main_loop within a main_loop_wait becomes unpredictable, and even worse, if the host notifier event arrives at the next main_loop_wait call, the unpredictable order between mirror_exit and virtio_queue_host_notifier_read is also a trouble. As shown below, this commit made the bug easier to trigger:
- Bug case 1:
main_loop_wait # 1
os_host_main_loop_wait
g_main_context_dispatch
aio_ctx_dispatch (qemu_aio_context)
...
mirror_run
bdrv_drain
(a) block_job_defer_to_main_loop
aio_ctx_dispatch (iohandler_ctx)
virtio_queue_host_notifier_read
...
virtio_submit_multiwrite
(b) blk_aio_multiwrite
main_loop_wait # 2
...
aio_dispatch
aio_bh_poll
(c) mirror_exit
- Bug case 2:
main_loop_wait # 1
os_host_main_loop_wait
g_main_context_dispatch
aio_ctx_dispatch (qemu_aio_context)
...
mirror_run
bdrv_drain
(a) block_job_defer_to_main_loop
main_loop_wait # 2
...
aio_ctx_dispatch (iohandler_ctx)
virtio_queue_host_notifier_read
...
virtio_submit_multiwrite
(b) blk_aio_multiwrite
aio_dispatch
aio_bh_poll
(c) mirror_exit
In both cases, (b) breaks the invariant wanted by (a) and (c).
Until then, the request loss has been silent. Later, 3f09bfbc7be added asserts at (c) to check the invariant (in bdrv_replace_in_backing_chain), and Max reported an assertion failure first visible there, by doing active committing while the guest is running bonnie++.
2.5 added bdrv_drained_begin at (a) to protect the dataplane case from similar problems, but we never realize the main loop bug until now.
As a bandage, this patch disables iohandler's external events temporarily together with bs->ctx.
Launchpad Bug: 1570134
--- block/mirror.c | 9 +++++++++ 1 file changed, 9 insertions(+)
block/mirror.c +9 -0
@@ -495,6 +495,9 @@ out:block_job_completed (&s->common, data->ret);g_free (data);bdrv_drained_end (src); +if (qemu_get_aio_context () ==bdrv_get_aio_context (src)) { +aio_enable_external (iohandler_get_aio_context ()); + }bdrv_unref (src); }
@@ -716,6 +719,12 @@ immediate_exit:/* Before we switch to target in mirror_exit, make sure data doesn't * change. */ bdrv_drained_begin (s->common.bs); +if (qemu_get_aio_context () ==bdrv_get_aio_context (bs)) { +/* FIXME: virtio host notifiers run on iohandler_ctx, therefore the +* above bdrv_drained_end isn't enough to quiesce it. This is ugly, we +* need a block layer API change to achieve this. */ +aio_disable_external (iohandler_get_aio_context ()); + }block_job_defer_to_main_loop (&s->common, mirror_exit, data); }
Signature · 1 line
1.8.3.1
[Qemu-devel] [PULL 1/5] iohandler: Introduce iohandler_get_aio_context
In reply to Kevin WolfFrom: Fam Zheng <[email protected]>
--- include/qemu/main-loop.h | 1 + iohandler.c | 6 ++++++ stubs/Makefile.objs | 1 + stubs/iohandler.c | 8 ++++++++ 4 files changed, 16 insertions(+) create mode 100644 stubs/iohandler.c
include/qemu/main-loop.h +1 -0
@@ -204,6 +204,7 @@ void qemu_set_fd_handler(int fd, void *opaque);GSource *iohandler_get_g_source (void ); +AioContext *iohandler_get_aio_context (void );#ifdef CONFIG_POSIX /** * qemu_add_child_watch: Register a child process for reaping.
iohandler.c +6 -0
@@ -44,6 +44,12 @@ static void iohandler_init(void) } } +AioContext *iohandler_get_aio_context (void ) +{ +iohandler_init (); +return iohandler_ctx; +} +GSource *iohandler_get_g_source (void ) {iohandler_init ();
stubs/Makefile.objs +1 -0
@@ -40,3 +40,4 @@ stub-obj-y += qmp_pc_dimm_device_list.o stub-obj-y += target-monitor-defs.o stub-obj-y += target-get-monitor-def.o stub-obj-y += vhost.o +stub-obj-y += iohandler.o
stubs/iohandler.c new file +8 -0
@@ -0,0 +1,8 @@ +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qemu/main-loop.h" + +AioContext *iohandler_get_aio_context (void ) +{ +abort (); +}
Signature · 1 line
1.8.3.1
Peter Maydell
AppliedQuoted 15 lines · Kevin Wolf
The following changes since commit
ee1e0f8e5d3682c561edcdceccff72b9d9b16d8b:util: align memory allocations to 2M on AArch64 (2016-04-22 12:26:01 +0100)
are available in the git repository at:
git://repo.or.cz/qemu/kevin.gittags/for-upstreamfor you to fetch changes up to
ab27c3b5e7408693dde0b565f050aa55c4a1bcef:mirror: Workaround for unexpected iohandler events during completion (2016-04-22 16:44:09 +0200)
----------------------------------------------------------------
Mirror block job fixes for 2.6.0-rc4
Quoted 7 lines
----------------------------------------------------------------
Fam Zheng (5):
iohandler: Introduceiohandler_get_aio_context
event-notifier: Add "is_external" parameter
virtio: Mark host notifiers as external
aio-posix: Skip external nodes inaio_dispatch
mirror: Workaround for unexpected iohandler events during completion
Applied, thanks.
-- PMM
Re: [Qemu-devel] [PULL 4/5] aio-posix: Skip external nodes in aio_dispatch
In reply to Kevin WolfQuoted 7 lines · Kevin Wolf
From: Fam Zheng <[email protected]>
aio_polldoesn't poll the external nodes so this should never be true, butaio_ctx_dispatchmay get notified by the events from GSource. To makebdrv_drained_begineffective in main loop, we should check theis_externalflag here too.
Quoted 6 lines
Also do the check in
aio_pendingsoaio_dispatchis not called superfluously, when there is no events other than external ones.Signed-off-by Fam Zheng <[email protected]>Reviewed-by Jeff Cody <[email protected]>Signed-off-by Kevin Wolf <[email protected]>
Are you going to do the aio-win32 version? I'm not sure what's the state of ioeventfd emulation.
Paolo
Quoted 40 lines
--- aio-posix.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)aio-posix.c +6
-2@@ -282,10 +282,12 @@ bool aio_pending(AioContext *ctx)int revents; revents = node->pfd.revents & node->pfd.events; -if (revents & (G_IO_IN |G_IO_HUP |G_IO_ERR ) && node->io_read) { +if (revents & (G_IO_IN |G_IO_HUP |G_IO_ERR ) && node->io_read && +aio_node_check (ctx, node->is_external)) {return true; } -if (revents & (G_IO_OUT |G_IO_ERR ) && node->io_write) { +if (revents & (G_IO_OUT |G_IO_ERR ) && node->io_write && +aio_node_check (ctx, node->is_external)) {return true; } }@@ -323,6 +325,7 @@ bool aio_dispatch(AioContext *ctx)if (!node->deleted && (revents & (G_IO_IN |G_IO_HUP |G_IO_ERR )) && +aio_node_check (ctx, node->is_external) && node->io_read) { node->io_read (node->opaque);@@ -333,6 +336,7 @@ bool aio_dispatch(AioContext *ctx) }if (!node->deleted && (revents & (G_IO_OUT |G_IO_ERR )) && +aio_node_check (ctx, node->is_external) && node->io_write) { node->io_write (node->opaque); progress = true;