]> Git Repo - qemu.git/blob - hw/block/dataplane/virtio-blk.c
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[qemu.git] / hw / block / dataplane / virtio-blk.c
1 /*
2  * Dedicated thread for virtio-blk I/O processing
3  *
4  * Copyright 2012 IBM, Corp.
5  * Copyright 2012 Red Hat, Inc. and/or its affiliates
6  *
7  * Authors:
8  *   Stefan Hajnoczi <[email protected]>
9  *
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.
12  *
13  */
14
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"
28
29 struct VirtIOBlockDataPlane {
30     bool starting;
31     bool stopping;
32
33     VirtIOBlkConf *conf;
34     VirtIODevice *vdev;
35     QEMUBH *bh;                     /* bh for guest notification */
36     unsigned long *batch_notify_vqs;
37     bool batch_notifications;
38
39     /* Note that these EventNotifiers are assigned by value.  This is
40      * fine as long as you do not call event_notifier_cleanup on them
41      * (because you don't own the file descriptor or handle; you just
42      * use it).
43      */
44     IOThread *iothread;
45     AioContext *ctx;
46 };
47
48 /* Raise an interrupt to signal guest, if necessary */
49 void virtio_blk_data_plane_notify(VirtIOBlockDataPlane *s, VirtQueue *vq)
50 {
51     if (s->batch_notifications) {
52         set_bit(virtio_get_queue_index(vq), s->batch_notify_vqs);
53         qemu_bh_schedule(s->bh);
54     } else {
55         virtio_notify_irqfd(s->vdev, vq);
56     }
57 }
58
59 static void notify_guest_bh(void *opaque)
60 {
61     VirtIOBlockDataPlane *s = opaque;
62     unsigned nvqs = s->conf->num_queues;
63     unsigned long bitmap[BITS_TO_LONGS(nvqs)];
64     unsigned j;
65
66     memcpy(bitmap, s->batch_notify_vqs, sizeof(bitmap));
67     memset(s->batch_notify_vqs, 0, sizeof(bitmap));
68
69     for (j = 0; j < nvqs; j += BITS_PER_LONG) {
70         unsigned long bits = bitmap[j];
71
72         while (bits != 0) {
73             unsigned i = j + ctzl(bits);
74             VirtQueue *vq = virtio_get_queue(s->vdev, i);
75
76             virtio_notify_irqfd(s->vdev, vq);
77
78             bits &= bits - 1; /* clear right-most bit */
79         }
80     }
81 }
82
83 /* Context: QEMU global mutex held */
84 bool virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf,
85                                   VirtIOBlockDataPlane **dataplane,
86                                   Error **errp)
87 {
88     VirtIOBlockDataPlane *s;
89     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
90     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
91
92     *dataplane = NULL;
93
94     if (conf->iothread) {
95         if (!k->set_guest_notifiers || !k->ioeventfd_assign) {
96             error_setg(errp,
97                        "device is incompatible with iothread "
98                        "(transport does not support notifiers)");
99             return false;
100         }
101         if (!virtio_device_ioeventfd_enabled(vdev)) {
102             error_setg(errp, "ioeventfd is required for iothread");
103             return false;
104         }
105
106         /* If dataplane is (re-)enabled while the guest is running there could
107          * be block jobs that can conflict.
108          */
109         if (blk_op_is_blocked(conf->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
110             error_prepend(errp, "cannot start virtio-blk dataplane: ");
111             return false;
112         }
113     }
114     /* Don't try if transport does not support notifiers. */
115     if (!virtio_device_ioeventfd_enabled(vdev)) {
116         return false;
117     }
118
119     s = g_new0(VirtIOBlockDataPlane, 1);
120     s->vdev = vdev;
121     s->conf = conf;
122
123     if (conf->iothread) {
124         s->iothread = conf->iothread;
125         object_ref(OBJECT(s->iothread));
126         s->ctx = iothread_get_aio_context(s->iothread);
127     } else {
128         s->ctx = qemu_get_aio_context();
129     }
130     s->bh = aio_bh_new(s->ctx, notify_guest_bh, s);
131     s->batch_notify_vqs = bitmap_new(conf->num_queues);
132
133     *dataplane = s;
134
135     return true;
136 }
137
138 /* Context: QEMU global mutex held */
139 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
140 {
141     VirtIOBlock *vblk;
142
143     if (!s) {
144         return;
145     }
146
147     vblk = VIRTIO_BLK(s->vdev);
148     assert(!vblk->dataplane_started);
149     g_free(s->batch_notify_vqs);
150     qemu_bh_delete(s->bh);
151     if (s->iothread) {
152         object_unref(OBJECT(s->iothread));
153     }
154     g_free(s);
155 }
156
157 static bool virtio_blk_data_plane_handle_output(VirtIODevice *vdev,
158                                                 VirtQueue *vq)
159 {
160     VirtIOBlock *s = (VirtIOBlock *)vdev;
161
162     assert(s->dataplane);
163     assert(s->dataplane_started);
164
165     return virtio_blk_handle_vq(s, vq);
166 }
167
168 /* Context: QEMU global mutex held */
169 int virtio_blk_data_plane_start(VirtIODevice *vdev)
170 {
171     VirtIOBlock *vblk = VIRTIO_BLK(vdev);
172     VirtIOBlockDataPlane *s = vblk->dataplane;
173     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vblk)));
174     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
175     unsigned i;
176     unsigned nvqs = s->conf->num_queues;
177     int r;
178
179     if (vblk->dataplane_started || s->starting) {
180         return 0;
181     }
182
183     s->starting = true;
184
185     if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
186         s->batch_notifications = true;
187     } else {
188         s->batch_notifications = false;
189     }
190
191     /* Set up guest notifier (irq) */
192     r = k->set_guest_notifiers(qbus->parent, nvqs, true);
193     if (r != 0) {
194         fprintf(stderr, "virtio-blk failed to set guest notifier (%d), "
195                 "ensure -enable-kvm is set\n", r);
196         goto fail_guest_notifiers;
197     }
198
199     /* Set up virtqueue notify */
200     for (i = 0; i < nvqs; i++) {
201         r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, true);
202         if (r != 0) {
203             fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
204             while (i--) {
205                 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
206                 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
207             }
208             goto fail_guest_notifiers;
209         }
210     }
211
212     s->starting = false;
213     vblk->dataplane_started = true;
214     trace_virtio_blk_data_plane_start(s);
215
216     blk_set_aio_context(s->conf->conf.blk, s->ctx);
217
218     /* Kick right away to begin processing requests already in vring */
219     for (i = 0; i < nvqs; i++) {
220         VirtQueue *vq = virtio_get_queue(s->vdev, i);
221
222         event_notifier_set(virtio_queue_get_host_notifier(vq));
223     }
224
225     /* Get this show started by hooking up our callbacks */
226     aio_context_acquire(s->ctx);
227     for (i = 0; i < nvqs; i++) {
228         VirtQueue *vq = virtio_get_queue(s->vdev, i);
229
230         virtio_queue_aio_set_host_notifier_handler(vq, s->ctx,
231                 virtio_blk_data_plane_handle_output);
232     }
233     aio_context_release(s->ctx);
234     return 0;
235
236   fail_guest_notifiers:
237     vblk->dataplane_disabled = true;
238     s->starting = false;
239     vblk->dataplane_started = true;
240     return -ENOSYS;
241 }
242
243 /* Stop notifications for new requests from guest.
244  *
245  * Context: BH in IOThread
246  */
247 static void virtio_blk_data_plane_stop_bh(void *opaque)
248 {
249     VirtIOBlockDataPlane *s = opaque;
250     unsigned i;
251
252     for (i = 0; i < s->conf->num_queues; i++) {
253         VirtQueue *vq = virtio_get_queue(s->vdev, i);
254
255         virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, NULL);
256     }
257 }
258
259 /* Context: QEMU global mutex held */
260 void virtio_blk_data_plane_stop(VirtIODevice *vdev)
261 {
262     VirtIOBlock *vblk = VIRTIO_BLK(vdev);
263     VirtIOBlockDataPlane *s = vblk->dataplane;
264     BusState *qbus = qdev_get_parent_bus(DEVICE(vblk));
265     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
266     unsigned i;
267     unsigned nvqs = s->conf->num_queues;
268
269     if (!vblk->dataplane_started || s->stopping) {
270         return;
271     }
272
273     /* Better luck next time. */
274     if (vblk->dataplane_disabled) {
275         vblk->dataplane_disabled = false;
276         vblk->dataplane_started = false;
277         return;
278     }
279     s->stopping = true;
280     trace_virtio_blk_data_plane_stop(s);
281
282     aio_context_acquire(s->ctx);
283     aio_wait_bh_oneshot(s->ctx, virtio_blk_data_plane_stop_bh, s);
284
285     /* Drain and switch bs back to the QEMU main loop */
286     blk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context());
287
288     aio_context_release(s->ctx);
289
290     for (i = 0; i < nvqs; i++) {
291         virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
292         virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
293     }
294
295     /* Clean up guest notifier (irq) */
296     k->set_guest_notifiers(qbus->parent, nvqs, false);
297
298     vblk->dataplane_started = false;
299     s->stopping = false;
300 }
This page took 0.041171 seconds and 4 git commands to generate.