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 */
page_idle_get_folio(unsigned long pfn)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
page_idle_clear_pte_refs_one(struct folio * folio,struct vm_area_struct * vma,unsigned long addr,void * arg)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 pmd_t pmdval = pmdp_get(pvmw.pmd);
75
76 if (likely(pmd_present(pmdval)))
77 referenced |= pmdp_clear_young_notify(vma, addr, pvmw.pmd);
78 referenced |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + PMD_SIZE);
79 } else {
80 /* unexpected pmd-mapped page? */
81 WARN_ON_ONCE(1);
82 }
83 }
84
85 if (referenced) {
86 folio_clear_idle(folio);
87 /*
88 * We cleared the referenced bit in a mapping to this page. To
89 * avoid interference with page reclaim, mark it young so that
90 * folio_referenced() will return > 0.
91 */
92 folio_set_young(folio);
93 }
94 return true;
95 }
96
page_idle_clear_pte_refs(struct folio * folio)97 static void page_idle_clear_pte_refs(struct folio *folio)
98 {
99 /*
100 * Since rwc.try_lock is unused, rwc is effectively immutable, so we
101 * can make it static to save some cycles and stack.
102 */
103 static struct rmap_walk_control rwc = {
104 .rmap_one = page_idle_clear_pte_refs_one,
105 .anon_lock = folio_lock_anon_vma_read,
106 };
107
108 if (!folio_mapped(folio) || !folio_raw_mapping(folio))
109 return;
110
111 if (!folio_trylock(folio))
112 return;
113
114 rmap_walk(folio, &rwc);
115 folio_unlock(folio);
116 }
117
page_idle_bitmap_read(struct file * file,struct kobject * kobj,const struct bin_attribute * attr,char * buf,loff_t pos,size_t count)118 static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
119 const struct bin_attribute *attr, char *buf,
120 loff_t pos, size_t count)
121 {
122 u64 *out = (u64 *)buf;
123 struct folio *folio;
124 unsigned long pfn, end_pfn;
125 int bit;
126
127 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
128 return -EINVAL;
129
130 pfn = pos * BITS_PER_BYTE;
131 if (pfn >= max_pfn)
132 return 0;
133
134 end_pfn = pfn + count * BITS_PER_BYTE;
135 if (end_pfn > max_pfn)
136 end_pfn = max_pfn;
137
138 for (; pfn < end_pfn; pfn++) {
139 bit = pfn % BITMAP_CHUNK_BITS;
140 if (!bit)
141 *out = 0ULL;
142 folio = page_idle_get_folio(pfn);
143 if (folio) {
144 if (folio_test_idle(folio)) {
145 /*
146 * The page might have been referenced via a
147 * pte, in which case it is not idle. Clear
148 * refs and recheck.
149 */
150 page_idle_clear_pte_refs(folio);
151 if (folio_test_idle(folio))
152 *out |= 1ULL << bit;
153 }
154 folio_put(folio);
155 }
156 if (bit == BITMAP_CHUNK_BITS - 1)
157 out++;
158 cond_resched();
159 }
160 return (char *)out - buf;
161 }
162
page_idle_bitmap_write(struct file * file,struct kobject * kobj,const struct bin_attribute * attr,char * buf,loff_t pos,size_t count)163 static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
164 const struct bin_attribute *attr, char *buf,
165 loff_t pos, size_t count)
166 {
167 const u64 *in = (u64 *)buf;
168 struct folio *folio;
169 unsigned long pfn, end_pfn;
170 int bit;
171
172 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
173 return -EINVAL;
174
175 pfn = pos * BITS_PER_BYTE;
176 if (pfn >= max_pfn)
177 return -ENXIO;
178
179 end_pfn = pfn + count * BITS_PER_BYTE;
180 if (end_pfn > max_pfn)
181 end_pfn = max_pfn;
182
183 for (; pfn < end_pfn; pfn++) {
184 bit = pfn % BITMAP_CHUNK_BITS;
185 if ((*in >> bit) & 1) {
186 folio = page_idle_get_folio(pfn);
187 if (folio) {
188 page_idle_clear_pte_refs(folio);
189 folio_set_idle(folio);
190 folio_put(folio);
191 }
192 }
193 if (bit == BITMAP_CHUNK_BITS - 1)
194 in++;
195 cond_resched();
196 }
197 return (char *)in - buf;
198 }
199
200 static const struct bin_attribute page_idle_bitmap_attr =
201 __BIN_ATTR(bitmap, 0600,
202 page_idle_bitmap_read, page_idle_bitmap_write, 0);
203
204 static const struct bin_attribute *const page_idle_bin_attrs[] = {
205 &page_idle_bitmap_attr,
206 NULL,
207 };
208
209 static const struct attribute_group page_idle_attr_group = {
210 .bin_attrs = page_idle_bin_attrs,
211 .name = "page_idle",
212 };
213
page_idle_init(void)214 static int __init page_idle_init(void)
215 {
216 int err;
217
218 err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
219 if (err) {
220 pr_err("page_idle: register sysfs failed\n");
221 return err;
222 }
223 return 0;
224 }
225 subsys_initcall(page_idle_init);
226