Kernel KVM virtualization development
 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 3/3] vfio/type1: Track GUP vs PFNMAP in external page pinning
Date: Thu, 19 Nov 2026 18:56:59 +0000	[thread overview]
Message-ID: <20261119185659.889654-4-aaronlewis@google.com> (raw)
In-Reply-To: <20261119185659.889654-1-aaronlewis@google.com>

Like the DMA map/unmap path, external page pinning for emulated IOMMU
drivers (vfio_iommu_type1_pin_pages()) uses the same heuristic
(is_invalid_reserved_pfn()) to infer if a struct vfio_pfn was PFNMAP'd
or GUP-pinned in vaddr_get_pfns().

That heuristic falls apart for reserved GUP-pinned mappings such as
device-dax. They are treated as PFNMAP, so they are never unpinned,
which leaks page pins and refcounts. They are also left out of locked_vm
accounting.

Record in struct vfio_pfn whether vaddr_get_pfns() used the PFNMAP path,
and use that (vpfn->pfnmap) across all external pinning, unpinning, and
locked_vm accounting paths instead of inferring it.

As with the DMA map/unmap path, GUP-pinned reserved pages are now
charged against RLIMIT_MEMLOCK. When no IOMMU-backed domain is present,
vfio_pin_pages() on such memory can now fail with -ENOMEM where it
previously bypassed the limit.

Opportunistically, drop the forward declaration of put_pfn(), which is
no longer needed.

Fixes: a54eb55045ae ("vfio iommu type1: Add support for mediated devices")
Signed-off-by: Aaron Lewis <aaronlewis@google.com>
---
 drivers/vfio/vfio_iommu_type1.c | 39 ++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index ee40ba942fd5..b9c67ac0b16e 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -130,6 +130,7 @@ struct vfio_pfn {
 	dma_addr_t		iova;		/* Device address */
 	unsigned long		pfn;		/* Host pfn */
 	unsigned int		ref_count;
+	bool			pfnmap;
 };
 
 struct vfio_regions {
@@ -152,8 +153,6 @@ struct vfio_regions {
 #define DIRTY_BITMAP_PAGES_MAX	 ((u64)INT_MAX)
 #define DIRTY_BITMAP_SIZE_MAX	 DIRTY_BITMAP_BYTES(DIRTY_BITMAP_PAGES_MAX)
 
-static int put_pfn(unsigned long pfn, int prot);
-
 static struct vfio_iommu_group*
 vfio_iommu_find_iommu_group(struct vfio_iommu *iommu,
 			    struct iommu_group *iommu_group);
@@ -384,7 +383,7 @@ static void vfio_unlink_pfn(struct vfio_dma *dma, struct vfio_pfn *old)
 }
 
 static int vfio_add_to_pfn_list(struct vfio_dma *dma, dma_addr_t iova,
-				unsigned long pfn)
+				unsigned long pfn, bool pfnmap)
 {
 	struct vfio_pfn *vpfn;
 
@@ -395,6 +394,7 @@ static int vfio_add_to_pfn_list(struct vfio_dma *dma, dma_addr_t iova,
 	vpfn->iova = iova;
 	vpfn->pfn = pfn;
 	vpfn->ref_count = 1;
+	vpfn->pfnmap = pfnmap;
 	vfio_link_pfn(dma, vpfn);
 	return 0;
 }
@@ -429,7 +429,10 @@ static int vfio_iova_put_vfio_pfn(struct vfio_dma *dma, struct vfio_pfn *vpfn)
 
 	vpfn->ref_count--;
 	if (!vpfn->ref_count) {
-		ret = put_pfn(vpfn->pfn, dma->prot);
+		if (!vpfn->pfnmap) {
+			put_gup_pfns(vpfn->pfn, 1, dma->prot);
+			ret = 1;
+		}
 		vfio_remove_from_pfn_list(dma, vpfn);
 	}
 	return ret;
@@ -655,7 +658,7 @@ static long vpfn_pages(struct vfio_dma *dma,
 	if (likely(!top))
 		return 0;
 
-	if (!is_invalid_reserved_pfn(top->pfn))
+	if (!top->pfnmap)
 		ret++;
 
 	prev = next = &top->node;
@@ -664,7 +667,7 @@ static long vpfn_pages(struct vfio_dma *dma,
 		vpfn = rb_entry(prev, struct vfio_pfn, node);
 		if (vpfn->iova < iova_start)
 			break;
-		if (!is_invalid_reserved_pfn(vpfn->pfn))
+		if (!vpfn->pfnmap)
 			ret++;
 	}
 
@@ -672,7 +675,7 @@ static long vpfn_pages(struct vfio_dma *dma,
 		vpfn = rb_entry(next, struct vfio_pfn, node);
 		if (vpfn->iova >= iova_end)
 			break;
-		if (!is_invalid_reserved_pfn(vpfn->pfn))
+		if (!vpfn->pfnmap)
 			ret++;
 	}
 
@@ -853,7 +856,8 @@ static long vfio_unpin_pages_remote(struct vfio_dma *dma, dma_addr_t iova,
 }
 
 static int vfio_pin_page_external(struct vfio_dma *dma, unsigned long vaddr,
-				  unsigned long *pfn_base, bool do_accounting)
+				  unsigned long *pfn_base, bool do_accounting,
+				  bool *pfnmap)
 {
 	struct vfio_batch batch;
 	struct mm_struct *mm;
@@ -869,12 +873,13 @@ static int vfio_pin_page_external(struct vfio_dma *dma, unsigned long vaddr,
 	if (ret != 1)
 		goto out;
 
+	*pfnmap = vfio_batch_is_pfnmap(&batch);
 	ret = 0;
 
-	if (do_accounting && !is_invalid_reserved_pfn(*pfn_base)) {
+	if (do_accounting && !*pfnmap) {
 		ret = vfio_lock_acct(dma, 1, false);
 		if (ret) {
-			put_pfn(*pfn_base, dma->prot);
+			put_gup_pfns(*pfn_base, 1, dma->prot);
 			if (ret == -ENOMEM)
 				pr_warn("%s: Task %s (%d) RLIMIT_MEMLOCK "
 					"(%ld) exceeded\n", __func__,
@@ -957,6 +962,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
 		unsigned long phys_pfn;
 		dma_addr_t iova;
 		struct vfio_pfn *vpfn;
+		bool pfnmap;
 
 		iova = user_iova + PAGE_SIZE * i;
 		dma = vfio_find_dma(iommu, iova, PAGE_SIZE);
@@ -978,7 +984,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
 
 		remote_vaddr = dma->vaddr + (iova - dma->iova);
 		ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn,
-					     do_accounting);
+					     do_accounting, &pfnmap);
 		if (ret)
 			goto pin_unwind;
 
@@ -987,10 +993,13 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
 			goto pin_unwind;
 		}
 
-		ret = vfio_add_to_pfn_list(dma, iova, phys_pfn);
+		ret = vfio_add_to_pfn_list(dma, iova, phys_pfn, pfnmap);
 		if (ret) {
-			if (put_pfn(phys_pfn, dma->prot) && do_accounting)
-				vfio_lock_acct(dma, -1, true);
+			if (!pfnmap) {
+				put_gup_pfns(phys_pfn, 1, dma->prot);
+				if (do_accounting)
+					vfio_lock_acct(dma, -1, true);
+			}
 			goto pin_unwind;
 		}
 
@@ -2471,7 +2480,7 @@ static void vfio_iommu_unmap_unpin_reaccount(struct vfio_iommu *iommu)
 			struct vfio_pfn *vpfn = rb_entry(p, struct vfio_pfn,
 							 node);
 
-			if (!is_invalid_reserved_pfn(vpfn->pfn))
+			if (!vpfn->pfnmap)
 				locked++;
 		}
 		vfio_lock_acct(dma, locked - unlocked, true);
-- 
2.56.1.708.g77570a6a1d-goog


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

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Aaron Lewis [this message]

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-4-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox