]> Git Repo - qemu.git/blob - hw/block/dataplane/virtio-blk.c
Merge remote-tracking branch 'remotes/bonzini/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 "trace.h"
16 #include "qemu/iov.h"
17 #include "qemu/thread.h"
18 #include "qemu/error-report.h"
19 #include "hw/virtio/dataplane/vring.h"
20 #include "sysemu/block-backend.h"
21 #include "hw/virtio/virtio-blk.h"
22 #include "virtio-blk.h"
23 #include "block/aio.h"
24 #include "hw/virtio/virtio-bus.h"
25 #include "qom/object_interfaces.h"
26
27 struct VirtIOBlockDataPlane {
28     bool started;
29     bool starting;
30     bool stopping;
31     bool disabled;
32
33     VirtIOBlkConf *conf;
34
35     VirtIODevice *vdev;
36     Vring vring;                    /* virtqueue vring */
37     EventNotifier *guest_notifier;  /* irq */
38     QEMUBH *bh;                     /* bh for guest notification */
39
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
43      * use it).
44      */
45     IOThread *iothread;
46     IOThread internal_iothread_obj;
47     AioContext *ctx;
48     EventNotifier host_notifier;    /* doorbell */
49
50     /* Operation blocker on BDS */
51     Error *blocker;
52     void (*saved_complete_request)(struct VirtIOBlockReq *req,
53                                    unsigned char status);
54 };
55
56 /* Raise an interrupt to signal guest, if necessary */
57 static void notify_guest(VirtIOBlockDataPlane *s)
58 {
59     if (!vring_should_notify(s->vdev, &s->vring)) {
60         return;
61     }
62
63     event_notifier_set(s->guest_notifier);
64 }
65
66 static void notify_guest_bh(void *opaque)
67 {
68     VirtIOBlockDataPlane *s = opaque;
69
70     notify_guest(s);
71 }
72
73 static void complete_request_vring(VirtIOBlockReq *req, unsigned char status)
74 {
75     VirtIOBlockDataPlane *s = req->dev->dataplane;
76     stb_p(&req->in->status, status);
77
78     vring_push(&req->dev->dataplane->vring, &req->elem,
79                req->qiov.size + sizeof(*req->in));
80
81     /* Suppress notification to guest by BH and its scheduled
82      * flag because requests are completed as a batch after io
83      * plug & unplug is introduced, and the BH can still be
84      * executed in dataplane aio context even after it is
85      * stopped, so needn't worry about notification loss with BH.
86      */
87     qemu_bh_schedule(s->bh);
88 }
89
90 static void handle_notify(EventNotifier *e)
91 {
92     VirtIOBlockDataPlane *s = container_of(e, VirtIOBlockDataPlane,
93                                            host_notifier);
94     VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
95
96     event_notifier_test_and_clear(&s->host_notifier);
97     blk_io_plug(s->conf->conf.blk);
98     for (;;) {
99         MultiReqBuffer mrb = {
100             .num_writes = 0,
101         };
102         int ret;
103
104         /* Disable guest->host notifies to avoid unnecessary vmexits */
105         vring_disable_notification(s->vdev, &s->vring);
106
107         for (;;) {
108             VirtIOBlockReq *req = virtio_blk_alloc_request(vblk);
109
110             ret = vring_pop(s->vdev, &s->vring, &req->elem);
111             if (ret < 0) {
112                 virtio_blk_free_request(req);
113                 break; /* no more requests */
114             }
115
116             trace_virtio_blk_data_plane_process_request(s, req->elem.out_num,
117                                                         req->elem.in_num,
118                                                         req->elem.index);
119
120             virtio_blk_handle_request(req, &mrb);
121         }
122
123         virtio_submit_multiwrite(s->conf->conf.blk, &mrb);
124
125         if (likely(ret == -EAGAIN)) { /* vring emptied */
126             /* Re-enable guest->host notifies and stop processing the vring.
127              * But if the guest has snuck in more descriptors, keep processing.
128              */
129             if (vring_enable_notification(s->vdev, &s->vring)) {
130                 break;
131             }
132         } else { /* fatal error */
133             break;
134         }
135     }
136     blk_io_unplug(s->conf->conf.blk);
137 }
138
139 /* Context: QEMU global mutex held */
140 void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf,
141                                   VirtIOBlockDataPlane **dataplane,
142                                   Error **errp)
143 {
144     VirtIOBlockDataPlane *s;
145     Error *local_err = NULL;
146     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
147     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
148
149     *dataplane = NULL;
150
151     if (!conf->data_plane && !conf->iothread) {
152         return;
153     }
154
155     /* Don't try if transport does not support notifiers. */
156     if (!k->set_guest_notifiers || !k->set_host_notifier) {
157         error_setg(errp,
158                    "device is incompatible with x-data-plane "
159                    "(transport does not support notifiers)");
160         return;
161     }
162
163     /* If dataplane is (re-)enabled while the guest is running there could be
164      * block jobs that can conflict.
165      */
166     if (blk_op_is_blocked(conf->conf.blk, BLOCK_OP_TYPE_DATAPLANE,
167                           &local_err)) {
168         error_setg(errp, "cannot start dataplane thread: %s",
169                    error_get_pretty(local_err));
170         error_free(local_err);
171         return;
172     }
173
174     s = g_new0(VirtIOBlockDataPlane, 1);
175     s->vdev = vdev;
176     s->conf = conf;
177
178     if (conf->iothread) {
179         s->iothread = conf->iothread;
180         object_ref(OBJECT(s->iothread));
181     } else {
182         /* Create per-device IOThread if none specified.  This is for
183          * x-data-plane option compatibility.  If x-data-plane is removed we
184          * can drop this.
185          */
186         object_initialize(&s->internal_iothread_obj,
187                           sizeof(s->internal_iothread_obj),
188                           TYPE_IOTHREAD);
189         user_creatable_complete(OBJECT(&s->internal_iothread_obj), &error_abort);
190         s->iothread = &s->internal_iothread_obj;
191     }
192     s->ctx = iothread_get_aio_context(s->iothread);
193     s->bh = aio_bh_new(s->ctx, notify_guest_bh, s);
194
195     error_setg(&s->blocker, "block device is in use by data plane");
196     blk_op_block_all(conf->conf.blk, s->blocker);
197     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_RESIZE, s->blocker);
198     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_DRIVE_DEL, s->blocker);
199     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_BACKUP_SOURCE, s->blocker);
200     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_CHANGE, s->blocker);
201     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_COMMIT_SOURCE, s->blocker);
202     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_COMMIT_TARGET, s->blocker);
203     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_EJECT, s->blocker);
204     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, s->blocker);
205     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, s->blocker);
206     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE,
207                    s->blocker);
208     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_MIRROR, s->blocker);
209     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_STREAM, s->blocker);
210     blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_REPLACE, s->blocker);
211
212     *dataplane = s;
213 }
214
215 /* Context: QEMU global mutex held */
216 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
217 {
218     if (!s) {
219         return;
220     }
221
222     virtio_blk_data_plane_stop(s);
223     blk_op_unblock_all(s->conf->conf.blk, s->blocker);
224     error_free(s->blocker);
225     object_unref(OBJECT(s->iothread));
226     qemu_bh_delete(s->bh);
227     g_free(s);
228 }
229
230 /* Context: QEMU global mutex held */
231 void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)
232 {
233     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
234     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
235     VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
236     VirtQueue *vq;
237     int r;
238
239     if (s->started || s->disabled) {
240         return;
241     }
242
243     if (s->starting) {
244         return;
245     }
246
247     s->starting = true;
248
249     vq = virtio_get_queue(s->vdev, 0);
250     if (!vring_setup(&s->vring, s->vdev, 0)) {
251         goto fail_vring;
252     }
253
254     /* Set up guest notifier (irq) */
255     r = k->set_guest_notifiers(qbus->parent, 1, true);
256     if (r != 0) {
257         fprintf(stderr, "virtio-blk failed to set guest notifier (%d), "
258                 "ensure -enable-kvm is set\n", r);
259         goto fail_guest_notifiers;
260     }
261     s->guest_notifier = virtio_queue_get_guest_notifier(vq);
262
263     /* Set up virtqueue notify */
264     r = k->set_host_notifier(qbus->parent, 0, true);
265     if (r != 0) {
266         fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
267         goto fail_host_notifier;
268     }
269     s->host_notifier = *virtio_queue_get_host_notifier(vq);
270
271     s->saved_complete_request = vblk->complete_request;
272     vblk->complete_request = complete_request_vring;
273
274     s->starting = false;
275     s->started = true;
276     trace_virtio_blk_data_plane_start(s);
277
278     blk_set_aio_context(s->conf->conf.blk, s->ctx);
279
280     /* Kick right away to begin processing requests already in vring */
281     event_notifier_set(virtio_queue_get_host_notifier(vq));
282
283     /* Get this show started by hooking up our callbacks */
284     aio_context_acquire(s->ctx);
285     aio_set_event_notifier(s->ctx, &s->host_notifier, handle_notify);
286     aio_context_release(s->ctx);
287     return;
288
289   fail_host_notifier:
290     k->set_guest_notifiers(qbus->parent, 1, false);
291   fail_guest_notifiers:
292     vring_teardown(&s->vring, s->vdev, 0);
293     s->disabled = true;
294   fail_vring:
295     s->starting = false;
296 }
297
298 /* Context: QEMU global mutex held */
299 void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)
300 {
301     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
302     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
303     VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
304
305
306     /* Better luck next time. */
307     if (s->disabled) {
308         s->disabled = false;
309         return;
310     }
311     if (!s->started || s->stopping) {
312         return;
313     }
314     s->stopping = true;
315     vblk->complete_request = s->saved_complete_request;
316     trace_virtio_blk_data_plane_stop(s);
317
318     aio_context_acquire(s->ctx);
319
320     /* Stop notifications for new requests from guest */
321     aio_set_event_notifier(s->ctx, &s->host_notifier, NULL);
322
323     /* Drain and switch bs back to the QEMU main loop */
324     blk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context());
325
326     aio_context_release(s->ctx);
327
328     /* Sync vring state back to virtqueue so that non-dataplane request
329      * processing can continue when we disable the host notifier below.
330      */
331     vring_teardown(&s->vring, s->vdev, 0);
332
333     k->set_host_notifier(qbus->parent, 0, false);
334
335     /* Clean up guest notifier (irq) */
336     k->set_guest_notifiers(qbus->parent, 1, false);
337
338     s->started = false;
339     s->stopping = false;
340 }
This page took 0.042333 seconds and 4 git commands to generate.