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

Mario Casquero posted 1 patch 1 month, 2 weeks ago
Failed in applying to current master (apply log)
There is a newer version of this series
hw/vfio/pci.c | 19 +++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)
[PATCH] vfio/pci: Propagate errors in vfio_pci_load_rom() using Error API
Posted by Mario Casquero 1 month, 2 weeks ago
Updates vfio_pci_load_rom() to use Error API for error propagation
instead of error_report(), improving error handling consistency.

Signed-off-by: Mario Casquero <mcasquer@redhat.com>
---
 hw/vfio/pci.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 9df04fa2d7..11876fd5b4 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -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,10 @@ 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");
+        error_setg(errp, "vfio-pci: Cannot read device rom at %s. "
+                   "Device option ROM contents are probably invalid "
+                   "(check dmesg). Skip option ROM probe with rombar=0, "
+                   "or load from file with romfile=", vbasedev->name);
         return false;
     }
 
@@ -1076,9 +1076,8 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
             if (bytes == -EINTR || bytes == -EAGAIN) {
                 continue;
             }
-            error_report("vfio: Error reading device ROM: %s",
-                         strreaderror(bytes));
-
+            error_setg(errp, "vfio: Error reading device ROM: %s",
+                       strreaderror(bytes));
             break;
         }
     }
@@ -1147,7 +1146,7 @@ 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);
+        vdev->rom_read_failed = !vfio_pci_load_rom(vdev, NULL);
     }
 
     memcpy(&val, vdev->rom + addr,
-- 
2.54.0
Re: [PATCH] vfio/pci: Propagate errors in vfio_pci_load_rom() using Error API
Posted by Cédric Le Goater 1 month, 1 week ago
Hello Mario,

On 6/11/26 11:43, Mario Casquero wrote:
> Updates vfio_pci_load_rom() to use Error API for error propagation
> instead of error_report(), improving error handling consistency.
> 
> Signed-off-by: Mario Casquero <mcasquer@redhat.com>
> ---
>   hw/vfio/pci.c | 19 +++++++++----------
>   1 file changed, 9 insertions(+), 10 deletions(-)
> 
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index 9df04fa2d7..11876fd5b4 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -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,10 @@ 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");
> +        error_setg(errp, "vfio-pci: Cannot read device rom at %s. "
> +                   "Device option ROM contents are probably invalid "
> +                   "(check dmesg). Skip option ROM probe with rombar=0, "
> +                   "or load from file with romfile=", vbasedev->name);

Here I would add :

     error_setg(errp, "vfio-pci: Cannot read device rom at %s", vbasedev->name);

and keep the error_printf() as it is.

The error_setg() propagates the error and error_printf() emits
the "hint" whether the caller handles or not the error.


>           return false;
>       }
>   
> @@ -1076,9 +1076,8 @@ static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
>               if (bytes == -EINTR || bytes == -EAGAIN) {
>                   continue;
>               }
> -            error_report("vfio: Error reading device ROM: %s",
> -                         strreaderror(bytes));
> -
> +            error_setg(errp, "vfio: Error reading device ROM: %s",
> +                       strreaderror(bytes));

May be instead :

     error_setg_errno(errp, -bytes, "vfio: Error reading device ROM");

and since vdev->rom_size and vdev->rom_offset are left dirty :

     g_free(vdev->rom);
     vdev->rom = NULL;
     vdev->rom_size = 0;
     vdev->rom_offset = 0;
     return false;


>               break;
>           }
>       }
> @@ -1147,7 +1146,7 @@ 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);
> +        vdev->rom_read_failed = !vfio_pci_load_rom(vdev, NULL);

The NULL 'errp' parameter will silently discard the error of
vfio_pci_load_rom(). All error_setg() / error_setg_errno() calls
become no-ops.

This would be better :

     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);
     }

Thanks,

C.

>       }
>   
>       memcpy(&val, vdev->rom + addr,