Merge tag 'soc-drivers-6.9' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-block.git] / mm / damon / core.c
CommitLineData
2224d848
SP
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Data Access Monitor
4 *
6ad59a38 5 * Author: SeongJae Park <sj@kernel.org>
2224d848
SP
6 */
7
8#define pr_fmt(fmt) "damon: " fmt
9
10#include <linux/damon.h>
11#include <linux/delay.h>
12#include <linux/kthread.h>
ee801b7d 13#include <linux/mm.h>
2224d848 14#include <linux/slab.h>
38683e00 15#include <linux/string.h>
2224d848 16
2fcb9362
SP
17#define CREATE_TRACE_POINTS
18#include <trace/events/damon.h>
19
17ccae8b
SP
20#ifdef CONFIG_DAMON_KUNIT_TEST
21#undef DAMON_MIN_REGION
22#define DAMON_MIN_REGION 1
23#endif
24
2224d848
SP
25static DEFINE_MUTEX(damon_lock);
26static int nr_running_ctxs;
8b9b0d33 27static bool running_exclusive_ctxs;
2224d848 28
9f7b053a
SP
29static DEFINE_MUTEX(damon_ops_lock);
30static struct damon_operations damon_registered_ops[NR_DAMON_OPS];
31
a1870944
DL
32static struct kmem_cache *damon_region_cache __ro_after_init;
33
9f7b053a 34/* Should be called under damon_ops_lock with id smaller than NR_DAMON_OPS */
152e5617 35static bool __damon_is_registered_ops(enum damon_ops_id id)
9f7b053a
SP
36{
37 struct damon_operations empty_ops = {};
38
39 if (!memcmp(&empty_ops, &damon_registered_ops[id], sizeof(empty_ops)))
40 return false;
41 return true;
42}
43
152e5617
SP
44/**
45 * damon_is_registered_ops() - Check if a given damon_operations is registered.
46 * @id: Id of the damon_operations to check if registered.
47 *
48 * Return: true if the ops is set, false otherwise.
49 */
50bool damon_is_registered_ops(enum damon_ops_id id)
51{
52 bool registered;
53
54 if (id >= NR_DAMON_OPS)
55 return false;
56 mutex_lock(&damon_ops_lock);
57 registered = __damon_is_registered_ops(id);
58 mutex_unlock(&damon_ops_lock);
59 return registered;
60}
61
9f7b053a
SP
62/**
63 * damon_register_ops() - Register a monitoring operations set to DAMON.
64 * @ops: monitoring operations set to register.
65 *
66 * This function registers a monitoring operations set of valid &struct
67 * damon_operations->id so that others can find and use them later.
68 *
69 * Return: 0 on success, negative error code otherwise.
70 */
71int damon_register_ops(struct damon_operations *ops)
72{
73 int err = 0;
74
75 if (ops->id >= NR_DAMON_OPS)
76 return -EINVAL;
77 mutex_lock(&damon_ops_lock);
78 /* Fail for already registered ops */
152e5617 79 if (__damon_is_registered_ops(ops->id)) {
9f7b053a
SP
80 err = -EINVAL;
81 goto out;
82 }
83 damon_registered_ops[ops->id] = *ops;
84out:
85 mutex_unlock(&damon_ops_lock);
86 return err;
87}
88
89/**
90 * damon_select_ops() - Select a monitoring operations to use with the context.
91 * @ctx: monitoring context to use the operations.
92 * @id: id of the registered monitoring operations to select.
93 *
94 * This function finds registered monitoring operations set of @id and make
95 * @ctx to use it.
96 *
97 * Return: 0 on success, negative error code otherwise.
98 */
99int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id)
100{
101 int err = 0;
102
103 if (id >= NR_DAMON_OPS)
104 return -EINVAL;
105
106 mutex_lock(&damon_ops_lock);
152e5617 107 if (!__damon_is_registered_ops(id))
9f7b053a
SP
108 err = -EINVAL;
109 else
110 ctx->ops = damon_registered_ops[id];
111 mutex_unlock(&damon_ops_lock);
112 return err;
113}
114
f23b8eee
SP
115/*
116 * Construct a damon_region struct
117 *
118 * Returns the pointer to the new struct if success, or NULL otherwise
119 */
120struct damon_region *damon_new_region(unsigned long start, unsigned long end)
121{
122 struct damon_region *region;
123
a1870944 124 region = kmem_cache_alloc(damon_region_cache, GFP_KERNEL);
f23b8eee
SP
125 if (!region)
126 return NULL;
127
128 region->ar.start = start;
129 region->ar.end = end;
130 region->nr_accesses = 0;
80333828 131 region->nr_accesses_bp = 0;
f23b8eee
SP
132 INIT_LIST_HEAD(&region->list);
133
fda504fa
SP
134 region->age = 0;
135 region->last_nr_accesses = 0;
136
f23b8eee
SP
137 return region;
138}
139
f23b8eee
SP
140void damon_add_region(struct damon_region *r, struct damon_target *t)
141{
142 list_add_tail(&r->list, &t->regions_list);
b9a6ac4e 143 t->nr_regions++;
f23b8eee
SP
144}
145
b9a6ac4e 146static void damon_del_region(struct damon_region *r, struct damon_target *t)
f23b8eee
SP
147{
148 list_del(&r->list);
b9a6ac4e 149 t->nr_regions--;
f23b8eee
SP
150}
151
152static void damon_free_region(struct damon_region *r)
153{
a1870944 154 kmem_cache_free(damon_region_cache, r);
f23b8eee
SP
155}
156
b9a6ac4e 157void damon_destroy_region(struct damon_region *r, struct damon_target *t)
f23b8eee 158{
b9a6ac4e 159 damon_del_region(r, t);
f23b8eee
SP
160 damon_free_region(r);
161}
162
d0723bc0
SP
163/*
164 * Check whether a region is intersecting an address range
165 *
166 * Returns true if it is.
167 */
168static bool damon_intersect(struct damon_region *r,
169 struct damon_addr_range *re)
170{
171 return !(r->ar.end <= re->start || re->end <= r->ar.start);
172}
173
9c950c22
SP
174/*
175 * Fill holes in regions with new regions.
176 */
177static int damon_fill_regions_holes(struct damon_region *first,
178 struct damon_region *last, struct damon_target *t)
179{
180 struct damon_region *r = first;
181
182 damon_for_each_region_from(r, t) {
183 struct damon_region *next, *newr;
184
185 if (r == last)
186 break;
187 next = damon_next_region(r);
188 if (r->ar.end != next->ar.start) {
189 newr = damon_new_region(r->ar.end, next->ar.start);
190 if (!newr)
191 return -ENOMEM;
192 damon_insert_region(newr, r, next, t);
193 }
194 }
195 return 0;
196}
197
d0723bc0
SP
198/*
199 * damon_set_regions() - Set regions of a target for given address ranges.
200 * @t: the given target.
201 * @ranges: array of new monitoring target ranges.
202 * @nr_ranges: length of @ranges.
203 *
204 * This function adds new regions to, or modify existing regions of a
205 * monitoring target to fit in specific ranges.
206 *
207 * Return: 0 if success, or negative error code otherwise.
208 */
209int damon_set_regions(struct damon_target *t, struct damon_addr_range *ranges,
210 unsigned int nr_ranges)
211{
212 struct damon_region *r, *next;
213 unsigned int i;
9c950c22 214 int err;
d0723bc0
SP
215
216 /* Remove regions which are not in the new ranges */
217 damon_for_each_region_safe(r, next, t) {
218 for (i = 0; i < nr_ranges; i++) {
219 if (damon_intersect(r, &ranges[i]))
220 break;
221 }
222 if (i == nr_ranges)
223 damon_destroy_region(r, t);
224 }
225
36001cba 226 r = damon_first_region(t);
d0723bc0
SP
227 /* Add new regions or resize existing regions to fit in the ranges */
228 for (i = 0; i < nr_ranges; i++) {
229 struct damon_region *first = NULL, *last, *newr;
230 struct damon_addr_range *range;
231
232 range = &ranges[i];
233 /* Get the first/last regions intersecting with the range */
36001cba 234 damon_for_each_region_from(r, t) {
d0723bc0
SP
235 if (damon_intersect(r, range)) {
236 if (!first)
237 first = r;
238 last = r;
239 }
240 if (r->ar.start >= range->end)
241 break;
242 }
243 if (!first) {
244 /* no region intersects with this range */
245 newr = damon_new_region(
246 ALIGN_DOWN(range->start,
247 DAMON_MIN_REGION),
248 ALIGN(range->end, DAMON_MIN_REGION));
249 if (!newr)
250 return -ENOMEM;
251 damon_insert_region(newr, damon_prev_region(r), r, t);
252 } else {
253 /* resize intersecting regions to fit in this range */
254 first->ar.start = ALIGN_DOWN(range->start,
255 DAMON_MIN_REGION);
256 last->ar.end = ALIGN(range->end, DAMON_MIN_REGION);
9c950c22
SP
257
258 /* fill possible holes in the range */
259 err = damon_fill_regions_holes(first, last, t);
260 if (err)
261 return err;
d0723bc0
SP
262 }
263 }
264 return 0;
265}
266
98def236
SP
267struct damos_filter *damos_new_filter(enum damos_filter_type type,
268 bool matching)
269{
270 struct damos_filter *filter;
271
272 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
273 if (!filter)
274 return NULL;
275 filter->type = type;
276 filter->matching = matching;
5f1fc67f 277 INIT_LIST_HEAD(&filter->list);
98def236
SP
278 return filter;
279}
280
281void damos_add_filter(struct damos *s, struct damos_filter *f)
282{
283 list_add_tail(&f->list, &s->filters);
284}
285
286static void damos_del_filter(struct damos_filter *f)
287{
288 list_del(&f->list);
289}
290
291static void damos_free_filter(struct damos_filter *f)
292{
293 kfree(f);
294}
295
296void damos_destroy_filter(struct damos_filter *f)
297{
298 damos_del_filter(f);
299 damos_free_filter(f);
300}
301
70e0c1d1
SP
302/* initialize private fields of damos_quota and return the pointer */
303static struct damos_quota *damos_quota_init_priv(struct damos_quota *quota)
304{
305 quota->total_charged_sz = 0;
306 quota->total_charged_ns = 0;
307 quota->esz = 0;
308 quota->charged_sz = 0;
309 quota->charged_from = 0;
310 quota->charge_target_from = NULL;
311 quota->charge_addr_from = 0;
312 return quota;
313}
314
f5a79d7c 315struct damos *damon_new_scheme(struct damos_access_pattern *pattern,
42f994b7
SP
316 enum damos_action action,
317 unsigned long apply_interval_us,
318 struct damos_quota *quota,
f5a79d7c 319 struct damos_watermarks *wmarks)
1f366e42
SP
320{
321 struct damos *scheme;
322
323 scheme = kmalloc(sizeof(*scheme), GFP_KERNEL);
324 if (!scheme)
325 return NULL;
02f17037 326 scheme->pattern = *pattern;
1f366e42 327 scheme->action = action;
42f994b7
SP
328 scheme->apply_interval_us = apply_interval_us;
329 /*
330 * next_apply_sis will be set when kdamond starts. While kdamond is
331 * running, it will also updated when it is added to the DAMON context,
332 * or damon_attrs are updated.
333 */
334 scheme->next_apply_sis = 0;
98def236 335 INIT_LIST_HEAD(&scheme->filters);
0e92c2ee 336 scheme->stat = (struct damos_stat){};
1f366e42
SP
337 INIT_LIST_HEAD(&scheme->list);
338
70e0c1d1 339 scheme->quota = *(damos_quota_init_priv(quota));
2b8a248d 340
02f17037 341 scheme->wmarks = *wmarks;
ee801b7d
SP
342 scheme->wmarks.activated = true;
343
1f366e42
SP
344 return scheme;
345}
346
42f994b7
SP
347static void damos_set_next_apply_sis(struct damos *s, struct damon_ctx *ctx)
348{
349 unsigned long sample_interval = ctx->attrs.sample_interval ?
350 ctx->attrs.sample_interval : 1;
351 unsigned long apply_interval = s->apply_interval_us ?
352 s->apply_interval_us : ctx->attrs.aggr_interval;
353
354 s->next_apply_sis = ctx->passed_sample_intervals +
355 apply_interval / sample_interval;
356}
357
1f366e42
SP
358void damon_add_scheme(struct damon_ctx *ctx, struct damos *s)
359{
360 list_add_tail(&s->list, &ctx->schemes);
42f994b7 361 damos_set_next_apply_sis(s, ctx);
1f366e42
SP
362}
363
364static void damon_del_scheme(struct damos *s)
365{
366 list_del(&s->list);
367}
368
369static void damon_free_scheme(struct damos *s)
370{
371 kfree(s);
372}
373
374void damon_destroy_scheme(struct damos *s)
375{
98def236
SP
376 struct damos_filter *f, *next;
377
378 damos_for_each_filter_safe(f, next, s)
379 damos_destroy_filter(f);
1f366e42
SP
380 damon_del_scheme(s);
381 damon_free_scheme(s);
382}
383
f23b8eee
SP
384/*
385 * Construct a damon_target struct
386 *
387 * Returns the pointer to the new struct if success, or NULL otherwise
388 */
1971bd63 389struct damon_target *damon_new_target(void)
f23b8eee
SP
390{
391 struct damon_target *t;
392
393 t = kmalloc(sizeof(*t), GFP_KERNEL);
394 if (!t)
395 return NULL;
396
1971bd63 397 t->pid = NULL;
b9a6ac4e 398 t->nr_regions = 0;
f23b8eee 399 INIT_LIST_HEAD(&t->regions_list);
b1f44cda 400 INIT_LIST_HEAD(&t->list);
f23b8eee
SP
401
402 return t;
403}
404
405void damon_add_target(struct damon_ctx *ctx, struct damon_target *t)
406{
b9a6ac4e 407 list_add_tail(&t->list, &ctx->adaptive_targets);
f23b8eee
SP
408}
409
b5ca3e83
XH
410bool damon_targets_empty(struct damon_ctx *ctx)
411{
412 return list_empty(&ctx->adaptive_targets);
413}
414
f23b8eee
SP
415static void damon_del_target(struct damon_target *t)
416{
417 list_del(&t->list);
418}
419
420void damon_free_target(struct damon_target *t)
421{
422 struct damon_region *r, *next;
423
424 damon_for_each_region_safe(r, next, t)
425 damon_free_region(r);
426 kfree(t);
427}
428
429void damon_destroy_target(struct damon_target *t)
430{
431 damon_del_target(t);
432 damon_free_target(t);
433}
434
b9a6ac4e
SP
435unsigned int damon_nr_regions(struct damon_target *t)
436{
437 return t->nr_regions;
438}
439
2224d848
SP
440struct damon_ctx *damon_new_ctx(void)
441{
442 struct damon_ctx *ctx;
443
444 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
445 if (!ctx)
446 return NULL;
447
6376a824
SP
448 init_completion(&ctx->kdamond_started);
449
cbeaa77b
SP
450 ctx->attrs.sample_interval = 5 * 1000;
451 ctx->attrs.aggr_interval = 100 * 1000;
452 ctx->attrs.ops_update_interval = 60 * 1000 * 1000;
2224d848 453
4472edf6
SP
454 ctx->passed_sample_intervals = 0;
455 /* These will be set from kdamond_init_intervals_sis() */
456 ctx->next_aggregation_sis = 0;
457 ctx->next_ops_update_sis = 0;
2224d848
SP
458
459 mutex_init(&ctx->kdamond_lock);
460
cbeaa77b
SP
461 ctx->attrs.min_nr_regions = 10;
462 ctx->attrs.max_nr_regions = 1000;
b9a6ac4e
SP
463
464 INIT_LIST_HEAD(&ctx->adaptive_targets);
1f366e42 465 INIT_LIST_HEAD(&ctx->schemes);
2224d848
SP
466
467 return ctx;
468}
469
f23b8eee 470static void damon_destroy_targets(struct damon_ctx *ctx)
2224d848 471{
f23b8eee
SP
472 struct damon_target *t, *next_t;
473
f7d911c3
SP
474 if (ctx->ops.cleanup) {
475 ctx->ops.cleanup(ctx);
f23b8eee
SP
476 return;
477 }
478
479 damon_for_each_target_safe(t, next_t, ctx)
480 damon_destroy_target(t);
481}
482
483void damon_destroy_ctx(struct damon_ctx *ctx)
484{
1f366e42
SP
485 struct damos *s, *next_s;
486
f23b8eee 487 damon_destroy_targets(ctx);
1f366e42
SP
488
489 damon_for_each_scheme_safe(s, next_s, ctx)
490 damon_destroy_scheme(s);
491
2224d848
SP
492 kfree(ctx);
493}
494
2f5bef5a
SP
495static unsigned int damon_age_for_new_attrs(unsigned int age,
496 struct damon_attrs *old_attrs, struct damon_attrs *new_attrs)
497{
498 return age * old_attrs->aggr_interval / new_attrs->aggr_interval;
499}
500
501/* convert access ratio in bp (per 10,000) to nr_accesses */
502static unsigned int damon_accesses_bp_to_nr_accesses(
503 unsigned int accesses_bp, struct damon_attrs *attrs)
504{
d35963bf 505 return accesses_bp * damon_max_nr_accesses(attrs) / 10000;
2f5bef5a
SP
506}
507
508/* convert nr_accesses to access ratio in bp (per 10,000) */
509static unsigned int damon_nr_accesses_to_accesses_bp(
510 unsigned int nr_accesses, struct damon_attrs *attrs)
511{
d35963bf 512 return nr_accesses * 10000 / damon_max_nr_accesses(attrs);
2f5bef5a
SP
513}
514
515static unsigned int damon_nr_accesses_for_new_attrs(unsigned int nr_accesses,
516 struct damon_attrs *old_attrs, struct damon_attrs *new_attrs)
517{
518 return damon_accesses_bp_to_nr_accesses(
519 damon_nr_accesses_to_accesses_bp(
520 nr_accesses, old_attrs),
521 new_attrs);
522}
523
524static void damon_update_monitoring_result(struct damon_region *r,
525 struct damon_attrs *old_attrs, struct damon_attrs *new_attrs)
526{
527 r->nr_accesses = damon_nr_accesses_for_new_attrs(r->nr_accesses,
528 old_attrs, new_attrs);
80333828 529 r->nr_accesses_bp = r->nr_accesses * 10000;
2f5bef5a
SP
530 r->age = damon_age_for_new_attrs(r->age, old_attrs, new_attrs);
531}
532
533/*
534 * region->nr_accesses is the number of sampling intervals in the last
535 * aggregation interval that access to the region has found, and region->age is
536 * the number of aggregation intervals that its access pattern has maintained.
537 * For the reason, the real meaning of the two fields depend on current
538 * sampling interval and aggregation interval. This function updates
539 * ->nr_accesses and ->age of given damon_ctx's regions for new damon_attrs.
540 */
541static void damon_update_monitoring_results(struct damon_ctx *ctx,
542 struct damon_attrs *new_attrs)
543{
544 struct damon_attrs *old_attrs = &ctx->attrs;
545 struct damon_target *t;
546 struct damon_region *r;
547
548 /* if any interval is zero, simply forgive conversion */
549 if (!old_attrs->sample_interval || !old_attrs->aggr_interval ||
550 !new_attrs->sample_interval ||
551 !new_attrs->aggr_interval)
552 return;
553
554 damon_for_each_target(t, ctx)
555 damon_for_each_region(r, t)
556 damon_update_monitoring_result(
557 r, old_attrs, new_attrs);
558}
559
2224d848
SP
560/**
561 * damon_set_attrs() - Set attributes for the monitoring.
562 * @ctx: monitoring context
bead3b00 563 * @attrs: monitoring attributes
2224d848 564 *
27e68c4b
SP
565 * This function should be called while the kdamond is not running, or an
566 * access check results aggregation is not ongoing (e.g., from
567 * &struct damon_callback->after_aggregation or
568 * &struct damon_callback->after_wmarks_check callbacks).
569 *
2224d848
SP
570 * Every time interval is in micro-seconds.
571 *
572 * Return: 0 on success, negative error code otherwise.
573 */
bead3b00 574int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)
2224d848 575{
4472edf6
SP
576 unsigned long sample_interval = attrs->sample_interval ?
577 attrs->sample_interval : 1;
42f994b7 578 struct damos *s;
4472edf6 579
bead3b00 580 if (attrs->min_nr_regions < 3)
b9a6ac4e 581 return -EINVAL;
bead3b00 582 if (attrs->min_nr_regions > attrs->max_nr_regions)
b9a6ac4e 583 return -EINVAL;
5ff6e2ff
KW
584 if (attrs->sample_interval > attrs->aggr_interval)
585 return -EINVAL;
b9a6ac4e 586
4472edf6
SP
587 ctx->next_aggregation_sis = ctx->passed_sample_intervals +
588 attrs->aggr_interval / sample_interval;
589 ctx->next_ops_update_sis = ctx->passed_sample_intervals +
590 attrs->ops_update_interval / sample_interval;
591
2f5bef5a 592 damon_update_monitoring_results(ctx, attrs);
bead3b00 593 ctx->attrs = *attrs;
42f994b7
SP
594
595 damon_for_each_scheme(s, ctx)
596 damos_set_next_apply_sis(s, ctx);
597
2224d848
SP
598 return 0;
599}
600
1f366e42
SP
601/**
602 * damon_set_schemes() - Set data access monitoring based operation schemes.
603 * @ctx: monitoring context
604 * @schemes: array of the schemes
605 * @nr_schemes: number of entries in @schemes
606 *
607 * This function should not be called while the kdamond of the context is
608 * running.
1f366e42 609 */
cc713520 610void damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes,
1f366e42
SP
611 ssize_t nr_schemes)
612{
613 struct damos *s, *next;
614 ssize_t i;
615
616 damon_for_each_scheme_safe(s, next, ctx)
617 damon_destroy_scheme(s);
618 for (i = 0; i < nr_schemes; i++)
619 damon_add_scheme(ctx, schemes[i]);
1f366e42
SP
620}
621
4bc05954
SP
622/**
623 * damon_nr_running_ctxs() - Return number of currently running contexts.
624 */
625int damon_nr_running_ctxs(void)
626{
627 int nr_ctxs;
628
629 mutex_lock(&damon_lock);
630 nr_ctxs = nr_running_ctxs;
631 mutex_unlock(&damon_lock);
632
633 return nr_ctxs;
634}
635
b9a6ac4e
SP
636/* Returns the size upper limit for each monitoring region */
637static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
638{
639 struct damon_target *t;
640 struct damon_region *r;
641 unsigned long sz = 0;
642
643 damon_for_each_target(t, ctx) {
644 damon_for_each_region(r, t)
ab63f63f 645 sz += damon_sz_region(r);
b9a6ac4e
SP
646 }
647
cbeaa77b
SP
648 if (ctx->attrs.min_nr_regions)
649 sz /= ctx->attrs.min_nr_regions;
b9a6ac4e
SP
650 if (sz < DAMON_MIN_REGION)
651 sz = DAMON_MIN_REGION;
652
653 return sz;
654}
655
2224d848
SP
656static int kdamond_fn(void *data);
657
658/*
659 * __damon_start() - Starts monitoring with given context.
660 * @ctx: monitoring context
661 *
662 * This function should be called while damon_lock is hold.
663 *
664 * Return: 0 on success, negative error code otherwise.
665 */
666static int __damon_start(struct damon_ctx *ctx)
667{
668 int err = -EBUSY;
669
670 mutex_lock(&ctx->kdamond_lock);
671 if (!ctx->kdamond) {
672 err = 0;
6376a824 673 reinit_completion(&ctx->kdamond_started);
2224d848
SP
674 ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d",
675 nr_running_ctxs);
676 if (IS_ERR(ctx->kdamond)) {
677 err = PTR_ERR(ctx->kdamond);
7ec1992b 678 ctx->kdamond = NULL;
6376a824
SP
679 } else {
680 wait_for_completion(&ctx->kdamond_started);
2224d848
SP
681 }
682 }
683 mutex_unlock(&ctx->kdamond_lock);
684
685 return err;
686}
687
688/**
689 * damon_start() - Starts the monitorings for a given group of contexts.
690 * @ctxs: an array of the pointers for contexts to start monitoring
691 * @nr_ctxs: size of @ctxs
8b9b0d33 692 * @exclusive: exclusiveness of this contexts group
2224d848
SP
693 *
694 * This function starts a group of monitoring threads for a group of monitoring
695 * contexts. One thread per each context is created and run in parallel. The
8b9b0d33
SP
696 * caller should handle synchronization between the threads by itself. If
697 * @exclusive is true and a group of threads that created by other
698 * 'damon_start()' call is currently running, this function does nothing but
699 * returns -EBUSY.
2224d848
SP
700 *
701 * Return: 0 on success, negative error code otherwise.
702 */
8b9b0d33 703int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive)
2224d848
SP
704{
705 int i;
706 int err = 0;
707
708 mutex_lock(&damon_lock);
8b9b0d33
SP
709 if ((exclusive && nr_running_ctxs) ||
710 (!exclusive && running_exclusive_ctxs)) {
2224d848
SP
711 mutex_unlock(&damon_lock);
712 return -EBUSY;
713 }
714
715 for (i = 0; i < nr_ctxs; i++) {
716 err = __damon_start(ctxs[i]);
717 if (err)
718 break;
719 nr_running_ctxs++;
720 }
8b9b0d33
SP
721 if (exclusive && nr_running_ctxs)
722 running_exclusive_ctxs = true;
2224d848
SP
723 mutex_unlock(&damon_lock);
724
725 return err;
726}
727
728/*
8b9b0d33 729 * __damon_stop() - Stops monitoring of a given context.
2224d848
SP
730 * @ctx: monitoring context
731 *
732 * Return: 0 on success, negative error code otherwise.
733 */
734static int __damon_stop(struct damon_ctx *ctx)
735{
0f91d133
CD
736 struct task_struct *tsk;
737
2224d848 738 mutex_lock(&ctx->kdamond_lock);
0f91d133
CD
739 tsk = ctx->kdamond;
740 if (tsk) {
741 get_task_struct(tsk);
2224d848 742 mutex_unlock(&ctx->kdamond_lock);
6309727e 743 kthread_stop_put(tsk);
2224d848
SP
744 return 0;
745 }
746 mutex_unlock(&ctx->kdamond_lock);
747
748 return -EPERM;
749}
750
751/**
752 * damon_stop() - Stops the monitorings for a given group of contexts.
753 * @ctxs: an array of the pointers for contexts to stop monitoring
754 * @nr_ctxs: size of @ctxs
755 *
756 * Return: 0 on success, negative error code otherwise.
757 */
758int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
759{
760 int i, err = 0;
761
762 for (i = 0; i < nr_ctxs; i++) {
763 /* nr_running_ctxs is decremented in kdamond_fn */
764 err = __damon_stop(ctxs[i]);
765 if (err)
8b9b0d33 766 break;
2224d848 767 }
2224d848
SP
768 return err;
769}
770
f23b8eee
SP
771/*
772 * Reset the aggregated monitoring results ('nr_accesses' of each region).
773 */
774static void kdamond_reset_aggregated(struct damon_ctx *c)
775{
776 struct damon_target *t;
76fd0285 777 unsigned int ti = 0; /* target's index */
f23b8eee
SP
778
779 damon_for_each_target(t, c) {
780 struct damon_region *r;
781
2fcb9362 782 damon_for_each_region(r, t) {
2d00946b 783 trace_damon_aggregated(ti, r, damon_nr_regions(t));
fda504fa 784 r->last_nr_accesses = r->nr_accesses;
f23b8eee 785 r->nr_accesses = 0;
2fcb9362 786 }
76fd0285 787 ti++;
f23b8eee
SP
788 }
789}
790
4ed98243
KX
791static void damon_split_region_at(struct damon_target *t,
792 struct damon_region *r, unsigned long sz_r);
2b8a248d 793
38683e00
SP
794static bool __damos_valid_target(struct damon_region *r, struct damos *s)
795{
796 unsigned long sz;
affa87c7 797 unsigned int nr_accesses = r->nr_accesses_bp / 10000;
38683e00 798
ab63f63f 799 sz = damon_sz_region(r);
f5a79d7c
YD
800 return s->pattern.min_sz_region <= sz &&
801 sz <= s->pattern.max_sz_region &&
affa87c7
SP
802 s->pattern.min_nr_accesses <= nr_accesses &&
803 nr_accesses <= s->pattern.max_nr_accesses &&
f5a79d7c
YD
804 s->pattern.min_age_region <= r->age &&
805 r->age <= s->pattern.max_age_region;
38683e00
SP
806}
807
808static bool damos_valid_target(struct damon_ctx *c, struct damon_target *t,
809 struct damon_region *r, struct damos *s)
810{
811 bool ret = __damos_valid_target(r, s);
812
f7d911c3 813 if (!ret || !s->quota.esz || !c->ops.get_scheme_score)
38683e00
SP
814 return ret;
815
f7d911c3 816 return c->ops.get_scheme_score(c, t, r, s) >= s->quota.min_score;
38683e00
SP
817}
818
2ea34989
SP
819/*
820 * damos_skip_charged_region() - Check if the given region or starting part of
821 * it is already charged for the DAMOS quota.
822 * @t: The target of the region.
823 * @rp: The pointer to the region.
824 * @s: The scheme to be applied.
825 *
826 * If a quota of a scheme has exceeded in a quota charge window, the scheme's
827 * action would applied to only a part of the target access pattern fulfilling
828 * regions. To avoid applying the scheme action to only already applied
829 * regions, DAMON skips applying the scheme action to the regions that charged
830 * in the previous charge window.
831 *
832 * This function checks if a given region should be skipped or not for the
833 * reason. If only the starting part of the region has previously charged,
834 * this function splits the region into two so that the second one covers the
835 * area that not charged in the previous charge widnow and saves the second
836 * region in *rp and returns false, so that the caller can apply DAMON action
837 * to the second one.
838 *
839 * Return: true if the region should be entirely skipped, false otherwise.
840 */
841static bool damos_skip_charged_region(struct damon_target *t,
842 struct damon_region **rp, struct damos *s)
843{
844 struct damon_region *r = *rp;
845 struct damos_quota *quota = &s->quota;
846 unsigned long sz_to_skip;
847
848 /* Skip previously charged regions */
849 if (quota->charge_target_from) {
850 if (t != quota->charge_target_from)
851 return true;
852 if (r == damon_last_region(t)) {
853 quota->charge_target_from = NULL;
854 quota->charge_addr_from = 0;
855 return true;
856 }
857 if (quota->charge_addr_from &&
858 r->ar.end <= quota->charge_addr_from)
859 return true;
860
861 if (quota->charge_addr_from && r->ar.start <
862 quota->charge_addr_from) {
863 sz_to_skip = ALIGN_DOWN(quota->charge_addr_from -
864 r->ar.start, DAMON_MIN_REGION);
865 if (!sz_to_skip) {
866 if (damon_sz_region(r) <= DAMON_MIN_REGION)
867 return true;
868 sz_to_skip = DAMON_MIN_REGION;
869 }
870 damon_split_region_at(t, r, sz_to_skip);
871 r = damon_next_region(r);
872 *rp = r;
873 }
874 quota->charge_target_from = NULL;
875 quota->charge_addr_from = 0;
876 }
877 return false;
878}
879
d1cbbf62
SP
880static void damos_update_stat(struct damos *s,
881 unsigned long sz_tried, unsigned long sz_applied)
882{
883 s->stat.nr_tried++;
884 s->stat.sz_tried += sz_tried;
885 if (sz_applied)
886 s->stat.nr_applied++;
887 s->stat.sz_applied += sz_applied;
888}
889
17e7c724
SP
890static bool __damos_filter_out(struct damon_ctx *ctx, struct damon_target *t,
891 struct damon_region *r, struct damos_filter *filter)
ab9bda00
SP
892{
893 bool matched = false;
17e7c724
SP
894 struct damon_target *ti;
895 int target_idx = 0;
ab9bda00
SP
896 unsigned long start, end;
897
898 switch (filter->type) {
17e7c724
SP
899 case DAMOS_FILTER_TYPE_TARGET:
900 damon_for_each_target(ti, ctx) {
901 if (ti == t)
902 break;
903 target_idx++;
904 }
905 matched = target_idx == filter->target_idx;
906 break;
ab9bda00
SP
907 case DAMOS_FILTER_TYPE_ADDR:
908 start = ALIGN_DOWN(filter->addr_range.start, DAMON_MIN_REGION);
909 end = ALIGN_DOWN(filter->addr_range.end, DAMON_MIN_REGION);
910
911 /* inside the range */
912 if (start <= r->ar.start && r->ar.end <= end) {
913 matched = true;
914 break;
915 }
916 /* outside of the range */
917 if (r->ar.end <= start || end <= r->ar.start) {
918 matched = false;
919 break;
920 }
921 /* start before the range and overlap */
922 if (r->ar.start < start) {
923 damon_split_region_at(t, r, start - r->ar.start);
924 matched = false;
925 break;
926 }
927 /* start inside the range */
928 damon_split_region_at(t, r, end - r->ar.start);
929 matched = true;
930 break;
931 default:
13b2a4b2 932 return false;
ab9bda00
SP
933 }
934
935 return matched == filter->matching;
936}
937
17e7c724
SP
938static bool damos_filter_out(struct damon_ctx *ctx, struct damon_target *t,
939 struct damon_region *r, struct damos *s)
ab9bda00
SP
940{
941 struct damos_filter *filter;
942
943 damos_for_each_filter(filter, s) {
17e7c724 944 if (__damos_filter_out(ctx, t, r, filter))
ab9bda00
SP
945 return true;
946 }
947 return false;
948}
949
e63a30c5
SP
950static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t,
951 struct damon_region *r, struct damos *s)
952{
953 struct damos_quota *quota = &s->quota;
954 unsigned long sz = damon_sz_region(r);
955 struct timespec64 begin, end;
956 unsigned long sz_applied = 0;
44467bbb 957 int err = 0;
c603c630
SP
958 /*
959 * We plan to support multiple context per kdamond, as DAMON sysfs
960 * implies with 'nr_contexts' file. Nevertheless, only single context
961 * per kdamond is supported for now. So, we can simply use '0' context
962 * index here.
963 */
964 unsigned int cidx = 0;
965 struct damos *siter; /* schemes iterator */
966 unsigned int sidx = 0;
967 struct damon_target *titer; /* targets iterator */
968 unsigned int tidx = 0;
969 bool do_trace = false;
970
971 /* get indices for trace_damos_before_apply() */
972 if (trace_damos_before_apply_enabled()) {
973 damon_for_each_scheme(siter, c) {
974 if (siter == s)
975 break;
976 sidx++;
977 }
978 damon_for_each_target(titer, c) {
979 if (titer == t)
980 break;
981 tidx++;
982 }
983 do_trace = true;
984 }
e63a30c5
SP
985
986 if (c->ops.apply_scheme) {
987 if (quota->esz && quota->charged_sz + sz > quota->esz) {
988 sz = ALIGN_DOWN(quota->esz - quota->charged_sz,
989 DAMON_MIN_REGION);
990 if (!sz)
991 goto update_stat;
992 damon_split_region_at(t, r, sz);
993 }
17e7c724 994 if (damos_filter_out(c, t, r, s))
ab9bda00 995 return;
e63a30c5 996 ktime_get_coarse_ts64(&begin);
44467bbb
SP
997 if (c->callback.before_damos_apply)
998 err = c->callback.before_damos_apply(c, t, r, s);
c603c630
SP
999 if (!err) {
1000 trace_damos_before_apply(cidx, sidx, tidx, r,
1001 damon_nr_regions(t), do_trace);
44467bbb 1002 sz_applied = c->ops.apply_scheme(c, t, r, s);
c603c630 1003 }
e63a30c5
SP
1004 ktime_get_coarse_ts64(&end);
1005 quota->total_charged_ns += timespec64_to_ns(&end) -
1006 timespec64_to_ns(&begin);
1007 quota->charged_sz += sz;
1008 if (quota->esz && quota->charged_sz >= quota->esz) {
1009 quota->charge_target_from = t;
1010 quota->charge_addr_from = r->ar.end + 1;
1011 }
1012 }
1013 if (s->action != DAMOS_STAT)
1014 r->age = 0;
1015
1016update_stat:
d1cbbf62 1017 damos_update_stat(s, sz, sz_applied);
e63a30c5
SP
1018}
1019
1f366e42
SP
1020static void damon_do_apply_schemes(struct damon_ctx *c,
1021 struct damon_target *t,
1022 struct damon_region *r)
1023{
1024 struct damos *s;
1f366e42
SP
1025
1026 damon_for_each_scheme(s, c) {
2b8a248d 1027 struct damos_quota *quota = &s->quota;
2b8a248d 1028
e9e3db69
SP
1029 if (c->passed_sample_intervals != s->next_apply_sis)
1030 continue;
1031
ee801b7d
SP
1032 if (!s->wmarks.activated)
1033 continue;
1034
2b8a248d 1035 /* Check the quota */
1cd24303 1036 if (quota->esz && quota->charged_sz >= quota->esz)
2b8a248d
SP
1037 continue;
1038
2ea34989
SP
1039 if (damos_skip_charged_region(t, &r, s))
1040 continue;
50585192 1041
38683e00 1042 if (!damos_valid_target(c, t, r, s))
1f366e42 1043 continue;
2b8a248d 1044
e63a30c5 1045 damos_apply_scheme(c, t, r, s);
1f366e42
SP
1046 }
1047}
1048
9294a037
SP
1049/*
1050 * damon_feed_loop_next_input() - get next input to achieve a target score.
1051 * @last_input The last input.
1052 * @score Current score that made with @last_input.
1053 *
1054 * Calculate next input to achieve the target score, based on the last input
1055 * and current score. Assuming the input and the score are positively
1056 * proportional, calculate how much compensation should be added to or
1057 * subtracted from the last input as a proportion of the last input. Avoid
1058 * next input always being zero by setting it non-zero always. In short form
1059 * (assuming support of float and signed calculations), the algorithm is as
1060 * below.
1061 *
1062 * next_input = max(last_input * ((goal - current) / goal + 1), 1)
1063 *
1064 * For simple implementation, we assume the target score is always 10,000. The
1065 * caller should adjust @score for this.
1066 *
1067 * Returns next input that assumed to achieve the target score.
1068 */
1069static unsigned long damon_feed_loop_next_input(unsigned long last_input,
1070 unsigned long score)
1071{
1072 const unsigned long goal = 10000;
1073 unsigned long score_goal_diff = max(goal, score) - min(goal, score);
1074 unsigned long score_goal_diff_bp = score_goal_diff * 10000 / goal;
1075 unsigned long compensation = last_input * score_goal_diff_bp / 10000;
1076 /* Set minimum input as 10000 to avoid compensation be zero */
1077 const unsigned long min_input = 10000;
1078
1079 if (goal > score)
1080 return last_input + compensation;
1081 if (last_input > compensation + min_input)
1082 return last_input - compensation;
1083 return min_input;
1084}
1085
1086/* Shouldn't be called if quota->ms, quota->sz, and quota->get_score unset */
1cd24303
SP
1087static void damos_set_effective_quota(struct damos_quota *quota)
1088{
1089 unsigned long throughput;
1090 unsigned long esz;
1091
9294a037 1092 if (!quota->ms && !quota->get_score) {
1cd24303
SP
1093 quota->esz = quota->sz;
1094 return;
1095 }
1096
9294a037
SP
1097 if (quota->get_score) {
1098 quota->esz_bp = damon_feed_loop_next_input(
1099 max(quota->esz_bp, 10000UL),
1100 quota->get_score(quota->get_score_arg));
1101 esz = quota->esz_bp / 10000;
1102 }
1103
1104 if (quota->ms) {
1105 if (quota->total_charged_ns)
1106 throughput = quota->total_charged_sz * 1000000 /
1107 quota->total_charged_ns;
1108 else
1109 throughput = PAGE_SIZE * 1024;
1110 if (quota->get_score)
1111 esz = min(throughput * quota->ms, esz);
1112 else
1113 esz = throughput * quota->ms;
1114 }
1cd24303
SP
1115
1116 if (quota->sz && quota->sz < esz)
1117 esz = quota->sz;
9294a037 1118
1cd24303
SP
1119 quota->esz = esz;
1120}
1121
898810e5 1122static void damos_adjust_quota(struct damon_ctx *c, struct damos *s)
1f366e42 1123{
898810e5 1124 struct damos_quota *quota = &s->quota;
1f366e42 1125 struct damon_target *t;
898810e5
SP
1126 struct damon_region *r;
1127 unsigned long cumulated_sz;
1128 unsigned int score, max_score = 0;
2b8a248d 1129
9294a037 1130 if (!quota->ms && !quota->sz && !quota->get_score)
898810e5 1131 return;
2b8a248d 1132
898810e5
SP
1133 /* New charge window starts */
1134 if (time_after_eq(jiffies, quota->charged_from +
1135 msecs_to_jiffies(quota->reset_interval))) {
1136 if (quota->esz && quota->charged_sz >= quota->esz)
1137 s->stat.qt_exceeds++;
1138 quota->total_charged_sz += quota->charged_sz;
1139 quota->charged_from = jiffies;
1140 quota->charged_sz = 0;
1141 damos_set_effective_quota(quota);
1142 }
ee801b7d 1143
898810e5
SP
1144 if (!c->ops.get_scheme_score)
1145 return;
2b8a248d 1146
898810e5
SP
1147 /* Fill up the score histogram */
1148 memset(quota->histogram, 0, sizeof(quota->histogram));
1149 damon_for_each_target(t, c) {
1150 damon_for_each_region(r, t) {
1151 if (!__damos_valid_target(r, s))
1152 continue;
1153 score = c->ops.get_scheme_score(c, t, r, s);
1154 quota->histogram[score] += damon_sz_region(r);
1155 if (score > max_score)
1156 max_score = score;
2b8a248d 1157 }
898810e5 1158 }
38683e00 1159
898810e5
SP
1160 /* Set the min score limit */
1161 for (cumulated_sz = 0, score = max_score; ; score--) {
1162 cumulated_sz += quota->histogram[score];
1163 if (cumulated_sz >= quota->esz || !score)
1164 break;
1165 }
1166 quota->min_score = score;
1167}
38683e00 1168
898810e5
SP
1169static void kdamond_apply_schemes(struct damon_ctx *c)
1170{
1171 struct damon_target *t;
1172 struct damon_region *r, *next_r;
1173 struct damos *s;
42f994b7
SP
1174 unsigned long sample_interval = c->attrs.sample_interval ?
1175 c->attrs.sample_interval : 1;
1176 bool has_schemes_to_apply = false;
38683e00 1177
898810e5 1178 damon_for_each_scheme(s, c) {
42f994b7
SP
1179 if (c->passed_sample_intervals != s->next_apply_sis)
1180 continue;
1181
898810e5
SP
1182 if (!s->wmarks.activated)
1183 continue;
1184
42f994b7
SP
1185 has_schemes_to_apply = true;
1186
898810e5 1187 damos_adjust_quota(c, s);
2b8a248d 1188 }
1f366e42 1189
42f994b7
SP
1190 if (!has_schemes_to_apply)
1191 return;
1192
1f366e42 1193 damon_for_each_target(t, c) {
2b8a248d 1194 damon_for_each_region_safe(r, next_r, t)
1f366e42
SP
1195 damon_do_apply_schemes(c, t, r);
1196 }
e9e3db69
SP
1197
1198 damon_for_each_scheme(s, c) {
1199 if (c->passed_sample_intervals != s->next_apply_sis)
1200 continue;
1201 s->next_apply_sis +=
1202 (s->apply_interval_us ? s->apply_interval_us :
1203 c->attrs.aggr_interval) / sample_interval;
1204 }
1f366e42
SP
1205}
1206
b9a6ac4e
SP
1207/*
1208 * Merge two adjacent regions into one region
1209 */
1210static void damon_merge_two_regions(struct damon_target *t,
1211 struct damon_region *l, struct damon_region *r)
1212{
652e0446 1213 unsigned long sz_l = damon_sz_region(l), sz_r = damon_sz_region(r);
b9a6ac4e
SP
1214
1215 l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) /
1216 (sz_l + sz_r);
80333828 1217 l->nr_accesses_bp = l->nr_accesses * 10000;
fda504fa 1218 l->age = (l->age * sz_l + r->age * sz_r) / (sz_l + sz_r);
b9a6ac4e
SP
1219 l->ar.end = r->ar.end;
1220 damon_destroy_region(r, t);
1221}
1222
b9a6ac4e
SP
1223/*
1224 * Merge adjacent regions having similar access frequencies
1225 *
1226 * t target affected by this merge operation
1227 * thres '->nr_accesses' diff threshold for the merge
1228 * sz_limit size upper limit of each region
1229 */
1230static void damon_merge_regions_of(struct damon_target *t, unsigned int thres,
1231 unsigned long sz_limit)
1232{
1233 struct damon_region *r, *prev = NULL, *next;
1234
1235 damon_for_each_region_safe(r, next, t) {
d720bbbd 1236 if (abs(r->nr_accesses - r->last_nr_accesses) > thres)
fda504fa
SP
1237 r->age = 0;
1238 else
1239 r->age++;
1240
b9a6ac4e 1241 if (prev && prev->ar.end == r->ar.start &&
d720bbbd 1242 abs(prev->nr_accesses - r->nr_accesses) <= thres &&
652e0446 1243 damon_sz_region(prev) + damon_sz_region(r) <= sz_limit)
b9a6ac4e
SP
1244 damon_merge_two_regions(t, prev, r);
1245 else
1246 prev = r;
1247 }
1248}
1249
1250/*
1251 * Merge adjacent regions having similar access frequencies
1252 *
1253 * threshold '->nr_accesses' diff threshold for the merge
1254 * sz_limit size upper limit of each region
1255 *
1256 * This function merges monitoring target regions which are adjacent and their
1257 * access frequencies are similar. This is for minimizing the monitoring
1258 * overhead under the dynamically changeable access pattern. If a merge was
1259 * unnecessarily made, later 'kdamond_split_regions()' will revert it.
1260 */
1261static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
1262 unsigned long sz_limit)
1263{
1264 struct damon_target *t;
1265
1266 damon_for_each_target(t, c)
1267 damon_merge_regions_of(t, threshold, sz_limit);
1268}
1269
1270/*
1271 * Split a region in two
1272 *
1273 * r the region to be split
1274 * sz_r size of the first sub-region that will be made
1275 */
4ed98243
KX
1276static void damon_split_region_at(struct damon_target *t,
1277 struct damon_region *r, unsigned long sz_r)
b9a6ac4e
SP
1278{
1279 struct damon_region *new;
1280
1281 new = damon_new_region(r->ar.start + sz_r, r->ar.end);
1282 if (!new)
1283 return;
1284
1285 r->ar.end = new->ar.start;
1286
fda504fa
SP
1287 new->age = r->age;
1288 new->last_nr_accesses = r->last_nr_accesses;
80333828 1289 new->nr_accesses_bp = r->nr_accesses_bp;
1f3730fd 1290 new->nr_accesses = r->nr_accesses;
fda504fa 1291
b9a6ac4e
SP
1292 damon_insert_region(new, r, damon_next_region(r), t);
1293}
1294
1295/* Split every region in the given target into 'nr_subs' regions */
4ed98243 1296static void damon_split_regions_of(struct damon_target *t, int nr_subs)
b9a6ac4e
SP
1297{
1298 struct damon_region *r, *next;
1299 unsigned long sz_region, sz_sub = 0;
1300 int i;
1301
1302 damon_for_each_region_safe(r, next, t) {
ab63f63f 1303 sz_region = damon_sz_region(r);
b9a6ac4e
SP
1304
1305 for (i = 0; i < nr_subs - 1 &&
1306 sz_region > 2 * DAMON_MIN_REGION; i++) {
1307 /*
1308 * Randomly select size of left sub-region to be at
1309 * least 10 percent and at most 90% of original region
1310 */
1311 sz_sub = ALIGN_DOWN(damon_rand(1, 10) *
1312 sz_region / 10, DAMON_MIN_REGION);
1313 /* Do not allow blank region */
1314 if (sz_sub == 0 || sz_sub >= sz_region)
1315 continue;
1316
4ed98243 1317 damon_split_region_at(t, r, sz_sub);
b9a6ac4e
SP
1318 sz_region = sz_sub;
1319 }
1320 }
1321}
1322
1323/*
1324 * Split every target region into randomly-sized small regions
1325 *
1326 * This function splits every target region into random-sized small regions if
1327 * current total number of the regions is equal or smaller than half of the
1328 * user-specified maximum number of regions. This is for maximizing the
1329 * monitoring accuracy under the dynamically changeable access patterns. If a
1330 * split was unnecessarily made, later 'kdamond_merge_regions()' will revert
1331 * it.
1332 */
1333static void kdamond_split_regions(struct damon_ctx *ctx)
1334{
1335 struct damon_target *t;
1336 unsigned int nr_regions = 0;
1337 static unsigned int last_nr_regions;
1338 int nr_subregions = 2;
1339
1340 damon_for_each_target(t, ctx)
1341 nr_regions += damon_nr_regions(t);
1342
cbeaa77b 1343 if (nr_regions > ctx->attrs.max_nr_regions / 2)
b9a6ac4e
SP
1344 return;
1345
1346 /* Maybe the middle of the region has different access frequency */
1347 if (last_nr_regions == nr_regions &&
cbeaa77b 1348 nr_regions < ctx->attrs.max_nr_regions / 3)
b9a6ac4e
SP
1349 nr_subregions = 3;
1350
1351 damon_for_each_target(t, ctx)
4ed98243 1352 damon_split_regions_of(t, nr_subregions);
b9a6ac4e
SP
1353
1354 last_nr_regions = nr_regions;
1355}
1356
2224d848
SP
1357/*
1358 * Check whether current monitoring should be stopped
1359 *
1360 * The monitoring is stopped when either the user requested to stop, or all
1361 * monitoring targets are invalid.
1362 *
1363 * Returns true if need to stop current monitoring.
1364 */
1365static bool kdamond_need_stop(struct damon_ctx *ctx)
1366{
f23b8eee 1367 struct damon_target *t;
2224d848 1368
0f91d133 1369 if (kthread_should_stop())
2224d848
SP
1370 return true;
1371
f7d911c3 1372 if (!ctx->ops.target_valid)
2224d848
SP
1373 return false;
1374
f23b8eee 1375 damon_for_each_target(t, ctx) {
f7d911c3 1376 if (ctx->ops.target_valid(t))
f23b8eee
SP
1377 return false;
1378 }
1379
1380 return true;
2224d848
SP
1381}
1382
ee801b7d
SP
1383static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
1384{
ee801b7d
SP
1385 switch (metric) {
1386 case DAMOS_WMARK_FREE_MEM_RATE:
987ffa5a
HY
1387 return global_zone_page_state(NR_FREE_PAGES) * 1000 /
1388 totalram_pages();
ee801b7d
SP
1389 default:
1390 break;
1391 }
1392 return -EINVAL;
1393}
1394
1395/*
1396 * Returns zero if the scheme is active. Else, returns time to wait for next
1397 * watermark check in micro-seconds.
1398 */
1399static unsigned long damos_wmark_wait_us(struct damos *scheme)
1400{
1401 unsigned long metric;
1402
1403 if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
1404 return 0;
1405
1406 metric = damos_wmark_metric_value(scheme->wmarks.metric);
1407 /* higher than high watermark or lower than low watermark */
1408 if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
1409 if (scheme->wmarks.activated)
01078655 1410 pr_debug("deactivate a scheme (%d) for %s wmark\n",
ee801b7d
SP
1411 scheme->action,
1412 metric > scheme->wmarks.high ?
1413 "high" : "low");
1414 scheme->wmarks.activated = false;
1415 return scheme->wmarks.interval;
1416 }
1417
1418 /* inactive and higher than middle watermark */
1419 if ((scheme->wmarks.high >= metric && metric >= scheme->wmarks.mid) &&
1420 !scheme->wmarks.activated)
1421 return scheme->wmarks.interval;
1422
1423 if (!scheme->wmarks.activated)
1424 pr_debug("activate a scheme (%d)\n", scheme->action);
1425 scheme->wmarks.activated = true;
1426 return 0;
1427}
1428
1429static void kdamond_usleep(unsigned long usecs)
1430{
4de46a30
SP
1431 /* See Documentation/timers/timers-howto.rst for the thresholds */
1432 if (usecs > 20 * USEC_PER_MSEC)
70e92748 1433 schedule_timeout_idle(usecs_to_jiffies(usecs));
ee801b7d 1434 else
70e92748 1435 usleep_idle_range(usecs, usecs + 1);
ee801b7d
SP
1436}
1437
1438/* Returns negative error code if it's not activated but should return */
1439static int kdamond_wait_activation(struct damon_ctx *ctx)
1440{
1441 struct damos *s;
1442 unsigned long wait_time;
1443 unsigned long min_wait_time = 0;
78049e94 1444 bool init_wait_time = false;
ee801b7d
SP
1445
1446 while (!kdamond_need_stop(ctx)) {
1447 damon_for_each_scheme(s, ctx) {
1448 wait_time = damos_wmark_wait_us(s);
78049e94
JK
1449 if (!init_wait_time || wait_time < min_wait_time) {
1450 init_wait_time = true;
ee801b7d 1451 min_wait_time = wait_time;
78049e94 1452 }
ee801b7d
SP
1453 }
1454 if (!min_wait_time)
1455 return 0;
1456
1457 kdamond_usleep(min_wait_time);
6e74d2bf
SP
1458
1459 if (ctx->callback.after_wmarks_check &&
1460 ctx->callback.after_wmarks_check(ctx))
1461 break;
ee801b7d
SP
1462 }
1463 return -EBUSY;
1464}
1465
4472edf6
SP
1466static void kdamond_init_intervals_sis(struct damon_ctx *ctx)
1467{
1468 unsigned long sample_interval = ctx->attrs.sample_interval ?
1469 ctx->attrs.sample_interval : 1;
42f994b7
SP
1470 unsigned long apply_interval;
1471 struct damos *scheme;
4472edf6
SP
1472
1473 ctx->passed_sample_intervals = 0;
1474 ctx->next_aggregation_sis = ctx->attrs.aggr_interval / sample_interval;
1475 ctx->next_ops_update_sis = ctx->attrs.ops_update_interval /
1476 sample_interval;
42f994b7
SP
1477
1478 damon_for_each_scheme(scheme, ctx) {
1479 apply_interval = scheme->apply_interval_us ?
1480 scheme->apply_interval_us : ctx->attrs.aggr_interval;
1481 scheme->next_apply_sis = apply_interval / sample_interval;
1482 }
4472edf6
SP
1483}
1484
2224d848
SP
1485/*
1486 * The monitoring daemon that runs as a kernel thread
1487 */
1488static int kdamond_fn(void *data)
1489{
cef4493f 1490 struct damon_ctx *ctx = data;
f23b8eee
SP
1491 struct damon_target *t;
1492 struct damon_region *r, *next;
b9a6ac4e
SP
1493 unsigned int max_nr_accesses = 0;
1494 unsigned long sz_limit = 0;
2224d848 1495
42e4cef5 1496 pr_debug("kdamond (%d) starts\n", current->pid);
2224d848 1497
6376a824 1498 complete(&ctx->kdamond_started);
4472edf6
SP
1499 kdamond_init_intervals_sis(ctx);
1500
f7d911c3
SP
1501 if (ctx->ops.init)
1502 ctx->ops.init(ctx);
2224d848 1503 if (ctx->callback.before_start && ctx->callback.before_start(ctx))
29454cf6 1504 goto done;
2224d848 1505
b9a6ac4e
SP
1506 sz_limit = damon_region_sz_limit(ctx);
1507
29454cf6 1508 while (!kdamond_need_stop(ctx)) {
4472edf6
SP
1509 /*
1510 * ctx->attrs and ctx->next_{aggregation,ops_update}_sis could
1511 * be changed from after_wmarks_check() or after_aggregation()
1512 * callbacks. Read the values here, and use those for this
1513 * iteration. That is, damon_set_attrs() updated new values
1514 * are respected from next iteration.
1515 */
1516 unsigned long next_aggregation_sis = ctx->next_aggregation_sis;
1517 unsigned long next_ops_update_sis = ctx->next_ops_update_sis;
1518 unsigned long sample_interval = ctx->attrs.sample_interval;
1519
29454cf6
KX
1520 if (kdamond_wait_activation(ctx))
1521 break;
ee801b7d 1522
f7d911c3
SP
1523 if (ctx->ops.prepare_access_checks)
1524 ctx->ops.prepare_access_checks(ctx);
2224d848 1525 if (ctx->callback.after_sampling &&
29454cf6
KX
1526 ctx->callback.after_sampling(ctx))
1527 break;
2224d848 1528
4472edf6
SP
1529 kdamond_usleep(sample_interval);
1530 ctx->passed_sample_intervals++;
2224d848 1531
f7d911c3
SP
1532 if (ctx->ops.check_accesses)
1533 max_nr_accesses = ctx->ops.check_accesses(ctx);
2224d848 1534
4472edf6 1535 if (ctx->passed_sample_intervals == next_aggregation_sis) {
b9a6ac4e
SP
1536 kdamond_merge_regions(ctx,
1537 max_nr_accesses / 10,
1538 sz_limit);
2224d848 1539 if (ctx->callback.after_aggregation &&
29454cf6
KX
1540 ctx->callback.after_aggregation(ctx))
1541 break;
42f994b7
SP
1542 }
1543
1544 /*
1545 * do kdamond_apply_schemes() after kdamond_merge_regions() if
1546 * possible, to reduce overhead
1547 */
1548 if (!list_empty(&ctx->schemes))
1549 kdamond_apply_schemes(ctx);
1550
1551 sample_interval = ctx->attrs.sample_interval ?
1552 ctx->attrs.sample_interval : 1;
1553 if (ctx->passed_sample_intervals == next_aggregation_sis) {
1554 ctx->next_aggregation_sis = next_aggregation_sis +
1555 ctx->attrs.aggr_interval / sample_interval;
1556
f23b8eee 1557 kdamond_reset_aggregated(ctx);
b9a6ac4e 1558 kdamond_split_regions(ctx);
f7d911c3
SP
1559 if (ctx->ops.reset_aggregated)
1560 ctx->ops.reset_aggregated(ctx);
2224d848
SP
1561 }
1562
4472edf6
SP
1563 if (ctx->passed_sample_intervals == next_ops_update_sis) {
1564 ctx->next_ops_update_sis = next_ops_update_sis +
1565 ctx->attrs.ops_update_interval /
1566 sample_interval;
f7d911c3
SP
1567 if (ctx->ops.update)
1568 ctx->ops.update(ctx);
b9a6ac4e 1569 sz_limit = damon_region_sz_limit(ctx);
2224d848
SP
1570 }
1571 }
29454cf6 1572done:
f23b8eee
SP
1573 damon_for_each_target(t, ctx) {
1574 damon_for_each_region_safe(r, next, t)
b9a6ac4e 1575 damon_destroy_region(r, t);
f23b8eee 1576 }
2224d848 1577
0f91d133
CD
1578 if (ctx->callback.before_terminate)
1579 ctx->callback.before_terminate(ctx);
f7d911c3
SP
1580 if (ctx->ops.cleanup)
1581 ctx->ops.cleanup(ctx);
2224d848 1582
42e4cef5 1583 pr_debug("kdamond (%d) finishes\n", current->pid);
2224d848
SP
1584 mutex_lock(&ctx->kdamond_lock);
1585 ctx->kdamond = NULL;
1586 mutex_unlock(&ctx->kdamond_lock);
1587
1588 mutex_lock(&damon_lock);
1589 nr_running_ctxs--;
8b9b0d33
SP
1590 if (!nr_running_ctxs && running_exclusive_ctxs)
1591 running_exclusive_ctxs = false;
2224d848
SP
1592 mutex_unlock(&damon_lock);
1593
5f7fe2b9 1594 return 0;
2224d848 1595}
17ccae8b 1596
0d83b2d8
XH
1597/*
1598 * struct damon_system_ram_region - System RAM resource address region of
1599 * [@start, @end).
1600 * @start: Start address of the region (inclusive).
1601 * @end: End address of the region (exclusive).
1602 */
1603struct damon_system_ram_region {
1604 unsigned long start;
1605 unsigned long end;
1606};
1607
1608static int walk_system_ram(struct resource *res, void *arg)
1609{
1610 struct damon_system_ram_region *a = arg;
1611
1612 if (a->end - a->start < resource_size(res)) {
1613 a->start = res->start;
1614 a->end = res->end;
1615 }
1616 return 0;
1617}
1618
1619/*
1620 * Find biggest 'System RAM' resource and store its start and end address in
1621 * @start and @end, respectively. If no System RAM is found, returns false.
1622 */
233f0b31
KX
1623static bool damon_find_biggest_system_ram(unsigned long *start,
1624 unsigned long *end)
0d83b2d8
XH
1625
1626{
1627 struct damon_system_ram_region arg = {};
1628
1629 walk_system_ram_res(0, ULONG_MAX, &arg, walk_system_ram);
1630 if (arg.end <= arg.start)
1631 return false;
1632
1633 *start = arg.start;
1634 *end = arg.end;
1635 return true;
1636}
1637
233f0b31
KX
1638/**
1639 * damon_set_region_biggest_system_ram_default() - Set the region of the given
1640 * monitoring target as requested, or biggest 'System RAM'.
1641 * @t: The monitoring target to set the region.
1642 * @start: The pointer to the start address of the region.
1643 * @end: The pointer to the end address of the region.
1644 *
1645 * This function sets the region of @t as requested by @start and @end. If the
1646 * values of @start and @end are zero, however, this function finds the biggest
1647 * 'System RAM' resource and sets the region to cover the resource. In the
1648 * latter case, this function saves the start and end addresses of the resource
1649 * in @start and @end, respectively.
1650 *
1651 * Return: 0 on success, negative error code otherwise.
1652 */
1653int damon_set_region_biggest_system_ram_default(struct damon_target *t,
1654 unsigned long *start, unsigned long *end)
1655{
1656 struct damon_addr_range addr_range;
1657
1658 if (*start > *end)
1659 return -EINVAL;
1660
1661 if (!*start && !*end &&
1662 !damon_find_biggest_system_ram(start, end))
1663 return -EINVAL;
1664
1665 addr_range.start = *start;
1666 addr_range.end = *end;
1667 return damon_set_regions(t, &addr_range, 1);
1668}
1669
d2c062ad
SP
1670/*
1671 * damon_moving_sum() - Calculate an inferred moving sum value.
1672 * @mvsum: Inferred sum of the last @len_window values.
1673 * @nomvsum: Non-moving sum of the last discrete @len_window window values.
1674 * @len_window: The number of last values to take care of.
1675 * @new_value: New value that will be added to the pseudo moving sum.
1676 *
1677 * Moving sum (moving average * window size) is good for handling noise, but
1678 * the cost of keeping past values can be high for arbitrary window size. This
1679 * function implements a lightweight pseudo moving sum function that doesn't
1680 * keep the past window values.
1681 *
1682 * It simply assumes there was no noise in the past, and get the no-noise
1683 * assumed past value to drop from @nomvsum and @len_window. @nomvsum is a
1684 * non-moving sum of the last window. For example, if @len_window is 10 and we
1685 * have 25 values, @nomvsum is the sum of the 11th to 20th values of the 25
1686 * values. Hence, this function simply drops @nomvsum / @len_window from
1687 * given @mvsum and add @new_value.
1688 *
1689 * For example, if @len_window is 10 and @nomvsum is 50, the last 10 values for
1690 * the last window could be vary, e.g., 0, 10, 0, 10, 0, 10, 0, 0, 0, 20. For
1691 * calculating next moving sum with a new value, we should drop 0 from 50 and
1692 * add the new value. However, this function assumes it got value 5 for each
1693 * of the last ten times. Based on the assumption, when the next value is
1694 * measured, it drops the assumed past value, 5 from the current sum, and add
1695 * the new value to get the updated pseduo-moving average.
1696 *
1697 * This means the value could have errors, but the errors will be disappeared
1698 * for every @len_window aligned calls. For example, if @len_window is 10, the
1699 * pseudo moving sum with 11th value to 19th value would have an error. But
1700 * the sum with 20th value will not have the error.
1701 *
1702 * Return: Pseudo-moving average after getting the @new_value.
1703 */
863803a7 1704static unsigned int damon_moving_sum(unsigned int mvsum, unsigned int nomvsum,
d2c062ad
SP
1705 unsigned int len_window, unsigned int new_value)
1706{
1707 return mvsum - nomvsum / len_window + new_value;
1708}
1709
78fbfb15
SP
1710/**
1711 * damon_update_region_access_rate() - Update the access rate of a region.
1712 * @r: The DAMON region to update for its access check result.
1713 * @accessed: Whether the region has accessed during last sampling interval.
ace30fb2 1714 * @attrs: The damon_attrs of the DAMON context.
78fbfb15
SP
1715 *
1716 * Update the access rate of a region with the region's last sampling interval
1717 * access check result.
1718 *
1719 * Usually this will be called by &damon_operations->check_accesses callback.
1720 */
ace30fb2
SP
1721void damon_update_region_access_rate(struct damon_region *r, bool accessed,
1722 struct damon_attrs *attrs)
78fbfb15 1723{
ace30fb2
SP
1724 unsigned int len_window = 1;
1725
1726 /*
1727 * sample_interval can be zero, but cannot be larger than
1728 * aggr_interval, owing to validation of damon_set_attrs().
1729 */
1730 if (attrs->sample_interval)
62f76a7b 1731 len_window = damon_max_nr_accesses(attrs);
ace30fb2
SP
1732 r->nr_accesses_bp = damon_moving_sum(r->nr_accesses_bp,
1733 r->last_nr_accesses * 10000, len_window,
1734 accessed ? 10000 : 0);
1735
78fbfb15
SP
1736 if (accessed)
1737 r->nr_accesses++;
1738}
1739
a1870944
DL
1740static int __init damon_init(void)
1741{
1742 damon_region_cache = KMEM_CACHE(damon_region, 0);
1743 if (unlikely(!damon_region_cache)) {
1744 pr_err("creating damon_region_cache fails\n");
1745 return -ENOMEM;
1746 }
1747
1748 return 0;
1749}
1750
1751subsys_initcall(damon_init);
1752
17ccae8b 1753#include "core-test.h"