page-flags.h 23 KB

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