[PATCH 1/2] mm/page_ext: Add page_ext_get_phys()

Mostafa Saleh posted 2 patches 2 weeks, 5 days ago
There is a newer version of this series
[PATCH 1/2] mm/page_ext: Add page_ext_get_phys()
Posted by Mostafa Saleh 2 weeks, 5 days ago
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
Re: [PATCH 1/2] mm/page_ext: Add page_ext_get_phys()
Posted by Jörg Rödel 2 weeks, 5 days ago
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
Re: [PATCH 1/2] mm/page_ext: Add page_ext_get_phys()
Posted by Mostafa Saleh 2 weeks, 5 days ago
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