page_idle.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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);
  40. if (unlikely(!PageLRU(page))) {
  41. put_page(page);
  42. page = NULL;
  43. }
  44. spin_unlock_irq(&zone->lru_lock);
  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. spinlock_t *ptl;
  53. pmd_t *pmd;
  54. pte_t *pte;
  55. bool referenced = false;
  56. if (unlikely(PageTransHuge(page))) {
  57. pmd = page_check_address_pmd(page, mm, addr,
  58. PAGE_CHECK_ADDRESS_PMD_FLAG, &ptl);
  59. if (pmd) {
  60. referenced = pmdp_clear_young_notify(vma, addr, pmd);
  61. spin_unlock(ptl);
  62. }
  63. } else {
  64. pte = page_check_address(page, mm, addr, &ptl, 0);
  65. if (pte) {
  66. referenced = ptep_clear_young_notify(vma, addr, pte);
  67. pte_unmap_unlock(pte, ptl);
  68. }
  69. }
  70. if (referenced) {
  71. clear_page_idle(page);
  72. /*
  73. * We cleared the referenced bit in a mapping to this page. To
  74. * avoid interference with page reclaim, mark it young so that
  75. * page_referenced() will return > 0.
  76. */
  77. set_page_young(page);
  78. }
  79. return SWAP_AGAIN;
  80. }
  81. static void page_idle_clear_pte_refs(struct page *page)
  82. {
  83. /*
  84. * Since rwc.arg is unused, rwc is effectively immutable, so we
  85. * can make it static const to save some cycles and stack.
  86. */
  87. static const struct rmap_walk_control rwc = {
  88. .rmap_one = page_idle_clear_pte_refs_one,
  89. .anon_lock = page_lock_anon_vma_read,
  90. };
  91. bool need_lock;
  92. if (!page_mapped(page) ||
  93. !page_rmapping(page))
  94. return;
  95. need_lock = !PageAnon(page) || PageKsm(page);
  96. if (need_lock && !trylock_page(page))
  97. return;
  98. rmap_walk(page, (struct rmap_walk_control *)&rwc);
  99. if (need_lock)
  100. unlock_page(page);
  101. }
  102. static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
  103. struct bin_attribute *attr, char *buf,
  104. loff_t pos, size_t count)
  105. {
  106. u64 *out = (u64 *)buf;
  107. struct page *page;
  108. unsigned long pfn, end_pfn;
  109. int bit;
  110. if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
  111. return -EINVAL;
  112. pfn = pos * BITS_PER_BYTE;
  113. if (pfn >= max_pfn)
  114. return 0;
  115. end_pfn = pfn + count * BITS_PER_BYTE;
  116. if (end_pfn > max_pfn)
  117. end_pfn = ALIGN(max_pfn, BITMAP_CHUNK_BITS);
  118. for (; pfn < end_pfn; pfn++) {
  119. bit = pfn % BITMAP_CHUNK_BITS;
  120. if (!bit)
  121. *out = 0ULL;
  122. page = page_idle_get_page(pfn);
  123. if (page) {
  124. if (page_is_idle(page)) {
  125. /*
  126. * The page might have been referenced via a
  127. * pte, in which case it is not idle. Clear
  128. * refs and recheck.
  129. */
  130. page_idle_clear_pte_refs(page);
  131. if (page_is_idle(page))
  132. *out |= 1ULL << bit;
  133. }
  134. put_page(page);
  135. }
  136. if (bit == BITMAP_CHUNK_BITS - 1)
  137. out++;
  138. cond_resched();
  139. }
  140. return (char *)out - buf;
  141. }
  142. static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
  143. struct bin_attribute *attr, char *buf,
  144. loff_t pos, size_t count)
  145. {
  146. const u64 *in = (u64 *)buf;
  147. struct page *page;
  148. unsigned long pfn, end_pfn;
  149. int bit;
  150. if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
  151. return -EINVAL;
  152. pfn = pos * BITS_PER_BYTE;
  153. if (pfn >= max_pfn)
  154. return -ENXIO;
  155. end_pfn = pfn + count * BITS_PER_BYTE;
  156. if (end_pfn > max_pfn)
  157. end_pfn = ALIGN(max_pfn, BITMAP_CHUNK_BITS);
  158. for (; pfn < end_pfn; pfn++) {
  159. bit = pfn % BITMAP_CHUNK_BITS;
  160. if ((*in >> bit) & 1) {
  161. page = page_idle_get_page(pfn);
  162. if (page) {
  163. page_idle_clear_pte_refs(page);
  164. set_page_idle(page);
  165. put_page(page);
  166. }
  167. }
  168. if (bit == BITMAP_CHUNK_BITS - 1)
  169. in++;
  170. cond_resched();
  171. }
  172. return (char *)in - buf;
  173. }
  174. static struct bin_attribute page_idle_bitmap_attr =
  175. __BIN_ATTR(bitmap, S_IRUSR | S_IWUSR,
  176. page_idle_bitmap_read, page_idle_bitmap_write, 0);
  177. static struct bin_attribute *page_idle_bin_attrs[] = {
  178. &page_idle_bitmap_attr,
  179. NULL,
  180. };
  181. static struct attribute_group page_idle_attr_group = {
  182. .bin_attrs = page_idle_bin_attrs,
  183. .name = "page_idle",
  184. };
  185. #ifndef CONFIG_64BIT
  186. static bool need_page_idle(void)
  187. {
  188. return true;
  189. }
  190. struct page_ext_operations page_idle_ops = {
  191. .need = need_page_idle,
  192. };
  193. #endif
  194. static int __init page_idle_init(void)
  195. {
  196. int err;
  197. err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
  198. if (err) {
  199. pr_err("page_idle: register sysfs failed\n");
  200. return err;
  201. }
  202. return 0;
  203. }
  204. subsys_initcall(page_idle_init);