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.
17 #include "qemu/thread.h"
18 #include "qemu/error-report.h"
19 #include "hw/virtio/virtio-access.h"
20 #include "hw/virtio/dataplane/vring.h"
21 #include "hw/virtio/dataplane/vring-accessors.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
{
38 Vring vring
; /* virtqueue vring */
39 EventNotifier
*guest_notifier
; /* irq */
40 QEMUBH
*bh
; /* bh for guest notification */
42 /* Note that these EventNotifiers are assigned by value. This is
43 * fine as long as you do not call event_notifier_cleanup on them
44 * (because you don't own the file descriptor or handle; you just
48 IOThread internal_iothread_obj
;
50 EventNotifier host_notifier
; /* doorbell */
52 /* Operation blocker on BDS */
54 void (*saved_complete_request
)(struct VirtIOBlockReq
*req
,
55 unsigned char status
);
58 /* Raise an interrupt to signal guest, if necessary */
59 static void notify_guest(VirtIOBlockDataPlane
*s
)
61 if (!vring_should_notify(s
->vdev
, &s
->vring
)) {
65 event_notifier_set(s
->guest_notifier
);
68 static void notify_guest_bh(void *opaque
)
70 VirtIOBlockDataPlane
*s
= opaque
;
75 static void complete_request_vring(VirtIOBlockReq
*req
, unsigned char status
)
77 VirtIOBlockDataPlane
*s
= req
->dev
->dataplane
;
78 stb_p(&req
->in
->status
, status
);
80 vring_push(s
->vdev
, &req
->dev
->dataplane
->vring
, &req
->elem
,
81 req
->qiov
.size
+ sizeof(*req
->in
));
83 /* Suppress notification to guest by BH and its scheduled
84 * flag because requests are completed as a batch after io
85 * plug & unplug is introduced, and the BH can still be
86 * executed in dataplane aio context even after it is
87 * stopped, so needn't worry about notification loss with BH.
89 qemu_bh_schedule(s
->bh
);
92 static void handle_notify(EventNotifier
*e
)
94 VirtIOBlockDataPlane
*s
= container_of(e
, VirtIOBlockDataPlane
,
96 VirtIOBlock
*vblk
= VIRTIO_BLK(s
->vdev
);
98 event_notifier_test_and_clear(&s
->host_notifier
);
99 blk_io_plug(s
->conf
->conf
.blk
);
101 MultiReqBuffer mrb
= {};
104 /* Disable guest->host notifies to avoid unnecessary vmexits */
105 vring_disable_notification(s
->vdev
, &s
->vring
);
108 VirtIOBlockReq
*req
= virtio_blk_alloc_request(vblk
);
110 ret
= vring_pop(s
->vdev
, &s
->vring
, &req
->elem
);
112 virtio_blk_free_request(req
);
113 break; /* no more requests */
116 trace_virtio_blk_data_plane_process_request(s
, req
->elem
.out_num
,
120 virtio_blk_handle_request(req
, &mrb
);
124 virtio_blk_submit_multireq(s
->conf
->conf
.blk
, &mrb
);
127 if (likely(ret
== -EAGAIN
)) { /* vring emptied */
128 /* Re-enable guest->host notifies and stop processing the vring.
129 * But if the guest has snuck in more descriptors, keep processing.
131 if (vring_enable_notification(s
->vdev
, &s
->vring
)) {
134 } else { /* fatal error */
138 blk_io_unplug(s
->conf
->conf
.blk
);
141 /* Context: QEMU global mutex held */
142 void virtio_blk_data_plane_create(VirtIODevice
*vdev
, VirtIOBlkConf
*conf
,
143 VirtIOBlockDataPlane
**dataplane
,
146 VirtIOBlockDataPlane
*s
;
147 Error
*local_err
= NULL
;
148 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
149 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
153 if (!conf
->data_plane
&& !conf
->iothread
) {
157 /* Don't try if transport does not support notifiers. */
158 if (!k
->set_guest_notifiers
|| !k
->set_host_notifier
) {
160 "device is incompatible with x-data-plane "
161 "(transport does not support notifiers)");
165 /* If dataplane is (re-)enabled while the guest is running there could be
166 * block jobs that can conflict.
168 if (blk_op_is_blocked(conf
->conf
.blk
, BLOCK_OP_TYPE_DATAPLANE
,
170 error_setg(errp
, "cannot start dataplane thread: %s",
171 error_get_pretty(local_err
));
172 error_free(local_err
);
176 s
= g_new0(VirtIOBlockDataPlane
, 1);
180 if (conf
->iothread
) {
181 s
->iothread
= conf
->iothread
;
182 object_ref(OBJECT(s
->iothread
));
184 /* Create per-device IOThread if none specified. This is for
185 * x-data-plane option compatibility. If x-data-plane is removed we
188 object_initialize(&s
->internal_iothread_obj
,
189 sizeof(s
->internal_iothread_obj
),
191 user_creatable_complete(OBJECT(&s
->internal_iothread_obj
), &error_abort
);
192 s
->iothread
= &s
->internal_iothread_obj
;
194 s
->ctx
= iothread_get_aio_context(s
->iothread
);
195 s
->bh
= aio_bh_new(s
->ctx
, notify_guest_bh
, s
);
197 error_setg(&s
->blocker
, "block device is in use by data plane");
198 blk_op_block_all(conf
->conf
.blk
, s
->blocker
);
199 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_RESIZE
, s
->blocker
);
200 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_DRIVE_DEL
, s
->blocker
);
201 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_BACKUP_SOURCE
, s
->blocker
);
202 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_CHANGE
, s
->blocker
);
203 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_COMMIT_SOURCE
, s
->blocker
);
204 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_COMMIT_TARGET
, s
->blocker
);
205 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_EJECT
, s
->blocker
);
206 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, s
->blocker
);
207 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, s
->blocker
);
208 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
,
210 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_MIRROR
, s
->blocker
);
211 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_STREAM
, s
->blocker
);
212 blk_op_unblock(conf
->conf
.blk
, BLOCK_OP_TYPE_REPLACE
, s
->blocker
);
217 /* Context: QEMU global mutex held */
218 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane
*s
)
224 virtio_blk_data_plane_stop(s
);
225 blk_op_unblock_all(s
->conf
->conf
.blk
, s
->blocker
);
226 error_free(s
->blocker
);
227 object_unref(OBJECT(s
->iothread
));
228 qemu_bh_delete(s
->bh
);
232 /* Context: QEMU global mutex held */
233 void virtio_blk_data_plane_start(VirtIOBlockDataPlane
*s
)
235 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
->vdev
)));
236 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
237 VirtIOBlock
*vblk
= VIRTIO_BLK(s
->vdev
);
241 if (s
->started
|| s
->disabled
) {
251 vq
= virtio_get_queue(s
->vdev
, 0);
252 if (!vring_setup(&s
->vring
, s
->vdev
, 0)) {
256 /* Set up guest notifier (irq) */
257 r
= k
->set_guest_notifiers(qbus
->parent
, 1, true);
259 fprintf(stderr
, "virtio-blk failed to set guest notifier (%d), "
260 "ensure -enable-kvm is set\n", r
);
261 goto fail_guest_notifiers
;
263 s
->guest_notifier
= virtio_queue_get_guest_notifier(vq
);
265 /* Set up virtqueue notify */
266 r
= k
->set_host_notifier(qbus
->parent
, 0, true);
268 fprintf(stderr
, "virtio-blk failed to set host notifier (%d)\n", r
);
269 goto fail_host_notifier
;
271 s
->host_notifier
= *virtio_queue_get_host_notifier(vq
);
273 s
->saved_complete_request
= vblk
->complete_request
;
274 vblk
->complete_request
= complete_request_vring
;
278 trace_virtio_blk_data_plane_start(s
);
280 blk_set_aio_context(s
->conf
->conf
.blk
, s
->ctx
);
282 /* Kick right away to begin processing requests already in vring */
283 event_notifier_set(virtio_queue_get_host_notifier(vq
));
285 /* Get this show started by hooking up our callbacks */
286 aio_context_acquire(s
->ctx
);
287 aio_set_event_notifier(s
->ctx
, &s
->host_notifier
, handle_notify
);
288 aio_context_release(s
->ctx
);
292 k
->set_guest_notifiers(qbus
->parent
, 1, false);
293 fail_guest_notifiers
:
294 vring_teardown(&s
->vring
, s
->vdev
, 0);
300 /* Context: QEMU global mutex held */
301 void virtio_blk_data_plane_stop(VirtIOBlockDataPlane
*s
)
303 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
->vdev
)));
304 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
305 VirtIOBlock
*vblk
= VIRTIO_BLK(s
->vdev
);
308 /* Better luck next time. */
313 if (!s
->started
|| s
->stopping
) {
317 vblk
->complete_request
= s
->saved_complete_request
;
318 trace_virtio_blk_data_plane_stop(s
);
320 aio_context_acquire(s
->ctx
);
322 /* Stop notifications for new requests from guest */
323 aio_set_event_notifier(s
->ctx
, &s
->host_notifier
, NULL
);
325 /* Drain and switch bs back to the QEMU main loop */
326 blk_set_aio_context(s
->conf
->conf
.blk
, qemu_get_aio_context());
328 aio_context_release(s
->ctx
);
330 /* Sync vring state back to virtqueue so that non-dataplane request
331 * processing can continue when we disable the host notifier below.
333 vring_teardown(&s
->vring
, s
->vdev
, 0);
335 k
->set_host_notifier(qbus
->parent
, 0, false);
337 /* Clean up guest notifier (irq) */
338 k
->set_guest_notifiers(qbus
->parent
, 1, false);