Skip to contentSkip to conversation

mm/page_alloc: Add __free_pages documentation

PATCH Applied Matthew Wilcox (Oracle) 4 participants 4 messages Oct 27, 2020 – Oct 28, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/page_alloc: Add __free_pages documentation

Provide some guidance towards when this might not be the right interface to use.

---
 mm/page_alloc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
mm/page_alloc.c +20 -0
index 23f5066bd4a5..e613177b8041 100644
@@ -5007,6 +5007,26 @@ static inline void free_the_page(struct page *page, unsigned int order)
 		__free_pages_ok(page, order, FPI_NONE);
 }
 
+/**
+ * __free_pages - Free pages allocated with alloc_pages().
+ * @page: The page pointer returned from alloc_pages().
+ * @order: The order of the allocation.
+ *
+ * This function can free multi-page allocations that are not compound
+ * pages.  It does not check that the @order passed in matches that of
+ * the allocation, so it is easy to leak memory.  Freeing more memory
+ * than was allocated will probably emit a warning.
+ *
+ * If the last reference to this page is speculative, it will be released
+ * by put_page() which only frees the first page of a non-compound
+ * allocation.  To prevent the remaining pages from being leaked, we free
+ * the subsequent pages here.  If you want to use the page's reference
+ * count to decide when to free the allocation, you should allocate a
+ * compound page, and use put_page() instead of __free_pages().
+ *
+ * Context: May be called in interrupt context or while holding a normal
+ * spinlock, but not in NMI context or while holding a raw spinlock.
+ */
 void __free_pages(struct page *page, unsigned int order)
 {
 	if (put_page_testzero(page))
Signature · 1 line
2.28.0
Reviewed-by on v1 1/1
#1 lore raw

I like the explanation.

Quoted 45 lines · Matthew Wilcox (Oracle)

Provide some guidance towards when this might not be the right interface to use.

---
mm/page_alloc.c | 20 ++++++++++++++++++++

1 file changed, 20 insertions(+)
mm/page_alloc.c +20 -0
index 23f5066bd4a5..e613177b8041 100644
@@ -5007,6 +5007,26 @@ static inline void free_the_page(struct page *page, unsigned int order)
 		__free_pages_ok(page, order, FPI_NONE);
 }
 
+/**
+ * __free_pages - Free pages allocated with alloc_pages().
+ * @page: The page pointer returned from alloc_pages().
+ * @order: The order of the allocation.
+ *
+ * This function can free multi-page allocations that are not compound
+ * pages.  It does not check that the @order passed in matches that of
+ * the allocation, so it is easy to leak memory.  Freeing more memory
+ * than was allocated will probably emit a warning.
+ *
+ * If the last reference to this page is speculative, it will be released
+ * by put_page() which only frees the first page of a non-compound
+ * allocation.  To prevent the remaining pages from being leaked, we free
+ * the subsequent pages here.  If you want to use the page's reference
+ * count to decide when to free the allocation, you should allocate a
+ * compound page, and use put_page() instead of __free_pages().
+ *
+ * Context: May be called in interrupt context or while holding a normal
+ * spinlock, but not in NMI context or while holding a raw spinlock.
+ */
 void __free_pages(struct page *page, unsigned int order)
 {
 	if (put_page_testzero(page))
Signature · 1 line
2.28.0
Acked-by on v1 1/1
#2 lore raw
In reply to Matthew Wilcox (Oracle)
Quoted 4 lines · Matthew Wilcox (Oracle)

Provide some guidance towards when this might not be the right interface to use.

Quoted 36 lines
---
  mm/page_alloc.c | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)
mm/page_alloc.c +20 -0
index 23f5066bd4a5..e613177b8041 100644
@@ -5007,6 +5007,26 @@ static inline void free_the_page(struct page *page, unsigned int order)
  		__free_pages_ok(page, order, FPI_NONE);
  }
 
+/**
+ * __free_pages - Free pages allocated with alloc_pages().
+ * @page: The page pointer returned from alloc_pages().
+ * @order: The order of the allocation.
+ *
+ * This function can free multi-page allocations that are not compound
+ * pages.  It does not check that the @order passed in matches that of
+ * the allocation, so it is easy to leak memory.  Freeing more memory
+ * than was allocated will probably emit a warning.
+ *
+ * If the last reference to this page is speculative, it will be released
+ * by put_page() which only frees the first page of a non-compound
+ * allocation.  To prevent the remaining pages from being leaked, we free
+ * the subsequent pages here.  If you want to use the page's reference
+ * count to decide when to free the allocation, you should allocate a
+ * compound page, and use put_page() instead of __free_pages().
+ *
+ * Context: May be called in interrupt context or while holding a normal
+ * spinlock, but not in NMI context or while holding a raw spinlock.
+ */
  void __free_pages(struct page *page, unsigned int order)
  {
  	if (put_page_testzero(page))
Reviewed-by on v1 1/1
#3 lore raw
In reply to Matthew Wilcox (Oracle)
Quoted 4 lines · Matthew Wilcox (Oracle)

Provide some guidance towards when this might not be the right interface to use.

Quoted 39 lines
---
 mm/page_alloc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
mm/page_alloc.c +20 -0
index 23f5066bd4a5..e613177b8041 100644
@@ -5007,6 +5007,26 @@ static inline void free_the_page(struct page *page, unsigned int order)
 		__free_pages_ok(page, order, FPI_NONE);
 }
 
+/**
+ * __free_pages - Free pages allocated with alloc_pages().
+ * @page: The page pointer returned from alloc_pages().
+ * @order: The order of the allocation.
+ *
+ * This function can free multi-page allocations that are not compound
+ * pages.  It does not check that the @order passed in matches that of
+ * the allocation, so it is easy to leak memory.  Freeing more memory
+ * than was allocated will probably emit a warning.
+ *
+ * If the last reference to this page is speculative, it will be released
+ * by put_page() which only frees the first page of a non-compound
+ * allocation.  To prevent the remaining pages from being leaked, we free
+ * the subsequent pages here.  If you want to use the page's reference
+ * count to decide when to free the allocation, you should allocate a
+ * compound page, and use put_page() instead of __free_pages().
+ *
+ * Context: May be called in interrupt context or while holding a normal
+ * spinlock, but not in NMI context or while holding a raw spinlock.
+ */
 void __free_pages(struct page *page, unsigned int order)
 {
 	if (put_page_testzero(page))
Signature · 1 line
2.28.0
Signature · 2 lines
Sincerely yours,
Mike.