1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PAGE_REF_H
3 #define _LINUX_PAGE_REF_H
5 #include <linux/atomic.h>
6 #include <linux/mm_types.h>
7 #include <linux/page-flags.h>
8 #include <linux/tracepoint-defs.h>
10 extern struct tracepoint __tracepoint_page_ref_set
;
11 extern struct tracepoint __tracepoint_page_ref_mod
;
12 extern struct tracepoint __tracepoint_page_ref_mod_and_test
;
13 extern struct tracepoint __tracepoint_page_ref_mod_and_return
;
14 extern struct tracepoint __tracepoint_page_ref_mod_unless
;
15 extern struct tracepoint __tracepoint_page_ref_freeze
;
16 extern struct tracepoint __tracepoint_page_ref_unfreeze
;
18 #ifdef CONFIG_DEBUG_PAGE_REF
21 * Ideally we would want to use the trace_<tracepoint>_enabled() helper
22 * functions. But due to include header file issues, that is not
23 * feasible. Instead we have to open code the static key functions.
25 * See trace_##name##_enabled(void) in include/linux/tracepoint.h
27 #define page_ref_tracepoint_active(t) static_key_false(&(t).key)
29 extern void __page_ref_set(struct page
*page
, int v
);
30 extern void __page_ref_mod(struct page
*page
, int v
);
31 extern void __page_ref_mod_and_test(struct page
*page
, int v
, int ret
);
32 extern void __page_ref_mod_and_return(struct page
*page
, int v
, int ret
);
33 extern void __page_ref_mod_unless(struct page
*page
, int v
, int u
);
34 extern void __page_ref_freeze(struct page
*page
, int v
, int ret
);
35 extern void __page_ref_unfreeze(struct page
*page
, int v
);
39 #define page_ref_tracepoint_active(t) false
41 static inline void __page_ref_set(struct page
*page
, int v
)
44 static inline void __page_ref_mod(struct page
*page
, int v
)
47 static inline void __page_ref_mod_and_test(struct page
*page
, int v
, int ret
)
50 static inline void __page_ref_mod_and_return(struct page
*page
, int v
, int ret
)
53 static inline void __page_ref_mod_unless(struct page
*page
, int v
, int u
)
56 static inline void __page_ref_freeze(struct page
*page
, int v
, int ret
)
59 static inline void __page_ref_unfreeze(struct page
*page
, int v
)
65 static inline int page_ref_count(struct page
*page
)
67 return atomic_read(&page
->_refcount
);
70 static inline int page_count(struct page
*page
)
72 return atomic_read(&compound_head(page
)->_refcount
);
75 static inline void set_page_count(struct page
*page
, int v
)
77 atomic_set(&page
->_refcount
, v
);
78 if (page_ref_tracepoint_active(__tracepoint_page_ref_set
))
79 __page_ref_set(page
, v
);
83 * Setup the page count before being freed into the page allocator for
84 * the first time (boot or memory hotplug)
86 static inline void init_page_count(struct page
*page
)
88 set_page_count(page
, 1);
91 static inline void page_ref_add(struct page
*page
, int nr
)
93 atomic_add(nr
, &page
->_refcount
);
94 if (page_ref_tracepoint_active(__tracepoint_page_ref_mod
))
95 __page_ref_mod(page
, nr
);
98 static inline void page_ref_sub(struct page
*page
, int nr
)
100 atomic_sub(nr
, &page
->_refcount
);
101 if (page_ref_tracepoint_active(__tracepoint_page_ref_mod
))
102 __page_ref_mod(page
, -nr
);
105 static inline void page_ref_inc(struct page
*page
)
107 atomic_inc(&page
->_refcount
);
108 if (page_ref_tracepoint_active(__tracepoint_page_ref_mod
))
109 __page_ref_mod(page
, 1);
112 static inline void page_ref_dec(struct page
*page
)
114 atomic_dec(&page
->_refcount
);
115 if (page_ref_tracepoint_active(__tracepoint_page_ref_mod
))
116 __page_ref_mod(page
, -1);
119 static inline int page_ref_sub_and_test(struct page
*page
, int nr
)
121 int ret
= atomic_sub_and_test(nr
, &page
->_refcount
);
123 if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_test
))
124 __page_ref_mod_and_test(page
, -nr
, ret
);
128 static inline int page_ref_inc_return(struct page
*page
)
130 int ret
= atomic_inc_return(&page
->_refcount
);
132 if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_return
))
133 __page_ref_mod_and_return(page
, 1, ret
);
137 static inline int page_ref_dec_and_test(struct page
*page
)
139 int ret
= atomic_dec_and_test(&page
->_refcount
);
141 if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_test
))
142 __page_ref_mod_and_test(page
, -1, ret
);
146 static inline int page_ref_dec_return(struct page
*page
)
148 int ret
= atomic_dec_return(&page
->_refcount
);
150 if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_return
))
151 __page_ref_mod_and_return(page
, -1, ret
);
155 static inline int page_ref_add_unless(struct page
*page
, int nr
, int u
)
157 int ret
= atomic_add_unless(&page
->_refcount
, nr
, u
);
159 if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_unless
))
160 __page_ref_mod_unless(page
, nr
, ret
);
164 static inline int page_ref_freeze(struct page
*page
, int count
)
166 int ret
= likely(atomic_cmpxchg(&page
->_refcount
, count
, 0) == count
);
168 if (page_ref_tracepoint_active(__tracepoint_page_ref_freeze
))
169 __page_ref_freeze(page
, count
, ret
);
173 static inline void page_ref_unfreeze(struct page
*page
, int count
)
175 VM_BUG_ON_PAGE(page_count(page
) != 0, page
);
176 VM_BUG_ON(count
== 0);
179 atomic_set(&page
->_refcount
, count
);
180 if (page_ref_tracepoint_active(__tracepoint_page_ref_unfreeze
))
181 __page_ref_unfreeze(page
, count
);