Merge branches 'acpi-bus' and 'acpi-video'
[linux-block.git] / mm / damon / core.c
CommitLineData
2224d848
SP
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Data Access Monitor
4 *
5 * Author: SeongJae Park <sjpark@amazon.de>
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;
131 INIT_LIST_HEAD(&region->list);
132
fda504fa
SP
133 region->age = 0;
134 region->last_nr_accesses = 0;
135
f23b8eee
SP
136 return region;
137}
138
f23b8eee
SP
139void damon_add_region(struct damon_region *r, struct damon_target *t)
140{
141 list_add_tail(&r->list, &t->regions_list);
b9a6ac4e 142 t->nr_regions++;
f23b8eee
SP
143}
144
b9a6ac4e 145static void damon_del_region(struct damon_region *r, struct damon_target *t)
f23b8eee
SP
146{
147 list_del(&r->list);
b9a6ac4e 148 t->nr_regions--;
f23b8eee
SP
149}
150
151static void damon_free_region(struct damon_region *r)
152{
a1870944 153 kmem_cache_free(damon_region_cache, r);
f23b8eee
SP
154}
155
b9a6ac4e 156void damon_destroy_region(struct damon_region *r, struct damon_target *t)
f23b8eee 157{
b9a6ac4e 158 damon_del_region(r, t);
f23b8eee
SP
159 damon_free_region(r);
160}
161
d0723bc0
SP
162/*
163 * Check whether a region is intersecting an address range
164 *
165 * Returns true if it is.
166 */
167static bool damon_intersect(struct damon_region *r,
168 struct damon_addr_range *re)
169{
170 return !(r->ar.end <= re->start || re->end <= r->ar.start);
171}
172
9c950c22
SP
173/*
174 * Fill holes in regions with new regions.
175 */
176static int damon_fill_regions_holes(struct damon_region *first,
177 struct damon_region *last, struct damon_target *t)
178{
179 struct damon_region *r = first;
180
181 damon_for_each_region_from(r, t) {
182 struct damon_region *next, *newr;
183
184 if (r == last)
185 break;
186 next = damon_next_region(r);
187 if (r->ar.end != next->ar.start) {
188 newr = damon_new_region(r->ar.end, next->ar.start);
189 if (!newr)
190 return -ENOMEM;
191 damon_insert_region(newr, r, next, t);
192 }
193 }
194 return 0;
195}
196
d0723bc0
SP
197/*
198 * damon_set_regions() - Set regions of a target for given address ranges.
199 * @t: the given target.
200 * @ranges: array of new monitoring target ranges.
201 * @nr_ranges: length of @ranges.
202 *
203 * This function adds new regions to, or modify existing regions of a
204 * monitoring target to fit in specific ranges.
205 *
206 * Return: 0 if success, or negative error code otherwise.
207 */
208int damon_set_regions(struct damon_target *t, struct damon_addr_range *ranges,
209 unsigned int nr_ranges)
210{
211 struct damon_region *r, *next;
212 unsigned int i;
9c950c22 213 int err;
d0723bc0
SP
214
215 /* Remove regions which are not in the new ranges */
216 damon_for_each_region_safe(r, next, t) {
217 for (i = 0; i < nr_ranges; i++) {
218 if (damon_intersect(r, &ranges[i]))
219 break;
220 }
221 if (i == nr_ranges)
222 damon_destroy_region(r, t);
223 }
224
36001cba 225 r = damon_first_region(t);
d0723bc0
SP
226 /* Add new regions or resize existing regions to fit in the ranges */
227 for (i = 0; i < nr_ranges; i++) {
228 struct damon_region *first = NULL, *last, *newr;
229 struct damon_addr_range *range;
230
231 range = &ranges[i];
232 /* Get the first/last regions intersecting with the range */
36001cba 233 damon_for_each_region_from(r, t) {
d0723bc0
SP
234 if (damon_intersect(r, range)) {
235 if (!first)
236 first = r;
237 last = r;
238 }
239 if (r->ar.start >= range->end)
240 break;
241 }
242 if (!first) {
243 /* no region intersects with this range */
244 newr = damon_new_region(
245 ALIGN_DOWN(range->start,
246 DAMON_MIN_REGION),
247 ALIGN(range->end, DAMON_MIN_REGION));
248 if (!newr)
249 return -ENOMEM;
250 damon_insert_region(newr, damon_prev_region(r), r, t);
251 } else {
252 /* resize intersecting regions to fit in this range */
253 first->ar.start = ALIGN_DOWN(range->start,
254 DAMON_MIN_REGION);
255 last->ar.end = ALIGN(range->end, DAMON_MIN_REGION);
9c950c22
SP
256
257 /* fill possible holes in the range */
258 err = damon_fill_regions_holes(first, last, t);
259 if (err)
260 return err;
d0723bc0
SP
261 }
262 }
263 return 0;
264}
265
98def236
SP
266struct damos_filter *damos_new_filter(enum damos_filter_type type,
267 bool matching)
268{
269 struct damos_filter *filter;
270
271 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
272 if (!filter)
273 return NULL;
274 filter->type = type;
275 filter->matching = matching;
5f1fc67f 276 INIT_LIST_HEAD(&filter->list);
98def236
SP
277 return filter;
278}
279
280void damos_add_filter(struct damos *s, struct damos_filter *f)
281{
282 list_add_tail(&f->list, &s->filters);
283}
284
285static void damos_del_filter(struct damos_filter *f)
286{
287 list_del(&f->list);
288}
289
290static void damos_free_filter(struct damos_filter *f)
291{
292 kfree(f);
293}
294
295void damos_destroy_filter(struct damos_filter *f)
296{
297 damos_del_filter(f);
298 damos_free_filter(f);
299}
300
70e0c1d1
SP
301/* initialize private fields of damos_quota and return the pointer */
302static struct damos_quota *damos_quota_init_priv(struct damos_quota *quota)
303{
304 quota->total_charged_sz = 0;
305 quota->total_charged_ns = 0;
306 quota->esz = 0;
307 quota->charged_sz = 0;
308 quota->charged_from = 0;
309 quota->charge_target_from = NULL;
310 quota->charge_addr_from = 0;
311 return quota;
312}
313
f5a79d7c
YD
314struct damos *damon_new_scheme(struct damos_access_pattern *pattern,
315 enum damos_action action, struct damos_quota *quota,
316 struct damos_watermarks *wmarks)
1f366e42
SP
317{
318 struct damos *scheme;
319
320 scheme = kmalloc(sizeof(*scheme), GFP_KERNEL);
321 if (!scheme)
322 return NULL;
02f17037 323 scheme->pattern = *pattern;
1f366e42 324 scheme->action = action;
98def236 325 INIT_LIST_HEAD(&scheme->filters);
0e92c2ee 326 scheme->stat = (struct damos_stat){};
1f366e42
SP
327 INIT_LIST_HEAD(&scheme->list);
328
70e0c1d1 329 scheme->quota = *(damos_quota_init_priv(quota));
2b8a248d 330
02f17037 331 scheme->wmarks = *wmarks;
ee801b7d
SP
332 scheme->wmarks.activated = true;
333
1f366e42
SP
334 return scheme;
335}
336
337void damon_add_scheme(struct damon_ctx *ctx, struct damos *s)
338{
339 list_add_tail(&s->list, &ctx->schemes);
340}
341
342static void damon_del_scheme(struct damos *s)
343{
344 list_del(&s->list);
345}
346
347static void damon_free_scheme(struct damos *s)
348{
349 kfree(s);
350}
351
352void damon_destroy_scheme(struct damos *s)
353{
98def236
SP
354 struct damos_filter *f, *next;
355
356 damos_for_each_filter_safe(f, next, s)
357 damos_destroy_filter(f);
1f366e42
SP
358 damon_del_scheme(s);
359 damon_free_scheme(s);
360}
361
f23b8eee
SP
362/*
363 * Construct a damon_target struct
364 *
365 * Returns the pointer to the new struct if success, or NULL otherwise
366 */
1971bd63 367struct damon_target *damon_new_target(void)
f23b8eee
SP
368{
369 struct damon_target *t;
370
371 t = kmalloc(sizeof(*t), GFP_KERNEL);
372 if (!t)
373 return NULL;
374
1971bd63 375 t->pid = NULL;
b9a6ac4e 376 t->nr_regions = 0;
f23b8eee 377 INIT_LIST_HEAD(&t->regions_list);
b1f44cda 378 INIT_LIST_HEAD(&t->list);
f23b8eee
SP
379
380 return t;
381}
382
383void damon_add_target(struct damon_ctx *ctx, struct damon_target *t)
384{
b9a6ac4e 385 list_add_tail(&t->list, &ctx->adaptive_targets);
f23b8eee
SP
386}
387
b5ca3e83
XH
388bool damon_targets_empty(struct damon_ctx *ctx)
389{
390 return list_empty(&ctx->adaptive_targets);
391}
392
f23b8eee
SP
393static void damon_del_target(struct damon_target *t)
394{
395 list_del(&t->list);
396}
397
398void damon_free_target(struct damon_target *t)
399{
400 struct damon_region *r, *next;
401
402 damon_for_each_region_safe(r, next, t)
403 damon_free_region(r);
404 kfree(t);
405}
406
407void damon_destroy_target(struct damon_target *t)
408{
409 damon_del_target(t);
410 damon_free_target(t);
411}
412
b9a6ac4e
SP
413unsigned int damon_nr_regions(struct damon_target *t)
414{
415 return t->nr_regions;
416}
417
2224d848
SP
418struct damon_ctx *damon_new_ctx(void)
419{
420 struct damon_ctx *ctx;
421
422 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
423 if (!ctx)
424 return NULL;
425
cbeaa77b
SP
426 ctx->attrs.sample_interval = 5 * 1000;
427 ctx->attrs.aggr_interval = 100 * 1000;
428 ctx->attrs.ops_update_interval = 60 * 1000 * 1000;
2224d848
SP
429
430 ktime_get_coarse_ts64(&ctx->last_aggregation);
f7d911c3 431 ctx->last_ops_update = ctx->last_aggregation;
2224d848
SP
432
433 mutex_init(&ctx->kdamond_lock);
434
cbeaa77b
SP
435 ctx->attrs.min_nr_regions = 10;
436 ctx->attrs.max_nr_regions = 1000;
b9a6ac4e
SP
437
438 INIT_LIST_HEAD(&ctx->adaptive_targets);
1f366e42 439 INIT_LIST_HEAD(&ctx->schemes);
2224d848
SP
440
441 return ctx;
442}
443
f23b8eee 444static void damon_destroy_targets(struct damon_ctx *ctx)
2224d848 445{
f23b8eee
SP
446 struct damon_target *t, *next_t;
447
f7d911c3
SP
448 if (ctx->ops.cleanup) {
449 ctx->ops.cleanup(ctx);
f23b8eee
SP
450 return;
451 }
452
453 damon_for_each_target_safe(t, next_t, ctx)
454 damon_destroy_target(t);
455}
456
457void damon_destroy_ctx(struct damon_ctx *ctx)
458{
1f366e42
SP
459 struct damos *s, *next_s;
460
f23b8eee 461 damon_destroy_targets(ctx);
1f366e42
SP
462
463 damon_for_each_scheme_safe(s, next_s, ctx)
464 damon_destroy_scheme(s);
465
2224d848
SP
466 kfree(ctx);
467}
468
2f5bef5a
SP
469static unsigned int damon_age_for_new_attrs(unsigned int age,
470 struct damon_attrs *old_attrs, struct damon_attrs *new_attrs)
471{
472 return age * old_attrs->aggr_interval / new_attrs->aggr_interval;
473}
474
475/* convert access ratio in bp (per 10,000) to nr_accesses */
476static unsigned int damon_accesses_bp_to_nr_accesses(
477 unsigned int accesses_bp, struct damon_attrs *attrs)
478{
479 unsigned int max_nr_accesses =
480 attrs->aggr_interval / attrs->sample_interval;
481
482 return accesses_bp * max_nr_accesses / 10000;
483}
484
485/* convert nr_accesses to access ratio in bp (per 10,000) */
486static unsigned int damon_nr_accesses_to_accesses_bp(
487 unsigned int nr_accesses, struct damon_attrs *attrs)
488{
489 unsigned int max_nr_accesses =
490 attrs->aggr_interval / attrs->sample_interval;
491
492 return nr_accesses * 10000 / max_nr_accesses;
493}
494
495static unsigned int damon_nr_accesses_for_new_attrs(unsigned int nr_accesses,
496 struct damon_attrs *old_attrs, struct damon_attrs *new_attrs)
497{
498 return damon_accesses_bp_to_nr_accesses(
499 damon_nr_accesses_to_accesses_bp(
500 nr_accesses, old_attrs),
501 new_attrs);
502}
503
504static void damon_update_monitoring_result(struct damon_region *r,
505 struct damon_attrs *old_attrs, struct damon_attrs *new_attrs)
506{
507 r->nr_accesses = damon_nr_accesses_for_new_attrs(r->nr_accesses,
508 old_attrs, new_attrs);
509 r->age = damon_age_for_new_attrs(r->age, old_attrs, new_attrs);
510}
511
512/*
513 * region->nr_accesses is the number of sampling intervals in the last
514 * aggregation interval that access to the region has found, and region->age is
515 * the number of aggregation intervals that its access pattern has maintained.
516 * For the reason, the real meaning of the two fields depend on current
517 * sampling interval and aggregation interval. This function updates
518 * ->nr_accesses and ->age of given damon_ctx's regions for new damon_attrs.
519 */
520static void damon_update_monitoring_results(struct damon_ctx *ctx,
521 struct damon_attrs *new_attrs)
522{
523 struct damon_attrs *old_attrs = &ctx->attrs;
524 struct damon_target *t;
525 struct damon_region *r;
526
527 /* if any interval is zero, simply forgive conversion */
528 if (!old_attrs->sample_interval || !old_attrs->aggr_interval ||
529 !new_attrs->sample_interval ||
530 !new_attrs->aggr_interval)
531 return;
532
533 damon_for_each_target(t, ctx)
534 damon_for_each_region(r, t)
535 damon_update_monitoring_result(
536 r, old_attrs, new_attrs);
537}
538
2224d848
SP
539/**
540 * damon_set_attrs() - Set attributes for the monitoring.
541 * @ctx: monitoring context
bead3b00 542 * @attrs: monitoring attributes
2224d848
SP
543 *
544 * This function should not be called while the kdamond is running.
545 * Every time interval is in micro-seconds.
546 *
547 * Return: 0 on success, negative error code otherwise.
548 */
bead3b00 549int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)
2224d848 550{
bead3b00 551 if (attrs->min_nr_regions < 3)
b9a6ac4e 552 return -EINVAL;
bead3b00 553 if (attrs->min_nr_regions > attrs->max_nr_regions)
b9a6ac4e 554 return -EINVAL;
5ff6e2ff
KW
555 if (attrs->sample_interval > attrs->aggr_interval)
556 return -EINVAL;
b9a6ac4e 557
2f5bef5a 558 damon_update_monitoring_results(ctx, attrs);
bead3b00 559 ctx->attrs = *attrs;
2224d848
SP
560 return 0;
561}
562
1f366e42
SP
563/**
564 * damon_set_schemes() - Set data access monitoring based operation schemes.
565 * @ctx: monitoring context
566 * @schemes: array of the schemes
567 * @nr_schemes: number of entries in @schemes
568 *
569 * This function should not be called while the kdamond of the context is
570 * running.
1f366e42 571 */
cc713520 572void damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes,
1f366e42
SP
573 ssize_t nr_schemes)
574{
575 struct damos *s, *next;
576 ssize_t i;
577
578 damon_for_each_scheme_safe(s, next, ctx)
579 damon_destroy_scheme(s);
580 for (i = 0; i < nr_schemes; i++)
581 damon_add_scheme(ctx, schemes[i]);
1f366e42
SP
582}
583
4bc05954
SP
584/**
585 * damon_nr_running_ctxs() - Return number of currently running contexts.
586 */
587int damon_nr_running_ctxs(void)
588{
589 int nr_ctxs;
590
591 mutex_lock(&damon_lock);
592 nr_ctxs = nr_running_ctxs;
593 mutex_unlock(&damon_lock);
594
595 return nr_ctxs;
596}
597
b9a6ac4e
SP
598/* Returns the size upper limit for each monitoring region */
599static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
600{
601 struct damon_target *t;
602 struct damon_region *r;
603 unsigned long sz = 0;
604
605 damon_for_each_target(t, ctx) {
606 damon_for_each_region(r, t)
ab63f63f 607 sz += damon_sz_region(r);
b9a6ac4e
SP
608 }
609
cbeaa77b
SP
610 if (ctx->attrs.min_nr_regions)
611 sz /= ctx->attrs.min_nr_regions;
b9a6ac4e
SP
612 if (sz < DAMON_MIN_REGION)
613 sz = DAMON_MIN_REGION;
614
615 return sz;
616}
617
2224d848
SP
618static int kdamond_fn(void *data);
619
620/*
621 * __damon_start() - Starts monitoring with given context.
622 * @ctx: monitoring context
623 *
624 * This function should be called while damon_lock is hold.
625 *
626 * Return: 0 on success, negative error code otherwise.
627 */
628static int __damon_start(struct damon_ctx *ctx)
629{
630 int err = -EBUSY;
631
632 mutex_lock(&ctx->kdamond_lock);
633 if (!ctx->kdamond) {
634 err = 0;
2224d848
SP
635 ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d",
636 nr_running_ctxs);
637 if (IS_ERR(ctx->kdamond)) {
638 err = PTR_ERR(ctx->kdamond);
7ec1992b 639 ctx->kdamond = NULL;
2224d848
SP
640 }
641 }
642 mutex_unlock(&ctx->kdamond_lock);
643
644 return err;
645}
646
647/**
648 * damon_start() - Starts the monitorings for a given group of contexts.
649 * @ctxs: an array of the pointers for contexts to start monitoring
650 * @nr_ctxs: size of @ctxs
8b9b0d33 651 * @exclusive: exclusiveness of this contexts group
2224d848
SP
652 *
653 * This function starts a group of monitoring threads for a group of monitoring
654 * contexts. One thread per each context is created and run in parallel. The
8b9b0d33
SP
655 * caller should handle synchronization between the threads by itself. If
656 * @exclusive is true and a group of threads that created by other
657 * 'damon_start()' call is currently running, this function does nothing but
658 * returns -EBUSY.
2224d848
SP
659 *
660 * Return: 0 on success, negative error code otherwise.
661 */
8b9b0d33 662int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive)
2224d848
SP
663{
664 int i;
665 int err = 0;
666
667 mutex_lock(&damon_lock);
8b9b0d33
SP
668 if ((exclusive && nr_running_ctxs) ||
669 (!exclusive && running_exclusive_ctxs)) {
2224d848
SP
670 mutex_unlock(&damon_lock);
671 return -EBUSY;
672 }
673
674 for (i = 0; i < nr_ctxs; i++) {
675 err = __damon_start(ctxs[i]);
676 if (err)
677 break;
678 nr_running_ctxs++;
679 }
8b9b0d33
SP
680 if (exclusive && nr_running_ctxs)
681 running_exclusive_ctxs = true;
2224d848
SP
682 mutex_unlock(&damon_lock);
683
684 return err;
685}
686
687/*
8b9b0d33 688 * __damon_stop() - Stops monitoring of a given context.
2224d848
SP
689 * @ctx: monitoring context
690 *
691 * Return: 0 on success, negative error code otherwise.
692 */
693static int __damon_stop(struct damon_ctx *ctx)
694{
0f91d133
CD
695 struct task_struct *tsk;
696
2224d848 697 mutex_lock(&ctx->kdamond_lock);
0f91d133
CD
698 tsk = ctx->kdamond;
699 if (tsk) {
700 get_task_struct(tsk);
2224d848 701 mutex_unlock(&ctx->kdamond_lock);
0f91d133
CD
702 kthread_stop(tsk);
703 put_task_struct(tsk);
2224d848
SP
704 return 0;
705 }
706 mutex_unlock(&ctx->kdamond_lock);
707
708 return -EPERM;
709}
710
711/**
712 * damon_stop() - Stops the monitorings for a given group of contexts.
713 * @ctxs: an array of the pointers for contexts to stop monitoring
714 * @nr_ctxs: size of @ctxs
715 *
716 * Return: 0 on success, negative error code otherwise.
717 */
718int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
719{
720 int i, err = 0;
721
722 for (i = 0; i < nr_ctxs; i++) {
723 /* nr_running_ctxs is decremented in kdamond_fn */
724 err = __damon_stop(ctxs[i]);
725 if (err)
8b9b0d33 726 break;
2224d848 727 }
2224d848
SP
728 return err;
729}
730
731/*
732 * damon_check_reset_time_interval() - Check if a time interval is elapsed.
733 * @baseline: the time to check whether the interval has elapsed since
734 * @interval: the time interval (microseconds)
735 *
736 * See whether the given time interval has passed since the given baseline
737 * time. If so, it also updates the baseline to current time for next check.
738 *
739 * Return: true if the time interval has passed, or false otherwise.
740 */
741static bool damon_check_reset_time_interval(struct timespec64 *baseline,
742 unsigned long interval)
743{
744 struct timespec64 now;
745
746 ktime_get_coarse_ts64(&now);
747 if ((timespec64_to_ns(&now) - timespec64_to_ns(baseline)) <
748 interval * 1000)
749 return false;
750 *baseline = now;
751 return true;
752}
753
754/*
755 * Check whether it is time to flush the aggregated information
756 */
757static bool kdamond_aggregate_interval_passed(struct damon_ctx *ctx)
758{
759 return damon_check_reset_time_interval(&ctx->last_aggregation,
cbeaa77b 760 ctx->attrs.aggr_interval);
2224d848
SP
761}
762
f23b8eee
SP
763/*
764 * Reset the aggregated monitoring results ('nr_accesses' of each region).
765 */
766static void kdamond_reset_aggregated(struct damon_ctx *c)
767{
768 struct damon_target *t;
76fd0285 769 unsigned int ti = 0; /* target's index */
f23b8eee
SP
770
771 damon_for_each_target(t, c) {
772 struct damon_region *r;
773
2fcb9362 774 damon_for_each_region(r, t) {
76fd0285 775 trace_damon_aggregated(t, ti, r, damon_nr_regions(t));
fda504fa 776 r->last_nr_accesses = r->nr_accesses;
f23b8eee 777 r->nr_accesses = 0;
2fcb9362 778 }
76fd0285 779 ti++;
f23b8eee
SP
780 }
781}
782
4ed98243
KX
783static void damon_split_region_at(struct damon_target *t,
784 struct damon_region *r, unsigned long sz_r);
2b8a248d 785
38683e00
SP
786static bool __damos_valid_target(struct damon_region *r, struct damos *s)
787{
788 unsigned long sz;
789
ab63f63f 790 sz = damon_sz_region(r);
f5a79d7c
YD
791 return s->pattern.min_sz_region <= sz &&
792 sz <= s->pattern.max_sz_region &&
793 s->pattern.min_nr_accesses <= r->nr_accesses &&
794 r->nr_accesses <= s->pattern.max_nr_accesses &&
795 s->pattern.min_age_region <= r->age &&
796 r->age <= s->pattern.max_age_region;
38683e00
SP
797}
798
799static bool damos_valid_target(struct damon_ctx *c, struct damon_target *t,
800 struct damon_region *r, struct damos *s)
801{
802 bool ret = __damos_valid_target(r, s);
803
f7d911c3 804 if (!ret || !s->quota.esz || !c->ops.get_scheme_score)
38683e00
SP
805 return ret;
806
f7d911c3 807 return c->ops.get_scheme_score(c, t, r, s) >= s->quota.min_score;
38683e00
SP
808}
809
2ea34989
SP
810/*
811 * damos_skip_charged_region() - Check if the given region or starting part of
812 * it is already charged for the DAMOS quota.
813 * @t: The target of the region.
814 * @rp: The pointer to the region.
815 * @s: The scheme to be applied.
816 *
817 * If a quota of a scheme has exceeded in a quota charge window, the scheme's
818 * action would applied to only a part of the target access pattern fulfilling
819 * regions. To avoid applying the scheme action to only already applied
820 * regions, DAMON skips applying the scheme action to the regions that charged
821 * in the previous charge window.
822 *
823 * This function checks if a given region should be skipped or not for the
824 * reason. If only the starting part of the region has previously charged,
825 * this function splits the region into two so that the second one covers the
826 * area that not charged in the previous charge widnow and saves the second
827 * region in *rp and returns false, so that the caller can apply DAMON action
828 * to the second one.
829 *
830 * Return: true if the region should be entirely skipped, false otherwise.
831 */
832static bool damos_skip_charged_region(struct damon_target *t,
833 struct damon_region **rp, struct damos *s)
834{
835 struct damon_region *r = *rp;
836 struct damos_quota *quota = &s->quota;
837 unsigned long sz_to_skip;
838
839 /* Skip previously charged regions */
840 if (quota->charge_target_from) {
841 if (t != quota->charge_target_from)
842 return true;
843 if (r == damon_last_region(t)) {
844 quota->charge_target_from = NULL;
845 quota->charge_addr_from = 0;
846 return true;
847 }
848 if (quota->charge_addr_from &&
849 r->ar.end <= quota->charge_addr_from)
850 return true;
851
852 if (quota->charge_addr_from && r->ar.start <
853 quota->charge_addr_from) {
854 sz_to_skip = ALIGN_DOWN(quota->charge_addr_from -
855 r->ar.start, DAMON_MIN_REGION);
856 if (!sz_to_skip) {
857 if (damon_sz_region(r) <= DAMON_MIN_REGION)
858 return true;
859 sz_to_skip = DAMON_MIN_REGION;
860 }
861 damon_split_region_at(t, r, sz_to_skip);
862 r = damon_next_region(r);
863 *rp = r;
864 }
865 quota->charge_target_from = NULL;
866 quota->charge_addr_from = 0;
867 }
868 return false;
869}
870
d1cbbf62
SP
871static void damos_update_stat(struct damos *s,
872 unsigned long sz_tried, unsigned long sz_applied)
873{
874 s->stat.nr_tried++;
875 s->stat.sz_tried += sz_tried;
876 if (sz_applied)
877 s->stat.nr_applied++;
878 s->stat.sz_applied += sz_applied;
879}
880
e63a30c5
SP
881static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t,
882 struct damon_region *r, struct damos *s)
883{
884 struct damos_quota *quota = &s->quota;
885 unsigned long sz = damon_sz_region(r);
886 struct timespec64 begin, end;
887 unsigned long sz_applied = 0;
44467bbb 888 int err = 0;
e63a30c5
SP
889
890 if (c->ops.apply_scheme) {
891 if (quota->esz && quota->charged_sz + sz > quota->esz) {
892 sz = ALIGN_DOWN(quota->esz - quota->charged_sz,
893 DAMON_MIN_REGION);
894 if (!sz)
895 goto update_stat;
896 damon_split_region_at(t, r, sz);
897 }
898 ktime_get_coarse_ts64(&begin);
44467bbb
SP
899 if (c->callback.before_damos_apply)
900 err = c->callback.before_damos_apply(c, t, r, s);
901 if (!err)
902 sz_applied = c->ops.apply_scheme(c, t, r, s);
e63a30c5
SP
903 ktime_get_coarse_ts64(&end);
904 quota->total_charged_ns += timespec64_to_ns(&end) -
905 timespec64_to_ns(&begin);
906 quota->charged_sz += sz;
907 if (quota->esz && quota->charged_sz >= quota->esz) {
908 quota->charge_target_from = t;
909 quota->charge_addr_from = r->ar.end + 1;
910 }
911 }
912 if (s->action != DAMOS_STAT)
913 r->age = 0;
914
915update_stat:
d1cbbf62 916 damos_update_stat(s, sz, sz_applied);
e63a30c5
SP
917}
918
1f366e42
SP
919static void damon_do_apply_schemes(struct damon_ctx *c,
920 struct damon_target *t,
921 struct damon_region *r)
922{
923 struct damos *s;
1f366e42
SP
924
925 damon_for_each_scheme(s, c) {
2b8a248d 926 struct damos_quota *quota = &s->quota;
2b8a248d 927
ee801b7d
SP
928 if (!s->wmarks.activated)
929 continue;
930
2b8a248d 931 /* Check the quota */
1cd24303 932 if (quota->esz && quota->charged_sz >= quota->esz)
2b8a248d
SP
933 continue;
934
2ea34989
SP
935 if (damos_skip_charged_region(t, &r, s))
936 continue;
50585192 937
38683e00 938 if (!damos_valid_target(c, t, r, s))
1f366e42 939 continue;
2b8a248d 940
e63a30c5 941 damos_apply_scheme(c, t, r, s);
1f366e42
SP
942 }
943}
944
1cd24303
SP
945/* Shouldn't be called if quota->ms and quota->sz are zero */
946static void damos_set_effective_quota(struct damos_quota *quota)
947{
948 unsigned long throughput;
949 unsigned long esz;
950
951 if (!quota->ms) {
952 quota->esz = quota->sz;
953 return;
954 }
955
956 if (quota->total_charged_ns)
957 throughput = quota->total_charged_sz * 1000000 /
958 quota->total_charged_ns;
959 else
960 throughput = PAGE_SIZE * 1024;
961 esz = throughput * quota->ms;
962
963 if (quota->sz && quota->sz < esz)
964 esz = quota->sz;
965 quota->esz = esz;
966}
967
898810e5 968static void damos_adjust_quota(struct damon_ctx *c, struct damos *s)
1f366e42 969{
898810e5 970 struct damos_quota *quota = &s->quota;
1f366e42 971 struct damon_target *t;
898810e5
SP
972 struct damon_region *r;
973 unsigned long cumulated_sz;
974 unsigned int score, max_score = 0;
2b8a248d 975
898810e5
SP
976 if (!quota->ms && !quota->sz)
977 return;
2b8a248d 978
898810e5
SP
979 /* New charge window starts */
980 if (time_after_eq(jiffies, quota->charged_from +
981 msecs_to_jiffies(quota->reset_interval))) {
982 if (quota->esz && quota->charged_sz >= quota->esz)
983 s->stat.qt_exceeds++;
984 quota->total_charged_sz += quota->charged_sz;
985 quota->charged_from = jiffies;
986 quota->charged_sz = 0;
987 damos_set_effective_quota(quota);
988 }
ee801b7d 989
898810e5
SP
990 if (!c->ops.get_scheme_score)
991 return;
2b8a248d 992
898810e5
SP
993 /* Fill up the score histogram */
994 memset(quota->histogram, 0, sizeof(quota->histogram));
995 damon_for_each_target(t, c) {
996 damon_for_each_region(r, t) {
997 if (!__damos_valid_target(r, s))
998 continue;
999 score = c->ops.get_scheme_score(c, t, r, s);
1000 quota->histogram[score] += damon_sz_region(r);
1001 if (score > max_score)
1002 max_score = score;
2b8a248d 1003 }
898810e5 1004 }
38683e00 1005
898810e5
SP
1006 /* Set the min score limit */
1007 for (cumulated_sz = 0, score = max_score; ; score--) {
1008 cumulated_sz += quota->histogram[score];
1009 if (cumulated_sz >= quota->esz || !score)
1010 break;
1011 }
1012 quota->min_score = score;
1013}
38683e00 1014
898810e5
SP
1015static void kdamond_apply_schemes(struct damon_ctx *c)
1016{
1017 struct damon_target *t;
1018 struct damon_region *r, *next_r;
1019 struct damos *s;
38683e00 1020
898810e5
SP
1021 damon_for_each_scheme(s, c) {
1022 if (!s->wmarks.activated)
1023 continue;
1024
1025 damos_adjust_quota(c, s);
2b8a248d 1026 }
1f366e42
SP
1027
1028 damon_for_each_target(t, c) {
2b8a248d 1029 damon_for_each_region_safe(r, next_r, t)
1f366e42
SP
1030 damon_do_apply_schemes(c, t, r);
1031 }
1032}
1033
b9a6ac4e
SP
1034/*
1035 * Merge two adjacent regions into one region
1036 */
1037static void damon_merge_two_regions(struct damon_target *t,
1038 struct damon_region *l, struct damon_region *r)
1039{
652e0446 1040 unsigned long sz_l = damon_sz_region(l), sz_r = damon_sz_region(r);
b9a6ac4e
SP
1041
1042 l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) /
1043 (sz_l + sz_r);
fda504fa 1044 l->age = (l->age * sz_l + r->age * sz_r) / (sz_l + sz_r);
b9a6ac4e
SP
1045 l->ar.end = r->ar.end;
1046 damon_destroy_region(r, t);
1047}
1048
b9a6ac4e
SP
1049/*
1050 * Merge adjacent regions having similar access frequencies
1051 *
1052 * t target affected by this merge operation
1053 * thres '->nr_accesses' diff threshold for the merge
1054 * sz_limit size upper limit of each region
1055 */
1056static void damon_merge_regions_of(struct damon_target *t, unsigned int thres,
1057 unsigned long sz_limit)
1058{
1059 struct damon_region *r, *prev = NULL, *next;
1060
1061 damon_for_each_region_safe(r, next, t) {
d720bbbd 1062 if (abs(r->nr_accesses - r->last_nr_accesses) > thres)
fda504fa
SP
1063 r->age = 0;
1064 else
1065 r->age++;
1066
b9a6ac4e 1067 if (prev && prev->ar.end == r->ar.start &&
d720bbbd 1068 abs(prev->nr_accesses - r->nr_accesses) <= thres &&
652e0446 1069 damon_sz_region(prev) + damon_sz_region(r) <= sz_limit)
b9a6ac4e
SP
1070 damon_merge_two_regions(t, prev, r);
1071 else
1072 prev = r;
1073 }
1074}
1075
1076/*
1077 * Merge adjacent regions having similar access frequencies
1078 *
1079 * threshold '->nr_accesses' diff threshold for the merge
1080 * sz_limit size upper limit of each region
1081 *
1082 * This function merges monitoring target regions which are adjacent and their
1083 * access frequencies are similar. This is for minimizing the monitoring
1084 * overhead under the dynamically changeable access pattern. If a merge was
1085 * unnecessarily made, later 'kdamond_split_regions()' will revert it.
1086 */
1087static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
1088 unsigned long sz_limit)
1089{
1090 struct damon_target *t;
1091
1092 damon_for_each_target(t, c)
1093 damon_merge_regions_of(t, threshold, sz_limit);
1094}
1095
1096/*
1097 * Split a region in two
1098 *
1099 * r the region to be split
1100 * sz_r size of the first sub-region that will be made
1101 */
4ed98243
KX
1102static void damon_split_region_at(struct damon_target *t,
1103 struct damon_region *r, unsigned long sz_r)
b9a6ac4e
SP
1104{
1105 struct damon_region *new;
1106
1107 new = damon_new_region(r->ar.start + sz_r, r->ar.end);
1108 if (!new)
1109 return;
1110
1111 r->ar.end = new->ar.start;
1112
fda504fa
SP
1113 new->age = r->age;
1114 new->last_nr_accesses = r->last_nr_accesses;
1115
b9a6ac4e
SP
1116 damon_insert_region(new, r, damon_next_region(r), t);
1117}
1118
1119/* Split every region in the given target into 'nr_subs' regions */
4ed98243 1120static void damon_split_regions_of(struct damon_target *t, int nr_subs)
b9a6ac4e
SP
1121{
1122 struct damon_region *r, *next;
1123 unsigned long sz_region, sz_sub = 0;
1124 int i;
1125
1126 damon_for_each_region_safe(r, next, t) {
ab63f63f 1127 sz_region = damon_sz_region(r);
b9a6ac4e
SP
1128
1129 for (i = 0; i < nr_subs - 1 &&
1130 sz_region > 2 * DAMON_MIN_REGION; i++) {
1131 /*
1132 * Randomly select size of left sub-region to be at
1133 * least 10 percent and at most 90% of original region
1134 */
1135 sz_sub = ALIGN_DOWN(damon_rand(1, 10) *
1136 sz_region / 10, DAMON_MIN_REGION);
1137 /* Do not allow blank region */
1138 if (sz_sub == 0 || sz_sub >= sz_region)
1139 continue;
1140
4ed98243 1141 damon_split_region_at(t, r, sz_sub);
b9a6ac4e
SP
1142 sz_region = sz_sub;
1143 }
1144 }
1145}
1146
1147/*
1148 * Split every target region into randomly-sized small regions
1149 *
1150 * This function splits every target region into random-sized small regions if
1151 * current total number of the regions is equal or smaller than half of the
1152 * user-specified maximum number of regions. This is for maximizing the
1153 * monitoring accuracy under the dynamically changeable access patterns. If a
1154 * split was unnecessarily made, later 'kdamond_merge_regions()' will revert
1155 * it.
1156 */
1157static void kdamond_split_regions(struct damon_ctx *ctx)
1158{
1159 struct damon_target *t;
1160 unsigned int nr_regions = 0;
1161 static unsigned int last_nr_regions;
1162 int nr_subregions = 2;
1163
1164 damon_for_each_target(t, ctx)
1165 nr_regions += damon_nr_regions(t);
1166
cbeaa77b 1167 if (nr_regions > ctx->attrs.max_nr_regions / 2)
b9a6ac4e
SP
1168 return;
1169
1170 /* Maybe the middle of the region has different access frequency */
1171 if (last_nr_regions == nr_regions &&
cbeaa77b 1172 nr_regions < ctx->attrs.max_nr_regions / 3)
b9a6ac4e
SP
1173 nr_subregions = 3;
1174
1175 damon_for_each_target(t, ctx)
4ed98243 1176 damon_split_regions_of(t, nr_subregions);
b9a6ac4e
SP
1177
1178 last_nr_regions = nr_regions;
1179}
1180
2224d848 1181/*
f7d911c3
SP
1182 * Check whether it is time to check and apply the operations-related data
1183 * structures.
2224d848
SP
1184 *
1185 * Returns true if it is.
1186 */
f7d911c3 1187static bool kdamond_need_update_operations(struct damon_ctx *ctx)
2224d848 1188{
f7d911c3 1189 return damon_check_reset_time_interval(&ctx->last_ops_update,
cbeaa77b 1190 ctx->attrs.ops_update_interval);
2224d848
SP
1191}
1192
1193/*
1194 * Check whether current monitoring should be stopped
1195 *
1196 * The monitoring is stopped when either the user requested to stop, or all
1197 * monitoring targets are invalid.
1198 *
1199 * Returns true if need to stop current monitoring.
1200 */
1201static bool kdamond_need_stop(struct damon_ctx *ctx)
1202{
f23b8eee 1203 struct damon_target *t;
2224d848 1204
0f91d133 1205 if (kthread_should_stop())
2224d848
SP
1206 return true;
1207
f7d911c3 1208 if (!ctx->ops.target_valid)
2224d848
SP
1209 return false;
1210
f23b8eee 1211 damon_for_each_target(t, ctx) {
f7d911c3 1212 if (ctx->ops.target_valid(t))
f23b8eee
SP
1213 return false;
1214 }
1215
1216 return true;
2224d848
SP
1217}
1218
ee801b7d
SP
1219static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
1220{
1221 struct sysinfo i;
1222
1223 switch (metric) {
1224 case DAMOS_WMARK_FREE_MEM_RATE:
1225 si_meminfo(&i);
1226 return i.freeram * 1000 / i.totalram;
1227 default:
1228 break;
1229 }
1230 return -EINVAL;
1231}
1232
1233/*
1234 * Returns zero if the scheme is active. Else, returns time to wait for next
1235 * watermark check in micro-seconds.
1236 */
1237static unsigned long damos_wmark_wait_us(struct damos *scheme)
1238{
1239 unsigned long metric;
1240
1241 if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
1242 return 0;
1243
1244 metric = damos_wmark_metric_value(scheme->wmarks.metric);
1245 /* higher than high watermark or lower than low watermark */
1246 if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
1247 if (scheme->wmarks.activated)
01078655 1248 pr_debug("deactivate a scheme (%d) for %s wmark\n",
ee801b7d
SP
1249 scheme->action,
1250 metric > scheme->wmarks.high ?
1251 "high" : "low");
1252 scheme->wmarks.activated = false;
1253 return scheme->wmarks.interval;
1254 }
1255
1256 /* inactive and higher than middle watermark */
1257 if ((scheme->wmarks.high >= metric && metric >= scheme->wmarks.mid) &&
1258 !scheme->wmarks.activated)
1259 return scheme->wmarks.interval;
1260
1261 if (!scheme->wmarks.activated)
1262 pr_debug("activate a scheme (%d)\n", scheme->action);
1263 scheme->wmarks.activated = true;
1264 return 0;
1265}
1266
1267static void kdamond_usleep(unsigned long usecs)
1268{
4de46a30
SP
1269 /* See Documentation/timers/timers-howto.rst for the thresholds */
1270 if (usecs > 20 * USEC_PER_MSEC)
70e92748 1271 schedule_timeout_idle(usecs_to_jiffies(usecs));
ee801b7d 1272 else
70e92748 1273 usleep_idle_range(usecs, usecs + 1);
ee801b7d
SP
1274}
1275
1276/* Returns negative error code if it's not activated but should return */
1277static int kdamond_wait_activation(struct damon_ctx *ctx)
1278{
1279 struct damos *s;
1280 unsigned long wait_time;
1281 unsigned long min_wait_time = 0;
78049e94 1282 bool init_wait_time = false;
ee801b7d
SP
1283
1284 while (!kdamond_need_stop(ctx)) {
1285 damon_for_each_scheme(s, ctx) {
1286 wait_time = damos_wmark_wait_us(s);
78049e94
JK
1287 if (!init_wait_time || wait_time < min_wait_time) {
1288 init_wait_time = true;
ee801b7d 1289 min_wait_time = wait_time;
78049e94 1290 }
ee801b7d
SP
1291 }
1292 if (!min_wait_time)
1293 return 0;
1294
1295 kdamond_usleep(min_wait_time);
6e74d2bf
SP
1296
1297 if (ctx->callback.after_wmarks_check &&
1298 ctx->callback.after_wmarks_check(ctx))
1299 break;
ee801b7d
SP
1300 }
1301 return -EBUSY;
1302}
1303
2224d848
SP
1304/*
1305 * The monitoring daemon that runs as a kernel thread
1306 */
1307static int kdamond_fn(void *data)
1308{
cef4493f 1309 struct damon_ctx *ctx = data;
f23b8eee
SP
1310 struct damon_target *t;
1311 struct damon_region *r, *next;
b9a6ac4e
SP
1312 unsigned int max_nr_accesses = 0;
1313 unsigned long sz_limit = 0;
2224d848 1314
42e4cef5 1315 pr_debug("kdamond (%d) starts\n", current->pid);
2224d848 1316
f7d911c3
SP
1317 if (ctx->ops.init)
1318 ctx->ops.init(ctx);
2224d848 1319 if (ctx->callback.before_start && ctx->callback.before_start(ctx))
29454cf6 1320 goto done;
2224d848 1321
b9a6ac4e
SP
1322 sz_limit = damon_region_sz_limit(ctx);
1323
29454cf6
KX
1324 while (!kdamond_need_stop(ctx)) {
1325 if (kdamond_wait_activation(ctx))
1326 break;
ee801b7d 1327
f7d911c3
SP
1328 if (ctx->ops.prepare_access_checks)
1329 ctx->ops.prepare_access_checks(ctx);
2224d848 1330 if (ctx->callback.after_sampling &&
29454cf6
KX
1331 ctx->callback.after_sampling(ctx))
1332 break;
2224d848 1333
cbeaa77b 1334 kdamond_usleep(ctx->attrs.sample_interval);
2224d848 1335
f7d911c3
SP
1336 if (ctx->ops.check_accesses)
1337 max_nr_accesses = ctx->ops.check_accesses(ctx);
2224d848
SP
1338
1339 if (kdamond_aggregate_interval_passed(ctx)) {
b9a6ac4e
SP
1340 kdamond_merge_regions(ctx,
1341 max_nr_accesses / 10,
1342 sz_limit);
2224d848 1343 if (ctx->callback.after_aggregation &&
29454cf6
KX
1344 ctx->callback.after_aggregation(ctx))
1345 break;
64517d6e
HY
1346 if (!list_empty(&ctx->schemes))
1347 kdamond_apply_schemes(ctx);
f23b8eee 1348 kdamond_reset_aggregated(ctx);
b9a6ac4e 1349 kdamond_split_regions(ctx);
f7d911c3
SP
1350 if (ctx->ops.reset_aggregated)
1351 ctx->ops.reset_aggregated(ctx);
2224d848
SP
1352 }
1353
f7d911c3
SP
1354 if (kdamond_need_update_operations(ctx)) {
1355 if (ctx->ops.update)
1356 ctx->ops.update(ctx);
b9a6ac4e 1357 sz_limit = damon_region_sz_limit(ctx);
2224d848
SP
1358 }
1359 }
29454cf6 1360done:
f23b8eee
SP
1361 damon_for_each_target(t, ctx) {
1362 damon_for_each_region_safe(r, next, t)
b9a6ac4e 1363 damon_destroy_region(r, t);
f23b8eee 1364 }
2224d848 1365
0f91d133
CD
1366 if (ctx->callback.before_terminate)
1367 ctx->callback.before_terminate(ctx);
f7d911c3
SP
1368 if (ctx->ops.cleanup)
1369 ctx->ops.cleanup(ctx);
2224d848 1370
42e4cef5 1371 pr_debug("kdamond (%d) finishes\n", current->pid);
2224d848
SP
1372 mutex_lock(&ctx->kdamond_lock);
1373 ctx->kdamond = NULL;
1374 mutex_unlock(&ctx->kdamond_lock);
1375
1376 mutex_lock(&damon_lock);
1377 nr_running_ctxs--;
8b9b0d33
SP
1378 if (!nr_running_ctxs && running_exclusive_ctxs)
1379 running_exclusive_ctxs = false;
2224d848
SP
1380 mutex_unlock(&damon_lock);
1381
5f7fe2b9 1382 return 0;
2224d848 1383}
17ccae8b 1384
0d83b2d8
XH
1385/*
1386 * struct damon_system_ram_region - System RAM resource address region of
1387 * [@start, @end).
1388 * @start: Start address of the region (inclusive).
1389 * @end: End address of the region (exclusive).
1390 */
1391struct damon_system_ram_region {
1392 unsigned long start;
1393 unsigned long end;
1394};
1395
1396static int walk_system_ram(struct resource *res, void *arg)
1397{
1398 struct damon_system_ram_region *a = arg;
1399
1400 if (a->end - a->start < resource_size(res)) {
1401 a->start = res->start;
1402 a->end = res->end;
1403 }
1404 return 0;
1405}
1406
1407/*
1408 * Find biggest 'System RAM' resource and store its start and end address in
1409 * @start and @end, respectively. If no System RAM is found, returns false.
1410 */
233f0b31
KX
1411static bool damon_find_biggest_system_ram(unsigned long *start,
1412 unsigned long *end)
0d83b2d8
XH
1413
1414{
1415 struct damon_system_ram_region arg = {};
1416
1417 walk_system_ram_res(0, ULONG_MAX, &arg, walk_system_ram);
1418 if (arg.end <= arg.start)
1419 return false;
1420
1421 *start = arg.start;
1422 *end = arg.end;
1423 return true;
1424}
1425
233f0b31
KX
1426/**
1427 * damon_set_region_biggest_system_ram_default() - Set the region of the given
1428 * monitoring target as requested, or biggest 'System RAM'.
1429 * @t: The monitoring target to set the region.
1430 * @start: The pointer to the start address of the region.
1431 * @end: The pointer to the end address of the region.
1432 *
1433 * This function sets the region of @t as requested by @start and @end. If the
1434 * values of @start and @end are zero, however, this function finds the biggest
1435 * 'System RAM' resource and sets the region to cover the resource. In the
1436 * latter case, this function saves the start and end addresses of the resource
1437 * in @start and @end, respectively.
1438 *
1439 * Return: 0 on success, negative error code otherwise.
1440 */
1441int damon_set_region_biggest_system_ram_default(struct damon_target *t,
1442 unsigned long *start, unsigned long *end)
1443{
1444 struct damon_addr_range addr_range;
1445
1446 if (*start > *end)
1447 return -EINVAL;
1448
1449 if (!*start && !*end &&
1450 !damon_find_biggest_system_ram(start, end))
1451 return -EINVAL;
1452
1453 addr_range.start = *start;
1454 addr_range.end = *end;
1455 return damon_set_regions(t, &addr_range, 1);
1456}
1457
a1870944
DL
1458static int __init damon_init(void)
1459{
1460 damon_region_cache = KMEM_CACHE(damon_region, 0);
1461 if (unlikely(!damon_region_cache)) {
1462 pr_err("creating damon_region_cache fails\n");
1463 return -ENOMEM;
1464 }
1465
1466 return 0;
1467}
1468
1469subsys_initcall(damon_init);
1470
17ccae8b 1471#include "core-test.h"