2017-11-01 15:07:57 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2016-03-17 14:19:26 -07:00
|
|
|
#ifndef _LINUX_PAGE_REF_H
|
|
|
|
|
#define _LINUX_PAGE_REF_H
|
|
|
|
|
|
|
|
|
|
#include <linux/atomic.h>
|
|
|
|
|
#include <linux/mm_types.h>
|
|
|
|
|
#include <linux/page-flags.h>
|
2016-03-17 14:19:29 -07:00
|
|
|
#include <linux/tracepoint-defs.h>
|
|
|
|
|
|
2020-09-24 12:43:46 -04:00
|
|
|
DECLARE_TRACEPOINT(page_ref_set);
|
|
|
|
|
DECLARE_TRACEPOINT(page_ref_mod);
|
|
|
|
|
DECLARE_TRACEPOINT(page_ref_mod_and_test);
|
|
|
|
|
DECLARE_TRACEPOINT(page_ref_mod_and_return);
|
|
|
|
|
DECLARE_TRACEPOINT(page_ref_mod_unless);
|
|
|
|
|
DECLARE_TRACEPOINT(page_ref_freeze);
|
|
|
|
|
DECLARE_TRACEPOINT(page_ref_unfreeze);
|
2016-03-17 14:19:29 -07:00
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_PAGE_REF
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Ideally we would want to use the trace_<tracepoint>_enabled() helper
|
|
|
|
|
* functions. But due to include header file issues, that is not
|
|
|
|
|
* feasible. Instead we have to open code the static key functions.
|
|
|
|
|
*
|
|
|
|
|
* See trace_##name##_enabled(void) in include/linux/tracepoint.h
|
|
|
|
|
*/
|
2020-09-24 12:43:46 -04:00
|
|
|
#define page_ref_tracepoint_active(t) tracepoint_enabled(t)
|
2016-03-17 14:19:29 -07:00
|
|
|
|
|
|
|
|
extern void __page_ref_set(struct page *page, int v);
|
|
|
|
|
extern void __page_ref_mod(struct page *page, int v);
|
|
|
|
|
extern void __page_ref_mod_and_test(struct page *page, int v, int ret);
|
|
|
|
|
extern void __page_ref_mod_and_return(struct page *page, int v, int ret);
|
|
|
|
|
extern void __page_ref_mod_unless(struct page *page, int v, int u);
|
|
|
|
|
extern void __page_ref_freeze(struct page *page, int v, int ret);
|
|
|
|
|
extern void __page_ref_unfreeze(struct page *page, int v);
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#define page_ref_tracepoint_active(t) false
|
|
|
|
|
|
|
|
|
|
static inline void __page_ref_set(struct page *page, int v)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
static inline void __page_ref_mod(struct page *page, int v)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
static inline void __page_ref_mod_and_test(struct page *page, int v, int ret)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
static inline void __page_ref_mod_and_return(struct page *page, int v, int ret)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
static inline void __page_ref_mod_unless(struct page *page, int v, int u)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
static inline void __page_ref_freeze(struct page *page, int v, int ret)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
static inline void __page_ref_unfreeze(struct page *page, int v)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
2016-03-17 14:19:26 -07:00
|
|
|
|
2021-06-28 19:41:25 -07:00
|
|
|
static inline int page_ref_count(const struct page *page)
|
2016-03-17 14:19:26 -07:00
|
|
|
{
|
2016-05-19 17:10:49 -07:00
|
|
|
return atomic_read(&page->_refcount);
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
/**
|
|
|
|
|
* folio_ref_count - The reference count on this folio.
|
|
|
|
|
* @folio: The folio.
|
|
|
|
|
*
|
2026-05-26 21:00:30 +01:00
|
|
|
* Folios contain a reference count. When that reference count reaches
|
|
|
|
|
* zero, the folio is referred to as frozen. At this point, it will
|
|
|
|
|
* usually be returned to the memory allocator, but some parts of the
|
|
|
|
|
* kernel freeze folios in order to perform unusual operations on them
|
|
|
|
|
* such as splitting or migration.
|
|
|
|
|
*
|
2021-03-31 10:39:55 -04:00
|
|
|
* 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
|
|
|
|
|
*
|
2026-05-26 21:00:30 +01:00
|
|
|
* The reference count has three components: expected, temporary and
|
|
|
|
|
* spurious. The expected reference count of a folio is that which
|
|
|
|
|
* we would logically expect it to be from just reading the code.
|
|
|
|
|
* Temporary refcounts are gained by threads which need a temporary
|
|
|
|
|
* reference to make sure the folio isn't reallocated while they use it.
|
|
|
|
|
* Spurious refcounts are gained by threads which, thanks to RCU walks
|
|
|
|
|
* of the page tables or file cache, find a stale pointer to a folio.
|
|
|
|
|
* These threads will drop the refcount after discoveering the pointer
|
|
|
|
|
* is stale, but it can surprise other users to see the spurious refcount
|
|
|
|
|
* on a freshly allocated folio (eg they may see a refcount of 2 instead
|
|
|
|
|
* of 1).
|
|
|
|
|
*
|
2021-03-31 10:39:55 -04:00
|
|
|
* Return: The number of references to this folio.
|
|
|
|
|
*/
|
|
|
|
|
static inline int folio_ref_count(const struct folio *folio)
|
|
|
|
|
{
|
|
|
|
|
return page_ref_count(&folio->page);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 19:41:25 -07:00
|
|
|
static inline int page_count(const struct page *page)
|
2016-03-17 14:19:26 -07:00
|
|
|
{
|
2021-03-31 10:39:55 -04:00
|
|
|
return folio_ref_count(page_folio(page));
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void set_page_count(struct page *page, int v)
|
|
|
|
|
{
|
2016-05-19 17:10:49 -07:00
|
|
|
atomic_set(&page->_refcount, v);
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_set))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_set(page, v);
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline void folio_set_count(struct folio *folio, int v)
|
|
|
|
|
{
|
|
|
|
|
set_page_count(&folio->page, v);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 14:19:26 -07:00
|
|
|
/*
|
|
|
|
|
* Setup the page count before being freed into the page allocator for
|
|
|
|
|
* the first time (boot or memory hotplug)
|
|
|
|
|
*/
|
|
|
|
|
static inline void init_page_count(struct page *page)
|
|
|
|
|
{
|
|
|
|
|
set_page_count(page, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void page_ref_add(struct page *page, int nr)
|
|
|
|
|
{
|
2016-05-19 17:10:49 -07:00
|
|
|
atomic_add(nr, &page->_refcount);
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_mod))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_mod(page, nr);
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline void folio_ref_add(struct folio *folio, int nr)
|
|
|
|
|
{
|
|
|
|
|
page_ref_add(&folio->page, nr);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 14:19:26 -07:00
|
|
|
static inline void page_ref_sub(struct page *page, int nr)
|
|
|
|
|
{
|
2016-05-19 17:10:49 -07:00
|
|
|
atomic_sub(nr, &page->_refcount);
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_mod))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_mod(page, -nr);
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline void folio_ref_sub(struct folio *folio, int nr)
|
|
|
|
|
{
|
|
|
|
|
page_ref_sub(&folio->page, nr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int folio_ref_sub_return(struct folio *folio, int nr)
|
|
|
|
|
{
|
2024-04-24 20:19:09 +01:00
|
|
|
int ret = atomic_sub_return(nr, &folio->_refcount);
|
|
|
|
|
|
|
|
|
|
if (page_ref_tracepoint_active(page_ref_mod_and_return))
|
|
|
|
|
__page_ref_mod_and_return(&folio->page, -nr, ret);
|
|
|
|
|
return ret;
|
2021-03-31 10:39:55 -04:00
|
|
|
}
|
|
|
|
|
|
2016-03-17 14:19:26 -07:00
|
|
|
static inline void page_ref_inc(struct page *page)
|
|
|
|
|
{
|
2016-05-19 17:10:49 -07:00
|
|
|
atomic_inc(&page->_refcount);
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_mod))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_mod(page, 1);
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline void folio_ref_inc(struct folio *folio)
|
|
|
|
|
{
|
|
|
|
|
page_ref_inc(&folio->page);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 14:19:26 -07:00
|
|
|
static inline void page_ref_dec(struct page *page)
|
|
|
|
|
{
|
2016-05-19 17:10:49 -07:00
|
|
|
atomic_dec(&page->_refcount);
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_mod))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_mod(page, -1);
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline void folio_ref_dec(struct folio *folio)
|
|
|
|
|
{
|
|
|
|
|
page_ref_dec(&folio->page);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 14:19:26 -07:00
|
|
|
static inline int page_ref_sub_and_test(struct page *page, int nr)
|
|
|
|
|
{
|
2016-05-19 17:10:49 -07:00
|
|
|
int ret = atomic_sub_and_test(nr, &page->_refcount);
|
2016-03-17 14:19:29 -07:00
|
|
|
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_mod_and_test))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_mod_and_test(page, -nr, ret);
|
|
|
|
|
return ret;
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline int folio_ref_sub_and_test(struct folio *folio, int nr)
|
|
|
|
|
{
|
|
|
|
|
return page_ref_sub_and_test(&folio->page, nr);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-20 12:09:41 +02:00
|
|
|
static inline int page_ref_inc_return(struct page *page)
|
|
|
|
|
{
|
|
|
|
|
int ret = atomic_inc_return(&page->_refcount);
|
|
|
|
|
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_mod_and_return))
|
2016-06-20 12:09:41 +02:00
|
|
|
__page_ref_mod_and_return(page, 1, ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline int folio_ref_inc_return(struct folio *folio)
|
|
|
|
|
{
|
|
|
|
|
return page_ref_inc_return(&folio->page);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 14:19:26 -07:00
|
|
|
static inline int page_ref_dec_and_test(struct page *page)
|
|
|
|
|
{
|
2016-05-19 17:10:49 -07:00
|
|
|
int ret = atomic_dec_and_test(&page->_refcount);
|
2016-03-17 14:19:29 -07:00
|
|
|
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_mod_and_test))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_mod_and_test(page, -1, ret);
|
|
|
|
|
return ret;
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline int folio_ref_dec_and_test(struct folio *folio)
|
|
|
|
|
{
|
|
|
|
|
return page_ref_dec_and_test(&folio->page);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 14:19:26 -07:00
|
|
|
static inline int page_ref_dec_return(struct page *page)
|
|
|
|
|
{
|
2016-05-19 17:10:49 -07:00
|
|
|
int ret = atomic_dec_return(&page->_refcount);
|
2016-03-17 14:19:29 -07:00
|
|
|
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_mod_and_return))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_mod_and_return(page, -1, ret);
|
|
|
|
|
return ret;
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline int folio_ref_dec_return(struct folio *folio)
|
|
|
|
|
{
|
|
|
|
|
return page_ref_dec_return(&folio->page);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 13:19:39 +00:00
|
|
|
static inline bool page_ref_add_unless_zero(struct page *page, int nr)
|
2016-03-17 14:19:26 -07:00
|
|
|
{
|
2026-03-01 13:19:39 +00:00
|
|
|
bool ret = atomic_add_unless(&page->_refcount, nr, 0);
|
2016-03-17 14:19:29 -07:00
|
|
|
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_mod_unless))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_mod_unless(page, nr, ret);
|
|
|
|
|
return ret;
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-01 13:19:39 +00:00
|
|
|
static inline bool folio_ref_add_unless_zero(struct folio *folio, int nr)
|
2021-03-31 10:39:55 -04:00
|
|
|
{
|
2026-03-01 13:19:39 +00:00
|
|
|
return page_ref_add_unless_zero(&folio->page, nr);
|
2021-03-31 10:39:55 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 16:33:22 -04:00
|
|
|
/**
|
|
|
|
|
* folio_try_get - Attempt to increase the refcount on a folio.
|
|
|
|
|
* @folio: The folio.
|
|
|
|
|
*
|
|
|
|
|
* If you do not already have a reference to a folio, you can attempt to
|
|
|
|
|
* get one using this function. It may fail if, for example, the folio
|
|
|
|
|
* has been freed since you found a pointer to it, or it is frozen for
|
|
|
|
|
* the purposes of splitting or migration.
|
|
|
|
|
*
|
|
|
|
|
* Return: True if the reference count was successfully incremented.
|
|
|
|
|
*/
|
|
|
|
|
static inline bool folio_try_get(struct folio *folio)
|
|
|
|
|
{
|
2026-03-01 13:19:39 +00:00
|
|
|
return folio_ref_add_unless_zero(folio, 1);
|
2021-05-10 16:33:22 -04:00
|
|
|
}
|
|
|
|
|
|
2024-06-25 13:53:50 -07:00
|
|
|
static inline bool folio_ref_try_add(struct folio *folio, int count)
|
2021-05-10 16:33:22 -04:00
|
|
|
{
|
2026-03-01 13:19:39 +00:00
|
|
|
return folio_ref_add_unless_zero(folio, count);
|
2021-05-10 16:33:22 -04:00
|
|
|
}
|
|
|
|
|
|
2016-03-17 14:19:26 -07:00
|
|
|
static inline int page_ref_freeze(struct page *page, int count)
|
|
|
|
|
{
|
2016-05-19 17:10:49 -07:00
|
|
|
int ret = likely(atomic_cmpxchg(&page->_refcount, count, 0) == count);
|
2016-03-17 14:19:29 -07:00
|
|
|
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_freeze))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_freeze(page, count, ret);
|
|
|
|
|
return ret;
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline int folio_ref_freeze(struct folio *folio, int count)
|
|
|
|
|
{
|
|
|
|
|
return page_ref_freeze(&folio->page, count);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 14:19:26 -07:00
|
|
|
static inline void page_ref_unfreeze(struct page *page, int count)
|
|
|
|
|
{
|
|
|
|
|
VM_BUG_ON_PAGE(page_count(page) != 0, page);
|
|
|
|
|
VM_BUG_ON(count == 0);
|
|
|
|
|
|
2018-04-05 16:23:24 -07:00
|
|
|
atomic_set_release(&page->_refcount, count);
|
2020-09-24 12:43:46 -04:00
|
|
|
if (page_ref_tracepoint_active(page_ref_unfreeze))
|
2016-03-17 14:19:29 -07:00
|
|
|
__page_ref_unfreeze(page, count);
|
2016-03-17 14:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:39:55 -04:00
|
|
|
static inline void folio_ref_unfreeze(struct folio *folio, int count)
|
|
|
|
|
{
|
|
|
|
|
page_ref_unfreeze(&folio->page, count);
|
|
|
|
|
}
|
2016-03-17 14:19:26 -07:00
|
|
|
#endif
|