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