Skip to contentSkip to conversation

vfio/pci: Initialize rom_read_failed in vfio_pci_load_rom()

PATCH Superseded Mario Casquero 2 participants 3 messages Jun 1

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio/pci: Initialize rom_read_failed in vfio_pci_load_rom()

When vfio_device_get_region_info() fails in vfio_pci_load_rom(), the function returns without setting vdev->rom_read_failed to true, and without allocating vdev->rom. This leaves vdev->rom as NULL.

---
 hw/vfio/pci.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
hw/vfio/pci.c +6 -4
index 9c06b25e63..d73614d3de 100644
@@ -1027,7 +1027,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
     }
 }
 
-static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
 {
     VFIODevice *vbasedev = &vdev->vbasedev;
     struct vfio_region_info *reg_info = NULL;
@@ -1041,7 +1041,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
 
     if (ret != 0) {
         error_report("vfio: Error getting ROM info: %s", strerror(-ret));
-        return;
+        return false;
     }
 
     trace_vfio_pci_load_rom(vbasedev->name, (unsigned long)reg_info->size,
@@ -1057,7 +1057,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
         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");
-        return;
+        return false;
     }
 
     vdev->rom = g_malloc(size);
@@ -1113,6 +1113,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
             data[6] = -csum;
         }
     }
+
+    return true;
 }
 
 /* "Raw" read of underlying config space. */
@@ -1146,7 +1148,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)) {
-        vfio_pci_load_rom(vdev);
+        vdev->rom_read_failed = !vfio_pci_load_rom(vdev);
     }
 
     memcpy(&val, vdev->rom + addr,

--
2.54.0

Reviewed-by on v1 1/1
#1 lore raw
Quoted 50 lines · Mario Casquero

When vfio_device_get_region_info() fails in vfio_pci_load_rom(), the function returns without setting vdev->rom_read_failed to true, and without allocating vdev->rom. This leaves vdev->rom as NULL.

---
  hw/vfio/pci.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
hw/vfio/pci.c +5 -3
index 9c06b25e63..d73614d3de 100644
@@ -1027,7 +1027,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
      }
  }
 
-static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
  {
      VFIODevice *vbasedev = &vdev->vbasedev;
      struct vfio_region_info *reg_info = NULL;
@@ -1041,7 +1041,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
 
      if (ret != 0) {
          error_report("vfio: Error getting ROM info: %s", strerror(-ret));
-        return;
+        return false;
      }
 
      trace_vfio_pci_load_rom(vbasedev->name, (unsigned long)reg_info->size,
@@ -1057,7 +1057,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
          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");
-        return;
+        return false;
      }
 
      vdev->rom = g_malloc(size);
@@ -1113,6 +1113,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
              data[6] = -csum;
          }
      }
+
+    return true;
  }
 
  /* "Raw" read of underlying config space. */

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

Quoted 8 lines
hw/vfio/pci.c +1 -1
@@ -1146,7 +1148,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)) {
-        vfio_pci_load_rom(vdev);
+        vdev->rom_read_failed = !vfio_pci_load_rom(vdev);
      }
 
      memcpy(&val, vdev->rom + addr,

In a followup patch, I would add a guest error log when the guest attempts to read from a ROM that failed to load.

Quoted 3 lines

--
2.54.0

Anyhow,

Thanks !

C.

#2 lore raw
In reply to Mario Casquero

hw/vfio/pci.c static void vfio_pci_load_rom(VFIOPCIDevice *vdev)

Quoted 32 lines · Mario Casquero

When vfio_device_get_region_info() fails in vfio_pci_load_rom(), the function returns without setting vdev->rom_read_failed to true, and without allocating vdev->rom. This leaves vdev->rom as NULL.

---
  hw/vfio/pci.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
hw/vfio/pci.c +2 -2
index 9c06b25e63..d73614d3de 100644
@@ -1027,7 +1027,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)
      }
  }
 
-static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
+static bool vfio_pci_load_rom(VFIOPCIDevice *vdev)
  {
      VFIODevice *vbasedev = &vdev->vbasedev;
      struct vfio_region_info *reg_info = NULL;
@@ -1041,7 +1041,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
 
      if (ret != 0) {
          error_report("vfio: Error getting ROM info: %s", strerror(-ret));
-        return;
+        return false;
      }
 
      trace_vfio_pci_load_rom(vbasedev->name, (unsigned long)reg_info->size,
@@ -1057,7 +1057,7 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)

One little nit, you could remove the vdev->rom_read_failed assignment here :

        vdev->rom_read_failed = true;

Could you send a v2 please ?

Thanks,

C.

Quoted 29 lines
hw/vfio/pci.c +4 -3
          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");
-        return;
+        return false;
      }
 
      vdev->rom = g_malloc(size);
@@ -1113,6 +1113,8 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
              data[6] = -csum;
          }
      }
+
+    return true;
  }
 
  /* "Raw" read of underlying config space. */
@@ -1146,7 +1148,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)) {
-        vfio_pci_load_rom(vdev);
+        vdev->rom_read_failed = !vfio_pci_load_rom(vdev);
      }
 
      memcpy(&val, vdev->rom + addr,
--

2.54.0