From nobody Sun Sep 28 11:12:54 2025 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; 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 Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547600621029378.63448757075537; Tue, 15 Jan 2019 17:03:41 -0800 (PST) Received: from localhost ([127.0.0.1]:36705 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjZcA-0003oe-Fl for importer@patchew.org; Tue, 15 Jan 2019 20:03:30 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57789) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjZaC-0002iz-BS for qemu-devel@nongnu.org; Tue, 15 Jan 2019 20:01:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjZa3-0004Vf-Oy for qemu-devel@nongnu.org; Tue, 15 Jan 2019 20:01:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:44608) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gjZa2-0004Ri-Ba for qemu-devel@nongnu.org; Tue, 15 Jan 2019 20:01:19 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 594FCC0587CE; Wed, 16 Jan 2019 01:01:13 +0000 (UTC) Received: from probe.bos.redhat.com (dhcp-17-221.bos.redhat.com [10.18.17.221]) by smtp.corp.redhat.com (Postfix) with ESMTP id C499419C7C; Wed, 16 Jan 2019 01:01:12 +0000 (UTC) From: John Snow To: qemu-devel@nongnu.org Date: Tue, 15 Jan 2019 20:01:03 -0500 Message-Id: <20190116010106.27626-6-jsnow@redhat.com> In-Reply-To: <20190116010106.27626-1-jsnow@redhat.com> References: <20190116010106.27626-1-jsnow@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 16 Jan 2019 01:01:13 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 5/8] block/mirror: fix and improve do_sync_target_write 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: peter.maydell@linaro.org, Vladimir Sementsov-Ogievskiy , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Use bdrv_dirty_bitmap_next_dirty_area() instead of bdrv_dirty_iter_next_area(), because of the following problems of bdrv_dirty_iter_next_area(): 1. Using HBitmap iterators we should carefully handle unaligned offset, as first call to hbitmap_iter_next() may return a value less than original offset (actually, it will be original offset rounded down to bitmap granularity). This handling is not done in do_sync_target_write(). 2. bdrv_dirty_iter_next_area() handles unaligned max_offset incorrectly: look at the code: if (max_offset =3D=3D iter->bitmap->size) { /* If max_offset points to the image end, round it up by the * bitmap granularity */ gran_max_offset =3D ROUND_UP(max_offset, granularity); } else { gran_max_offset =3D max_offset; } ret =3D hbitmap_iter_next(&iter->hbi, false); if (ret < 0 || ret + granularity > gran_max_offset) { return false; } and assume that max_offset !=3D iter->bitmap->size but still unaligned. if 0 < ret < max_offset we found dirty area, but the function can return false in this case (if ret + granularity > max_offset). 3. bdrv_dirty_iter_next_area() uses inefficient loop to find the end of the dirty area. Let's use more efficient hbitmap_next_zero instead (bdrv_dirty_bitmap_next_dirty_area() do so) Signed-off-by: Vladimir Sementsov-Ogievskiy Signed-off-by: John Snow --- block/mirror.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index f0b211a9c8..24ede6fdaa 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -1185,25 +1185,23 @@ do_sync_target_write(MirrorBlockJob *job, MirrorMet= hod method, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) { - BdrvDirtyBitmapIter *iter; QEMUIOVector target_qiov; - uint64_t dirty_offset; - int dirty_bytes; + uint64_t dirty_offset =3D offset; + uint64_t dirty_bytes; =20 if (qiov) { qemu_iovec_init(&target_qiov, qiov->niov); } =20 - iter =3D bdrv_dirty_iter_new(job->dirty_bitmap); - bdrv_set_dirty_iter(iter, offset); - while (true) { bool valid_area; int ret; =20 bdrv_dirty_bitmap_lock(job->dirty_bitmap); - valid_area =3D bdrv_dirty_iter_next_area(iter, offset + bytes, - &dirty_offset, &dirty_bytes= ); + dirty_bytes =3D MIN(offset + bytes - dirty_offset, INT_MAX); + valid_area =3D bdrv_dirty_bitmap_next_dirty_area(job->dirty_bitmap, + &dirty_offset, + &dirty_bytes); if (!valid_area) { bdrv_dirty_bitmap_unlock(job->dirty_bitmap); break; @@ -1259,9 +1257,10 @@ do_sync_target_write(MirrorBlockJob *job, MirrorMeth= od method, break; } } + + dirty_offset +=3D dirty_bytes; } =20 - bdrv_dirty_iter_free(iter); if (qiov) { qemu_iovec_destroy(&target_qiov); } --=20 2.17.2