From nobody Wed Feb 11 03:26:04 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496224283141477.3844194685249; Wed, 31 May 2017 02:51:23 -0700 (PDT) Received: from localhost ([::1]:58171 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG0Hh-0000B6-FW for importer@patchew.org; Wed, 31 May 2017 05:51:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41570) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG0AG-0002RC-D2 for qemu-devel@nongnu.org; Wed, 31 May 2017 05:43:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG0AF-0001jF-Fz for qemu-devel@nongnu.org; Wed, 31 May 2017 05:43:40 -0400 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]:35696) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dG0AF-0001id-6m; Wed, 31 May 2017 05:43:39 -0400 Received: by mail-wr0-x242.google.com with SMTP id g76so1057095wrd.2; Wed, 31 May 2017 02:43:39 -0700 (PDT) Received: from localhost.localdomain (94-39-157-43.adsl-ull.clienti.tiscali.it. [94.39.157.43]) by smtp.gmail.com with ESMTPSA id b30sm18150162wra.6.2017.05.31.02.43.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 31 May 2017 02:43:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=2lOKiYU5BvIY+b6jaD36UDy07F0NjLRpC9hF5PX811o=; b=lu+yETotSeRfm5t2EM3XdeWnGyuomCRzDlSj6XR0mOcDTGWMaxOw69pRj1/dh8jpG4 lO7ZJF6bXhdSiCWK5G8Mk/2o3DTRzz3wvCueJjwW4JMH5+93+Kt7wr7Og1c5bHuu6Rl0 9QmpZBDV3NZ5QWbQ7xcnVe36XTv+4u3qruIniQgCGuIQRQLS5rriXoGEhX9TjeUYZlTp W6bP3VKhmFkJSTNiB2ztosjXIia1r9YAfs3CTWG6W8tZAPWsgFiXdKCMx8/Yq5kLrJmo zsbnXblRSvWxSlo44ZKT6UHeh9oaYLDTfUeg7jJJRz1RbVcK0Jwi3SGWXskSslCo/AHy 4AuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=2lOKiYU5BvIY+b6jaD36UDy07F0NjLRpC9hF5PX811o=; b=VpQgOI/427HpOrrw2R1IippJdOl91BcQgEGwwayuofNSr7qEosnxjVjIGAuZq9g/ND hgGEme1ounJ0U3cDEXyCnFD5rRZBok411pkSUA35P1jTRbHVnsOfxT0+UTcMIBuJlAw3 z2JD0kDe3aMJUiCebxq1N5vrbpMmXd0KjWtgCvAgP9g2CHoKNfkwYSnumOVZkkRSxVEf IBrZqS1jUB6L5c7PcXwi/BIzv/fA9UI1yQensnUXCQIiC4CCjfmsqkk0EtNl5xunYK5V 5hsV10opF1GixbSvbso5ZzrKcVe3YydWbn+vIEo632V2ON7N9uWclL2zl83dbalhlVyv CJcw== X-Gm-Message-State: AODbwcB3I8NyFe49q4kxGOpkus7759UbSGXiYDPsvO3KFKNwI3j+VhKP DXmGe+7CsDrlHVZh5E4= X-Received: by 10.223.171.77 with SMTP id r13mr10168298wrc.83.1496223817950; Wed, 31 May 2017 02:43:37 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 11:43:22 +0200 Message-Id: <20170531094330.1808-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170531094330.1808-1-pbonzini@redhat.com> References: <20170531094330.1808-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::242 Subject: [Qemu-devel] [PATCH 03/11] vdi: make it thread-safe X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, famz@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The VirtualBox driver is using a mutex to order all allocating writes, but it is not protecting accesses to the bitmap because they implicitly happen under the AioContext mutex. Change this to use a CoRwlock explicitly. Signed-off-by: Paolo Bonzini Reviewed-by: Eric Blake --- block/vdi.c | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/block/vdi.c b/block/vdi.c index d12d9cdc79..57f1a037c8 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -172,7 +172,7 @@ typedef struct { /* VDI header (converted to host endianness). */ VdiHeader header; =20 - CoMutex write_lock; + CoRwlock bmap_lock; =20 Error *migration_blocker; } BDRVVdiState; @@ -485,7 +485,7 @@ static int vdi_open(BlockDriverState *bs, QDict *option= s, int flags, goto fail_free_bmap; } =20 - qemu_co_mutex_init(&s->write_lock); + qemu_co_rwlock_init(&s->bmap_lock); =20 return 0; =20 @@ -557,7 +557,9 @@ vdi_co_preadv(BlockDriverState *bs, uint64_t offset, ui= nt64_t bytes, n_bytes, offset); =20 /* prepare next AIO request */ + qemu_co_rwlock_rdlock(&s->bmap_lock); bmap_entry =3D le32_to_cpu(s->bmap[block_index]); + qemu_co_rwlock_unlock(&s->bmap_lock); if (!VDI_IS_ALLOCATED(bmap_entry)) { /* Block not allocated, return zeros, no need to wait. */ qemu_iovec_memset(qiov, bytes_done, 0, n_bytes); @@ -595,6 +597,7 @@ vdi_co_pwritev(BlockDriverState *bs, uint64_t offset, u= int64_t bytes, uint32_t block_index; uint32_t offset_in_block; uint32_t n_bytes; + uint64_t data_offset; uint32_t bmap_first =3D VDI_UNALLOCATED; uint32_t bmap_last =3D VDI_UNALLOCATED; uint8_t *block =3D NULL; @@ -614,10 +617,19 @@ vdi_co_pwritev(BlockDriverState *bs, uint64_t offset,= uint64_t bytes, n_bytes, offset); =20 /* prepare next AIO request */ + qemu_co_rwlock_rdlock(&s->bmap_lock); bmap_entry =3D le32_to_cpu(s->bmap[block_index]); if (!VDI_IS_ALLOCATED(bmap_entry)) { /* Allocate new block and write to it. */ - uint64_t data_offset; + qemu_co_rwlock_upgrade(&s->bmap_lock); + bmap_entry =3D le32_to_cpu(s->bmap[block_index]); + if (VDI_IS_ALLOCATED(bmap_entry)) { + /* A concurrent allocation did the work for us. */ + qemu_co_rwlock_downgrade(&s->bmap_lock); + goto nonallocating_write; + } + bmap_entry =3D s->header.blocks_allocated; s->bmap[block_index] =3D cpu_to_le32(bmap_entry); s->header.blocks_allocated++; @@ -635,30 +647,18 @@ vdi_co_pwritev(BlockDriverState *bs, uint64_t offset,= uint64_t bytes, memset(block + offset_in_block + n_bytes, 0, s->block_size - n_bytes - offset_in_block); =20 - /* Note that this coroutine does not yield anywhere from readi= ng the - * bmap entry until here, so in regards to all the coroutines = trying - * to write to this cluster, the one doing the allocation will - * always be the first to try to acquire the lock. - * Therefore, it is also the first that will actually be able = to - * acquire the lock and thus the padded cluster is written bef= ore - * the other coroutines can write to the affected area. */ - qemu_co_mutex_lock(&s->write_lock); + /* Write the new block under CoRwLock write-side protection, + * so this full-cluster write does not overlap a partial write + * of the same cluster, issued from the "else" branch. + */ ret =3D bdrv_pwrite(bs->file, data_offset, block, s->block_siz= e); - qemu_co_mutex_unlock(&s->write_lock); + qemu_co_rwlock_unlock(&s->bmap_lock); } else { - uint64_t data_offset =3D s->header.offset_data + - (uint64_t)bmap_entry * s->block_size + - offset_in_block; - qemu_co_mutex_lock(&s->write_lock); - /* This lock is only used to make sure the following write ope= ration - * is executed after the write issued by the coroutine allocat= ing - * this cluster, therefore we do not need to keep it locked. - * As stated above, the allocating coroutine will always try t= o lock - * the mutex before all the other concurrent accesses to that - * cluster, therefore at this point we can be absolutely certa= in - * that that write operation has returned (there may be other = writes - * in flight, but they do not concern this very operation). */ - qemu_co_mutex_unlock(&s->write_lock); +nonallocating_write: + data_offset =3D s->header.offset_data + + (uint64_t)bmap_entry * s->block_size + + offset_in_block; + qemu_co_rwlock_unlock(&s->bmap_lock); =20 qemu_iovec_reset(&local_qiov); qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes); --=20 2.13.0