Skip to contentSkip to conversation

hw/vfio: Build various objects once

PATCH In review Philippe Mathieu-Daudé 3 participants 11 messages Mar 11, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover hw/vfio: Build various objects once

Hi Cédric,

Here are the VFIO cleanup patches ready enough for 10.0, with Richard and Eric comments from v2 addressed.

I'd prefer the rest (of v2) to wait for 10.1.

Thanks,

Phil.

Philippe Mathieu-Daudé (8):
system: Declare qemu_[min/max]rampagesize() in 'system/hostmem.h' hw/vfio/spapr: Do not include <linux/kvm.h> hw/vfio/common: Include missing 'system/tcg.h' header hw/vfio/common: Get target page size using runtime helpers hw/vfio: Compile some common objects once
hw/vfio: Compile more objects once
hw/vfio: Compile iommufd.c once
hw/vfio: Compile display.c once

 include/exec/ram_addr.h    |  3 ---
 include/system/hostmem.h   |  3 +++
 hw/ppc/spapr_caps.c        |  1 +
 hw/s390x/s390-virtio-ccw.c |  1 +
 hw/vfio/common.c           |  9 ++++++---
 hw/vfio/iommufd.c          |  1 -
 hw/vfio/migration.c        |  1 -
 hw/vfio/spapr.c            |  4 +---
 hw/vfio/meson.build        | 27 ++++++++++++++++-----------
 9 files changed, 28 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH v3 1/8 system: Declare qemu_[min/max]rampagesize() in 'system/hostmem.h'

Both qemu_minrampagesize() and qemu_maxrampagesize() are related to host memory backends, having the following call stack:

qemu_minrampagesize()

     -> find_min_backend_pagesize()
         -> object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)

qemu_maxrampagesize()

     -> find_max_backend_pagesize()
        -> object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)

Having TYPE_MEMORY_BACKEND defined in "system/hostmem.h":

include/system/hostmem.h:23:#define TYPE_MEMORY_BACKEND "memory-backend"

Move their prototype declaration to "system/hostmem.h".

Message-Id: <[email protected]>

---
 include/exec/ram_addr.h    | 3 ---
 include/system/hostmem.h   | 3 +++
 hw/ppc/spapr_caps.c        | 1 +
 hw/s390x/s390-virtio-ccw.c | 1 +
 hw/vfio/spapr.c            | 1 +
 5 files changed, 6 insertions(+), 3 deletions(-)
include/exec/ram_addr.h +0 -3
index 3d8df4edf15..e4c28fbec9b 100644
@@ -102,9 +102,6 @@ static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr,
 
 bool ramblock_is_pmem(RAMBlock *rb);
 
-long qemu_minrampagesize(void);
-long qemu_maxrampagesize(void);
-
 /**
  * qemu_ram_alloc_from_file,
  * qemu_ram_alloc_from_fd:  Allocate a ram block from the specified backing
include/system/hostmem.h +3 -0
index 5c21ca55c01..62642e602ca 100644
@@ -93,4 +93,7 @@ bool host_memory_backend_is_mapped(HostMemoryBackend *backend);
 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev);
 char *host_memory_backend_get_name(HostMemoryBackend *backend);
 
+long qemu_minrampagesize(void);
+long qemu_maxrampagesize(void);
+
 #endif
hw/ppc/spapr_caps.c +1 -0
index 904bff87ce1..9e53d0c1fd1 100644
@@ -34,6 +34,7 @@
 #include "kvm_ppc.h"
 #include "migration/vmstate.h"
 #include "system/tcg.h"
+#include "system/hostmem.h"
 
 #include "hw/ppc/spapr.h"
 
hw/s390x/s390-virtio-ccw.c +1 -0
index a9b3db19f63..75b32182eb0 100644
@@ -41,6 +41,7 @@
 #include "hw/s390x/tod.h"
 #include "system/system.h"
 #include "system/cpus.h"
+#include "system/hostmem.h"
 #include "target/s390x/kvm/pv.h"
 #include "migration/blocker.h"
 #include "qapi/visitor.h"
hw/vfio/spapr.c +1 -0
index ad4c499eafe..237f96dd3fa 100644
@@ -15,6 +15,7 @@
 #include <linux/kvm.h>
 #endif
 #include "system/kvm.h"
+#include "system/hostmem.h"
 #include "exec/address-spaces.h"
 
 #include "hw/vfio/vfio-common.h"
Signature · 1 line
2.47.1
Acked-by on v3 1/8
#2 lore raw

Re: [PATCH-for-10.0 v3 1/8] system: Declare qemu_[min/max]rampagesize() in 'system/hostmem.h'

Quoted 16 lines · Philippe Mathieu-Daudé

Both qemu_minrampagesize() and qemu_maxrampagesize() are related to host memory backends, having the following call stack:

   qemu_minrampagesize()
      -> find_min_backend_pagesize()
          -> object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)

   qemu_maxrampagesize()
      -> find_max_backend_pagesize()
         -> object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)

Having TYPE_MEMORY_BACKEND defined in "system/hostmem.h":

include/system/hostmem.h:23:#define TYPE_MEMORY_BACKEND "memory-backend"

Quoted 7 lines

Move their prototype declaration to "system/hostmem.h".

Message-Id: <[email protected]>
---

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw

PATCH v3 2/8 hw/vfio/spapr: Do not include <linux/kvm.h>

In reply to Philippe Mathieu-Daudé

<linux/kvm.h> is already included by "system/kvm.h" in the next line.

Message-Id: <[email protected]>

---
 hw/vfio/spapr.c | 3 ---
 1 file changed, 3 deletions(-)
hw/vfio/spapr.c +0 -3
index 237f96dd3fa..1a5d1611f2c 100644
@@ -11,9 +11,6 @@
 #include "qemu/osdep.h"
 #include <sys/ioctl.h>
 #include <linux/vfio.h>
-#ifdef CONFIG_KVM
-#include <linux/kvm.h>
-#endif
 #include "system/kvm.h"
 #include "system/hostmem.h"
 #include "exec/address-spaces.h"
Signature · 1 line
2.47.1
#4 lore raw

PATCH v3 3/8 hw/vfio/common: Include missing 'system/tcg.h' header

In reply to Philippe Mathieu-Daudé

Always include necessary headers explicitly, to avoid when refactoring unrelated ones:

hw/vfio/common.c:1176:45: error: implicit declaration of function ‘tcg_enabled’;

   1176 |                                             tcg_enabled() ? DIRTY_CLIENTS_ALL :
        |                                             ^~~~~~~~~~~

Message-Id: <[email protected]>

---
 hw/vfio/common.c | 1 +
 1 file changed, 1 insertion(+)
hw/vfio/common.c +1 -0
index 7a4010ef4ee..b1596b6bf64 100644
@@ -42,6 +42,7 @@
 #include "migration/misc.h"
 #include "migration/blocker.h"
 #include "migration/qemu-file.h"
+#include "system/tcg.h"
 #include "system/tpm.h"
 
 VFIODeviceList vfio_device_list =
Signature · 1 line
2.47.1
#5 lore raw

PATCH v3 4/8 hw/vfio/common: Get target page size using runtime helpers

In reply to Philippe Mathieu-Daudé

Prefer runtime helpers to get target page size.

Message-Id: <[email protected]>

---
 hw/vfio/common.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/vfio/common.c +5 -3
index b1596b6bf64..1a0d9290f88 100644
@@ -30,6 +30,7 @@
 #include "exec/address-spaces.h"
 #include "exec/memory.h"
 #include "exec/ram_addr.h"
+#include "exec/target_page.h"
 #include "hw/hw.h"
 #include "qemu/error-report.h"
 #include "qemu/main-loop.h"
@@ -393,13 +394,14 @@ static void vfio_register_ram_discard_listener(VFIOContainerBase *bcontainer,
                                                MemoryRegionSection *section)
 {
     RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr);
+    int target_page_size = qemu_target_page_size();
     VFIORamDiscardListener *vrdl;
 
     /* Ignore some corner cases not relevant in practice. */
-    g_assert(QEMU_IS_ALIGNED(section->offset_within_region, TARGET_PAGE_SIZE));
+    g_assert(QEMU_IS_ALIGNED(section->offset_within_region, target_page_size));
     g_assert(QEMU_IS_ALIGNED(section->offset_within_address_space,
-                             TARGET_PAGE_SIZE));
-    g_assert(QEMU_IS_ALIGNED(int128_get64(section->size), TARGET_PAGE_SIZE));
+                             target_page_size));
+    g_assert(QEMU_IS_ALIGNED(int128_get64(section->size), target_page_size));
 
     vrdl = g_new0(VFIORamDiscardListener, 1);
     vrdl->bcontainer = bcontainer;
Signature · 1 line
2.47.1
#6 lore raw

PATCH v3 5/8 hw/vfio: Compile some common objects once

In reply to Philippe Mathieu-Daudé

Some files don't rely on any target-specific knowledge and can be compiled once:

- helpers.c
- container-base.c
- migration.c (removing unnecessary "exec/ram_addr.h")
- migration-multifd.c
- cpr.c

Message-Id: <[email protected]>

---
 hw/vfio/migration.c |  1 -
 hw/vfio/meson.build | 13 ++++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)
hw/vfio/migration.c +0 -1
index 416643ddd69..fbff46cfc35 100644
@@ -27,7 +27,6 @@
 #include "qapi/error.h"
 #include "qapi/qapi-events-vfio.h"
 #include "exec/ramlist.h"
-#include "exec/ram_addr.h"
 #include "pci.h"
 #include "trace.h"
 #include "hw/hw.h"
hw/vfio/meson.build +8 -5
index 260d65febd6..8e376cfcbf8 100644
@@ -1,12 +1,7 @@
 vfio_ss = ss.source_set()
 vfio_ss.add(files(
-  'helpers.c',
   'common.c',
-  'container-base.c',
   'container.c',
-  'migration.c',
-  'migration-multifd.c',
-  'cpr.c',
 ))
 vfio_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr.c'))
 vfio_ss.add(when: 'CONFIG_IOMMUFD', if_true: files(
@@ -25,3 +20,11 @@ vfio_ss.add(when: 'CONFIG_VFIO_AP', if_true: files('ap.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_IGD', if_true: files('igd.c'))
 
 specific_ss.add_all(when: 'CONFIG_VFIO', if_true: vfio_ss)
+
+system_ss.add(when: 'CONFIG_VFIO', if_true: files(
+  'helpers.c',
+  'container-base.c',
+  'migration.c',
+  'migration-multifd.c',
+  'cpr.c',
+))
Signature · 1 line
2.47.1
#7 lore raw

PATCH v3 6/8 hw/vfio: Compile more objects once

In reply to Philippe Mathieu-Daudé

These files depend on the VFIO symbol in their Kconfig definition. They don't rely on target specific definitions, move them to system_ss[] to build them once.

Message-Id: <[email protected]>

---
 hw/vfio/meson.build | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/vfio/meson.build +2 -2
index 8e376cfcbf8..784eae4b559 100644
@@ -14,13 +14,13 @@ vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
 ))
 vfio_ss.add(when: 'CONFIG_VFIO_CCW', if_true: files('ccw.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_PLATFORM', if_true: files('platform.c'))
-vfio_ss.add(when: 'CONFIG_VFIO_XGMAC', if_true: files('calxeda-xgmac.c'))
-vfio_ss.add(when: 'CONFIG_VFIO_AMD_XGBE', if_true: files('amd-xgbe.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_AP', if_true: files('ap.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_IGD', if_true: files('igd.c'))
 
 specific_ss.add_all(when: 'CONFIG_VFIO', if_true: vfio_ss)
 
+system_ss.add(when: 'CONFIG_VFIO_XGMAC', if_true: files('calxeda-xgmac.c'))
+system_ss.add(when: 'CONFIG_VFIO_AMD_XGBE', if_true: files('amd-xgbe.c'))
 system_ss.add(when: 'CONFIG_VFIO', if_true: files(
   'helpers.c',
   'container-base.c',
Signature · 1 line
2.47.1
#8 lore raw

PATCH v3 7/8 hw/vfio: Compile iommufd.c once

In reply to Philippe Mathieu-Daudé

Removing unused "exec/ram_addr.h" header allow to compile iommufd.c once for all targets.

Message-Id: <[email protected]>

---
 hw/vfio/iommufd.c   | 1 -
 hw/vfio/meson.build | 6 +++---
 2 files changed, 3 insertions(+), 4 deletions(-)
hw/vfio/iommufd.c +0 -1
index df61edffc08..42c8412bbf5 100644
@@ -25,7 +25,6 @@
 #include "qemu/cutils.h"
 #include "qemu/chardev_open.h"
 #include "pci.h"
-#include "exec/ram_addr.h"
 
 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
                             ram_addr_t size, void *vaddr, bool readonly)
hw/vfio/meson.build +3 -3
index 784eae4b559..5c9ec7e8971 100644
@@ -4,9 +4,6 @@ vfio_ss.add(files(
   'container.c',
 ))
 vfio_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr.c'))
-vfio_ss.add(when: 'CONFIG_IOMMUFD', if_true: files(
-  'iommufd.c',
-))
 vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
   'display.c',
   'pci-quirks.c',
@@ -28,3 +25,6 @@ system_ss.add(when: 'CONFIG_VFIO', if_true: files(
   'migration-multifd.c',
   'cpr.c',
 ))
+system_ss.add(when: ['CONFIG_VFIO', 'CONFIG_IOMMUFD'], if_true: files(
+  'iommufd.c',
+))
Signature · 1 line
2.47.1
#9 lore raw

PATCH v3 8/8 hw/vfio: Compile display.c once

In reply to Philippe Mathieu-Daudé

display.c doesn't rely on target specific definitions, move it to system_ss[] to build it once.

Message-Id: <[email protected]>

---
 hw/vfio/meson.build | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/vfio/meson.build +3 -1
index 5c9ec7e8971..a8939c83865 100644
@@ -5,7 +5,6 @@ vfio_ss.add(files(
 ))
 vfio_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
-  'display.c',
   'pci-quirks.c',
   'pci.c',
 ))
@@ -28,3 +27,6 @@ system_ss.add(when: 'CONFIG_VFIO', if_true: files(
 system_ss.add(when: ['CONFIG_VFIO', 'CONFIG_IOMMUFD'], if_true: files(
   'iommufd.c',
 ))
+system_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
+  'display.c',
+))
Signature · 1 line
2.47.1
#10 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 4 lines · Philippe Mathieu-Daudé

Hi Cédric,

Here are the VFIO cleanup patches ready enough for 10.0, with Richard and Eric comments from v2 addressed.

I dropped the previously applied patches from the v2 series and applied this v3 in vfio-next instead.

Quoted 1 line

I'd prefer the rest (of v2) to wait for 10.1.

yes.

Thanks,

C.

Quoted 25 lines

Thanks,

Phil.

Philippe Mathieu-Daudé (8):
system: Declare qemu_[min/max]rampagesize() in 'system/hostmem.h' hw/vfio/spapr: Do not include <linux/kvm.h> hw/vfio/common: Include missing 'system/tcg.h' header hw/vfio/common: Get target page size using runtime helpers hw/vfio: Compile some common objects once
hw/vfio: Compile more objects once
hw/vfio: Compile iommufd.c once
hw/vfio: Compile display.c once

  include/exec/ram_addr.h    |  3 ---
  include/system/hostmem.h   |  3 +++
  hw/ppc/spapr_caps.c        |  1 +
  hw/s390x/s390-virtio-ccw.c |  1 +
  hw/vfio/common.c           |  9 ++++++---
  hw/vfio/iommufd.c          |  1 -
  hw/vfio/migration.c        |  1 -
  hw/vfio/spapr.c            |  4 +---
  hw/vfio/meson.build        | 27 ++++++++++++++++-----------
  9 files changed, 28 insertions(+), 22 deletions(-)