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/thread.h"
20 #include "qemu/error-report.h"
21 #include "hw/virtio/virtio-access.h"
22 #include "sysemu/block-backend.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
;
38 /* Note that these EventNotifiers are assigned by value. This is
39 * fine as long as you do not call event_notifier_cleanup on them
40 * (because you don't own the file descriptor or handle; you just
47 /* Raise an interrupt to signal guest, if necessary */
48 void virtio_blk_data_plane_notify(VirtIOBlockDataPlane
*s
, VirtQueue
*vq
)
50 set_bit(virtio_get_queue_index(vq
), s
->batch_notify_vqs
);
51 qemu_bh_schedule(s
->bh
);
54 static void notify_guest_bh(void *opaque
)
56 VirtIOBlockDataPlane
*s
= opaque
;
57 unsigned nvqs
= s
->conf
->num_queues
;
58 unsigned long bitmap
[BITS_TO_LONGS(nvqs
)];
61 memcpy(bitmap
, s
->batch_notify_vqs
, sizeof(bitmap
));
62 memset(s
->batch_notify_vqs
, 0, sizeof(bitmap
));
64 for (j
= 0; j
< nvqs
; j
+= BITS_PER_LONG
) {
65 unsigned long bits
= bitmap
[j
];
68 unsigned i
= j
+ ctzl(bits
);
69 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
71 if (virtio_should_notify(s
->vdev
, vq
)) {
72 event_notifier_set(virtio_queue_get_guest_notifier(vq
));
75 bits
&= bits
- 1; /* clear right-most bit */
80 /* Context: QEMU global mutex held */
81 void virtio_blk_data_plane_create(VirtIODevice
*vdev
, VirtIOBlkConf
*conf
,
82 VirtIOBlockDataPlane
**dataplane
,
85 VirtIOBlockDataPlane
*s
;
86 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
87 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
91 if (!conf
->iothread
) {
95 /* Don't try if transport does not support notifiers. */
96 if (!k
->set_guest_notifiers
|| !k
->ioeventfd_started
) {
98 "device is incompatible with dataplane "
99 "(transport does not support notifiers)");
103 /* If dataplane is (re-)enabled while the guest is running there could be
104 * block jobs that can conflict.
106 if (blk_op_is_blocked(conf
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
107 error_prepend(errp
, "cannot start dataplane thread: ");
111 s
= g_new0(VirtIOBlockDataPlane
, 1);
115 s
->iothread
= conf
->iothread
;
116 object_ref(OBJECT(s
->iothread
));
117 s
->ctx
= iothread_get_aio_context(s
->iothread
);
118 s
->bh
= aio_bh_new(s
->ctx
, notify_guest_bh
, s
);
119 s
->batch_notify_vqs
= bitmap_new(conf
->num_queues
);
124 /* Context: QEMU global mutex held */
125 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane
*s
)
131 virtio_blk_data_plane_stop(s
);
132 g_free(s
->batch_notify_vqs
);
133 qemu_bh_delete(s
->bh
);
134 object_unref(OBJECT(s
->iothread
));
138 static void virtio_blk_data_plane_handle_output(VirtIODevice
*vdev
,
141 VirtIOBlock
*s
= (VirtIOBlock
*)vdev
;
143 assert(s
->dataplane
);
144 assert(s
->dataplane_started
);
146 virtio_blk_handle_vq(s
, vq
);
149 /* Context: QEMU global mutex held */
150 void virtio_blk_data_plane_start(VirtIOBlockDataPlane
*s
)
152 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
->vdev
)));
153 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
154 VirtIOBlock
*vblk
= VIRTIO_BLK(s
->vdev
);
156 unsigned nvqs
= s
->conf
->num_queues
;
159 if (vblk
->dataplane_started
|| s
->starting
) {
165 /* Set up guest notifier (irq) */
166 r
= k
->set_guest_notifiers(qbus
->parent
, nvqs
, true);
168 fprintf(stderr
, "virtio-blk failed to set guest notifier (%d), "
169 "ensure -enable-kvm is set\n", r
);
170 goto fail_guest_notifiers
;
173 /* Set up virtqueue notify */
174 for (i
= 0; i
< nvqs
; i
++) {
175 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, true);
177 fprintf(stderr
, "virtio-blk failed to set host notifier (%d)\n", r
);
179 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
181 goto fail_guest_notifiers
;
186 vblk
->dataplane_started
= true;
187 trace_virtio_blk_data_plane_start(s
);
189 blk_set_aio_context(s
->conf
->conf
.blk
, s
->ctx
);
191 /* Kick right away to begin processing requests already in vring */
192 for (i
= 0; i
< nvqs
; i
++) {
193 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
195 event_notifier_set(virtio_queue_get_host_notifier(vq
));
198 /* Get this show started by hooking up our callbacks */
199 aio_context_acquire(s
->ctx
);
200 for (i
= 0; i
< nvqs
; i
++) {
201 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
203 virtio_queue_aio_set_host_notifier_handler(vq
, s
->ctx
,
204 virtio_blk_data_plane_handle_output
);
206 aio_context_release(s
->ctx
);
209 fail_guest_notifiers
:
210 vblk
->dataplane_disabled
= true;
212 vblk
->dataplane_started
= true;
215 /* Context: QEMU global mutex held */
216 void virtio_blk_data_plane_stop(VirtIOBlockDataPlane
*s
)
218 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
->vdev
)));
219 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
220 VirtIOBlock
*vblk
= VIRTIO_BLK(s
->vdev
);
222 unsigned nvqs
= s
->conf
->num_queues
;
224 if (!vblk
->dataplane_started
|| s
->stopping
) {
228 /* Better luck next time. */
229 if (vblk
->dataplane_disabled
) {
230 vblk
->dataplane_disabled
= false;
231 vblk
->dataplane_started
= false;
235 trace_virtio_blk_data_plane_stop(s
);
237 aio_context_acquire(s
->ctx
);
239 /* Stop notifications for new requests from guest */
240 for (i
= 0; i
< nvqs
; i
++) {
241 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
243 virtio_queue_aio_set_host_notifier_handler(vq
, s
->ctx
, NULL
);
246 /* Drain and switch bs back to the QEMU main loop */
247 blk_set_aio_context(s
->conf
->conf
.blk
, qemu_get_aio_context());
249 aio_context_release(s
->ctx
);
251 for (i
= 0; i
< nvqs
; i
++) {
252 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
255 /* Clean up guest notifier (irq) */
256 k
->set_guest_notifiers(qbus
->parent
, nvqs
, false);
258 vblk
->dataplane_started
= false;