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
);
92 if (!k
->set_guest_notifiers
|| !k
->ioeventfd_assign
) {
94 "device is incompatible with iothread "
95 "(transport does not support notifiers)");
98 if (!virtio_device_ioeventfd_enabled(vdev
)) {
99 error_setg(errp
, "ioeventfd is required for iothread");
103 /* If dataplane is (re-)enabled while the guest is running there could
104 * be 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 virtio-blk dataplane: ");
111 /* Don't try if transport does not support notifiers. */
112 if (!virtio_device_ioeventfd_enabled(vdev
)) {
116 s
= g_new0(VirtIOBlockDataPlane
, 1);
120 if (conf
->iothread
) {
121 s
->iothread
= conf
->iothread
;
122 object_ref(OBJECT(s
->iothread
));
123 s
->ctx
= iothread_get_aio_context(s
->iothread
);
125 s
->ctx
= qemu_get_aio_context();
127 s
->bh
= aio_bh_new(s
->ctx
, notify_guest_bh
, s
);
128 s
->batch_notify_vqs
= bitmap_new(conf
->num_queues
);
133 /* Context: QEMU global mutex held */
134 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane
*s
)
142 vblk
= VIRTIO_BLK(s
->vdev
);
143 assert(!vblk
->dataplane_started
);
144 g_free(s
->batch_notify_vqs
);
145 qemu_bh_delete(s
->bh
);
147 object_unref(OBJECT(s
->iothread
));
152 static void virtio_blk_data_plane_handle_output(VirtIODevice
*vdev
,
155 VirtIOBlock
*s
= (VirtIOBlock
*)vdev
;
157 assert(s
->dataplane
);
158 assert(s
->dataplane_started
);
160 virtio_blk_handle_vq(s
, vq
);
163 /* Context: QEMU global mutex held */
164 int virtio_blk_data_plane_start(VirtIODevice
*vdev
)
166 VirtIOBlock
*vblk
= VIRTIO_BLK(vdev
);
167 VirtIOBlockDataPlane
*s
= vblk
->dataplane
;
168 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vblk
)));
169 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
171 unsigned nvqs
= s
->conf
->num_queues
;
174 if (vblk
->dataplane_started
|| s
->starting
) {
180 /* Set up guest notifier (irq) */
181 r
= k
->set_guest_notifiers(qbus
->parent
, nvqs
, true);
183 fprintf(stderr
, "virtio-blk failed to set guest notifier (%d), "
184 "ensure -enable-kvm is set\n", r
);
185 goto fail_guest_notifiers
;
188 /* Set up virtqueue notify */
189 for (i
= 0; i
< nvqs
; i
++) {
190 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, true);
192 fprintf(stderr
, "virtio-blk failed to set host notifier (%d)\n", r
);
194 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
196 goto fail_guest_notifiers
;
201 vblk
->dataplane_started
= true;
202 trace_virtio_blk_data_plane_start(s
);
204 blk_set_aio_context(s
->conf
->conf
.blk
, s
->ctx
);
206 /* Kick right away to begin processing requests already in vring */
207 for (i
= 0; i
< nvqs
; i
++) {
208 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
210 event_notifier_set(virtio_queue_get_host_notifier(vq
));
213 /* Get this show started by hooking up our callbacks */
214 aio_context_acquire(s
->ctx
);
215 for (i
= 0; i
< nvqs
; i
++) {
216 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
218 virtio_queue_aio_set_host_notifier_handler(vq
, s
->ctx
,
219 virtio_blk_data_plane_handle_output
);
221 aio_context_release(s
->ctx
);
224 fail_guest_notifiers
:
225 vblk
->dataplane_disabled
= true;
227 vblk
->dataplane_started
= true;
231 /* Context: QEMU global mutex held */
232 void virtio_blk_data_plane_stop(VirtIODevice
*vdev
)
234 VirtIOBlock
*vblk
= VIRTIO_BLK(vdev
);
235 VirtIOBlockDataPlane
*s
= vblk
->dataplane
;
236 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vblk
));
237 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
239 unsigned nvqs
= s
->conf
->num_queues
;
241 if (!vblk
->dataplane_started
|| s
->stopping
) {
245 /* Better luck next time. */
246 if (vblk
->dataplane_disabled
) {
247 vblk
->dataplane_disabled
= false;
248 vblk
->dataplane_started
= false;
252 trace_virtio_blk_data_plane_stop(s
);
254 aio_context_acquire(s
->ctx
);
256 /* Stop notifications for new requests from guest */
257 for (i
= 0; i
< nvqs
; i
++) {
258 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
260 virtio_queue_aio_set_host_notifier_handler(vq
, s
->ctx
, NULL
);
263 /* Drain and switch bs back to the QEMU main loop */
264 blk_set_aio_context(s
->conf
->conf
.blk
, qemu_get_aio_context());
266 aio_context_release(s
->ctx
);
268 for (i
= 0; i
< nvqs
; i
++) {
269 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
272 /* Clean up guest notifier (irq) */
273 k
->set_guest_notifiers(qbus
->parent
, nvqs
, false);
275 vblk
->dataplane_started
= false;