]>
Commit | Line | Data |
---|---|---|
e72f66a0 SH |
1 | /* |
2 | * Dedicated thread for virtio-blk I/O processing | |
3 | * | |
4 | * Copyright 2012 IBM, Corp. | |
5 | * Copyright 2012 Red Hat, Inc. and/or its affiliates | |
6 | * | |
7 | * Authors: | |
8 | * Stefan Hajnoczi <[email protected]> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
11 | * See the COPYING file in the top-level directory. | |
12 | * | |
13 | */ | |
14 | ||
80c71a24 | 15 | #include "qemu/osdep.h" |
da34e65c | 16 | #include "qapi/error.h" |
e72f66a0 SH |
17 | #include "trace.h" |
18 | #include "qemu/iov.h" | |
db725815 | 19 | #include "qemu/main-loop.h" |
e72f66a0 | 20 | #include "qemu/thread.h" |
b4a42f81 | 21 | #include "qemu/error-report.h" |
b0e5d90e | 22 | #include "hw/virtio/virtio-access.h" |
0d09e41a PB |
23 | #include "hw/virtio/virtio-blk.h" |
24 | #include "virtio-blk.h" | |
2c20e711 | 25 | #include "block/aio.h" |
1c819449 | 26 | #include "hw/virtio/virtio-bus.h" |
54bee5c2 | 27 | #include "qom/object_interfaces.h" |
e72f66a0 | 28 | |
e72f66a0 | 29 | struct VirtIOBlockDataPlane { |
8caf907f | 30 | bool starting; |
cd7fdfe5 | 31 | bool stopping; |
e72f66a0 | 32 | |
2a30307f | 33 | VirtIOBlkConf *conf; |
e72f66a0 | 34 | VirtIODevice *vdev; |
5b2ffbe4 | 35 | QEMUBH *bh; /* bh for guest notification */ |
e21737ab | 36 | unsigned long *batch_notify_vqs; |
12c1c7d7 | 37 | bool batch_notifications; |
e72f66a0 | 38 | |
2c20e711 PB |
39 | /* Note that these EventNotifiers are assigned by value. This is |
40 | * fine as long as you do not call event_notifier_cleanup on them | |
41 | * (because you don't own the file descriptor or handle; you just | |
42 | * use it). | |
43 | */ | |
48ff2692 | 44 | IOThread *iothread; |
2c20e711 | 45 | AioContext *ctx; |
e72f66a0 SH |
46 | }; |
47 | ||
48 | /* Raise an interrupt to signal guest, if necessary */ | |
b234cdda | 49 | void virtio_blk_data_plane_notify(VirtIOBlockDataPlane *s, VirtQueue *vq) |
e72f66a0 | 50 | { |
12c1c7d7 SL |
51 | if (s->batch_notifications) { |
52 | set_bit(virtio_get_queue_index(vq), s->batch_notify_vqs); | |
53 | qemu_bh_schedule(s->bh); | |
54 | } else { | |
55 | virtio_notify_irqfd(s->vdev, vq); | |
56 | } | |
e72f66a0 SH |
57 | } |
58 | ||
5b2ffbe4 ML |
59 | static void notify_guest_bh(void *opaque) |
60 | { | |
61 | VirtIOBlockDataPlane *s = opaque; | |
e21737ab SH |
62 | unsigned nvqs = s->conf->num_queues; |
63 | unsigned long bitmap[BITS_TO_LONGS(nvqs)]; | |
64 | unsigned j; | |
5b2ffbe4 | 65 | |
e21737ab SH |
66 | memcpy(bitmap, s->batch_notify_vqs, sizeof(bitmap)); |
67 | memset(s->batch_notify_vqs, 0, sizeof(bitmap)); | |
68 | ||
69 | for (j = 0; j < nvqs; j += BITS_PER_LONG) { | |
725fe5d1 | 70 | unsigned long bits = bitmap[j / BITS_PER_LONG]; |
03de2f52 | 71 | |
e21737ab SH |
72 | while (bits != 0) { |
73 | unsigned i = j + ctzl(bits); | |
74 | VirtQueue *vq = virtio_get_queue(s->vdev, i); | |
75 | ||
83d768b5 | 76 | virtio_notify_irqfd(s->vdev, vq); |
e21737ab SH |
77 | |
78 | bits &= bits - 1; /* clear right-most bit */ | |
79 | } | |
80 | } | |
e72f66a0 SH |
81 | } |
82 | ||
48ff2692 | 83 | /* Context: QEMU global mutex held */ |
9d3b1551 | 84 | bool virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf, |
3ffeeef7 AF |
85 | VirtIOBlockDataPlane **dataplane, |
86 | Error **errp) | |
e72f66a0 SH |
87 | { |
88 | VirtIOBlockDataPlane *s; | |
a9968c77 CH |
89 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
90 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
e72f66a0 SH |
91 | |
92 | *dataplane = NULL; | |
93 | ||
9ffe337c PB |
94 | if (conf->iothread) { |
95 | if (!k->set_guest_notifiers || !k->ioeventfd_assign) { | |
96 | error_setg(errp, | |
97 | "device is incompatible with iothread " | |
98 | "(transport does not support notifiers)"); | |
9d3b1551 | 99 | return false; |
9ffe337c PB |
100 | } |
101 | if (!virtio_device_ioeventfd_enabled(vdev)) { | |
102 | error_setg(errp, "ioeventfd is required for iothread"); | |
9d3b1551 | 103 | return false; |
9ffe337c | 104 | } |
e72f66a0 | 105 | |
9ffe337c PB |
106 | /* If dataplane is (re-)enabled while the guest is running there could |
107 | * be block jobs that can conflict. | |
108 | */ | |
109 | if (blk_op_is_blocked(conf->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) { | |
110 | error_prepend(errp, "cannot start virtio-blk dataplane: "); | |
9d3b1551 | 111 | return false; |
9ffe337c | 112 | } |
a9968c77 | 113 | } |
9ffe337c PB |
114 | /* Don't try if transport does not support notifiers. */ |
115 | if (!virtio_device_ioeventfd_enabled(vdev)) { | |
9d3b1551 | 116 | return false; |
b0f2027c SH |
117 | } |
118 | ||
e72f66a0 SH |
119 | s = g_new0(VirtIOBlockDataPlane, 1); |
120 | s->vdev = vdev; | |
2a30307f | 121 | s->conf = conf; |
e72f66a0 | 122 | |
9ffe337c PB |
123 | if (conf->iothread) { |
124 | s->iothread = conf->iothread; | |
125 | object_ref(OBJECT(s->iothread)); | |
126 | s->ctx = iothread_get_aio_context(s->iothread); | |
127 | } else { | |
128 | s->ctx = qemu_get_aio_context(); | |
129 | } | |
5b2ffbe4 | 130 | s->bh = aio_bh_new(s->ctx, notify_guest_bh, s); |
e21737ab | 131 | s->batch_notify_vqs = bitmap_new(conf->num_queues); |
48ff2692 | 132 | |
e72f66a0 | 133 | *dataplane = s; |
9d3b1551 MZ |
134 | |
135 | return true; | |
e72f66a0 SH |
136 | } |
137 | ||
48ff2692 | 138 | /* Context: QEMU global mutex held */ |
e72f66a0 SH |
139 | void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s) |
140 | { | |
9ffe337c PB |
141 | VirtIOBlock *vblk; |
142 | ||
e72f66a0 SH |
143 | if (!s) { |
144 | return; | |
145 | } | |
146 | ||
9ffe337c PB |
147 | vblk = VIRTIO_BLK(s->vdev); |
148 | assert(!vblk->dataplane_started); | |
e21737ab | 149 | g_free(s->batch_notify_vqs); |
5b2ffbe4 | 150 | qemu_bh_delete(s->bh); |
9ffe337c PB |
151 | if (s->iothread) { |
152 | object_unref(OBJECT(s->iothread)); | |
153 | } | |
e72f66a0 SH |
154 | g_free(s); |
155 | } | |
156 | ||
48ff2692 | 157 | /* Context: QEMU global mutex held */ |
9ffe337c | 158 | int virtio_blk_data_plane_start(VirtIODevice *vdev) |
e72f66a0 | 159 | { |
9ffe337c PB |
160 | VirtIOBlock *vblk = VIRTIO_BLK(vdev); |
161 | VirtIOBlockDataPlane *s = vblk->dataplane; | |
162 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vblk))); | |
1c819449 | 163 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
c7040ff6 | 164 | AioContext *old_context; |
51b04ac5 SH |
165 | unsigned i; |
166 | unsigned nvqs = s->conf->num_queues; | |
97896a48 | 167 | Error *local_err = NULL; |
267e1a20 | 168 | int r; |
e72f66a0 | 169 | |
2906cddf | 170 | if (vblk->dataplane_started || s->starting) { |
9ffe337c | 171 | return 0; |
8caf907f CH |
172 | } |
173 | ||
174 | s->starting = true; | |
e72f66a0 | 175 | |
12c1c7d7 SL |
176 | if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) { |
177 | s->batch_notifications = true; | |
178 | } else { | |
179 | s->batch_notifications = false; | |
180 | } | |
181 | ||
e72f66a0 | 182 | /* Set up guest notifier (irq) */ |
51b04ac5 | 183 | r = k->set_guest_notifiers(qbus->parent, nvqs, true); |
267e1a20 | 184 | if (r != 0) { |
a1d30f28 TH |
185 | error_report("virtio-blk failed to set guest notifier (%d), " |
186 | "ensure -accel kvm is set.", r); | |
f9907ebc | 187 | goto fail_guest_notifiers; |
e72f66a0 | 188 | } |
e72f66a0 | 189 | |
9cf4fd87 GK |
190 | /* |
191 | * Batch all the host notifiers in a single transaction to avoid | |
192 | * quadratic time complexity in address_space_update_ioeventfds(). | |
193 | */ | |
d0267da6 GK |
194 | memory_region_transaction_begin(); |
195 | ||
e72f66a0 | 196 | /* Set up virtqueue notify */ |
51b04ac5 SH |
197 | for (i = 0; i < nvqs; i++) { |
198 | r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, true); | |
199 | if (r != 0) { | |
d0267da6 GK |
200 | int j = i; |
201 | ||
51b04ac5 SH |
202 | fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r); |
203 | while (i--) { | |
204 | virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); | |
d0267da6 GK |
205 | } |
206 | ||
9cf4fd87 GK |
207 | /* |
208 | * The transaction expects the ioeventfds to be open when it | |
209 | * commits. Do it now, before the cleanup loop. | |
210 | */ | |
d0267da6 GK |
211 | memory_region_transaction_commit(); |
212 | ||
213 | while (j--) { | |
5b807181 | 214 | virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), j); |
51b04ac5 | 215 | } |
570fe439 | 216 | goto fail_host_notifiers; |
51b04ac5 | 217 | } |
e72f66a0 | 218 | } |
e926d9b8 | 219 | |
d0267da6 GK |
220 | memory_region_transaction_commit(); |
221 | ||
9a4b6a63 SH |
222 | /* |
223 | * These fields are visible to the IOThread so we rely on implicit barriers | |
224 | * in aio_context_acquire() on the write side and aio_notify_accept() on | |
225 | * the read side. | |
226 | */ | |
8caf907f | 227 | s->starting = false; |
2906cddf | 228 | vblk->dataplane_started = true; |
e72f66a0 SH |
229 | trace_virtio_blk_data_plane_start(s); |
230 | ||
c7040ff6 SL |
231 | old_context = blk_get_aio_context(s->conf->conf.blk); |
232 | aio_context_acquire(old_context); | |
97896a48 | 233 | r = blk_set_aio_context(s->conf->conf.blk, s->ctx, &local_err); |
c7040ff6 | 234 | aio_context_release(old_context); |
97896a48 KW |
235 | if (r < 0) { |
236 | error_report_err(local_err); | |
570fe439 | 237 | goto fail_aio_context; |
97896a48 | 238 | } |
580b6b2a | 239 | |
49b44549 SL |
240 | /* Process queued requests before the ones in vring */ |
241 | virtio_blk_process_queued_requests(vblk, false); | |
242 | ||
e72f66a0 | 243 | /* Kick right away to begin processing requests already in vring */ |
51b04ac5 SH |
244 | for (i = 0; i < nvqs; i++) { |
245 | VirtQueue *vq = virtio_get_queue(s->vdev, i); | |
246 | ||
247 | event_notifier_set(virtio_queue_get_host_notifier(vq)); | |
248 | } | |
e72f66a0 | 249 | |
48ff2692 SH |
250 | /* Get this show started by hooking up our callbacks */ |
251 | aio_context_acquire(s->ctx); | |
51b04ac5 SH |
252 | for (i = 0; i < nvqs; i++) { |
253 | VirtQueue *vq = virtio_get_queue(s->vdev, i); | |
254 | ||
db608fb7 | 255 | virtio_queue_aio_attach_host_notifier(vq, s->ctx); |
51b04ac5 | 256 | } |
48ff2692 | 257 | aio_context_release(s->ctx); |
9ffe337c | 258 | return 0; |
f9907ebc | 259 | |
570fe439 | 260 | fail_aio_context: |
d0267da6 GK |
261 | memory_region_transaction_begin(); |
262 | ||
570fe439 GK |
263 | for (i = 0; i < nvqs; i++) { |
264 | virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); | |
d0267da6 GK |
265 | } |
266 | ||
267 | memory_region_transaction_commit(); | |
268 | ||
269 | for (i = 0; i < nvqs; i++) { | |
570fe439 GK |
270 | virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i); |
271 | } | |
272 | fail_host_notifiers: | |
273 | k->set_guest_notifiers(qbus->parent, nvqs, false); | |
f9907ebc | 274 | fail_guest_notifiers: |
49b44549 SL |
275 | /* |
276 | * If we failed to set up the guest notifiers queued requests will be | |
277 | * processed on the main context. | |
278 | */ | |
279 | virtio_blk_process_queued_requests(vblk, false); | |
eb41cf78 | 280 | vblk->dataplane_disabled = true; |
f9907ebc | 281 | s->starting = false; |
2906cddf | 282 | vblk->dataplane_started = true; |
9ffe337c | 283 | return -ENOSYS; |
e72f66a0 SH |
284 | } |
285 | ||
1010cadf SH |
286 | /* Stop notifications for new requests from guest. |
287 | * | |
288 | * Context: BH in IOThread | |
289 | */ | |
290 | static void virtio_blk_data_plane_stop_bh(void *opaque) | |
291 | { | |
292 | VirtIOBlockDataPlane *s = opaque; | |
293 | unsigned i; | |
294 | ||
295 | for (i = 0; i < s->conf->num_queues; i++) { | |
296 | VirtQueue *vq = virtio_get_queue(s->vdev, i); | |
297 | ||
db608fb7 | 298 | virtio_queue_aio_detach_host_notifier(vq, s->ctx); |
1010cadf SH |
299 | } |
300 | } | |
301 | ||
48ff2692 | 302 | /* Context: QEMU global mutex held */ |
9ffe337c | 303 | void virtio_blk_data_plane_stop(VirtIODevice *vdev) |
e72f66a0 | 304 | { |
9ffe337c PB |
305 | VirtIOBlock *vblk = VIRTIO_BLK(vdev); |
306 | VirtIOBlockDataPlane *s = vblk->dataplane; | |
307 | BusState *qbus = qdev_get_parent_bus(DEVICE(vblk)); | |
1c819449 | 308 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
51b04ac5 SH |
309 | unsigned i; |
310 | unsigned nvqs = s->conf->num_queues; | |
2f5f70fa | 311 | |
2906cddf PB |
312 | if (!vblk->dataplane_started || s->stopping) { |
313 | return; | |
314 | } | |
2f5f70fa CH |
315 | |
316 | /* Better luck next time. */ | |
eb41cf78 PB |
317 | if (vblk->dataplane_disabled) { |
318 | vblk->dataplane_disabled = false; | |
2906cddf | 319 | vblk->dataplane_started = false; |
e72f66a0 SH |
320 | return; |
321 | } | |
cd7fdfe5 | 322 | s->stopping = true; |
e72f66a0 SH |
323 | trace_virtio_blk_data_plane_stop(s); |
324 | ||
48ff2692 | 325 | aio_context_acquire(s->ctx); |
1010cadf | 326 | aio_wait_bh_oneshot(s->ctx, virtio_blk_data_plane_stop_bh, s); |
48ff2692 | 327 | |
97896a48 KW |
328 | /* Drain and try to switch bs back to the QEMU main loop. If other users |
329 | * keep the BlockBackend in the iothread, that's ok */ | |
330 | blk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context(), NULL); | |
e72f66a0 | 331 | |
48ff2692 | 332 | aio_context_release(s->ctx); |
e72f66a0 | 333 | |
9cf4fd87 GK |
334 | /* |
335 | * Batch all the host notifiers in a single transaction to avoid | |
336 | * quadratic time complexity in address_space_update_ioeventfds(). | |
337 | */ | |
d0267da6 GK |
338 | memory_region_transaction_begin(); |
339 | ||
51b04ac5 SH |
340 | for (i = 0; i < nvqs; i++) { |
341 | virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); | |
d0267da6 GK |
342 | } |
343 | ||
9cf4fd87 GK |
344 | /* |
345 | * The transaction expects the ioeventfds to be open when it | |
346 | * commits. Do it now, before the cleanup loop. | |
347 | */ | |
d0267da6 GK |
348 | memory_region_transaction_commit(); |
349 | ||
350 | for (i = 0; i < nvqs; i++) { | |
76143618 | 351 | virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i); |
51b04ac5 | 352 | } |
e72f66a0 | 353 | |
ebb6ff25 PMD |
354 | qemu_bh_cancel(s->bh); |
355 | notify_guest_bh(s); /* final chance to notify guest */ | |
356 | ||
e72f66a0 | 357 | /* Clean up guest notifier (irq) */ |
51b04ac5 | 358 | k->set_guest_notifiers(qbus->parent, nvqs, false); |
e72f66a0 | 359 | |
2906cddf | 360 | vblk->dataplane_started = false; |
cd7fdfe5 | 361 | s->stopping = false; |
e72f66a0 | 362 | } |