Skip to content

Commit d983f15

Browse files
author
Alex Williamson
committed
vfio/type1: Use mapping page mask for pfnmaps
JIRA: https://issues.redhat.com/browse/RHEL-85587 commit 0fd0684 Author: Alex Williamson <alex.williamson@redhat.com> Date: Tue Feb 18 15:22:06 2025 -0700 vfio/type1: Use mapping page mask for pfnmaps vfio-pci supports huge_fault for PCI MMIO BARs and will insert pud and pmd mappings for well aligned mappings. follow_pfnmap_start() walks the page table and therefore knows the page mask of the level where the address is found and returns this through follow_pfnmap_args.addr_mask. Subsequent pfns from this address until the end of the mapping page are necessarily consecutive. Use this information to retrieve a range of pfnmap pfns in a single pass. With optimal mappings and alignment on systems with 1GB pud and 4KB page size, this reduces iterations for DMA mapping PCI BARs by a factor of 256K. In real world testing, the overhead of iterating pfns for a VM DMA mapping a 32GB PCI BAR is reduced from ~1s to sub-millisecond overhead. Reviewed-by: Peter Xu <peterx@redhat.com> Reviewed-by: Mitchell Augustin <mitchell.augustin@canonical.com> Tested-by: Mitchell Augustin <mitchell.augustin@canonical.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/20250218222209.1382449-7-alex.williamson@redhat.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com> Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 28ec429 commit d983f15

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

drivers/vfio/vfio_iommu_type1.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ static void vfio_batch_fini(struct vfio_batch *batch)
521521

522522
static int follow_fault_pfn(struct vm_area_struct *vma, struct mm_struct *mm,
523523
unsigned long vaddr, unsigned long *pfn,
524-
bool write_fault)
524+
unsigned long *addr_mask, bool write_fault)
525525
{
526526
struct follow_pfnmap_args args = { .vma = vma, .address = vaddr };
527527
int ret;
@@ -545,10 +545,12 @@ static int follow_fault_pfn(struct vm_area_struct *vma, struct mm_struct *mm,
545545
return ret;
546546
}
547547

548-
if (write_fault && !args.writable)
548+
if (write_fault && !args.writable) {
549549
ret = -EFAULT;
550-
else
550+
} else {
551551
*pfn = args.pfn;
552+
*addr_mask = args.addr_mask;
553+
}
552554

553555
follow_pfnmap_end(&args);
554556
return ret;
@@ -591,15 +593,22 @@ static long vaddr_get_pfns(struct mm_struct *mm, unsigned long vaddr,
591593
vma = vma_lookup(mm, vaddr);
592594

593595
if (vma && vma->vm_flags & VM_PFNMAP) {
594-
ret = follow_fault_pfn(vma, mm, vaddr, pfn, prot & IOMMU_WRITE);
596+
unsigned long addr_mask;
597+
598+
ret = follow_fault_pfn(vma, mm, vaddr, pfn, &addr_mask,
599+
prot & IOMMU_WRITE);
595600
if (ret == -EAGAIN)
596601
goto retry;
597602

598603
if (!ret) {
599-
if (is_invalid_reserved_pfn(*pfn))
600-
ret = 1;
601-
else
604+
if (is_invalid_reserved_pfn(*pfn)) {
605+
unsigned long epfn;
606+
607+
epfn = (*pfn | (~addr_mask >> PAGE_SHIFT)) + 1;
608+
ret = min_t(long, npages, epfn - *pfn);
609+
} else {
602610
ret = -EFAULT;
611+
}
603612
}
604613
}
605614
done:

0 commit comments

Comments
 (0)