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 16 #include "internal.h" 17 18 #define BITMAP_CHUNK_SIZE sizeof(u64) 19 #define BITMAP_CHUNK_BITS (BITMAP_CHUNK_SIZE * BITS_PER_BYTE) 20 21 /* 22 * Idle page tracking only considers user memory pages, for other types of 23 * pages the idle flag is always unset and an attempt to set it is silently 24 * ignored. 25 * 26 * We treat a page as a user memory page if it is on an LRU list, because it is 27 * always safe to pass such a page to rmap_walk(), which is essential for idle 28 * page tracking. With such an indicator of user pages we can skip isolated 29 * pages, but since there are not usually many of them, it will hardly affect 30 * the overall result. 31 * 32 * This function tries to get a user memory page by pfn as described above. 33 */ 34 static struct folio *page_idle_get_folio(unsigned long pfn) 35 { 36 struct page *page = pfn_to_online_page(pfn); 37 struct folio *folio; 38 39 if (!page || PageTail(page)) 40 return NULL; 41 42 folio = page_folio(page); 43 if (!folio_test_lru(folio) || !folio_try_get(folio)) 44 return NULL; 45 if (unlikely(page_folio(page) != folio || !folio_test_lru(folio))) { 46 folio_put(folio); 47 folio = NULL; 48 } 49 return folio; 50 } 51 52 static bool page_idle_clear_pte_refs_one(struct folio *folio, 53 struct vm_area_struct *vma, 54 unsigned long addr, void *arg) 55 { 56 DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, addr, 0); 57 bool referenced = false; 58 59 while (page_vma_mapped_walk(&pvmw)) { 60 addr = pvmw.address; 61 if (pvmw.pte) { 62 /* 63 * For PTE-mapped THP, one sub page is referenced, 64 * the whole THP is referenced. 65 * 66 * PFN swap PTEs, such as device-exclusive ones, that 67 * actually map pages are "old" from a CPU perspective. 68 * The MMU notifier takes care of any device aspects. 69 */ 70 if (likely(pte_present(ptep_get(pvmw.pte)))) 71 referenced |= ptep_test_and_clear_young(vma, addr, pvmw.pte); 72 referenced |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + PAGE_SIZE); 73 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { 74 if (pmdp_clear_young_notify(vma, addr, pvmw.pmd)) 75 referenced = true; 76 } else { 77 /* unexpected pmd-mapped page? */ 78 WARN_ON_ONCE(1); 79 } 80 } 81 82 if (referenced) { 83 folio_clear_idle(folio); 84 /* 85 * We cleared the referenced bit in a mapping to this page. To 86 * avoid interference with page reclaim, mark it young so that 87 * folio_referenced() will return > 0. 88 */ 89 folio_set_young(folio); 90 } 91 return true; 92 } 93 94 static void page_idle_clear_pte_refs(struct folio *folio) 95 { 96 /* 97 * Since rwc.try_lock is unused, rwc is effectively immutable, so we 98 * can make it static to save some cycles and stack. 99 */ 100 static struct rmap_walk_control rwc = { 101 .rmap_one = page_idle_clear_pte_refs_one, 102 .anon_lock = folio_lock_anon_vma_read, 103 }; 104 105 if (!folio_mapped(folio) || !folio_raw_mapping(folio)) 106 return; 107 108 if (!folio_trylock(folio)) 109 return; 110 111 rmap_walk(folio, &rwc); 112 folio_unlock(folio); 113 } 114 115 static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj, 116 const struct bin_attribute *attr, char *buf, 117 loff_t pos, size_t count) 118 { 119 u64 *out = (u64 *)buf; 120 struct folio *folio; 121 unsigned long pfn, end_pfn; 122 int bit; 123 124 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE) 125 return -EINVAL; 126 127 pfn = pos * BITS_PER_BYTE; 128 if (pfn >= max_pfn) 129 return 0; 130 131 end_pfn = pfn + count * BITS_PER_BYTE; 132 if (end_pfn > max_pfn) 133 end_pfn = max_pfn; 134 135 for (; pfn < end_pfn; pfn++) { 136 bit = pfn % BITMAP_CHUNK_BITS; 137 if (!bit) 138 *out = 0ULL; 139 folio = page_idle_get_folio(pfn); 140 if (folio) { 141 if (folio_test_idle(folio)) { 142 /* 143 * The page might have been referenced via a 144 * pte, in which case it is not idle. Clear 145 * refs and recheck. 146 */ 147 page_idle_clear_pte_refs(folio); 148 if (folio_test_idle(folio)) 149 *out |= 1ULL << bit; 150 } 151 folio_put(folio); 152 } 153 if (bit == BITMAP_CHUNK_BITS - 1) 154 out++; 155 cond_resched(); 156 } 157 return (char *)out - buf; 158 } 159 160 static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj, 161 const struct bin_attribute *attr, char *buf, 162 loff_t pos, size_t count) 163 { 164 const u64 *in = (u64 *)buf; 165 struct folio *folio; 166 unsigned long pfn, end_pfn; 167 int bit; 168 169 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE) 170 return -EINVAL; 171 172 pfn = pos * BITS_PER_BYTE; 173 if (pfn >= max_pfn) 174 return -ENXIO; 175 176 end_pfn = pfn + count * BITS_PER_BYTE; 177 if (end_pfn > max_pfn) 178 end_pfn = max_pfn; 179 180 for (; pfn < end_pfn; pfn++) { 181 bit = pfn % BITMAP_CHUNK_BITS; 182 if ((*in >> bit) & 1) { 183 folio = page_idle_get_folio(pfn); 184 if (folio) { 185 page_idle_clear_pte_refs(folio); 186 folio_set_idle(folio); 187 folio_put(folio); 188 } 189 } 190 if (bit == BITMAP_CHUNK_BITS - 1) 191 in++; 192 cond_resched(); 193 } 194 return (char *)in - buf; 195 } 196 197 static const struct bin_attribute page_idle_bitmap_attr = 198 __BIN_ATTR(bitmap, 0600, 199 page_idle_bitmap_read, page_idle_bitmap_write, 0); 200 201 static const struct bin_attribute *const page_idle_bin_attrs[] = { 202 &page_idle_bitmap_attr, 203 NULL, 204 }; 205 206 static const struct attribute_group page_idle_attr_group = { 207 .bin_attrs = page_idle_bin_attrs, 208 .name = "page_idle", 209 }; 210 211 static int __init page_idle_init(void) 212 { 213 int err; 214 215 err = sysfs_create_group(mm_kobj, &page_idle_attr_group); 216 if (err) { 217 pr_err("page_idle: register sysfs failed\n"); 218 return err; 219 } 220 return 0; 221 } 222 subsys_initcall(page_idle_init); 223