virtio-blk: always use dataplane path if ioeventfd is active
[qemu/kevin.git] / hw / block / dataplane / virtio-blk.c
blob90ef557c8c92795c9822c4b7cd8f2dba697deecf
1 /*
2 * Dedicated thread for virtio-blk I/O processing
4 * Copyright 2012 IBM, Corp.
5 * Copyright 2012 Red Hat, Inc. and/or its affiliates
7 * Authors:
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"
17 #include "trace.h"
18 #include "qemu/iov.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 {
30 bool starting;
31 bool stopping;
33 VirtIOBlkConf *conf;
34 VirtIODevice *vdev;
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
41 * use it).
43 IOThread *iothread;
44 AioContext *ctx;
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)];
59 unsigned j;
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];
67 while (bits != 0) {
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,
83 Error **errp)
85 VirtIOBlockDataPlane *s;
86 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
87 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
89 *dataplane = NULL;
91 if (conf->iothread) {
92 if (!k->set_guest_notifiers || !k->ioeventfd_assign) {
93 error_setg(errp,
94 "device is incompatible with iothread "
95 "(transport does not support notifiers)");
96 return;
98 if (!virtio_device_ioeventfd_enabled(vdev)) {
99 error_setg(errp, "ioeventfd is required for iothread");
100 return;
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: ");
108 return;
111 /* Don't try if transport does not support notifiers. */
112 if (!virtio_device_ioeventfd_enabled(vdev)) {
113 return;
116 s = g_new0(VirtIOBlockDataPlane, 1);
117 s->vdev = vdev;
118 s->conf = conf;
120 if (conf->iothread) {
121 s->iothread = conf->iothread;
122 object_ref(OBJECT(s->iothread));
123 s->ctx = iothread_get_aio_context(s->iothread);
124 } else {
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);
130 *dataplane = s;
133 /* Context: QEMU global mutex held */
134 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
136 VirtIOBlock *vblk;
138 if (!s) {
139 return;
142 vblk = VIRTIO_BLK(s->vdev);
143 assert(!vblk->dataplane_started);
144 g_free(s->batch_notify_vqs);
145 qemu_bh_delete(s->bh);
146 if (s->iothread) {
147 object_unref(OBJECT(s->iothread));
149 g_free(s);
152 static void virtio_blk_data_plane_handle_output(VirtIODevice *vdev,
153 VirtQueue *vq)
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);
170 unsigned i;
171 unsigned nvqs = s->conf->num_queues;
172 int r;
174 if (vblk->dataplane_started || s->starting) {
175 return 0;
178 s->starting = true;
180 /* Set up guest notifier (irq) */
181 r = k->set_guest_notifiers(qbus->parent, nvqs, true);
182 if (r != 0) {
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);
191 if (r != 0) {
192 fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
193 while (i--) {
194 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
196 goto fail_guest_notifiers;
200 s->starting = false;
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);
222 return 0;
224 fail_guest_notifiers:
225 vblk->dataplane_disabled = true;
226 s->starting = false;
227 vblk->dataplane_started = true;
228 return -ENOSYS;
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);
238 unsigned i;
239 unsigned nvqs = s->conf->num_queues;
241 if (!vblk->dataplane_started || s->stopping) {
242 return;
245 /* Better luck next time. */
246 if (vblk->dataplane_disabled) {
247 vblk->dataplane_disabled = false;
248 vblk->dataplane_started = false;
249 return;
251 s->stopping = true;
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;
276 s->stopping = false;