~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to .pc/ubuntu/CVE-2013-4377.patch/hw/block/dataplane/virtio-blk.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-02-04 12:13:08 UTC
  • mfrom: (10.1.45 sid)
  • Revision ID: package-import@ubuntu.com-20140204121308-1xq92lrfs75agw2g
Tags: 1.7.0+dfsg-3ubuntu1~ppa1
* Merge 1.7.0+dfsg-3 from debian.  Remaining changes:
  - debian/patches/ubuntu:
    * expose-vmx_qemu64cpu.patch
    * linaro (omap3) and arm64 patches
    * ubuntu/target-ppc-add-stubs-for-kvm-breakpoints: fix FTBFS
      on ppc
    * ubuntu/CVE-2013-4377.patch: fix denial of service via virtio
  - debian/qemu-system-x86.modprobe: set kvm_intel nested=1 options
  - debian/control:
    * add arm64 to Architectures
    * add qemu-common and qemu-system-aarch64 packages
  - debian/qemu-system-common.install: add debian/tmp/usr/lib
  - debian/qemu-system-common.preinst: add kvm group
  - debian/qemu-system-common.postinst: remove acl placed by udev,
    and add udevadm trigger.
  - qemu-system-x86.links: add eepro100.rom, remove pxe-virtio,
    pxe-e1000 and pxe-rtl8139.
  - add qemu-system-x86.qemu-kvm.upstart and .default
  - qemu-user-static.postinst-in: remove arm64 binfmt
  - debian/rules:
    * allow parallel build
    * add aarch64 to system_targets and sys_systems
    * add qemu-kvm-spice links
    * install qemu-system-x86.modprobe
  - add debian/qemu-system-common.links for OVMF.fd link
* Remove kvm-img, kvm-nbd, kvm-ifup and kvm-ifdown symlinks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <stefanha@redhat.com>
 
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 "ioq.h"
 
21
#include "block/block.h"
 
22
#include "hw/virtio/virtio-blk.h"
 
23
#include "virtio-blk.h"
 
24
#include "block/aio.h"
 
25
#include "hw/virtio/virtio-bus.h"
 
26
 
 
27
enum {
 
28
    SEG_MAX = 126,                  /* maximum number of I/O segments */
 
29
    VRING_MAX = SEG_MAX + 2,        /* maximum number of vring descriptors */
 
30
    REQ_MAX = VRING_MAX,            /* maximum number of requests in the vring,
 
31
                                     * is VRING_MAX / 2 with traditional and
 
32
                                     * VRING_MAX with indirect descriptors */
 
33
};
 
34
 
 
35
typedef struct {
 
36
    struct iocb iocb;               /* Linux AIO control block */
 
37
    QEMUIOVector *inhdr;            /* iovecs for virtio_blk_inhdr */
 
38
    unsigned int head;              /* vring descriptor index */
 
39
    struct iovec *bounce_iov;       /* used if guest buffers are unaligned */
 
40
    QEMUIOVector *read_qiov;        /* for read completion /w bounce buffer */
 
41
} VirtIOBlockRequest;
 
42
 
 
43
struct VirtIOBlockDataPlane {
 
44
    bool started;
 
45
    bool starting;
 
46
    bool stopping;
 
47
    QEMUBH *start_bh;
 
48
    QemuThread thread;
 
49
 
 
50
    VirtIOBlkConf *blk;
 
51
    int fd;                         /* image file descriptor */
 
52
 
 
53
    VirtIODevice *vdev;
 
54
    Vring vring;                    /* virtqueue vring */
 
55
    EventNotifier *guest_notifier;  /* irq */
 
56
 
 
57
    /* Note that these EventNotifiers are assigned by value.  This is
 
58
     * fine as long as you do not call event_notifier_cleanup on them
 
59
     * (because you don't own the file descriptor or handle; you just
 
60
     * use it).
 
61
     */
 
62
    AioContext *ctx;
 
63
    EventNotifier io_notifier;      /* Linux AIO completion */
 
64
    EventNotifier host_notifier;    /* doorbell */
 
65
 
 
66
    IOQueue ioqueue;                /* Linux AIO queue (should really be per
 
67
                                       dataplane thread) */
 
68
    VirtIOBlockRequest requests[REQ_MAX]; /* pool of requests, managed by the
 
69
                                             queue */
 
70
 
 
71
    unsigned int num_reqs;
 
72
};
 
73
 
 
74
/* Raise an interrupt to signal guest, if necessary */
 
75
static void notify_guest(VirtIOBlockDataPlane *s)
 
76
{
 
77
    if (!vring_should_notify(s->vdev, &s->vring)) {
 
78
        return;
 
79
    }
 
80
 
 
81
    event_notifier_set(s->guest_notifier);
 
82
}
 
83
 
 
84
static void complete_request(struct iocb *iocb, ssize_t ret, void *opaque)
 
85
{
 
86
    VirtIOBlockDataPlane *s = opaque;
 
87
    VirtIOBlockRequest *req = container_of(iocb, VirtIOBlockRequest, iocb);
 
88
    struct virtio_blk_inhdr hdr;
 
89
    int len;
 
90
 
 
91
    if (likely(ret >= 0)) {
 
92
        hdr.status = VIRTIO_BLK_S_OK;
 
93
        len = ret;
 
94
    } else {
 
95
        hdr.status = VIRTIO_BLK_S_IOERR;
 
96
        len = 0;
 
97
    }
 
98
 
 
99
    trace_virtio_blk_data_plane_complete_request(s, req->head, ret);
 
100
 
 
101
    if (req->read_qiov) {
 
102
        assert(req->bounce_iov);
 
103
        qemu_iovec_from_buf(req->read_qiov, 0, req->bounce_iov->iov_base, len);
 
104
        qemu_iovec_destroy(req->read_qiov);
 
105
        g_slice_free(QEMUIOVector, req->read_qiov);
 
106
    }
 
107
 
 
108
    if (req->bounce_iov) {
 
109
        qemu_vfree(req->bounce_iov->iov_base);
 
110
        g_slice_free(struct iovec, req->bounce_iov);
 
111
    }
 
112
 
 
113
    qemu_iovec_from_buf(req->inhdr, 0, &hdr, sizeof(hdr));
 
114
    qemu_iovec_destroy(req->inhdr);
 
115
    g_slice_free(QEMUIOVector, req->inhdr);
 
116
 
 
117
    /* According to the virtio specification len should be the number of bytes
 
118
     * written to, but for virtio-blk it seems to be the number of bytes
 
119
     * transferred plus the status bytes.
 
120
     */
 
121
    vring_push(&s->vring, req->head, len + sizeof(hdr));
 
122
 
 
123
    s->num_reqs--;
 
124
}
 
125
 
 
126
static void complete_request_early(VirtIOBlockDataPlane *s, unsigned int head,
 
127
                                   QEMUIOVector *inhdr, unsigned char status)
 
128
{
 
129
    struct virtio_blk_inhdr hdr = {
 
130
        .status = status,
 
131
    };
 
132
 
 
133
    qemu_iovec_from_buf(inhdr, 0, &hdr, sizeof(hdr));
 
134
    qemu_iovec_destroy(inhdr);
 
135
    g_slice_free(QEMUIOVector, inhdr);
 
136
 
 
137
    vring_push(&s->vring, head, sizeof(hdr));
 
138
    notify_guest(s);
 
139
}
 
140
 
 
141
/* Get disk serial number */
 
142
static void do_get_id_cmd(VirtIOBlockDataPlane *s,
 
143
                          struct iovec *iov, unsigned int iov_cnt,
 
144
                          unsigned int head, QEMUIOVector *inhdr)
 
145
{
 
146
    char id[VIRTIO_BLK_ID_BYTES];
 
147
 
 
148
    /* Serial number not NUL-terminated when shorter than buffer */
 
149
    strncpy(id, s->blk->serial ? s->blk->serial : "", sizeof(id));
 
150
    iov_from_buf(iov, iov_cnt, 0, id, sizeof(id));
 
151
    complete_request_early(s, head, inhdr, VIRTIO_BLK_S_OK);
 
152
}
 
153
 
 
154
static int do_rdwr_cmd(VirtIOBlockDataPlane *s, bool read,
 
155
                       struct iovec *iov, unsigned int iov_cnt,
 
156
                       long long offset, unsigned int head,
 
157
                       QEMUIOVector *inhdr)
 
158
{
 
159
    struct iocb *iocb;
 
160
    QEMUIOVector qiov;
 
161
    struct iovec *bounce_iov = NULL;
 
162
    QEMUIOVector *read_qiov = NULL;
 
163
 
 
164
    qemu_iovec_init_external(&qiov, iov, iov_cnt);
 
165
    if (!bdrv_qiov_is_aligned(s->blk->conf.bs, &qiov)) {
 
166
        void *bounce_buffer = qemu_blockalign(s->blk->conf.bs, qiov.size);
 
167
 
 
168
        if (read) {
 
169
            /* Need to copy back from bounce buffer on completion */
 
170
            read_qiov = g_slice_new(QEMUIOVector);
 
171
            qemu_iovec_init(read_qiov, iov_cnt);
 
172
            qemu_iovec_concat_iov(read_qiov, iov, iov_cnt, 0, qiov.size);
 
173
        } else {
 
174
            qemu_iovec_to_buf(&qiov, 0, bounce_buffer, qiov.size);
 
175
        }
 
176
 
 
177
        /* Redirect I/O to aligned bounce buffer */
 
178
        bounce_iov = g_slice_new(struct iovec);
 
179
        bounce_iov->iov_base = bounce_buffer;
 
180
        bounce_iov->iov_len = qiov.size;
 
181
        iov = bounce_iov;
 
182
        iov_cnt = 1;
 
183
    }
 
184
 
 
185
    iocb = ioq_rdwr(&s->ioqueue, read, iov, iov_cnt, offset);
 
186
 
 
187
    /* Fill in virtio block metadata needed for completion */
 
188
    VirtIOBlockRequest *req = container_of(iocb, VirtIOBlockRequest, iocb);
 
189
    req->head = head;
 
190
    req->inhdr = inhdr;
 
191
    req->bounce_iov = bounce_iov;
 
192
    req->read_qiov = read_qiov;
 
193
    return 0;
 
194
}
 
195
 
 
196
static int process_request(IOQueue *ioq, struct iovec iov[],
 
197
                           unsigned int out_num, unsigned int in_num,
 
198
                           unsigned int head)
 
199
{
 
200
    VirtIOBlockDataPlane *s = container_of(ioq, VirtIOBlockDataPlane, ioqueue);
 
201
    struct iovec *in_iov = &iov[out_num];
 
202
    struct virtio_blk_outhdr outhdr;
 
203
    QEMUIOVector *inhdr;
 
204
    size_t in_size;
 
205
 
 
206
    /* Copy in outhdr */
 
207
    if (unlikely(iov_to_buf(iov, out_num, 0, &outhdr,
 
208
                            sizeof(outhdr)) != sizeof(outhdr))) {
 
209
        error_report("virtio-blk request outhdr too short");
 
210
        return -EFAULT;
 
211
    }
 
212
    iov_discard_front(&iov, &out_num, sizeof(outhdr));
 
213
 
 
214
    /* Grab inhdr for later */
 
215
    in_size = iov_size(in_iov, in_num);
 
216
    if (in_size < sizeof(struct virtio_blk_inhdr)) {
 
217
        error_report("virtio_blk request inhdr too short");
 
218
        return -EFAULT;
 
219
    }
 
220
    inhdr = g_slice_new(QEMUIOVector);
 
221
    qemu_iovec_init(inhdr, 1);
 
222
    qemu_iovec_concat_iov(inhdr, in_iov, in_num,
 
223
            in_size - sizeof(struct virtio_blk_inhdr),
 
224
            sizeof(struct virtio_blk_inhdr));
 
225
    iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr));
 
226
 
 
227
    /* TODO Linux sets the barrier bit even when not advertised! */
 
228
    outhdr.type &= ~VIRTIO_BLK_T_BARRIER;
 
229
 
 
230
    switch (outhdr.type) {
 
231
    case VIRTIO_BLK_T_IN:
 
232
        do_rdwr_cmd(s, true, in_iov, in_num, outhdr.sector * 512, head, inhdr);
 
233
        return 0;
 
234
 
 
235
    case VIRTIO_BLK_T_OUT:
 
236
        do_rdwr_cmd(s, false, iov, out_num, outhdr.sector * 512, head, inhdr);
 
237
        return 0;
 
238
 
 
239
    case VIRTIO_BLK_T_SCSI_CMD:
 
240
        /* TODO support SCSI commands */
 
241
        complete_request_early(s, head, inhdr, VIRTIO_BLK_S_UNSUPP);
 
242
        return 0;
 
243
 
 
244
    case VIRTIO_BLK_T_FLUSH:
 
245
        /* TODO fdsync not supported by Linux AIO, do it synchronously here! */
 
246
        if (qemu_fdatasync(s->fd) < 0) {
 
247
            complete_request_early(s, head, inhdr, VIRTIO_BLK_S_IOERR);
 
248
        } else {
 
249
            complete_request_early(s, head, inhdr, VIRTIO_BLK_S_OK);
 
250
        }
 
251
        return 0;
 
252
 
 
253
    case VIRTIO_BLK_T_GET_ID:
 
254
        do_get_id_cmd(s, in_iov, in_num, head, inhdr);
 
255
        return 0;
 
256
 
 
257
    default:
 
258
        error_report("virtio-blk unsupported request type %#x", outhdr.type);
 
259
        qemu_iovec_destroy(inhdr);
 
260
        g_slice_free(QEMUIOVector, inhdr);
 
261
        return -EFAULT;
 
262
    }
 
263
}
 
264
 
 
265
static void handle_notify(EventNotifier *e)
 
266
{
 
267
    VirtIOBlockDataPlane *s = container_of(e, VirtIOBlockDataPlane,
 
268
                                           host_notifier);
 
269
 
 
270
    /* There is one array of iovecs into which all new requests are extracted
 
271
     * from the vring.  Requests are read from the vring and the translated
 
272
     * descriptors are written to the iovecs array.  The iovecs do not have to
 
273
     * persist across handle_notify() calls because the kernel copies the
 
274
     * iovecs on io_submit().
 
275
     *
 
276
     * Handling io_submit() EAGAIN may require storing the requests across
 
277
     * handle_notify() calls until the kernel has sufficient resources to
 
278
     * accept more I/O.  This is not implemented yet.
 
279
     */
 
280
    struct iovec iovec[VRING_MAX];
 
281
    struct iovec *end = &iovec[VRING_MAX];
 
282
    struct iovec *iov = iovec;
 
283
 
 
284
    /* When a request is read from the vring, the index of the first descriptor
 
285
     * (aka head) is returned so that the completed request can be pushed onto
 
286
     * the vring later.
 
287
     *
 
288
     * The number of hypervisor read-only iovecs is out_num.  The number of
 
289
     * hypervisor write-only iovecs is in_num.
 
290
     */
 
291
    int head;
 
292
    unsigned int out_num = 0, in_num = 0;
 
293
    unsigned int num_queued;
 
294
 
 
295
    event_notifier_test_and_clear(&s->host_notifier);
 
296
    for (;;) {
 
297
        /* Disable guest->host notifies to avoid unnecessary vmexits */
 
298
        vring_disable_notification(s->vdev, &s->vring);
 
299
 
 
300
        for (;;) {
 
301
            head = vring_pop(s->vdev, &s->vring, iov, end, &out_num, &in_num);
 
302
            if (head < 0) {
 
303
                break; /* no more requests */
 
304
            }
 
305
 
 
306
            trace_virtio_blk_data_plane_process_request(s, out_num, in_num,
 
307
                                                        head);
 
308
 
 
309
            if (process_request(&s->ioqueue, iov, out_num, in_num, head) < 0) {
 
310
                vring_set_broken(&s->vring);
 
311
                break;
 
312
            }
 
313
            iov += out_num + in_num;
 
314
        }
 
315
 
 
316
        if (likely(head == -EAGAIN)) { /* vring emptied */
 
317
            /* Re-enable guest->host notifies and stop processing the vring.
 
318
             * But if the guest has snuck in more descriptors, keep processing.
 
319
             */
 
320
            if (vring_enable_notification(s->vdev, &s->vring)) {
 
321
                break;
 
322
            }
 
323
        } else { /* head == -ENOBUFS or fatal error, iovecs[] is depleted */
 
324
            /* Since there are no iovecs[] left, stop processing for now.  Do
 
325
             * not re-enable guest->host notifies since the I/O completion
 
326
             * handler knows to check for more vring descriptors anyway.
 
327
             */
 
328
            break;
 
329
        }
 
330
    }
 
331
 
 
332
    num_queued = ioq_num_queued(&s->ioqueue);
 
333
    if (num_queued > 0) {
 
334
        s->num_reqs += num_queued;
 
335
 
 
336
        int rc = ioq_submit(&s->ioqueue);
 
337
        if (unlikely(rc < 0)) {
 
338
            fprintf(stderr, "ioq_submit failed %d\n", rc);
 
339
            exit(1);
 
340
        }
 
341
    }
 
342
}
 
343
 
 
344
static void handle_io(EventNotifier *e)
 
345
{
 
346
    VirtIOBlockDataPlane *s = container_of(e, VirtIOBlockDataPlane,
 
347
                                           io_notifier);
 
348
 
 
349
    event_notifier_test_and_clear(&s->io_notifier);
 
350
    if (ioq_run_completion(&s->ioqueue, complete_request, s) > 0) {
 
351
        notify_guest(s);
 
352
    }
 
353
 
 
354
    /* If there were more requests than iovecs, the vring will not be empty yet
 
355
     * so check again.  There should now be enough resources to process more
 
356
     * requests.
 
357
     */
 
358
    if (unlikely(vring_more_avail(&s->vring))) {
 
359
        handle_notify(&s->host_notifier);
 
360
    }
 
361
}
 
362
 
 
363
static void *data_plane_thread(void *opaque)
 
364
{
 
365
    VirtIOBlockDataPlane *s = opaque;
 
366
 
 
367
    while (!s->stopping || s->num_reqs > 0) {
 
368
        aio_poll(s->ctx, true);
 
369
    }
 
370
    return NULL;
 
371
}
 
372
 
 
373
static void start_data_plane_bh(void *opaque)
 
374
{
 
375
    VirtIOBlockDataPlane *s = opaque;
 
376
 
 
377
    qemu_bh_delete(s->start_bh);
 
378
    s->start_bh = NULL;
 
379
    qemu_thread_create(&s->thread, data_plane_thread,
 
380
                       s, QEMU_THREAD_JOINABLE);
 
381
}
 
382
 
 
383
bool virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *blk,
 
384
                                  VirtIOBlockDataPlane **dataplane)
 
385
{
 
386
    VirtIOBlockDataPlane *s;
 
387
    int fd;
 
388
 
 
389
    *dataplane = NULL;
 
390
 
 
391
    if (!blk->data_plane) {
 
392
        return true;
 
393
    }
 
394
 
 
395
    if (blk->scsi) {
 
396
        error_report("device is incompatible with x-data-plane, use scsi=off");
 
397
        return false;
 
398
    }
 
399
 
 
400
    if (blk->config_wce) {
 
401
        error_report("device is incompatible with x-data-plane, "
 
402
                     "use config-wce=off");
 
403
        return false;
 
404
    }
 
405
 
 
406
    /* If dataplane is (re-)enabled while the guest is running there could be
 
407
     * block jobs that can conflict.
 
408
     */
 
409
    if (bdrv_in_use(blk->conf.bs)) {
 
410
        error_report("cannot start dataplane thread while device is in use");
 
411
        return false;
 
412
    }
 
413
 
 
414
    fd = raw_get_aio_fd(blk->conf.bs);
 
415
    if (fd < 0) {
 
416
        error_report("drive is incompatible with x-data-plane, "
 
417
                     "use format=raw,cache=none,aio=native");
 
418
        return false;
 
419
    }
 
420
 
 
421
    s = g_new0(VirtIOBlockDataPlane, 1);
 
422
    s->vdev = vdev;
 
423
    s->fd = fd;
 
424
    s->blk = blk;
 
425
 
 
426
    /* Prevent block operations that conflict with data plane thread */
 
427
    bdrv_set_in_use(blk->conf.bs, 1);
 
428
 
 
429
    *dataplane = s;
 
430
    return true;
 
431
}
 
432
 
 
433
void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
 
434
{
 
435
    if (!s) {
 
436
        return;
 
437
    }
 
438
 
 
439
    virtio_blk_data_plane_stop(s);
 
440
    bdrv_set_in_use(s->blk->conf.bs, 0);
 
441
    g_free(s);
 
442
}
 
443
 
 
444
void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)
 
445
{
 
446
    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
 
447
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
 
448
    VirtQueue *vq;
 
449
    int i;
 
450
 
 
451
    if (s->started) {
 
452
        return;
 
453
    }
 
454
 
 
455
    if (s->starting) {
 
456
        return;
 
457
    }
 
458
 
 
459
    s->starting = true;
 
460
 
 
461
    vq = virtio_get_queue(s->vdev, 0);
 
462
    if (!vring_setup(&s->vring, s->vdev, 0)) {
 
463
        s->starting = false;
 
464
        return;
 
465
    }
 
466
 
 
467
    s->ctx = aio_context_new();
 
468
 
 
469
    /* Set up guest notifier (irq) */
 
470
    if (k->set_guest_notifiers(qbus->parent, 1, true) != 0) {
 
471
        fprintf(stderr, "virtio-blk failed to set guest notifier, "
 
472
                "ensure -enable-kvm is set\n");
 
473
        exit(1);
 
474
    }
 
475
    s->guest_notifier = virtio_queue_get_guest_notifier(vq);
 
476
 
 
477
    /* Set up virtqueue notify */
 
478
    if (k->set_host_notifier(qbus->parent, 0, true) != 0) {
 
479
        fprintf(stderr, "virtio-blk failed to set host notifier\n");
 
480
        exit(1);
 
481
    }
 
482
    s->host_notifier = *virtio_queue_get_host_notifier(vq);
 
483
    aio_set_event_notifier(s->ctx, &s->host_notifier, handle_notify);
 
484
 
 
485
    /* Set up ioqueue */
 
486
    ioq_init(&s->ioqueue, s->fd, REQ_MAX);
 
487
    for (i = 0; i < ARRAY_SIZE(s->requests); i++) {
 
488
        ioq_put_iocb(&s->ioqueue, &s->requests[i].iocb);
 
489
    }
 
490
    s->io_notifier = *ioq_get_notifier(&s->ioqueue);
 
491
    aio_set_event_notifier(s->ctx, &s->io_notifier, handle_io);
 
492
 
 
493
    s->starting = false;
 
494
    s->started = true;
 
495
    trace_virtio_blk_data_plane_start(s);
 
496
 
 
497
    /* Kick right away to begin processing requests already in vring */
 
498
    event_notifier_set(virtio_queue_get_host_notifier(vq));
 
499
 
 
500
    /* Spawn thread in BH so it inherits iothread cpusets */
 
501
    s->start_bh = qemu_bh_new(start_data_plane_bh, s);
 
502
    qemu_bh_schedule(s->start_bh);
 
503
}
 
504
 
 
505
void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)
 
506
{
 
507
    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
 
508
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
 
509
    if (!s->started || s->stopping) {
 
510
        return;
 
511
    }
 
512
    s->stopping = true;
 
513
    trace_virtio_blk_data_plane_stop(s);
 
514
 
 
515
    /* Stop thread or cancel pending thread creation BH */
 
516
    if (s->start_bh) {
 
517
        qemu_bh_delete(s->start_bh);
 
518
        s->start_bh = NULL;
 
519
    } else {
 
520
        aio_notify(s->ctx);
 
521
        qemu_thread_join(&s->thread);
 
522
    }
 
523
 
 
524
    aio_set_event_notifier(s->ctx, &s->io_notifier, NULL);
 
525
    ioq_cleanup(&s->ioqueue);
 
526
 
 
527
    aio_set_event_notifier(s->ctx, &s->host_notifier, NULL);
 
528
    k->set_host_notifier(qbus->parent, 0, false);
 
529
 
 
530
    aio_context_unref(s->ctx);
 
531
 
 
532
    /* Clean up guest notifier (irq) */
 
533
    k->set_guest_notifiers(qbus->parent, 1, false);
 
534
 
 
535
    vring_teardown(&s->vring, s->vdev, 0);
 
536
    s->started = false;
 
537
    s->stopping = false;
 
538
}