Commit | Line | Data |
---|---|---|
3f49584b SP |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * DAMON Primitives for Virtual Address Spaces | |
4 | * | |
5 | * Author: SeongJae Park <sjpark@amazon.de> | |
6 | */ | |
7 | ||
8 | #define pr_fmt(fmt) "damon-va: " fmt | |
9 | ||
6dea8add | 10 | #include <asm-generic/mman-common.h> |
46c3a0ac | 11 | #include <linux/highmem.h> |
3f49584b | 12 | #include <linux/hugetlb.h> |
3f49584b | 13 | #include <linux/mmu_notifier.h> |
3f49584b SP |
14 | #include <linux/page_idle.h> |
15 | #include <linux/pagewalk.h> | |
8581fd40 | 16 | #include <linux/sched/mm.h> |
46c3a0ac SP |
17 | |
18 | #include "prmtv-common.h" | |
3f49584b | 19 | |
17ccae8b SP |
20 | #ifdef CONFIG_DAMON_VADDR_KUNIT_TEST |
21 | #undef DAMON_MIN_REGION | |
22 | #define DAMON_MIN_REGION 1 | |
23 | #endif | |
24 | ||
3f49584b SP |
25 | /* |
26 | * 't->id' should be the pointer to the relevant 'struct pid' having reference | |
27 | * count. Caller must put the returned task, unless it is NULL. | |
28 | */ | |
29 | #define damon_get_task_struct(t) \ | |
30 | (get_pid_task((struct pid *)t->id, PIDTYPE_PID)) | |
31 | ||
32 | /* | |
33 | * Get the mm_struct of the given target | |
34 | * | |
35 | * Caller _must_ put the mm_struct after use, unless it is NULL. | |
36 | * | |
37 | * Returns the mm_struct of the target on success, NULL on failure | |
38 | */ | |
39 | static struct mm_struct *damon_get_mm(struct damon_target *t) | |
40 | { | |
41 | struct task_struct *task; | |
42 | struct mm_struct *mm; | |
43 | ||
44 | task = damon_get_task_struct(t); | |
45 | if (!task) | |
46 | return NULL; | |
47 | ||
48 | mm = get_task_mm(task); | |
49 | put_task_struct(task); | |
50 | return mm; | |
51 | } | |
52 | ||
53 | /* | |
54 | * Functions for the initial monitoring target regions construction | |
55 | */ | |
56 | ||
57 | /* | |
58 | * Size-evenly split a region into 'nr_pieces' small regions | |
59 | * | |
60 | * Returns 0 on success, or negative error code otherwise. | |
61 | */ | |
62 | static int damon_va_evenly_split_region(struct damon_target *t, | |
63 | struct damon_region *r, unsigned int nr_pieces) | |
64 | { | |
65 | unsigned long sz_orig, sz_piece, orig_end; | |
66 | struct damon_region *n = NULL, *next; | |
67 | unsigned long start; | |
68 | ||
69 | if (!r || !nr_pieces) | |
70 | return -EINVAL; | |
71 | ||
72 | orig_end = r->ar.end; | |
73 | sz_orig = r->ar.end - r->ar.start; | |
74 | sz_piece = ALIGN_DOWN(sz_orig / nr_pieces, DAMON_MIN_REGION); | |
75 | ||
76 | if (!sz_piece) | |
77 | return -EINVAL; | |
78 | ||
79 | r->ar.end = r->ar.start + sz_piece; | |
80 | next = damon_next_region(r); | |
81 | for (start = r->ar.end; start + sz_piece <= orig_end; | |
82 | start += sz_piece) { | |
83 | n = damon_new_region(start, start + sz_piece); | |
84 | if (!n) | |
85 | return -ENOMEM; | |
86 | damon_insert_region(n, r, next, t); | |
87 | r = n; | |
88 | } | |
89 | /* complement last region for possible rounding error */ | |
90 | if (n) | |
91 | n->ar.end = orig_end; | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | static unsigned long sz_range(struct damon_addr_range *r) | |
97 | { | |
98 | return r->end - r->start; | |
99 | } | |
100 | ||
101 | static void swap_ranges(struct damon_addr_range *r1, | |
102 | struct damon_addr_range *r2) | |
103 | { | |
104 | struct damon_addr_range tmp; | |
105 | ||
106 | tmp = *r1; | |
107 | *r1 = *r2; | |
108 | *r2 = tmp; | |
109 | } | |
110 | ||
111 | /* | |
112 | * Find three regions separated by two biggest unmapped regions | |
113 | * | |
114 | * vma the head vma of the target address space | |
115 | * regions an array of three address ranges that results will be saved | |
116 | * | |
117 | * This function receives an address space and finds three regions in it which | |
118 | * separated by the two biggest unmapped regions in the space. Please refer to | |
119 | * below comments of '__damon_va_init_regions()' function to know why this is | |
120 | * necessary. | |
121 | * | |
122 | * Returns 0 if success, or negative error code otherwise. | |
123 | */ | |
124 | static int __damon_va_three_regions(struct vm_area_struct *vma, | |
125 | struct damon_addr_range regions[3]) | |
126 | { | |
127 | struct damon_addr_range gap = {0}, first_gap = {0}, second_gap = {0}; | |
128 | struct vm_area_struct *last_vma = NULL; | |
129 | unsigned long start = 0; | |
130 | struct rb_root rbroot; | |
131 | ||
132 | /* Find two biggest gaps so that first_gap > second_gap > others */ | |
133 | for (; vma; vma = vma->vm_next) { | |
134 | if (!last_vma) { | |
135 | start = vma->vm_start; | |
136 | goto next; | |
137 | } | |
138 | ||
139 | if (vma->rb_subtree_gap <= sz_range(&second_gap)) { | |
140 | rbroot.rb_node = &vma->vm_rb; | |
141 | vma = rb_entry(rb_last(&rbroot), | |
142 | struct vm_area_struct, vm_rb); | |
143 | goto next; | |
144 | } | |
145 | ||
146 | gap.start = last_vma->vm_end; | |
147 | gap.end = vma->vm_start; | |
148 | if (sz_range(&gap) > sz_range(&second_gap)) { | |
149 | swap_ranges(&gap, &second_gap); | |
150 | if (sz_range(&second_gap) > sz_range(&first_gap)) | |
151 | swap_ranges(&second_gap, &first_gap); | |
152 | } | |
153 | next: | |
154 | last_vma = vma; | |
155 | } | |
156 | ||
157 | if (!sz_range(&second_gap) || !sz_range(&first_gap)) | |
158 | return -EINVAL; | |
159 | ||
160 | /* Sort the two biggest gaps by address */ | |
161 | if (first_gap.start > second_gap.start) | |
162 | swap_ranges(&first_gap, &second_gap); | |
163 | ||
164 | /* Store the result */ | |
165 | regions[0].start = ALIGN(start, DAMON_MIN_REGION); | |
166 | regions[0].end = ALIGN(first_gap.start, DAMON_MIN_REGION); | |
167 | regions[1].start = ALIGN(first_gap.end, DAMON_MIN_REGION); | |
168 | regions[1].end = ALIGN(second_gap.start, DAMON_MIN_REGION); | |
169 | regions[2].start = ALIGN(second_gap.end, DAMON_MIN_REGION); | |
170 | regions[2].end = ALIGN(last_vma->vm_end, DAMON_MIN_REGION); | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | /* | |
176 | * Get the three regions in the given target (task) | |
177 | * | |
178 | * Returns 0 on success, negative error code otherwise. | |
179 | */ | |
180 | static int damon_va_three_regions(struct damon_target *t, | |
181 | struct damon_addr_range regions[3]) | |
182 | { | |
183 | struct mm_struct *mm; | |
184 | int rc; | |
185 | ||
186 | mm = damon_get_mm(t); | |
187 | if (!mm) | |
188 | return -EINVAL; | |
189 | ||
190 | mmap_read_lock(mm); | |
191 | rc = __damon_va_three_regions(mm->mmap, regions); | |
192 | mmap_read_unlock(mm); | |
193 | ||
194 | mmput(mm); | |
195 | return rc; | |
196 | } | |
197 | ||
198 | /* | |
199 | * Initialize the monitoring target regions for the given target (task) | |
200 | * | |
201 | * t the given target | |
202 | * | |
203 | * Because only a number of small portions of the entire address space | |
204 | * is actually mapped to the memory and accessed, monitoring the unmapped | |
205 | * regions is wasteful. That said, because we can deal with small noises, | |
206 | * tracking every mapping is not strictly required but could even incur a high | |
207 | * overhead if the mapping frequently changes or the number of mappings is | |
208 | * high. The adaptive regions adjustment mechanism will further help to deal | |
209 | * with the noise by simply identifying the unmapped areas as a region that | |
210 | * has no access. Moreover, applying the real mappings that would have many | |
211 | * unmapped areas inside will make the adaptive mechanism quite complex. That | |
212 | * said, too huge unmapped areas inside the monitoring target should be removed | |
213 | * to not take the time for the adaptive mechanism. | |
214 | * | |
215 | * For the reason, we convert the complex mappings to three distinct regions | |
216 | * that cover every mapped area of the address space. Also the two gaps | |
217 | * between the three regions are the two biggest unmapped areas in the given | |
218 | * address space. In detail, this function first identifies the start and the | |
219 | * end of the mappings and the two biggest unmapped areas of the address space. | |
220 | * Then, it constructs the three regions as below: | |
221 | * | |
222 | * [mappings[0]->start, big_two_unmapped_areas[0]->start) | |
223 | * [big_two_unmapped_areas[0]->end, big_two_unmapped_areas[1]->start) | |
224 | * [big_two_unmapped_areas[1]->end, mappings[nr_mappings - 1]->end) | |
225 | * | |
226 | * As usual memory map of processes is as below, the gap between the heap and | |
227 | * the uppermost mmap()-ed region, and the gap between the lowermost mmap()-ed | |
228 | * region and the stack will be two biggest unmapped regions. Because these | |
229 | * gaps are exceptionally huge areas in usual address space, excluding these | |
230 | * two biggest unmapped regions will be sufficient to make a trade-off. | |
231 | * | |
232 | * <heap> | |
233 | * <BIG UNMAPPED REGION 1> | |
234 | * <uppermost mmap()-ed region> | |
235 | * (other mmap()-ed regions and small unmapped regions) | |
236 | * <lowermost mmap()-ed region> | |
237 | * <BIG UNMAPPED REGION 2> | |
238 | * <stack> | |
239 | */ | |
240 | static void __damon_va_init_regions(struct damon_ctx *ctx, | |
241 | struct damon_target *t) | |
242 | { | |
243 | struct damon_region *r; | |
244 | struct damon_addr_range regions[3]; | |
245 | unsigned long sz = 0, nr_pieces; | |
246 | int i; | |
247 | ||
248 | if (damon_va_three_regions(t, regions)) { | |
249 | pr_err("Failed to get three regions of target %lu\n", t->id); | |
250 | return; | |
251 | } | |
252 | ||
253 | for (i = 0; i < 3; i++) | |
254 | sz += regions[i].end - regions[i].start; | |
255 | if (ctx->min_nr_regions) | |
256 | sz /= ctx->min_nr_regions; | |
257 | if (sz < DAMON_MIN_REGION) | |
258 | sz = DAMON_MIN_REGION; | |
259 | ||
260 | /* Set the initial three regions of the target */ | |
261 | for (i = 0; i < 3; i++) { | |
262 | r = damon_new_region(regions[i].start, regions[i].end); | |
263 | if (!r) { | |
264 | pr_err("%d'th init region creation failed\n", i); | |
265 | return; | |
266 | } | |
267 | damon_add_region(r, t); | |
268 | ||
269 | nr_pieces = (regions[i].end - regions[i].start) / sz; | |
270 | damon_va_evenly_split_region(t, r, nr_pieces); | |
271 | } | |
272 | } | |
273 | ||
274 | /* Initialize '->regions_list' of every target (task) */ | |
cdeed009 | 275 | static void damon_va_init(struct damon_ctx *ctx) |
3f49584b SP |
276 | { |
277 | struct damon_target *t; | |
278 | ||
279 | damon_for_each_target(t, ctx) { | |
280 | /* the user may set the target regions as they want */ | |
281 | if (!damon_nr_regions(t)) | |
282 | __damon_va_init_regions(ctx, t); | |
283 | } | |
284 | } | |
285 | ||
286 | /* | |
287 | * Functions for the dynamic monitoring target regions update | |
288 | */ | |
289 | ||
290 | /* | |
291 | * Check whether a region is intersecting an address range | |
292 | * | |
293 | * Returns true if it is. | |
294 | */ | |
cdeed009 XH |
295 | static bool damon_intersect(struct damon_region *r, |
296 | struct damon_addr_range *re) | |
3f49584b SP |
297 | { |
298 | return !(r->ar.end <= re->start || re->end <= r->ar.start); | |
299 | } | |
300 | ||
301 | /* | |
302 | * Update damon regions for the three big regions of the given target | |
303 | * | |
304 | * t the given target | |
305 | * bregions the three big regions of the target | |
306 | */ | |
307 | static void damon_va_apply_three_regions(struct damon_target *t, | |
308 | struct damon_addr_range bregions[3]) | |
309 | { | |
310 | struct damon_region *r, *next; | |
a460a360 | 311 | unsigned int i; |
3f49584b SP |
312 | |
313 | /* Remove regions which are not in the three big regions now */ | |
314 | damon_for_each_region_safe(r, next, t) { | |
315 | for (i = 0; i < 3; i++) { | |
316 | if (damon_intersect(r, &bregions[i])) | |
317 | break; | |
318 | } | |
319 | if (i == 3) | |
320 | damon_destroy_region(r, t); | |
321 | } | |
322 | ||
323 | /* Adjust intersecting regions to fit with the three big regions */ | |
324 | for (i = 0; i < 3; i++) { | |
325 | struct damon_region *first = NULL, *last; | |
326 | struct damon_region *newr; | |
327 | struct damon_addr_range *br; | |
328 | ||
329 | br = &bregions[i]; | |
330 | /* Get the first and last regions which intersects with br */ | |
331 | damon_for_each_region(r, t) { | |
332 | if (damon_intersect(r, br)) { | |
333 | if (!first) | |
334 | first = r; | |
335 | last = r; | |
336 | } | |
337 | if (r->ar.start >= br->end) | |
338 | break; | |
339 | } | |
340 | if (!first) { | |
341 | /* no damon_region intersects with this big region */ | |
342 | newr = damon_new_region( | |
343 | ALIGN_DOWN(br->start, | |
344 | DAMON_MIN_REGION), | |
345 | ALIGN(br->end, DAMON_MIN_REGION)); | |
346 | if (!newr) | |
347 | continue; | |
348 | damon_insert_region(newr, damon_prev_region(r), r, t); | |
349 | } else { | |
350 | first->ar.start = ALIGN_DOWN(br->start, | |
351 | DAMON_MIN_REGION); | |
352 | last->ar.end = ALIGN(br->end, DAMON_MIN_REGION); | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | /* | |
358 | * Update regions for current memory mappings | |
359 | */ | |
cdeed009 | 360 | static void damon_va_update(struct damon_ctx *ctx) |
3f49584b SP |
361 | { |
362 | struct damon_addr_range three_regions[3]; | |
363 | struct damon_target *t; | |
364 | ||
365 | damon_for_each_target(t, ctx) { | |
366 | if (damon_va_three_regions(t, three_regions)) | |
367 | continue; | |
368 | damon_va_apply_three_regions(t, three_regions); | |
369 | } | |
370 | } | |
371 | ||
3f49584b SP |
372 | static int damon_mkold_pmd_entry(pmd_t *pmd, unsigned long addr, |
373 | unsigned long next, struct mm_walk *walk) | |
374 | { | |
375 | pte_t *pte; | |
376 | spinlock_t *ptl; | |
377 | ||
378 | if (pmd_huge(*pmd)) { | |
379 | ptl = pmd_lock(walk->mm, pmd); | |
380 | if (pmd_huge(*pmd)) { | |
381 | damon_pmdp_mkold(pmd, walk->mm, addr); | |
382 | spin_unlock(ptl); | |
383 | return 0; | |
384 | } | |
385 | spin_unlock(ptl); | |
386 | } | |
387 | ||
388 | if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) | |
389 | return 0; | |
390 | pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); | |
391 | if (!pte_present(*pte)) | |
392 | goto out; | |
393 | damon_ptep_mkold(pte, walk->mm, addr); | |
394 | out: | |
395 | pte_unmap_unlock(pte, ptl); | |
396 | return 0; | |
397 | } | |
398 | ||
199b50f4 | 399 | static const struct mm_walk_ops damon_mkold_ops = { |
3f49584b SP |
400 | .pmd_entry = damon_mkold_pmd_entry, |
401 | }; | |
402 | ||
403 | static void damon_va_mkold(struct mm_struct *mm, unsigned long addr) | |
404 | { | |
405 | mmap_read_lock(mm); | |
406 | walk_page_range(mm, addr, addr + 1, &damon_mkold_ops, NULL); | |
407 | mmap_read_unlock(mm); | |
408 | } | |
409 | ||
410 | /* | |
411 | * Functions for the access checking of the regions | |
412 | */ | |
413 | ||
b627b774 | 414 | static void __damon_va_prepare_access_check(struct damon_ctx *ctx, |
3f49584b SP |
415 | struct mm_struct *mm, struct damon_region *r) |
416 | { | |
417 | r->sampling_addr = damon_rand(r->ar.start, r->ar.end); | |
418 | ||
419 | damon_va_mkold(mm, r->sampling_addr); | |
420 | } | |
421 | ||
cdeed009 | 422 | static void damon_va_prepare_access_checks(struct damon_ctx *ctx) |
3f49584b SP |
423 | { |
424 | struct damon_target *t; | |
425 | struct mm_struct *mm; | |
426 | struct damon_region *r; | |
427 | ||
428 | damon_for_each_target(t, ctx) { | |
429 | mm = damon_get_mm(t); | |
430 | if (!mm) | |
431 | continue; | |
432 | damon_for_each_region(r, t) | |
b627b774 | 433 | __damon_va_prepare_access_check(ctx, mm, r); |
3f49584b SP |
434 | mmput(mm); |
435 | } | |
436 | } | |
437 | ||
438 | struct damon_young_walk_private { | |
439 | unsigned long *page_sz; | |
440 | bool young; | |
441 | }; | |
442 | ||
443 | static int damon_young_pmd_entry(pmd_t *pmd, unsigned long addr, | |
444 | unsigned long next, struct mm_walk *walk) | |
445 | { | |
446 | pte_t *pte; | |
447 | spinlock_t *ptl; | |
448 | struct page *page; | |
449 | struct damon_young_walk_private *priv = walk->private; | |
450 | ||
451 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
452 | if (pmd_huge(*pmd)) { | |
453 | ptl = pmd_lock(walk->mm, pmd); | |
454 | if (!pmd_huge(*pmd)) { | |
455 | spin_unlock(ptl); | |
456 | goto regular_page; | |
457 | } | |
458 | page = damon_get_page(pmd_pfn(*pmd)); | |
459 | if (!page) | |
460 | goto huge_out; | |
461 | if (pmd_young(*pmd) || !page_is_idle(page) || | |
462 | mmu_notifier_test_young(walk->mm, | |
463 | addr)) { | |
464 | *priv->page_sz = ((1UL) << HPAGE_PMD_SHIFT); | |
465 | priv->young = true; | |
466 | } | |
467 | put_page(page); | |
468 | huge_out: | |
469 | spin_unlock(ptl); | |
470 | return 0; | |
471 | } | |
472 | ||
473 | regular_page: | |
474 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ | |
475 | ||
476 | if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) | |
477 | return -EINVAL; | |
478 | pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); | |
479 | if (!pte_present(*pte)) | |
480 | goto out; | |
481 | page = damon_get_page(pte_pfn(*pte)); | |
482 | if (!page) | |
483 | goto out; | |
484 | if (pte_young(*pte) || !page_is_idle(page) || | |
485 | mmu_notifier_test_young(walk->mm, addr)) { | |
486 | *priv->page_sz = PAGE_SIZE; | |
487 | priv->young = true; | |
488 | } | |
489 | put_page(page); | |
490 | out: | |
491 | pte_unmap_unlock(pte, ptl); | |
492 | return 0; | |
493 | } | |
494 | ||
199b50f4 | 495 | static const struct mm_walk_ops damon_young_ops = { |
3f49584b SP |
496 | .pmd_entry = damon_young_pmd_entry, |
497 | }; | |
498 | ||
499 | static bool damon_va_young(struct mm_struct *mm, unsigned long addr, | |
500 | unsigned long *page_sz) | |
501 | { | |
502 | struct damon_young_walk_private arg = { | |
503 | .page_sz = page_sz, | |
504 | .young = false, | |
505 | }; | |
506 | ||
507 | mmap_read_lock(mm); | |
508 | walk_page_range(mm, addr, addr + 1, &damon_young_ops, &arg); | |
509 | mmap_read_unlock(mm); | |
510 | return arg.young; | |
511 | } | |
512 | ||
513 | /* | |
514 | * Check whether the region was accessed after the last preparation | |
515 | * | |
516 | * mm 'mm_struct' for the given virtual address space | |
517 | * r the region to be checked | |
518 | */ | |
b627b774 | 519 | static void __damon_va_check_access(struct damon_ctx *ctx, |
3f49584b SP |
520 | struct mm_struct *mm, struct damon_region *r) |
521 | { | |
522 | static struct mm_struct *last_mm; | |
523 | static unsigned long last_addr; | |
524 | static unsigned long last_page_sz = PAGE_SIZE; | |
525 | static bool last_accessed; | |
526 | ||
527 | /* If the region is in the last checked page, reuse the result */ | |
528 | if (mm == last_mm && (ALIGN_DOWN(last_addr, last_page_sz) == | |
529 | ALIGN_DOWN(r->sampling_addr, last_page_sz))) { | |
530 | if (last_accessed) | |
531 | r->nr_accesses++; | |
532 | return; | |
533 | } | |
534 | ||
535 | last_accessed = damon_va_young(mm, r->sampling_addr, &last_page_sz); | |
536 | if (last_accessed) | |
537 | r->nr_accesses++; | |
538 | ||
539 | last_mm = mm; | |
540 | last_addr = r->sampling_addr; | |
541 | } | |
542 | ||
cdeed009 | 543 | static unsigned int damon_va_check_accesses(struct damon_ctx *ctx) |
3f49584b SP |
544 | { |
545 | struct damon_target *t; | |
546 | struct mm_struct *mm; | |
547 | struct damon_region *r; | |
548 | unsigned int max_nr_accesses = 0; | |
549 | ||
550 | damon_for_each_target(t, ctx) { | |
551 | mm = damon_get_mm(t); | |
552 | if (!mm) | |
553 | continue; | |
554 | damon_for_each_region(r, t) { | |
b627b774 | 555 | __damon_va_check_access(ctx, mm, r); |
3f49584b SP |
556 | max_nr_accesses = max(r->nr_accesses, max_nr_accesses); |
557 | } | |
558 | mmput(mm); | |
559 | } | |
560 | ||
561 | return max_nr_accesses; | |
562 | } | |
563 | ||
564 | /* | |
565 | * Functions for the target validity check and cleanup | |
566 | */ | |
567 | ||
568 | bool damon_va_target_valid(void *target) | |
569 | { | |
570 | struct damon_target *t = target; | |
571 | struct task_struct *task; | |
572 | ||
573 | task = damon_get_task_struct(t); | |
574 | if (task) { | |
575 | put_task_struct(task); | |
576 | return true; | |
577 | } | |
578 | ||
579 | return false; | |
580 | } | |
581 | ||
6dea8add SP |
582 | #ifndef CONFIG_ADVISE_SYSCALLS |
583 | static int damos_madvise(struct damon_target *target, struct damon_region *r, | |
584 | int behavior) | |
585 | { | |
586 | return -EINVAL; | |
587 | } | |
588 | #else | |
589 | static int damos_madvise(struct damon_target *target, struct damon_region *r, | |
590 | int behavior) | |
591 | { | |
592 | struct mm_struct *mm; | |
593 | int ret = -ENOMEM; | |
594 | ||
595 | mm = damon_get_mm(target); | |
596 | if (!mm) | |
597 | goto out; | |
598 | ||
599 | ret = do_madvise(mm, PAGE_ALIGN(r->ar.start), | |
600 | PAGE_ALIGN(r->ar.end - r->ar.start), behavior); | |
601 | mmput(mm); | |
602 | out: | |
603 | return ret; | |
604 | } | |
605 | #endif /* CONFIG_ADVISE_SYSCALLS */ | |
606 | ||
cdeed009 | 607 | static int damon_va_apply_scheme(struct damon_ctx *ctx, struct damon_target *t, |
6dea8add SP |
608 | struct damon_region *r, struct damos *scheme) |
609 | { | |
610 | int madv_action; | |
611 | ||
612 | switch (scheme->action) { | |
613 | case DAMOS_WILLNEED: | |
614 | madv_action = MADV_WILLNEED; | |
615 | break; | |
616 | case DAMOS_COLD: | |
617 | madv_action = MADV_COLD; | |
618 | break; | |
619 | case DAMOS_PAGEOUT: | |
620 | madv_action = MADV_PAGEOUT; | |
621 | break; | |
622 | case DAMOS_HUGEPAGE: | |
623 | madv_action = MADV_HUGEPAGE; | |
624 | break; | |
625 | case DAMOS_NOHUGEPAGE: | |
626 | madv_action = MADV_NOHUGEPAGE; | |
627 | break; | |
2f0b548c SP |
628 | case DAMOS_STAT: |
629 | return 0; | |
6dea8add | 630 | default: |
6dea8add SP |
631 | return -EINVAL; |
632 | } | |
633 | ||
634 | return damos_madvise(t, r, madv_action); | |
635 | } | |
636 | ||
cdeed009 XH |
637 | static int damon_va_scheme_score(struct damon_ctx *context, |
638 | struct damon_target *t, struct damon_region *r, | |
639 | struct damos *scheme) | |
198f0f4c SP |
640 | { |
641 | ||
642 | switch (scheme->action) { | |
643 | case DAMOS_PAGEOUT: | |
644 | return damon_pageout_score(context, r, scheme); | |
645 | default: | |
646 | break; | |
647 | } | |
648 | ||
649 | return DAMOS_MAX_SCORE; | |
650 | } | |
651 | ||
3f49584b SP |
652 | void damon_va_set_primitives(struct damon_ctx *ctx) |
653 | { | |
654 | ctx->primitive.init = damon_va_init; | |
655 | ctx->primitive.update = damon_va_update; | |
656 | ctx->primitive.prepare_access_checks = damon_va_prepare_access_checks; | |
657 | ctx->primitive.check_accesses = damon_va_check_accesses; | |
658 | ctx->primitive.reset_aggregated = NULL; | |
659 | ctx->primitive.target_valid = damon_va_target_valid; | |
660 | ctx->primitive.cleanup = NULL; | |
6dea8add | 661 | ctx->primitive.apply_scheme = damon_va_apply_scheme; |
198f0f4c | 662 | ctx->primitive.get_scheme_score = damon_va_scheme_score; |
3f49584b | 663 | } |
17ccae8b SP |
664 | |
665 | #include "vaddr-test.h" |