page_idle.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 folio *page_idle_get_folio(unsigned long pfn)
  32. {
  33. struct page *page = pfn_to_online_page(pfn);
  34. struct folio *folio;
  35. if (!page || PageTail(page))
  36. return NULL;
  37. folio = page_folio(page);
  38. if (!folio_test_lru(folio) || !folio_try_get(folio))
  39. return NULL;
  40. if (unlikely(page_folio(page) != folio || !folio_test_lru(folio))) {
  41. folio_put(folio);
  42. folio = NULL;
  43. }
  44. return folio;
  45. }
  46. static bool page_idle_clear_pte_refs_one(struct folio *folio,
  47. struct vm_area_struct *vma,
  48. unsigned long addr, void *arg)
  49. {
  50. DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, addr, 0);
  51. bool referenced = false;
  52. while (page_vma_mapped_walk(&pvmw)) {
  53. addr = pvmw.address;
  54. if (pvmw.pte) {
  55. /*
  56. * For PTE-mapped THP, one sub page is referenced,
  57. * the whole THP is referenced.
  58. */
  59. if (ptep_clear_young_notify(vma, addr, pvmw.pte))
  60. referenced = true;
  61. } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
  62. if (pmdp_clear_young_notify(vma, addr, pvmw.pmd))
  63. referenced = true;
  64. } else {
  65. /* unexpected pmd-mapped page? */
  66. WARN_ON_ONCE(1);
  67. }
  68. }
  69. if (referenced) {
  70. folio_clear_idle(folio);
  71. /*
  72. * We cleared the referenced bit in a mapping to this page. To
  73. * avoid interference with page reclaim, mark it young so that
  74. * folio_referenced() will return > 0.
  75. */
  76. folio_set_young(folio);
  77. }
  78. return true;
  79. }
  80. static void page_idle_clear_pte_refs(struct folio *folio)
  81. {
  82. /*
  83. * Since rwc.try_lock is unused, rwc is effectively immutable, so we
  84. * can make it static to save some cycles and stack.
  85. */
  86. static struct rmap_walk_control rwc = {
  87. .rmap_one = page_idle_clear_pte_refs_one,
  88. .anon_lock = folio_lock_anon_vma_read,
  89. };
  90. bool need_lock;
  91. if (!folio_mapped(folio) || !folio_raw_mapping(folio))
  92. return;
  93. need_lock = !folio_test_anon(folio) || folio_test_ksm(folio);
  94. if (need_lock && !folio_trylock(folio))
  95. return;
  96. rmap_walk(folio, &rwc);
  97. if (need_lock)
  98. folio_unlock(folio);
  99. }
  100. static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
  101. struct bin_attribute *attr, char *buf,
  102. loff_t pos, size_t count)
  103. {
  104. u64 *out = (u64 *)buf;
  105. struct folio *folio;
  106. unsigned long pfn, end_pfn;
  107. int bit;
  108. if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
  109. return -EINVAL;
  110. pfn = pos * BITS_PER_BYTE;
  111. if (pfn >= max_pfn)
  112. return 0;
  113. end_pfn = pfn + count * BITS_PER_BYTE;
  114. if (end_pfn > max_pfn)
  115. end_pfn = max_pfn;
  116. for (; pfn < end_pfn; pfn++) {
  117. bit = pfn % BITMAP_CHUNK_BITS;
  118. if (!bit)
  119. *out = 0ULL;
  120. folio = page_idle_get_folio(pfn);
  121. if (folio) {
  122. if (folio_test_idle(folio)) {
  123. /*
  124. * The page might have been referenced via a
  125. * pte, in which case it is not idle. Clear
  126. * refs and recheck.
  127. */
  128. page_idle_clear_pte_refs(folio);
  129. if (folio_test_idle(folio))
  130. *out |= 1ULL << bit;
  131. }
  132. folio_put(folio);
  133. }
  134. if (bit == BITMAP_CHUNK_BITS - 1)
  135. out++;
  136. cond_resched();
  137. }
  138. return (char *)out - buf;
  139. }
  140. static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
  141. struct bin_attribute *attr, char *buf,
  142. loff_t pos, size_t count)
  143. {
  144. const u64 *in = (u64 *)buf;
  145. struct folio *folio;
  146. unsigned long pfn, end_pfn;
  147. int bit;
  148. if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
  149. return -EINVAL;
  150. pfn = pos * BITS_PER_BYTE;
  151. if (pfn >= max_pfn)
  152. return -ENXIO;
  153. end_pfn = pfn + count * BITS_PER_BYTE;
  154. if (end_pfn > max_pfn)
  155. end_pfn = max_pfn;
  156. for (; pfn < end_pfn; pfn++) {
  157. bit = pfn % BITMAP_CHUNK_BITS;
  158. if ((*in >> bit) & 1) {
  159. folio = page_idle_get_folio(pfn);
  160. if (folio) {
  161. page_idle_clear_pte_refs(folio);
  162. folio_set_idle(folio);
  163. folio_put(folio);
  164. }
  165. }
  166. if (bit == BITMAP_CHUNK_BITS - 1)
  167. in++;
  168. cond_resched();
  169. }
  170. return (char *)in - buf;
  171. }
  172. static struct bin_attribute page_idle_bitmap_attr =
  173. __BIN_ATTR(bitmap, 0600,
  174. page_idle_bitmap_read, page_idle_bitmap_write, 0);
  175. static struct bin_attribute *page_idle_bin_attrs[] = {
  176. &page_idle_bitmap_attr,
  177. NULL,
  178. };
  179. static const struct attribute_group page_idle_attr_group = {
  180. .bin_attrs = page_idle_bin_attrs,
  181. .name = "page_idle",
  182. };
  183. static int __init page_idle_init(void)
  184. {
  185. int err;
  186. err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
  187. if (err) {
  188. pr_err("page_idle: register sysfs failed\n");
  189. return err;
  190. }
  191. return 0;
  192. }
  193. subsys_initcall(page_idle_init);