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-access.h"
23 #include "hw/virtio/virtio-blk.h"
24 #include "virtio-blk.h"
25 #include "block/aio.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "qom/object_interfaces.h"
29 struct VirtIOBlockDataPlane
{
35 QEMUBH
*bh
; /* bh for guest notification */
36 unsigned long *batch_notify_vqs
;
37 bool batch_notifications
;
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
48 /* Raise an interrupt to signal guest, if necessary */
49 void virtio_blk_data_plane_notify(VirtIOBlockDataPlane
*s
, VirtQueue
*vq
)
51 if (s
->batch_notifications
) {
52 set_bit(virtio_get_queue_index(vq
), s
->batch_notify_vqs
);
53 qemu_bh_schedule(s
->bh
);
55 virtio_notify_irqfd(s
->vdev
, vq
);
59 static void notify_guest_bh(void *opaque
)
61 VirtIOBlockDataPlane
*s
= opaque
;
62 unsigned nvqs
= s
->conf
->num_queues
;
63 unsigned long bitmap
[BITS_TO_LONGS(nvqs
)];
66 memcpy(bitmap
, s
->batch_notify_vqs
, sizeof(bitmap
));
67 memset(s
->batch_notify_vqs
, 0, sizeof(bitmap
));
69 for (j
= 0; j
< nvqs
; j
+= BITS_PER_LONG
) {
70 unsigned long bits
= bitmap
[j
/ BITS_PER_LONG
];
73 unsigned i
= j
+ ctzl(bits
);
74 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
76 virtio_notify_irqfd(s
->vdev
, vq
);
78 bits
&= bits
- 1; /* clear right-most bit */
83 /* Context: QEMU global mutex held */
84 bool virtio_blk_data_plane_create(VirtIODevice
*vdev
, VirtIOBlkConf
*conf
,
85 VirtIOBlockDataPlane
**dataplane
,
88 VirtIOBlockDataPlane
*s
;
89 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
90 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
95 if (!k
->set_guest_notifiers
|| !k
->ioeventfd_assign
) {
97 "device is incompatible with iothread "
98 "(transport does not support notifiers)");
101 if (!virtio_device_ioeventfd_enabled(vdev
)) {
102 error_setg(errp
, "ioeventfd is required for iothread");
106 /* If dataplane is (re-)enabled while the guest is running there could
107 * be block jobs that can conflict.
109 if (blk_op_is_blocked(conf
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
110 error_prepend(errp
, "cannot start virtio-blk dataplane: ");
114 /* Don't try if transport does not support notifiers. */
115 if (!virtio_device_ioeventfd_enabled(vdev
)) {
119 s
= g_new0(VirtIOBlockDataPlane
, 1);
123 if (conf
->iothread
) {
124 s
->iothread
= conf
->iothread
;
125 object_ref(OBJECT(s
->iothread
));
126 s
->ctx
= iothread_get_aio_context(s
->iothread
);
128 s
->ctx
= qemu_get_aio_context();
130 s
->bh
= aio_bh_new(s
->ctx
, notify_guest_bh
, s
);
131 s
->batch_notify_vqs
= bitmap_new(conf
->num_queues
);
138 /* Context: QEMU global mutex held */
139 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane
*s
)
147 vblk
= VIRTIO_BLK(s
->vdev
);
148 assert(!vblk
->dataplane_started
);
149 g_free(s
->batch_notify_vqs
);
150 qemu_bh_delete(s
->bh
);
152 object_unref(OBJECT(s
->iothread
));
157 static bool virtio_blk_data_plane_handle_output(VirtIODevice
*vdev
,
160 VirtIOBlock
*s
= (VirtIOBlock
*)vdev
;
162 assert(s
->dataplane
);
163 assert(s
->dataplane_started
);
165 return virtio_blk_handle_vq(s
, vq
);
168 /* Context: QEMU global mutex held */
169 int virtio_blk_data_plane_start(VirtIODevice
*vdev
)
171 VirtIOBlock
*vblk
= VIRTIO_BLK(vdev
);
172 VirtIOBlockDataPlane
*s
= vblk
->dataplane
;
173 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vblk
)));
174 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
175 AioContext
*old_context
;
177 unsigned nvqs
= s
->conf
->num_queues
;
178 Error
*local_err
= NULL
;
181 if (vblk
->dataplane_started
|| s
->starting
) {
187 if (!virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
188 s
->batch_notifications
= true;
190 s
->batch_notifications
= false;
193 /* Set up guest notifier (irq) */
194 r
= k
->set_guest_notifiers(qbus
->parent
, nvqs
, true);
196 error_report("virtio-blk failed to set guest notifier (%d), "
197 "ensure -accel kvm is set.", r
);
198 goto fail_guest_notifiers
;
201 /* Set up virtqueue notify */
202 for (i
= 0; i
< nvqs
; i
++) {
203 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, true);
205 fprintf(stderr
, "virtio-blk failed to set host notifier (%d)\n", r
);
207 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
208 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), i
);
210 goto fail_host_notifiers
;
215 vblk
->dataplane_started
= true;
216 trace_virtio_blk_data_plane_start(s
);
218 old_context
= blk_get_aio_context(s
->conf
->conf
.blk
);
219 aio_context_acquire(old_context
);
220 r
= blk_set_aio_context(s
->conf
->conf
.blk
, s
->ctx
, &local_err
);
221 aio_context_release(old_context
);
223 error_report_err(local_err
);
224 goto fail_aio_context
;
227 /* Process queued requests before the ones in vring */
228 virtio_blk_process_queued_requests(vblk
, false);
230 /* Kick right away to begin processing requests already in vring */
231 for (i
= 0; i
< nvqs
; i
++) {
232 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
234 event_notifier_set(virtio_queue_get_host_notifier(vq
));
237 /* Get this show started by hooking up our callbacks */
238 aio_context_acquire(s
->ctx
);
239 for (i
= 0; i
< nvqs
; i
++) {
240 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
242 virtio_queue_aio_set_host_notifier_handler(vq
, s
->ctx
,
243 virtio_blk_data_plane_handle_output
);
245 aio_context_release(s
->ctx
);
249 for (i
= 0; i
< nvqs
; i
++) {
250 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
251 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), i
);
254 k
->set_guest_notifiers(qbus
->parent
, nvqs
, false);
255 fail_guest_notifiers
:
257 * If we failed to set up the guest notifiers queued requests will be
258 * processed on the main context.
260 virtio_blk_process_queued_requests(vblk
, false);
261 vblk
->dataplane_disabled
= true;
263 vblk
->dataplane_started
= true;
267 /* Stop notifications for new requests from guest.
269 * Context: BH in IOThread
271 static void virtio_blk_data_plane_stop_bh(void *opaque
)
273 VirtIOBlockDataPlane
*s
= opaque
;
276 for (i
= 0; i
< s
->conf
->num_queues
; i
++) {
277 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
279 virtio_queue_aio_set_host_notifier_handler(vq
, s
->ctx
, NULL
);
283 /* Context: QEMU global mutex held */
284 void virtio_blk_data_plane_stop(VirtIODevice
*vdev
)
286 VirtIOBlock
*vblk
= VIRTIO_BLK(vdev
);
287 VirtIOBlockDataPlane
*s
= vblk
->dataplane
;
288 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vblk
));
289 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
291 unsigned nvqs
= s
->conf
->num_queues
;
293 if (!vblk
->dataplane_started
|| s
->stopping
) {
297 /* Better luck next time. */
298 if (vblk
->dataplane_disabled
) {
299 vblk
->dataplane_disabled
= false;
300 vblk
->dataplane_started
= false;
304 trace_virtio_blk_data_plane_stop(s
);
306 aio_context_acquire(s
->ctx
);
307 aio_wait_bh_oneshot(s
->ctx
, virtio_blk_data_plane_stop_bh
, s
);
309 /* Drain and try to switch bs back to the QEMU main loop. If other users
310 * keep the BlockBackend in the iothread, that's ok */
311 blk_set_aio_context(s
->conf
->conf
.blk
, qemu_get_aio_context(), NULL
);
313 aio_context_release(s
->ctx
);
315 for (i
= 0; i
< nvqs
; i
++) {
316 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
317 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), i
);
320 qemu_bh_cancel(s
->bh
);
321 notify_guest_bh(s
); /* final chance to notify guest */
323 /* Clean up guest notifier (irq) */
324 k
->set_guest_notifiers(qbus
->parent
, nvqs
, false);
326 vblk
->dataplane_started
= false;