mm: Add folio reference count functions

These functions mirror their page reference counterparts.  Also add
the kernel-doc to the mm-api and correct the return type of
page_ref_add_unless() to bool.  No change to generated code.
Signed-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Acked-by: default avatarJeff Layton <jlayton@kernel.org>
Acked-by: default avatarKirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: default avatarVlastimil Babka <vbabka@suse.cz>
Reviewed-by: default avatarWilliam Kucharski <william.kucharski@oracle.com>
Reviewed-by: default avatarDavid Howells <dhowells@redhat.com>
Acked-by: default avatarMike Rapoport <rppt@linux.ibm.com>
parent 9e9edb20
...@@ -98,4 +98,5 @@ More Memory Management Functions ...@@ -98,4 +98,5 @@ More Memory Management Functions
.. kernel-doc:: include/linux/page-flags.h .. kernel-doc:: include/linux/page-flags.h
.. kernel-doc:: include/linux/mm.h .. kernel-doc:: include/linux/mm.h
:internal: :internal:
.. kernel-doc:: include/linux/page_ref.h
.. kernel-doc:: include/linux/mmzone.h .. kernel-doc:: include/linux/mmzone.h
...@@ -67,9 +67,31 @@ static inline int page_ref_count(const struct page *page) ...@@ -67,9 +67,31 @@ static inline int page_ref_count(const struct page *page)
return atomic_read(&page->_refcount); return atomic_read(&page->_refcount);
} }
/**
* folio_ref_count - The reference count on this folio.
* @folio: The folio.
*
* The refcount is usually incremented by calls to folio_get() and
* decremented by calls to folio_put(). Some typical users of the
* folio refcount:
*
* - Each reference from a page table
* - The page cache
* - Filesystem private data
* - The LRU list
* - Pipes
* - Direct IO which references this page in the process address space
*
* Return: The number of references to this folio.
*/
static inline int folio_ref_count(const struct folio *folio)
{
return page_ref_count(&folio->page);
}
static inline int page_count(const struct page *page) static inline int page_count(const struct page *page)
{ {
return atomic_read(&compound_head(page)->_refcount); return folio_ref_count(page_folio(page));
} }
static inline void set_page_count(struct page *page, int v) static inline void set_page_count(struct page *page, int v)
...@@ -79,6 +101,11 @@ static inline void set_page_count(struct page *page, int v) ...@@ -79,6 +101,11 @@ static inline void set_page_count(struct page *page, int v)
__page_ref_set(page, v); __page_ref_set(page, v);
} }
static inline void folio_set_count(struct folio *folio, int v)
{
set_page_count(&folio->page, v);
}
/* /*
* Setup the page count before being freed into the page allocator for * Setup the page count before being freed into the page allocator for
* the first time (boot or memory hotplug) * the first time (boot or memory hotplug)
...@@ -95,6 +122,11 @@ static inline void page_ref_add(struct page *page, int nr) ...@@ -95,6 +122,11 @@ static inline void page_ref_add(struct page *page, int nr)
__page_ref_mod(page, nr); __page_ref_mod(page, nr);
} }
static inline void folio_ref_add(struct folio *folio, int nr)
{
page_ref_add(&folio->page, nr);
}
static inline void page_ref_sub(struct page *page, int nr) static inline void page_ref_sub(struct page *page, int nr)
{ {
atomic_sub(nr, &page->_refcount); atomic_sub(nr, &page->_refcount);
...@@ -102,6 +134,11 @@ static inline void page_ref_sub(struct page *page, int nr) ...@@ -102,6 +134,11 @@ static inline void page_ref_sub(struct page *page, int nr)
__page_ref_mod(page, -nr); __page_ref_mod(page, -nr);
} }
static inline void folio_ref_sub(struct folio *folio, int nr)
{
page_ref_sub(&folio->page, nr);
}
static inline int page_ref_sub_return(struct page *page, int nr) static inline int page_ref_sub_return(struct page *page, int nr)
{ {
int ret = atomic_sub_return(nr, &page->_refcount); int ret = atomic_sub_return(nr, &page->_refcount);
...@@ -111,6 +148,11 @@ static inline int page_ref_sub_return(struct page *page, int nr) ...@@ -111,6 +148,11 @@ static inline int page_ref_sub_return(struct page *page, int nr)
return ret; return ret;
} }
static inline int folio_ref_sub_return(struct folio *folio, int nr)
{
return page_ref_sub_return(&folio->page, nr);
}
static inline void page_ref_inc(struct page *page) static inline void page_ref_inc(struct page *page)
{ {
atomic_inc(&page->_refcount); atomic_inc(&page->_refcount);
...@@ -118,6 +160,11 @@ static inline void page_ref_inc(struct page *page) ...@@ -118,6 +160,11 @@ static inline void page_ref_inc(struct page *page)
__page_ref_mod(page, 1); __page_ref_mod(page, 1);
} }
static inline void folio_ref_inc(struct folio *folio)
{
page_ref_inc(&folio->page);
}
static inline void page_ref_dec(struct page *page) static inline void page_ref_dec(struct page *page)
{ {
atomic_dec(&page->_refcount); atomic_dec(&page->_refcount);
...@@ -125,6 +172,11 @@ static inline void page_ref_dec(struct page *page) ...@@ -125,6 +172,11 @@ static inline void page_ref_dec(struct page *page)
__page_ref_mod(page, -1); __page_ref_mod(page, -1);
} }
static inline void folio_ref_dec(struct folio *folio)
{
page_ref_dec(&folio->page);
}
static inline int page_ref_sub_and_test(struct page *page, int nr) static inline int page_ref_sub_and_test(struct page *page, int nr)
{ {
int ret = atomic_sub_and_test(nr, &page->_refcount); int ret = atomic_sub_and_test(nr, &page->_refcount);
...@@ -134,6 +186,11 @@ static inline int page_ref_sub_and_test(struct page *page, int nr) ...@@ -134,6 +186,11 @@ static inline int page_ref_sub_and_test(struct page *page, int nr)
return ret; return ret;
} }
static inline int folio_ref_sub_and_test(struct folio *folio, int nr)
{
return page_ref_sub_and_test(&folio->page, nr);
}
static inline int page_ref_inc_return(struct page *page) static inline int page_ref_inc_return(struct page *page)
{ {
int ret = atomic_inc_return(&page->_refcount); int ret = atomic_inc_return(&page->_refcount);
...@@ -143,6 +200,11 @@ static inline int page_ref_inc_return(struct page *page) ...@@ -143,6 +200,11 @@ static inline int page_ref_inc_return(struct page *page)
return ret; return ret;
} }
static inline int folio_ref_inc_return(struct folio *folio)
{
return page_ref_inc_return(&folio->page);
}
static inline int page_ref_dec_and_test(struct page *page) static inline int page_ref_dec_and_test(struct page *page)
{ {
int ret = atomic_dec_and_test(&page->_refcount); int ret = atomic_dec_and_test(&page->_refcount);
...@@ -152,6 +214,11 @@ static inline int page_ref_dec_and_test(struct page *page) ...@@ -152,6 +214,11 @@ static inline int page_ref_dec_and_test(struct page *page)
return ret; return ret;
} }
static inline int folio_ref_dec_and_test(struct folio *folio)
{
return page_ref_dec_and_test(&folio->page);
}
static inline int page_ref_dec_return(struct page *page) static inline int page_ref_dec_return(struct page *page)
{ {
int ret = atomic_dec_return(&page->_refcount); int ret = atomic_dec_return(&page->_refcount);
...@@ -161,6 +228,11 @@ static inline int page_ref_dec_return(struct page *page) ...@@ -161,6 +228,11 @@ static inline int page_ref_dec_return(struct page *page)
return ret; return ret;
} }
static inline int folio_ref_dec_return(struct folio *folio)
{
return page_ref_dec_return(&folio->page);
}
static inline bool page_ref_add_unless(struct page *page, int nr, int u) static inline bool page_ref_add_unless(struct page *page, int nr, int u)
{ {
bool ret = atomic_add_unless(&page->_refcount, nr, u); bool ret = atomic_add_unless(&page->_refcount, nr, u);
...@@ -170,6 +242,11 @@ static inline bool page_ref_add_unless(struct page *page, int nr, int u) ...@@ -170,6 +242,11 @@ static inline bool page_ref_add_unless(struct page *page, int nr, int u)
return ret; return ret;
} }
static inline bool folio_ref_add_unless(struct folio *folio, int nr, int u)
{
return page_ref_add_unless(&folio->page, nr, u);
}
static inline int page_ref_freeze(struct page *page, int count) static inline int page_ref_freeze(struct page *page, int count)
{ {
int ret = likely(atomic_cmpxchg(&page->_refcount, count, 0) == count); int ret = likely(atomic_cmpxchg(&page->_refcount, count, 0) == count);
...@@ -179,6 +256,11 @@ static inline int page_ref_freeze(struct page *page, int count) ...@@ -179,6 +256,11 @@ static inline int page_ref_freeze(struct page *page, int count)
return ret; return ret;
} }
static inline int folio_ref_freeze(struct folio *folio, int count)
{
return page_ref_freeze(&folio->page, count);
}
static inline void page_ref_unfreeze(struct page *page, int count) static inline void page_ref_unfreeze(struct page *page, int count)
{ {
VM_BUG_ON_PAGE(page_count(page) != 0, page); VM_BUG_ON_PAGE(page_count(page) != 0, page);
...@@ -189,4 +271,8 @@ static inline void page_ref_unfreeze(struct page *page, int count) ...@@ -189,4 +271,8 @@ static inline void page_ref_unfreeze(struct page *page, int count)
__page_ref_unfreeze(page, count); __page_ref_unfreeze(page, count);
} }
static inline void folio_ref_unfreeze(struct folio *folio, int count)
{
page_ref_unfreeze(&folio->page, count);
}
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment