Skip to contentSkip to conversation

vfio/container: pass MemoryRegion to DMA operations

PATCH In review John Levon 4 participants 7 messages May 21, 2025 – May 31

lore
Share & tools
lore

John Levon

author
#0 lore raw

PATCH 1/1 vfio/container: pass MemoryRegion to DMA operations

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message; CPR will also will need this.

---
 include/hw/vfio/vfio-container-base.h | 9 +++++----
 hw/vfio/container-base.c              | 4 ++--
 hw/vfio/container.c                   | 3 ++-
 hw/vfio/iommufd.c                     | 3 ++-
 hw/vfio/listener.c                    | 6 +++---
 5 files changed, 14 insertions(+), 11 deletions(-)
include/hw/vfio/vfio-container-base.h +5 -4
index f9e561cb08..3feb773e5f 100644
@@ -78,7 +78,7 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
 
 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                            hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly);
+                           void *vaddr, bool readonly, MemoryRegion *mr);
 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
                              hwaddr iova, ram_addr_t size,
                              IOMMUTLBEntry *iotlb, bool unmap_all);
@@ -151,20 +151,21 @@ struct VFIOIOMMUClass {
     /**
      * @dma_map
      *
-     * Map an address range into the container.
+     * Map an address range into the container. Note that the memory region is
+     * referenced within an RCU read lock region across this call.
      *
      * @bcontainer: #VFIOContainerBase to use
      * @iova: start address to map
      * @size: size of the range to map
      * @vaddr: process virtual address of mapping
      * @readonly: true if mapping should be readonly
+     * @mr: the memory region for this mapping
      *
      * Returns 0 to indicate success and -errno otherwise.
      */
     int (*dma_map)(const VFIOContainerBase *bcontainer,
                    hwaddr iova, ram_addr_t size,
-                   void *vaddr, bool readonly);
-
+                   void *vaddr, bool readonly, MemoryRegion *mr);
     /**
      * @dma_unmap
      *
hw/vfio/container-base.c +2 -2
index 1c6ca94b60..d834bd4822 100644
@@ -75,12 +75,12 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
 
 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                            hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly)
+                           void *vaddr, bool readonly, MemoryRegion *mr)
 {
     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
 
     g_assert(vioc->dma_map);
-    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly);
+    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly, mr);
 }
 
 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
hw/vfio/container.c +2 -1
index a9f0dbaec4..a8c76eb481 100644
@@ -207,7 +207,8 @@ static int vfio_legacy_dma_unmap(const VFIOContainerBase *bcontainer,
 }
 
 static int vfio_legacy_dma_map(const VFIOContainerBase *bcontainer, hwaddr iova,
-                               ram_addr_t size, void *vaddr, bool readonly)
+                               ram_addr_t size, void *vaddr, bool readonly,
+                               MemoryRegion *mr)
 {
     const VFIOContainer *container = container_of(bcontainer, VFIOContainer,
                                                   bcontainer);
hw/vfio/iommufd.c +2 -1
index 6b2696793f..46a3b36301 100644
@@ -34,7 +34,8 @@
             TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
 
 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
-                            ram_addr_t size, void *vaddr, bool readonly)
+                            ram_addr_t size, void *vaddr, bool readonly,
+                            MemoryRegion *mr)
 {
     const VFIOIOMMUFDContainer *container =
         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
hw/vfio/listener.c +3 -3
index 38e3dc82cf..7495866123 100644
@@ -170,7 +170,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
          */
         ret = vfio_container_dma_map(bcontainer, iova,
                                      iotlb->addr_mask + 1, vaddr,
-                                     read_only);
+                                     read_only, mr);
         if (ret) {
             error_report("vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
                          "0x%"HWADDR_PRIx", %p) = %d (%s)",
@@ -240,7 +240,7 @@ static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl,
         vaddr = memory_region_get_ram_ptr(section->mr) + start;
 
         ret = vfio_container_dma_map(bcontainer, iova, next - start,
-                                     vaddr, section->readonly);
+                                     vaddr, section->readonly, section->mr);
         if (ret) {
             /* Rollback */
             vfio_ram_discard_notify_discard(rdl, section);
@@ -564,7 +564,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
     }
 
     ret = vfio_container_dma_map(bcontainer, iova, int128_get64(llsize),
-                                 vaddr, section->readonly);
+                                 vaddr, section->readonly, section->mr);
     if (ret) {
         error_setg(&err, "vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
                    "0x%"HWADDR_PRIx", %p) = %d (%s)",
Signature · 1 line
2.43.0
author
#1 lore raw
Quoted 4 lines · John Levon

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message; CPR will also will need this.

Sent this out separately against vfio-next as we both depend on it, thanks.

regards
john

Reviewed-by on v1 1/1
#2 lore raw
In reply to John Levon
Quoted 9 lines · John Levon

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message; CPR will also will need this.

Quoted 7 lines
---
  include/hw/vfio/vfio-container-base.h | 9 +++++----
  hw/vfio/container-base.c              | 4 ++--
  hw/vfio/container.c                   | 3 ++-
  hw/vfio/iommufd.c                     | 3 ++-
  hw/vfio/listener.c                    | 6 +++---
  5 files changed, 14 insertions(+), 11 deletions(-)

Steven,

Can you base the live update series on this patch ?

Thanks,

C.

Quoted 117 lines
include/hw/vfio/vfio-container-base.h +5 -4
index f9e561cb08..3feb773e5f 100644
@@ -78,7 +78,7 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
 
  int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                             hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly);
+                           void *vaddr, bool readonly, MemoryRegion *mr);
  int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
                               hwaddr iova, ram_addr_t size,
                               IOMMUTLBEntry *iotlb, bool unmap_all);
@@ -151,20 +151,21 @@ struct VFIOIOMMUClass {
      /**
       * @dma_map
       *
-     * Map an address range into the container.
+     * Map an address range into the container. Note that the memory region is
+     * referenced within an RCU read lock region across this call.
       *
       * @bcontainer: #VFIOContainerBase to use
       * @iova: start address to map
       * @size: size of the range to map
       * @vaddr: process virtual address of mapping
       * @readonly: true if mapping should be readonly
+     * @mr: the memory region for this mapping
       *
       * Returns 0 to indicate success and -errno otherwise.
       */
      int (*dma_map)(const VFIOContainerBase *bcontainer,
                     hwaddr iova, ram_addr_t size,
-                   void *vaddr, bool readonly);
-
+                   void *vaddr, bool readonly, MemoryRegion *mr);
      /**
       * @dma_unmap
       *
hw/vfio/container-base.c +2 -2
index 1c6ca94b60..d834bd4822 100644
@@ -75,12 +75,12 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
 
  int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                             hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly)
+                           void *vaddr, bool readonly, MemoryRegion *mr)
  {
      VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
 
      g_assert(vioc->dma_map);
-    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly);
+    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly, mr);
  }
 
  int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
hw/vfio/container.c +2 -1
index a9f0dbaec4..a8c76eb481 100644
@@ -207,7 +207,8 @@ static int vfio_legacy_dma_unmap(const VFIOContainerBase *bcontainer,
  }
 
  static int vfio_legacy_dma_map(const VFIOContainerBase *bcontainer, hwaddr iova,
-                               ram_addr_t size, void *vaddr, bool readonly)
+                               ram_addr_t size, void *vaddr, bool readonly,
+                               MemoryRegion *mr)
  {
      const VFIOContainer *container = container_of(bcontainer, VFIOContainer,
                                                    bcontainer);
hw/vfio/iommufd.c +2 -1
index 6b2696793f..46a3b36301 100644
@@ -34,7 +34,8 @@
              TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
 
  static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
-                            ram_addr_t size, void *vaddr, bool readonly)
+                            ram_addr_t size, void *vaddr, bool readonly,
+                            MemoryRegion *mr)
  {
      const VFIOIOMMUFDContainer *container =
          container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
hw/vfio/listener.c +3 -3
index 38e3dc82cf..7495866123 100644
@@ -170,7 +170,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
           */
          ret = vfio_container_dma_map(bcontainer, iova,
                                       iotlb->addr_mask + 1, vaddr,
-                                     read_only);
+                                     read_only, mr);
          if (ret) {
              error_report("vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
                           "0x%"HWADDR_PRIx", %p) = %d (%s)",
@@ -240,7 +240,7 @@ static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl,
          vaddr = memory_region_get_ram_ptr(section->mr) + start;
 
          ret = vfio_container_dma_map(bcontainer, iova, next - start,
-                                     vaddr, section->readonly);
+                                     vaddr, section->readonly, section->mr);
          if (ret) {
              /* Rollback */
              vfio_ram_discard_notify_discard(rdl, section);
@@ -564,7 +564,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
      }
 
      ret = vfio_container_dma_map(bcontainer, iova, int128_get64(llsize),
-                                 vaddr, section->readonly);
+                                 vaddr, section->readonly, section->mr);
      if (ret) {
          error_setg(&err, "vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
                     "0x%"HWADDR_PRIx", %p) = %d (%s)",
#3 lore raw
Quoted 18 lines · Cédric Le Goater
Quoted 9 lines · John Levon

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message; CPR will also will need this.

Quoted 7 lines

---
  include/hw/vfio/vfio-container-base.h | 9 +++++---- hw/vfio/container-base.c              | 4 ++-- hw/vfio/container.c                   | 3 ++- hw/vfio/iommufd.c                     | 3 ++- hw/vfio/listener.c                    | 6 +++---

  5 files changed, 14 insertions(+), 11 deletions(-)
Quoted 3 lines

Steven,

Can you base the live update series on this patch ?

Yes, its already on my todo list for V4.

- Steve

Quoted 109 lines
Quoted 108 lines
include/hw/vfio/vfio-container-base.h +5 -4
index f9e561cb08..3feb773e5f 100644
@@ -78,7 +78,7 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
   int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                              hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly);
+                           void *vaddr, bool readonly, MemoryRegion *mr);
   int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
                                hwaddr iova, ram_addr_t size,
                                IOMMUTLBEntry *iotlb, bool unmap_all);
@@ -151,20 +151,21 @@ struct VFIOIOMMUClass {
       /**
        * @dma_map
        *
-     * Map an address range into the container.
+     * Map an address range into the container. Note that the memory region is
+     * referenced within an RCU read lock region across this call.
        *
        * @bcontainer: #VFIOContainerBase to use
        * @iova: start address to map
        * @size: size of the range to map
        * @vaddr: process virtual address of mapping
        * @readonly: true if mapping should be readonly
+     * @mr: the memory region for this mapping
        *
        * Returns 0 to indicate success and -errno otherwise.
        */
       int (*dma_map)(const VFIOContainerBase *bcontainer,
                      hwaddr iova, ram_addr_t size,
-                   void *vaddr, bool readonly);
-
+                   void *vaddr, bool readonly, MemoryRegion *mr);
       /**
        * @dma_unmap
        *
hw/vfio/container-base.c +2 -2
index 1c6ca94b60..d834bd4822 100644
@@ -75,12 +75,12 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
   int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                              hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly)
+                           void *vaddr, bool readonly, MemoryRegion *mr)
   {
       VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
       g_assert(vioc->dma_map);
-    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly);
+    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly, mr);
   }
   int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
hw/vfio/container.c +2 -1
index a9f0dbaec4..a8c76eb481 100644
@@ -207,7 +207,8 @@ static int vfio_legacy_dma_unmap(const VFIOContainerBase *bcontainer,
   }
   static int vfio_legacy_dma_map(const VFIOContainerBase *bcontainer, hwaddr iova,
-                               ram_addr_t size, void *vaddr, bool readonly)
+                               ram_addr_t size, void *vaddr, bool readonly,
+                               MemoryRegion *mr)
   {
       const VFIOContainer *container = container_of(bcontainer, VFIOContainer,
                                                     bcontainer);
hw/vfio/iommufd.c +0 -0
index 6b2696793f..46a3b36301 100644
@@ -34,7 +34,8 @@

              TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
  static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova, -                            ram_addr_t size, void *vaddr, bool readonly) +                            ram_addr_t size, void *vaddr, bool readonly, +                            MemoryRegion *mr)
  {
      const VFIOIOMMUFDContainer *container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);

hw/vfio/listener.c +1 -1
index 38e3dc82cf..7495866123 100644
@@ -170,7 +170,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
            */
           ret = vfio_container_dma_map(bcontainer, iova,
                                        iotlb->addr_mask + 1, vaddr,
-                                     read_only);
+                                     read_only, mr);
           if (ret) {

              error_report("vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx", %p) = %d (%s)",

hw/vfio/listener.c +2 -2
@@ -240,7 +240,7 @@ static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl,
           vaddr = memory_region_get_ram_ptr(section->mr) + start;
           ret = vfio_container_dma_map(bcontainer, iova, next - start,
-                                     vaddr, section->readonly);
+                                     vaddr, section->readonly, section->mr);
           if (ret) {
               /* Rollback */
               vfio_ram_discard_notify_discard(rdl, section);
@@ -564,7 +564,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
       }
       ret = vfio_container_dma_map(bcontainer, iova, int128_get64(llsize),
-                                 vaddr, section->readonly);
+                                 vaddr, section->readonly, section->mr);
       if (ret) {

          error_setg(&err, "vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx", %p) = %d (%s)",

#4 lore raw
Quoted 19 lines · Steven Sistare
Quoted 18 lines · Cédric Le Goater
Quoted 16 lines · John Levon

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message; CPR will also will need this.

---
  include/hw/vfio/vfio-container-base.h | 9 +++++---- hw/vfio/container-base.c              | 4 ++-- hw/vfio/container.c                   | 3 ++- hw/vfio/iommufd.c                     | 3 ++- hw/vfio/listener.c                    | 6 +++---

  5 files changed, 14 insertions(+), 11 deletions(-)
Quoted 5 lines
Quoted 3 lines

Steven,

Can you base the live update series on this patch ?

Yes, its already on my todo list for V4.

Could you add your R-b ?

Thanks,

C.

Reviewed-by
#5 lore raw
Quoted 20 lines · Cédric Le Goater
Quoted 19 lines · Steven Sistare
Quoted 18 lines · Cédric Le Goater
Quoted 16 lines · John Levon

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message; CPR will also will need this.

---
  include/hw/vfio/vfio-container-base.h | 9 +++++---- hw/vfio/container-base.c              | 4 ++-- hw/vfio/container.c                   | 3 ++- hw/vfio/iommufd.c                     | 3 ++- hw/vfio/listener.c                    | 6 +++---

  5 files changed, 14 insertions(+), 11 deletions(-)
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Steven,

Can you base the live update series on this patch ?

Yes, its already on my todo list for V4.

Could you add your R-b ?

- Steve

Reviewed-by on v1 1/1
#6 lore raw
In reply to John Levon
Quoted 4 lines · John Levon

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message; CPR will also will need this.

"will ... will" typo.

Quoted 38 lines
---
  include/hw/vfio/vfio-container-base.h | 9 +++++----
  hw/vfio/container-base.c              | 4 ++--
  hw/vfio/container.c                   | 3 ++-
  hw/vfio/iommufd.c                     | 3 ++-
  hw/vfio/listener.c                    | 6 +++---
  5 files changed, 14 insertions(+), 11 deletions(-)
include/hw/vfio/vfio-container-base.h +3 -2
index f9e561cb08..3feb773e5f 100644
@@ -78,7 +78,7 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
 
  int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                             hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly);
+                           void *vaddr, bool readonly, MemoryRegion *mr);
  int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
                               hwaddr iova, ram_addr_t size,
                               IOMMUTLBEntry *iotlb, bool unmap_all);
@@ -151,20 +151,21 @@ struct VFIOIOMMUClass {
      /**
       * @dma_map
       *
-     * Map an address range into the container.
+     * Map an address range into the container. Note that the memory region is
+     * referenced within an RCU read lock region across this call.
       *
       * @bcontainer: #VFIOContainerBase to use
       * @iova: start address to map
       * @size: size of the range to map
       * @vaddr: process virtual address of mapping

include/hw/vfio/vfio-container-base.h struct VFIOIOMMUClass { · line 168

Quoted 10 lines
include/hw/vfio/vfio-container-base.h +2 -2
       * @readonly: true if mapping should be readonly
+     * @mr: the memory region for this mapping
       *
       * Returns 0 to indicate success and -errno otherwise.
       */
      int (*dma_map)(const VFIOContainerBase *bcontainer,
                     hwaddr iova, ram_addr_t size,
-                   void *vaddr, bool readonly);
-
+                   void *vaddr, bool readonly, MemoryRegion *mr);

Since the memory region shouldn't be altered, preferably "const MemoryRegion *mr" (here and in vfio_container_dma_map).