Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH 0/3] vfio/type1: Fix pin leaks for reserved GUP-pinned pages
@ 2026-11-19 18:56 Aaron Lewis
  2026-11-19 18:56 ` [PATCH 1/3] vfio/type1: Skip reserved PFNs in vpfn_pages() Aaron Lewis
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Aaron Lewis @ 2026-11-19 18:56 UTC (permalink / raw)
  To: kvm; +Cc: alex, dmatlack, jgg, Aaron Lewis

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
-- 
2.56.1.708.g77570a6a1d-goog


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-19 19:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 18:56 [PATCH 0/3] vfio/type1: Fix pin leaks for reserved GUP-pinned pages Aaron Lewis
2026-11-19 18:56 ` [PATCH 1/3] vfio/type1: Skip reserved PFNs in vpfn_pages() Aaron Lewis
2026-11-19 18:56 ` [PATCH 2/3] vfio/type1: Track GUP vs PFNMAP in DMA map/unmap Aaron Lewis
2026-11-19 19:12   ` sashiko-bot
2026-11-19 18:56 ` [PATCH 3/3] vfio/type1: Track GUP vs PFNMAP in external page pinning Aaron Lewis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox