mm: Add folio_get()
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Mon, 7 Dec 2020 04:04:57 +0000 (23:04 -0500)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Mon, 27 Sep 2021 13:27:29 +0000 (09:27 -0400)
If we know we have a folio, we can call folio_get() instead
of get_page() and save the overhead of calling compound_head().
No change to generated code.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Zi Yan <ziy@nvidia.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Acked-by: Jeff Layton <jlayton@kernel.org>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: William Kucharski <william.kucharski@oracle.com>
Reviewed-by: David Howells <dhowells@redhat.com>
Acked-by: Mike Rapoport <rppt@linux.ibm.com>
include/linux/mm.h

index 28a84d8..63685d9 100644 (file)
@@ -1224,18 +1224,26 @@ static inline bool is_pci_p2pdma_page(const struct page *page)
 }
 
 /* 127: arbitrary random number, small enough to assemble well */
-#define page_ref_zero_or_close_to_overflow(page) \
-       ((unsigned int) page_ref_count(page) + 127u <= 127u)
+#define folio_ref_zero_or_close_to_overflow(folio) \
+       ((unsigned int) folio_ref_count(folio) + 127u <= 127u)
+
+/**
+ * folio_get - Increment the reference count on a folio.
+ * @folio: The folio.
+ *
+ * Context: May be called in any context, as long as you know that
+ * you have a refcount on the folio.  If you do not already have one,
+ * folio_try_get() may be the right interface for you to use.
+ */
+static inline void folio_get(struct folio *folio)
+{
+       VM_BUG_ON_FOLIO(folio_ref_zero_or_close_to_overflow(folio), folio);
+       folio_ref_inc(folio);
+}
 
 static inline void get_page(struct page *page)
 {
-       page = compound_head(page);
-       /*
-        * Getting a normal page or the head of a compound page
-        * requires to already have an elevated page->_refcount.
-        */
-       VM_BUG_ON_PAGE(page_ref_zero_or_close_to_overflow(page), page);
-       page_ref_inc(page);
+       folio_get(page_folio(page));
 }
 
 bool __must_check try_grab_page(struct page *page, unsigned int flags);