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
{
35 /* Note that these EventNotifiers are assigned by value. This is
36 * fine as long as you do not call event_notifier_cleanup on them
37 * (because you don't own the file descriptor or handle; you just
44 /* Raise an interrupt to signal guest, if necessary */
45 void virtio_blk_data_plane_notify(VirtIOBlockDataPlane
*s
, VirtQueue
*vq
)
47 virtio_notify_irqfd(s
->vdev
, vq
);
50 /* Context: QEMU global mutex held */
51 bool virtio_blk_data_plane_create(VirtIODevice
*vdev
, VirtIOBlkConf
*conf
,
52 VirtIOBlockDataPlane
**dataplane
,
55 VirtIOBlockDataPlane
*s
;
56 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
57 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
62 if (!k
->set_guest_notifiers
|| !k
->ioeventfd_assign
) {
64 "device is incompatible with iothread "
65 "(transport does not support notifiers)");
68 if (!virtio_device_ioeventfd_enabled(vdev
)) {
69 error_setg(errp
, "ioeventfd is required for iothread");
73 /* If dataplane is (re-)enabled while the guest is running there could
74 * be block jobs that can conflict.
76 if (blk_op_is_blocked(conf
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
77 error_prepend(errp
, "cannot start virtio-blk dataplane: ");
81 /* Don't try if transport does not support notifiers. */
82 if (!virtio_device_ioeventfd_enabled(vdev
)) {
86 s
= g_new0(VirtIOBlockDataPlane
, 1);
91 s
->iothread
= conf
->iothread
;
92 object_ref(OBJECT(s
->iothread
));
93 s
->ctx
= iothread_get_aio_context(s
->iothread
);
95 s
->ctx
= qemu_get_aio_context();
103 /* Context: QEMU global mutex held */
104 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane
*s
)
112 vblk
= VIRTIO_BLK(s
->vdev
);
113 assert(!vblk
->dataplane_started
);
115 object_unref(OBJECT(s
->iothread
));
120 /* Context: QEMU global mutex held */
121 int virtio_blk_data_plane_start(VirtIODevice
*vdev
)
123 VirtIOBlock
*vblk
= VIRTIO_BLK(vdev
);
124 VirtIOBlockDataPlane
*s
= vblk
->dataplane
;
125 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vblk
)));
126 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
128 unsigned nvqs
= s
->conf
->num_queues
;
129 Error
*local_err
= NULL
;
132 if (vblk
->dataplane_started
|| s
->starting
) {
138 /* Set up guest notifier (irq) */
139 r
= k
->set_guest_notifiers(qbus
->parent
, nvqs
, true);
141 error_report("virtio-blk failed to set guest notifier (%d), "
142 "ensure -accel kvm is set.", r
);
143 goto fail_guest_notifiers
;
147 * Batch all the host notifiers in a single transaction to avoid
148 * quadratic time complexity in address_space_update_ioeventfds().
150 memory_region_transaction_begin();
152 /* Set up virtqueue notify */
153 for (i
= 0; i
< nvqs
; i
++) {
154 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, true);
158 fprintf(stderr
, "virtio-blk failed to set host notifier (%d)\n", r
);
160 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
164 * The transaction expects the ioeventfds to be open when it
165 * commits. Do it now, before the cleanup loop.
167 memory_region_transaction_commit();
170 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), j
);
172 goto fail_host_notifiers
;
176 memory_region_transaction_commit();
178 trace_virtio_blk_data_plane_start(s
);
180 r
= blk_set_aio_context(s
->conf
->conf
.blk
, s
->ctx
, &local_err
);
182 error_report_err(local_err
);
183 goto fail_aio_context
;
186 /* Kick right away to begin processing requests already in vring */
187 for (i
= 0; i
< nvqs
; i
++) {
188 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
190 event_notifier_set(virtio_queue_get_host_notifier(vq
));
194 * These fields must be visible to the IOThread when it processes the
195 * virtqueue, otherwise it will think dataplane has not started yet.
197 * Make sure ->dataplane_started is false when blk_set_aio_context() is
198 * called above so that draining does not cause the host notifier to be
199 * detached/attached prematurely.
202 vblk
->dataplane_started
= true;
203 smp_wmb(); /* paired with aio_notify_accept() on the read side */
205 /* Get this show started by hooking up our callbacks */
206 if (!blk_in_drain(s
->conf
->conf
.blk
)) {
207 for (i
= 0; i
< nvqs
; i
++) {
208 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
210 virtio_queue_aio_attach_host_notifier(vq
, s
->ctx
);
216 memory_region_transaction_begin();
218 for (i
= 0; i
< nvqs
; i
++) {
219 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
222 memory_region_transaction_commit();
224 for (i
= 0; i
< nvqs
; i
++) {
225 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), i
);
228 k
->set_guest_notifiers(qbus
->parent
, nvqs
, false);
229 fail_guest_notifiers
:
230 vblk
->dataplane_disabled
= true;
235 /* Stop notifications for new requests from guest.
237 * Context: BH in IOThread
239 static void virtio_blk_data_plane_stop_bh(void *opaque
)
241 VirtIOBlockDataPlane
*s
= opaque
;
244 for (i
= 0; i
< s
->conf
->num_queues
; i
++) {
245 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
246 EventNotifier
*host_notifier
= virtio_queue_get_host_notifier(vq
);
248 virtio_queue_aio_detach_host_notifier(vq
, s
->ctx
);
251 * Test and clear notifier after disabling event, in case poll callback
252 * didn't have time to run.
254 virtio_queue_host_notifier_read(host_notifier
);
258 /* Context: QEMU global mutex held */
259 void virtio_blk_data_plane_stop(VirtIODevice
*vdev
)
261 VirtIOBlock
*vblk
= VIRTIO_BLK(vdev
);
262 VirtIOBlockDataPlane
*s
= vblk
->dataplane
;
263 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vblk
));
264 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
266 unsigned nvqs
= s
->conf
->num_queues
;
268 if (!vblk
->dataplane_started
|| s
->stopping
) {
272 /* Better luck next time. */
273 if (vblk
->dataplane_disabled
) {
274 vblk
->dataplane_disabled
= false;
275 vblk
->dataplane_started
= false;
279 trace_virtio_blk_data_plane_stop(s
);
281 if (!blk_in_drain(s
->conf
->conf
.blk
)) {
282 aio_wait_bh_oneshot(s
->ctx
, virtio_blk_data_plane_stop_bh
, s
);
286 * Batch all the host notifiers in a single transaction to avoid
287 * quadratic time complexity in address_space_update_ioeventfds().
289 memory_region_transaction_begin();
291 for (i
= 0; i
< nvqs
; i
++) {
292 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
296 * The transaction expects the ioeventfds to be open when it
297 * commits. Do it now, before the cleanup loop.
299 memory_region_transaction_commit();
301 for (i
= 0; i
< nvqs
; i
++) {
302 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), i
);
306 * Set ->dataplane_started to false before draining so that host notifiers
307 * are not detached/attached anymore.
309 vblk
->dataplane_started
= false;
311 /* Wait for virtio_blk_dma_restart_bh() and in flight I/O to complete */
312 blk_drain(s
->conf
->conf
.blk
);
315 * Try to switch bs back to the QEMU main loop. If other users keep the
316 * BlockBackend in the iothread, that's ok
318 blk_set_aio_context(s
->conf
->conf
.blk
, qemu_get_aio_context(), NULL
);
320 /* Clean up guest notifier (irq) */
321 k
->set_guest_notifiers(qbus
->parent
, nvqs
, false);