1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/memblock.h>
5 #include <linux/sysfs.h>
6 #include <linux/kobject.h>
7 #include <linux/memory_hotplug.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>
16 #define BITMAP_CHUNK_SIZE sizeof(u64)
17 #define BITMAP_CHUNK_BITS (BITMAP_CHUNK_SIZE * BITS_PER_BYTE)
20 * Idle page tracking only considers user memory pages, for other types of
21 * pages the idle flag is always unset and an attempt to set it is silently
24 * We treat a page as a user memory page if it is on an LRU list, because it is
25 * always safe to pass such a page to rmap_walk(), which is essential for idle
26 * page tracking. With such an indicator of user pages we can skip isolated
27 * pages, but since there are not usually many of them, it will hardly affect
30 * This function tries to get a user memory page by pfn as described above.
32 static struct page *page_idle_get_page(unsigned long pfn)
34 struct page *page = pfn_to_online_page(pfn);
37 if (!page || !PageLRU(page) ||
38 !get_page_unless_zero(page))
41 pgdat = page_pgdat(page);
42 spin_lock_irq(&pgdat->lru_lock);
43 if (unlikely(!PageLRU(page))) {
47 spin_unlock_irq(&pgdat->lru_lock);
51 static bool page_idle_clear_pte_refs_one(struct page *page,
52 struct vm_area_struct *vma,
53 unsigned long addr, void *arg)
55 struct page_vma_mapped_walk pvmw = {
60 bool referenced = false;
62 while (page_vma_mapped_walk(&pvmw)) {
66 * For PTE-mapped THP, one sub page is referenced,
67 * the whole THP is referenced.
69 if (ptep_clear_young_notify(vma, addr, pvmw.pte))
71 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
72 if (pmdp_clear_young_notify(vma, addr, pvmw.pmd))
75 /* unexpected pmd-mapped page? */
81 clear_page_idle(page);
83 * We cleared the referenced bit in a mapping to this page. To
84 * avoid interference with page reclaim, mark it young so that
85 * page_referenced() will return > 0.
92 static void page_idle_clear_pte_refs(struct page *page)
95 * Since rwc.arg is unused, rwc is effectively immutable, so we
96 * can make it static const to save some cycles and stack.
98 static const struct rmap_walk_control rwc = {
99 .rmap_one = page_idle_clear_pte_refs_one,
100 .anon_lock = page_lock_anon_vma_read,
104 if (!page_mapped(page) ||
105 !page_rmapping(page))
108 need_lock = !PageAnon(page) || PageKsm(page);
109 if (need_lock && !trylock_page(page))
112 rmap_walk(page, (struct rmap_walk_control *)&rwc);
118 static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
119 struct bin_attribute *attr, char *buf,
120 loff_t pos, size_t count)
122 u64 *out = (u64 *)buf;
124 unsigned long pfn, end_pfn;
127 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
130 pfn = pos * BITS_PER_BYTE;
134 end_pfn = pfn + count * BITS_PER_BYTE;
135 if (end_pfn > max_pfn)
138 for (; pfn < end_pfn; pfn++) {
139 bit = pfn % BITMAP_CHUNK_BITS;
142 page = page_idle_get_page(pfn);
144 if (page_is_idle(page)) {
146 * The page might have been referenced via a
147 * pte, in which case it is not idle. Clear
150 page_idle_clear_pte_refs(page);
151 if (page_is_idle(page))
156 if (bit == BITMAP_CHUNK_BITS - 1)
160 return (char *)out - buf;
163 static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
164 struct bin_attribute *attr, char *buf,
165 loff_t pos, size_t count)
167 const u64 *in = (u64 *)buf;
169 unsigned long pfn, end_pfn;
172 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
175 pfn = pos * BITS_PER_BYTE;
179 end_pfn = pfn + count * BITS_PER_BYTE;
180 if (end_pfn > max_pfn)
183 for (; pfn < end_pfn; pfn++) {
184 bit = pfn % BITMAP_CHUNK_BITS;
185 if ((*in >> bit) & 1) {
186 page = page_idle_get_page(pfn);
188 page_idle_clear_pte_refs(page);
193 if (bit == BITMAP_CHUNK_BITS - 1)
197 return (char *)in - buf;
200 static struct bin_attribute page_idle_bitmap_attr =
201 __BIN_ATTR(bitmap, 0600,
202 page_idle_bitmap_read, page_idle_bitmap_write, 0);
204 static struct bin_attribute *page_idle_bin_attrs[] = {
205 &page_idle_bitmap_attr,
209 static const struct attribute_group page_idle_attr_group = {
210 .bin_attrs = page_idle_bin_attrs,
215 static bool need_page_idle(void)
219 struct page_ext_operations page_idle_ops = {
220 .need = need_page_idle,
224 static int __init page_idle_init(void)
228 err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
230 pr_err("page_idle: register sysfs failed\n");
235 subsys_initcall(page_idle_init);