From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
Fam Zheng <famz@redhat.com>,
qemu-block@nongnu.org, Juan Quintela <quintela@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
Amit Shah <amit.shah@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [PULL 10/23] block-migration: acquire AioContext as necessary
Date: Wed, 24 Feb 2016 22:35:37 +0200 [thread overview]
Message-ID: <1456343639-3471-11-git-send-email-mst@redhat.com> (raw)
In-Reply-To: <1456343639-3471-1-git-send-email-mst@redhat.com>
From: Paolo Bonzini <pbonzini@redhat.com>
This is needed because dataplane will run during block migration as well.
The block device migration code is quite liberal in taking the iothread
mutex. For simplicity, keep it the same way, even though one could
actually choose between the BQL (for regular BlockDriverStates) and
the AioContext (for dataplane BlockDriverStates). When the block layer
is made fully thread safe, aio_context_acquire shall go away altogether.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
---
migration/block.c | 65 ++++++++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 52 insertions(+), 13 deletions(-)
diff --git a/migration/block.c b/migration/block.c
index 3a8330a..72883d7 100644
--- a/migration/block.c
+++ b/migration/block.c
@@ -54,17 +54,25 @@ typedef struct BlkMigDevState {
int shared_base;
int64_t total_sectors;
QSIMPLEQ_ENTRY(BlkMigDevState) entry;
+ Error *blocker;
/* Only used by migration thread. Does not need a lock. */
int bulk_completed;
int64_t cur_sector;
int64_t cur_dirty;
- /* Protected by block migration lock. */
+ /* Data in the aio_bitmap is protected by block migration lock.
+ * Allocation and free happen during setup and cleanup respectively.
+ */
unsigned long *aio_bitmap;
+
+ /* Protected by block migration lock. */
int64_t completed_sectors;
+
+ /* During migration this is protected by iothread lock / AioContext.
+ * Allocation and free happen during setup and cleanup respectively.
+ */
BdrvDirtyBitmap *dirty_bitmap;
- Error *blocker;
} BlkMigDevState;
typedef struct BlkMigBlock {
@@ -100,7 +108,7 @@ typedef struct BlkMigState {
int prev_progress;
int bulk_completed;
- /* Lock must be taken _inside_ the iothread lock. */
+ /* Lock must be taken _inside_ the iothread lock and any AioContexts. */
QemuMutex lock;
} BlkMigState;
@@ -264,11 +272,13 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
if (bmds->shared_base) {
qemu_mutex_lock_iothread();
+ aio_context_acquire(bdrv_get_aio_context(bs));
while (cur_sector < total_sectors &&
!bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
&nr_sectors)) {
cur_sector += nr_sectors;
}
+ aio_context_release(bdrv_get_aio_context(bs));
qemu_mutex_unlock_iothread();
}
@@ -302,11 +312,21 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
block_mig_state.submitted++;
blk_mig_unlock();
+ /* We do not know if bs is under the main thread (and thus does
+ * not acquire the AioContext when doing AIO) or rather under
+ * dataplane. Thus acquire both the iothread mutex and the
+ * AioContext.
+ *
+ * This is ugly and will disappear when we make bdrv_* thread-safe,
+ * without the need to acquire the AioContext.
+ */
qemu_mutex_lock_iothread();
+ aio_context_acquire(bdrv_get_aio_context(bmds->bs));
blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
nr_sectors, blk_mig_read_cb, blk);
bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector, nr_sectors);
+ aio_context_release(bdrv_get_aio_context(bmds->bs));
qemu_mutex_unlock_iothread();
bmds->cur_sector = cur_sector + nr_sectors;
@@ -321,8 +341,10 @@ static int set_dirty_tracking(void)
int ret;
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
+ aio_context_acquire(bdrv_get_aio_context(bmds->bs));
bmds->dirty_bitmap = bdrv_create_dirty_bitmap(bmds->bs, BLOCK_SIZE,
NULL, NULL);
+ aio_context_release(bdrv_get_aio_context(bmds->bs));
if (!bmds->dirty_bitmap) {
ret = -errno;
goto fail;
@@ -333,18 +355,24 @@ static int set_dirty_tracking(void)
fail:
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
if (bmds->dirty_bitmap) {
+ aio_context_acquire(bdrv_get_aio_context(bmds->bs));
bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
+ aio_context_release(bdrv_get_aio_context(bmds->bs));
}
}
return ret;
}
+/* Called with iothread lock taken. */
+
static void unset_dirty_tracking(void)
{
BlkMigDevState *bmds;
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
+ aio_context_acquire(bdrv_get_aio_context(bmds->bs));
bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
+ aio_context_release(bdrv_get_aio_context(bmds->bs));
}
}
@@ -444,7 +472,7 @@ static void blk_mig_reset_dirty_cursor(void)
}
}
-/* Called with iothread lock taken. */
+/* Called with iothread lock and AioContext taken. */
static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
int is_async)
@@ -527,7 +555,9 @@ static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
int ret = 1;
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
+ aio_context_acquire(bdrv_get_aio_context(bmds->bs));
ret = mig_save_device_dirty(f, bmds, is_async);
+ aio_context_release(bdrv_get_aio_context(bmds->bs));
if (ret <= 0) {
break;
}
@@ -585,7 +615,9 @@ static int64_t get_remaining_dirty(void)
int64_t dirty = 0;
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
+ aio_context_acquire(bdrv_get_aio_context(bmds->bs));
dirty += bdrv_get_dirty_count(bmds->dirty_bitmap);
+ aio_context_release(bdrv_get_aio_context(bmds->bs));
}
return dirty << BDRV_SECTOR_BITS;
@@ -597,21 +629,28 @@ static void block_migration_cleanup(void *opaque)
{
BlkMigDevState *bmds;
BlkMigBlock *blk;
+ AioContext *ctx;
bdrv_drain_all();
unset_dirty_tracking();
- blk_mig_lock();
while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
bdrv_op_unblock_all(bmds->bs, bmds->blocker);
error_free(bmds->blocker);
+
+ /* Save ctx, because bmds->bs can disappear during bdrv_unref. */
+ ctx = bdrv_get_aio_context(bmds->bs);
+ aio_context_acquire(ctx);
bdrv_unref(bmds->bs);
+ aio_context_release(ctx);
+
g_free(bmds->aio_bitmap);
g_free(bmds);
}
+ blk_mig_lock();
while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
g_free(blk->buf);
@@ -633,13 +672,12 @@ static int block_save_setup(QEMUFile *f, void *opaque)
/* start track dirty blocks */
ret = set_dirty_tracking();
+ qemu_mutex_unlock_iothread();
+
if (ret) {
- qemu_mutex_unlock_iothread();
return ret;
}
- qemu_mutex_unlock_iothread();
-
ret = flush_blks(f);
blk_mig_reset_dirty_cursor();
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
@@ -761,17 +799,18 @@ static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
uint64_t pending;
qemu_mutex_lock_iothread();
+ pending = get_remaining_dirty();
+ qemu_mutex_unlock_iothread();
+
blk_mig_lock();
- pending = get_remaining_dirty() +
- block_mig_state.submitted * BLOCK_SIZE +
- block_mig_state.read_done * BLOCK_SIZE;
+ pending += block_mig_state.submitted * BLOCK_SIZE +
+ block_mig_state.read_done * BLOCK_SIZE;
+ blk_mig_unlock();
/* Report at least one block pending during bulk phase */
if (pending <= max_size && !block_mig_state.bulk_completed) {
pending = max_size + BLOCK_SIZE;
}
- blk_mig_unlock();
- qemu_mutex_unlock_iothread();
DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
/* We don't do postcopy */
--
MST
next prev parent reply other threads:[~2016-02-24 20:35 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-24 20:35 [Qemu-devel] [PULL 00/23] vhost, virtio, pci, pc Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 01/23] bios-linker-loader: document+validate input Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 02/23] vhost-user: don't merge regions with different fds Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 03/23] move get_current_ram_size to virtio-balloon.c Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 04/23] pc-dimm: rename pc_dimm_built_list() Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 05/23] pc-dimm: add pc_dimm_build_list() Michael S. Tsirkin
2016-02-25 7:01 ` Vladimir Sementsov-Ogievskiy
2016-02-25 8:39 ` Michael S. Tsirkin
2016-02-25 8:55 ` Vladimir Sementsov-Ogievskiy
2016-02-25 9:11 ` Michael S. Tsirkin
2016-02-25 9:54 ` Vladimir Sementsov-Ogievskiy
2016-02-25 10:09 ` Paolo Bonzini
2016-02-25 10:12 ` Michael S. Tsirkin
2016-02-25 10:22 ` Michael S. Tsirkin
2016-02-26 9:08 ` Vladimir Sementsov-Ogievskiy
2016-02-26 9:08 ` Vladimir Sementsov-Ogievskiy
2016-02-24 20:35 ` [Qemu-devel] [PULL 06/23] virtio-balloon: rewrite get_current_ram_size() Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 07/23] balloon: Use only 'pc-dimm' type dimm for ballooning Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 08/23] pci core: function pci_host_bus_register() cleanup Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 09/23] pci core: function pci_bus_init() cleanup Michael S. Tsirkin
2016-02-24 20:35 ` Michael S. Tsirkin [this message]
2016-02-24 20:35 ` [Qemu-devel] [PULL 11/23] vring: make vring_enable_notification return void Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 12/23] virtio: add AioContext-specific function for host notifiers Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 13/23] virtio: export vring_notify as virtio_should_notify Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 14/23] virtio-blk: fix "disabled data plane" mode Michael S. Tsirkin
2016-02-24 20:35 ` [Qemu-devel] [PULL 15/23] virtio-blk: do not use vring in dataplane Michael S. Tsirkin
2016-02-24 20:36 ` [Qemu-devel] [PULL 16/23] virtio-scsi: " Michael S. Tsirkin
2016-02-24 20:36 ` [Qemu-devel] [PULL 17/23] vring: remove Michael S. Tsirkin
2016-02-24 20:36 ` [Qemu-devel] [PULL 18/23] tests/vhost-user-bridge: fix build on 32 bit systems Michael S. Tsirkin
2016-02-24 20:36 ` [Qemu-devel] [PULL 19/23] q35: Remove old machine versions Michael S. Tsirkin
2016-02-24 20:36 ` [Qemu-devel] [PULL 20/23] machine: Remove no_tco field Michael S. Tsirkin
2016-02-24 20:36 ` [Qemu-devel] [PULL 21/23] ich9: Remove enable_tco arguments from init functions Michael S. Tsirkin
2016-02-24 20:36 ` [Qemu-devel] [PULL 22/23] q35: Remove unused q35-acpi-dsdt.aml file Michael S. Tsirkin
2016-02-24 20:36 ` [Qemu-devel] [PULL 23/23] q35: No need to check gigabyte_align Michael S. Tsirkin
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=1456343639-3471-11-git-send-email-mst@redhat.com \
--to=mst@redhat.com \
--cc=amit.shah@redhat.com \
--cc=famz@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--cc=stefanha@redhat.com \
/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).