The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2 0/2] iommu: debug-pagealloc: Small fixes for corner cases
@ 2026-12-02 11:20 Mostafa Saleh
  2026-12-02 11:20 ` [PATCH v2 1/2] iommu: debug-pagealloc: Fix overflow checks Mostafa Saleh
  2026-12-02 11:20 ` [PATCH v2 2/2] iommu: debug-pagealloc: Fix mappings starting at phys 0 Mostafa Saleh
  0 siblings, 2 replies; 4+ messages in thread
From: Mostafa Saleh @ 2026-12-02 11:20 UTC (permalink / raw)
  To: iommu, linux-kernel; +Cc: joro, will, robin.murphy, Mostafa Saleh

Small fixes for iommu-debug-pagealloc, for overflow checks and
the aother for a corner case with physical address of 0.

The first one flagged by Sashiko in
https://sashiko.dev/#/patchset/20261111185931.94882-1-griffoul%40gmail.com
The second one was found by AI while reviewing the first change.

v1: https://lore.kernel.org/all/20261130164628.3170884-1-smostafa@google.com/

Mostafa Saleh (2):
  iommu: debug-pagealloc: Fix overflow checks
  iommu: debug-pagealloc: Fix mappings starting at phys 0

 drivers/iommu/iommu-debug-pagealloc.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

-- 
2.56.1.911.g883d8bcb20-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] iommu: debug-pagealloc: Fix overflow checks
  2026-12-02 11:20 [PATCH v2 0/2] iommu: debug-pagealloc: Small fixes for corner cases Mostafa Saleh
@ 2026-12-02 11:20 ` Mostafa Saleh
  2026-12-02 11:20 ` [PATCH v2 2/2] iommu: debug-pagealloc: Fix mappings starting at phys 0 Mostafa Saleh
  1 sibling, 0 replies; 4+ messages in thread
From: Mostafa Saleh @ 2026-12-02 11:20 UTC (permalink / raw)
  To: iommu, linux-kernel; +Cc: joro, will, robin.murphy, Mostafa Saleh, Sashiko

There are few corner cases with the overflow checks:

- __iommu_debug_map() checks phys + size for overflow into a size_t.
  On a 32-bit kernel with a 64-bit phys_addr_t (as ARM_LPAE) this
  would report an overflow for every mapping that ends at or above
  4 GiB.
  The mapping warns and is not counted, and its unmap then underflows
  the page refcounts.

- check_add_overflow() uses size instead of "size - 1" which would
  fire if the top of the range was mapped.

And while at it, make the end in __iommu_debug_update_iova() an
unsigned long instead of size_t for consistency.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/patchset/20261111185931.94882-1-griffoul%40gmail.com
Fixes: 7e84593795b3 ("iommu: debug-pagealloc: Track IOMMU pages")
Signed-off-by: Mostafa Saleh <smostafa@google.com>
---
 drivers/iommu/iommu-debug-pagealloc.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/iommu/iommu-debug-pagealloc.c b/drivers/iommu/iommu-debug-pagealloc.c
index 80164df5bab1..5faf53aa29f9 100644
--- a/drivers/iommu/iommu-debug-pagealloc.c
+++ b/drivers/iommu/iommu-debug-pagealloc.c
@@ -98,10 +98,11 @@ 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)
 {
-	size_t off, end;
+	size_t off;
+	phys_addr_t end;
 	size_t page_size = iommu_debug_page_size(domain);
 
-	if (WARN_ON(!phys || check_add_overflow(phys, size, &end)))
+	if (!size || WARN_ON(!phys || check_add_overflow(phys, size - 1, &end)))
 		return;
 
 	for (off = 0 ; off < size ; off += page_size)
@@ -111,10 +112,11 @@ void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys, size_t siz
 static void __iommu_debug_update_iova(struct iommu_domain *domain,
 				      unsigned long iova, size_t size, bool inc)
 {
-	size_t off, end;
+	size_t off;
+	unsigned long end;
 	size_t page_size = iommu_debug_page_size(domain);
 
-	if (WARN_ON(check_add_overflow(iova, size, &end)))
+	if (!size || WARN_ON(check_add_overflow(iova, size - 1, &end)))
 		return;
 
 	for (off = 0 ; off < size ; off += page_size) {
-- 
2.56.1.911.g883d8bcb20-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] iommu: debug-pagealloc: Fix mappings starting at phys 0
  2026-12-02 11:20 [PATCH v2 0/2] iommu: debug-pagealloc: Small fixes for corner cases Mostafa Saleh
  2026-12-02 11:20 ` [PATCH v2 1/2] iommu: debug-pagealloc: Fix overflow checks Mostafa Saleh
@ 2026-12-02 11:20 ` Mostafa Saleh
  2026-12-03  7:48   ` Jörg Rödel
  1 sibling, 1 reply; 4+ messages in thread
From: Mostafa Saleh @ 2026-12-02 11:20 UTC (permalink / raw)
  To: iommu, linux-kernel; +Cc: joro, will, robin.murphy, Mostafa Saleh

__iommu_debug_map() warns and counts nothing when phys is 0, but unmap
only skips the page that iommu_iova_to_phys() reports as 0 and
decrements the others,
Unmapping a multi-page mapping that starts at physical address 0
therefore underflows every other page.

Drop the warning and skip only that page, as unmap does.

Fixes: 7e84593795b3 ("iommu: debug-pagealloc: Track IOMMU pages")
Signed-off-by: Mostafa Saleh <smostafa@google.com>
---
 drivers/iommu/iommu-debug-pagealloc.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/iommu-debug-pagealloc.c b/drivers/iommu/iommu-debug-pagealloc.c
index 5faf53aa29f9..348fc4c57ed2 100644
--- a/drivers/iommu/iommu-debug-pagealloc.c
+++ b/drivers/iommu/iommu-debug-pagealloc.c
@@ -102,11 +102,16 @@ void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys, size_t siz
 	phys_addr_t end;
 	size_t page_size = iommu_debug_page_size(domain);
 
-	if (!size || WARN_ON(!phys || check_add_overflow(phys, size - 1, &end)))
+	if (!size || WARN_ON(check_add_overflow(phys, size - 1, &end)))
 		return;
 
-	for (off = 0 ; off < size ; off += page_size)
+	for (off = 0 ; off < size ; off += page_size) {
+		/* Unmap cannot tell PA 0 from an unmapped IOVA */
+		if (!(phys + off))
+			continue;
+
 		iommu_debug_inc_page(phys + off);
+	}
 }
 
 static void __iommu_debug_update_iova(struct iommu_domain *domain,
-- 
2.56.1.911.g883d8bcb20-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 2/2] iommu: debug-pagealloc: Fix mappings starting at phys 0
  2026-12-02 11:20 ` [PATCH v2 2/2] iommu: debug-pagealloc: Fix mappings starting at phys 0 Mostafa Saleh
@ 2026-12-03  7:48   ` Jörg Rödel
  0 siblings, 0 replies; 4+ messages in thread
From: Jörg Rödel @ 2026-12-03  7:48 UTC (permalink / raw)
  To: Mostafa Saleh; +Cc: iommu, linux-kernel, will, robin.murphy

On Wed, Dec 02, 2026 at 11:20:46AM +0000, Mostafa Saleh wrote:
> __iommu_debug_map() warns and counts nothing when phys is 0, but unmap
> only skips the page that iommu_iova_to_phys() reports as 0 and
> decrements the others,
> Unmapping a multi-page mapping that starts at physical address 0
> therefore underflows every other page.
> 
> Drop the warning and skip only that page, as unmap does.
> 
> Fixes: 7e84593795b3 ("iommu: debug-pagealloc: Track IOMMU pages")
> Signed-off-by: Mostafa Saleh <smostafa@google.com>
> ---
>  drivers/iommu/iommu-debug-pagealloc.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iommu/iommu-debug-pagealloc.c b/drivers/iommu/iommu-debug-pagealloc.c
> index 5faf53aa29f9..348fc4c57ed2 100644
> --- a/drivers/iommu/iommu-debug-pagealloc.c
> +++ b/drivers/iommu/iommu-debug-pagealloc.c
> @@ -102,11 +102,16 @@ void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys, size_t siz
>  	phys_addr_t end;
>  	size_t page_size = iommu_debug_page_size(domain);
>  
> -	if (!size || WARN_ON(!phys || check_add_overflow(phys, size - 1, &end)))
> +	if (!size || WARN_ON(check_add_overflow(phys, size - 1, &end)))
>  		return;

Do phys and size need alignment checks for page_size? Otherwise ...

>  
> -	for (off = 0 ; off < size ; off += page_size)
> +	for (off = 0 ; off < size ; off += page_size) {

... off can overflow here when the above checked add with size passed but
page_size is added here.

-Joerg

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-03  7:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 11:20 [PATCH v2 0/2] iommu: debug-pagealloc: Small fixes for corner cases Mostafa Saleh
2026-12-02 11:20 ` [PATCH v2 1/2] iommu: debug-pagealloc: Fix overflow checks Mostafa Saleh
2026-12-02 11:20 ` [PATCH v2 2/2] iommu: debug-pagealloc: Fix mappings starting at phys 0 Mostafa Saleh
2026-12-03  7:48   ` Jörg Rödel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox