page_ref.h 4.94 KB
Newer Older
1 2 3 4 5 6
#ifndef _LINUX_PAGE_REF_H
#define _LINUX_PAGE_REF_H

#include <linux/atomic.h>
#include <linux/mm_types.h>
#include <linux/page-flags.h>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <linux/tracepoint-defs.h>

extern struct tracepoint __tracepoint_page_ref_set;
extern struct tracepoint __tracepoint_page_ref_mod;
extern struct tracepoint __tracepoint_page_ref_mod_and_test;
extern struct tracepoint __tracepoint_page_ref_mod_and_return;
extern struct tracepoint __tracepoint_page_ref_mod_unless;
extern struct tracepoint __tracepoint_page_ref_freeze;
extern struct tracepoint __tracepoint_page_ref_unfreeze;

#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
 */
#define page_ref_tracepoint_active(t) static_key_false(&(t).key)

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
63 64 65

static inline int page_ref_count(struct page *page)
{
66
	return atomic_read(&page->_refcount);
67 68 69 70
}

static inline int page_count(struct page *page)
{
71
	return atomic_read(&compound_head(page)->_refcount);
72 73 74 75
}

static inline void set_page_count(struct page *page, int v)
{
76
	atomic_set(&page->_refcount, v);
77 78
	if (page_ref_tracepoint_active(__tracepoint_page_ref_set))
		__page_ref_set(page, v);
79 80 81 82 83 84 85 86 87 88 89 90 91
}

/*
 * 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)
{
92
	atomic_add(nr, &page->_refcount);
93 94
	if (page_ref_tracepoint_active(__tracepoint_page_ref_mod))
		__page_ref_mod(page, nr);
95 96 97 98
}

static inline void page_ref_sub(struct page *page, int nr)
{
99
	atomic_sub(nr, &page->_refcount);
100 101
	if (page_ref_tracepoint_active(__tracepoint_page_ref_mod))
		__page_ref_mod(page, -nr);
102 103 104 105
}

static inline void page_ref_inc(struct page *page)
{
106
	atomic_inc(&page->_refcount);
107 108
	if (page_ref_tracepoint_active(__tracepoint_page_ref_mod))
		__page_ref_mod(page, 1);
109 110 111 112
}

static inline void page_ref_dec(struct page *page)
{
113
	atomic_dec(&page->_refcount);
114 115
	if (page_ref_tracepoint_active(__tracepoint_page_ref_mod))
		__page_ref_mod(page, -1);
116 117 118 119
}

static inline int page_ref_sub_and_test(struct page *page, int nr)
{
120
	int ret = atomic_sub_and_test(nr, &page->_refcount);
121 122 123 124

	if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_test))
		__page_ref_mod_and_test(page, -nr, ret);
	return ret;
125 126
}

127 128 129 130 131 132 133 134 135
static inline int page_ref_inc_return(struct page *page)
{
	int ret = atomic_inc_return(&page->_refcount);

	if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_return))
		__page_ref_mod_and_return(page, 1, ret);
	return ret;
}

136 137
static inline int page_ref_dec_and_test(struct page *page)
{
138
	int ret = atomic_dec_and_test(&page->_refcount);
139 140 141 142

	if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_test))
		__page_ref_mod_and_test(page, -1, ret);
	return ret;
143 144 145 146
}

static inline int page_ref_dec_return(struct page *page)
{
147
	int ret = atomic_dec_return(&page->_refcount);
148 149 150 151

	if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_return))
		__page_ref_mod_and_return(page, -1, ret);
	return ret;
152 153 154 155
}

static inline int page_ref_add_unless(struct page *page, int nr, int u)
{
156
	int ret = atomic_add_unless(&page->_refcount, nr, u);
157 158 159 160

	if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_unless))
		__page_ref_mod_unless(page, nr, ret);
	return ret;
161 162 163 164
}

static inline int page_ref_freeze(struct page *page, int count)
{
165
	int ret = likely(atomic_cmpxchg(&page->_refcount, count, 0) == count);
166 167 168 169

	if (page_ref_tracepoint_active(__tracepoint_page_ref_freeze))
		__page_ref_freeze(page, count, ret);
	return ret;
170 171 172 173 174 175 176
}

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);

177
	atomic_set(&page->_refcount, count);
178 179
	if (page_ref_tracepoint_active(__tracepoint_page_ref_unfreeze))
		__page_ref_unfreeze(page, count);
180 181 182
}

#endif