1 | The following changes since commit 98b2e3c9ab3abfe476a2b02f8f51813edb90e72d: | 1 | The following changes since commit 887cba855bb6ff4775256f7968409281350b568c: |
---|---|---|---|
2 | 2 | ||
3 | Merge remote-tracking branch 'remotes/stefanha/tags/block-pull-request' into staging (2019-10-08 16:08:35 +0100) | 3 | configure: Fix cross-building for RISCV host (v5) (2023-07-11 17:56:09 +0100) |
4 | 4 | ||
5 | are available in the Git repository at: | 5 | are available in the Git repository at: |
6 | 6 | ||
7 | https://github.com/stefanha/qemu.git tags/block-pull-request | 7 | https://gitlab.com/stefanha/qemu.git tags/block-pull-request |
8 | 8 | ||
9 | for you to fetch changes up to 69de48445a0d6169f1e2a6c5bfab994e1c810e33: | 9 | for you to fetch changes up to 75dcb4d790bbe5327169fd72b185960ca58e2fa6: |
10 | 10 | ||
11 | test-bdrv-drain: fix iothread_join() hang (2019-10-14 09:48:01 +0100) | 11 | virtio-blk: fix host notifier issues during dataplane start/stop (2023-07-12 15:20:32 -0400) |
12 | 12 | ||
13 | ---------------------------------------------------------------- | 13 | ---------------------------------------------------------------- |
14 | Pull request | 14 | Pull request |
15 | 15 | ||
16 | ---------------------------------------------------------------- | 16 | ---------------------------------------------------------------- |
17 | 17 | ||
18 | Stefan Hajnoczi (1): | 18 | Stefan Hajnoczi (1): |
19 | test-bdrv-drain: fix iothread_join() hang | 19 | virtio-blk: fix host notifier issues during dataplane start/stop |
20 | 20 | ||
21 | tests/iothread.c | 10 ++++++++-- | 21 | hw/block/dataplane/virtio-blk.c | 67 +++++++++++++++++++-------------- |
22 | 1 file changed, 8 insertions(+), 2 deletions(-) | 22 | 1 file changed, 38 insertions(+), 29 deletions(-) |
23 | 23 | ||
24 | -- | 24 | -- |
25 | 2.21.0 | 25 | 2.40.1 |
26 | |||
27 | diff view generated by jsdifflib |
1 | tests/test-bdrv-drain can hang in tests/iothread.c:iothread_run(): | 1 | The main loop thread can consume 100% CPU when using --device |
---|---|---|---|
2 | virtio-blk-pci,iothread=<iothread>. ppoll() constantly returns but | ||
3 | reading virtqueue host notifiers fails with EAGAIN. The file descriptors | ||
4 | are stale and remain registered with the AioContext because of bugs in | ||
5 | the virtio-blk dataplane start/stop code. | ||
2 | 6 | ||
3 | while (!atomic_read(&iothread->stopping)) { | 7 | The problem is that the dataplane start/stop code involves drain |
4 | aio_poll(iothread->ctx, true); | 8 | operations, which call virtio_blk_drained_begin() and |
5 | } | 9 | virtio_blk_drained_end() at points where the host notifier is not |
10 | operational: | ||
11 | - In virtio_blk_data_plane_start(), blk_set_aio_context() drains after | ||
12 | vblk->dataplane_started has been set to true but the host notifier has | ||
13 | not been attached yet. | ||
14 | - In virtio_blk_data_plane_stop(), blk_drain() and blk_set_aio_context() | ||
15 | drain after the host notifier has already been detached but with | ||
16 | vblk->dataplane_started still set to true. | ||
6 | 17 | ||
7 | The iothread_join() function works as follows: | 18 | I would like to simplify ->ioeventfd_start/stop() to avoid interactions |
19 | with drain entirely, but couldn't find a way to do that. Instead, this | ||
20 | patch accepts the fragile nature of the code and reorders it so that | ||
21 | vblk->dataplane_started is false during drain operations. This way the | ||
22 | virtio_blk_drained_begin() and virtio_blk_drained_end() calls don't | ||
23 | touch the host notifier. The result is that | ||
24 | virtio_blk_data_plane_start() and virtio_blk_data_plane_stop() have | ||
25 | complete control over the host notifier and stale file descriptors are | ||
26 | no longer left in the AioContext. | ||
8 | 27 | ||
9 | void iothread_join(IOThread *iothread) | 28 | This patch fixes the 100% CPU consumption in the main loop thread and |
10 | { | 29 | correctly moves host notifier processing to the IOThread. |
11 | iothread->stopping = true; | ||
12 | aio_notify(iothread->ctx); | ||
13 | qemu_thread_join(&iothread->thread); | ||
14 | 30 | ||
15 | If iothread_run() checks iothread->stopping before the iothread_join() | 31 | Fixes: 1665d9326fd2 ("virtio-blk: implement BlockDevOps->drained_begin()") |
16 | thread sets stopping to true, then aio_notify() may be optimized away | 32 | Reported-by: Lukáš Doktor <ldoktor@redhat.com> |
17 | and iothread_run() hangs forever in aio_poll(). | 33 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> |
18 | 34 | Tested-by: Lukas Doktor <ldoktor@redhat.com> | |
19 | The correct way to change iothread->stopping is from a BH that executes | 35 | Message-id: 20230704151527.193586-1-stefanha@redhat.com |
20 | within iothread_run(). This ensures that iothread->stopping is checked | ||
21 | after we set it to true. | ||
22 | |||
23 | This was already fixed for ./iothread.c (note this is a different source | ||
24 | file!) by commit 2362a28ea11c145e1a13ae79342d76dc118a72a6 ("iothread: | ||
25 | fix iothread_stop() race condition"), but not for tests/iothread.c. | ||
26 | |||
27 | Fixes: 0c330a734b51c177ab8488932ac3b0c4d63a718a | ||
28 | ("aio: introduce aio_co_schedule and aio_co_wake") | ||
29 | Reported-by: Dr. David Alan Gilbert <dgilbert@redhat.com> | ||
30 | Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> | ||
31 | Message-Id: <20191003100103.331-1-stefanha@redhat.com> | ||
32 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | 36 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> |
33 | --- | 37 | --- |
34 | tests/iothread.c | 10 ++++++++-- | 38 | hw/block/dataplane/virtio-blk.c | 67 +++++++++++++++++++-------------- |
35 | 1 file changed, 8 insertions(+), 2 deletions(-) | 39 | 1 file changed, 38 insertions(+), 29 deletions(-) |
36 | 40 | ||
37 | diff --git a/tests/iothread.c b/tests/iothread.c | 41 | diff --git a/hw/block/dataplane/virtio-blk.c b/hw/block/dataplane/virtio-blk.c |
38 | index XXXXXXX..XXXXXXX 100644 | 42 | index XXXXXXX..XXXXXXX 100644 |
39 | --- a/tests/iothread.c | 43 | --- a/hw/block/dataplane/virtio-blk.c |
40 | +++ b/tests/iothread.c | 44 | +++ b/hw/block/dataplane/virtio-blk.c |
41 | @@ -XXX,XX +XXX,XX @@ static void *iothread_run(void *opaque) | 45 | @@ -XXX,XX +XXX,XX @@ int virtio_blk_data_plane_start(VirtIODevice *vdev) |
42 | return NULL; | 46 | |
47 | memory_region_transaction_commit(); | ||
48 | |||
49 | - /* | ||
50 | - * These fields are visible to the IOThread so we rely on implicit barriers | ||
51 | - * in aio_context_acquire() on the write side and aio_notify_accept() on | ||
52 | - * the read side. | ||
53 | - */ | ||
54 | - s->starting = false; | ||
55 | - vblk->dataplane_started = true; | ||
56 | trace_virtio_blk_data_plane_start(s); | ||
57 | |||
58 | old_context = blk_get_aio_context(s->conf->conf.blk); | ||
59 | @@ -XXX,XX +XXX,XX @@ int virtio_blk_data_plane_start(VirtIODevice *vdev) | ||
60 | event_notifier_set(virtio_queue_get_host_notifier(vq)); | ||
61 | } | ||
62 | |||
63 | + /* | ||
64 | + * These fields must be visible to the IOThread when it processes the | ||
65 | + * virtqueue, otherwise it will think dataplane has not started yet. | ||
66 | + * | ||
67 | + * Make sure ->dataplane_started is false when blk_set_aio_context() is | ||
68 | + * called above so that draining does not cause the host notifier to be | ||
69 | + * detached/attached prematurely. | ||
70 | + */ | ||
71 | + s->starting = false; | ||
72 | + vblk->dataplane_started = true; | ||
73 | + smp_wmb(); /* paired with aio_notify_accept() on the read side */ | ||
74 | + | ||
75 | /* Get this show started by hooking up our callbacks */ | ||
76 | if (!blk_in_drain(s->conf->conf.blk)) { | ||
77 | aio_context_acquire(s->ctx); | ||
78 | @@ -XXX,XX +XXX,XX @@ int virtio_blk_data_plane_start(VirtIODevice *vdev) | ||
79 | fail_guest_notifiers: | ||
80 | vblk->dataplane_disabled = true; | ||
81 | s->starting = false; | ||
82 | - vblk->dataplane_started = true; | ||
83 | return -ENOSYS; | ||
43 | } | 84 | } |
44 | 85 | ||
45 | -void iothread_join(IOThread *iothread) | 86 | @@ -XXX,XX +XXX,XX @@ void virtio_blk_data_plane_stop(VirtIODevice *vdev) |
46 | +static void iothread_stop_bh(void *opaque) | 87 | aio_wait_bh_oneshot(s->ctx, virtio_blk_data_plane_stop_bh, s); |
47 | { | 88 | } |
48 | + IOThread *iothread = opaque; | 89 | |
90 | + /* | ||
91 | + * Batch all the host notifiers in a single transaction to avoid | ||
92 | + * quadratic time complexity in address_space_update_ioeventfds(). | ||
93 | + */ | ||
94 | + memory_region_transaction_begin(); | ||
49 | + | 95 | + |
50 | iothread->stopping = true; | 96 | + for (i = 0; i < nvqs; i++) { |
51 | - aio_notify(iothread->ctx); | 97 | + virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); |
52 | +} | 98 | + } |
53 | + | 99 | + |
54 | +void iothread_join(IOThread *iothread) | 100 | + /* |
55 | +{ | 101 | + * The transaction expects the ioeventfds to be open when it |
56 | + aio_bh_schedule_oneshot(iothread->ctx, iothread_stop_bh, iothread); | 102 | + * commits. Do it now, before the cleanup loop. |
57 | qemu_thread_join(&iothread->thread); | 103 | + */ |
58 | qemu_cond_destroy(&iothread->init_done_cond); | 104 | + memory_region_transaction_commit(); |
59 | qemu_mutex_destroy(&iothread->init_done_lock); | 105 | + |
106 | + for (i = 0; i < nvqs; i++) { | ||
107 | + virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i); | ||
108 | + } | ||
109 | + | ||
110 | + /* | ||
111 | + * Set ->dataplane_started to false before draining so that host notifiers | ||
112 | + * are not detached/attached anymore. | ||
113 | + */ | ||
114 | + vblk->dataplane_started = false; | ||
115 | + | ||
116 | aio_context_acquire(s->ctx); | ||
117 | |||
118 | /* Wait for virtio_blk_dma_restart_bh() and in flight I/O to complete */ | ||
119 | @@ -XXX,XX +XXX,XX @@ void virtio_blk_data_plane_stop(VirtIODevice *vdev) | ||
120 | |||
121 | aio_context_release(s->ctx); | ||
122 | |||
123 | - /* | ||
124 | - * Batch all the host notifiers in a single transaction to avoid | ||
125 | - * quadratic time complexity in address_space_update_ioeventfds(). | ||
126 | - */ | ||
127 | - memory_region_transaction_begin(); | ||
128 | - | ||
129 | - for (i = 0; i < nvqs; i++) { | ||
130 | - virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); | ||
131 | - } | ||
132 | - | ||
133 | - /* | ||
134 | - * The transaction expects the ioeventfds to be open when it | ||
135 | - * commits. Do it now, before the cleanup loop. | ||
136 | - */ | ||
137 | - memory_region_transaction_commit(); | ||
138 | - | ||
139 | - for (i = 0; i < nvqs; i++) { | ||
140 | - virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i); | ||
141 | - } | ||
142 | - | ||
143 | qemu_bh_cancel(s->bh); | ||
144 | notify_guest_bh(s); /* final chance to notify guest */ | ||
145 | |||
146 | /* Clean up guest notifier (irq) */ | ||
147 | k->set_guest_notifiers(qbus->parent, nvqs, false); | ||
148 | |||
149 | - vblk->dataplane_started = false; | ||
150 | s->stopping = false; | ||
151 | } | ||
60 | -- | 152 | -- |
61 | 2.21.0 | 153 | 2.40.1 |
62 | 154 | ||
63 | 155 | diff view generated by jsdifflib |