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 
__damon_pa_mkold(struct folio * folio,struct vm_area_struct * vma,unsigned long addr,void * arg)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 
damon_pa_mkold(unsigned long paddr)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 
__damon_pa_prepare_access_check(struct damon_region * r)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 
damon_pa_prepare_access_checks(struct damon_ctx * ctx)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 
__damon_pa_young(struct folio * folio,struct vm_area_struct * vma,unsigned long addr,void * arg)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 
damon_pa_young(unsigned long paddr,unsigned long * folio_sz)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 		folio_put(folio);
134 		goto out;
135 	}
136 
137 	need_lock = !folio_test_anon(folio) || folio_test_ksm(folio);
138 	if (need_lock && !folio_trylock(folio)) {
139 		folio_put(folio);
140 		return false;
141 	}
142 
143 	rmap_walk(folio, &rwc);
144 
145 	if (need_lock)
146 		folio_unlock(folio);
147 	folio_put(folio);
148 
149 out:
150 	*folio_sz = folio_size(folio);
151 	return accessed;
152 }
153 
__damon_pa_check_access(struct damon_region * r)154 static void __damon_pa_check_access(struct damon_region *r)
155 {
156 	static unsigned long last_addr;
157 	static unsigned long last_folio_sz = PAGE_SIZE;
158 	static bool last_accessed;
159 
160 	/* If the region is in the last checked page, reuse the result */
161 	if (ALIGN_DOWN(last_addr, last_folio_sz) ==
162 				ALIGN_DOWN(r->sampling_addr, last_folio_sz)) {
163 		if (last_accessed)
164 			r->nr_accesses++;
165 		return;
166 	}
167 
168 	last_accessed = damon_pa_young(r->sampling_addr, &last_folio_sz);
169 	if (last_accessed)
170 		r->nr_accesses++;
171 
172 	last_addr = r->sampling_addr;
173 }
174 
damon_pa_check_accesses(struct damon_ctx * ctx)175 static unsigned int damon_pa_check_accesses(struct damon_ctx *ctx)
176 {
177 	struct damon_target *t;
178 	struct damon_region *r;
179 	unsigned int max_nr_accesses = 0;
180 
181 	damon_for_each_target(t, ctx) {
182 		damon_for_each_region(r, t) {
183 			__damon_pa_check_access(r);
184 			max_nr_accesses = max(r->nr_accesses, max_nr_accesses);
185 		}
186 	}
187 
188 	return max_nr_accesses;
189 }
190 
__damos_pa_filter_out(struct damos_filter * filter,struct folio * folio)191 static bool __damos_pa_filter_out(struct damos_filter *filter,
192 		struct folio *folio)
193 {
194 	bool matched = false;
195 	struct mem_cgroup *memcg;
196 
197 	switch (filter->type) {
198 	case DAMOS_FILTER_TYPE_ANON:
199 		matched = folio_test_anon(folio);
200 		break;
201 	case DAMOS_FILTER_TYPE_MEMCG:
202 		rcu_read_lock();
203 		memcg = folio_memcg_check(folio);
204 		if (!memcg)
205 			matched = false;
206 		else
207 			matched = filter->memcg_id == mem_cgroup_id(memcg);
208 		rcu_read_unlock();
209 		break;
210 	default:
211 		break;
212 	}
213 
214 	return matched == filter->matching;
215 }
216 
217 /*
218  * damos_pa_filter_out - Return true if the page should be filtered out.
219  */
damos_pa_filter_out(struct damos * scheme,struct folio * folio)220 static bool damos_pa_filter_out(struct damos *scheme, struct folio *folio)
221 {
222 	struct damos_filter *filter;
223 
224 	damos_for_each_filter(filter, scheme) {
225 		if (__damos_pa_filter_out(filter, folio))
226 			return true;
227 	}
228 	return false;
229 }
230 
damon_pa_pageout(struct damon_region * r,struct damos * s)231 static unsigned long damon_pa_pageout(struct damon_region *r, struct damos *s)
232 {
233 	unsigned long addr, applied;
234 	LIST_HEAD(folio_list);
235 
236 	for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) {
237 		struct folio *folio = damon_get_folio(PHYS_PFN(addr));
238 
239 		if (!folio)
240 			continue;
241 
242 		if (damos_pa_filter_out(s, folio)) {
243 			folio_put(folio);
244 			continue;
245 		}
246 
247 		folio_clear_referenced(folio);
248 		folio_test_clear_young(folio);
249 		if (!folio_isolate_lru(folio)) {
250 			folio_put(folio);
251 			continue;
252 		}
253 		if (folio_test_unevictable(folio))
254 			folio_putback_lru(folio);
255 		else
256 			list_add(&folio->lru, &folio_list);
257 		folio_put(folio);
258 	}
259 	applied = reclaim_pages(&folio_list);
260 	cond_resched();
261 	return applied * PAGE_SIZE;
262 }
263 
damon_pa_mark_accessed_or_deactivate(struct damon_region * r,struct damos * s,bool mark_accessed)264 static inline unsigned long damon_pa_mark_accessed_or_deactivate(
265 		struct damon_region *r, struct damos *s, bool mark_accessed)
266 {
267 	unsigned long addr, applied = 0;
268 
269 	for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) {
270 		struct folio *folio = damon_get_folio(PHYS_PFN(addr));
271 
272 		if (!folio)
273 			continue;
274 
275 		if (damos_pa_filter_out(s, folio)) {
276 			folio_put(folio);
277 			continue;
278 		}
279 
280 		if (mark_accessed)
281 			folio_mark_accessed(folio);
282 		else
283 			folio_deactivate(folio);
284 		folio_put(folio);
285 		applied += folio_nr_pages(folio);
286 	}
287 	return applied * PAGE_SIZE;
288 }
289 
damon_pa_mark_accessed(struct damon_region * r,struct damos * s)290 static unsigned long damon_pa_mark_accessed(struct damon_region *r,
291 	struct damos *s)
292 {
293 	return damon_pa_mark_accessed_or_deactivate(r, s, true);
294 }
295 
damon_pa_deactivate_pages(struct damon_region * r,struct damos * s)296 static unsigned long damon_pa_deactivate_pages(struct damon_region *r,
297 	struct damos *s)
298 {
299 	return damon_pa_mark_accessed_or_deactivate(r, s, false);
300 }
301 
damon_pa_apply_scheme(struct damon_ctx * ctx,struct damon_target * t,struct damon_region * r,struct damos * scheme)302 static unsigned long damon_pa_apply_scheme(struct damon_ctx *ctx,
303 		struct damon_target *t, struct damon_region *r,
304 		struct damos *scheme)
305 {
306 	switch (scheme->action) {
307 	case DAMOS_PAGEOUT:
308 		return damon_pa_pageout(r, scheme);
309 	case DAMOS_LRU_PRIO:
310 		return damon_pa_mark_accessed(r, scheme);
311 	case DAMOS_LRU_DEPRIO:
312 		return damon_pa_deactivate_pages(r, scheme);
313 	case DAMOS_STAT:
314 		break;
315 	default:
316 		/* DAMOS actions that not yet supported by 'paddr'. */
317 		break;
318 	}
319 	return 0;
320 }
321 
damon_pa_scheme_score(struct damon_ctx * context,struct damon_target * t,struct damon_region * r,struct damos * scheme)322 static int damon_pa_scheme_score(struct damon_ctx *context,
323 		struct damon_target *t, struct damon_region *r,
324 		struct damos *scheme)
325 {
326 	switch (scheme->action) {
327 	case DAMOS_PAGEOUT:
328 		return damon_cold_score(context, r, scheme);
329 	case DAMOS_LRU_PRIO:
330 		return damon_hot_score(context, r, scheme);
331 	case DAMOS_LRU_DEPRIO:
332 		return damon_cold_score(context, r, scheme);
333 	default:
334 		break;
335 	}
336 
337 	return DAMOS_MAX_SCORE;
338 }
339 
damon_pa_initcall(void)340 static int __init damon_pa_initcall(void)
341 {
342 	struct damon_operations ops = {
343 		.id = DAMON_OPS_PADDR,
344 		.init = NULL,
345 		.update = NULL,
346 		.prepare_access_checks = damon_pa_prepare_access_checks,
347 		.check_accesses = damon_pa_check_accesses,
348 		.reset_aggregated = NULL,
349 		.target_valid = NULL,
350 		.cleanup = NULL,
351 		.apply_scheme = damon_pa_apply_scheme,
352 		.get_scheme_score = damon_pa_scheme_score,
353 	};
354 
355 	return damon_register_ops(&ops);
356 };
357 
358 subsys_initcall(damon_pa_initcall);
359