vfio/type1: Fix pin leaks for reserved GUP-pinned pages
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest3 patches 2 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/3 | vfio/type1: Skip reserved PFNs in vpfn_pages()drivers/ | +8 | 0 replies | |
| 2/3 | vfio/type1: Track GUP vs PFNMAP in DMA map/unmapdrivers/ | +58 | 0 replies | |
| 3/3 | vfio/type1: Track GUP vs PFNMAP in external page pinningdrivers/ | +24 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH 0/3] vfio/type1: Fix pin leaks for reserved GUP-pinned pages
vfio/type1 uses PageReserved(), via is_invalid_reserved_pfn(), to guess whether a pfn was GUP-pinned or came from a VM_PFNMAP mapping. Reserved pfns are assumed to be PFNMAP, so they are not accounted in locked_vm and are never unpinned.
That guess is wrong for reserved pages that GUP does pin, such as device-dax. ZONE_DEVICE struct pages are marked PG_reserved when they are initialized, and device-dax, unlike fsdax, can be long-term pinned. When such memory is DMA mapped, or pinned through vfio_pin_pages(), the pins are never dropped.
Commit f6c84a52cc41 ("vfio/type1: optimize vfio_pin_pages_remote()") assumed that long-term GUP never returns reserved pages, so patch 2 also drops the comment that says so.
This series records how each pfn was obtained instead of inferring it:
- Patch 1 makes vpfn_pages() count only the externally pinned pfns that were actually accounted. It fixes a locked_vm imbalance on its own, and patch 2 depends on it.
- Patch 2 tracks GUP vs PFNMAP per vfio_dma for the DMA map/unmap path.
- Patch 3 tracks it per vfio_pfn for external page pinning.
Two things to call out:
- Reserved GUP-pinned pages are now charged against RLIMIT_MEMLOCK like any other long-term pinned page, so mappings that used to bypass the limit can now fail with -ENOMEM.
- A single DMA mapping that contains both PFNMAP and GUP-pinned pfns still falls back to the PageReserved() heuristic on unpin, because type1 does not record which pfn is which. That stays correct unless the GUP-pinned pages are reserved. In that case their pins still leak and, since they are now accounted, so does their locked_vm accounting. Closing that needs per-range tracking.
The DMA map/unmap path (patch 2) was tested on vfio/next by adding device-dax and /dev/mem mappings locally to vfio_dma_mapping_test:
1. 1GiB (262,144 pages) device-dax mapping, checking /proc/vmstat (after flushing /proc/sys/vm/stat_refresh) before and after running:
./vfio_dma_mapping_test -v vfio_type1v2_iommu_devdax_1gb <BDF>
Before fix:
nr_foll_pin_acquired: 27471 -> 289615 (+262144)
nr_foll_pin_released: 27471 -> 27471 (+0, leaked)With fix:
nr_foll_pin_acquired: 18786 -> 280930 (+262144)
nr_foll_pin_released: 18786 -> 280930 (+262144)2. /dev/mem (VM_PFNMAP) mapping of a reserved physical page with a valid struct page (memmap=4M$0x340000000), confirming DMA map/unmap still succeeds with +0/+0 pin deltas and no unpin warnings.
If desirable, I can add the test updates to this series, though the changes depend on host-specific device-dax paths and reserved physical addresses.
Aaron Lewis (3):
vfio/type1: Skip reserved PFNs in vpfn_pages() vfio/type1: Track GUP vs PFNMAP in DMA map/unmap vfio/type1: Track GUP vs PFNMAP in external page pinning
drivers/vfio/vfio_iommu_type1.c | 147 +++++++++++++++++++------------- 1 file changed, 87 insertions(+), 60 deletions(-)
base-commit: 076d7e989cb69f6ded7d60f2a080eb909930061b
Signature · 1 line
2.56.1.708.g77570a6a1d-goog