The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Mostafa Saleh <smostafa@google.com>
To: Fred Griffoul <griffoul@gmail.com>
Cc: Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
	Jason Gunthorpe <jgg@ziepe.ca>, Kevin Tian <kevin.tian@intel.com>,
	Robin Murphy <robin.murphy@arm.com>,
	Shuah Khan <shuah@kernel.org>,
	Guanghui Feng <guanghuifeng@linux.alibaba.com>,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, nh-open-source@amazon.com
Subject: Re: [PATCH 0/3] iommupt: Unmap and return the PFNs in one walk
Date: Mon, 30 Nov 2026 14:59:10 +0000	[thread overview]
Message-ID: <aw2PPoSJI-mRVAI7@google.com> (raw)
In-Reply-To: <20261111185931.94882-1-griffoul@gmail.com>

On Wed, Nov 11, 2026 at 06:59:28PM +0000, Fred Griffoul wrote:
> From: Fred Griffoul <fgriffo@amazon.co.uk>
> 
> When iommufd unmaps an area, it must unpin the user pages, but it keeps
> no list of their PFNs. It reads each one back from the IOMMU domain
> with iommu_iova_to_phys(), one CPU page at a time, then walks the page
> table again in iommu_unmap(). With 4 KiB pages, a 1 GiB IOPTE costs
> 262144 lookups before the unmap starts.
> 
> This series lets the iommupt unmap walk report the physical range of
> each IOPTE as it removes it, so iommufd unmaps and collects the PFNs
> in one walk. The walk also steps over holes, where iommu_unmap() stops
> at the first one.

I looked into this a long time ago, in a different context for pKVM,
where we need to recover the physical addresses for unmapped pages for
the sake of tracking also.

There are similar attempts, with callback backs (at io-pgtable level)
- https://lore.kernel.org/kvmarm/20241212180423.1578358-41-smostafa@google.com/
- https://android.googlesource.com/kernel/common/+/4d1f47f8079859aa5859c6505cb09739154e0287%5E%21/

I wanted to post that for VFIO also, but I was not sure of the value,
so it would be interesting if you have run any benchmarks?

The main challenges I faced with this approach:
- io-pgtable-arm clears table entries without descending, if covered
  by the unmap range which requires extra walks, I see that iommupt
  already walks the whole range anyway which doesn't require extra
  changes.
- Ordering with TLB invalidation, with pKVM, the page tracking has to
  be strictly done after the PTE was cleared and TLBIs are issued,
  it would clear the PTE first and then record and batch physical
  addresses in the callback. This series will keep the PTE live but
  similarly uses batching in the callback, which should be ok.

There are a few Sashiko comments in:
https://sashiko.dev/#/patchset/20261111185931.94882-1-griffoul%40gmail.com
I believe they are true and need to be fixed, one of those is already
existing in the tree in iommu-debug-pagealloc, I will send a patch for
that.

One more thing, is stepping over holes, does IOMMUFD exercise that?

Thanks,
Mostafa

> 
> Patch 1 adds iommu_unmap_sparse(), patch 2 uses it in
> iopt_area_unpin_domain(), and patch 3 adds a selftest for the areas
> that keep the per-page read-back.
> 
> An earlier series [1] proposed iova_to_phys_length() so that iommufd
> can read the PFNs back one IOPTE at a time instead of one page at a
> time. The unmap is still a second walk there. This series collects
> the PFNs in the unmap walk itself.
> 
> The series applies to iommu/next. I wrote parts of the code and the
> changelogs with Kiro, an AI coding assistant using Claude models, and
> used it to run the tests. I reviewed and tested all of it.
> 
> [1] https://lore.kernel.org/linux-iommu/20260603151804.1963871-1-guanghuifeng@linux.alibaba.com/
> 
> Suggested-by: Jason Gunthorpe <jgg@ziepe.ca>
> 
> Fred Griffoul (3):
>   iommupt: Add iommu_unmap_sparse()
>   iommufd: Use iommu_unmap_sparse() to unpin domain pages
>   iommufd/selftest: Unmap areas that start inside a CPU page
> 
>  drivers/iommu/generic_pt/iommu_pt.h       |  61 ++++++++-
>  drivers/iommu/generic_pt/kunit_iommu_pt.h | 144 ++++++++++++++++++++++
>  drivers/iommu/iommu-debug-pagealloc.c     |  13 ++
>  drivers/iommu/iommu-priv.h                |  24 ++++
>  drivers/iommu/iommu.c                     |  76 ++++++++++++
>  drivers/iommu/iommufd/pages.c             |  98 ++++++++++++++-
>  include/linux/generic_pt/iommu.h          |  24 ++++
>  include/linux/iommu.h                     |  22 ++++
>  tools/testing/selftests/iommu/iommufd.c   |  30 +++++
>  9 files changed, 486 insertions(+), 6 deletions(-)
> 
> 
> base-commit: 2cf4be57b301ed2d7af82e70cff5dfd96894d8de
> -- 
> 2.47.3
> 

      parent reply	other threads:[~2026-11-30 14:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 18:59 [PATCH 0/3] iommupt: Unmap and return the PFNs in one walk Fred Griffoul
2026-11-11 18:59 ` [PATCH 1/3] iommupt: Add iommu_unmap_sparse() Fred Griffoul
2026-11-11 18:59 ` [PATCH 2/3] iommufd: Use iommu_unmap_sparse() to unpin domain pages Fred Griffoul
2026-11-11 18:59 ` [PATCH 3/3] iommufd/selftest: Unmap areas that start inside a CPU page Fred Griffoul
2026-11-30 14:59 ` Mostafa Saleh [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=aw2PPoSJI-mRVAI7@google.com \
    --to=smostafa@google.com \
    --cc=griffoul@gmail.com \
    --cc=guanghuifeng@linux.alibaba.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=nh-open-source@amazon.com \
    --cc=robin.murphy@arm.com \
    --cc=shuah@kernel.org \
    --cc=will@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