1 // SPDX-License-Identifier: GPL-2.0
5 * Author: SeongJae Park <sjpark@amazon.de>
8 #define pr_fmt(fmt) "damon: " fmt
10 #include <linux/damon.h>
11 #include <linux/delay.h>
12 #include <linux/kthread.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
17 #define CREATE_TRACE_POINTS
18 #include <trace/events/damon.h>
20 #ifdef CONFIG_DAMON_KUNIT_TEST
21 #undef DAMON_MIN_REGION
22 #define DAMON_MIN_REGION 1
25 static DEFINE_MUTEX(damon_lock);
26 static int nr_running_ctxs;
27 static bool running_exclusive_ctxs;
29 static DEFINE_MUTEX(damon_ops_lock);
30 static struct damon_operations damon_registered_ops[NR_DAMON_OPS];
32 /* Should be called under damon_ops_lock with id smaller than NR_DAMON_OPS */
33 static bool __damon_is_registered_ops(enum damon_ops_id id)
35 struct damon_operations empty_ops = {};
37 if (!memcmp(&empty_ops, &damon_registered_ops[id], sizeof(empty_ops)))
43 * damon_is_registered_ops() - Check if a given damon_operations is registered.
44 * @id: Id of the damon_operations to check if registered.
46 * Return: true if the ops is set, false otherwise.
48 bool damon_is_registered_ops(enum damon_ops_id id)
52 if (id >= NR_DAMON_OPS)
54 mutex_lock(&damon_ops_lock);
55 registered = __damon_is_registered_ops(id);
56 mutex_unlock(&damon_ops_lock);
61 * damon_register_ops() - Register a monitoring operations set to DAMON.
62 * @ops: monitoring operations set to register.
64 * This function registers a monitoring operations set of valid &struct
65 * damon_operations->id so that others can find and use them later.
67 * Return: 0 on success, negative error code otherwise.
69 int damon_register_ops(struct damon_operations *ops)
73 if (ops->id >= NR_DAMON_OPS)
75 mutex_lock(&damon_ops_lock);
76 /* Fail for already registered ops */
77 if (__damon_is_registered_ops(ops->id)) {
81 damon_registered_ops[ops->id] = *ops;
83 mutex_unlock(&damon_ops_lock);
88 * damon_select_ops() - Select a monitoring operations to use with the context.
89 * @ctx: monitoring context to use the operations.
90 * @id: id of the registered monitoring operations to select.
92 * This function finds registered monitoring operations set of @id and make
95 * Return: 0 on success, negative error code otherwise.
97 int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id)
101 if (id >= NR_DAMON_OPS)
104 mutex_lock(&damon_ops_lock);
105 if (!__damon_is_registered_ops(id))
108 ctx->ops = damon_registered_ops[id];
109 mutex_unlock(&damon_ops_lock);
114 * Construct a damon_region struct
116 * Returns the pointer to the new struct if success, or NULL otherwise
118 struct damon_region *damon_new_region(unsigned long start, unsigned long end)
120 struct damon_region *region;
122 region = kmalloc(sizeof(*region), GFP_KERNEL);
126 region->ar.start = start;
127 region->ar.end = end;
128 region->nr_accesses = 0;
129 INIT_LIST_HEAD(®ion->list);
132 region->last_nr_accesses = 0;
137 void damon_add_region(struct damon_region *r, struct damon_target *t)
139 list_add_tail(&r->list, &t->regions_list);
143 static void damon_del_region(struct damon_region *r, struct damon_target *t)
149 static void damon_free_region(struct damon_region *r)
154 void damon_destroy_region(struct damon_region *r, struct damon_target *t)
156 damon_del_region(r, t);
157 damon_free_region(r);
161 * Check whether a region is intersecting an address range
163 * Returns true if it is.
165 static bool damon_intersect(struct damon_region *r,
166 struct damon_addr_range *re)
168 return !(r->ar.end <= re->start || re->end <= r->ar.start);
172 * damon_set_regions() - Set regions of a target for given address ranges.
173 * @t: the given target.
174 * @ranges: array of new monitoring target ranges.
175 * @nr_ranges: length of @ranges.
177 * This function adds new regions to, or modify existing regions of a
178 * monitoring target to fit in specific ranges.
180 * Return: 0 if success, or negative error code otherwise.
182 int damon_set_regions(struct damon_target *t, struct damon_addr_range *ranges,
183 unsigned int nr_ranges)
185 struct damon_region *r, *next;
188 /* Remove regions which are not in the new ranges */
189 damon_for_each_region_safe(r, next, t) {
190 for (i = 0; i < nr_ranges; i++) {
191 if (damon_intersect(r, &ranges[i]))
195 damon_destroy_region(r, t);
198 /* Add new regions or resize existing regions to fit in the ranges */
199 for (i = 0; i < nr_ranges; i++) {
200 struct damon_region *first = NULL, *last, *newr;
201 struct damon_addr_range *range;
204 /* Get the first/last regions intersecting with the range */
205 damon_for_each_region(r, t) {
206 if (damon_intersect(r, range)) {
211 if (r->ar.start >= range->end)
215 /* no region intersects with this range */
216 newr = damon_new_region(
217 ALIGN_DOWN(range->start,
219 ALIGN(range->end, DAMON_MIN_REGION));
222 damon_insert_region(newr, damon_prev_region(r), r, t);
224 /* resize intersecting regions to fit in this range */
225 first->ar.start = ALIGN_DOWN(range->start,
227 last->ar.end = ALIGN(range->end, DAMON_MIN_REGION);
233 struct damos *damon_new_scheme(
234 unsigned long min_sz_region, unsigned long max_sz_region,
235 unsigned int min_nr_accesses, unsigned int max_nr_accesses,
236 unsigned int min_age_region, unsigned int max_age_region,
237 enum damos_action action, struct damos_quota *quota,
238 struct damos_watermarks *wmarks)
240 struct damos *scheme;
242 scheme = kmalloc(sizeof(*scheme), GFP_KERNEL);
245 scheme->min_sz_region = min_sz_region;
246 scheme->max_sz_region = max_sz_region;
247 scheme->min_nr_accesses = min_nr_accesses;
248 scheme->max_nr_accesses = max_nr_accesses;
249 scheme->min_age_region = min_age_region;
250 scheme->max_age_region = max_age_region;
251 scheme->action = action;
252 scheme->stat = (struct damos_stat){};
253 INIT_LIST_HEAD(&scheme->list);
255 scheme->quota.ms = quota->ms;
256 scheme->quota.sz = quota->sz;
257 scheme->quota.reset_interval = quota->reset_interval;
258 scheme->quota.weight_sz = quota->weight_sz;
259 scheme->quota.weight_nr_accesses = quota->weight_nr_accesses;
260 scheme->quota.weight_age = quota->weight_age;
261 scheme->quota.total_charged_sz = 0;
262 scheme->quota.total_charged_ns = 0;
263 scheme->quota.esz = 0;
264 scheme->quota.charged_sz = 0;
265 scheme->quota.charged_from = 0;
266 scheme->quota.charge_target_from = NULL;
267 scheme->quota.charge_addr_from = 0;
269 scheme->wmarks.metric = wmarks->metric;
270 scheme->wmarks.interval = wmarks->interval;
271 scheme->wmarks.high = wmarks->high;
272 scheme->wmarks.mid = wmarks->mid;
273 scheme->wmarks.low = wmarks->low;
274 scheme->wmarks.activated = true;
279 void damon_add_scheme(struct damon_ctx *ctx, struct damos *s)
281 list_add_tail(&s->list, &ctx->schemes);
284 static void damon_del_scheme(struct damos *s)
289 static void damon_free_scheme(struct damos *s)
294 void damon_destroy_scheme(struct damos *s)
297 damon_free_scheme(s);
301 * Construct a damon_target struct
303 * Returns the pointer to the new struct if success, or NULL otherwise
305 struct damon_target *damon_new_target(void)
307 struct damon_target *t;
309 t = kmalloc(sizeof(*t), GFP_KERNEL);
315 INIT_LIST_HEAD(&t->regions_list);
320 void damon_add_target(struct damon_ctx *ctx, struct damon_target *t)
322 list_add_tail(&t->list, &ctx->adaptive_targets);
325 bool damon_targets_empty(struct damon_ctx *ctx)
327 return list_empty(&ctx->adaptive_targets);
330 static void damon_del_target(struct damon_target *t)
335 void damon_free_target(struct damon_target *t)
337 struct damon_region *r, *next;
339 damon_for_each_region_safe(r, next, t)
340 damon_free_region(r);
344 void damon_destroy_target(struct damon_target *t)
347 damon_free_target(t);
350 unsigned int damon_nr_regions(struct damon_target *t)
352 return t->nr_regions;
355 struct damon_ctx *damon_new_ctx(void)
357 struct damon_ctx *ctx;
359 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
363 ctx->sample_interval = 5 * 1000;
364 ctx->aggr_interval = 100 * 1000;
365 ctx->ops_update_interval = 60 * 1000 * 1000;
367 ktime_get_coarse_ts64(&ctx->last_aggregation);
368 ctx->last_ops_update = ctx->last_aggregation;
370 mutex_init(&ctx->kdamond_lock);
372 ctx->min_nr_regions = 10;
373 ctx->max_nr_regions = 1000;
375 INIT_LIST_HEAD(&ctx->adaptive_targets);
376 INIT_LIST_HEAD(&ctx->schemes);
381 static void damon_destroy_targets(struct damon_ctx *ctx)
383 struct damon_target *t, *next_t;
385 if (ctx->ops.cleanup) {
386 ctx->ops.cleanup(ctx);
390 damon_for_each_target_safe(t, next_t, ctx)
391 damon_destroy_target(t);
394 void damon_destroy_ctx(struct damon_ctx *ctx)
396 struct damos *s, *next_s;
398 damon_destroy_targets(ctx);
400 damon_for_each_scheme_safe(s, next_s, ctx)
401 damon_destroy_scheme(s);
407 * damon_set_attrs() - Set attributes for the monitoring.
408 * @ctx: monitoring context
409 * @sample_int: time interval between samplings
410 * @aggr_int: time interval between aggregations
411 * @ops_upd_int: time interval between monitoring operations updates
412 * @min_nr_reg: minimal number of regions
413 * @max_nr_reg: maximum number of regions
415 * This function should not be called while the kdamond is running.
416 * Every time interval is in micro-seconds.
418 * Return: 0 on success, negative error code otherwise.
420 int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
421 unsigned long aggr_int, unsigned long ops_upd_int,
422 unsigned long min_nr_reg, unsigned long max_nr_reg)
426 if (min_nr_reg > max_nr_reg)
429 ctx->sample_interval = sample_int;
430 ctx->aggr_interval = aggr_int;
431 ctx->ops_update_interval = ops_upd_int;
432 ctx->min_nr_regions = min_nr_reg;
433 ctx->max_nr_regions = max_nr_reg;
439 * damon_set_schemes() - Set data access monitoring based operation schemes.
440 * @ctx: monitoring context
441 * @schemes: array of the schemes
442 * @nr_schemes: number of entries in @schemes
444 * This function should not be called while the kdamond of the context is
447 * Return: 0 if success, or negative error code otherwise.
449 int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes,
452 struct damos *s, *next;
455 damon_for_each_scheme_safe(s, next, ctx)
456 damon_destroy_scheme(s);
457 for (i = 0; i < nr_schemes; i++)
458 damon_add_scheme(ctx, schemes[i]);
463 * damon_nr_running_ctxs() - Return number of currently running contexts.
465 int damon_nr_running_ctxs(void)
469 mutex_lock(&damon_lock);
470 nr_ctxs = nr_running_ctxs;
471 mutex_unlock(&damon_lock);
476 /* Returns the size upper limit for each monitoring region */
477 static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
479 struct damon_target *t;
480 struct damon_region *r;
481 unsigned long sz = 0;
483 damon_for_each_target(t, ctx) {
484 damon_for_each_region(r, t)
485 sz += r->ar.end - r->ar.start;
488 if (ctx->min_nr_regions)
489 sz /= ctx->min_nr_regions;
490 if (sz < DAMON_MIN_REGION)
491 sz = DAMON_MIN_REGION;
496 static int kdamond_fn(void *data);
499 * __damon_start() - Starts monitoring with given context.
500 * @ctx: monitoring context
502 * This function should be called while damon_lock is hold.
504 * Return: 0 on success, negative error code otherwise.
506 static int __damon_start(struct damon_ctx *ctx)
510 mutex_lock(&ctx->kdamond_lock);
513 ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d",
515 if (IS_ERR(ctx->kdamond)) {
516 err = PTR_ERR(ctx->kdamond);
520 mutex_unlock(&ctx->kdamond_lock);
526 * damon_start() - Starts the monitorings for a given group of contexts.
527 * @ctxs: an array of the pointers for contexts to start monitoring
528 * @nr_ctxs: size of @ctxs
529 * @exclusive: exclusiveness of this contexts group
531 * This function starts a group of monitoring threads for a group of monitoring
532 * contexts. One thread per each context is created and run in parallel. The
533 * caller should handle synchronization between the threads by itself. If
534 * @exclusive is true and a group of threads that created by other
535 * 'damon_start()' call is currently running, this function does nothing but
538 * Return: 0 on success, negative error code otherwise.
540 int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive)
545 mutex_lock(&damon_lock);
546 if ((exclusive && nr_running_ctxs) ||
547 (!exclusive && running_exclusive_ctxs)) {
548 mutex_unlock(&damon_lock);
552 for (i = 0; i < nr_ctxs; i++) {
553 err = __damon_start(ctxs[i]);
558 if (exclusive && nr_running_ctxs)
559 running_exclusive_ctxs = true;
560 mutex_unlock(&damon_lock);
566 * __damon_stop() - Stops monitoring of a given context.
567 * @ctx: monitoring context
569 * Return: 0 on success, negative error code otherwise.
571 static int __damon_stop(struct damon_ctx *ctx)
573 struct task_struct *tsk;
575 mutex_lock(&ctx->kdamond_lock);
578 get_task_struct(tsk);
579 mutex_unlock(&ctx->kdamond_lock);
581 put_task_struct(tsk);
584 mutex_unlock(&ctx->kdamond_lock);
590 * damon_stop() - Stops the monitorings for a given group of contexts.
591 * @ctxs: an array of the pointers for contexts to stop monitoring
592 * @nr_ctxs: size of @ctxs
594 * Return: 0 on success, negative error code otherwise.
596 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
600 for (i = 0; i < nr_ctxs; i++) {
601 /* nr_running_ctxs is decremented in kdamond_fn */
602 err = __damon_stop(ctxs[i]);
610 * damon_check_reset_time_interval() - Check if a time interval is elapsed.
611 * @baseline: the time to check whether the interval has elapsed since
612 * @interval: the time interval (microseconds)
614 * See whether the given time interval has passed since the given baseline
615 * time. If so, it also updates the baseline to current time for next check.
617 * Return: true if the time interval has passed, or false otherwise.
619 static bool damon_check_reset_time_interval(struct timespec64 *baseline,
620 unsigned long interval)
622 struct timespec64 now;
624 ktime_get_coarse_ts64(&now);
625 if ((timespec64_to_ns(&now) - timespec64_to_ns(baseline)) <
633 * Check whether it is time to flush the aggregated information
635 static bool kdamond_aggregate_interval_passed(struct damon_ctx *ctx)
637 return damon_check_reset_time_interval(&ctx->last_aggregation,
642 * Reset the aggregated monitoring results ('nr_accesses' of each region).
644 static void kdamond_reset_aggregated(struct damon_ctx *c)
646 struct damon_target *t;
647 unsigned int ti = 0; /* target's index */
649 damon_for_each_target(t, c) {
650 struct damon_region *r;
652 damon_for_each_region(r, t) {
653 trace_damon_aggregated(t, ti, r, damon_nr_regions(t));
654 r->last_nr_accesses = r->nr_accesses;
661 static void damon_split_region_at(struct damon_ctx *ctx,
662 struct damon_target *t, struct damon_region *r,
665 static bool __damos_valid_target(struct damon_region *r, struct damos *s)
669 sz = r->ar.end - r->ar.start;
670 return s->min_sz_region <= sz && sz <= s->max_sz_region &&
671 s->min_nr_accesses <= r->nr_accesses &&
672 r->nr_accesses <= s->max_nr_accesses &&
673 s->min_age_region <= r->age && r->age <= s->max_age_region;
676 static bool damos_valid_target(struct damon_ctx *c, struct damon_target *t,
677 struct damon_region *r, struct damos *s)
679 bool ret = __damos_valid_target(r, s);
681 if (!ret || !s->quota.esz || !c->ops.get_scheme_score)
684 return c->ops.get_scheme_score(c, t, r, s) >= s->quota.min_score;
687 static void damon_do_apply_schemes(struct damon_ctx *c,
688 struct damon_target *t,
689 struct damon_region *r)
693 damon_for_each_scheme(s, c) {
694 struct damos_quota *quota = &s->quota;
695 unsigned long sz = r->ar.end - r->ar.start;
696 struct timespec64 begin, end;
697 unsigned long sz_applied = 0;
699 if (!s->wmarks.activated)
702 /* Check the quota */
703 if (quota->esz && quota->charged_sz >= quota->esz)
706 /* Skip previously charged regions */
707 if (quota->charge_target_from) {
708 if (t != quota->charge_target_from)
710 if (r == damon_last_region(t)) {
711 quota->charge_target_from = NULL;
712 quota->charge_addr_from = 0;
715 if (quota->charge_addr_from &&
716 r->ar.end <= quota->charge_addr_from)
719 if (quota->charge_addr_from && r->ar.start <
720 quota->charge_addr_from) {
721 sz = ALIGN_DOWN(quota->charge_addr_from -
722 r->ar.start, DAMON_MIN_REGION);
724 if (r->ar.end - r->ar.start <=
727 sz = DAMON_MIN_REGION;
729 damon_split_region_at(c, t, r, sz);
730 r = damon_next_region(r);
731 sz = r->ar.end - r->ar.start;
733 quota->charge_target_from = NULL;
734 quota->charge_addr_from = 0;
737 if (!damos_valid_target(c, t, r, s))
740 /* Apply the scheme */
741 if (c->ops.apply_scheme) {
743 quota->charged_sz + sz > quota->esz) {
744 sz = ALIGN_DOWN(quota->esz - quota->charged_sz,
748 damon_split_region_at(c, t, r, sz);
750 ktime_get_coarse_ts64(&begin);
751 sz_applied = c->ops.apply_scheme(c, t, r, s);
752 ktime_get_coarse_ts64(&end);
753 quota->total_charged_ns += timespec64_to_ns(&end) -
754 timespec64_to_ns(&begin);
755 quota->charged_sz += sz;
756 if (quota->esz && quota->charged_sz >= quota->esz) {
757 quota->charge_target_from = t;
758 quota->charge_addr_from = r->ar.end + 1;
761 if (s->action != DAMOS_STAT)
766 s->stat.sz_tried += sz;
768 s->stat.nr_applied++;
769 s->stat.sz_applied += sz_applied;
773 /* Shouldn't be called if quota->ms and quota->sz are zero */
774 static void damos_set_effective_quota(struct damos_quota *quota)
776 unsigned long throughput;
780 quota->esz = quota->sz;
784 if (quota->total_charged_ns)
785 throughput = quota->total_charged_sz * 1000000 /
786 quota->total_charged_ns;
788 throughput = PAGE_SIZE * 1024;
789 esz = throughput * quota->ms;
791 if (quota->sz && quota->sz < esz)
796 static void kdamond_apply_schemes(struct damon_ctx *c)
798 struct damon_target *t;
799 struct damon_region *r, *next_r;
802 damon_for_each_scheme(s, c) {
803 struct damos_quota *quota = &s->quota;
804 unsigned long cumulated_sz;
805 unsigned int score, max_score = 0;
807 if (!s->wmarks.activated)
810 if (!quota->ms && !quota->sz)
813 /* New charge window starts */
814 if (time_after_eq(jiffies, quota->charged_from +
816 quota->reset_interval))) {
817 if (quota->esz && quota->charged_sz >= quota->esz)
818 s->stat.qt_exceeds++;
819 quota->total_charged_sz += quota->charged_sz;
820 quota->charged_from = jiffies;
821 quota->charged_sz = 0;
822 damos_set_effective_quota(quota);
825 if (!c->ops.get_scheme_score)
828 /* Fill up the score histogram */
829 memset(quota->histogram, 0, sizeof(quota->histogram));
830 damon_for_each_target(t, c) {
831 damon_for_each_region(r, t) {
832 if (!__damos_valid_target(r, s))
834 score = c->ops.get_scheme_score(
836 quota->histogram[score] +=
837 r->ar.end - r->ar.start;
838 if (score > max_score)
843 /* Set the min score limit */
844 for (cumulated_sz = 0, score = max_score; ; score--) {
845 cumulated_sz += quota->histogram[score];
846 if (cumulated_sz >= quota->esz || !score)
849 quota->min_score = score;
852 damon_for_each_target(t, c) {
853 damon_for_each_region_safe(r, next_r, t)
854 damon_do_apply_schemes(c, t, r);
858 static inline unsigned long sz_damon_region(struct damon_region *r)
860 return r->ar.end - r->ar.start;
864 * Merge two adjacent regions into one region
866 static void damon_merge_two_regions(struct damon_target *t,
867 struct damon_region *l, struct damon_region *r)
869 unsigned long sz_l = sz_damon_region(l), sz_r = sz_damon_region(r);
871 l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) /
873 l->age = (l->age * sz_l + r->age * sz_r) / (sz_l + sz_r);
874 l->ar.end = r->ar.end;
875 damon_destroy_region(r, t);
879 * Merge adjacent regions having similar access frequencies
881 * t target affected by this merge operation
882 * thres '->nr_accesses' diff threshold for the merge
883 * sz_limit size upper limit of each region
885 static void damon_merge_regions_of(struct damon_target *t, unsigned int thres,
886 unsigned long sz_limit)
888 struct damon_region *r, *prev = NULL, *next;
890 damon_for_each_region_safe(r, next, t) {
891 if (abs(r->nr_accesses - r->last_nr_accesses) > thres)
896 if (prev && prev->ar.end == r->ar.start &&
897 abs(prev->nr_accesses - r->nr_accesses) <= thres &&
898 sz_damon_region(prev) + sz_damon_region(r) <= sz_limit)
899 damon_merge_two_regions(t, prev, r);
906 * Merge adjacent regions having similar access frequencies
908 * threshold '->nr_accesses' diff threshold for the merge
909 * sz_limit size upper limit of each region
911 * This function merges monitoring target regions which are adjacent and their
912 * access frequencies are similar. This is for minimizing the monitoring
913 * overhead under the dynamically changeable access pattern. If a merge was
914 * unnecessarily made, later 'kdamond_split_regions()' will revert it.
916 static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
917 unsigned long sz_limit)
919 struct damon_target *t;
921 damon_for_each_target(t, c)
922 damon_merge_regions_of(t, threshold, sz_limit);
926 * Split a region in two
928 * r the region to be split
929 * sz_r size of the first sub-region that will be made
931 static void damon_split_region_at(struct damon_ctx *ctx,
932 struct damon_target *t, struct damon_region *r,
935 struct damon_region *new;
937 new = damon_new_region(r->ar.start + sz_r, r->ar.end);
941 r->ar.end = new->ar.start;
944 new->last_nr_accesses = r->last_nr_accesses;
946 damon_insert_region(new, r, damon_next_region(r), t);
949 /* Split every region in the given target into 'nr_subs' regions */
950 static void damon_split_regions_of(struct damon_ctx *ctx,
951 struct damon_target *t, int nr_subs)
953 struct damon_region *r, *next;
954 unsigned long sz_region, sz_sub = 0;
957 damon_for_each_region_safe(r, next, t) {
958 sz_region = r->ar.end - r->ar.start;
960 for (i = 0; i < nr_subs - 1 &&
961 sz_region > 2 * DAMON_MIN_REGION; i++) {
963 * Randomly select size of left sub-region to be at
964 * least 10 percent and at most 90% of original region
966 sz_sub = ALIGN_DOWN(damon_rand(1, 10) *
967 sz_region / 10, DAMON_MIN_REGION);
968 /* Do not allow blank region */
969 if (sz_sub == 0 || sz_sub >= sz_region)
972 damon_split_region_at(ctx, t, r, sz_sub);
979 * Split every target region into randomly-sized small regions
981 * This function splits every target region into random-sized small regions if
982 * current total number of the regions is equal or smaller than half of the
983 * user-specified maximum number of regions. This is for maximizing the
984 * monitoring accuracy under the dynamically changeable access patterns. If a
985 * split was unnecessarily made, later 'kdamond_merge_regions()' will revert
988 static void kdamond_split_regions(struct damon_ctx *ctx)
990 struct damon_target *t;
991 unsigned int nr_regions = 0;
992 static unsigned int last_nr_regions;
993 int nr_subregions = 2;
995 damon_for_each_target(t, ctx)
996 nr_regions += damon_nr_regions(t);
998 if (nr_regions > ctx->max_nr_regions / 2)
1001 /* Maybe the middle of the region has different access frequency */
1002 if (last_nr_regions == nr_regions &&
1003 nr_regions < ctx->max_nr_regions / 3)
1006 damon_for_each_target(t, ctx)
1007 damon_split_regions_of(ctx, t, nr_subregions);
1009 last_nr_regions = nr_regions;
1013 * Check whether it is time to check and apply the operations-related data
1016 * Returns true if it is.
1018 static bool kdamond_need_update_operations(struct damon_ctx *ctx)
1020 return damon_check_reset_time_interval(&ctx->last_ops_update,
1021 ctx->ops_update_interval);
1025 * Check whether current monitoring should be stopped
1027 * The monitoring is stopped when either the user requested to stop, or all
1028 * monitoring targets are invalid.
1030 * Returns true if need to stop current monitoring.
1032 static bool kdamond_need_stop(struct damon_ctx *ctx)
1034 struct damon_target *t;
1036 if (kthread_should_stop())
1039 if (!ctx->ops.target_valid)
1042 damon_for_each_target(t, ctx) {
1043 if (ctx->ops.target_valid(t))
1050 static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
1055 case DAMOS_WMARK_FREE_MEM_RATE:
1057 return i.freeram * 1000 / i.totalram;
1065 * Returns zero if the scheme is active. Else, returns time to wait for next
1066 * watermark check in micro-seconds.
1068 static unsigned long damos_wmark_wait_us(struct damos *scheme)
1070 unsigned long metric;
1072 if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
1075 metric = damos_wmark_metric_value(scheme->wmarks.metric);
1076 /* higher than high watermark or lower than low watermark */
1077 if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
1078 if (scheme->wmarks.activated)
1079 pr_debug("deactivate a scheme (%d) for %s wmark\n",
1081 metric > scheme->wmarks.high ?
1083 scheme->wmarks.activated = false;
1084 return scheme->wmarks.interval;
1087 /* inactive and higher than middle watermark */
1088 if ((scheme->wmarks.high >= metric && metric >= scheme->wmarks.mid) &&
1089 !scheme->wmarks.activated)
1090 return scheme->wmarks.interval;
1092 if (!scheme->wmarks.activated)
1093 pr_debug("activate a scheme (%d)\n", scheme->action);
1094 scheme->wmarks.activated = true;
1098 static void kdamond_usleep(unsigned long usecs)
1100 /* See Documentation/timers/timers-howto.rst for the thresholds */
1101 if (usecs > 20 * USEC_PER_MSEC)
1102 schedule_timeout_idle(usecs_to_jiffies(usecs));
1104 usleep_idle_range(usecs, usecs + 1);
1107 /* Returns negative error code if it's not activated but should return */
1108 static int kdamond_wait_activation(struct damon_ctx *ctx)
1111 unsigned long wait_time;
1112 unsigned long min_wait_time = 0;
1113 bool init_wait_time = false;
1115 while (!kdamond_need_stop(ctx)) {
1116 damon_for_each_scheme(s, ctx) {
1117 wait_time = damos_wmark_wait_us(s);
1118 if (!init_wait_time || wait_time < min_wait_time) {
1119 init_wait_time = true;
1120 min_wait_time = wait_time;
1126 kdamond_usleep(min_wait_time);
1128 if (ctx->callback.after_wmarks_check &&
1129 ctx->callback.after_wmarks_check(ctx))
1136 * The monitoring daemon that runs as a kernel thread
1138 static int kdamond_fn(void *data)
1140 struct damon_ctx *ctx = data;
1141 struct damon_target *t;
1142 struct damon_region *r, *next;
1143 unsigned int max_nr_accesses = 0;
1144 unsigned long sz_limit = 0;
1147 pr_debug("kdamond (%d) starts\n", current->pid);
1151 if (ctx->callback.before_start && ctx->callback.before_start(ctx))
1154 sz_limit = damon_region_sz_limit(ctx);
1156 while (!kdamond_need_stop(ctx) && !done) {
1157 if (kdamond_wait_activation(ctx)) {
1162 if (ctx->ops.prepare_access_checks)
1163 ctx->ops.prepare_access_checks(ctx);
1164 if (ctx->callback.after_sampling &&
1165 ctx->callback.after_sampling(ctx)) {
1170 kdamond_usleep(ctx->sample_interval);
1172 if (ctx->ops.check_accesses)
1173 max_nr_accesses = ctx->ops.check_accesses(ctx);
1175 if (kdamond_aggregate_interval_passed(ctx)) {
1176 kdamond_merge_regions(ctx,
1177 max_nr_accesses / 10,
1179 if (ctx->callback.after_aggregation &&
1180 ctx->callback.after_aggregation(ctx)) {
1184 kdamond_apply_schemes(ctx);
1185 kdamond_reset_aggregated(ctx);
1186 kdamond_split_regions(ctx);
1187 if (ctx->ops.reset_aggregated)
1188 ctx->ops.reset_aggregated(ctx);
1191 if (kdamond_need_update_operations(ctx)) {
1192 if (ctx->ops.update)
1193 ctx->ops.update(ctx);
1194 sz_limit = damon_region_sz_limit(ctx);
1197 damon_for_each_target(t, ctx) {
1198 damon_for_each_region_safe(r, next, t)
1199 damon_destroy_region(r, t);
1202 if (ctx->callback.before_terminate)
1203 ctx->callback.before_terminate(ctx);
1204 if (ctx->ops.cleanup)
1205 ctx->ops.cleanup(ctx);
1207 pr_debug("kdamond (%d) finishes\n", current->pid);
1208 mutex_lock(&ctx->kdamond_lock);
1209 ctx->kdamond = NULL;
1210 mutex_unlock(&ctx->kdamond_lock);
1212 mutex_lock(&damon_lock);
1214 if (!nr_running_ctxs && running_exclusive_ctxs)
1215 running_exclusive_ctxs = false;
1216 mutex_unlock(&damon_lock);
1221 #include "core-test.h"