1 // SPDX-License-Identifier: GPL-2.0
3 * DAMON Primitives for Virtual Address Spaces
8 #define pr_fmt(fmt) "damon-va: " fmt
10 #include <asm-generic/mman-common.h>
11 #include <linux/highmem.h>
12 #include <linux/hugetlb.h>
13 #include <linux/mmu_notifier.h>
14 #include <linux/page_idle.h>
15 #include <linux/pagewalk.h>
16 #include <linux/sched/mm.h>
18 #include "prmtv-common.h"
20 #ifdef CONFIG_DAMON_VADDR_KUNIT_TEST
21 #undef DAMON_MIN_REGION
22 #define DAMON_MIN_REGION 1
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.
29 #define damon_get_task_struct(t) \
30 (get_pid_task((struct pid *)t->id, PIDTYPE_PID))
33 * Get the mm_struct of the given target
35 * Caller _must_ put the mm_struct after use, unless it is NULL.
37 * Returns the mm_struct of the target on success, NULL on failure
39 static struct mm_struct *damon_get_mm(struct damon_target *t)
41 struct task_struct *task;
44 task = damon_get_task_struct(t);
48 mm = get_task_mm(task);
49 put_task_struct(task);
54 * Functions for the initial monitoring target regions construction
58 * Size-evenly split a region into 'nr_pieces' small regions
60 * Returns 0 on success, or negative error code otherwise.
62 static int damon_va_evenly_split_region(struct damon_target *t,
63 struct damon_region *r, unsigned int nr_pieces)
65 unsigned long sz_orig, sz_piece, orig_end;
66 struct damon_region *n = NULL, *next;
73 sz_orig = r->ar.end - r->ar.start;
74 sz_piece = ALIGN_DOWN(sz_orig / nr_pieces, DAMON_MIN_REGION);
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;
83 n = damon_new_region(start, start + sz_piece);
86 damon_insert_region(n, r, next, t);
89 /* complement last region for possible rounding error */
96 static unsigned long sz_range(struct damon_addr_range *r)
98 return r->end - r->start;
101 static void swap_ranges(struct damon_addr_range *r1,
102 struct damon_addr_range *r2)
104 struct damon_addr_range tmp;
112 * Find three regions separated by two biggest unmapped regions
114 * vma the head vma of the target address space
115 * regions an array of three address ranges that results will be saved
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
122 * Returns 0 if success, or negative error code otherwise.
124 static int __damon_va_three_regions(struct vm_area_struct *vma,
125 struct damon_addr_range regions[3])
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;
132 /* Find two biggest gaps so that first_gap > second_gap > others */
133 for (; vma; vma = vma->vm_next) {
135 start = vma->vm_start;
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);
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);
157 if (!sz_range(&second_gap) || !sz_range(&first_gap))
160 /* Sort the two biggest gaps by address */
161 if (first_gap.start > second_gap.start)
162 swap_ranges(&first_gap, &second_gap);
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);
176 * Get the three regions in the given target (task)
178 * Returns 0 on success, negative error code otherwise.
180 static int damon_va_three_regions(struct damon_target *t,
181 struct damon_addr_range regions[3])
183 struct mm_struct *mm;
186 mm = damon_get_mm(t);
191 rc = __damon_va_three_regions(mm->mmap, regions);
192 mmap_read_unlock(mm);
199 * Initialize the monitoring target regions for the given target (task)
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.
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:
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)
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.
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>
240 static void __damon_va_init_regions(struct damon_ctx *ctx,
241 struct damon_target *t)
243 struct damon_region *r;
244 struct damon_addr_range regions[3];
245 unsigned long sz = 0, nr_pieces;
248 if (damon_va_three_regions(t, regions)) {
249 pr_err("Failed to get three regions of target %lu\n", t->id);
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;
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);
264 pr_err("%d'th init region creation failed\n", i);
267 damon_add_region(r, t);
269 nr_pieces = (regions[i].end - regions[i].start) / sz;
270 damon_va_evenly_split_region(t, r, nr_pieces);
274 /* Initialize '->regions_list' of every target (task) */
275 void damon_va_init(struct damon_ctx *ctx)
277 struct damon_target *t;
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);
287 * Functions for the dynamic monitoring target regions update
291 * Check whether a region is intersecting an address range
293 * Returns true if it is.
295 static bool damon_intersect(struct damon_region *r, struct damon_addr_range *re)
297 return !(r->ar.end <= re->start || re->end <= r->ar.start);
301 * Update damon regions for the three big regions of the given target
304 * bregions the three big regions of the target
306 static void damon_va_apply_three_regions(struct damon_target *t,
307 struct damon_addr_range bregions[3])
309 struct damon_region *r, *next;
312 /* Remove regions which are not in the three big regions now */
313 damon_for_each_region_safe(r, next, t) {
314 for (i = 0; i < 3; i++) {
315 if (damon_intersect(r, &bregions[i]))
319 damon_destroy_region(r, t);
322 /* Adjust intersecting regions to fit with the three big regions */
323 for (i = 0; i < 3; i++) {
324 struct damon_region *first = NULL, *last;
325 struct damon_region *newr;
326 struct damon_addr_range *br;
329 /* Get the first and last regions which intersects with br */
330 damon_for_each_region(r, t) {
331 if (damon_intersect(r, br)) {
336 if (r->ar.start >= br->end)
340 /* no damon_region intersects with this big region */
341 newr = damon_new_region(
342 ALIGN_DOWN(br->start,
344 ALIGN(br->end, DAMON_MIN_REGION));
347 damon_insert_region(newr, damon_prev_region(r), r, t);
349 first->ar.start = ALIGN_DOWN(br->start,
351 last->ar.end = ALIGN(br->end, DAMON_MIN_REGION);
357 * Update regions for current memory mappings
359 void damon_va_update(struct damon_ctx *ctx)
361 struct damon_addr_range three_regions[3];
362 struct damon_target *t;
364 damon_for_each_target(t, ctx) {
365 if (damon_va_three_regions(t, three_regions))
367 damon_va_apply_three_regions(t, three_regions);
371 static int damon_mkold_pmd_entry(pmd_t *pmd, unsigned long addr,
372 unsigned long next, struct mm_walk *walk)
377 if (pmd_huge(*pmd)) {
378 ptl = pmd_lock(walk->mm, pmd);
379 if (pmd_huge(*pmd)) {
380 damon_pmdp_mkold(pmd, walk->mm, addr);
387 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
389 pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
390 if (!pte_present(*pte))
392 damon_ptep_mkold(pte, walk->mm, addr);
394 pte_unmap_unlock(pte, ptl);
398 static const struct mm_walk_ops damon_mkold_ops = {
399 .pmd_entry = damon_mkold_pmd_entry,
402 static void damon_va_mkold(struct mm_struct *mm, unsigned long addr)
405 walk_page_range(mm, addr, addr + 1, &damon_mkold_ops, NULL);
406 mmap_read_unlock(mm);
410 * Functions for the access checking of the regions
413 static void damon_va_prepare_access_check(struct damon_ctx *ctx,
414 struct mm_struct *mm, struct damon_region *r)
416 r->sampling_addr = damon_rand(r->ar.start, r->ar.end);
418 damon_va_mkold(mm, r->sampling_addr);
421 void damon_va_prepare_access_checks(struct damon_ctx *ctx)
423 struct damon_target *t;
424 struct mm_struct *mm;
425 struct damon_region *r;
427 damon_for_each_target(t, ctx) {
428 mm = damon_get_mm(t);
431 damon_for_each_region(r, t)
432 damon_va_prepare_access_check(ctx, mm, r);
437 struct damon_young_walk_private {
438 unsigned long *page_sz;
442 static int damon_young_pmd_entry(pmd_t *pmd, unsigned long addr,
443 unsigned long next, struct mm_walk *walk)
448 struct damon_young_walk_private *priv = walk->private;
450 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
451 if (pmd_huge(*pmd)) {
452 ptl = pmd_lock(walk->mm, pmd);
453 if (!pmd_huge(*pmd)) {
457 page = damon_get_page(pmd_pfn(*pmd));
460 if (pmd_young(*pmd) || !page_is_idle(page) ||
461 mmu_notifier_test_young(walk->mm,
463 *priv->page_sz = ((1UL) << HPAGE_PMD_SHIFT);
473 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
475 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
477 pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
478 if (!pte_present(*pte))
480 page = damon_get_page(pte_pfn(*pte));
483 if (pte_young(*pte) || !page_is_idle(page) ||
484 mmu_notifier_test_young(walk->mm, addr)) {
485 *priv->page_sz = PAGE_SIZE;
490 pte_unmap_unlock(pte, ptl);
494 static const struct mm_walk_ops damon_young_ops = {
495 .pmd_entry = damon_young_pmd_entry,
498 static bool damon_va_young(struct mm_struct *mm, unsigned long addr,
499 unsigned long *page_sz)
501 struct damon_young_walk_private arg = {
507 walk_page_range(mm, addr, addr + 1, &damon_young_ops, &arg);
508 mmap_read_unlock(mm);
513 * Check whether the region was accessed after the last preparation
515 * mm 'mm_struct' for the given virtual address space
516 * r the region to be checked
518 static void damon_va_check_access(struct damon_ctx *ctx,
519 struct mm_struct *mm, struct damon_region *r)
521 static struct mm_struct *last_mm;
522 static unsigned long last_addr;
523 static unsigned long last_page_sz = PAGE_SIZE;
524 static bool last_accessed;
526 /* If the region is in the last checked page, reuse the result */
527 if (mm == last_mm && (ALIGN_DOWN(last_addr, last_page_sz) ==
528 ALIGN_DOWN(r->sampling_addr, last_page_sz))) {
534 last_accessed = damon_va_young(mm, r->sampling_addr, &last_page_sz);
539 last_addr = r->sampling_addr;
542 unsigned int damon_va_check_accesses(struct damon_ctx *ctx)
544 struct damon_target *t;
545 struct mm_struct *mm;
546 struct damon_region *r;
547 unsigned int max_nr_accesses = 0;
549 damon_for_each_target(t, ctx) {
550 mm = damon_get_mm(t);
553 damon_for_each_region(r, t) {
554 damon_va_check_access(ctx, mm, r);
555 max_nr_accesses = max(r->nr_accesses, max_nr_accesses);
560 return max_nr_accesses;
564 * Functions for the target validity check and cleanup
567 bool damon_va_target_valid(void *target)
569 struct damon_target *t = target;
570 struct task_struct *task;
572 task = damon_get_task_struct(t);
574 put_task_struct(task);
581 #ifndef CONFIG_ADVISE_SYSCALLS
582 static int damos_madvise(struct damon_target *target, struct damon_region *r,
588 static int damos_madvise(struct damon_target *target, struct damon_region *r,
591 struct mm_struct *mm;
594 mm = damon_get_mm(target);
598 ret = do_madvise(mm, PAGE_ALIGN(r->ar.start),
599 PAGE_ALIGN(r->ar.end - r->ar.start), behavior);
604 #endif /* CONFIG_ADVISE_SYSCALLS */
606 int damon_va_apply_scheme(struct damon_ctx *ctx, struct damon_target *t,
607 struct damon_region *r, struct damos *scheme)
611 switch (scheme->action) {
613 madv_action = MADV_WILLNEED;
616 madv_action = MADV_COLD;
619 madv_action = MADV_PAGEOUT;
622 madv_action = MADV_HUGEPAGE;
624 case DAMOS_NOHUGEPAGE:
625 madv_action = MADV_NOHUGEPAGE;
633 return damos_madvise(t, r, madv_action);
636 int damon_va_scheme_score(struct damon_ctx *context, struct damon_target *t,
637 struct damon_region *r, struct damos *scheme)
640 switch (scheme->action) {
642 return damon_pageout_score(context, r, scheme);
647 return DAMOS_MAX_SCORE;
650 void damon_va_set_primitives(struct damon_ctx *ctx)
652 ctx->primitive.init = damon_va_init;
653 ctx->primitive.update = damon_va_update;
654 ctx->primitive.prepare_access_checks = damon_va_prepare_access_checks;
655 ctx->primitive.check_accesses = damon_va_check_accesses;
656 ctx->primitive.reset_aggregated = NULL;
657 ctx->primitive.target_valid = damon_va_target_valid;
658 ctx->primitive.cleanup = NULL;
659 ctx->primitive.apply_scheme = damon_va_apply_scheme;
660 ctx->primitive.get_scheme_score = damon_va_scheme_score;
663 #include "vaddr-test.h"