Merge tag 'sparc-for-6.10-tag1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / include / linux / page_ref.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
fe896d18
JK
2#ifndef _LINUX_PAGE_REF_H
3#define _LINUX_PAGE_REF_H
4
5#include <linux/atomic.h>
6#include <linux/mm_types.h>
7#include <linux/page-flags.h>
95813b8f
JK
8#include <linux/tracepoint-defs.h>
9
c65fc227
SRV
10DECLARE_TRACEPOINT(page_ref_set);
11DECLARE_TRACEPOINT(page_ref_mod);
12DECLARE_TRACEPOINT(page_ref_mod_and_test);
13DECLARE_TRACEPOINT(page_ref_mod_and_return);
14DECLARE_TRACEPOINT(page_ref_mod_unless);
15DECLARE_TRACEPOINT(page_ref_freeze);
16DECLARE_TRACEPOINT(page_ref_unfreeze);
95813b8f
JK
17
18#ifdef CONFIG_DEBUG_PAGE_REF
19
20/*
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.
24 *
25 * See trace_##name##_enabled(void) in include/linux/tracepoint.h
26 */
c65fc227 27#define page_ref_tracepoint_active(t) tracepoint_enabled(t)
95813b8f
JK
28
29extern void __page_ref_set(struct page *page, int v);
30extern void __page_ref_mod(struct page *page, int v);
31extern void __page_ref_mod_and_test(struct page *page, int v, int ret);
32extern void __page_ref_mod_and_return(struct page *page, int v, int ret);
33extern void __page_ref_mod_unless(struct page *page, int v, int u);
34extern void __page_ref_freeze(struct page *page, int v, int ret);
35extern void __page_ref_unfreeze(struct page *page, int v);
36
37#else
38
39#define page_ref_tracepoint_active(t) false
40
41static inline void __page_ref_set(struct page *page, int v)
42{
43}
44static inline void __page_ref_mod(struct page *page, int v)
45{
46}
47static inline void __page_ref_mod_and_test(struct page *page, int v, int ret)
48{
49}
50static inline void __page_ref_mod_and_return(struct page *page, int v, int ret)
51{
52}
53static inline void __page_ref_mod_unless(struct page *page, int v, int u)
54{
55}
56static inline void __page_ref_freeze(struct page *page, int v, int ret)
57{
58}
59static inline void __page_ref_unfreeze(struct page *page, int v)
60{
61}
62
63#endif
fe896d18 64
5f7dadf3 65static inline int page_ref_count(const struct page *page)
fe896d18 66{
0139aa7b 67 return atomic_read(&page->_refcount);
fe896d18
JK
68}
69
c24016ac
MWO
70/**
71 * folio_ref_count - The reference count on this folio.
72 * @folio: The folio.
73 *
74 * The refcount is usually incremented by calls to folio_get() and
75 * decremented by calls to folio_put(). Some typical users of the
76 * folio refcount:
77 *
78 * - Each reference from a page table
79 * - The page cache
80 * - Filesystem private data
81 * - The LRU list
82 * - Pipes
83 * - Direct IO which references this page in the process address space
84 *
85 * Return: The number of references to this folio.
86 */
87static inline int folio_ref_count(const struct folio *folio)
88{
89 return page_ref_count(&folio->page);
90}
91
5f7dadf3 92static inline int page_count(const struct page *page)
fe896d18 93{
c24016ac 94 return folio_ref_count(page_folio(page));
fe896d18
JK
95}
96
97static inline void set_page_count(struct page *page, int v)
98{
0139aa7b 99 atomic_set(&page->_refcount, v);
c65fc227 100 if (page_ref_tracepoint_active(page_ref_set))
95813b8f 101 __page_ref_set(page, v);
fe896d18
JK
102}
103
c24016ac
MWO
104static inline void folio_set_count(struct folio *folio, int v)
105{
106 set_page_count(&folio->page, v);
107}
108
fe896d18
JK
109/*
110 * Setup the page count before being freed into the page allocator for
111 * the first time (boot or memory hotplug)
112 */
113static inline void init_page_count(struct page *page)
114{
115 set_page_count(page, 1);
116}
117
118static inline void page_ref_add(struct page *page, int nr)
119{
0139aa7b 120 atomic_add(nr, &page->_refcount);
c65fc227 121 if (page_ref_tracepoint_active(page_ref_mod))
95813b8f 122 __page_ref_mod(page, nr);
fe896d18
JK
123}
124
c24016ac
MWO
125static inline void folio_ref_add(struct folio *folio, int nr)
126{
127 page_ref_add(&folio->page, nr);
128}
129
fe896d18
JK
130static inline void page_ref_sub(struct page *page, int nr)
131{
0139aa7b 132 atomic_sub(nr, &page->_refcount);
c65fc227 133 if (page_ref_tracepoint_active(page_ref_mod))
95813b8f 134 __page_ref_mod(page, -nr);
fe896d18
JK
135}
136
c24016ac
MWO
137static inline void folio_ref_sub(struct folio *folio, int nr)
138{
139 page_ref_sub(&folio->page, nr);
140}
141
498aefbc 142static inline int folio_ref_sub_return(struct folio *folio, int nr)
566d774a 143{
498aefbc 144 int ret = atomic_sub_return(nr, &folio->_refcount);
566d774a 145
c65fc227 146 if (page_ref_tracepoint_active(page_ref_mod_and_return))
498aefbc 147 __page_ref_mod_and_return(&folio->page, -nr, ret);
566d774a
JH
148 return ret;
149}
150
fe896d18
JK
151static inline void page_ref_inc(struct page *page)
152{
0139aa7b 153 atomic_inc(&page->_refcount);
c65fc227 154 if (page_ref_tracepoint_active(page_ref_mod))
95813b8f 155 __page_ref_mod(page, 1);
fe896d18
JK
156}
157
c24016ac
MWO
158static inline void folio_ref_inc(struct folio *folio)
159{
160 page_ref_inc(&folio->page);
161}
162
fe896d18
JK
163static inline void page_ref_dec(struct page *page)
164{
0139aa7b 165 atomic_dec(&page->_refcount);
c65fc227 166 if (page_ref_tracepoint_active(page_ref_mod))
95813b8f 167 __page_ref_mod(page, -1);
fe896d18
JK
168}
169
c24016ac
MWO
170static inline void folio_ref_dec(struct folio *folio)
171{
172 page_ref_dec(&folio->page);
173}
174
fe896d18
JK
175static inline int page_ref_sub_and_test(struct page *page, int nr)
176{
0139aa7b 177 int ret = atomic_sub_and_test(nr, &page->_refcount);
95813b8f 178
c65fc227 179 if (page_ref_tracepoint_active(page_ref_mod_and_test))
95813b8f
JK
180 __page_ref_mod_and_test(page, -nr, ret);
181 return ret;
fe896d18
JK
182}
183
c24016ac
MWO
184static inline int folio_ref_sub_and_test(struct folio *folio, int nr)
185{
186 return page_ref_sub_and_test(&folio->page, nr);
187}
188
df9b2b4a
DH
189static inline int page_ref_inc_return(struct page *page)
190{
191 int ret = atomic_inc_return(&page->_refcount);
192
c65fc227 193 if (page_ref_tracepoint_active(page_ref_mod_and_return))
df9b2b4a
DH
194 __page_ref_mod_and_return(page, 1, ret);
195 return ret;
196}
197
c24016ac
MWO
198static inline int folio_ref_inc_return(struct folio *folio)
199{
200 return page_ref_inc_return(&folio->page);
201}
202
fe896d18
JK
203static inline int page_ref_dec_and_test(struct page *page)
204{
0139aa7b 205 int ret = atomic_dec_and_test(&page->_refcount);
95813b8f 206
c65fc227 207 if (page_ref_tracepoint_active(page_ref_mod_and_test))
95813b8f
JK
208 __page_ref_mod_and_test(page, -1, ret);
209 return ret;
fe896d18
JK
210}
211
c24016ac
MWO
212static inline int folio_ref_dec_and_test(struct folio *folio)
213{
214 return page_ref_dec_and_test(&folio->page);
215}
216
fe896d18
JK
217static inline int page_ref_dec_return(struct page *page)
218{
0139aa7b 219 int ret = atomic_dec_return(&page->_refcount);
95813b8f 220
c65fc227 221 if (page_ref_tracepoint_active(page_ref_mod_and_return))
95813b8f
JK
222 __page_ref_mod_and_return(page, -1, ret);
223 return ret;
fe896d18
JK
224}
225
c24016ac
MWO
226static inline int folio_ref_dec_return(struct folio *folio)
227{
228 return page_ref_dec_return(&folio->page);
229}
230
c2530328 231static inline bool page_ref_add_unless(struct page *page, int nr, int u)
fe896d18 232{
c2530328 233 bool ret = atomic_add_unless(&page->_refcount, nr, u);
95813b8f 234
c65fc227 235 if (page_ref_tracepoint_active(page_ref_mod_unless))
95813b8f
JK
236 __page_ref_mod_unless(page, nr, ret);
237 return ret;
fe896d18
JK
238}
239
c24016ac
MWO
240static inline bool folio_ref_add_unless(struct folio *folio, int nr, int u)
241{
242 return page_ref_add_unless(&folio->page, nr, u);
243}
244
020853b6
MWO
245/**
246 * folio_try_get - Attempt to increase the refcount on a folio.
247 * @folio: The folio.
248 *
249 * If you do not already have a reference to a folio, you can attempt to
250 * get one using this function. It may fail if, for example, the folio
251 * has been freed since you found a pointer to it, or it is frozen for
252 * the purposes of splitting or migration.
253 *
254 * Return: True if the reference count was successfully incremented.
255 */
256static inline bool folio_try_get(struct folio *folio)
257{
258 return folio_ref_add_unless(folio, 1, 0);
259}
260
261static inline bool folio_ref_try_add_rcu(struct folio *folio, int count)
262{
263#ifdef CONFIG_TINY_RCU
264 /*
265 * The caller guarantees the folio will not be freed from interrupt
266 * context, so (on !SMP) we only need preemption to be disabled
267 * and TINY_RCU does that for us.
268 */
269# ifdef CONFIG_PREEMPT_COUNT
270 VM_BUG_ON(!in_atomic() && !irqs_disabled());
271# endif
272 VM_BUG_ON_FOLIO(folio_ref_count(folio) == 0, folio);
273 folio_ref_add(folio, count);
274#else
275 if (unlikely(!folio_ref_add_unless(folio, count, 0))) {
276 /* Either the folio has been freed, or will be freed. */
277 return false;
278 }
279#endif
280 return true;
281}
282
283/**
284 * folio_try_get_rcu - Attempt to increase the refcount on a folio.
285 * @folio: The folio.
286 *
287 * This is a version of folio_try_get() optimised for non-SMP kernels.
288 * If you are still holding the rcu_read_lock() after looking up the
289 * page and know that the page cannot have its refcount decreased to
290 * zero in interrupt context, you can use this instead of folio_try_get().
291 *
292 * Example users include get_user_pages_fast() (as pages are not unmapped
293 * from interrupt context) and the page cache lookups (as pages are not
294 * truncated from interrupt context). We also know that pages are not
295 * frozen in interrupt context for the purposes of splitting or migration.
296 *
297 * You can also use this function if you're holding a lock that prevents
298 * pages being frozen & removed; eg the i_pages lock for the page cache
8651a137 299 * or the mmap_lock or page table lock for page tables. In this case,
020853b6
MWO
300 * it will always succeed, and you could have used a plain folio_get(),
301 * but it's sometimes more convenient to have a common function called
302 * from both locked and RCU-protected contexts.
303 *
304 * Return: True if the reference count was successfully incremented.
305 */
306static inline bool folio_try_get_rcu(struct folio *folio)
307{
308 return folio_ref_try_add_rcu(folio, 1);
309}
310
fe896d18
JK
311static inline int page_ref_freeze(struct page *page, int count)
312{
0139aa7b 313 int ret = likely(atomic_cmpxchg(&page->_refcount, count, 0) == count);
95813b8f 314
c65fc227 315 if (page_ref_tracepoint_active(page_ref_freeze))
95813b8f
JK
316 __page_ref_freeze(page, count, ret);
317 return ret;
fe896d18
JK
318}
319
c24016ac
MWO
320static inline int folio_ref_freeze(struct folio *folio, int count)
321{
322 return page_ref_freeze(&folio->page, count);
323}
324
fe896d18
JK
325static inline void page_ref_unfreeze(struct page *page, int count)
326{
327 VM_BUG_ON_PAGE(page_count(page) != 0, page);
328 VM_BUG_ON(count == 0);
329
03f5d58f 330 atomic_set_release(&page->_refcount, count);
c65fc227 331 if (page_ref_tracepoint_active(page_ref_unfreeze))
95813b8f 332 __page_ref_unfreeze(page, count);
fe896d18
JK
333}
334
c24016ac
MWO
335static inline void folio_ref_unfreeze(struct folio *folio, int count)
336{
337 page_ref_unfreeze(&folio->page, count);
338}
fe896d18 339#endif