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
{
36 VirtQueue
*vq
; /* virtqueue vring */
37 QEMUBH
*bh
; /* bh for guest notification */
38 unsigned long *batch_notify_vqs
;
40 /* Note that these EventNotifiers are assigned by value. This is
41 * fine as long as you do not call event_notifier_cleanup on them
42 * (because you don't own the file descriptor or handle; you just
49 /* Raise an interrupt to signal guest, if necessary */
50 void virtio_blk_data_plane_notify(VirtIOBlockDataPlane
*s
, VirtQueue
*vq
)
52 set_bit(virtio_get_queue_index(vq
), s
->batch_notify_vqs
);
53 qemu_bh_schedule(s
->bh
);
56 static void notify_guest_bh(void *opaque
)
58 VirtIOBlockDataPlane
*s
= opaque
;
59 unsigned nvqs
= s
->conf
->num_queues
;
60 unsigned long bitmap
[BITS_TO_LONGS(nvqs
)];
63 memcpy(bitmap
, s
->batch_notify_vqs
, sizeof(bitmap
));
64 memset(s
->batch_notify_vqs
, 0, sizeof(bitmap
));
66 for (j
= 0; j
< nvqs
; j
+= BITS_PER_LONG
) {
67 unsigned long bits
= bitmap
[j
];
70 unsigned i
= j
+ ctzl(bits
);
71 VirtQueue
*vq
= virtio_get_queue(s
->vdev
, i
);
73 if (virtio_should_notify(s
->vdev
, vq
)) {
74 event_notifier_set(virtio_queue_get_guest_notifier(vq
));
77 bits
&= bits
- 1; /* clear right-most bit */
82 /* Context: QEMU global mutex held */
83 void virtio_blk_data_plane_create(VirtIODevice
*vdev
, VirtIOBlkConf
*conf
,
84 VirtIOBlockDataPlane
**dataplane
,
87 VirtIOBlockDataPlane
*s
;
88 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
89 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
93 if (!conf
->iothread
) {
97 /* Don't try if transport does not support notifiers. */
98 if (!k
->set_guest_notifiers
|| !k
->ioeventfd_started
) {
100 "device is incompatible with dataplane "
101 "(transport does not support notifiers)");
105 /* If dataplane is (re-)enabled while the guest is running there could be
106 * block jobs that can conflict.
108 if (blk_op_is_blocked(conf
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
109 error_prepend(errp
, "cannot start dataplane thread: ");
113 s
= g_new0(VirtIOBlockDataPlane
, 1);
117 if (conf
->iothread
) {
118 s
->iothread
= conf
->iothread
;
119 object_ref(OBJECT(s
->iothread
));
121 s
->ctx
= iothread_get_aio_context(s
->iothread
);
122 s
->bh
= aio_bh_new(s
->ctx
, notify_guest_bh
, s
);
123 s
->batch_notify_vqs
= bitmap_new(conf
->num_queues
);
128 /* Context: QEMU global mutex held */
129 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane
*s
)
135 virtio_blk_data_plane_stop(s
);
136 g_free(s
->batch_notify_vqs
);
137 qemu_bh_delete(s
->bh
);
138 object_unref(OBJECT(s
->iothread
));
142 static void virtio_blk_data_plane_handle_output(VirtIODevice
*vdev
,
145 VirtIOBlock
*s
= (VirtIOBlock
*)vdev
;
147 assert(s
->dataplane
);
148 assert(s
->dataplane_started
);
150 virtio_blk_handle_vq(s
, vq
);
153 /* Context: QEMU global mutex held */
154 void virtio_blk_data_plane_start(VirtIOBlockDataPlane
*s
)
156 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
->vdev
)));
157 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
158 VirtIOBlock
*vblk
= VIRTIO_BLK(s
->vdev
);
161 if (vblk
->dataplane_started
|| s
->starting
) {
166 s
->vq
= virtio_get_queue(s
->vdev
, 0);
168 /* Set up guest notifier (irq) */
169 r
= k
->set_guest_notifiers(qbus
->parent
, 1, true);
171 fprintf(stderr
, "virtio-blk failed to set guest notifier (%d), "
172 "ensure -enable-kvm is set\n", r
);
173 goto fail_guest_notifiers
;
176 /* Set up virtqueue notify */
177 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), 0, true);
179 fprintf(stderr
, "virtio-blk failed to set host notifier (%d)\n", r
);
180 goto fail_host_notifier
;
184 vblk
->dataplane_started
= true;
185 trace_virtio_blk_data_plane_start(s
);
187 blk_set_aio_context(s
->conf
->conf
.blk
, s
->ctx
);
189 /* Kick right away to begin processing requests already in vring */
190 event_notifier_set(virtio_queue_get_host_notifier(s
->vq
));
192 /* Get this show started by hooking up our callbacks */
193 aio_context_acquire(s
->ctx
);
194 virtio_queue_aio_set_host_notifier_handler(s
->vq
, s
->ctx
,
195 virtio_blk_data_plane_handle_output
);
196 aio_context_release(s
->ctx
);
200 k
->set_guest_notifiers(qbus
->parent
, 1, false);
201 fail_guest_notifiers
:
202 vblk
->dataplane_disabled
= true;
204 vblk
->dataplane_started
= true;
207 /* Context: QEMU global mutex held */
208 void virtio_blk_data_plane_stop(VirtIOBlockDataPlane
*s
)
210 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
->vdev
)));
211 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
212 VirtIOBlock
*vblk
= VIRTIO_BLK(s
->vdev
);
214 if (!vblk
->dataplane_started
|| s
->stopping
) {
218 /* Better luck next time. */
219 if (vblk
->dataplane_disabled
) {
220 vblk
->dataplane_disabled
= false;
221 vblk
->dataplane_started
= false;
225 trace_virtio_blk_data_plane_stop(s
);
227 aio_context_acquire(s
->ctx
);
229 /* Stop notifications for new requests from guest */
230 virtio_queue_aio_set_host_notifier_handler(s
->vq
, s
->ctx
, NULL
);
232 /* Drain and switch bs back to the QEMU main loop */
233 blk_set_aio_context(s
->conf
->conf
.blk
, qemu_get_aio_context());
235 aio_context_release(s
->ctx
);
237 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), 0, false);
239 /* Clean up guest notifier (irq) */
240 k
->set_guest_notifiers(qbus
->parent
, 1, false);
242 vblk
->dataplane_started
= false;