iommufd: Enable noiommu mode for cdev
Subsystems (6)
v4 cover iommufd: Enable noiommu mode for cdev
VFIO's unsafe_noiommu_mode has long provided a way for userspace drivers to operate on platforms lacking a hardware IOMMU. Today, IOMMUFD also supports No-IOMMU mode for group-based devices under vfio_compat mode. However, IOMMUFD's native character device (cdev) does not yet support No-IOMMU mode, which is the purpose of this patch.
In summary, we have:
|-------------------------+------+---------------| | Device access mode | VFIO | IOMMUFD | |-------------------------+------+---------------| | group /dev/vfio/$GROUP | Yes | Yes | |-------------------------+------+---------------| | cdev /dev/vfio/devices/ | No | This patch | |-------------------------+------+---------------|
Beyond enabling cdev for IOMMUFD, this patch also addresses the following deficiencies in the current No-IOMMU mode suggested by Jason[1]:
- Devices operating under No-IOMMU mode are limited to device-level UAPI access, without container or IOAS-level capabilities. Consequently, user-space drivers lack structured mechanisms for page pinning and often resort to mlock(), which is less robust than pin_user_pages() used for devices backed by a physical IOMMU. For example, mlock() does not prevent page migration.
- There is no architectural mechanism for obtaining physical addresses for DMA. As a workaround, user-space drivers frequently rely on /proc/pagemap tricks or hardcoded values.
By allowing noiommu device access to IOMMUFD IOAS and HWPT objects, this patch brings No-IOMMU mode closer to full citizenship within the IOMMU subsystem. In addition to addressing the two deficiencies mentioned above, the expectation is that it will also enable No-IOMMU devices to seamlessly participate in live update sessions via KHO [2].
Furthermore, these devices will use the IOMMUFD-based ownership checking model for VFIO_DEVICE_PCI_HOT_RESET, eliminating the need for an iommufd_access object as required in a previous attempt [3].
ChangeLog:
V4:
- Fix various corner cases pointed out by (Sashiko) Details in each patch changelog.
V3:
- Improve error handling [3/10] (Mostafa)
- Simplify vfio_device_is_noiommu logic and merged in [6/10] (Mostafa)
- Fix build dependency on !GENERIC_ATOMIC64
- Add comment to explain the design difference over the legacy noiommu VFIO code.[1/10]
V2:
- Fix build depenency by adding IOMMU_SUPPORT in [8/11]
- Add an optimization to scan beyond the first page for a contiguous physical address range and return its length instead of a single page.[4/11]
Since RFC[4]:
- Abandoned dummy iommu driver approach as patch 1-3 absorbed the changes into iommufd.
[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://lore.kernel.org/linux-pci/[email protected]/
[3] https://lore.kernel.org/kvm/[email protected]/
[4] https://lore.kernel.org/linux-iommu/[email protected]/
Thanks,
Jacob
Jacob Pan (7):
iommufd: Add an ioctl IOMMU_IOAS_GET_PA to query PA from IOVA
vfio: Allow null group for noiommu without containers
vfio: Introduce and set noiommu flag on vfio_device
vfio: Enable cdev noiommu mode under iommufd vfio:selftest: Handle VFIO noiommu cdev selftests/vfio: Add iommufd noiommu mode selftest for cdev
Documentation: Update VFIO NOIOMMU mode
Jason Gunthorpe (3):
iommufd: Support a HWPT without an iommu driver for noiommu
iommufd: Move igroup allocation to a function
iommufd: Allow binding to a noiommu device
Documentation/driver-api/vfio.rst | 45 +- drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/device.c | 175 ++++-- drivers/iommu/iommufd/hw_pagetable.c | 11 +- drivers/iommu/iommufd/hwpt_noiommu.c | 102 ++++ drivers/iommu/iommufd/io_pagetable.c | 60 ++ drivers/iommu/iommufd/ioas.c | 25 + drivers/iommu/iommufd/iommufd_private.h | 5 + drivers/iommu/iommufd/main.c | 3 + drivers/vfio/Kconfig | 8 +- drivers/vfio/group.c | 41 +- drivers/vfio/iommufd.c | 7 - drivers/vfio/vfio.h | 28 +- drivers/vfio/vfio_main.c | 12 +- include/linux/vfio.h | 10 + include/uapi/linux/iommufd.h | 25 + tools/testing/selftests/vfio/Makefile | 1 + .../lib/include/libvfio/vfio_pci_device.h | 1 + .../selftests/vfio/lib/vfio_pci_device.c | 32 +- .../vfio/vfio_iommufd_noiommu_test.c | 567 ++++++++++++++++++ .../vfio/vfio_pci_liveupdate_kexec_test.c | 9 + 21 files changed, 1074 insertions(+), 94 deletions(-) create mode 100644 drivers/iommu/iommufd/hwpt_noiommu.c create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_noiommu_test.c
Signature · 1 line
2.34.1
PATCH v4 1/10 iommufd: Support a HWPT without an iommu driver for noiommu
From: Jason Gunthorpe <[email protected]>
Create just a little part of a real iommu driver, enough to slot in under the dev_iommu_ops() and allow iommufd to call domain_alloc_paging_flags() and fail everything else.
This allows explicitly creating a HWPT under an IOAS.
---
v4:
- Make iommufd_noiommu_ops const
v3:
- Add comment to explain the design difference over the legacy noiommu VFIO code.
fix const hwpt
--- drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/hw_pagetable.c | 11 ++- drivers/iommu/iommufd/hwpt_noiommu.c | 102 ++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 2 + 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 drivers/iommu/iommufd/hwpt_noiommu.c
drivers/iommu/iommufd/Makefile +1 -0
@@ -10,6 +10,7 @@ iommufd-y := \ vfio_compat.o \ viommu.o +iommufd-$(CONFIG_VFIO_NOIOMMU) += hwpt_noiommu.o iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o obj-$(CONFIG_IOMMUFD) += iommufd.o
drivers/iommu/iommufd/hw_pagetable.c +9 -2
@@ -8,6 +8,13 @@#include "../iommu-priv.h" #include "iommufd_private.h" +static const struct iommu_ops *get_iommu_ops (struct iommufd_device *idev) +{ +if (IS_ENABLED (CONFIG_VFIO_NOIOMMU ) && !idev->igroup->group) +return &iommufd_noiommu_ops; +return dev_iommu_ops (idev->dev); +} +static void __iommufd_hwpt_destroy (struct iommufd_hw_pagetable *hwpt) {if (hwpt->domain)
@@ -114,7 +121,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,IOMMU_HWPT_ALLOC_DIRTY_TRACKING |IOMMU_HWPT_FAULT_ID_VALID |IOMMU_HWPT_ALLOC_PASID ; -const struct iommu_ops *ops =dev_iommu_ops (idev->dev); +const struct iommu_ops *ops =get_iommu_ops (idev);struct iommufd_hwpt_paging *hwpt_paging;struct iommufd_hw_pagetable *hwpt;int rc;
@@ -229,7 +236,7 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, struct iommufd_device *idev, u32 flags, const struct iommu_user_data *user_data) { - const struct iommu_ops *ops = dev_iommu_ops(idev->dev); + const struct iommu_ops *ops = get_iommu_ops(idev);struct iommufd_hwpt_nested *hwpt_nested;struct iommufd_hw_pagetable *hwpt;int rc;
drivers/iommu/iommufd/hwpt_noiommu.c new file +102 -0
102 lines not on this page · raw message
drivers/iommu/iommufd/iommufd_private.h +2 -0
@@ -464,6 +464,8 @@ static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx,refcount_dec (&hwpt->obj.users); } +extern const struct iommu_ops iommufd_noiommu_ops; +struct iommufd_attach ;struct iommufd_group {
Signature · 1 line
2.34.1
PATCH v4 2/10 iommufd: Move igroup allocation to a function
In reply to Jacob PanFrom: Jason Gunthorpe <[email protected]>
So it can be reused in the next patch which allows binding to noiommu device.
---
v3:
- Moved null group check out to the next patch (Mostafa)
--- drivers/iommu/iommufd/device.c | 43 +++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 16 deletions(-)
drivers/iommu/iommufd/device.c +27 -16
@@ -56,6 +56,30 @@ static bool iommufd_group_try_get(struct iommufd_group *igroup,return kref_get_unless_zero (&igroup->ref); } +static struct iommufd_group *iommufd_alloc_group (struct iommufd_ctx *ictx, +struct iommu_group *group) +{ +struct iommufd_group *new_igroup; + + new_igroup =kzalloc (sizeof (*new_igroup),GFP_KERNEL ); +if (!new_igroup) +return ERR_PTR (-ENOMEM ); + +kref_init (&new_igroup->ref); +mutex_init (&new_igroup->lock); +xa_init (&new_igroup->pasid_attach); + new_igroup->sw_msi_start =PHYS_ADDR_MAX ; +/* group reference moves into new_igroup */ + new_igroup->group = group; + +/* +* The ictx is not additionally refcounted here becase all objects using +* an igroup must put it before their destroy completes. +*/ + new_igroup->ictx = ictx; +return new_igroup; +} +/* * iommufd needs to store some more data for each iommu_group, we keep a * parallel xarray indexed by iommu_group id to hold this instead of putting it
@@ -87,25 +111,12 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, }xa_unlock (&ictx->groups); - new_igroup =kzalloc_obj (*new_igroup); -if (!new_igroup) { + new_igroup =iommufd_alloc_group (ictx, group); +if (IS_ERR (new_igroup)) {iommu_group_put (group); -return ERR_PTR (-ENOMEM ); +return new_igroup; } -kref_init (&new_igroup->ref); -mutex_init (&new_igroup->lock); -xa_init (&new_igroup->pasid_attach); - new_igroup->sw_msi_start =PHYS_ADDR_MAX ; -/* group reference moves into new_igroup */ - new_igroup->group = group; - -/* -* The ictx is not additionally refcounted here becase all objects using -* an igroup must put it before their destroy completes. -*/ - new_igroup->ictx = ictx; -/* * We dropped the lock so igroup is invalid. NULL is a safe and likely * value to assume for the xa_cmpxchg algorithm.
Signature · 1 line
2.34.1
PATCH v4 3/10 iommufd: Allow binding to a noiommu device
In reply to Jacob PanFrom: Jason Gunthorpe <[email protected]>
Allow iommufd to bind devices without an IOMMU (noiommu mode) by creating a dummy IOMMU group for such devices and skipping hwpt operations.
This enables noiommu devices to operate through the same iommufd API as IOMMU- capable devices.
---
v4:
- handle partially initialized idev w/o igroup (Sashiko)
v3:
- handle new error cases iommufd_device_bind. (Mostafa)
--- drivers/iommu/iommufd/device.c | 132 +++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 39 deletions(-)
drivers/iommu/iommufd/device.c +93 -39
@@ -23,6 +23,11 @@ struct iommufd_attach {struct xarray device_array; }; +static bool is_vfio_noiommu (struct iommufd_device *idev) +{ +return !device_iommu_mapped (idev->dev) || !idev->dev->iommu; +} +static void iommufd_group_release (struct kref *kref) { struct iommufd_group *igroup =
@@ -30,9 +35,11 @@ static void iommufd_group_release(struct kref *kref)WARN_ON (!xa_empty (&igroup->pasid_attach)); -xa_cmpxchg (&igroup->ictx->groups,iommu_group_id (igroup->group), igroup, -NULL ,GFP_KERNEL ); -iommu_group_put (igroup->group); +if (igroup->group) { +xa_cmpxchg (&igroup->ictx->groups,iommu_group_id (igroup->group), + igroup,NULL ,GFP_KERNEL ); +iommu_group_put (igroup->group); + }mutex_destroy (&igroup->lock);kfree (igroup); }
@@ -204,32 +211,19 @@ void iommufd_device_destroy(struct iommufd_object *obj)struct iommufd_device *idev = container_of(obj, struct iommufd_device, obj); -iommu_device_release_dma_owner (idev->dev); +if (!idev->igroup) +return ; +if (!is_vfio_noiommu (idev)) +iommu_device_release_dma_owner (idev->dev);iommufd_put_group (idev->igroup);if (!iommufd_selftest_is_mock_dev (idev->dev))iommufd_ctx_put (idev->ictx); } -/** -* iommufd_device_bind - Bind a physical device to an iommu fd -* @ictx: iommufd file descriptor -* @dev: Pointer to a physical device struct -* @id: Output ID number to return to userspace for this device -* -* A successful bind establishes an ownership over the device and returns -* struct iommufd_device pointer, otherwise returns error pointer. -* -* A driver using this API must set driver_managed_dma and must not touch -* the device until this routine succeeds and establishes ownership. -* -* Binding a PCI device places the entire RID under iommufd control. -* -* The caller must undo this with iommufd_device_unbind() -*/ -struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, -struct device *dev,u32 *id) +static int iommufd_bind_iommu (struct iommufd_device *idev) { -struct iommufd_device *idev; +struct iommufd_ctx *ictx = idev->ictx; +struct device *dev = idev->dev;struct iommufd_group *igroup;int rc;
@@ -238,11 +232,11 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,* to restore cache coherency. */ if (!device_iommu_capable (dev,IOMMU_CAP_CACHE_COHERENCY )) -return ERR_PTR (-EINVAL ); +return -EINVAL ; - igroup =iommufd_get_group (ictx, dev); + igroup =iommufd_get_group (idev->ictx, dev);if (IS_ERR (igroup)) -return ERR_CAST (igroup); +return PTR_ERR (igroup);/* * For historical compat with VFIO the insecure interrupt path is
@@ -268,21 +262,69 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,if (rc) goto out_group_put; +/* igroup refcount moves into iommufd_device */ + idev->igroup = igroup; +return 0 ; + +out_group_put: +iommufd_put_group (igroup); +return rc; +} + +/** +* iommufd_device_bind - Bind a physical device to an iommu fd +* @ictx: iommufd file descriptor +* @dev: Pointer to a physical device struct +* @id: Output ID number to return to userspace for this device +* +* A successful bind establishes an ownership over the device and returns +* struct iommufd_device pointer, otherwise returns error pointer. +* +* A driver using this API must set driver_managed_dma and must not touch +* the device until this routine succeeds and establishes ownership. +* +* Binding a PCI device places the entire RID under iommufd control. +* +* The caller must undo this with iommufd_device_unbind() +*/ +struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, +struct device *dev,u32 *id) +{ +struct iommufd_device *idev; +int rc; + idev =iommufd_object_alloc (ictx, idev,IOMMUFD_OBJ_DEVICE ); -if (IS_ERR (idev)) { - rc =PTR_ERR (idev); - goto out_release_owner; - } +if (IS_ERR (idev)) +return idev; + idev->ictx = ictx; -if (!iommufd_selftest_is_mock_dev (dev)) -iommufd_ctx_get (ictx); idev->dev = dev; idev->enforce_cache_coherency =device_iommu_capable (dev,IOMMU_CAP_ENFORCE_CACHE_COHERENCY ); + +if (!is_vfio_noiommu (idev)) { + rc =iommufd_bind_iommu (idev); +if (rc) + goto err_out; + }else { +struct iommufd_group *igroup; + +/* +* Create a dummy igroup, lots of stuff expects ths igroup to be +* present, but a NULL igroup->group is OK +*/ + igroup =iommufd_alloc_group (ictx,NULL ); +if (IS_ERR (igroup)) { + rc =PTR_ERR (igroup); + goto err_out; + } + idev->igroup = igroup; + } + +if (!iommufd_selftest_is_mock_dev (dev)) +iommufd_ctx_get (ictx);/* The calling driver is a user until iommufd_device_unbind() */ refcount_inc (&idev->obj.users); -/* igroup refcount moves into iommufd_device */ - idev->igroup = igroup;/* * If the caller fails after this success it must call
@@ -294,11 +336,14 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, *id = idev->obj.id;return idev; -out_release_owner: -iommu_device_release_dma_owner (dev); -out_group_put: -iommufd_put_group (igroup); +err_out: +/* +* Be careful that iommufd_device_destroy() can handle partial +* initialization. +*/ +iommufd_object_abort_and_destroy (ictx, &idev->obj);return ERR_PTR (rc); + }EXPORT_SYMBOL_NS_GPL (iommufd_device_bind,"IOMMUFD" );
@@ -512,6 +557,9 @@ static int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,struct iommufd_attach_handle *handle;int rc; +if (is_vfio_noiommu (idev)) +return 0 ; +if (!iommufd_hwpt_compatible_device (hwpt, idev))return -EINVAL ;
@@ -559,6 +607,9 @@ static void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt, {struct iommufd_attach_handle *handle; +if (is_vfio_noiommu (idev)) +return ; + handle =iommufd_device_get_attach_handle (idev, pasid);if (pasid ==IOMMU_NO_PASID )iommu_detach_group_handle (hwpt->domain, idev->igroup->group);
@@ -577,6 +628,9 @@ static int iommufd_hwpt_replace_device(struct iommufd_device *idev,struct iommufd_attach_handle *handle, *old_handle;int rc; +if (is_vfio_noiommu (idev)) +return 0 ; +if (!iommufd_hwpt_compatible_device (hwpt, idev))return -EINVAL ;
@@ -652,7 +706,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, goto err_release_devid; } -if (attach_resv) { +if (attach_resv && !is_vfio_noiommu (idev)) { rc =iommufd_device_attach_reserved_iova (idev, hwpt_paging);if (rc) goto err_release_devid;
Signature · 1 line
2.34.1
PATCH v4 4/10 iommufd: Add an ioctl IOMMU_IOAS_GET_PA to query PA from IOVA
In reply to Jacob PanTo support no-IOMMU mode where userspace drivers perform unsafe DMA using physical addresses, introduce a new API to retrieve the physical address of a user-allocated DMA buffer that has been mapped to an IOVA via IOAS. The mapping is backed by mock I/O page tables maintained by generic IOMMUPT framework.
---
v4:
- Fix unaligned IOVA length (Sashiko)
v2:
- Scan the contiguous physical-address span beyond the first page and return its length.
--- drivers/iommu/iommufd/io_pagetable.c | 60 +++++++++++++++++++++++++ drivers/iommu/iommufd/ioas.c | 25 +++++++++++ drivers/iommu/iommufd/iommufd_private.h | 3 ++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 25 +++++++++++ 5 files changed, 116 insertions(+)
drivers/iommu/iommufd/io_pagetable.c +60 -0
@@ -849,6 +849,66 @@ int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova,return iopt_unmap_iova_range (iopt, iova, iova_last, unmapped); } +int iopt_get_phys (struct io_pagetable *iopt,unsigned long iova,u64 *paddr, +u64 *length) +{ +struct iopt_area *area; +u64 tmp_length =0 ; +u64 tmp_paddr =0 ; +int rc =0 ; + +if (!IS_ENABLED (CONFIG_VFIO_NOIOMMU )) +return -EOPNOTSUPP ; + +down_read (&iopt->iova_rwsem); + area =iopt_area_iter_first (iopt, iova, iova); +if (!area || !area->pages) { + rc = -ENOENT ; + goto unlock_exit; + } + +if (!area->storage_domain || + area->storage_domain->owner != &iommufd_noiommu_ops) { + rc = -EOPNOTSUPP ; + goto unlock_exit; + } + + *paddr =iommu_iova_to_phys (area->storage_domain, iova); +if (!*paddr) { + rc = -EINVAL ; + goto unlock_exit; + } + + tmp_length =PAGE_SIZE -offset_in_page (iova); + tmp_paddr = *paddr; +/* +* Scan the domain for the contiguous physical address length so that +* userspace search can be optimized for fewer ioctls. +*/ +while (iova <iopt_area_last_iova (area)) { +unsigned long next_iova; +u64 next_paddr; + +if (check_add_overflow (iova,PAGE_SIZE , &next_iova)) +break ; + + next_paddr =iommu_iova_to_phys (area->storage_domain, next_iova); + +if (!next_paddr || next_paddr != tmp_paddr +PAGE_SIZE ) +break ; + + iova = next_iova; + tmp_paddr +=PAGE_SIZE ; + tmp_length +=PAGE_SIZE ; + } + *length = tmp_length; + +unlock_exit: +up_read (&iopt->iova_rwsem); + +return rc; +} +int iopt_unmap_all (struct io_pagetable *iopt,unsigned long *unmapped) {/* If the IOVAs are empty then unmap all succeeds */
drivers/iommu/iommufd/ioas.c +25 -0
@@ -375,6 +375,31 @@ int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd)return rc; } +int iommufd_ioas_get_pa (struct iommufd_ucmd *ucmd) +{ +struct iommu_ioas_get_pa *cmd = ucmd->cmd; +struct iommufd_ioas *ioas; +int rc; + +if (cmd->flags || cmd->__reserved) +return -EOPNOTSUPP ; + + ioas =iommufd_get_ioas (ucmd->ictx, cmd->ioas_id); +if (IS_ERR (ioas)) +return PTR_ERR (ioas); + + rc =iopt_get_phys (&ioas->iopt, cmd->iova, &cmd->out_phys, + &cmd->out_length); +if (rc) + goto out_put; + + rc =iommufd_ucmd_respond (ucmd,sizeof (*cmd)); +out_put: +iommufd_put_object (ucmd->ictx, &ioas->obj); + +return rc; +} +static void iommufd_release_all_iova_rwsem (struct iommufd_ctx *ictx,struct xarray *ioas_list) {
drivers/iommu/iommufd/iommufd_private.h +3 -0
@@ -118,6 +118,8 @@ int iopt_map_pages(struct io_pagetable *iopt, struct list_head *pages_list,int iopt_unmap_iova (struct io_pagetable *iopt,unsigned long iova,unsigned long length,unsigned long *unmapped);int iopt_unmap_all (struct io_pagetable *iopt,unsigned long *unmapped); +int iopt_get_phys (struct io_pagetable *iopt,unsigned long iova,u64 *paddr, +u64 *length); int iopt_read_and_clear_dirty_data(struct io_pagetable *iopt, struct iommu_domain *domain,
@@ -346,6 +348,7 @@ int iommufd_ioas_map_file(struct iommufd_ucmd *ucmd);int iommufd_ioas_change_process (struct iommufd_ucmd *ucmd);int iommufd_ioas_copy (struct iommufd_ucmd *ucmd);int iommufd_ioas_unmap (struct iommufd_ucmd *ucmd); +int iommufd_ioas_get_pa (struct iommufd_ucmd *ucmd);int iommufd_ioas_option (struct iommufd_ucmd *ucmd);int iommufd_option_rlimit_mode (struct iommu_option *cmd,struct iommufd_ctx *ictx);
drivers/iommu/iommufd/main.c +3 -0
@@ -432,6 +432,7 @@ union ucmd_buffer {struct iommu_veventq_alloc veventq;struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu; +struct iommu_ioas_get_pa get_pa;#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif
@@ -484,6 +485,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { struct iommu_ioas_map_file, iova),IOCTL_OP (IOMMU_IOAS_UNMAP ,iommufd_ioas_unmap ,struct iommu_ioas_unmap ,length ), +IOCTL_OP (IOMMU_IOAS_GET_PA ,iommufd_ioas_get_pa ,struct iommu_ioas_get_pa , +out_phys ),IOCTL_OP (IOMMU_OPTION ,iommufd_option ,struct iommu_option ,val64 ),IOCTL_OP (IOMMU_VDEVICE_ALLOC ,iommufd_vdevice_alloc_ioctl , struct iommu_vdevice_alloc, virt_id),
include/uapi/linux/iommufd.h +25 -0
@@ -57,6 +57,7 @@ enum {IOMMUFD_CMD_IOAS_CHANGE_PROCESS =0x92 ,IOMMUFD_CMD_VEVENTQ_ALLOC =0x93 ,IOMMUFD_CMD_HW_QUEUE_ALLOC =0x94 , + IOMMUFD_CMD_IOAS_GET_PA = 0x95, };/**
@@ -219,6 +220,30 @@ struct iommu_ioas_map { };#define IOMMU_IOAS_MAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP) +/** +* struct iommu_ioas_get_pa - ioctl(IOMMU_IOAS_GET_PA) +* @size: sizeof(struct iommu_ioas_get_pa) +* @flags: Reserved, must be 0 for now +* @ioas_id: IOAS ID to query IOVA to PA mapping from +* @__reserved: Must be 0 +* @iova: IOVA to query +* @out_length: Number of bytes contiguous physical address starting from phys +* @out_phys: Output physical address the IOVA maps to +* +* Query the physical address backing an IOVA range. The entire range must be +* mapped already. For noiommu devices doing unsafe DMA only. +*/ +struct iommu_ioas_get_pa { +__u32 size; +__u32 flags; +__u32 ioas_id; +__u32 __reserved; +__aligned_u64 iova; +__aligned_u64 out_length; +__aligned_u64 out_phys; +}; +#define IOMMU_IOAS_GET_PA _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_GET_PA) +/** * struct iommu_ioas_map_file - ioctl(IOMMU_IOAS_MAP_FILE) * @size: sizeof(struct iommu_ioas_map_file)
Signature · 1 line
2.34.1
PATCH v4 5/10 vfio: Allow null group for noiommu without containers
In reply to Jacob PanIn case of noiommu mode is enabled for VFIO cdev without VFIO container nor IOMMUFD provided compatibility container, there is no need to create a dummy group. Update the group operations to tolerate null group pointer.
---
v4: (Jason)
- Avoid null pointer deref in error unwind
- Add null group check in vfio_device_group_unregister
- repartition to include vfio_device_has_group() in this patch
--- drivers/vfio/group.c | 20 ++++++++++++++++++++ drivers/vfio/vfio.h | 17 +++++++++++++++++ drivers/vfio/vfio_main.c | 14 ++++++++++++++ include/linux/vfio.h | 9 +++++++++ 4 files changed, 60 insertions(+)
drivers/vfio/group.c +20 -0
@@ -390,6 +390,9 @@ int vfio_device_block_group(struct vfio_device *device)struct vfio_group *group = device->group;int ret =0 ; +if (vfio_null_group_allowed () && !group) +return 0 ; +mutex_lock (&group->group_lock);if (group->opened_file) { ret = -EBUSY ;
@@ -407,6 +410,9 @@ void vfio_device_unblock_group(struct vfio_device *device) {struct vfio_group *group = device->group; +if (vfio_null_group_allowed () && !group) +return ; +mutex_lock (&group->group_lock); group->cdev_device_open_cnt--;mutex_unlock (&group->group_lock);
@@ -598,6 +604,14 @@ static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,struct vfio_group *group;int ret; +/* +* With noiommu enabled under cdev interface only, there is no need to +* create a vfio_group if the group based containers are not enabled. +* The cdev interface is exclusively used for iommufd. +*/ +if (vfio_null_group_allowed ()) +return NULL ; + iommu_group =iommu_group_alloc ();if (IS_ERR (iommu_group))return ERR_CAST (iommu_group);
@@ -705,6 +719,9 @@ void vfio_device_remove_group(struct vfio_device *device)struct vfio_group *group = device->group;struct iommu_group *iommu_group; +if (!group) +return ; +if (group->type ==VFIO_NO_IOMMU || group->type ==VFIO_EMULATED_IOMMU )iommu_group_remove_device (device->dev);
@@ -756,6 +773,9 @@ void vfio_device_group_register(struct vfio_device *device)void vfio_device_group_unregister (struct vfio_device *device) { +if (!device->group) +return ; +mutex_lock (&device->group->device_lock);list_del (&device->group_next);mutex_unlock (&device->group->device_lock);
drivers/vfio/vfio.h +17 -0
@@ -114,6 +114,18 @@ bool vfio_device_has_container(struct vfio_device *device);int __init vfio_group_init (void );void vfio_group_cleanup (void ); +/* +* With noiommu enabled and no containers are supported, allow devices that +* don't have a dummy group. +*/ +static inline bool vfio_null_group_allowed (void ) +{ +if (vfio_noiommu && (!IS_ENABLED (CONFIG_VFIO_CONTAINER ) && !IS_ENABLED (CONFIG_IOMMUFD_VFIO_CONTAINER ))) +return true; + +return false; +} +static inline bool vfio_device_is_noiommu (struct vfio_device *vdev) { return IS_ENABLED(CONFIG_VFIO_NOIOMMU) &&
@@ -190,6 +202,11 @@ static inline void vfio_group_cleanup(void) { } +static inline bool vfio_null_group_allowed (void ) +{ +return false; +} +static inline bool vfio_device_is_noiommu (struct vfio_device *vdev) {return false;
drivers/vfio/vfio_main.c +14 -0
@@ -358,6 +358,10 @@ static int __vfio_register_dev(struct vfio_device *device,/* Refcounting can't start until the driver calls register */ refcount_set (&device->refcount,1 ); +/* noiommu device w/o container may have NULL group */ +if (!vfio_device_has_group (device)) +return 0 ; +vfio_device_group_register (device);vfio_device_debugfs_init (device);
@@ -392,6 +396,16 @@ void vfio_unregister_group_dev(struct vfio_device *device)bool interrupted = false;long rc; +/* +* For noiommu devices without a container, thus no dummy group, +* simply delete and unregister to balance refcount. +*/ +if (!vfio_device_has_group (device)) { +vfio_device_del (device); +vfio_device_put_registration (device); +return ; + } +/* * Prevent new device opened by userspace via the * VFIO_GROUP_GET_DEVICE_FD in the group path.
include/linux/vfio.h +9 -0
@@ -328,6 +328,10 @@ struct iommu_group *vfio_file_iommu_group(struct file *file);#if IS_ENABLED (CONFIG_VFIO_GROUP )bool vfio_file_is_group (struct file *file);bool vfio_file_has_dev (struct file *file,struct vfio_device *device); +static inline bool vfio_device_has_group (struct vfio_device *device) +{ +return device->group; +}#else static inline bool vfio_file_is_group (struct file *file) {
@@ -338,6 +342,11 @@ static inline bool vfio_file_has_dev(struct file *file, struct vfio_device *devi {return false; } + +static inline bool vfio_device_has_group (struct vfio_device *device) +{ +return false; +}#endif bool vfio_file_is_valid (struct file *file);bool vfio_file_enforced_coherent (struct file *file);
Signature · 1 line
2.34.1
PATCH v4 6/10 vfio: Introduce and set noiommu flag on vfio_device
In reply to Jacob PanWhen a VFIO device is added to a noiommu group, set the noiommu flag on the vfio_device structure to indicate that the device operates in noiommu mode.
Also update function signatures to pass vfio_device instead of device, which has the direct access to the noiommu flag.
---
v3:
- Squashed with vfio noiommu logic check patch 7/11 of v2 (Mostafa)
--- drivers/vfio/group.c | 21 +++++++++++---------- drivers/vfio/vfio.h | 3 +-- include/linux/vfio.h | 1 + 3 files changed, 13 insertions(+), 12 deletions(-)
drivers/vfio/group.c +11 -10
@@ -597,7 +597,7 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,return ret; } -static struct vfio_group *vfio_noiommu_group_alloc (struct device *dev, +static struct vfio_group *vfio_noiommu_group_alloc (struct vfio_device *vdev,enum vfio_group_type type) {struct iommu_group *iommu_group;
@@ -619,7 +619,7 @@ static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev, ret =iommu_group_set_name (iommu_group,"vfio-noiommu" );if (ret) goto out_put_group; - ret =iommu_group_add_device (iommu_group, dev); + ret =iommu_group_add_device (iommu_group, vdev->dev);if (ret) goto out_put_group;
@@ -634,7 +634,7 @@ static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,return group; out_remove_device: -iommu_group_remove_device (dev); +iommu_group_remove_device (vdev->dev); out_put_group:iommu_group_put (iommu_group);return ERR_PTR (ret);
@@ -655,23 +655,24 @@ static bool vfio_group_has_device(struct vfio_group *group, struct device *dev)return false; } -static struct vfio_group *vfio_group_find_or_alloc (struct device *dev) +static struct vfio_group *vfio_group_find_or_alloc (struct vfio_device *vdev) {struct iommu_group *iommu_group;struct vfio_group *group; - iommu_group =iommu_group_get (dev); + iommu_group =iommu_group_get (vdev->dev);if (!iommu_group && vfio_noiommu) { + vdev->noiommu =1 ;/* * With noiommu enabled, create an IOMMU group for devices that * don't already have one, implying no IOMMU hardware/driver * exists. Taint the kernel because we're about to give a DMA * capable device to a user without IOMMU protection. */ - group =vfio_noiommu_group_alloc (dev,VFIO_NO_IOMMU ); + group =vfio_noiommu_group_alloc (vdev,VFIO_NO_IOMMU );if (!IS_ERR (group)) {add_taint (TAINT_USER ,LOCKDEP_STILL_OK ); -dev_warn (dev,"Adding kernel taint for vfio-noiommu group on device\n" ); +dev_warn (vdev->dev,"Adding kernel taint for vfio-noiommu group on device\n" ); }return group; }
@@ -682,7 +683,7 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)mutex_lock (&vfio.group_lock); group =vfio_group_find_from_iommu (iommu_group);if (group) { -if (WARN_ON (vfio_group_has_device (group, dev))) +if (WARN_ON (vfio_group_has_device (group, vdev->dev))) group =ERR_PTR (-EINVAL );else refcount_inc (&group->drivers);
@@ -702,9 +703,9 @@ int vfio_device_set_group(struct vfio_device *device,struct vfio_group *group;if (type ==VFIO_IOMMU ) - group =vfio_group_find_or_alloc (device->dev); + group =vfio_group_find_or_alloc (device);else - group =vfio_noiommu_group_alloc (device->dev, type); + group =vfio_noiommu_group_alloc (device, type);if (IS_ERR (group))return PTR_ERR (group);
drivers/vfio/vfio.h +1 -2
@@ -128,8 +128,7 @@ static inline bool vfio_null_group_allowed(void)static inline bool vfio_device_is_noiommu (struct vfio_device *vdev) { -return IS_ENABLED (CONFIG_VFIO_NOIOMMU ) && - vdev->group->type ==VFIO_NO_IOMMU ; +return IS_ENABLED (CONFIG_VFIO_NOIOMMU ) && vdev->noiommu; }#else struct vfio_group ;
include/linux/vfio.h +1 -0
@@ -72,6 +72,7 @@ struct vfio_device { u8 iommufd_attached:1;#endif u8 cdev_opened:1; + u8 noiommu:1;#ifdef CONFIG_DEBUG_FS /* * debug_root is a static property of the vfio_device
Signature · 1 line
2.34.1
PATCH v4 7/10 vfio: Enable cdev noiommu mode under iommufd
In reply to Jacob PanNow that devices under noiommu mode can bind with IOMMUFD and perform IOAS operations, lift restrictions on cdev from VFIO side.
No IOMMU cdevs are explicitly named with noiommu prefix. e.g.
/dev/vfio/
|-- 7
|-- devices
| `-- noiommu-vfio0
`-- vfio---
v4:
- Move vfio_device_has_group() related out to 5/10
- Keep wait loop in vfio_unregister_group_dev (Jason)
v3:
- Add explict dependency on !GENERIC_ATOMIC64
v2:
- Fix build dependency on IOMMU_SUPPORT
--- drivers/vfio/Kconfig | 8 ++++++-- drivers/vfio/iommufd.c | 7 ------- drivers/vfio/vfio.h | 8 +------- drivers/vfio/vfio_main.c | 20 ++++++-------------- 4 files changed, 13 insertions(+), 30 deletions(-)
drivers/vfio/Kconfig +6 -2
@@ -22,8 +22,7 @@ config VFIO_DEVICE_CDEV The VFIO device cdev is another way for userspace to get device access. Userspace gets device fd by opening device cdev under /dev/vfio/devices/vfioX, and then bind the device fd with an iommufd - to set up secure DMA context for device access. This interface does - not support noiommu. + to set up secure DMA context for device access. If you don't know what to do here, say N.
@@ -63,6 +62,11 @@ endif config VFIO_NOIOMMU bool "VFIO No-IOMMU support" depends on VFIO_GROUP + depends on !GENERIC_ATOMIC64 # IOMMU_PT_AMDV1 requires cmpxchg64 + select GENERIC_PT + select IOMMU_PT + select IOMMU_PT_AMDV1 + depends on IOMMU_SUPPORT help VFIO is built on the ability to isolate devices using the IOMMU. Only with an IOMMU can userspace access to DMA capable devices be
drivers/vfio/iommufd.c +0 -7
@@ -25,10 +25,6 @@ int vfio_df_iommufd_bind(struct vfio_device_file *df)lockdep_assert_held (&vdev->dev_set->lock); -/* Returns 0 to permit device opening under noiommu mode */ -if (vfio_device_is_noiommu (vdev)) -return 0 ; -return vdev->ops->bind_iommufd (vdev, ictx, &df->devid); }
@@ -58,9 +54,6 @@ void vfio_df_iommufd_unbind(struct vfio_device_file *df)lockdep_assert_held (&vdev->dev_set->lock); -if (vfio_device_is_noiommu (vdev)) -return ; -if (vdev->ops->unbind_iommufd) vdev->ops->unbind_iommufd (vdev); }
drivers/vfio/vfio.h +1 -7
@@ -376,19 +376,13 @@ void vfio_init_device_cdev(struct vfio_device *device);static inline int vfio_device_add (struct vfio_device *device) { -/* cdev does not support noiommu device */ -if (vfio_device_is_noiommu (device)) -return device_add (&device->device);vfio_init_device_cdev (device);return cdev_device_add (&device->cdev, &device->device); }static inline void vfio_device_del (struct vfio_device *device) { -if (vfio_device_is_noiommu (device)) -device_del (&device->device); -else -cdev_device_del (&device->cdev, &device->device); +cdev_device_del (&device->cdev, &device->device); }int vfio_device_fops_cdev_open (struct inode *inode,struct file *filep);
drivers/vfio/vfio_main.c +6 -14
@@ -332,13 +332,15 @@ static int __vfio_register_dev(struct vfio_device *device,if (!device->dev_set)vfio_assign_device_set (device, device); - ret =dev_set_name (&device->device,"vfio%d" , device->index); + ret =vfio_device_set_group (device, type);if (ret)return ret; - ret =vfio_device_set_group (device, type); +/* Just to be safe, expose to user explicitly noiommu cdev node */ + ret =dev_set_name (&device->device,"%svfio%d" , + device->noiommu ?"noiommu-" :"" , device->index);if (ret) -return ret; + goto err_out;/* * VFIO always sets IOMMU_CACHE because we offer no way for userspace to
@@ -359,7 +361,7 @@ static int __vfio_register_dev(struct vfio_device *device,refcount_set (&device->refcount,1 );/* noiommu device w/o container may have NULL group */ -if (!vfio_device_has_group (device)) +if (vfio_device_is_noiommu (device) && !vfio_device_has_group (device))return 0 ;vfio_device_group_register (device);
@@ -396,16 +398,6 @@ void vfio_unregister_group_dev(struct vfio_device *device)bool interrupted = false;long rc; -/* -* For noiommu devices without a container, thus no dummy group, -* simply delete and unregister to balance refcount. -*/ -if (!vfio_device_has_group (device)) { -vfio_device_del (device); -vfio_device_put_registration (device); -return ; - } -/* * Prevent new device opened by userspace via the * VFIO_GROUP_GET_DEVICE_FD in the group path.
Signature · 1 line
2.34.1
PATCH v4 8/10 vfio:selftest: Handle VFIO noiommu cdev
In reply to Jacob PanWith unsafe DMA noiommu mode, the vfio devices are prefixed with noiommu-, e.g./dev/vfio/
|-- devices | `-- noiommu-vfio0 |-- noiommu-0
`-- vfio
Let vfio tests, such as luo kexec test, accommodate the noiommu device files.
--- .../lib/include/libvfio/vfio_pci_device.h | 1 + .../selftests/vfio/lib/vfio_pci_device.c | 32 ++++++++++++++++--- .../vfio/vfio_pci_liveupdate_kexec_test.c | 9 ++++++ 3 files changed, 38 insertions(+), 4 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +1 -0
@@ -124,6 +124,7 @@ static inline bool vfio_pci_device_match(struct vfio_pci_device *device, }const char *vfio_pci_get_cdev_path (const char *bdf); +int vfio_pci_noiommu_mode_enabled (void );/* Low-level routines for setting up a struct vfio_pci_device */ struct vfio_pci_device *vfio_pci_device_alloc (const char *bdf,struct iommu *iommu);
tools/testing/selftests/vfio/lib/vfio_pci_device.c +28 -4
@@ -225,11 +225,14 @@ void vfio_pci_group_setup(struct vfio_pci_device *device)struct vfio_group_status group_status = { .argsz =sizeof (group_status), }; -char group_path[32 ]; +char group_path[64 ];int group; group =vfio_pci_get_group_from_dev (device->bdf); -snprintf (group_path,sizeof (group_path),"/dev/vfio/%d" , group); +if (vfio_pci_noiommu_mode_enabled ()) +snprintf (group_path,sizeof (group_path),"/dev/vfio/noiommu-%d" , group); +else +snprintf (group_path,sizeof (group_path),"/dev/vfio/%d" , group); device->group_fd =open (group_path,O_RDWR );VFIO_ASSERT_GE (device->group_fd,0 ,"open(%s) failed\n" , group_path);
@@ -294,6 +297,24 @@ static void vfio_pci_device_setup(struct vfio_pci_device *device) device->msi_eventfds[i] =-1 ; } + +int vfio_pci_noiommu_mode_enabled (void ) +{ +const char *path ="/sys/module/vfio/parameters/enable_unsafe_noiommu_mode" ; +FILE *f; +int c; + + f =fopen (path,"re" ); +if (!f) +return 0 ; + + c =fgetc (f); +fclose (f); +if (c =='Y' || c =='y' ) +return 1 ; +return 0 ; +} +const char *vfio_pci_get_cdev_path (const char *bdf) {char dir_path[PATH_MAX ];
@@ -310,8 +331,11 @@ const char *vfio_pci_get_cdev_path(const char *bdf)VFIO_ASSERT_NOT_NULL (dir,"Failed to open directory %s\n" , dir_path);while ((entry =readdir (dir)) !=NULL ) { -/* Find the file that starts with "vfio" */ -if (strncmp ("vfio" , entry->d_name,4 )) +/* Find the file that starts with "noiommu-vfio" or "vfio" */ +if (vfio_pci_noiommu_mode_enabled ()) { +if (strncmp ("noiommu-vfio" , entry->d_name,strlen ("noiommu-vfio" ))) +continue ; + }else if (strncmp ("vfio" , entry->d_name,4 ))continue ;snprintf (cdev_path,PATH_MAX ,"/dev/vfio/devices/%s" , entry->d_name);
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +9 -0
@@ -159,6 +159,15 @@ static void check_open_vfio_device_fails(void)VFIO_ASSERT_EQ (errno,EBUSY );free ((void *)cdev_path); +/* +* In no-IOMMU mode the group lives at /dev/vfio/noiommu-<N> and +* cannot be added to a Type1 IOMMU container, so the container-based +* access check below doesn't apply. The cdev check above already +* covers that the device is inaccessible in this mode. +*/ +if (vfio_pci_noiommu_mode_enabled ()) +return ; +for (i =0 ; i < nr_iommu_modes; i++) {if (!iommu_modes[i].container_path)continue ;
Signature · 1 line
2.34.1
PATCH v4 9/10 selftests/vfio: Add iommufd noiommu mode selftest for cdev
In reply to Jacob PanAdd comprehensive selftest for VFIO device operations with iommufd in noiommu mode. Tests cover:
- Device binding to iommufd
- IOAS (I/O Address Space) allocation, mapping with dummy IOVA
- Retrieve PA from dummy IOVA
- Device attach/detach operations as usual
---
v4:
- Add a test case for unaligned IOVA.
v2:
- Use huge page ioas map to test GET_PA searching for contiguous PA range.
--- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_iommufd_noiommu_test.c | 567 ++++++++++++++++++ 2 files changed, 568 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_noiommu_test.c
tools/testing/selftests/vfio/Makefile +1 -0
@@ -9,6 +9,7 @@ CFLAGS = $(KHDR_INCLUDES) TEST_GEN_PROGS += vfio_dma_mapping_test TEST_GEN_PROGS += vfio_dma_mapping_mmio_test TEST_GEN_PROGS += vfio_iommufd_setup_test +TEST_GEN_PROGS += vfio_iommufd_noiommu_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_device_init_perf_test TEST_GEN_PROGS += vfio_pci_driver_test
tools/testing/selftests/vfio/vfio_iommufd_noiommu_test.c new file +567 -0
567 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 10/10 Documentation: Update VFIO NOIOMMU mode
In reply to Jacob PanDocument the NOIOMMU mode with newly added cdev support under iommufd.
--- Documentation/driver-api/vfio.rst | 45 +++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-)
Documentation/driver-api/vfio.rst +43 -2
@@ -275,8 +275,6 @@ in a VFIO group. With CONFIG_VFIO_DEVICE_CDEV=y the user can now acquire a device fd by directly opening a character device /dev/vfio/devices/vfioX where "X" is the number allocated uniquely by VFIO for registered devices. -cdev interface does not support noiommu devices, so user should use -the legacy group interface if noiommu is wanted. The cdev only works with IOMMUFD. Both VFIO drivers and applications must adapt to the new cdev security model which requires using
@@ -370,6 +368,49 @@ IOMMUFD IOAS/HWPT to enable userspace DMA:: /* Other device operations as stated in "VFIO Usage Example" */ +VFIO NOIOMMU mode +------------------------------------------------------------------------------- +VFIO also supports a no-IOMMU mode, intended for usages where unsafe DMA can +be performed by userspace drivers w/o physical IOMMU protection. This mode +is controlled by the parameter: + +/sys/module/vfio/parameters/enable_unsafe_noiommu_mode + +Upon enabling this mode, with an assigned device, the user will be presented +with a VFIO group and device file, e.g.:: + + /dev/vfio/ + |-- devices + | `-- noiommu-vfio0 /* VFIO device cdev */ + |-- noiommu-0 /* VFIO group */ + `-- vfio + +The capabilities vary depending on the device programming interface and kernel +configuration used. The following table summarizes the differences: + ++-------------------+---------------------+----------------------+ +| Feature | VFIO group | VFIO device cdev | ++===================+=====================+======================+ +| VFIO device UAPI | Yes | Yes | ++-------------------+---------------------+----------------------+ +| VFIO container | No | No | ++-------------------+---------------------+----------------------+ +| IOMMUFD IOAS | No | Yes* | ++-------------------+---------------------+----------------------+ + +Note that the VFIO container case includes IOMMUFD provided VFIO compatibility +interfaces when either CONFIG_VFIO_CONTAINER or CONFIG_IOMMUFD_VFIO_CONTAINER is +enabled. + +* IOMMUFD UAPI is available for VFIO device cdev to pin and map user memory with + the ability to retrieve physical addresses for DMA command submission. + +A new IOMMUFD ioctl IOMMU_IOAS_GET_PA is added to retrieve the physical address +for a given user virtual address. Note that IOMMU_IOAS_MAP_FIXED_IOVA flag is +ignored in no-IOMMU mode since there is no physical DMA remapping hardware. +tools/testing/selftests/vfio/vfio_iommufd_noiommu_test.c provides an example of +using this ioctl in no-IOMMU mode. + VFIO User API -------------------------------------------------------------------------------
Signature · 1 line
2.34.1
Tian, Kevin
maintainerRE: [PATCH V4 01/10] iommufd: Support a HWPT without an iommu driver for noiommu
In reply to Jacob Pandrivers/iommu/iommufd/hwpt_noiommu.c line 4
Quoted 7 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AMdrivers/iommu/iommufd/hwpt_noiommu.c new file +4
-0+// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES +*/
the years are not latest...
Quoted 18 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +17
-0+ +static struct iommu_domain * +noiommu_alloc_paging_flags (struct device *dev,u32 flags, +const struct iommu_user_data *user_data) +{ +struct pt_iommu_amdv1_cfg cfg = {}; +struct noiommu_domain *dom; +int rc; + +if (flags || user_data) +return ERR_PTR (-EOPNOTSUPP ); + + cfg.common.hw_max_vasz_lg2 =64 ; + cfg.common.hw_max_oasz_lg2 =52 ; + cfg.starting_level =2 ; + cfg.common.features = + (BIT(PT_FEAT_DYNAMIC_TOP) |
BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES)|
drivers/iommu/iommufd/hwpt_noiommu.c line 54
Quoted 1 line
drivers/iommu/iommufd/hwpt_noiommu.c new file +1
-0+ BIT(PT_FEAT_AMDV1_FORCE_COHERENCE));
PT_FEAT_AMDV1_ENCRYPT_TABLES is useless here.
Quoted 8 lines
+/*
+ * AMDV1 is used as a dummy page table for no-IOMMU mode, similar to the
+ * iommufd selftest mock page table.
+ * Unlike legacy VFIO no-IOMMU mode, where no container level APIs are
+ * supported, this allows IOAS and hwpt objects to exist without hardware
+ * IOMMU support. IOVAs are used only for IOVA-to-PA lookups not for
+ * hardware translation in DMA.
it's confusing between above "legacy VFIO no-IOMMU mode" and following "legacy VFIO group-container based noiommu mode".
Quoted 29 lines
+ *
+ * This is only used with iommufd and cdev-based interfaces and does not
+ * apply to legacy VFIO group-container based noiommu mode.
+ */drivers/iommu/iommufd/hwpt_noiommu.c new file +8
-0+static const struct iommu_domain_ops noiommu_amdv1_ops = { +IOMMU_PT_DOMAIN_OPS (amdv1), + .free = noiommu_domain_free, +}; + +const struct iommu_ops iommufd_noiommu_ops = { + .domain_alloc_paging_flags = noiommu_alloc_paging_flags, +};drivers/iommu/iommufd/iommufd_private.h +2
-0@@ -464,6 +464,8 @@ static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx,refcount_dec (&hwpt->obj.users); } +extern const struct iommu_ops iommufd_noiommu_ops; +struct iommufd_attach ;struct
iommufd_group{
--
2.34.1
RE: [PATCH V4 02/10] iommufd: Move igroup allocation to a function
In reply to Jacob PanQuoted 11 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AMFrom: Jason Gunthorpe <[email protected]>
So it can be reused in the next patch which allows binding to noiommu device.
Reviewed-by Samiullah Khawaja <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>
with a nit:
drivers/iommu/iommufd/device.c static bool iommufd_group_try_get(struct iommufd_group *igroup, · line 78
Quoted 5 lines
drivers/iommu/iommufd/device.c +5
-0+ +/* +* The ictx is not additionally refcounted here becase all objects using +* an igroup must put it before their destroy completes. +*/
s/becase/because/, though it's not added by this patch
Tian, Kevin
maintainerRE: [PATCH V4 03/10] iommufd: Allow binding to a noiommu device
In reply to Jacob Pandrivers/iommu/iommufd/device.c struct iommufd_attach { · line 30
Quoted 8 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AMdrivers/iommu/iommufd/device.c +5
-0+static bool is_vfio_noiommu (struct iommufd_device *idev) +{ +return !device_iommu_mapped (idev->dev) || !idev->dev->iommu; +} +
could you add some words why both conditions are checked? It's not obvious to me.
btw there is no need to add "_vfio_" in the name.
drivers/iommu/iommufd/device.c struct iommufd_device · line 238
Quoted 10 lines
drivers/iommu/iommufd/device.c +2
-2@@ -238,11 +232,11 @@ struct iommufd_device*iommufd_device_bind(struct iommufd_ctx *ictx, * to restore cache coherency. */ if (!device_iommu_capable (dev,IOMMU_CAP_CACHE_COHERENCY )) -return ERR_PTR (-EINVAL ); +return -EINVAL ; - igroup =iommufd_get_group (ictx, dev); + igroup =iommufd_get_group (idev->ictx, dev);
pointless change.
Quoted 32 lines
drivers/iommu/iommufd/device.c +9
-6+struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, +struct device *dev,u32 *id) +{ +struct iommufd_device *idev; +int rc; + idev =iommufd_object_alloc (ictx, idev,IOMMUFD_OBJ_DEVICE ); -if (IS_ERR (idev)) { - rc =PTR_ERR (idev); - goto out_release_owner; - } +if (IS_ERR (idev)) +return idev; + idev->ictx = ictx; -if (!iommufd_selftest_is_mock_dev (dev)) -iommufd_ctx_get (ictx); idev->dev = dev; idev->enforce_cache_coherency = device_iommu_capable(dev,
IOMMU_CAP_ENFORCE_CACHE_COHERENCY);drivers/iommu/iommufd/device.c +10
-0+ +if (!is_vfio_noiommu (idev)) { + rc =iommufd_bind_iommu (idev); +if (rc) + goto err_out; + }else { +struct iommufd_group *igroup; + +/* +* Create a dummy igroup, lots of stuff expects ths igroup to be
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 321
Quoted 8 lines
drivers/iommu/iommufd/device.c +8
-0+* present, but a NULL igroup->group is OK +*/ + igroup =iommufd_alloc_group (ictx,NULL ); +if (IS_ERR (igroup)) { + rc =PTR_ERR (igroup); + goto err_out; + } + idev->igroup = igroup;
though simple it's slightly clearer to move them into a wrapper e.g. iommufd_bind_dummy_iommu().
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 325
Quoted 4 lines
drivers/iommu/iommufd/device.c +4
-0+ } + +if (!iommufd_selftest_is_mock_dev (dev)) +iommufd_ctx_get (ictx);
better add a comment for the ordering here - this must be done here otherwise iommufd_device_destroy() will have a problem to handle partial initialization.
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 285
Quoted 4 lines
drivers/iommu/iommufd/device.c +0
-2/* The calling driver is a user until iommufd_device_unbind() */ refcount_inc (&idev->obj.users); -/* igroup refcount moves into iommufd_device */ - idev->igroup = igroup;
this is common, why not leaving it here?
Tian, Kevin
maintainerRE: [PATCH V4 04/10] iommufd: Add an ioctl IOMMU_IOAS_GET_PA to query PA from IOVA
In reply to Jacob PanQuoted 10 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AMTo support no-IOMMU mode where userspace drivers perform unsafe DMA using physical addresses, introduce a new API to retrieve the physical address of a user-allocated DMA buffer that has been mapped to an IOVA via IOAS. The mapping is backed by mock I/O page tables maintained
by generic IOMMUPT framework.
Quoted 3 lines
Suggested-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>
Your s-o-b should be the last
Quoted 13 lines
drivers/iommu/iommufd/io_pagetable.c +12
-0+/* +* Scan the domain for the contiguous physical address length so that +* userspace search can be optimized for fewer ioctls. +*/ +while (iova <iopt_area_last_iova (area)) { +unsigned long next_iova; +u64 next_paddr; + +if (check_add_overflow (iova,PAGE_SIZE , &next_iova)) +break ; + + next_paddr = iommu_iova_to_phys(area->storage_domain,
next_iova);
drivers/iommu/iommufd/io_pagetable.c int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova, · line 898
Quoted 3 lines
drivers/iommu/iommufd/io_pagetable.c +3
-0+ +if (!next_paddr || next_paddr != tmp_paddr +PAGE_SIZE ) +break ;
the 2nd condition should be WARN_ON?
drivers/iommu/iommufd/main.c union ucmd_buffer { · line 435
Quoted 5 lines
drivers/iommu/iommufd/main.c +1
-0@@ -432,6 +432,7 @@ union ucmd_buffer {struct iommu_veventq_alloc veventq;struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu; +struct iommu_ioas_get_pa get_pa;
alphabetic order
drivers/iommu/iommufd/main.c static const struct iommufd_ioctl_op · line 492
Quoted 12 lines
drivers/iommu/iommufd/main.c +2
-0#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif @@ -484,6 +485,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { struct iommu_ioas_map_file, iova),IOCTL_OP (IOMMU_IOAS_UNMAP ,iommufd_ioas_unmap ,struct iommu_ioas_unmap ,length ), +IOCTL_OP (IOMMU_IOAS_GET_PA ,iommufd_ioas_get_pa ,struct iommu_ioas_get_pa , + out_phys),
ditto
Quoted 26 lines
+/**
+ * structiommu_ioas_get_pa-ioctl(IOMMU_IOAS_GET_PA)
+ * @size:sizeof(struct iommu_ioas_get_pa)
+ * @flags: Reserved, must be 0 for now
+ * @ioas_id: IOAS ID to query IOVA to PA mapping from
+ * @__reserved: Must be 0
+ * @iova: IOVA to query
+ * @out_length: Number of bytes contiguous physical address starting from phys
+ * @out_phys: Output physical address the IOVA maps to
+ *
+ * Query the physical address backing an IOVA range. The entire range must be
+ * mapped already. For noiommu devices doing unsafe DMA only.
+ */include/uapi/linux/iommufd.h +10
-0+struct iommu_ioas_get_pa { +__u32 size; +__u32 flags; +__u32 ioas_id; +__u32 __reserved; +__aligned_u64 iova; +__aligned_u64 out_length; +__aligned_u64 out_phys; +}; +#define IOMMU_IOAS_GET_PA _IO(IOMMUFD_TYPE,
Quoted 2 lines
IOMMUFD_CMD_IOAS_GET_PA)
+
Based on its purpose let's make the restriction in the name, e.g.
IOMMUFD_CMD_IOAS_NOIOMMU_GET_PA
Tian, Kevin
maintainerRE: [PATCH V4 05/10] vfio: Allow null group for noiommu without containers
In reply to Jacob PanQuoted 12 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AM+/*
+ * With noiommu enabled and no containers are supported, allow devices that
+ * don't have a dummy group.
+ */drivers/vfio/vfio.h +3
-0+static inline bool vfio_null_group_allowed (void ) +{ + if (vfio_noiommu && (!IS_ENABLED(CONFIG_VFIO_CONTAINER)&& !IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)))
drivers/vfio/vfio.h bool vfio_device_has_container(struct vfio_device *device); · line 127
Quoted 4 lines
drivers/vfio/vfio.h +4
-0+return true; + +return false; +}
'allow' kind of implies a policy choice, but throughput this patch it's actually a mandatory state when those conditions are true. Can we strengthen the words here and other places to not given an impression that even when null group is allowed it's still possible to create a vfio group for that situation?
Just like another comment said, iommufd is the only choice in this case, then probably could call it vfio_has_iommufd_group()?
Quoted 7 lines
drivers/vfio/vfio_main.c +1
-0@@ -392,6 +396,16 @@ void vfio_unregister_group_dev(struct vfio_device *device)bool interrupted = false;long rc; +/*
drivers/vfio/vfio_main.c void vfio_unregister_group_dev(struct vfio_device · line 410
Quoted 10 lines
drivers/vfio/vfio_main.c +9
-0+* For noiommu devices without a container, thus no dummy group, +* simply delete and unregister to balance refcount. +*/ +if (!vfio_device_has_group (device)) { +vfio_device_del (device); +vfio_device_put_registration (device); +return ; + } +/*
can you elaborate why the following wait loop is also skipped? then above is removed by patch7 later...
Alex Williamson
maintainerRe: [PATCH V4 04/10] iommufd: Add an ioctl IOMMU_IOAS_GET_PA to query PA from IOVA
In reply to Jacob PanQuoted 73 lines · On Tue, 14 Apr 2026 14:14:06 -0700 Jacob Pan <[email protected]>…
To support no-IOMMU mode where userspace drivers perform unsafe DMA using physical addresses, introduce a new API to retrieve the physical address of a user-allocated DMA buffer that has been mapped to an IOVA via IOAS. The mapping is backed by mock I/O page tables maintained by generic IOMMUPT framework.
Suggested-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>---
v4:
- Fix unaligned IOVA length (Sashiko)
v2:
- Scan the contiguous physical-address span beyond the first page and return its length.--- drivers/iommu/iommufd/io_pagetable.c | 60 +++++++++++++++++++++++++ drivers/iommu/iommufd/ioas.c | 25 +++++++++++ drivers/iommu/iommufd/iommufd_private.h | 3 ++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 25 +++++++++++ 5 files changed, 116 insertions(+)drivers/iommu/iommufd/io_pagetable.c +43
-0@@ -849,6 +849,66 @@ int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova,return iopt_unmap_iova_range (iopt, iova, iova_last, unmapped); } +int iopt_get_phys (struct io_pagetable *iopt,unsigned long iova,u64 *paddr, +u64 *length) +{ +struct iopt_area *area; +u64 tmp_length =0 ; +u64 tmp_paddr =0 ; +int rc =0 ; + +if (!IS_ENABLED (CONFIG_VFIO_NOIOMMU )) +return -EOPNOTSUPP ; + +down_read (&iopt->iova_rwsem); + area =iopt_area_iter_first (iopt, iova, iova); +if (!area || !area->pages) { + rc = -ENOENT ; + goto unlock_exit; + } + +if (!area->storage_domain || + area->storage_domain->owner != &iommufd_noiommu_ops) { + rc = -EOPNOTSUPP ; + goto unlock_exit; + } + + *paddr =iommu_iova_to_phys (area->storage_domain, iova); +if (!*paddr) { + rc = -EINVAL ; + goto unlock_exit; + } + + tmp_length =PAGE_SIZE -offset_in_page (iova); + tmp_paddr = *paddr; +/* +* Scan the domain for the contiguous physical address length so that +* userspace search can be optimized for fewer ioctls. +*/ +while (iova <iopt_area_last_iova (area)) { +unsigned long next_iova; +u64 next_paddr; + +if (check_add_overflow (iova,PAGE_SIZE , &next_iova)) +break ; +
drivers/iommu/iommufd/io_pagetable.c int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova, · line 904
Quoted 10 lines
drivers/iommu/iommufd/io_pagetable.c +10
-0+ next_paddr =iommu_iova_to_phys (area->storage_domain, next_iova); + +if (!next_paddr || next_paddr != tmp_paddr +PAGE_SIZE ) +break ; + + iova = next_iova; + tmp_paddr +=PAGE_SIZE ; + tmp_length +=PAGE_SIZE ; + } + *length = tmp_length;
If next_iova exceeds iopt_area_last_iova(area) AND exists in storage_domain AND happens to be physically contiguous, tmp_length is advanced and the return length exceeds the mapping. Otherwise this also always does an iommu_iova_to_phys() one iteration beyond the area last iova. Thanks,
Alex
Quoted 131 lines
drivers/iommu/iommufd/io_pagetable.c +7
-0+ +unlock_exit: +up_read (&iopt->iova_rwsem); + +return rc; +} +int iopt_unmap_all (struct io_pagetable *iopt,unsigned long *unmapped) {/* If the IOVAs are empty then unmap all succeeds */ drivers/iommu/iommufd/ioas.c +25
-0@@ -375,6 +375,31 @@ int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd)return rc; } +int iommufd_ioas_get_pa (struct iommufd_ucmd *ucmd) +{ +struct iommu_ioas_get_pa *cmd = ucmd->cmd; +struct iommufd_ioas *ioas; +int rc; + +if (cmd->flags || cmd->__reserved) +return -EOPNOTSUPP ; + + ioas =iommufd_get_ioas (ucmd->ictx, cmd->ioas_id); +if (IS_ERR (ioas)) +return PTR_ERR (ioas); + + rc =iopt_get_phys (&ioas->iopt, cmd->iova, &cmd->out_phys, + &cmd->out_length); +if (rc) + goto out_put; + + rc =iommufd_ucmd_respond (ucmd,sizeof (*cmd)); +out_put: +iommufd_put_object (ucmd->ictx, &ioas->obj); + +return rc; +} +static void iommufd_release_all_iova_rwsem (struct iommufd_ctx *ictx,struct xarray *ioas_list) {drivers/iommu/iommufd/iommufd_private.h +3
-0@@ -118,6 +118,8 @@ int iopt_map_pages(struct io_pagetable *iopt, struct list_head *pages_list,int iopt_unmap_iova (struct io_pagetable *iopt,unsigned long iova,unsigned long length,unsigned long *unmapped);int iopt_unmap_all (struct io_pagetable *iopt,unsigned long *unmapped); +int iopt_get_phys (struct io_pagetable *iopt,unsigned long iova,u64 *paddr, +u64 *length); int iopt_read_and_clear_dirty_data(struct io_pagetable *iopt, struct iommu_domain *domain,@@ -346,6 +348,7 @@ int iommufd_ioas_map_file(struct iommufd_ucmd *ucmd);int iommufd_ioas_change_process (struct iommufd_ucmd *ucmd);int iommufd_ioas_copy (struct iommufd_ucmd *ucmd);int iommufd_ioas_unmap (struct iommufd_ucmd *ucmd); +int iommufd_ioas_get_pa (struct iommufd_ucmd *ucmd);int iommufd_ioas_option (struct iommufd_ucmd *ucmd);int iommufd_option_rlimit_mode (struct iommu_option *cmd,struct iommufd_ctx *ictx);drivers/iommu/iommufd/main.c +3
-0@@ -432,6 +432,7 @@ union ucmd_buffer {struct iommu_veventq_alloc veventq;struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu; +struct iommu_ioas_get_pa get_pa;#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif @@ -484,6 +485,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { struct iommu_ioas_map_file, iova),IOCTL_OP (IOMMU_IOAS_UNMAP ,iommufd_ioas_unmap ,struct iommu_ioas_unmap ,length ), +IOCTL_OP (IOMMU_IOAS_GET_PA ,iommufd_ioas_get_pa ,struct iommu_ioas_get_pa , +out_phys ),IOCTL_OP (IOMMU_OPTION ,iommufd_option ,struct iommu_option ,val64 ),IOCTL_OP (IOMMU_VDEVICE_ALLOC ,iommufd_vdevice_alloc_ioctl , struct iommu_vdevice_alloc, virt_id),include/uapi/linux/iommufd.h +25
-0@@ -57,6 +57,7 @@ enum {IOMMUFD_CMD_IOAS_CHANGE_PROCESS =0x92 ,IOMMUFD_CMD_VEVENTQ_ALLOC =0x93 ,IOMMUFD_CMD_HW_QUEUE_ALLOC =0x94 , + IOMMUFD_CMD_IOAS_GET_PA = 0x95, };/** @@ -219,6 +220,30 @@ struct iommu_ioas_map { };#define IOMMU_IOAS_MAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP) +/** +* struct iommu_ioas_get_pa - ioctl(IOMMU_IOAS_GET_PA) +* @size: sizeof(struct iommu_ioas_get_pa) +* @flags: Reserved, must be 0 for now +* @ioas_id: IOAS ID to query IOVA to PA mapping from +* @__reserved: Must be 0 +* @iova: IOVA to query +* @out_length: Number of bytes contiguous physical address starting from phys +* @out_phys: Output physical address the IOVA maps to +* +* Query the physical address backing an IOVA range. The entire range must be +* mapped already. For noiommu devices doing unsafe DMA only. +*/ +struct iommu_ioas_get_pa { +__u32 size; +__u32 flags; +__u32 ioas_id; +__u32 __reserved; +__aligned_u64 iova; +__aligned_u64 out_length; +__aligned_u64 out_phys; +}; +#define IOMMU_IOAS_GET_PA _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_GET_PA) +/** * struct iommu_ioas_map_file - ioctl(IOMMU_IOAS_MAP_FILE) * @size: sizeof(struct iommu_ioas_map_file)
Alex Williamson
maintainerRe: [PATCH V4 05/10] vfio: Allow null group for noiommu without containers
In reply to Jacob PanQuoted 24 lines · On Tue, 14 Apr 2026 14:14:07 -0700 Jacob Pan <[email protected]>…
In case of noiommu mode is enabled for VFIO cdev without VFIO container nor IOMMUFD provided compatibility container, there is no need to create a dummy group. Update the group operations to tolerate null group pointer.
Signed-off-by Jacob Pan <[email protected]>---
v4: (Jason)
- Avoid null pointer deref in error unwind
- Add null group check invfio_device_group_unregister
- repartition to includevfio_device_has_group()in this patch--- drivers/vfio/group.c | 20 ++++++++++++++++++++ drivers/vfio/vfio.h | 17 +++++++++++++++++ drivers/vfio/vfio_main.c | 14 ++++++++++++++ include/linux/vfio.h | 9 +++++++++ 4 files changed, 60 insertions(+)drivers/vfio/group.c +0
-0
drivers/vfio/group.c int vfio_device_block_group(struct vfio_device *device) · line 394
Quoted 5 lines
drivers/vfio/group.c +2
-0@@ -390,6 +390,9 @@ int vfio_device_block_group(struct vfio_device *device)struct vfio_group *group = device->group;int ret =0 ; +if (vfio_null_group_allowed () && !group) +return 0 ;
I think this comes down to the fact that at the end of this series, VFIO_NOIOMMU still depends on VFIO_GROUP. vfio_null_group_allowed() can only return true if CONTAINER support is entirely disabled. Why do we still select VFIO_GROUP for VFIO_NOIOMMU and build group.s when there's no container support to use it?
Also note that vfio_noiommu is S_IWUSR, so it is mutable at runtime. Thanks,
Alex
Quoted 143 lines
drivers/vfio/group.c +18
-0+mutex_lock (&group->group_lock);if (group->opened_file) { ret = -EBUSY ;@@ -407,6 +410,9 @@ void vfio_device_unblock_group(struct vfio_device *device) {struct vfio_group *group = device->group; +if (vfio_null_group_allowed () && !group) +return ; +mutex_lock (&group->group_lock); group->cdev_device_open_cnt--;mutex_unlock (&group->group_lock);@@ -598,6 +604,14 @@ static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,struct vfio_group *group;int ret; +/* +* With noiommu enabled under cdev interface only, there is no need to +* create a vfio_group if the group based containers are not enabled. +* The cdev interface is exclusively used for iommufd. +*/ +if (vfio_null_group_allowed ()) +return NULL ; + iommu_group =iommu_group_alloc ();if (IS_ERR (iommu_group))return ERR_CAST (iommu_group);@@ -705,6 +719,9 @@ void vfio_device_remove_group(struct vfio_device *device)struct vfio_group *group = device->group;struct iommu_group *iommu_group; +if (!group) +return ; +if (group->type ==VFIO_NO_IOMMU || group->type ==VFIO_EMULATED_IOMMU )iommu_group_remove_device (device->dev);@@ -756,6 +773,9 @@ void vfio_device_group_register(struct vfio_device *device)void vfio_device_group_unregister (struct vfio_device *device) { +if (!device->group) +return ; +mutex_lock (&device->group->device_lock);list_del (&device->group_next);mutex_unlock (&device->group->device_lock);drivers/vfio/vfio.h +17
-0@@ -114,6 +114,18 @@ bool vfio_device_has_container(struct vfio_device *device);int __init vfio_group_init (void );void vfio_group_cleanup (void ); +/* +* With noiommu enabled and no containers are supported, allow devices that +* don't have a dummy group. +*/ +static inline bool vfio_null_group_allowed (void ) +{ +if (vfio_noiommu && (!IS_ENABLED (CONFIG_VFIO_CONTAINER ) && !IS_ENABLED (CONFIG_IOMMUFD_VFIO_CONTAINER ))) +return true; + +return false; +} +static inline bool vfio_device_is_noiommu (struct vfio_device *vdev) { return IS_ENABLED(CONFIG_VFIO_NOIOMMU) &&@@ -190,6 +202,11 @@ static inline void vfio_group_cleanup(void) { } +static inline bool vfio_null_group_allowed (void ) +{ +return false; +} +static inline bool vfio_device_is_noiommu (struct vfio_device *vdev) {return false;drivers/vfio/vfio_main.c +14
-0@@ -358,6 +358,10 @@ static int __vfio_register_dev(struct vfio_device *device,/* Refcounting can't start until the driver calls register */ refcount_set (&device->refcount,1 ); +/* noiommu device w/o container may have NULL group */ +if (!vfio_device_has_group (device)) +return 0 ; +vfio_device_group_register (device);vfio_device_debugfs_init (device);@@ -392,6 +396,16 @@ void vfio_unregister_group_dev(struct vfio_device *device)bool interrupted = false;long rc; +/* +* For noiommu devices without a container, thus no dummy group, +* simply delete and unregister to balance refcount. +*/ +if (!vfio_device_has_group (device)) { +vfio_device_del (device); +vfio_device_put_registration (device); +return ; + } +/* * Prevent new device opened by userspace via the * VFIO_GROUP_GET_DEVICE_FD in the group path. include/linux/vfio.h +9
-0@@ -328,6 +328,10 @@ struct iommu_group *vfio_file_iommu_group(struct file *file);#if IS_ENABLED (CONFIG_VFIO_GROUP )bool vfio_file_is_group (struct file *file);bool vfio_file_has_dev (struct file *file,struct vfio_device *device); +static inline bool vfio_device_has_group (struct vfio_device *device) +{ +return device->group; +}#else static inline bool vfio_file_is_group (struct file *file) {@@ -338,6 +342,11 @@ static inline bool vfio_file_has_dev(struct file *file, struct vfio_device *devi {return false; } + +static inline bool vfio_device_has_group (struct vfio_device *device) +{ +return false; +}#endif bool vfio_file_is_valid (struct file *file);bool vfio_file_enforced_coherent (struct file *file);
Alex Williamson
maintainerRe: [PATCH V4 07/10] vfio: Enable cdev noiommu mode under iommufd
In reply to Jacob PanQuoted 10 lines · On Tue, 14 Apr 2026 14:14:09 -0700 Jacob Pan <[email protected]>…
Now that devices under noiommu mode can bind with IOMMUFD and perform IOAS operations, lift restrictions on cdev from VFIO side.
No IOMMU cdevs are explicitly named with noiommu prefix. e.g.
/dev/vfio/ |-- 7 |-- devices | `-- noiommu-vfio0 `-- vfio
The group interface already does this, so the "7" is not representative. In fact, since the no-iommu groups are created as device are bound, chances are they'd be in sync for a single device, so this would be 'noiommu-0'. NB. it's correctly represented in the subsequent patches.
Quoted 33 lines
Signed-off-by Jacob Pan <[email protected]>---
v4:
- Movevfio_device_has_group()related out to 5/10
- Keep wait loop invfio_unregister_group_dev(Jason)
v3:
- Add explict dependency on !GENERIC_ATOMIC64
v2:
- Fix build dependency onIOMMU_SUPPORT--- drivers/vfio/Kconfig | 8 ++++++-- drivers/vfio/iommufd.c | 7 ------- drivers/vfio/vfio.h | 8 +------- drivers/vfio/vfio_main.c | 20 ++++++-------------- 4 files changed, 13 insertions(+), 30 deletions(-)drivers/vfio/Kconfig +1
-2@@ -22,8 +22,7 @@ config VFIO_DEVICE_CDEV The VFIO device cdev is another way for userspace to get device access. Userspace gets device fd by opening device cdev under /dev/vfio/devices/vfioX, and then bind the device fd with an iommufd - to set up secure DMA context for device access. This interface does - not support noiommu. + to set up secure DMA context for device access. If you don't know what to do here, say N.
drivers/vfio/Kconfig endif · line 69
Quoted 8 lines
drivers/vfio/Kconfig +5
-0@@ -63,6 +62,11 @@ endif config VFIO_NOIOMMU bool "VFIO No-IOMMU support" depends on VFIO_GROUP + depends on !GENERIC_ATOMIC64 # IOMMU_PT_AMDV1 requires cmpxchg64 + select GENERIC_PT + select IOMMU_PT + select IOMMU_PT_AMDV1 + depends on IOMMU_SUPPORT
Cosmetic nit, group the depends together.
Noting my previous concern about why we keep group support for non-container builds, what about making VFIO_GROUP_NOIOMMU and VFIO_CDEV_NOIOMMU?
Also, vfio no-iommu is traditionally gated on CAP_SYS_RAWIO, but those tests are all in the vfio group code and not replicated here for cdev, afaict. That would relax the usage requirements quite significantly. Thanks,
Alex
Quoted 102 lines
drivers/vfio/Kconfig +0
-0help VFIO is built on the ability to isolate devices using the IOMMU. Only with an IOMMU can userspace access to DMA capable devices bedrivers/vfio/iommufd.c +0
-7@@ -25,10 +25,6 @@ int vfio_df_iommufd_bind(struct vfio_device_file *df)lockdep_assert_held (&vdev->dev_set->lock); -/* Returns 0 to permit device opening under noiommu mode */ -if (vfio_device_is_noiommu (vdev)) -return 0 ; -return vdev->ops->bind_iommufd (vdev, ictx, &df->devid); }@@ -58,9 +54,6 @@ void vfio_df_iommufd_unbind(struct vfio_device_file *df)lockdep_assert_held (&vdev->dev_set->lock); -if (vfio_device_is_noiommu (vdev)) -return ; -if (vdev->ops->unbind_iommufd) vdev->ops->unbind_iommufd (vdev); }drivers/vfio/vfio.h +1
-7@@ -376,19 +376,13 @@ void vfio_init_device_cdev(struct vfio_device *device);static inline int vfio_device_add (struct vfio_device *device) { -/* cdev does not support noiommu device */ -if (vfio_device_is_noiommu (device)) -return device_add (&device->device);vfio_init_device_cdev (device);return cdev_device_add (&device->cdev, &device->device); }static inline void vfio_device_del (struct vfio_device *device) { -if (vfio_device_is_noiommu (device)) -device_del (&device->device); -else -cdev_device_del (&device->cdev, &device->device); +cdev_device_del (&device->cdev, &device->device); }int vfio_device_fops_cdev_open (struct inode *inode,struct file *filep);drivers/vfio/vfio_main.c +6
-14@@ -332,13 +332,15 @@ static int __vfio_register_dev(struct vfio_device *device,if (!device->dev_set)vfio_assign_device_set (device, device); - ret =dev_set_name (&device->device,"vfio%d" , device->index); + ret =vfio_device_set_group (device, type);if (ret)return ret; - ret =vfio_device_set_group (device, type); +/* Just to be safe, expose to user explicitly noiommu cdev node */ + ret =dev_set_name (&device->device,"%svfio%d" , + device->noiommu ?"noiommu-" :"" , device->index);if (ret) -return ret; + goto err_out;/* * VFIO always sets IOMMU_CACHE because we offer no way for userspace to @@ -359,7 +361,7 @@ static int __vfio_register_dev(struct vfio_device *device,refcount_set (&device->refcount,1 );/* noiommu device w/o container may have NULL group */ -if (!vfio_device_has_group (device)) +if (vfio_device_is_noiommu (device) && !vfio_device_has_group (device))return 0 ;vfio_device_group_register (device);@@ -396,16 +398,6 @@ void vfio_unregister_group_dev(struct vfio_device *device)bool interrupted = false;long rc; -/* -* For noiommu devices without a container, thus no dummy group, -* simply delete and unregister to balance refcount. -*/ -if (!vfio_device_has_group (device)) { -vfio_device_del (device); -vfio_device_put_registration (device); -return ; - } -/* * Prevent new device opened by userspace via the * VFIO_GROUP_GET_DEVICE_FD in the group path.
Re: [PATCH V4 05/10] vfio: Allow null group for noiommu without containers
In reply to Tian, KevinHi Kevin,
Quoted 23 lines · Kevin
Quoted 12 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AM+/*
+ * With noiommu enabled and no containers are supported, allow devices that
+ * don't have a dummy group.
+ */drivers/vfio/vfio.h +3
-0+static inline bool vfio_null_group_allowed (void ) +{ + if (vfio_noiommu && (!IS_ENABLED(CONFIG_VFIO_CONTAINER)&& !IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)))
drivers/vfio/vfio.h bool vfio_device_has_container(struct vfio_device *device); · line 127
Quoted 4 lines
drivers/vfio/vfio.h +4
-0+return true; + +return false; +}'allow' kind of implies a policy choice, but throughput this patch it's actually a mandatory state when those conditions are true. Can we strengthen the words here and other places to not given an impression that even when null group is allowed it's still possible to create a vfio group for that situation?
Quoted 2 lines
Just like another comment said, iommufd is the only choice in this case, then probably could call it
vfio_has_iommufd_group()?
Indeed this condition is not an option. I am ok with either vfio_has_iommufd_group() or directly name what is being checked vfio_noiommu_no_container()
Quoted 6 lines
Quoted 6 lines
drivers/vfio/vfio_main.c +0
-0@@ -392,6 +396,16 @@ void vfio_unregister_group_dev(struct vfio_device *device)bool interrupted = false;long rc;
Quoted 15 lines
drivers/vfio/vfio_main.c void vfio_unregister_group_dev(struct vfio_device *device) · line 409
Quoted 12 lines
drivers/vfio/vfio_main.c +10
-0+/* +* For noiommu devices without a container, thus no dummy group, +* simply delete and unregister to balance refcount. +*/ +if (!vfio_device_has_group (device)) { +vfio_device_del (device); +vfio_device_put_registration (device); +return ; + } +/* can you elaborate why the following wait loop is also skipped? then above is removed by patch7 later...
My mistake, will delete this from both patches.
Thanks for catching this.
Jacob
Re: [PATCH V4 05/10] vfio: Allow null group for noiommu without containers
In reply to Alex WilliamsonHi Alex,
Quoted 44 lines · On Thu, 16 Apr 2026 14:06:01 -0600 Alex Williamson <[email protected]> wrote:
From: Alex Williamson <[email protected]>
To: Jacob Pan <[email protected]>
Cc: [email protected], "[email protected]" <[email protected]>, Jason Gunthorpe <[email protected]>, Joerg Roedel <[email protected]>, Mostafa Saleh <[email protected]>, David Matlack <[email protected]>, Robin Murphy <[email protected]>, Nicolin Chen <[email protected]>, "Tian, Kevin" <[email protected]>, Yi Liu <[email protected]>, [email protected], [email protected], Will Deacon <[email protected]>, Baolu Lu <[email protected]>, [email protected] Subject: Re: [PATCH V4 05/10] vfio: Allow null group for noiommu without containers Date: Thu, 16 Apr 2026 14:06:01 -0600
X-Mailer: Claws Mail 4.3.1 (GTK 3.24.51; x86_64-pc-linux-gnu)Quoted 27 lines · On Tue, 14 Apr 2026 14:14:07 -0700 Jacob Pan <[email protected]>…
In case of noiommu mode is enabled for VFIO cdev without VFIO container nor IOMMUFD provided compatibility container, there is no need to create a dummy group. Update the group operations to tolerate null group pointer.
Signed-off-by Jacob Pan <[email protected]>---
v4: (Jason)
- Avoid null pointer deref in error unwind
- Add null group check invfio_device_group_unregister
- repartition to includevfio_device_has_group()in this patch--- drivers/vfio/group.c | 20 ++++++++++++++++++++ drivers/vfio/vfio.h | 17 +++++++++++++++++ drivers/vfio/vfio_main.c | 14 ++++++++++++++ include/linux/vfio.h | 9 +++++++++ 4 files changed, 60 insertions(+)drivers/vfio/group.c +0
-0@@ -390,6 +390,9 @@ int vfio_device_block_group(struct vfio_device *device) struct vfio_group *group = device->group;int ret =0 ;
Quoted 8 lines
drivers/vfio/group.c int vfio_device_block_group(struct vfio_device *device) · line 394
Quoted 2 lines
drivers/vfio/group.c +2
-0+if (vfio_null_group_allowed () && !group) +return 0 ;I think this comes down to the fact that at the end of this series,
VFIO_NOIOMMUstill depends onVFIO_GROUP.vfio_null_group_allowed()can only return true if CONTAINER support is entirely disabled. Why do we still selectVFIO_GROUPforVFIO_NOIOMMUand build group.s when there's no container support to use it?
If we solve this in Kconfig, I think the dependency should be config VFIO_GROUP
bool "Support for the VFIO group /dev/vfio/$group_id"
+ depends on !(VFIO_NOIOMMU && !(VFIO_CONTAINER ||
IOMMUFD_VFIO_CONTAINER))
But this causes circular dependency in that
symbol VFIO_NOIOMMU depends on VFIO_GROUP symbol VFIO_GROUP depends on VFIO_NOIOMMU
If we cannot force VFIO_GROUP=n when container is entirely disabled and NOIOMMU & cdev is enabled, then user is free to set VFIO_GROUP=y, which creates a VFIO_GROUP that cannot be used due to lack of container. There is no functional issue but less clean.
i.e.
# tree /dev/vfio//dev/vfio/
|-- devices
| `-- noiommu-vfio0
`-- noiommu-0 //not usable
# ls /sys/class/vfio noiommu-0
Maybe there is a way to force VFIO_GROUP=n w/o the circular dependency?
Quoted 1 line
Also note that
vfio_noiommuisS_IWUSR, so it is mutable at runtime.
Good point, maybe we can make it a one-way latch? i.e.
- echo 1 > .../enable_unsafe_noiommu_mode — works (n→y)
- echo 0 > .../enable_unsafe_noiommu_mode — returns -EPERM (y→n blocked)
- Boot param vfio.enable_unsafe_noiommu_mode=1 — works
- Writing 1 when already 1 — no-op, succeeds
Re: [PATCH V4 01/10] iommufd: Support a HWPT without an iommu driver for noiommu
In reply to Tian, KevinHi Kevin,
Quoted 9 lines · Kevin
drivers/iommu/iommufd/hwpt_noiommu.c line 4
Quoted 7 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AMdrivers/iommu/iommufd/hwpt_noiommu.c new file +4
-0+// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES +*/ the years are not latest...
will fix.
Quoted 13 lines
Quoted 13 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +13
-0+ +static struct iommu_domain * +noiommu_alloc_paging_flags (struct device *dev,u32 flags, +const struct iommu_user_data *user_data) +{ +struct pt_iommu_amdv1_cfg cfg = {}; +struct noiommu_domain *dom; +int rc; + +if (flags || user_data) +return ERR_PTR (-EOPNOTSUPP ); + + cfg.common.hw_max_vasz_lg2 =64 ;
Quoted 8 lines
drivers/iommu/iommufd/hwpt_noiommu.c line 54
Quoted 6 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +4
-0+ cfg.common.hw_max_oasz_lg2 =52 ; + cfg.starting_level =2 ; + cfg.common.features = + (BIT(PT_FEAT_DYNAMIC_TOP) |
BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES)|drivers/iommu/iommufd/hwpt_noiommu.c new file +1
-0+ BIT(PT_FEAT_AMDV1_FORCE_COHERENCE));
PT_FEAT_AMDV1_ENCRYPT_TABLESis useless here.
Technically yes but we need this flag to match forced features since we are sharing with the real AMDv1. Otherwise, this will fail with EOPNOTSUPP.
static int pt_init_common(struct pt_common *common)
{
...
/* Requested features must match features compiled into this format */ if ((common->features & ~(unsigned int)PT_SUPPORTED_FEATURES) || (!IS_ENABLED(CONFIG_DEBUG_GENERIC_PT) && (common->features & PT_FORCE_ENABLED_FEATURES) != PT_FORCE_ENABLED_FEATURES)) return -EOPNOTSUPP;
where in iommu_amdv1.c:
#define PT_FORCE_ENABLED_FEATURES
\ (BIT(PT_FEAT_DYNAMIC_TOP) | BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES) | \
BIT(PT_FEAT_AMDV1_FORCE_COHERENCE))
Quoted 12 lines
Quoted 11 lines
+/*
+ * AMDV1 is used as a dummy page table for no-IOMMU mode, similar to the
+ * iommufd selftest mock page table.
+ * Unlike legacy VFIO no-IOMMU mode, where no container level APIs are
+ * supported, this allows IOAS and hwpt objects to exist without hardware
+ * IOMMU support. IOVAs are used only for IOVA-to-PA lookups not for
+ * hardware translation in DMA.
Quoted 3 lines
it's confusing between above "legacy VFIO no-IOMMU mode" and following "legacy VFIO group-container based noiommu mode".
will make them consistent.
Quoted 30 lines
Quoted 30 lines
+ *
+ * This is only used with iommufd and cdev-based interfaces and does not
+ * apply to legacy VFIO group-container based noiommu mode.
+ */drivers/iommu/iommufd/hwpt_noiommu.c new file +8
-0+static const struct iommu_domain_ops noiommu_amdv1_ops = { +IOMMU_PT_DOMAIN_OPS (amdv1), + .free = noiommu_domain_free, +}; + +const struct iommu_ops iommufd_noiommu_ops = { + .domain_alloc_paging_flags = noiommu_alloc_paging_flags, +};drivers/iommu/iommufd/iommufd_private.h +2
-0@@ -464,6 +464,8 @@ static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, refcount_dec(&hwpt->obj.users); } +extern const struct iommu_ops iommufd_noiommu_ops; +struct iommufd_attach ;struct
iommufd_group{
--
2.34.1
Alex Williamson
maintainerRe: [PATCH V4 05/10] vfio: Allow null group for noiommu without containers
In reply to Jacob PanQuoted 76 lines · On Fri, 17 Apr 2026 10:06:09 -0700 Jacob Pan <[email protected]>…
Hi Alex,
Quoted 44 lines · On Thu, 16 Apr 2026 14:06:01 -0600 Alex Williamson <[email protected]> wrote:
From: Alex Williamson <[email protected]>
To: Jacob Pan <[email protected]>
Cc: [email protected], "[email protected]" <[email protected]>, Jason Gunthorpe <[email protected]>, Joerg Roedel <[email protected]>, Mostafa Saleh <[email protected]>, David Matlack <[email protected]>, Robin Murphy <[email protected]>, Nicolin Chen <[email protected]>, "Tian, Kevin" <[email protected]>, Yi Liu <[email protected]>, [email protected], [email protected], Will Deacon <[email protected]>, Baolu Lu <[email protected]>, [email protected] Subject: Re: [PATCH V4 05/10] vfio: Allow null group for noiommu without containers Date: Thu, 16 Apr 2026 14:06:01 -0600
X-Mailer: Claws Mail 4.3.1 (GTK 3.24.51; x86_64-pc-linux-gnu)Quoted 27 lines · On Tue, 14 Apr 2026 14:14:07 -0700 Jacob Pan <[email protected]>…
In case of noiommu mode is enabled for VFIO cdev without VFIO container nor IOMMUFD provided compatibility container, there is no need to create a dummy group. Update the group operations to tolerate null group pointer.
Signed-off-by Jacob Pan <[email protected]>---
v4: (Jason)
- Avoid null pointer deref in error unwind
- Add null group check invfio_device_group_unregister
- repartition to includevfio_device_has_group()in this patch--- drivers/vfio/group.c | 20 ++++++++++++++++++++ drivers/vfio/vfio.h | 17 +++++++++++++++++ drivers/vfio/vfio_main.c | 14 ++++++++++++++ include/linux/vfio.h | 9 +++++++++ 4 files changed, 60 insertions(+)drivers/vfio/group.c +0
-0@@ -390,6 +390,9 @@ int vfio_device_block_group(struct vfio_device *device) struct vfio_group *group = device->group;int ret =0 ;Quoted 8 lines
drivers/vfio/group.c int vfio_device_block_group(struct vfio_device *device) · line 394
Quoted 2 lines
drivers/vfio/group.c +2
-0+if (vfio_null_group_allowed () && !group) +return 0 ;I think this comes down to the fact that at the end of this series,
VFIO_NOIOMMUstill depends onVFIO_GROUP.vfio_null_group_allowed()can only return true if CONTAINER support is entirely disabled. Why do we still selectVFIO_GROUPforVFIO_NOIOMMUand build group.s when there's no container support to use it?If we solve this in
Kconfig, I think the dependency should be configVFIO_GROUP
bool "Support for the VFIO group/dev/vfio/$group_id"
+ depends on !(VFIO_NOIOMMU && !(VFIO_CONTAINER ||
IOMMUFD_VFIO_CONTAINER))
But this causes circular dependency in that
symbolVFIO_NOIOMMUdepends onVFIO_GROUPsymbolVFIO_GROUPdepends onVFIO_NOIOMMUIf we cannot force VFIO_GROUP=n when container is entirely disabled and NOIOMMU & cdev is enabled, then user is free to set VFIO_GROUP=y, which creates a
VFIO_GROUPthat cannot be used due to lack of container. There is no functional issue but less clean.
i.e.
# tree/dev/vfio//dev/vfio/
|-- devices
| `-- noiommu-vfio0
`-- noiommu-0 //not usable
Quoted 4 lines
# ls /sys/class/vfio noiommu-0Maybe there is a way to force VFIO_GROUP=n w/o the circular dependency?
What I'm trying to point out is that vfio_null_group_allowed() is being used in a scenario in group.c that shouldn't exist. If all container support is disabled, all group support should also be disabled, regardless of no-iommu. Otherwise we get into the scenario you show above. No-iommu is a feature, currently only a feature of the group/container model, but that's what we're trying to address here.
I'm not sure what the Kconfig looks like to achieve that.
Quoted 6 lines
Quoted 1 line
Also note that
vfio_noiommuisS_IWUSR, so it is mutable at runtime.Good point, maybe we can make it a one-way latch? i.e.
- echo 1 > .../enable_unsafe_noiommu_mode — works (n→y)
- echo 0 > .../enable_unsafe_noiommu_mode — returns-EPERM(y→n blocked)
- Boot param vfio.enable_unsafe_noiommu_mode=1 — works
- Writing 1 when already 1 — no-op, succeeds
It's a question of whether you'll break anyone. IIRC, group-based no-iommu works that you can enabled it, create a no-iommu group, disabled it, and the no-iommu group continues to work. Is it useful, does anyone use it... I dunno. Thanks,
Alex
Tian, Kevin
maintainerRE: [PATCH V4 01/10] iommufd: Support a HWPT without an iommu driver for noiommu
In reply to Jacob PanQuoted 32 lines
From: Jacob Pan <[email protected]>
Sent: Saturday, April 18, 2026 6:00 AMHi Kevin,
Quoted 9 lines · Kevin
drivers/iommu/iommufd/hwpt_noiommu.c line 54
Quoted 7 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AM(quoted diff) +2
-0+ cfg.common.features = + (BIT(PT_FEAT_DYNAMIC_TOP) |
BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES)|drivers/iommu/iommufd/hwpt_noiommu.c new file +1
-0+ BIT(PT_FEAT_AMDV1_FORCE_COHERENCE));
PT_FEAT_AMDV1_ENCRYPT_TABLESis useless here.Technically yes but we need this flag to match forced features since we are sharing with the real AMDv1. Otherwise, this will fail with EOPNOTSUPP.
static intpt_init_common(struct pt_common *common)
{
.../* Requested features must match features compiled into this format */ if ((common->features & ~(unsigned int)PT_SUPPORTED_FEATURES) || (!IS_ENABLED(CONFIG_DEBUG_GENERIC_PT) && (common->features & PT_FORCE_ENABLED_FEATURES) != PT_FORCE_ENABLED_FEATURES)) return -EOPNOTSUPP;
Quoted 5 lines
where in
iommu_amdv1.c:
#definePT_FORCE_ENABLED_FEATURES
\ (BIT(PT_FEAT_DYNAMIC_TOP)|BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES)| \
BIT(PT_FEAT_AMDV1_FORCE_COHERENCE))
Okay, make sense.
btw iommufd selftest just removed the amdv1 format (will be in 7.1). then let's not mention it in this series.
Jason Gunthorpe
maintainerRe: [PATCH V4 01/10] iommufd: Support a HWPT without an iommu driver for noiommu
Quoted 21 lines · Kevin
Quoted 15 lines
Technically yes but we need this flag to match forced features since we are sharing with the real AMDv1. Otherwise, this will fail with EOPNOTSUPP.
static intpt_init_common(struct pt_common *common)
{
.../* Requested features must match features compiled into this format */ if ((common->features & ~(unsigned int)PT_SUPPORTED_FEATURES) || (!IS_ENABLED(CONFIG_DEBUG_GENERIC_PT) && (common->features & PT_FORCE_ENABLED_FEATURES) != PT_FORCE_ENABLED_FEATURES)) return -EOPNOTSUPP;Quoted 5 lines
where in
iommu_amdv1.c:
#definePT_FORCE_ENABLED_FEATURES
\ (BIT(PT_FEAT_DYNAMIC_TOP)|BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES)| \
BIT(PT_FEAT_AMDV1_FORCE_COHERENCE))
Quoted 4 lines
Okay, make sense.
btw iommufd selftest just removed the amdv1 format (will be in 7.1). then let's not mention it in this series.
Why? The selftest was removed because it wasn't used by the userspace selftest, the format is still there
I wanted to use amdv1 for this because it has the best multi-page size options of all the formats.
Jason
Tian, Kevin
maintainerRE: [PATCH V4 01/10] iommufd: Support a HWPT without an iommu driver for noiommu
Quoted 28 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 23, 2026 12:03 AMQuoted 15 lines · Kevin
Quoted 15 lines
Technically yes but we need this flag to match forced features since we are sharing with the real AMDv1. Otherwise, this will fail with EOPNOTSUPP.
static intpt_init_common(struct pt_common *common)
{
.../* Requested features must match features compiled into this format */ if ((common->features & ~(unsigned int)PT_SUPPORTED_FEATURES) || (!IS_ENABLED(CONFIG_DEBUG_GENERIC_PT) && (common->features & PT_FORCE_ENABLED_FEATURES) != PT_FORCE_ENABLED_FEATURES)) return -EOPNOTSUPP;Quoted 3 lines
Quoted 3 lines
where in
iommu_amdv1.c:
#definePT_FORCE_ENABLED_FEATURES
\ (BIT(PT_FEAT_DYNAMIC_TOP)|BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES)|
Quoted 5 lines
Quoted 2 lines
\
BIT(PT_FEAT_AMDV1_FORCE_COHERENCE))Okay, make sense.
Quoted 9 lines
Quoted 2 lines
btw iommufd selftest just removed the amdv1 format (will be in 7.1). then let's not mention it in this series.
Why? The selftest was removed because it wasn't used by the userspace selftest, the format is still there
I wanted to use amdv1 for this because it has the best multi-page size options of all the formats.
I meant to avoid mentioning selftest like below:
+ * AMDV1 is used as a dummy page table for no-IOMMU mode, similar to the
+ * iommufd selftest mock page table.
"similar to" is not helpful here, and is now not the fact any more.
Jason Gunthorpe
maintainerRe: [PATCH V4 01/10] iommufd: Support a HWPT without an iommu driver for noiommu
Quoted 6 lines · Kevin
I meant to avoid mentioning selftest like below:
+ * AMDV1 is used as a dummy page table for no-IOMMU mode, similar to the
+ * iommufd selftest mock page table."similar to" is not helpful here, and is now not the fact any more.
The mock page table is still amdv1, just a little different flavour of it :)
Jason
Tian, Kevin
maintainerRE: [PATCH V4 01/10] iommufd: Support a HWPT without an iommu driver for noiommu
Quoted 7 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, April 23, 2026 10:51 PMQuoted 2 lines · Kevin
I meant to avoid mentioning selftest like below:
Quoted 9 lines
Quoted 1 line
+ * AMDV1 is used as a dummy page table for no-IOMMU mode, similar to
the
Quoted 3 lines
+ * iommufd selftest mock page table.
"similar to" is not helpful here, and is now not the fact any more.
The mock page table is still amdv1, just a little different flavour of it :)
ah, you are right.
Re: [PATCH V4 01/10] iommufd: Support a HWPT without an iommu driver for noiommu
In reply to Jacob PanHi Jacob,
Quoted 32 lines · Jacob Pan
From: Jason Gunthorpe <[email protected]>
Create just a little part of a real iommu driver, enough to slot in under the
dev_iommu_ops()and allow iommufd to calldomain_alloc_paging_flags()and fail everything else.This allows explicitly creating a HWPT under an IOAS.
Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>---
v4:
- Makeiommufd_noiommu_opsconst
v3:
- Add comment to explain the design difference over the legacy noiommu VFIO code.fix const hwpt
--- drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/hw_pagetable.c | 11 ++- drivers/iommu/iommufd/hwpt_noiommu.c | 102 ++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 2 + 4 files changed, 114 insertions(+), 2 deletions(-)create mode 100644
drivers/iommu/iommufd/hwpt_noiommu.cdrivers/iommu/iommufd/Makefile +0
-0
drivers/iommu/iommufd/Makefile iommufd-y := \ · line 13
Quoted 4 lines
drivers/iommu/iommufd/Makefile +1
-0@@ -10,6 +10,7 @@ iommufd-y := \ vfio_compat.o \ viommu.o +iommufd-$(CONFIG_VFIO_NOIOMMU) += hwpt_noiommu.o
is it better to have a separate kconfig for it? Also, VFIO_NOIOMMU depends on VFIO_GROUP, this does not sound good. For one, cdev's noiommu support should not depend on groups. For two, this means the iommufd noiommu support is only for VFIO. It closes the gate for other userpsace drivers. Perhaps a IOMMUFD_NOIOMMU kconfig for it.
VFIO_GROUP | VFIO_DEVICE_CDEV | IOMMUFD_NOIOMMU | VFIO_NOIOMMU allowed?
-----------+------------------+-----------------+----------------------
Y | Y | Y | Y (both paths work)
Y | Y | N | N (cdev path lacks IOMMUFD_NOIOMMU)
Y | N | Y | Y (group path only,
cdev irrelevant)
Y | N | N | Y (group path only,
cdev irrelevant)
N | Y | Y | Y (cdev path only)
N | Y | N | N (cdev path lacks IOMMUFD_NOIOMMU)
N | N | any | N (no VFIO interface
at all)
Then VFIO_NOIOMMU Kconfig dependency is:
depends on VFIO_GROUP || VFIO_DEVICE_CDEV
depends on !VFIO_DEVICE_CDEV || IOMMUFD_NOIOMMUQuoted 45 lines
drivers/iommu/iommufd/Makefile +0
-0iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o obj-$(CONFIG_IOMMUFD) += iommufd.odrivers/iommu/iommufd/hw_pagetable.c +9
-2@@ -8,6 +8,13 @@#include "../iommu-priv.h" #include "iommufd_private.h" +static const struct iommu_ops *get_iommu_ops (struct iommufd_device *idev) +{ +if (IS_ENABLED (CONFIG_VFIO_NOIOMMU ) && !idev->igroup->group) +return &iommufd_noiommu_ops; +return dev_iommu_ops (idev->dev); +} +static void __iommufd_hwpt_destroy (struct iommufd_hw_pagetable *hwpt) {if (hwpt->domain)@@ -114,7 +121,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,IOMMU_HWPT_ALLOC_DIRTY_TRACKING |IOMMU_HWPT_FAULT_ID_VALID |IOMMU_HWPT_ALLOC_PASID ; -const struct iommu_ops *ops =dev_iommu_ops (idev->dev); +const struct iommu_ops *ops =get_iommu_ops (idev);struct iommufd_hwpt_paging *hwpt_paging;struct iommufd_hw_pagetable *hwpt;int rc;@@ -229,7 +236,7 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, struct iommufd_device *idev, u32 flags, const struct iommu_user_data *user_data) { - const struct iommu_ops *ops = dev_iommu_ops(idev->dev); + const struct iommu_ops *ops = get_iommu_ops(idev);struct iommufd_hwpt_nested *hwpt_nested;struct iommufd_hw_pagetable *hwpt;int rc;drivers/iommu/iommufd/hwpt_noiommu.c new file +0
-0
drivers/iommu/iommufd/hwpt_noiommu.c line 6
Quoted 6 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +6
-0@@ -0,0 +1,102 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES +*/ +#include <linux/iommu.h> +#include <linux/generic_pt/iommu.h>
how about moving it to the first one? :)
Quoted 49 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +49
-0+#include "iommufd_private.h" + +static const struct iommu_domain_ops noiommu_amdv1_ops; + +struct noiommu_domain { +union { +struct iommu_domain domain; +struct pt_iommu_amdv1 amdv1; + }; +spinlock_t lock; +}; +PT_IOMMU_CHECK_DOMAIN(struct noiommu_domain, amdv1.iommu, domain); + +static void noiommu_change_top (struct pt_iommu *iommu_table, +phys_addr_t top_paddr,unsigned int top_level) +{ +} + +static spinlock_t *noiommu_get_top_lock (struct pt_iommu *iommupt) +{ +struct noiommu_domain *domain = + container_of(iommupt, struct noiommu_domain, amdv1.iommu); + +return &domain->lock; +} + +static const struct pt_iommu_driver_ops noiommu_driver_ops = { + .get_top_lock = noiommu_get_top_lock, + .change_top = noiommu_change_top, +}; + +static struct iommu_domain * +noiommu_alloc_paging_flags (struct device *dev,u32 flags, +const struct iommu_user_data *user_data) +{ +struct pt_iommu_amdv1_cfg cfg = {}; +struct noiommu_domain *dom; +int rc; + +if (flags || user_data) +return ERR_PTR (-EOPNOTSUPP ); + + cfg.common.hw_max_vasz_lg2 =64 ; + cfg.common.hw_max_oasz_lg2 =52 ; + cfg.starting_level =2 ; + cfg.common.features = + (BIT (PT_FEAT_DYNAMIC_TOP ) |BIT (PT_FEAT_AMDV1_ENCRYPT_TABLES ) | +BIT (PT_FEAT_AMDV1_FORCE_COHERENCE )); +
drivers/iommu/iommufd/hwpt_noiommu.c line 65
Quoted 10 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +10
-0+ dom =kzalloc (sizeof (*dom),GFP_KERNEL ); +if (!dom) +return ERR_PTR (-ENOMEM ); + +spin_lock_init (&dom->lock); + dom->amdv1.iommu.nid =NUMA_NO_NODE ; + dom->amdv1.iommu.driver_ops = &noiommu_driver_ops; + dom->domain.ops = &noiommu_amdv1_ops; + +/* Use mock page table which is based on AMDV1 */
"mock" may mislead reader that this is test code... may just explain why choose AMDV1 page table.
Quoted 15 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +15
-0+ rc =pt_iommu_amdv1_init (&dom->amdv1, &cfg,GFP_KERNEL ); +if (rc) { +kfree (dom); +return ERR_PTR (rc); + } + +return &dom->domain; +} + +static void noiommu_domain_free (struct iommu_domain *iommu_domain) +{ +struct noiommu_domain *domain = + container_of(iommu_domain, struct noiommu_domain, domain); + +pt_iommu_deinit (&domain->amdv1.iommu);
drivers/iommu/iommufd/hwpt_noiommu.c line 90
Quoted 10 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +10
-0+kfree (domain); +} + +/* +* AMDV1 is used as a dummy page table for no-IOMMU mode, similar to the +* iommufd selftest mock page table. +* Unlike legacy VFIO no-IOMMU mode, where no container level APIs are +* supported, this allows IOAS and hwpt objects to exist without hardware +* IOMMU support. IOVAs are used only for IOVA-to-PA lookups not for +* hardware translation in DMA.
Not sure if the above comment is helpful. To me, IOAS/hwpt operation support is the natural advantage of using cdev. What helps here is explaining why the specific ops are defined for noiommu_amdv1_ops. If you'd keep it, may go in the commit message. :)
Regards,
Yi Liu
Quoted 25 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +12
-0+* +* This is only used with iommufd and cdev-based interfaces and does not +* apply to legacy VFIO group-container based noiommu mode. +*/ +static const struct iommu_domain_ops noiommu_amdv1_ops = { +IOMMU_PT_DOMAIN_OPS (amdv1), + .free = noiommu_domain_free, +}; + +const struct iommu_ops iommufd_noiommu_ops = { + .domain_alloc_paging_flags = noiommu_alloc_paging_flags, +};drivers/iommu/iommufd/iommufd_private.h +2
-0@@ -464,6 +464,8 @@ static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx,refcount_dec (&hwpt->obj.users); } +extern const struct iommu_ops iommufd_noiommu_ops; +struct iommufd_attach ;struct iommufd_group {
Re: [PATCH V4 02/10] iommufd: Move igroup allocation to a function
In reply to Jacob PanQuoted 9 lines · Jacob Pan
From: Jason Gunthorpe <[email protected]>
So it can be reused in the next patch which allows binding to noiommu device.
Reviewed-by Samiullah Khawaja <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>
Quoted 6 lines
---
v3:
- Moved null group check out to the next patch (Mostafa)--- drivers/iommu/iommufd/device.c | 43 +++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 16 deletions(-)
Quoted 64 lines
drivers/iommu/iommufd/device.c +27
-16@@ -56,6 +56,30 @@ static bool iommufd_group_try_get(struct iommufd_group *igroup,return kref_get_unless_zero (&igroup->ref); } +static struct iommufd_group *iommufd_alloc_group (struct iommufd_ctx *ictx, +struct iommu_group *group) +{ +struct iommufd_group *new_igroup; + + new_igroup =kzalloc (sizeof (*new_igroup),GFP_KERNEL ); +if (!new_igroup) +return ERR_PTR (-ENOMEM ); + +kref_init (&new_igroup->ref); +mutex_init (&new_igroup->lock); +xa_init (&new_igroup->pasid_attach); + new_igroup->sw_msi_start =PHYS_ADDR_MAX ; +/* group reference moves into new_igroup */ + new_igroup->group = group; + +/* +* The ictx is not additionally refcounted here becase all objects using +* an igroup must put it before their destroy completes. +*/ + new_igroup->ictx = ictx; +return new_igroup; +} +/* * iommufd needs to store some more data for each iommu_group, we keep a * parallel xarray indexed by iommu_group id to hold this instead of putting it @@ -87,25 +111,12 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, }xa_unlock (&ictx->groups); - new_igroup =kzalloc_obj (*new_igroup); -if (!new_igroup) { + new_igroup =iommufd_alloc_group (ictx, group); +if (IS_ERR (new_igroup)) {iommu_group_put (group); -return ERR_PTR (-ENOMEM ); +return new_igroup; } -kref_init (&new_igroup->ref); -mutex_init (&new_igroup->lock); -xa_init (&new_igroup->pasid_attach); - new_igroup->sw_msi_start =PHYS_ADDR_MAX ; -/* group reference moves into new_igroup */ - new_igroup->group = group; - -/* -* The ictx is not additionally refcounted here becase all objects using -* an igroup must put it before their destroy completes. -*/ - new_igroup->ictx = ictx; -/* * We dropped the lock so igroup is invalid. NULL is a safe and likely * value to assume for the xa_cmpxchg algorithm.
Re: [PATCH V4 03/10] iommufd: Allow binding to a noiommu device
In reply to Jacob PanQuoted 25 lines · Jacob Pan
From: Jason Gunthorpe <[email protected]>
Allow iommufd to bind devices without an IOMMU (noiommu mode) by creating a dummy IOMMU group for such devices and skipping hwpt operations.
This enables noiommu devices to operate through the same iommufd API as IOMMU- capable devices.
Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>---
v4:
- handle partially initialized idev w/o igroup (Sashiko)
v3:
- handle new error casesiommufd_device_bind. (Mostafa)--- drivers/iommu/iommufd/device.c | 132 +++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 39 deletions(-)drivers/iommu/iommufd/device.c +0
-0
drivers/iommu/iommufd/device.c struct iommufd_attach { · line 26
Quoted 4 lines
drivers/iommu/iommufd/device.c +1
-0@@ -23,6 +23,11 @@ struct iommufd_attach {struct xarray device_array; }; +static bool is_vfio_noiommu (struct iommufd_device *idev)
perhaps iommufd_device_is_noiommu().
Quoted 63 lines
drivers/iommu/iommufd/device.c +16
-23+{ +return !device_iommu_mapped (idev->dev) || !idev->dev->iommu; +} +static void iommufd_group_release (struct kref *kref) { struct iommufd_group *igroup =@@ -30,9 +35,11 @@ static void iommufd_group_release(struct kref *kref)WARN_ON (!xa_empty (&igroup->pasid_attach)); -xa_cmpxchg (&igroup->ictx->groups,iommu_group_id (igroup->group), igroup, -NULL ,GFP_KERNEL ); -iommu_group_put (igroup->group); +if (igroup->group) { +xa_cmpxchg (&igroup->ictx->groups,iommu_group_id (igroup->group), + igroup,NULL ,GFP_KERNEL ); +iommu_group_put (igroup->group); + }mutex_destroy (&igroup->lock);kfree (igroup); }@@ -204,32 +211,19 @@ void iommufd_device_destroy(struct iommufd_object *obj)struct iommufd_device *idev = container_of(obj, struct iommufd_device, obj); -iommu_device_release_dma_owner (idev->dev); +if (!idev->igroup) +return ; +if (!is_vfio_noiommu (idev)) +iommu_device_release_dma_owner (idev->dev);iommufd_put_group (idev->igroup);if (!iommufd_selftest_is_mock_dev (idev->dev))iommufd_ctx_put (idev->ictx); } -/** -* iommufd_device_bind - Bind a physical device to an iommu fd -* @ictx: iommufd file descriptor -* @dev: Pointer to a physical device struct -* @id: Output ID number to return to userspace for this device -* -* A successful bind establishes an ownership over the device and returns -* struct iommufd_device pointer, otherwise returns error pointer. -* -* A driver using this API must set driver_managed_dma and must not touch -* the device until this routine succeeds and establishes ownership. -* -* Binding a PCI device places the entire RID under iommufd control. -* -* The caller must undo this with iommufd_device_unbind() -*/ -struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, -struct device *dev,u32 *id) +static int iommufd_bind_iommu (struct iommufd_device *idev) { -struct iommufd_device *idev; +struct iommufd_ctx *ictx = idev->ictx; +struct device *dev = idev->dev;struct iommufd_group *igroup;int rc;
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 237
Quoted 8 lines
drivers/iommu/iommufd/device.c +2
-2@@ -238,11 +232,11 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,* to restore cache coherency. */ if (!device_iommu_capable (dev,IOMMU_CAP_CACHE_COHERENCY )) -return ERR_PTR (-EINVAL ); +return -EINVAL ; - igroup =iommufd_get_group (ictx, dev); + igroup =iommufd_get_group (idev->ictx, dev);
no need as you already have a local ictx.
Quoted 45 lines
drivers/iommu/iommufd/device.c +32
-4if (IS_ERR (igroup)) -return ERR_CAST (igroup); +return PTR_ERR (igroup);/* * For historical compat with VFIO the insecure interrupt path is @@ -268,21 +262,69 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,if (rc) goto out_group_put; +/* igroup refcount moves into iommufd_device */ + idev->igroup = igroup; +return 0 ; + +out_group_put: +iommufd_put_group (igroup); +return rc; +} + +/** +* iommufd_device_bind - Bind a physical device to an iommu fd +* @ictx: iommufd file descriptor +* @dev: Pointer to a physical device struct +* @id: Output ID number to return to userspace for this device +* +* A successful bind establishes an ownership over the device and returns +* struct iommufd_device pointer, otherwise returns error pointer. +* +* A driver using this API must set driver_managed_dma and must not touch +* the device until this routine succeeds and establishes ownership. +* +* Binding a PCI device places the entire RID under iommufd control. +* +* The caller must undo this with iommufd_device_unbind() +*/ +struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, +struct device *dev,u32 *id) +{ +struct iommufd_device *idev; +int rc; + idev =iommufd_object_alloc (ictx, idev,IOMMUFD_OBJ_DEVICE ); -if (IS_ERR (idev)) { - rc =PTR_ERR (idev); - goto out_release_owner;
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 303
Quoted 10 lines
drivers/iommu/iommufd/device.c +3
-3- } +if (IS_ERR (idev)) +return idev; + idev->ictx = ictx; -if (!iommufd_selftest_is_mock_dev (dev)) -iommufd_ctx_get (ictx); idev->dev = dev; idev->enforce_cache_coherency =device_iommu_capable (dev,IOMMU_CAP_ENFORCE_CACHE_COHERENCY );
Functioanlly, it is harmless since device_iommu_capable() returns false for noiommu device. But conceptually, it is nice to move it to iommufd_bind_iommu().
Quoted 32 lines
drivers/iommu/iommufd/device.c +22
-2+ +if (!is_vfio_noiommu (idev)) { + rc =iommufd_bind_iommu (idev); +if (rc) + goto err_out; + }else { +struct iommufd_group *igroup; + +/* +* Create a dummy igroup, lots of stuff expects ths igroup to be +* present, but a NULL igroup->group is OK +*/ + igroup =iommufd_alloc_group (ictx,NULL ); +if (IS_ERR (igroup)) { + rc =PTR_ERR (igroup); + goto err_out; + } + idev->igroup = igroup; + } + +if (!iommufd_selftest_is_mock_dev (dev)) +iommufd_ctx_get (ictx);/* The calling driver is a user until iommufd_device_unbind() */ refcount_inc (&idev->obj.users); -/* igroup refcount moves into iommufd_device */ - idev->igroup = igroup;/* * If the caller fails after this success it must call @@ -294,11 +336,14 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, *id = idev->obj.id;return idev;
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 343
Quoted 10 lines
drivers/iommu/iommufd/device.c +5
-4-out_release_owner: -iommu_device_release_dma_owner (dev); -out_group_put: -iommufd_put_group (igroup); +err_out: +/* +* Be careful that iommufd_device_destroy() can handle partial +* initialization. +*/
no native speaker. This is a bit strange to me. I think the comment wants to highligh that iommufd_device_destroy() can handle partial initialization. But the above comment seems to warn something...
how about below?
/*
* iommufd_device_destroy() handles partially initialized idev,
* so iommufd_object_abort_and_destroy() is safe to call here. */
Quoted 37 lines
drivers/iommu/iommufd/device.c +11
-0+iommufd_object_abort_and_destroy (ictx, &idev->obj);return ERR_PTR (rc); + }EXPORT_SYMBOL_NS_GPL (iommufd_device_bind,"IOMMUFD" );@@ -512,6 +557,9 @@ static int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,struct iommufd_attach_handle *handle;int rc; +if (is_vfio_noiommu (idev)) +return 0 ; +if (!iommufd_hwpt_compatible_device (hwpt, idev))return -EINVAL ;@@ -559,6 +607,9 @@ static void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt, {struct iommufd_attach_handle *handle; +if (is_vfio_noiommu (idev)) +return ; + handle =iommufd_device_get_attach_handle (idev, pasid);if (pasid ==IOMMU_NO_PASID )iommu_detach_group_handle (hwpt->domain, idev->igroup->group);@@ -577,6 +628,9 @@ static int iommufd_hwpt_replace_device(struct iommufd_device *idev,struct iommufd_attach_handle *handle, *old_handle;int rc; +if (is_vfio_noiommu (idev)) +return 0 ; +if (!iommufd_hwpt_compatible_device (hwpt, idev))return -EINVAL ;
drivers/iommu/iommufd/device.c int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, · line 709
Quoted 5 lines
drivers/iommu/iommufd/device.c +1
-1@@ -652,7 +706,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, goto err_release_devid; } -if (attach_resv) { +if (attach_resv && !is_vfio_noiommu (idev)) {
how about the iommufd_hw_pagetable_detach() path? Does it also need to check noiommu device?
drivers/iommu/iommufd/device.c int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, · line 712
Quoted 3 lines
drivers/iommu/iommufd/device.c +0
-0rc =iommufd_device_attach_reserved_iova (idev, hwpt_paging);if (rc) goto err_release_devid;
Regards,
Yi Liu
Re: [PATCH V4 10/10] Documentation: Update VFIO NOIOMMU mode
In reply to Jacob PanQuoted 55 lines · Jacob Pan
Document the NOIOMMU mode with newly added cdev support under iommufd.
Cc Jonathan Corbet <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- Documentation/driver-api/vfio.rst | 45 +++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-)Documentation/driver-api/vfio.rst +30
-2@@ -275,8 +275,6 @@ in a VFIO group. With CONFIG_VFIO_DEVICE_CDEV=y the user can now acquire a device fd by directly opening a character device /dev/vfio/devices/vfioX where "X" is the number allocated uniquely by VFIO for registered devices. -cdev interface does not support noiommu devices, so user should use -the legacy group interface if noiommu is wanted. The cdev only works with IOMMUFD. Both VFIO drivers and applications must adapt to the new cdev security model which requires using@@ -370,6 +368,49 @@ IOMMUFD IOAS/HWPT to enable userspace DMA:: /* Other device operations as stated in "VFIO Usage Example" */ +VFIO NOIOMMU mode +------------------------------------------------------------------------------- +VFIO also supports a no-IOMMU mode, intended for usages where unsafe DMA can +be performed by userspace drivers w/o physical IOMMU protection. This mode +is controlled by the parameter: + +/sys/module/vfio/parameters/enable_unsafe_noiommu_mode + +Upon enabling this mode, with an assigned device, the user will be presented +with a VFIO group and device file, e.g.:: + + /dev/vfio/ + |-- devices + | `-- noiommu-vfio0 /* VFIO device cdev */ + |-- noiommu-0 /* VFIO group */ + `-- vfio + +The capabilities vary depending on the device programming interface and kernel +configuration used. The following table summarizes the differences: + ++-------------------+---------------------+----------------------+ +| Feature | VFIO group | VFIO device cdev | ++===================+=====================+======================+ +| VFIO device UAPI | Yes | Yes | ++-------------------+---------------------+----------------------+ +| VFIO container | No | No | ++-------------------+---------------------+----------------------+ +| IOMMUFD IOAS | No | Yes* | ++-------------------+---------------------+----------------------+ +
Documentation/driver-api/vfio.rst IOMMUFD IOAS/HWPT to enable userspace DMA:: · line 410
Quoted 10 lines
Documentation/driver-api/vfio.rst +10
-0+Note that the VFIO container case includes IOMMUFD provided VFIO compatibility +interfaces when either CONFIG_VFIO_CONTAINER or CONFIG_IOMMUFD_VFIO_CONTAINER is +enabled. + +* IOMMUFD UAPI is available for VFIO device cdev to pin and map user memory with + the ability to retrieve physical addresses for DMA command submission. + +A new IOMMUFD ioctl IOMMU_IOAS_GET_PA is added to retrieve the physical address +for a given user virtual address. Note that IOMMU_IOAS_MAP_FIXED_IOVA flag is +ignored in no-IOMMU mode since there is no physical DMA remapping hardware.
not quite get this. Does it mean the the iova field is not used when invoking IOMMU_IOAS_MAP ioctl? Hence the iova will be allocated by iommufd and returned to userspace? Also, how does kernel know it should ignore IOMMU_IOAS_MAP_FIXED_IOVA flag?
Regards,
Yi Liu
Quoted 6 lines
Documentation/driver-api/vfio.rst +3
-0+tools/testing/selftests/vfio/vfio_iommufd_noiommu_test.c provides an example of +using this ioctl in no-IOMMU mode. + VFIO User API -------------------------------------------------------------------------------
Re: [PATCH V4 01/10] iommufd: Support a HWPT without an iommu driver for noiommu
In reply to Yi LiuHi Yi,
Quoted 58 lines · On Tue, 28 Apr 2026 15:45:08 +0800 Yi Liu <[email protected]> wrote:
Hi Jacob,
Quoted 32 lines · Jacob Pan
From: Jason Gunthorpe <[email protected]>
Create just a little part of a real iommu driver, enough to slot in under the
dev_iommu_ops()and allow iommufd to calldomain_alloc_paging_flags()and fail everything else.This allows explicitly creating a HWPT under an IOAS.
Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>---
v4:
- Makeiommufd_noiommu_opsconst
v3:
- Add comment to explain the design difference over the legacy noiommu VFIO code.fix const hwpt
--- drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/hw_pagetable.c | 11 ++- drivers/iommu/iommufd/hwpt_noiommu.c | 102++++++++++++++++++++++++
drivers/iommu/iommufd/iommufd_private.h|
2 + 4 files changed, 114 insertions(+), 2 deletions(-)
create mode 100644drivers/iommu/iommufd/hwpt_noiommu.c(quoted diff) +0
-0b/drivers/iommu/iommufd/Makefile index
71d692c9a8f4..2b1a020b14a6100644 --- a/drivers/iommu/iommufd/Makefile
+++ b/drivers/iommu/iommufd/Makefiledrivers/iommu/iommufd/Makefile iommufd-y := \ · line 13
Quoted 4 lines
drivers/iommu/iommufd/Makefile +1
-0@@ -10,6 +10,7 @@ iommufd-y := \ vfio_compat.o \ viommu.o +iommufd-$(CONFIG_VFIO_NOIOMMU) += hwpt_noiommu.ois it better to have a separate kconfig for it? Also,
VFIO_NOIOMMUdepends onVFIO_GROUP, this does not sound good. For one, cdev's noiommu support should not depend on groups. For two, this means the iommufd noiommu support is only for VFIO. It closes the gate for other userpsace drivers. Perhaps aIOMMUFD_NOIOMMUkconfig for it.
VFIO_GROUP|VFIO_DEVICE_CDEV|IOMMUFD_NOIOMMU|VFIO_NOIOMMUallowed?
-----------+------------------+-----------------+----------------------
Y | Y | Y | Y (both paths work) Y| Y | N | N (cdev path lacks IOMMUFD_NOIOMMU) Y | N | Y | Y (group path only, cdev irrelevant) Y | N | N | Y (group path only, cdev irrelevant) N | Y | Y | Y (cdev path only) N | Y | N | N (cdev path lacks
Quoted 9 lines
IOMMUFD_NOIOMMU)
N | N | any | N (no VFIO
interface at all)Then
VFIO_NOIOMMUKconfigdependency is:depends on VFIO_GROUP || VFIO_DEVICE_CDEV depends on !VFIO_DEVICE_CDEV || IOMMUFD_NOIOMMU
yes, separating the two Kconfigs is cleaner. Alex also suggested similar to this in another thread, just different names.
https://lore.kernel.org/linux-iommu/[email protected]/T/#m1a687fd4c9a9d24b6413d7a6d5cd09ba3f7ddb5e
- VFIO_GROUP_NOIOMMU
- VFIO_CDEV_NOIOMMU // IOMMUFD_NOIOMMU sounds more generic, and VFIO_DEVICE_CDEV already depends on IOMMUFD.
I am working on covering all the cases above and looks promising. Thanks for the matrix, I will put this in the doc patch.
Quoted 46 lines
Quoted 46 lines
drivers/iommu/iommufd/Makefile +0
-0iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o obj-$(CONFIG_IOMMUFD) += iommufd.o(quoted diff) +0
-0
b/drivers/iommu/iommufd/hw_pagetable.cindexfe789c2dc0c9..37316d77277d100644 ---a/drivers/iommu/iommufd/hw_pagetable.c+++b/drivers/iommu/iommufd/hw_pagetable.c@@ -8,6 +8,13 @@
#include "../iommu-priv.h"
#include "iommufd_private.h"+static const struct
iommu_ops*get_iommu_ops(structiommufd_device*idev) +{(quoted diff) +1
-0+ if (IS_ENABLED(CONFIG_VFIO_NOIOMMU) &&!idev->igroup->group)
drivers/iommu/iommufd/hw_pagetable.c +4
-0+return &iommufd_noiommu_ops; +return dev_iommu_ops (idev->dev); +} + static void __iommufd_hwpt_destroy(struct iommufd_hw_pagetable*hwpt) {
if (hwpt->domain)drivers/iommu/iommufd/hw_pagetable.c +2
-2@@ -114,7 +121,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, IOMMU_HWPT_ALLOC_DIRTY_TRACKING |IOMMU_HWPT_FAULT_ID_VALID |IOMMU_HWPT_ALLOC_PASID ; -const struct iommu_ops *ops =dev_iommu_ops (idev->dev); +const struct iommu_ops *ops =get_iommu_ops (idev);struct iommufd_hwpt_paging *hwpt_paging;struct iommufd_hw_pagetable *hwpt;int rc;@@ -229,7 +236,7 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, struct iommufd_device *idev, u32 flags, const struct iommu_user_data *user_data) { -const struct iommu_ops *ops =dev_iommu_ops (idev->dev); +const struct iommu_ops *ops =get_iommu_ops (idev);struct iommufd_hwpt_nested *hwpt_nested;struct iommufd_hw_pagetable *hwpt;int rc;(quoted diff) +0
-0
b/drivers/iommu/iommufd/hwpt_noiommu.cnew file mode 100644 index 000000000000..1c8cae02beecdrivers/iommu/iommufd/hwpt_noiommu.c new file +0
-0@@ -0,0 +1,102 @@
Quoted 8 lines
drivers/iommu/iommufd/hwpt_noiommu.c line 6
Quoted 6 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +6
-0+// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES +*/ +#include <linux/iommu.h> +#include <linux/generic_pt/iommu.h> how about moving it to the first one? :)
will do.
Quoted 56 lines
Quoted 56 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +15
-0+#include "iommufd_private.h" + +static const struct iommu_domain_ops noiommu_amdv1_ops; + +struct noiommu_domain { +union { +struct iommu_domain domain; +struct pt_iommu_amdv1 amdv1; + }; +spinlock_t lock; +}; +PT_IOMMU_CHECK_DOMAIN(struct noiommu_domain, amdv1.iommu, domain); + +static void noiommu_change_top (struct pt_iommu *iommu_table, + phys_addr_t top_paddr, unsigned int
top_level) +{drivers/iommu/iommufd/hwpt_noiommu.c new file +6
-0+} + +static spinlock_t *noiommu_get_top_lock (struct pt_iommu *iommupt) +{ +struct noiommu_domain *domain = + container_of(iommupt, struct noiommu_domain,amdv1.iommu); +
drivers/iommu/iommufd/hwpt_noiommu.c new file +24
-0+return &domain->lock; +} + +static const struct pt_iommu_driver_ops noiommu_driver_ops = { + .get_top_lock = noiommu_get_top_lock, + .change_top = noiommu_change_top, +}; + +static struct iommu_domain * +noiommu_alloc_paging_flags (struct device *dev,u32 flags, +const struct iommu_user_data *user_data) +{ +struct pt_iommu_amdv1_cfg cfg = {}; +struct noiommu_domain *dom; +int rc; + +if (flags || user_data) +return ERR_PTR (-EOPNOTSUPP ); + + cfg.common.hw_max_vasz_lg2 =64 ; + cfg.common.hw_max_oasz_lg2 =52 ; + cfg.starting_level =2 ; + cfg.common.features = + (BIT(PT_FEAT_DYNAMIC_TOP) |
BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES)|drivers/iommu/iommufd/hwpt_noiommu.c new file +7
-0+ BIT(PT_FEAT_AMDV1_FORCE_COHERENCE)); + + dom =kzalloc (sizeof (*dom),GFP_KERNEL ); +if (!dom) +return ERR_PTR (-ENOMEM ); + +spin_lock_init (&dom->lock);
Quoted 8 lines
drivers/iommu/iommufd/hwpt_noiommu.c line 65
Quoted 5 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +5
-0+ dom->amdv1.iommu.nid =NUMA_NO_NODE ; + dom->amdv1.iommu.driver_ops = &noiommu_driver_ops; + dom->domain.ops = &noiommu_amdv1_ops; + +/* Use mock page table which is based on AMDV1 */ "mock" may mislead reader that this is test code... may just explain why choose AMDV1 page table.
I will quote Jason's comment "because it has the best multi-page size options of all the formats.". I also want to stress that this page table is not used by HW for DMA translation, maybe "SW only".
Let me delete this comment and explain this later in the domain ops comment.
Quoted 27 lines
Quoted 27 lines
drivers/iommu/iommufd/hwpt_noiommu.c new file +13
-0+ rc =pt_iommu_amdv1_init (&dom->amdv1, &cfg,GFP_KERNEL ); +if (rc) { +kfree (dom); +return ERR_PTR (rc); + } + +return &dom->domain; +} + +static void noiommu_domain_free (struct iommu_domain *iommu_domain) +{ +struct noiommu_domain *domain = + container_of(iommu_domain, struct noiommu_domain,domain); +
drivers/iommu/iommufd/hwpt_noiommu.c new file +6
-0+pt_iommu_deinit (&domain->amdv1.iommu); +kfree (domain); +} + +/* +* AMDV1 is used as a dummy page table for no-IOMMU mode, similar to the
+ * iommufd selftest mock page table.
+ * Unlike legacy VFIO no-IOMMU mode, where no container level APIs are
+ * supported, this allows IOAS and hwpt objects to exist without hardware
Quoted 8 lines
Quoted 3 lines
+ * IOMMU support. IOVAs are used only for IOVA-to-PA lookups not for
+ * hardware translation in DMA.Not sure if the above comment is helpful. To me, IOAS/hwpt operation support is the natural advantage of using cdev. What helps here is explaining why the specific ops are defined for
noiommu_amdv1_ops. If you'd keep it, may go in the commit message. :)
Agreed, I kind of mixing two things here. I will move the high-level "why noiommu needs a page table" explanation to the commit message. Only keep the domain op explanation:
/*
* Domain ops for iommufd no-IOMMU mode. Uses AMDV1 format as a
* SW-only IOPT because it has the best multi-page size options
* of all the formats. IOVAs serve only for IOVA-to-PA lookups,
* not for hardware DMA translation.
*/
Quoted 30 lines
Regards,
Yi LiuQuoted 26 lines
+ *
+ * This is only used with iommufd and cdev-based interfaces and does not
+ * apply to legacy VFIO group-container based noiommu mode.
+ */drivers/iommu/iommufd/hwpt_noiommu.c new file +8
-0+static const struct iommu_domain_ops noiommu_amdv1_ops = { +IOMMU_PT_DOMAIN_OPS (amdv1), + .free = noiommu_domain_free, +}; + +const struct iommu_ops iommufd_noiommu_ops = { + .domain_alloc_paging_flags = noiommu_alloc_paging_flags, +};(quoted diff) +0
-0
b/drivers/iommu/iommufd/iommufd_private.hindex6ac1965199e9..2682b5baa6e9100644 ---a/drivers/iommu/iommufd/iommufd_private.h+++b/drivers/iommu/iommufd/iommufd_private.h@@ -464,6 +464,8 @@ static inline void iommufd_hw_pagetable_put(structiommufd_ctx*ictx,refcount_dec(&hwpt->obj.users); }drivers/iommu/iommufd/iommufd_private.h +2
-0+extern const struct iommu_ops iommufd_noiommu_ops; +struct iommufd_attach ;struct iommufd_group {
Re: [PATCH V4 07/10] vfio: Enable cdev noiommu mode under iommufd
In reply to Alex WilliamsonHi Alex,
Quoted 14 lines · On Thu, 16 Apr 2026 14:49:15 -0600 Alex Williamson <[email protected]> wrote:
Quoted 10 lines · On Tue, 14 Apr 2026 14:14:09 -0700 Jacob Pan <[email protected]>…
Now that devices under noiommu mode can bind with IOMMUFD and perform IOAS operations, lift restrictions on cdev from VFIO side.
No IOMMU cdevs are explicitly named with noiommu prefix. e.g.
/dev/vfio/ |-- 7 |-- devices | `-- noiommu-vfio0 `-- vfio
Quoted 6 lines
The group interface already does this, so the "7" is not representative. In fact, since the no-iommu groups are created as device are bound, chances are they'd be in sync for a single device, so this would be 'noiommu-0'. NB. it's correctly represented in the subsequent patches.
will fix.
Quoted 34 lines
Quoted 34 lines
Signed-off-by Jacob Pan <[email protected]>---
v4:
- Movevfio_device_has_group()related out to 5/10
- Keep wait loop invfio_unregister_group_dev(Jason)
v3:
- Add explict dependency on !GENERIC_ATOMIC64
v2:
- Fix build dependency onIOMMU_SUPPORT--- drivers/vfio/Kconfig | 8 ++++++-- drivers/vfio/iommufd.c | 7 ------- drivers/vfio/vfio.h | 8 +------- drivers/vfio/vfio_main.c | 20 ++++++-------------- 4 files changed, 13 insertions(+), 30 deletions(-)drivers/vfio/Kconfig +0
-0@@ -22,8 +22,7 @@ config VFIO_DEVICE_CDEV The VFIO device cdev is another way for userspace to getdevice access. Userspace gets device fd by opening device cdev under
/dev/vfio/devices/vfioX, and then bind the device fd
with an iommufddrivers/vfio/Kconfig +0
-1- to set up secure DMA context for device access. Thisinterface does
drivers/vfio/Kconfig +1
-1- not support noiommu. + to set up secure DMA context for device access. If you don't know what to do here, say N.
Quoted 13 lines
drivers/vfio/Kconfig endif · line 69
Quoted 10 lines
drivers/vfio/Kconfig +1
-0@@ -63,6 +62,11 @@ endif config VFIO_NOIOMMU bool "VFIO No-IOMMU support" depends on VFIO_GROUP + depends on !GENERIC_ATOMIC64 # IOMMU_PT_AMDV1 requirescmpxchg64
drivers/vfio/Kconfig +4
-0+ select GENERIC_PT + select IOMMU_PT + select IOMMU_PT_AMDV1 + depends on IOMMU_SUPPORTCosmetic nit, group the depends together.
will do.
Quoted 3 lines
Noting my previous concern about why we keep group support for non-container builds, what about making
VFIO_GROUP_NOIOMMUandVFIO_CDEV_NOIOMMU?
Yes, separate CONFIG option made it much cleaner. No need to do all the null group checks and avoided problem caused by mutable vfio_noiommu. I am also adding an IOMMUFD_NOIOMMU option where CDEV_NOIOMMU depends on, just to avoid layering violation. Will be in v5.
vfio/Kconfig
+config VFIO_CDEV_NOIOMMU
+ bool "VFIO cdev No-IOMMU support" + depends on VFIO_DEVICE_CDEV + depends on IOMMUFD_NOIOMMU and iommufd/Kconfig +config IOMMUFD_NOIOMMU + bool "IOMMUFD no-IOMMU support" + depends on !GENERIC_ATOMIC64 # IOMMU_PT_AMDV1 requires cmpxchg64 + select GENERIC_PT + select IOMMU_PT + select IOMMU_PT_AMDV1
Quoted 3 lines
Also, vfio no-iommu is traditionally gated on
CAP_SYS_RAWIO, but those tests are all in the vfio group code and not replicated here for cdev, afaict. That would relax the usage requirements quite significantly.
Good point, I will add CAP_SYS_RAWIO enforcement in cdev open and iommufd bind path to be parity with the group model.
Quoted 106 lines
Quoted 106 lines
drivers/vfio/Kconfig +0
-0help VFIO is built on the ability to isolate devices usingthe IOMMU. Only with an IOMMU can userspace access to DMA capable devices be diff --git
a/drivers/vfio/iommufd.cb/drivers/vfio/iommufd.cindexa38d262c6028..26c9c3068c77100644drivers/vfio/iommufd.c +0
-7@@ -25,10 +25,6 @@ int vfio_df_iommufd_bind(struct vfio_device_file *df)lockdep_assert_held (&vdev->dev_set->lock); -/* Returns 0 to permit device opening under noiommu mode */ -if (vfio_device_is_noiommu (vdev)) -return 0 ; -return vdev->ops->bind_iommufd (vdev, ictx, &df->devid); }@@ -58,9 +54,6 @@ void vfio_df_iommufd_unbind(struct vfio_device_file *df) lockdep_assert_held(&vdev->dev_set->lock); -if (vfio_device_is_noiommu (vdev)) -return ; -if (vdev->ops->unbind_iommufd) vdev->ops->unbind_iommufd (vdev); }drivers/vfio/vfio.h +1
-7@@ -376,19 +376,13 @@ void vfio_init_device_cdev(struct vfio_device *device);static inline int vfio_device_add (struct vfio_device *device) { -/* cdev does not support noiommu device */ -if (vfio_device_is_noiommu (device)) -return device_add (&device->device);vfio_init_device_cdev (device);return cdev_device_add (&device->cdev, &device->device); }static inline void vfio_device_del (struct vfio_device *device) { -if (vfio_device_is_noiommu (device)) -device_del (&device->device); -else -cdev_device_del (&device->cdev, &device->device); +cdev_device_del (&device->cdev, &device->device); } int vfio_device_fops_cdev_open(struct inode *inode, struct file*filep); diff --git
a/drivers/vfio/vfio_main.cb/drivers/vfio/vfio_main.cindex5d7c2d014689..3ae3d34c21cc100644drivers/vfio/vfio_main.c +2
-2@@ -332,13 +332,15 @@ static int __vfio_register_dev(struct vfio_device *device, if (!device->dev_set)vfio_assign_device_set (device, device); - ret =dev_set_name (&device->device,"vfio%d" , device->index); + ret =vfio_device_set_group (device, type);if (ret)return ret; - ret =vfio_device_set_group (device, type); +/* Just to be safe, expose to user explicitly noiommu cdev node */
drivers/vfio/vfio_main.c +2
-0+ ret = dev_set_name(&device->device, "%svfio%d", + device->noiommu ?"noiommu-" : "",
device->index); if (ret)drivers/vfio/vfio_main.c +1
-1-return ret; + goto err_out;/* * VFIO always sets IOMMU_CACHE because we offer no way for userspace to @@ -359,7 +361,7 @@ static int
__vfio_register_dev(structvfio_device*device,refcount_set(&device->refcount, 1);drivers/vfio/vfio_main.c +1
-1/* noiommu device w/o container may have NULL group */ -if (!vfio_device_has_group (device)) + if (vfio_device_is_noiommu(device) &&!vfio_device_has_group(device)) return 0;
drivers/vfio/vfio_main.c +0
-10vfio_device_group_register (device);@@ -396,16 +398,6 @@ void vfio_unregister_group_dev(struct vfio_device *device) bool interrupted = false;long rc; -/* -* For noiommu devices without a container, thus no dummy group, -* simply delete and unregister to balance refcount. -*/ -if (!vfio_device_has_group (device)) { -vfio_device_del (device); -vfio_device_put_registration (device); -return ; - } -/* * Prevent new device opened by userspace via the *
VFIO_GROUP_GET_DEVICE_FDin the group path.
Re: [PATCH V4 10/10] Documentation: Update VFIO NOIOMMU mode
In reply to Yi LiuHi Yi,
Quoted 67 lines · On Tue, 28 Apr 2026 15:46:02 +0800 Yi Liu <[email protected]> wrote:
Quoted 66 lines · Jacob Pan
Document the NOIOMMU mode with newly added cdev support under iommufd.
Cc Jonathan Corbet <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- Documentation/driver-api/vfio.rst | 45+++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-)
(quoted diff) +0
-0
b/Documentation/driver-api/vfio.rstindex2a21a42c9386..da6f77414c3b100644 ---a/Documentation/driver-api/vfio.rst+++b/Documentation/driver-api/vfio.rst@@ -275,8 +275,6 @@ in a VFIO group. WithCONFIG_VFIO_DEVICE_CDEV=ythe user can now acquire a device fd by directly opening a character device/dev/vfio/devices/vfioXwhere "X" is the number allocated uniquely by VFIO for registered devices. -cdev interface does not support noiommu devices, so user should use -the legacy group interface if noiommu is wanted.
The cdev only works with IOMMUFD. Both VFIO drivers and
applications must adapt to the new cdev security model which requires using @@ -370,6 +368,49 @@ IOMMUFD IOAS/HWPT to enable userspace DMA::
/* Other device operations as stated in "VFIO Usage
Example" */Documentation/driver-api/vfio.rst +3
-0+VFIO NOIOMMU mode +------------------------------------------------------------------------------- +VFIO also supports a no-IOMMU mode, intended for usages whereunsafe DMA can +be performed by userspace drivers w/o physical IOMMU protection. This mode +is controlled by the parameter:
Documentation/driver-api/vfio.rst +4
-0+ +/sys/module/vfio/parameters/enable_unsafe_noiommu_mode + +Upon enabling this mode, with an assigned device, the user will bepresented +with a VFIO group and device file, e.g.::
Documentation/driver-api/vfio.rst +8
-0+ + /dev/vfio/ + |-- devices + | `-- noiommu-vfio0 /* VFIO device cdev */ + |-- noiommu-0 /* VFIO group */ + `-- vfio + +The capabilities vary depending on the device programminginterface and kernel +configuration used. The following table summarizes the differences: +
++-------------------+---------------------+----------------------+
+| Feature | VFIO group | VFIO device cdev |
++===================+=====================+======================+
+| VFIO device UAPI | Yes | Yes |
++-------------------+---------------------+----------------------+
+| VFIO container | No | No |
++-------------------+---------------------+----------------------+
+| IOMMUFD IOAS | No | Yes* |
++-------------------+---------------------+----------------------+(quoted diff) +2
-0+ +Note that the VFIO container case includes IOMMUFD provided VFIOcompatibility +interfaces when either
CONFIG_VFIO_CONTAINERorCONFIG_IOMMUFD_VFIO_CONTAINERis +enabled.(quoted diff) +2
-0+ +* IOMMUFD UAPI is available for VFIO device cdev to pin and mapuser memory with
+ the ability to retrieve physical addresses for DMA command submission. +
+A new IOMMUFD ioctlIOMMU_IOAS_GET_PAis added to retrieve the
Quoted 9 lines
Quoted 3 lines
physical address +for a given user virtual address. Note that
IOMMU_IOAS_MAP_FIXED_IOVAflag is +ignored in no-IOMMU mode since there is no physical DMA remapping hardware.not quite get this. Does it mean the the iova field is not used when invoking
IOMMU_IOAS_MAPioctl? Hence the iova will be allocated by iommufd and returned to userspace? Also, how does kernel know it should ignoreIOMMU_IOAS_MAP_FIXED_IOVAflag?
Good question. Let me clarify — I should reword this in the cover letter.
The IOMMU_IOAS_MAP_FIXED_IOVA flag is NOT silently ignored by the kernel. Userspace can still pass it as with normal IOMMU mode, otherwise IOPT_ALLOC_IOVA kicks in and IOMMUFD allocates an IOVA from the allowed range.
What I meant is that in no-IOMMU mode, the IOVA itself has no physical significance — there is no DMA remapping hardware programming the IOVA→PA translation. The IOVA is simply used as a key in the software IO page table (IOMMU_PT_AMDV1 backed) to track the pinned pages and their physical addresses.
So from the userspace API perspective, the flow is identical:
1. IOMMU_IOAS_MAP with FIXED_IOVA flag and user VA → pins pages, stores PA in the SW page table at the given IOVA
2. IOMMU_IOAS_GET_PA with that IOVA → returns the physical address for DMA command submission
The kernel doesn't need special logic to "know" it should ignore anything — the same iopt_map code path runs; it's just that the backing domain is iommufd_noiommu_ops (a software page table) rather than a hardware IOMMU domain.
The user is assumed to know how to use the special IOVA as the unsafe noiommu mode is enabled by the user.
I'll fix the misleading wording in the next version.
Re: [PATCH V4 04/10] iommufd: Add an ioctl IOMMU_IOAS_GET_PA to query PA from IOVA
In reply to Alex WilliamsonHi Alex,
Quoted 103 lines · On Thu, 16 Apr 2026 13:32:57 -0600 Alex Williamson <[email protected]> wrote:
From: Alex Williamson <[email protected]>
To: Jacob Pan <[email protected]>
Cc: [email protected], "[email protected]" <[email protected]>, Jason Gunthorpe <[email protected]>, Joerg Roedel <[email protected]>, Mostafa Saleh <[email protected]>, David Matlack <[email protected]>, Robin Murphy <[email protected]>, Nicolin Chen <[email protected]>, "Tian, Kevin" <[email protected]>, Yi Liu <[email protected]>, [email protected], [email protected], Will Deacon <[email protected]>, Baolu Lu <[email protected]>, [email protected] Subject: Re: [PATCH V4 04/10] iommufd: Add an ioctlIOMMU_IOAS_GET_PAto query PA from IOVA Date: Thu, 16 Apr 2026 13:32:57 -0600 X-Mailer: Claws Mail 4.3.1 (GTK 3.24.51; x86_64-pc-linux-gnu)Quoted 85 lines · On Tue, 14 Apr 2026 14:14:06 -0700 Jacob Pan <[email protected]>…
To support no-IOMMU mode where userspace drivers perform unsafe DMA using physical addresses, introduce a new API to retrieve the physical address of a user-allocated DMA buffer that has been mapped to an IOVA via IOAS. The mapping is backed by mock I/O page tables maintained by generic IOMMUPT framework.
Suggested-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>---
v4:
- Fix unaligned IOVA length (Sashiko)
v2:
- Scan the contiguous physical-address span beyond the first
page and return its length. ---drivers/iommu/iommufd/io_pagetable.c | 60 +++++++++++++++++++++++++ drivers/iommu/iommufd/ioas.c | 25 +++++++++++ drivers/iommu/iommufd/iommufd_private.h | 3 ++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 25 +++++++++++ 5 files changed, 116 insertions(+)(quoted diff) +0
-0
b/drivers/iommu/iommufd/io_pagetable.cindexee003bb2f647..04336a8e12f5100644 ---a/drivers/iommu/iommufd/io_pagetable.c+++b/drivers/iommu/iommufd/io_pagetable.c@@ -849,6 +849,66 @@ int iopt_unmap_iova(structio_pagetable*iopt, unsigned long iova, returniopt_unmap_iova_range(iopt, iova, iova_last, unmapped); }+int iopt_get_phys(struct
io_pagetable*iopt, unsigned long iova, u64 *paddr,drivers/iommu/iommufd/io_pagetable.c +33
-0+ u64 *length) +{ +struct iopt_area *area; +u64 tmp_length =0 ; +u64 tmp_paddr =0 ; +int rc =0 ; + +if (!IS_ENABLED (CONFIG_VFIO_NOIOMMU )) +return -EOPNOTSUPP ; + +down_read (&iopt->iova_rwsem); + area =iopt_area_iter_first (iopt, iova, iova); +if (!area || !area->pages) { + rc = -ENOENT ; + goto unlock_exit; + } + +if (!area->storage_domain || + area->storage_domain->owner != &iommufd_noiommu_ops) { + rc = -EOPNOTSUPP ; + goto unlock_exit; + } + + *paddr =iommu_iova_to_phys (area->storage_domain, iova); +if (!*paddr) { + rc = -EINVAL ; + goto unlock_exit; + } + + tmp_length =PAGE_SIZE -offset_in_page (iova); + tmp_paddr = *paddr; +/* +* Scan the domain for the contiguous physical address length so that
drivers/iommu/iommufd/io_pagetable.c +7
-0+* userspace search can be optimized for fewer ioctls. +*/ +while (iova <iopt_area_last_iova (area)) { +unsigned long next_iova; +u64 next_paddr; + + if (check_add_overflow(iova, PAGE_SIZE,
&next_iova))drivers/iommu/iommufd/io_pagetable.c +3
-0+break ; + + next_paddr =
iommu_iova_to_phys(area->storage_domain, next_iova); +(quoted diff) +1
-0+ if (!next_paddr || next_paddr != tmp_paddr +
PAGE_SIZE)drivers/iommu/iommufd/io_pagetable.c +5
-0+break ; + + iova = next_iova; + tmp_paddr +=PAGE_SIZE ; + tmp_length +=PAGE_SIZE ;
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ } + *length = tmp_length;If
next_iovaexceedsiopt_area_last_iova(area)AND exists instorage_domainAND happens to be physically contiguous,tmp_lengthis advanced and the return length exceeds the mapping. Otherwise this also always does aniommu_iova_to_phys()one iteration beyond the area last iova. Thanks,
you are right, let me add a check.
@@ -892,6 +892,9 @@ int iopt_get_phys(struct io_pagetable *iopt, unsigned long iova, u64 *paddr,
if (check_add_overflow(iova, PAGE_SIZE, &next_iova))
break;
+ if (next_iova > iopt_area_last_iova(area))
+ break;
+
next_paddr = iommu_iova_to_phys(area->storage_domain,
next_iova);Re: [PATCH V4 04/10] iommufd: Add an ioctl IOMMU_IOAS_GET_PA to query PA from IOVA
In reply to Tian, KevinHi Kevin,
Quoted 10 lines · Kevin
Quoted 10 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AMTo support no-IOMMU mode where userspace drivers perform unsafe DMA using physical addresses, introduce a new API to retrieve the physical address of a user-allocated DMA buffer that has been mapped to an IOVA via IOAS. The mapping is backed by mock I/O page tables maintained
by generic IOMMUPT framework.
Quoted 5 lines
Quoted 3 lines
Suggested-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>Your s-o-b should be the last
will do.
Quoted 14 lines
Quoted 14 lines
(quoted diff) +2
-0+ /* + * Scan the domain for the contiguous physical addresslength so that
drivers/iommu/iommufd/io_pagetable.c +7
-0+* userspace search can be optimized for fewer ioctls. +*/ +while (iova <iopt_area_last_iova (area)) { +unsigned long next_iova; +u64 next_paddr; + + if (check_add_overflow(iova, PAGE_SIZE,
&next_iova))(quoted diff) +2
-0+ break; +
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +1
-0+ next_paddr =
iommu_iova_to_phys(area->storage_domain, next_iova);(quoted diff) +2
-0+ + if (!next_paddr || next_paddr != tmp_paddr +
PAGE_SIZE)(quoted diff) +1
-0+ break;the 2nd condition should be
WARN_ON?
I don't think it should be a WARN_ON since PA non-contiguity is one of the normal exit conditions.
Quoted 9 lines
drivers/iommu/iommufd/main.c union ucmd_buffer { · line 435
Quoted 6 lines
drivers/iommu/iommufd/main.c +1
-0@@ -432,6 +432,7 @@ union ucmd_buffer {struct iommu_veventq_alloc veventq;struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu; +struct iommu_ioas_get_pa get_pa;alphabetic order
will do.
Quoted 6 lines
Quoted 6 lines
drivers/iommu/iommufd/main.c +0
-0#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif @@ -484,6 +485,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { struct iommu_ioas_map_file, iova),
Quoted 8 lines
Review static const struct iommufd_ioctl_op · line 492
Quoted 6 lines
(quoted diff) +2
-0IOCTL_OP(IOMMU_IOAS_UNMAP, iommufd_ioas_unmap, struct iommu_ioas_unmap, length), + IOCTL_OP(IOMMU_IOAS_GET_PA, iommufd_ioas_get_pa, struct iommu_ioas_get_pa, + out_phys),ditto
will do
Quoted 31 lines
Quoted 26 lines
+/**
+ * structiommu_ioas_get_pa-ioctl(IOMMU_IOAS_GET_PA)
+ * @size:sizeof(struct iommu_ioas_get_pa)
+ * @flags: Reserved, must be 0 for now
+ * @ioas_id: IOAS ID to query IOVA to PA mapping from
+ * @__reserved: Must be 0
+ * @iova: IOVA to query
+ * @out_length: Number of bytes contiguous physical address starting from phys
+ * @out_phys: Output physical address the IOVA maps to
+ *
+ * Query the physical address backing an IOVA range. The entire range must be
+ * mapped already. For noiommu devices doing unsafe DMA only.
+ */include/uapi/linux/iommufd.h +10
-0+struct iommu_ioas_get_pa { +__u32 size; +__u32 flags; +__u32 ioas_id; +__u32 __reserved; +__aligned_u64 iova; +__aligned_u64 out_length; +__aligned_u64 out_phys; +}; +#define IOMMU_IOAS_GET_PA _IO(IOMMUFD_TYPE,Quoted 2 lines
IOMMUFD_CMD_IOAS_GET_PA)
+Based on its purpose let's make the restriction in the name, e.g.
Quoted 1 line
IOMMUFD_CMD_IOAS_NOIOMMU_GET_PA
yeah, make sense. will rename all the function names as well.
Re: [PATCH V4 02/10] iommufd: Move igroup allocation to a function
In reply to Tian, KevinHi Kevin,
Quoted 16 lines · Kevin
Quoted 11 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AMFrom: Jason Gunthorpe <[email protected]>
So it can be reused in the next patch which allows binding to noiommu device.
Reviewed-by Samiullah Khawaja <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>Reviewed-by Kevin Tian <[email protected]>with a nit:
Quoted 8 lines
drivers/iommu/iommufd/device.c static bool iommufd_group_try_get(struct iommufd_group *igroup, · line 78
Quoted 6 lines
drivers/iommu/iommufd/device.c +3
-0+ +/* +* The ictx is not additionally refcounted here becase all objects using
drivers/iommu/iommufd/device.c +2
-0+* an igroup must put it before their destroy completes. +*/ s/becase/because/, though it's not added by this patch
will do.
Thanks,
Re: [PATCH V4 03/10] iommufd: Allow binding to a noiommu device
In reply to Tian, KevinHi Kevin,
Quoted 12 lines · Kevin
Quoted 8 lines
From: Jacob Pan <[email protected]>
Sent: Wednesday, April 15, 2026 5:14 AMdrivers/iommu/iommufd/device.c +3
-0+static bool is_vfio_noiommu (struct iommufd_device *idev) +{ + return !device_iommu_mapped(idev->dev) ||!idev->dev->iommu; +}
+could you add some words why both conditions are checked? It's not obvious to me.
Quoted 1 line
btw there is no need to add "_vfio_" in the name.
Actually, I think the first check is not needed. group can be NULL if no containers selected nor VFIO_GROUP is set.
I will change it to
+/*
+ * A noiommu device has no IOMMU driver attached regardless of whether it
+ * enters via the cdev path (no iommu_group) or the group path (fake
+ * noiommu iommu_group). In both cases dev->iommu is NULL.
+ */
+static bool iommufd_device_is_noiommu(struct iommufd_device *idev) +{
+ return !idev->dev->iommu;
+}
Quoted 9 lines
Quoted 9 lines
drivers/iommu/iommufd/device.c +1
-1@@ -238,11 +232,11 @@ struct iommufd_device*iommufd_device_bind(struct iommufd_ctx *ictx, * to restore cache coherency. */ if (!device_iommu_capable (dev,IOMMU_CAP_CACHE_COHERENCY )) -return ERR_PTR (-EINVAL ); +return -EINVAL ;
Quoted 4 lines
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 237
Quoted 2 lines
drivers/iommu/iommufd/device.c +1
-1- igroup =iommufd_get_group (ictx, dev); + igroup =iommufd_get_group (idev->ictx, dev);pointless change.
will fix.
Quoted 37 lines
Quoted 37 lines
(quoted diff) +5
-0+struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, + struct device *dev, u32 *id) +{ + struct iommufd_device *idev; + int rc; + idev = iommufd_object_alloc(ictx, idev,
IOMMUFD_OBJ_DEVICE);drivers/iommu/iommufd/device.c +3
-6-if (IS_ERR (idev)) { - rc =PTR_ERR (idev); - goto out_release_owner; - } +if (IS_ERR (idev)) +return idev; + idev->ictx = ictx; -if (!iommufd_selftest_is_mock_dev (dev)) -iommufd_ctx_get (ictx); idev->dev = dev; idev->enforce_cache_coherency = device_iommu_capable(dev,
IOMMU_CAP_ENFORCE_CACHE_COHERENCY);drivers/iommu/iommufd/device.c +10
-0+ +if (!is_vfio_noiommu (idev)) { + rc =iommufd_bind_iommu (idev); +if (rc) + goto err_out; + }else { +struct iommufd_group *igroup; + +/* +* Create a dummy igroup, lots of stuff expects ths igroup to be
drivers/iommu/iommufd/device.c +3
-0+* present, but a NULL igroup->group is OK +*/ + igroup =iommufd_alloc_group (ictx,NULL );
Quoted 8 lines
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 321
Quoted 5 lines
drivers/iommu/iommufd/device.c +5
-0+if (IS_ERR (igroup)) { + rc =PTR_ERR (igroup); + goto err_out; + } + idev->igroup = igroup;though simple it's slightly clearer to move them into a wrapper e.g.
iommufd_bind_dummy_iommu().
will move to a helper:
static int iommufd_bind_noiommu(struct iommufd_device *idev)
{
struct iommufd_group *igroup;
igroup = iommufd_alloc_group(idev->ictx, NULL);
if (IS_ERR(igroup))
return PTR_ERR(igroup);
idev->igroup = igroup;
return 0;
}Quoted 9 lines
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 325
Quoted 4 lines
drivers/iommu/iommufd/device.c +4
-0+ } + +if (!iommufd_selftest_is_mock_dev (dev)) +iommufd_ctx_get (ictx);better add a comment for the ordering here - this must be done here otherwise
iommufd_device_destroy()will have a problem to handle partial initialization.
will do.
Quoted 6 lines
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 285
Quoted 4 lines
/* The calling driver is a user until
iommufd_device_unbind()*/refcount_inc(&idev->obj.users);drivers/iommu/iommufd/device.c +0
-2-/* igroup refcount moves into iommufd_device */ - idev->igroup = igroup;this is common, why not leaving it here?
I think it is just easier to assign in each helper function where igroup is allocated/get.
As you suggested above, iommufd_bind_noiommu(struct iommufd_device *idev)
Re: [PATCH V4 03/10] iommufd: Allow binding to a noiommu device
In reply to Yi LiuHi Yi,
Quoted 31 lines · On Tue, 28 Apr 2026 15:45:56 +0800 Yi Liu <[email protected]> wrote:
Quoted 30 lines · Jacob Pan
From: Jason Gunthorpe <[email protected]>
Allow iommufd to bind devices without an IOMMU (noiommu mode) by creating a dummy IOMMU group for such devices and skipping hwpt operations.
This enables noiommu devices to operate through the same iommufd API as IOMMU- capable devices.
Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Jacob Pan <[email protected]>---
v4:
- handle partially initialized idev w/o igroup (Sashiko)
v3:
- handle new error casesiommufd_device_bind. (Mostafa)--- drivers/iommu/iommufd/device.c | 132+++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 39 deletions(-)
(quoted diff) +0
-0
b/drivers/iommu/iommufd/device.cindex2f30ea069f66..0283ac39be55100644 ---a/drivers/iommu/iommufd/device.c
+++b/drivers/iommu/iommufd/device.cdrivers/iommu/iommufd/device.c +0
-0@@ -23,6 +23,11 @@ struct iommufd_attach {struct xarray device_array; };
Quoted 4 lines
drivers/iommu/iommufd/device.c struct iommufd_attach { · line 26
Quoted 1 line
drivers/iommu/iommufd/device.c +1
-0+static bool is_vfio_noiommu (struct iommufd_device *idev)perhaps
iommufd_device_is_noiommu().
sounds good, remove vfio.
Quoted 77 lines
Quoted 77 lines
drivers/iommu/iommufd/device.c +8
-3+{ +return !device_iommu_mapped (idev->dev) || !idev->dev->iommu; +} +static void iommufd_group_release (struct kref *kref) { struct iommufd_group *igroup =@@ -30,9 +35,11 @@ static void iommufd_group_release(struct kref *kref)WARN_ON (!xa_empty (&igroup->pasid_attach)); -xa_cmpxchg (&igroup->ictx->groups,iommu_group_id (igroup->group), igroup, -NULL ,GFP_KERNEL ); -iommu_group_put (igroup->group); + if (igroup->group) { +xa_cmpxchg (&igroup->ictx->groups,iommu_group_id (igroup->group), + igroup,NULL ,GFP_KERNEL ); +iommu_group_put (igroup->group); + }mutex_destroy (&igroup->lock);kfree(igroup); }(quoted diff) +0
-0@@ -204,32 +211,19 @@ void iommufd_device_destroy(struct
iommufd_object*obj) structiommufd_device*idev =
container_of(obj, struct iommufd_device, obj);drivers/iommu/iommufd/device.c +4
-8-iommu_device_release_dma_owner (idev->dev); +if (!idev->igroup) +return ; +if (!is_vfio_noiommu (idev)) +iommu_device_release_dma_owner (idev->dev);iommufd_put_group (idev->igroup);if (!iommufd_selftest_is_mock_dev (idev->dev))iommufd_ctx_put (idev->ictx); } -/** -* iommufd_device_bind - Bind a physical device to an iommu fd -* @ictx: iommufd file descriptor -* @dev: Pointer to a physical device struct -* @id: Output ID number to return to userspace for this device -* -* A successful bind establishes an ownership over the device and returns
- * structiommufd_devicepointer, otherwise returns error pointer.
- *
- * A driver using this API must setdriver_managed_dmaand must not touch
- * the device until this routine succeeds and establishes ownership.
- *
- * Binding a PCI device places the entire RID under iommufd control.
- *
- * The caller must undo this withiommufd_device_unbind()
- */
-structiommufd_device*iommufd_device_bind(structiommufd_ctx*ictx,(quoted diff) +0
-1- struct device *dev, u32*id) +static int
iommufd_bind_iommu(struct iommufd_device *idev)
{drivers/iommu/iommufd/device.c +3
-2-struct iommufd_device *idev; +struct iommufd_ctx *ictx = idev->ictx; +struct device *dev = idev->dev;struct iommufd_group *igroup;int rc;@@ -238,11 +232,11 @@ struct iommufd_device*iommufd_device_bind(struct iommufd_ctx *ictx, * to restore cache coherency. */ if (!device_iommu_capable (dev,IOMMU_CAP_CACHE_COHERENCY )) -return ERR_PTR (-EINVAL ); +return -EINVAL ;
Quoted 4 lines
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 237
Quoted 2 lines
drivers/iommu/iommufd/device.c +1
-1- igroup =iommufd_get_group (ictx, dev); + igroup =iommufd_get_group (idev->ictx, dev);no need as you already have a local ictx.
will do.
Quoted 59 lines
Quoted 59 lines
drivers/iommu/iommufd/device.c +1
-1if (IS_ERR (igroup)) -return ERR_CAST (igroup); +return PTR_ERR (igroup);/* * For historical compat with VFIO the insecure interrupt
path is @@ -268,21 +262,69 @@ structiommufd_device*iommufd_device_bind(structiommufd_ctx*ictx, if (rc)
gotoout_group_put;drivers/iommu/iommufd/device.c +16
-0+/* igroup refcount moves into iommufd_device */ + idev->igroup = igroup; +return 0 ; + +out_group_put: +iommufd_put_group (igroup); +return rc; +} + +/** +* iommufd_device_bind - Bind a physical device to an iommu fd +* @ictx: iommufd file descriptor +* @dev: Pointer to a physical device struct +* @id: Output ID number to return to userspace for this device +* +* A successful bind establishes an ownership over the device and returns
+ * structiommufd_devicepointer, otherwise returns error pointer.
+ *
+ * A driver using this API must setdriver_managed_dmaand must not touch
+ * the device until this routine succeeds and establishes ownership.
+ *
+ * Binding a PCI device places the entire RID under iommufd control.
+ *
+ * The caller must undo this withiommufd_device_unbind()
+ */
+structiommufd_device*iommufd_device_bind(structiommufd_ctx*ictx,(quoted diff) +1
-0+ struct device *dev, u32*id) +{
drivers/iommu/iommufd/device.c +3
-0+struct iommufd_device *idev; +int rc; + idev = iommufd_object_alloc(ictx, idev,
IOMMUFD_OBJ_DEVICE);drivers/iommu/iommufd/device.c +3
-6-if (IS_ERR (idev)) { - rc =PTR_ERR (idev); - goto out_release_owner; - } +if (IS_ERR (idev)) +return idev; + idev->ictx = ictx; -if (!iommufd_selftest_is_mock_dev (dev)) -iommufd_ctx_get (ictx);
Quoted 8 lines
Quoted 4 lines
idev->dev= dev;
idev->enforce_cache_coherency=
device_iommu_capable(dev,IOMMU_CAP_ENFORCE_CACHE_COHERENCY);Functioanlly, it is harmless since
device_iommu_capable()returns false for noiommu device. But conceptually, it is nice to move it toiommufd_bind_iommu().
make sense, will move it.
Quoted 35 lines
Quoted 35 lines
drivers/iommu/iommufd/device.c +10
-0+ +if (!is_vfio_noiommu (idev)) { + rc =iommufd_bind_iommu (idev); +if (rc) + goto err_out; + }else { +struct iommufd_group *igroup; + +/* +* Create a dummy igroup, lots of stuff expects ths igroup to be
drivers/iommu/iommufd/device.c +12
-0+* present, but a NULL igroup->group is OK +*/ + igroup =iommufd_alloc_group (ictx,NULL ); +if (IS_ERR (igroup)) { + rc =PTR_ERR (igroup); + goto err_out; + } + idev->igroup = igroup; + } + +if (!iommufd_selftest_is_mock_dev (dev)) +iommufd_ctx_get (ictx);/* The calling driver is a user until
iommufd_device_unbind()*/refcount_inc(&idev->obj.users);drivers/iommu/iommufd/device.c +0
-2-/* igroup refcount moves into iommufd_device */ - idev->igroup = igroup;/* * If the caller fails after this success it must call @@ -294,11 +336,14 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, *id = idev->obj.id;return idev;
Quoted 16 lines
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 343
Quoted 10 lines
drivers/iommu/iommufd/device.c +3
-4-out_release_owner: -iommu_device_release_dma_owner (dev); -out_group_put: -iommufd_put_group (igroup); +err_out: +/* +* Be careful that iommufd_device_destroy() can handle partial
drivers/iommu/iommufd/device.c +2
-0+* initialization. +*/ no native speaker. This is a bit strange to me. I think the comment wants to highligh that
iommufd_device_destroy()can handle partial initialization. But the above comment seems to warn something... how about below?
yes, that is better, This is not a warning, just explaining.
Quoted 45 lines
/*
*iommufd_device_destroy()handles partially initialized idev,
* soiommufd_object_abort_and_destroy()is safe to call here. */Quoted 40 lines
drivers/iommu/iommufd/device.c +8
-0+iommufd_object_abort_and_destroy (ictx, &idev->obj);return ERR_PTR (rc); + }EXPORT_SYMBOL_NS_GPL (iommufd_device_bind,"IOMMUFD" );@@ -512,6 +557,9 @@ static int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt, struct iommufd_attach_handle *handle;int rc; +if (is_vfio_noiommu (idev)) +return 0 ; +if (!iommufd_hwpt_compatible_device (hwpt, idev))return -EINVAL ;@@ -559,6 +607,9 @@ static void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt, {struct iommufd_attach_handle *handle; +if (is_vfio_noiommu (idev)) +return ; + handle =iommufd_device_get_attach_handle (idev, pasid);if (pasid ==IOMMU_NO_PASID ) iommu_detach_group_handle(hwpt->domain,
idev->igroup->group); @@ -577,6 +628,9 @@ static int iommufd_hwpt_replace_device(structiommufd_device*idev, structiommufd_attach_handle*handle,*old_handle; int rc;drivers/iommu/iommufd/device.c +3
-0+if (is_vfio_noiommu (idev)) +return 0 ; +if (!iommufd_hwpt_compatible_device (hwpt, idev))return -EINVAL ;@@ -652,7 +706,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, goto err_release_devid; }
Quoted 5 lines
drivers/iommu/iommufd/device.c int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, · line 709
Quoted 2 lines
drivers/iommu/iommufd/device.c +1
-1-if (attach_resv) { +if (attach_resv && !is_vfio_noiommu (idev)) {how about the
iommufd_hw_pagetable_detach()path? Does it also need to check noiommu device?
I don't think it is needed. iopt_remove_reserved_iova() is safe to call when nothing was added — it's a no-op. But I can add the guard for symmetry if preferred.
Quoted 7 lines
Review int iommufd_hw_pagetable_attach(struct · line 712
Quoted 4 lines
(quoted diff) +0
-0rc = iommufd_device_attach_reserved_iova(idev, hwpt_paging); if (rc) goto err_release_devid;Regards,
Yi Liu