Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/mm/damon/vaddr.c
26278 views
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
* DAMON Code for Virtual Address Spaces
4
*
5
* Author: SeongJae Park <[email protected]>
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
*/
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
*/
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
*/
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
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
*/
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
*/
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
*/
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) */
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
*/
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
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
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
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
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
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
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
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
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
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
*/
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
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
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
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
*/
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
/* If the folio is already in the right node, don't do anything */
715
if (folio_nid(folio) == dests->node_id_arr[i])
716
return;
717
718
isolate:
719
if (!folio_isolate_lru(folio))
720
return;
721
722
list_add(&folio->lru, &migration_lists[i]);
723
}
724
725
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
726
static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
727
unsigned long next, struct mm_walk *walk)
728
{
729
struct damos_va_migrate_private *priv = walk->private;
730
struct list_head *migration_lists = priv->migration_lists;
731
struct damos *s = priv->scheme;
732
struct damos_migrate_dests *dests = &s->migrate_dests;
733
struct folio *folio;
734
spinlock_t *ptl;
735
pmd_t pmde;
736
737
ptl = pmd_lock(walk->mm, pmd);
738
pmde = pmdp_get(pmd);
739
740
if (!pmd_present(pmde) || !pmd_trans_huge(pmde))
741
goto unlock;
742
743
/* Tell page walk code to not split the PMD */
744
walk->action = ACTION_CONTINUE;
745
746
folio = damon_get_folio(pmd_pfn(pmde));
747
if (!folio)
748
goto unlock;
749
750
if (damos_va_filter_out(s, folio, walk->vma, addr, NULL, pmd))
751
goto put_folio;
752
753
damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
754
migration_lists);
755
756
put_folio:
757
folio_put(folio);
758
unlock:
759
spin_unlock(ptl);
760
return 0;
761
}
762
#else
763
#define damos_va_migrate_pmd_entry NULL
764
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
765
766
static int damos_va_migrate_pte_entry(pte_t *pte, unsigned long addr,
767
unsigned long next, struct mm_walk *walk)
768
{
769
struct damos_va_migrate_private *priv = walk->private;
770
struct list_head *migration_lists = priv->migration_lists;
771
struct damos *s = priv->scheme;
772
struct damos_migrate_dests *dests = &s->migrate_dests;
773
struct folio *folio;
774
pte_t ptent;
775
776
ptent = ptep_get(pte);
777
if (pte_none(ptent) || !pte_present(ptent))
778
return 0;
779
780
folio = damon_get_folio(pte_pfn(ptent));
781
if (!folio)
782
return 0;
783
784
if (damos_va_filter_out(s, folio, walk->vma, addr, pte, NULL))
785
goto put_folio;
786
787
damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
788
migration_lists);
789
790
put_folio:
791
folio_put(folio);
792
return 0;
793
}
794
795
/*
796
* Functions for the target validity check and cleanup
797
*/
798
799
static bool damon_va_target_valid(struct damon_target *t)
800
{
801
struct task_struct *task;
802
803
task = damon_get_task_struct(t);
804
if (task) {
805
put_task_struct(task);
806
return true;
807
}
808
809
return false;
810
}
811
812
static void damon_va_cleanup_target(struct damon_target *t)
813
{
814
put_pid(t->pid);
815
}
816
817
#ifndef CONFIG_ADVISE_SYSCALLS
818
static unsigned long damos_madvise(struct damon_target *target,
819
struct damon_region *r, int behavior)
820
{
821
return 0;
822
}
823
#else
824
static unsigned long damos_madvise(struct damon_target *target,
825
struct damon_region *r, int behavior)
826
{
827
struct mm_struct *mm;
828
unsigned long start = PAGE_ALIGN(r->ar.start);
829
unsigned long len = PAGE_ALIGN(damon_sz_region(r));
830
unsigned long applied;
831
832
mm = damon_get_mm(target);
833
if (!mm)
834
return 0;
835
836
applied = do_madvise(mm, start, len, behavior) ? 0 : len;
837
mmput(mm);
838
839
return applied;
840
}
841
#endif /* CONFIG_ADVISE_SYSCALLS */
842
843
static unsigned long damos_va_migrate(struct damon_target *target,
844
struct damon_region *r, struct damos *s,
845
unsigned long *sz_filter_passed)
846
{
847
LIST_HEAD(folio_list);
848
struct damos_va_migrate_private priv;
849
struct mm_struct *mm;
850
int nr_dests;
851
int nid;
852
bool use_target_nid;
853
unsigned long applied = 0;
854
struct damos_migrate_dests *dests = &s->migrate_dests;
855
struct mm_walk_ops walk_ops = {
856
.pmd_entry = damos_va_migrate_pmd_entry,
857
.pte_entry = damos_va_migrate_pte_entry,
858
.walk_lock = PGWALK_RDLOCK,
859
};
860
861
use_target_nid = dests->nr_dests == 0;
862
nr_dests = use_target_nid ? 1 : dests->nr_dests;
863
priv.scheme = s;
864
priv.migration_lists = kmalloc_array(nr_dests,
865
sizeof(*priv.migration_lists), GFP_KERNEL);
866
if (!priv.migration_lists)
867
return 0;
868
869
for (int i = 0; i < nr_dests; i++)
870
INIT_LIST_HEAD(&priv.migration_lists[i]);
871
872
873
mm = damon_get_mm(target);
874
if (!mm)
875
goto free_lists;
876
877
mmap_read_lock(mm);
878
walk_page_range(mm, r->ar.start, r->ar.end, &walk_ops, &priv);
879
mmap_read_unlock(mm);
880
mmput(mm);
881
882
for (int i = 0; i < nr_dests; i++) {
883
nid = use_target_nid ? s->target_nid : dests->node_id_arr[i];
884
applied += damon_migrate_pages(&priv.migration_lists[i], nid);
885
cond_resched();
886
}
887
888
free_lists:
889
kfree(priv.migration_lists);
890
return applied * PAGE_SIZE;
891
}
892
893
static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx,
894
struct damon_target *t, struct damon_region *r,
895
struct damos *scheme, unsigned long *sz_filter_passed)
896
{
897
int madv_action;
898
899
switch (scheme->action) {
900
case DAMOS_WILLNEED:
901
madv_action = MADV_WILLNEED;
902
break;
903
case DAMOS_COLD:
904
madv_action = MADV_COLD;
905
break;
906
case DAMOS_PAGEOUT:
907
madv_action = MADV_PAGEOUT;
908
break;
909
case DAMOS_HUGEPAGE:
910
madv_action = MADV_HUGEPAGE;
911
break;
912
case DAMOS_NOHUGEPAGE:
913
madv_action = MADV_NOHUGEPAGE;
914
break;
915
case DAMOS_MIGRATE_HOT:
916
case DAMOS_MIGRATE_COLD:
917
return damos_va_migrate(t, r, scheme, sz_filter_passed);
918
case DAMOS_STAT:
919
return 0;
920
default:
921
/*
922
* DAMOS actions that are not yet supported by 'vaddr'.
923
*/
924
return 0;
925
}
926
927
return damos_madvise(t, r, madv_action);
928
}
929
930
static int damon_va_scheme_score(struct damon_ctx *context,
931
struct damon_target *t, struct damon_region *r,
932
struct damos *scheme)
933
{
934
935
switch (scheme->action) {
936
case DAMOS_PAGEOUT:
937
return damon_cold_score(context, r, scheme);
938
case DAMOS_MIGRATE_HOT:
939
return damon_hot_score(context, r, scheme);
940
case DAMOS_MIGRATE_COLD:
941
return damon_cold_score(context, r, scheme);
942
default:
943
break;
944
}
945
946
return DAMOS_MAX_SCORE;
947
}
948
949
static int __init damon_va_initcall(void)
950
{
951
struct damon_operations ops = {
952
.id = DAMON_OPS_VADDR,
953
.init = damon_va_init,
954
.update = damon_va_update,
955
.prepare_access_checks = damon_va_prepare_access_checks,
956
.check_accesses = damon_va_check_accesses,
957
.target_valid = damon_va_target_valid,
958
.cleanup_target = damon_va_cleanup_target,
959
.cleanup = NULL,
960
.apply_scheme = damon_va_apply_scheme,
961
.get_scheme_score = damon_va_scheme_score,
962
};
963
/* ops for fixed virtual address ranges */
964
struct damon_operations ops_fvaddr = ops;
965
int err;
966
967
/* Don't set the monitoring target regions for the entire mapping */
968
ops_fvaddr.id = DAMON_OPS_FVADDR;
969
ops_fvaddr.init = NULL;
970
ops_fvaddr.update = NULL;
971
972
err = damon_register_ops(&ops);
973
if (err)
974
return err;
975
return damon_register_ops(&ops_fvaddr);
976
};
977
978
subsys_initcall(damon_va_initcall);
979
980
#include "tests/vaddr-kunit.h"
981
982