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 EventNotifier
*guest_notifier
; /* irq */
38 QEMUBH
*bh
; /* bh for guest notification */
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
)
52 qemu_bh_schedule(s
->bh
);
55 static void notify_guest_bh(void *opaque
)
57 VirtIOBlockDataPlane
*s
= opaque
;
59 if (!virtio_should_notify(s
->vdev
, s
->vq
)) {
63 event_notifier_set(s
->guest_notifier
);
66 /* Context: QEMU global mutex held */
67 void virtio_blk_data_plane_create(VirtIODevice
*vdev
, VirtIOBlkConf
*conf
,
68 VirtIOBlockDataPlane
**dataplane
,
71 VirtIOBlockDataPlane
*s
;
72 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
73 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
77 if (!conf
->iothread
) {
81 /* Don't try if transport does not support notifiers. */
82 if (!k
->set_guest_notifiers
|| !k
->ioeventfd_started
) {
84 "device is incompatible with dataplane "
85 "(transport does not support notifiers)");
89 /* If dataplane is (re-)enabled while the guest is running there could be
90 * block jobs that can conflict.
92 if (blk_op_is_blocked(conf
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
, errp
)) {
93 error_prepend(errp
, "cannot start dataplane thread: ");
97 s
= g_new0(VirtIOBlockDataPlane
, 1);
101 if (conf
->iothread
) {
102 s
->iothread
= conf
->iothread
;
103 object_ref(OBJECT(s
->iothread
));
105 s
->ctx
= iothread_get_aio_context(s
->iothread
);
106 s
->bh
= aio_bh_new(s
->ctx
, notify_guest_bh
, s
);
111 /* Context: QEMU global mutex held */
112 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane
*s
)
118 virtio_blk_data_plane_stop(s
);
119 qemu_bh_delete(s
->bh
);
120 object_unref(OBJECT(s
->iothread
));
124 static void virtio_blk_data_plane_handle_output(VirtIODevice
*vdev
,
127 VirtIOBlock
*s
= (VirtIOBlock
*)vdev
;
129 assert(s
->dataplane
);
130 assert(s
->dataplane_started
);
132 virtio_blk_handle_vq(s
, vq
);
135 /* Context: QEMU global mutex held */
136 void virtio_blk_data_plane_start(VirtIOBlockDataPlane
*s
)
138 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
->vdev
)));
139 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
140 VirtIOBlock
*vblk
= VIRTIO_BLK(s
->vdev
);
143 if (vblk
->dataplane_started
|| s
->starting
) {
148 s
->vq
= virtio_get_queue(s
->vdev
, 0);
150 /* Set up guest notifier (irq) */
151 r
= k
->set_guest_notifiers(qbus
->parent
, 1, true);
153 fprintf(stderr
, "virtio-blk failed to set guest notifier (%d), "
154 "ensure -enable-kvm is set\n", r
);
155 goto fail_guest_notifiers
;
157 s
->guest_notifier
= virtio_queue_get_guest_notifier(s
->vq
);
159 /* Set up virtqueue notify */
160 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), 0, true);
162 fprintf(stderr
, "virtio-blk failed to set host notifier (%d)\n", r
);
163 goto fail_host_notifier
;
167 vblk
->dataplane_started
= true;
168 trace_virtio_blk_data_plane_start(s
);
170 blk_set_aio_context(s
->conf
->conf
.blk
, s
->ctx
);
172 /* Kick right away to begin processing requests already in vring */
173 event_notifier_set(virtio_queue_get_host_notifier(s
->vq
));
175 /* Get this show started by hooking up our callbacks */
176 aio_context_acquire(s
->ctx
);
177 virtio_queue_aio_set_host_notifier_handler(s
->vq
, s
->ctx
,
178 virtio_blk_data_plane_handle_output
);
179 aio_context_release(s
->ctx
);
183 k
->set_guest_notifiers(qbus
->parent
, 1, false);
184 fail_guest_notifiers
:
185 vblk
->dataplane_disabled
= true;
187 vblk
->dataplane_started
= true;
190 /* Context: QEMU global mutex held */
191 void virtio_blk_data_plane_stop(VirtIOBlockDataPlane
*s
)
193 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
->vdev
)));
194 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
195 VirtIOBlock
*vblk
= VIRTIO_BLK(s
->vdev
);
197 if (!vblk
->dataplane_started
|| s
->stopping
) {
201 /* Better luck next time. */
202 if (vblk
->dataplane_disabled
) {
203 vblk
->dataplane_disabled
= false;
204 vblk
->dataplane_started
= false;
208 trace_virtio_blk_data_plane_stop(s
);
210 aio_context_acquire(s
->ctx
);
212 /* Stop notifications for new requests from guest */
213 virtio_queue_aio_set_host_notifier_handler(s
->vq
, s
->ctx
, NULL
);
215 /* Drain and switch bs back to the QEMU main loop */
216 blk_set_aio_context(s
->conf
->conf
.blk
, qemu_get_aio_context());
218 aio_context_release(s
->ctx
);
220 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), 0, false);
222 /* Clean up guest notifier (irq) */
223 k
->set_guest_notifiers(qbus
->parent
, 1, false);
225 vblk
->dataplane_started
= false;