From nobody Mon Apr 29 05:14:59 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1557039842; cv=none; d=zoho.com; s=zohoarc; b=kaszoZMZxtJcs1gO6UIFry91A7ay1o5mVv1yMR0V5wlT4Q4GEmOdSaRKRW5oo7FhSuO325jT3z0StnY+NwSCZn5OW4qx5K3D1Jn/xPH8KusUc3jedKNCiXbJCBc7MVb+pZWTIjnrzEDGeKqTp0+rEvifg0BFNog+qhRKrDRou0c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1557039842; h=Content-Type:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=MmdPn2j3MQDZ0ABPx7fxvhHnwGB/rDIevk+Xmc4WUnw=; b=C5XtyclmCZaJiVp+F6y2SwsEAFheCX2QQFApJRycfSPsHzfBWQuZ87FQbQIFyd8hC9pflhcA78O3LzKOYIvCxM2tlivu0BHjx8dDXaJv3pISxM6U1UrraW8w1cwxnqydYWLSfuQP4vwsTVKPw0wQmXazorVgliDmjZrQPGjg7cM= ARC-Authentication-Results: i=1; mx.zoho.com; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1557039842810195.26304086962227; Sun, 5 May 2019 00:04:02 -0700 (PDT) Received: from localhost ([127.0.0.1]:37334 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNBBb-0006SO-AB for importer@patchew.org; Sun, 05 May 2019 03:03:47 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43257) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNBAZ-00066n-07 for qemu-devel@nongnu.org; Sun, 05 May 2019 03:02:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hNBAX-0006rb-Uj for qemu-devel@nongnu.org; Sun, 05 May 2019 03:02:42 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:2207 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hNBAV-0006fH-Jk; Sun, 05 May 2019 03:02:39 -0400 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 2DAE08304B1FABB270A5; Sun, 5 May 2019 15:02:30 +0800 (CST) Received: from HGHY1z004218071.china.huawei.com (10.177.29.32) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.439.0; Sun, 5 May 2019 15:02:24 +0800 From: Xiang Zheng To: , Date: Sun, 5 May 2019 15:00:59 +0800 Message-ID: <20190505070059.4664-1-zhengxiang9@huawei.com> X-Mailer: git-send-email 2.15.1.windows.2 MIME-Version: 1.0 X-Originating-IP: [10.177.29.32] X-CFilter-Loop: Reflected X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 45.249.212.190 Subject: [Qemu-devel] [PATCH] pflash: Only read non-zero parts of backend image X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, peter.maydell@linaro.org, ard.biesheuvel@linaro.org, armbru@redhat.com, mreitz@redhat.com, Xiang Zheng , stefanha@redhat.com, guoheyi@huawei.com, wanghaibin.wang@huawei.com, lersek@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently we fill the memory space with two 64MB NOR images when using persistent UEFI variables on virt board. Actually we only use a very small(non-zero) part of the memory while the rest significant large(zero) part of memory is wasted. So this patch checks the block status and only writes the non-zero part into memory. This requires pflash devices to use sparse files for backends. Suggested-by: Kevin Wolf Signed-off-by: Xiang Zheng --- hw/block/block.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/hw/block/block.c b/hw/block/block.c index bf56c76..3cb9d4c 100644 --- a/hw/block/block.c +++ b/hw/block/block.c @@ -15,6 +15,44 @@ #include "qapi/qapi-types-block.h" =20 /* + * Read the non-zeroes parts of @blk into @buf + * Reading all of the @blk is expensive if the zeroes parts of @blk + * is large enough. Therefore check the block status and only write + * the non-zeroes block into @buf. + * + * Return 0 on success, non-zero on error. + */ +static int blk_pread_nonzeroes(BlockBackend *blk, void *buf) +{ + int ret; + int64_t target_size, bytes, offset =3D 0; + BlockDriverState *bs =3D blk_bs(blk); + + target_size =3D bdrv_getlength(bs); + if (target_size < 0) { + return target_size; + } + + for (;;) { + bytes =3D MIN(target_size - offset, BDRV_REQUEST_MAX_SECTORS); + if (bytes <=3D 0) { + return 0; + } + ret =3D bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL); + if (ret < 0) { + return ret; + } + if (!(ret & BDRV_BLOCK_ZERO)) { + ret =3D bdrv_pread(bs->file, offset, (uint8_t *) buf + offset,= bytes); + if (ret < 0) { + return ret; + } + } + offset +=3D bytes; + } +} + +/* * Read the entire contents of @blk into @buf. * @blk's contents must be @size bytes, and @size must be at most * BDRV_REQUEST_MAX_BYTES. @@ -53,7 +91,7 @@ bool blk_check_size_and_read_all(BlockBackend *blk, void = *buf, hwaddr size, * block device and read only on demand. */ assert(size <=3D BDRV_REQUEST_MAX_BYTES); - ret =3D blk_pread(blk, 0, buf, size); + ret =3D blk_pread_nonzeroes(blk, buf); if (ret < 0) { error_setg_errno(errp, -ret, "can't read block backend"); return false; --=20 1.8.3.1