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