page-flags.h 24 KB

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