From: Li Zhe <lizhe.67@xxxxxxxxxxxxx> This patchset is an integration of the two previous patchsets[1][2]. When vfio_pin_pages_remote() is called with a range of addresses that includes large folios, the function currently performs individual statistics counting operations for each page. This can lead to significant performance overheads, especially when dealing with large ranges of pages. The function vfio_unpin_pages_remote() has a similar issue, where executing put_pfn() for each pfn brings considerable consumption. This patchset primarily optimizes the performance of the relevant functions by batching the less efficient operations mentioned before. The first two patch optimizes the performance of the function vfio_pin_pages_remote(), while the remaining patches optimize the performance of the function vfio_unpin_pages_remote(). The performance test results, based on v6.16, for completing the 16G VFIO MAP/UNMAP DMA, obtained through unit test[3] with slight modifications[4], are as follows. Base(6.16): ------- AVERAGE (MADV_HUGEPAGE) -------- VFIO MAP DMA in 0.049 s (328.5 GB/s) VFIO UNMAP DMA in 0.141 s (113.7 GB/s) ------- AVERAGE (MAP_POPULATE) -------- VFIO MAP DMA in 0.268 s (59.6 GB/s) VFIO UNMAP DMA in 0.307 s (52.2 GB/s) ------- AVERAGE (HUGETLBFS) -------- VFIO MAP DMA in 0.051 s (310.9 GB/s) VFIO UNMAP DMA in 0.135 s (118.6 GB/s) With this patchset: ------- AVERAGE (MADV_HUGEPAGE) -------- VFIO MAP DMA in 0.025 s (633.1 GB/s) VFIO UNMAP DMA in 0.044 s (363.2 GB/s) ------- AVERAGE (MAP_POPULATE) -------- VFIO MAP DMA in 0.249 s (64.2 GB/s) VFIO UNMAP DMA in 0.289 s (55.3 GB/s) ------- AVERAGE (HUGETLBFS) -------- VFIO MAP DMA in 0.030 s (533.2 GB/s) VFIO UNMAP DMA in 0.044 s (361.3 GB/s) For large folio, we achieve an over 40% performance improvement for VFIO MAP DMA and an over 67% performance improvement for VFIO DMA UNMAP. For small folios, the performance test results show a slight improvement with the performance before optimization. [1]: https://lore.kernel.org/all/20250529064947.38433-1-lizhe.67@xxxxxxxxxxxxx/ [2]: https://lore.kernel.org/all/20250620032344.13382-1-lizhe.67@xxxxxxxxxxxxx/#t [3]: https://github.com/awilliam/tests/blob/vfio-pci-mem-dma-map/vfio-pci-mem-dma-map.c [4]: https://lore.kernel.org/all/20250610031013.98556-1-lizhe.67@xxxxxxxxxxxxx/ Li Zhe (5): mm: introduce num_pages_contiguous() vfio/type1: optimize vfio_pin_pages_remote() vfio/type1: batch vfio_find_vpfn() in function vfio_unpin_pages_remote() vfio/type1: introduce a new member has_rsvd for struct vfio_dma vfio/type1: optimize vfio_unpin_pages_remote() drivers/vfio/vfio_iommu_type1.c | 112 ++++++++++++++++++++++++++------ include/linux/mm.h | 7 +- include/linux/mm_inline.h | 35 ++++++++++ 3 files changed, 132 insertions(+), 22 deletions(-) --- Changelogs: v4->v5: - Update the performance test results based on v6.16. - Re-implement num_pages_contiguous() without relying on nth_page(), and relocate it into mm_inline.h. - Merge the fixup patch into the original patch (patch #2). v3->v4: - Fix an indentation issue in patch #2. v2->v3: - Add a "Suggested-by" and a "Reviewed-by" tag. - Address the compilation errors introduced by patch #1. - Resolved several variable type issues. - Add clarification for function num_pages_contiguous(). v1->v2: - Update the performance test results. - The function num_pages_contiguous() is extracted and placed in a separate commit. - The phrase 'for large folio' has been removed from the patchset title. v4: https://lore.kernel.org/all/20250710085355.54208-1-lizhe.67@xxxxxxxxxxxxx/ v3: https://lore.kernel.org/all/20250707064950.72048-1-lizhe.67@xxxxxxxxxxxxx/ v2: https://lore.kernel.org/all/20250704062602.33500-1-lizhe.67@xxxxxxxxxxxxx/ v1: https://lore.kernel.org/all/20250630072518.31846-1-lizhe.67@xxxxxxxxxxxxx/ -- 2.20.1