From patchwork Wed Jan 20 07:12:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Denis V. Lunev" X-Patchwork-Id: 8069341 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 1BE34BEEE5 for ; Wed, 20 Jan 2016 07:12:47 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5F9F220268 for ; Wed, 20 Jan 2016 07:12:46 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 786F620265 for ; Wed, 20 Jan 2016 07:12:45 +0000 (UTC) Received: from localhost ([::1]:40622 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLmwe-0001kb-I6 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 20 Jan 2016 02:12:44 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46488) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLmwT-0001k1-HM for qemu-devel@nongnu.org; Wed, 20 Jan 2016 02:12:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aLmwQ-0000hk-Hr for qemu-devel@nongnu.org; Wed, 20 Jan 2016 02:12:33 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:3542 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLmwQ-0000fn-5f for qemu-devel@nongnu.org; Wed, 20 Jan 2016 02:12:30 -0500 Received: from hades.sw.ru ([10.30.8.132]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id u0J7Q7c1024401; Tue, 19 Jan 2016 10:26:11 +0300 (MSK) From: "Denis V. Lunev" To: Date: Wed, 20 Jan 2016 10:12:19 +0300 Message-Id: <1453273940-15382-2-git-send-email-den@openvz.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1453273940-15382-1-git-send-email-den@openvz.org> References: <1453273940-15382-1-git-send-email-den@openvz.org> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x X-Received-From: 195.214.232.25 Cc: Kevin Wolf , "Denis V. Lunev" , qemu-devel@nongnu.org, Paolo Bonzini Subject: [Qemu-devel] [PATCH 1/2] block: fix assert in qcow2_get_specific_info X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP There is a possibility to hit assert qcow2_get_specific_info that s->qcow_version is undefined. This happens when VM in starting from suspended state, i.e. it processes incoming migration, and in the same time 'info block' is called. The problem is that in the qcow2_invalidate_cache closes and the image and memsets BDRVQcowState in the middle. The patch moves processing of qcow2_get_specific_info into coroutine context and ensures that qcow2_invalidate_cache and qcow2_get_specific_info can not run simultaneosly. Signed-off-by: Denis V. Lunev CC: Kevin Wolf CC: Paolo Bonzini --- block/qcow2.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- block/qcow2.h | 2 ++ 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 1789af4..12eda24 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1740,6 +1740,10 @@ static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp) Error *local_err = NULL; int ret; + qemu_co_mutex_lock(&s->lock); + s->in_transient_state = true; + qemu_co_mutex_unlock(&s->lock); + /* * Backing files are read-only which makes all of their metadata immutable, * that means we don't have to worry about reopening them here. @@ -1753,10 +1757,10 @@ static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp) bdrv_invalidate_cache(bs->file->bs, &local_err); if (local_err) { error_propagate(errp, local_err); - return; + goto done; } - memset(s, 0, sizeof(BDRVQcow2State)); + memset(s, 0, offsetof(BDRVQcow2State, in_transient_state)); options = qdict_clone_shallow(bs->options); ret = qcow2_open(bs, options, flags, &local_err); @@ -1765,13 +1769,18 @@ static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp) error_setg(errp, "Could not reopen qcow2 layer: %s", error_get_pretty(local_err)); error_free(local_err); - return; + goto done; } else if (ret < 0) { error_setg_errno(errp, -ret, "Could not reopen qcow2 layer"); - return; + goto done; } s->cipher = cipher; + +done: + qemu_co_mutex_lock(&s->lock); + s->in_transient_state = false; + qemu_co_mutex_unlock(&s->lock); } static size_t header_ext_add(char *buf, uint32_t magic, const void *s, @@ -2778,11 +2787,21 @@ static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) return 0; } -static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs) + +static ImageInfoSpecific *qcow2_co_get_specific_info(BlockDriverState *bs) { BDRVQcow2State *s = bs->opaque; + AioContext *ctx = bdrv_get_aio_context(bs); + ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1); + qemu_co_mutex_lock(&s->lock); + while (s->in_transient_state) { + qemu_co_mutex_unlock(&s->lock); + aio_poll(ctx, true); + qemu_co_mutex_lock(&s->lock); + } + *spec_info = (ImageInfoSpecific){ .type = IMAGE_INFO_SPECIFIC_KIND_QCOW2, .u.qcow2 = g_new(ImageInfoSpecificQCow2, 1), @@ -2808,10 +2827,45 @@ static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs) * added without having it covered here */ assert(false); } + qemu_co_mutex_unlock(&s->lock); return spec_info; } +struct InfoCo { + BlockDriverState *bs; + ImageInfoSpecific *info; +}; + +static void qcow2_co_get_specific_info_entry(void *opaque) +{ + struct InfoCo *ret = opaque; + ret->info = qcow2_co_get_specific_info(ret->bs); +} + +static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs) +{ + Coroutine *co; + struct InfoCo info_co = { + .bs = bs, + }; + + if (qemu_in_coroutine()) { + /* Fast-path if already in coroutine context */ + qcow2_co_get_specific_info_entry(&info_co); + } else { + AioContext *aio_context = bdrv_get_aio_context(bs); + + co = qemu_coroutine_create(qcow2_co_get_specific_info_entry); + qemu_coroutine_enter(co, &info_co); + while (info_co.info == NULL) { + aio_poll(aio_context, true); + } + } + + return info_co.info; +} + #if 0 static void dump_refcounts(BlockDriverState *bs) { diff --git a/block/qcow2.h b/block/qcow2.h index a063a3c..1114528 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -293,6 +293,8 @@ typedef struct BDRVQcow2State { * override) */ char *image_backing_file; char *image_backing_format; + + bool in_transient_state; } BDRVQcow2State; typedef struct Qcow2COWRegion {