All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mostafa Saleh <smostafa@google.com>
To: Yuanhe Shu <xiangzao@linux.alibaba.com>
Cc: joro@8bytes.org, will@kernel.org, jgg@ziepe.ca,
	robin.murphy@arm.com, baolu.lu@linux.intel.com,
	kevin.tian@intel.com, praan@google.com, skhawaja@google.com,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] iommu: Add __iommu_debug_unmap_phys() to drop refs by physical address
Date: Thu, 27 Aug 2026 16:13:26 +0000	[thread overview]
Message-ID: <apBiJk_zhaNjW0er@google.com> (raw)
In-Reply-To: <20260827145855.1616223-2-xiangzao@linux.alibaba.com>

On Thu, Aug 27, 2026 at 10:58:54PM +0800, Yuanhe Shu wrote:
> The IOMMU_DEBUG_PAGEALLOC sanitizer takes a reference on each page at
> iommu_map() time, keyed by physical address, and the only way to drop
> them is the IOVA based iommu_debug_unmap_begin()/end().  A path that
> tears down a page table with mappings still installed has no IOVA to
> unmap with, so add a physical address based counterpart of
> __iommu_debug_map() for those paths, sharing the counting loop through
> __iommu_debug_update_phys().

But the IOVA can be calculated when walking the table from freeing
context or am I missing something?

Thanks,
Mostafa

> 
> Export the iommu_debug_initialized static key, as the generic_pt format
> code using these helpers can be built as a module.
> 
> Signed-off-by: Yuanhe Shu <xiangzao@linux.alibaba.com>
> ---
> Build tested as built-in and as a module (AMD_IOMMU=n,
> CONFIG_IOMMU_PT_AMDV1=m).
> 
>  drivers/iommu/iommu-debug-pagealloc.c | 30 ++++++++++++++++++++++++---
>  drivers/iommu/iommu-priv.h            | 24 +++++++++++++++++++++
>  2 files changed, 51 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iommu/iommu-debug-pagealloc.c b/drivers/iommu/iommu-debug-pagealloc.c
> index 80164df5bab1..a2e55164e6a4 100644
> --- a/drivers/iommu/iommu-debug-pagealloc.c
> +++ b/drivers/iommu/iommu-debug-pagealloc.c
> @@ -15,6 +15,8 @@
>  
>  static bool needed;
>  DEFINE_STATIC_KEY_FALSE(iommu_debug_initialized);
> +/* The generic_pt format code using the key can be built as a module */
> +EXPORT_SYMBOL_GPL(iommu_debug_initialized);
>  
>  struct iommu_debug_metadata {
>  	atomic_t ref;
> @@ -96,7 +98,8 @@ void __iommu_debug_check_unmapped(const struct page *page, int numpages)
>  	}
>  }
>  
> -void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys, size_t size)
> +static void __iommu_debug_update_phys(struct iommu_domain *domain,
> +				      phys_addr_t phys, size_t size, bool inc)
>  {
>  	size_t off, end;
>  	size_t page_size = iommu_debug_page_size(domain);
> @@ -104,9 +107,30 @@ void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys, size_t siz
>  	if (WARN_ON(!phys || check_add_overflow(phys, size, &end)))
>  		return;
>  
> -	for (off = 0 ; off < size ; off += page_size)
> -		iommu_debug_inc_page(phys + off);
> +	for (off = 0 ; off < size ; off += page_size) {
> +		if (inc)
> +			iommu_debug_inc_page(phys + off);
> +		else
> +			iommu_debug_dec_page(phys + off);
> +	}
> +}
> +
> +void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys, size_t size)
> +{
> +	__iommu_debug_update_phys(domain, phys, size, true);
> +}
> +
> +/*
> + * Physical address counterpart of __iommu_debug_map(), for teardown paths
> + * that destroy mapped entries without an IOVA.  The OAs must have been
> + * accounted by a prior iommu_map().
> + */
> +void __iommu_debug_unmap_phys(struct iommu_domain *domain, phys_addr_t phys,
> +			      size_t size)
> +{
> +	__iommu_debug_update_phys(domain, phys, size, false);
>  }
> +EXPORT_SYMBOL_GPL(__iommu_debug_unmap_phys);
>  
>  static void __iommu_debug_update_iova(struct iommu_domain *domain,
>  				      unsigned long iova, size_t size, bool inc)
> diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h
> index aaffad5854fc..12528a40bcd8 100644
> --- a/drivers/iommu/iommu-priv.h
> +++ b/drivers/iommu/iommu-priv.h
> @@ -71,11 +71,18 @@ int iommu_replace_device_pasid(struct iommu_domain *domain,
>  
>  void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys,
>  		       size_t size);
> +void __iommu_debug_unmap_phys(struct iommu_domain *domain, phys_addr_t phys,
> +			      size_t size);
>  void __iommu_debug_unmap_begin(struct iommu_domain *domain,
>  			       unsigned long iova, size_t size);
>  void __iommu_debug_unmap_end(struct iommu_domain *domain,
>  			     unsigned long iova, size_t size, size_t unmapped);
>  
> +static inline bool iommu_debug_pagealloc_enabled(void)
> +{
> +	return static_branch_unlikely(&iommu_debug_initialized);
> +}
> +
>  static inline void iommu_debug_map(struct iommu_domain *domain,
>  				   phys_addr_t phys, size_t size)
>  {
> @@ -83,6 +90,13 @@ static inline void iommu_debug_map(struct iommu_domain *domain,
>  		__iommu_debug_map(domain, phys, size);
>  }
>  
> +static inline void iommu_debug_unmap_phys(struct iommu_domain *domain,
> +					  phys_addr_t phys, size_t size)
> +{
> +	if (static_branch_unlikely(&iommu_debug_initialized))
> +		__iommu_debug_unmap_phys(domain, phys, size);
> +}
> +
>  static inline void iommu_debug_unmap_begin(struct iommu_domain *domain,
>  					   unsigned long iova, size_t size)
>  {
> @@ -101,11 +115,21 @@ static inline void iommu_debug_unmap_end(struct iommu_domain *domain,
>  void iommu_debug_init(void);
>  
>  #else
> +static inline bool iommu_debug_pagealloc_enabled(void)
> +{
> +	return false;
> +}
> +
>  static inline void iommu_debug_map(struct iommu_domain *domain,
>  				   phys_addr_t phys, size_t size)
>  {
>  }
>  
> +static inline void iommu_debug_unmap_phys(struct iommu_domain *domain,
> +					  phys_addr_t phys, size_t size)
> +{
> +}
> +
>  static inline void iommu_debug_unmap_begin(struct iommu_domain *domain,
>  					   unsigned long iova, size_t size)
>  {
> -- 
> 2.43.5
> 

  reply	other threads:[~2026-08-27 16:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-27 14:58 [PATCH 0/2] iommu: Drop IOMMU_DEBUG_PAGEALLOC refs on iommupt domain teardown Yuanhe Shu
2026-08-27 14:58 ` [PATCH 1/2] iommu: Add __iommu_debug_unmap_phys() to drop refs by physical address Yuanhe Shu
2026-08-27 16:13   ` Mostafa Saleh [this message]
2026-08-27 16:17     ` Mostafa Saleh
2026-08-27 16:21   ` Jason Gunthorpe
2026-08-31  6:21     ` Yuanhe Shu
2026-08-27 14:58 ` [PATCH 2/2] iommupt: Drop pagealloc references during domain deinit Yuanhe Shu
2026-08-27 16:18   ` Jason Gunthorpe
2026-08-31  6:16     ` Yuanhe Shu
2026-08-27 16:05 ` [PATCH 0/2] iommu: Drop IOMMU_DEBUG_PAGEALLOC refs on iommupt domain teardown Mostafa Saleh
2026-08-31  6:23   ` Yuanhe Shu

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=apBiJk_zhaNjW0er@google.com \
    --to=smostafa@google.com \
    --cc=baolu.lu@linux.intel.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=praan@google.com \
    --cc=robin.murphy@arm.com \
    --cc=skhawaja@google.com \
    --cc=will@kernel.org \
    --cc=xiangzao@linux.alibaba.com \
    /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.