We have trouble keeping track of whether we've already called
compound_head() to ensure we're not operating on a tail page.  Further,
it's never clear whether we intend a struct page to refer to PAGE_SIZE
bytes or page_size(compound_head(page)).

Introduce a new type 'struct folio' that always refers to an entire
(possibly compound) page, and points to the head page (or base page).

Signed-off-by: Matthew Wilcox (Oracle) <wi...@infradead.org>
---
 include/linux/mm.h       |  5 +++++
 include/linux/mm_types.h | 17 +++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index d1f64744ace2..7db9a10f084b 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -916,6 +916,11 @@ static inline unsigned int compound_order(struct page 
*page)
        return page[1].compound_order;
 }
 
+static inline unsigned int folio_order(struct folio *folio)
+{
+       return compound_order(&folio->page);
+}
+
 static inline bool hpage_pincount_available(struct page *page)
 {
        /*
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 65df8abd90bd..d7e487d9998f 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -223,6 +223,23 @@ struct page {
 #endif
 } _struct_page_alignment;
 
+/*
+ * A struct folio is either a base (order-0) page or the head page of
+ * a compound page.
+ */
+struct folio {
+       struct page page;
+};
+
+static inline struct folio *page_folio(struct page *page)
+{
+       unsigned long head = READ_ONCE(page->compound_head);
+
+       if (unlikely(head & 1))
+               return (struct folio *)(head - 1);
+       return (struct folio *)page;
+}
+
 static inline atomic_t *compound_mapcount_ptr(struct page *page)
 {
        return &page[1].compound_mapcount;
-- 
2.29.2

Reply via email to