From nobody Mon May 6 20:31:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1571847896; cv=none; d=zoho.com; s=zohoarc; b=irIAZXzjsDaE8+HC3li5K5ZCSwgWwdhevOtMxII7BJVJMhrGyVPmGOemuwRsxMD6jM43gXYPw07cYgG5aRwEyPXFcxD0oZ1uguW8yGIVqSihQpMw4mn49zDLjdWbnr7U4hHkQJI7ptVCZvyJ/InPVEzS5sp3Um5QeIpUJ2i8484= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571847896; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=v5LhvtZSfFq0VX5A7OuDSQtwwsHYnVMcy/wI7iSilQc=; b=SAl1schjHsGOqntJ4wxPmj6f9xDXo9XSQo30YJP2nBJY9Kg9HI67DMBccaatLJY5auCurxsR1h01gibuVYzfwvWHR8Cto0SgORwpRLkXTo9CRfTVIBnHL30G4U9kUujq83GtUG9Izd62HDrC9y97p5/71aNLbemgJuO832UFn0Y= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1571847896971995.8227535311656; Wed, 23 Oct 2019 09:24:56 -0700 (PDT) Received: from localhost ([::1]:41106 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNJRP-0000AE-KY for importer@patchew.org; Wed, 23 Oct 2019 12:24:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39782) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNIXJ-0005Gh-Gd for qemu-devel@nongnu.org; Wed, 23 Oct 2019 11:26:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNIXH-0002il-Dz for qemu-devel@nongnu.org; Wed, 23 Oct 2019 11:26:57 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:38037 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNIXF-0002gq-De for qemu-devel@nongnu.org; Wed, 23 Oct 2019 11:26:55 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-188-T5EIT1y5NdaTF3P0VLOOoA-1; Wed, 23 Oct 2019 11:26:48 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B3326800D4E; Wed, 23 Oct 2019 15:26:46 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-192.ams2.redhat.com [10.36.117.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8101960852; Wed, 23 Oct 2019 15:26:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571844411; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v5LhvtZSfFq0VX5A7OuDSQtwwsHYnVMcy/wI7iSilQc=; b=NhFLODuWfKE/wCsgupk+uRrCWauDpTZ0FLghGEfBD0hiSVBwvcCs52udny7B2Ept1kU7Q9 aawhQfcGY4R2VzecKPxkE4vAb49+QeiBAQz8uxtUl6poya+DyVOqGsy4cFYAzQTPVMpbgk dSXBIKfZDIZSvsch9PS5M6nZ8USZePQ= From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH 1/3] coroutine: Add qemu_co_mutex_assert_locked() Date: Wed, 23 Oct 2019 17:26:18 +0200 Message-Id: <20191023152620.13166-2-kwolf@redhat.com> In-Reply-To: <20191023152620.13166-1-kwolf@redhat.com> References: <20191023152620.13166-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: T5EIT1y5NdaTF3P0VLOOoA-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, psyhomb@gmail.com, michael@weiser.dinsnail.net, vsementsov@virtuozzo.com, den@virtuozzo.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org, dgilbert@redhat.com, mreitz@redhat.com, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Some functions require that the caller holds a certain CoMutex for them to operate correctly. Add a function so that they can assert the lock is really held. Cc: qemu-stable@nongnu.org Signed-off-by: Kevin Wolf --- include/qemu/coroutine.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 9801e7f5a4..a36bcfe5c8 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -167,6 +167,13 @@ void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex); */ void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex); =20 +/** + * Assert that the current coroutine holds @mutex. + */ +static inline coroutine_fn void qemu_co_mutex_assert_locked(CoMutex *mutex) +{ + assert(mutex->locked && mutex->holder =3D=3D qemu_coroutine_self()); +} =20 /** * CoQueues are a mechanism to queue coroutines in order to continue execu= ting --=20 2.20.1 From nobody Mon May 6 20:31:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1571846092; cv=none; d=zoho.com; s=zohoarc; b=kl98WoZdshp96vIKIVFeHCMKBhKLx1j639PBIQiCyfwh/KItIoskXgS+Atg8vFhsnlN8tvJzG3koE6b32mECkSvqhtX4/zkDodMGAwiQS8G/dKhxU7EbzAXqw0glSQMf11Ah2NfW8TKtdzdMoAJiC55AGLwbDVN/B1Jj/KCUYC0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571846092; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Qo4k4Dw+pnxV7TbicL18z1fOMWIRfsoSaltyjb9eTJE=; b=W0CkIXdeiLzzzbh5r3IE19JYUIkWt6b3+R1k1f8d4EyhqUOPFvPNHDMRZiYpS2JxqSpgT/5dRr1bO8N491mhGn80G2KRBCJ5Rt1u21Pft8NlUl2z1JwXKXSAYi4yeC37NdDGohDQ1jov9xS83HrTUEgbCWY94okyUfC3qeYJSnM= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1571846092727575.4975253370425; Wed, 23 Oct 2019 08:54:52 -0700 (PDT) Received: from localhost ([::1]:40170 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNIyJ-0003tY-7P for importer@patchew.org; Wed, 23 Oct 2019 11:54:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39844) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNIXN-0005ND-BD for qemu-devel@nongnu.org; Wed, 23 Oct 2019 11:27:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNIXM-0002lX-9J for qemu-devel@nongnu.org; Wed, 23 Oct 2019 11:27:01 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:32646 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNIXM-0002l9-5t for qemu-devel@nongnu.org; Wed, 23 Oct 2019 11:27:00 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-235-ySeDEXHHOMO2UOw_IhgC1w-1; Wed, 23 Oct 2019 11:26:56 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4F4F31005500; Wed, 23 Oct 2019 15:26:53 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-192.ams2.redhat.com [10.36.117.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0B7B4608C1; Wed, 23 Oct 2019 15:26:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571844419; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qo4k4Dw+pnxV7TbicL18z1fOMWIRfsoSaltyjb9eTJE=; b=erUmzOWtDr4Fj6M+m4nm0vJePxVB8PVg4mJ/hfjS74TOt1uhV9EErxtf9KJMRWxOgA0XGr jpWQIIySxJvjvwnf8Hz7jD7mwtzv8g8liXOsndiwUCnUuzBxBD6tZv8hABdY2bjhr85exD DQD89WSxQHACIPabDddy2pOzvICQmfk= From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH 2/3] qcow2: Assert that qcow2_cache_get() callers hold s->lock Date: Wed, 23 Oct 2019 17:26:19 +0200 Message-Id: <20191023152620.13166-3-kwolf@redhat.com> In-Reply-To: <20191023152620.13166-1-kwolf@redhat.com> References: <20191023152620.13166-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: ySeDEXHHOMO2UOw_IhgC1w-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, psyhomb@gmail.com, michael@weiser.dinsnail.net, vsementsov@virtuozzo.com, den@virtuozzo.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org, dgilbert@redhat.com, mreitz@redhat.com, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" qcow2_cache_do_get() requires that s->lock is locked because it can yield between picking a cache entry and actually taking ownership of it by setting offset and increasing the reference count. Add an assertion to make sure the caller really holds the lock. The function can be called outside of coroutine context, where bdrv_pread and flushes become synchronous operations. The lock cannot and need not be taken in this case. Signed-off-by: Kevin Wolf Reviewed-by: Denis V. Lunev Tested-by: Michael Weiser --- block/qcow2-cache.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c index d29b038a67..75b13dad99 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -327,6 +327,9 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qco= w2Cache *c, int min_lru_index =3D -1; =20 assert(offset !=3D 0); + if (qemu_in_coroutine()) { + qemu_co_mutex_assert_locked(&s->lock); + } =20 trace_qcow2_cache_get(qemu_coroutine_self(), c =3D=3D s->l2_table_cach= e, offset, read_from_disk); @@ -386,6 +389,8 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qco= w2Cache *c, } } =20 + assert(c->entries[i].ref =3D=3D 0); + assert(c->entries[i].offset =3D=3D 0); c->entries[i].offset =3D offset; =20 /* And return the right table */ --=20 2.20.1 From nobody Mon May 6 20:31:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1571848169; cv=none; d=zoho.com; s=zohoarc; b=NZKaCv8sGNew7uOw7NZQdz8R5RSsnU7P+ah8Ub3vRa8R8Pkev9wgzx2lipCMuYMmt2qz9MREq2wyq//8xeJ6ROIozfsvW2QtitETq8FKXvKAPJp7PIoFkTukJhifIv4CDQXTUsiY8rbGa84863MglE7lcm84WyfKYdYmFvykqgg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571848169; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=90Ia9SFR3pnny2eRawjSZIKQHSdVWiZbhyuLrDWnZEI=; b=cKSjU/fqWTVM7POnA2qZEezH9FUeohPN0y+oUUBQ2XynDt7FmTP56544FDRqAnwF/06r9xDfcTfEgqgC4qp7DHS4d4lS6tJHCdwADDbyjIG7W7ZfftuoOGy44E94XTrtLz63mYlKBskP8njPAdVA6LrGcpPLZ52j17cJlcDm2fY= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15718481695451023.650347874836; Wed, 23 Oct 2019 09:29:29 -0700 (PDT) Received: from localhost ([::1]:41206 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNJVn-0004ao-C9 for importer@patchew.org; Wed, 23 Oct 2019 12:29:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39870) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNIXO-0005Os-Ct for qemu-devel@nongnu.org; Wed, 23 Oct 2019 11:27:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNIXN-0002mI-8Z for qemu-devel@nongnu.org; Wed, 23 Oct 2019 11:27:02 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:50531 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNIXN-0002lh-4t for qemu-devel@nongnu.org; Wed, 23 Oct 2019 11:27:01 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-213-r1svjNF2Nm2DQ-iOF2l_5Q-1; Wed, 23 Oct 2019 11:26:57 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D409C476; Wed, 23 Oct 2019 15:26:55 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-192.ams2.redhat.com [10.36.117.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9DCCC6061E; Wed, 23 Oct 2019 15:26:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571844420; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=90Ia9SFR3pnny2eRawjSZIKQHSdVWiZbhyuLrDWnZEI=; b=KTTlrEMAG3fZV/L5NIw5QNxUPoTlWfn/xso435sGei/Gd2eECBVIZOHRTAiLAahibhtZ/g wi4jyQLYV+3BWLsrF+PQrMRluZ/smGhJnjJVm1MV0q8LGMBuCqbAZY/kLXWyDfyzLGoK/2 /5/ZRZst40RTCrFNugE4+1rulABo6gs= From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH 3/3] qcow2: Fix corruption bug in qcow2_detect_metadata_preallocation() Date: Wed, 23 Oct 2019 17:26:20 +0200 Message-Id: <20191023152620.13166-4-kwolf@redhat.com> In-Reply-To: <20191023152620.13166-1-kwolf@redhat.com> References: <20191023152620.13166-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: r1svjNF2Nm2DQ-iOF2l_5Q-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, psyhomb@gmail.com, michael@weiser.dinsnail.net, vsementsov@virtuozzo.com, den@virtuozzo.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org, dgilbert@redhat.com, mreitz@redhat.com, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" qcow2_detect_metadata_preallocation() calls qcow2_get_refcount() which requires s->lock to be taken to protect its accesses to the refcount table and refcount blocks. However, nothing in this code path actually took the lock. This could cause the same cache entry to be used by two requests at the same time, for different tables at different offsets, resulting in image corruption. As it would be preferable to base the detection on consistent data (even though it's just heuristics), let's take the lock not only around the qcow2_get_refcount() calls, but around the whole function. This patch takes the lock in qcow2_co_block_status() earlier and asserts in qcow2_detect_metadata_preallocation() that we hold the lock. Fixes: 69f47505ee66afaa513305de0c1895a224e52c45 Cc: qemu-stable@nongnu.org Reported-by: Michael Weiser Signed-off-by: Kevin Wolf Reviewed-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-refcount.c | 2 ++ block/qcow2.c | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index ef965d7895..0d64bf5a5e 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -3455,6 +3455,8 @@ int qcow2_detect_metadata_preallocation(BlockDriverSt= ate *bs) int64_t i, end_cluster, cluster_count =3D 0, threshold; int64_t file_length, real_allocation, real_clusters; =20 + qemu_co_mutex_assert_locked(&s->lock); + file_length =3D bdrv_getlength(bs->file->bs); if (file_length < 0) { return file_length; diff --git a/block/qcow2.c b/block/qcow2.c index 8b05933565..0bc69e6996 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1916,6 +1916,8 @@ static int coroutine_fn qcow2_co_block_status(BlockDr= iverState *bs, unsigned int bytes; int status =3D 0; =20 + qemu_co_mutex_lock(&s->lock); + if (!s->metadata_preallocation_checked) { ret =3D qcow2_detect_metadata_preallocation(bs); s->metadata_preallocation =3D (ret =3D=3D 1); @@ -1923,7 +1925,6 @@ static int coroutine_fn qcow2_co_block_status(BlockDr= iverState *bs, } =20 bytes =3D MIN(INT_MAX, count); - qemu_co_mutex_lock(&s->lock); ret =3D qcow2_get_cluster_offset(bs, offset, &bytes, &cluster_offset); qemu_co_mutex_unlock(&s->lock); if (ret < 0) { --=20 2.20.1