page_idle.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <linux/init.h>
  2. #include <linux/bootmem.h>
  3. #include <linux/fs.h>
  4. #include <linux/sysfs.h>
  5. #include <linux/kobject.h>
  6. #include <linux/mm.h>
  7. #include <linux/mmzone.h>
  8. #include <linux/pagemap.h>
  9. #include <linux/rmap.h>
  10. #include <linux/mmu_notifier.h>
  11. #include <linux/page_ext.h>
  12. #include <linux/page_idle.h>
  13. #define BITMAP_CHUNK_SIZE sizeof(u64)
  14. #define BITMAP_CHUNK_BITS (BITMAP_CHUNK_SIZE * BITS_PER_BYTE)
  15. /*
  16. * Idle page tracking only considers user memory pages, for other types of
  17. * pages the idle flag is always unset and an attempt to set it is silently
  18. * ignored.
  19. *
  20. * We treat a page as a user memory page if it is on an LRU list, because it is
  21. * always safe to pass such a page to rmap_walk(), which is essential for idle
  22. * page tracking. With such an indicator of user pages we can skip isolated
  23. * pages, but since there are not usually many of them, it will hardly affect
  24. * the overall result.
  25. *
  26. * This function tries to get a user memory page by pfn as described above.
  27. */
  28. static struct page *page_idle_get_page(unsigned long pfn)
  29. {
  30. struct page *page;
  31. struct zone *zone;
  32. if (!pfn_valid(pfn))
  33. return NULL;
  34. page = pfn_to_page(pfn);
  35. if (!page || !PageLRU(page) ||
  36. !get_page_unless_zero(page))
  37. return NULL;
  38. zone = page_zone(page);
  39. spin_lock_irq(zone_lru_lock(zone));
  40. if (unlikely(!PageLRU(page))) {
  41. put_page(page);
  42. page = NULL;
  43. }
  44. spin_unlock_irq(zone_lru_lock(zone));
  45. return page;
  46. }
  47. static int page_idle_clear_pte_refs_one(struct page *page,
  48. struct vm_area_struct *vma,
  49. unsigned long addr, void *arg)
  50. {
  51. struct mm_struct *mm = vma->vm_mm;
  52. pmd_t *pmd;
  53. pte_t *pte;
  54. spinlock_t *ptl;
  55. bool referenced = false;
  56. if (!page_check_address_transhuge(page, mm, addr, &pmd, &pte, &ptl))
  57. return SWAP_AGAIN;
  58. if (pte) {
  59. referenced = ptep_clear_young_notify(vma, addr, pte);
  60. pte_unmap(pte);
  61. } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
  62. referenced = pmdp_clear_young_notify(vma, addr, pmd);
  63. } else {
  64. /* unexpected pmd-mapped page? */
  65. WARN_ON_ONCE(1);
  66. }
  67. spin_unlock(ptl);
  68. if (referenced) {
  69. clear_page_idle(page);
  70. /*
  71. * We cleared the referenced bit in a mapping to this page. To
  72. * avoid interference with page reclaim, mark it young so that
  73. * page_referenced() will return > 0.
  74. */
  75. set_page_young(page);
  76. }
  77. return SWAP_AGAIN;
  78. }
  79. static void page_idle_clear_pte_refs(struct page *page)
  80. {
  81. /*
  82. * Since rwc.arg is unused, rwc is effectively immutable, so we
  83. * can make it static const to save some cycles and stack.
  84. */
  85. static const struct rmap_walk_control rwc = {
  86. .rmap_one = page_idle_clear_pte_refs_one,
  87. .anon_lock = page_lock_anon_vma_read,
  88. };
  89. bool need_lock;
  90. if (!page_mapped(page) ||
  91. !page_rmapping(page))
  92. return;
  93. need_lock = !PageAnon(page) || PageKsm(page);
  94. if (need_lock && !trylock_page(page))
  95. return;
  96. rmap_walk(page, (struct rmap_walk_control *)&rwc);
  97. if (need_lock)
  98. unlock_page(page);
  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 page *page;
  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 = ALIGN(max_pfn, BITMAP_CHUNK_BITS);
  116. for (; pfn < end_pfn; pfn++) {
  117. bit = pfn % BITMAP_CHUNK_BITS;
  118. if (!bit)
  119. *out = 0ULL;
  120. page = page_idle_get_page(pfn);
  121. if (page) {
  122. if (page_is_idle(page)) {
  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(page);
  129. if (page_is_idle(page))
  130. *out |= 1ULL << bit;
  131. }
  132. put_page(page);
  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 page *page;
  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 = ALIGN(max_pfn, BITMAP_CHUNK_BITS);
  156. for (; pfn < end_pfn; pfn++) {
  157. bit = pfn % BITMAP_CHUNK_BITS;
  158. if ((*in >> bit) & 1) {
  159. page = page_idle_get_page(pfn);
  160. if (page) {
  161. page_idle_clear_pte_refs(page);
  162. set_page_idle(page);
  163. put_page(page);
  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, S_IRUSR | S_IWUSR,
  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 struct attribute_group page_idle_attr_group = {
  180. .bin_attrs = page_idle_bin_attrs,
  181. .name = "page_idle",
  182. };
  183. #ifndef CONFIG_64BIT
  184. static bool need_page_idle(void)
  185. {
  186. return true;
  187. }
  188. struct page_ext_operations page_idle_ops = {
  189. .need = need_page_idle,
  190. };
  191. #endif
  192. static int __init page_idle_init(void)
  193. {
  194. int err;
  195. err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
  196. if (err) {
  197. pr_err("page_idle: register sysfs failed\n");
  198. return err;
  199. }
  200. return 0;
  201. }
  202. subsys_initcall(page_idle_init);