From 75e79f5a08c9d6a573bace48403799fd432b5dbd Mon Sep 17 00:00:00 2001 From: Fiona Ebner Date: Tue, 23 Jan 2024 14:50:44 +0100 Subject: [PATCH 1/5] block/io_uring: improve error message when init fails The man page for io_uring_queue_init states: > io_uring_queue_init(3) returns 0 on success and -errno on failure. and the man page for io_uring_setup (which is one of the functions where the return value of io_uring_queue_init() can come from) states: > On error, a negative error code is returned. The caller should not > rely on errno variable. Tested using 'sysctl kernel.io_uring_disabled=2'. Output before this change: > failed to init linux io_uring ring Output after this change: > failed to init linux io_uring ring: Operation not permitted Signed-off-by: Fiona Ebner Signed-off-by: Stefan Hajnoczi Message-ID: <20240123135044.204985-1-f.ebner@proxmox.com> --- block/io_uring.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/io_uring.c b/block/io_uring.c index d77ae55745..d11b2051ab 100644 --- a/block/io_uring.c +++ b/block/io_uring.c @@ -432,7 +432,7 @@ LuringState *luring_init(Error **errp) rc = io_uring_queue_init(MAX_ENTRIES, ring, 0); if (rc < 0) { - error_setg_errno(errp, errno, "failed to init linux io_uring ring"); + error_setg_errno(errp, -rc, "failed to init linux io_uring ring"); g_free(s); return NULL; } From 615eaeab3d318ba239d54141a4251746782f65c1 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Tue, 30 Jan 2024 12:20:01 +0000 Subject: [PATCH 2/5] block/blkio: Make s->mem_region_alignment be 64 bits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With GCC 14 the code failed to compile on i686 (and was wrong for any version of GCC): ../block/blkio.c: In function ‘blkio_file_open’: ../block/blkio.c:857:28: error: passing argument 3 of ‘blkio_get_uint64’ from incompatible pointer type [-Wincompatible-pointer-types] 857 | &s->mem_region_alignment); | ^~~~~~~~~~~~~~~~~~~~~~~~ | | | size_t * {aka unsigned int *} In file included from ../block/blkio.c:12: /usr/include/blkio.h:49:67: note: expected ‘uint64_t *’ {aka ‘long long unsigned int *’} but argument is of type ‘size_t *’ {aka ‘unsigned int *’} 49 | int blkio_get_uint64(struct blkio *b, const char *name, uint64_t *value); | ~~~~~~~~~~^~~~~ Signed-off-by: Richard W.M. Jones Message-id: 20240130122006.2977938-1-rjones@redhat.com Signed-off-by: Stefan Hajnoczi --- block/blkio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/blkio.c b/block/blkio.c index 0a0a6c0f5f..bc2f21784c 100644 --- a/block/blkio.c +++ b/block/blkio.c @@ -68,7 +68,7 @@ typedef struct { CoQueue bounce_available; /* The value of the "mem-region-alignment" property */ - size_t mem_region_alignment; + uint64_t mem_region_alignment; /* Can we skip adding/deleting blkio_mem_regions? */ bool needs_mem_regions; From d5eaeefbdac30d9ca62612f18e2d3f4509272856 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Mon, 29 Jan 2024 19:27:12 -0500 Subject: [PATCH 3/5] pflash: fix sectors vs bytes confusion in blk_pread_nonzeroes() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The following expression is incorrect because blk_pread_nonzeroes() deals in units of bytes, not sectors: bytes = MIN(size - offset, BDRV_REQUEST_MAX_SECTORS) ^^^^^^^ BDRV_REQUEST_MAX_BYTES is the appropriate constant. Fixes: a4b15a8b9ef2 ("pflash: Only read non-zero parts of backend image") Cc: Xiang Zheng Signed-off-by: Stefan Hajnoczi Reviewed-by: Philippe Mathieu-Daudé Message-id: 20240130002712.257815-1-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi --- hw/block/block.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/block/block.c b/hw/block/block.c index 9f52ee6e72..ff503002aa 100644 --- a/hw/block/block.c +++ b/hw/block/block.c @@ -30,7 +30,7 @@ static int blk_pread_nonzeroes(BlockBackend *blk, hwaddr size, void *buf) BlockDriverState *bs = blk_bs(blk); for (;;) { - bytes = MIN(size - offset, BDRV_REQUEST_MAX_SECTORS); + bytes = MIN(size - offset, BDRV_REQUEST_MAX_BYTES); if (bytes <= 0) { return 0; } From 956ef499902b09aa8a3e70791455ccf45645032d Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Tue, 30 Jan 2024 09:30:31 +0200 Subject: [PATCH 4/5] hw/core/qdev.c: add qdev_get_human_name() Add a simple method to return some kind of human readable identifier for use in error messages. Reviewed-by: Stefan Hajnoczi Signed-off-by: Manos Pitsidianakis Message-id: 8b566bfced98ae44be1fcc1f8e7215f0c3393aa1.1706598705.git.manos.pitsidianakis@linaro.org Signed-off-by: Stefan Hajnoczi --- hw/core/qdev.c | 8 ++++++++ include/hw/qdev-core.h | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 43d863b0c5..c68d0f7c51 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -879,6 +879,14 @@ Object *qdev_get_machine(void) return dev; } +char *qdev_get_human_name(DeviceState *dev) +{ + g_assert(dev != NULL); + + return dev->id ? + g_strdup(dev->id) : object_get_canonical_path(OBJECT(dev)); +} + static MachineInitPhase machine_phase; bool phase_check(MachineInitPhase phase) diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 151d968238..66338f479f 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -993,6 +993,20 @@ const char *qdev_fw_name(DeviceState *dev); void qdev_assert_realized_properly(void); Object *qdev_get_machine(void); +/** + * qdev_get_human_name() - Return a human-readable name for a device + * @dev: The device. Must be a valid and non-NULL pointer. + * + * .. note:: + * This function is intended for user friendly error messages. + * + * Returns: A newly allocated string containing the device id if not null, + * else the object canonical path. + * + * Use g_free() to free it. + */ +char *qdev_get_human_name(DeviceState *dev); + /* FIXME: make this a link<> */ bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp); From 954b33daee83fe79293fd81c2f7371db48e7d6bd Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Tue, 30 Jan 2024 09:30:32 +0200 Subject: [PATCH 5/5] hw/block/block.c: improve confusing blk_check_size_and_read_all() error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In cases where a device tries to read more bytes than the block device contains, the error is vague: "device requires X bytes, block backend provides Y bytes". This patch changes the errors of this function to include the block backend name, the device id and device type name where appropriate. Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Manos Pitsidianakis Message-id: 7260eadff22c08457740117c1bb7bd2b4353acb9.1706598705.git.manos.pitsidianakis@linaro.org Signed-off-by: Stefan Hajnoczi --- hw/block/block.c | 25 +++++++++++++++---------- hw/block/m25p80.c | 3 ++- hw/block/pflash_cfi01.c | 4 ++-- hw/block/pflash_cfi02.c | 2 +- include/hw/block/block.h | 4 ++-- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/hw/block/block.c b/hw/block/block.c index ff503002aa..3ceca7dce6 100644 --- a/hw/block/block.c +++ b/hw/block/block.c @@ -54,29 +54,30 @@ static int blk_pread_nonzeroes(BlockBackend *blk, hwaddr size, void *buf) * BDRV_REQUEST_MAX_BYTES. * On success, return true. * On failure, store an error through @errp and return false. - * Note that the error messages do not identify the block backend. - * TODO Since callers don't either, this can result in confusing - * errors. + * * This function not intended for actual block devices, which read on * demand. It's for things like memory devices that (ab)use a block * backend to provide persistence. */ -bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size, - Error **errp) +bool blk_check_size_and_read_all(BlockBackend *blk, DeviceState *dev, + void *buf, hwaddr size, Error **errp) { int64_t blk_len; int ret; + g_autofree char *dev_id = NULL; blk_len = blk_getlength(blk); if (blk_len < 0) { error_setg_errno(errp, -blk_len, - "can't get size of block backend"); + "can't get size of %s block backend", blk_name(blk)); return false; } if (blk_len != size) { - error_setg(errp, "device requires %" HWADDR_PRIu " bytes, " - "block backend provides %" PRIu64 " bytes", - size, blk_len); + dev_id = qdev_get_human_name(dev); + error_setg(errp, "%s device '%s' requires %" HWADDR_PRIu + " bytes, %s block backend provides %" PRIu64 " bytes", + object_get_typename(OBJECT(dev)), dev_id, size, + blk_name(blk), blk_len); return false; } @@ -89,7 +90,11 @@ bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size, assert(size <= BDRV_REQUEST_MAX_BYTES); ret = blk_pread_nonzeroes(blk, size, buf); if (ret < 0) { - error_setg_errno(errp, -ret, "can't read block backend"); + dev_id = qdev_get_human_name(dev); + error_setg_errno(errp, -ret, "can't read %s block backend" + " for %s device '%s'", + blk_name(blk), object_get_typename(OBJECT(dev)), + dev_id); return false; } return true; diff --git a/hw/block/m25p80.c b/hw/block/m25p80.c index 26ce895628..0a12030a3a 100644 --- a/hw/block/m25p80.c +++ b/hw/block/m25p80.c @@ -1617,7 +1617,8 @@ static void m25p80_realize(SSIPeripheral *ss, Error **errp) trace_m25p80_binding(s); s->storage = blk_blockalign(s->blk, s->size); - if (!blk_check_size_and_read_all(s->blk, s->storage, s->size, errp)) { + if (!blk_check_size_and_read_all(s->blk, DEVICE(s), + s->storage, s->size, errp)) { return; } } else { diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index f956f8bcf7..1bda8424b9 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -848,8 +848,8 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp) } if (pfl->blk) { - if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, total_len, - errp)) { + if (!blk_check_size_and_read_all(pfl->blk, dev, pfl->storage, + total_len, errp)) { vmstate_unregister_ram(&pfl->mem, DEVICE(pfl)); return; } diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c index 6fa56f14c0..2314142373 100644 --- a/hw/block/pflash_cfi02.c +++ b/hw/block/pflash_cfi02.c @@ -902,7 +902,7 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp) } if (pfl->blk) { - if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, + if (!blk_check_size_and_read_all(pfl->blk, dev, pfl->storage, pfl->chip_len, errp)) { vmstate_unregister_ram(&pfl->orig_mem, DEVICE(pfl)); return; diff --git a/include/hw/block/block.h b/include/hw/block/block.h index 15fff66435..de3946a5f1 100644 --- a/include/hw/block/block.h +++ b/include/hw/block/block.h @@ -88,8 +88,8 @@ static inline unsigned int get_physical_block_exp(BlockConf *conf) /* Backend access helpers */ -bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size, - Error **errp); +bool blk_check_size_and_read_all(BlockBackend *blk, DeviceState *dev, + void *buf, hwaddr size, Error **errp); /* Configuration helpers */