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>
18 #define BITMAP_CHUNK_SIZE sizeof(u64)
19 #define BITMAP_CHUNK_BITS (BITMAP_CHUNK_SIZE * BITS_PER_BYTE)
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
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
32 * This function tries to get a user memory page by pfn as described above.
34 static struct folio *page_idle_get_folio(unsigned long pfn)
36 struct page *page = pfn_to_online_page(pfn);
39 if (!page || PageTail(page))
42 folio = page_folio(page);
43 if (!folio_test_lru(folio) || !folio_try_get(folio))
45 if (unlikely(page_folio(page) != folio || !folio_test_lru(folio))) {
52 static bool page_idle_clear_pte_refs_one(struct folio *folio,
53 struct vm_area_struct *vma,
54 unsigned long addr, void *arg)
56 DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, addr, 0);
57 bool referenced = false;
59 while (page_vma_mapped_walk(&pvmw)) {
63 * For PTE-mapped THP, one sub page is referenced,
64 * the whole THP is referenced.
66 if (ptep_clear_young_notify(vma, addr, pvmw.pte))
68 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
69 if (pmdp_clear_young_notify(vma, addr, pvmw.pmd))
72 /* unexpected pmd-mapped page? */
78 folio_clear_idle(folio);
80 * We cleared the referenced bit in a mapping to this page. To
81 * avoid interference with page reclaim, mark it young so that
82 * folio_referenced() will return > 0.
84 folio_set_young(folio);
89 static void page_idle_clear_pte_refs(struct folio *folio)
92 * Since rwc.try_lock is unused, rwc is effectively immutable, so we
93 * can make it static to save some cycles and stack.
95 static struct rmap_walk_control rwc = {
96 .rmap_one = page_idle_clear_pte_refs_one,
97 .anon_lock = folio_lock_anon_vma_read,
101 if (!folio_mapped(folio) || !folio_raw_mapping(folio))
104 need_lock = !folio_test_anon(folio) || folio_test_ksm(folio);
105 if (need_lock && !folio_trylock(folio))
108 rmap_walk(folio, &rwc);
114 static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
115 struct bin_attribute *attr, char *buf,
116 loff_t pos, size_t count)
118 u64 *out = (u64 *)buf;
120 unsigned long pfn, end_pfn;
123 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
126 pfn = pos * BITS_PER_BYTE;
130 end_pfn = pfn + count * BITS_PER_BYTE;
131 if (end_pfn > max_pfn)
134 for (; pfn < end_pfn; pfn++) {
135 bit = pfn % BITMAP_CHUNK_BITS;
138 folio = page_idle_get_folio(pfn);
140 if (folio_test_idle(folio)) {
142 * The page might have been referenced via a
143 * pte, in which case it is not idle. Clear
146 page_idle_clear_pte_refs(folio);
147 if (folio_test_idle(folio))
152 if (bit == BITMAP_CHUNK_BITS - 1)
156 return (char *)out - buf;
159 static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
160 struct bin_attribute *attr, char *buf,
161 loff_t pos, size_t count)
163 const u64 *in = (u64 *)buf;
165 unsigned long pfn, end_pfn;
168 if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
171 pfn = pos * BITS_PER_BYTE;
175 end_pfn = pfn + count * BITS_PER_BYTE;
176 if (end_pfn > max_pfn)
179 for (; pfn < end_pfn; pfn++) {
180 bit = pfn % BITMAP_CHUNK_BITS;
181 if ((*in >> bit) & 1) {
182 folio = page_idle_get_folio(pfn);
184 page_idle_clear_pte_refs(folio);
185 folio_set_idle(folio);
189 if (bit == BITMAP_CHUNK_BITS - 1)
193 return (char *)in - buf;
196 static struct bin_attribute page_idle_bitmap_attr =
197 __BIN_ATTR(bitmap, 0600,
198 page_idle_bitmap_read, page_idle_bitmap_write, 0);
200 static struct bin_attribute *page_idle_bin_attrs[] = {
201 &page_idle_bitmap_attr,
205 static const struct attribute_group page_idle_attr_group = {
206 .bin_attrs = page_idle_bin_attrs,
210 static int __init page_idle_init(void)
214 err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
216 pr_err("page_idle: register sysfs failed\n");
221 subsys_initcall(page_idle_init);