1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/bootmem.h>
5 #include <linux/sysfs.h>
6 #include <linux/kobject.h>
8 #include <linux/mmzone.h>
9 #include <linux/pagemap.h>
10 #include <linux/rmap.h>
11 #include <linux/mmu_notifier.h>
12 #include <linux/page_ext.h>
13 #include <linux/page_idle.h>
15 #define BITMAP_CHUNK_SIZE sizeof(u64)
16 #define BITMAP_CHUNK_BITS (BITMAP_CHUNK_SIZE * BITS_PER_BYTE)
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
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
29 * This function tries to get a user memory page by pfn as described above.
31 static struct page *page_idle_get_page(unsigned long pfn)
39 page = pfn_to_page(pfn);
40 if (!page || !PageLRU(page) ||
41 !get_page_unless_zero(page))
44 zone = page_zone(page);
45 spin_lock_irq(zone_lru_lock(zone));
46 if (unlikely(!PageLRU(page))) {
50 spin_unlock_irq(zone_lru_lock(zone));
54 static bool page_idle_clear_pte_refs_one(struct page *page,
55 struct vm_area_struct *vma,
56 unsigned long addr, void *arg)
58 struct page_vma_mapped_walk pvmw = {
63 bool referenced = false;
65 while (page_vma_mapped_walk(&pvmw)) {
69 * For PTE-mapped THP, one sub page is referenced,
70 * the whole THP is referenced.
72 if (ptep_clear_young_notify(vma, addr, pvmw.pte))
74 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
75 if (pmdp_clear_young_notify(vma, addr, pvmw.pmd))
78 /* unexpected pmd-mapped page? */
84 clear_page_idle(page);
86 * We cleared the referenced bit in a mapping to this page. To
87 * avoid interference with page reclaim, mark it young so that
88 * page_referenced() will return > 0.
95 static void page_idle_clear_pte_refs(struct page *page)
98 * Since rwc.arg is unused, rwc is effectively immutable, so we
99 * can make it static const to save some cycles and stack.
101 static const struct rmap_walk_control rwc = {
102 .rmap_one = page_idle_clear_pte_refs_one,
103 .anon_lock = page_lock_anon_vma_read,
107 if (!page_mapped(page) ||
108 !page_rmapping(page))
111 need_lock = !PageAnon(page) || PageKsm(page);
112 if (need_lock && !trylock_page(page))
115 rmap_walk(page, (struct rmap_walk_control *)&rwc);
121 static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
122 struct bin_attribute *attr, char *buf,
123 loff_t pos, size_t count)
125 u64 *out = (u64 *)buf;
127 unsigned long pfn, end_pfn;
130 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
133 pfn = pos * BITS_PER_BYTE;
137 end_pfn = pfn + count * BITS_PER_BYTE;
138 if (end_pfn > max_pfn)
139 end_pfn = ALIGN(max_pfn, BITMAP_CHUNK_BITS);
141 for (; pfn < end_pfn; pfn++) {
142 bit = pfn % BITMAP_CHUNK_BITS;
145 page = page_idle_get_page(pfn);
147 if (page_is_idle(page)) {
149 * The page might have been referenced via a
150 * pte, in which case it is not idle. Clear
153 page_idle_clear_pte_refs(page);
154 if (page_is_idle(page))
159 if (bit == BITMAP_CHUNK_BITS - 1)
163 return (char *)out - buf;
166 static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
167 struct bin_attribute *attr, char *buf,
168 loff_t pos, size_t count)
170 const u64 *in = (u64 *)buf;
172 unsigned long pfn, end_pfn;
175 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
178 pfn = pos * BITS_PER_BYTE;
182 end_pfn = pfn + count * BITS_PER_BYTE;
183 if (end_pfn > max_pfn)
184 end_pfn = ALIGN(max_pfn, BITMAP_CHUNK_BITS);
186 for (; pfn < end_pfn; pfn++) {
187 bit = pfn % BITMAP_CHUNK_BITS;
188 if ((*in >> bit) & 1) {
189 page = page_idle_get_page(pfn);
191 page_idle_clear_pte_refs(page);
196 if (bit == BITMAP_CHUNK_BITS - 1)
200 return (char *)in - buf;
203 static struct bin_attribute page_idle_bitmap_attr =
204 __BIN_ATTR(bitmap, 0600,
205 page_idle_bitmap_read, page_idle_bitmap_write, 0);
207 static struct bin_attribute *page_idle_bin_attrs[] = {
208 &page_idle_bitmap_attr,
212 static const struct attribute_group page_idle_attr_group = {
213 .bin_attrs = page_idle_bin_attrs,
218 static bool need_page_idle(void)
222 struct page_ext_operations page_idle_ops = {
223 .need = need_page_idle,
227 static int __init page_idle_init(void)
231 err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
233 pr_err("page_idle: register sysfs failed\n");
238 subsys_initcall(page_idle_init);