mm_inline.h 3.4 KB

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