2 * Dedicated thread for virtio-blk I/O processing
4 * Copyright 2012 IBM, Corp.
5 * Copyright 2012 Red Hat, Inc. and/or its affiliates
8 * Stefan Hajnoczi <stefanha@redhat.com>
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.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/thread.h"
21 #include "qemu/error-report.h"
22 #include "hw/virtio/virtio-blk.h"
23 #include "virtio-blk.h"
24 #include "block/aio.h"
25 #include "hw/virtio/virtio-bus.h"
26 #include "qom/object_interfaces.h"
28 struct VirtIOBlockDataPlane
{
36 * The AioContext for each virtqueue. The BlockDriverState will use the
37 * first element as its AioContext.
39 AioContext
**vq_aio_context
;
42 /* Raise an interrupt to signal guest, if necessary */
43 void virtio_blk_data_plane_notify(VirtIOBlockDataPlane
*s
, VirtQueue
*vq
)
45 virtio_notify_irqfd(s
->vdev
, vq
);
48 /* Generate vq:AioContext mappings from a validated iothread-vq-mapping list */
50 apply_vq_mapping(IOThreadVirtQueueMappingList
*iothread_vq_mapping_list
,
51 AioContext
**vq_aio_context
, uint16_t num_queues
)
53 IOThreadVirtQueueMappingList
*node
;
54 size_t num_iothreads
= 0;
55 size_t cur_iothread
= 0;
57 for (node
= iothread_vq_mapping_list
; node
; node
= node
->next
) {
61 for (node
= iothread_vq_mapping_list
; node
; node
= node
->next
) {
62 IOThread
*iothread
= iothread_by_id(node
->value
->iothread
);
63 AioContext
*ctx
= iothread_get_aio_context(iothread
);
65 /* Released in virtio_blk_data_plane_destroy() */
66 object_ref(OBJECT(iothread
));
68 if (node
->value
->vqs
) {
71 /* Explicit vq:IOThread assignment */
72 for (vq
= node
->value
->vqs
; vq
; vq
= vq
->next
) {
73 vq_aio_context
[vq
->value
] = ctx
;
76 /* Round-robin vq:IOThread assignment */
77 for (unsigned i
= cur_iothread
; i
< num_queues
;
79 vq_aio_context
[i
] = ctx
;
87 /* Context: BQL held */
88 bool virtio_blk_data_plane_create(VirtIODevice
*vdev
, VirtIOBlkConf
*conf
,
89 VirtIOBlockDataPlane
**dataplane
,
92 VirtIOBlockDataPlane
*s
;
93 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
94 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
98 if (conf
->iothread
|| conf
->iothread_vq_mapping_list
) {
99 if (!k
->set_guest_notifiers
|| !k
->ioeventfd_assign
) {
101 "device is incompatible with iothread "
102 "(transport does not support notifiers)");
105 if (!virtio_device_ioeventfd_enabled(vdev
)) {
106 error_setg(errp
, "ioeventfd is required for iothread");
110 /* If dataplane is (re-)enabled while the guest is running there could
111 * be block jobs that can conflict.
113 if (blk_op_is_blocked(conf
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
114 error_prepend(errp
, "cannot start virtio-blk dataplane: ");
118 /* Don't try if transport does not support notifiers. */
119 if (!virtio_device_ioeventfd_enabled(vdev
)) {
123 s
= g_new0(VirtIOBlockDataPlane
, 1);
126 s
->vq_aio_context
= g_new(AioContext
*, conf
->num_queues
);
128 if (conf
->iothread_vq_mapping_list
) {
129 apply_vq_mapping(conf
->iothread_vq_mapping_list
, s
->vq_aio_context
,
131 } else if (conf
->iothread
) {
132 AioContext
*ctx
= iothread_get_aio_context(conf
->iothread
);
133 for (unsigned i
= 0; i
< conf
->num_queues
; i
++) {
134 s
->vq_aio_context
[i
] = ctx
;
137 /* Released in virtio_blk_data_plane_destroy() */
138 object_ref(OBJECT(conf
->iothread
));
140 AioContext
*ctx
= qemu_get_aio_context();
141 for (unsigned i
= 0; i
< conf
->num_queues
; i
++) {
142 s
->vq_aio_context
[i
] = ctx
;
151 /* Context: BQL held */
152 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane
*s
)
161 vblk
= VIRTIO_BLK(s
->vdev
);
162 assert(!vblk
->dataplane_started
);
165 if (conf
->iothread_vq_mapping_list
) {
166 IOThreadVirtQueueMappingList
*node
;
168 for (node
= conf
->iothread_vq_mapping_list
; node
; node
= node
->next
) {
169 IOThread
*iothread
= iothread_by_id(node
->value
->iothread
);
170 object_unref(OBJECT(iothread
));
174 if (conf
->iothread
) {
175 object_unref(OBJECT(conf
->iothread
));
178 g_free(s
->vq_aio_context
);
182 /* Context: BQL held */
183 int virtio_blk_data_plane_start(VirtIODevice
*vdev
)
185 VirtIOBlock
*vblk
= VIRTIO_BLK(vdev
);
186 VirtIOBlockDataPlane
*s
= vblk
->dataplane
;
187 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vblk
)));
188 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
190 unsigned nvqs
= s
->conf
->num_queues
;
191 Error
*local_err
= NULL
;
194 if (vblk
->dataplane_started
|| s
->starting
) {
200 /* Set up guest notifier (irq) */
201 r
= k
->set_guest_notifiers(qbus
->parent
, nvqs
, true);
203 error_report("virtio-blk failed to set guest notifier (%d), "
204 "ensure -accel kvm is set.", r
);
205 goto fail_guest_notifiers
;
209 * Batch all the host notifiers in a single transaction to avoid
210 * quadratic time complexity in address_space_update_ioeventfds().
212 memory_region_transaction_begin();
214 /* Set up virtqueue notify */
215 for (i
= 0; i
< nvqs
; i
++) {
216 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, true);
220 fprintf(stderr
, "virtio-blk failed to set host notifier (%d)\n", r
);
222 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
226 * The transaction expects the ioeventfds to be open when it
227 * commits. Do it now, before the cleanup loop.
229 memory_region_transaction_commit();
232 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), j
);
234 goto fail_host_notifiers
;
238 memory_region_transaction_commit();
240 trace_virtio_blk_data_plane_start(s
);
242 r
= blk_set_aio_context(s
->conf
->conf
.blk
, s
->vq_aio_context
[0],
245 error_report_err(local_err
);
246 goto fail_aio_context
;
250 * These fields must be visible to the IOThread when it processes the
251 * virtqueue, otherwise it will think dataplane has not started yet.
253 * Make sure ->dataplane_started is false when blk_set_aio_context() is
254 * called above so that draining does not cause the host notifier to be
255 * detached/attached prematurely.
258 vblk
->dataplane_started
= true;
259 smp_wmb(); /* paired with aio_notify_accept() on the read side */
261 /* Get this show started by hooking up our callbacks */
262 if (!blk_in_drain(s
->conf
->conf
.blk
)) {
263 for (i
= 0; i
< nvqs
; i
++) {
264 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
265 AioContext
*ctx
= s
->vq_aio_context
[i
];
267 /* Kick right away to begin processing requests already in vring */
268 event_notifier_set(virtio_queue_get_host_notifier(vq
));
270 virtio_queue_aio_attach_host_notifier(vq
, ctx
);
276 memory_region_transaction_begin();
278 for (i
= 0; i
< nvqs
; i
++) {
279 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
282 memory_region_transaction_commit();
284 for (i
= 0; i
< nvqs
; i
++) {
285 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), i
);
288 k
->set_guest_notifiers(qbus
->parent
, nvqs
, false);
289 fail_guest_notifiers
:
290 vblk
->dataplane_disabled
= true;
295 /* Stop notifications for new requests from guest.
297 * Context: BH in IOThread
299 static void virtio_blk_data_plane_stop_vq_bh(void *opaque
)
301 VirtQueue
*vq
= opaque
;
302 EventNotifier
*host_notifier
= virtio_queue_get_host_notifier(vq
);
304 virtio_queue_aio_detach_host_notifier(vq
, qemu_get_current_aio_context());
307 * Test and clear notifier after disabling event, in case poll callback
308 * didn't have time to run.
310 virtio_queue_host_notifier_read(host_notifier
);
313 /* Context: BQL held */
314 void virtio_blk_data_plane_stop(VirtIODevice
*vdev
)
316 VirtIOBlock
*vblk
= VIRTIO_BLK(vdev
);
317 VirtIOBlockDataPlane
*s
= vblk
->dataplane
;
318 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vblk
));
319 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
321 unsigned nvqs
= s
->conf
->num_queues
;
323 if (!vblk
->dataplane_started
|| s
->stopping
) {
327 /* Better luck next time. */
328 if (vblk
->dataplane_disabled
) {
329 vblk
->dataplane_disabled
= false;
330 vblk
->dataplane_started
= false;
334 trace_virtio_blk_data_plane_stop(s
);
336 if (!blk_in_drain(s
->conf
->conf
.blk
)) {
337 for (i
= 0; i
< nvqs
; i
++) {
338 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
339 AioContext
*ctx
= s
->vq_aio_context
[i
];
341 aio_wait_bh_oneshot(ctx
, virtio_blk_data_plane_stop_vq_bh
, vq
);
346 * Batch all the host notifiers in a single transaction to avoid
347 * quadratic time complexity in address_space_update_ioeventfds().
349 memory_region_transaction_begin();
351 for (i
= 0; i
< nvqs
; i
++) {
352 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
356 * The transaction expects the ioeventfds to be open when it
357 * commits. Do it now, before the cleanup loop.
359 memory_region_transaction_commit();
361 for (i
= 0; i
< nvqs
; i
++) {
362 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), i
);
366 * Set ->dataplane_started to false before draining so that host notifiers
367 * are not detached/attached anymore.
369 vblk
->dataplane_started
= false;
371 /* Wait for virtio_blk_dma_restart_bh() and in flight I/O to complete */
372 blk_drain(s
->conf
->conf
.blk
);
375 * Try to switch bs back to the QEMU main loop. If other users keep the
376 * BlockBackend in the iothread, that's ok
378 blk_set_aio_context(s
->conf
->conf
.blk
, qemu_get_aio_context(), NULL
);
380 /* Clean up guest notifier (irq) */
381 k
->set_guest_notifiers(qbus
->parent
, nvqs
, false);
386 void virtio_blk_data_plane_detach(VirtIOBlockDataPlane
*s
)
388 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
->vdev
);
390 for (uint16_t i
= 0; i
< s
->conf
->num_queues
; i
++) {
391 VirtQueue
*vq
= virtio_get_queue(vdev
, i
);
392 virtio_queue_aio_detach_host_notifier(vq
, s
->vq_aio_context
[i
]);
396 void virtio_blk_data_plane_attach(VirtIOBlockDataPlane
*s
)
398 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
->vdev
);
400 for (uint16_t i
= 0; i
< s
->conf
->num_queues
; i
++) {
401 VirtQueue
*vq
= virtio_get_queue(vdev
, i
);
402 virtio_queue_aio_attach_host_notifier(vq
, s
->vq_aio_context
[i
]);