xref: /openbmc/linux/mm/page_idle.c (revision ba936421)
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 page *page_idle_get_page(unsigned long pfn)
35 {
36 	struct page *page = pfn_to_online_page(pfn);
37 
38 	if (!page || !PageLRU(page) ||
39 	    !get_page_unless_zero(page))
40 		return NULL;
41 
42 	if (unlikely(!PageLRU(page))) {
43 		put_page(page);
44 		page = NULL;
45 	}
46 	return page;
47 }
48 
49 static bool page_idle_clear_pte_refs_one(struct folio *folio,
50 					struct vm_area_struct *vma,
51 					unsigned long addr, void *arg)
52 {
53 	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, addr, 0);
54 	bool referenced = false;
55 
56 	while (page_vma_mapped_walk(&pvmw)) {
57 		addr = pvmw.address;
58 		if (pvmw.pte) {
59 			/*
60 			 * For PTE-mapped THP, one sub page is referenced,
61 			 * the whole THP is referenced.
62 			 */
63 			if (ptep_clear_young_notify(vma, addr, pvmw.pte))
64 				referenced = true;
65 		} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
66 			if (pmdp_clear_young_notify(vma, addr, pvmw.pmd))
67 				referenced = true;
68 		} else {
69 			/* unexpected pmd-mapped page? */
70 			WARN_ON_ONCE(1);
71 		}
72 	}
73 
74 	if (referenced) {
75 		folio_clear_idle(folio);
76 		/*
77 		 * We cleared the referenced bit in a mapping to this page. To
78 		 * avoid interference with page reclaim, mark it young so that
79 		 * folio_referenced() will return > 0.
80 		 */
81 		folio_set_young(folio);
82 	}
83 	return true;
84 }
85 
86 static void page_idle_clear_pte_refs(struct page *page)
87 {
88 	struct folio *folio = page_folio(page);
89 	/*
90 	 * Since rwc.arg is unused, rwc is effectively immutable, so we
91 	 * can make it static const to save some cycles and stack.
92 	 */
93 	static const struct rmap_walk_control rwc = {
94 		.rmap_one = page_idle_clear_pte_refs_one,
95 		.anon_lock = folio_lock_anon_vma_read,
96 	};
97 	bool need_lock;
98 
99 	if (!folio_mapped(folio) || !folio_raw_mapping(folio))
100 		return;
101 
102 	need_lock = !folio_test_anon(folio) || folio_test_ksm(folio);
103 	if (need_lock && !folio_trylock(folio))
104 		return;
105 
106 	rmap_walk(folio, &rwc);
107 
108 	if (need_lock)
109 		folio_unlock(folio);
110 }
111 
112 static ssize_t page_idle_bitmap_read(struct file *file, struct kobject *kobj,
113 				     struct bin_attribute *attr, char *buf,
114 				     loff_t pos, size_t count)
115 {
116 	u64 *out = (u64 *)buf;
117 	struct page *page;
118 	unsigned long pfn, end_pfn;
119 	int bit;
120 
121 	if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
122 		return -EINVAL;
123 
124 	pfn = pos * BITS_PER_BYTE;
125 	if (pfn >= max_pfn)
126 		return 0;
127 
128 	end_pfn = pfn + count * BITS_PER_BYTE;
129 	if (end_pfn > max_pfn)
130 		end_pfn = max_pfn;
131 
132 	for (; pfn < end_pfn; pfn++) {
133 		bit = pfn % BITMAP_CHUNK_BITS;
134 		if (!bit)
135 			*out = 0ULL;
136 		page = page_idle_get_page(pfn);
137 		if (page) {
138 			if (page_is_idle(page)) {
139 				/*
140 				 * The page might have been referenced via a
141 				 * pte, in which case it is not idle. Clear
142 				 * refs and recheck.
143 				 */
144 				page_idle_clear_pte_refs(page);
145 				if (page_is_idle(page))
146 					*out |= 1ULL << bit;
147 			}
148 			put_page(page);
149 		}
150 		if (bit == BITMAP_CHUNK_BITS - 1)
151 			out++;
152 		cond_resched();
153 	}
154 	return (char *)out - buf;
155 }
156 
157 static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj,
158 				      struct bin_attribute *attr, char *buf,
159 				      loff_t pos, size_t count)
160 {
161 	const u64 *in = (u64 *)buf;
162 	struct page *page;
163 	unsigned long pfn, end_pfn;
164 	int bit;
165 
166 	if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE)
167 		return -EINVAL;
168 
169 	pfn = pos * BITS_PER_BYTE;
170 	if (pfn >= max_pfn)
171 		return -ENXIO;
172 
173 	end_pfn = pfn + count * BITS_PER_BYTE;
174 	if (end_pfn > max_pfn)
175 		end_pfn = max_pfn;
176 
177 	for (; pfn < end_pfn; pfn++) {
178 		bit = pfn % BITMAP_CHUNK_BITS;
179 		if ((*in >> bit) & 1) {
180 			page = page_idle_get_page(pfn);
181 			if (page) {
182 				page_idle_clear_pte_refs(page);
183 				set_page_idle(page);
184 				put_page(page);
185 			}
186 		}
187 		if (bit == BITMAP_CHUNK_BITS - 1)
188 			in++;
189 		cond_resched();
190 	}
191 	return (char *)in - buf;
192 }
193 
194 static struct bin_attribute page_idle_bitmap_attr =
195 		__BIN_ATTR(bitmap, 0600,
196 			   page_idle_bitmap_read, page_idle_bitmap_write, 0);
197 
198 static struct bin_attribute *page_idle_bin_attrs[] = {
199 	&page_idle_bitmap_attr,
200 	NULL,
201 };
202 
203 static const struct attribute_group page_idle_attr_group = {
204 	.bin_attrs = page_idle_bin_attrs,
205 	.name = "page_idle",
206 };
207 
208 static int __init page_idle_init(void)
209 {
210 	int err;
211 
212 	err = sysfs_create_group(mm_kobj, &page_idle_attr_group);
213 	if (err) {
214 		pr_err("page_idle: register sysfs failed\n");
215 		return err;
216 	}
217 	return 0;
218 }
219 subsys_initcall(page_idle_init);
220