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, which can never be swapped out. Some
21 * of them might not even exist (eg empty_bad_page)...
22 *
23 * The PG_private bitflag is set on pagecache pages if they contain filesystem
24 * specific data (which is normally at page->private). It can be used by
25 * private allocations for its own usage.
26 *
27 * During initiation of disk I/O, PG_locked is set. This bit is set before I/O
28 * and cleared when writeback _starts_ or when read _completes_. PG_writeback
29 * is set before writeback starts and cleared when it finishes.
30 *
31 * PG_locked also pins a page in pagecache, and blocks truncation of the file
32 * while it is held.
33 *
34 * page_waitqueue(page) is a wait queue of all tasks waiting for the page
35 * to become unlocked.
36 *
37 * PG_uptodate tells whether the page's contents is valid. When a read
38 * completes, the page becomes uptodate, unless a disk I/O error happened.
39 *
40 * PG_referenced, PG_reclaim are used for page reclaim for anonymous and
41 * file-backed pagecache (see mm/vmscan.c).
42 *
43 * PG_error is set to indicate that an I/O error occurred on this page.
44 *
45 * PG_arch_1 is an architecture specific page state bit. The generic code
46 * guarantees that this bit is cleared for a page when it first is entered into
47 * the page cache.
48 *
49 * PG_highmem pages are not permanently mapped into the kernel virtual address
50 * space, they need to be kmapped separately for doing IO on the pages. The
51 * struct page (these bits with information) are always mapped into kernel
52 * address space...
53 *
54 * PG_hwpoison indicates that a page got corrupted in hardware and contains
55 * data with incorrect ECC bits that triggered a machine check. Accessing is
56 * not safe since it may cause another machine check. Don't touch!
57 */
58
59 /*
60 * Don't use the *_dontuse flags. Use the macros. Otherwise you'll break
61 * locked- and dirty-page accounting.
62 *
63 * The page flags field is split into two parts, the main flags area
64 * which extends from the low bits upwards, and the fields area which
65 * extends from the high bits downwards.
66 *
67 * | FIELD | ... | FLAGS |
68 * N-1 ^ 0
69 * (NR_PAGEFLAGS)
70 *
71 * The fields area is reserved for fields mapping zone, node (for NUMA) and
72 * SPARSEMEM section (for variants of SPARSEMEM that require section ids like
73 * SPARSEMEM_EXTREME with !SPARSEMEM_VMEMMAP).
74 */
75 enum pageflags {
76 PG_locked, /* Page is locked. Don't touch. */
77 PG_referenced,
78 PG_uptodate,
79 PG_dirty,
80 PG_lru,
81 PG_active,
82 PG_workingset,
83 PG_waiters, /* Page has waiters, check its waitqueue. Must be bit #7 and in the same byte as "PG_locked" */
84 PG_error,
85 PG_slab,
86 PG_owner_priv_1, /* Owner use. If pagecache, fs may use*/
87 PG_arch_1,
88 PG_reserved,
89 PG_private, /* If pagecache, has fs-private data */
90 PG_private_2, /* If pagecache, has fs aux data */
91 PG_writeback, /* Page is under writeback */
92 PG_head, /* A head page */
93 PG_mappedtodisk, /* Has blocks allocated on-disk */
94 PG_reclaim, /* To be reclaimed asap */
95 PG_swapbacked, /* Page is backed by RAM/swap */
96 PG_unevictable, /* Page is "unevictable" */
97 #ifdef CONFIG_MMU
98 PG_mlocked, /* Page is vma mlocked */
99 #endif
100 #ifdef CONFIG_ARCH_USES_PG_UNCACHED
101 PG_uncached, /* Page has been mapped as uncached */
102 #endif
103 #ifdef CONFIG_MEMORY_FAILURE
104 PG_hwpoison, /* hardware poisoned page. Don't touch */
105 #endif
106 #if defined(CONFIG_IDLE_PAGE_TRACKING) && defined(CONFIG_64BIT)
107 PG_young,
108 PG_idle,
109 #endif
110 __NR_PAGEFLAGS,
111
112 /* Filesystems */
113 PG_checked = PG_owner_priv_1,
114
115 /* SwapBacked */
116 PG_swapcache = PG_owner_priv_1, /* Swap page: swp_entry_t in private */
117
118 /* Two page bits are conscripted by FS-Cache to maintain local caching
119 * state. These bits are set on pages belonging to the netfs's inodes
120 * when those inodes are being locally cached.
121 */
122 PG_fscache = PG_private_2, /* page backed by cache */
123
124 /* XEN */
125 /* Pinned in Xen as a read-only pagetable page. */
126 PG_pinned = PG_owner_priv_1,
127 /* Pinned as part of domain save (see xen_mm_pin_all()). */
128 PG_savepinned = PG_dirty,
129 /* Has a grant mapping of another (foreign) domain's page. */
130 PG_foreign = PG_owner_priv_1,
131
132 /* SLOB */
133 PG_slob_free = PG_private,
134
135 /* Compound pages. Stored in first tail page's flags */
136 PG_double_map = PG_private_2,
137
138 /* non-lru isolated movable page */
139 PG_isolated = PG_reclaim,
140 };
141
142 #ifndef __GENERATING_BOUNDS_H
143
144 struct page; /* forward declaration */
145
compound_head(struct page * page)146 static inline struct page *compound_head(struct page *page)
147 {
148 unsigned long head = READ_ONCE(page->compound_head);
149
150 if (unlikely(head & 1))
151 return (struct page *) (head - 1);
152 return page;
153 }
154
PageTail(struct page * page)155 static __always_inline int PageTail(struct page *page)
156 {
157 return READ_ONCE(page->compound_head) & 1;
158 }
159
PageCompound(struct page * page)160 static __always_inline int PageCompound(struct page *page)
161 {
162 return test_bit(PG_head, &page->flags) || PageTail(page);
163 }
164
165 /*
166 * Page flags policies wrt compound pages
167 *
168 * PF_ANY:
169 * the page flag is relevant for small, head and tail pages.
170 *
171 * PF_HEAD:
172 * for compound page all operations related to the page flag applied to
173 * head page.
174 *
175 * PF_ONLY_HEAD:
176 * for compound page, callers only ever operate on the head page.
177 *
178 * PF_NO_TAIL:
179 * modifications of the page flag must be done on small or head pages,
180 * checks can be done on tail pages too.
181 *
182 * PF_NO_COMPOUND:
183 * the page flag is not relevant for compound pages.
184 */
185 #define PF_ANY(page, enforce) page
186 #define PF_HEAD(page, enforce) compound_head(page)
187 #define PF_ONLY_HEAD(page, enforce) ({ \
188 VM_BUG_ON_PGFLAGS(PageTail(page), page); \
189 page;})
190 #define PF_NO_TAIL(page, enforce) ({ \
191 VM_BUG_ON_PGFLAGS(enforce && PageTail(page), page); \
192 compound_head(page);})
193 #define PF_NO_COMPOUND(page, enforce) ({ \
194 VM_BUG_ON_PGFLAGS(enforce && PageCompound(page), page); \
195 page;})
196
197 /*
198 * Macros to create function definitions for page flags
199 */
200 #define TESTPAGEFLAG(uname, lname, policy) \
201 static __always_inline int Page##uname(struct page *page) \
202 { return test_bit(PG_##lname, &policy(page, 0)->flags); }
203
204 #define SETPAGEFLAG(uname, lname, policy) \
205 static __always_inline void SetPage##uname(struct page *page) \
206 { set_bit(PG_##lname, &policy(page, 1)->flags); }
207
208 #define CLEARPAGEFLAG(uname, lname, policy) \
209 static __always_inline void ClearPage##uname(struct page *page) \
210 { clear_bit(PG_##lname, &policy(page, 1)->flags); }
211
212 #define __SETPAGEFLAG(uname, lname, policy) \
213 static __always_inline void __SetPage##uname(struct page *page) \
214 { __set_bit(PG_##lname, &policy(page, 1)->flags); }
215
216 #define __CLEARPAGEFLAG(uname, lname, policy) \
217 static __always_inline void __ClearPage##uname(struct page *page) \
218 { __clear_bit(PG_##lname, &policy(page, 1)->flags); }
219
220 #define TESTSETFLAG(uname, lname, policy) \
221 static __always_inline int TestSetPage##uname(struct page *page) \
222 { return test_and_set_bit(PG_##lname, &policy(page, 1)->flags); }
223
224 #define TESTCLEARFLAG(uname, lname, policy) \
225 static __always_inline int TestClearPage##uname(struct page *page) \
226 { return test_and_clear_bit(PG_##lname, &policy(page, 1)->flags); }
227
228 #define PAGEFLAG(uname, lname, policy) \
229 TESTPAGEFLAG(uname, lname, policy) \
230 SETPAGEFLAG(uname, lname, policy) \
231 CLEARPAGEFLAG(uname, lname, policy)
232
233 #define __PAGEFLAG(uname, lname, policy) \
234 TESTPAGEFLAG(uname, lname, policy) \
235 __SETPAGEFLAG(uname, lname, policy) \
236 __CLEARPAGEFLAG(uname, lname, policy)
237
238 #define TESTSCFLAG(uname, lname, policy) \
239 TESTSETFLAG(uname, lname, policy) \
240 TESTCLEARFLAG(uname, lname, policy)
241
242 #define TESTPAGEFLAG_FALSE(uname) \
243 static inline int Page##uname(const struct page *page) { return 0; }
244
245 #define SETPAGEFLAG_NOOP(uname) \
246 static inline void SetPage##uname(struct page *page) { }
247
248 #define CLEARPAGEFLAG_NOOP(uname) \
249 static inline void ClearPage##uname(struct page *page) { }
250
251 #define __CLEARPAGEFLAG_NOOP(uname) \
252 static inline void __ClearPage##uname(struct page *page) { }
253
254 #define TESTSETFLAG_FALSE(uname) \
255 static inline int TestSetPage##uname(struct page *page) { return 0; }
256
257 #define TESTCLEARFLAG_FALSE(uname) \
258 static inline int TestClearPage##uname(struct page *page) { return 0; }
259
260 #define PAGEFLAG_FALSE(uname) TESTPAGEFLAG_FALSE(uname) \
261 SETPAGEFLAG_NOOP(uname) CLEARPAGEFLAG_NOOP(uname)
262
263 #define TESTSCFLAG_FALSE(uname) \
264 TESTSETFLAG_FALSE(uname) TESTCLEARFLAG_FALSE(uname)
265
266 __PAGEFLAG(Locked, locked, PF_NO_TAIL)
267 PAGEFLAG(Waiters, waiters, PF_ONLY_HEAD) __CLEARPAGEFLAG(Waiters, waiters, PF_ONLY_HEAD)
268 PAGEFLAG(Error, error, PF_NO_TAIL) TESTCLEARFLAG(Error, error, PF_NO_TAIL)
269 PAGEFLAG(Referenced, referenced, PF_HEAD)
270 TESTCLEARFLAG(Referenced, referenced, PF_HEAD)
271 __SETPAGEFLAG(Referenced, referenced, PF_HEAD)
272 PAGEFLAG(Dirty, dirty, PF_HEAD) TESTSCFLAG(Dirty, dirty, PF_HEAD)
273 __CLEARPAGEFLAG(Dirty, dirty, PF_HEAD)
274 PAGEFLAG(LRU, lru, PF_HEAD) __CLEARPAGEFLAG(LRU, lru, PF_HEAD)
275 PAGEFLAG(Active, active, PF_HEAD) __CLEARPAGEFLAG(Active, active, PF_HEAD)
276 TESTCLEARFLAG(Active, active, PF_HEAD)
277 PAGEFLAG(Workingset, workingset, PF_HEAD)
278 TESTCLEARFLAG(Workingset, workingset, PF_HEAD)
279 __PAGEFLAG(Slab, slab, PF_NO_TAIL)
280 __PAGEFLAG(SlobFree, slob_free, PF_NO_TAIL)
281 PAGEFLAG(Checked, checked, PF_NO_COMPOUND) /* Used by some filesystems */
282
283 /* Xen */
284 PAGEFLAG(Pinned, pinned, PF_NO_COMPOUND)
285 TESTSCFLAG(Pinned, pinned, PF_NO_COMPOUND)
286 PAGEFLAG(SavePinned, savepinned, PF_NO_COMPOUND);
287 PAGEFLAG(Foreign, foreign, PF_NO_COMPOUND);
288
PAGEFLAG(Reserved,reserved,PF_NO_COMPOUND)289 PAGEFLAG(Reserved, reserved, PF_NO_COMPOUND)
290 __CLEARPAGEFLAG(Reserved, reserved, PF_NO_COMPOUND)
291 PAGEFLAG(SwapBacked, swapbacked, PF_NO_TAIL)
292 __CLEARPAGEFLAG(SwapBacked, swapbacked, PF_NO_TAIL)
293 __SETPAGEFLAG(SwapBacked, swapbacked, PF_NO_TAIL)
294
295 /*
296 * Private page markings that may be used by the filesystem that owns the page
297 * for its own purposes.
298 * - PG_private and PG_private_2 cause releasepage() and co to be invoked
299 */
300 PAGEFLAG(Private, private, PF_ANY) __SETPAGEFLAG(Private, private, PF_ANY)
301 __CLEARPAGEFLAG(Private, private, PF_ANY)
302 PAGEFLAG(Private2, private_2, PF_ANY) TESTSCFLAG(Private2, private_2, PF_ANY)
303 PAGEFLAG(OwnerPriv1, owner_priv_1, PF_ANY)
304 TESTCLEARFLAG(OwnerPriv1, owner_priv_1, PF_ANY)
305
306 /*
307 * Only test-and-set exist for PG_writeback. The unconditional operators are
308 * risky: they bypass page accounting.
309 */
310 TESTPAGEFLAG(Writeback, writeback, PF_NO_TAIL)
311 TESTSCFLAG(Writeback, writeback, PF_NO_TAIL)
312 PAGEFLAG(MappedToDisk, mappedtodisk, PF_NO_TAIL)
313
314 /* PG_readahead is only used for reads; PG_reclaim is only for writes */
315 PAGEFLAG(Reclaim, reclaim, PF_NO_TAIL)
316 TESTCLEARFLAG(Reclaim, reclaim, PF_NO_TAIL)
317 PAGEFLAG(Readahead, reclaim, PF_NO_COMPOUND)
318 TESTCLEARFLAG(Readahead, reclaim, PF_NO_COMPOUND)
319
320 #ifdef CONFIG_HIGHMEM
321 /*
322 * Must use a macro here due to header dependency issues. page_zone() is not
323 * available at this point.
324 */
325 #define PageHighMem(__p) is_highmem_idx(page_zonenum(__p))
326 #else
327 PAGEFLAG_FALSE(HighMem)
328 #endif
329
330 #ifdef CONFIG_SWAP
331 static __always_inline int PageSwapCache(struct page *page)
332 {
333 #ifdef CONFIG_THP_SWAP
334 page = compound_head(page);
335 #endif
336 return PageSwapBacked(page) && test_bit(PG_swapcache, &page->flags);
337
338 }
SETPAGEFLAG(SwapCache,swapcache,PF_NO_TAIL)339 SETPAGEFLAG(SwapCache, swapcache, PF_NO_TAIL)
340 CLEARPAGEFLAG(SwapCache, swapcache, PF_NO_TAIL)
341 #else
342 PAGEFLAG_FALSE(SwapCache)
343 #endif
344
345 PAGEFLAG(Unevictable, unevictable, PF_HEAD)
346 __CLEARPAGEFLAG(Unevictable, unevictable, PF_HEAD)
347 TESTCLEARFLAG(Unevictable, unevictable, PF_HEAD)
348
349 #ifdef CONFIG_MMU
350 PAGEFLAG(Mlocked, mlocked, PF_NO_TAIL)
351 __CLEARPAGEFLAG(Mlocked, mlocked, PF_NO_TAIL)
352 TESTSCFLAG(Mlocked, mlocked, PF_NO_TAIL)
353 #else
354 PAGEFLAG_FALSE(Mlocked) __CLEARPAGEFLAG_NOOP(Mlocked)
355 TESTSCFLAG_FALSE(Mlocked)
356 #endif
357
358 #ifdef CONFIG_ARCH_USES_PG_UNCACHED
359 PAGEFLAG(Uncached, uncached, PF_NO_COMPOUND)
360 #else
361 PAGEFLAG_FALSE(Uncached)
362 #endif
363
364 #ifdef CONFIG_MEMORY_FAILURE
365 PAGEFLAG(HWPoison, hwpoison, PF_ANY)
366 TESTSCFLAG(HWPoison, hwpoison, PF_ANY)
367 #define __PG_HWPOISON (1UL << PG_hwpoison)
368 #else
369 PAGEFLAG_FALSE(HWPoison)
370 #define __PG_HWPOISON 0
371 #endif
372
373 #if defined(CONFIG_IDLE_PAGE_TRACKING) && defined(CONFIG_64BIT)
374 TESTPAGEFLAG(Young, young, PF_ANY)
375 SETPAGEFLAG(Young, young, PF_ANY)
376 TESTCLEARFLAG(Young, young, PF_ANY)
377 PAGEFLAG(Idle, idle, PF_ANY)
378 #endif
379
380 /*
381 * On an anonymous page mapped into a user virtual memory area,
382 * page->mapping points to its anon_vma, not to a struct address_space;
383 * with the PAGE_MAPPING_ANON bit set to distinguish it. See rmap.h.
384 *
385 * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
386 * the PAGE_MAPPING_MOVABLE bit may be set along with the PAGE_MAPPING_ANON
387 * bit; and then page->mapping points, not to an anon_vma, but to a private
388 * structure which KSM associates with that merged page. See ksm.h.
389 *
390 * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is used for non-lru movable
391 * page and then page->mapping points a struct address_space.
392 *
393 * Please note that, confusingly, "page_mapping" refers to the inode
394 * address_space which maps the page from disk; whereas "page_mapped"
395 * refers to user virtual address space into which the page is mapped.
396 */
397 #define PAGE_MAPPING_ANON 0x1
398 #define PAGE_MAPPING_MOVABLE 0x2
399 #define PAGE_MAPPING_KSM (PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
400 #define PAGE_MAPPING_FLAGS (PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE)
401
402 static __always_inline int PageMappingFlags(struct page *page)
403 {
404 return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) != 0;
405 }
406
PageAnon(struct page * page)407 static __always_inline int PageAnon(struct page *page)
408 {
409 page = compound_head(page);
410 return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
411 }
412
__PageMovable(struct page * page)413 static __always_inline int __PageMovable(struct page *page)
414 {
415 return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
416 PAGE_MAPPING_MOVABLE;
417 }
418
419 #ifdef CONFIG_KSM
420 /*
421 * A KSM page is one of those write-protected "shared pages" or "merged pages"
422 * which KSM maps into multiple mms, wherever identical anonymous page content
423 * is found in VM_MERGEABLE vmas. It's a PageAnon page, pointing not to any
424 * anon_vma, but to that page's node of the stable tree.
425 */
PageKsm(struct page * page)426 static __always_inline int PageKsm(struct page *page)
427 {
428 page = compound_head(page);
429 return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
430 PAGE_MAPPING_KSM;
431 }
432 #else
433 TESTPAGEFLAG_FALSE(Ksm)
434 #endif
435
436 u64 stable_page_flags(struct page *page);
437
PageUptodate(struct page * page)438 static inline int PageUptodate(struct page *page)
439 {
440 int ret;
441 page = compound_head(page);
442 ret = test_bit(PG_uptodate, &(page)->flags);
443 /*
444 * Must ensure that the data we read out of the page is loaded
445 * _after_ we've loaded page->flags to check for PageUptodate.
446 * We can skip the barrier if the page is not uptodate, because
447 * we wouldn't be reading anything from it.
448 *
449 * See SetPageUptodate() for the other side of the story.
450 */
451 if (ret)
452 smp_rmb();
453
454 return ret;
455 }
456
__SetPageUptodate(struct page * page)457 static __always_inline void __SetPageUptodate(struct page *page)
458 {
459 VM_BUG_ON_PAGE(PageTail(page), page);
460 smp_wmb();
461 __set_bit(PG_uptodate, &page->flags);
462 }
463
SetPageUptodate(struct page * page)464 static __always_inline void SetPageUptodate(struct page *page)
465 {
466 VM_BUG_ON_PAGE(PageTail(page), page);
467 /*
468 * Memory barrier must be issued before setting the PG_uptodate bit,
469 * so that all previous stores issued in order to bring the page
470 * uptodate are actually visible before PageUptodate becomes true.
471 */
472 smp_wmb();
473 set_bit(PG_uptodate, &page->flags);
474 }
475
476 CLEARPAGEFLAG(Uptodate, uptodate, PF_NO_TAIL)
477
478 int test_clear_page_writeback(struct page *page);
479 int __test_set_page_writeback(struct page *page, bool keep_write);
480
481 #define test_set_page_writeback(page) \
482 __test_set_page_writeback(page, false)
483 #define test_set_page_writeback_keepwrite(page) \
484 __test_set_page_writeback(page, true)
485
set_page_writeback(struct page * page)486 static inline void set_page_writeback(struct page *page)
487 {
488 test_set_page_writeback(page);
489 }
490
set_page_writeback_keepwrite(struct page * page)491 static inline void set_page_writeback_keepwrite(struct page *page)
492 {
493 test_set_page_writeback_keepwrite(page);
494 }
495
__PAGEFLAG(Head,head,PF_ANY)496 __PAGEFLAG(Head, head, PF_ANY) CLEARPAGEFLAG(Head, head, PF_ANY)
497
498 static __always_inline void set_compound_head(struct page *page, struct page *head)
499 {
500 WRITE_ONCE(page->compound_head, (unsigned long)head + 1);
501 }
502
clear_compound_head(struct page * page)503 static __always_inline void clear_compound_head(struct page *page)
504 {
505 WRITE_ONCE(page->compound_head, 0);
506 }
507
508 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
ClearPageCompound(struct page * page)509 static inline void ClearPageCompound(struct page *page)
510 {
511 BUG_ON(!PageHead(page));
512 ClearPageHead(page);
513 }
514 #endif
515
516 #define PG_head_mask ((1UL << PG_head))
517
518 #ifdef CONFIG_HUGETLB_PAGE
519 int PageHuge(struct page *page);
520 int PageHeadHuge(struct page *page);
521 bool page_huge_active(struct page *page);
522 #else
523 TESTPAGEFLAG_FALSE(Huge)
TESTPAGEFLAG_FALSE(HeadHuge)524 TESTPAGEFLAG_FALSE(HeadHuge)
525
526 static inline bool page_huge_active(struct page *page)
527 {
528 return 0;
529 }
530 #endif
531
532
533 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
534 /*
535 * PageHuge() only returns true for hugetlbfs pages, but not for
536 * normal or transparent huge pages.
537 *
538 * PageTransHuge() returns true for both transparent huge and
539 * hugetlbfs pages, but not normal pages. PageTransHuge() can only be
540 * called only in the core VM paths where hugetlbfs pages can't exist.
541 */
PageTransHuge(struct page * page)542 static inline int PageTransHuge(struct page *page)
543 {
544 VM_BUG_ON_PAGE(PageTail(page), page);
545 return PageHead(page);
546 }
547
548 /*
549 * PageTransCompound returns true for both transparent huge pages
550 * and hugetlbfs pages, so it should only be called when it's known
551 * that hugetlbfs pages aren't involved.
552 */
PageTransCompound(struct page * page)553 static inline int PageTransCompound(struct page *page)
554 {
555 return PageCompound(page);
556 }
557
558 /*
559 * PageTransCompoundMap is the same as PageTransCompound, but it also
560 * guarantees the primary MMU has the entire compound page mapped
561 * through pmd_trans_huge, which in turn guarantees the secondary MMUs
562 * can also map the entire compound page. This allows the secondary
563 * MMUs to call get_user_pages() only once for each compound page and
564 * to immediately map the entire compound page with a single secondary
565 * MMU fault. If there will be a pmd split later, the secondary MMUs
566 * will get an update through the MMU notifier invalidation through
567 * split_huge_pmd().
568 *
569 * Unlike PageTransCompound, this is safe to be called only while
570 * split_huge_pmd() cannot run from under us, like if protected by the
571 * MMU notifier, otherwise it may result in page->_mapcount check false
572 * positives.
573 *
574 * We have to treat page cache THP differently since every subpage of it
575 * would get _mapcount inc'ed once it is PMD mapped. But, it may be PTE
576 * mapped in the current process so comparing subpage's _mapcount to
577 * compound_mapcount to filter out PTE mapped case.
578 */
PageTransCompoundMap(struct page * page)579 static inline int PageTransCompoundMap(struct page *page)
580 {
581 struct page *head;
582
583 if (!PageTransCompound(page))
584 return 0;
585
586 if (PageAnon(page))
587 return atomic_read(&page->_mapcount) < 0;
588
589 head = compound_head(page);
590 /* File THP is PMD mapped and not PTE mapped */
591 return atomic_read(&page->_mapcount) ==
592 atomic_read(compound_mapcount_ptr(head));
593 }
594
595 /*
596 * PageTransTail returns true for both transparent huge pages
597 * and hugetlbfs pages, so it should only be called when it's known
598 * that hugetlbfs pages aren't involved.
599 */
PageTransTail(struct page * page)600 static inline int PageTransTail(struct page *page)
601 {
602 return PageTail(page);
603 }
604
605 /*
606 * PageDoubleMap indicates that the compound page is mapped with PTEs as well
607 * as PMDs.
608 *
609 * This is required for optimization of rmap operations for THP: we can postpone
610 * per small page mapcount accounting (and its overhead from atomic operations)
611 * until the first PMD split.
612 *
613 * For the page PageDoubleMap means ->_mapcount in all sub-pages is offset up
614 * by one. This reference will go away with last compound_mapcount.
615 *
616 * See also __split_huge_pmd_locked() and page_remove_anon_compound_rmap().
617 */
PageDoubleMap(struct page * page)618 static inline int PageDoubleMap(struct page *page)
619 {
620 return PageHead(page) && test_bit(PG_double_map, &page[1].flags);
621 }
622
SetPageDoubleMap(struct page * page)623 static inline void SetPageDoubleMap(struct page *page)
624 {
625 VM_BUG_ON_PAGE(!PageHead(page), page);
626 set_bit(PG_double_map, &page[1].flags);
627 }
628
ClearPageDoubleMap(struct page * page)629 static inline void ClearPageDoubleMap(struct page *page)
630 {
631 VM_BUG_ON_PAGE(!PageHead(page), page);
632 clear_bit(PG_double_map, &page[1].flags);
633 }
TestSetPageDoubleMap(struct page * page)634 static inline int TestSetPageDoubleMap(struct page *page)
635 {
636 VM_BUG_ON_PAGE(!PageHead(page), page);
637 return test_and_set_bit(PG_double_map, &page[1].flags);
638 }
639
TestClearPageDoubleMap(struct page * page)640 static inline int TestClearPageDoubleMap(struct page *page)
641 {
642 VM_BUG_ON_PAGE(!PageHead(page), page);
643 return test_and_clear_bit(PG_double_map, &page[1].flags);
644 }
645
646 #else
647 TESTPAGEFLAG_FALSE(TransHuge)
648 TESTPAGEFLAG_FALSE(TransCompound)
649 TESTPAGEFLAG_FALSE(TransCompoundMap)
650 TESTPAGEFLAG_FALSE(TransTail)
651 PAGEFLAG_FALSE(DoubleMap)
652 TESTSETFLAG_FALSE(DoubleMap)
653 TESTCLEARFLAG_FALSE(DoubleMap)
654 #endif
655
656 /*
657 * For pages that are never mapped to userspace, page->mapcount may be
658 * used for storing extra information about page type. Any value used
659 * for this purpose must be <= -2, but it's better start not too close
660 * to -2 so that an underflow of the page_mapcount() won't be mistaken
661 * for a special page.
662 */
663 #define PAGE_MAPCOUNT_OPS(uname, lname) \
664 static __always_inline int Page##uname(struct page *page) \
665 { \
666 return atomic_read(&page->_mapcount) == \
667 PAGE_##lname##_MAPCOUNT_VALUE; \
668 } \
669 static __always_inline void __SetPage##uname(struct page *page) \
670 { \
671 VM_BUG_ON_PAGE(atomic_read(&page->_mapcount) != -1, page); \
672 atomic_set(&page->_mapcount, PAGE_##lname##_MAPCOUNT_VALUE); \
673 } \
674 static __always_inline void __ClearPage##uname(struct page *page) \
675 { \
676 VM_BUG_ON_PAGE(!Page##uname(page), page); \
677 atomic_set(&page->_mapcount, -1); \
678 }
679
680 /*
681 * PageBuddy() indicate that the page is free and in the buddy system
682 * (see mm/page_alloc.c).
683 */
684 #define PAGE_BUDDY_MAPCOUNT_VALUE (-128)
685 PAGE_MAPCOUNT_OPS(Buddy, BUDDY)
686
687 /*
688 * PageBalloon() is set on pages that are on the balloon page list
689 * (see mm/balloon_compaction.c).
690 */
691 #define PAGE_BALLOON_MAPCOUNT_VALUE (-256)
692 PAGE_MAPCOUNT_OPS(Balloon, BALLOON)
693
694 /*
695 * If kmemcg is enabled, the buddy allocator will set PageKmemcg() on
696 * pages allocated with __GFP_ACCOUNT. It gets cleared on page free.
697 */
698 #define PAGE_KMEMCG_MAPCOUNT_VALUE (-512)
699 PAGE_MAPCOUNT_OPS(Kmemcg, KMEMCG)
700
701 extern bool is_free_buddy_page(struct page *page);
702
703 __PAGEFLAG(Isolated, isolated, PF_ANY);
704
705 /*
706 * If network-based swap is enabled, sl*b must keep track of whether pages
707 * were allocated from pfmemalloc reserves.
708 */
PageSlabPfmemalloc(struct page * page)709 static inline int PageSlabPfmemalloc(struct page *page)
710 {
711 VM_BUG_ON_PAGE(!PageSlab(page), page);
712 return PageActive(page);
713 }
714
SetPageSlabPfmemalloc(struct page * page)715 static inline void SetPageSlabPfmemalloc(struct page *page)
716 {
717 VM_BUG_ON_PAGE(!PageSlab(page), page);
718 SetPageActive(page);
719 }
720
__ClearPageSlabPfmemalloc(struct page * page)721 static inline void __ClearPageSlabPfmemalloc(struct page *page)
722 {
723 VM_BUG_ON_PAGE(!PageSlab(page), page);
724 __ClearPageActive(page);
725 }
726
ClearPageSlabPfmemalloc(struct page * page)727 static inline void ClearPageSlabPfmemalloc(struct page *page)
728 {
729 VM_BUG_ON_PAGE(!PageSlab(page), page);
730 ClearPageActive(page);
731 }
732
733 #ifdef CONFIG_MMU
734 #define __PG_MLOCKED (1UL << PG_mlocked)
735 #else
736 #define __PG_MLOCKED 0
737 #endif
738
739 /*
740 * Flags checked when a page is freed. Pages being freed should not have
741 * these flags set. It they are, there is a problem.
742 */
743 #define PAGE_FLAGS_CHECK_AT_FREE \
744 (1UL << PG_lru | 1UL << PG_locked | \
745 1UL << PG_private | 1UL << PG_private_2 | \
746 1UL << PG_writeback | 1UL << PG_reserved | \
747 1UL << PG_slab | 1UL << PG_active | \
748 1UL << PG_unevictable | __PG_MLOCKED)
749
750 /*
751 * Flags checked when a page is prepped for return by the page allocator.
752 * Pages being prepped should not have these flags set. It they are set,
753 * there has been a kernel bug or struct page corruption.
754 *
755 * __PG_HWPOISON is exceptional because it needs to be kept beyond page's
756 * alloc-free cycle to prevent from reusing the page.
757 */
758 #define PAGE_FLAGS_CHECK_AT_PREP \
759 (((1UL << NR_PAGEFLAGS) - 1) & ~__PG_HWPOISON)
760
761 #define PAGE_FLAGS_PRIVATE \
762 (1UL << PG_private | 1UL << PG_private_2)
763 /**
764 * page_has_private - Determine if page has private stuff
765 * @page: The page to be checked
766 *
767 * Determine if a page has private stuff, indicating that release routines
768 * should be invoked upon it.
769 */
page_has_private(struct page * page)770 static inline int page_has_private(struct page *page)
771 {
772 return !!(page->flags & PAGE_FLAGS_PRIVATE);
773 }
774
775 #undef PF_ANY
776 #undef PF_HEAD
777 #undef PF_ONLY_HEAD
778 #undef PF_NO_TAIL
779 #undef PF_NO_COMPOUND
780 #endif /* !__GENERATING_BOUNDS_H */
781
782 #endif /* PAGE_FLAGS_H */
783