From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47626) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1apcbi-0004XT-Ei for qemu-devel@nongnu.org; Mon, 11 Apr 2016 10:14:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1apcbe-00027w-5f for qemu-devel@nongnu.org; Mon, 11 Apr 2016 10:14:26 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57425) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1apcbd-00027q-W9 for qemu-devel@nongnu.org; Mon, 11 Apr 2016 10:14:22 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 825E4BDD2 for ; Mon, 11 Apr 2016 14:14:21 +0000 (UTC) From: Stefan Hajnoczi Date: Mon, 11 Apr 2016 15:14:16 +0100 Message-Id: <1460384056-29494-1-git-send-email-stefanha@redhat.com> Subject: [Qemu-devel] [RFC for-2.7] block: keep AioContext pointer in BlockBackend List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Max Reitz , Kevin Wolf , Paolo Bonzini , Stefan Hajnoczi blk_get/set_aio_context() delegate to BlockDriverState without storing the AioContext pointer in BlockBackend. There are two flaws: 1. BlockBackend falls back to the QEMU main loop AioContext when there is no root BlockDriverState. This means the drive loses its AioContext during media change and would break dataplane. 2. BlockBackend state used from multiple threads has no lock. Race conditions will creep in as functionality is moved from BlockDriverState to BlockBackend due to the absense of a lock. The monitor cannot access BlockBackend state safely while an IOThread is also accessing the state. Both issue #1 and #2 are mostly theoretical at the moment. I haven't figured out a way to trigger #1 with virtio-blk (does not support media change) or virtio-scsi (blocks the eject operation). #2 may be possible with block accounting statistics in BlockBackend but I'm not aware of a crash that can be triggered. This patch stores the AioContext pointer in BlockBackend and puts newly inserted BlockDriverStates into the AioContext. Signed-off-by: Stefan Hajnoczi --- block/block-backend.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/block/block-backend.c b/block/block-backend.c index d74f670..e39c583 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -30,6 +30,7 @@ static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb); struct BlockBackend { char *name; int refcnt; + AioContext *aio_context; BdrvChild *root; DriveInfo *legacy_dinfo; /* null unless created by drive_new() */ QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */ @@ -106,6 +107,7 @@ BlockBackend *blk_new(Error **errp) blk = g_new0(BlockBackend, 1); blk->refcnt = 1; + blk->aio_context = qemu_get_aio_context(); notifier_list_init(&blk->remove_bs_notifiers); notifier_list_init(&blk->insert_bs_notifiers); QTAILQ_INSERT_TAIL(&block_backends, blk, link); @@ -433,6 +435,7 @@ void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs) { assert(!blk->root && !bs->blk); bdrv_ref(bs); + bdrv_set_aio_context(bs, blk->aio_context); blk->root = bdrv_root_attach_child(bs, "root", &child_root); bs->blk = blk; @@ -1348,13 +1351,7 @@ void blk_op_unblock_all(BlockBackend *blk, Error *reason) AioContext *blk_get_aio_context(BlockBackend *blk) { - BlockDriverState *bs = blk_bs(blk); - - if (bs) { - return bdrv_get_aio_context(bs); - } else { - return qemu_get_aio_context(); - } + return blk->aio_context; } static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb) @@ -1367,6 +1364,8 @@ void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) { BlockDriverState *bs = blk_bs(blk); + blk->aio_context = new_context; + if (bs) { bdrv_set_aio_context(bs, new_context); } -- 2.5.5