From nobody Sat Feb 7 05:49:39 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 1498743447115795.1297865265062; Thu, 29 Jun 2017 06:37:27 -0700 (PDT) Received: from localhost ([::1]:39409 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQZdK-0005rB-1q for importer@patchew.org; Thu, 29 Jun 2017 09:37:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48845) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQZUF-0005jO-3R for qemu-devel@nongnu.org; Thu, 29 Jun 2017 09:28:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dQZUD-0001KP-Te for qemu-devel@nongnu.org; Thu, 29 Jun 2017 09:27:59 -0400 Received: from mail-wr0-x243.google.com ([2a00:1450:400c:c0c::243]:33444) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dQZUD-0001JV-JN; Thu, 29 Jun 2017 09:27:57 -0400 Received: by mail-wr0-x243.google.com with SMTP id x23so36841359wrb.0; Thu, 29 Jun 2017 06:27:57 -0700 (PDT) Received: from localhost.localdomain (94-39-191-51.adsl-ull.clienti.tiscali.it. [94.39.191.51]) by smtp.gmail.com with ESMTPSA id i22sm4087691wrb.30.2017.06.29.06.27.55 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 29 Jun 2017 06:27:55 -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=IQ3sP7ulf32JJI7A5vtAWHEchGXMEn+1mxgYn0v4IcA=; b=A/Oplq56/+Az3m0NiW7Y7VtjuBLTDCHzd39oFGTOVARWgWjAn9sj9wULie8qvzlatl 5PBropw8Y0kFeCRkXzdltZcqV1qjX7lMl9/C6DtqGOggim6QHxYoX7EOWUJQ7zC6eIeC /jrkPq/R7TJUd5kXYPFBZ73rGzCao9lAxTyG1lh7u0ISuUYICrQZFmylMPKkbwYV6Zom SUeZdIi0LkmlmDJXV8iK/4pshhvsLiWDpvDag7MvQJ5zhhKJsWshi1XIUp/D0nxZmBIo BOyRFqVk7PLRUQJ8wq+IDvCM9VEQQsEF9ytbmc2Jtrj//axCEFvAzl+MlYBoMyo+mnp3 HwiQ== 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=IQ3sP7ulf32JJI7A5vtAWHEchGXMEn+1mxgYn0v4IcA=; b=Prm1wKhdqzyzbFGZpUjlnHoOiomteep49q5aZNmU95FlSeXPdem/zIE3VosbZ20eaN /NQzpU5rwG7ze1eufvX5FbqHjSe2h9hOhgYCMAslOMnxvgfOfEVqXB4zRQ99MIbO/VEE 1LJn02CRmFsNjzaHjNAqSTp7/57arZkD/vkWG2DSM+hCVAi0bReiM9ZZ2sKE8yPcYSd2 uI4XuNXofaUoSMkTfTw+xKYUQSq626uYfJ8AwEVEp7ueJtH8GcsCAglNLHoDi52n6cmI qrt73TrTVI4g9CxOeojT5PUZ5KB8XGOWKOF1y3flEIVnsIkjRaY0uZ5YBl4ersOaDAho 1E5w== X-Gm-Message-State: AKS2vOximChyWLps8lQdStQJ7OWeHXfVPBGluIzrxI13IOkplbqnXuWW cQLKP+6eyaf3vwUdnjI= X-Received: by 10.223.128.177 with SMTP id 46mr12165322wrl.150.1498742876315; Thu, 29 Jun 2017 06:27:56 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 29 Jun 2017 15:27:41 +0200 Message-Id: <20170629132749.997-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170629132749.997-1-pbonzini@redhat.com> References: <20170629132749.997-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::243 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, qemu-block@nongnu.org, stefanha@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. Reviewed-by: Eric Blake Reviewed-by: Stefan Hajnoczi Signed-off-by: Paolo Bonzini --- block/vdi.c | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/block/vdi.c b/block/vdi.c index 79af47763b..080e1562e3 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