* [PATCH 1/2] mm/page_ext: Add page_ext_get_phys()
2026-01-19 14:22 [PATCH 0/2] iommu: debug-pagealloc: Remove pfn_valid() usage Mostafa Saleh
@ 2026-01-19 14:22 ` Mostafa Saleh
2026-01-19 15:48 ` Jörg Rödel
2026-01-19 14:22 ` [PATCH 2/2] iommu: debug-pagealloc: Use page_ext_get_phys() Mostafa Saleh
2026-01-19 14:26 ` [PATCH 0/2] iommu: debug-pagealloc: Remove pfn_valid() usage David Hildenbrand (Red Hat)
2 siblings, 1 reply; 8+ messages in thread
From: Mostafa Saleh @ 2026-01-19 14:22 UTC (permalink / raw)
To: linux-mm, iommu, linux-kernel, linux-doc
Cc: corbet, joro, will, robin.murphy, akpm, vbabka, surenb, mhocko,
jackmanb, hannes, ziy, david, lorenzo.stoakes, Liam.Howlett,
rppt, xiaqinxin, baolu.lu, rdunlap, Mostafa Saleh
In the next patches, IOMMU code will add data to page_ext.
The IOMMU code would operate on physical addresses which
can be outside of system RAM.
Add a new function page_ext_get_phys() to abstract the logic of
checking the address and returning the page_ext.
Signed-off-by: Mostafa Saleh <smostafa@google.com>
---
include/linux/page_ext.h | 6 ++++++
mm/page_ext.c | 23 +++++++++++++++++++++++
2 files changed, 29 insertions(+)
diff --git a/include/linux/page_ext.h b/include/linux/page_ext.h
index 76c817162d2f..bd373496e166 100644
--- a/include/linux/page_ext.h
+++ b/include/linux/page_ext.h
@@ -93,6 +93,7 @@ static inline bool page_ext_iter_next_fast_possible(unsigned long next_pfn)
#endif
extern struct page_ext *page_ext_get(const struct page *page);
+extern struct page_ext *page_ext_get_phys(phys_addr_t phys);
extern void page_ext_put(struct page_ext *page_ext);
extern struct page_ext *page_ext_lookup(unsigned long pfn);
@@ -215,6 +216,11 @@ static inline struct page_ext *page_ext_get(const struct page *page)
return NULL;
}
+static inline struct page_ext *page_ext_get_phys(phys_addr_t phys)
+{
+ return NULL;
+}
+
static inline void page_ext_put(struct page_ext *page_ext)
{
}
diff --git a/mm/page_ext.c b/mm/page_ext.c
index 297e4cd8ce90..5fe65a0ac4f3 100644
--- a/mm/page_ext.c
+++ b/mm/page_ext.c
@@ -538,6 +538,29 @@ struct page_ext *page_ext_get(const struct page *page)
return page_ext;
}
+/**
+ * page_ext_get_phys() - Get the page_ext structure for a physical address.
+ * @phys: The physical address to query.
+ *
+ * This function safely gets the `struct page_ext` associated with a given
+ * physical address. It performs validation to ensure the address corresponds
+ * to a valid, online struct page before attempting to access it.
+ * It should return NULL for (MMIO, ZONE_DEVICE, holes, offline memory)
+ *
+ * Return: NULL if no page_ext exists for this physical address.
+ * Context: Any context. Caller may not sleep until they have called
+ * page_ext_put().
+ */
+struct page_ext *page_ext_get_phys(phys_addr_t phys)
+{
+ struct page *page = pfn_to_online_page(__phys_to_pfn(phys));
+
+ if (!page)
+ return NULL;
+
+ return page_ext_get(page);
+}
+
/**
* page_ext_put() - Working with page extended information is done.
* @page_ext: Page extended information received from page_ext_get().
--
2.52.0.457.g6b5491de43-goog
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH 1/2] mm/page_ext: Add page_ext_get_phys()
2026-01-19 14:22 ` [PATCH 1/2] mm/page_ext: Add page_ext_get_phys() Mostafa Saleh
@ 2026-01-19 15:48 ` Jörg Rödel
2026-01-19 15:55 ` Mostafa Saleh
0 siblings, 1 reply; 8+ messages in thread
From: Jörg Rödel @ 2026-01-19 15:48 UTC (permalink / raw)
To: Mostafa Saleh
Cc: linux-mm, iommu, linux-kernel, linux-doc, corbet, will,
robin.murphy, akpm, vbabka, surenb, mhocko, jackmanb, hannes,
ziy, david, lorenzo.stoakes, Liam.Howlett, rppt, xiaqinxin,
baolu.lu, rdunlap
On Mon, Jan 19, 2026 at 02:22:45PM +0000, Mostafa Saleh wrote:
> +static inline struct page_ext *page_ext_get_phys(phys_addr_t phys)
The name is misleading as it indicates that the function returns a physical
address. Maybe name it page_ext_from_phys()?
> +{
> + return NULL;
> +}
> +
> static inline void page_ext_put(struct page_ext *page_ext)
> {
> }
> diff --git a/mm/page_ext.c b/mm/page_ext.c
> index 297e4cd8ce90..5fe65a0ac4f3 100644
> --- a/mm/page_ext.c
> +++ b/mm/page_ext.c
> @@ -538,6 +538,29 @@ struct page_ext *page_ext_get(const struct page *page)
> return page_ext;
> }
>
> +/**
> + * page_ext_get_phys() - Get the page_ext structure for a physical address.
> + * @phys: The physical address to query.
> + *
> + * This function safely gets the `struct page_ext` associated with a given
> + * physical address. It performs validation to ensure the address corresponds
> + * to a valid, online struct page before attempting to access it.
> + * It should return NULL for (MMIO, ZONE_DEVICE, holes, offline memory)
It should?
-Joerg
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH 1/2] mm/page_ext: Add page_ext_get_phys()
2026-01-19 15:48 ` Jörg Rödel
@ 2026-01-19 15:55 ` Mostafa Saleh
0 siblings, 0 replies; 8+ messages in thread
From: Mostafa Saleh @ 2026-01-19 15:55 UTC (permalink / raw)
To: Jörg Rödel
Cc: linux-mm, iommu, linux-kernel, linux-doc, corbet, will,
robin.murphy, akpm, vbabka, surenb, mhocko, jackmanb, hannes,
ziy, david, lorenzo.stoakes, Liam.Howlett, rppt, xiaqinxin,
baolu.lu, rdunlap
On Mon, Jan 19, 2026 at 3:49 PM Jörg Rödel <joro@8bytes.org> wrote:
>
> On Mon, Jan 19, 2026 at 02:22:45PM +0000, Mostafa Saleh wrote:
> > +static inline struct page_ext *page_ext_get_phys(phys_addr_t phys)
>
> The name is misleading as it indicates that the function returns a physical
> address. Maybe name it page_ext_from_phys()?
I will update it.
>
> > +{
> > + return NULL;
> > +}
> > +
> > static inline void page_ext_put(struct page_ext *page_ext)
> > {
> > }
> > diff --git a/mm/page_ext.c b/mm/page_ext.c
> > index 297e4cd8ce90..5fe65a0ac4f3 100644
> > --- a/mm/page_ext.c
> > +++ b/mm/page_ext.c
> > @@ -538,6 +538,29 @@ struct page_ext *page_ext_get(const struct page *page)
> > return page_ext;
> > }
> >
> > +/**
> > + * page_ext_get_phys() - Get the page_ext structure for a physical address.
> > + * @phys: The physical address to query.
> > + *
> > + * This function safely gets the `struct page_ext` associated with a given
> > + * physical address. It performs validation to ensure the address corresponds
> > + * to a valid, online struct page before attempting to access it.
> > + * It should return NULL for (MMIO, ZONE_DEVICE, holes, offline memory)
>
> It should?
>
Yes, I can make it more definitive as "it returns"
Thanks,
Mostafa
> -Joerg
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/2] iommu: debug-pagealloc: Use page_ext_get_phys()
2026-01-19 14:22 [PATCH 0/2] iommu: debug-pagealloc: Remove pfn_valid() usage Mostafa Saleh
2026-01-19 14:22 ` [PATCH 1/2] mm/page_ext: Add page_ext_get_phys() Mostafa Saleh
@ 2026-01-19 14:22 ` Mostafa Saleh
2026-01-19 14:26 ` [PATCH 0/2] iommu: debug-pagealloc: Remove pfn_valid() usage David Hildenbrand (Red Hat)
2 siblings, 0 replies; 8+ messages in thread
From: Mostafa Saleh @ 2026-01-19 14:22 UTC (permalink / raw)
To: linux-mm, iommu, linux-kernel, linux-doc
Cc: corbet, joro, will, robin.murphy, akpm, vbabka, surenb, mhocko,
jackmanb, hannes, ziy, david, lorenzo.stoakes, Liam.Howlett,
rppt, xiaqinxin, baolu.lu, rdunlap, Mostafa Saleh
Instead of calling pfn_valid() and then getting the page, call
the newly added function page_ext_get_phys(), which would also
check for MMIO and offline memory and return NULL in that case.
Signed-off-by: Mostafa Saleh <smostafa@google.com>
---
drivers/iommu/iommu-debug-pagealloc.c | 31 ++++++++++++---------------
1 file changed, 14 insertions(+), 17 deletions(-)
diff --git a/drivers/iommu/iommu-debug-pagealloc.c b/drivers/iommu/iommu-debug-pagealloc.c
index c080a38f45a4..9343af3bdbb6 100644
--- a/drivers/iommu/iommu-debug-pagealloc.c
+++ b/drivers/iommu/iommu-debug-pagealloc.c
@@ -30,14 +30,6 @@ struct page_ext_operations page_iommu_debug_ops = {
.need = need_iommu_debug,
};
-static struct page_ext *get_iommu_page_ext(phys_addr_t phys)
-{
- struct page *page = phys_to_page(phys);
- struct page_ext *page_ext = page_ext_get(page);
-
- return page_ext;
-}
-
static struct iommu_debug_metadata *get_iommu_data(struct page_ext *page_ext)
{
return page_ext_data(page_ext, &page_iommu_debug_ops);
@@ -45,18 +37,26 @@ static struct iommu_debug_metadata *get_iommu_data(struct page_ext *page_ext)
static void iommu_debug_inc_page(phys_addr_t phys)
{
- struct page_ext *page_ext = get_iommu_page_ext(phys);
- struct iommu_debug_metadata *d = get_iommu_data(page_ext);
+ struct page_ext *page_ext = page_ext_get_phys(phys);
+ struct iommu_debug_metadata *d;
+
+ if (!page_ext)
+ return;
+ d = get_iommu_data(page_ext);
WARN_ON(atomic_inc_return_relaxed(&d->ref) <= 0);
page_ext_put(page_ext);
}
static void iommu_debug_dec_page(phys_addr_t phys)
{
- struct page_ext *page_ext = get_iommu_page_ext(phys);
- struct iommu_debug_metadata *d = get_iommu_data(page_ext);
+ struct page_ext *page_ext = page_ext_get_phys(phys);
+ struct iommu_debug_metadata *d;
+
+ if (!page_ext)
+ return;
+ d = get_iommu_data(page_ext);
WARN_ON(atomic_dec_return_relaxed(&d->ref) < 0);
page_ext_put(page_ext);
}
@@ -104,11 +104,8 @@ 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) {
- if (!pfn_valid(__phys_to_pfn(phys + off)))
- continue;
+ for (off = 0 ; off < size ; off += page_size)
iommu_debug_inc_page(phys + off);
- }
}
static void __iommu_debug_update_iova(struct iommu_domain *domain,
@@ -123,7 +120,7 @@ static void __iommu_debug_update_iova(struct iommu_domain *domain,
for (off = 0 ; off < size ; off += page_size) {
phys_addr_t phys = iommu_iova_to_phys(domain, iova + off);
- if (!phys || !pfn_valid(__phys_to_pfn(phys)))
+ if (!phys)
continue;
if (inc)
--
2.52.0.457.g6b5491de43-goog
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH 0/2] iommu: debug-pagealloc: Remove pfn_valid() usage
2026-01-19 14:22 [PATCH 0/2] iommu: debug-pagealloc: Remove pfn_valid() usage Mostafa Saleh
2026-01-19 14:22 ` [PATCH 1/2] mm/page_ext: Add page_ext_get_phys() Mostafa Saleh
2026-01-19 14:22 ` [PATCH 2/2] iommu: debug-pagealloc: Use page_ext_get_phys() Mostafa Saleh
@ 2026-01-19 14:26 ` David Hildenbrand (Red Hat)
2026-01-19 15:09 ` Mostafa Saleh
2 siblings, 1 reply; 8+ messages in thread
From: David Hildenbrand (Red Hat) @ 2026-01-19 14:26 UTC (permalink / raw)
To: Mostafa Saleh, linux-mm, iommu, linux-kernel, linux-doc
Cc: corbet, joro, will, robin.murphy, akpm, vbabka, surenb, mhocko,
jackmanb, hannes, ziy, lorenzo.stoakes, Liam.Howlett, rppt,
xiaqinxin, baolu.lu, rdunlap
On 1/19/26 15:22, Mostafa Saleh wrote:
> This is a small fix for the new config IOMMU_DEBUG_PAGEALLOC based
> on the discussion:
> https://lore.kernel.org/linux-iommu/CAFgf54pBAUm3ao-UJksiuGKtvv4wzRyFq_uKwLe0H1ettO4DLQ@mail.gmail.com/
>
> Where it was concluded that pfn_valid() is not enough to validate
> physical addresses before access to page_ext.
>
> The first patch introduces a new function in page_ext that takes a
> physical address as an argument, and the second patch uses it instead
> of calling pfn_valid() and phys_to_page()
>
> Benchmarks with the new implementation can be found in:
> https://lore.kernel.org/linux-iommu/20260114164322.787125-1-smostafa@google.com/
>
> This series applies to iommu/core tree.
This is part of v7 [1], right? Can't we just apply v7 instead or are the
commit IDs already stable?
[1] https://lkml.kernel.org/r/20260114164322.787125-1-smostafa@google.com
--
Cheers
David
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 0/2] iommu: debug-pagealloc: Remove pfn_valid() usage
2026-01-19 14:26 ` [PATCH 0/2] iommu: debug-pagealloc: Remove pfn_valid() usage David Hildenbrand (Red Hat)
@ 2026-01-19 15:09 ` Mostafa Saleh
2026-01-19 15:16 ` David Hildenbrand (Red Hat)
0 siblings, 1 reply; 8+ messages in thread
From: Mostafa Saleh @ 2026-01-19 15:09 UTC (permalink / raw)
To: David Hildenbrand (Red Hat)
Cc: linux-mm, iommu, linux-kernel, linux-doc, corbet, joro, will,
robin.murphy, akpm, vbabka, surenb, mhocko, jackmanb, hannes,
ziy, lorenzo.stoakes, Liam.Howlett, rppt, xiaqinxin, baolu.lu,
rdunlap
On Mon, Jan 19, 2026 at 2:27 PM David Hildenbrand (Red Hat)
<david@kernel.org> wrote:
>
> On 1/19/26 15:22, Mostafa Saleh wrote:
> > This is a small fix for the new config IOMMU_DEBUG_PAGEALLOC based
> > on the discussion:
> > https://lore.kernel.org/linux-iommu/CAFgf54pBAUm3ao-UJksiuGKtvv4wzRyFq_uKwLe0H1ettO4DLQ@mail.gmail.com/
> >
> > Where it was concluded that pfn_valid() is not enough to validate
> > physical addresses before access to page_ext.
> >
> > The first patch introduces a new function in page_ext that takes a
> > physical address as an argument, and the second patch uses it instead
> > of calling pfn_valid() and phys_to_page()
> >
> > Benchmarks with the new implementation can be found in:
> > https://lore.kernel.org/linux-iommu/20260114164322.787125-1-smostafa@google.com/
> >
> > This series applies to iommu/core tree.
>
> This is part of v7 [1], right? Can't we just apply v7 instead or are the
> commit IDs already stable?
>
It should be stable according to Will:
https://lore.kernel.org/linux-iommu/20260114164322.787125-1-smostafa@google.com/T/#m59d0b455e3f2160cb6f9980a0ae65bc481c53898
Thanks,
Mostafa
> [1] https://lkml.kernel.org/r/20260114164322.787125-1-smostafa@google.com
>
> --
> Cheers
>
> David
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 0/2] iommu: debug-pagealloc: Remove pfn_valid() usage
2026-01-19 15:09 ` Mostafa Saleh
@ 2026-01-19 15:16 ` David Hildenbrand (Red Hat)
0 siblings, 0 replies; 8+ messages in thread
From: David Hildenbrand (Red Hat) @ 2026-01-19 15:16 UTC (permalink / raw)
To: Mostafa Saleh
Cc: linux-mm, iommu, linux-kernel, linux-doc, corbet, joro, will,
robin.murphy, akpm, vbabka, surenb, mhocko, jackmanb, hannes,
ziy, lorenzo.stoakes, Liam.Howlett, rppt, xiaqinxin, baolu.lu,
rdunlap
On 1/19/26 16:09, Mostafa Saleh wrote:
> On Mon, Jan 19, 2026 at 2:27 PM David Hildenbrand (Red Hat)
> <david@kernel.org> wrote:
>>
>> On 1/19/26 15:22, Mostafa Saleh wrote:
>>> This is a small fix for the new config IOMMU_DEBUG_PAGEALLOC based
>>> on the discussion:
>>> https://lore.kernel.org/linux-iommu/CAFgf54pBAUm3ao-UJksiuGKtvv4wzRyFq_uKwLe0H1ettO4DLQ@mail.gmail.com/
>>>
>>> Where it was concluded that pfn_valid() is not enough to validate
>>> physical addresses before access to page_ext.
>>>
>>> The first patch introduces a new function in page_ext that takes a
>>> physical address as an argument, and the second patch uses it instead
>>> of calling pfn_valid() and phys_to_page()
>>>
>>> Benchmarks with the new implementation can be found in:
>>> https://lore.kernel.org/linux-iommu/20260114164322.787125-1-smostafa@google.com/
>>>
>>> This series applies to iommu/core tree.
>>
>> This is part of v7 [1], right? Can't we just apply v7 instead or are the
>> commit IDs already stable?
>>
>
> It should be stable according to Will:
> https://lore.kernel.org/linux-iommu/20260114164322.787125-1-smostafa@google.com/T/#m59d0b455e3f2160cb6f9980a0ae65bc481c53898
Shame, probably applied a bit too fast ...
--
Cheers
David
^ permalink raw reply [flat|nested] 8+ messages in thread