Skip to contentSkip to conversation

vfio-user: support multiple FDs backing region mmaps

PATCH Superseded Naman Gulati 4 participants 16 messages Oct 14 – Oct 23

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover vfio-user: support multiple FDs backing region mmaps

This series extends the vfio-user protocol specification to allow servers to pass multiple file descriptors for different sparse memory areas to be mmap'ed within a single device region.

This extension is intended for exposing paravirtualized PCI devices via a vfio-user server that may have multiple unique sub-regions in a BAR backed by some mmap'able buffers sourced from distinct file descriptors.

This change introduces a new capability:
VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS

Unlike VFIO_REGION_INFO_CAP_SPARSE_MMAP (which maps all sparse areas from a single open file descriptor), this new capability defines an array of sparse mmap areas where each area contains an index into the array of file descriptors.

When all sparse areas share a single file descriptor, servers may continue to emit VFIO_REGION_INFO_CAP_SPARSE_MMAP and send exactly one file descriptor in SCM_RIGHTS for backwards compatibility with older clients.

The extensions in this series also lays the groundwork to make use of the in-development series [1] extending vfio-pci to return distinct dmabuf FDs for discrete regions of a device's BAR. A vfio-user server may choose to return dmabuf FDs for various regions of a device's BAR to map a vfio passthrough device into the guest, in order to retain control of the device and be able to revoke mappings passed to the VM.

libvfio-user server side changes WIP on github [2].

[1] https://lore.kernel.org/all/[email protected]
[2] https://github.com/NamanGulati/libvfio-user/tree/multi-region-fd

---
Changes from v4:
- Set .setup_sparse_mmaps = vfio_default_setup_sparse_mmaps in vfio_device_io_ops_ioctl. (Cedric)
- Return -EINVAL in vfio_user_device_io_get_region_info() when VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS is present without received FDs. (Cedric)
- Dropped redundant VFIO_USER_MAX_REGIONS bounds checks in vfio_user_device_io_get_region_info() and vfio_user_device_io_setup_sparse_mmaps(). (Thanos)
- Validated sparse_fds->nr_areas against info->argsz and switched to

    g_try_new0() for region->mmaps. (Thanos)

- Included area info in invalid area error message and added comments
for recv_fds resets in hw/vfio-user/proxy.c. (Thanos)
- Link to v4: https://lore.kernel.org/qemu-devel/[email protected]

Changes from v3:
- Added "sparse_mmap_fds" capability to VFIO_USER_VERSION so servers
know whether the client supports
VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS. (Thanos)
- Changed VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS capability ID to 0x8001. (Thanos)
- Clarified and enforced that VFIO_REGION_INFO_CAP_SPARSE_MMAP and VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS are mutually exclusive per region,
number of FDs in SCM_RIGHTS must not exceed nr_areas, zero-sized areas
are not permitted, offset + size must not exceed the region size, and
areas must not overlap. (Thanos)
- Link to v3: https://lore.kernel.org/qemu-devel/[email protected]

Changes from v2:
- As suggested by Cedric: kept core VFIO changes minimal by only
extending VFIOMMmap to hold each mmap subregion's fd & fd_offset.
- Moved multi-FD management and capability parsing into hw/vfio-user,
and using VFIOUserProxy to hold region multi-fds.
- Resend: rebased to head.
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]

Changes from RFC:
- Refactored the get_region_info callback to keep vbasedev constant
and take in a VFIORegionFDs* out parameter to store the received fds.
- Link to RFC: https://lore.kernel.org/qemu-devel/[email protected]
---

Naman Gulati (4):
vfio-user: add multi-fd region mmap capability
vfio: add 'fd' and 'fd_offset' to VFIOMmap
vfio: add setup_sparse_mmaps hook to VFIODeviceIOOps
vfio-user: support multiple FDs for sparse mmap regions

 docs/interop/vfio-user.rst    |  86 ++++++++++++++++++++++--
 hw/vfio-user/protocol.h       |  35 ++++++++--
 hw/vfio-user/proxy.h          |   2 +
 hw/vfio/vfio-region.h         |   9 ++-
 include/hw/vfio/vfio-device.h |  16 +++++
 hw/vfio-user/device.c         | 121 ++++++++++++++++++++++++++++++++--
 hw/vfio-user/proxy.c          |  52 +++++++++++++--
 hw/vfio/device.c              |   1 +
 hw/vfio/pci.c                 |   2 +
 hw/vfio/region.c              |  45 ++++++++++---
 10 files changed, 337 insertions(+), 32 deletions(-)
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#1 lore raw

PATCH v5 1/4 vfio-user: add multi-fd region mmap capability

Extend the vfio-user protocol specification to allow servers to pass multiple file descriptors for different sparse memory areas within a single device region with capability VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS (0x8001), negotiated via the "sparse_mmap_fds" capability in VFIO_USER_VERSION.

VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS defines an array of sparse mmap areas where each area contains:
- offset: Start of the area within the device region (BAR).
- fd_offset: Start of the mapping within the backing file descriptor.
- size: Size of the mappable area.
- fd_index: Zero-based index into the array of file descriptors received in the SCM_RIGHTS ancillary data of the VFIO_USER_DEVICE_GET_REGION_INFO reply message.

---
 docs/interop/vfio-user.rst | 86 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 82 insertions(+), 4 deletions(-)
docs/interop/vfio-user.rst +82 -4
index 1c92f442d2..b49781a755 100644
@@ -125,7 +125,13 @@ Sparse Regions
 A region can be memory-mappable in whole or in part. When only a subset of a
 region can be mapped by the client, a ``VFIO_REGION_INFO_CAP_SPARSE_MMAP``
 capability is included in the region info reply. This capability describes
-which portions can be mapped by the client.
+which portions can be mapped by the client. When sparse mmap areas are backed
+by multiple distinct file descriptors, a
+``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` capability is included in the region
+info reply instead. ``VFIO_REGION_INFO_CAP_SPARSE_MMAP`` and
+``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` cannot both be specified for the same
+region; however, different regions of a device may independently use either
+capability.
 
 .. Note::
    For example, in a virtual NVMe controller, sparse regions can be used so
@@ -536,6 +542,10 @@ Capabilities:
 | write_multiple     | boolean | ``VFIO_USER_REGION_WRITE_MULTI`` messages     |
 |                    |         | are supported if the value is ``true``.       |
 +--------------------+---------+-----------------------------------------------+
+| sparse_mmap_fds    | boolean | ``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS``      |
+|                    |         | capability is supported if the value is       |
+|                    |         | ``true``.                                     |
++--------------------+---------+-----------------------------------------------+
 
 The ``twin_socket`` capability object holds these name/value pairs:
 
@@ -859,9 +869,11 @@ Reply
   * ``VFIO_REGION_INFO_FLAG_READ`` allows client read access to the region.
   * ``VFIO_REGION_INFO_FLAG_WRITE`` allows client write access to the region.
   * ``VFIO_REGION_INFO_FLAG_MMAP`` specifies the client can mmap() the region.
-    When this flag is set, the reply will include a file descriptor in its
-    meta-data. On ``AF_UNIX`` sockets, the file descriptors will be passed as
-    ``SCM_RIGHTS`` type ancillary data.
+    When this flag is set, the reply will include one or more file descriptors
+    in its meta-data (up to the number of sparse mmap areas when
+    ``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` is present, or a single file
+    descriptor otherwise). On ``AF_UNIX`` sockets, the file descriptors will be
+    passed as ``SCM_RIGHTS`` type ancillary data.
   * ``VFIO_REGION_INFO_FLAG_CAPS`` indicates additional capabilities found in the
     reply.
 
@@ -950,6 +962,72 @@ The VFIO sparse mmap area is defined in ``<linux/vfio.h>`` (``struct
 vfio_region_info_cap_sparse_mmap``).
 
 
+VFIO sparse mmap fds cap header
+"""""""""""""""""""""""""""""""
+
++----------------------+--------------------------------------+
+| Name                 | Value                                |
++======================+======================================+
+| id                   | VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS |
++----------------------+--------------------------------------+
+| version              | 0x1                                  |
++----------------------+--------------------------------------+
+| next                 | <next>                               |
++----------------------+--------------------------------------+
+| sparse mmap fds info | VFIO region info sparse mmap fds     |
++----------------------+--------------------------------------+
+
+This capability is defined when a region supports direct access by the client
+via mmap() where different sparse mmap areas are backed by distinct file
+descriptors in the ``SCM_RIGHTS`` ancillary data array. The server may only
+include this capability if the client advertised ``sparse_mmap_fds`` as
+``true`` in ``VFIO_USER_VERSION``. Capability ID
+``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` is defined as ``0x8001`` to avoid
+clashing with kernel VFIO capabilities.
+
+VFIO region info cap sparse mmap fds
+""""""""""""""""""""""""""""""""""""
+
++-----------+--------+------+
+| Name      | Offset | Size |
++===========+========+======+
+| nr_areas  | 0      | 4    |
++-----------+--------+------+
+| reserved  | 4      | 4    |
++-----------+--------+------+
+| offset    | 8      | 8    |
++-----------+--------+------+
+| fd_offset | 16     | 8    |
++-----------+--------+------+
+| size      | 24     | 8    |
++-----------+--------+------+
+| fd_index  | 32     | 4    |
++-----------+--------+------+
+| pad       | 36     | 4    |
++-----------+--------+------+
+| ...       |        |      |
++-----------+--------+------+
+
+* *nr_areas* is the number of sparse mmap areas in the region.
+* *offset*, *fd_offset*, *size*, and *fd_index* describe a single area that can be
+  mapped by the client. There will be *nr_areas* tuples of offset, fd_offset, size,
+  fd_index, and pad. Sparse mmap areas must not overlap.
+* *offset* specifies the start of the area within the device region (BAR).
+* *fd_offset* specifies the start of the mapping within the backing file descriptor
+  referenced by *fd_index*.
+* *size* specifies the size of the area. Zero-sized areas (*size* equal to 0)
+  are not permitted, and *offset* + *size* must not exceed the region *size*.
+* *fd_index* is the zero-based index into the array of file descriptors
+  received in the ``SCM_RIGHTS`` ancillary data of the
+  ``VFIO_USER_DEVICE_GET_REGION_INFO`` reply message. The number of file
+  descriptors sent in ``SCM_RIGHTS`` must be at least 1 and must not exceed
+  *nr_areas*.
+
+If all sparse mmap areas are backed by a single file descriptor, the server
+may instead send ``VFIO_REGION_INFO_CAP_SPARSE_MMAP`` and exactly one file
+descriptor in ``SCM_RIGHTS`` for compatibility with older clients.
+
+
 ``VFIO_USER_DEVICE_GET_REGION_IO_FDS``
 --------------------------------------
 
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
Reviewed-by on v5 1/4
#2 lore raw

Re: [PATCH v5 1/4] vfio-user: add multi-fd region mmap capability

Quoted 15 lines · Naman Gulati

Extend the vfio-user protocol specification to allow servers to pass multiple file descriptors for different sparse memory areas within a single device region with capability VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS (0x8001), negotiated via the "sparse_mmap_fds" capability in VFIO_USER_VERSION.

VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS defines an array of sparse mmap areas where each area contains:
- offset: Start of the area within the device region (BAR).
- fd_offset: Start of the mapping within the backing file descriptor.
- size: Size of the mappable area.
- fd_index: Zero-based index into the array of file descriptors received in the SCM_RIGHTS ancillary data of the VFIO_USER_DEVICE_GET_REGION_INFO reply message.

Quoted 2 lines

regards
john

#3 lore raw

PATCH v5 2/4 vfio: add 'fd' and 'fd_offset' to VFIOMmap

In reply to Naman Gulati

Allow each sparse mmap subregion to specify its own file descriptor and file offset for the mmap call.

Add 'fd' (set to -1 when the region fd should be used) and 'fd_offset' to VFIOMmap, and update vfio_region_mmap() to use them.

---
 hw/vfio/vfio-region.h |  4 +++-
 hw/vfio/pci.c         |  2 ++
 hw/vfio/region.c      | 26 +++++++++++++++++++-------
 3 files changed, 24 insertions(+), 8 deletions(-)
hw/vfio/vfio-region.h +3 -1
index 58b236f113..9f6a5cf7d5 100644
@@ -14,8 +14,10 @@
 typedef struct VFIOMmap {
     MemoryRegion mem;
     void *mmap;
-    off_t offset;
+    off_t offset;       /* offset within the region */
     size_t size;
+    int fd;             /* per-mmap fd, or -1 to use the region fd */
+    uint64_t fd_offset; /* offset within fd for the mmap call */
 } VFIOMmap;
 
 typedef struct VFIODevice VFIODevice;
hw/vfio/pci.c +2 -0
index 428ab2f069..f851082114 100644
@@ -1623,6 +1623,7 @@ static void vfio_pci_fixup_msix_region(VFIOPCIDevice *vdev)
                                   vdev->msix->table_bar, 0, 0);
         } else {
             region->mmaps[0].offset = end;
+            region->mmaps[0].fd_offset += end;
             region->mmaps[0].size = region->size - end;
             trace_vfio_msix_fixup(vdev->vbasedev.name,
                               vdev->msix->table_bar, region->mmaps[0].offset,
@@ -1649,6 +1650,7 @@ static void vfio_pci_fixup_msix_region(VFIOPCIDevice *vdev)
                               region->mmaps[0].offset + region->mmaps[0].size);
 
         region->mmaps[1].offset = end;
+        region->mmaps[1].fd_offset += end;
         region->mmaps[1].size = region->size - end;
         trace_vfio_msix_fixup(vdev->vbasedev.name,
                               vdev->msix->table_bar, region->mmaps[1].offset,
hw/vfio/region.c +19 -7
index 54ad11a6c8..16efed2fce 100644
@@ -189,6 +189,9 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
                                             sparse->areas[i].size - 1);
             region->mmaps[j].offset = sparse->areas[i].offset;
             region->mmaps[j].size = sparse->areas[i].size;
+            region->mmaps[j].fd = -1;
+            region->mmaps[j].fd_offset = region->fd_offset +
+                                         sparse->areas[i].offset;
             j++;
         }
     }
@@ -262,6 +265,8 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
                 region->mmaps = g_new0(VFIOMmap, region->nr_mmaps);
                 region->mmaps[0].offset = 0;
                 region->mmaps[0].size = region->size;
+                region->mmaps[0].fd = -1;
+                region->mmaps[0].fd_offset = region->fd_offset;
             } else if (ret) {
                 return ret;
             }
@@ -354,7 +359,6 @@ int vfio_region_mmap(VFIORegion *region)
     off_t map_offset = 0;
     size_t align;
     char *name;
-    int fd;
 
     if (!region->mem || !region->nr_mmaps) {
         return 0;
@@ -391,8 +395,6 @@ int vfio_region_mmap(VFIORegion *region)
         return ret;
     }
 
-    fd = vfio_device_get_region_fd(region->vbasedev, region->nr);
-
     map_align = (void *)ROUND_UP((uintptr_t)map_base, (uintptr_t)align);
     munmap(map_base, map_align - map_base);
     munmap(map_align + region->size,
@@ -404,12 +406,22 @@ int vfio_region_mmap(VFIORegion *region)
      * offsets being in ascending order.
      */
     for (i = 0; i < region->nr_mmaps; i++) {
+        int fd = region->mmaps[i].fd;
+
+        if (fd < 0) {
+            fd = vfio_device_get_region_fd(region->vbasedev, region->nr);
+        }
+        if (fd < 0) {
+            ret = -EINVAL;
+            munmap(map_align + map_offset, region->size - map_offset);
+            goto no_mmap;
+        }
+
         munmap(map_align + map_offset, region->mmaps[i].offset - map_offset);
         region->mmaps[i].mmap = mmap(map_align + region->mmaps[i].offset,
                                      region->mmaps[i].size, prot,
                                      MAP_SHARED | MAP_FIXED, fd,
-                                     region->fd_offset +
-                                     region->mmaps[i].offset);
+                                     region->mmaps[i].fd_offset);
         if (region->mmaps[i].mmap == MAP_FAILED) {
             ret = -errno;
             /*
@@ -454,8 +466,8 @@ int vfio_region_mmap(VFIORegion *region)
 
 no_mmap:
     trace_vfio_region_mmap_fault(memory_region_name(region->mem), i,
-                                 region->fd_offset + region->mmaps[i].offset,
-                                 region->fd_offset + region->mmaps[i].offset +
+                                 region->mmaps[i].fd_offset,
+                                 region->mmaps[i].fd_offset +
                                  region->mmaps[i].size - 1, ret);
 
     region->mmaps[i].mmap = NULL;
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
Reviewed-by on v5 2/4
#4 lore raw

Re: [PATCH v5 2/4] vfio: add 'fd' and 'fd_offset' to VFIOMmap

Quoted 116 lines · Naman Gulati

Allow each sparse mmap subregion to specify its own file descriptor and file offset for the mmap call.

Add 'fd' (set to -1 when the region fd should be used) and 'fd_offset' to VFIOMmap, and update vfio_region_mmap() to use them.

---
  hw/vfio/vfio-region.h |  4 +++-
  hw/vfio/pci.c         |  2 ++
  hw/vfio/region.c      | 26 +++++++++++++++++++-------
  3 files changed, 24 insertions(+), 8 deletions(-)
hw/vfio/vfio-region.h +3 -1
index 58b236f113..9f6a5cf7d5 100644
@@ -14,8 +14,10 @@
  typedef struct VFIOMmap {
      MemoryRegion mem;
      void *mmap;
-    off_t offset;
+    off_t offset;       /* offset within the region */
      size_t size;
+    int fd;             /* per-mmap fd, or -1 to use the region fd */
+    uint64_t fd_offset; /* offset within fd for the mmap call */
  } VFIOMmap;
 
  typedef struct VFIODevice VFIODevice;
hw/vfio/pci.c +2 -0
index 428ab2f069..f851082114 100644
@@ -1623,6 +1623,7 @@ static void vfio_pci_fixup_msix_region(VFIOPCIDevice *vdev)
                                    vdev->msix->table_bar, 0, 0);
          } else {
              region->mmaps[0].offset = end;
+            region->mmaps[0].fd_offset += end;
              region->mmaps[0].size = region->size - end;
              trace_vfio_msix_fixup(vdev->vbasedev.name,
                                vdev->msix->table_bar, region->mmaps[0].offset,
@@ -1649,6 +1650,7 @@ static void vfio_pci_fixup_msix_region(VFIOPCIDevice *vdev)
                                region->mmaps[0].offset + region->mmaps[0].size);
 
          region->mmaps[1].offset = end;
+        region->mmaps[1].fd_offset += end;
          region->mmaps[1].size = region->size - end;
          trace_vfio_msix_fixup(vdev->vbasedev.name,
                                vdev->msix->table_bar, region->mmaps[1].offset,
hw/vfio/region.c +17 -5
index 54ad11a6c8..16efed2fce 100644
@@ -189,6 +189,9 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
                                              sparse->areas[i].size - 1);
              region->mmaps[j].offset = sparse->areas[i].offset;
              region->mmaps[j].size = sparse->areas[i].size;
+            region->mmaps[j].fd = -1;
+            region->mmaps[j].fd_offset = region->fd_offset +
+                                         sparse->areas[i].offset;
              j++;
          }
      }
@@ -262,6 +265,8 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
                  region->mmaps = g_new0(VFIOMmap, region->nr_mmaps);
                  region->mmaps[0].offset = 0;
                  region->mmaps[0].size = region->size;
+                region->mmaps[0].fd = -1;
+                region->mmaps[0].fd_offset = region->fd_offset;
              } else if (ret) {
                  return ret;
              }
@@ -354,7 +359,6 @@ int vfio_region_mmap(VFIORegion *region)
      off_t map_offset = 0;
      size_t align;
      char *name;
-    int fd;
 
      if (!region->mem || !region->nr_mmaps) {
          return 0;
@@ -391,8 +395,6 @@ int vfio_region_mmap(VFIORegion *region)
          return ret;
      }
 
-    fd = vfio_device_get_region_fd(region->vbasedev, region->nr);
-
      map_align = (void *)ROUND_UP((uintptr_t)map_base, (uintptr_t)align);
      munmap(map_base, map_align - map_base);
      munmap(map_align + region->size,
@@ -404,12 +406,22 @@ int vfio_region_mmap(VFIORegion *region)
       * offsets being in ascending order.
       */
      for (i = 0; i < region->nr_mmaps; i++) {
+        int fd = region->mmaps[i].fd;
+
+        if (fd < 0) {
+            fd = vfio_device_get_region_fd(region->vbasedev, region->nr);
+        }
+        if (fd < 0) {
+            ret = -EINVAL;
+            munmap(map_align + map_offset, region->size - map_offset);
+            goto no_mmap;
+        }
+
          munmap(map_align + map_offset, region->mmaps[i].offset - map_offset);
          region->mmaps[i].mmap = mmap(map_align + region->mmaps[i].offset,
                                       region->mmaps[i].size, prot,
                                       MAP_SHARED | MAP_FIXED, fd,
-                                     region->fd_offset +
-                                     region->mmaps[i].offset);
+                                     region->mmaps[i].fd_offset);
          if (region->mmaps[i].mmap == MAP_FAILED) {
              ret = -errno;
              /*

hw/vfio/region.c int vfio_region_mmap(VFIORegion *region) · line 473

Quoted 10 lines
hw/vfio/region.c +2 -2
@@ -454,8 +466,8 @@ int vfio_region_mmap(VFIORegion *region)
 
  no_mmap:
      trace_vfio_region_mmap_fault(memory_region_name(region->mem), i,
-                                 region->fd_offset + region->mmaps[i].offset,
-                                 region->fd_offset + region->mmaps[i].offset +
+                                 region->mmaps[i].fd_offset,
+                                 region->mmaps[i].fd_offset +
                                   region->mmaps[i].size - 1, ret);
 
      region->mmaps[i].mmap = NULL;

Thanks,

C.

Reviewed-by on v5 2/4
#5 lore raw

Re: [PATCH v5 2/4] vfio: add 'fd' and 'fd_offset' to VFIOMmap

In reply to Naman Gulati
Quoted 8 lines · Naman Gulati

Allow each sparse mmap subregion to specify its own file descriptor and file offset for the mmap call.

Add 'fd' (set to -1 when the region fd should be used) and 'fd_offset' to VFIOMmap, and update vfio_region_mmap() to use them.

thanks
john

#6 lore raw

PATCH v5 3/4 vfio: add setup_sparse_mmaps hook to VFIODeviceIOOps

In reply to Naman Gulati

Add a setup_sparse_mmaps callback to VFIODeviceIOOps so transport backends (such as vfio-user) can parse custom sparse mmap capabilities, falling back to vfio_default_setup_sparse_mmaps() to process shared VFIO mmap capabilities.

---
 hw/vfio/vfio-region.h         |  5 +++++
 include/hw/vfio/vfio-device.h | 16 ++++++++++++++++
 hw/vfio/device.c              |  1 +
 hw/vfio/region.c              | 19 ++++++++++++++++---
 4 files changed, 38 insertions(+), 3 deletions(-)
hw/vfio/vfio-region.h +5 -0
index 9f6a5cf7d5..a0c9533f9f 100644
@@ -46,4 +46,9 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
 void vfio_region_exit(VFIORegion *region);
 void vfio_region_finalize(VFIORegion *region);
 
+struct vfio_region_info;
+int vfio_default_setup_sparse_mmaps(VFIORegion *region,
+                                    struct vfio_region_info *info,
+                                    Error **errp);
+
 #endif /* HW_VFIO_REGION_H */
include/hw/vfio/vfio-device.h +16 -0
index 8472420d3f..3136e152f2 100644
@@ -43,6 +43,7 @@ enum {
 
 typedef struct VFIODeviceOps VFIODeviceOps;
 typedef struct VFIODeviceIOOps VFIODeviceIOOps;
+typedef struct VFIORegion VFIORegion;
 typedef struct VFIOMigration VFIOMigration;
 
 typedef struct IOMMUFDBackend IOMMUFDBackend;
@@ -218,6 +219,21 @@ struct VFIODeviceIOOps {
     int (*get_region_info)(VFIODevice *vdev,
                            struct vfio_region_info *info, int *fd);
 
+    /**
+     * @setup_sparse_mmaps
+     *
+     * Parse sparse mmap capabilities for a region and initialize region->mmaps.
+     *
+     * @region: #VFIORegion to set up
+     * @info: region info struct containing capabilities
+     * @errp: pointer to Error*, to store an error if it happens
+     *
+     * Returns 0 on success, -ENODEV if not sparse, or negative error code.
+     */
+    int (*setup_sparse_mmaps)(VFIORegion *region,
+                              struct vfio_region_info *info,
+                              Error **errp);
+
     /**
      * @get_irq_info
      *
hw/vfio/device.c +1 -0
index 4f11959633..94d6e7ce09 100644
@@ -662,6 +662,7 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
 
     .device_feature = vfio_device_io_device_feature,
     .get_region_info = vfio_device_io_get_region_info,
+    .setup_sparse_mmaps = vfio_default_setup_sparse_mmaps,
     .get_irq_info = vfio_device_io_get_irq_info,
     .set_irqs = vfio_device_io_set_irqs,
     .region_read = vfio_device_io_region_read,
hw/vfio/region.c +16 -3
index 16efed2fce..94cfb708bf 100644
@@ -162,9 +162,9 @@ static int vfio_mmap_compare_offset(const void *a, const void *b)
     return 0;
 }
 
-static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
-                                          struct vfio_region_info *info,
-                                          Error **errp)
+int vfio_default_setup_sparse_mmaps(VFIORegion *region,
+                                    struct vfio_region_info *info,
+                                    Error **errp)
 {
     struct vfio_info_cap_header *hdr;
     struct vfio_region_info_cap_sparse_mmap *sparse;
@@ -198,6 +198,19 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
 
     region->nr_mmaps = j;
     region->mmaps = g_realloc(region->mmaps, j * sizeof(VFIOMmap));
+    return 0;
+}
+
+static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
+                                          struct vfio_region_info *info,
+                                          Error **errp)
+{
+    int ret, i;
+
+    ret = region->vbasedev->io_ops->setup_sparse_mmaps(region, info, errp);
+    if (ret) {
+        return ret;
+    }
 
     /*
      * Sort sparse mmaps by offset to ensure proper handling of gaps
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
Reviewed-by on v5 3/4
#7 lore raw

Re: [PATCH v5 3/4] vfio: add setup_sparse_mmaps hook to VFIODeviceIOOps

Quoted 101 lines · Naman Gulati

Add a setup_sparse_mmaps callback to VFIODeviceIOOps so transport backends (such as vfio-user) can parse custom sparse mmap capabilities, falling back to vfio_default_setup_sparse_mmaps() to process shared VFIO mmap capabilities.

---
  hw/vfio/vfio-region.h         |  5 +++++
  include/hw/vfio/vfio-device.h | 16 ++++++++++++++++
  hw/vfio/device.c              |  1 +
  hw/vfio/region.c              | 19 ++++++++++++++++---
  4 files changed, 38 insertions(+), 3 deletions(-)
hw/vfio/vfio-region.h +5 -0
index 9f6a5cf7d5..a0c9533f9f 100644
@@ -46,4 +46,9 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
  void vfio_region_exit(VFIORegion *region);
  void vfio_region_finalize(VFIORegion *region);
 
+struct vfio_region_info;
+int vfio_default_setup_sparse_mmaps(VFIORegion *region,
+                                    struct vfio_region_info *info,
+                                    Error **errp);
+
  #endif /* HW_VFIO_REGION_H */
include/hw/vfio/vfio-device.h +16 -0
index 8472420d3f..3136e152f2 100644
@@ -43,6 +43,7 @@ enum {
 
  typedef struct VFIODeviceOps VFIODeviceOps;
  typedef struct VFIODeviceIOOps VFIODeviceIOOps;
+typedef struct VFIORegion VFIORegion;
  typedef struct VFIOMigration VFIOMigration;
 
  typedef struct IOMMUFDBackend IOMMUFDBackend;
@@ -218,6 +219,21 @@ struct VFIODeviceIOOps {
      int (*get_region_info)(VFIODevice *vdev,
                             struct vfio_region_info *info, int *fd);
 
+    /**
+     * @setup_sparse_mmaps
+     *
+     * Parse sparse mmap capabilities for a region and initialize region->mmaps.
+     *
+     * @region: #VFIORegion to set up
+     * @info: region info struct containing capabilities
+     * @errp: pointer to Error*, to store an error if it happens
+     *
+     * Returns 0 on success, -ENODEV if not sparse, or negative error code.
+     */
+    int (*setup_sparse_mmaps)(VFIORegion *region,
+                              struct vfio_region_info *info,
+                              Error **errp);
+
      /**
       * @get_irq_info
       *
hw/vfio/device.c +1 -0
index 4f11959633..94d6e7ce09 100644
@@ -662,6 +662,7 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
 
      .device_feature = vfio_device_io_device_feature,
      .get_region_info = vfio_device_io_get_region_info,
+    .setup_sparse_mmaps = vfio_default_setup_sparse_mmaps,
      .get_irq_info = vfio_device_io_get_irq_info,
      .set_irqs = vfio_device_io_set_irqs,
      .region_read = vfio_device_io_region_read,
hw/vfio/region.c +9 -3
index 16efed2fce..94cfb708bf 100644
@@ -162,9 +162,9 @@ static int vfio_mmap_compare_offset(const void *a, const void *b)
      return 0;
  }
 
-static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
-                                          struct vfio_region_info *info,
-                                          Error **errp)
+int vfio_default_setup_sparse_mmaps(VFIORegion *region,
+                                    struct vfio_region_info *info,
+                                    Error **errp)
  {
      struct vfio_info_cap_header *hdr;
      struct vfio_region_info_cap_sparse_mmap *sparse;
@@ -198,6 +198,19 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
 
      region->nr_mmaps = j;
      region->mmaps = g_realloc(region->mmaps, j * sizeof(VFIOMmap));
+    return 0;
+}
+
+static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
+                                          struct vfio_region_info *info,
+                                          Error **errp)

hw/vfio/region.c static int vfio_setup_region_sparse_mmaps(VFIORegion *region, · line 216

Quoted 10 lines
hw/vfio/region.c +7 -0
+{
+    int ret, i;
+
+    ret = region->vbasedev->io_ops->setup_sparse_mmaps(region, info, errp);
+    if (ret) {
+        return ret;
+    }
 
      /*
       * Sort sparse mmaps by offset to ensure proper handling of gaps

Thanks,

C.

#8 lore raw

Re: [PATCH v5 3/4] vfio: add setup_sparse_mmaps hook to VFIODeviceIOOps

In reply to Naman Gulati
Quoted 19 lines · Naman Gulati

Add a setup_sparse_mmaps callback to VFIODeviceIOOps so transport backends (such as vfio-user) can parse custom sparse mmap capabilities, falling back to vfio_default_setup_sparse_mmaps() to process shared VFIO mmap capabilities.

---
  hw/vfio/vfio-region.h         |  5 +++++
  include/hw/vfio/vfio-device.h | 16 ++++++++++++++++
  hw/vfio/device.c              |  1 +
  hw/vfio/region.c              | 19 ++++++++++++++++---
  4 files changed, 38 insertions(+), 3 deletions(-)
hw/vfio/vfio-region.h +0 -0
index 9f6a5cf7d5..a0c9533f9f 100644

hw/vfio/vfio-region.h void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled); · line 52

Quoted 7 lines
hw/vfio/vfio-region.h +4 -0
@@ -46,4 +46,9 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
  void vfio_region_exit(VFIORegion *region);
  void vfio_region_finalize(VFIORegion *region);
 
+struct vfio_region_info;
+int vfio_default_setup_sparse_mmaps(VFIORegion *region,
+                                    struct vfio_region_info *info,
+                                    Error **errp);

If you resend, I'd prefer this routine to be named :

vfio_region_setup_sparse_mmaps()

Thanks,

C.

Quoted 85 lines
hw/vfio/vfio-region.h +1 -0
+
  #endif /* HW_VFIO_REGION_H */
include/hw/vfio/vfio-device.h +16 -0
index 8472420d3f..3136e152f2 100644
@@ -43,6 +43,7 @@ enum {
 
  typedef struct VFIODeviceOps VFIODeviceOps;
  typedef struct VFIODeviceIOOps VFIODeviceIOOps;
+typedef struct VFIORegion VFIORegion;
  typedef struct VFIOMigration VFIOMigration;
 
  typedef struct IOMMUFDBackend IOMMUFDBackend;
@@ -218,6 +219,21 @@ struct VFIODeviceIOOps {
      int (*get_region_info)(VFIODevice *vdev,
                             struct vfio_region_info *info, int *fd);
 
+    /**
+     * @setup_sparse_mmaps
+     *
+     * Parse sparse mmap capabilities for a region and initialize region->mmaps.
+     *
+     * @region: #VFIORegion to set up
+     * @info: region info struct containing capabilities
+     * @errp: pointer to Error*, to store an error if it happens
+     *
+     * Returns 0 on success, -ENODEV if not sparse, or negative error code.
+     */
+    int (*setup_sparse_mmaps)(VFIORegion *region,
+                              struct vfio_region_info *info,
+                              Error **errp);
+
      /**
       * @get_irq_info
       *
hw/vfio/device.c +1 -0
index 4f11959633..94d6e7ce09 100644
@@ -662,6 +662,7 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
 
      .device_feature = vfio_device_io_device_feature,
      .get_region_info = vfio_device_io_get_region_info,
+    .setup_sparse_mmaps = vfio_default_setup_sparse_mmaps,
      .get_irq_info = vfio_device_io_get_irq_info,
      .set_irqs = vfio_device_io_set_irqs,
      .region_read = vfio_device_io_region_read,
hw/vfio/region.c +16 -3
index 16efed2fce..94cfb708bf 100644
@@ -162,9 +162,9 @@ static int vfio_mmap_compare_offset(const void *a, const void *b)
      return 0;
  }
 
-static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
-                                          struct vfio_region_info *info,
-                                          Error **errp)
+int vfio_default_setup_sparse_mmaps(VFIORegion *region,
+                                    struct vfio_region_info *info,
+                                    Error **errp)
  {
      struct vfio_info_cap_header *hdr;
      struct vfio_region_info_cap_sparse_mmap *sparse;
@@ -198,6 +198,19 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
 
      region->nr_mmaps = j;
      region->mmaps = g_realloc(region->mmaps, j * sizeof(VFIOMmap));
+    return 0;
+}
+
+static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
+                                          struct vfio_region_info *info,
+                                          Error **errp)
+{
+    int ret, i;
+
+    ret = region->vbasedev->io_ops->setup_sparse_mmaps(region, info, errp);
+    if (ret) {
+        return ret;
+    }
 
      /*
       * Sort sparse mmaps by offset to ensure proper handling of gaps
Reviewed-by on v5 3/4
#9 lore raw

Re: [PATCH v5 3/4] vfio: add setup_sparse_mmaps hook to VFIODeviceIOOps

In reply to Naman Gulati
Quoted 7 lines · Naman Gulati

Add a setup_sparse_mmaps callback to VFIODeviceIOOps so transport backends (such as vfio-user) can parse custom sparse mmap capabilities, falling back to vfio_default_setup_sparse_mmaps() to process shared VFIO mmap capabilities.

regards
john

#10 lore raw

PATCH v5 4/4 vfio-user: support multiple FDs for sparse mmap regions

In reply to Naman Gulati

Add client support for capability VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS (capability ID 0x8001) in vfio-user, and advertise support via the "sparse_mmap_fds" capability in VFIO_USER_VERSION.

Update vfio_user_device_io_get_region_info() to receive multiple file descriptors passed in SCM_RIGHTS ancillary data for a region, store them in VFIOUserProxy, and implement setup_sparse_mmaps in vfio_user_device_io_ops_sock to validate and populate per-area file descriptors and fd_offset values.

---
 hw/vfio-user/protocol.h |  35 ++++++++++--
 hw/vfio-user/proxy.h    |   2 +
 hw/vfio-user/device.c   | 121 +++++++++++++++++++++++++++++++++++++---
 hw/vfio-user/proxy.c    |  52 +++++++++++++++--
 4 files changed, 193 insertions(+), 17 deletions(-)
hw/vfio-user/protocol.h +29 -6
index c478d1353f..b823689254 100644
@@ -15,6 +15,8 @@
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
+#define VFIO_USER_MAX_REGIONS 100
+
 typedef struct {
     uint16_t id;
     uint16_t command;
@@ -69,12 +71,13 @@ typedef struct {
 #define VFIO_USER_CAP           "capabilities"
 
 /* "capabilities" members */
-#define VFIO_USER_CAP_MAX_FDS   "max_msg_fds"
-#define VFIO_USER_CAP_MAX_XFER  "max_data_xfer_size"
-#define VFIO_USER_CAP_PGSIZES   "pgsizes"
-#define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
-#define VFIO_USER_CAP_MIGR      "migration"
-#define VFIO_USER_CAP_MULTI     "write_multiple"
+#define VFIO_USER_CAP_MAX_FDS           "max_msg_fds"
+#define VFIO_USER_CAP_MAX_XFER          "max_data_xfer_size"
+#define VFIO_USER_CAP_PGSIZES           "pgsizes"
+#define VFIO_USER_CAP_MAP_MAX           "max_dma_maps"
+#define VFIO_USER_CAP_MIGR              "migration"
+#define VFIO_USER_CAP_MULTI             "write_multiple"
+#define VFIO_USER_CAP_SPARSE_MMAP_FDS   "sparse_mmap_fds"
 
 /* "migration" members */
 #define VFIO_USER_CAP_PGSIZE            "pgsize"
@@ -166,6 +169,26 @@ typedef struct {
     uint64_t offset;
 } VFIOUserRegionInfo;
 
+/*
+ * VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS
+ */
+#define VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS 0x8001
+
+struct vfio_region_sparse_mmap_fd_area {
+    uint64_t offset;
+    uint64_t fd_offset;
+    uint64_t size;
+    uint32_t fd_index;
+    uint32_t pad;
+};
+
+struct vfio_region_info_cap_sparse_mmap_fds {
+    struct vfio_info_cap_header header;
+    uint32_t nr_areas;
+    uint32_t reserved;
+    struct vfio_region_sparse_mmap_fd_area areas[];
+};
+
 /*
  * VFIO_USER_DEVICE_GET_IRQ_INFO
  * imported from struct vfio_irq_info
hw/vfio-user/proxy.h +2 -0
index 7b97460cc5..4b9834a436 100644
@@ -72,6 +72,7 @@ typedef struct VFIOUserProxy {
     AioContext *ctx;
     QEMUBH *req_bh;
     bool async_ops;
+    VFIOUserFDs *region_fds[VFIO_USER_MAX_REGIONS];
 
     /*
      * above only changed when BQL is held
@@ -111,6 +112,7 @@ bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 
 VFIOUserFDs *vfio_user_getfds(int numfds);
 void vfio_user_putfds(VFIOUserMsg *msg);
+void vfio_user_free_fds(VFIOUserFDs *fds);
 
 void vfio_user_disable_posted_writes(VFIOUserProxy *proxy);
 
hw/vfio-user/device.c +114 -7

162 lines not on this page · raw message

hw/vfio-user/proxy.c +48 -4

107 lines not on this page · raw message

Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#11 lore raw

Re: [PATCH v5 4/4] vfio-user: support multiple FDs for sparse mmap regions

Quoted 123 lines · Naman Gulati

Add client support for capability VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS (capability ID 0x8001) in vfio-user, and advertise support via the "sparse_mmap_fds" capability in VFIO_USER_VERSION.

Update vfio_user_device_io_get_region_info() to receive multiple file descriptors passed in SCM_RIGHTS ancillary data for a region, store them in VFIOUserProxy, and implement setup_sparse_mmaps in vfio_user_device_io_ops_sock to validate and populate per-area file descriptors and fd_offset values.

---
  hw/vfio-user/protocol.h |  35 ++++++++++--
  hw/vfio-user/proxy.h    |   2 +
  hw/vfio-user/device.c   | 121 +++++++++++++++++++++++++++++++++++++---
  hw/vfio-user/proxy.c    |  52 +++++++++++++++--
  4 files changed, 193 insertions(+), 17 deletions(-)
hw/vfio-user/protocol.h +29 -6
index c478d1353f..b823689254 100644
@@ -15,6 +15,8 @@
   * SPDX-License-Identifier: GPL-2.0-or-later
   */
 
+#define VFIO_USER_MAX_REGIONS 100
+
  typedef struct {
      uint16_t id;
      uint16_t command;
@@ -69,12 +71,13 @@ typedef struct {
  #define VFIO_USER_CAP           "capabilities"
 
  /* "capabilities" members */
-#define VFIO_USER_CAP_MAX_FDS   "max_msg_fds"
-#define VFIO_USER_CAP_MAX_XFER  "max_data_xfer_size"
-#define VFIO_USER_CAP_PGSIZES   "pgsizes"
-#define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
-#define VFIO_USER_CAP_MIGR      "migration"
-#define VFIO_USER_CAP_MULTI     "write_multiple"
+#define VFIO_USER_CAP_MAX_FDS           "max_msg_fds"
+#define VFIO_USER_CAP_MAX_XFER          "max_data_xfer_size"
+#define VFIO_USER_CAP_PGSIZES           "pgsizes"
+#define VFIO_USER_CAP_MAP_MAX           "max_dma_maps"
+#define VFIO_USER_CAP_MIGR              "migration"
+#define VFIO_USER_CAP_MULTI             "write_multiple"
+#define VFIO_USER_CAP_SPARSE_MMAP_FDS   "sparse_mmap_fds"
 
  /* "migration" members */
  #define VFIO_USER_CAP_PGSIZE            "pgsize"
@@ -166,6 +169,26 @@ typedef struct {
      uint64_t offset;
  } VFIOUserRegionInfo;
 
+/*
+ * VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS
+ */
+#define VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS 0x8001
+
+struct vfio_region_sparse_mmap_fd_area {
+    uint64_t offset;
+    uint64_t fd_offset;
+    uint64_t size;
+    uint32_t fd_index;
+    uint32_t pad;
+};
+
+struct vfio_region_info_cap_sparse_mmap_fds {
+    struct vfio_info_cap_header header;
+    uint32_t nr_areas;
+    uint32_t reserved;
+    struct vfio_region_sparse_mmap_fd_area areas[];
+};
+
  /*
   * VFIO_USER_DEVICE_GET_IRQ_INFO
   * imported from struct vfio_irq_info
hw/vfio-user/proxy.h +2 -0
index 7b97460cc5..4b9834a436 100644
@@ -72,6 +72,7 @@ typedef struct VFIOUserProxy {
      AioContext *ctx;
      QEMUBH *req_bh;
      bool async_ops;
+    VFIOUserFDs *region_fds[VFIO_USER_MAX_REGIONS];
 
      /*
       * above only changed when BQL is held
@@ -111,6 +112,7 @@ bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 
  VFIOUserFDs *vfio_user_getfds(int numfds);
  void vfio_user_putfds(VFIOUserMsg *msg);
+void vfio_user_free_fds(VFIOUserFDs *fds);
 
  void vfio_user_disable_posted_writes(VFIOUserProxy *proxy);
 
hw/vfio-user/device.c +3 -2
index 1a01e748cd..4fbf536a04 100644
@@ -14,12 +14,14 @@
 
  #include "hw/vfio-user/device.h"
  #include "hw/vfio-user/trace.h"
+#include "hw/vfio/vfio-region.h"
+#include "hw/vfio/vfio-helpers.h"
+#include "hw/vfio/trace.h"
 
  /*
   * These are to defend against a malign server trying
   * to force us to run out of memory.
   */
-#define VFIO_USER_MAX_REGIONS   100
  #define VFIO_USER_MAX_IRQS      50
 
  bool vfio_user_get_device_info(VFIOUserProxy *proxy,
@@ -209,14 +211,17 @@ static int vfio_user_device_io_get_region_info(VFIODevice *vbasedev,
                                                 struct vfio_region_info *info,
                                                 int *fd)
  {
-    VFIOUserFDs fds = { 0, 1, fd};

hw/vfio-user/device.c static int vfio_user_device_io_get_region_info(VFIODevice *vbasedev, · line 222

Quoted 10 lines
hw/vfio-user/device.c +5 -1
-    int ret;
+    int fds[VFIO_USER_MAX_MAX_FDS];
+    VFIOUserFDs user_fds = { 0, VFIO_USER_MAX_MAX_FDS, fds };
+    int i, ret;
+
+    *fd = -1;
 
      if (info->index > vbasedev->num_initial_regions) {
          return -EINVAL;
      }

info->index is validated above

hw/vfio-user/device.c static int vfio_user_device_io_get_region_info(VFIODevice *vbasedev, · line 224

Quoted 2 lines
hw/vfio-user/device.c +1 -1
-    ret = vfio_user_get_region_info(vbasedev->proxy, info, &fds);
+    ret = vfio_user_get_region_info(vbasedev->proxy, info, &user_fds);

info is modified by vfio_user_get_region_info() :

  memcpy(info, &msgp->argsz, info->argsz);

without revalidation of the udpated fields and ...

Quoted 8 lines
hw/vfio-user/device.c +0 -1
      if (ret) {
          return ret;
      }
@@ -225,10 +230,30 @@ static int vfio_user_device_io_get_region_info(VFIODevice *vbasedev,
      if ((info->flags & VFIO_REGION_INFO_FLAG_CAPS) &&
          (info->cap_offset < sizeof(*info)
           || info->cap_offset + sizeof(struct vfio_info_cap_header) > info->argsz)) {
-        return -EINVAL;

hw/vfio-user/device.c static int vfio_user_device_io_get_region_info(VFIODevice *vbasedev, · line 242

Quoted 10 lines
hw/vfio-user/device.c +10 -0
+        ret = -EINVAL;
+    } else if (vfio_get_region_info_cap(info,
+                                        VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS)) {
+        if (user_fds.recv_fds > 0) {
+            VFIOUserFDs *saved = vfio_user_getfds(user_fds.recv_fds);
+
+            saved->recv_fds = user_fds.recv_fds;
+            memcpy(saved->fds, fds, user_fds.recv_fds * sizeof(int));
+            vfio_user_free_fds(vbasedev->proxy->region_fds[info->index]);
+            vbasedev->proxy->region_fds[info->index] = saved;

info->index could be bogus. This is a pre-existing flaw. Would be good to fix.

Thanks,

C.

Quoted 233 lines
hw/vfio-user/device.c +95 -2
+            return 0;
+        }
+        ret = -EINVAL;
+    } else if (user_fds.recv_fds > 0) {
+        *fd = fds[0];
+        fds[0] = -1;
      }
 
-    return 0;
+    for (i = 0; i < user_fds.recv_fds; i++) {
+        if (fds[i] >= 0) {
+            close(fds[i]);
+        }
+    }
+    return ret;
  }
 
  static int vfio_user_device_io_get_irq_info(VFIODevice *vbasedev,
@@ -518,12 +543,94 @@ static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
  /*
   * Socket-based io_ops
   */
+static int vfio_user_device_io_setup_sparse_mmaps(VFIORegion *region,
+                                                  struct vfio_region_info *info,
+                                                  Error **errp)
+{
+    struct vfio_info_cap_header *hdr;
+    struct vfio_region_info_cap_sparse_mmap_fds *sparse_fds;
+    VFIOUserFDs *user_fds;
+    size_t cap_offset;
+    int i;
+
+    hdr = vfio_get_region_info_cap(info, VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS);
+    if (!hdr) {
+        return vfio_default_setup_sparse_mmaps(region, info, errp);
+    }
+
+    if (vfio_get_region_info_cap(info, VFIO_REGION_INFO_CAP_SPARSE_MMAP)) {
+        error_setg(errp, "%s: region %d cannot specify both "
+                   "SPARSE_MMAP and SPARSE_MMAP_FDS", __func__, region->nr);
+        return -EINVAL;
+    }
+
+    sparse_fds = container_of(hdr, struct vfio_region_info_cap_sparse_mmap_fds,
+                              header);
+
+    cap_offset = (uintptr_t)hdr - (uintptr_t)info;
+    if (cap_offset > info->argsz ||
+        info->argsz - cap_offset < sizeof(*sparse_fds) ||
+        sparse_fds->nr_areas >
+            (info->argsz - cap_offset - sizeof(*sparse_fds)) /
+            sizeof(sparse_fds->areas[0])) {
+        error_setg(errp, "%s: invalid nr_areas %u in region %d",
+                   __func__, sparse_fds->nr_areas, region->nr);
+        return -EINVAL;
+    }
+
+    trace_vfio_region_sparse_mmap_header(region->vbasedev->name,
+                                         region->nr, sparse_fds->nr_areas);
+
+    user_fds = region->vbasedev->proxy->region_fds[region->nr];
+    if (!user_fds || user_fds->recv_fds > sparse_fds->nr_areas) {
+        error_setg(errp, "%s: invalid fd count %d for %u sparse mmap areas "
+                   "in region %d", __func__,
+                   user_fds ? user_fds->recv_fds : 0,
+                   sparse_fds->nr_areas, region->nr);
+        return -EINVAL;
+    }
+
+    region->mmaps = g_try_new0(VFIOMmap, sparse_fds->nr_areas);
+    if (!region->mmaps) {
+        error_setg(errp, "%s: failed to allocate %u mmaps for region %d",
+                   __func__, sparse_fds->nr_areas, region->nr);
+        return -ENOMEM;
+    }
+
+    for (i = 0; i < sparse_fds->nr_areas; i++) {
+        struct vfio_region_sparse_mmap_fd_area *area = &sparse_fds->areas[i];
+
+        if (area->size == 0 || area->offset > region->size ||
+            area->size > region->size - area->offset ||
+            area->fd_index >= user_fds->recv_fds) {
+            error_setg(errp, "%s: invalid sparse mmap area %d in region %d "
+                       "(offset=0x%"PRIx64" size=0x%"PRIx64
+                       " fd_offset=0x%"PRIx64" fd_index=%u)",
+                       __func__, i, region->nr, area->offset, area->size,
+                       area->fd_offset, area->fd_index);
+            g_free(region->mmaps);
+            region->mmaps = NULL;
+            return -EINVAL;
+        }
+
+        trace_vfio_region_sparse_mmap_entry(i, area->offset,
+                                            area->offset + area->size - 1);
+        region->mmaps[i].offset = area->offset;
+        region->mmaps[i].fd_offset = area->fd_offset;
+        region->mmaps[i].size = area->size;
+        region->mmaps[i].fd = user_fds->fds[area->fd_index];
+    }
+
+    region->nr_mmaps = sparse_fds->nr_areas;
+    return 0;
+}
+
  VFIODeviceIOOps vfio_user_device_io_ops_sock = {
      .device_feature = vfio_user_device_io_device_feature,
      .get_region_info = vfio_user_device_io_get_region_info,
+    .setup_sparse_mmaps = vfio_user_device_io_setup_sparse_mmaps,
      .get_irq_info = vfio_user_device_io_get_irq_info,
      .set_irqs = vfio_user_device_io_set_irqs,
      .region_read = vfio_user_device_io_region_read,
      .region_write = vfio_user_device_io_region_write,
-
  };
hw/vfio-user/proxy.c +48 -4
index 197aee07bf..ba29085321 100644
@@ -363,6 +363,13 @@ static int vfio_user_recv_one(VFIOUserProxy *proxy, Error **errp)
          }
 
          memcpy(msg->fds->fds, fdp, numfds * sizeof(int));
+    } else if (msg->fds != NULL) {
+        /*
+         * recv_fds holds the capacity of msg->fds->fds on entry; reset it
+         * to 0 when the reply carries no FDs so the caller knows none were
+         * received.
+         */
+        msg->fds->recv_fds = 0;
      }
 
      /*
@@ -740,6 +747,11 @@ bool vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
 
      qemu_mutex_unlock(&proxy->lock);
 
+    if (!ok && fds != NULL) {
+        /* No reply was received; report 0 received FDs to the caller. */
+        fds->recv_fds = 0;
+    }
+
      return ok;
  }
 
@@ -867,17 +879,30 @@ void vfio_user_send_error(VFIOUserProxy *proxy, VFIOUserHdr *hdr, int error)
  }
 
  /*
- * Close FDs erroneously received in an incoming request.
+ * Close and free received FDs.
   */
-void vfio_user_putfds(VFIOUserMsg *msg)
+void vfio_user_free_fds(VFIOUserFDs *fds)
  {
-    VFIOUserFDs *fds = msg->fds;
      int i;
 
+    if (!fds) {
+        return;
+    }
+
      for (i = 0; i < fds->recv_fds; i++) {
-        close(fds->fds[i]);
+        if (fds->fds[i] >= 0) {
+            close(fds->fds[i]);
+        }
      }
      g_free(fds);
+}
+
+/*
+ * Close FDs erroneously received in an incoming request.
+ */
+void vfio_user_putfds(VFIOUserMsg *msg)
+{
+    vfio_user_free_fds(msg->fds);
      msg->fds = NULL;
  }
 
@@ -967,6 +992,7 @@ void vfio_user_set_handler(VFIODevice *vbasedev,
  void vfio_user_disconnect(VFIOUserProxy *proxy)
  {
      VFIOUserMsg *r1, *r2;
+    int i;
 
      qemu_mutex_lock(&proxy->lock);
 
@@ -1027,6 +1053,10 @@ void vfio_user_disconnect(VFIOUserProxy *proxy)
          vfio_user_iothread = NULL;
      }
 
+    for (i = 0; i < VFIO_USER_MAX_REGIONS; i++) {
+        vfio_user_free_fds(proxy->region_fds[i]);
+    }
+
      g_free(proxy->sockname);
      g_free(proxy);
  }
@@ -1214,6 +1244,18 @@ static bool check_multi(VFIOUserProxy *proxy, QObject *qobj, Error **errp)
      return true;
  }
 
+static bool check_sparse_mmap_fds(VFIOUserProxy *proxy, QObject *qobj,
+                                  Error **errp)
+{
+    QBool *qb = qobject_to(QBool, qobj);
+
+    if (qb == NULL) {
+        error_setg(errp, "malformed %s", VFIO_USER_CAP_SPARSE_MMAP_FDS);
+        return false;
+    }
+    return true;
+}
+
  static struct cap_entry caps_cap[] = {
      { VFIO_USER_CAP_MAX_FDS, check_max_fds },
      { VFIO_USER_CAP_MAX_XFER, check_max_xfer },
@@ -1221,6 +1263,7 @@ static struct cap_entry caps_cap[] = {
      { VFIO_USER_CAP_MAP_MAX, check_max_dma },
      { VFIO_USER_CAP_MIGR, check_migr },
      { VFIO_USER_CAP_MULTI, check_multi },
+    { VFIO_USER_CAP_SPARSE_MMAP_FDS, check_sparse_mmap_fds },
      { NULL }
  };
 
@@ -1280,6 +1323,7 @@ static GString *caps_json(void)
      qdict_put_int(capdict, VFIO_USER_CAP_PGSIZES, VFIO_USER_DEF_PGSIZE);
      qdict_put_int(capdict, VFIO_USER_CAP_MAP_MAX, VFIO_USER_DEF_MAP_MAX);
      qdict_put_bool(capdict, VFIO_USER_CAP_MULTI, true);
+    qdict_put_bool(capdict, VFIO_USER_CAP_SPARSE_MMAP_FDS, true);
 
      qdict_put_obj(dict, VFIO_USER_CAP, QOBJECT(capdict));
 
#12 lore raw

Re: [PATCH v5 4/4] vfio-user: support multiple FDs for sparse mmap regions

In reply to Naman Gulati
Quoted 34 lines · Naman Gulati
hw/vfio-user/protocol.h +9 -6
index c478d1353f..b823689254 100644
@@ -15,6 +15,8 @@
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
+#define VFIO_USER_MAX_REGIONS 100
+
 typedef struct {
     uint16_t id;
     uint16_t command;
@@ -69,12 +71,13 @@ typedef struct {
 #define VFIO_USER_CAP           "capabilities"
 
 /* "capabilities" members */
-#define VFIO_USER_CAP_MAX_FDS   "max_msg_fds"
-#define VFIO_USER_CAP_MAX_XFER  "max_data_xfer_size"
-#define VFIO_USER_CAP_PGSIZES   "pgsizes"
-#define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
-#define VFIO_USER_CAP_MIGR      "migration"
-#define VFIO_USER_CAP_MULTI     "write_multiple"
+#define VFIO_USER_CAP_MAX_FDS           "max_msg_fds"
+#define VFIO_USER_CAP_MAX_XFER          "max_data_xfer_size"
+#define VFIO_USER_CAP_PGSIZES           "pgsizes"
+#define VFIO_USER_CAP_MAP_MAX           "max_dma_maps"
+#define VFIO_USER_CAP_MIGR              "migration"
+#define VFIO_USER_CAP_MULTI             "write_multiple"
+#define VFIO_USER_CAP_SPARSE_MMAP_FDS   "sparse_mmap_fds"
 
 /* "migration" members */
 #define VFIO_USER_CAP_PGSIZE            "pgsize"

hw/vfio-user/protocol.h typedef struct { · line 175

Quoted 7 lines
hw/vfio-user/protocol.h +4 -0
@@ -166,6 +169,26 @@ typedef struct {
     uint64_t offset;
 } VFIOUserRegionInfo;
 
+/*
+ * VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS
+ */
+#define VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS 0x8001

I think a brief note on the "0x8000..." space WRT kernel vfio is worthwhile here.

Otherwise seems good modulo the other comments.

Please do send out your libvfio-user PR for review as well.

thanks
john

#13 lore raw

RE: [PATCH v5 4/4] vfio-user: support multiple FDs for sparse mmap regions

In reply to Naman Gulati
Quoted 256 lines

-----Original Message-----
From: [email protected] <qemu- [email protected]> On Behalf Of Naman Gulati
Sent: 14 October 2026 22:11
To: [email protected]
Cc: John Levon <[email protected]>; Thanos Makatos <[email protected]>; Cédric Le Goater <[email protected]>; Alex Williamson <[email protected]>; Pierrick Bouvier <[email protected]>; Matt Evans <[email protected]>; Naman Gulati <[email protected]>
Subject: [PATCH v5 4/4] vfio-user: support multiple FDs for sparse mmap regions

!-------------------------------------------------------------------|
CAUTION: External Email

|-------------------------------------------------------------------!

Add client support for capability VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS (capability ID 0x8001) in vfio-user, and advertise support via the "sparse_mmap_fds" capability in VFIO_USER_VERSION.

Update vfio_user_device_io_get_region_info() to receive multiple file descriptors passed in SCM_RIGHTS ancillary data for a region, store them
in VFIOUserProxy, and implement setup_sparse_mmaps in vfio_user_device_io_ops_sock to validate and populate per-area file descriptors and fd_offset values.

---
 hw/vfio-user/protocol.h |  35 ++++++++++--
 hw/vfio-user/proxy.h    |   2 +
 hw/vfio-user/device.c   | 121 +++++++++++++++++++++++++++++++++++++--
(quoted diff) +0 -1
-
 hw/vfio-user/proxy.c    |  52 +++++++++++++++--
 4 files changed, 193 insertions(+), 17 deletions(-)
hw/vfio-user/protocol.h +29 -6
index c478d1353f..b823689254 100644
@@ -15,6 +15,8 @@
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
+#define VFIO_USER_MAX_REGIONS 100
+
 typedef struct {
     uint16_t id;
     uint16_t command;
@@ -69,12 +71,13 @@ typedef struct {
 #define VFIO_USER_CAP           "capabilities"
 
 /* "capabilities" members */
-#define VFIO_USER_CAP_MAX_FDS   "max_msg_fds"
-#define VFIO_USER_CAP_MAX_XFER  "max_data_xfer_size"
-#define VFIO_USER_CAP_PGSIZES   "pgsizes"
-#define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
-#define VFIO_USER_CAP_MIGR      "migration"
-#define VFIO_USER_CAP_MULTI     "write_multiple"
+#define VFIO_USER_CAP_MAX_FDS           "max_msg_fds"
+#define VFIO_USER_CAP_MAX_XFER          "max_data_xfer_size"
+#define VFIO_USER_CAP_PGSIZES           "pgsizes"
+#define VFIO_USER_CAP_MAP_MAX           "max_dma_maps"
+#define VFIO_USER_CAP_MIGR              "migration"
+#define VFIO_USER_CAP_MULTI             "write_multiple"
+#define VFIO_USER_CAP_SPARSE_MMAP_FDS   "sparse_mmap_fds"
 
 /* "migration" members */
 #define VFIO_USER_CAP_PGSIZE            "pgsize"
@@ -166,6 +169,26 @@ typedef struct {
     uint64_t offset;
 } VFIOUserRegionInfo;
 
+/*
+ * VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS
+ */
+#define VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS 0x8001
+
+struct vfio_region_sparse_mmap_fd_area {
+    uint64_t offset;
+    uint64_t fd_offset;
+    uint64_t size;
+    uint32_t fd_index;
+    uint32_t pad;
+};
+
+struct vfio_region_info_cap_sparse_mmap_fds {
+    struct vfio_info_cap_header header;
+    uint32_t nr_areas;
+    uint32_t reserved;
+    struct vfio_region_sparse_mmap_fd_area areas[];
+};
+
 /*
  * VFIO_USER_DEVICE_GET_IRQ_INFO
  * imported from struct vfio_irq_info
hw/vfio-user/proxy.h +2 -0
index 7b97460cc5..4b9834a436 100644
@@ -72,6 +72,7 @@ typedef struct VFIOUserProxy {
     AioContext *ctx;
     QEMUBH *req_bh;
     bool async_ops;
+    VFIOUserFDs *region_fds[VFIO_USER_MAX_REGIONS];
 
     /*
      * above only changed when BQL is held
@@ -111,6 +112,7 @@ bool vfio_user_validate_version(VFIOUserProxy
 *proxy, Error **errp);
 
 VFIOUserFDs *vfio_user_getfds(int numfds);
 void vfio_user_putfds(VFIOUserMsg *msg);
+void vfio_user_free_fds(VFIOUserFDs *fds);
 
 void vfio_user_disable_posted_writes(VFIOUserProxy *proxy);
hw/vfio-user/device.c +9 -4
index 1a01e748cd..4fbf536a04 100644
@@ -14,12 +14,14 @@
 
 #include "hw/vfio-user/device.h"
 #include "hw/vfio-user/trace.h"
+#include "hw/vfio/vfio-region.h"
+#include "hw/vfio/vfio-helpers.h"
+#include "hw/vfio/trace.h"
 
 /*
  * These are to defend against a malign server trying
  * to force us to run out of memory.
  */
-#define VFIO_USER_MAX_REGIONS   100
 #define VFIO_USER_MAX_IRQS      50
 
 bool vfio_user_get_device_info(VFIOUserProxy *proxy,
@@ -209,14 +211,17 @@ static int
 vfio_user_device_io_get_region_info(VFIODevice *vbasedev,
                                                struct
 vfio_region_info *info,
                                                int *fd)
 {
-    VFIOUserFDs fds = { 0, 1, fd};
-    int ret;
+    int fds[VFIO_USER_MAX_MAX_FDS];
+    VFIOUserFDs user_fds = { 0, VFIO_USER_MAX_MAX_FDS, fds };
+    int i, ret;
+
+    *fd = -1;
 
     if (info->index > vbasedev->num_initial_regions) {
         return -EINVAL;
     }
 
-    ret = vfio_user_get_region_info(vbasedev->proxy, info, &fds);
+    ret = vfio_user_get_region_info(vbasedev->proxy, info,
 &user_fds);
     if (ret) {
         return ret;
     }
hw/vfio-user/device.c +63 -2
@@ -225,10 +230,30 @@ static int
 vfio_user_device_io_get_region_info(VFIODevice *vbasedev,
     if ((info->flags & VFIO_REGION_INFO_FLAG_CAPS) &&
         (info->cap_offset < sizeof(*info)
          || info->cap_offset + sizeof(struct vfio_info_cap_header) >
 info->argsz)) {
-        return -EINVAL;
+        ret = -EINVAL;
+    } else if (vfio_get_region_info_cap(info,
+
 VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS)) {
+        if (user_fds.recv_fds > 0) {
+            VFIOUserFDs *saved = vfio_user_getfds(user_fds.recv_fds);
+
+            saved->recv_fds = user_fds.recv_fds;
+            memcpy(saved->fds, fds, user_fds.recv_fds * sizeof(int));
+            vfio_user_free_fds(vbasedev->proxy->region_fds[info-
 >index]);
+            vbasedev->proxy->region_fds[info->index] = saved;
+            return 0;
+        }
+        ret = -EINVAL;
+    } else if (user_fds.recv_fds > 0) {
+        *fd = fds[0];
+        fds[0] = -1;
     }
 
-    return 0;
+    for (i = 0; i < user_fds.recv_fds; i++) {
+        if (fds[i] >= 0) {
+            close(fds[i]);
+        }
+    }
+    return ret;
 }
 
 static int vfio_user_device_io_get_irq_info(VFIODevice *vbasedev,
@@ -518,12 +543,94 @@ static int
 vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
 /*
  * Socket-based io_ops
  */
+static int vfio_user_device_io_setup_sparse_mmaps(VFIORegion *region,
+                                                  struct
 vfio_region_info *info,
+                                                  Error **errp)
+{
+    struct vfio_info_cap_header *hdr;
+    struct vfio_region_info_cap_sparse_mmap_fds *sparse_fds;
+    VFIOUserFDs *user_fds;
+    size_t cap_offset;
+    int i;
+
+    hdr = vfio_get_region_info_cap(info,
 VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS);
+    if (!hdr) {
+        return vfio_default_setup_sparse_mmaps(region, info, errp);
+    }
+
+    if (vfio_get_region_info_cap(info,
 VFIO_REGION_INFO_CAP_SPARSE_MMAP)) {
+        error_setg(errp, "%s: region %d cannot specify both "
+                   "SPARSE_MMAP and SPARSE_MMAP_FDS", __func__,
 region->nr);
+        return -EINVAL;
+    }
+
+    sparse_fds = container_of(hdr, struct
 vfio_region_info_cap_sparse_mmap_fds,
+                              header);
+
+    cap_offset = (uintptr_t)hdr - (uintptr_t)info;
+    if (cap_offset > info->argsz ||
+        info->argsz - cap_offset < sizeof(*sparse_fds) ||
+        sparse_fds->nr_areas >
+            (info->argsz - cap_offset - sizeof(*sparse_fds)) /
+            sizeof(sparse_fds->areas[0])) {
+        error_setg(errp, "%s: invalid nr_areas %u in region %d",
+                   __func__, sparse_fds->nr_areas, region->nr);
+        return -EINVAL;
+    }
+
+    trace_vfio_region_sparse_mmap_header(region->vbasedev->name,
+                                         region->nr, sparse_fds-
 >nr_areas);
+
+    user_fds = region->vbasedev->proxy->region_fds[region->nr];
+    if (!user_fds || user_fds->recv_fds > sparse_fds->nr_areas) {
+        error_setg(errp, "%s: invalid fd count %d for %u sparse mmap

areas "

hw/vfio-user/device.c static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index, · line 593

Quoted 7 lines
hw/vfio-user/device.c +7 -0
+                   "in region %d", __func__,
+                   user_fds ? user_fds->recv_fds : 0,
+                   sparse_fds->nr_areas, region->nr);
+        return -EINVAL;
+    }
+
+    region->mmaps = g_try_new0(VFIOMmap, sparse_fds->nr_areas);

IMO we should impose a limit on sparse_fds->nr_areas, g_try_new0 does prevent the server from crashing but a malicious client can allocate too much memory.

Quoted 182 lines
hw/vfio-user/device.c +2 -0
+    if (!region->mmaps) {
+        error_setg(errp, "%s: failed to allocate %u mmaps for region

%d",

hw/vfio-user/device.c +6 -0
+                   __func__, sparse_fds->nr_areas, region->nr);
+        return -ENOMEM;
+    }
+
+    for (i = 0; i < sparse_fds->nr_areas; i++) {
+        struct vfio_region_sparse_mmap_fd_area *area = &sparse_fds-
Quoted 1 line

areas[i];

hw/vfio-user/device.c +5 -0
+
+        if (area->size == 0 || area->offset > region->size ||
+            area->size > region->size - area->offset ||
+            area->fd_index >= user_fds->recv_fds) {
+            error_setg(errp, "%s: invalid sparse mmap area %d in

region %d "

hw/vfio-user/device.c +3 -0
+                       "(offset=0x%"PRIx64" size=0x%"PRIx64
+                       " fd_offset=0x%"PRIx64" fd_index=%u)",
+                       __func__, i, region->nr, area->offset, area-
Quoted 1 line

size,

hw/vfio-user/device.c +19 -2
+                       area->fd_offset, area->fd_index);
+            g_free(region->mmaps);
+            region->mmaps = NULL;
+            return -EINVAL;
+        }
+
+        trace_vfio_region_sparse_mmap_entry(i, area->offset,
+                                            area->offset + area->size
- 1);
+        region->mmaps[i].offset = area->offset;
+        region->mmaps[i].fd_offset = area->fd_offset;
+        region->mmaps[i].size = area->size;
+        region->mmaps[i].fd = user_fds->fds[area->fd_index];
+    }
+
+    region->nr_mmaps = sparse_fds->nr_areas;
+    return 0;
+}
+
 VFIODeviceIOOps vfio_user_device_io_ops_sock = {
     .device_feature = vfio_user_device_io_device_feature,
     .get_region_info = vfio_user_device_io_get_region_info,
+    .setup_sparse_mmaps = vfio_user_device_io_setup_sparse_mmaps,
     .get_irq_info = vfio_user_device_io_get_irq_info,
     .set_irqs = vfio_user_device_io_set_irqs,
     .region_read = vfio_user_device_io_region_read,
     .region_write = vfio_user_device_io_region_write,
-
 };
hw/vfio-user/proxy.c +3 -0
index 197aee07bf..ba29085321 100644
@@ -363,6 +363,13 @@ static int vfio_user_recv_one(VFIOUserProxy
 *proxy, Error **errp)
         }
 
         memcpy(msg->fds->fds, fdp, numfds * sizeof(int));
+    } else if (msg->fds != NULL) {
+        /*
+         * recv_fds holds the capacity of msg->fds->fds on entry;

reset it
+ * to 0 when the reply carries no FDs so the caller knows
none were

hw/vfio-user/proxy.c +5 -0
+         * received.
+         */
+        msg->fds->recv_fds = 0;
     }
 
     /*
@@ -740,6 +747,11 @@ bool vfio_user_send_wait(VFIOUserProxy *proxy,
 VFIOUserHdr *hdr,
 
     qemu_mutex_unlock(&proxy->lock);
 
+    if (!ok && fds != NULL) {
+        /* No reply was received; report 0 received FDs to the

caller. */

hw/vfio-user/proxy.c +37 -4
+        fds->recv_fds = 0;
+    }
+
     return ok;
 }
@@ -867,17 +879,30 @@ void vfio_user_send_error(VFIOUserProxy *proxy,
 VFIOUserHdr *hdr, int error)
 }
 
 /*
- * Close FDs erroneously received in an incoming request.
+ * Close and free received FDs.
  */
-void vfio_user_putfds(VFIOUserMsg *msg)
+void vfio_user_free_fds(VFIOUserFDs *fds)
 {
-    VFIOUserFDs *fds = msg->fds;
     int i;
 
+    if (!fds) {
+        return;
+    }
+
     for (i = 0; i < fds->recv_fds; i++) {
-        close(fds->fds[i]);
+        if (fds->fds[i] >= 0) {
+            close(fds->fds[i]);
+        }
     }
     g_free(fds);
+}
+
+/*
+ * Close FDs erroneously received in an incoming request.
+ */
+void vfio_user_putfds(VFIOUserMsg *msg)
+{
+    vfio_user_free_fds(msg->fds);
     msg->fds = NULL;
 }
@@ -967,6 +992,7 @@ void vfio_user_set_handler(VFIODevice *vbasedev,
 void vfio_user_disconnect(VFIOUserProxy *proxy)
 {
     VFIOUserMsg *r1, *r2;
+    int i;
 
     qemu_mutex_lock(&proxy->lock);
 
@@ -1027,6 +1053,10 @@ void vfio_user_disconnect(VFIOUserProxy *proxy)
         vfio_user_iothread = NULL;
     }
 
+    for (i = 0; i < VFIO_USER_MAX_REGIONS; i++) {
+        vfio_user_free_fds(proxy->region_fds[i]);
+    }
+
     g_free(proxy->sockname);
     g_free(proxy);
 }
@@ -1214,6 +1244,18 @@ static bool check_multi(VFIOUserProxy *proxy,
 QObject *qobj, Error **errp)
     return true;
 }
 
+static bool check_sparse_mmap_fds(VFIOUserProxy *proxy, QObject
 *qobj,
+                                  Error **errp)
+{
+    QBool *qb = qobject_to(QBool, qobj);
+
+    if (qb == NULL) {
+        error_setg(errp, "malformed %s",
 VFIO_USER_CAP_SPARSE_MMAP_FDS);
+        return false;
+    }
+    return true;
+}
+

static struct cap_entry caps_cap[] = {
{ VFIO_USER_CAP_MAX_FDS, check_max_fds },
{ VFIO_USER_CAP_MAX_XFER, check_max_xfer },

hw/vfio-user/proxy.c +2 -0
@@ -1221,6 +1263,7 @@ static struct cap_entry caps_cap[] = {
     { VFIO_USER_CAP_MAP_MAX, check_max_dma },
     { VFIO_USER_CAP_MIGR, check_migr },
     { VFIO_USER_CAP_MULTI, check_multi },
+    { VFIO_USER_CAP_SPARSE_MMAP_FDS, check_sparse_mmap_fds },
     { NULL }
 };
 
@@ -1280,6 +1323,7 @@ static GString *caps_json(void)
     qdict_put_int(capdict, VFIO_USER_CAP_PGSIZES,
 VFIO_USER_DEF_PGSIZE);
     qdict_put_int(capdict, VFIO_USER_CAP_MAP_MAX,
 VFIO_USER_DEF_MAP_MAX);
     qdict_put_bool(capdict, VFIO_USER_CAP_MULTI, true);
+    qdict_put_bool(capdict, VFIO_USER_CAP_SPARSE_MMAP_FDS, true);
 
     qdict_put_obj(dict, VFIO_USER_CAP, QOBJECT(capdict));

--
2.56.0.385.gd3acb90ef8-goog

#14 lore raw

Re: [PATCH v5 4/4] vfio-user: support multiple FDs for sparse mmap regions

Quoted 252 lines · Thanos Makatos
Quoted 252 lines

-----Original Message-----
From: [email protected] <qemu- [email protected]> On Behalf Of Naman Gulati
Sent: 14 October 2026 22:11
To: [email protected]
Cc: John Levon <[email protected]>; Thanos Makatos <[email protected]>; Cédric Le Goater <[email protected]>; Alex Williamson <[email protected]>; Pierrick Bouvier <[email protected]>; Matt Evans <[email protected]>; Naman Gulati <[email protected]>
Subject: [PATCH v5 4/4] vfio-user: support multiple FDs for sparse mmap regions

!-------------------------------------------------------------------|
CAUTION: External Email

|-------------------------------------------------------------------!

Add client support for capability VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS (capability ID 0x8001) in vfio-user, and advertise support via the "sparse_mmap_fds" capability in VFIO_USER_VERSION.

Update vfio_user_device_io_get_region_info() to receive multiple file descriptors passed in SCM_RIGHTS ancillary data for a region, store them
in VFIOUserProxy, and implement setup_sparse_mmaps in vfio_user_device_io_ops_sock to validate and populate per-area file descriptors and fd_offset values.

---
 hw/vfio-user/protocol.h |  35 ++++++++++--
 hw/vfio-user/proxy.h    |   2 +
 hw/vfio-user/device.c   | 121 +++++++++++++++++++++++++++++++++++++--
(quoted diff) +0 -1
-
 hw/vfio-user/proxy.c    |  52 +++++++++++++++--
 4 files changed, 193 insertions(+), 17 deletions(-)
hw/vfio-user/protocol.h +29 -6
index c478d1353f..b823689254 100644
@@ -15,6 +15,8 @@
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
+#define VFIO_USER_MAX_REGIONS 100
+
 typedef struct {
     uint16_t id;
     uint16_t command;
@@ -69,12 +71,13 @@ typedef struct {
 #define VFIO_USER_CAP           "capabilities"
 
 /* "capabilities" members */
-#define VFIO_USER_CAP_MAX_FDS   "max_msg_fds"
-#define VFIO_USER_CAP_MAX_XFER  "max_data_xfer_size"
-#define VFIO_USER_CAP_PGSIZES   "pgsizes"
-#define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
-#define VFIO_USER_CAP_MIGR      "migration"
-#define VFIO_USER_CAP_MULTI     "write_multiple"
+#define VFIO_USER_CAP_MAX_FDS           "max_msg_fds"
+#define VFIO_USER_CAP_MAX_XFER          "max_data_xfer_size"
+#define VFIO_USER_CAP_PGSIZES           "pgsizes"
+#define VFIO_USER_CAP_MAP_MAX           "max_dma_maps"
+#define VFIO_USER_CAP_MIGR              "migration"
+#define VFIO_USER_CAP_MULTI             "write_multiple"
+#define VFIO_USER_CAP_SPARSE_MMAP_FDS   "sparse_mmap_fds"
 
 /* "migration" members */
 #define VFIO_USER_CAP_PGSIZE            "pgsize"
@@ -166,6 +169,26 @@ typedef struct {
     uint64_t offset;
 } VFIOUserRegionInfo;
 
+/*
+ * VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS
+ */
+#define VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS 0x8001
+
+struct vfio_region_sparse_mmap_fd_area {
+    uint64_t offset;
+    uint64_t fd_offset;
+    uint64_t size;
+    uint32_t fd_index;
+    uint32_t pad;
+};
+
+struct vfio_region_info_cap_sparse_mmap_fds {
+    struct vfio_info_cap_header header;
+    uint32_t nr_areas;
+    uint32_t reserved;
+    struct vfio_region_sparse_mmap_fd_area areas[];
+};
+
 /*
  * VFIO_USER_DEVICE_GET_IRQ_INFO
  * imported from struct vfio_irq_info
hw/vfio-user/proxy.h +2 -0
index 7b97460cc5..4b9834a436 100644
@@ -72,6 +72,7 @@ typedef struct VFIOUserProxy {
     AioContext *ctx;
     QEMUBH *req_bh;
     bool async_ops;
+    VFIOUserFDs *region_fds[VFIO_USER_MAX_REGIONS];
 
     /*
      * above only changed when BQL is held
@@ -111,6 +112,7 @@ bool vfio_user_validate_version(VFIOUserProxy
 *proxy, Error **errp);
 
 VFIOUserFDs *vfio_user_getfds(int numfds);
 void vfio_user_putfds(VFIOUserMsg *msg);
+void vfio_user_free_fds(VFIOUserFDs *fds);
 
 void vfio_user_disable_posted_writes(VFIOUserProxy *proxy);
hw/vfio-user/device.c +9 -4
index 1a01e748cd..4fbf536a04 100644
@@ -14,12 +14,14 @@
 
 #include "hw/vfio-user/device.h"
 #include "hw/vfio-user/trace.h"
+#include "hw/vfio/vfio-region.h"
+#include "hw/vfio/vfio-helpers.h"
+#include "hw/vfio/trace.h"
 
 /*
  * These are to defend against a malign server trying
  * to force us to run out of memory.
  */
-#define VFIO_USER_MAX_REGIONS   100
 #define VFIO_USER_MAX_IRQS      50
 
 bool vfio_user_get_device_info(VFIOUserProxy *proxy,
@@ -209,14 +211,17 @@ static int
 vfio_user_device_io_get_region_info(VFIODevice *vbasedev,
                                                struct
 vfio_region_info *info,
                                                int *fd)
 {
-    VFIOUserFDs fds = { 0, 1, fd};
-    int ret;
+    int fds[VFIO_USER_MAX_MAX_FDS];
+    VFIOUserFDs user_fds = { 0, VFIO_USER_MAX_MAX_FDS, fds };
+    int i, ret;
+
+    *fd = -1;
 
     if (info->index > vbasedev->num_initial_regions) {
         return -EINVAL;
     }
 
-    ret = vfio_user_get_region_info(vbasedev->proxy, info, &fds);
+    ret = vfio_user_get_region_info(vbasedev->proxy, info,
 &user_fds);
     if (ret) {
         return ret;
     }
hw/vfio-user/device.c +59 -2
@@ -225,10 +230,30 @@ static int
 vfio_user_device_io_get_region_info(VFIODevice *vbasedev,
     if ((info->flags & VFIO_REGION_INFO_FLAG_CAPS) &&
         (info->cap_offset < sizeof(*info)
          || info->cap_offset + sizeof(struct vfio_info_cap_header) >
 info->argsz)) {
-        return -EINVAL;
+        ret = -EINVAL;
+    } else if (vfio_get_region_info_cap(info,
+
 VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS)) {
+        if (user_fds.recv_fds > 0) {
+            VFIOUserFDs *saved = vfio_user_getfds(user_fds.recv_fds);
+
+            saved->recv_fds = user_fds.recv_fds;
+            memcpy(saved->fds, fds, user_fds.recv_fds * sizeof(int));
+            vfio_user_free_fds(vbasedev->proxy->region_fds[info-
 >index]);
+            vbasedev->proxy->region_fds[info->index] = saved;
+            return 0;
+        }
+        ret = -EINVAL;
+    } else if (user_fds.recv_fds > 0) {
+        *fd = fds[0];
+        fds[0] = -1;
     }
 
-    return 0;
+    for (i = 0; i < user_fds.recv_fds; i++) {
+        if (fds[i] >= 0) {
+            close(fds[i]);
+        }
+    }
+    return ret;
 }
 
 static int vfio_user_device_io_get_irq_info(VFIODevice *vbasedev,
@@ -518,12 +543,94 @@ static int
 vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
 /*
  * Socket-based io_ops
  */
+static int vfio_user_device_io_setup_sparse_mmaps(VFIORegion *region,
+                                                  struct
 vfio_region_info *info,
+                                                  Error **errp)
+{
+    struct vfio_info_cap_header *hdr;
+    struct vfio_region_info_cap_sparse_mmap_fds *sparse_fds;
+    VFIOUserFDs *user_fds;
+    size_t cap_offset;
+    int i;
+
+    hdr = vfio_get_region_info_cap(info,
 VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS);
+    if (!hdr) {
+        return vfio_default_setup_sparse_mmaps(region, info, errp);
+    }
+
+    if (vfio_get_region_info_cap(info,
 VFIO_REGION_INFO_CAP_SPARSE_MMAP)) {
+        error_setg(errp, "%s: region %d cannot specify both "
+                   "SPARSE_MMAP and SPARSE_MMAP_FDS", __func__,
 region->nr);
+        return -EINVAL;
+    }
+
+    sparse_fds = container_of(hdr, struct
 vfio_region_info_cap_sparse_mmap_fds,
+                              header);
+
+    cap_offset = (uintptr_t)hdr - (uintptr_t)info;
+    if (cap_offset > info->argsz ||
+        info->argsz - cap_offset < sizeof(*sparse_fds) ||
+        sparse_fds->nr_areas >
+            (info->argsz - cap_offset - sizeof(*sparse_fds)) /
+            sizeof(sparse_fds->areas[0])) {
+        error_setg(errp, "%s: invalid nr_areas %u in region %d",
+                   __func__, sparse_fds->nr_areas, region->nr);
+        return -EINVAL;
+    }
+
+    trace_vfio_region_sparse_mmap_header(region->vbasedev->name,
+                                         region->nr, sparse_fds-
 >nr_areas);
Quoted 14 lines

hw/vfio-user/device.c static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index, · line 593

Quoted 12 lines
hw/vfio-user/device.c +4 -0
+
+    user_fds = region->vbasedev->proxy->region_fds[region->nr];
+    if (!user_fds || user_fds->recv_fds > sparse_fds->nr_areas) {
+        error_setg(errp, "%s: invalid fd count %d for %u sparse mmap

areas "

hw/vfio-user/device.c +7 -0
+                   "in region %d", __func__,
+                   user_fds ? user_fds->recv_fds : 0,
+                   sparse_fds->nr_areas, region->nr);
+        return -EINVAL;
+    }
+
+    region->mmaps = g_try_new0(VFIOMmap, sparse_fds->nr_areas);

IMO we should impose a limit on sparse_fds->nr_areas, g_try_new0 does prevent the server from crashing but a malicious client can allocate too much memory.

sparse_fds->nr_areas is already bounded by info->argsz via the check just above:

if (cap_offset > info->argsz ||

        info->argsz - cap_offset < sizeof(*sparse_fds) ||
        sparse_fds->nr_areas >
            (info->argsz - cap_offset - sizeof(*sparse_fds)) /
            sizeof(sparse_fds->areas[0])) {
        error_setg(errp, "%s: invalid nr_areas %u in region %d",
                   __func__, sparse_fds->nr_areas, region->nr);
        return -EINVAL;
    }

And in vfio_user_get_region_info(), info->argsz is capped by proxy->max_xfer_size (at most VFIO_USER_DEF_MAX_XFER = 1 MiB), so sparse_fds->nr_areas cannot exceed ~32K entries (the same bound that applies to VFIO_REGION_INFO_CAP_SPARSE_MMAP in vfio_region_setup_sparse_mmaps()).

Does that existing bound look sufficient to you, or would you still prefer an explicit smaller limit (e.g. a VFIO_USER_MAX_SPARSE_MMAPS constant alongside VFIO_USER_MAX_REGIONS)?

Quoted 184 lines
Quoted 183 lines
hw/vfio-user/device.c +2 -0
+    if (!region->mmaps) {
+        error_setg(errp, "%s: failed to allocate %u mmaps for region

%d",

hw/vfio-user/device.c +6 -0
+                   __func__, sparse_fds->nr_areas, region->nr);
+        return -ENOMEM;
+    }
+
+    for (i = 0; i < sparse_fds->nr_areas; i++) {
+        struct vfio_region_sparse_mmap_fd_area *area = &sparse_fds-
Quoted 1 line

areas[i];

hw/vfio-user/device.c +5 -0
+
+        if (area->size == 0 || area->offset > region->size ||
+            area->size > region->size - area->offset ||
+            area->fd_index >= user_fds->recv_fds) {
+            error_setg(errp, "%s: invalid sparse mmap area %d in

region %d "

hw/vfio-user/device.c +3 -0
+                       "(offset=0x%"PRIx64" size=0x%"PRIx64
+                       " fd_offset=0x%"PRIx64" fd_index=%u)",
+                       __func__, i, region->nr, area->offset, area-
Quoted 1 line

size,

hw/vfio-user/device.c +19 -2
+                       area->fd_offset, area->fd_index);
+            g_free(region->mmaps);
+            region->mmaps = NULL;
+            return -EINVAL;
+        }
+
+        trace_vfio_region_sparse_mmap_entry(i, area->offset,
+                                            area->offset + area->size
- 1);
+        region->mmaps[i].offset = area->offset;
+        region->mmaps[i].fd_offset = area->fd_offset;
+        region->mmaps[i].size = area->size;
+        region->mmaps[i].fd = user_fds->fds[area->fd_index];
+    }
+
+    region->nr_mmaps = sparse_fds->nr_areas;
+    return 0;
+}
+
 VFIODeviceIOOps vfio_user_device_io_ops_sock = {
     .device_feature = vfio_user_device_io_device_feature,
     .get_region_info = vfio_user_device_io_get_region_info,
+    .setup_sparse_mmaps = vfio_user_device_io_setup_sparse_mmaps,
     .get_irq_info = vfio_user_device_io_get_irq_info,
     .set_irqs = vfio_user_device_io_set_irqs,
     .region_read = vfio_user_device_io_region_read,
     .region_write = vfio_user_device_io_region_write,
-
 };
hw/vfio-user/proxy.c +3 -0
index 197aee07bf..ba29085321 100644
@@ -363,6 +363,13 @@ static int vfio_user_recv_one(VFIOUserProxy
 *proxy, Error **errp)
         }
 
         memcpy(msg->fds->fds, fdp, numfds * sizeof(int));
+    } else if (msg->fds != NULL) {
+        /*
+         * recv_fds holds the capacity of msg->fds->fds on entry;

reset it
+ * to 0 when the reply carries no FDs so the caller knows
none were

hw/vfio-user/proxy.c +5 -0
+         * received.
+         */
+        msg->fds->recv_fds = 0;
     }
 
     /*
@@ -740,6 +747,11 @@ bool vfio_user_send_wait(VFIOUserProxy *proxy,
 VFIOUserHdr *hdr,
 
     qemu_mutex_unlock(&proxy->lock);
 
+    if (!ok && fds != NULL) {
+        /* No reply was received; report 0 received FDs to the

caller. */

hw/vfio-user/proxy.c +37 -4
+        fds->recv_fds = 0;
+    }
+
     return ok;
 }
@@ -867,17 +879,30 @@ void vfio_user_send_error(VFIOUserProxy *proxy,
 VFIOUserHdr *hdr, int error)
 }
 
 /*
- * Close FDs erroneously received in an incoming request.
+ * Close and free received FDs.
  */
-void vfio_user_putfds(VFIOUserMsg *msg)
+void vfio_user_free_fds(VFIOUserFDs *fds)
 {
-    VFIOUserFDs *fds = msg->fds;
     int i;
 
+    if (!fds) {
+        return;
+    }
+
     for (i = 0; i < fds->recv_fds; i++) {
-        close(fds->fds[i]);
+        if (fds->fds[i] >= 0) {
+            close(fds->fds[i]);
+        }
     }
     g_free(fds);
+}
+
+/*
+ * Close FDs erroneously received in an incoming request.
+ */
+void vfio_user_putfds(VFIOUserMsg *msg)
+{
+    vfio_user_free_fds(msg->fds);
     msg->fds = NULL;
 }
@@ -967,6 +992,7 @@ void vfio_user_set_handler(VFIODevice *vbasedev,
 void vfio_user_disconnect(VFIOUserProxy *proxy)
 {
     VFIOUserMsg *r1, *r2;
+    int i;
 
     qemu_mutex_lock(&proxy->lock);
 
@@ -1027,6 +1053,10 @@ void vfio_user_disconnect(VFIOUserProxy *proxy)
         vfio_user_iothread = NULL;
     }
 
+    for (i = 0; i < VFIO_USER_MAX_REGIONS; i++) {
+        vfio_user_free_fds(proxy->region_fds[i]);
+    }
+
     g_free(proxy->sockname);
     g_free(proxy);
 }
@@ -1214,6 +1244,18 @@ static bool check_multi(VFIOUserProxy *proxy,
 QObject *qobj, Error **errp)
     return true;
 }
 
+static bool check_sparse_mmap_fds(VFIOUserProxy *proxy, QObject
 *qobj,
+                                  Error **errp)
+{
+    QBool *qb = qobject_to(QBool, qobj);
+
+    if (qb == NULL) {
+        error_setg(errp, "malformed %s",
 VFIO_USER_CAP_SPARSE_MMAP_FDS);
+        return false;
+    }
+    return true;
+}
+

static struct cap_entry caps_cap[] = {
{ VFIO_USER_CAP_MAX_FDS, check_max_fds },
{ VFIO_USER_CAP_MAX_XFER, check_max_xfer },

hw/vfio-user/proxy.c +2 -0
@@ -1221,6 +1263,7 @@ static struct cap_entry caps_cap[] = {
     { VFIO_USER_CAP_MAP_MAX, check_max_dma },
     { VFIO_USER_CAP_MIGR, check_migr },
     { VFIO_USER_CAP_MULTI, check_multi },
+    { VFIO_USER_CAP_SPARSE_MMAP_FDS, check_sparse_mmap_fds },
     { NULL }
 };
 
@@ -1280,6 +1323,7 @@ static GString *caps_json(void)
     qdict_put_int(capdict, VFIO_USER_CAP_PGSIZES,
 VFIO_USER_DEF_PGSIZE);
     qdict_put_int(capdict, VFIO_USER_CAP_MAP_MAX,
 VFIO_USER_DEF_MAP_MAX);
     qdict_put_bool(capdict, VFIO_USER_CAP_MULTI, true);
+    qdict_put_bool(capdict, VFIO_USER_CAP_SPARSE_MMAP_FDS, true);
 
     qdict_put_obj(dict, VFIO_USER_CAP, QOBJECT(capdict));

--
2.56.0.385.gd3acb90ef8-goog

Reviewed-by
#15 lore raw

RE: [PATCH v5 4/4] vfio-user: support multiple FDs for sparse mmap regions

Quoted 322 lines

-----Original Message-----
From: Naman Gulati <[email protected]>
Sent: 23 October 2026 22:42
To: Thanos Makatos <[email protected]>
Cc: [email protected]; John Levon <[email protected]>; Cédric Le Goater <[email protected]>; Alex Williamson <[email protected]>; Pierrick Bouvier <[email protected]>; Matt Evans <[email protected]>
Subject: Re: [PATCH v5 4/4] vfio-user: support multiple FDs for sparse mmap regions

!-------------------------------------------------------------------|
CAUTION: External Email

|-------------------------------------------------------------------!

Quoted 5 lines · Thanos Makatos
Quoted 5 lines

-----Original Message-----
From: [email protected]

<qemu-

Quoted 6 lines
Quoted 6 lines

[email protected]> On Behalf Of Naman Gulati
Sent: 14 October 2026 22:11
To: [email protected]
Cc: John Levon <[email protected]>; Thanos Makatos <[email protected]>; Cédric Le Goater <[email protected]>;

Alex

Quoted 7 lines
Quoted 7 lines

Williamson <[email protected]>; Pierrick Bouvier <[email protected]>; Matt Evans <[email protected]>; Naman Gulati <[email protected]>
Subject: [PATCH v5 4/4] vfio-user: support multiple FDs for sparse mmap regions

!-----------------------------------------------------------------

--|

Quoted 3 lines
Quoted 3 lines

CAUTION: External Email

|-----------------------------------------------------------------

--!

Quoted 2 lines
Quoted 2 lines

Add client support for capability

VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS

Quoted 4 lines
Quoted 4 lines

(capability ID 0x8001) in vfio-user, and advertise support via the "sparse_mmap_fds" capability in VFIO_USER_VERSION.

Update vfio_user_device_io_get_region_info() to receive multiple

file

Quoted 1 line
Quoted 1 line

descriptors passed in SCM_RIGHTS ancillary data for a region,

store

Quoted 3 lines
Quoted 3 lines

them
in VFIOUserProxy, and implement setup_sparse_mmaps in vfio_user_device_io_ops_sock to validate and populate per-area

file

Quoted 7 lines
Quoted 7 lines

descriptors and fd_offset values.

---
 hw/vfio-user/protocol.h |  35 ++++++++++--
 hw/vfio-user/proxy.h    |   2 +
 hw/vfio-user/device.c   | 121

+++++++++++++++++++++++++++++++++++++--

Quoted 123 lines
Quoted 123 lines
(quoted diff) +0 -1
-
 hw/vfio-user/proxy.c    |  52 +++++++++++++++--
 4 files changed, 193 insertions(+), 17 deletions(-)
hw/vfio-user/protocol.h +29 -6
index c478d1353f..b823689254 100644
@@ -15,6 +15,8 @@
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
+#define VFIO_USER_MAX_REGIONS 100
+
 typedef struct {
     uint16_t id;
     uint16_t command;
@@ -69,12 +71,13 @@ typedef struct {
 #define VFIO_USER_CAP           "capabilities"
 
 /* "capabilities" members */
-#define VFIO_USER_CAP_MAX_FDS   "max_msg_fds"
-#define VFIO_USER_CAP_MAX_XFER  "max_data_xfer_size"
-#define VFIO_USER_CAP_PGSIZES   "pgsizes"
-#define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
-#define VFIO_USER_CAP_MIGR      "migration"
-#define VFIO_USER_CAP_MULTI     "write_multiple"
+#define VFIO_USER_CAP_MAX_FDS           "max_msg_fds"
+#define VFIO_USER_CAP_MAX_XFER          "max_data_xfer_size"
+#define VFIO_USER_CAP_PGSIZES           "pgsizes"
+#define VFIO_USER_CAP_MAP_MAX           "max_dma_maps"
+#define VFIO_USER_CAP_MIGR              "migration"
+#define VFIO_USER_CAP_MULTI             "write_multiple"
+#define VFIO_USER_CAP_SPARSE_MMAP_FDS   "sparse_mmap_fds"
 
 /* "migration" members */
 #define VFIO_USER_CAP_PGSIZE            "pgsize"
@@ -166,6 +169,26 @@ typedef struct {
     uint64_t offset;
 } VFIOUserRegionInfo;
 
+/*
+ * VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS
+ */
+#define VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS 0x8001
+
+struct vfio_region_sparse_mmap_fd_area {
+    uint64_t offset;
+    uint64_t fd_offset;
+    uint64_t size;
+    uint32_t fd_index;
+    uint32_t pad;
+};
+
+struct vfio_region_info_cap_sparse_mmap_fds {
+    struct vfio_info_cap_header header;
+    uint32_t nr_areas;
+    uint32_t reserved;
+    struct vfio_region_sparse_mmap_fd_area areas[];
+};
+
 /*
  * VFIO_USER_DEVICE_GET_IRQ_INFO
  * imported from struct vfio_irq_info
hw/vfio-user/proxy.h +2 -0
index 7b97460cc5..4b9834a436 100644
@@ -72,6 +72,7 @@ typedef struct VFIOUserProxy {
     AioContext *ctx;
     QEMUBH *req_bh;
     bool async_ops;
+    VFIOUserFDs *region_fds[VFIO_USER_MAX_REGIONS];
 
     /*
      * above only changed when BQL is held
@@ -111,6 +112,7 @@ bool vfio_user_validate_version(VFIOUserProxy
 *proxy, Error **errp);
 
 VFIOUserFDs *vfio_user_getfds(int numfds);
 void vfio_user_putfds(VFIOUserMsg *msg);
+void vfio_user_free_fds(VFIOUserFDs *fds);
 
 void vfio_user_disable_posted_writes(VFIOUserProxy *proxy);
hw/vfio-user/device.c +8 -3
index 1a01e748cd..4fbf536a04 100644
@@ -14,12 +14,14 @@
 
 #include "hw/vfio-user/device.h"
 #include "hw/vfio-user/trace.h"
+#include "hw/vfio/vfio-region.h"
+#include "hw/vfio/vfio-helpers.h"
+#include "hw/vfio/trace.h"
 
 /*
  * These are to defend against a malign server trying
  * to force us to run out of memory.
  */
-#define VFIO_USER_MAX_REGIONS   100
 #define VFIO_USER_MAX_IRQS      50
 
 bool vfio_user_get_device_info(VFIOUserProxy *proxy,
@@ -209,14 +211,17 @@ static int
 vfio_user_device_io_get_region_info(VFIODevice *vbasedev,
                                                struct
 vfio_region_info *info,
                                                int *fd)
 {
-    VFIOUserFDs fds = { 0, 1, fd};
-    int ret;
+    int fds[VFIO_USER_MAX_MAX_FDS];
+    VFIOUserFDs user_fds = { 0, VFIO_USER_MAX_MAX_FDS, fds };
+    int i, ret;
+
+    *fd = -1;
 
     if (info->index > vbasedev->num_initial_regions) {
         return -EINVAL;
     }
 
Quoted 11 lines

hw/vfio-user/device.c static int · line 233

Quoted 11 lines
hw/vfio-user/device.c +1 -1
-    ret = vfio_user_get_region_info(vbasedev->proxy, info, &fds);
+    ret = vfio_user_get_region_info(vbasedev->proxy, info,
 &user_fds);
     if (ret) {
         return ret;
     }
hw/vfio-user/device.c +0 -0
@@ -225,10 +230,30 @@ static int
 vfio_user_device_io_get_region_info(VFIODevice *vbasedev,
     if ((info->flags & VFIO_REGION_INFO_FLAG_CAPS) &&
         (info->cap_offset < sizeof(*info)
          || info->cap_offset + sizeof(struct

vfio_info_cap_header) >

Quoted 8 lines

hw/vfio-user/device.c static int vfio_user_device_io_get_region_info(VFIODevice *vbasedev,

Quoted 8 lines
hw/vfio-user/device.c +5 -1
 info->argsz)) {
-        return -EINVAL;
+        ret = -EINVAL;
+    } else if (vfio_get_region_info_cap(info,
+
 VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS)) {
+        if (user_fds.recv_fds > 0) {
+            VFIOUserFDs *saved =

vfio_user_getfds(user_fds.recv_fds);

Quoted 3 lines

hw/vfio-user/device.c static int vfio_user_device_io_get_region_info(VFIODevice *vbasedev,

Quoted 3 lines
hw/vfio-user/device.c +3 -0
+
+            saved->recv_fds = user_fds.recv_fds;
+            memcpy(saved->fds, fds, user_fds.recv_fds *

sizeof(int));

Quoted 20 lines
Quoted 20 lines
hw/vfio-user/device.c +14 -1
+            vfio_user_free_fds(vbasedev->proxy->region_fds[info-
 >index]);
+            vbasedev->proxy->region_fds[info->index] = saved;
+            return 0;
+        }
+        ret = -EINVAL;
+    } else if (user_fds.recv_fds > 0) {
+        *fd = fds[0];
+        fds[0] = -1;
     }
 
-    return 0;
+    for (i = 0; i < user_fds.recv_fds; i++) {
+        if (fds[i] >= 0) {
+            close(fds[i]);
+        }
+    }
+    return ret;
 }
Quoted 3 lines
Quoted 3 lines

static int vfio_user_device_io_get_irq_info(VFIODevice *vbasedev,

hw/vfio-user/device.c +0 -0
@@ -518,12 +543,94 @@ static int

vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t

index,

Quoted 4 lines
Quoted 4 lines

/*
* Socket-based io_ops
*/
+static int vfio_user_device_io_setup_sparse_mmaps(VFIORegion

*region,

Quoted 6 lines
Quoted 6 lines

+ struct
vfio_region_info *info,

hw/vfio-user/device.c +4 -0
+                                                  Error **errp)
+{
+    struct vfio_info_cap_header *hdr;
+    struct vfio_region_info_cap_sparse_mmap_fds *sparse_fds;
Quoted 8 lines

hw/vfio-user/device.c

Quoted 8 lines
hw/vfio-user/device.c +5 -0
+    VFIOUserFDs *user_fds;
+    size_t cap_offset;
+    int i;
+
+    hdr = vfio_get_region_info_cap(info,

VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS);

hw/vfio-user/device.c +2 -0
+    if (!hdr) {
+        return vfio_default_setup_sparse_mmaps(region, info,

errp);

Quoted 27 lines
Quoted 27 lines
hw/vfio-user/device.c +3 -0
+    }
+
+    if (vfio_get_region_info_cap(info,

VFIO_REGION_INFO_CAP_SPARSE_MMAP)) {

hw/vfio-user/device.c +2 -0
+        error_setg(errp, "%s: region %d cannot specify both "
+                   "SPARSE_MMAP and SPARSE_MMAP_FDS", __func__,

region->nr);

hw/vfio-user/device.c +4 -0
+        return -EINVAL;
+    }
+
+    sparse_fds = container_of(hdr, struct

vfio_region_info_cap_sparse_mmap_fds,

hw/vfio-user/device.c +15 -0
+                              header);
+
+    cap_offset = (uintptr_t)hdr - (uintptr_t)info;
+    if (cap_offset > info->argsz ||
+        info->argsz - cap_offset < sizeof(*sparse_fds) ||
+        sparse_fds->nr_areas >
+            (info->argsz - cap_offset - sizeof(*sparse_fds)) /
+            sizeof(sparse_fds->areas[0])) {
+        error_setg(errp, "%s: invalid nr_areas %u in region %d",
+                   __func__, sparse_fds->nr_areas, region->nr);
+        return -EINVAL;
+    }
+
+    trace_vfio_region_sparse_mmap_header(region->vbasedev->name,
+                                         region->nr, sparse_fds-
Quoted 5 lines

hw/vfio-user/device.c static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,

Quoted 5 lines
Quoted 1 line

nr_areas);

hw/vfio-user/device.c +4 -0
+
+    user_fds = region->vbasedev->proxy->region_fds[region->nr];
+    if (!user_fds || user_fds->recv_fds > sparse_fds->nr_areas) {
+        error_setg(errp, "%s: invalid fd count %d for %u sparse

mmap

Quoted 10 lines

hw/vfio-user/device.c static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index, · line 593

Quoted 8 lines

areas "

hw/vfio-user/device.c +7 -0
+                   "in region %d", __func__,
+                   user_fds ? user_fds->recv_fds : 0,
+                   sparse_fds->nr_areas, region->nr);
+        return -EINVAL;
+    }
+
+    region->mmaps = g_try_new0(VFIOMmap, sparse_fds->nr_areas);

IMO we should impose a limit on sparse_fds->nr_areas, g_try_new0

does prevent the server from crashing but a malicious client can allocate too much memory.

sparse_fds->nr_areas is already bounded by info->argsz via the check just above:

if (cap_offset > info->argsz ||

        info->argsz - cap_offset < sizeof(*sparse_fds) ||
        sparse_fds->nr_areas >
            (info->argsz - cap_offset - sizeof(*sparse_fds)) /
            sizeof(sparse_fds->areas[0])) {
        error_setg(errp, "%s: invalid nr_areas %u in region %d",
                   __func__, sparse_fds->nr_areas, region->nr);
        return -EINVAL;
    }

And in vfio_user_get_region_info(), info->argsz is capped by proxy->max_xfer_size (at most VFIO_USER_DEF_MAX_XFER = 1 MiB), so sparse_fds->nr_areas cannot exceed ~32K entries (the same bound that applies to VFIO_REGION_INFO_CAP_SPARSE_MMAP in vfio_region_setup_sparse_mmaps()).

Quoted 3 lines

Does that existing bound look sufficient to you, or would you still prefer an explicit smaller limit (e.g. a VFIO_USER_MAX_SPARSE_MMAPS constant alongside VFIO_USER_MAX_REGIONS)?

That's fine, thanks.

Quoted 194 lines
Quoted 4 lines

hw/vfio-user/device.c

Quoted 4 lines
hw/vfio-user/device.c +2 -0
+    if (!region->mmaps) {
+        error_setg(errp, "%s: failed to allocate %u mmaps for

region

Quoted 7 lines

hw/vfio-user/device.c static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,

Quoted 7 lines

%d",

hw/vfio-user/device.c +6 -0
+                   __func__, sparse_fds->nr_areas, region->nr);
+        return -ENOMEM;
+    }
+
+    for (i = 0; i < sparse_fds->nr_areas; i++) {
+        struct vfio_region_sparse_mmap_fd_area *area =

&sparse_fds-

Quoted 10 lines

hw/vfio-user/device.c static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,

Quoted 10 lines
Quoted 1 line

areas[i];

hw/vfio-user/device.c +5 -0
+
+        if (area->size == 0 || area->offset > region->size ||
+            area->size > region->size - area->offset ||
+            area->fd_index >= user_fds->recv_fds) {
+            error_setg(errp, "%s: invalid sparse mmap area %d in

region %d "

hw/vfio-user/device.c +3 -0
+                       "(offset=0x%"PRIx64" size=0x%"PRIx64
+                       " fd_offset=0x%"PRIx64" fd_index=%u)",
+                       __func__, i, region->nr, area->offset,

area-

Quoted 51 lines

hw/vfio-user/device.c static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,

Quoted 9 lines
Quoted 1 line

size,

hw/vfio-user/device.c +8 -0
+                       area->fd_offset, area->fd_index);
+            g_free(region->mmaps);
+            region->mmaps = NULL;
+            return -EINVAL;
+        }
+
+        trace_vfio_region_sparse_mmap_entry(i, area->offset,
+                                            area->offset + area-

size

Quoted 41 lines

- 1);

hw/vfio-user/device.c +11 -1
+        region->mmaps[i].offset = area->offset;
+        region->mmaps[i].fd_offset = area->fd_offset;
+        region->mmaps[i].size = area->size;
+        region->mmaps[i].fd = user_fds->fds[area->fd_index];
+    }
+
+    region->nr_mmaps = sparse_fds->nr_areas;
+    return 0;
+}
+
 VFIODeviceIOOps vfio_user_device_io_ops_sock = {
     .device_feature = vfio_user_device_io_device_feature,
     .get_region_info = vfio_user_device_io_get_region_info,
+    .setup_sparse_mmaps = vfio_user_device_io_setup_sparse_mmaps,
     .get_irq_info = vfio_user_device_io_get_irq_info,
     .set_irqs = vfio_user_device_io_set_irqs,
     .region_read = vfio_user_device_io_region_read,
     .region_write = vfio_user_device_io_region_write,
-
 };
hw/vfio-user/proxy.c +3 -0
index 197aee07bf..ba29085321 100644
@@ -363,6 +363,13 @@ static int vfio_user_recv_one(VFIOUserProxy
 *proxy, Error **errp)
         }
 
         memcpy(msg->fds->fds, fdp, numfds * sizeof(int));
+    } else if (msg->fds != NULL) {
+        /*
+         * recv_fds holds the capacity of msg->fds->fds on entry;

reset it
+ * to 0 when the reply carries no FDs so the caller knows
none were

hw/vfio-user/proxy.c +3 -0
+         * received.
+         */
+        msg->fds->recv_fds = 0;
     }
Quoted 2 lines

hw/vfio-user/proxy.c bool vfio_user_send_wait(VFIOUserProxy

Quoted 2 lines
     /*
hw/vfio-user/proxy.c +0 -0
@@ -740,6 +747,11 @@ bool vfio_user_send_wait(VFIOUserProxy

*proxy,

Quoted 13 lines
Quoted 13 lines
hw/vfio-user/proxy.c +2 -0
 VFIOUserHdr *hdr,
 
     qemu_mutex_unlock(&proxy->lock);
 
+    if (!ok && fds != NULL) {
+        /* No reply was received; report 0 received FDs to the

caller. */

hw/vfio-user/proxy.c +3 -0
+        fds->recv_fds = 0;
+    }
+
     return ok;
 }
Quoted 1 line

hw/vfio-user/proxy.c void vfio_user_send_error(VFIOUserProxy

Quoted 1 line
hw/vfio-user/proxy.c +0 -0
@@ -867,17 +879,30 @@ void vfio_user_send_error(VFIOUserProxy

*proxy,

Quoted 35 lines
Quoted 35 lines
hw/vfio-user/proxy.c +17 -4
 VFIOUserHdr *hdr, int error)
 }
 
 /*
- * Close FDs erroneously received in an incoming request.
+ * Close and free received FDs.
  */
-void vfio_user_putfds(VFIOUserMsg *msg)
+void vfio_user_free_fds(VFIOUserFDs *fds)
 {
-    VFIOUserFDs *fds = msg->fds;
     int i;
 
+    if (!fds) {
+        return;
+    }
+
     for (i = 0; i < fds->recv_fds; i++) {
-        close(fds->fds[i]);
+        if (fds->fds[i] >= 0) {
+            close(fds->fds[i]);
+        }
     }
     g_free(fds);
+}
+
+/*
+ * Close FDs erroneously received in an incoming request.
+ */
+void vfio_user_putfds(VFIOUserMsg *msg)
+{
+    vfio_user_free_fds(msg->fds);
     msg->fds = NULL;
 }
Quoted 1 line

hw/vfio-user/proxy.c void vfio_user_set_handler(VFIODevice

Quoted 1 line
hw/vfio-user/proxy.c +0 -0
@@ -967,6 +992,7 @@ void vfio_user_set_handler(VFIODevice

*vbasedev,

Quoted 8 lines

hw/vfio-user/proxy.c void vfio_user_disconnect(VFIOUserProxy

Quoted 8 lines
hw/vfio-user/proxy.c +1 -0
 void vfio_user_disconnect(VFIOUserProxy *proxy)
 {
     VFIOUserMsg *r1, *r2;
+    int i;
 
     qemu_mutex_lock(&proxy->lock);
 
@@ -1027,6 +1053,10 @@ void vfio_user_disconnect(VFIOUserProxy

*proxy)

Quoted 11 lines

hw/vfio-user/proxy.c static bool check_multi(VFIOUserProxy

Quoted 11 lines
hw/vfio-user/proxy.c +4 -0
         vfio_user_iothread = NULL;
     }
 
+    for (i = 0; i < VFIO_USER_MAX_REGIONS; i++) {
+        vfio_user_free_fds(proxy->region_fds[i]);
+    }
+
     g_free(proxy->sockname);
     g_free(proxy);
 }
@@ -1214,6 +1244,18 @@ static bool check_multi(VFIOUserProxy

*proxy,

Quoted 43 lines
Quoted 42 lines
hw/vfio-user/proxy.c +12 -0
 QObject *qobj, Error **errp)
     return true;
 }
 
+static bool check_sparse_mmap_fds(VFIOUserProxy *proxy, QObject
 *qobj,
+                                  Error **errp)
+{
+    QBool *qb = qobject_to(QBool, qobj);
+
+    if (qb == NULL) {
+        error_setg(errp, "malformed %s",
 VFIO_USER_CAP_SPARSE_MMAP_FDS);
+        return false;
+    }
+    return true;
+}
+

static struct cap_entry caps_cap[] = {
{ VFIO_USER_CAP_MAX_FDS, check_max_fds },
{ VFIO_USER_CAP_MAX_XFER, check_max_xfer },

hw/vfio-user/proxy.c +2 -0
@@ -1221,6 +1263,7 @@ static struct cap_entry caps_cap[] = {
     { VFIO_USER_CAP_MAP_MAX, check_max_dma },
     { VFIO_USER_CAP_MIGR, check_migr },
     { VFIO_USER_CAP_MULTI, check_multi },
+    { VFIO_USER_CAP_SPARSE_MMAP_FDS, check_sparse_mmap_fds },
     { NULL }
 };
 
@@ -1280,6 +1323,7 @@ static GString *caps_json(void)
     qdict_put_int(capdict, VFIO_USER_CAP_PGSIZES,
 VFIO_USER_DEF_PGSIZE);
     qdict_put_int(capdict, VFIO_USER_CAP_MAP_MAX,
 VFIO_USER_DEF_MAP_MAX);
     qdict_put_bool(capdict, VFIO_USER_CAP_MULTI, true);
+    qdict_put_bool(capdict, VFIO_USER_CAP_SPARSE_MMAP_FDS, true);
 
     qdict_put_obj(dict, VFIO_USER_CAP, QOBJECT(capdict));

--
2.56.0.385.gd3acb90ef8-goog