All of lore.kernel.org
 help / color / mirror / Atom feed
From: Aaron Lewis <aaronlewis@google.com>
To: kvm@vger.kernel.org
Cc: alex@shazbot.org, dmatlack@google.com, jgg@nvidia.com,
	 Aaron Lewis <aaronlewis@google.com>
Subject: [PATCH 0/3] vfio/type1: Fix pin leaks for reserved GUP-pinned pages
Date: Thu, 19 Nov 2026 18:56:56 +0000	[thread overview]
Message-ID: <20261119185659.889654-1-aaronlewis@google.com> (raw)

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


             reply	other threads:[~2026-11-19 18:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 18:56 Aaron Lewis [this message]
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
2026-11-19 20:27 ` [PATCH 0/3] vfio/type1: Fix pin leaks for reserved GUP-pinned pages Jason Gunthorpe
2026-11-20 16:59   ` Aaron Lewis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119185659.889654-1-aaronlewis@google.com \
    --to=aaronlewis@google.com \
    --cc=alex@shazbot.org \
    --cc=dmatlack@google.com \
    --cc=jgg@nvidia.com \
    --cc=kvm@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.