vfio-user: support multiple FDs backing region mmaps
v3 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 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 | 71 +++++++++++++++++++++++++-- hw/vfio-user/protocol.h | 22 +++++++++ hw/vfio-user/proxy.h | 2 + hw/vfio/vfio-region.h | 9 +++- include/hw/vfio/vfio-device.h | 16 +++++++ hw/vfio-user/device.c | 90 ++++++++++++++++++++++++++++++++--- hw/vfio-user/proxy.c | 32 +++++++++++-- hw/vfio/pci.c | 2 + hw/vfio/region.c | 51 ++++++++++++++++---- 9 files changed, 269 insertions(+), 26 deletions(-)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
PATCH RESEND v3 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.
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 | 71 +++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-)
docs/interop/vfio-user.rst +67 -4
@@ -125,7 +125,10 @@ 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. .. Note:: For example, in a virtual NVMe controller, sparse regions can be used so
@@ -859,9 +862,9 @@ 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. 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 +953,66 @@ 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. Capability ID +``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` is defined as 16 (0x10) 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. +* *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*. +* *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. + +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.rc1.315.gc6ed9934b7-goog
RE: [PATCH v3 RESEND 1/4] vfio-user: add multi-fd region mmap capability
Quoted 148 lines
-----Original Message-----
From: Naman Gulati <[email protected]>
Sent: 07 October 2026 01:09
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 v3 RESEND 1/4] vfio-user: add multi-fd region mmap capability!-------------------------------------------------------------------|
CAUTION: External Email|-------------------------------------------------------------------!
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.
VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDSdefines 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 theSCM_RIGHTSancillary data of theVFIO_USER_DEVICE_GET_REGION_INFOreply message.Signed-off-by Naman Gulati <[email protected]>--- docs/interop/vfio-user.rst | 71 +++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-)docs/interop/vfio-user.rst +0
-0@@ -125,7 +125,10 @@ Sparse Regions A region can be memory-mappable in whole or in part. When only a subset ofa
region can be mapped by the client, a
``VFIO_REGION_INFO_CAP_SPARSE_MMAP``
capability is included in the region info reply. This capability describesdocs/interop/vfio-user.rst +1
-1-which portions can be mapped by the client. +which portions can be mapped by the client. When sparse mmap areas arebacked
+by multiple distinct file descriptors, a +``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` capability is included in the regiondocs/interop/vfio-user.rst +1
-0+info reply instead. .. Note:: For example, in a virtual NVMe controller, sparse regions can be used so@@ -859,9 +862,9 @@ Reply * ``VFIO_REGION_INFO_FLAG_READ`` allows client read access to the region. * ``VFIO_REGION_INFO_FLAG_WRITE`` allows client write access to theregion.
* ``VFIO_REGION_INFO_FLAG_MMAP`` specifies the client canmmap()the
region.docs/interop/vfio-user.rst +3
-3- 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. On ``AF_UNIX`` sockets, the file descriptors will be passed + as ``SCM_RIGHTS`` type ancillary data. * ``VFIO_REGION_INFO_FLAG_CAPS`` indicates additional capabilities foundin the
reply.docs/interop/vfio-user.rst +0
-0@@ -950,6 +953,66 @@ The VFIO sparse mmap area is defined in``<linux/vfio.h>`` (``struct
vfio_region_info_cap_sparse_mmap``).docs/interop/vfio-user.rst +19
-0+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. Capability ID +``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` is defined as 16 (0x10) toavoid
docs/interop/vfio-user.rst +27
-0+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 canbe
+ mapped by the client. There will be *nr_areas* tuples of offset,fd_offset, size,docs/interop/vfio-user.rst +3
-0+ fd_index, and pad. +* *offset* specifies the start of the area within the device region (BAR). +* *fd_offset* specifies the start of the mapping within the backing filedescriptor
docs/interop/vfio-user.rst +7
-0+ referenced by *fd_index*. +* *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. + +If all sparse mmap areas are backed by a single file descriptor, the server +may instead send ``VFIO_REGION_INFO_CAP_SPARSE_MMAP`` and exactlyone file
docs/interop/vfio-user.rst +3
-1+descriptor in ``SCM_RIGHTS`` for compatibility with older clients. + + ``VFIO_USER_DEVICE_GET_REGION_IO_FDS`` -------------------------------------- --
Quoted 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
This looks sensible to me, I'll take a closer look and follow up on this as well as the rest of the patches.
RE: [PATCH v3 RESEND 1/4] vfio-user: add multi-fd region mmap capability
In reply to Naman GulatiQuoted 22 lines
-----Original Message-----
From: Naman Gulati <[email protected]>
Sent: 07 October 2026 01:09
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 v3 RESEND 1/4] vfio-user: add multi-fd region mmap capability!-------------------------------------------------------------------|
CAUTION: External Email|-------------------------------------------------------------------!
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.
Quoted 8 lines
VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDSdefines 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 theSCM_RIGHTSancillary data of theVFIO_USER_DEVICE_GET_REGION_INFOreply message.
Don't we need a new capability in VFIO_USER_VERSION so that the server knows whether the client supports VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS before even sending it?
Quoted 20 lines
Signed-off-by Naman Gulati <[email protected]>--- docs/interop/vfio-user.rst | 71 +++++++++++++++++++++++++++++++++++--(quoted diff) +0
-1- 1 file changed, 67 insertions(+), 4 deletions(-)docs/interop/vfio-user.rst +0
-0@@ -125,7 +125,10 @@ Sparse Regions A region can be memory-mappable in whole or in part. When only asubset 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
describesdocs/interop/vfio-user.rst +1
-1-which portions can be mapped by the client. +which portions can be mapped by the client. When sparse mmap areas
Quoted 5 lines
are backed
+by multiple distinct file descriptors, a +``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` capability is included in the region
+info reply instead.
It's probably obvious but I don't see any harm in clearly stating that both VFIO_REGION_INFO_CAP_SPARSE_MMAP and VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS cannot be specified for the same region, and that some regions can use VFIO_REGION_INFO_CAP_SPARSE_MMAP while others VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS.
Quoted 17 lines
.. Note::
For example, in a virtual NVMe controller, sparse regions can be
used sodocs/interop/vfio-user.rst +0
-0@@ -859,9 +862,9 @@ Reply * ``VFIO_REGION_INFO_FLAG_READ`` allows client read access to theregion.
* ``VFIO_REGION_INFO_FLAG_WRITE`` allows client write access to the
region.
* ``VFIO_REGION_INFO_FLAG_MMAP`` specifies the client canmmap()
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 asdocs/interop/vfio-user.rst +1
-1- ``SCM_RIGHTS`` type ancillary data. + When this flag is set, the reply will include one or more file
Quoted 4 lines
descriptors
+ in its meta-data. On ``AF_UNIX`` sockets, the file descriptors
will be passed
+ as ``SCM_RIGHTS`` type ancillary data.
Sending more FDs than sub-regions shouldn't be permitted, right? If so, can we clarify this?
Quoted 25 lines
* ``VFIO_REGION_INFO_FLAG_CAPS`` indicates additional capabilities
found in the
reply.docs/interop/vfio-user.rst +0
-0@@ -950,6 +953,66 @@ The VFIO sparse mmap area is defined in``<linux/vfio.h>`` (``struct
vfio_region_info_cap_sparse_mmap``).docs/interop/vfio-user.rst +16
-0+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
Quoted 6 lines
the client
+viammap()where different sparse mmap areas are backed by distinct file
+descriptors in the ``SCM_RIGHTS`` ancillary data array. Capability ID +``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` is defined as 16 (0x10) to avoid
I'm not sure 0x10 will avoid clashes in the future, current highest VFIO capability is 5 (VFIO_REGION_INFO_CAP_NVLINK2_LNKSP) so we might clash sooner than we think. Using a much higher value (e.g. 0x1000) could be a bit better. Ideally, we should have some agreement with the VFIO maintainers about this, how about reserving the highest bit for VFIO "extensions"?
Quoted 27 lines
docs/interop/vfio-user.rst +27
-0+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
Quoted 1 line
area that can be
Could you clarify that zero-sized regions are not permitted?
Also, I think it's obvious but best to clarify anyway offset + size cannot exceed the region size.
Quoted 17 lines
+ mapped by the client. There will be *nr_areas* tuples of offset,
fd_offset, size,docs/interop/vfio-user.rst +2
-0+ fd_index, and pad. +* *offset* specifies the start of the area within the device region(BAR).
+* *fd_offset* specifies the start of the mapping within the backing file descriptordocs/interop/vfio-user.rst +2
-0+ referenced by *fd_index*. +* *fd_index* is the zero-based index into the array of filedescriptors
docs/interop/vfio-user.rst +4
-0+ received in the ``SCM_RIGHTS`` ancillary data of the + ``VFIO_USER_DEVICE_GET_REGION_INFO`` reply message. + +If all sparse mmap areas are backed by a single file descriptor, theserver
+may instead send ``VFIO_REGION_INFO_CAP_SPARSE_MMAP`` and exactly one file
docs/interop/vfio-user.rst
Quoted 2 lines
docs/interop/vfio-user.rst +2
-0+descriptor in ``SCM_RIGHTS`` for compatibility with older clients. +
Can such areas overlap? For VFIO_REGION_INFO_CAP_SPARSE_MMAP, I don't see how it could be useful but AFAIK it can't break anything, but when multiple FDs are involved it's not as clear.
Quoted 6 lines
+
``VFIO_USER_DEVICE_GET_REGION_IO_FDS``
----------------------------------------
2.56.0.rc1.315.gc6ed9934b7-goog
Re: [PATCH v3 RESEND 1/4] vfio-user: add multi-fd region mmap capability
Quoted 32 lines · Thanos Makatos
Quoted 23 lines
-----Original Message-----
From: Naman Gulati <[email protected]>
Sent: 07 October 2026 01:09
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 v3 RESEND 1/4] vfio-user: add multi-fd region mmap capability!-------------------------------------------------------------------|
CAUTION: External Email|-------------------------------------------------------------------!
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.Quoted 8 lines
VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDSdefines 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 theSCM_RIGHTSancillary data of theVFIO_USER_DEVICE_GET_REGION_INFOreply message.
Quoted 1 line
Don't we need a new capability in
VFIO_USER_VERSIONso that the server knows whether the client supportsVFIO_REGION_INFO_CAP_SPARSE_MMAP_FDSbefore even sending it?
Thanks for the review. Good catch, missed adding this. Will add a capability in the documentation, and update the code to advertise it in v4.
Quoted 20 lines
Quoted 20 lines
Signed-off-by Naman Gulati <[email protected]>--- docs/interop/vfio-user.rst | 71 +++++++++++++++++++++++++++++++++++--(quoted diff) +0
-1- 1 file changed, 67 insertions(+), 4 deletions(-)docs/interop/vfio-user.rst +0
-0@@ -125,7 +125,10 @@ Sparse Regions A region can be memory-mappable in whole or in part. When only asubset 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.
Quoted 8 lines
Quoted 6 lines
+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.It's probably obvious but I don't see any harm in clearly stating that both
VFIO_REGION_INFO_CAP_SPARSE_MMAPandVFIO_REGION_INFO_CAP_SPARSE_MMAP_FDScannot be specified for the same region, and that some regions can useVFIO_REGION_INFO_CAP_SPARSE_MMAPwhile othersVFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS.
Makes sense, will add that clarification, and also add a check in the code to return an error when the user specifies VFIO_REGION_INFO_CAP_SPARSE_MMAP and VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS for the same region.
Quoted 16 lines
Quoted 16 lines
.. Note::
For example, in a virtual NVMe controller, sparse regions can be
used sodocs/interop/vfio-user.rst +0
-0@@ -859,9 +862,9 @@ Reply * ``VFIO_REGION_INFO_FLAG_READ`` allows client read access to theregion.
* ``VFIO_REGION_INFO_FLAG_WRITE`` allows client write access to the
region.
* ``VFIO_REGION_INFO_FLAG_MMAP`` specifies the client canmmap()
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
Quoted 8 lines
Quoted 6 lines
docs/interop/vfio-user.rst +1
-1- ``SCM_RIGHTS`` type ancillary data. + When this flag is set, the reply will include one or more filedescriptors
+ in its meta-data. On ``AF_UNIX`` sockets, the file descriptors
will be passed
+ as ``SCM_RIGHTS`` type ancillary data.Sending more FDs than sub-regions shouldn't be permitted, right? If so, can we clarify this?
Will do.
Quoted 26 lines
Quoted 26 lines
* ``VFIO_REGION_INFO_FLAG_CAPS`` indicates additional capabilities
found in the
reply.docs/interop/vfio-user.rst +0
-0@@ -950,6 +953,66 @@ The VFIO sparse mmap area is defined in``<linux/vfio.h>`` (``struct
vfio_region_info_cap_sparse_mmap``).docs/interop/vfio-user.rst +16
-0+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
Quoted 8 lines
Quoted 6 lines
the client
+viammap()where different sparse mmap areas are backed by distinct file
+descriptors in the ``SCM_RIGHTS`` ancillary data array. Capability ID +``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` is defined as 16 (0x10) to avoidI'm not sure
0x10will avoid clashes in the future, current highest VFIO capability is 5 (VFIO_REGION_INFO_CAP_NVLINK2_LNKSP) so we might clash sooner than we think. Using a much higher value (e.g.0x1000) could be a bit better. Ideally, we should have some agreement with the VFIO maintainers about this, how about reserving the highest bit for VFIO "extensions"?
Sounds good, I can add this capability at 0x8000. If needed I can send a follow-up patch adding some documentation in include/uapi/linux/vfio.h specifying that capability IDs >= 0x8000 are reserved for "userspace VFIO extensions".
Quoted 32 lines
Quoted 28 lines
docs/interop/vfio-user.rst +27
-0+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 singleQuoted 1 line
area that can be
Could you clarify that zero-sized regions are not permitted?
Quoted 1 line
Also, I think it's obvious but best to clarify anyway offset + size cannot exceed the region size.
Will do.
Quoted 14 lines
Quoted 14 lines
+ mapped by the client. There will be *nr_areas* tuples of offset,
fd_offset, size,docs/interop/vfio-user.rst +2
-0+ fd_index, and pad. +* *offset* specifies the start of the area within the device region(BAR).
+* *fd_offset* specifies the start of the mapping within the backing file descriptordocs/interop/vfio-user.rst +2
-0+ referenced by *fd_index*. +* *fd_index* is the zero-based index into the array of filedescriptors
docs/interop/vfio-user.rst +3
-0+ received in the ``SCM_RIGHTS`` ancillary data of the + ``VFIO_USER_DEVICE_GET_REGION_INFO`` reply message. +
Quoted 8 lines
docs/interop/vfio-user.rst
Quoted 6 lines
+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 filedocs/interop/vfio-user.rst +2
-0+descriptor in ``SCM_RIGHTS`` for compatibility with older clients. +Can such areas overlap? For
VFIO_REGION_INFO_CAP_SPARSE_MMAP, I don't see how it could be useful but AFAIK it can't break anything, but when multiple FDs are involved it's not as clear.
I don't see a reason why these regions would need to overlap and the code already returns an error when the user specifies overlapping regions. Will add some text enforcing that these regions do not overlap.
Quoted 8 lines
Quoted 7 lines
+
``VFIO_USER_DEVICE_GET_REGION_IO_FDS``
----------------------------------------
2.56.0.rc1.315.gc6ed9934b7-goog
Thanos Makatos
Reviewed-byRE: [PATCH v3 RESEND 1/4] vfio-user: add multi-fd region mmap capability
Quoted 188 lines
-----Original Message-----
From: Naman Gulati <[email protected]>
Sent: 09 October 2026 17:54
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 v3 RESEND 1/4] vfio-user: add multi-fd region mmap capability!-------------------------------------------------------------------|
CAUTION: External Email|-------------------------------------------------------------------!
Quoted 7 lines · Thanos Makatos
Quoted 7 lines
-----Original Message-----
From: Naman Gulati <[email protected]>
Sent: 07 October 2026 01:09
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 v3 RESEND 1/4] vfio-user: add multi-fd region mmap capability!-----------------------------------------------------------------
--|
Quoted 3 lines
Quoted 3 lines
CAUTION: External Email
|-----------------------------------------------------------------
--!
Quoted 2 lines
Quoted 2 lines
Extend the vfio-user protocol specification to allow servers to
pass
Quoted 1 line
Quoted 1 line
multiple file descriptors for different sparse memory areas within
a
Quoted 4 lines
Quoted 4 lines
single device region with capability
VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS.
VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDSdefines an array of sparsemmap
Quoted 3 lines
Quoted 3 lines
areas where each area contains:
- offset: Start of the area within the device region (BAR).
-fd_offset: Start of the mapping within the backing filedescriptor.
Quoted 6 lines
Quoted 4 lines
- size: Size of the mappable area.
-fd_index: Zero-based index into the array of file descriptors received in theSCM_RIGHTSancillary data of theVFIO_USER_DEVICE_GET_REGION_INFOreply message.Don't we need a new capability in
VFIO_USER_VERSIONso that theserver knows whether the client supports
VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDSbefore even sending it?Thanks for the review. Good catch, missed adding this. Will add a capability in the documentation, and update the code to advertise it in v4.
Quoted 5 lines
Quoted 5 lines
Signed-off-by Naman Gulati <[email protected]>--- docs/interop/vfio-user.rst | 71+++++++++++++++++++++++++++++++++++--
Quoted 4 lines
docs/interop/vfio-
Quoted 4 lines
(quoted diff) +0
-1- 1 file changed, 67 insertions(+), 4 deletions(-)docs/interop/vfio- renamed from docs/interop/vfio-user.rst +0
-0
user.rstQuoted 4 lines
Quoted 4 lines
index
1c92f442d2..5744f58e31100644docs/interop/vfio-user.rst +0
-0@@ -125,7 +125,10 @@ Sparse RegionsQuoted 8 lines
docs/interop/vfio-user.rst
Quoted 8 lines
docs/interop/vfio-user.rst +0
-0A region can be memory-mappable in whole or in part. When only asubset 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
describesdocs/interop/vfio-user.rst +1
-1-which portions can be mapped by the client. +which portions can be mapped by the client. When sparse mmapareas
Quoted 3 lines
Quoted 3 lines
are backed
+by multiple distinct file descriptors, a +``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` capability is includedin
Quoted 4 lines
Quoted 2 lines
the region
+info reply instead.It's probably obvious but I don't see any harm in clearly stating
that both
VFIO_REGION_INFO_CAP_SPARSE_MMAPandVFIO_REGION_INFO_CAP_SPARSE_MMAP_FDScannot be specified for the same region, and that some regions can useVFIO_REGION_INFO_CAP_SPARSE_MMAPwhile othersVFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS.Makes sense, will add that clarification, and also add a check in the code to return an error when the user specifies
VFIO_REGION_INFO_CAP_SPARSE_MMAPandVFIO_REGION_INFO_CAP_SPARSE_MMAP_FDSfor the same region.Quoted 4 lines
Quoted 4 lines
.. Note::
For example, in a virtual NVMe controller, sparse regions canbe
Quoted 3 lines
docs/interop/vfio-user.rst Reply · line 862
Quoted 3 lines
used so
docs/interop/vfio-user.rst +0
-0@@ -859,9 +862,9 @@ Reply * ``VFIO_REGION_INFO_FLAG_READ`` allows client read access tothe
Quoted 2 lines
Quoted 2 lines
region.
* ``VFIO_REGION_INFO_FLAG_WRITE`` allows client write access tothe
Quoted 2 lines
Quoted 2 lines
region.
* ``VFIO_REGION_INFO_FLAG_MMAP`` specifies the client can
mmap()Quoted 2 lines
Quoted 2 lines
the region.
- When this flag is set, the reply will include a filedescriptor
Quoted 2 lines
Quoted 2 lines
in its
- meta-data. On ``AF_UNIX`` sockets, the file descriptors willbe
Quoted 3 lines
docs/interop/vfio-user.rst
Quoted 3 lines
passed as
docs/interop/vfio-user.rst +1
-1- ``SCM_RIGHTS`` type ancillary data. + When this flag is set, the reply will include one or morefile
Quoted 2 lines
Quoted 2 lines
descriptors
+ in its meta-data. On ``AF_UNIX`` sockets, the filedescriptors
Quoted 4 lines
Quoted 2 lines
will be passed
+ as ``SCM_RIGHTS`` type ancillary data.Sending more FDs than sub-regions shouldn't be permitted, right? If
so, can we clarify this?
Will do.
Quoted 2 lines
Quoted 2 lines
* ``VFIO_REGION_INFO_FLAG_CAPS`` indicates additional
capabilities
Quoted 8 lines
Quoted 8 lines
found in the
reply.docs/interop/vfio-user.rst +0
-0@@ -950,6 +953,66 @@ The VFIO sparse mmap area is defined in``<linux/vfio.h>`` (``struct
vfio_region_info_cap_sparse_mmap``).Quoted 16 lines
Quoted 6 lines
docs/interop/vfio-user.rst +6
-0+VFIO sparse mmap fds cap header +""""""""""""""""""""""""""""""" + ++----------------------+--------------------------------------+ +| Name | Value | ++======================+======================================+docs/interop/vfio-user.rst The VFIO sparse mmap area is defined in ``<linux/vfio.h>`` (``struct
Quoted 10 lines
docs/interop/vfio-user.rst +10
-0+| 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 accessby
Quoted 2 lines
Quoted 2 lines
the client
+viammap()where different sparse mmap areas are backed bydistinct
Quoted 2 lines
Quoted 2 lines
file
+descriptors in the ``SCM_RIGHTS`` ancillary data array.Capability ID
Quoted 1 line
Quoted 1 line
+``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` is defined as 16 (
0x10)to
Quoted 3 lines
Quoted 1 line
avoid
I'm not sure
0x10will avoid clashes in the future, current highestVFIO capability is 5 (
VFIO_REGION_INFO_CAP_NVLINK2_LNKSP) so we might clash sooner than we think. Using a much higher value (e.g.0x1000) could be a bit better. Ideally, we should have some agreement with the VFIO maintainers about this, how about reserving the highest bit for VFIO "extensions"?
Quoted 4 lines
Sounds good, I can add this capability at
0x8000. If needed I can send a follow-up patch adding some documentation ininclude/uapi/linux/vfio.hspecifying that capability IDs >=0x8000are reserved for "userspace VFIO extensions".
I think we'd need this confirmation before merging this patch. Otherwise, LGTM.
Quoted 65 lines
Quoted 33 lines
Quoted 29 lines
docs/interop/vfio-user.rst +27
-0+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 singleQuoted 1 line
area that can be
Could you clarify that zero-sized regions are not permitted?
Quoted 1 line
Also, I think it's obvious but best to clarify anyway offset + size
cannot exceed the region size.
Will do.
Quoted 2 lines
Quoted 2 lines
+ mapped by the client. There will be *nr_areas* tuples of
offset,
Quoted 3 lines
docs/interop/vfio-user.rst
Quoted 3 lines
fd_offset, size,docs/interop/vfio-user.rst +2
-0+ fd_index, and pad. +* *offset* specifies the start of the area within the deviceregion
Quoted 2 lines
Quoted 2 lines
(BAR).
+* *fd_offset* specifies the start of the mapping within thebacking
Quoted 8 lines
docs/interop/vfio-user.rst The VFIO sparse mmap area is defined in ``<linux/vfio.h>`` (``struct
Quoted 8 lines
file descriptor
docs/interop/vfio-user.rst +2
-0+ referenced by *fd_index*. +* *fd_index* is the zero-based index into the array of filedescriptors
docs/interop/vfio-user.rst +4
-0+ received in the ``SCM_RIGHTS`` ancillary data of the + ``VFIO_USER_DEVICE_GET_REGION_INFO`` reply message. + +If all sparse mmap areas are backed by a single file descriptor,the
Quoted 2 lines
Quoted 2 lines
server
+may instead send ``VFIO_REGION_INFO_CAP_SPARSE_MMAP`` and exactlyone
Quoted 2 lines
Quoted 2 lines
file
+descriptor in ``SCM_RIGHTS`` for compatibility with olderclients.
Quoted 2 lines
Quoted 1 line
+
Quoted 17 lines
Quoted 1 line
Can such areas overlap? For
VFIO_REGION_INFO_CAP_SPARSE_MMAP, Idon't see how it could be useful but AFAIK it can't break anything, but when multiple FDs are involved it's not as clear.
I don't see a reason why these regions would need to overlap and the code already returns an error when the user specifies overlapping regions. Will add some text enforcing that these regions do not overlap.
Quoted 8 lines
Quoted 7 lines
+
``VFIO_USER_DEVICE_GET_REGION_IO_FDS``
----------------------------------------
2.56.0.rc1.315.gc6ed9934b7-goog
PATCH RESEND v3 2/4 vfio: add 'fd' and 'fd_offset' to VFIOMmap
In reply to Naman GulatiAllow 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
@@ -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
@@ -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
@@ -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.rc1.315.gc6ed9934b7-goog
RE: [PATCH v3 RESEND 2/4] vfio: add 'fd' and 'fd_offset' to VFIOMmap
Quoted 159 lines
-----Original Message-----
From: Naman Gulati <[email protected]>
Sent: 07 October 2026 01:09
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 v3 RESEND 2/4] vfio: add 'fd' and 'fd_offset' to VFIOMmap!-------------------------------------------------------------------|
CAUTION: External Email|-------------------------------------------------------------------!
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 updatevfio_region_mmap()to use them.Signed-off-by Naman Gulati <[email protected]>--- 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@@ -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 +1
-0@@ -1623,6 +1623,7 @@ static voidvfio_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-Quoted 1 line
mmaps[0].offset,
hw/vfio/pci.c +1
-0@@ -1649,6 +1650,7 @@ static voidvfio_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-Quoted 1 line
mmaps[1].offset,
hw/vfio/region.c +3
-0@@ -189,6 +189,9 @@ static intvfio_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++;} }hw/vfio/region.c +2
-0@@ -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;}hw/vfio/region.c +0
-3@@ -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,hw/vfio/region.c +12
-4@@ -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-Quoted 1 line
mmaps[i].offset +
hw/vfio/region.c +2
-1+ region->mmaps[i].fd_offset, + region->mmaps[i].fd_offset + region->mmaps[i].size - 1, ret); region->mmaps[i].mmap =NULL ; --
Quoted 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
PATCH RESEND v3 3/4 vfio: add setup_sparse_mmaps hook to VFIODeviceIOOps
In reply to Naman GulatiAdd 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/region.c | 25 ++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-)
hw/vfio/vfio-region.h +5 -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); +#endif /* HW_VFIO_REGION_H */
include/hw/vfio/vfio-device.h +16 -0
@@ -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/region.c +22 -3
@@ -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,25 @@ 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; + +if (region->vbasedev->io_ops && + region->vbasedev->io_ops->setup_sparse_mmaps) { + ret = region->vbasedev->io_ops->setup_sparse_mmaps (region, info, errp); + }else { + ret =vfio_default_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.rc1.315.gc6ed9934b7-goog
PATCH RESEND v3 4/4 vfio-user: support multiple FDs for sparse mmap regions
In reply to Naman GulatiAdd client support for capability VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS (capability ID 16) in vfio-user.
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 populate per-area file descriptors and fd_offset values.
--- hw/vfio-user/protocol.h | 22 ++++++++++ hw/vfio-user/proxy.h | 2 + hw/vfio-user/device.c | 90 +++++++++++++++++++++++++++++++++++++---- hw/vfio-user/proxy.c | 32 +++++++++++++-- 4 files changed, 135 insertions(+), 11 deletions(-)
hw/vfio-user/protocol.h +22 -0
@@ -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;
@@ -166,6 +168,26 @@ typedef struct {uint64_t offset; } VFIOUserRegionInfo; +/* +* VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS +*/ +#define VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS 16 + +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
@@ -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 +83 -7
131 lines not on this page · raw message
hw/vfio-user/proxy.c +28 -4
@@ -363,6 +363,8 @@ static int vfio_user_recv_one(VFIOUserProxy *proxy, Error **errp) }memcpy (msg->fds->fds, fdp, numfds *sizeof (int )); + }else if (msg->fds !=NULL ) { + msg->fds->recv_fds =0 ; }/*
@@ -740,6 +742,10 @@ bool vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,qemu_mutex_unlock (&proxy->lock); +if (!ok && fds !=NULL ) { + fds->recv_fds =0 ; + } +return ok; }
@@ -867,17 +873,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 +986,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 +1047,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); }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Naman,
Quoted 55 lines · Naman Gulati
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_FDSUnlike
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_MMAPand send exactly one file descriptor inSCM_RIGHTSfor 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 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 theget_region_infocallback 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: addsetup_sparse_mmapshook to VFIODeviceIOOps
vfio-user: support multiple FDs for sparse mmap regions
Quoted 11 lines
docs/interop/vfio-user.rst | 71 +++++++++++++++++++++++++-- hw/vfio-user/protocol.h | 22 +++++++++ hw/vfio-user/proxy.h | 2 + hw/vfio/vfio-region.h | 9 +++- include/hw/vfio/vfio-device.h | 16 +++++++ hw/vfio-user/device.c | 90 ++++++++++++++++++++++++++++++++--- hw/vfio-user/proxy.c | 32 +++++++++++-- hw/vfio/pci.c | 2 + hw/vfio/region.c | 51 ++++++++++++++++---- 9 files changed, 269 insertions(+), 26 deletions(-)
There is no need to resend. We need some acks from the vfio-user team now.
Thanks,
C.