xref: /openbmc/linux/mm/damon/paddr.c (revision 2243acd5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DAMON Primitives for The Physical Address Space
4  *
5  * Author: SeongJae Park <sj@kernel.org>
6  */
7 
8 #define pr_fmt(fmt) "damon-pa: " fmt
9 
10 #include <linux/mmu_notifier.h>
11 #include <linux/page_idle.h>
12 #include <linux/pagemap.h>
13 #include <linux/rmap.h>
14 #include <linux/swap.h>
15 
16 #include "../internal.h"
17 #include "ops-common.h"
18 
19 static bool __damon_pa_mkold(struct folio *folio, struct vm_area_struct *vma,
20 		unsigned long addr, void *arg)
21 {
22 	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, addr, 0);
23 
24 	while (page_vma_mapped_walk(&pvmw)) {
25 		addr = pvmw.address;
26 		if (pvmw.pte)
27 			damon_ptep_mkold(pvmw.pte, vma->vm_mm, addr);
28 		else
29 			damon_pmdp_mkold(pvmw.pmd, vma->vm_mm, addr);
30 	}
31 	return true;
32 }
33 
34 static void damon_pa_mkold(unsigned long paddr)
35 {
36 	struct folio *folio = damon_get_folio(PHYS_PFN(paddr));
37 	struct rmap_walk_control rwc = {
38 		.rmap_one = __damon_pa_mkold,
39 		.anon_lock = folio_lock_anon_vma_read,
40 	};
41 	bool need_lock;
42 
43 	if (!folio)
44 		return;
45 
46 	if (!folio_mapped(folio) || !folio_raw_mapping(folio)) {
47 		folio_set_idle(folio);
48 		goto out;
49 	}
50 
51 	need_lock = !folio_test_anon(folio) || folio_test_ksm(folio);
52 	if (need_lock && !folio_trylock(folio))
53 		goto out;
54 
55 	rmap_walk(folio, &rwc);
56 
57 	if (need_lock)
58 		folio_unlock(folio);
59 
60 out:
61 	folio_put(folio);
62 }
63 
64 static void __damon_pa_prepare_access_check(struct damon_region *r)
65 {
66 	r->sampling_addr = damon_rand(r->ar.start, r->ar.end);
67 
68 	damon_pa_mkold(r->sampling_addr);
69 }
70 
71 static void damon_pa_prepare_access_checks(struct damon_ctx *ctx)
72 {
73 	struct damon_target *t;
74 	struct damon_region *r;
75 
76 	damon_for_each_target(t, ctx) {
77 		damon_for_each_region(r, t)
78 			__damon_pa_prepare_access_check(r);
79 	}
80 }
81 
82 static bool __damon_pa_young(struct folio *folio, struct vm_area_struct *vma,
83 		unsigned long addr, void *arg)
84 {
85 	bool *accessed = arg;
86 	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, addr, 0);
87 
88 	*accessed = false;
89 	while (page_vma_mapped_walk(&pvmw)) {
90 		addr = pvmw.address;
91 		if (pvmw.pte) {
92 			*accessed = pte_young(*pvmw.pte) ||
93 				!folio_test_idle(folio) ||
94 				mmu_notifier_test_young(vma->vm_mm, addr);
95 		} else {
96 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
97 			*accessed = pmd_young(*pvmw.pmd) ||
98 				!folio_test_idle(folio) ||
99 				mmu_notifier_test_young(vma->vm_mm, addr);
100 #else
101 			WARN_ON_ONCE(1);
102 #endif	/* CONFIG_TRANSPARENT_HUGEPAGE */
103 		}
104 		if (*accessed) {
105 			page_vma_mapped_walk_done(&pvmw);
106 			break;
107 		}
108 	}
109 
110 	/* If accessed, stop walking */
111 	return *accessed == false;
112 }
113 
114 static bool damon_pa_young(unsigned long paddr, unsigned long *folio_sz)
115 {
116 	struct folio *folio = damon_get_folio(PHYS_PFN(paddr));
117 	bool accessed = false;
118 	struct rmap_walk_control rwc = {
119 		.arg = &accessed,
120 		.rmap_one = __damon_pa_young,
121 		.anon_lock = folio_lock_anon_vma_read,
122 	};
123 	bool need_lock;
124 
125 	if (!folio)
126 		return false;
127 
128 	if (!folio_mapped(folio) || !folio_raw_mapping(folio)) {
129 		if (folio_test_idle(folio))
130 			accessed = false;
131 		else
132 			accessed = true;
133 		goto out;
134 	}
135 
136 	need_lock = !folio_test_anon(folio) || folio_test_ksm(folio);
137 	if (need_lock && !folio_trylock(folio)) {
138 		folio_put(folio);
139 		return false;
140 	}
141 
142 	rmap_walk(folio, &rwc);
143 
144 	if (need_lock)
145 		folio_unlock(folio);
146 
147 out:
148 	*folio_sz = folio_size(folio);
149 	folio_put(folio);
150 	return accessed;
151 }
152 
153 static void __damon_pa_check_access(struct damon_region *r)
154 {
155 	static unsigned long last_addr;
156 	static unsigned long last_folio_sz = PAGE_SIZE;
157 	static bool last_accessed;
158 
159 	/* If the region is in the last checked page, reuse the result */
160 	if (ALIGN_DOWN(last_addr, last_folio_sz) ==
161 				ALIGN_DOWN(r->sampling_addr, last_folio_sz)) {
162 		if (last_accessed)
163 			r->nr_accesses++;
164 		return;
165 	}
166 
167 	last_accessed = damon_pa_young(r->sampling_addr, &last_folio_sz);
168 	if (last_accessed)
169 		r->nr_accesses++;
170 
171 	last_addr = r->sampling_addr;
172 }
173 
174 static unsigned int damon_pa_check_accesses(struct damon_ctx *ctx)
175 {
176 	struct damon_target *t;
177 	struct damon_region *r;
178 	unsigned int max_nr_accesses = 0;
179 
180 	damon_for_each_target(t, ctx) {
181 		damon_for_each_region(r, t) {
182 			__damon_pa_check_access(r);
183 			max_nr_accesses = max(r->nr_accesses, max_nr_accesses);
184 		}
185 	}
186 
187 	return max_nr_accesses;
188 }
189 
190 static bool __damos_pa_filter_out(struct damos_filter *filter,
191 		struct folio *folio)
192 {
193 	bool matched = false;
194 	struct mem_cgroup *memcg;
195 
196 	switch (filter->type) {
197 	case DAMOS_FILTER_TYPE_ANON:
198 		matched = folio_test_anon(folio);
199 		break;
200 	case DAMOS_FILTER_TYPE_MEMCG:
201 		rcu_read_lock();
202 		memcg = folio_memcg_check(folio);
203 		if (!memcg)
204 			matched = false;
205 		else
206 			matched = filter->memcg_id == mem_cgroup_id(memcg);
207 		rcu_read_unlock();
208 		break;
209 	default:
210 		break;
211 	}
212 
213 	return matched == filter->matching;
214 }
215 
216 /*
217  * damos_pa_filter_out - Return true if the page should be filtered out.
218  */
219 static bool damos_pa_filter_out(struct damos *scheme, struct folio *folio)
220 {
221 	struct damos_filter *filter;
222 
223 	damos_for_each_filter(filter, scheme) {
224 		if (__damos_pa_filter_out(filter, folio))
225 			return true;
226 	}
227 	return false;
228 }
229 
230 static unsigned long damon_pa_pageout(struct damon_region *r, struct damos *s)
231 {
232 	unsigned long addr, applied;
233 	LIST_HEAD(folio_list);
234 
235 	for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) {
236 		struct folio *folio = damon_get_folio(PHYS_PFN(addr));
237 
238 		if (!folio)
239 			continue;
240 
241 		if (damos_pa_filter_out(s, folio)) {
242 			folio_put(folio);
243 			continue;
244 		}
245 
246 		folio_clear_referenced(folio);
247 		folio_test_clear_young(folio);
248 		if (!folio_isolate_lru(folio)) {
249 			folio_put(folio);
250 			continue;
251 		}
252 		if (folio_test_unevictable(folio))
253 			folio_putback_lru(folio);
254 		else
255 			list_add(&folio->lru, &folio_list);
256 		folio_put(folio);
257 	}
258 	applied = reclaim_pages(&folio_list);
259 	cond_resched();
260 	return applied * PAGE_SIZE;
261 }
262 
263 static inline unsigned long damon_pa_mark_accessed_or_deactivate(
264 		struct damon_region *r, struct damos *s, bool mark_accessed)
265 {
266 	unsigned long addr, applied = 0;
267 
268 	for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) {
269 		struct folio *folio = damon_get_folio(PHYS_PFN(addr));
270 
271 		if (!folio)
272 			continue;
273 
274 		if (damos_pa_filter_out(s, folio)) {
275 			folio_put(folio);
276 			continue;
277 		}
278 
279 		if (mark_accessed)
280 			folio_mark_accessed(folio);
281 		else
282 			folio_deactivate(folio);
283 		applied += folio_nr_pages(folio);
284 		folio_put(folio);
285 	}
286 	return applied * PAGE_SIZE;
287 }
288 
289 static unsigned long damon_pa_mark_accessed(struct damon_region *r,
290 	struct damos *s)
291 {
292 	return damon_pa_mark_accessed_or_deactivate(r, s, true);
293 }
294 
295 static unsigned long damon_pa_deactivate_pages(struct damon_region *r,
296 	struct damos *s)
297 {
298 	return damon_pa_mark_accessed_or_deactivate(r, s, false);
299 }
300 
301 static unsigned long damon_pa_apply_scheme(struct damon_ctx *ctx,
302 		struct damon_target *t, struct damon_region *r,
303 		struct damos *scheme)
304 {
305 	switch (scheme->action) {
306 	case DAMOS_PAGEOUT:
307 		return damon_pa_pageout(r, scheme);
308 	case DAMOS_LRU_PRIO:
309 		return damon_pa_mark_accessed(r, scheme);
310 	case DAMOS_LRU_DEPRIO:
311 		return damon_pa_deactivate_pages(r, scheme);
312 	case DAMOS_STAT:
313 		break;
314 	default:
315 		/* DAMOS actions that not yet supported by 'paddr'. */
316 		break;
317 	}
318 	return 0;
319 }
320 
321 static int damon_pa_scheme_score(struct damon_ctx *context,
322 		struct damon_target *t, struct damon_region *r,
323 		struct damos *scheme)
324 {
325 	switch (scheme->action) {
326 	case DAMOS_PAGEOUT:
327 		return damon_cold_score(context, r, scheme);
328 	case DAMOS_LRU_PRIO:
329 		return damon_hot_score(context, r, scheme);
330 	case DAMOS_LRU_DEPRIO:
331 		return damon_cold_score(context, r, scheme);
332 	default:
333 		break;
334 	}
335 
336 	return DAMOS_MAX_SCORE;
337 }
338 
339 static int __init damon_pa_initcall(void)
340 {
341 	struct damon_operations ops = {
342 		.id = DAMON_OPS_PADDR,
343 		.init = NULL,
344 		.update = NULL,
345 		.prepare_access_checks = damon_pa_prepare_access_checks,
346 		.check_accesses = damon_pa_check_accesses,
347 		.reset_aggregated = NULL,
348 		.target_valid = NULL,
349 		.cleanup = NULL,
350 		.apply_scheme = damon_pa_apply_scheme,
351 		.get_scheme_score = damon_pa_scheme_score,
352 	};
353 
354 	return damon_register_ops(&ops);
355 };
356 
357 subsys_initcall(damon_pa_initcall);
358