diff mbox series

[2/5] hugetlb: Add hugetlb_pfn_folio

Message ID 20240301214712.2853147-3-willy@infradead.org (mailing list archive)
State New
Headers show
Series Remove some races around folio_test_hugetlb | expand

Commit Message

Matthew Wilcox March 1, 2024, 9:47 p.m. UTC
Returns the hugetlb folio containing this PFN or NULL if the PFN
is not within a hugetlb folio.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 include/linux/hugetlb.h |  1 +
 mm/hugetlb.c            | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

Comments

Oscar Salvador March 5, 2024, 6:58 a.m. UTC | #1
On Fri, Mar 01, 2024 at 09:47:07PM +0000, Matthew Wilcox (Oracle) wrote:
> Returns the hugetlb folio containing this PFN or NULL if the PFN
> is not within a hugetlb folio.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>

Reviewed-by: Oscar Salvador <osalvador@suse.de>
diff mbox series

Patch

diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 77b30a8c6076..c1d23ab53752 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -163,6 +163,7 @@  bool hugetlb_reserve_pages(struct inode *inode, long from, long to,
 long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
 						long freed);
 bool isolate_hugetlb(struct folio *folio, struct list_head *list);
+struct folio *hugetlb_pfn_folio(unsigned long pfn);
 int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison);
 int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
 				bool *migratable_cleared);
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 963c25963b5e..0895debc36f3 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -124,6 +124,33 @@  bool folio_test_hugetlb(const struct folio *folio)
 }
 EXPORT_SYMBOL_GPL(folio_test_hugetlb);
 
+/**
+ * hugetlb_pfn_folio - Convert a pfn to a hugetlb folio.
+ * @pfn: The Page Frame Number.
+ *
+ * If this function returns NULL, the pfn definitely wasn't part of
+ * a hugetlb folio.  If a folio pointer is returned, this pfn was
+ * part of that hugetlb folio at some point.  If the caller holds
+ * the @hugetlb_lock, hugetlb folios may not be returned to the page
+ * allocator, so the folio will still be valid (it may or may not
+ * be free).
+ *
+ * Return: NULL if the pfn is not part of a hugetlb folio.
+ */
+struct folio *hugetlb_pfn_folio(unsigned long pfn)
+{
+	struct page *page = pfn_to_page(pfn);
+	struct folio *folio;
+
+	if (!PageCompound(page))
+		return NULL;
+	folio = page_folio(page);
+	if (folio->large_id != &hugetlb_lock)
+		return NULL;
+
+	return folio;
+}
+
 static inline bool subpool_is_free(struct hugepage_subpool *spool)
 {
 	if (spool->count)