Skip to contentSkip to conversation

hw/block/block.c: improve confusing error

PATCH Superseded Manos Pitsidianakis 2 participants 5 messages Jan 23, 2024 – Jan 30, 2024

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover hw/block/block.c: improve confusing error

In cases where a device tries to read more bytes than the block device contains with the blk_check_size_and_read_all() function, 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.

Version 2:
- Assert dev is not NULL on qdev_get_human_name (thanks Phil Mathieu-Daudé <[email protected]>)

Manos Pitsidianakis (2):
hw/core/qdev.c: add qdev_get_human_name() hw/block/block.c: improve confusing blk_check_size_and_read_all()
error

 hw/block/block.c         | 25 +++++++++++++++----------
 hw/block/m25p80.c        |  3 ++-
 hw/block/pflash_cfi01.c  |  4 ++--
 hw/block/pflash_cfi02.c  |  2 +-
 hw/core/qdev.c           |  8 ++++++++
 include/hw/block/block.h |  4 ++--
 include/hw/qdev-core.h   | 14 ++++++++++++++
 7 files changed, 44 insertions(+), 16 deletions(-)

Range-diff against v1:
1: 15b15d6d4f ! 1: 5fb5879708 hw/core/qdev.c: add qdev_get_human_name()
@@ hw/core/qdev.c: Object *qdev_get_machine(void)

+char *qdev_get_human_name(DeviceState *dev)
+{
-+ if (!dev) {
-+ return g_strdup("");
-+ }
++ g_assert(dev != NULL);
+
+ return dev->id ?
+ g_strdup(dev->id) : object_get_canonical_path(OBJECT(dev));
@@ include/hw/qdev-core.h: const char *qdev_fw_name(DeviceState *dev);

+/**
+ * qdev_get_human_name() - Return a human-readable name for a device
-+ * @dev: The 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 if not null. If @dev is NULL, it returns an
-+ * allocated empty string.
++ * else the object canonical path.
+ *
+ * Use g_free() to free it.
+ */
2: e3701762ed ! 2: 8e7eb17fbd hw/block/block.c: improve confusing blk_check_size_and_read_all() error
@@ Commit message
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é <[email protected]>

## hw/block/block.c ##

base-commit: 09be34717190c1620f0c6e5c8765b8da354aeb4b

Signature · 1 line
γαῖα πυρί μιχθήτω
#1 lore raw

PATCH v2 1/2 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.

---
 hw/core/qdev.c         |  8 ++++++++
 include/hw/qdev-core.h | 14 ++++++++++++++
 2 files changed, 22 insertions(+)
hw/core/qdev.c +8 -0
index 43d863b0c5..c68d0f7c51 100644
@@ -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)
include/hw/qdev-core.h +14 -0
index 151d968238..66338f479f 100644
@@ -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);
 
Signature · 1 line
γαῖα πυρί μιχθήτω
#3 lore raw

PATCH v2 2/2 hw/block/block.c: improve confusing blk_check_size_and_read_all() error

In reply to Manos Pitsidianakis

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.

---
 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(-)
hw/block/block.c +15 -10
index 9f52ee6e72..624389d62d 100644
@@ -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 with id='%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 with id='%s'",
+                         blk_name(blk), object_get_typename(OBJECT(dev)),
+                         dev_id);
         return false;
     }
     return true;
hw/block/m25p80.c +2 -1
index 26ce895628..0a12030a3a 100644
@@ -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 {
hw/block/pflash_cfi01.c +2 -2
index f956f8bcf7..1bda8424b9 100644
@@ -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;
         }
hw/block/pflash_cfi02.c +1 -1
index 6fa56f14c0..2314142373 100644
@@ -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;
include/hw/block/block.h +2 -2
index 15fff66435..de3946a5f1 100644
@@ -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 */
 
Signature · 1 line
γαῖα πυρί μιχθήτω
#4 lore raw

Re: [PATCH v2 2/2] hw/block/block.c: improve confusing blk_check_size_and_read_all() error

hw/block/block.c static int blk_pread_nonzeroes(BlockBackend *blk, hwaddr size, void *buf) · line 77

Quoted 6 lines · Manos Pitsidianakis
     if (blk_len != size) {
hw/block/block.c +2 -3
-        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 with id='%s' requires %" HWADDR_PRIu

Since qdev_get_human_name() falls back to returning the path instead of the id, this error message could be confusing. Perhaps avoid saying what dev_id is and let the user interpret it:

%s device '%s'

Quoted 7 lines
hw/block/block.c +3 -0
+                   " bytes, %s block backend provides %" PRIu64 " bytes",
+                   object_get_typename(OBJECT(dev)), dev_id, size,
+                   blk_name(blk), blk_len);
         return false;
     }

hw/block/block.c bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size, · line 95

Quoted 7 lines
hw/block/block.c +3 -1
@@ -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 with id='%s'",

Same here.

Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] hw/core/qdev.c: add qdev_get_human_name()

In reply to Manos Pitsidianakis
Quoted 8 lines · Manos Pitsidianakis

Add a simple method to return some kind of human readable identifier for use in error messages.

---
 hw/core/qdev.c         |  8 ++++++++
 include/hw/qdev-core.h | 14 ++++++++++++++
 2 files changed, 22 insertions(+)