From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-block@nongnu.org, "Kevin Wolf" <kwolf@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
qemu-arm@nongnu.org, "Hanna Reitz" <hreitz@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"John Snow" <jsnow@redhat.com>,
"Kashyap Chamarthy" <kchamart@redhat.com>,
"Xiang Zheng" <zhengxiang9@huawei.com>,
"Gerd Hoffmann" <kraxel@redhat.com>
Subject: [PATCH v2] pflash: Only read non-zero parts of backend image
Date: Tue, 20 Dec 2022 09:42:46 +0100 [thread overview]
Message-ID: <20221220084246.1984871-1-kraxel@redhat.com> (raw)
From: Xiang Zheng <zhengxiang9@huawei.com>
Currently we fill the VIRT_FLASH 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.
Signed-off-by: Xiang Zheng <zhengxiang9@huawei.com>
[ kraxel: rebased to latest master ]
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
hw/block/block.c | 36 +++++++++++++++++++++++++++++++++++-
1 file changed, 35 insertions(+), 1 deletion(-)
diff --git a/hw/block/block.c b/hw/block/block.c
index f9c4fe67673b..142ebe4267e4 100644
--- a/hw/block/block.c
+++ b/hw/block/block.c
@@ -14,6 +14,40 @@
#include "qapi/error.h"
#include "qapi/qapi-types-block.h"
+/*
+ * 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, hwaddr size, void *buf)
+{
+ int ret;
+ int64_t bytes, offset = 0;
+ BlockDriverState *bs = blk_bs(blk);
+
+ for (;;) {
+ bytes = MIN(size - offset, BDRV_REQUEST_MAX_SECTORS);
+ if (bytes <= 0) {
+ return 0;
+ }
+ ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL);
+ if (ret < 0) {
+ return ret;
+ }
+ if (!(ret & BDRV_BLOCK_ZERO)) {
+ ret = bdrv_pread(bs->file, offset, bytes,
+ (uint8_t *) buf + offset, 0);
+ if (ret < 0) {
+ return ret;
+ }
+ }
+ offset += bytes;
+ }
+}
+
/*
* Read the entire contents of @blk into @buf.
* @blk's contents must be @size bytes, and @size must be at most
@@ -53,7 +87,7 @@ bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size,
* block device and read only on demand.
*/
assert(size <= BDRV_REQUEST_MAX_BYTES);
- ret = blk_pread(blk, 0, size, buf, 0);
+ ret = blk_pread_nonzeroes(blk, size, buf);
if (ret < 0) {
error_setg_errno(errp, -ret, "can't read block backend");
return false;
--
2.38.1
next reply other threads:[~2022-12-20 8:43 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-20 8:42 Gerd Hoffmann [this message]
2022-12-20 9:08 ` [PATCH v2] pflash: Only read non-zero parts of backend image Daniel P. Berrangé
2022-12-20 9:30 ` Philippe Mathieu-Daudé
2022-12-20 15:33 ` Gerd Hoffmann
2022-12-23 14:33 ` Ard Biesheuvel
2023-01-02 11:11 ` Gerd Hoffmann
2023-01-10 14:08 ` Kevin Wolf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221220084246.1984871-1-kraxel@redhat.com \
--to=kraxel@redhat.com \
--cc=berrange@redhat.com \
--cc=hreitz@redhat.com \
--cc=jsnow@redhat.com \
--cc=kchamart@redhat.com \
--cc=kwolf@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=zhengxiang9@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).