]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm/page_ext: Add page_ext_get_from_phys()
authorMostafa Saleh <smostafa@google.com>
Tue, 20 Jan 2026 09:19:25 +0000 (09:19 +0000)
committerJoerg Roedel <joerg.roedel@amd.com>
Wed, 21 Jan 2026 11:51:48 +0000 (12:51 +0100)
The IOMMU code operates on physical addresses which can be outside
of system RAM.

Add a new function page_ext_get_from_phys() to abstract the logic of
checking the address and returning the page_ext.

Signed-off-by: Mostafa Saleh <smostafa@google.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
include/linux/page_ext.h
mm/page_ext.c

index 76c817162d2fb7bcf19e75d49c156cbd2ef5f72a..61e876e255e89cf0fb974078a1bd3f753b2a94fe 100644 (file)
@@ -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_from_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_from_phys(phys_addr_t phys)
+{
+       return NULL;
+}
+
 static inline void page_ext_put(struct page_ext *page_ext)
 {
 }
index 297e4cd8ce90d18f468cbdb6ff42b6dde4cac970..e2e92bd27ebdd0efb400e63aa92e1e0b9dc5053a 100644 (file)
@@ -538,6 +538,29 @@ struct page_ext *page_ext_get(const struct page *page)
        return page_ext;
 }
 
+/**
+ * page_ext_from_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 returns NULL for MMIO, ZONE_DEVICE, holes and 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_from_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().