Skip to contentSkip to conversation

vfio/pci: Propagate errors in vfio_pci_load_rom() using Error API

PATCH In review Mario Casquero 3 participants 4 messages Jun 25 – Jun 26

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 vfio/pci: Propagate errors in vfio_pci_load_rom() using Error API

Updates vfio_pci_load_rom() to use Error API for error propagation instead of error_report(), improving error handling consistency.

---
v4:
- Use error_append_hint() instead of error_printf()
- Reset rom_size and rom_offset to 0 in the error case
- Update error message to "Device ROM size is zero", different from read errors
v3:
- Use error_setg_errno() instead of error_setg() with strerror()
- Reset rom_size and rom_offset to 0 when freeing rom on error
- Use local_err with error_report_err() instead of passing NULL errp

 hw/vfio/pci.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)
hw/vfio/pci.c +21 -11
index 9df04fa2d7..61b4a6a5a3 100644
@@ -1027,7 +1027,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
     }
 }
 
-static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev, Error **errp)
 {
     VFIODevice *vbasedev = &vdev->vbasedev;
     struct vfio_region_info *reg_info = NULL;
@@ -1040,7 +1040,7 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
                                       &reg_info);
 
     if (ret != 0) {
-        error_report("vfio: Error getting ROM info: %s", strerror(-ret));
+        error_setg_errno(errp, -ret, "vfio: Error getting ROM info");
         return false;
     }
 
@@ -1052,10 +1052,13 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
     vdev->rom_offset = reg_info->offset;
 
     if (!vdev->rom_size) {
-        error_report("vfio-pci: Cannot read device rom at %s", vbasedev->name);
-        error_printf("Device option ROM contents are probably invalid "
-                    "(check dmesg).\nSkip option ROM probe with rombar=0, "
-                    "or load from file with romfile=\n");
+        vdev->rom_size = 0;
+        vdev->rom_offset = 0;
+        error_setg(errp, "vfio-pci: Device ROM size is zero at %s",
+                   vbasedev->name);
+        error_append_hint(errp, "Device option ROM contents are probably "
+                          "invalid (check dmesg).\nSkip option ROM probe "
+                          "with rombar=0, or load from file with romfile=\n");
         return false;
     }
 
@@ -1076,10 +1079,12 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
             if (bytes == -EINTR || bytes == -EAGAIN) {
                 continue;
             }
-            error_report("vfio: Error reading device ROM: %s",
-                         strreaderror(bytes));
-
-            break;
+            error_setg_errno(errp, -bytes, "vfio: Error reading device ROM");
+            g_free(vdev->rom);
+            vdev->rom = NULL;
+            vdev->rom_size = 0;
+            vdev->rom_offset = 0;
+            return false;
         }
     }
 
@@ -1147,7 +1152,12 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
 
     /* Load the ROM lazily when the guest tries to read it */
     if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
-        vdev->rom_read_failed = !vfio_pci_load_rom(vdev);
+        Error *local_err = NULL;
+
+        vdev->rom_read_failed = !vfio_pci_load_rom(vdev, &local_err);
+        if (vdev->rom_read_failed) {
+            error_report_err(local_err);
+        }
     }
 
     memcpy(&val, vdev->rom + addr,
Signature · 1 line
2.54.0
Reviewed-by on v4 1/1
#1 lore raw
Quoted 40 lines · Mario Casquero

Updates vfio_pci_load_rom() to use Error API for error propagation instead of error_report(), improving error handling consistency.

---
v4:
- Use error_append_hint() instead of error_printf()
- Reset rom_size and rom_offset to 0 in the error case
- Update error message to "Device ROM size is zero", different from read errors
v3:
- Use error_setg_errno() instead of error_setg() with strerror()
- Reset rom_size and rom_offset to 0 when freeing rom on error
- Use local_err with error_report_err() instead of passing NULL errp

  hw/vfio/pci.c | 32 +++++++++++++++++++++-----------
  1 file changed, 21 insertions(+), 11 deletions(-)
hw/vfio/pci.c +2 -2
index 9df04fa2d7..61b4a6a5a3 100644
@@ -1027,7 +1027,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
      }
  }
 
-static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev, Error **errp)
  {
      VFIODevice *vbasedev = &vdev->vbasedev;
      struct vfio_region_info *reg_info = NULL;
@@ -1040,7 +1040,7 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
                                        &reg_info);
 
      if (ret != 0) {
-        error_report("vfio: Error getting ROM info: %s", strerror(-ret));
+        error_setg_errno(errp, -ret, "vfio: Error getting ROM info");
          return false;
      }
 

hw/vfio/pci.c static bool vfio_pci_load_rom(VFIOPCIDevice *vdev) · line 1056

Quoted 9 lines
hw/vfio/pci.c +2 -4
@@ -1052,10 +1052,13 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
      vdev->rom_offset = reg_info->offset;
 
      if (!vdev->rom_size) {
-        error_report("vfio-pci: Cannot read device rom at %s", vbasedev->name);
-        error_printf("Device option ROM contents are probably invalid "
-                    "(check dmesg).\nSkip option ROM probe with rombar=0, "
-                    "or load from file with romfile=\n");
+        vdev->rom_size = 0;
+        vdev->rom_offset = 0;

Alternatively use reg_info->size & reg_info->offset in place, and set vdev->rom_size & vdev->rom_offset once at the end [*] of the function when we know no error is pending. Being at v4 could mean this function is doing too many things and should be split.

Quoted 13 lines
hw/vfio/pci.c +5 -1
+        error_setg(errp, "vfio-pci: Device ROM size is zero at %s",
+                   vbasedev->name);
+        error_append_hint(errp, "Device option ROM contents are probably "
+                          "invalid (check dmesg).\nSkip option ROM probe "
+                          "with rombar=0, or load from file with romfile=\n");
          return false;
      }
 
@@ -1076,10 +1079,12 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
              if (bytes == -EINTR || bytes == -EAGAIN) {
                  continue;
              }
-            error_report("vfio: Error reading device ROM: %s",

hw/vfio/pci.c static bool vfio_pci_load_rom(VFIOPCIDevice *vdev) · line 1088

Quoted 10 lines
hw/vfio/pci.c +6 -3
-                         strreaderror(bytes));
-
-            break;
+            error_setg_errno(errp, -bytes, "vfio: Error reading device ROM");
+            g_free(vdev->rom);
+            vdev->rom = NULL;
+            vdev->rom_size = 0;
+            vdev->rom_offset = 0;
+            return false;
          }

[*]

hw/vfio/pci.c static bool vfio_pci_load_rom(VFIOPCIDevice *vdev) · line 1089

Quoted 1 line
hw/vfio/pci.c +0 -0
      }

Current patch looks good enough, so:

Reviewed-by on v4 1/1
#2 lore raw
In reply to Mario Casquero
Quoted 78 lines · Mario Casquero

Updates vfio_pci_load_rom() to use Error API for error propagation instead of error_report(), improving error handling consistency.

---
v4:
- Use error_append_hint() instead of error_printf()
- Reset rom_size and rom_offset to 0 in the error case
- Update error message to "Device ROM size is zero", different from read errors
v3:
- Use error_setg_errno() instead of error_setg() with strerror()
- Reset rom_size and rom_offset to 0 when freeing rom on error
- Use local_err with error_report_err() instead of passing NULL errp

  hw/vfio/pci.c | 32 +++++++++++++++++++++-----------
  1 file changed, 21 insertions(+), 11 deletions(-)
hw/vfio/pci.c +15 -10
index 9df04fa2d7..61b4a6a5a3 100644
@@ -1027,7 +1027,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
      }
  }
 
-static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev, Error **errp)
  {
      VFIODevice *vbasedev = &vdev->vbasedev;
      struct vfio_region_info *reg_info = NULL;
@@ -1040,7 +1040,7 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
                                        &reg_info);
 
      if (ret != 0) {
-        error_report("vfio: Error getting ROM info: %s", strerror(-ret));
+        error_setg_errno(errp, -ret, "vfio: Error getting ROM info");
          return false;
      }
 
@@ -1052,10 +1052,13 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
      vdev->rom_offset = reg_info->offset;
 
      if (!vdev->rom_size) {
-        error_report("vfio-pci: Cannot read device rom at %s", vbasedev->name);
-        error_printf("Device option ROM contents are probably invalid "
-                    "(check dmesg).\nSkip option ROM probe with rombar=0, "
-                    "or load from file with romfile=\n");
+        vdev->rom_size = 0;
+        vdev->rom_offset = 0;
+        error_setg(errp, "vfio-pci: Device ROM size is zero at %s",
+                   vbasedev->name);
+        error_append_hint(errp, "Device option ROM contents are probably "
+                          "invalid (check dmesg).\nSkip option ROM probe "
+                          "with rombar=0, or load from file with romfile=\n");
          return false;
      }
 
@@ -1076,10 +1079,12 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
              if (bytes == -EINTR || bytes == -EAGAIN) {
                  continue;
              }
-            error_report("vfio: Error reading device ROM: %s",
-                         strreaderror(bytes));
-
-            break;
+            error_setg_errno(errp, -bytes, "vfio: Error reading device ROM");
+            g_free(vdev->rom);
+            vdev->rom = NULL;
+            vdev->rom_size = 0;
+            vdev->rom_offset = 0;
+            return false;
          }
      }
 
@@ -1147,7 +1152,12 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
 
      /* Load the ROM lazily when the guest tries to read it */
      if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 1163

Quoted 10 lines
hw/vfio/pci.c +6 -1
-        vdev->rom_read_failed = !vfio_pci_load_rom(vdev);
+        Error *local_err = NULL;
+
+        vdev->rom_read_failed = !vfio_pci_load_rom(vdev, &local_err);
+        if (vdev->rom_read_failed) {
+            error_report_err(local_err);
+        }
      }
 
      memcpy(&val, vdev->rom + addr,

Thanks,

C.

#3 lore raw
In reply to Mario Casquero
Quoted 78 lines · Mario Casquero

Updates vfio_pci_load_rom() to use Error API for error propagation instead of error_report(), improving error handling consistency.

---
v4:
- Use error_append_hint() instead of error_printf()
- Reset rom_size and rom_offset to 0 in the error case
- Update error message to "Device ROM size is zero", different from read errors
v3:
- Use error_setg_errno() instead of error_setg() with strerror()
- Reset rom_size and rom_offset to 0 when freeing rom on error
- Use local_err with error_report_err() instead of passing NULL errp

  hw/vfio/pci.c | 32 +++++++++++++++++++++-----------
  1 file changed, 21 insertions(+), 11 deletions(-)
hw/vfio/pci.c +15 -10
index 9df04fa2d7..61b4a6a5a3 100644
@@ -1027,7 +1027,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
      }
  }
 
-static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev, Error **errp)
  {
      VFIODevice *vbasedev = &vdev->vbasedev;
      struct vfio_region_info *reg_info = NULL;
@@ -1040,7 +1040,7 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
                                        &reg_info);
 
      if (ret != 0) {
-        error_report("vfio: Error getting ROM info: %s", strerror(-ret));
+        error_setg_errno(errp, -ret, "vfio: Error getting ROM info");
          return false;
      }
 
@@ -1052,10 +1052,13 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
      vdev->rom_offset = reg_info->offset;
 
      if (!vdev->rom_size) {
-        error_report("vfio-pci: Cannot read device rom at %s", vbasedev->name);
-        error_printf("Device option ROM contents are probably invalid "
-                    "(check dmesg).\nSkip option ROM probe with rombar=0, "
-                    "or load from file with romfile=\n");
+        vdev->rom_size = 0;
+        vdev->rom_offset = 0;
+        error_setg(errp, "vfio-pci: Device ROM size is zero at %s",
+                   vbasedev->name);
+        error_append_hint(errp, "Device option ROM contents are probably "
+                          "invalid (check dmesg).\nSkip option ROM probe "
+                          "with rombar=0, or load from file with romfile=\n");
          return false;
      }
 
@@ -1076,10 +1079,12 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
              if (bytes == -EINTR || bytes == -EAGAIN) {
                  continue;
              }
-            error_report("vfio: Error reading device ROM: %s",
-                         strreaderror(bytes));
-
-            break;
+            error_setg_errno(errp, -bytes, "vfio: Error reading device ROM");
+            g_free(vdev->rom);
+            vdev->rom = NULL;
+            vdev->rom_size = 0;
+            vdev->rom_offset = 0;
+            return false;
          }
      }
 
@@ -1147,7 +1152,12 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
 
      /* Load the ROM lazily when the guest tries to read it */
      if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 1163

Quoted 10 lines
hw/vfio/pci.c +6 -1
-        vdev->rom_read_failed = !vfio_pci_load_rom(vdev);
+        Error *local_err = NULL;
+
+        vdev->rom_read_failed = !vfio_pci_load_rom(vdev, &local_err);
+        if (vdev->rom_read_failed) {
+            error_report_err(local_err);
+        }
      }
 
      memcpy(&val, vdev->rom + addr,

Applied to

https://github.com/legoater/qemu vfio-next

Thanks,

C.