1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * DAMON Code for Virtual Address Spaces
4 *
5 * Author: SeongJae Park <sj@kernel.org>
6 */
7
8 #define pr_fmt(fmt) "damon-va: " fmt
9
10 #include <linux/highmem.h>
11 #include <linux/hugetlb.h>
12 #include <linux/mman.h>
13 #include <linux/mmu_notifier.h>
14 #include <linux/page_idle.h>
15 #include <linux/pagewalk.h>
16 #include <linux/sched/mm.h>
17
18 #include "../internal.h"
19 #include "ops-common.h"
20
21 #ifdef CONFIG_DAMON_VADDR_KUNIT_TEST
22 #undef DAMON_MIN_REGION
23 #define DAMON_MIN_REGION 1
24 #endif
25
26 /*
27 * 't->pid' should be the pointer to the relevant 'struct pid' having reference
28 * count. Caller must put the returned task, unless it is NULL.
29 */
damon_get_task_struct(struct damon_target * t)30 static inline struct task_struct *damon_get_task_struct(struct damon_target *t)
31 {
32 return get_pid_task(t->pid, PIDTYPE_PID);
33 }
34
35 /*
36 * Get the mm_struct of the given target
37 *
38 * Caller _must_ put the mm_struct after use, unless it is NULL.
39 *
40 * Returns the mm_struct of the target on success, NULL on failure
41 */
damon_get_mm(struct damon_target * t)42 static struct mm_struct *damon_get_mm(struct damon_target *t)
43 {
44 struct task_struct *task;
45 struct mm_struct *mm;
46
47 task = damon_get_task_struct(t);
48 if (!task)
49 return NULL;
50
51 mm = get_task_mm(task);
52 put_task_struct(task);
53 return mm;
54 }
55
56 /*
57 * Functions for the initial monitoring target regions construction
58 */
59
60 /*
61 * Size-evenly split a region into 'nr_pieces' small regions
62 *
63 * Returns 0 on success, or negative error code otherwise.
64 */
damon_va_evenly_split_region(struct damon_target * t,struct damon_region * r,unsigned int nr_pieces)65 static int damon_va_evenly_split_region(struct damon_target *t,
66 struct damon_region *r, unsigned int nr_pieces)
67 {
68 unsigned long sz_orig, sz_piece, orig_end;
69 struct damon_region *n = NULL, *next;
70 unsigned long start;
71 unsigned int i;
72
73 if (!r || !nr_pieces)
74 return -EINVAL;
75
76 if (nr_pieces == 1)
77 return 0;
78
79 orig_end = r->ar.end;
80 sz_orig = damon_sz_region(r);
81 sz_piece = ALIGN_DOWN(sz_orig / nr_pieces, DAMON_MIN_REGION);
82
83 if (!sz_piece)
84 return -EINVAL;
85
86 r->ar.end = r->ar.start + sz_piece;
87 next = damon_next_region(r);
88 for (start = r->ar.end, i = 1; i < nr_pieces; start += sz_piece, i++) {
89 n = damon_new_region(start, start + sz_piece);
90 if (!n)
91 return -ENOMEM;
92 damon_insert_region(n, r, next, t);
93 r = n;
94 }
95 /* complement last region for possible rounding error */
96 if (n)
97 n->ar.end = orig_end;
98
99 return 0;
100 }
101
sz_range(struct damon_addr_range * r)102 static unsigned long sz_range(struct damon_addr_range *r)
103 {
104 return r->end - r->start;
105 }
106
107 /*
108 * Find three regions separated by two biggest unmapped regions
109 *
110 * vma the head vma of the target address space
111 * regions an array of three address ranges that results will be saved
112 *
113 * This function receives an address space and finds three regions in it which
114 * separated by the two biggest unmapped regions in the space. Please refer to
115 * below comments of '__damon_va_init_regions()' function to know why this is
116 * necessary.
117 *
118 * Returns 0 if success, or negative error code otherwise.
119 */
__damon_va_three_regions(struct mm_struct * mm,struct damon_addr_range regions[3])120 static int __damon_va_three_regions(struct mm_struct *mm,
121 struct damon_addr_range regions[3])
122 {
123 struct damon_addr_range first_gap = {0}, second_gap = {0};
124 VMA_ITERATOR(vmi, mm, 0);
125 struct vm_area_struct *vma, *prev = NULL;
126 unsigned long start;
127
128 /*
129 * Find the two biggest gaps so that first_gap > second_gap > others.
130 * If this is too slow, it can be optimised to examine the maple
131 * tree gaps.
132 */
133 rcu_read_lock();
134 for_each_vma(vmi, vma) {
135 unsigned long gap;
136
137 if (!prev) {
138 start = vma->vm_start;
139 goto next;
140 }
141 gap = vma->vm_start - prev->vm_end;
142
143 if (gap > sz_range(&first_gap)) {
144 second_gap = first_gap;
145 first_gap.start = prev->vm_end;
146 first_gap.end = vma->vm_start;
147 } else if (gap > sz_range(&second_gap)) {
148 second_gap.start = prev->vm_end;
149 second_gap.end = vma->vm_start;
150 }
151 next:
152 prev = vma;
153 }
154 rcu_read_unlock();
155
156 if (!sz_range(&second_gap) || !sz_range(&first_gap))
157 return -EINVAL;
158
159 /* Sort the two biggest gaps by address */
160 if (first_gap.start > second_gap.start)
161 swap(first_gap, second_gap);
162
163 /* Store the result */
164 regions[0].start = ALIGN(start, DAMON_MIN_REGION);
165 regions[0].end = ALIGN(first_gap.start, DAMON_MIN_REGION);
166 regions[1].start = ALIGN(first_gap.end, DAMON_MIN_REGION);
167 regions[1].end = ALIGN(second_gap.start, DAMON_MIN_REGION);
168 regions[2].start = ALIGN(second_gap.end, DAMON_MIN_REGION);
169 regions[2].end = ALIGN(prev->vm_end, DAMON_MIN_REGION);
170
171 return 0;
172 }
173
174 /*
175 * Get the three regions in the given target (task)
176 *
177 * Returns 0 on success, negative error code otherwise.
178 */
damon_va_three_regions(struct damon_target * t,struct damon_addr_range regions[3])179 static int damon_va_three_regions(struct damon_target *t,
180 struct damon_addr_range regions[3])
181 {
182 struct mm_struct *mm;
183 int rc;
184
185 mm = damon_get_mm(t);
186 if (!mm)
187 return -EINVAL;
188
189 mmap_read_lock(mm);
190 rc = __damon_va_three_regions(mm, regions);
191 mmap_read_unlock(mm);
192
193 mmput(mm);
194 return rc;
195 }
196
197 /*
198 * Initialize the monitoring target regions for the given target (task)
199 *
200 * t the given target
201 *
202 * Because only a number of small portions of the entire address space
203 * is actually mapped to the memory and accessed, monitoring the unmapped
204 * regions is wasteful. That said, because we can deal with small noises,
205 * tracking every mapping is not strictly required but could even incur a high
206 * overhead if the mapping frequently changes or the number of mappings is
207 * high. The adaptive regions adjustment mechanism will further help to deal
208 * with the noise by simply identifying the unmapped areas as a region that
209 * has no access. Moreover, applying the real mappings that would have many
210 * unmapped areas inside will make the adaptive mechanism quite complex. That
211 * said, too huge unmapped areas inside the monitoring target should be removed
212 * to not take the time for the adaptive mechanism.
213 *
214 * For the reason, we convert the complex mappings to three distinct regions
215 * that cover every mapped area of the address space. Also the two gaps
216 * between the three regions are the two biggest unmapped areas in the given
217 * address space. In detail, this function first identifies the start and the
218 * end of the mappings and the two biggest unmapped areas of the address space.
219 * Then, it constructs the three regions as below:
220 *
221 * [mappings[0]->start, big_two_unmapped_areas[0]->start)
222 * [big_two_unmapped_areas[0]->end, big_two_unmapped_areas[1]->start)
223 * [big_two_unmapped_areas[1]->end, mappings[nr_mappings - 1]->end)
224 *
225 * As usual memory map of processes is as below, the gap between the heap and
226 * the uppermost mmap()-ed region, and the gap between the lowermost mmap()-ed
227 * region and the stack will be two biggest unmapped regions. Because these
228 * gaps are exceptionally huge areas in usual address space, excluding these
229 * two biggest unmapped regions will be sufficient to make a trade-off.
230 *
231 * <heap>
232 * <BIG UNMAPPED REGION 1>
233 * <uppermost mmap()-ed region>
234 * (other mmap()-ed regions and small unmapped regions)
235 * <lowermost mmap()-ed region>
236 * <BIG UNMAPPED REGION 2>
237 * <stack>
238 */
__damon_va_init_regions(struct damon_ctx * ctx,struct damon_target * t)239 static void __damon_va_init_regions(struct damon_ctx *ctx,
240 struct damon_target *t)
241 {
242 struct damon_target *ti;
243 struct damon_region *r;
244 struct damon_addr_range regions[3];
245 unsigned long sz = 0, nr_pieces;
246 int i, tidx = 0;
247
248 if (damon_va_three_regions(t, regions)) {
249 damon_for_each_target(ti, ctx) {
250 if (ti == t)
251 break;
252 tidx++;
253 }
254 pr_debug("Failed to get three regions of %dth target\n", tidx);
255 return;
256 }
257
258 for (i = 0; i < 3; i++)
259 sz += regions[i].end - regions[i].start;
260 if (ctx->attrs.min_nr_regions)
261 sz /= ctx->attrs.min_nr_regions;
262 if (sz < DAMON_MIN_REGION)
263 sz = DAMON_MIN_REGION;
264
265 /* Set the initial three regions of the target */
266 for (i = 0; i < 3; i++) {
267 r = damon_new_region(regions[i].start, regions[i].end);
268 if (!r) {
269 pr_err("%d'th init region creation failed\n", i);
270 return;
271 }
272 damon_add_region(r, t);
273
274 nr_pieces = (regions[i].end - regions[i].start) / sz;
275 damon_va_evenly_split_region(t, r, nr_pieces);
276 }
277 }
278
279 /* Initialize '->regions_list' of every target (task) */
damon_va_init(struct damon_ctx * ctx)280 static void damon_va_init(struct damon_ctx *ctx)
281 {
282 struct damon_target *t;
283
284 damon_for_each_target(t, ctx) {
285 /* the user may set the target regions as they want */
286 if (!damon_nr_regions(t))
287 __damon_va_init_regions(ctx, t);
288 }
289 }
290
291 /*
292 * Update regions for current memory mappings
293 */
damon_va_update(struct damon_ctx * ctx)294 static void damon_va_update(struct damon_ctx *ctx)
295 {
296 struct damon_addr_range three_regions[3];
297 struct damon_target *t;
298
299 damon_for_each_target(t, ctx) {
300 if (damon_va_three_regions(t, three_regions))
301 continue;
302 damon_set_regions(t, three_regions, 3);
303 }
304 }
305
damon_mkold_pmd_entry(pmd_t * pmd,unsigned long addr,unsigned long next,struct mm_walk * walk)306 static int damon_mkold_pmd_entry(pmd_t *pmd, unsigned long addr,
307 unsigned long next, struct mm_walk *walk)
308 {
309 pte_t *pte;
310 pmd_t pmde;
311 spinlock_t *ptl;
312
313 if (pmd_trans_huge(pmdp_get(pmd))) {
314 ptl = pmd_lock(walk->mm, pmd);
315 pmde = pmdp_get(pmd);
316
317 if (!pmd_present(pmde)) {
318 spin_unlock(ptl);
319 return 0;
320 }
321
322 if (pmd_trans_huge(pmde)) {
323 damon_pmdp_mkold(pmd, walk->vma, addr);
324 spin_unlock(ptl);
325 return 0;
326 }
327 spin_unlock(ptl);
328 }
329
330 pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
331 if (!pte) {
332 walk->action = ACTION_AGAIN;
333 return 0;
334 }
335 if (!pte_present(ptep_get(pte)))
336 goto out;
337 damon_ptep_mkold(pte, walk->vma, addr);
338 out:
339 pte_unmap_unlock(pte, ptl);
340 return 0;
341 }
342
343 #ifdef CONFIG_HUGETLB_PAGE
damon_hugetlb_mkold(pte_t * pte,struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr)344 static void damon_hugetlb_mkold(pte_t *pte, struct mm_struct *mm,
345 struct vm_area_struct *vma, unsigned long addr)
346 {
347 bool referenced = false;
348 pte_t entry = huge_ptep_get(mm, addr, pte);
349 struct folio *folio = pfn_folio(pte_pfn(entry));
350 unsigned long psize = huge_page_size(hstate_vma(vma));
351
352 folio_get(folio);
353
354 if (pte_young(entry)) {
355 referenced = true;
356 entry = pte_mkold(entry);
357 set_huge_pte_at(mm, addr, pte, entry, psize);
358 }
359
360 if (mmu_notifier_clear_young(mm, addr,
361 addr + huge_page_size(hstate_vma(vma))))
362 referenced = true;
363
364 if (referenced)
365 folio_set_young(folio);
366
367 folio_set_idle(folio);
368 folio_put(folio);
369 }
370
damon_mkold_hugetlb_entry(pte_t * pte,unsigned long hmask,unsigned long addr,unsigned long end,struct mm_walk * walk)371 static int damon_mkold_hugetlb_entry(pte_t *pte, unsigned long hmask,
372 unsigned long addr, unsigned long end,
373 struct mm_walk *walk)
374 {
375 struct hstate *h = hstate_vma(walk->vma);
376 spinlock_t *ptl;
377 pte_t entry;
378
379 ptl = huge_pte_lock(h, walk->mm, pte);
380 entry = huge_ptep_get(walk->mm, addr, pte);
381 if (!pte_present(entry))
382 goto out;
383
384 damon_hugetlb_mkold(pte, walk->mm, walk->vma, addr);
385
386 out:
387 spin_unlock(ptl);
388 return 0;
389 }
390 #else
391 #define damon_mkold_hugetlb_entry NULL
392 #endif /* CONFIG_HUGETLB_PAGE */
393
394 static const struct mm_walk_ops damon_mkold_ops = {
395 .pmd_entry = damon_mkold_pmd_entry,
396 .hugetlb_entry = damon_mkold_hugetlb_entry,
397 .walk_lock = PGWALK_RDLOCK,
398 };
399
damon_va_mkold(struct mm_struct * mm,unsigned long addr)400 static void damon_va_mkold(struct mm_struct *mm, unsigned long addr)
401 {
402 mmap_read_lock(mm);
403 walk_page_range(mm, addr, addr + 1, &damon_mkold_ops, NULL);
404 mmap_read_unlock(mm);
405 }
406
407 /*
408 * Functions for the access checking of the regions
409 */
410
__damon_va_prepare_access_check(struct mm_struct * mm,struct damon_region * r)411 static void __damon_va_prepare_access_check(struct mm_struct *mm,
412 struct damon_region *r)
413 {
414 r->sampling_addr = damon_rand(r->ar.start, r->ar.end);
415
416 damon_va_mkold(mm, r->sampling_addr);
417 }
418
damon_va_prepare_access_checks(struct damon_ctx * ctx)419 static void damon_va_prepare_access_checks(struct damon_ctx *ctx)
420 {
421 struct damon_target *t;
422 struct mm_struct *mm;
423 struct damon_region *r;
424
425 damon_for_each_target(t, ctx) {
426 mm = damon_get_mm(t);
427 if (!mm)
428 continue;
429 damon_for_each_region(r, t)
430 __damon_va_prepare_access_check(mm, r);
431 mmput(mm);
432 }
433 }
434
435 struct damon_young_walk_private {
436 /* size of the folio for the access checked virtual memory address */
437 unsigned long *folio_sz;
438 bool young;
439 };
440
damon_young_pmd_entry(pmd_t * pmd,unsigned long addr,unsigned long next,struct mm_walk * walk)441 static int damon_young_pmd_entry(pmd_t *pmd, unsigned long addr,
442 unsigned long next, struct mm_walk *walk)
443 {
444 pte_t *pte;
445 pte_t ptent;
446 spinlock_t *ptl;
447 struct folio *folio;
448 struct damon_young_walk_private *priv = walk->private;
449
450 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
451 if (pmd_trans_huge(pmdp_get(pmd))) {
452 pmd_t pmde;
453
454 ptl = pmd_lock(walk->mm, pmd);
455 pmde = pmdp_get(pmd);
456
457 if (!pmd_present(pmde)) {
458 spin_unlock(ptl);
459 return 0;
460 }
461
462 if (!pmd_trans_huge(pmde)) {
463 spin_unlock(ptl);
464 goto regular_page;
465 }
466 folio = damon_get_folio(pmd_pfn(pmde));
467 if (!folio)
468 goto huge_out;
469 if (pmd_young(pmde) || !folio_test_idle(folio) ||
470 mmu_notifier_test_young(walk->mm,
471 addr))
472 priv->young = true;
473 *priv->folio_sz = HPAGE_PMD_SIZE;
474 folio_put(folio);
475 huge_out:
476 spin_unlock(ptl);
477 return 0;
478 }
479
480 regular_page:
481 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
482
483 pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
484 if (!pte) {
485 walk->action = ACTION_AGAIN;
486 return 0;
487 }
488 ptent = ptep_get(pte);
489 if (!pte_present(ptent))
490 goto out;
491 folio = damon_get_folio(pte_pfn(ptent));
492 if (!folio)
493 goto out;
494 if (pte_young(ptent) || !folio_test_idle(folio) ||
495 mmu_notifier_test_young(walk->mm, addr))
496 priv->young = true;
497 *priv->folio_sz = folio_size(folio);
498 folio_put(folio);
499 out:
500 pte_unmap_unlock(pte, ptl);
501 return 0;
502 }
503
504 #ifdef CONFIG_HUGETLB_PAGE
damon_young_hugetlb_entry(pte_t * pte,unsigned long hmask,unsigned long addr,unsigned long end,struct mm_walk * walk)505 static int damon_young_hugetlb_entry(pte_t *pte, unsigned long hmask,
506 unsigned long addr, unsigned long end,
507 struct mm_walk *walk)
508 {
509 struct damon_young_walk_private *priv = walk->private;
510 struct hstate *h = hstate_vma(walk->vma);
511 struct folio *folio;
512 spinlock_t *ptl;
513 pte_t entry;
514
515 ptl = huge_pte_lock(h, walk->mm, pte);
516 entry = huge_ptep_get(walk->mm, addr, pte);
517 if (!pte_present(entry))
518 goto out;
519
520 folio = pfn_folio(pte_pfn(entry));
521 folio_get(folio);
522
523 if (pte_young(entry) || !folio_test_idle(folio) ||
524 mmu_notifier_test_young(walk->mm, addr))
525 priv->young = true;
526 *priv->folio_sz = huge_page_size(h);
527
528 folio_put(folio);
529
530 out:
531 spin_unlock(ptl);
532 return 0;
533 }
534 #else
535 #define damon_young_hugetlb_entry NULL
536 #endif /* CONFIG_HUGETLB_PAGE */
537
538 static const struct mm_walk_ops damon_young_ops = {
539 .pmd_entry = damon_young_pmd_entry,
540 .hugetlb_entry = damon_young_hugetlb_entry,
541 .walk_lock = PGWALK_RDLOCK,
542 };
543
damon_va_young(struct mm_struct * mm,unsigned long addr,unsigned long * folio_sz)544 static bool damon_va_young(struct mm_struct *mm, unsigned long addr,
545 unsigned long *folio_sz)
546 {
547 struct damon_young_walk_private arg = {
548 .folio_sz = folio_sz,
549 .young = false,
550 };
551
552 mmap_read_lock(mm);
553 walk_page_range(mm, addr, addr + 1, &damon_young_ops, &arg);
554 mmap_read_unlock(mm);
555 return arg.young;
556 }
557
558 /*
559 * Check whether the region was accessed after the last preparation
560 *
561 * mm 'mm_struct' for the given virtual address space
562 * r the region to be checked
563 */
__damon_va_check_access(struct mm_struct * mm,struct damon_region * r,bool same_target,struct damon_attrs * attrs)564 static void __damon_va_check_access(struct mm_struct *mm,
565 struct damon_region *r, bool same_target,
566 struct damon_attrs *attrs)
567 {
568 static unsigned long last_addr;
569 static unsigned long last_folio_sz = PAGE_SIZE;
570 static bool last_accessed;
571
572 if (!mm) {
573 damon_update_region_access_rate(r, false, attrs);
574 return;
575 }
576
577 /* If the region is in the last checked page, reuse the result */
578 if (same_target && (ALIGN_DOWN(last_addr, last_folio_sz) ==
579 ALIGN_DOWN(r->sampling_addr, last_folio_sz))) {
580 damon_update_region_access_rate(r, last_accessed, attrs);
581 return;
582 }
583
584 last_accessed = damon_va_young(mm, r->sampling_addr, &last_folio_sz);
585 damon_update_region_access_rate(r, last_accessed, attrs);
586
587 last_addr = r->sampling_addr;
588 }
589
damon_va_check_accesses(struct damon_ctx * ctx)590 static unsigned int damon_va_check_accesses(struct damon_ctx *ctx)
591 {
592 struct damon_target *t;
593 struct mm_struct *mm;
594 struct damon_region *r;
595 unsigned int max_nr_accesses = 0;
596 bool same_target;
597
598 damon_for_each_target(t, ctx) {
599 mm = damon_get_mm(t);
600 same_target = false;
601 damon_for_each_region(r, t) {
602 __damon_va_check_access(mm, r, same_target,
603 &ctx->attrs);
604 max_nr_accesses = max(r->nr_accesses, max_nr_accesses);
605 same_target = true;
606 }
607 if (mm)
608 mmput(mm);
609 }
610
611 return max_nr_accesses;
612 }
613
damos_va_filter_young_match(struct damos_filter * filter,struct folio * folio,struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pmd_t * pmdp)614 static bool damos_va_filter_young_match(struct damos_filter *filter,
615 struct folio *folio, struct vm_area_struct *vma,
616 unsigned long addr, pte_t *ptep, pmd_t *pmdp)
617 {
618 bool young = false;
619
620 if (ptep)
621 young = pte_young(ptep_get(ptep));
622 else if (pmdp)
623 young = pmd_young(pmdp_get(pmdp));
624
625 young = young || !folio_test_idle(folio) ||
626 mmu_notifier_test_young(vma->vm_mm, addr);
627
628 if (young && ptep)
629 damon_ptep_mkold(ptep, vma, addr);
630 else if (young && pmdp)
631 damon_pmdp_mkold(pmdp, vma, addr);
632
633 return young == filter->matching;
634 }
635
damos_va_filter_out(struct damos * scheme,struct folio * folio,struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pmd_t * pmdp)636 static bool damos_va_filter_out(struct damos *scheme, struct folio *folio,
637 struct vm_area_struct *vma, unsigned long addr,
638 pte_t *ptep, pmd_t *pmdp)
639 {
640 struct damos_filter *filter;
641 bool matched;
642
643 if (scheme->core_filters_allowed)
644 return false;
645
646 damos_for_each_ops_filter(filter, scheme) {
647 /*
648 * damos_folio_filter_match checks the young filter by doing an
649 * rmap on the folio to find its page table. However, being the
650 * vaddr scheme, we have direct access to the page tables, so
651 * use that instead.
652 */
653 if (filter->type == DAMOS_FILTER_TYPE_YOUNG)
654 matched = damos_va_filter_young_match(filter, folio,
655 vma, addr, ptep, pmdp);
656 else
657 matched = damos_folio_filter_match(filter, folio);
658
659 if (matched)
660 return !filter->allow;
661 }
662 return scheme->ops_filters_default_reject;
663 }
664
665 struct damos_va_migrate_private {
666 struct list_head *migration_lists;
667 struct damos *scheme;
668 };
669
670 /*
671 * Place the given folio in the migration_list corresponding to where the folio
672 * should be migrated.
673 *
674 * The algorithm used here is similar to weighted_interleave_nid()
675 */
damos_va_migrate_dests_add(struct folio * folio,struct vm_area_struct * vma,unsigned long addr,struct damos_migrate_dests * dests,struct list_head * migration_lists)676 static void damos_va_migrate_dests_add(struct folio *folio,
677 struct vm_area_struct *vma, unsigned long addr,
678 struct damos_migrate_dests *dests,
679 struct list_head *migration_lists)
680 {
681 pgoff_t ilx;
682 int order;
683 unsigned int target;
684 unsigned int weight_total = 0;
685 int i;
686
687 /*
688 * If dests is empty, there is only one migration list corresponding
689 * to s->target_nid.
690 */
691 if (!dests->nr_dests) {
692 i = 0;
693 goto isolate;
694 }
695
696 order = folio_order(folio);
697 ilx = vma->vm_pgoff >> order;
698 ilx += (addr - vma->vm_start) >> (PAGE_SHIFT + order);
699
700 for (i = 0; i < dests->nr_dests; i++)
701 weight_total += dests->weight_arr[i];
702
703 /* If the total weights are somehow 0, don't migrate at all */
704 if (!weight_total)
705 return;
706
707 target = ilx % weight_total;
708 for (i = 0; i < dests->nr_dests; i++) {
709 if (target < dests->weight_arr[i])
710 break;
711 target -= dests->weight_arr[i];
712 }
713
714 isolate:
715 if (!folio_isolate_lru(folio))
716 return;
717
718 list_add(&folio->lru, &migration_lists[i]);
719 }
720
721 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
damos_va_migrate_pmd_entry(pmd_t * pmd,unsigned long addr,unsigned long next,struct mm_walk * walk)722 static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
723 unsigned long next, struct mm_walk *walk)
724 {
725 struct damos_va_migrate_private *priv = walk->private;
726 struct list_head *migration_lists = priv->migration_lists;
727 struct damos *s = priv->scheme;
728 struct damos_migrate_dests *dests = &s->migrate_dests;
729 struct folio *folio;
730 spinlock_t *ptl;
731 pmd_t pmde;
732
733 ptl = pmd_lock(walk->mm, pmd);
734 pmde = pmdp_get(pmd);
735
736 if (!pmd_present(pmde) || !pmd_trans_huge(pmde))
737 goto unlock;
738
739 /* Tell page walk code to not split the PMD */
740 walk->action = ACTION_CONTINUE;
741
742 folio = damon_get_folio(pmd_pfn(pmde));
743 if (!folio)
744 goto unlock;
745
746 if (damos_va_filter_out(s, folio, walk->vma, addr, NULL, pmd))
747 goto put_folio;
748
749 damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
750 migration_lists);
751
752 put_folio:
753 folio_put(folio);
754 unlock:
755 spin_unlock(ptl);
756 return 0;
757 }
758 #else
759 #define damos_va_migrate_pmd_entry NULL
760 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
761
damos_va_migrate_pte_entry(pte_t * pte,unsigned long addr,unsigned long next,struct mm_walk * walk)762 static int damos_va_migrate_pte_entry(pte_t *pte, unsigned long addr,
763 unsigned long next, struct mm_walk *walk)
764 {
765 struct damos_va_migrate_private *priv = walk->private;
766 struct list_head *migration_lists = priv->migration_lists;
767 struct damos *s = priv->scheme;
768 struct damos_migrate_dests *dests = &s->migrate_dests;
769 struct folio *folio;
770 pte_t ptent;
771
772 ptent = ptep_get(pte);
773 if (pte_none(ptent) || !pte_present(ptent))
774 return 0;
775
776 folio = damon_get_folio(pte_pfn(ptent));
777 if (!folio)
778 return 0;
779
780 if (damos_va_filter_out(s, folio, walk->vma, addr, pte, NULL))
781 goto put_folio;
782
783 damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
784 migration_lists);
785
786 put_folio:
787 folio_put(folio);
788 return 0;
789 }
790
791 /*
792 * Functions for the target validity check and cleanup
793 */
794
damon_va_target_valid(struct damon_target * t)795 static bool damon_va_target_valid(struct damon_target *t)
796 {
797 struct task_struct *task;
798
799 task = damon_get_task_struct(t);
800 if (task) {
801 put_task_struct(task);
802 return true;
803 }
804
805 return false;
806 }
807
damon_va_cleanup_target(struct damon_target * t)808 static void damon_va_cleanup_target(struct damon_target *t)
809 {
810 put_pid(t->pid);
811 }
812
813 #ifndef CONFIG_ADVISE_SYSCALLS
damos_madvise(struct damon_target * target,struct damon_region * r,int behavior)814 static unsigned long damos_madvise(struct damon_target *target,
815 struct damon_region *r, int behavior)
816 {
817 return 0;
818 }
819 #else
damos_madvise(struct damon_target * target,struct damon_region * r,int behavior)820 static unsigned long damos_madvise(struct damon_target *target,
821 struct damon_region *r, int behavior)
822 {
823 struct mm_struct *mm;
824 unsigned long start = PAGE_ALIGN(r->ar.start);
825 unsigned long len = PAGE_ALIGN(damon_sz_region(r));
826 unsigned long applied;
827
828 mm = damon_get_mm(target);
829 if (!mm)
830 return 0;
831
832 applied = do_madvise(mm, start, len, behavior) ? 0 : len;
833 mmput(mm);
834
835 return applied;
836 }
837 #endif /* CONFIG_ADVISE_SYSCALLS */
838
damos_va_migrate(struct damon_target * target,struct damon_region * r,struct damos * s,unsigned long * sz_filter_passed)839 static unsigned long damos_va_migrate(struct damon_target *target,
840 struct damon_region *r, struct damos *s,
841 unsigned long *sz_filter_passed)
842 {
843 LIST_HEAD(folio_list);
844 struct damos_va_migrate_private priv;
845 struct mm_struct *mm;
846 int nr_dests;
847 int nid;
848 bool use_target_nid;
849 unsigned long applied = 0;
850 struct damos_migrate_dests *dests = &s->migrate_dests;
851 struct mm_walk_ops walk_ops = {
852 .pmd_entry = damos_va_migrate_pmd_entry,
853 .pte_entry = damos_va_migrate_pte_entry,
854 .walk_lock = PGWALK_RDLOCK,
855 };
856
857 use_target_nid = dests->nr_dests == 0;
858 nr_dests = use_target_nid ? 1 : dests->nr_dests;
859 priv.scheme = s;
860 priv.migration_lists = kmalloc_array(nr_dests,
861 sizeof(*priv.migration_lists), GFP_KERNEL);
862 if (!priv.migration_lists)
863 return 0;
864
865 for (int i = 0; i < nr_dests; i++)
866 INIT_LIST_HEAD(&priv.migration_lists[i]);
867
868
869 mm = damon_get_mm(target);
870 if (!mm)
871 goto free_lists;
872
873 mmap_read_lock(mm);
874 walk_page_range(mm, r->ar.start, r->ar.end, &walk_ops, &priv);
875 mmap_read_unlock(mm);
876 mmput(mm);
877
878 for (int i = 0; i < nr_dests; i++) {
879 nid = use_target_nid ? s->target_nid : dests->node_id_arr[i];
880 applied += damon_migrate_pages(&priv.migration_lists[i], nid);
881 cond_resched();
882 }
883
884 free_lists:
885 kfree(priv.migration_lists);
886 return applied * PAGE_SIZE;
887 }
888
damon_va_apply_scheme(struct damon_ctx * ctx,struct damon_target * t,struct damon_region * r,struct damos * scheme,unsigned long * sz_filter_passed)889 static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx,
890 struct damon_target *t, struct damon_region *r,
891 struct damos *scheme, unsigned long *sz_filter_passed)
892 {
893 int madv_action;
894
895 switch (scheme->action) {
896 case DAMOS_WILLNEED:
897 madv_action = MADV_WILLNEED;
898 break;
899 case DAMOS_COLD:
900 madv_action = MADV_COLD;
901 break;
902 case DAMOS_PAGEOUT:
903 madv_action = MADV_PAGEOUT;
904 break;
905 case DAMOS_HUGEPAGE:
906 madv_action = MADV_HUGEPAGE;
907 break;
908 case DAMOS_NOHUGEPAGE:
909 madv_action = MADV_NOHUGEPAGE;
910 break;
911 case DAMOS_MIGRATE_HOT:
912 case DAMOS_MIGRATE_COLD:
913 return damos_va_migrate(t, r, scheme, sz_filter_passed);
914 case DAMOS_STAT:
915 return 0;
916 default:
917 /*
918 * DAMOS actions that are not yet supported by 'vaddr'.
919 */
920 return 0;
921 }
922
923 return damos_madvise(t, r, madv_action);
924 }
925
damon_va_scheme_score(struct damon_ctx * context,struct damon_target * t,struct damon_region * r,struct damos * scheme)926 static int damon_va_scheme_score(struct damon_ctx *context,
927 struct damon_target *t, struct damon_region *r,
928 struct damos *scheme)
929 {
930
931 switch (scheme->action) {
932 case DAMOS_PAGEOUT:
933 return damon_cold_score(context, r, scheme);
934 case DAMOS_MIGRATE_HOT:
935 return damon_hot_score(context, r, scheme);
936 case DAMOS_MIGRATE_COLD:
937 return damon_cold_score(context, r, scheme);
938 default:
939 break;
940 }
941
942 return DAMOS_MAX_SCORE;
943 }
944
damon_va_initcall(void)945 static int __init damon_va_initcall(void)
946 {
947 struct damon_operations ops = {
948 .id = DAMON_OPS_VADDR,
949 .init = damon_va_init,
950 .update = damon_va_update,
951 .prepare_access_checks = damon_va_prepare_access_checks,
952 .check_accesses = damon_va_check_accesses,
953 .target_valid = damon_va_target_valid,
954 .cleanup_target = damon_va_cleanup_target,
955 .cleanup = NULL,
956 .apply_scheme = damon_va_apply_scheme,
957 .get_scheme_score = damon_va_scheme_score,
958 };
959 /* ops for fixed virtual address ranges */
960 struct damon_operations ops_fvaddr = ops;
961 int err;
962
963 /* Don't set the monitoring target regions for the entire mapping */
964 ops_fvaddr.id = DAMON_OPS_FVADDR;
965 ops_fvaddr.init = NULL;
966 ops_fvaddr.update = NULL;
967
968 err = damon_register_ops(&ops);
969 if (err)
970 return err;
971 return damon_register_ops(&ops_fvaddr);
972 };
973
974 subsys_initcall(damon_va_initcall);
975
976 #include "tests/vaddr-kunit.h"
977