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 1/3] vfio/type1: Skip reserved PFNs in vpfn_pages()
Date: Thu, 19 Nov 2026 18:56:57 +0000	[thread overview]
Message-ID: <20261119185659.889654-2-aaronlewis@google.com> (raw)
In-Reply-To: <20261119185659.889654-1-aaronlewis@google.com>

vpfn_pages() is used by vfio_pin_pages_remote() and
vfio_unpin_pages_remote() to count how many pages in an IOVA range are
already accounted in dma->locked_vm via external page pinning.

However, while vfio_pin_page_external() and
vfio_iommu_unmap_unpin_reaccount() skip invalid and reserved PFNs when
accounting dma->locked_vm, vpfn_pages() counts every entry in
dma->pfn_list indiscriminately. If a reserved PFN is externally pinned,
vpfn_pages() reports it as accounted in dma->locked_vm even though it
never was.

As a result, vfio_unpin_pages_remote() inflates locked_vm when it
unwinds a failed vfio_iommu_replay(). vfio_pin_pages_remote() would
under-account for the same reason, but today it only consults
vpfn_pages() for runs that start with a non-reserved PFN. A subsequent
patch removes that restriction and relies on vpfn_pages() being exact.

Check is_invalid_reserved_pfn() in vpfn_pages() so it only counts
externally pinned PFNs that were actually accounted.

Fixes: f6c84a52cc41 ("vfio/type1: optimize vfio_pin_pages_remote()")
Fixes: d10872050ffe ("vfio/type1: batch vfio_find_vpfn() in function vfio_unpin_pages_remote()")
Signed-off-by: Aaron Lewis <aaronlewis@google.com>
---
 drivers/vfio/vfio_iommu_type1.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index f7addfbe1aab..7bad0508dd21 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -641,7 +641,7 @@ static long vpfn_pages(struct vfio_dma *dma,
 {
 	dma_addr_t iova_end = iova_start + (nr_pages << PAGE_SHIFT);
 	struct vfio_pfn *top = vfio_find_vpfn_range(dma, iova_start, iova_end);
-	long ret = 1;
+	long ret = 0;
 	struct vfio_pfn *vpfn;
 	struct rb_node *prev;
 	struct rb_node *next;
@@ -649,20 +649,25 @@ static long vpfn_pages(struct vfio_dma *dma,
 	if (likely(!top))
 		return 0;
 
+	if (!is_invalid_reserved_pfn(top->pfn))
+		ret++;
+
 	prev = next = &top->node;
 
 	while ((prev = rb_prev(prev))) {
 		vpfn = rb_entry(prev, struct vfio_pfn, node);
 		if (vpfn->iova < iova_start)
 			break;
-		ret++;
+		if (!is_invalid_reserved_pfn(vpfn->pfn))
+			ret++;
 	}
 
 	while ((next = rb_next(next))) {
 		vpfn = rb_entry(next, struct vfio_pfn, node);
 		if (vpfn->iova >= iova_end)
 			break;
-		ret++;
+		if (!is_invalid_reserved_pfn(vpfn->pfn))
+			ret++;
 	}
 
 	return ret;
-- 
2.56.1.708.g77570a6a1d-goog


  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 ` Aaron Lewis [this message]
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

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-2-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