Skip to contentSkip to conversation

aio: properly bubble up errors from initialization

RFC PATCH Superseded Nishanth Aravamudan 4 participants 14 messages Jun 14, 2018 – Jun 21, 2018 Block layer

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 aio: properly bubble up errors from initialization

laio_init() can fail for a couple of reasons, which will lead to a NULL pointer dereference in laio_attach_aio_context().

To solve this, add a aio_linux_aio_setup() path which is called where aio_get_linux_aio() is called currently, but can propogate errors up.

virtio-block and virtio-scsi call this new function before calling blk_io_plug() (which eventually calls aio_get_linux_aio). This is necessary because plug/unplug currently assume they do not fail.

It is trivial to make qemu segfault in my testing. Set /proc/sys/fs/aio-max-nr to 0 and start a guest with aio=native,cache=directsync. With this patch, the guest successfully starts (but obviously isn't using native AIO). Setting aio-max-nr back up to a reasonable value, AIO contexts are consumed normally.

---
 block/block-backend.c          | 10 ++++++++++
 block/file-posix.c             | 27 +++++++++++++++++++++++++++
 block/io.c                     | 27 +++++++++++++++++++++++++++
 block/linux-aio.c              | 15 ++++++++++-----
 hw/block/virtio-blk.c          |  4 ++++
 hw/scsi/virtio-scsi.c          |  4 ++++
 include/block/aio.h            |  3 +++
 include/block/block.h          |  1 +
 include/block/block_int.h      |  1 +
 include/block/raw-aio.h        |  2 +-
 include/sysemu/block-backend.h |  1 +
 stubs/linux-aio.c              |  2 +-
 util/async.c                   | 14 +++++++++++---
 13 files changed, 101 insertions(+), 10 deletions(-)
block/block-backend.c +10 -0
index d55c328736..2a18bb2af4 100644
@@ -1946,6 +1946,16 @@ void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify)
     notifier_list_add(&blk->insert_bs_notifiers, notify);
 }
 
+int blk_io_plug_setup(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+
+    if (bs) {
+        return bdrv_io_plug_setup(bs);
+    }
+    return 0;
+}
+
 void blk_io_plug(BlockBackend *blk)
 {
     BlockDriverState *bs = blk_bs(blk);
block/file-posix.c +27 -0
index 07bb061fe4..adaba7c366 100644
@@ -1665,6 +1665,12 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,
             type |= QEMU_AIO_MISALIGNED;
 #ifdef CONFIG_LINUX_AIO
         } else if (s->use_linux_aio) {
+            int rc;
+            rc = aio_linux_aio_setup(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                s->use_linux_aio = 0;
+                return rc;
+            }
             LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
             assert(qiov->size == bytes);
             return laio_co_submit(bs, aio, s->fd, offset, qiov, type);
@@ -1690,12 +1696,28 @@ static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset,
     return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_WRITE);
 }
 
+static int raw_aio_plug_setup(BlockDriverState *bs)
+{
+    int rc = 0;
+#ifdef CONFIG_LINUX_AIO
+    BDRVRawState *s = bs->opaque;
+    if (s->use_linux_aio) {
+        rc = aio_linux_aio_setup(bdrv_get_aio_context(bs));
+        if (rc != 0) {
+            s->use_linux_aio = 0;
+        }
+    }
+#endif
+    return rc;
+}
+
 static void raw_aio_plug(BlockDriverState *bs)
 {
 #ifdef CONFIG_LINUX_AIO
     BDRVRawState *s = bs->opaque;
     if (s->use_linux_aio) {
         LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
+        assert(aio != NULL);
         laio_io_plug(bs, aio);
     }
 #endif
@@ -1707,6 +1729,7 @@ static void raw_aio_unplug(BlockDriverState *bs)
     BDRVRawState *s = bs->opaque;
     if (s->use_linux_aio) {
         LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
+        assert(aio != NULL);
         laio_io_unplug(bs, aio);
     }
 #endif
@@ -2599,6 +2622,7 @@ BlockDriver bdrv_file = {
     .bdrv_co_copy_range_from = raw_co_copy_range_from,
     .bdrv_co_copy_range_to  = raw_co_copy_range_to,
     .bdrv_refresh_limits = raw_refresh_limits,
+    .bdrv_io_plug_setup = raw_aio_plug_setup,
     .bdrv_io_plug = raw_aio_plug,
     .bdrv_io_unplug = raw_aio_unplug,
 
@@ -3079,6 +3103,7 @@ static BlockDriver bdrv_host_device = {
     .bdrv_co_copy_range_from = raw_co_copy_range_from,
     .bdrv_co_copy_range_to  = raw_co_copy_range_to,
     .bdrv_refresh_limits = raw_refresh_limits,
+    .bdrv_io_plug_setup = raw_aio_plug_setup,
     .bdrv_io_plug = raw_aio_plug,
     .bdrv_io_unplug = raw_aio_unplug,
 
@@ -3201,6 +3226,7 @@ static BlockDriver bdrv_host_cdrom = {
     .bdrv_co_pwritev        = raw_co_pwritev,
     .bdrv_aio_flush	= raw_aio_flush,
     .bdrv_refresh_limits = raw_refresh_limits,
+    .bdrv_io_plug_setup = raw_aio_plug_setup,
     .bdrv_io_plug = raw_aio_plug,
     .bdrv_io_unplug = raw_aio_unplug,
 
@@ -3331,6 +3357,7 @@ static BlockDriver bdrv_host_cdrom = {
     .bdrv_co_pwritev        = raw_co_pwritev,
     .bdrv_aio_flush	= raw_aio_flush,
     .bdrv_refresh_limits = raw_refresh_limits,
+    .bdrv_io_plug_setup = raw_aio_plug_setup,
     .bdrv_io_plug = raw_aio_plug,
     .bdrv_io_unplug = raw_aio_unplug,
 
block/io.c +27 -0
index b7beaeeb9f..3c9711f6ed 100644
@@ -2779,6 +2779,33 @@ void bdrv_add_before_write_notifier(BlockDriverState *bs,
     notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
 }
 
+int bdrv_io_plug_setup(BlockDriverState *bs)
+{
+    int rc;
+    BdrvChild *child;
+
+    QLIST_FOREACH(child, &bs->children, next) {
+        // XXX: do we need to undo the successful setups if we fail midway?
+        rc = bdrv_io_plug_setup(child->bs);
+        if (rc != 0) {
+            return rc;
+        }
+    }
+
+    if (atomic_fetch_inc(&bs->io_plugged) == 0) {
+        BlockDriver *drv = bs->drv;
+        if (drv && drv->bdrv_io_plug_setup) {
+            rc = drv->bdrv_io_plug_setup(bs);
+            // XXX: do we need to undo the successful setups if we fail midway?
+            if (rc != 0) {
+                return rc;
+            }
+        }
+    }
+
+    return 0;
+}
+
 void bdrv_io_plug(BlockDriverState *bs)
 {
     BdrvChild *child;
block/linux-aio.c +10 -5
index 88b8d55ec7..4d799f85fe 100644
@@ -470,28 +470,33 @@ void laio_attach_aio_context(LinuxAioState *s, AioContext *new_context)
                            qemu_laio_poll_cb);
 }
 
-LinuxAioState *laio_init(void)
+int laio_init(LinuxAioState **linux_aio)
 {
+    int rc;
     LinuxAioState *s;
 
     s = g_malloc0(sizeof(*s));
-    if (event_notifier_init(&s->e, false) < 0) {
+    rc = event_notifier_init(&s->e, false);
+    if (rc < 0) {
         goto out_free_state;
     }
 
-    if (io_setup(MAX_EVENTS, &s->ctx) != 0) {
+    rc = io_setup(MAX_EVENTS, &s->ctx);
+    if (rc != 0) {
         goto out_close_efd;
     }
 
     ioq_init(&s->io_q);
 
-    return s;
+    *linux_aio = s;
+    return 0;
 
 out_close_efd:
     event_notifier_cleanup(&s->e);
 out_free_state:
     g_free(s);
-    return NULL;
+    *linux_aio = NULL;
+    return rc;
 }
 
 void laio_cleanup(LinuxAioState *s)
hw/block/virtio-blk.c +4 -0
index 50b5c869e3..a2cd008a4c 100644
@@ -598,6 +598,9 @@ bool virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)
     bool progress = false;
 
     aio_context_acquire(blk_get_aio_context(s->blk));
+    if (blk_io_plug_setup(s->blk) != 0) {
+        goto error;
+    }
     blk_io_plug(s->blk);
 
     do {
@@ -620,6 +623,7 @@ bool virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)
     }
 
     blk_io_unplug(s->blk);
+error:
     aio_context_release(blk_get_aio_context(s->blk));
     return progress;
 }
hw/scsi/virtio-scsi.c +4 -0
index 3aa99717e2..7d17f082d5 100644
@@ -569,6 +569,10 @@ static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
         return -ENOBUFS;
     }
     scsi_req_ref(req->sreq);
+    rc = blk_io_plug_setup(d->conf.blk);
+    if (rc != 0) {
+        return rc;
+    }
     blk_io_plug(d->conf.blk);
     return 0;
 }
include/block/aio.h +3 -0
index ae6f354e6c..64881c24b9 100644
@@ -381,6 +381,9 @@ GSource *aio_get_g_source(AioContext *ctx);
 /* Return the ThreadPool bound to this AioContext */
 struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
 
+/* Setup the LinuxAioState bound to this AioContext */
+int aio_linux_aio_setup(AioContext *ctx);
+
 /* Return the LinuxAioState bound to this AioContext */
 struct LinuxAioState *aio_get_linux_aio(AioContext *ctx);
 
include/block/block.h +1 -0
index e677080c4e..2974f64ad2 100644
@@ -548,6 +548,7 @@ void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context);
 int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz);
 int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo);
 
+int bdrv_io_plug_setup(BlockDriverState *bs);
 void bdrv_io_plug(BlockDriverState *bs);
 void bdrv_io_unplug(BlockDriverState *bs);
 
include/block/block_int.h +1 -0
index 327e478a73..2e5ffbdc4f 100644
@@ -388,6 +388,7 @@ struct BlockDriver {
                                     AioContext *new_context);
 
     /* io queue for linux-aio */
+    int (*bdrv_io_plug_setup)(BlockDriverState *bs);
     void (*bdrv_io_plug)(BlockDriverState *bs);
     void (*bdrv_io_unplug)(BlockDriverState *bs);
 
include/block/raw-aio.h +1 -1
index 0e717fd475..81b90e5fc6 100644
@@ -43,7 +43,7 @@
 /* linux-aio.c - Linux native implementation */
 #ifdef CONFIG_LINUX_AIO
 typedef struct LinuxAioState LinuxAioState;
-LinuxAioState *laio_init(void);
+int laio_init(LinuxAioState **linux_aio);
 void laio_cleanup(LinuxAioState *s);
 int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, int fd,
                                 uint64_t offset, QEMUIOVector *qiov, int type);
include/sysemu/block-backend.h +1 -0
index 8d03d493c2..7a1093f8f0 100644
@@ -197,6 +197,7 @@ void blk_remove_aio_context_notifier(BlockBackend *blk,
                                      void *opaque);
 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify);
 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify);
+int blk_io_plug_setup(BlockBackend *blk);
 void blk_io_plug(BlockBackend *blk);
 void blk_io_unplug(BlockBackend *blk);
 BlockAcctStats *blk_get_stats(BlockBackend *blk);
stubs/linux-aio.c +1 -1
index ed47bd443c..88ab927e35 100644
@@ -21,7 +21,7 @@ void laio_attach_aio_context(LinuxAioState *s, AioContext *new_context)
     abort();
 }
 
-LinuxAioState *laio_init(void)
+int laio_init(LinuxAioState **linux_aio)
 {
     abort();
 }
util/async.c +11 -3
index 03f62787f2..34534aae63 100644
@@ -323,12 +323,20 @@ ThreadPool *aio_get_thread_pool(AioContext *ctx)
 }
 
 #ifdef CONFIG_LINUX_AIO
-LinuxAioState *aio_get_linux_aio(AioContext *ctx)
+int aio_linux_aio_setup(AioContext *ctx)
 {
+    int rc = 0;
     if (!ctx->linux_aio) {
-        ctx->linux_aio = laio_init();
-        laio_attach_aio_context(ctx->linux_aio, ctx);
+        rc = laio_init(&ctx->linux_aio);
+        if (rc == 0) {
+            laio_attach_aio_context(ctx->linux_aio, ctx);
+        }
     }
+    return rc;
+}
+
+LinuxAioState *aio_get_linux_aio(AioContext *ctx)
+{
     return ctx->linux_aio;
 }
 #endif
Signature · 1 line
2.17.1
#1 lore raw
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH] [RFC] aio: properly bubble up errors from initialization

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
e72bf49783 aio: properly bubble up errors from initialization

=== OUTPUT BEGIN ===
Checking PATCH 1/1: aio: properly bubble up errors from initialization...
ERROR: do not use C99 // comments
#146: FILE: block/io.c:2788:
+ // XXX: do we need to undo the successful setups if we fail midway?

ERROR: do not use C99 // comments
#157: FILE: block/io.c:2799:
+ // XXX: do we need to undo the successful setups if we fail midway?

total: 2 errors, 0 warnings, 258 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]

#2 lore raw
In reply to Nishanth Aravamudan

Am 15.06.2018 um 01:21 hat Nishanth Aravamudan geschrieben:

Quoted 10 lines

laio_init() can fail for a couple of reasons, which will lead to a NULL pointer dereference in laio_attach_aio_context().

To solve this, add a aio_linux_aio_setup() path which is called where aio_get_linux_aio() is called currently, but can propogate errors up.

virtio-block and virtio-scsi call this new function before calling blk_io_plug() (which eventually calls aio_get_linux_aio). This is necessary because plug/unplug currently assume they do not fail.

Quoted 7 lines

It is trivial to make qemu segfault in my testing. Set /proc/sys/fs/aio-max-nr to 0 and start a guest with aio=native,cache=directsync. With this patch, the guest successfully starts (but obviously isn't using native AIO). Setting aio-max-nr back up to a reasonable value, AIO contexts are consumed normally.

This is not a reasonable fix for several reasons:

* You frame this as a problem of blk_io_plug(), but that's not what it is. It is a problem of delayed initialisation of Linux AIO that may in the future affect other operations as well.

* This approch would need a fix in every device that uses a problematic operation. You came across virtio + blk_io_plug(), but that are probably not the only cases in the long run, which would make the code spread much wider than it should.

* There is only a single block driver that actually implements the new callback. This is a sign that this is not a generally useful callback.

Instead, the fix should be done locally in the file-posix driver, and the virtio devices shouldn't be touched at all. I think it would be good enough to call laio_init() when attaching to a new AioContext and to switch to the thread pool if it fails, like you already do. Maybe an error_report() would be appropriate to log the fact that we're not using the requested AIO mode.

Kevin

#3 lore raw

Hi Kevin,

Quoted 33 lines · Kevin Wolf

Am 15.06.2018 um 01:21 hat Nishanth Aravamudan geschrieben:

Quoted 10 lines

laio_init() can fail for a couple of reasons, which will lead to a NULL pointer dereference in laio_attach_aio_context().

To solve this, add a aio_linux_aio_setup() path which is called where aio_get_linux_aio() is called currently, but can propogate errors up.

virtio-block and virtio-scsi call this new function before calling blk_io_plug() (which eventually calls aio_get_linux_aio). This is necessary because plug/unplug currently assume they do not fail.

Quoted 7 lines

It is trivial to make qemu segfault in my testing. Set /proc/sys/fs/aio-max-nr to 0 and start a guest with aio=native,cache=directsync. With this patch, the guest successfully starts (but obviously isn't using native AIO). Setting aio-max-nr back up to a reasonable value, AIO contexts are consumed normally.

This is not a reasonable fix for several reasons:

* You frame this as a problem of blk_io_plug(), but that's not what it is. It is a problem of delayed initialisation of Linux AIO that may in the future affect other operations as well.

* This approch would need a fix in every device that uses a problematic operation. You came across virtio + blk_io_plug(), but that are probably not the only cases in the long run, which would make the code spread much wider than it should.

* There is only a single block driver that actually implements the new callback. This is a sign that this is not a generally useful callback.

Quoted 6 lines

Instead, the fix should be done locally in the file-posix driver, and the virtio devices shouldn't be touched at all. I think it would be good enough to call laio_init() when attaching to a new AioContext and to switch to the thread pool if it fails, like you already do. Maybe an error_report() would be appropriate to log the fact that we're not using the requested AIO mode.

Thank you for the constructive feedback! I will work on a v2 ASAP.

-Nish

#4 lore raw

RFC PATCH v2 1/1 aio: properly bubble up errors from initialization

In reply to Nishanth Aravamudan

laio_init() can fail for a couple of reasons, which will lead to a NULL pointer dereference in laio_attach_aio_context().

To solve this, add a aio_setup_linux_aio() function which is called before aio_get_linux_aio() where it is called currently, and which propogates setup errors up. The signature of aio_get_linux_aio() was not modified, because it seems preferable to return the actual errno from the possible failing initialization calls.

With respect to the error-handling in the file-posix.c, we properly bubble any errors up in raw_co_prw and in the case s of raw_aio_{,un}plug, the result is the same as if s->use_linux_aio was not set (but there is no bubbling up). In all three cases, if the setup function fails, we fallback to the thread pool and an error message is emitted.

It is trivial to make qemu segfault in my testing. Set /proc/sys/fs/aio-max-nr to 0 and start a guest with aio=native,cache=directsync. With this patch, the guest successfully starts (but obviously isn't using native AIO). Setting aio-max-nr back up to a reasonable value, AIO contexts are consumed normally.

---

Changes from v1 -> v2:

Rather than affect virtio-scsi/blk at all, make all the changes internal to file-posix.c. Thanks to Kevin Wolf for the suggested change.

---
 block/file-posix.c      | 24 ++++++++++++++++++++++++
 block/linux-aio.c       | 15 ++++++++++-----
 include/block/aio.h     |  3 +++
 include/block/raw-aio.h |  2 +-
 stubs/linux-aio.c       |  2 +-
 util/async.c            | 15 ++++++++++++---
 6 files changed, 51 insertions(+), 10 deletions(-)
block/file-posix.c +24 -0
index 07bb061fe4..2415d09bf1 100644
@@ -1665,6 +1665,14 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,
             type |= QEMU_AIO_MISALIGNED;
 #ifdef CONFIG_LINUX_AIO
         } else if (s->use_linux_aio) {
+            int rc;
+            rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                error_report("Unable to use native AIO, falling back to "
+                             "thread pool.");
+                s->use_linux_aio = 0;
+                return rc;
+            }
             LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
             assert(qiov->size == bytes);
             return laio_co_submit(bs, aio, s->fd, offset, qiov, type);
@@ -1695,6 +1703,14 @@ static void raw_aio_plug(BlockDriverState *bs)
 #ifdef CONFIG_LINUX_AIO
     BDRVRawState *s = bs->opaque;
     if (s->use_linux_aio) {
+        int rc;
+        rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+        if (rc != 0) {
+            error_report("Unable to use native AIO, falling back to "
+                         "thread pool.");
+            s->use_linux_aio = 0;
+            return;
+        }
         LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
         laio_io_plug(bs, aio);
     }
@@ -1706,6 +1722,14 @@ static void raw_aio_unplug(BlockDriverState *bs)
 #ifdef CONFIG_LINUX_AIO
     BDRVRawState *s = bs->opaque;
     if (s->use_linux_aio) {
+        int rc;
+        rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+        if (rc != 0) {
+            error_report("Unable to use native AIO, falling back to "
+                         "thread pool.");
+            s->use_linux_aio = 0;
+            return;
+        }
         LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
         laio_io_unplug(bs, aio);
     }
block/linux-aio.c +10 -5
index 88b8d55ec7..4d799f85fe 100644
@@ -470,28 +470,33 @@ void laio_attach_aio_context(LinuxAioState *s, AioContext *new_context)
                            qemu_laio_poll_cb);
 }
 
-LinuxAioState *laio_init(void)
+int laio_init(LinuxAioState **linux_aio)
 {
+    int rc;
     LinuxAioState *s;
 
     s = g_malloc0(sizeof(*s));
-    if (event_notifier_init(&s->e, false) < 0) {
+    rc = event_notifier_init(&s->e, false);
+    if (rc < 0) {
         goto out_free_state;
     }
 
-    if (io_setup(MAX_EVENTS, &s->ctx) != 0) {
+    rc = io_setup(MAX_EVENTS, &s->ctx);
+    if (rc != 0) {
         goto out_close_efd;
     }
 
     ioq_init(&s->io_q);
 
-    return s;
+    *linux_aio = s;
+    return 0;
 
 out_close_efd:
     event_notifier_cleanup(&s->e);
 out_free_state:
     g_free(s);
-    return NULL;
+    *linux_aio = NULL;
+    return rc;
 }
 
 void laio_cleanup(LinuxAioState *s)
include/block/aio.h +3 -0
index ae6f354e6c..8900516ac5 100644
@@ -381,6 +381,9 @@ GSource *aio_get_g_source(AioContext *ctx);
 /* Return the ThreadPool bound to this AioContext */
 struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
 
+/* Setup the LinuxAioState bound to this AioContext */
+int aio_setup_linux_aio(AioContext *ctx);
+
 /* Return the LinuxAioState bound to this AioContext */
 struct LinuxAioState *aio_get_linux_aio(AioContext *ctx);
 
include/block/raw-aio.h +1 -1
index 0e717fd475..81b90e5fc6 100644
@@ -43,7 +43,7 @@
 /* linux-aio.c - Linux native implementation */
 #ifdef CONFIG_LINUX_AIO
 typedef struct LinuxAioState LinuxAioState;
-LinuxAioState *laio_init(void);
+int laio_init(LinuxAioState **linux_aio);
 void laio_cleanup(LinuxAioState *s);
 int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, int fd,
                                 uint64_t offset, QEMUIOVector *qiov, int type);
stubs/linux-aio.c +1 -1
index ed47bd443c..88ab927e35 100644
@@ -21,7 +21,7 @@ void laio_attach_aio_context(LinuxAioState *s, AioContext *new_context)
     abort();
 }
 
-LinuxAioState *laio_init(void)
+int laio_init(LinuxAioState **linux_aio)
 {
     abort();
 }
util/async.c +12 -3
index 03f62787f2..b3fb67af3c 100644
@@ -323,12 +323,21 @@ ThreadPool *aio_get_thread_pool(AioContext *ctx)
 }
 
 #ifdef CONFIG_LINUX_AIO
-LinuxAioState *aio_get_linux_aio(AioContext *ctx)
+int aio_setup_linux_aio(AioContext *ctx)
 {
+    int rc;
+    rc = 0;
     if (!ctx->linux_aio) {
-        ctx->linux_aio = laio_init();
-        laio_attach_aio_context(ctx->linux_aio, ctx);
+        rc = laio_init(&ctx->linux_aio);
+        if (rc == 0) {
+            laio_attach_aio_context(ctx->linux_aio, ctx);
+        }
     }
+    return rc;
+}
+
+LinuxAioState *aio_get_linux_aio(AioContext *ctx)
+{
     return ctx->linux_aio;
 }
 #endif
Signature · 1 line
2.17.1
#5 lore raw
Quoted 6 lines · Nishanth Aravamudan via Qemu-devel

laio_init() can fail for a couple of reasons, which will lead to a NULL pointer dereference in laio_attach_aio_context().

To solve this, add a aio_setup_linux_aio() function which is called before aio_get_linux_aio() where it is called currently, and which propogates setup errors up. The signature of aio_get_linux_aio() was not

s/propogates/propagates/

Quoted 16 lines

modified, because it seems preferable to return the actual errno from the possible failing initialization calls.

With respect to the error-handling in the file-posix.c, we properly bubble any errors up in raw_co_prw and in the case s of raw_aio_{,un}plug, the result is the same as if s->use_linux_aio was not set (but there is no bubbling up). In all three cases, if the setup function fails, we fallback to the thread pool and an error message is emitted.

It is trivial to make qemu segfault in my testing. Set /proc/sys/fs/aio-max-nr to 0 and start a guest with aio=native,cache=directsync. With this patch, the guest successfully starts (but obviously isn't using native AIO). Setting aio-max-nr back up to a reasonable value, AIO contexts are consumed normally.

Quoted 5 lines

---

Changes from v1 -> v2:

When posting a v2, it's best to post as a new thread, rather than in-reply-to the v1 thread, so that automated tooling knows to check the new patch. More patch submission tips at https://wiki.qemu.org/Contribute/SubmitAPatch

Quoted 17 lines

Rather than affect virtio-scsi/blk at all, make all the changes internal to file-posix.c. Thanks to Kevin Wolf for the suggested change.

---
  block/file-posix.c      | 24 ++++++++++++++++++++++++
  block/linux-aio.c       | 15 ++++++++++-----
  include/block/aio.h     |  3 +++
  include/block/raw-aio.h |  2 +-
  stubs/linux-aio.c       |  2 +-
  util/async.c            | 15 ++++++++++++---
  6 files changed, 51 insertions(+), 10 deletions(-)
block/file-posix.c +0 -0
index 07bb061fe4..2415d09bf1 100644

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1672

Quoted 8 lines
block/file-posix.c +5 -0
@@ -1665,6 +1665,14 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,
              type |= QEMU_AIO_MISALIGNED;
  #ifdef CONFIG_LINUX_AIO
          } else if (s->use_linux_aio) {
+            int rc;
+            rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                error_report("Unable to use native AIO, falling back to "
+                             "thread pool.");

In general, error_report() should not output a trailing '.'.

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1674

Quoted 2 lines
block/file-posix.c +2 -0
+                s->use_linux_aio = 0;
+                return rc;

Wait - the message claims we are falling back, but the non-zero return code sounds like we are returning an error instead of falling back. (My preference - if the user requested something and we can't do it, it's better to error than to fall back to something that does not match the user's request).

Quoted 5 lines
block/file-posix.c +1 -0
+            }
              LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
              assert(qiov->size == bytes);
              return laio_co_submit(bs, aio, s->fd, offset, qiov, type);

block/file-posix.c static void raw_aio_plug(BlockDriverState *bs) · line 1711

Quoted 9 lines
block/file-posix.c +6 -0
@@ -1695,6 +1703,14 @@ static void raw_aio_plug(BlockDriverState *bs)
  #ifdef CONFIG_LINUX_AIO
      BDRVRawState *s = bs->opaque;
      if (s->use_linux_aio) {
+        int rc;
+        rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+        if (rc != 0) {
+            error_report("Unable to use native AIO, falling back to "
+                         "thread pool.");
+            s->use_linux_aio = 0;

Should s->use_linux_aio be a bool instead of an int?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#6 lore raw
Quoted 9 lines · Eric Blake
Quoted 6 lines · Nishanth Aravamudan via Qemu-devel

laio_init() can fail for a couple of reasons, which will lead to a NULL pointer dereference in laio_attach_aio_context().

To solve this, add a aio_setup_linux_aio() function which is called before aio_get_linux_aio() where it is called currently, and which propogates setup errors up. The signature of aio_get_linux_aio() was not

s/propogates/propagates/

Thanks!

Quoted 18 lines
Quoted 18 lines

modified, because it seems preferable to return the actual errno from the possible failing initialization calls.

With respect to the error-handling in the file-posix.c, we properly bubble any errors up in raw_co_prw and in the case s of raw_aio_{,un}plug, the result is the same as if s->use_linux_aio was not set (but there is no bubbling up). In all three cases, if the setup function fails, we fallback to the thread pool and an error message is emitted.

It is trivial to make qemu segfault in my testing. Set /proc/sys/fs/aio-max-nr to 0 and start a guest with aio=native,cache=directsync. With this patch, the guest successfully starts (but obviously isn't using native AIO). Setting aio-max-nr back up to a reasonable value, AIO contexts are consumed normally.

Quoted 8 lines
Quoted 3 lines

---

Changes from v1 -> v2:

When posting a v2, it's best to post as a new thread, rather than in-reply-to the v1 thread, so that automated tooling knows to check the new patch. More patch submission tips at
https://wiki.qemu.org/Contribute/SubmitAPatch

My apologies! I'll fix this in a (future) v3.

Quoted 11 lines
Quoted 11 lines

Rather than affect virtio-scsi/blk at all, make all the changes internal to file-posix.c. Thanks to Kevin Wolf for the suggested change.

---
  block/file-posix.c      | 24 ++++++++++++++++++++++++
  block/linux-aio.c       | 15 ++++++++++-----
  include/block/aio.h     |  3 +++
  include/block/raw-aio.h |  2 +-
  stubs/linux-aio.c       |  2 +-
  util/async.c            | 15 ++++++++++++---
  6 files changed, 51 insertions(+), 10 deletions(-)
Quoted 15 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1672

Quoted 13 lines
block/file-posix.c +5 -0
index 07bb061fe4..2415d09bf1 100644
@@ -1665,6 +1665,14 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,
              type |= QEMU_AIO_MISALIGNED;
  #ifdef CONFIG_LINUX_AIO
          } else if (s->use_linux_aio) {
+            int rc;
+            rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                error_report("Unable to use native AIO, falling back to "
+                             "thread pool.");

In general, error_report() should not output a trailing '.'.

Will fix.

Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1674

Quoted 2 lines
block/file-posix.c +2 -0
+                s->use_linux_aio = 0;
+                return rc;

Wait - the message claims we are falling back, but the non-zero return code sounds like we are returning an error instead of falling back. (My preference - if the user requested something and we can't do it, it's better to error than to fall back to something that does not match the user's request).

I think that makes sense, I hadn't tested this specific case (in my reading of the code, it wasn't clear to me if raw_co_prw() could be called before raw_aio_plug() had been called, but I think returning the error code up should be handled correctly. What about the cases where there is no error handling (the other two changes in the patch)?

Quoted 8 lines
Quoted 8 lines
block/file-posix.c +1 -0
+            }
              LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
              assert(qiov->size == bytes);
              return laio_co_submit(bs, aio, s->fd, offset, qiov, type);
@@ -1695,6 +1703,14 @@ static void raw_aio_plug(BlockDriverState *bs)
  #ifdef CONFIG_LINUX_AIO
      BDRVRawState *s = bs->opaque;
      if (s->use_linux_aio) {
Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1673

Quoted 6 lines
block/file-posix.c +6 -0
+        int rc;
+        rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+        if (rc != 0) {
+            error_report("Unable to use native AIO, falling back to "
+                         "thread pool.");
+            s->use_linux_aio = 0;

Should s->use_linux_aio be a bool instead of an int?

It is:

    bool use_linux_aio:1;

would you prefer I did a preparatory patch that converted users to true/false?

Thanks,
Nish

#7 lore raw
Quoted 2 lines · Nishanth Aravamudan
Quoted 1 line · Eric Blake

On 06/15/2018 12:47 PM, Nishanth Aravamudan via Qemu-devel wrote:

<snip>

Quoted 20 lines
Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1672

Quoted 6 lines
          } else if (s->use_linux_aio) {
block/file-posix.c +5 -0
+            int rc;
+            rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                error_report("Unable to use native AIO, falling back to "
+                             "thread pool.");

In general, error_report() should not output a trailing '.'.

Will fix.

Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1674

Quoted 2 lines
block/file-posix.c +2 -0
+                s->use_linux_aio = 0;
+                return rc;

Wait - the message claims we are falling back, but the non-zero return code sounds like we are returning an error instead of falling back. (My preference - if the user requested something and we can't do it, it's better to error than to fall back to something that does not match the user's request).

Quoted 5 lines

I think that makes sense, I hadn't tested this specific case (in my reading of the code, it wasn't clear to me if raw_co_prw() could be called before raw_aio_plug() had been called, but I think returning the error code up should be handled correctly. What about the cases where there is no error handling (the other two changes in the patch)?

While looking at doing these changes, I realized that I'm not quite sure what the right approach is here. My original rationale for returning non-zero was that AIO was requested but could not be completed. I haven't fully tracked back the calling paths, but I assumed it would get retried at the top level, and since we indicated to not use AIO on subsequent calls, it will succeed and use threads then (note, that I do now realize this means a mismatch between the qemu command-line and the in-use AIO model).

In practice, with my v2 patch, where I do return a non-zero error-code from this function, qemu does not exit (nor is any logging other than that I added emitted on the monitor). If I do not fallback, I imagine we would just continuously see this error message and IO might not actually every occur? Reworking all of the callpath to fail on non-zero returns from raw_co_prw() seems like a fair bit of work, but if that is what is being requested, I can try that (it will just take a while). Alternatively, I can produce a v3 quickly that does not bubble the actual errno all the way up (since it does seem like it is ignored anyways?).

<snip>

Quoted 10 lines
Quoted 3 lines
Quoted 1 line

+ s->use_linux_aio = 0;

Should s->use_linux_aio be a bool instead of an int?

It is:

    bool use_linux_aio:1;

would you prefer I did a preparatory patch that converted users to true/false?

Sorry, I misunderstood this -- only my patch does an assignment, so I'll switch to 'false'.

Thanks,
Nish

#8 lore raw
Quoted 41 lines · Nishanth Aravamudan
Quoted 2 lines · Nishanth Aravamudan
Quoted 1 line · Eric Blake

On 06/15/2018 12:47 PM, Nishanth Aravamudan via Qemu-devel wrote:

<snip>

Quoted 20 lines
Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1672

Quoted 6 lines
          } else if (s->use_linux_aio) {
block/file-posix.c +5 -0
+            int rc;
+            rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                error_report("Unable to use native AIO, falling back to "
+                             "thread pool.");

In general, error_report() should not output a trailing '.'.

Will fix.

Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1674

Quoted 2 lines
block/file-posix.c +2 -0
+                s->use_linux_aio = 0;
+                return rc;

Wait - the message claims we are falling back, but the non-zero return code sounds like we are returning an error instead of falling back. (My preference - if the user requested something and we can't do it, it's better to error than to fall back to something that does not match the user's request).

Quoted 5 lines

I think that makes sense, I hadn't tested this specific case (in my reading of the code, it wasn't clear to me if raw_co_prw() could be called before raw_aio_plug() had been called, but I think returning the error code up should be handled correctly. What about the cases where there is no error handling (the other two changes in the patch)?

While looking at doing these changes, I realized that I'm not quite sure what the right approach is here. My original rationale for returning non-zero was that AIO was requested but could not be completed. I haven't fully tracked back the calling paths, but I assumed it would get retried at the top level, and since we indicated to not use AIO on subsequent calls, it will succeed and use threads then (note, that I do now realize this means a mismatch between the qemu command-line and the in-use AIO model).

Quoted 10 lines

In practice, with my v2 patch, where I do return a non-zero error-code from this function, qemu does not exit (nor is any logging other than that I added emitted on the monitor). If I do not fallback, I imagine we would just continuously see this error message and IO might not actually every occur? Reworking all of the callpath to fail on non-zero returns from raw_co_prw() seems like a fair bit of work, but if that is what is being requested, I can try that (it will just take a while). Alternatively, I can produce a v3 quickly that does not bubble the actual errno all the way up (since it does seem like it is ignored anyways?).

Sorry for the noise, but I had one more thought. Would it be appropriate to push the _setup() call up to when we parse the arguments about aio=native? E.g., we already check there if cache=directsync is specified and error out if not. We could, in theory, also call laio_init() there (via the new function) and error out to the CLI if that fails. Then the runtime paths would simply be able to use the context that was setup earlier? I would need to verify the laio_cleanup() happens correctly still.

Thanks,
Nish

#9 lore raw

Am 20.06.2018 um 00:54 hat Nishanth Aravamudan geschrieben:

Quoted 53 lines · Nishanth Aravamudan
Quoted 41 lines · Nishanth Aravamudan
Quoted 2 lines · Eric Blake
Quoted 1 line · Nishanth Aravamudan via Qemu-devel

<snip>

Quoted 20 lines
Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1672

Quoted 6 lines
          } else if (s->use_linux_aio) {
block/file-posix.c +5 -0
+            int rc;
+            rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                error_report("Unable to use native AIO, falling back to "
+                             "thread pool.");

In general, error_report() should not output a trailing '.'.

Will fix.

Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1674

Quoted 2 lines
block/file-posix.c +2 -0
+                s->use_linux_aio = 0;
+                return rc;

Wait - the message claims we are falling back, but the non-zero return code sounds like we are returning an error instead of falling back. (My preference - if the user requested something and we can't do it, it's better to error than to fall back to something that does not match the user's request).

Quoted 5 lines

I think that makes sense, I hadn't tested this specific case (in my reading of the code, it wasn't clear to me if raw_co_prw() could be called before raw_aio_plug() had been called, but I think returning the error code up should be handled correctly. What about the cases where there is no error handling (the other two changes in the patch)?

While looking at doing these changes, I realized that I'm not quite sure what the right approach is here. My original rationale for returning non-zero was that AIO was requested but could not be completed. I haven't fully tracked back the calling paths, but I assumed it would get retried at the top level, and since we indicated to not use AIO on subsequent calls, it will succeed and use threads then (note, that I do now realize this means a mismatch between the qemu command-line and the in-use AIO model).

Quoted 10 lines

In practice, with my v2 patch, where I do return a non-zero error-code from this function, qemu does not exit (nor is any logging other than that I added emitted on the monitor). If I do not fallback, I imagine we would just continuously see this error message and IO might not actually every occur? Reworking all of the callpath to fail on non-zero returns from raw_co_prw() seems like a fair bit of work, but if that is what is being requested, I can try that (it will just take a while). Alternatively, I can produce a v3 quickly that does not bubble the actual errno all the way up (since it does seem like it is ignored anyways?).

Quoted 4 lines

Sorry for the noise, but I had one more thought. Would it be appropriate to push the _setup() call up to when we parse the arguments about aio=native? E.g., we already check there if cache=directsync is specified and error out if not.

We already do this:

     /* Currently Linux does AIO only for files opened with O_DIRECT */
    if (s->use_linux_aio && !(s->open_flags & O_DIRECT)) {
        error_setg(errp, "aio=native was specified, but it requires "
                         "cache.direct=on, which was not specified.");
        ret = -EINVAL;
        goto fail;
    }

laio_init() is about other types of errors. But anyway, yes, calling laio_init() already in .bdrv_open() is possible. Returning errors from .bdrv_open() is nice and easy and we should do it.

However, we may also need to call laio_init() again when switching to a different I/O thread after the image is already opened. This is what I meant when I commented on v1 that you should do this in the .bdrv_attach_aio_context callback. The problem here is that we can't return an error there and the guest is already using the image. In this case, logging an error and falling back to the thread pool seems to be the best option we have.

Kevin

#10 lore raw
Quoted 54 lines · Kevin Wolf

Am 20.06.2018 um 00:54 hat Nishanth Aravamudan geschrieben:

Quoted 53 lines · Nishanth Aravamudan
Quoted 51 lines · Nishanth Aravamudan
Quoted 2 lines · Eric Blake
Quoted 1 line · Nishanth Aravamudan via Qemu-devel

<snip>

Quoted 20 lines
Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1672

Quoted 6 lines
          } else if (s->use_linux_aio) {
block/file-posix.c +5 -0
+            int rc;
+            rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                error_report("Unable to use native AIO, falling back to "
+                             "thread pool.");

In general, error_report() should not output a trailing '.'.

Will fix.

Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, · line 1674

Quoted 2 lines
block/file-posix.c +2 -0
+                s->use_linux_aio = 0;
+                return rc;

Wait - the message claims we are falling back, but the non-zero return code sounds like we are returning an error instead of falling back. (My preference - if the user requested something and we can't do it, it's better to error than to fall back to something that does not match the user's request).

Quoted 5 lines

I think that makes sense, I hadn't tested this specific case (in my reading of the code, it wasn't clear to me if raw_co_prw() could be called before raw_aio_plug() had been called, but I think returning the error code up should be handled correctly. What about the cases where there is no error handling (the other two changes in the patch)?

While looking at doing these changes, I realized that I'm not quite sure what the right approach is here. My original rationale for returning non-zero was that AIO was requested but could not be completed. I haven't fully tracked back the calling paths, but I assumed it would get retried at the top level, and since we indicated to not use AIO on subsequent calls, it will succeed and use threads then (note, that I do now realize this means a mismatch between the qemu command-line and the in-use AIO model).

In practice, with my v2 patch, where I do return a non-zero error-code from this function, qemu does not exit (nor is any logging other than that I added emitted on the monitor). If I do not fallback, I imagine we would just continuously see this error message and IO might not actually every occur? Reworking all of the callpath to fail on non-zero returns from raw_co_prw() seems like a fair bit of work, but if that is what is being requested, I can try that (it will just take a while). Alternatively, I can produce a v3 quickly that does not bubble the actual errno all the way up (since it does seem like it is ignored anyways?).

Quoted 6 lines
Quoted 4 lines

Sorry for the noise, but I had one more thought. Would it be appropriate to push the _setup() call up to when we parse the arguments about aio=native? E.g., we already check there if cache=directsync is specified and error out if not.

We already do this:

Right, I stated above it already is done, I simply meant adding a second check here that we can obtain and setup the AIO context successfully.

Quoted 11 lines
     /* Currently Linux does AIO only for files opened with O_DIRECT */
    if (s->use_linux_aio && !(s->open_flags & O_DIRECT)) {
        error_setg(errp, "aio=native was specified, but it requires "
                         "cache.direct=on, which was not specified.");
        ret = -EINVAL;
        goto fail;
    }

laio_init() is about other types of errors. But anyway, yes, calling laio_init() already in .bdrv_open() is possible. Returning errors from .bdrv_open() is nice and easy and we should do it.

Ack.

Quoted 7 lines

However, we may also need to call laio_init() again when switching to a different I/O thread after the image is already opened. This is what I meant when I commented on v1 that you should do this in the .bdrv_attach_aio_context callback. The problem here is that we can't return an error there and the guest is already using the image. In this case, logging an error and falling back to the thread pool seems to be the best option we have.

Is this is a request for new functionality? Just trying to understand, because aiui, block/file-posix.c does not implement the bdrv_attach_aio_context callback currently. Instead, aio_get_linux_aio() is called from three places, raw_co_prw, raw_aio_plug and raw_aio_unplug, which calls into laio_init() and laio_attach_aio_context(). I can add the callback you suggest with appropriate error handling (I suppose it would point to laio_attach_aio_context, possibly with some modifications) and remove the call from aio_get_linux_aio()? Just trying to understand the request a bit better, as I don't see where exactly iothreads get switched and how that is implemented currently (and thus where laio_init() would get called again in the current code).

Thanks in advance!
-Nish

#11 lore raw
Quoted 87 lines · Nishanth Aravamudan
Quoted 54 lines · Kevin Wolf

Am 20.06.2018 um 00:54 hat Nishanth Aravamudan geschrieben:

Quoted 53 lines · Nishanth Aravamudan
Quoted 51 lines · Nishanth Aravamudan
Quoted 2 lines · Eric Blake
Quoted 1 line · Nishanth Aravamudan via Qemu-devel

<snip>

Quoted 20 lines
Quoted 8 lines

Review

Quoted 6 lines
          } else if (s->use_linux_aio) {
(quoted diff) +5 -0
+            int rc;
+            rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                error_report("Unable to use native AIO, falling back to "
+                             "thread pool.");

In general, error_report() should not output a trailing '.'.

Will fix.

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                s->use_linux_aio = 0;
+                return rc;

Wait - the message claims we are falling back, but the non-zero return code sounds like we are returning an error instead of falling back. (My preference - if the user requested something and we can't do it, it's better to error than to fall back to something that does not match the user's request).

Quoted 5 lines

I think that makes sense, I hadn't tested this specific case (in my reading of the code, it wasn't clear to me if raw_co_prw() could be called before raw_aio_plug() had been called, but I think returning the error code up should be handled correctly. What about the cases where there is no error handling (the other two changes in the patch)?

While looking at doing these changes, I realized that I'm not quite sure what the right approach is here. My original rationale for returning non-zero was that AIO was requested but could not be completed. I haven't fully tracked back the calling paths, but I assumed it would get retried at the top level, and since we indicated to not use AIO on subsequent calls, it will succeed and use threads then (note, that I do now realize this means a mismatch between the qemu command-line and the in-use AIO model).

In practice, with my v2 patch, where I do return a non-zero error-code from this function, qemu does not exit (nor is any logging other than that I added emitted on the monitor). If I do not fallback, I imagine we would just continuously see this error message and IO might not actually every occur? Reworking all of the callpath to fail on non-zero returns from raw_co_prw() seems like a fair bit of work, but if that is what is being requested, I can try that (it will just take a while). Alternatively, I can produce a v3 quickly that does not bubble the actual errno all the way up (since it does seem like it is ignored anyways?).

Quoted 6 lines
Quoted 4 lines

Sorry for the noise, but I had one more thought. Would it be appropriate to push the _setup() call up to when we parse the arguments about aio=native? E.g., we already check there if cache=directsync is specified and error out if not.

We already do this:

Right, I stated above it already is done, I simply meant adding a second check here that we can obtain and setup the AIO context successfully.

Quoted 11 lines
     /* Currently Linux does AIO only for files opened with O_DIRECT */
    if (s->use_linux_aio && !(s->open_flags & O_DIRECT)) {
        error_setg(errp, "aio=native was specified, but it requires "
                         "cache.direct=on, which was not specified.");
        ret = -EINVAL;
        goto fail;
    }

laio_init() is about other types of errors. But anyway, yes, calling laio_init() already in .bdrv_open() is possible. Returning errors from .bdrv_open() is nice and easy and we should do it.

Ack.

Quoted 7 lines

However, we may also need to call laio_init() again when switching to a different I/O thread after the image is already opened. This is what I meant when I commented on v1 that you should do this in the .bdrv_attach_aio_context callback. The problem here is that we can't return an error there and the guest is already using the image. In this case, logging an error and falling back to the thread pool seems to be the best option we have.

Quoted 12 lines

Is this is a request for new functionality? Just trying to understand, because aiui, block/file-posix.c does not implement the bdrv_attach_aio_context callback currently. Instead, aio_get_linux_aio() is called from three places, raw_co_prw, raw_aio_plug and raw_aio_unplug, which calls into laio_init() and laio_attach_aio_context(). I can add the callback you suggest with appropriate error handling (I suppose it would point to laio_attach_aio_context, possibly with some modifications) and remove the call from aio_get_linux_aio()? Just trying to understand the request a bit better, as I don't see where exactly iothreads get switched and how that is implemented currently (and thus where laio_init() would get called again in the current code).

While I waited for a reply to this, I started coding on what I think was being asked for and have come to the conclusion that there are actually three bugs here :)

Test cases (with one disk attached to the VM):

1) Set /proc/sys/fs/max-aio-nr to 0. Specify aio=native and qemu dies with a SIGSEGV.
- This case is understood and pushing the laio_init()-return code check to the bdrv_open() path fixes this (and allows for the failure to be communicated to the user).

2) Set /proc/sys/fs/max-aio-nr to 128. Specify aio=native and some number of IOThreads. Over qmp issue a x-blockdev-set-iothread command to move the block device node to one of the IOThreads. qemu eventually dies with a SIGSEGV.
- I am fairly sure this is the case you described above, and is fixed by re-implementing the bdrv_{attach,detach}_aio_context callbacks. I have a patch that does this and successfully tested the SEGV is avoided.

3) Set /proc/sys/fs/max-aio-nr to 512 (I think 256 would be sufficient, though). Specify aio=native and some number of IOThreads. Over qmp issue a x-blockdev-set-iothread command to move the block device node to one of the IOThreads. Shutdown the guest normally. qemu dies with a SIGABRT.
- This appears to be because there is a mismatch in aio_context_{acquire,release} calls (this is my hypothesis right now). The abort comes from bdrv_flush -> aio_context_release and an EPERM from qemu_mutex_unlock_impl() which I believe is just reflecting an EPERM from pthread_mutex_unlock? My theory is that the main qemu thread acquired the aio mutex but then the IOThread released it? I will try and trace the mutexes tomorrow, but I still don't have a fix for this case.

Thanks,
Nish

#12 lore raw

Am 21.06.2018 um 05:26 hat Nishanth Aravamudan geschrieben:

Quoted 61 lines · Nishanth Aravamudan
Quoted 60 lines · Kevin Wolf
Quoted 59 lines

Am 20.06.2018 um 00:54 hat Nishanth Aravamudan geschrieben:

Quoted 57 lines · Nishanth Aravamudan
Quoted 41 lines · Nishanth Aravamudan
Quoted 2 lines · Eric Blake
Quoted 1 line · Nishanth Aravamudan via Qemu-devel

<snip>

Quoted 20 lines
Quoted 8 lines

Review

Quoted 6 lines
          } else if (s->use_linux_aio) {
(quoted diff) +5 -0
+            int rc;
+            rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                error_report("Unable to use native AIO, falling back to "
+                             "thread pool.");

In general, error_report() should not output a trailing '.'.

Will fix.

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                s->use_linux_aio = 0;
+                return rc;

Wait - the message claims we are falling back, but the non-zero return code sounds like we are returning an error instead of falling back. (My preference - if the user requested something and we can't do it, it's better to error than to fall back to something that does not match the user's request).

Quoted 5 lines

I think that makes sense, I hadn't tested this specific case (in my reading of the code, it wasn't clear to me if raw_co_prw() could be called before raw_aio_plug() had been called, but I think returning the error code up should be handled correctly. What about the cases where there is no error handling (the other two changes in the patch)?

While looking at doing these changes, I realized that I'm not quite sure what the right approach is here. My original rationale for returning non-zero was that AIO was requested but could not be completed. I haven't fully tracked back the calling paths, but I assumed it would get retried at the top level, and since we indicated to not use AIO on subsequent calls, it will succeed and use threads then (note, that I do now realize this means a mismatch between the qemu command-line and the in-use AIO model).

Quoted 10 lines

In practice, with my v2 patch, where I do return a non-zero error-code from this function, qemu does not exit (nor is any logging other than that I added emitted on the monitor). If I do not fallback, I imagine we would just continuously see this error message and IO might not actually every occur? Reworking all of the callpath to fail on non-zero returns from raw_co_prw() seems like a fair bit of work, but if that is what is being requested, I can try that (it will just take a while). Alternatively, I can produce a v3 quickly that does not bubble the actual errno all the way up (since it does seem like it is ignored anyways?).

Sorry for the noise, but I had one more thought. Would it be appropriate to push the _setup() call up to when we parse the arguments about aio=native? E.g., we already check there if cache=directsync is specified and error out if not.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

We already do this:

Right, I stated above it already is done, I simply meant adding a second check here that we can obtain and setup the AIO context successfully.

Yes, sorry, I misread.

Quoted 22 lines
Quoted 22 lines
Quoted 11 lines
     /* Currently Linux does AIO only for files opened with O_DIRECT */
    if (s->use_linux_aio && !(s->open_flags & O_DIRECT)) {
        error_setg(errp, "aio=native was specified, but it requires "
                         "cache.direct=on, which was not specified.");
        ret = -EINVAL;
        goto fail;
    }

laio_init() is about other types of errors. But anyway, yes, calling laio_init() already in .bdrv_open() is possible. Returning errors from .bdrv_open() is nice and easy and we should do it.

Ack.

Quoted 7 lines

However, we may also need to call laio_init() again when switching to a different I/O thread after the image is already opened. This is what I meant when I commented on v1 that you should do this in the .bdrv_attach_aio_context callback. The problem here is that we can't return an error there and the guest is already using the image. In this case, logging an error and falling back to the thread pool seems to be the best option we have.

Quoted 12 lines
Quoted 4 lines

Is this is a request for new functionality? Just trying to understand, because aiui, block/file-posix.c does not implement the bdrv_attach_aio_context callback currently. Instead, aio_get_linux_aio() is called from three places, raw_co_prw, raw_aio_plug and

Quoted 8 lines

raw_aio_unplug, which calls into laio_init() and laio_attach_aio_context(). I can add the callback you suggest with appropriate error handling (I suppose it would point to laio_attach_aio_context, possibly with some modifications) and remove the call from aio_get_linux_aio()? Just trying to understand the request a bit better, as I don't see where exactly iothreads get switched and how that is implemented currently (and thus where laio_init() would get called again in the current code).

It's not new functionality, but moving things to a different place.

Our goal is that aio_get_linux_aio() never returns NULL in the I/O path so that we don't have to duplicate the error handling everywhere. For each AioContext, aio_get_linux_aio() can return NULL only the first time. Once it successfully returned a LinuxAioState, it will never return NULL again for the same AioContext.

You cover the QEMU main loop AioContext when you call aio_get_linux_aio() in raw_open_common(). But whenever the AioContext is changed, we can get NULL again on the next aio_get_linux_aio() call because we don't know whether that AioContext already successfully returned a LinuxAioState before.

If you call once it in .bdrv_attach_aio_context() and disable Linux AIO if it fails, you have centralised all the error paths that you currently need in all I/O paths.

Quoted 11 lines

While I waited for a reply to this, I started coding on what I think was being asked for and have come to the conclusion that there are actually three bugs here :)

Test cases (with one disk attached to the VM):

1) Set /proc/sys/fs/max-aio-nr to 0. Specify aio=native and qemu dies with a SIGSEGV.
- This case is understood and pushing the laio_init()-return code check to the bdrv_open() path fixes this (and allows for the failure to be communicated to the user).

Right.

Quoted 9 lines

2) Set /proc/sys/fs/max-aio-nr to 128. Specify aio=native and some number of IOThreads. Over qmp issue a x-blockdev-set-iothread command to move the block device node to one of the IOThreads. qemu eventually dies with a SIGSEGV.
- I am fairly sure this is the case you described above, and is fixed by re-implementing the bdrv_{attach,detach}_aio_context callbacks. I have a patch that does this and successfully tested the SEGV is avoided.

Quoted 12 lines

3) Set /proc/sys/fs/max-aio-nr to 512 (I think 256 would be sufficient, though). Specify aio=native and some number of IOThreads. Over qmp issue a x-blockdev-set-iothread command to move the block device node to one of the IOThreads. Shutdown the guest normally. qemu dies with a SIGABRT.
- This appears to be because there is a mismatch in aio_context_{acquire,release} calls (this is my hypothesis right now). The abort comes from bdrv_flush -> aio_context_release and an EPERM from qemu_mutex_unlock_impl() which I believe is just reflecting an EPERM from pthread_mutex_unlock? My theory is that the main qemu thread acquired the aio mutex but then the IOThread released it? I will try and trace the mutexes tomorrow, but I still don't have a fix for this case.

x-blockdev-set-iothread is not the proper way to achieve what you want. You can only correctly use it with images that are not attached to any guest device. If you move a block node to a different I/O thread (and therefore AioContext) under the feet of the guest device, crashes aren't completely unexpected.

The proper way to test this would be -device virtio-blk,iothread=... In this case, the raw BlockDriverState is first created in the main AioContext, but when the virtio-blk device is initialised, it moves it to the specified I/O thread.

Kevin

#13 lore raw
Quoted 118 lines · Kevin Wolf

Am 21.06.2018 um 05:26 hat Nishanth Aravamudan geschrieben:

Quoted 61 lines · Nishanth Aravamudan
Quoted 60 lines · Kevin Wolf
Quoted 59 lines

Am 20.06.2018 um 00:54 hat Nishanth Aravamudan geschrieben:

Quoted 57 lines · Nishanth Aravamudan
Quoted 41 lines · Nishanth Aravamudan
Quoted 2 lines · Eric Blake
Quoted 1 line · Nishanth Aravamudan via Qemu-devel

<snip>

Quoted 20 lines
Quoted 8 lines

Review

Quoted 6 lines
          } else if (s->use_linux_aio) {
(quoted diff) +5 -0
+            int rc;
+            rc = aio_setup_linux_aio(bdrv_get_aio_context(bs));
+            if (rc != 0) {
+                error_report("Unable to use native AIO, falling back to "
+                             "thread pool.");

In general, error_report() should not output a trailing '.'.

Will fix.

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                s->use_linux_aio = 0;
+                return rc;

Wait - the message claims we are falling back, but the non-zero return code sounds like we are returning an error instead of falling back. (My preference - if the user requested something and we can't do it, it's better to error than to fall back to something that does not match the user's request).

Quoted 5 lines

I think that makes sense, I hadn't tested this specific case (in my reading of the code, it wasn't clear to me if raw_co_prw() could be called before raw_aio_plug() had been called, but I think returning the error code up should be handled correctly. What about the cases where there is no error handling (the other two changes in the patch)?

While looking at doing these changes, I realized that I'm not quite sure what the right approach is here. My original rationale for returning non-zero was that AIO was requested but could not be completed. I haven't fully tracked back the calling paths, but I assumed it would get retried at the top level, and since we indicated to not use AIO on subsequent calls, it will succeed and use threads then (note, that I do now realize this means a mismatch between the qemu command-line and the in-use AIO model).

Quoted 10 lines

In practice, with my v2 patch, where I do return a non-zero error-code from this function, qemu does not exit (nor is any logging other than that I added emitted on the monitor). If I do not fallback, I imagine we would just continuously see this error message and IO might not actually every occur? Reworking all of the callpath to fail on non-zero returns from raw_co_prw() seems like a fair bit of work, but if that is what is being requested, I can try that (it will just take a while). Alternatively, I can produce a v3 quickly that does not bubble the actual errno all the way up (since it does seem like it is ignored anyways?).

Sorry for the noise, but I had one more thought. Would it be appropriate to push the _setup() call up to when we parse the arguments about aio=native? E.g., we already check there if cache=directsync is specified and error out if not.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

We already do this:

Right, I stated above it already is done, I simply meant adding a second check here that we can obtain and setup the AIO context successfully.

Yes, sorry, I misread.

Quoted 22 lines
Quoted 22 lines
Quoted 11 lines
     /* Currently Linux does AIO only for files opened with O_DIRECT */
    if (s->use_linux_aio && !(s->open_flags & O_DIRECT)) {
        error_setg(errp, "aio=native was specified, but it requires "
                         "cache.direct=on, which was not specified.");
        ret = -EINVAL;
        goto fail;
    }

laio_init() is about other types of errors. But anyway, yes, calling laio_init() already in .bdrv_open() is possible. Returning errors from .bdrv_open() is nice and easy and we should do it.

Ack.

Quoted 7 lines

However, we may also need to call laio_init() again when switching to a different I/O thread after the image is already opened. This is what I meant when I commented on v1 that you should do this in the .bdrv_attach_aio_context callback. The problem here is that we can't return an error there and the guest is already using the image. In this case, logging an error and falling back to the thread pool seems to be the best option we have.

Quoted 12 lines
Quoted 4 lines

Is this is a request for new functionality? Just trying to understand, because aiui, block/file-posix.c does not implement the bdrv_attach_aio_context callback currently. Instead, aio_get_linux_aio() is called from three places, raw_co_prw, raw_aio_plug and

Quoted 8 lines

raw_aio_unplug, which calls into laio_init() and laio_attach_aio_context(). I can add the callback you suggest with appropriate error handling (I suppose it would point to laio_attach_aio_context, possibly with some modifications) and remove the call from aio_get_linux_aio()? Just trying to understand the request a bit better, as I don't see where exactly iothreads get switched and how that is implemented currently (and thus where laio_init() would get called again in the current code).

It's not new functionality, but moving things to a different place.

Our goal is that aio_get_linux_aio() never returns NULL in the I/O path so that we don't have to duplicate the error handling everywhere. For each AioContext, aio_get_linux_aio() can return NULL only the first time. Once it successfully returned a LinuxAioState, it will never return NULL again for the same AioContext.

You cover the QEMU main loop AioContext when you call aio_get_linux_aio() in raw_open_common(). But whenever the AioContext is changed, we can get NULL again on the next aio_get_linux_aio() call because we don't know whether that AioContext already successfully returned a LinuxAioState before.

Quoted 3 lines

If you call once it in .bdrv_attach_aio_context() and disable Linux AIO if it fails, you have centralised all the error paths that you currently need in all I/O paths.

Thanks for the clarification!

Quoted 42 lines
Quoted 11 lines

While I waited for a reply to this, I started coding on what I think was being asked for and have come to the conclusion that there are actually three bugs here :)

Test cases (with one disk attached to the VM):

1) Set /proc/sys/fs/max-aio-nr to 0. Specify aio=native and qemu dies with a SIGSEGV.
- This case is understood and pushing the laio_init()-return code check to the bdrv_open() path fixes this (and allows for the failure to be communicated to the user).

Right.

Quoted 9 lines

2) Set /proc/sys/fs/max-aio-nr to 128. Specify aio=native and some number of IOThreads. Over qmp issue a x-blockdev-set-iothread command to move the block device node to one of the IOThreads. qemu eventually dies with a SIGSEGV.
- I am fairly sure this is the case you described above, and is fixed by re-implementing the bdrv_{attach,detach}_aio_context callbacks. I have a patch that does this and successfully tested the SEGV is avoided.

Quoted 12 lines

3) Set /proc/sys/fs/max-aio-nr to 512 (I think 256 would be sufficient, though). Specify aio=native and some number of IOThreads. Over qmp issue a x-blockdev-set-iothread command to move the block device node to one of the IOThreads. Shutdown the guest normally. qemu dies with a SIGABRT.
- This appears to be because there is a mismatch in aio_context_{acquire,release} calls (this is my hypothesis right now). The abort comes from bdrv_flush -> aio_context_release and an EPERM from qemu_mutex_unlock_impl() which I believe is just reflecting an EPERM from pthread_mutex_unlock? My theory is that the main qemu thread acquired the aio mutex but then the IOThread released it? I will try and trace the mutexes tomorrow, but I still don't have a fix for this case.

x-blockdev-set-iothread is not the proper way to achieve what you want. You can only correctly use it with images that are not attached to any guest device. If you move a block node to a different I/O thread (and therefore AioContext) under the feet of the guest device, crashes aren't completely unexpected.

Quoted 4 lines

The proper way to test this would be -device virtio-blk,iothread=... In this case, the raw BlockDriverState is first created in the main AioContext, but when the virtio-blk device is initialised, it moves it to the specified I/O thread.

Thanks again! Turns out 2) is definitely present in master. 3) does not occur. So I will work on making my patch into 2 patches and repost a v3 later today.

Thanks,
Nish