iommu: iommufd: Explicitly check for VM_PFNMAP in iommufd_ioas_map
PATCH 1/1 iommu: iommufd: Explicitly check for VM_PFNMAP in iommufd_ioas_map
The iommufd_ioas_map function currently returns -EFAULT when attempting to map VM_PFNMAP VMAs because pin_user_pages_fast() cannot handle such mappings. This error code is misleading and does not accurately reflect the nature of the failure.
Add an explicit check for the VM_PFNMAP flag before attempting the pin_user_pages operation. If VM_PFNMAP is set, return -EOPNOTSUPP to clearly indicate that PFNMAP regions are not supported through the IOMMU_IOAS_MAP interface.
This change improves error reporting and helps userspace applications distinguish between different failure modes when working with special mappings like MMIO regions.
Note that Jason Gunthorpe is working on extending IOMMU_IOAS_MAP_FILE to support dma-buf file descriptors for MMIO BARs[1], which will provide a secure and controlled method for sharing device memory. Until that support is available, PFNMAP mappings through IOMMUFD are not supported. [1]https://lore.kernel.org/all/[email protected]/
--- drivers/iommu/iommufd/ioas.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
drivers/iommu/iommufd/ioas.c +28 -0
@@ -241,6 +241,32 @@ int iommufd_ioas_map_file(struct iommufd_ucmd *ucmd)return rc; } +/** +* iommufd_check_vm_pfnmap - Check if a user address has the VM_PFNMAP flag set +* @vaddr: User virtual address to check +* +* This function checks if the VMA (Virtual Memory Area) containing the given +* virtual address has the VM_PFNMAP flag set. This flag is typically used for +* memory regions that directly map hardware resources (e.g., PCI BARs). +* +* Returns: true if VM_PFNMAP is set, false otherwise. +*/ +static bool iommufd_check_vm_pfnmap (unsigned long vaddr) +{ +struct mm_struct *mm = current->mm; +struct vm_area_struct *vma; +bool ret = false; + +mmap_read_lock (mm); + vaddr =untagged_addr_remote (mm, vaddr); + vma =vma_lookup (mm, vaddr); +if (vma && vma->vm_flags &VM_PFNMAP ) + ret = true; +mmap_read_unlock (mm); + +return ret; +} +int iommufd_ioas_map (struct iommufd_ucmd *ucmd) {struct iommu_ioas_map *cmd = ucmd->cmd;
@@ -254,6 +280,8 @@ int iommufd_ioas_map(struct iommufd_ucmd *ucmd) IOMMU_IOAS_MAP_READABLE)) || cmd->__reserved)return -EOPNOTSUPP ; +if (iommufd_check_vm_pfnmap (cmd->user_va)) +return -EOPNOTSUPP ;if (cmd->iova >=ULONG_MAX || cmd->length >=ULONG_MAX )return -EOVERFLOW ;
Signature · 1 line
2.39.3
Quoted 4 lines · Shuai Xue
The
iommufd_ioas_mapfunction currently returns-EFAULTwhen attempting to mapVM_PFNMAPVMAs becausepin_user_pages_fast()cannot handle such mappings. This error code is misleading and does not accurately reflect the nature of the failure.
Sure, but why do you care? Userspace should know not to do this based on how it created the mmaps, not rely on errnos to figure it out after the fact.
drivers/iommu/iommufd/ioas.c int iommufd_ioas_map_file(struct iommufd_ucmd *ucmd) · line 265
Quoted 12 lines
drivers/iommu/iommufd/ioas.c +12
-0+static bool iommufd_check_vm_pfnmap (unsigned long vaddr) +{ +struct mm_struct *mm = current->mm; +struct vm_area_struct *vma; +bool ret = false; + +mmap_read_lock (mm); + vaddr =untagged_addr_remote (mm, vaddr); + vma =vma_lookup (mm, vaddr); +if (vma && vma->vm_flags &VM_PFNMAP ) + ret = true; +mmap_read_unlock (mm);
This isn't really sufficient, the range can span multiple VMAs and you can hit special PTEs in PFNMAPs, or you can hit P2P struct pages in fully normal VMAs.
I think if you really want this errno distinction it should come from pin_user_pages() directly as only it knows the reason it didn't work.
Jason
Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Shuai Xue
The
iommufd_ioas_mapfunction currently returns-EFAULTwhen attempting to mapVM_PFNMAPVMAs becausepin_user_pages_fast()cannot handle such mappings. This error code is misleading and does not accurately reflect the nature of the failure.
Hi, Jason,
Quoted 4 lines
Sure, but why do you care? Userspace should know not to do this based on how it created the mmaps, not rely on errnos to figure it out after the fact.
We run different VMMs (QEMU, Kata Containers) to meet diverse business requirements, while our production environment deploys various evolving kernel versions. Additionally, we are migrating from VFIO Type 1 to IOMMUFD. Although IOMMUFD claims to provide compatible iommufd_vfio_ioctl APIs, these APIs are not fully compatible in practice. For example, with VFIO_IOMMU_MAP_DMA, iommufd_vfio_map_dma doesn't support MMIO mapping, and we can only rely on the implicit EFAULT error from pin_user_pages_fast(). (I initially considered adding explicit checks in iommufd_vfio_map_dma, but I noticed you plan to add dma_buf support there.)
While we certainly aim for a seamless migration from VFIO Type 1 to IOMMUFD, as you know, this isn't always feasible.
For GPU-related issues encountered in production, the debugging path is quite long - from business teams to virtualization teams, and finally to our kernel team.
Therefore, having explicit checks with deterministic error codes returned to userspace would be greatly appreciated.
Quoted 18 lines
drivers/iommu/iommufd/ioas.c int iommufd_ioas_map_file(struct iommufd_ucmd *ucmd) · line 265
Quoted 13 lines
drivers/iommu/iommufd/ioas.c +12
-0+static bool iommufd_check_vm_pfnmap (unsigned long vaddr) +{ +struct mm_struct *mm = current->mm; +struct vm_area_struct *vma; +bool ret = false; + +mmap_read_lock (mm); + vaddr =untagged_addr_remote (mm, vaddr); + vma =vma_lookup (mm, vaddr); +if (vma && vma->vm_flags &VM_PFNMAP ) + ret = true; +mmap_read_unlock (mm);This isn't really sufficient, the range can span multiple VMAs and you can hit special PTEs in PFNMAPs, or you can hit P2P struct pages in fully normal VMAs.
Quoted 3 lines
I think if you really want this errno distinction it should come from
pin_user_pages()directly as only it knows the reason it didn't work.
Aha, I see. Thank you for pointing out this issue. The check indeed needs to be more comprehensive. Do you mind use pin_user_pages() as a precheck?
Thanks for quick reply.
Best Regards,
Shuai
Quoted 6 lines · Shuai Xue
We run different VMMs (QEMU, Kata Containers) to meet diverse business requirements, while our production environment deploys various evolving kernel versions. Additionally, we are migrating from VFIO Type 1 to IOMMUFD. Although IOMMUFD claims to provide compatible
iommufd_vfio_ioctlAPIs, these APIs are not fully compatible in practice.
Well, it aims to, but we are not there yet. Hopefully in the coming months the MMIO to VFIO will be supported in type 1 emulation as well.
But broadly the EFAULT return here always means the underlying VMA is incompatible with IOMMUFD, I'm not sure there is that much value in further determining why exactly it is incompatible.
Quoted 3 lines
Aha, I see. Thank you for pointing out this issue. The check indeed needs to be more comprehensive. Do you mind use
pin_user_pages()as a precheck?
I mean we already call pin_user_pages deep inside the mapping code and propogate whatever error code it gives back up to userspace. If it gives a more specific code then it will be returned naturally, no need to change iommufd at all.
Jason