xref: /linux-6.15/include/linux/page-flags.h (revision 9cb75552)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Macros for manipulating and testing page->flags
4  */
5 
6 #ifndef PAGE_FLAGS_H
7 #define PAGE_FLAGS_H
8 
9 #include <linux/types.h>
10 #include <linux/bug.h>
11 #include <linux/mmdebug.h>
12 #ifndef __GENERATING_BOUNDS_H
13 #include <linux/mm_types.h>
14 #include <generated/bounds.h>
15 #endif /* !__GENERATING_BOUNDS_H */
16 
17 /*
18  * Various page->flags bits:
19  *
20  * PG_reserved is set for special pages. The "struct page" of such a page
21  * should in general not be touched (e.g. set dirty) except by its owner.
22  * Pages marked as PG_reserved include:
23  * - Pages part of the kernel image (including vDSO) and similar (e.g. BIOS,
24  *   initrd, HW tables)
25  * - Pages reserved or allocated early during boot (before the page allocator
26  *   was initialized). This includes (depending on the architecture) the
27  *   initial vmemmap, initial page tables, crashkernel, elfcorehdr, and much
28  *   much more. Once (if ever) freed, PG_reserved is cleared and they will
29  *   be given to the page allocator.
30  * - Pages falling into physical memory gaps - not IORESOURCE_SYSRAM. Trying
31  *   to read/write these pages might end badly. Don't touch!
32  * - The zero page(s)
33  * - Pages allocated in the context of kexec/kdump (loaded kernel image,
34  *   control pages, vmcoreinfo)
35  * - MMIO/DMA pages. Some architectures don't allow to ioremap pages that are
36  *   not marked PG_reserved (as they might be in use by somebody else who does
37  *   not respect the caching strategy).
38  * - MCA pages on ia64
39  * - Pages holding CPU notes for POWER Firmware Assisted Dump
40  * - Device memory (e.g. PMEM, DAX, HMM)
41  * Some PG_reserved pages will be excluded from the hibernation image.
42  * PG_reserved does in general not hinder anybody from dumping or swapping
43  * and is no longer required for remap_pfn_range(). ioremap might require it.
44  * Consequently, PG_reserved for a page mapped into user space can indicate
45  * the zero page, the vDSO, MMIO pages or device memory.
46  *
47  * The PG_private bitflag is set on pagecache pages if they contain filesystem
48  * specific data (which is normally at page->private). It can be used by
49  * private allocations for its own usage.
50  *
51  * During initiation of disk I/O, PG_locked is set. This bit is set before I/O
52  * and cleared when writeback _starts_ or when read _completes_. PG_writeback
53  * is set before writeback starts and cleared when it finishes.
54  *
55  * PG_locked also pins a page in pagecache, and blocks truncation of the file
56  * while it is held.
57  *
58  * page_waitqueue(page) is a wait queue of all tasks waiting for the page
59  * to become unlocked.
60  *
61  * PG_swapbacked is set when a page uses swap as a backing storage.  This are
62  * usually PageAnon or shmem pages but please note that even anonymous pages
63  * might lose their PG_swapbacked flag when they simply can be dropped (e.g. as
64  * a result of MADV_FREE).
65  *
66  * PG_referenced, PG_reclaim are used for page reclaim for anonymous and
67  * file-backed pagecache (see mm/vmscan.c).
68  *
69  * PG_arch_1 is an architecture specific page state bit.  The generic code
70  * guarantees that this bit is cleared for a page when it first is entered into
71  * the page cache.
72  *
73  * PG_hwpoison indicates that a page got corrupted in hardware and contains
74  * data with incorrect ECC bits that triggered a machine check. Accessing is
75  * not safe since it may cause another machine check. Don't touch!
76  */
77 
78 /*
79  * Don't use the pageflags directly.  Use the PageFoo macros.
80  *
81  * The page flags field is split into two parts, the main flags area
82  * which extends from the low bits upwards, and the fields area which
83  * extends from the high bits downwards.
84  *
85  *  | FIELD | ... | FLAGS |
86  *  N-1           ^       0
87  *               (NR_PAGEFLAGS)
88  *
89  * The fields area is reserved for fields mapping zone, node (for NUMA) and
90  * SPARSEMEM section (for variants of SPARSEMEM that require section ids like
91  * SPARSEMEM_EXTREME with !SPARSEMEM_VMEMMAP).
92  */
93 enum pageflags {
94 	PG_locked,		/* Page is locked. Don't touch. */
95 	PG_writeback,		/* Page is under writeback */
96 	PG_referenced,
97 	PG_uptodate,
98 	PG_dirty,
99 	PG_lru,
100 	PG_head,		/* Must be in bit 6 */
101 	PG_waiters,		/* Page has waiters, check its waitqueue. Must be bit #7 and in the same byte as "PG_locked" */
102 	PG_active,
103 	PG_workingset,
104 	PG_owner_priv_1,	/* Owner use. If pagecache, fs may use */
105 	PG_owner_2,		/* Owner use. If pagecache, fs may use */
106 	PG_arch_1,
107 	PG_reserved,
108 	PG_private,		/* If pagecache, has fs-private data */
109 	PG_private_2,		/* If pagecache, has fs aux data */
110 	PG_reclaim,		/* To be reclaimed asap */
111 	PG_swapbacked,		/* Page is backed by RAM/swap */
112 	PG_unevictable,		/* Page is "unevictable"  */
113 #ifdef CONFIG_MMU
114 	PG_mlocked,		/* Page is vma mlocked */
115 #endif
116 #ifdef CONFIG_MEMORY_FAILURE
117 	PG_hwpoison,		/* hardware poisoned page. Don't touch */
118 #endif
119 #if defined(CONFIG_PAGE_IDLE_FLAG) && defined(CONFIG_64BIT)
120 	PG_young,
121 	PG_idle,
122 #endif
123 #ifdef CONFIG_ARCH_USES_PG_ARCH_2
124 	PG_arch_2,
125 #endif
126 #ifdef CONFIG_ARCH_USES_PG_ARCH_3
127 	PG_arch_3,
128 #endif
129 	__NR_PAGEFLAGS,
130 
131 	PG_readahead = PG_reclaim,
132 
133 	/* Anonymous memory (and shmem) */
134 	PG_swapcache = PG_owner_priv_1, /* Swap page: swp_entry_t in private */
135 	/* Some filesystems */
136 	PG_checked = PG_owner_priv_1,
137 
138 	/*
139 	 * Depending on the way an anonymous folio can be mapped into a page
140 	 * table (e.g., single PMD/PUD/CONT of the head page vs. PTE-mapped
141 	 * THP), PG_anon_exclusive may be set only for the head page or for
142 	 * tail pages of an anonymous folio. For now, we only expect it to be
143 	 * set on tail pages for PTE-mapped THP.
144 	 */
145 	PG_anon_exclusive = PG_owner_2,
146 
147 	/*
148 	 * Set if all buffer heads in the folio are mapped.
149 	 * Filesystems which do not use BHs can use it for their own purpose.
150 	 */
151 	PG_mappedtodisk = PG_owner_2,
152 
153 	/* Two page bits are conscripted by FS-Cache to maintain local caching
154 	 * state.  These bits are set on pages belonging to the netfs's inodes
155 	 * when those inodes are being locally cached.
156 	 */
157 	PG_fscache = PG_private_2,	/* page backed by cache */
158 
159 	/* XEN */
160 	/* Pinned in Xen as a read-only pagetable page. */
161 	PG_pinned = PG_owner_priv_1,
162 	/* Pinned as part of domain save (see xen_mm_pin_all()). */
163 	PG_savepinned = PG_dirty,
164 	/* Has a grant mapping of another (foreign) domain's page. */
165 	PG_foreign = PG_owner_priv_1,
166 	/* Remapped by swiotlb-xen. */
167 	PG_xen_remapped = PG_owner_priv_1,
168 
169 	/* non-lru isolated movable page */
170 	PG_isolated = PG_reclaim,
171 
172 	/* Only valid for buddy pages. Used to track pages that are reported */
173 	PG_reported = PG_uptodate,
174 
175 #ifdef CONFIG_MEMORY_HOTPLUG
176 	/* For self-hosted memmap pages */
177 	PG_vmemmap_self_hosted = PG_owner_priv_1,
178 #endif
179 
180 	/*
181 	 * Flags only valid for compound pages.  Stored in first tail page's
182 	 * flags word.  Cannot use the first 8 flags or any flag marked as
183 	 * PF_ANY.
184 	 */
185 
186 	/* At least one page in this folio has the hwpoison flag set */
187 	PG_has_hwpoisoned = PG_active,
188 	PG_large_rmappable = PG_workingset, /* anon or file-backed */
189 };
190 
191 #define PAGEFLAGS_MASK		((1UL << NR_PAGEFLAGS) - 1)
192 
193 #ifndef __GENERATING_BOUNDS_H
194 
195 #ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
196 DECLARE_STATIC_KEY_FALSE(hugetlb_optimize_vmemmap_key);
197 
198 /*
199  * Return the real head page struct iff the @page is a fake head page, otherwise
200  * return the @page itself. See Documentation/mm/vmemmap_dedup.rst.
201  */
202 static __always_inline const struct page *page_fixed_fake_head(const struct page *page)
203 {
204 	if (!static_branch_unlikely(&hugetlb_optimize_vmemmap_key))
205 		return page;
206 
207 	/*
208 	 * Only addresses aligned with PAGE_SIZE of struct page may be fake head
209 	 * struct page. The alignment check aims to avoid access the fields (
210 	 * e.g. compound_head) of the @page[1]. It can avoid touch a (possibly)
211 	 * cold cacheline in some cases.
212 	 */
213 	if (IS_ALIGNED((unsigned long)page, PAGE_SIZE) &&
214 	    test_bit(PG_head, &page->flags)) {
215 		/*
216 		 * We can safely access the field of the @page[1] with PG_head
217 		 * because the @page is a compound page composed with at least
218 		 * two contiguous pages.
219 		 */
220 		unsigned long head = READ_ONCE(page[1].compound_head);
221 
222 		if (likely(head & 1))
223 			return (const struct page *)(head - 1);
224 	}
225 	return page;
226 }
227 #else
228 static inline const struct page *page_fixed_fake_head(const struct page *page)
229 {
230 	return page;
231 }
232 #endif
233 
234 static __always_inline int page_is_fake_head(const struct page *page)
235 {
236 	return page_fixed_fake_head(page) != page;
237 }
238 
239 static __always_inline unsigned long _compound_head(const struct page *page)
240 {
241 	unsigned long head = READ_ONCE(page->compound_head);
242 
243 	if (unlikely(head & 1))
244 		return head - 1;
245 	return (unsigned long)page_fixed_fake_head(page);
246 }
247 
248 #define compound_head(page)	((typeof(page))_compound_head(page))
249 
250 /**
251  * page_folio - Converts from page to folio.
252  * @p: The page.
253  *
254  * Every page is part of a folio.  This function cannot be called on a
255  * NULL pointer.
256  *
257  * Context: No reference, nor lock is required on @page.  If the caller
258  * does not hold a reference, this call may race with a folio split, so
259  * it should re-check the folio still contains this page after gaining
260  * a reference on the folio.
261  * Return: The folio which contains this page.
262  */
263 #define page_folio(p)		(_Generic((p),				\
264 	const struct page *:	(const struct folio *)_compound_head(p), \
265 	struct page *:		(struct folio *)_compound_head(p)))
266 
267 /**
268  * folio_page - Return a page from a folio.
269  * @folio: The folio.
270  * @n: The page number to return.
271  *
272  * @n is relative to the start of the folio.  This function does not
273  * check that the page number lies within @folio; the caller is presumed
274  * to have a reference to the page.
275  */
276 #define folio_page(folio, n)	nth_page(&(folio)->page, n)
277 
278 static __always_inline int PageTail(const struct page *page)
279 {
280 	return READ_ONCE(page->compound_head) & 1 || page_is_fake_head(page);
281 }
282 
283 static __always_inline int PageCompound(const struct page *page)
284 {
285 	return test_bit(PG_head, &page->flags) ||
286 	       READ_ONCE(page->compound_head) & 1;
287 }
288 
289 #define	PAGE_POISON_PATTERN	-1l
290 static inline int PagePoisoned(const struct page *page)
291 {
292 	return READ_ONCE(page->flags) == PAGE_POISON_PATTERN;
293 }
294 
295 #ifdef CONFIG_DEBUG_VM
296 void page_init_poison(struct page *page, size_t size);
297 #else
298 static inline void page_init_poison(struct page *page, size_t size)
299 {
300 }
301 #endif
302 
303 static const unsigned long *const_folio_flags(const struct folio *folio,
304 		unsigned n)
305 {
306 	const struct page *page = &folio->page;
307 
308 	VM_BUG_ON_PGFLAGS(PageTail(page), page);
309 	VM_BUG_ON_PGFLAGS(n > 0 && !test_bit(PG_head, &page->flags), page);
310 	return &page[n].flags;
311 }
312 
313 static unsigned long *folio_flags(struct folio *folio, unsigned n)
314 {
315 	struct page *page = &folio->page;
316 
317 	VM_BUG_ON_PGFLAGS(PageTail(page), page);
318 	VM_BUG_ON_PGFLAGS(n > 0 && !test_bit(PG_head, &page->flags), page);
319 	return &page[n].flags;
320 }
321 
322 /*
323  * Page flags policies wrt compound pages
324  *
325  * PF_POISONED_CHECK
326  *     check if this struct page poisoned/uninitialized
327  *
328  * PF_ANY:
329  *     the page flag is relevant for small, head and tail pages.
330  *
331  * PF_HEAD:
332  *     for compound page all operations related to the page flag applied to
333  *     head page.
334  *
335  * PF_NO_TAIL:
336  *     modifications of the page flag must be done on small or head pages,
337  *     checks can be done on tail pages too.
338  *
339  * PF_NO_COMPOUND:
340  *     the page flag is not relevant for compound pages.
341  *
342  * PF_SECOND:
343  *     the page flag is stored in the first tail page.
344  */
345 #define PF_POISONED_CHECK(page) ({					\
346 		VM_BUG_ON_PGFLAGS(PagePoisoned(page), page);		\
347 		page; })
348 #define PF_ANY(page, enforce)	PF_POISONED_CHECK(page)
349 #define PF_HEAD(page, enforce)	PF_POISONED_CHECK(compound_head(page))
350 #define PF_NO_TAIL(page, enforce) ({					\
351 		VM_BUG_ON_PGFLAGS(enforce && PageTail(page), page);	\
352 		PF_POISONED_CHECK(compound_head(page)); })
353 #define PF_NO_COMPOUND(page, enforce) ({				\
354 		VM_BUG_ON_PGFLAGS(enforce && PageCompound(page), page);	\
355 		PF_POISONED_CHECK(page); })
356 #define PF_SECOND(page, enforce) ({					\
357 		VM_BUG_ON_PGFLAGS(!PageHead(page), page);		\
358 		PF_POISONED_CHECK(&page[1]); })
359 
360 /* Which page is the flag stored in */
361 #define FOLIO_PF_ANY		0
362 #define FOLIO_PF_HEAD		0
363 #define FOLIO_PF_NO_TAIL	0
364 #define FOLIO_PF_NO_COMPOUND	0
365 #define FOLIO_PF_SECOND		1
366 
367 #define FOLIO_HEAD_PAGE		0
368 #define FOLIO_SECOND_PAGE	1
369 
370 /*
371  * Macros to create function definitions for page flags
372  */
373 #define FOLIO_TEST_FLAG(name, page)					\
374 static __always_inline bool folio_test_##name(const struct folio *folio) \
375 { return test_bit(PG_##name, const_folio_flags(folio, page)); }
376 
377 #define FOLIO_SET_FLAG(name, page)					\
378 static __always_inline void folio_set_##name(struct folio *folio)	\
379 { set_bit(PG_##name, folio_flags(folio, page)); }
380 
381 #define FOLIO_CLEAR_FLAG(name, page)					\
382 static __always_inline void folio_clear_##name(struct folio *folio)	\
383 { clear_bit(PG_##name, folio_flags(folio, page)); }
384 
385 #define __FOLIO_SET_FLAG(name, page)					\
386 static __always_inline void __folio_set_##name(struct folio *folio)	\
387 { __set_bit(PG_##name, folio_flags(folio, page)); }
388 
389 #define __FOLIO_CLEAR_FLAG(name, page)					\
390 static __always_inline void __folio_clear_##name(struct folio *folio)	\
391 { __clear_bit(PG_##name, folio_flags(folio, page)); }
392 
393 #define FOLIO_TEST_SET_FLAG(name, page)					\
394 static __always_inline bool folio_test_set_##name(struct folio *folio)	\
395 { return test_and_set_bit(PG_##name, folio_flags(folio, page)); }
396 
397 #define FOLIO_TEST_CLEAR_FLAG(name, page)				\
398 static __always_inline bool folio_test_clear_##name(struct folio *folio) \
399 { return test_and_clear_bit(PG_##name, folio_flags(folio, page)); }
400 
401 #define FOLIO_FLAG(name, page)						\
402 FOLIO_TEST_FLAG(name, page)						\
403 FOLIO_SET_FLAG(name, page)						\
404 FOLIO_CLEAR_FLAG(name, page)
405 
406 #define TESTPAGEFLAG(uname, lname, policy)				\
407 FOLIO_TEST_FLAG(lname, FOLIO_##policy)					\
408 static __always_inline int Page##uname(const struct page *page)		\
409 { return test_bit(PG_##lname, &policy(page, 0)->flags); }
410 
411 #define SETPAGEFLAG(uname, lname, policy)				\
412 FOLIO_SET_FLAG(lname, FOLIO_##policy)					\
413 static __always_inline void SetPage##uname(struct page *page)		\
414 { set_bit(PG_##lname, &policy(page, 1)->flags); }
415 
416 #define CLEARPAGEFLAG(uname, lname, policy)				\
417 FOLIO_CLEAR_FLAG(lname, FOLIO_##policy)					\
418 static __always_inline void ClearPage##uname(struct page *page)		\
419 { clear_bit(PG_##lname, &policy(page, 1)->flags); }
420 
421 #define __SETPAGEFLAG(uname, lname, policy)				\
422 __FOLIO_SET_FLAG(lname, FOLIO_##policy)					\
423 static __always_inline void __SetPage##uname(struct page *page)		\
424 { __set_bit(PG_##lname, &policy(page, 1)->flags); }
425 
426 #define __CLEARPAGEFLAG(uname, lname, policy)				\
427 __FOLIO_CLEAR_FLAG(lname, FOLIO_##policy)				\
428 static __always_inline void __ClearPage##uname(struct page *page)	\
429 { __clear_bit(PG_##lname, &policy(page, 1)->flags); }
430 
431 #define TESTSETFLAG(uname, lname, policy)				\
432 FOLIO_TEST_SET_FLAG(lname, FOLIO_##policy)				\
433 static __always_inline int TestSetPage##uname(struct page *page)	\
434 { return test_and_set_bit(PG_##lname, &policy(page, 1)->flags); }
435 
436 #define TESTCLEARFLAG(uname, lname, policy)				\
437 FOLIO_TEST_CLEAR_FLAG(lname, FOLIO_##policy)				\
438 static __always_inline int TestClearPage##uname(struct page *page)	\
439 { return test_and_clear_bit(PG_##lname, &policy(page, 1)->flags); }
440 
441 #define PAGEFLAG(uname, lname, policy)					\
442 	TESTPAGEFLAG(uname, lname, policy)				\
443 	SETPAGEFLAG(uname, lname, policy)				\
444 	CLEARPAGEFLAG(uname, lname, policy)
445 
446 #define __PAGEFLAG(uname, lname, policy)				\
447 	TESTPAGEFLAG(uname, lname, policy)				\
448 	__SETPAGEFLAG(uname, lname, policy)				\
449 	__CLEARPAGEFLAG(uname, lname, policy)
450 
451 #define TESTSCFLAG(uname, lname, policy)				\
452 	TESTSETFLAG(uname, lname, policy)				\
453 	TESTCLEARFLAG(uname, lname, policy)
454 
455 #define FOLIO_TEST_FLAG_FALSE(name)					\
456 static inline bool folio_test_##name(const struct folio *folio)		\
457 { return false; }
458 #define FOLIO_SET_FLAG_NOOP(name)					\
459 static inline void folio_set_##name(struct folio *folio) { }
460 #define FOLIO_CLEAR_FLAG_NOOP(name)					\
461 static inline void folio_clear_##name(struct folio *folio) { }
462 #define __FOLIO_SET_FLAG_NOOP(name)					\
463 static inline void __folio_set_##name(struct folio *folio) { }
464 #define __FOLIO_CLEAR_FLAG_NOOP(name)					\
465 static inline void __folio_clear_##name(struct folio *folio) { }
466 #define FOLIO_TEST_SET_FLAG_FALSE(name)					\
467 static inline bool folio_test_set_##name(struct folio *folio)		\
468 { return false; }
469 #define FOLIO_TEST_CLEAR_FLAG_FALSE(name)				\
470 static inline bool folio_test_clear_##name(struct folio *folio)		\
471 { return false; }
472 
473 #define FOLIO_FLAG_FALSE(name)						\
474 FOLIO_TEST_FLAG_FALSE(name)						\
475 FOLIO_SET_FLAG_NOOP(name)						\
476 FOLIO_CLEAR_FLAG_NOOP(name)
477 
478 #define TESTPAGEFLAG_FALSE(uname, lname)				\
479 FOLIO_TEST_FLAG_FALSE(lname)						\
480 static inline int Page##uname(const struct page *page) { return 0; }
481 
482 #define SETPAGEFLAG_NOOP(uname, lname)					\
483 FOLIO_SET_FLAG_NOOP(lname)						\
484 static inline void SetPage##uname(struct page *page) {  }
485 
486 #define CLEARPAGEFLAG_NOOP(uname, lname)				\
487 FOLIO_CLEAR_FLAG_NOOP(lname)						\
488 static inline void ClearPage##uname(struct page *page) {  }
489 
490 #define __CLEARPAGEFLAG_NOOP(uname, lname)				\
491 __FOLIO_CLEAR_FLAG_NOOP(lname)						\
492 static inline void __ClearPage##uname(struct page *page) {  }
493 
494 #define TESTSETFLAG_FALSE(uname, lname)					\
495 FOLIO_TEST_SET_FLAG_FALSE(lname)					\
496 static inline int TestSetPage##uname(struct page *page) { return 0; }
497 
498 #define TESTCLEARFLAG_FALSE(uname, lname)				\
499 FOLIO_TEST_CLEAR_FLAG_FALSE(lname)					\
500 static inline int TestClearPage##uname(struct page *page) { return 0; }
501 
502 #define PAGEFLAG_FALSE(uname, lname) TESTPAGEFLAG_FALSE(uname, lname)	\
503 	SETPAGEFLAG_NOOP(uname, lname) CLEARPAGEFLAG_NOOP(uname, lname)
504 
505 #define TESTSCFLAG_FALSE(uname, lname)					\
506 	TESTSETFLAG_FALSE(uname, lname) TESTCLEARFLAG_FALSE(uname, lname)
507 
508 __PAGEFLAG(Locked, locked, PF_NO_TAIL)
509 FOLIO_FLAG(waiters, FOLIO_HEAD_PAGE)
510 FOLIO_FLAG(referenced, FOLIO_HEAD_PAGE)
511 	FOLIO_TEST_CLEAR_FLAG(referenced, FOLIO_HEAD_PAGE)
512 	__FOLIO_SET_FLAG(referenced, FOLIO_HEAD_PAGE)
513 PAGEFLAG(Dirty, dirty, PF_HEAD) TESTSCFLAG(Dirty, dirty, PF_HEAD)
514 	__CLEARPAGEFLAG(Dirty, dirty, PF_HEAD)
515 PAGEFLAG(LRU, lru, PF_HEAD) __CLEARPAGEFLAG(LRU, lru, PF_HEAD)
516 	TESTCLEARFLAG(LRU, lru, PF_HEAD)
517 FOLIO_FLAG(active, FOLIO_HEAD_PAGE)
518 	__FOLIO_CLEAR_FLAG(active, FOLIO_HEAD_PAGE)
519 	FOLIO_TEST_CLEAR_FLAG(active, FOLIO_HEAD_PAGE)
520 PAGEFLAG(Workingset, workingset, PF_HEAD)
521 	TESTCLEARFLAG(Workingset, workingset, PF_HEAD)
522 PAGEFLAG(Checked, checked, PF_NO_COMPOUND)	   /* Used by some filesystems */
523 
524 /* Xen */
525 PAGEFLAG(Pinned, pinned, PF_NO_COMPOUND)
526 	TESTSCFLAG(Pinned, pinned, PF_NO_COMPOUND)
527 PAGEFLAG(SavePinned, savepinned, PF_NO_COMPOUND);
528 PAGEFLAG(Foreign, foreign, PF_NO_COMPOUND);
529 PAGEFLAG(XenRemapped, xen_remapped, PF_NO_COMPOUND)
530 	TESTCLEARFLAG(XenRemapped, xen_remapped, PF_NO_COMPOUND)
531 
532 PAGEFLAG(Reserved, reserved, PF_NO_COMPOUND)
533 	__CLEARPAGEFLAG(Reserved, reserved, PF_NO_COMPOUND)
534 	__SETPAGEFLAG(Reserved, reserved, PF_NO_COMPOUND)
535 FOLIO_FLAG(swapbacked, FOLIO_HEAD_PAGE)
536 	__FOLIO_CLEAR_FLAG(swapbacked, FOLIO_HEAD_PAGE)
537 	__FOLIO_SET_FLAG(swapbacked, FOLIO_HEAD_PAGE)
538 
539 /*
540  * Private page markings that may be used by the filesystem that owns the page
541  * for its own purposes.
542  * - PG_private and PG_private_2 cause release_folio() and co to be invoked
543  */
544 PAGEFLAG(Private, private, PF_ANY)
545 PAGEFLAG(Private2, private_2, PF_ANY) TESTSCFLAG(Private2, private_2, PF_ANY)
546 
547 /* owner_2 can be set on tail pages for anon memory */
548 FOLIO_FLAG(owner_2, FOLIO_HEAD_PAGE)
549 
550 /*
551  * Only test-and-set exist for PG_writeback.  The unconditional operators are
552  * risky: they bypass page accounting.
553  */
554 TESTPAGEFLAG(Writeback, writeback, PF_NO_TAIL)
555 	TESTSCFLAG(Writeback, writeback, PF_NO_TAIL)
556 PAGEFLAG(MappedToDisk, mappedtodisk, PF_NO_TAIL)
557 
558 /* PG_readahead is only used for reads; PG_reclaim is only for writes */
559 PAGEFLAG(Reclaim, reclaim, PF_NO_TAIL)
560 	TESTCLEARFLAG(Reclaim, reclaim, PF_NO_TAIL)
561 FOLIO_FLAG(readahead, FOLIO_HEAD_PAGE)
562 	FOLIO_TEST_CLEAR_FLAG(readahead, FOLIO_HEAD_PAGE)
563 
564 #ifdef CONFIG_HIGHMEM
565 /*
566  * Must use a macro here due to header dependency issues. page_zone() is not
567  * available at this point.
568  */
569 #define PageHighMem(__p) is_highmem_idx(page_zonenum(__p))
570 #define folio_test_highmem(__f)	is_highmem_idx(folio_zonenum(__f))
571 #else
572 PAGEFLAG_FALSE(HighMem, highmem)
573 #endif
574 
575 #ifdef CONFIG_SWAP
576 static __always_inline bool folio_test_swapcache(const struct folio *folio)
577 {
578 	return folio_test_swapbacked(folio) &&
579 			test_bit(PG_swapcache, const_folio_flags(folio, 0));
580 }
581 
582 FOLIO_SET_FLAG(swapcache, FOLIO_HEAD_PAGE)
583 FOLIO_CLEAR_FLAG(swapcache, FOLIO_HEAD_PAGE)
584 #else
585 FOLIO_FLAG_FALSE(swapcache)
586 #endif
587 
588 FOLIO_FLAG(unevictable, FOLIO_HEAD_PAGE)
589 	__FOLIO_CLEAR_FLAG(unevictable, FOLIO_HEAD_PAGE)
590 	FOLIO_TEST_CLEAR_FLAG(unevictable, FOLIO_HEAD_PAGE)
591 
592 #ifdef CONFIG_MMU
593 FOLIO_FLAG(mlocked, FOLIO_HEAD_PAGE)
594 	__FOLIO_CLEAR_FLAG(mlocked, FOLIO_HEAD_PAGE)
595 	FOLIO_TEST_CLEAR_FLAG(mlocked, FOLIO_HEAD_PAGE)
596 	FOLIO_TEST_SET_FLAG(mlocked, FOLIO_HEAD_PAGE)
597 #else
598 FOLIO_FLAG_FALSE(mlocked)
599 	__FOLIO_CLEAR_FLAG_NOOP(mlocked)
600 	FOLIO_TEST_CLEAR_FLAG_FALSE(mlocked)
601 	FOLIO_TEST_SET_FLAG_FALSE(mlocked)
602 #endif
603 
604 #ifdef CONFIG_MEMORY_FAILURE
605 PAGEFLAG(HWPoison, hwpoison, PF_ANY)
606 TESTSCFLAG(HWPoison, hwpoison, PF_ANY)
607 #define __PG_HWPOISON (1UL << PG_hwpoison)
608 #else
609 PAGEFLAG_FALSE(HWPoison, hwpoison)
610 #define __PG_HWPOISON 0
611 #endif
612 
613 #ifdef CONFIG_PAGE_IDLE_FLAG
614 #ifdef CONFIG_64BIT
615 FOLIO_TEST_FLAG(young, FOLIO_HEAD_PAGE)
616 FOLIO_SET_FLAG(young, FOLIO_HEAD_PAGE)
617 FOLIO_TEST_CLEAR_FLAG(young, FOLIO_HEAD_PAGE)
618 FOLIO_FLAG(idle, FOLIO_HEAD_PAGE)
619 #endif
620 /* See page_idle.h for !64BIT workaround */
621 #else /* !CONFIG_PAGE_IDLE_FLAG */
622 FOLIO_FLAG_FALSE(young)
623 FOLIO_TEST_CLEAR_FLAG_FALSE(young)
624 FOLIO_FLAG_FALSE(idle)
625 #endif
626 
627 /*
628  * PageReported() is used to track reported free pages within the Buddy
629  * allocator. We can use the non-atomic version of the test and set
630  * operations as both should be shielded with the zone lock to prevent
631  * any possible races on the setting or clearing of the bit.
632  */
633 __PAGEFLAG(Reported, reported, PF_NO_COMPOUND)
634 
635 #ifdef CONFIG_MEMORY_HOTPLUG
636 PAGEFLAG(VmemmapSelfHosted, vmemmap_self_hosted, PF_ANY)
637 #else
638 PAGEFLAG_FALSE(VmemmapSelfHosted, vmemmap_self_hosted)
639 #endif
640 
641 /*
642  * On an anonymous folio mapped into a user virtual memory area,
643  * folio->mapping points to its anon_vma, not to a struct address_space;
644  * with the PAGE_MAPPING_ANON bit set to distinguish it.  See rmap.h.
645  *
646  * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
647  * the PAGE_MAPPING_MOVABLE bit may be set along with the PAGE_MAPPING_ANON
648  * bit; and then folio->mapping points, not to an anon_vma, but to a private
649  * structure which KSM associates with that merged page.  See ksm.h.
650  *
651  * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is used for non-lru movable
652  * page and then folio->mapping points to a struct movable_operations.
653  *
654  * Please note that, confusingly, "folio_mapping" refers to the inode
655  * address_space which maps the folio from disk; whereas "folio_mapped"
656  * refers to user virtual address space into which the folio is mapped.
657  *
658  * For slab pages, since slab reuses the bits in struct page to store its
659  * internal states, the folio->mapping does not exist as such, nor do
660  * these flags below.  So in order to avoid testing non-existent bits,
661  * please make sure that folio_test_slab(folio) actually evaluates to
662  * false before calling the following functions (e.g., folio_test_anon).
663  * See mm/slab.h.
664  */
665 #define PAGE_MAPPING_ANON	0x1
666 #define PAGE_MAPPING_MOVABLE	0x2
667 #define PAGE_MAPPING_KSM	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
668 #define PAGE_MAPPING_FLAGS	(PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
669 
670 /*
671  * Different with flags above, this flag is used only for fsdax mode.  It
672  * indicates that this page->mapping is now under reflink case.
673  */
674 #define PAGE_MAPPING_DAX_SHARED	((void *)0x1)
675 
676 static __always_inline bool folio_mapping_flags(const struct folio *folio)
677 {
678 	return ((unsigned long)folio->mapping & PAGE_MAPPING_FLAGS) != 0;
679 }
680 
681 static __always_inline bool PageMappingFlags(const struct page *page)
682 {
683 	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) != 0;
684 }
685 
686 static __always_inline bool folio_test_anon(const struct folio *folio)
687 {
688 	return ((unsigned long)folio->mapping & PAGE_MAPPING_ANON) != 0;
689 }
690 
691 static __always_inline bool PageAnon(const struct page *page)
692 {
693 	return folio_test_anon(page_folio(page));
694 }
695 
696 static __always_inline bool __folio_test_movable(const struct folio *folio)
697 {
698 	return ((unsigned long)folio->mapping & PAGE_MAPPING_FLAGS) ==
699 			PAGE_MAPPING_MOVABLE;
700 }
701 
702 static __always_inline bool __PageMovable(const struct page *page)
703 {
704 	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
705 				PAGE_MAPPING_MOVABLE;
706 }
707 
708 #ifdef CONFIG_KSM
709 /*
710  * A KSM page is one of those write-protected "shared pages" or "merged pages"
711  * which KSM maps into multiple mms, wherever identical anonymous page content
712  * is found in VM_MERGEABLE vmas.  It's a PageAnon page, pointing not to any
713  * anon_vma, but to that page's node of the stable tree.
714  */
715 static __always_inline bool folio_test_ksm(const struct folio *folio)
716 {
717 	return ((unsigned long)folio->mapping & PAGE_MAPPING_FLAGS) ==
718 				PAGE_MAPPING_KSM;
719 }
720 
721 static __always_inline bool PageKsm(const struct page *page)
722 {
723 	return folio_test_ksm(page_folio(page));
724 }
725 #else
726 TESTPAGEFLAG_FALSE(Ksm, ksm)
727 #endif
728 
729 u64 stable_page_flags(const struct page *page);
730 
731 /**
732  * folio_xor_flags_has_waiters - Change some folio flags.
733  * @folio: The folio.
734  * @mask: Bits set in this word will be changed.
735  *
736  * This must only be used for flags which are changed with the folio
737  * lock held.  For example, it is unsafe to use for PG_dirty as that
738  * can be set without the folio lock held.  It can also only be used
739  * on flags which are in the range 0-6 as some of the implementations
740  * only affect those bits.
741  *
742  * Return: Whether there are tasks waiting on the folio.
743  */
744 static inline bool folio_xor_flags_has_waiters(struct folio *folio,
745 		unsigned long mask)
746 {
747 	return xor_unlock_is_negative_byte(mask, folio_flags(folio, 0));
748 }
749 
750 /**
751  * folio_test_uptodate - Is this folio up to date?
752  * @folio: The folio.
753  *
754  * The uptodate flag is set on a folio when every byte in the folio is
755  * at least as new as the corresponding bytes on storage.  Anonymous
756  * and CoW folios are always uptodate.  If the folio is not uptodate,
757  * some of the bytes in it may be; see the is_partially_uptodate()
758  * address_space operation.
759  */
760 static inline bool folio_test_uptodate(const struct folio *folio)
761 {
762 	bool ret = test_bit(PG_uptodate, const_folio_flags(folio, 0));
763 	/*
764 	 * Must ensure that the data we read out of the folio is loaded
765 	 * _after_ we've loaded folio->flags to check the uptodate bit.
766 	 * We can skip the barrier if the folio is not uptodate, because
767 	 * we wouldn't be reading anything from it.
768 	 *
769 	 * See folio_mark_uptodate() for the other side of the story.
770 	 */
771 	if (ret)
772 		smp_rmb();
773 
774 	return ret;
775 }
776 
777 static inline bool PageUptodate(const struct page *page)
778 {
779 	return folio_test_uptodate(page_folio(page));
780 }
781 
782 static __always_inline void __folio_mark_uptodate(struct folio *folio)
783 {
784 	smp_wmb();
785 	__set_bit(PG_uptodate, folio_flags(folio, 0));
786 }
787 
788 static __always_inline void folio_mark_uptodate(struct folio *folio)
789 {
790 	/*
791 	 * Memory barrier must be issued before setting the PG_uptodate bit,
792 	 * so that all previous stores issued in order to bring the folio
793 	 * uptodate are actually visible before folio_test_uptodate becomes true.
794 	 */
795 	smp_wmb();
796 	set_bit(PG_uptodate, folio_flags(folio, 0));
797 }
798 
799 static __always_inline void __SetPageUptodate(struct page *page)
800 {
801 	__folio_mark_uptodate((struct folio *)page);
802 }
803 
804 static __always_inline void SetPageUptodate(struct page *page)
805 {
806 	folio_mark_uptodate((struct folio *)page);
807 }
808 
809 CLEARPAGEFLAG(Uptodate, uptodate, PF_NO_TAIL)
810 
811 void __folio_start_writeback(struct folio *folio, bool keep_write);
812 void set_page_writeback(struct page *page);
813 
814 #define folio_start_writeback(folio)			\
815 	__folio_start_writeback(folio, false)
816 #define folio_start_writeback_keepwrite(folio)	\
817 	__folio_start_writeback(folio, true)
818 
819 static __always_inline bool folio_test_head(const struct folio *folio)
820 {
821 	return test_bit(PG_head, const_folio_flags(folio, FOLIO_PF_ANY));
822 }
823 
824 static __always_inline int PageHead(const struct page *page)
825 {
826 	PF_POISONED_CHECK(page);
827 	return test_bit(PG_head, &page->flags) && !page_is_fake_head(page);
828 }
829 
830 __SETPAGEFLAG(Head, head, PF_ANY)
831 __CLEARPAGEFLAG(Head, head, PF_ANY)
832 CLEARPAGEFLAG(Head, head, PF_ANY)
833 
834 /**
835  * folio_test_large() - Does this folio contain more than one page?
836  * @folio: The folio to test.
837  *
838  * Return: True if the folio is larger than one page.
839  */
840 static inline bool folio_test_large(const struct folio *folio)
841 {
842 	return folio_test_head(folio);
843 }
844 
845 static __always_inline void set_compound_head(struct page *page, struct page *head)
846 {
847 	WRITE_ONCE(page->compound_head, (unsigned long)head + 1);
848 }
849 
850 static __always_inline void clear_compound_head(struct page *page)
851 {
852 	WRITE_ONCE(page->compound_head, 0);
853 }
854 
855 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
856 static inline void ClearPageCompound(struct page *page)
857 {
858 	BUG_ON(!PageHead(page));
859 	ClearPageHead(page);
860 }
861 FOLIO_FLAG(large_rmappable, FOLIO_SECOND_PAGE)
862 #else
863 FOLIO_FLAG_FALSE(large_rmappable)
864 #endif
865 
866 #define PG_head_mask ((1UL << PG_head))
867 
868 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
869 /*
870  * PageHuge() only returns true for hugetlbfs pages, but not for
871  * normal or transparent huge pages.
872  *
873  * PageTransHuge() returns true for both transparent huge and
874  * hugetlbfs pages, but not normal pages. PageTransHuge() can only be
875  * called only in the core VM paths where hugetlbfs pages can't exist.
876  */
877 static inline int PageTransHuge(const struct page *page)
878 {
879 	VM_BUG_ON_PAGE(PageTail(page), page);
880 	return PageHead(page);
881 }
882 
883 /*
884  * PageTransCompound returns true for both transparent huge pages
885  * and hugetlbfs pages, so it should only be called when it's known
886  * that hugetlbfs pages aren't involved.
887  */
888 static inline int PageTransCompound(const struct page *page)
889 {
890 	return PageCompound(page);
891 }
892 
893 /*
894  * PageTransTail returns true for both transparent huge pages
895  * and hugetlbfs pages, so it should only be called when it's known
896  * that hugetlbfs pages aren't involved.
897  */
898 static inline int PageTransTail(const struct page *page)
899 {
900 	return PageTail(page);
901 }
902 #else
903 TESTPAGEFLAG_FALSE(TransHuge, transhuge)
904 TESTPAGEFLAG_FALSE(TransCompound, transcompound)
905 TESTPAGEFLAG_FALSE(TransCompoundMap, transcompoundmap)
906 TESTPAGEFLAG_FALSE(TransTail, transtail)
907 #endif
908 
909 #if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
910 /*
911  * PageHasHWPoisoned indicates that at least one subpage is hwpoisoned in the
912  * compound page.
913  *
914  * This flag is set by hwpoison handler.  Cleared by THP split or free page.
915  */
916 PAGEFLAG(HasHWPoisoned, has_hwpoisoned, PF_SECOND)
917 	TESTSCFLAG(HasHWPoisoned, has_hwpoisoned, PF_SECOND)
918 #else
919 PAGEFLAG_FALSE(HasHWPoisoned, has_hwpoisoned)
920 	TESTSCFLAG_FALSE(HasHWPoisoned, has_hwpoisoned)
921 #endif
922 
923 /*
924  * For pages that do not use mapcount, page_type may be used.
925  * The low 24 bits of pagetype may be used for your own purposes, as long
926  * as you are careful to not affect the top 8 bits.  The low bits of
927  * pagetype will be overwritten when you clear the page_type from the page.
928  */
929 enum pagetype {
930 	/* 0x00-0x7f are positive numbers, ie mapcount */
931 	/* Reserve 0x80-0xef for mapcount overflow. */
932 	PGTY_buddy	= 0xf0,
933 	PGTY_offline	= 0xf1,
934 	PGTY_table	= 0xf2,
935 	PGTY_guard	= 0xf3,
936 	PGTY_hugetlb	= 0xf4,
937 	PGTY_slab	= 0xf5,
938 	PGTY_zsmalloc	= 0xf6,
939 	PGTY_unaccepted	= 0xf7,
940 
941 	PGTY_mapcount_underflow = 0xff
942 };
943 
944 static inline bool page_type_has_type(int page_type)
945 {
946 	return page_type < (PGTY_mapcount_underflow << 24);
947 }
948 
949 /* This takes a mapcount which is one more than page->_mapcount */
950 static inline bool page_mapcount_is_type(unsigned int mapcount)
951 {
952 	return page_type_has_type(mapcount - 1);
953 }
954 
955 static inline bool page_has_type(const struct page *page)
956 {
957 	return page_mapcount_is_type(data_race(page->page_type));
958 }
959 
960 #define FOLIO_TYPE_OPS(lname, fname)					\
961 static __always_inline bool folio_test_##fname(const struct folio *folio) \
962 {									\
963 	return data_race(folio->page.page_type >> 24) == PGTY_##lname;	\
964 }									\
965 static __always_inline void __folio_set_##fname(struct folio *folio)	\
966 {									\
967 	VM_BUG_ON_FOLIO(data_race(folio->page.page_type) != UINT_MAX,	\
968 			folio);						\
969 	folio->page.page_type = (unsigned int)PGTY_##lname << 24;	\
970 }									\
971 static __always_inline void __folio_clear_##fname(struct folio *folio)	\
972 {									\
973 	VM_BUG_ON_FOLIO(!folio_test_##fname(folio), folio);		\
974 	folio->page.page_type = UINT_MAX;				\
975 }
976 
977 #define PAGE_TYPE_OPS(uname, lname, fname)				\
978 FOLIO_TYPE_OPS(lname, fname)						\
979 static __always_inline int Page##uname(const struct page *page)		\
980 {									\
981 	return data_race(page->page_type >> 24) == PGTY_##lname;	\
982 }									\
983 static __always_inline void __SetPage##uname(struct page *page)		\
984 {									\
985 	VM_BUG_ON_PAGE(data_race(page->page_type) != UINT_MAX, page);	\
986 	page->page_type = (unsigned int)PGTY_##lname << 24;		\
987 }									\
988 static __always_inline void __ClearPage##uname(struct page *page)	\
989 {									\
990 	VM_BUG_ON_PAGE(!Page##uname(page), page);			\
991 	page->page_type = UINT_MAX;					\
992 }
993 
994 /*
995  * PageBuddy() indicates that the page is free and in the buddy system
996  * (see mm/page_alloc.c).
997  */
998 PAGE_TYPE_OPS(Buddy, buddy, buddy)
999 
1000 /*
1001  * PageOffline() indicates that the page is logically offline although the
1002  * containing section is online. (e.g. inflated in a balloon driver or
1003  * not onlined when onlining the section).
1004  * The content of these pages is effectively stale. Such pages should not
1005  * be touched (read/write/dump/save) except by their owner.
1006  *
1007  * When a memory block gets onlined, all pages are initialized with a
1008  * refcount of 1 and PageOffline(). generic_online_page() will
1009  * take care of clearing PageOffline().
1010  *
1011  * If a driver wants to allow to offline unmovable PageOffline() pages without
1012  * putting them back to the buddy, it can do so via the memory notifier by
1013  * decrementing the reference count in MEM_GOING_OFFLINE and incrementing the
1014  * reference count in MEM_CANCEL_OFFLINE. When offlining, the PageOffline()
1015  * pages (now with a reference count of zero) are treated like free (unmanaged)
1016  * pages, allowing the containing memory block to get offlined. A driver that
1017  * relies on this feature is aware that re-onlining the memory block will
1018  * require not giving them to the buddy via generic_online_page().
1019  *
1020  * Memory offlining code will not adjust the managed page count for any
1021  * PageOffline() pages, treating them like they were never exposed to the
1022  * buddy using generic_online_page().
1023  *
1024  * There are drivers that mark a page PageOffline() and expect there won't be
1025  * any further access to page content. PFN walkers that read content of random
1026  * pages should check PageOffline() and synchronize with such drivers using
1027  * page_offline_freeze()/page_offline_thaw().
1028  */
1029 PAGE_TYPE_OPS(Offline, offline, offline)
1030 
1031 extern void page_offline_freeze(void);
1032 extern void page_offline_thaw(void);
1033 extern void page_offline_begin(void);
1034 extern void page_offline_end(void);
1035 
1036 /*
1037  * Marks pages in use as page tables.
1038  */
1039 PAGE_TYPE_OPS(Table, table, pgtable)
1040 
1041 /*
1042  * Marks guardpages used with debug_pagealloc.
1043  */
1044 PAGE_TYPE_OPS(Guard, guard, guard)
1045 
1046 FOLIO_TYPE_OPS(slab, slab)
1047 
1048 /**
1049  * PageSlab - Determine if the page belongs to the slab allocator
1050  * @page: The page to test.
1051  *
1052  * Context: Any context.
1053  * Return: True for slab pages, false for any other kind of page.
1054  */
1055 static inline bool PageSlab(const struct page *page)
1056 {
1057 	return folio_test_slab(page_folio(page));
1058 }
1059 
1060 #ifdef CONFIG_HUGETLB_PAGE
1061 FOLIO_TYPE_OPS(hugetlb, hugetlb)
1062 #else
1063 FOLIO_TEST_FLAG_FALSE(hugetlb)
1064 #endif
1065 
1066 PAGE_TYPE_OPS(Zsmalloc, zsmalloc, zsmalloc)
1067 
1068 /*
1069  * Mark pages that has to be accepted before touched for the first time.
1070  *
1071  * Serialized with zone lock.
1072  */
1073 PAGE_TYPE_OPS(Unaccepted, unaccepted, unaccepted)
1074 
1075 /**
1076  * PageHuge - Determine if the page belongs to hugetlbfs
1077  * @page: The page to test.
1078  *
1079  * Context: Any context.
1080  * Return: True for hugetlbfs pages, false for anon pages or pages
1081  * belonging to other filesystems.
1082  */
1083 static inline bool PageHuge(const struct page *page)
1084 {
1085 	return folio_test_hugetlb(page_folio(page));
1086 }
1087 
1088 /*
1089  * Check if a page is currently marked HWPoisoned. Note that this check is
1090  * best effort only and inherently racy: there is no way to synchronize with
1091  * failing hardware.
1092  */
1093 static inline bool is_page_hwpoison(const struct page *page)
1094 {
1095 	const struct folio *folio;
1096 
1097 	if (PageHWPoison(page))
1098 		return true;
1099 	folio = page_folio(page);
1100 	return folio_test_hugetlb(folio) && PageHWPoison(&folio->page);
1101 }
1102 
1103 bool is_free_buddy_page(const struct page *page);
1104 
1105 PAGEFLAG(Isolated, isolated, PF_ANY);
1106 
1107 static __always_inline int PageAnonExclusive(const struct page *page)
1108 {
1109 	VM_BUG_ON_PGFLAGS(!PageAnon(page), page);
1110 	/*
1111 	 * HugeTLB stores this information on the head page; THP keeps it per
1112 	 * page
1113 	 */
1114 	if (PageHuge(page))
1115 		page = compound_head(page);
1116 	return test_bit(PG_anon_exclusive, &PF_ANY(page, 1)->flags);
1117 }
1118 
1119 static __always_inline void SetPageAnonExclusive(struct page *page)
1120 {
1121 	VM_BUG_ON_PGFLAGS(!PageAnon(page) || PageKsm(page), page);
1122 	VM_BUG_ON_PGFLAGS(PageHuge(page) && !PageHead(page), page);
1123 	set_bit(PG_anon_exclusive, &PF_ANY(page, 1)->flags);
1124 }
1125 
1126 static __always_inline void ClearPageAnonExclusive(struct page *page)
1127 {
1128 	VM_BUG_ON_PGFLAGS(!PageAnon(page) || PageKsm(page), page);
1129 	VM_BUG_ON_PGFLAGS(PageHuge(page) && !PageHead(page), page);
1130 	clear_bit(PG_anon_exclusive, &PF_ANY(page, 1)->flags);
1131 }
1132 
1133 static __always_inline void __ClearPageAnonExclusive(struct page *page)
1134 {
1135 	VM_BUG_ON_PGFLAGS(!PageAnon(page), page);
1136 	VM_BUG_ON_PGFLAGS(PageHuge(page) && !PageHead(page), page);
1137 	__clear_bit(PG_anon_exclusive, &PF_ANY(page, 1)->flags);
1138 }
1139 
1140 #ifdef CONFIG_MMU
1141 #define __PG_MLOCKED		(1UL << PG_mlocked)
1142 #else
1143 #define __PG_MLOCKED		0
1144 #endif
1145 
1146 /*
1147  * Flags checked when a page is freed.  Pages being freed should not have
1148  * these flags set.  If they are, there is a problem.
1149  */
1150 #define PAGE_FLAGS_CHECK_AT_FREE				\
1151 	(1UL << PG_lru		| 1UL << PG_locked	|	\
1152 	 1UL << PG_private	| 1UL << PG_private_2	|	\
1153 	 1UL << PG_writeback	| 1UL << PG_reserved	|	\
1154 	 1UL << PG_active 	|				\
1155 	 1UL << PG_unevictable	| __PG_MLOCKED | LRU_GEN_MASK)
1156 
1157 /*
1158  * Flags checked when a page is prepped for return by the page allocator.
1159  * Pages being prepped should not have these flags set.  If they are set,
1160  * there has been a kernel bug or struct page corruption.
1161  *
1162  * __PG_HWPOISON is exceptional because it needs to be kept beyond page's
1163  * alloc-free cycle to prevent from reusing the page.
1164  */
1165 #define PAGE_FLAGS_CHECK_AT_PREP	\
1166 	((PAGEFLAGS_MASK & ~__PG_HWPOISON) | LRU_GEN_MASK | LRU_REFS_MASK)
1167 
1168 /*
1169  * Flags stored in the second page of a compound page.  They may overlap
1170  * the CHECK_AT_FREE flags above, so need to be cleared.
1171  */
1172 #define PAGE_FLAGS_SECOND						\
1173 	(0xffUL /* order */		| 1UL << PG_has_hwpoisoned |	\
1174 	 1UL << PG_large_rmappable)
1175 
1176 #define PAGE_FLAGS_PRIVATE				\
1177 	(1UL << PG_private | 1UL << PG_private_2)
1178 /**
1179  * folio_has_private - Determine if folio has private stuff
1180  * @folio: The folio to be checked
1181  *
1182  * Determine if a folio has private stuff, indicating that release routines
1183  * should be invoked upon it.
1184  */
1185 static inline int folio_has_private(const struct folio *folio)
1186 {
1187 	return !!(folio->flags & PAGE_FLAGS_PRIVATE);
1188 }
1189 
1190 #undef PF_ANY
1191 #undef PF_HEAD
1192 #undef PF_NO_TAIL
1193 #undef PF_NO_COMPOUND
1194 #undef PF_SECOND
1195 #endif /* !__GENERATING_BOUNDS_H */
1196 
1197 #endif	/* PAGE_FLAGS_H */
1198