page_idle.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/memblock.h>
  4. #include <linux/fs.h>
  5. #include <linux/sysfs.h>
  6. #include <linux/kobject.h>
  7. #include <linux/memory_hotplug.h>
  8. #include <linux/mm.h>
  9. #include <linux/mmzone.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/rmap.h>
  12. #include <linux/mmu_notifier.h>
  13. #include <linux/page_ext.h>
  14. #include <linux/page_idle.h>
  15. #include "internal.h"
  16. #define BITMAP_CHUNK_SIZE sizeof(u64)
  17. #define BITMAP_CHUNK_BITS (BITMAP_CHUNK_SIZE * BITS_PER_BYTE)
  18. /*
  19. * Idle page tracking only considers user memory pages, for other types of
  20. * pages the idle flag is always unset and an attempt to set it is silently
  21. * ignored.
  22. *
  23. * We treat a page as a user memory page if it is on an LRU list, because it is
  24. * always safe to pass such a page to rmap_walk(), which is essential for idle
  25. * page tracking. With such an indicator of user pages we can skip isolated
  26. * pages, but since there are not usually many of them, it will hardly affect
  27. * the overall result.
  28. *
  29. * This function tries to get a user memory page by pfn as described above.
  30. */
  31. static struct page *page_idle_get_page(unsigned long pfn)
  32. {
  33. struct page *page = pfn_to_online_page(pfn);
  34. if (!page || !PageLRU(page) ||
  35. !get_page_unless_zero(page))
  36. return NULL;
  37. if (unlikely(!PageLRU(page))) {
  38. put_page(page);
  39. page = NULL;
  40. }
  41. return page;
  42. }
  43. static bool page_idle_clear_pte_refs_one(struct folio *folio,
  44. struct vm_area_struct *vma,
  45. unsigned long addr, void *arg)
  46. {
  47. DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, addr, 0);
  48. bool referenced = false;
  49. while (page_vma_mapped_walk(&pvmw)) {
  50. addr = pvmw.address;
  51. if (pvmw.pte) {
  52. /*
  53. * For PTE-mapped THP, one sub page is referenced,
  54. * the whole THP is referenced.
  55. */
  56. if (ptep_clear_young_notify(vma, addr, pvmw.pte))
  57. referenced = true;
  58. } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
  59. if (pmdp_clear_young_notify(vma, addr, pvmw.pmd))
  60. referenced = true;
  61. } else {
  62. /* unexpected pmd-mapped page? */
  63. WARN_ON_ONCE(1);
  64. }
  65. }
  66. if (referenced) {
  67. folio_clear_idle(folio);
  68. /*
  69. * We cleared the referenced bit in a mapping to this page. To
  70. * avoid interference with page reclaim, mark it young so that
  71. * folio_referenced() will return > 0.
  72. */
  73. folio_set_young(folio);
  74. }
  75. return true;
  76. }
  77. static void page_idle_clear_pte_refs(struct page *page)
  78. {
  79. struct folio *folio = page_folio(page);
  80. /*
  81. * Since rwc.try_lock is unused, rwc is effectively immutable, so we
  82. * can make it static to save some cycles and stack.
  83. */
  84. static struct rmap_walk_control rwc = {
  85. .rmap_one = page_idle_clear_pte_refs_one,
  86. .anon_lock = folio_lock_anon_vma_read,
  87. };
  88. bool need_lock;
  89. if (!folio_mapped(folio) || !folio_raw_mapping(folio))
  90. return;
  91. need_lock = !folio_test_anon(folio) || folio_test_ksm(folio);
  92. if (need_lock && !folio_trylock(folio))
  93. return;
  94. rmap_walk(folio, &rwc);
  95. if (need_lock)
  96. folio_unlock(folio);
  97. }
  98. static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
  99. struct bin_attribute *attr, char *buf,
  100. loff_t pos, size_t count)
  101. {
  102. u64 *out = (u64 *)buf;
  103. struct page *page;
  104. unsigned long pfn, end_pfn;
  105. int bit;
  106. if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
  107. return -EINVAL;
  108. pfn = pos * BITS_PER_BYTE;
  109. if (pfn >= max_pfn)
  110. return 0;
  111. end_pfn = pfn + count * BITS_PER_BYTE;
  112. if (end_pfn > max_pfn)
  113. end_pfn = max_pfn;
  114. for (; pfn < end_pfn; pfn++) {
  115. bit = pfn % BITMAP_CHUNK_BITS;
  116. if (!bit)
  117. *out = 0ULL;
  118. page = page_idle_get_page(pfn);
  119. if (page) {
  120. if (page_is_idle(page)) {
  121. /*
  122. * The page might have been referenced via a
  123. * pte, in which case it is not idle. Clear
  124. * refs and recheck.
  125. */
  126. page_idle_clear_pte_refs(page);
  127. if (page_is_idle(page))
  128. *out |= 1ULL << bit;
  129. }
  130. put_page(page);
  131. }
  132. if (bit == BITMAP_CHUNK_BITS - 1)
  133. out++;
  134. cond_resched();
  135. }
  136. return (char *)out - buf;
  137. }
  138. static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
  139. struct bin_attribute *attr, char *buf,
  140. loff_t pos, size_t count)
  141. {
  142. const u64 *in = (u64 *)buf;
  143. struct page *page;
  144. unsigned long pfn, end_pfn;
  145. int bit;
  146. if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
  147. return -EINVAL;
  148. pfn = pos * BITS_PER_BYTE;
  149. if (pfn >= max_pfn)
  150. return -ENXIO;
  151. end_pfn = pfn + count * BITS_PER_BYTE;
  152. if (end_pfn > max_pfn)
  153. end_pfn = max_pfn;
  154. for (; pfn < end_pfn; pfn++) {
  155. bit = pfn % BITMAP_CHUNK_BITS;
  156. if ((*in >> bit) & 1) {
  157. page = page_idle_get_page(pfn);
  158. if (page) {
  159. page_idle_clear_pte_refs(page);
  160. set_page_idle(page);
  161. put_page(page);
  162. }
  163. }
  164. if (bit == BITMAP_CHUNK_BITS - 1)
  165. in++;
  166. cond_resched();
  167. }
  168. return (char *)in - buf;
  169. }
  170. static struct bin_attribute page_idle_bitmap_attr =
  171. __BIN_ATTR(bitmap, 0600,
  172. page_idle_bitmap_read, page_idle_bitmap_write, 0);
  173. static struct bin_attribute *page_idle_bin_attrs[] = {
  174. &page_idle_bitmap_attr,
  175. NULL,
  176. };
  177. static const struct attribute_group page_idle_attr_group = {
  178. .bin_attrs = page_idle_bin_attrs,
  179. .name = "page_idle",
  180. };
  181. static int __init page_idle_init(void)
  182. {
  183. int err;
  184. err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
  185. if (err) {
  186. pr_err("page_idle: register sysfs failed\n");
  187. return err;
  188. }
  189. return 0;
  190. }
  191. subsys_initcall(page_idle_init);