page-flags.h 28 KB

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