Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Aaron Lewis" <aaronlewis@google.com>, kvm@vger.kernel.org
Subject: Re: [PATCH 2/3] vfio/type1: Track GUP vs PFNMAP in DMA map/unmap
Date: Thu, 19 Nov 2026 19:12:15 +0000	[thread overview]
Message-ID: <sashiko-outbox-223837@kernel.org> (raw)
In-Reply-To: <20261119185659.889654-3-aaronlewis@google.com>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] vfio/type1: page pin leak in vfio_pin_pages_remote() [1]
--

commit 7c0ecb4964283632e7393bd0af979809631123dd
Author: Aaron Lewis <aaronlewis@google.com>

vfio/type1: Track GUP vs PFNMAP in DMA map/unmap

This commit replaces has_rsvd and has_non_rsvd in struct vfio_dma with
has_pfnmap and has_gup_pinned to track whether pages were pinned via GUP
or mapped via PFNMAP in vaddr_get_pfns(), and clears these flags when the
DMA range is unpinned.

> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> index 7bad0508dd214..ee40ba942fd5e 100644
> --- a/drivers/vfio/vfio_iommu_type1.c
> +++ b/drivers/vfio/vfio_iommu_type1.c
[ ... ]
> @@ -685,7 +700,7 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,
>  	unsigned long pfn;
>  	struct mm_struct *mm = current->mm;
>  	long ret, pinned = 0, lock_acct = 0;
> -	bool rsvd;
> +	bool pfnmap = false;
>  	dma_addr_t iova = vaddr - dma->vaddr + dma->iova;
>  
>  	/* This code path is only user initiated */
> @@ -696,7 +711,6 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,
>  		/* Leftover pages in batch from an earlier call. */
>  		*pfn_base = page_to_pfn(batch->pages[batch->offset]);
>  		pfn = *pfn_base;
> -		rsvd = is_invalid_reserved_pfn(*pfn_base);
>  	} else {
>  		*pfn_base = 0;
>  	}
> @@ -721,12 +735,18 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,
>  
>  			if (!*pfn_base) {
>  				*pfn_base = pfn;
> -				rsvd = is_invalid_reserved_pfn(*pfn_base);
> +				pfnmap = vfio_batch_is_pfnmap(batch);
> +			} else if (pfnmap != vfio_batch_is_pfnmap(batch)) {
> +				/*
> +				 * Handle PFNMAP and GUP pages separately; any
> +				 * GUP pages remain in batch for the next call.
> +				 */
> +				goto out;
>  			}

[Severity: Critical]
This problem wasn't introduced by this patch, but can this overwrite
*pfn_base and leak page pins [1] when the first page frame mapped in
the loop is PFN 0?

*pfn_base is initialized to 0 to indicate that no base frame has been set
yet. If the initial vaddr_get_pfns() call returns PFN 0, *pfn_base remains
0, so on the next batch refill if (!*pfn_base) evaluates to true again and
overwrites *pfn_base and pfnmap with the next batch's values. The
contiguity check (pfn != *pfn_base + pinned) then fails and returns the
overwritten *pfn_base to the caller, mapping the wrong physical pages into
the IOMMU, leaking the pin on PFN 0, and unpinning the wrong PFN range on
unmap. Checking !pinned instead of !*pfn_base would avoid treating PFN 0 as
uninitialized.

[1] https://sashiko.dev/#/bug/linux-8352e900-d371-44e3-bf35-a0a7fd55ac4b

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119185659.889654-1-aaronlewis@google.com?part=2

  reply	other threads:[~2026-11-19 19:12 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 [this message]
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=sashiko-outbox-223837@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aaronlewis@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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