From nobody Tue Apr 30 00:15:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1576749957; cv=none; d=zohomail.com; s=zohoarc; b=IU0SyMAWIKRSzRaEB8PHKyzgIig4L/E5nrIWkrqQIsStq/F+hhPwcWEfkAjYs9+Q8LekMzPT3CHj+lko6RRrOyQ+JyQMuBpxEWHWnioaVWfGvp8s2woZAoESlRz5+tOwf7XoZRU5hSueARJgnWIGTFuS2YECiBRZbPEnjWWDUNo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576749957; h=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=T/Dm/ytRwQ+0y7rCNfUPa9tFs5BtPbUfTWEfcX0wamo=; b=I4BGptR77PZ+IS2xCzHB0z6I1IbPc+yiEXEwxvnDjoKu1GpE0/m+3kPD2A6EGg6DQJg5L3V0bLSn5FEoX77uIRvEALgzE2xEqsVf15y52/MzBhWauqtl47qxlR7/zA1NpTjp77cV9CV8FdgHh6cOvmDPgdtgwGFYks8bJmmip7g= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 157674995777124.12388970878078; Thu, 19 Dec 2019 02:05:57 -0800 (PST) Received: from localhost ([::1]:38088 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsgu-000500-3E for importer@patchew.org; Thu, 19 Dec 2019 05:05:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39085) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsf4-0002g0-DN for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihsf2-0007vx-Rl for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:02 -0500 Received: from relay.sw.ru ([185.231.240.75]:57604) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihsev-0007Y7-Jf; Thu, 19 Dec 2019 05:03:53 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ihser-0004j2-Ef; Thu, 19 Dec 2019 13:03:49 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v3 01/10] hbitmap: assert that we don't create bitmap larger than INT64_MAX Date: Thu, 19 Dec 2019 13:03:39 +0300 Message-Id: <20191219100348.24827-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191219100348.24827-1-vsementsov@virtuozzo.com> References: <20191219100348.24827-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" We have APIs which returns signed int64_t, to be able to return error. Therefore we can't handle bitmaps with absolute size larger than (INT64_MAX+1). Still, keep maximum to be INT64_MAX which is a bit safer. Note, that bitmaps are used to represent disk images, which can't exceed INT64_MAX anyway. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- util/hbitmap.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/util/hbitmap.c b/util/hbitmap.c index 242c6e519c..7f9b3e0cd7 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -716,6 +716,7 @@ HBitmap *hbitmap_alloc(uint64_t size, int granularity) HBitmap *hb =3D g_new0(struct HBitmap, 1); unsigned i; =20 + assert(size <=3D INT64_MAX); hb->orig_size =3D size; =20 assert(granularity >=3D 0 && granularity < 64); @@ -746,6 +747,7 @@ void hbitmap_truncate(HBitmap *hb, uint64_t size) uint64_t num_elements =3D size; uint64_t old; =20 + assert(size <=3D INT64_MAX); hb->orig_size =3D size; =20 /* Size comes in as logical elements, adjust for granularity. */ --=20 2.21.0 From nobody Tue Apr 30 00:15:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1576750084; cv=none; d=zohomail.com; s=zohoarc; b=F3vlGOyOonUU9KNF+kp/YHRKJEN86fVVyaepErat0sQSXP/5RVz/zxy+3m6xzJSmeR9fPXszBEVhddXFgM9UmAWAMvjkL2xWl0nSMHSQc+e+AXObeMdTWV1JYOrm+oNg8EBApGXSoqi+HPYNhKtiylXxAiIBHdhgZYmN2v0IOq4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576750084; h=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=IGySixoEzO99XQprMtY+zSnPNwAdXD4cNC52aBlVLDw=; b=hLw/rR2riv/3F4KUVyG3Xckug/a1QiUphRrzEpjQmZ2pLpmNo8icxe2Mhqv/cF6d+I+zMSj2ESINZ/4etM9gxocXty3GEQ0q3sz5G25WDStors0WaI70brWxAsmloIVGPbX77xFvqZZh/BeMWQIzqhZ2ljU22xwZH28kfi1qED8= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 1576750084456743.8678944805423; Thu, 19 Dec 2019 02:08:04 -0800 (PST) Received: from localhost ([::1]:38124 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsix-0000W4-4O for importer@patchew.org; Thu, 19 Dec 2019 05:08:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39150) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsf4-0002hE-VT for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihsf2-0007vR-P3 for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:02 -0500 Received: from relay.sw.ru ([185.231.240.75]:57598) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihsev-0007Y4-Hn; Thu, 19 Dec 2019 05:03:53 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ihser-0004j2-QV; Thu, 19 Dec 2019 13:03:50 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v3 02/10] hbitmap: move hbitmap_iter_next_word to hbitmap.c Date: Thu, 19 Dec 2019 13:03:40 +0300 Message-Id: <20191219100348.24827-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191219100348.24827-1-vsementsov@virtuozzo.com> References: <20191219100348.24827-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The function is definitely internal (it's not used by third party and it has complicated interface). Move it to .c file. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- include/qemu/hbitmap.h | 30 ------------------------------ util/hbitmap.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h index 1bf944ca3d..ab227b117f 100644 --- a/include/qemu/hbitmap.h +++ b/include/qemu/hbitmap.h @@ -362,34 +362,4 @@ void hbitmap_free_meta(HBitmap *hb); */ int64_t hbitmap_iter_next(HBitmapIter *hbi); =20 -/** - * hbitmap_iter_next_word: - * @hbi: HBitmapIter to operate on. - * @p_cur: Location where to store the next non-zero word. - * - * Return the index of the next nonzero word that is set in @hbi's - * associated HBitmap, and set *p_cur to the content of that word - * (bits before the index that was passed to hbitmap_iter_init are - * trimmed on the first call). Return -1, and set *p_cur to zero, - * if all remaining words are zero. - */ -static inline size_t hbitmap_iter_next_word(HBitmapIter *hbi, unsigned lon= g *p_cur) -{ - unsigned long cur =3D hbi->cur[HBITMAP_LEVELS - 1]; - - if (cur =3D=3D 0) { - cur =3D hbitmap_iter_skip_words(hbi); - if (cur =3D=3D 0) { - *p_cur =3D 0; - return -1; - } - } - - /* The next call will resume work from the next word. */ - hbi->cur[HBITMAP_LEVELS - 1] =3D 0; - *p_cur =3D cur; - return hbi->pos; -} - - #endif diff --git a/util/hbitmap.c b/util/hbitmap.c index 7f9b3e0cd7..a368dc5ef7 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -298,6 +298,35 @@ uint64_t hbitmap_count(const HBitmap *hb) return hb->count << hb->granularity; } =20 +/** + * hbitmap_iter_next_word: + * @hbi: HBitmapIter to operate on. + * @p_cur: Location where to store the next non-zero word. + * + * Return the index of the next nonzero word that is set in @hbi's + * associated HBitmap, and set *p_cur to the content of that word + * (bits before the index that was passed to hbitmap_iter_init are + * trimmed on the first call). Return -1, and set *p_cur to zero, + * if all remaining words are zero. + */ +static size_t hbitmap_iter_next_word(HBitmapIter *hbi, unsigned long *p_cu= r) +{ + unsigned long cur =3D hbi->cur[HBITMAP_LEVELS - 1]; + + if (cur =3D=3D 0) { + cur =3D hbitmap_iter_skip_words(hbi); + if (cur =3D=3D 0) { + *p_cur =3D 0; + return -1; + } + } + + /* The next call will resume work from the next word. */ + hbi->cur[HBITMAP_LEVELS - 1] =3D 0; + *p_cur =3D cur; + return hbi->pos; +} + /* Count the number of set bits between start and end, not accounting for * the granularity. Also an example of how to use hbitmap_iter_next_word. */ --=20 2.21.0 From nobody Tue Apr 30 00:15:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1576750066; cv=none; d=zohomail.com; s=zohoarc; b=kn6fP14hOUrG+Rr0nOPEXwbsmfON+F/Esg/lKUpspsdd5VoHDHiFOixJ/y+U0Ug+bzhW7PGj3pge7tMH4sIN7iwVRuv2Ir1ihAMwAF1wxjaNCPZrwQi8uSri5Y0m0RmWQtdXMzzi5VP7ey+cR03RSGezITsZPqDBwlIvFh+lSEs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576750066; h=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=Eqid/UGns8f3U8FRmxf+rx4nGVHqiqgt2ERTSR1nkp8=; b=Oj1K4w/k7J9GNwbbax5hOX+PPMKtL03HTMb10o7o+0/+JcCQgjcX/s6+AAGsEyu8M0FdpzEY2MPdZXDqbd0ZupiCz7rEj5T1pLQ7KHsTsG/2FzuVwQH4nitJxYrK4HgDWcP/Q/PNKE8jQZl4lfknTu5l/BNcrKdYXHwgT5o8oqs= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 1576750066743604.9314595060631; Thu, 19 Dec 2019 02:07:46 -0800 (PST) Received: from localhost ([::1]:38122 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsif-0008PU-8J for importer@patchew.org; Thu, 19 Dec 2019 05:07:45 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39145) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsf5-0002h2-22 for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihsf2-0007ue-EC for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:02 -0500 Received: from relay.sw.ru ([185.231.240.75]:57602) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihsev-0007Y5-Ja; Thu, 19 Dec 2019 05:03:53 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ihses-0004j2-6y; Thu, 19 Dec 2019 13:03:50 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v3 03/10] hbitmap: unpublish hbitmap_iter_skip_words Date: Thu, 19 Dec 2019 13:03:41 +0300 Message-Id: <20191219100348.24827-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191219100348.24827-1-vsementsov@virtuozzo.com> References: <20191219100348.24827-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Function is internal and even commented as internal. Drop its definition from .h file. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- include/qemu/hbitmap.h | 7 ------- util/hbitmap.c | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h index ab227b117f..15837a0e2d 100644 --- a/include/qemu/hbitmap.h +++ b/include/qemu/hbitmap.h @@ -297,13 +297,6 @@ void hbitmap_free(HBitmap *hb); */ void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first= ); =20 -/* hbitmap_iter_skip_words: - * @hbi: HBitmapIter to operate on. - * - * Internal function used by hbitmap_iter_next and hbitmap_iter_next_word. - */ -unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi); - /* hbitmap_next_zero: * * Find next not dirty bit within selected range. If not found, return -1. diff --git a/util/hbitmap.c b/util/hbitmap.c index a368dc5ef7..26145d4b9e 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -104,7 +104,7 @@ struct HBitmap { /* Advance hbi to the next nonzero word and return it. hbi->pos * is updated. Returns zero if we reach the end of the bitmap. */ -unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi) +static unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi) { size_t pos =3D hbi->pos; const HBitmap *hb =3D hbi->hb; --=20 2.21.0 From nobody Tue Apr 30 00:15:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1576750218; cv=none; d=zohomail.com; s=zohoarc; b=Y0yyd5/Mws4y1uY9mxjFrHlafBBO3+ODjX+qXWJKGA0dmz6t8qoo54zOeLY+vYsxowWKd7rENhW+AwUV3FCJEvm76JVTjypFJDfoRYeuQCviUVc+7a1KoIEFFgPFw4IaW2Kk2YRAB1ir2a0uU1fGIY5T/GPjqF3jU2M++4Xujuw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576750218; h=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=PH+1Sx0Humm4kjpkZe2eX7qcqSzNid/qVlcXa6hJ9Iw=; b=YODxGZI1zA0a4cdrckqwn/UN9GuvvId7dOIJIOj4lFKwsIyX+GEpJjWMfqpcpsmTpVIBoNKm6KQ3jFcZVGj3akhENopdBWehhE4cyhPNUqsBRdZNTrm8kgkU2yI2nEqn1BXDVycZSWYIN/lI7e4YX1eIQpA1mjKw74mVPaZ7m5c= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 1576750218020413.4478716555494; Thu, 19 Dec 2019 02:10:18 -0800 (PST) Received: from localhost ([::1]:38166 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsl6-0003p4-Sa for importer@patchew.org; Thu, 19 Dec 2019 05:10:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39491) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsf7-0002l9-8z for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihsf5-00083d-9s for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:05 -0500 Received: from relay.sw.ru ([185.231.240.75]:57612) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihsev-0007YX-Jg; Thu, 19 Dec 2019 05:03:53 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ihses-0004j2-EU; Thu, 19 Dec 2019 13:03:50 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v3 04/10] hbitmap: drop meta bitmaps as they are unused Date: Thu, 19 Dec 2019 13:03:42 +0300 Message-Id: <20191219100348.24827-5-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191219100348.24827-1-vsementsov@virtuozzo.com> References: <20191219100348.24827-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- include/qemu/hbitmap.h | 21 -------- tests/test-hbitmap.c | 115 ----------------------------------------- util/hbitmap.c | 16 ------ 3 files changed, 152 deletions(-) diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h index 15837a0e2d..df922d8517 100644 --- a/include/qemu/hbitmap.h +++ b/include/qemu/hbitmap.h @@ -325,27 +325,6 @@ int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t = start, uint64_t count); bool hbitmap_next_dirty_area(const HBitmap *hb, uint64_t *start, uint64_t *count); =20 -/* hbitmap_create_meta: - * Create a "meta" hbitmap to track dirtiness of the bits in this HBitmap. - * The caller owns the created bitmap and must call hbitmap_free_meta(hb) = to - * free it. - * - * Currently, we only guarantee that if a bit in the hbitmap is changed it - * will be reflected in the meta bitmap, but we do not yet guarantee the - * opposite. - * - * @hb: The HBitmap to operate on. - * @chunk_size: How many bits in @hb does one bit in the meta track. - */ -HBitmap *hbitmap_create_meta(HBitmap *hb, int chunk_size); - -/* hbitmap_free_meta: - * Free the meta bitmap of @hb. - * - * @hb: The HBitmap whose meta bitmap should be freed. - */ -void hbitmap_free_meta(HBitmap *hb); - /** * hbitmap_iter_next: * @hbi: HBitmapIter to operate on. diff --git a/tests/test-hbitmap.c b/tests/test-hbitmap.c index e1f867085f..aeaa0b3f22 100644 --- a/tests/test-hbitmap.c +++ b/tests/test-hbitmap.c @@ -22,7 +22,6 @@ =20 typedef struct TestHBitmapData { HBitmap *hb; - HBitmap *meta; unsigned long *bits; size_t size; size_t old_size; @@ -94,14 +93,6 @@ static void hbitmap_test_init(TestHBitmapData *data, } } =20 -static void hbitmap_test_init_meta(TestHBitmapData *data, - uint64_t size, int granularity, - int meta_chunk) -{ - hbitmap_test_init(data, size, granularity); - data->meta =3D hbitmap_create_meta(data->hb, meta_chunk); -} - static inline size_t hbitmap_test_array_size(size_t bits) { size_t n =3D DIV_ROUND_UP(bits, BITS_PER_LONG); @@ -144,9 +135,6 @@ static void hbitmap_test_teardown(TestHBitmapData *data, const void *unused) { if (data->hb) { - if (data->meta) { - hbitmap_free_meta(data->hb); - } hbitmap_free(data->hb); data->hb =3D NULL; } @@ -648,96 +636,6 @@ static void test_hbitmap_truncate_shrink_large(TestHBi= tmapData *data, hbitmap_test_truncate(data, size, -diff, 0); } =20 -static void hbitmap_check_meta(TestHBitmapData *data, - int64_t start, int count) -{ - int64_t i; - - for (i =3D 0; i < data->size; i++) { - if (i >=3D start && i < start + count) { - g_assert(hbitmap_get(data->meta, i)); - } else { - g_assert(!hbitmap_get(data->meta, i)); - } - } -} - -static void hbitmap_test_meta(TestHBitmapData *data, - int64_t start, int count, - int64_t check_start, int check_count) -{ - hbitmap_reset_all(data->hb); - hbitmap_reset_all(data->meta); - - /* Test "unset" -> "unset" will not update meta. */ - hbitmap_reset(data->hb, start, count); - hbitmap_check_meta(data, 0, 0); - - /* Test "unset" -> "set" will update meta */ - hbitmap_set(data->hb, start, count); - hbitmap_check_meta(data, check_start, check_count); - - /* Test "set" -> "set" will not update meta */ - hbitmap_reset_all(data->meta); - hbitmap_set(data->hb, start, count); - hbitmap_check_meta(data, 0, 0); - - /* Test "set" -> "unset" will update meta */ - hbitmap_reset_all(data->meta); - hbitmap_reset(data->hb, start, count); - hbitmap_check_meta(data, check_start, check_count); -} - -static void hbitmap_test_meta_do(TestHBitmapData *data, int chunk_size) -{ - uint64_t size =3D chunk_size * 100; - hbitmap_test_init_meta(data, size, 0, chunk_size); - - hbitmap_test_meta(data, 0, 1, 0, chunk_size); - hbitmap_test_meta(data, 0, chunk_size, 0, chunk_size); - hbitmap_test_meta(data, chunk_size - 1, 1, 0, chunk_size); - hbitmap_test_meta(data, chunk_size - 1, 2, 0, chunk_size * 2); - hbitmap_test_meta(data, chunk_size - 1, chunk_size + 1, 0, chunk_size = * 2); - hbitmap_test_meta(data, chunk_size - 1, chunk_size + 2, 0, chunk_size = * 3); - hbitmap_test_meta(data, 7 * chunk_size - 1, chunk_size + 2, - 6 * chunk_size, chunk_size * 3); - hbitmap_test_meta(data, size - 1, 1, size - chunk_size, chunk_size); - hbitmap_test_meta(data, 0, size, 0, size); -} - -static void test_hbitmap_meta_byte(TestHBitmapData *data, const void *unus= ed) -{ - hbitmap_test_meta_do(data, BITS_PER_BYTE); -} - -static void test_hbitmap_meta_word(TestHBitmapData *data, const void *unus= ed) -{ - hbitmap_test_meta_do(data, BITS_PER_LONG); -} - -static void test_hbitmap_meta_sector(TestHBitmapData *data, const void *un= used) -{ - hbitmap_test_meta_do(data, BDRV_SECTOR_SIZE * BITS_PER_BYTE); -} - -/** - * Create an HBitmap and test set/unset. - */ -static void test_hbitmap_meta_one(TestHBitmapData *data, const void *unuse= d) -{ - int i; - int64_t offsets[] =3D { - 0, 1, L1 - 1, L1, L1 + 1, L2 - 1, L2, L2 + 1, L3 - 1, L3, L3 + 1 - }; - - hbitmap_test_init_meta(data, L3 * 2, 0, 1); - for (i =3D 0; i < ARRAY_SIZE(offsets); i++) { - hbitmap_test_meta(data, offsets[i], 1, offsets[i], 1); - hbitmap_test_meta(data, offsets[i], L1, offsets[i], L1); - hbitmap_test_meta(data, offsets[i], L2, offsets[i], L2); - } -} - static void test_hbitmap_serialize_align(TestHBitmapData *data, const void *unused) { @@ -750,13 +648,6 @@ static void test_hbitmap_serialize_align(TestHBitmapDa= ta *data, g_assert_cmpint(r, =3D=3D, 64 << 3); } =20 -static void test_hbitmap_meta_zero(TestHBitmapData *data, const void *unus= ed) -{ - hbitmap_test_init_meta(data, 0, 0, 1); - - hbitmap_check_meta(data, 0, 0); -} - static void hbitmap_test_serialize_range(TestHBitmapData *data, uint8_t *buf, size_t buf_size, uint64_t pos, uint64_t count) @@ -1165,12 +1056,6 @@ int main(int argc, char **argv) hbitmap_test_add("/hbitmap/truncate/shrink/large", test_hbitmap_truncate_shrink_large); =20 - hbitmap_test_add("/hbitmap/meta/zero", test_hbitmap_meta_zero); - hbitmap_test_add("/hbitmap/meta/one", test_hbitmap_meta_one); - hbitmap_test_add("/hbitmap/meta/byte", test_hbitmap_meta_byte); - hbitmap_test_add("/hbitmap/meta/word", test_hbitmap_meta_word); - hbitmap_test_add("/hbitmap/meta/sector", test_hbitmap_meta_sector); - hbitmap_test_add("/hbitmap/serialize/align", test_hbitmap_serialize_align); hbitmap_test_add("/hbitmap/serialize/basic", diff --git a/util/hbitmap.c b/util/hbitmap.c index 26145d4b9e..b6d4b99a06 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -905,22 +905,6 @@ bool hbitmap_merge(const HBitmap *a, const HBitmap *b,= HBitmap *result) return true; } =20 -HBitmap *hbitmap_create_meta(HBitmap *hb, int chunk_size) -{ - assert(!(chunk_size & (chunk_size - 1))); - assert(!hb->meta); - hb->meta =3D hbitmap_alloc(hb->size << hb->granularity, - hb->granularity + ctz32(chunk_size)); - return hb->meta; -} - -void hbitmap_free_meta(HBitmap *hb) -{ - assert(hb->meta); - hbitmap_free(hb->meta); - hb->meta =3D NULL; -} - char *hbitmap_sha256(const HBitmap *bitmap, Error **errp) { size_t size =3D bitmap->sizes[HBITMAP_LEVELS - 1] * sizeof(unsigned lo= ng); --=20 2.21.0 From nobody Tue Apr 30 00:15:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1576750281; cv=none; d=zohomail.com; s=zohoarc; b=jlvdmRtsMtwg5GwjiH3j2z4Osac5OANl8IMRNtALjl9PYND+wETkxKY+WfMUhH9UolqTpiYAtTwGIRaK2JWbJHGhSU2Dl7etBvVGjThDw4yu6dxmqnXBTXP/J5Si0hEmAKW7HK+U5C+S59pyOGuv9qBUDOCrCsgl4ZV4ztPamdI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576750281; h=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=KxeOAW2jPUbjpt8SMEQ63cTSo313gSwdmdQw/GnE10k=; b=YoeGfWT+o1NNwLTUhG0MLDKO9cBlW+/hQmLGOBr+v6zxOqcsTuXd2+vbM6qaXKGmextPY4fyv6sCVJjex9ssewlqWs4k3IZU/j89CTAt2CqLdffFKhiBnYmEzYbjsQ66Q9o3ZP8+LoSOyPsKrxBJmADwu+mKehV2nEfijQCBI3g= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 1576750281577879.1086664759531; Thu, 19 Dec 2019 02:11:21 -0800 (PST) Received: from localhost ([::1]:38184 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsm8-000513-EX for importer@patchew.org; Thu, 19 Dec 2019 05:11:20 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39500) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsf7-0002lB-Aq for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihsf4-00082s-Vv for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:05 -0500 Received: from relay.sw.ru ([185.231.240.75]:57616) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihsev-0007an-Aa; Thu, 19 Dec 2019 05:03:53 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ihses-0004j2-Lq; Thu, 19 Dec 2019 13:03:50 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v3 05/10] block/dirty-bitmap: switch _next_dirty_area and _next_zero to int64_t Date: Thu, 19 Dec 2019 13:03:43 +0300 Message-Id: <20191219100348.24827-6-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191219100348.24827-1-vsementsov@virtuozzo.com> References: <20191219100348.24827-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" We are going to introduce bdrv_dirty_bitmap_next_dirty so that same variable may be used to store its return value and to be its parameter, so it would int64_t. Similarly, we are going to refactor hbitmap_next_dirty_area to use hbitmap_next_dirty together with hbitmap_next_zero, therefore we want hbitmap_next_zero parameter type to be int64_t too. So, for convenience update all parameters of *_next_zero and *_next_dirty_area to be int64_t. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- include/block/dirty-bitmap.h | 6 +++--- include/qemu/hbitmap.h | 7 +++---- block/dirty-bitmap.c | 6 +++--- nbd/server.c | 2 +- tests/test-hbitmap.c | 32 ++++++++++++++++---------------- util/hbitmap.c | 13 ++++++++----- 6 files changed, 34 insertions(+), 32 deletions(-) diff --git a/include/block/dirty-bitmap.h b/include/block/dirty-bitmap.h index e2b20ecab9..27c72cc56a 100644 --- a/include/block/dirty-bitmap.h +++ b/include/block/dirty-bitmap.h @@ -105,10 +105,10 @@ for (bitmap =3D bdrv_dirty_bitmap_first(bs); bitmap; \ bitmap =3D bdrv_dirty_bitmap_next(bitmap)) =20 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp= ); -int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offs= et, - uint64_t bytes); +int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offse= t, + int64_t bytes); bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, - uint64_t *offset, uint64_t *bytes); + int64_t *offset, int64_t *bytes); BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, Error **errp); =20 diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h index df922d8517..b6e85f3d5d 100644 --- a/include/qemu/hbitmap.h +++ b/include/qemu/hbitmap.h @@ -304,10 +304,10 @@ void hbitmap_iter_init(HBitmapIter *hbi, const HBitma= p *hb, uint64_t first); * @hb: The HBitmap to operate on * @start: The bit to start from. * @count: Number of bits to proceed. If @start+@count > bitmap size, the = whole - * bitmap is looked through. You can use UINT64_MAX as @count to search up= to + * bitmap is looked through. You can use INT64_MAX as @count to search up = to * the bitmap end. */ -int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t start, uint64_t coun= t); +int64_t hbitmap_next_zero(const HBitmap *hb, int64_t start, int64_t count); =20 /* hbitmap_next_dirty_area: * @hb: The HBitmap to operate on @@ -322,8 +322,7 @@ int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t s= tart, uint64_t count); * @offset and @bytes appropriately. Otherwise returns false and leaves @o= ffset * and @bytes unchanged. */ -bool hbitmap_next_dirty_area(const HBitmap *hb, uint64_t *start, - uint64_t *count); +bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t *start, int64_t *c= ount); =20 /** * hbitmap_iter_next: diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c index 7039e82520..af9f5411a6 100644 --- a/block/dirty-bitmap.c +++ b/block/dirty-bitmap.c @@ -860,14 +860,14 @@ char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap = *bitmap, Error **errp) return hbitmap_sha256(bitmap->bitmap, errp); } =20 -int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offs= et, - uint64_t bytes) +int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offse= t, + int64_t bytes) { return hbitmap_next_zero(bitmap->bitmap, offset, bytes); } =20 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, - uint64_t *offset, uint64_t *bytes) + int64_t *offset, int64_t *bytes) { return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes); } diff --git a/nbd/server.c b/nbd/server.c index 24ebc1a805..a4b348eb32 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -2055,7 +2055,7 @@ static unsigned int bitmap_to_extents(BdrvDirtyBitmap= *bitmap, uint64_t offset, bool next_dirty =3D !dirty; =20 if (dirty) { - end =3D bdrv_dirty_bitmap_next_zero(bitmap, begin, UINT64_MAX); + end =3D bdrv_dirty_bitmap_next_zero(bitmap, begin, INT64_MAX); } else { bdrv_set_dirty_iter(it, begin); end =3D bdrv_dirty_iter_next(it); diff --git a/tests/test-hbitmap.c b/tests/test-hbitmap.c index aeaa0b3f22..0e1e5c64dd 100644 --- a/tests/test-hbitmap.c +++ b/tests/test-hbitmap.c @@ -837,7 +837,7 @@ static void test_hbitmap_next_zero_check_range(TestHBit= mapData *data, =20 static void test_hbitmap_next_zero_check(TestHBitmapData *data, int64_t st= art) { - test_hbitmap_next_zero_check_range(data, start, UINT64_MAX); + test_hbitmap_next_zero_check_range(data, start, INT64_MAX); } =20 static void test_hbitmap_next_zero_do(TestHBitmapData *data, int granulari= ty) @@ -905,11 +905,11 @@ static void test_hbitmap_next_zero_after_truncate(Tes= tHBitmapData *data, } =20 static void test_hbitmap_next_dirty_area_check(TestHBitmapData *data, - uint64_t offset, - uint64_t count) + int64_t offset, + int64_t count) { - uint64_t off1, off2; - uint64_t len1 =3D 0, len2; + int64_t off1, off2; + int64_t len1 =3D 0, len2; bool ret1, ret2; int64_t end; =20 @@ -945,24 +945,24 @@ static void test_hbitmap_next_dirty_area_do(TestHBitm= apData *data, int granularity) { hbitmap_test_init(data, L3, granularity); - test_hbitmap_next_dirty_area_check(data, 0, UINT64_MAX); + test_hbitmap_next_dirty_area_check(data, 0, INT64_MAX); test_hbitmap_next_dirty_area_check(data, 0, 1); test_hbitmap_next_dirty_area_check(data, L3 - 1, 1); =20 hbitmap_set(data->hb, L2, 1); test_hbitmap_next_dirty_area_check(data, 0, 1); test_hbitmap_next_dirty_area_check(data, 0, L2); - test_hbitmap_next_dirty_area_check(data, 0, UINT64_MAX); - test_hbitmap_next_dirty_area_check(data, L2 - 1, UINT64_MAX); + test_hbitmap_next_dirty_area_check(data, 0, INT64_MAX); + test_hbitmap_next_dirty_area_check(data, L2 - 1, INT64_MAX); test_hbitmap_next_dirty_area_check(data, L2 - 1, 1); test_hbitmap_next_dirty_area_check(data, L2 - 1, 2); test_hbitmap_next_dirty_area_check(data, L2 - 1, 3); - test_hbitmap_next_dirty_area_check(data, L2, UINT64_MAX); + test_hbitmap_next_dirty_area_check(data, L2, INT64_MAX); test_hbitmap_next_dirty_area_check(data, L2, 1); test_hbitmap_next_dirty_area_check(data, L2 + 1, 1); =20 hbitmap_set(data->hb, L2 + 5, L1); - test_hbitmap_next_dirty_area_check(data, 0, UINT64_MAX); + test_hbitmap_next_dirty_area_check(data, 0, INT64_MAX); test_hbitmap_next_dirty_area_check(data, L2 - 2, 8); test_hbitmap_next_dirty_area_check(data, L2 + 1, 5); test_hbitmap_next_dirty_area_check(data, L2 + 1, 3); @@ -974,16 +974,16 @@ static void test_hbitmap_next_dirty_area_do(TestHBitm= apData *data, test_hbitmap_next_dirty_area_check(data, L2 + 1, 0); =20 hbitmap_set(data->hb, L2 * 2, L3 - L2 * 2); - test_hbitmap_next_dirty_area_check(data, 0, UINT64_MAX); - test_hbitmap_next_dirty_area_check(data, L2, UINT64_MAX); - test_hbitmap_next_dirty_area_check(data, L2 + 1, UINT64_MAX); - test_hbitmap_next_dirty_area_check(data, L2 + 5 + L1 - 1, UINT64_MAX); + test_hbitmap_next_dirty_area_check(data, 0, INT64_MAX); + test_hbitmap_next_dirty_area_check(data, L2, INT64_MAX); + test_hbitmap_next_dirty_area_check(data, L2 + 1, INT64_MAX); + test_hbitmap_next_dirty_area_check(data, L2 + 5 + L1 - 1, INT64_MAX); test_hbitmap_next_dirty_area_check(data, L2 + 5 + L1, 5); test_hbitmap_next_dirty_area_check(data, L2 * 2 - L1, L1 + 1); test_hbitmap_next_dirty_area_check(data, L2 * 2, L2); =20 hbitmap_set(data->hb, 0, L3); - test_hbitmap_next_dirty_area_check(data, 0, UINT64_MAX); + test_hbitmap_next_dirty_area_check(data, 0, INT64_MAX); } =20 static void test_hbitmap_next_dirty_area_0(TestHBitmapData *data, @@ -1010,7 +1010,7 @@ static void test_hbitmap_next_dirty_area_after_trunca= te(TestHBitmapData *data, hbitmap_test_init(data, L1, 0); hbitmap_test_truncate_impl(data, L1 * 2); hbitmap_set(data->hb, L1 + 1, 1); - test_hbitmap_next_dirty_area_check(data, 0, UINT64_MAX); + test_hbitmap_next_dirty_area_check(data, 0, INT64_MAX); } =20 int main(int argc, char **argv) diff --git a/util/hbitmap.c b/util/hbitmap.c index b6d4b99a06..df22f06be6 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -193,7 +193,7 @@ void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap = *hb, uint64_t first) } } =20 -int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t start, uint64_t coun= t) +int64_t hbitmap_next_zero(const HBitmap *hb, int64_t start, int64_t count) { size_t pos =3D (start >> hb->granularity) >> BITS_PER_LEVEL; unsigned long *last_lev =3D hb->levels[HBITMAP_LEVELS - 1]; @@ -202,6 +202,8 @@ int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t s= tart, uint64_t count) uint64_t end_bit, sz; int64_t res; =20 + assert(start >=3D 0 && count >=3D 0); + if (start >=3D hb->orig_size || count =3D=3D 0) { return -1; } @@ -244,14 +246,15 @@ int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t= start, uint64_t count) return res; } =20 -bool hbitmap_next_dirty_area(const HBitmap *hb, uint64_t *start, - uint64_t *count) +bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t *start, int64_t *c= ount) { HBitmapIter hbi; int64_t firt_dirty_off, area_end; uint32_t granularity =3D 1UL << hb->granularity; uint64_t end; =20 + assert(*start >=3D 0 && *count >=3D 0); + if (*start >=3D hb->orig_size || *count =3D=3D 0) { return false; } @@ -834,8 +837,8 @@ bool hbitmap_can_merge(const HBitmap *a, const HBitmap = *b) */ static void hbitmap_sparse_merge(HBitmap *dst, const HBitmap *src) { - uint64_t offset =3D 0; - uint64_t count =3D src->orig_size; + int64_t offset =3D 0; + int64_t count =3D src->orig_size; =20 while (hbitmap_next_dirty_area(src, &offset, &count)) { hbitmap_set(dst, offset, count); --=20 2.21.0 From nobody Tue Apr 30 00:15:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1576750060; cv=none; d=zohomail.com; s=zohoarc; b=XLPlipHdqg4gYZshs6MtbkENGyGL7I6B0EPk0/x3Bt/03OKktZM8BFHYatTqFTA6mHTPJAmw37WQKDuARh2PaNAk4H0XMi9pjYBPCaSxOFJjn0gYmYGvijlS2kbEXlWf8SeQ7Lil7cZIeJK5Yh6xP5XDdu2dUaBfsBJt0QXqDZs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576750060; h=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=UecOWtAKh8IAxv8y01++yqSUqNQVC0tDH79/vHoh1Uk=; b=FPSIZGgTPXe7LgbKgnI6zvluWzlO43xHQ+mnxZnSfztpo9zBtaVbK8Bd5Vg/Ln813xqACjXF++uJFafLZZOBODREVEA0QOQkbAjZHXsn2wpAYoPDXwHYcO9Ezd+lPN5QHLwBH9LtYuhGhB/o34d/00tzQN2riHz/uSZuDK92DO0= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 1576750060543975.6038154313876; Thu, 19 Dec 2019 02:07:40 -0800 (PST) Received: from localhost ([::1]:38120 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsiZ-0008Bg-1l for importer@patchew.org; Thu, 19 Dec 2019 05:07:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39716) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsf8-0002nn-M7 for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihsf6-00089S-KU for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:06 -0500 Received: from relay.sw.ru ([185.231.240.75]:57618) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihsev-0007bX-I0; Thu, 19 Dec 2019 05:03:53 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ihset-0004j2-30; Thu, 19 Dec 2019 13:03:51 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v3 06/10] block/dirty-bitmap: add _next_dirty API Date: Thu, 19 Dec 2019 13:03:44 +0300 Message-Id: <20191219100348.24827-7-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191219100348.24827-1-vsementsov@virtuozzo.com> References: <20191219100348.24827-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" We have bdrv_dirty_bitmap_next_zero, let's add corresponding bdrv_dirty_bitmap_next_dirty, which is more comfortable to use than bitmap iterators in some cases. For test modify test_hbitmap_next_zero_check_range to check both next_zero and next_dirty and add some new checks. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- include/block/dirty-bitmap.h | 2 + include/qemu/hbitmap.h | 13 ++++ block/dirty-bitmap.c | 6 ++ tests/test-hbitmap.c | 130 ++++++++++++++++++++--------------- util/hbitmap.c | 60 ++++++++-------- 5 files changed, 126 insertions(+), 85 deletions(-) diff --git a/include/block/dirty-bitmap.h b/include/block/dirty-bitmap.h index 27c72cc56a..b1f0de12db 100644 --- a/include/block/dirty-bitmap.h +++ b/include/block/dirty-bitmap.h @@ -105,6 +105,8 @@ for (bitmap =3D bdrv_dirty_bitmap_first(bs); bitmap; \ bitmap =3D bdrv_dirty_bitmap_next(bitmap)) =20 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp= ); +int64_t bdrv_dirty_bitmap_next_dirty(BdrvDirtyBitmap *bitmap, int64_t offs= et, + int64_t bytes); int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offse= t, int64_t bytes); bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h index b6e85f3d5d..a4b032b270 100644 --- a/include/qemu/hbitmap.h +++ b/include/qemu/hbitmap.h @@ -297,6 +297,19 @@ void hbitmap_free(HBitmap *hb); */ void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first= ); =20 +/* + * hbitmap_next_dirty: + * + * Find next dirty bit within selected range. If not found, return -1. + * + * @hb: The HBitmap to operate on + * @start: The bit to start from. + * @count: Number of bits to proceed. If @start+@count > bitmap size, the = whole + * bitmap is looked through. You can use UINT64_MAX as @count to search up= to + * the bitmap end. + */ +int64_t hbitmap_next_dirty(const HBitmap *hb, int64_t start, int64_t count= ); + /* hbitmap_next_zero: * * Find next not dirty bit within selected range. If not found, return -1. diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c index af9f5411a6..1b14c8eb26 100644 --- a/block/dirty-bitmap.c +++ b/block/dirty-bitmap.c @@ -860,6 +860,12 @@ char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *= bitmap, Error **errp) return hbitmap_sha256(bitmap->bitmap, errp); } =20 +int64_t bdrv_dirty_bitmap_next_dirty(BdrvDirtyBitmap *bitmap, int64_t offs= et, + int64_t bytes) +{ + return hbitmap_next_dirty(bitmap->bitmap, offset, bytes); +} + int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offse= t, int64_t bytes) { diff --git a/tests/test-hbitmap.c b/tests/test-hbitmap.c index 0e1e5c64dd..e3f1b3f361 100644 --- a/tests/test-hbitmap.c +++ b/tests/test-hbitmap.c @@ -816,92 +816,108 @@ static void test_hbitmap_iter_and_reset(TestHBitmapD= ata *data, hbitmap_iter_next(&hbi); } =20 -static void test_hbitmap_next_zero_check_range(TestHBitmapData *data, - uint64_t start, - uint64_t count) +static void test_hbitmap_next_x_check_range(TestHBitmapData *data, + uint64_t start, + uint64_t count) { - int64_t ret1 =3D hbitmap_next_zero(data->hb, start, count); - int64_t ret2 =3D start; + int64_t next_zero =3D hbitmap_next_zero(data->hb, start, count); + int64_t next_dirty =3D hbitmap_next_dirty(data->hb, start, count); + int64_t next; int64_t end =3D start >=3D data->size || data->size - start < count ? data->size : start + count; + bool first_bit =3D hbitmap_get(data->hb, start); =20 - for ( ; ret2 < end && hbitmap_get(data->hb, ret2); ret2++) { + for (next =3D start; + next < end && hbitmap_get(data->hb, next) =3D=3D first_bit; + next++) + { ; } - if (ret2 =3D=3D end) { - ret2 =3D -1; + + if (next =3D=3D end) { + next =3D -1; } =20 - g_assert_cmpint(ret1, =3D=3D, ret2); + g_assert_cmpint(next_dirty, =3D=3D, first_bit ? start : next); + g_assert_cmpint(next_zero, =3D=3D, first_bit ? next : start); } =20 -static void test_hbitmap_next_zero_check(TestHBitmapData *data, int64_t st= art) +static void test_hbitmap_next_x_check(TestHBitmapData *data, int64_t start) { - test_hbitmap_next_zero_check_range(data, start, INT64_MAX); + test_hbitmap_next_x_check_range(data, start, INT64_MAX); } =20 -static void test_hbitmap_next_zero_do(TestHBitmapData *data, int granulari= ty) +static void test_hbitmap_next_x_do(TestHBitmapData *data, int granularity) { hbitmap_test_init(data, L3, granularity); - test_hbitmap_next_zero_check(data, 0); - test_hbitmap_next_zero_check(data, L3 - 1); - test_hbitmap_next_zero_check_range(data, 0, 1); - test_hbitmap_next_zero_check_range(data, L3 - 1, 1); + test_hbitmap_next_x_check(data, 0); + test_hbitmap_next_x_check(data, L3 - 1); + test_hbitmap_next_x_check_range(data, 0, 1); + test_hbitmap_next_x_check_range(data, L3 - 1, 1); =20 hbitmap_set(data->hb, L2, 1); - test_hbitmap_next_zero_check(data, 0); - test_hbitmap_next_zero_check(data, L2 - 1); - test_hbitmap_next_zero_check(data, L2); - test_hbitmap_next_zero_check(data, L2 + 1); - test_hbitmap_next_zero_check_range(data, 0, 1); - test_hbitmap_next_zero_check_range(data, 0, L2); - test_hbitmap_next_zero_check_range(data, L2 - 1, 1); - test_hbitmap_next_zero_check_range(data, L2 - 1, 2); - test_hbitmap_next_zero_check_range(data, L2, 1); - test_hbitmap_next_zero_check_range(data, L2 + 1, 1); + test_hbitmap_next_x_check(data, 0); + test_hbitmap_next_x_check(data, L2 - 1); + test_hbitmap_next_x_check(data, L2); + test_hbitmap_next_x_check(data, L2 + 1); + test_hbitmap_next_x_check_range(data, 0, 1); + test_hbitmap_next_x_check_range(data, 0, L2); + test_hbitmap_next_x_check_range(data, L2 - 1, 1); + test_hbitmap_next_x_check_range(data, L2 - 1, 2); + test_hbitmap_next_x_check_range(data, L2, 1); + test_hbitmap_next_x_check_range(data, L2 + 1, 1); =20 hbitmap_set(data->hb, L2 + 5, L1); - test_hbitmap_next_zero_check(data, 0); - test_hbitmap_next_zero_check(data, L2 + 1); - test_hbitmap_next_zero_check(data, L2 + 2); - test_hbitmap_next_zero_check(data, L2 + 5); - test_hbitmap_next_zero_check(data, L2 + L1 - 1); - test_hbitmap_next_zero_check(data, L2 + L1); - test_hbitmap_next_zero_check_range(data, L2, 6); - test_hbitmap_next_zero_check_range(data, L2 + 1, 3); - test_hbitmap_next_zero_check_range(data, L2 + 4, L1); - test_hbitmap_next_zero_check_range(data, L2 + 5, L1); + test_hbitmap_next_x_check(data, 0); + test_hbitmap_next_x_check(data, L2 - L1); + test_hbitmap_next_x_check(data, L2 + 1); + test_hbitmap_next_x_check(data, L2 + 2); + test_hbitmap_next_x_check(data, L2 + 5); + test_hbitmap_next_x_check(data, L2 + L1 - 1); + test_hbitmap_next_x_check(data, L2 + L1); + test_hbitmap_next_x_check(data, L2 + L1 + 1); + test_hbitmap_next_x_check_range(data, L2 - 2, L1); + test_hbitmap_next_x_check_range(data, L2, 4); + test_hbitmap_next_x_check_range(data, L2, 6); + test_hbitmap_next_x_check_range(data, L2 + 1, 3); + test_hbitmap_next_x_check_range(data, L2 + 4, L1); + test_hbitmap_next_x_check_range(data, L2 + 5, L1); + test_hbitmap_next_x_check_range(data, L2 + 5 + L1 - 1, 1); + test_hbitmap_next_x_check_range(data, L2 + 5 + L1, 1); + test_hbitmap_next_x_check_range(data, L2 + 5 + L1 + 1, 1); =20 hbitmap_set(data->hb, L2 * 2, L3 - L2 * 2); - test_hbitmap_next_zero_check(data, L2 * 2 - L1); - test_hbitmap_next_zero_check(data, L2 * 2 - 2); - test_hbitmap_next_zero_check(data, L2 * 2 - 1); - test_hbitmap_next_zero_check(data, L2 * 2); - test_hbitmap_next_zero_check(data, L3 - 1); - test_hbitmap_next_zero_check_range(data, L2 * 2 - L1, L1 + 1); - test_hbitmap_next_zero_check_range(data, L2 * 2, L2); + test_hbitmap_next_x_check(data, L2 * 2 - L1); + test_hbitmap_next_x_check(data, L2 * 2 - 2); + test_hbitmap_next_x_check(data, L2 * 2 - 1); + test_hbitmap_next_x_check(data, L2 * 2); + test_hbitmap_next_x_check(data, L2 * 2 + 1); + test_hbitmap_next_x_check(data, L2 * 2 + L1); + test_hbitmap_next_x_check(data, L3 - 1); + test_hbitmap_next_x_check_range(data, L2 * 2 - L1, L1 + 1); + test_hbitmap_next_x_check_range(data, L2 * 2, L2); =20 hbitmap_set(data->hb, 0, L3); - test_hbitmap_next_zero_check(data, 0); + test_hbitmap_next_x_check(data, 0); } =20 -static void test_hbitmap_next_zero_0(TestHBitmapData *data, const void *un= used) +static void test_hbitmap_next_x_0(TestHBitmapData *data, const void *unuse= d) { - test_hbitmap_next_zero_do(data, 0); + test_hbitmap_next_x_do(data, 0); } =20 -static void test_hbitmap_next_zero_4(TestHBitmapData *data, const void *un= used) +static void test_hbitmap_next_x_4(TestHBitmapData *data, const void *unuse= d) { - test_hbitmap_next_zero_do(data, 4); + test_hbitmap_next_x_do(data, 4); } =20 -static void test_hbitmap_next_zero_after_truncate(TestHBitmapData *data, - const void *unused) +static void test_hbitmap_next_x_after_truncate(TestHBitmapData *data, + const void *unused) { hbitmap_test_init(data, L1, 0); hbitmap_test_truncate_impl(data, L1 * 2); hbitmap_set(data->hb, 0, L1); - test_hbitmap_next_zero_check(data, 0); + test_hbitmap_next_x_check(data, 0); } =20 static void test_hbitmap_next_dirty_area_check(TestHBitmapData *data, @@ -1068,12 +1084,12 @@ int main(int argc, char **argv) hbitmap_test_add("/hbitmap/iter/iter_and_reset", test_hbitmap_iter_and_reset); =20 - hbitmap_test_add("/hbitmap/next_zero/next_zero_0", - test_hbitmap_next_zero_0); - hbitmap_test_add("/hbitmap/next_zero/next_zero_4", - test_hbitmap_next_zero_4); - hbitmap_test_add("/hbitmap/next_zero/next_zero_after_truncate", - test_hbitmap_next_zero_after_truncate); + hbitmap_test_add("/hbitmap/next_zero/next_x_0", + test_hbitmap_next_x_0); + hbitmap_test_add("/hbitmap/next_zero/next_x_4", + test_hbitmap_next_x_4); + hbitmap_test_add("/hbitmap/next_zero/next_x_after_truncate", + test_hbitmap_next_x_after_truncate); =20 hbitmap_test_add("/hbitmap/next_dirty_area/next_dirty_area_0", test_hbitmap_next_dirty_area_0); diff --git a/util/hbitmap.c b/util/hbitmap.c index df22f06be6..d23f4b9678 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -193,6 +193,30 @@ void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap= *hb, uint64_t first) } } =20 +int64_t hbitmap_next_dirty(const HBitmap *hb, int64_t start, int64_t count) +{ + HBitmapIter hbi; + int64_t firt_dirty_off; + uint64_t end; + + assert(start >=3D 0 && count >=3D 0); + + if (start >=3D hb->orig_size || count =3D=3D 0) { + return -1; + } + + end =3D count > hb->orig_size - start ? hb->orig_size : start + count; + + hbitmap_iter_init(&hbi, hb, start); + firt_dirty_off =3D hbitmap_iter_next(&hbi); + + if (firt_dirty_off < 0 || firt_dirty_off >=3D end) { + return -1; + } + + return MAX(start, firt_dirty_off); +} + int64_t hbitmap_next_zero(const HBitmap *hb, int64_t start, int64_t count) { size_t pos =3D (start >> hb->granularity) >> BITS_PER_LEVEL; @@ -248,40 +272,20 @@ int64_t hbitmap_next_zero(const HBitmap *hb, int64_t = start, int64_t count) =20 bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t *start, int64_t *c= ount) { - HBitmapIter hbi; - int64_t firt_dirty_off, area_end; - uint32_t granularity =3D 1UL << hb->granularity; - uint64_t end; - - assert(*start >=3D 0 && *count >=3D 0); - - if (*start >=3D hb->orig_size || *count =3D=3D 0) { - return false; - } - - end =3D *count > hb->orig_size - *start ? hb->orig_size : *start + *co= unt; - - hbitmap_iter_init(&hbi, hb, *start); - firt_dirty_off =3D hbitmap_iter_next(&hbi); + int64_t area_start, area_end; =20 - if (firt_dirty_off < 0 || firt_dirty_off >=3D end) { + area_start =3D hbitmap_next_dirty(hb, *start, *count); + if (area_start < 0) { return false; } =20 - if (firt_dirty_off + granularity >=3D end) { - area_end =3D end; - } else { - area_end =3D hbitmap_next_zero(hb, firt_dirty_off + granularity, - end - firt_dirty_off - granularity); - if (area_end < 0) { - area_end =3D end; - } + area_end =3D hbitmap_next_zero(hb, area_start, *start + *count - area_= start); + if (area_end < 0) { + area_end =3D MIN(hb->orig_size, *start + *count); } =20 - if (firt_dirty_off > *start) { - *start =3D firt_dirty_off; - } - *count =3D area_end - *start; + *start =3D area_start; + *count =3D area_end - area_start; =20 return true; } --=20 2.21.0 From nobody Tue Apr 30 00:15:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1576750165; cv=none; d=zohomail.com; s=zohoarc; b=L45ThSVih3mt+fPh+tXIKMRU9QuWIdAgNupM+f69Pkbr52ZNtSdAosS2z2bllcc36lqnoCs058qFM1SUcjB1XZYCxPKi4iR4oFttU/B2+5IRNeME1QOtZQ7lJTTcpkkQFsFcFAgfTqDUFtCeKq26Ugg2LgSxnm5J0o6OvyEZclk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576750165; h=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=fRFWPI2x7gHaoX5YkXvhXsJlZB2gjQ+VdylYZoEX08U=; b=nqfjyhyu3SrQxo+fEaFrfJxWzFliI8eg/OooQrOZJEGUerFJbvfYVx+wMgqTFESVL8ePxOluKSdWyo2L061rEKAIcrAmmTcI4CXtzfvy4GUVS3xR8gJxf0/cCfrG12pkSn0RKhQKH27vB4AP20WgwaIrR6k7oP5RN82iFmfJq7w= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 1576750165903787.9773106210259; Thu, 19 Dec 2019 02:09:25 -0800 (PST) Received: from localhost ([::1]:38148 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihskG-0002YV-JO for importer@patchew.org; Thu, 19 Dec 2019 05:09:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39352) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsf6-0002jc-C6 for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihsf3-0007yG-OW for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:04 -0500 Received: from relay.sw.ru ([185.231.240.75]:57622) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihsev-0007cy-92; Thu, 19 Dec 2019 05:03:53 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ihset-0004j2-An; Thu, 19 Dec 2019 13:03:51 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v3 07/10] block/dirty-bitmap: improve _next_dirty_area API Date: Thu, 19 Dec 2019 13:03:45 +0300 Message-Id: <20191219100348.24827-8-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191219100348.24827-1-vsementsov@virtuozzo.com> References: <20191219100348.24827-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Firstly, _next_dirty_area is for scenarios when we may contiguously search for next dirty area inside some limited region, so it is more comfortable to specify "end" which should not be recalculated on each iteration. Secondly, let's add a possibility to limit resulting area size, not limiting searching area. This will be used in NBD code in further commit. (Note that now bdrv_dirty_bitmap_next_dirty_area is unused) Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- include/block/dirty-bitmap.h | 3 ++- include/qemu/hbitmap.h | 25 ++++++++++++--------- block/dirty-bitmap.c | 6 +++-- tests/test-hbitmap.c | 43 +++++++++++++++++++++++------------- util/hbitmap.c | 41 +++++++++++++++++++++------------- 5 files changed, 74 insertions(+), 44 deletions(-) diff --git a/include/block/dirty-bitmap.h b/include/block/dirty-bitmap.h index b1f0de12db..8a10029418 100644 --- a/include/block/dirty-bitmap.h +++ b/include/block/dirty-bitmap.h @@ -110,7 +110,8 @@ int64_t bdrv_dirty_bitmap_next_dirty(BdrvDirtyBitmap *b= itmap, int64_t offset, int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offse= t, int64_t bytes); bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, - int64_t *offset, int64_t *bytes); + int64_t start, int64_t end, int64_t max_dirty_count, + int64_t *dirty_start, int64_t *dirty_count); BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, Error **errp); =20 diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h index a4b032b270..4e7a6e72ef 100644 --- a/include/qemu/hbitmap.h +++ b/include/qemu/hbitmap.h @@ -324,18 +324,21 @@ int64_t hbitmap_next_zero(const HBitmap *hb, int64_t = start, int64_t count); =20 /* hbitmap_next_dirty_area: * @hb: The HBitmap to operate on - * @start: in-out parameter. - * in: the offset to start from - * out: (if area found) start of found area - * @count: in-out parameter. - * in: length of requested region - * out: length of found area - * - * If dirty area found within [@start, @start + @count), returns true and = sets - * @offset and @bytes appropriately. Otherwise returns false and leaves @o= ffset - * and @bytes unchanged. + * @start: the offset to start from + * @end: end of requested area + * @max_dirty_count: limit for out parameter dirty_count + * @dirty_start: on success: start of found area + * @dirty_count: on success: length of found area + * + * If dirty area found within [@start, @end), returns true and sets + * @dirty_start and @dirty_count appropriately. @dirty_count will not exce= ed + * @max_dirty_count. + * If dirty area was not found, returns false and leaves @dirty_start and + * @dirty_count unchanged. */ -bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t *start, int64_t *c= ount); +bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t start, int64_t end, + int64_t max_dirty_count, + int64_t *dirty_start, int64_t *dirty_count); =20 /** * hbitmap_iter_next: diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c index 1b14c8eb26..063793e316 100644 --- a/block/dirty-bitmap.c +++ b/block/dirty-bitmap.c @@ -873,9 +873,11 @@ int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *b= itmap, int64_t offset, } =20 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, - int64_t *offset, int64_t *bytes) + int64_t start, int64_t end, int64_t max_dirty_count, + int64_t *dirty_start, int64_t *dirty_count) { - return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes); + return hbitmap_next_dirty_area(bitmap->bitmap, start, end, max_dirty_c= ount, + dirty_start, dirty_count); } =20 /** diff --git a/tests/test-hbitmap.c b/tests/test-hbitmap.c index e3f1b3f361..d75e84a76a 100644 --- a/tests/test-hbitmap.c +++ b/tests/test-hbitmap.c @@ -920,18 +920,19 @@ static void test_hbitmap_next_x_after_truncate(TestHB= itmapData *data, test_hbitmap_next_x_check(data, 0); } =20 -static void test_hbitmap_next_dirty_area_check(TestHBitmapData *data, - int64_t offset, - int64_t count) +static void test_hbitmap_next_dirty_area_check_limited(TestHBitmapData *da= ta, + int64_t offset, + int64_t count, + int64_t max_dirty) { int64_t off1, off2; int64_t len1 =3D 0, len2; bool ret1, ret2; int64_t end; =20 - off1 =3D offset; - len1 =3D count; - ret1 =3D hbitmap_next_dirty_area(data->hb, &off1, &len1); + ret1 =3D hbitmap_next_dirty_area(data->hb, + offset, count =3D=3D INT64_MAX ? INT64_MAX : offset + count, m= ax_dirty, + &off1, &len1); =20 end =3D offset > data->size || data->size - offset < count ? data->siz= e : offset + co= unt; @@ -940,21 +941,25 @@ static void test_hbitmap_next_dirty_area_check(TestHB= itmapData *data, ; } =20 - for (len2 =3D 1; off2 + len2 < end && hbitmap_get(data->hb, off2 + len= 2); - len2++) { + for (len2 =3D 1; (off2 + len2 < end && len2 < max_dirty && + hbitmap_get(data->hb, off2 + len2)); len2++) + { ; } =20 ret2 =3D off2 < end; - if (!ret2) { - /* leave unchanged */ - off2 =3D offset; - len2 =3D count; + g_assert_cmpint(ret1, =3D=3D, ret2); + + if (ret2) { + g_assert_cmpint(off1, =3D=3D, off2); + g_assert_cmpint(len1, =3D=3D, len2); } +} =20 - g_assert_cmpint(ret1, =3D=3D, ret2); - g_assert_cmpint(off1, =3D=3D, off2); - g_assert_cmpint(len1, =3D=3D, len2); +static void test_hbitmap_next_dirty_area_check(TestHBitmapData *data, + int64_t offset, int64_t cou= nt) +{ + test_hbitmap_next_dirty_area_check_limited(data, offset, count, INT64_= MAX); } =20 static void test_hbitmap_next_dirty_area_do(TestHBitmapData *data, @@ -964,6 +969,7 @@ static void test_hbitmap_next_dirty_area_do(TestHBitmap= Data *data, test_hbitmap_next_dirty_area_check(data, 0, INT64_MAX); test_hbitmap_next_dirty_area_check(data, 0, 1); test_hbitmap_next_dirty_area_check(data, L3 - 1, 1); + test_hbitmap_next_dirty_area_check_limited(data, 0, INT64_MAX, 1); =20 hbitmap_set(data->hb, L2, 1); test_hbitmap_next_dirty_area_check(data, 0, 1); @@ -976,6 +982,8 @@ static void test_hbitmap_next_dirty_area_do(TestHBitmap= Data *data, test_hbitmap_next_dirty_area_check(data, L2, INT64_MAX); test_hbitmap_next_dirty_area_check(data, L2, 1); test_hbitmap_next_dirty_area_check(data, L2 + 1, 1); + test_hbitmap_next_dirty_area_check_limited(data, 0, INT64_MAX, 1); + test_hbitmap_next_dirty_area_check_limited(data, L2 - 1, 2, 1); =20 hbitmap_set(data->hb, L2 + 5, L1); test_hbitmap_next_dirty_area_check(data, 0, INT64_MAX); @@ -988,6 +996,8 @@ static void test_hbitmap_next_dirty_area_do(TestHBitmap= Data *data, test_hbitmap_next_dirty_area_check(data, L2 + L1, L1); test_hbitmap_next_dirty_area_check(data, L2, 0); test_hbitmap_next_dirty_area_check(data, L2 + 1, 0); + test_hbitmap_next_dirty_area_check_limited(data, L2 + 3, INT64_MAX, 3); + test_hbitmap_next_dirty_area_check_limited(data, L2 + 3, 7, 10); =20 hbitmap_set(data->hb, L2 * 2, L3 - L2 * 2); test_hbitmap_next_dirty_area_check(data, 0, INT64_MAX); @@ -997,6 +1007,9 @@ static void test_hbitmap_next_dirty_area_do(TestHBitma= pData *data, test_hbitmap_next_dirty_area_check(data, L2 + 5 + L1, 5); test_hbitmap_next_dirty_area_check(data, L2 * 2 - L1, L1 + 1); test_hbitmap_next_dirty_area_check(data, L2 * 2, L2); + test_hbitmap_next_dirty_area_check_limited(data, L2 * 2 + 1, INT64_MAX= , 5); + test_hbitmap_next_dirty_area_check_limited(data, L2 * 2 + 1, 10, 5); + test_hbitmap_next_dirty_area_check_limited(data, L2 * 2 + 1, 2, 5); =20 hbitmap_set(data->hb, 0, L3); test_hbitmap_next_dirty_area_check(data, 0, INT64_MAX); diff --git a/util/hbitmap.c b/util/hbitmap.c index d23f4b9678..2a1661ec1d 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -270,22 +270,34 @@ int64_t hbitmap_next_zero(const HBitmap *hb, int64_t = start, int64_t count) return res; } =20 -bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t *start, int64_t *c= ount) +bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t start, int64_t end, + int64_t max_dirty_count, + int64_t *dirty_start, int64_t *dirty_count) { - int64_t area_start, area_end; + int64_t next_zero; =20 - area_start =3D hbitmap_next_dirty(hb, *start, *count); - if (area_start < 0) { + assert(start >=3D 0 && end >=3D 0 && max_dirty_count > 0); + + if (start >=3D hb->orig_size || end <=3D start) { + return false; + } + + end =3D MIN(end, hb->orig_size); + + start =3D hbitmap_next_dirty(hb, start, end - start); + if (start < 0) { return false; } =20 - area_end =3D hbitmap_next_zero(hb, area_start, *start + *count - area_= start); - if (area_end < 0) { - area_end =3D MIN(hb->orig_size, *start + *count); + end =3D start + MIN(end - start, max_dirty_count); + + next_zero =3D hbitmap_next_zero(hb, start, end - start); + if (next_zero >=3D 0) { + end =3D next_zero; } =20 - *start =3D area_start; - *count =3D area_end - area_start; + *dirty_start =3D start; + *dirty_count =3D end - start; =20 return true; } @@ -844,13 +856,12 @@ static void hbitmap_sparse_merge(HBitmap *dst, const = HBitmap *src) int64_t offset =3D 0; int64_t count =3D src->orig_size; =20 - while (hbitmap_next_dirty_area(src, &offset, &count)) { + for (offset =3D 0; + hbitmap_next_dirty_area(src, offset, src->orig_size, INT64_MAX, + &offset, &count); + offset +=3D count) + { hbitmap_set(dst, offset, count); - offset +=3D count; - if (offset >=3D src->orig_size) { - break; - } - count =3D src->orig_size - offset; } } =20 --=20 2.21.0 From nobody Tue Apr 30 00:15:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1576750153; cv=none; d=zohomail.com; s=zohoarc; b=aum9mo5W7mx5GgDMClpUi6O6Z3o1Rxpcn10rz/ytTIMADtggkAv0NwGZr70JVzs6oux6Qr0nNkpymG0gVEshQoJLuiqROibaK3GEwJwoeuh9dY94bkuyH7ksIy6SZ/T91opWkWsOu+EE4CF02nPWHGdnrEeLvYzRi24XEzWZHj0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576750153; h=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=mxl7AvlYRGyD1c31Ap6unJ9LssiZmZCVQU5dZFUrKmw=; b=A5LZnstkJRa0gNSNDg6/Kmnf4vmpmXvJgxHNhsa4JhIcxyUcttqkCJFEtRWGtkgiJYiwq3MGBHjzomTpwREdX7KKxpuBL+FWFXUIIGWeWOrup8YPia0DPFwpndNgI1TGNk8Jm5NUHoShU73afyjFDm2YWOlKifDci/is8sxanAM= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 1576750153440938.5140873748232; Thu, 19 Dec 2019 02:09:13 -0800 (PST) Received: from localhost ([::1]:38146 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsk3-0002Ep-Hs for importer@patchew.org; Thu, 19 Dec 2019 05:09:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39594) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsf7-0002mG-Tj for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihsf5-00085P-OG for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:05 -0500 Received: from relay.sw.ru ([185.231.240.75]:57626) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihsev-0007do-QP; Thu, 19 Dec 2019 05:03:54 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ihset-0004j2-I4; Thu, 19 Dec 2019 13:03:51 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v3 08/10] nbd/server: introduce NBDExtentArray Date: Thu, 19 Dec 2019 13:03:46 +0300 Message-Id: <20191219100348.24827-9-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191219100348.24827-1-vsementsov@virtuozzo.com> References: <20191219100348.24827-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Introduce NBDExtentArray class, to handle extents list creation in more controlled way and with less OUT parameters in functions. Signed-off-by: Vladimir Sementsov-Ogievskiy --- nbd/server.c | 201 ++++++++++++++++++++++++++++----------------------- 1 file changed, 109 insertions(+), 92 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index a4b348eb32..cc722adc31 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1909,27 +1909,89 @@ static int coroutine_fn nbd_co_send_sparse_read(NBD= Client *client, return ret; } =20 +typedef struct NBDExtentArray { + NBDExtent *extents; + unsigned int nb_alloc; + unsigned int count; + uint64_t total_length; + bool converted; /* extents are converted to BE, no more changes allowe= d */ +} NBDExtentArray; + +static NBDExtentArray *nbd_extent_array_new(unsigned int nb_alloc) +{ + NBDExtentArray *ea =3D g_new0(NBDExtentArray, 1); + + ea->nb_alloc =3D nb_alloc; + ea->extents =3D g_new(NBDExtent, nb_alloc); + + return ea; +} + +static void nbd_extent_array_free(NBDExtentArray *ea) +{ + g_free(ea->extents); + g_free(ea); +} +G_DEFINE_AUTOPTR_CLEANUP_FUNC(NBDExtentArray, nbd_extent_array_free); + +/* Further modifications of the array after conversion are abandoned */ +static void nbd_extent_array_convert_to_be(NBDExtentArray *ea) +{ + int i; + + if (ea->converted) { + return; + } + ea->converted =3D true; + + for (i =3D 0; i < ea->count; i++) { + ea->extents[i].flags =3D cpu_to_be32(ea->extents[i].flags); + ea->extents[i].length =3D cpu_to_be32(ea->extents[i].length); + } +} + /* - * Populate @extents from block status. Update @bytes to be the actual - * length encoded (which may be smaller than the original), and update - * @nb_extents to the number of extents used. - * - * Returns zero on success and -errno on bdrv_block_status_above failure. + * Add extent to NBDExtentArray. If extent can't be added (no available sp= ace), + * return -1. + * For safety, when returning -1 for the first time, the array is converted + * to BE and further modifications are abandoned. */ -static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset, - uint64_t *bytes, NBDExtent *extents, - unsigned int *nb_extents) +static int nbd_extent_array_add(NBDExtentArray *ea, + uint32_t length, uint32_t flags) { - uint64_t remaining_bytes =3D *bytes; - NBDExtent *extent =3D extents, *extents_end =3D extents + *nb_extents; - bool first_extent =3D true; + assert(!ea->converted); + + if (!length) { + return 0; + } + + /* Extend previous extent if flags are the same */ + if (ea->count > 0 && flags =3D=3D ea->extents[ea->count - 1].flags) { + ea->extents[ea->count - 1].length +=3D length; + ea->total_length +=3D length; + return 0; + } + + if (ea->count >=3D ea->nb_alloc) { + nbd_extent_array_convert_to_be(ea); + return -1; + } + + ea->total_length +=3D length; + ea->extents[ea->count] =3D (NBDExtent) {.length =3D length, .flags =3D= flags}; + ea->count++; =20 - assert(*nb_extents); - while (remaining_bytes) { + return 0; +} + +static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset, + uint64_t bytes, NBDExtentArray *ea) +{ + while (bytes) { uint32_t flags; int64_t num; - int ret =3D bdrv_block_status_above(bs, NULL, offset, remaining_by= tes, - &num, NULL, NULL); + int ret =3D bdrv_block_status_above(bs, NULL, offset, bytes, &num, + NULL, NULL); =20 if (ret < 0) { return ret; @@ -1938,60 +2000,37 @@ static int blockstatus_to_extents(BlockDriverState = *bs, uint64_t offset, flags =3D (ret & BDRV_BLOCK_ALLOCATED ? 0 : NBD_STATE_HOLE) | (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0); =20 - if (first_extent) { - extent->flags =3D flags; - extent->length =3D num; - first_extent =3D false; - } else if (flags =3D=3D extent->flags) { - /* extend current extent */ - extent->length +=3D num; - } else { - if (extent + 1 =3D=3D extents_end) { - break; - } - - /* start new extent */ - extent++; - extent->flags =3D flags; - extent->length =3D num; + if (nbd_extent_array_add(ea, num, flags) < 0) { + return 0; } - offset +=3D num; - remaining_bytes -=3D num; - } - - extents_end =3D extent + 1; =20 - for (extent =3D extents; extent < extents_end; extent++) { - extent->flags =3D cpu_to_be32(extent->flags); - extent->length =3D cpu_to_be32(extent->length); + offset +=3D num; + bytes -=3D num; } =20 - *bytes -=3D remaining_bytes; - *nb_extents =3D extents_end - extents; - return 0; } =20 -/* nbd_co_send_extents +/* + * nbd_co_send_extents * - * @length is only for tracing purposes (and may be smaller or larger - * than the client's original request). @last controls whether - * NBD_REPLY_FLAG_DONE is sent. @extents should already be in - * big-endian format. + * @ea is converted to BE by the function + * @last controls whether NBD_REPLY_FLAG_DONE is sent. */ static int nbd_co_send_extents(NBDClient *client, uint64_t handle, - NBDExtent *extents, unsigned int nb_extents, - uint64_t length, bool last, - uint32_t context_id, Error **errp) + NBDExtentArray *ea, + bool last, uint32_t context_id, Error **err= p) { NBDStructuredMeta chunk; - struct iovec iov[] =3D { {.iov_base =3D &chunk, .iov_len =3D sizeof(chunk)}, - {.iov_base =3D extents, .iov_len =3D nb_extents * sizeof(extents[0= ])} + {.iov_base =3D ea->extents, .iov_len =3D ea->count * sizeof(ea->ex= tents[0])} }; =20 - trace_nbd_co_send_extents(handle, nb_extents, context_id, length, last= ); + nbd_extent_array_convert_to_be(ea); + + trace_nbd_co_send_extents(handle, ea->count, context_id, ea->total_len= gth, + last); set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0, NBD_REPLY_TYPE_BLOCK_STATUS, handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len); @@ -2009,39 +2048,27 @@ static int nbd_co_send_block_status(NBDClient *clie= nt, uint64_t handle, { int ret; unsigned int nb_extents =3D dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_E= XTENTS; - NBDExtent *extents =3D g_new(NBDExtent, nb_extents); - uint64_t final_length =3D length; + g_autoptr(NBDExtentArray) ea =3D nbd_extent_array_new(nb_extents); =20 - ret =3D blockstatus_to_extents(bs, offset, &final_length, extents, - &nb_extents); + ret =3D blockstatus_to_extents(bs, offset, length, ea); if (ret < 0) { - g_free(extents); return nbd_co_send_structured_error( client, handle, -ret, "can't get block status", errp); } =20 - ret =3D nbd_co_send_extents(client, handle, extents, nb_extents, - final_length, last, context_id, errp); - - g_free(extents); - - return ret; + return nbd_co_send_extents(client, handle, ea, last, context_id, errp); } =20 /* - * Populate @extents from a dirty bitmap. Unless @dont_fragment, the - * final extent may exceed the original @length. Store in @length the - * byte length encoded (which may be smaller or larger than the - * original), and return the number of extents used. + * Populate @ea from a dirty bitmap. Unless @dont_fragment, the + * final extent may exceed the original @length. */ -static unsigned int bitmap_to_extents(BdrvDirtyBitmap *bitmap, uint64_t of= fset, - uint64_t *length, NBDExtent *extents, - unsigned int nb_extents, - bool dont_fragment) +static void bitmap_to_extents(BdrvDirtyBitmap *bitmap, + uint64_t offset, uint64_t length, + NBDExtentArray *ea, bool dont_fragment) { uint64_t begin =3D offset, end =3D offset; - uint64_t overall_end =3D offset + *length; - unsigned int i =3D 0; + uint64_t overall_end =3D offset + length; BdrvDirtyBitmapIter *it; bool dirty; =20 @@ -2050,8 +2077,7 @@ static unsigned int bitmap_to_extents(BdrvDirtyBitmap= *bitmap, uint64_t offset, it =3D bdrv_dirty_iter_new(bitmap); dirty =3D bdrv_dirty_bitmap_get_locked(bitmap, offset); =20 - assert(begin < overall_end && nb_extents); - while (begin < overall_end && i < nb_extents) { + while (begin < overall_end) { bool next_dirty =3D !dirty; =20 if (dirty) { @@ -2071,9 +2097,10 @@ static unsigned int bitmap_to_extents(BdrvDirtyBitma= p *bitmap, uint64_t offset, end =3D overall_end; } =20 - extents[i].length =3D cpu_to_be32(end - begin); - extents[i].flags =3D cpu_to_be32(dirty ? NBD_STATE_DIRTY : 0); - i++; + if (nbd_extent_array_add(ea, end - begin, + dirty ? NBD_STATE_DIRTY : 0) < 0) { + break; + } begin =3D end; dirty =3D next_dirty; } @@ -2083,8 +2110,6 @@ static unsigned int bitmap_to_extents(BdrvDirtyBitmap= *bitmap, uint64_t offset, bdrv_dirty_bitmap_unlock(bitmap); =20 assert(offset < end); - *length =3D end - offset; - return i; } =20 static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle, @@ -2092,20 +2117,12 @@ static int nbd_co_send_bitmap(NBDClient *client, ui= nt64_t handle, uint32_t length, bool dont_fragment, bool la= st, uint32_t context_id, Error **errp) { - int ret; unsigned int nb_extents =3D dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_E= XTENTS; - NBDExtent *extents =3D g_new(NBDExtent, nb_extents); - uint64_t final_length =3D length; + g_autoptr(NBDExtentArray) ea =3D nbd_extent_array_new(nb_extents); =20 - nb_extents =3D bitmap_to_extents(bitmap, offset, &final_length, extent= s, - nb_extents, dont_fragment); + bitmap_to_extents(bitmap, offset, length, ea, dont_fragment); =20 - ret =3D nbd_co_send_extents(client, handle, extents, nb_extents, - final_length, last, context_id, errp); - - g_free(extents); - - return ret; + return nbd_co_send_extents(client, handle, ea, last, context_id, errp); } =20 /* nbd_co_receive_request --=20 2.21.0 From nobody Tue Apr 30 00:15:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1576749982; cv=none; d=zohomail.com; s=zohoarc; b=ZZVxX1MBpi18P+mrcpqQufFe7+TP6vS0pXN1J9X58QmbZ9vIJbVGAxt2d8hoOWyqGzCe4gcd53Zm2okg6E7FnDOv82cSVo8IuiyYizAxADK4SCsFZv2CjOtvX8Vd7Bo2rQ4TLRI4Js6xNUpLQRRRCK0vKgsMnwnNzComfybS3LI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576749982; h=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=ep7mi0npdGEUmjbOMjzsCp7Atj3AlmEB3drFNRrUhgc=; b=R6DZKYqXGekSc6qY9iUGKjUZnkmQrHd9kWpVjqwGOLFgq1pTw5s45Oj6r9aph9/mqFa1vMB4O5Njp1mutcl/KMOrYEKUoSnIzrEYU3uFyNF4ctl4qPg9dYvv4dix2hVreMBRE6c5szsyxFkFzK6TCbSUxEI7pKhGSadM2qsyWu4= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 157674998252545.56530027405586; Thu, 19 Dec 2019 02:06:22 -0800 (PST) Received: from localhost ([::1]:38095 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihshI-0005hN-R5 for importer@patchew.org; Thu, 19 Dec 2019 05:06:20 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39081) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsf4-0002fw-FY for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihsf2-0007vV-P8 for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:02 -0500 Received: from relay.sw.ru ([185.231.240.75]:57632) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihsev-0007fI-FZ; Thu, 19 Dec 2019 05:03:53 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ihset-0004j2-St; Thu, 19 Dec 2019 13:03:52 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v3 09/10] nbd/server: use bdrv_dirty_bitmap_next_dirty_area Date: Thu, 19 Dec 2019 13:03:47 +0300 Message-Id: <20191219100348.24827-10-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191219100348.24827-1-vsementsov@virtuozzo.com> References: <20191219100348.24827-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Use bdrv_dirty_bitmap_next_dirty_area for bitmap_to_extents. Since bdrv_dirty_bitmap_next_dirty_area is very accurate in its interface, we'll never exceed requested region with last chunk. So, we don't need dont_fragment, and bitmap_to_extents() interface becomes clean enough to not require any comment. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake --- nbd/server.c | 59 +++++++++++++++++----------------------------------- 1 file changed, 19 insertions(+), 40 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index cc722adc31..461566a051 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -2059,57 +2059,36 @@ static int nbd_co_send_block_status(NBDClient *clie= nt, uint64_t handle, return nbd_co_send_extents(client, handle, ea, last, context_id, errp); } =20 -/* - * Populate @ea from a dirty bitmap. Unless @dont_fragment, the - * final extent may exceed the original @length. - */ +/* Populate @ea from a dirty bitmap. */ static void bitmap_to_extents(BdrvDirtyBitmap *bitmap, uint64_t offset, uint64_t length, - NBDExtentArray *ea, bool dont_fragment) + NBDExtentArray *es) { - uint64_t begin =3D offset, end =3D offset; - uint64_t overall_end =3D offset + length; - BdrvDirtyBitmapIter *it; - bool dirty; + int64_t start, dirty_start, dirty_count; + int64_t end =3D offset + length; + bool full =3D false; =20 bdrv_dirty_bitmap_lock(bitmap); =20 - it =3D bdrv_dirty_iter_new(bitmap); - dirty =3D bdrv_dirty_bitmap_get_locked(bitmap, offset); - - while (begin < overall_end) { - bool next_dirty =3D !dirty; - - if (dirty) { - end =3D bdrv_dirty_bitmap_next_zero(bitmap, begin, INT64_MAX); - } else { - bdrv_set_dirty_iter(it, begin); - end =3D bdrv_dirty_iter_next(it); - } - if (end =3D=3D -1 || end - begin > UINT32_MAX) { - /* Cap to an aligned value < 4G beyond begin. */ - end =3D MIN(bdrv_dirty_bitmap_size(bitmap), - begin + UINT32_MAX + 1 - - bdrv_dirty_bitmap_granularity(bitmap)); - next_dirty =3D dirty; - } - if (dont_fragment && end > overall_end) { - end =3D overall_end; - } - - if (nbd_extent_array_add(ea, end - begin, - dirty ? NBD_STATE_DIRTY : 0) < 0) { + for (start =3D offset; + bdrv_dirty_bitmap_next_dirty_area(bitmap, start, end, INT32_MAX, + &dirty_start, &dirty_count); + start =3D dirty_start + dirty_count) + { + if ((nbd_extent_array_add(es, dirty_start - start, 0) < 0) || + (nbd_extent_array_add(es, dirty_count, NBD_STATE_DIRTY) < 0)) + { + full =3D true; break; } - begin =3D end; - dirty =3D next_dirty; } =20 - bdrv_dirty_iter_free(it); + if (!full) { + /* last non dirty extent */ + nbd_extent_array_add(es, end - start, 0); + } =20 bdrv_dirty_bitmap_unlock(bitmap); - - assert(offset < end); } =20 static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle, @@ -2120,7 +2099,7 @@ static int nbd_co_send_bitmap(NBDClient *client, uint= 64_t handle, unsigned int nb_extents =3D dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_E= XTENTS; g_autoptr(NBDExtentArray) ea =3D nbd_extent_array_new(nb_extents); =20 - bitmap_to_extents(bitmap, offset, length, ea, dont_fragment); + bitmap_to_extents(bitmap, offset, length, ea); =20 return nbd_co_send_extents(client, handle, ea, last, context_id, errp); } --=20 2.21.0 From nobody Tue Apr 30 00:15:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1576749949; cv=none; d=zohomail.com; s=zohoarc; b=XO0TTwEyPyrMn2BCi0xhzNLZkKAT/upLcnhPREZLIamofYKIcegZvUNVxCqaQ4TUDxYItGykOOirv8XdtBX31NIS6VRcJ581IO+XBeM8DNZQ4d6W0nBUtSpwycIVgharUxe+NiO/RNUbHL252lmu3wJkKvrzMQapzxDT3XMpBOM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576749949; h=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=o2x/wdf2GkSc7iEjdFh0kwUmwx6cPrggkhTZKdnDZsU=; b=lFYTylHDmtMjRV65w+dCWh9cI1ZvfPVQd9mFSBLBSRUpiNnghTkhfwwwJ9oDZqmS1ZNRk2R2DGDKsWAi4nQleiSQZh10Wb5DbrT7QIQxtXSbylVgESNfAFAtw6EzjTAxhgs5BR6TIxUYYSZ0xjIAjpUAwxGWUb/Bz04Rr+wGbCA= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.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 1576749949525758.2423767542875; Thu, 19 Dec 2019 02:05:49 -0800 (PST) Received: from localhost ([::1]:38080 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsgl-0004lm-5I for importer@patchew.org; Thu, 19 Dec 2019 05:05:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39778) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihsf9-0002of-2x for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihsf3-0007xx-Lu for qemu-devel@nongnu.org; Thu, 19 Dec 2019 05:04:06 -0500 Received: from relay.sw.ru ([185.231.240.75]:57636) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihsev-0007gM-MA; Thu, 19 Dec 2019 05:03:53 -0500 Received: from vovaso.qa.sw.ru ([10.94.3.0] helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1ihseu-0004j2-6p; Thu, 19 Dec 2019 13:03:52 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v3 10/10] block/qcow2-bitmap: use bdrv_dirty_bitmap_next_dirty Date: Thu, 19 Dec 2019 13:03:48 +0300 Message-Id: <20191219100348.24827-11-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191219100348.24827-1-vsementsov@virtuozzo.com> References: <20191219100348.24827-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" store_bitmap_data() loop does bdrv_set_dirty_iter() on each iteration, which means that we actually don't need iterator itself and we can use simpler bitmap API. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- block/qcow2-bitmap.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c index c6c8ebbe89..015f5d18d2 100644 --- a/block/qcow2-bitmap.c +++ b/block/qcow2-bitmap.c @@ -1289,7 +1289,6 @@ static uint64_t *store_bitmap_data(BlockDriverState *= bs, uint64_t bm_size =3D bdrv_dirty_bitmap_size(bitmap); const char *bm_name =3D bdrv_dirty_bitmap_name(bitmap); uint8_t *buf =3D NULL; - BdrvDirtyBitmapIter *dbi; uint64_t *tb; uint64_t tb_size =3D size_to_clusters(s, @@ -1308,12 +1307,14 @@ static uint64_t *store_bitmap_data(BlockDriverState= *bs, return NULL; } =20 - dbi =3D bdrv_dirty_iter_new(bitmap); buf =3D g_malloc(s->cluster_size); limit =3D bytes_covered_by_bitmap_cluster(s, bitmap); assert(DIV_ROUND_UP(bm_size, limit) =3D=3D tb_size); =20 - while ((offset =3D bdrv_dirty_iter_next(dbi)) >=3D 0) { + offset =3D 0; + while ((offset =3D bdrv_dirty_bitmap_next_dirty(bitmap, offset, INT64_= MAX)) + >=3D 0) + { uint64_t cluster =3D offset / limit; uint64_t end, write_size; int64_t off; @@ -1360,19 +1361,17 @@ static uint64_t *store_bitmap_data(BlockDriverState= *bs, break; } =20 - bdrv_set_dirty_iter(dbi, end); + offset =3D end; } =20 *bitmap_table_size =3D tb_size; g_free(buf); - bdrv_dirty_iter_free(dbi); =20 return tb; =20 fail: clear_bitmap_table(bs, tb, tb_size); g_free(buf); - bdrv_dirty_iter_free(dbi); g_free(tb); =20 return NULL; --=20 2.21.0