From: "Cédric Le Goater" <clg@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Cédric Le Goater" <clg@redhat.com>,
"Alex Williamson" <alex@shazbot.org>
Subject: [PULL 05/27] vfio/iommufd: Merge .dma_map_file() into .dma_map()
Date: Tue, 7 Jul 2026 08:28:58 +0200 [thread overview]
Message-ID: <20260707062920.317302-6-clg@redhat.com> (raw)
In-Reply-To: <20260707062920.317302-1-clg@redhat.com>
Simplify the VFIOIOMMUClass interface by removing the dma_map_file
handler. Move the logic to decide between the standard virtual and
file-backed mapping into the IOMMUFD backend, utilizing the
MemoryRegion already passed to the dma_map handler.
This removes redundant dispatch logic from the generic container layer
and let backends to manage their own mapping strategies. This is
similar to the vfio-user implementation.
Reviewed-by: Alex Williamson <alex@shazbot.org>
Link: https://lore.kernel.org/qemu-devel/20260608055758.359002-1-clg@redhat.com
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
include/hw/vfio/vfio-container.h | 15 ----------
hw/vfio/container.c | 38 ------------------------
hw/vfio/iommufd.c | 50 +++++++++++++++++++++++---------
3 files changed, 37 insertions(+), 66 deletions(-)
diff --git a/include/hw/vfio/vfio-container.h b/include/hw/vfio/vfio-container.h
index b2e7f4312c31ed4b776ddb195b021e84ebed556f..a15ee2df2b9c82d7c5004685aa91c3339587e06c 100644
--- a/include/hw/vfio/vfio-container.h
+++ b/include/hw/vfio/vfio-container.h
@@ -172,21 +172,6 @@ struct VFIOIOMMUClass {
int (*dma_map)(const VFIOContainer *bcontainer,
hwaddr iova, uint64_t size,
void *vaddr, bool readonly, MemoryRegion *mr);
- /**
- * @dma_map_file
- *
- * Map a file range for the container.
- *
- * @bcontainer: #VFIOContainer to use for map
- * @iova: start address to map
- * @size: size of the range to map
- * @fd: descriptor of the file to map
- * @start: starting file offset of the range to map
- * @readonly: map read only if true
- */
- int (*dma_map_file)(const VFIOContainer *bcontainer,
- hwaddr iova, uint64_t size,
- int fd, unsigned long start, bool readonly);
/**
* @dma_unmap
*
diff --git a/hw/vfio/container.c b/hw/vfio/container.c
index 56bd9ac0095df7b0f4da85f1d8dcb8d571fa9f2a..d09a6637324c6bf29b2fe22369082f87e280c98f 100644
--- a/hw/vfio/container.c
+++ b/hw/vfio/container.c
@@ -15,7 +15,6 @@
#include <linux/vfio.h>
#include "system/tcg.h"
-#include "system/ramblock.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "hw/vfio/vfio-container.h"
@@ -74,49 +73,12 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
bcontainer->space = space;
}
-static bool vfio_container_can_dma_map_file(VFIOContainer *bcontainer,
- MemoryRegion *mr, int *fd)
-{
- VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
- RAMBlock *rb = mr->ram_block;
-
- if (!vioc->dma_map_file || !rb) {
- return false;
- }
-
- *fd = qemu_ram_get_fd(rb);
- if (*fd < 0) {
- return false;
- }
-
- /*
- * We can use IOMMU DMA mapping (IOMMU_IOAS_MAP_FILE) for :
- *
- * 1) Guest RAM blocks explicitly configured as shared (MAP_SHARED)
- * 2) RAM device sub-regions (MMIO BARs)
- *
- * Private RAM mappings (MAP_PRIVATE) are strictly excluded. Because
- * they are subject to copy-on-write (COW) anomalies, their underlying
- * PFNs can permanently diverge from the backing file
- */
- return qemu_ram_is_shared(rb) || memory_region_is_ram_device(mr);
-}
-
int vfio_container_dma_map(VFIOContainer *bcontainer,
hwaddr iova, uint64_t size,
void *vaddr, bool readonly, MemoryRegion *mr)
{
VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
- int mfd;
- if (vfio_container_can_dma_map_file(bcontainer, mr, &mfd)) {
- RAMBlock *rb = mr->ram_block;
- unsigned long start = vaddr - qemu_ram_get_host_addr(rb);
- unsigned long offset = qemu_ram_get_fd_offset(rb);
-
- return vioc->dma_map_file(bcontainer, iova, size, mfd, start + offset,
- readonly);
- }
g_assert(vioc->dma_map);
return vioc->dma_map(bcontainer, iova, size, vaddr, readonly, mr);
}
diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
index 68f2ae6f9f667bb05c20e452e8c5b43da6558098..6ff668d2597e07c6953cf0f1997514aec199642c 100644
--- a/hw/vfio/iommufd.c
+++ b/hw/vfio/iommufd.c
@@ -20,6 +20,7 @@
#include "trace.h"
#include "qapi/error.h"
#include "system/iommufd.h"
+#include "system/ramblock.h"
#include "hw/core/iommu.h"
#include "hw/core/qdev.h"
#include "hw/vfio/vfio-cpr.h"
@@ -35,26 +36,50 @@
#define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \
TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
+static bool iommufd_cdev_can_map_file_dma(MemoryRegion *mr, int *fd)
+{
+ RAMBlock *rb = mr ? mr->ram_block : NULL;
+
+ if (!rb) {
+ return false;
+ }
+
+ *fd = qemu_ram_get_fd(rb);
+ if (*fd < 0) {
+ return false;
+ }
+
+ /*
+ * Use iommufd_backend_map_file_dma() (IOMMU_IOAS_MAP_FILE) for:
+ * 1) Guest RAM blocks explicitly configured as shared (MAP_SHARED)
+ * 2) RAM device sub-regions (MMIO BARs)
+ *
+ * Private RAM mappings (MAP_PRIVATE) are excluded: copy-on-write
+ * semantics can cause their underlying PFNs to permanently diverge
+ * from the backing file.
+ */
+ return qemu_ram_is_shared(rb) || memory_region_is_ram_device(mr);
+}
+
static int iommufd_cdev_map(const VFIOContainer *bcontainer, hwaddr iova,
uint64_t size, void *vaddr, bool readonly,
MemoryRegion *mr)
{
const VFIOIOMMUFDContainer *container = VFIO_IOMMU_IOMMUFD(bcontainer);
+ int fd;
- return iommufd_backend_map_dma(container->be,
- container->ioas_id,
- iova, size, vaddr, readonly);
-}
+ if (iommufd_cdev_can_map_file_dma(mr, &fd)) {
+ RAMBlock *rb = mr->ram_block;
+ unsigned long start = vaddr - qemu_ram_get_host_addr(rb);
+ unsigned long offset = qemu_ram_get_fd_offset(rb);
-static int iommufd_cdev_map_file(const VFIOContainer *bcontainer,
- hwaddr iova, uint64_t size,
- int fd, unsigned long start, bool readonly)
-{
- const VFIOIOMMUFDContainer *container = VFIO_IOMMU_IOMMUFD(bcontainer);
+ return iommufd_backend_map_file_dma(container->be, container->ioas_id,
+ iova, size, fd,
+ start + offset, readonly);
+ }
- return iommufd_backend_map_file_dma(container->be,
- container->ioas_id,
- iova, size, fd, start, readonly);
+ return iommufd_backend_map_dma(container->be, container->ioas_id,
+ iova, size, vaddr, readonly);
}
static int iommufd_cdev_unmap(const VFIOContainer *bcontainer,
@@ -929,7 +954,6 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, const void *data)
VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
vioc->dma_map = iommufd_cdev_map;
- vioc->dma_map_file = iommufd_cdev_map_file;
vioc->dma_unmap = iommufd_cdev_unmap;
vioc->attach_device = iommufd_cdev_attach;
vioc->detach_device = iommufd_cdev_detach;
--
2.54.0
next prev parent reply other threads:[~2026-07-07 6:30 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-07 6:28 [PULL 00/27] vfio queue Cédric Le Goater
2026-07-07 6:28 ` [PULL 01/27] vfio/pci: Initialize rom_read_failed in vfio_pci_load_rom() Cédric Le Goater
2026-07-07 6:28 ` [PULL 02/27] vfio/pci: Fix information leak in vfio_rom_read() Cédric Le Goater
2026-07-07 6:28 ` [PULL 03/27] docs: Update vfio-user spec to describe DMA access mode bits Cédric Le Goater
2026-07-07 6:28 ` [PULL 04/27] vfio-user: validate VERSION replies Cédric Le Goater
2026-07-07 6:28 ` Cédric Le Goater [this message]
2026-07-07 6:28 ` [PULL 06/27] migration: Propagate errors in migration_completion_precopy() Cédric Le Goater
2026-07-07 6:29 ` [PULL 07/27] migration/ram: Use migration_bitmap_sync_precopy() for postcopy discard Cédric Le Goater
2026-07-07 6:29 ` [PULL 08/27] migration: Run final save_query_pending at switchover Cédric Le Goater
2026-07-07 6:29 ` [PULL 09/27] migration: Log the approver in qemu_loadvm_approve_switchover() Cédric Le Goater
2026-07-07 6:29 ` [PULL 10/27] migration: Replace switchover_ack_needed SaveVMHandler Cédric Le Goater
2026-07-07 6:29 ` [PULL 11/27] migration: Rename switchover-ack code to legacy Cédric Le Goater
2026-07-07 6:29 ` [PULL 12/27] migration: Make switchover-ack re-usable Cédric Le Goater
2026-07-07 6:29 ` [PULL 13/27] migration: Fail migration if switchover-ack is requested after switchover decision Cédric Le Goater
2026-07-07 6:29 ` [PULL 14/27] vfio/migration: Extract VFIO_MIG_FLAG_DEV_INIT_DATA_SENT sending to helper Cédric Le Goater
2026-07-07 6:29 ` [PULL 15/27] vfio/migration: Add Error ** parameter to vfio_migration_init() Cédric Le Goater
2026-07-07 6:29 ` [PULL 16/27] vfio/migration: Add new switchover-ack mechanism Cédric Le Goater
2026-07-07 6:29 ` [PULL 17/27] vfio/migration: Implement VFIO_PRECOPY_INFO_REINIT feature Cédric Le Goater
2026-07-07 6:29 ` [PULL 18/27] vfio/migration: Check VFIO_PRECOPY_INFO_REINIT during switchover Cédric Le Goater
2026-07-07 6:29 ` [PULL 19/27] migration: Enable new switchover-ack Cédric Le Goater
2026-07-07 6:29 ` [PULL 20/27] migration: Refactor migration_completion_precopy() to return bool Cédric Le Goater
2026-07-07 6:29 ` [PULL 21/27] migration: Fix "switchover" used as a verb in comments and docs Cédric Le Goater
2026-07-07 6:29 ` [PULL 22/27] iommufd: Introduce handler for device ATS support Cédric Le Goater
2026-07-07 6:29 ` [PULL 23/27] vfio/pci: Add ats property Cédric Le Goater
2026-07-07 6:29 ` [PULL 24/27] vfio/pci: Propagate errors in vfio_pci_load_rom() using Error API Cédric Le Goater
2026-07-07 6:29 ` [PULL 25/27] vfio/listener: Fix translated_addr for non-identity-mapped RAM sections Cédric Le Goater
2026-07-07 6:29 ` [PULL 26/27] backends/iommufd: Fix dev_id and type order in viommu trace Cédric Le Goater
2026-07-07 6:29 ` [PULL 27/27] vfio/pci: Reject invalid MSI-X Table and PBA BIR values Cédric Le Goater
2026-07-08 5:53 ` [PULL 00/27] vfio queue Stefan Hajnoczi
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=20260707062920.317302-6-clg@redhat.com \
--to=clg@redhat.com \
--cc=alex@shazbot.org \
--cc=qemu-devel@nongnu.org \
/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.