Skip to contentSkip to conversation

Block layer patches

GIT PULL Kevin Wolf 5 participants 31 messages Sep 27, 2016 – Oct 7, 2016 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

The following changes since commit 7cfdc02dae0d2ff58c897496cfdbbafc0eda0f3f:

Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging (2016-09-26 19:47:00 +0100)

are available in the git repository at:

git://repo.or.cz/qemu/kevin.git tags/for-upstream

for you to fetch changes up to 3b856cebe5e93547852c156ca2119d075e62aed7:

coroutine: reduce stack size to 60kB (2016-09-27 14:05:21 +0200)

----------------------------------------------------------------
Block layer patches

----------------------------------------------------------------
John Snow (3):
block: reintroduce bdrv_flush_all
qemu: use bdrv_flush_all for vm_stop et al
block-backend: remove blk_flush_all

Kevin Wolf (8):
block: Fix error path in qmp_blockdev_change_medium()
block: Drop aio/cache consistency check from qmp_blockdev_add() block/qapi: Use separate options type for curl driver block/qapi: Move 'aio' option to file driver
block: Parse 'detect-zeroes' in bdrv_open_common()
block: Use 'detect-zeroes' option for 'blockdev-change-medium'
block: Move 'discard' option to bdrv_open_common()
block: Remove qemu_root_bds_opts

Peter Lieven (7):
oslib-posix: add helpers for stack alloc and free
coroutine-sigaltstack: rename coroutine struct appropriately
coroutine: add a macro for the coroutine stack size
coroutine-ucontext: use helper for allocating stack memory
coroutine-sigaltstack: use helper for allocating stack memory
oslib-posix: add a configure switch to debug stack usage
coroutine: reduce stack size to 60kB

 block.c                        |  50 +++++++++++++++++-
 block/block-backend.c          |  31 ++----------
 block/io.c                     |  25 +++++++++
 block/raw-posix.c              |  44 +++++++++-------
 block/raw-win32.c              |  56 +++++++++++++++++++--
 blockdev.c                     | 112 +++--------------------------------------
 configure                      |  19 +++++++
 cpus.c                         |   4 +-
 hw/i386/xen/xen_platform.c     |   2 -
 hw/ide/piix.c                  |   4 ++
 include/block/block.h          |   2 +
 include/qemu/coroutine_int.h   |   2 +
 include/sysemu/block-backend.h |   3 +-
 include/sysemu/os-posix.h      |  27 ++++++++++
 qapi/block-core.json           |  31 ++++++++----
 tests/qemu-iotests/087         |   4 +-
 tests/qemu-iotests/087.out     |   2 +-
 util/coroutine-sigaltstack.c   |  25 ++++-----
 util/coroutine-ucontext.c      |  11 ++--
 util/coroutine-win32.c         |   2 +-
 util/oslib-posix.c             |  77 ++++++++++++++++++++++++++++
 21 files changed, 342 insertions(+), 191 deletions(-)
author
#1 lore raw

[Qemu-devel] [PULL 05/18] block: Drop aio/cache consistency check from qmp_blockdev_add()

The TODO comment has been addressed a while ago and this is now checked in raw-posix, so we don't have to special case this in blockdev-add any more.

---
 blockdev.c                 | 15 ---------------
 tests/qemu-iotests/087.out |  2 +-
 2 files changed, 1 insertion(+), 16 deletions(-)
blockdev.c +0 -15
index 62d0dd0..7820f42 100644
@@ -3832,21 +3832,6 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
     QDict *qdict;
     Error *local_err = NULL;
 
-    /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
-     * cache.direct=false instead of silently switching to aio=threads, except
-     * when called from drive_new().
-     *
-     * For now, simply forbidding the combination for all drivers will do. */
-    if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
-        bool direct = options->has_cache &&
-                      options->cache->has_direct &&
-                      options->cache->direct;
-        if (!direct) {
-            error_setg(errp, "aio=native requires cache.direct=true");
-            goto fail;
-        }
-    }
-
     visit_type_BlockdevOptions(v, NULL, &options, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
tests/qemu-iotests/087.out +1 -1
index bef6862..cd02eae 100644
@@ -27,7 +27,7 @@ QMP_VERSION
 Testing:
 QMP_VERSION
 {"return": {}}
-{"error": {"class": "GenericError", "desc": "aio=native requires cache.direct=true"}}
+{"error": {"class": "GenericError", "desc": "aio=native was specified, but it requires cache.direct=on, which was not specified."}}
 {"return": {}}
 {"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "SHUTDOWN"}
 
Signature · 1 line
1.8.3.1
author
#2 lore raw

[Qemu-devel] [PULL 02/18] qemu: use bdrv_flush_all for vm_stop et al

In reply to Kevin Wolf

From: John Snow <[email protected]>

Reimplement bdrv_flush_all for vm_stop. In contrast to blk_flush_all, bdrv_flush_all does not have device model restrictions. This allows us to flush and halt unconditionally without error.

This allows us to do things like migrate when we have a device with an open tray, but has a node that may need to be flushed, or nodes that aren't currently attached to any device and need to be flushed.

Specifically, this allows us to migrate when we have a CDROM with an open tray.

---
 cpus.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
cpus.c +2 -2
index e39ccb7..96d9352 100644
@@ -751,7 +751,7 @@ static int do_vm_stop(RunState state)
     }
 
     bdrv_drain_all();
-    ret = blk_flush_all();
+    ret = bdrv_flush_all();
 
     return ret;
 }
@@ -1494,7 +1494,7 @@ int vm_stop_force_state(RunState state)
         bdrv_drain_all();
         /* Make sure to return an error if the flush in a previous vm_stop()
          * failed. */
-        return blk_flush_all();
+        return bdrv_flush_all();
     }
 }
 
Signature · 1 line
1.8.3.1
author
#3 lore raw

[Qemu-devel] [PULL 01/18] block: reintroduce bdrv_flush_all

In reply to Kevin Wolf

From: John Snow <[email protected]>

Commit fe1a9cbc moved the flush_all routine from the bdrv layer to the block-backend layer. In doing so, however, the semantics of the routine changed slightly such that flush_all now used blk_flush instead of bdrv_flush.

blk_flush can fail if the attached device model reports that it is not "available," (i.e. the tray is open.) This changed the semantics of flush_all such that it can now fail for e.g. open CDROM drives.

Reintroduce bdrv_flush_all to regain the old semantics without having to alter the behavior of blk_flush or blk_flush_all, which are already 'doing the right thing.'

---
 block/io.c            | 25 +++++++++++++++++++++++++
 include/block/block.h |  1 +
 2 files changed, 26 insertions(+)
block/io.c +25 -0
index fdf7080..57a2eeb 100644
@@ -1619,6 +1619,31 @@ int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset,
                            BDRV_REQ_ZERO_WRITE | flags);
 }
 
+/*
+ * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
+ */
+int bdrv_flush_all(void)
+{
+    BdrvNextIterator it;
+    BlockDriverState *bs = NULL;
+    int result = 0;
+
+    for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
+        AioContext *aio_context = bdrv_get_aio_context(bs);
+        int ret;
+
+        aio_context_acquire(aio_context);
+        ret = bdrv_flush(bs);
+        if (ret < 0 && !result) {
+            result = ret;
+        }
+        aio_context_release(aio_context);
+    }
+
+    return result;
+}
+
+
 typedef struct BdrvCoGetBlockStatusData {
     BlockDriverState *bs;
     BlockDriverState *base;
include/block/block.h +1 -0
index e18233a..811b060 100644
@@ -333,6 +333,7 @@ int bdrv_inactivate_all(void);
 /* Ensure contents are flushed to disk.  */
 int bdrv_flush(BlockDriverState *bs);
 int coroutine_fn bdrv_co_flush(BlockDriverState *bs);
+int bdrv_flush_all(void);
 void bdrv_close_all(void);
 void bdrv_drain(BlockDriverState *bs);
 void coroutine_fn bdrv_co_drain(BlockDriverState *bs);
Signature · 1 line
1.8.3.1
author
#4 lore raw

[Qemu-devel] [PULL 03/18] block-backend: remove blk_flush_all

In reply to Kevin Wolf

From: John Snow <[email protected]>

We can teach Xen to drain and flush each device as it needs to, instead of trying to flush ALL devices. This removes the last user of blk_flush_all.

The function is therefore removed under the premise that any new uses of blk_flush_all would be the wrong paradigm: either flush the single device that requires flushing, or use an appropriate flush_all mechanism from outside of the BlkBackend layer.

---
 block/block-backend.c          | 22 ----------------------
 hw/i386/xen/xen_platform.c     |  2 --
 hw/ide/piix.c                  |  4 ++++
 include/sysemu/block-backend.h |  1 -
 4 files changed, 4 insertions(+), 25 deletions(-)
block/block-backend.c +0 -22
index 0bd19ab..f34bad5 100644
@@ -1640,28 +1640,6 @@ int blk_commit_all(void)
     return 0;
 }
 
-int blk_flush_all(void)
-{
-    BlockBackend *blk = NULL;
-    int result = 0;
-
-    while ((blk = blk_all_next(blk)) != NULL) {
-        AioContext *aio_context = blk_get_aio_context(blk);
-        int ret;
-
-        aio_context_acquire(aio_context);
-        if (blk_is_inserted(blk)) {
-            ret = blk_flush(blk);
-            if (ret < 0 && !result) {
-                result = ret;
-            }
-        }
-        aio_context_release(aio_context);
-    }
-
-    return result;
-}
-
 
 /* throttling disk I/O limits */
 void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg)
hw/i386/xen/xen_platform.c +0 -2
index aa78393..f85635c 100644
@@ -134,8 +134,6 @@ static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t v
            devices, and bit 2 the non-primary-master IDE devices. */
         if (val & UNPLUG_ALL_IDE_DISKS) {
             DPRINTF("unplug disks\n");
-            blk_drain_all();
-            blk_flush_all();
             pci_unplug_disks(pci_dev->bus);
         }
         if (val & UNPLUG_ALL_NICS) {
hw/ide/piix.c +4 -0
index c190fca..d5777fd 100644
@@ -179,6 +179,10 @@ int pci_piix3_xen_ide_unplug(DeviceState *dev)
         if (di != NULL && !di->media_cd) {
             BlockBackend *blk = blk_by_legacy_dinfo(di);
             DeviceState *ds = blk_get_attached_dev(blk);
+
+            blk_drain(blk);
+            blk_flush(blk);
+
             if (ds) {
                 blk_detach_dev(blk, ds);
             }
include/sysemu/block-backend.h +0 -1
index 3b29317..24d1d85 100644
@@ -152,7 +152,6 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
 int blk_co_pdiscard(BlockBackend *blk, int64_t offset, int count);
 int blk_co_flush(BlockBackend *blk);
 int blk_flush(BlockBackend *blk);
-int blk_flush_all(void);
 int blk_commit_all(void);
 void blk_drain(BlockBackend *blk);
 void blk_drain_all(void);
Signature · 1 line
1.8.3.1
author
#5 lore raw

[Qemu-devel] [PULL 06/18] block/qapi: Use separate options type for curl driver

In reply to Kevin Wolf

We're going to add an option to the file drivers which doesn't apply to the curl drivers, so give them a separate option type.

---
 qapi/block-core.json | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
qapi/block-core.json +18 -7
index 92193ab..b5fdd42 100644
@@ -1721,8 +1721,7 @@
 ##
 # @BlockdevOptionsFile
 #
-# Driver specific block device options for the file backend and similar
-# protocols.
+# Driver specific block device options for the file backend.
 #
 # @filename:    path to the image file
 #
@@ -2211,6 +2210,18 @@
             '*top-id': 'str' } }
 
 ##
+# @BlockdevOptionsCurl
+#
+# Driver specific block device options for the curl backend.
+#
+# @filename:    path to the image file
+#
+# Since: 1.7
+##
+{ 'struct': 'BlockdevOptionsCurl',
+  'data': { 'filename': 'str' } }
+
+##
 # @BlockdevOptions
 #
 # Options for creating a block device.  Many options are available for all
@@ -2248,13 +2259,13 @@
       'cloop':      'BlockdevOptionsGenericFormat',
       'dmg':        'BlockdevOptionsGenericFormat',
       'file':       'BlockdevOptionsFile',
-      'ftp':        'BlockdevOptionsFile',
-      'ftps':       'BlockdevOptionsFile',
+      'ftp':        'BlockdevOptionsCurl',
+      'ftps':       'BlockdevOptionsCurl',
       'gluster':    'BlockdevOptionsGluster',
       'host_cdrom': 'BlockdevOptionsFile',
       'host_device':'BlockdevOptionsFile',
-      'http':       'BlockdevOptionsFile',
-      'https':      'BlockdevOptionsFile',
+      'http':       'BlockdevOptionsCurl',
+      'https':      'BlockdevOptionsCurl',
 # TODO iscsi: Wait for structured options
       'luks':       'BlockdevOptionsLUKS',
 # TODO nbd: Should take InetSocketAddress for 'host'?
@@ -2271,7 +2282,7 @@
       'replication':'BlockdevOptionsReplication',
 # TODO sheepdog: Wait for structured options
 # TODO ssh: Should take InetSocketAddress for 'host'?
-      'tftp':       'BlockdevOptionsFile',
+      'tftp':       'BlockdevOptionsCurl',
       'vdi':        'BlockdevOptionsGenericFormat',
       'vhdx':       'BlockdevOptionsGenericFormat',
       'vmdk':       'BlockdevOptionsGenericCOWFormat',
Signature · 1 line
1.8.3.1
author
#6 lore raw

[Qemu-devel] [PULL 08/18] block: Parse 'detect-zeroes' in bdrv_open_common()

In reply to Kevin Wolf

Amongst others, this means that you can now use the 'detect-zeroes' option for non-top-level nodes in blockdev-add, like the QAPI schema promises.

---
 block.c    | 33 +++++++++++++++++++++++++++++++++
 blockdev.c |  9 +--------
 2 files changed, 34 insertions(+), 8 deletions(-)
block.c +33 -0
index 493ecf3..1f10457 100644
@@ -42,6 +42,7 @@
 #include "qapi-event.h"
 #include "qemu/cutils.h"
 #include "qemu/id.h"
+#include "qapi/util.h"
 
 #ifdef CONFIG_BSD
 #include <sys/ioctl.h>
@@ -954,6 +955,11 @@ static QemuOptsList bdrv_runtime_opts = {
             .type = QEMU_OPT_BOOL,
             .help = "Node is opened in read-only mode",
         },
+        {
+            .name = "detect-zeroes",
+            .type = QEMU_OPT_STRING,
+            .help = "try to optimize zero writes (off, on, unmap)",
+        },
         { /* end of list */ }
     },
 };
@@ -970,6 +976,7 @@ static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
     const char *filename;
     const char *driver_name = NULL;
     const char *node_name = NULL;
+    const char *detect_zeroes;
     QemuOpts *opts;
     BlockDriver *drv;
     Error *local_err = NULL;
@@ -1038,6 +1045,32 @@ static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
         }
     }
 
+    detect_zeroes = qemu_opt_get(opts, "detect-zeroes");
+    if (detect_zeroes) {
+        BlockdevDetectZeroesOptions value =
+            qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
+                            detect_zeroes,
+                            BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
+                            BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
+                            &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            ret = -EINVAL;
+            goto fail_opts;
+        }
+
+        if (value == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
+            !(bs->open_flags & BDRV_O_UNMAP))
+        {
+            error_setg(errp, "setting detect-zeroes to unmap is not allowed "
+                             "without setting discard operation to unmap");
+            ret = -EINVAL;
+            goto fail_opts;
+        }
+
+        bs->detect_zeroes = value;
+    }
+
     if (filename != NULL) {
         pstrcpy(bs->filename, sizeof(bs->filename), filename);
     } else {
blockdev.c +1 -8
index 7820f42..511260c 100644
@@ -658,7 +658,6 @@ static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
     BlockDriverState *bs;
     QemuOpts *opts;
     Error *local_error = NULL;
-    BlockdevDetectZeroesOptions detect_zeroes;
     int bdrv_flags = 0;
 
     opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
@@ -673,7 +672,7 @@ static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
     }
 
     extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
-                                    &detect_zeroes, &local_error);
+                                    NULL, &local_error);
     if (local_error) {
         error_propagate(errp, local_error);
         goto fail;
@@ -695,8 +694,6 @@ static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
         goto fail_no_bs_opts;
     }
 
-    bs->detect_zeroes = detect_zeroes;
-
 fail_no_bs_opts:
     qemu_opts_del(opts);
     return bs;
@@ -4136,10 +4133,6 @@ static QemuOptsList qemu_root_bds_opts = {
             .name = "copy-on-read",
             .type = QEMU_OPT_BOOL,
             .help = "copy read data from backing file into image file",
-        },{
-            .name = "detect-zeroes",
-            .type = QEMU_OPT_STRING,
-            .help = "try to optimize zero writes (off, on, unmap)",
         },
         { /* end of list */ }
     },
Signature · 1 line
1.8.3.1
author
#7 lore raw

[Qemu-devel] [PULL 11/18] block: Remove qemu_root_bds_opts

In reply to Kevin Wolf

The remaining options in qemu_root_bds_opts (aio and copy-on-read) aren't used any more, the QAPI schema doesn't contain them. Therefore all the code processing qemu_root_bds_opts options is dead and can be removed.

---
 blockdev.c | 54 +-----------------------------------------------------
 1 file changed, 1 insertion(+), 53 deletions(-)
blockdev.c +1 -53
index e2ace04..07ec733 100644
@@ -633,34 +633,11 @@ err_no_opts:
     return NULL;
 }
 
-static QemuOptsList qemu_root_bds_opts;
-
 /* Takes the ownership of bs_opts */
 static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
 {
-    BlockDriverState *bs;
-    QemuOpts *opts;
-    Error *local_error = NULL;
     int bdrv_flags = 0;
 
-    opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
-    if (!opts) {
-        goto fail;
-    }
-
-    qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
-    if (local_error) {
-        error_propagate(errp, local_error);
-        goto fail;
-    }
-
-    extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
-                                    NULL, &local_error);
-    if (local_error) {
-        error_propagate(errp, local_error);
-        goto fail;
-    }
-
     /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
      * with other callers) rather than what we want as the real defaults.
      * Apply the defaults here instead. */
@@ -672,19 +649,7 @@ static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
         bdrv_flags |= BDRV_O_INACTIVE;
     }
 
-    bs = bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
-    if (!bs) {
-        goto fail_no_bs_opts;
-    }
-
-fail_no_bs_opts:
-    qemu_opts_del(opts);
-    return bs;
-
-fail:
-    qemu_opts_del(opts);
-    QDECREF(bs_opts);
-    return NULL;
+    return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
 }
 
 void blockdev_close_all_bdrv_states(void)
@@ -4099,23 +4064,6 @@ QemuOptsList qemu_common_drive_opts = {
     },
 };
 
-static QemuOptsList qemu_root_bds_opts = {
-    .name = "root-bds",
-    .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head),
-    .desc = {
-        {
-            .name = "aio",
-            .type = QEMU_OPT_STRING,
-            .help = "host AIO implementation (threads, native)",
-        },{
-            .name = "copy-on-read",
-            .type = QEMU_OPT_BOOL,
-            .help = "copy read data from backing file into image file",
-        },
-        { /* end of list */ }
-    },
-};
-
 QemuOptsList qemu_drive_opts = {
     .name = "drive",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
Signature · 1 line
1.8.3.1
author
#8 lore raw

[Qemu-devel] [PULL 04/18] block: Fix error path in qmp_blockdev_change_medium()

In reply to Kevin Wolf

Commit 00949bab incorrectly changed one instance of &err into errp while touching the line. Change it back.

---
 blockdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
blockdev.c +1 -1
index 29c6561..62d0dd0 100644
@@ -2614,7 +2614,7 @@ void qmp_blockdev_change_medium(bool has_device, const char *device,
     error_free(err);
     err = NULL;
 
-    qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp);
+    qmp_x_blockdev_remove_medium(has_device, device, has_id, id, &err);
     if (err) {
         error_propagate(errp, err);
         goto fail;
Signature · 1 line
1.8.3.1
author
#9 lore raw

[Qemu-devel] [PULL 09/18] block: Use 'detect-zeroes' option for 'blockdev-change-medium'

In reply to Kevin Wolf

Instead of modifying the new BDS after it has been opened, use the newly supported 'detect-zeroes' option in bdrv_open_common() so that all requirements are checked (detect-zeroes=unmap requires discard=unmap).

---
 block/block-backend.c          | 9 ++++-----
 blockdev.c                     | 9 ++++++---
 include/sysemu/block-backend.h | 2 +-
 3 files changed, 11 insertions(+), 9 deletions(-)
block/block-backend.c +4 -5
index f34bad5..639294b 100644
@@ -1592,13 +1592,12 @@ void blk_update_root_state(BlockBackend *blk)
 }
 
 /*
- * Applies the information in the root state to the given BlockDriverState. This
- * does not include the flags which have to be specified for bdrv_open(), use
- * blk_get_open_flags_from_root_state() to inquire them.
+ * Returns the detect-zeroes setting to be used for bdrv_open() of a
+ * BlockDriverState which is supposed to inherit the root state.
  */
-void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs)
+bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk)
 {
-    bs->detect_zeroes = blk->root_state.detect_zeroes;
+    return blk->root_state.detect_zeroes;
 }
 
 /*
blockdev.c +6 -3
index 511260c..7b87bd8 100644
@@ -2546,6 +2546,7 @@ void qmp_blockdev_change_medium(bool has_device, const char *device,
     BlockBackend *blk;
     BlockDriverState *medium_bs = NULL;
     int bdrv_flags;
+    bool detect_zeroes;
     int rc;
     QDict *options = NULL;
     Error *err = NULL;
@@ -2585,8 +2586,12 @@ void qmp_blockdev_change_medium(bool has_device, const char *device,
         abort();
     }
 
+    options = qdict_new();
+    detect_zeroes = blk_get_detect_zeroes_from_root_state(blk);
+    qdict_put(options, "detect-zeroes",
+              qstring_from_str(detect_zeroes ? "on" : "off"));
+
     if (has_format) {
-        options = qdict_new();
         qdict_put(options, "driver", qstring_from_str(format));
     }
 
@@ -2623,8 +2628,6 @@ void qmp_blockdev_change_medium(bool has_device, const char *device,
         goto fail;
     }
 
-    blk_apply_root_state(blk, medium_bs);
-
     qmp_blockdev_close_tray(has_device, device, has_id, id, errp);
 
 fail:
include/sysemu/block-backend.h +1 -1
index 24d1d85..a7993af 100644
@@ -198,7 +198,7 @@ void blk_io_unplug(BlockBackend *blk);
 BlockAcctStats *blk_get_stats(BlockBackend *blk);
 BlockBackendRootState *blk_get_root_state(BlockBackend *blk);
 void blk_update_root_state(BlockBackend *blk);
-void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs);
+bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk);
 int blk_get_open_flags_from_root_state(BlockBackend *blk);
 
 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
Signature · 1 line
1.8.3.1
author
#10 lore raw

[Qemu-devel] [PULL 10/18] block: Move 'discard' option to bdrv_open_common()

In reply to Kevin Wolf

This enables its use for nested child nodes. The compatibility between the 'discard' and 'detect-zeroes' setting is checked in bdrv_open_common() now as the former setting isn't available before calling bdrv_open() any more.

---
 block.c               | 17 ++++++++++++++++-
 blockdev.c            | 25 -------------------------
 include/block/block.h |  1 +
 3 files changed, 17 insertions(+), 26 deletions(-)
block.c +16 -1
index 1f10457..bb1f1ec 100644
@@ -765,7 +765,7 @@ static void bdrv_inherited_options(int *child_flags, QDict *child_options,
     /* Our block drivers take care to send flushes and respect unmap policy,
      * so we can default to enable both on lower layers regardless of the
      * corresponding parent options. */
-    flags |= BDRV_O_UNMAP;
+    qdict_set_default_str(child_options, BDRV_OPT_DISCARD, "unmap");
 
     /* Clear flags that only apply to the top layer */
     flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ |
@@ -960,6 +960,11 @@ static QemuOptsList bdrv_runtime_opts = {
             .type = QEMU_OPT_STRING,
             .help = "try to optimize zero writes (off, on, unmap)",
         },
+        {
+            .name = "discard",
+            .type = QEMU_OPT_STRING,
+            .help = "discard operation (ignore/off, unmap/on)",
+        },
         { /* end of list */ }
     },
 };
@@ -976,6 +981,7 @@ static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
     const char *filename;
     const char *driver_name = NULL;
     const char *node_name = NULL;
+    const char *discard;
     const char *detect_zeroes;
     QemuOpts *opts;
     BlockDriver *drv;
@@ -1045,6 +1051,15 @@ static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
         }
     }
 
+    discard = qemu_opt_get(opts, "discard");
+    if (discard != NULL) {
+        if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) {
+            error_setg(errp, "Invalid discard option");
+            ret = -EINVAL;
+            goto fail_opts;
+        }
+    }
+
     detect_zeroes = qemu_opt_get(opts, "detect-zeroes");
     if (detect_zeroes) {
         BlockdevDetectZeroesOptions value =
blockdev.c +0 -25
index 7b87bd8..e2ace04 100644
@@ -356,7 +356,6 @@ static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
     const char **throttling_group, ThrottleConfig *throttle_cfg,
     BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
 {
-    const char *discard;
     Error *local_error = NULL;
     const char *aio;
 
@@ -365,13 +364,6 @@ static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
             *bdrv_flags |= BDRV_O_COPY_ON_READ;
         }
 
-        if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
-            if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
-                error_setg(errp, "Invalid discard option");
-                return;
-            }
-        }
-
         if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
             if (!strcmp(aio, "native")) {
                 *bdrv_flags |= BDRV_O_NATIVE_AIO;
@@ -449,15 +441,6 @@ static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
             error_propagate(errp, local_error);
             return;
         }
-
-        if (bdrv_flags &&
-            *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
-            !(*bdrv_flags & BDRV_O_UNMAP))
-        {
-            error_setg(errp, "setting detect-zeroes to unmap is not allowed "
-                             "without setting discard operation to unmap");
-            return;
-        }
     }
 }
 
@@ -3990,10 +3973,6 @@ QemuOptsList qemu_common_drive_opts = {
             .type = QEMU_OPT_BOOL,
             .help = "enable/disable snapshot mode",
         },{
-            .name = "discard",
-            .type = QEMU_OPT_STRING,
-            .help = "discard operation (ignore/off, unmap/on)",
-        },{
             .name = "aio",
             .type = QEMU_OPT_STRING,
             .help = "host AIO implementation (threads, native)",
@@ -4125,10 +4104,6 @@ static QemuOptsList qemu_root_bds_opts = {
     .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head),
     .desc = {
         {
-            .name = "discard",
-            .type = QEMU_OPT_STRING,
-            .help = "discard operation (ignore/off, unmap/on)",
-        },{
             .name = "aio",
             .type = QEMU_OPT_STRING,
             .help = "host AIO implementation (threads, native)",
include/block/block.h +1 -0
index 811b060..107c603 100644
@@ -108,6 +108,7 @@ typedef struct HDGeometry {
 #define BDRV_OPT_CACHE_DIRECT   "cache.direct"
 #define BDRV_OPT_CACHE_NO_FLUSH "cache.no-flush"
 #define BDRV_OPT_READ_ONLY      "read-only"
+#define BDRV_OPT_DISCARD        "discard"
 
 
 #define BDRV_SECTOR_BITS   9
Signature · 1 line
1.8.3.1
#11 lore raw

Re: [Qemu-devel] [PULL 10/18] block: Move 'discard' option to bdrv_open_common()

Quoted 4 lines · Kevin Wolf

This enables its use for nested child nodes. The compatibility between the 'discard' and 'detect-zeroes' setting is checked in bdrv_open_common() now as the former setting isn't available before calling bdrv_open() any more.

Seems this change makes libvirt not see the discard option any more, "virsh start" throws this error now ...

error: Failed to start domain fedora-org-xhci
error: unsupported configuration: discard is not supported by this QEMU binary

... and git bisect found this commit.

cheers,
Gerd

author
#12 lore raw

Re: [Qemu-devel] [PULL 10/18] block: Move 'discard' option to bdrv_open_common()

Am 07.10.2016 um 11:01 hat Gerd Hoffmann geschrieben:

Quoted 6 lines · Kevin Wolf
Quoted 4 lines

This enables its use for nested child nodes. The compatibility between the 'discard' and 'detect-zeroes' setting is checked in bdrv_open_common() now as the former setting isn't available before calling bdrv_open() any more.

Quoted 8 lines

Seems this change makes libvirt not see the discard option any more, "virsh start" throws this error now ...

error: Failed to start domain fedora-org-xhci
error: unsupported configuration: discard is not supported by this QEMU binary

... and git bisect found this commit.

The fix is already on the list:

[PATCH] block: Add bdrv_runtime_opts to query-command-line-options

Kevin

author
#13 lore raw

[Qemu-devel] [PULL 16/18] coroutine-sigaltstack: use helper for allocating stack memory

In reply to Kevin Wolf

From: Peter Lieven <[email protected]>

---
 util/coroutine-sigaltstack.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
util/coroutine-sigaltstack.c +5 -4
index a5bcb7e..f6fc49a 100644
@@ -33,6 +33,7 @@
 typedef struct {
     Coroutine base;
     void *stack;
+    size_t stack_size;
     sigjmp_buf env;
 } CoroutineSigAltStack;
 
@@ -143,7 +144,6 @@ static void coroutine_trampoline(int signal)
 
 Coroutine *qemu_coroutine_new(void)
 {
-    const size_t stack_size = COROUTINE_STACK_SIZE;
     CoroutineSigAltStack *co;
     CoroutineThreadState *coTS;
     struct sigaction sa;
@@ -164,7 +164,8 @@ Coroutine *qemu_coroutine_new(void)
      */
 
     co = g_malloc0(sizeof(*co));
-    co->stack = g_malloc(stack_size);
+    co->stack_size = COROUTINE_STACK_SIZE;
+    co->stack = qemu_alloc_stack(&co->stack_size);
     co->base.entry_arg = &old_env; /* stash away our jmp_buf */
 
     coTS = coroutine_get_thread_state();
@@ -189,7 +190,7 @@ Coroutine *qemu_coroutine_new(void)
      * Set the new stack.
      */
     ss.ss_sp = co->stack;
-    ss.ss_size = stack_size;
+    ss.ss_size = co->stack_size;
     ss.ss_flags = 0;
     if (sigaltstack(&ss, &oss) < 0) {
         abort();
@@ -253,7 +254,7 @@ void qemu_coroutine_delete(Coroutine *co_)
 {
     CoroutineSigAltStack *co = DO_UPCAST(CoroutineSigAltStack, base, co_);
 
-    g_free(co->stack);
+    qemu_free_stack(co->stack, co->stack_size);
     g_free(co);
 }
 
Signature · 1 line
1.8.3.1
author
#14 lore raw

[Qemu-devel] [PULL 18/18] coroutine: reduce stack size to 60kB

In reply to Kevin Wolf

From: Peter Lieven <[email protected]>

evaluation with the recently introduced maximum stack usage monitoring revealed that the actual used stack size was never above 4kB so allocating 1MB stack for each coroutine is a lot of wasted memory. So reduce the stack size to 60kB which should still give enough head room. The guard page added in qemu_alloc_stack will catch a potential stack overflow introduced by this commit. The 60kB + guard page will result in an allocation of 64kB per coroutine on systems where a page is 4kB.

---
 include/qemu/coroutine_int.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/qemu/coroutine_int.h +1 -1
index 14d4f1d..be14260 100644
@@ -28,7 +28,7 @@
 #include "qemu/queue.h"
 #include "qemu/coroutine.h"
 
-#define COROUTINE_STACK_SIZE (1 << 20)
+#define COROUTINE_STACK_SIZE 61440
 
 typedef enum {
     COROUTINE_YIELD = 1,
Signature · 1 line
1.8.3.1
author
#15 lore raw

[Qemu-devel] [PULL 07/18] block/qapi: Move 'aio' option to file driver

In reply to Kevin Wolf

The option whether or not to use a native AIO interface really isn't a generic option for all drivers, but only applies to the native file protocols. This patch moves the option in blockdev-add to the appropriate places (raw-posix and raw-win32).

We still have to keep the flag BDRV_O_NATIVE_AIO for compatibility because so far the AIO option was usually specified on the wrong layer (the top-level format driver, which didn't even look at it) and then inherited by the protocol driver (where it was actually used). We can't forbid this use except in new interfaces.

---
 block/raw-posix.c      | 44 ++++++++++++++++++++++++---------------
 block/raw-win32.c      | 56 +++++++++++++++++++++++++++++++++++++++++++++-----
 qapi/block-core.json   |  6 +++---
 tests/qemu-iotests/087 |  4 ++--
 4 files changed, 83 insertions(+), 27 deletions(-)
block/raw-posix.c +27 -17
index 6ed7547..166e9d1 100644
@@ -143,6 +143,7 @@ typedef struct BDRVRawState {
     bool has_discard:1;
     bool has_write_zeroes:1;
     bool discard_zeroes:1;
+    bool use_linux_aio:1;
     bool has_fallocate;
     bool needs_alignment;
 } BDRVRawState;
@@ -367,18 +368,6 @@ static void raw_parse_flags(int bdrv_flags, int *open_flags)
     }
 }
 
-#ifdef CONFIG_LINUX_AIO
-static bool raw_use_aio(int bdrv_flags)
-{
-    /*
-     * Currently Linux do AIO only for files opened with O_DIRECT
-     * specified so check NOCACHE flag too
-     */
-    return (bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
-                         (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO);
-}
-#endif
-
 static void raw_parse_filename(const char *filename, QDict *options,
                                Error **errp)
 {
@@ -399,6 +388,11 @@ static QemuOptsList raw_runtime_opts = {
             .type = QEMU_OPT_STRING,
             .help = "File name of the image",
         },
+        {
+            .name = "aio",
+            .type = QEMU_OPT_STRING,
+            .help = "host AIO implementation (threads, native)",
+        },
         { /* end of list */ }
     },
 };
@@ -410,6 +404,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
     QemuOpts *opts;
     Error *local_err = NULL;
     const char *filename = NULL;
+    BlockdevAioOptions aio, aio_default;
     int fd, ret;
     struct stat st;
 
@@ -429,6 +424,18 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
         goto fail;
     }
 
+    aio_default = (bdrv_flags & BDRV_O_NATIVE_AIO)
+                  ? BLOCKDEV_AIO_OPTIONS_NATIVE
+                  : BLOCKDEV_AIO_OPTIONS_THREADS;
+    aio = qapi_enum_parse(BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
+                          BLOCKDEV_AIO_OPTIONS__MAX, aio_default, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        ret = -EINVAL;
+        goto fail;
+    }
+    s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE);
+
     s->open_flags = open_flags;
     raw_parse_flags(bdrv_flags, &s->open_flags);
 
@@ -444,14 +451,15 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
     s->fd = fd;
 
 #ifdef CONFIG_LINUX_AIO
-    if (!raw_use_aio(bdrv_flags) && (bdrv_flags & BDRV_O_NATIVE_AIO)) {
+     /* 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;
     }
 #else
-    if (bdrv_flags & BDRV_O_NATIVE_AIO) {
+    if (s->use_linux_aio) {
         error_setg(errp, "aio=native was specified, but is not supported "
                          "in this build.");
         ret = -EINVAL;
@@ -1256,7 +1264,7 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,
         if (!bdrv_qiov_is_aligned(bs, qiov)) {
             type |= QEMU_AIO_MISALIGNED;
 #ifdef CONFIG_LINUX_AIO
-        } else if (bs->open_flags & BDRV_O_NATIVE_AIO) {
+        } else if (s->use_linux_aio) {
             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);
@@ -1285,7 +1293,8 @@ static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset,
 static void raw_aio_plug(BlockDriverState *bs)
 {
 #ifdef CONFIG_LINUX_AIO
-    if (bs->open_flags & BDRV_O_NATIVE_AIO) {
+    BDRVRawState *s = bs->opaque;
+    if (s->use_linux_aio) {
         LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
         laio_io_plug(bs, aio);
     }
@@ -1295,7 +1304,8 @@ static void raw_aio_plug(BlockDriverState *bs)
 static void raw_aio_unplug(BlockDriverState *bs)
 {
 #ifdef CONFIG_LINUX_AIO
-    if (bs->open_flags & BDRV_O_NATIVE_AIO) {
+    BDRVRawState *s = bs->opaque;
+    if (s->use_linux_aio) {
         LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
         laio_io_unplug(bs, aio);
     }
block/raw-win32.c +51 -5

120 lines not on this page · raw message

qapi/block-core.json +3 -3
index b5fdd42..9d797b8 100644
@@ -1724,11 +1724,13 @@
 # Driver specific block device options for the file backend.
 #
 # @filename:    path to the image file
+# @aio:         #optional AIO backend (default: threads) (since: 2.8)
 #
 # Since: 1.7
 ##
 { 'struct': 'BlockdevOptionsFile',
-  'data': { 'filename': 'str' } }
+  'data': { 'filename': 'str',
+            '*aio': 'BlockdevAioOptions' } }
 
 ##
 # @BlockdevOptionsNull
@@ -2232,7 +2234,6 @@
 #                 This option is required on the top level of blockdev-add.
 # @discard:       #optional discard-related options (default: ignore)
 # @cache:         #optional cache-related options
-# @aio:           #optional AIO backend (default: threads)
 # @read-only:     #optional whether the block device should be read-only
 #                 (default: false)
 # @detect-zeroes: #optional detect and optimize zero writes (Since 2.1)
@@ -2247,7 +2248,6 @@
             '*node-name': 'str',
             '*discard': 'BlockdevDiscardOptions',
             '*cache': 'BlockdevCacheOptions',
-            '*aio': 'BlockdevAioOptions',
             '*read-only': 'bool',
             '*detect-zeroes': 'BlockdevDetectZeroesOptions' },
   'discriminator': 'driver',
tests/qemu-iotests/087 +2 -2
index 5c04577..b1ac71f 100755
@@ -117,10 +117,10 @@ run_qemu <<EOF
       "options": {
         "driver": "$IMGFMT",
         "node-name": "disk",
-        "aio": "native",
         "file": {
             "driver": "file",
-            "filename": "$TEST_IMG"
+            "filename": "$TEST_IMG",
+            "aio": "native"
         }
       }
     }
Signature · 1 line
1.8.3.1
author
#16 lore raw

[Qemu-devel] [PULL 12/18] oslib-posix: add helpers for stack alloc and free

In reply to Kevin Wolf

From: Peter Lieven <[email protected]>

the allocated stack will be adjusted to the minimum supported stack size by the OS and rounded up to be a multiple of the system pagesize. Additionally an architecture dependent guard page is added to the stack to catch stack overflows.

---
 include/sysemu/os-posix.h | 27 +++++++++++++++++++++++++++
 util/oslib-posix.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)
include/sysemu/os-posix.h +27 -0
index 9c7dfdf..3cfedbc 100644
@@ -60,4 +60,31 @@ int qemu_utimens(const char *path, const qemu_timespec *times);
 
 bool is_daemonized(void);
 
+/**
+ * qemu_alloc_stack:
+ * @sz: pointer to a size_t holding the requested usable stack size
+ *
+ * Allocate memory that can be used as a stack, for instance for
+ * coroutines. If the memory cannot be allocated, this function
+ * will abort (like g_malloc()). This function also inserts an
+ * additional guard page to catch a potential stack overflow.
+ * Note that the memory required for the guard page and alignment
+ * and minimal stack size restrictions will increase the value of sz.
+ *
+ * The allocated stack must be freed with qemu_free_stack().
+ *
+ * Returns: pointer to (the lowest address of) the stack memory.
+ */
+void *qemu_alloc_stack(size_t *sz);
+
+/**
+ * qemu_free_stack:
+ * @stack: stack to free
+ * @sz: size of stack in bytes
+ *
+ * Free a stack allocated via qemu_alloc_stack(). Note that sz must
+ * be exactly the adjusted stack size returned by qemu_alloc_stack.
+ */
+void qemu_free_stack(void *stack, size_t sz);
+
 #endif
util/oslib-posix.c +42 -0

45 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author
#17 lore raw

[Qemu-devel] [PULL 13/18] coroutine-sigaltstack: rename coroutine struct appropriately

In reply to Kevin Wolf

From: Peter Lieven <[email protected]>

The name of the sigaltstack coroutine struct was misleading.

---
 util/coroutine-sigaltstack.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
util/coroutine-sigaltstack.c +8 -8
index a7c3366..171cd44 100644
@@ -34,7 +34,7 @@ typedef struct {
     Coroutine base;
     void *stack;
     sigjmp_buf env;
-} CoroutineUContext;
+} CoroutineSigAltStack;
 
 /**
  * Per-thread coroutine bookkeeping
@@ -44,7 +44,7 @@ typedef struct {
     Coroutine *current;
 
     /** The default coroutine */
-    CoroutineUContext leader;
+    CoroutineSigAltStack leader;
 
     /** Information for the signal handler (trampoline) */
     sigjmp_buf tr_reenter;
@@ -89,7 +89,7 @@ static void __attribute__((constructor)) coroutine_init(void)
  * (from the signal handler when it is not signal handling, read ahead
  * for more information).
  */
-static void coroutine_bootstrap(CoroutineUContext *self, Coroutine *co)
+static void coroutine_bootstrap(CoroutineSigAltStack *self, Coroutine *co)
 {
     /* Initialize longjmp environment and switch back the caller */
     if (!sigsetjmp(self->env, 0)) {
@@ -109,7 +109,7 @@ static void coroutine_bootstrap(CoroutineUContext *self, Coroutine *co)
  */
 static void coroutine_trampoline(int signal)
 {
-    CoroutineUContext *self;
+    CoroutineSigAltStack *self;
     Coroutine *co;
     CoroutineThreadState *coTS;
 
@@ -144,7 +144,7 @@ static void coroutine_trampoline(int signal)
 Coroutine *qemu_coroutine_new(void)
 {
     const size_t stack_size = 1 << 20;
-    CoroutineUContext *co;
+    CoroutineSigAltStack *co;
     CoroutineThreadState *coTS;
     struct sigaction sa;
     struct sigaction osa;
@@ -251,7 +251,7 @@ Coroutine *qemu_coroutine_new(void)
 
 void qemu_coroutine_delete(Coroutine *co_)
 {
-    CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
+    CoroutineSigAltStack *co = DO_UPCAST(CoroutineSigAltStack, base, co_);
 
     g_free(co->stack);
     g_free(co);
@@ -260,8 +260,8 @@ void qemu_coroutine_delete(Coroutine *co_)
 CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
                                       CoroutineAction action)
 {
-    CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
-    CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
+    CoroutineSigAltStack *from = DO_UPCAST(CoroutineSigAltStack, base, from_);
+    CoroutineSigAltStack *to = DO_UPCAST(CoroutineSigAltStack, base, to_);
     CoroutineThreadState *s = coroutine_get_thread_state();
     int ret;
 
Signature · 1 line
1.8.3.1
author
#18 lore raw

[Qemu-devel] [PULL 15/18] coroutine-ucontext: use helper for allocating stack memory

In reply to Kevin Wolf

From: Peter Lieven <[email protected]>

---
 util/coroutine-ucontext.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
util/coroutine-ucontext.c +6 -5
index 31254ab..6621f3f 100644
@@ -34,6 +34,7 @@
 typedef struct {
     Coroutine base;
     void *stack;
+    size_t stack_size;
     sigjmp_buf env;
 
 #ifdef CONFIG_VALGRIND_H
@@ -82,7 +83,6 @@ static void coroutine_trampoline(int i0, int i1)
 
 Coroutine *qemu_coroutine_new(void)
 {
-    const size_t stack_size = COROUTINE_STACK_SIZE;
     CoroutineUContext *co;
     ucontext_t old_uc, uc;
     sigjmp_buf old_env;
@@ -101,17 +101,18 @@ Coroutine *qemu_coroutine_new(void)
     }
 
     co = g_malloc0(sizeof(*co));
-    co->stack = g_malloc(stack_size);
+    co->stack_size = COROUTINE_STACK_SIZE;
+    co->stack = qemu_alloc_stack(&co->stack_size);
     co->base.entry_arg = &old_env; /* stash away our jmp_buf */
 
     uc.uc_link = &old_uc;
     uc.uc_stack.ss_sp = co->stack;
-    uc.uc_stack.ss_size = stack_size;
+    uc.uc_stack.ss_size = co->stack_size;
     uc.uc_stack.ss_flags = 0;
 
 #ifdef CONFIG_VALGRIND_H
     co->valgrind_stack_id =
-        VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size);
+        VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size);
 #endif
 
     arg.p = co;
@@ -149,7 +150,7 @@ void qemu_coroutine_delete(Coroutine *co_)
     valgrind_stack_deregister(co);
 #endif
 
-    g_free(co->stack);
+    qemu_free_stack(co->stack, co->stack_size);
     g_free(co);
 }
 
Signature · 1 line
1.8.3.1
author
#19 lore raw

[Qemu-devel] [PULL 14/18] coroutine: add a macro for the coroutine stack size

In reply to Kevin Wolf

From: Peter Lieven <[email protected]>

---
 include/qemu/coroutine_int.h | 2 ++
 util/coroutine-sigaltstack.c | 2 +-
 util/coroutine-ucontext.c    | 2 +-
 util/coroutine-win32.c       | 2 +-
 4 files changed, 5 insertions(+), 3 deletions(-)
include/qemu/coroutine_int.h +2 -0
index 6df9d33..14d4f1d 100644
@@ -28,6 +28,8 @@
 #include "qemu/queue.h"
 #include "qemu/coroutine.h"
 
+#define COROUTINE_STACK_SIZE (1 << 20)
+
 typedef enum {
     COROUTINE_YIELD = 1,
     COROUTINE_TERMINATE = 2,
util/coroutine-sigaltstack.c +1 -1
index 171cd44..a5bcb7e 100644
@@ -143,7 +143,7 @@ static void coroutine_trampoline(int signal)
 
 Coroutine *qemu_coroutine_new(void)
 {
-    const size_t stack_size = 1 << 20;
+    const size_t stack_size = COROUTINE_STACK_SIZE;
     CoroutineSigAltStack *co;
     CoroutineThreadState *coTS;
     struct sigaction sa;
util/coroutine-ucontext.c +1 -1
index 2bb7e10..31254ab 100644
@@ -82,7 +82,7 @@ static void coroutine_trampoline(int i0, int i1)
 
 Coroutine *qemu_coroutine_new(void)
 {
-    const size_t stack_size = 1 << 20;
+    const size_t stack_size = COROUTINE_STACK_SIZE;
     CoroutineUContext *co;
     ucontext_t old_uc, uc;
     sigjmp_buf old_env;
util/coroutine-win32.c +1 -1
index 02e28e8..de6bd4f 100644
@@ -71,7 +71,7 @@ static void CALLBACK coroutine_trampoline(void *co_)
 
 Coroutine *qemu_coroutine_new(void)
 {
-    const size_t stack_size = 1 << 20;
+    const size_t stack_size = COROUTINE_STACK_SIZE;
     CoroutineWin32 *co;
 
     co = g_malloc0(sizeof(*co));
Signature · 1 line
1.8.3.1
author
#20 lore raw

[Qemu-devel] [PULL 17/18] oslib-posix: add a configure switch to debug stack usage

In reply to Kevin Wolf

From: Peter Lieven <[email protected]>

this adds a knob to track the maximum stack usage of stacks created by qemu_alloc_stack.

---
 configure          | 19 +++++++++++++++++++
 util/oslib-posix.c | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
configure +19 -0
index 8fa62ad..93ef00a 100755
@@ -296,6 +296,7 @@ libiscsi=""
 libnfs=""
 coroutine=""
 coroutine_pool=""
+debug_stack_usage="no"
 seccomp=""
 glusterfs=""
 glusterfs_xlator_opt="no"
@@ -1004,6 +1005,8 @@ for opt do
   ;;
   --enable-coroutine-pool) coroutine_pool="yes"
   ;;
+  --enable-debug-stack-usage) debug_stack_usage="yes"
+  ;;
   --disable-docs) docs="no"
   ;;
   --enable-docs) docs="yes"
@@ -4276,6 +4279,17 @@ if test "$coroutine" = "gthread" -a "$coroutine_pool" = "yes"; then
   error_exit "'gthread' coroutine backend does not support pool (use --disable-coroutine-pool)"
 fi
 
+if test "$debug_stack_usage" = "yes"; then
+  if test "$cpu" = "ia64" -o "$cpu" = "hppa"; then
+    error_exit "stack usage debugging is not supported for $cpu"
+  fi
+  if test "$coroutine_pool" = "yes"; then
+    echo "WARN: disabling coroutine pool for stack usage debugging"
+    coroutine_pool=no
+  fi
+fi
+
+
 ##########################################
 # check if we have open_by_handle_at
 
@@ -4861,6 +4875,7 @@ echo "QGA MSI support   $guest_agent_msi"
 echo "seccomp support   $seccomp"
 echo "coroutine backend $coroutine"
 echo "coroutine pool    $coroutine_pool"
+echo "debug stack usage $debug_stack_usage"
 echo "GlusterFS support $glusterfs"
 echo "Archipelago support $archipelago"
 echo "gcov              $gcov_tool"
@@ -5330,6 +5345,10 @@ else
   echo "CONFIG_COROUTINE_POOL=0" >> $config_host_mak
 fi
 
+if test "$debug_stack_usage" = "yes" ; then
+  echo "CONFIG_DEBUG_STACK_USAGE=y" >> $config_host_mak
+fi
+
 if test "$open_by_handle_at" = "yes" ; then
   echo "CONFIG_OPEN_BY_HANDLE=y" >> $config_host_mak
 fi
util/oslib-posix.c +35 -0
index d950c34..aaec189 100644
@@ -50,6 +50,10 @@
 
 #include "qemu/mmap-alloc.h"
 
+#ifdef CONFIG_DEBUG_STACK_USAGE
+#include "qemu/error-report.h"
+#endif
+
 int qemu_get_thread_id(void)
 {
 #if defined(__linux__)
@@ -503,6 +507,9 @@ pid_t qemu_fork(Error **errp)
 void *qemu_alloc_stack(size_t *sz)
 {
     void *ptr, *guardpage;
+#ifdef CONFIG_DEBUG_STACK_USAGE
+    void *ptr2;
+#endif
     size_t pagesz = getpagesize();
 #ifdef _SC_THREAD_STACK_MIN
     /* avoid stacks smaller than _SC_THREAD_STACK_MIN */
@@ -534,10 +541,38 @@ void *qemu_alloc_stack(size_t *sz)
         abort();
     }
 
+#ifdef CONFIG_DEBUG_STACK_USAGE
+    for (ptr2 = ptr + pagesz; ptr2 < ptr + *sz; ptr2 += sizeof(uint32_t)) {
+        *(uint32_t *)ptr2 = 0xdeadbeaf;
+    }
+#endif
+
     return ptr;
 }
 
+#ifdef CONFIG_DEBUG_STACK_USAGE
+static __thread unsigned int max_stack_usage;
+#endif
+
 void qemu_free_stack(void *stack, size_t sz)
 {
+#ifdef CONFIG_DEBUG_STACK_USAGE
+    unsigned int usage;
+    void *ptr;
+
+    for (ptr = stack + getpagesize(); ptr < stack + sz;
+         ptr += sizeof(uint32_t)) {
+        if (*(uint32_t *)ptr != 0xdeadbeaf) {
+            break;
+        }
+    }
+    usage = sz - (uintptr_t) (ptr - stack);
+    if (usage > max_stack_usage) {
+        error_report("thread %d max stack usage increased from %u to %u",
+                     qemu_get_thread_id(), max_stack_usage, usage);
+        max_stack_usage = usage;
+    }
+#endif
+
     munmap(stack, sz);
 }
Signature · 1 line
1.8.3.1
#21 lore raw
In reply to Kevin Wolf
Quoted 9 lines · Kevin Wolf

The following changes since commit 7cfdc02dae0d2ff58c897496cfdbbafc0eda0f3f:

Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging (2016-09-26 19:47:00 +0100)

are available in the git repository at:

git://repo.or.cz/qemu/kevin.git tags/for-upstream

Quoted 8 lines

for you to fetch changes up to 3b856cebe5e93547852c156ca2119d075e62aed7:

coroutine: reduce stack size to 60kB (2016-09-27 14:05:21 +0200)

----------------------------------------------------------------
Block layer patches

----------------------------------------------------------------

I see 'make check' failures on x86-64 host, clang Linux:

  /i386/ahci/migrate/ncq/simple:                                       OK
  /i386/ahci/migrate/ncq/halted:                                       OK
  /i386/ahci/cdrom/dma/single:                                         OK
  /i386/ahci/cdrom/dma/multi:                                          OK

/i386/ahci/cdrom/pio/single:
Broken pipe
FAIL
GTester: last random seed: R02Sa8f729848b07c3b3e5ee67368f9d0350 (pid=10590)
/i386/ahci/cdrom/pio/multi:
Broken pipe
FAIL
GTester: last random seed: R02Se85704e04bbd382223983c878723b811 (pid=10598)
FAIL: tests/ahci-test
TEST: tests/hd-geo-test... (pid=10601)
/i386/hd-geo/ide/none: OK

thanks
-- PMM

author
#22 lore raw

Am 27.09.2016 um 21:42 hat Peter Maydell geschrieben:

Quoted 25 lines · Kevin Wolf
Quoted 9 lines

The following changes since commit 7cfdc02dae0d2ff58c897496cfdbbafc0eda0f3f:

Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging (2016-09-26 19:47:00 +0100)

are available in the git repository at:

git://repo.or.cz/qemu/kevin.git tags/for-upstream

Quoted 8 lines

for you to fetch changes up to 3b856cebe5e93547852c156ca2119d075e62aed7:

coroutine: reduce stack size to 60kB (2016-09-27 14:05:21 +0200)

----------------------------------------------------------------
Block layer patches

----------------------------------------------------------------

I see 'make check' failures on x86-64 host, clang Linux:

  /i386/ahci/migrate/ncq/simple:                                       OK
  /i386/ahci/migrate/ncq/halted:                                       OK
  /i386/ahci/cdrom/dma/single:                                         OK
  /i386/ahci/cdrom/dma/multi:                                          OK
Quoted 13 lines

/i386/ahci/cdrom/pio/single:
Broken pipe
FAIL
GTester: last random seed: R02Sa8f729848b07c3b3e5ee67368f9d0350 (pid=10590)
/i386/ahci/cdrom/pio/multi:
Broken pipe
FAIL
GTester: last random seed: R02Se85704e04bbd382223983c878723b811 (pid=10598)
FAIL: tests/ahci-test
TEST: tests/hd-geo-test... (pid=10601)
/i386/hd-geo/ide/none: OK

I asked on IRC, but as you don't seem to be around at the moment, I'll keep things on the list instead.

Can you tell me the exact 'configure' and 'make check' command lines you're using? I always run the tests with clang/Linux on x86_64 before sending a pull request, and I repeated it now, but the tests pass for me.

This is what I'm doing (yesterday I built all targets, today only i386-softmmu because that seems to be what fails for you):

$ clang --version
clang version 3.4.2 (tags/RELEASE_34/dot2-final)
Target: x86_64-redhat-linux-gnu
Thread model: posix
$ ../qemu/configure --cc=clang --host-cc=clang --cxx=clang++ --target-list=i386-softmmu
$ make -j4
$ make check

Kevin

#23 lore raw
Quoted 1 line · Kevin Wolf

[what clang config?]

This is with Ubuntu Xenial's clang. configure args are

'--cc=clang' '--cxx=clang++' '--enable-gtk'
'--extra-cflags=-fsanitize=undefined -Werror'

and the build is just 'make all -j8 && make check' in the build directory.

thanks
-- PMM

#24 lore raw
In reply to Kevin Wolf
Quoted 40 lines · Kevin Wolf

Am 27.09.2016 um 21:42 hat Peter Maydell geschrieben:

Quoted 25 lines · Kevin Wolf
Quoted 9 lines

The following changes since commit 7cfdc02dae0d2ff58c897496cfdbbafc0eda0f3f:

Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging (2016-09-26 19:47:00 +0100)

are available in the git repository at:

git://repo.or.cz/qemu/kevin.git tags/for-upstream

Quoted 8 lines

for you to fetch changes up to 3b856cebe5e93547852c156ca2119d075e62aed7:

coroutine: reduce stack size to 60kB (2016-09-27 14:05:21 +0200)

----------------------------------------------------------------
Block layer patches

----------------------------------------------------------------

I see 'make check' failures on x86-64 host, clang Linux:

  /i386/ahci/migrate/ncq/simple:                                       OK
  /i386/ahci/migrate/ncq/halted:                                       OK
  /i386/ahci/cdrom/dma/single:                                         OK
  /i386/ahci/cdrom/dma/multi:                                          OK
Quoted 13 lines

/i386/ahci/cdrom/pio/single:
Broken pipe
FAIL
GTester: last random seed: R02Sa8f729848b07c3b3e5ee67368f9d0350 (pid=10590)
/i386/ahci/cdrom/pio/multi:
Broken pipe
FAIL
GTester: last random seed: R02Se85704e04bbd382223983c878723b811 (pid=10598)
FAIL: tests/ahci-test
TEST: tests/hd-geo-test... (pid=10601)
/i386/hd-geo/ide/none: OK

Quoted 2 lines

I asked on IRC, but as you don't seem to be around at the moment, I'll keep things on the list instead.

I got a gdb backtrace:

Thread 1 "qemu-system-i38" received signal SIGSEGV, Segmentation fault. 0x00005555561dea15 in address_space_translate (as=0x55555a46bfc0, addr=1106048, xlat=0x7ffff7e0d050, plen=0x7ffff7e0d058,

    is_write=false) at /home/petmay01/linaro/qemu-for-merges/exec.c:423

423 {

Backtrace suggests we've run out of stack due to some infinite recursion:

#0 0x00005555561dea15 in address_space_translate (as=0x55555a46bfc0, addr=1106048, xlat=0x7ffff7e0d050, plen=0x7ffff7e0d058, is_write=false) at /home/petmay01/linaro/qemu-for-merges/exec.c:423 #1 0x00005555561edeab in address_space_map (as=<optimised out>, addr=1106048, plen=<optimised out>, is_write=false)

    at /home/petmay01/linaro/qemu-for-merges/exec.c:2909

#2 0x0000555556840b9b in ahci_populate_sglist (as=0x55555a46bfc0, addr=1106048, dir=DMA_DIRECTION_TO_DEVICE, len=<optimised out>)
at /home/petmay01/linaro/qemu-for-merges/include/sysemu/dma.h:135
#3 0x0000555556840b9b in ahci_populate_sglist (ad=<optimised out>, sglist=<optimised out>, cmd=<optimised out>, limit=<optimised out>, offset=1592) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:863
#4 0x0000555556844de4 in ahci_dma_prepare_buf (dma=0x55555a475b48, limit=<optimised out>)

    at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1366

#5 0x000055555684354c in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1295
#6 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #7 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318
#8 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #9 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #10 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #11 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #12 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #13 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #14 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #15 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #16 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #17 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #18 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #19 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #20 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #21 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #22 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #23 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #24 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #25 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #26 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #27 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #28 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #29 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #30 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #31 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #32 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #33 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #34 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #35 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #36 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #37 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #38 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #39 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #40 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #41 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318

[skip a lot of repeated stack frames]

#393 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #394 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #395 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #396 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #397 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #398 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #399 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #400 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #401 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #402 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #403 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #404 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #405 0x0000555556809cfc in ide_buffered_readv_cb (opaque=0x5555594f57e0, ret=<optimised out>)

    at /home/petmay01/linaro/qemu-for-merges/hw/ide/core.c:605

#406 0x0000555556df7f73 in blk_aio_complete (acb=0x55555a4387c0) at /home/petmay01/linaro/qemu-for-merges/block/block-backend.c:943 #407 0x0000555556f676f1 in coroutine_trampoline (i0=<optimised out>, i1=<optimised out>)
at /home/petmay01/linaro/qemu-for-merges/util/coroutine-ucontext.c:79
#408 0x00007fffdca05590 in __start_context () at /lib/x86_64-linux-gnu/libc.so.6 #409 0x00007fffffffc318 in ()
#410 0x0000000000000000 in ()

thanks
-- PMM

author
#25 lore raw

Am 28.09.2016 um 21:03 hat Peter Maydell geschrieben:

Quoted 49 lines · Kevin Wolf
Quoted 40 lines

Am 27.09.2016 um 21:42 hat Peter Maydell geschrieben:

Quoted 25 lines · Kevin Wolf
Quoted 9 lines

The following changes since commit 7cfdc02dae0d2ff58c897496cfdbbafc0eda0f3f:

Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging (2016-09-26 19:47:00 +0100)

are available in the git repository at:

git://repo.or.cz/qemu/kevin.git tags/for-upstream

Quoted 8 lines

for you to fetch changes up to 3b856cebe5e93547852c156ca2119d075e62aed7:

coroutine: reduce stack size to 60kB (2016-09-27 14:05:21 +0200)

----------------------------------------------------------------
Block layer patches

----------------------------------------------------------------

I see 'make check' failures on x86-64 host, clang Linux:

  /i386/ahci/migrate/ncq/simple:                                       OK
  /i386/ahci/migrate/ncq/halted:                                       OK
  /i386/ahci/cdrom/dma/single:                                         OK
  /i386/ahci/cdrom/dma/multi:                                          OK
Quoted 13 lines

/i386/ahci/cdrom/pio/single:
Broken pipe
FAIL
GTester: last random seed: R02Sa8f729848b07c3b3e5ee67368f9d0350 (pid=10590)
/i386/ahci/cdrom/pio/multi:
Broken pipe
FAIL
GTester: last random seed: R02Se85704e04bbd382223983c878723b811 (pid=10598)
FAIL: tests/ahci-test
TEST: tests/hd-geo-test... (pid=10601)
/i386/hd-geo/ide/none: OK

Quoted 2 lines

I asked on IRC, but as you don't seem to be around at the moment, I'll keep things on the list instead.

I got a gdb backtrace:

Thread 1 "qemu-system-i38" received signal SIGSEGV, Segmentation fault. 0x00005555561dea15 in address_space_translate (as=0x55555a46bfc0, addr=1106048, xlat=0x7ffff7e0d050, plen=0x7ffff7e0d058,

Quoted 6 lines
    is_write=false) at /home/petmay01/linaro/qemu-for-merges/exec.c:423

423 {

Backtrace suggests we've run out of stack due to some infinite recursion:

Thanks, Peter, this is useful.

The series contains a patch that reduces the coroutine stack size, so I guess it's not quite infinite, but pretty deep recursion anyway. I will drop that final patch that reduces the stack size and hope that the rest will pass your testing (I tried some more to reproduce it, but I still didn't manage to).

John, can you have a look at the IDE code and check whether we can get rid of the deep recursion? It seems that the test issues a large request that is then split into many small requests. But it should be possible to do this iteratively rather than recursively.

Kevin

Quoted 132 lines

#0 0x00005555561dea15 in address_space_translate (as=0x55555a46bfc0, addr=1106048, xlat=0x7ffff7e0d050, plen=0x7ffff7e0d058, is_write=false) at /home/petmay01/linaro/qemu-for-merges/exec.c:423 #1 0x00005555561edeab in address_space_map (as=<optimised out>, addr=1106048, plen=<optimised out>, is_write=false)

    at /home/petmay01/linaro/qemu-for-merges/exec.c:2909

#2 0x0000555556840b9b in ahci_populate_sglist (as=0x55555a46bfc0, addr=1106048, dir=DMA_DIRECTION_TO_DEVICE, len=<optimised out>)
at /home/petmay01/linaro/qemu-for-merges/include/sysemu/dma.h:135
#3 0x0000555556840b9b in ahci_populate_sglist (ad=<optimised out>, sglist=<optimised out>, cmd=<optimised out>, limit=<optimised out>, offset=1592) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:863
#4 0x0000555556844de4 in ahci_dma_prepare_buf (dma=0x55555a475b48, limit=<optimised out>)

    at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1366

#5 0x000055555684354c in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1295
#6 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #7 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318
#8 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #9 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #10 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #11 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #12 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #13 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #14 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #15 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #16 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #17 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #18 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #19 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #20 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #21 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #22 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #23 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #24 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #25 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #26 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #27 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #28 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #29 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #30 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #31 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #32 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #33 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #34 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #35 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #36 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #37 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #38 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #39 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #40 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #41 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318

[skip a lot of repeated stack frames]

#393 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #394 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #395 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #396 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #397 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #398 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #399 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #400 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #401 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #402 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #403 0x0000555556843662 in ahci_start_transfer (dma=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/ahci.c:1318 #404 0x00005555568250cb in ide_atapi_cmd_reply_end (s=<optimised out>) at /home/petmay01/linaro/qemu-for-merges/hw/ide/atapi.c:324 #405 0x0000555556809cfc in ide_buffered_readv_cb (opaque=0x5555594f57e0, ret=<optimised out>)

    at /home/petmay01/linaro/qemu-for-merges/hw/ide/core.c:605

#406 0x0000555556df7f73 in blk_aio_complete (acb=0x55555a4387c0) at /home/petmay01/linaro/qemu-for-merges/block/block-backend.c:943 #407 0x0000555556f676f1 in coroutine_trampoline (i0=<optimised out>, i1=<optimised out>)
at /home/petmay01/linaro/qemu-for-merges/util/coroutine-ucontext.c:79
#408 0x00007fffdca05590 in __start_context () at /lib/x86_64-linux-gnu/libc.so.6 #409 0x00007fffffffc318 in ()
#410 0x0000000000000000 in ()

thanks
-- PMM

#26 lore raw
Quoted 4 lines · Kevin Wolf

John, can you have a look at the IDE code and check whether we can get rid of the deep recursion? It seems that the test issues a large request that is then split into many small requests. But it should be possible to do this iteratively rather than recursively.

http://wiki.qemu.org/Features/IDE

"Rewrite ide_atapi_cmd_reply_end to be fully non-recursive; and ideally less confusing."

Guess I have to float this one to the top.

--js

#27 lore raw
Quoted 8 lines · John Snow
Quoted 4 lines · Kevin Wolf

John, can you have a look at the IDE code and check whether we can get rid of the deep recursion? It seems that the test issues a large request that is then split into many small requests. But it should be possible to do this iteratively rather than recursively.

Quoted 6 lines

http://wiki.qemu.org/Features/IDE

"Rewrite ide_atapi_cmd_reply_end to be fully non-recursive; and ideally less confusing."

Guess I have to float this one to the top.

Would it be enough to hide the call to s->bus->dma->ops->start_transfer behind a bottom half?

Paolo

#28 lore raw
Quoted 16 lines · Paolo Bonzini
Quoted 13 lines · John Snow
Quoted 4 lines · Kevin Wolf

John, can you have a look at the IDE code and check whether we can get rid of the deep recursion? It seems that the test issues a large request that is then split into many small requests. But it should be possible to do this iteratively rather than recursively.

http://wiki.qemu.org/Features/IDE

"Rewrite ide_atapi_cmd_reply_end to be fully non-recursive; and ideally less confusing."

Quoted 7 lines
Quoted 1 line

Guess I have to float this one to the top.

Would it be enough to hide the call to s->bus->dma->ops->start_transfer behind a bottom half?

Paolo

Probably the simplest way, even if not the prettiest.

#29 lore raw
In reply to Kevin Wolf
Quoted 5 lines · Kevin Wolf

The series contains a patch that reduces the coroutine stack size, so I guess it's not quite infinite, but pretty deep recursion anyway. I will drop that final patch that reduces the stack size and hope that the rest will pass your testing (I tried some more to reproduce it, but I still didn't manage to).

Ah, I see. I suspect the clang build (since it has the sanitizer enabled) is a worst-case for stack usage.

Is it possible for a guest to issue a sufficiently large request that it blows the stack, or is that capped somehow?

thanks
-- PMM

#30 lore raw
Quoted 10 lines · Peter Maydell
Quoted 5 lines · Kevin Wolf

The series contains a patch that reduces the coroutine stack size, so I guess it's not quite infinite, but pretty deep recursion anyway. I will drop that final patch that reduces the stack size and hope that the rest will pass your testing (I tried some more to reproduce it, but I still didn't manage to).

Ah, I see. I suspect the clang build (since it has the sanitizer enabled) is a worst-case for stack usage.

Quoted 7 lines

Is it possible for a guest to issue a sufficiently large request that it blows the stack, or is that capped somehow?

thanks
-- PMM

If the qtest can blow the stack, so can a guest. In practice it does not happen that guests send requests so large, but mum's the word on a naughty guest.

--js