mm_inline.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef LINUX_MM_INLINE_H
  3. #define LINUX_MM_INLINE_H
  4. #include <linux/huge_mm.h>
  5. #include <linux/swap.h>
  6. /**
  7. * page_is_file_cache - should the page be on a file LRU or anon LRU?
  8. * @page: the page to test
  9. *
  10. * Returns 1 if @page is page cache page backed by a regular filesystem,
  11. * or 0 if @page is anonymous, tmpfs or otherwise ram or swap backed.
  12. * Used by functions that manipulate the LRU lists, to sort a page
  13. * onto the right LRU list.
  14. *
  15. * We would like to get this info without a page flag, but the state
  16. * needs to survive until the page is last deleted from the LRU, which
  17. * could be as far down as __page_cache_release.
  18. */
  19. static inline int page_is_file_cache(struct page *page)
  20. {
  21. return !PageSwapBacked(page);
  22. }
  23. static __always_inline void __update_lru_size(struct lruvec *lruvec,
  24. enum lru_list lru, enum zone_type zid,
  25. int nr_pages)
  26. {
  27. struct pglist_data *pgdat = lruvec_pgdat(lruvec);
  28. __mod_node_page_state(pgdat, NR_LRU_BASE + lru, nr_pages);
  29. __mod_zone_page_state(&pgdat->node_zones[zid],
  30. NR_ZONE_LRU_BASE + lru, nr_pages);
  31. }
  32. static __always_inline void update_lru_size(struct lruvec *lruvec,
  33. enum lru_list lru, enum zone_type zid,
  34. int nr_pages)
  35. {
  36. __update_lru_size(lruvec, lru, zid, nr_pages);
  37. #ifdef CONFIG_MEMCG
  38. mem_cgroup_update_lru_size(lruvec, lru, zid, nr_pages);
  39. #endif
  40. }
  41. static __always_inline void add_page_to_lru_list(struct page *page,
  42. struct lruvec *lruvec, enum lru_list lru)
  43. {
  44. update_lru_size(lruvec, lru, page_zonenum(page), hpage_nr_pages(page));
  45. list_add(&page->lru, &lruvec->lists[lru]);
  46. }
  47. static __always_inline void add_page_to_lru_list_tail(struct page *page,
  48. struct lruvec *lruvec, enum lru_list lru)
  49. {
  50. update_lru_size(lruvec, lru, page_zonenum(page), hpage_nr_pages(page));
  51. list_add_tail(&page->lru, &lruvec->lists[lru]);
  52. }
  53. static __always_inline void del_page_from_lru_list(struct page *page,
  54. struct lruvec *lruvec, enum lru_list lru)
  55. {
  56. list_del(&page->lru);
  57. update_lru_size(lruvec, lru, page_zonenum(page), -hpage_nr_pages(page));
  58. }
  59. /**
  60. * page_lru_base_type - which LRU list type should a page be on?
  61. * @page: the page to test
  62. *
  63. * Used for LRU list index arithmetic.
  64. *
  65. * Returns the base LRU type - file or anon - @page should be on.
  66. */
  67. static inline enum lru_list page_lru_base_type(struct page *page)
  68. {
  69. if (page_is_file_cache(page))
  70. return LRU_INACTIVE_FILE;
  71. return LRU_INACTIVE_ANON;
  72. }
  73. /**
  74. * page_off_lru - which LRU list was page on? clearing its lru flags.
  75. * @page: the page to test
  76. *
  77. * Returns the LRU list a page was on, as an index into the array of LRU
  78. * lists; and clears its Unevictable or Active flags, ready for freeing.
  79. */
  80. static __always_inline enum lru_list page_off_lru(struct page *page)
  81. {
  82. enum lru_list lru;
  83. if (PageUnevictable(page)) {
  84. __ClearPageUnevictable(page);
  85. lru = LRU_UNEVICTABLE;
  86. } else {
  87. lru = page_lru_base_type(page);
  88. if (PageActive(page)) {
  89. __ClearPageActive(page);
  90. lru += LRU_ACTIVE;
  91. }
  92. }
  93. return lru;
  94. }
  95. /**
  96. * page_lru - which LRU list should a page be on?
  97. * @page: the page to test
  98. *
  99. * Returns the LRU list a page should be on, as an index
  100. * into the array of LRU lists.
  101. */
  102. static __always_inline enum lru_list page_lru(struct page *page)
  103. {
  104. enum lru_list lru;
  105. if (PageUnevictable(page))
  106. lru = LRU_UNEVICTABLE;
  107. else {
  108. lru = page_lru_base_type(page);
  109. if (PageActive(page))
  110. lru += LRU_ACTIVE;
  111. }
  112. return lru;
  113. }
  114. #define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
  115. #endif