From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 24/25] block: Make bdrv_next() keep strong references
Date: Fri, 17 Nov 2017 19:16:52 +0100 [thread overview]
Message-ID: <20171117181653.20651-25-kwolf@redhat.com> (raw)
In-Reply-To: <20171117181653.20651-1-kwolf@redhat.com>
From: Max Reitz <mreitz@redhat.com>
On one hand, it is a good idea for bdrv_next() to return a strong
reference because ideally nearly every pointer should be refcounted.
This fixes intermittent failure of iotest 194.
On the other, it is absolutely necessary for bdrv_next() itself to keep
a strong reference to both the BB (in its first phase) and the BDS (at
least in the second phase) because when called the next time, it will
dereference those objects to get a link to the next one. Therefore, it
needs these objects to stay around until then. Just storing the pointer
to the next in the iterator is not really viable because that pointer
might become invalid as well.
Both arguments taken together means we should probably just invoke
bdrv_ref() and blk_ref() in bdrv_next(). This means we have to assert
that bdrv_next() is always called from the main loop, but that was
probably necessary already before this patch and judging from the
callers, it also looks to actually be the case.
Keeping these strong references means however that callers need to give
them up if they decide to abort the iteration early. They can do so
through the new bdrv_next_cleanup() function.
Suggested-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20171110172545.32609-1-mreitz@redhat.com
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
include/block/block.h | 1 +
block.c | 3 +++
block/block-backend.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
block/snapshot.c | 6 ++++++
migration/block.c | 1 +
5 files changed, 57 insertions(+), 2 deletions(-)
diff --git a/include/block/block.h b/include/block/block.h
index fbc21daf62..c05cac57e5 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -461,6 +461,7 @@ typedef struct BdrvNextIterator {
BlockDriverState *bdrv_first(BdrvNextIterator *it);
BlockDriverState *bdrv_next(BdrvNextIterator *it);
+void bdrv_next_cleanup(BdrvNextIterator *it);
BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs);
bool bdrv_is_encrypted(BlockDriverState *bs);
diff --git a/block.c b/block.c
index 996778cfa0..6c8ef98dfa 100644
--- a/block.c
+++ b/block.c
@@ -4255,6 +4255,7 @@ void bdrv_invalidate_cache_all(Error **errp)
aio_context_release(aio_context);
if (local_err) {
error_propagate(errp, local_err);
+ bdrv_next_cleanup(&it);
return;
}
}
@@ -4330,6 +4331,7 @@ int bdrv_inactivate_all(void)
for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
ret = bdrv_inactivate_recurse(bs, pass);
if (ret < 0) {
+ bdrv_next_cleanup(&it);
goto out;
}
}
@@ -4864,6 +4866,7 @@ bool bdrv_is_first_non_filter(BlockDriverState *candidate)
/* candidate is the first non filter */
if (perm) {
+ bdrv_next_cleanup(&it);
return true;
}
}
diff --git a/block/block-backend.c b/block/block-backend.c
index f10b1db612..5836cb3087 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -442,21 +442,37 @@ BlockBackend *blk_next(BlockBackend *blk)
* the monitor or attached to a BlockBackend */
BlockDriverState *bdrv_next(BdrvNextIterator *it)
{
- BlockDriverState *bs;
+ BlockDriverState *bs, *old_bs;
+
+ /* Must be called from the main loop */
+ assert(qemu_get_current_aio_context() == qemu_get_aio_context());
/* First, return all root nodes of BlockBackends. In order to avoid
* returning a BDS twice when multiple BBs refer to it, we only return it
* if the BB is the first one in the parent list of the BDS. */
if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
+ BlockBackend *old_blk = it->blk;
+
+ old_bs = old_blk ? blk_bs(old_blk) : NULL;
+
do {
it->blk = blk_all_next(it->blk);
bs = it->blk ? blk_bs(it->blk) : NULL;
} while (it->blk && (bs == NULL || bdrv_first_blk(bs) != it->blk));
+ if (it->blk) {
+ blk_ref(it->blk);
+ }
+ blk_unref(old_blk);
+
if (bs) {
+ bdrv_ref(bs);
+ bdrv_unref(old_bs);
return bs;
}
it->phase = BDRV_NEXT_MONITOR_OWNED;
+ } else {
+ old_bs = it->bs;
}
/* Then return the monitor-owned BDSes without a BB attached. Ignore all
@@ -467,18 +483,46 @@ BlockDriverState *bdrv_next(BdrvNextIterator *it)
bs = it->bs;
} while (bs && bdrv_has_blk(bs));
+ if (bs) {
+ bdrv_ref(bs);
+ }
+ bdrv_unref(old_bs);
+
return bs;
}
-BlockDriverState *bdrv_first(BdrvNextIterator *it)
+static void bdrv_next_reset(BdrvNextIterator *it)
{
*it = (BdrvNextIterator) {
.phase = BDRV_NEXT_BACKEND_ROOTS,
};
+}
+BlockDriverState *bdrv_first(BdrvNextIterator *it)
+{
+ bdrv_next_reset(it);
return bdrv_next(it);
}
+/* Must be called when aborting a bdrv_next() iteration before
+ * bdrv_next() returns NULL */
+void bdrv_next_cleanup(BdrvNextIterator *it)
+{
+ /* Must be called from the main loop */
+ assert(qemu_get_current_aio_context() == qemu_get_aio_context());
+
+ if (it->phase == BDRV_NEXT_BACKEND_ROOTS) {
+ if (it->blk) {
+ bdrv_unref(blk_bs(it->blk));
+ blk_unref(it->blk);
+ }
+ } else {
+ bdrv_unref(it->bs);
+ }
+
+ bdrv_next_reset(it);
+}
+
/*
* Add a BlockBackend into the list of backends referenced by the monitor, with
* the given @name acting as the handle for the monitor.
diff --git a/block/snapshot.c b/block/snapshot.c
index 1d5ab5f90f..be0743abac 100644
--- a/block/snapshot.c
+++ b/block/snapshot.c
@@ -417,6 +417,7 @@ bool bdrv_all_can_snapshot(BlockDriverState **first_bad_bs)
}
aio_context_release(ctx);
if (!ok) {
+ bdrv_next_cleanup(&it);
goto fail;
}
}
@@ -444,6 +445,7 @@ int bdrv_all_delete_snapshot(const char *name, BlockDriverState **first_bad_bs,
}
aio_context_release(ctx);
if (ret < 0) {
+ bdrv_next_cleanup(&it);
goto fail;
}
}
@@ -469,6 +471,7 @@ int bdrv_all_goto_snapshot(const char *name, BlockDriverState **first_bad_bs)
}
aio_context_release(ctx);
if (err < 0) {
+ bdrv_next_cleanup(&it);
goto fail;
}
}
@@ -494,6 +497,7 @@ int bdrv_all_find_snapshot(const char *name, BlockDriverState **first_bad_bs)
}
aio_context_release(ctx);
if (err < 0) {
+ bdrv_next_cleanup(&it);
goto fail;
}
}
@@ -525,6 +529,7 @@ int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,
}
aio_context_release(ctx);
if (err < 0) {
+ bdrv_next_cleanup(&it);
goto fail;
}
}
@@ -548,6 +553,7 @@ BlockDriverState *bdrv_all_find_vmstate_bs(void)
aio_context_release(ctx);
if (found) {
+ bdrv_next_cleanup(&it);
break;
}
}
diff --git a/migration/block.c b/migration/block.c
index 3282809583..7147171bb7 100644
--- a/migration/block.c
+++ b/migration/block.c
@@ -415,6 +415,7 @@ static int init_blk_migration(QEMUFile *f)
sectors = bdrv_nb_sectors(bs);
if (sectors <= 0) {
ret = sectors;
+ bdrv_next_cleanup(&it);
goto out;
}
--
2.13.6
next prev parent reply other threads:[~2017-11-17 18:17 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-17 18:16 [Qemu-devel] [PULL 00/25] Block layer patches for 2.11.0-rc2 Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 01/25] replication: Fix replication open fail Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 02/25] qemu-iotests: Use -nographic in 182 Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 03/25] block: Fix error path in bdrv_backing_update_filename() Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 04/25] qcow2: don't permit changing encryption parameters Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 05/25] block: Deprecate bdrv_set_read_only() and users Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 06/25] qcow2: fix image corruption after committing qcow2 image into base Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 07/25] block: Fix permissions in image activation Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 08/25] iotests: test clearing unknown autoclear_features by qcow2 Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 09/25] qcow2: fix image corruption on commit with persistent bitmap Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 10/25] qapi/qnull: Add own header Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 11/25] qapi/qlist: Add qlist_append_null() macro Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 12/25] qapi: Add qobject_is_equal() Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 13/25] block: qobject_is_equal() in bdrv_reopen_prepare() Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 14/25] iotests: Add test for non-string option reopening Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 15/25] tests: Add check-qobject for equality tests Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 16/25] iotests: Add test for failing qemu-img commit Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 17/25] qcow2: reject unaligned offsets in write compressed Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 18/25] qcow2: check_errors are fatal Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 19/25] qcow2: Unaligned zero cluster in handle_alloc() Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 20/25] block: Guard against NULL bs->drv Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 21/25] qcow2: Add bounds check to get_refblock_offset() Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 22/25] qcow2: Refuse to get unaligned offsets from cache Kevin Wolf
2017-11-17 18:16 ` [Qemu-devel] [PULL 23/25] qcow2: Fix overly broad madvise() Kevin Wolf
2017-11-17 18:16 ` Kevin Wolf [this message]
2017-11-17 18:16 ` [Qemu-devel] [PULL 25/25] iotests: Make 087 pass without AIO enabled Kevin Wolf
2017-11-20 14:53 ` [Qemu-devel] [PULL 00/25] Block layer patches for 2.11.0-rc2 Peter Maydell
2017-11-20 15:24 ` Kevin Wolf
2017-11-20 17:16 ` Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171117181653.20651-25-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).