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