All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vipin Sharma <vipinsh@google.com>
To: Aaron Lewis <aaronlewis@google.com>
Cc: kvm@vger.kernel.org, alex@shazbot.org, dmatlack@google.com,
	 jgg@nvidia.com
Subject: Re: [PATCH 2/3] vfio/type1: Track GUP vs PFNMAP in DMA map/unmap
Date: Tue, 1 Dec 2026 16:07:21 -0800	[thread overview]
Message-ID: <20261201234244.GC2554154.vipinsh@google.com> (raw)
In-Reply-To: <20261119185659.889654-3-aaronlewis@google.com>

On Thu, Nov 19, 2026 at 06:56:58PM +0000, Aaron Lewis wrote:
> Both vfio_pin_pages_remote() and vfio_unpin_pages_remote() use
> PageReserved() as a heuristic to infer whether a page was PFNMAP'd or
> GUP-pinned in vaddr_get_pfns(). That heuristic breaks down when the same
> reserved struct pages can appear on both paths:
> 
> - DevDax pages mapped from /dev/dax* are reserved and GUP-pinned.
> - Those same pages mapped from /dev/mem (PFNMAP) are also reserved,
>   but they are *not* GUP-pinned.
> 
> Because of this, when unpinning the DMA region, vfio_unpin_pages_remote()
> doesn't know whether those reserved pages were GUP-pinned and skips
> unpinning them altogether, *leaking page pins and refcounts*.
> 
> Fix this by replacing has_rsvd and has_non_rsvd in struct vfio_dma with
> has_pfnmap and has_gup_pinned, tracking which path vaddr_get_pfns()
> actually used, and clearing them when a DMA range is unpinned. Since
> PFNMAP pages bypass struct vfio_batch, this also fixes locked_vm
> accounting and batch error-unwind unpinning for reserved GUP-pinned
> pages.

This commit has a user visible changes as devdax will now be charged to
RLIMIT_MEMLOCK, we should call it out explicitly in this commit message.
will get 

> 
> This isn't an exhaustive fix because mixed mappings are still allowed.
> For example, if a single DMA region contains both GUP-pinned and
> PFNMAP pages, vfio_unpin_pages_remote() can't tell them apart. Handle
> this by preserving the existing put_pfn() fallback, and add
> a comment explaining the situation.

We still have the bug with the mixed mapping. I was thinking about two
options:

1. We reject DMA mapping (vfio_dma) which have mixed mapping. Allow only
   one type PFNMAP or GUP but not both. I am not sure how big of user
   visible impact it will cause and also not sure if it is very common
   to have mixed mappings.

2. Alternatively, we can also store pfnmap range in vfio_dma. Basically,
   with what half of your patch 2 does (separating PFNMAP and GUP
   processing in vfio_pin_pages_remote) and then storing a pfn mapped
   ranges into an interval tree. During unmap, we can unpin iova ranges
   outside of those pfns. This approach will not leak any pins and gup
   pages will be unpinnned even in mixed case. 
   https://github.com/shvipin/linux/tree/vfio/type1_pfnmap_ranges

   Specifically, in my tree the commit
   78fe78498c30 ("vfio/type1: Track PFNMAP ranges to fix pin leaks in DMA unmap")

I am more inclined with approach 2, as that handles almost all of the
cases. It is not tested but overall it gives the direction. WDYT?
> 
> Lastly, clear the new flags once a vfio_dma is fully unpinned. A vfio_dma
> can outlive its pins and be pinned again later, after
> vfio_iommu_unmap_unpin_reaccount() or a failed vfio_iommu_replay(), by
> which time the user may have remapped the range. A stale has_rsvd only
> selected the slower unpin path, but a stale has_pfnmap would send a
> mapping that is now entirely GUP-pinned down the wrong unpin path.
> 
> Fixes: 3565fce3a659 ("mm, x86: get_user_pages() for dax mappings")

Is this correct fixes tag?

> Signed-off-by: Aaron Lewis <aaronlewis@google.com>
> ---
>  drivers/vfio/vfio_iommu_type1.c | 103 ++++++++++++++++++--------------
>  1 file changed, 58 insertions(+), 45 deletions(-)
> 
> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> index 7bad0508dd21..ee40ba942fd5 100644
> --- a/drivers/vfio/vfio_iommu_type1.c
> +++ b/drivers/vfio/vfio_iommu_type1.c
> @@ -93,8 +93,8 @@ struct vfio_dma {
>  	bool			iommu_mapped;
>  	bool			lock_cap;	/* capable(CAP_IPC_LOCK) */
>  	bool			vaddr_invalid;
> -	bool			has_rsvd;	/* has 1 or more rsvd pfns */
> -	bool			has_non_rsvd;	/* has 1 or more !rsvd pfns */
> +	bool			has_pfnmap;	/* has 1 or more pfnmap pfns */
> +	bool			has_gup_pinned;	/* has 1 or more GUP-pinned pfns */

The patch contains a rename (fields and put_valid_unreserved_pfns()), the fix, 
and the flag-clearing. I think splitting rename to a no-function-change
patch will make it easier to review. Same with clearing flag?


  parent reply	other threads:[~2026-12-02  0:07 UTC|newest]

Thread overview: 13+ 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-12-01 23:42   ` Vipin Sharma
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-12-02  0:07   ` Vipin Sharma [this message]
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
2026-11-25 19:34     ` Alex Williamson
2026-11-30 14:40       ` Aaron Lewis
2026-11-30 23:52         ` Alex Williamson
2026-12-02  0:25           ` Vipin Sharma

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=20261201234244.GC2554154.vipinsh@google.com \
    --to=vipinsh@google.com \
    --cc=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.