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