From: Naman Gulati <namangulati@google.com>
To: qemu-devel@nongnu.org
Cc: "John Levon" <john.levon@nutanix.com>,
"Thanos Makatos" <thanos.makatos@nutanix.com>,
"Cédric Le Goater" <clg@redhat.com>,
"Alex Williamson" <alex@shazbot.org>,
"Pierrick Bouvier" <pierrick.bouvier@oss.qualcomm.com>,
"Matt Evans" <matt@ozlabs.org>,
"Naman Gulati" <namangulati@google.com>
Subject: [PATCH v5 2/4] vfio: add 'fd' and 'fd_offset' to VFIOMmap
Date: Wed, 14 Oct 2026 21:10:39 +0000 [thread overview]
Message-ID: <20261014211041.1087331-3-namangulati@google.com> (raw)
In-Reply-To: <20261014211041.1087331-1-namangulati@google.com>
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.
Reviewed-by: Thanos Makatos <thanos.makatos@nutanix.com>
Signed-off-by: Naman Gulati <namangulati@google.com>
---
hw/vfio/vfio-region.h | 4 +++-
hw/vfio/pci.c | 2 ++
hw/vfio/region.c | 26 +++++++++++++++++++-------
3 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/hw/vfio/vfio-region.h b/hw/vfio/vfio-region.h
index 58b236f113..9f6a5cf7d5 100644
--- a/hw/vfio/vfio-region.h
+++ b/hw/vfio/vfio-region.h
@@ -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;
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 428ab2f069..f851082114 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -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,
diff --git a/hw/vfio/region.c b/hw/vfio/region.c
index 54ad11a6c8..16efed2fce 100644
--- a/hw/vfio/region.c
+++ b/hw/vfio/region.c
@@ -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;
--
2.56.0.385.gd3acb90ef8-goog
next prev parent reply other threads:[~2026-10-14 21:11 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-14 21:10 [PATCH v5 0/4] vfio-user: support multiple FDs backing region mmaps Naman Gulati
2026-10-14 21:10 ` [PATCH v5 1/4] vfio-user: add multi-fd region mmap capability Naman Gulati
2026-10-20 15:37 ` John Levon
2026-10-14 21:10 ` Naman Gulati [this message]
2026-10-15 7:39 ` [PATCH v5 2/4] vfio: add 'fd' and 'fd_offset' to VFIOMmap Cédric Le Goater
2026-10-20 15:37 ` John Levon
2026-10-14 21:10 ` [PATCH v5 3/4] vfio: add setup_sparse_mmaps hook to VFIODeviceIOOps Naman Gulati
2026-10-15 7:39 ` Cédric Le Goater
2026-10-15 7:41 ` Cédric Le Goater
2026-10-20 15:38 ` John Levon
2026-10-14 21:10 ` [PATCH v5 4/4] vfio-user: support multiple FDs for sparse mmap regions Naman Gulati
2026-10-15 7:45 ` Cédric Le Goater
2026-10-20 15:35 ` John Levon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261014211041.1087331-3-namangulati@google.com \
--to=namangulati@google.com \
--cc=alex@shazbot.org \
--cc=clg@redhat.com \
--cc=john.levon@nutanix.com \
--cc=matt@ozlabs.org \
--cc=pierrick.bouvier@oss.qualcomm.com \
--cc=qemu-devel@nongnu.org \
--cc=thanos.makatos@nutanix.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.