1 // SPDX-License-Identifier: GPL-2.0
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 static struct kmem_cache *damon_region_cache __ro_after_init;
34 /* Should be called under damon_ops_lock with id smaller than NR_DAMON_OPS */
35 static bool __damon_is_registered_ops(enum damon_ops_id id)
37 struct damon_operations empty_ops = {};
39 if (!memcmp(&empty_ops, &damon_registered_ops[id], sizeof(empty_ops)))
45 * damon_is_registered_ops() - Check if a given damon_operations is registered.
46 * @id: Id of the damon_operations to check if registered.
48 * Return: true if the ops is set, false otherwise.
50 bool damon_is_registered_ops(enum damon_ops_id id)
54 if (id >= NR_DAMON_OPS)
56 mutex_lock(&damon_ops_lock);
57 registered = __damon_is_registered_ops(id);
58 mutex_unlock(&damon_ops_lock);
63 * damon_register_ops() - Register a monitoring operations set to DAMON.
64 * @ops: monitoring operations set to register.
66 * This function registers a monitoring operations set of valid &struct
67 * damon_operations->id so that others can find and use them later.
69 * Return: 0 on success, negative error code otherwise.
71 int damon_register_ops(struct damon_operations *ops)
75 if (ops->id >= NR_DAMON_OPS)
77 mutex_lock(&damon_ops_lock);
78 /* Fail for already registered ops */
79 if (__damon_is_registered_ops(ops->id)) {
83 damon_registered_ops[ops->id] = *ops;
85 mutex_unlock(&damon_ops_lock);
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.
94 * This function finds registered monitoring operations set of @id and make
97 * Return: 0 on success, negative error code otherwise.
99 int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id)
103 if (id >= NR_DAMON_OPS)
106 mutex_lock(&damon_ops_lock);
107 if (!__damon_is_registered_ops(id))
110 ctx->ops = damon_registered_ops[id];
111 mutex_unlock(&damon_ops_lock);
116 * Construct a damon_region struct
118 * Returns the pointer to the new struct if success, or NULL otherwise
120 struct damon_region *damon_new_region(unsigned long start, unsigned long end)
122 struct damon_region *region;
124 region = kmem_cache_alloc(damon_region_cache, GFP_KERNEL);
128 region->ar.start = start;
129 region->ar.end = end;
130 region->nr_accesses = 0;
131 region->nr_accesses_bp = 0;
132 INIT_LIST_HEAD(®ion->list);
135 region->last_nr_accesses = 0;
140 void damon_add_region(struct damon_region *r, struct damon_target *t)
142 list_add_tail(&r->list, &t->regions_list);
146 static void damon_del_region(struct damon_region *r, struct damon_target *t)
152 static void damon_free_region(struct damon_region *r)
154 kmem_cache_free(damon_region_cache, r);
157 void damon_destroy_region(struct damon_region *r, struct damon_target *t)
159 damon_del_region(r, t);
160 damon_free_region(r);
164 * Check whether a region is intersecting an address range
166 * Returns true if it is.
168 static bool damon_intersect(struct damon_region *r,
169 struct damon_addr_range *re)
171 return !(r->ar.end <= re->start || re->end <= r->ar.start);
175 * Fill holes in regions with new regions.
177 static int damon_fill_regions_holes(struct damon_region *first,
178 struct damon_region *last, struct damon_target *t)
180 struct damon_region *r = first;
182 damon_for_each_region_from(r, t) {
183 struct damon_region *next, *newr;
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);
192 damon_insert_region(newr, r, next, t);
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.
204 * This function adds new regions to, or modify existing regions of a
205 * monitoring target to fit in specific ranges.
207 * Return: 0 if success, or negative error code otherwise.
209 int damon_set_regions(struct damon_target *t, struct damon_addr_range *ranges,
210 unsigned int nr_ranges)
212 struct damon_region *r, *next;
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]))
223 damon_destroy_region(r, t);
226 r = damon_first_region(t);
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;
233 /* Get the first/last regions intersecting with the range */
234 damon_for_each_region_from(r, t) {
235 if (damon_intersect(r, range)) {
240 if (r->ar.start >= range->end)
244 /* no region intersects with this range */
245 newr = damon_new_region(
246 ALIGN_DOWN(range->start,
248 ALIGN(range->end, DAMON_MIN_REGION));
251 damon_insert_region(newr, damon_prev_region(r), r, t);
253 /* resize intersecting regions to fit in this range */
254 first->ar.start = ALIGN_DOWN(range->start,
256 last->ar.end = ALIGN(range->end, DAMON_MIN_REGION);
258 /* fill possible holes in the range */
259 err = damon_fill_regions_holes(first, last, t);
267 struct damos_filter *damos_new_filter(enum damos_filter_type type,
270 struct damos_filter *filter;
272 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
276 filter->matching = matching;
277 INIT_LIST_HEAD(&filter->list);
281 void damos_add_filter(struct damos *s, struct damos_filter *f)
283 list_add_tail(&f->list, &s->filters);
286 static void damos_del_filter(struct damos_filter *f)
291 static void damos_free_filter(struct damos_filter *f)
296 void damos_destroy_filter(struct damos_filter *f)
299 damos_free_filter(f);
302 /* initialize private fields of damos_quota and return the pointer */
303 static struct damos_quota *damos_quota_init_priv(struct damos_quota *quota)
305 quota->total_charged_sz = 0;
306 quota->total_charged_ns = 0;
308 quota->charged_sz = 0;
309 quota->charged_from = 0;
310 quota->charge_target_from = NULL;
311 quota->charge_addr_from = 0;
315 struct damos *damon_new_scheme(struct damos_access_pattern *pattern,
316 enum damos_action action,
317 unsigned long apply_interval_us,
318 struct damos_quota *quota,
319 struct damos_watermarks *wmarks)
321 struct damos *scheme;
323 scheme = kmalloc(sizeof(*scheme), GFP_KERNEL);
326 scheme->pattern = *pattern;
327 scheme->action = action;
328 scheme->apply_interval_us = apply_interval_us;
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.
334 scheme->next_apply_sis = 0;
335 INIT_LIST_HEAD(&scheme->filters);
336 scheme->stat = (struct damos_stat){};
337 INIT_LIST_HEAD(&scheme->list);
339 scheme->quota = *(damos_quota_init_priv(quota));
341 scheme->wmarks = *wmarks;
342 scheme->wmarks.activated = true;
347 static void damos_set_next_apply_sis(struct damos *s, struct damon_ctx *ctx)
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;
354 s->next_apply_sis = ctx->passed_sample_intervals +
355 apply_interval / sample_interval;
358 void damon_add_scheme(struct damon_ctx *ctx, struct damos *s)
360 list_add_tail(&s->list, &ctx->schemes);
361 damos_set_next_apply_sis(s, ctx);
364 static void damon_del_scheme(struct damos *s)
369 static void damon_free_scheme(struct damos *s)
374 void damon_destroy_scheme(struct damos *s)
376 struct damos_filter *f, *next;
378 damos_for_each_filter_safe(f, next, s)
379 damos_destroy_filter(f);
381 damon_free_scheme(s);
385 * Construct a damon_target struct
387 * Returns the pointer to the new struct if success, or NULL otherwise
389 struct damon_target *damon_new_target(void)
391 struct damon_target *t;
393 t = kmalloc(sizeof(*t), GFP_KERNEL);
399 INIT_LIST_HEAD(&t->regions_list);
400 INIT_LIST_HEAD(&t->list);
405 void damon_add_target(struct damon_ctx *ctx, struct damon_target *t)
407 list_add_tail(&t->list, &ctx->adaptive_targets);
410 bool damon_targets_empty(struct damon_ctx *ctx)
412 return list_empty(&ctx->adaptive_targets);
415 static void damon_del_target(struct damon_target *t)
420 void damon_free_target(struct damon_target *t)
422 struct damon_region *r, *next;
424 damon_for_each_region_safe(r, next, t)
425 damon_free_region(r);
429 void damon_destroy_target(struct damon_target *t)
432 damon_free_target(t);
435 unsigned int damon_nr_regions(struct damon_target *t)
437 return t->nr_regions;
440 struct damon_ctx *damon_new_ctx(void)
442 struct damon_ctx *ctx;
444 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
448 init_completion(&ctx->kdamond_started);
450 ctx->attrs.sample_interval = 5 * 1000;
451 ctx->attrs.aggr_interval = 100 * 1000;
452 ctx->attrs.ops_update_interval = 60 * 1000 * 1000;
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;
459 mutex_init(&ctx->kdamond_lock);
461 ctx->attrs.min_nr_regions = 10;
462 ctx->attrs.max_nr_regions = 1000;
464 INIT_LIST_HEAD(&ctx->adaptive_targets);
465 INIT_LIST_HEAD(&ctx->schemes);
470 static void damon_destroy_targets(struct damon_ctx *ctx)
472 struct damon_target *t, *next_t;
474 if (ctx->ops.cleanup) {
475 ctx->ops.cleanup(ctx);
479 damon_for_each_target_safe(t, next_t, ctx)
480 damon_destroy_target(t);
483 void damon_destroy_ctx(struct damon_ctx *ctx)
485 struct damos *s, *next_s;
487 damon_destroy_targets(ctx);
489 damon_for_each_scheme_safe(s, next_s, ctx)
490 damon_destroy_scheme(s);
495 static unsigned int damon_age_for_new_attrs(unsigned int age,
496 struct damon_attrs *old_attrs, struct damon_attrs *new_attrs)
498 return age * old_attrs->aggr_interval / new_attrs->aggr_interval;
501 /* convert access ratio in bp (per 10,000) to nr_accesses */
502 static unsigned int damon_accesses_bp_to_nr_accesses(
503 unsigned int accesses_bp, struct damon_attrs *attrs)
505 return accesses_bp * damon_max_nr_accesses(attrs) / 10000;
508 /* convert nr_accesses to access ratio in bp (per 10,000) */
509 static unsigned int damon_nr_accesses_to_accesses_bp(
510 unsigned int nr_accesses, struct damon_attrs *attrs)
512 return nr_accesses * 10000 / damon_max_nr_accesses(attrs);
515 static unsigned int damon_nr_accesses_for_new_attrs(unsigned int nr_accesses,
516 struct damon_attrs *old_attrs, struct damon_attrs *new_attrs)
518 return damon_accesses_bp_to_nr_accesses(
519 damon_nr_accesses_to_accesses_bp(
520 nr_accesses, old_attrs),
524 static void damon_update_monitoring_result(struct damon_region *r,
525 struct damon_attrs *old_attrs, struct damon_attrs *new_attrs)
527 r->nr_accesses = damon_nr_accesses_for_new_attrs(r->nr_accesses,
528 old_attrs, new_attrs);
529 r->nr_accesses_bp = r->nr_accesses * 10000;
530 r->age = damon_age_for_new_attrs(r->age, old_attrs, new_attrs);
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.
541 static void damon_update_monitoring_results(struct damon_ctx *ctx,
542 struct damon_attrs *new_attrs)
544 struct damon_attrs *old_attrs = &ctx->attrs;
545 struct damon_target *t;
546 struct damon_region *r;
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)
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);
561 * damon_set_attrs() - Set attributes for the monitoring.
562 * @ctx: monitoring context
563 * @attrs: monitoring attributes
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).
570 * Every time interval is in micro-seconds.
572 * Return: 0 on success, negative error code otherwise.
574 int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)
576 unsigned long sample_interval = attrs->sample_interval ?
577 attrs->sample_interval : 1;
580 if (attrs->min_nr_regions < 3)
582 if (attrs->min_nr_regions > attrs->max_nr_regions)
584 if (attrs->sample_interval > attrs->aggr_interval)
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;
592 damon_update_monitoring_results(ctx, attrs);
595 damon_for_each_scheme(s, ctx)
596 damos_set_next_apply_sis(s, ctx);
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
607 * This function should not be called while the kdamond of the context is
610 void damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes,
613 struct damos *s, *next;
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]);
623 * damon_nr_running_ctxs() - Return number of currently running contexts.
625 int damon_nr_running_ctxs(void)
629 mutex_lock(&damon_lock);
630 nr_ctxs = nr_running_ctxs;
631 mutex_unlock(&damon_lock);
636 /* Returns the size upper limit for each monitoring region */
637 static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
639 struct damon_target *t;
640 struct damon_region *r;
641 unsigned long sz = 0;
643 damon_for_each_target(t, ctx) {
644 damon_for_each_region(r, t)
645 sz += damon_sz_region(r);
648 if (ctx->attrs.min_nr_regions)
649 sz /= ctx->attrs.min_nr_regions;
650 if (sz < DAMON_MIN_REGION)
651 sz = DAMON_MIN_REGION;
656 static int kdamond_fn(void *data);
659 * __damon_start() - Starts monitoring with given context.
660 * @ctx: monitoring context
662 * This function should be called while damon_lock is hold.
664 * Return: 0 on success, negative error code otherwise.
666 static int __damon_start(struct damon_ctx *ctx)
670 mutex_lock(&ctx->kdamond_lock);
673 reinit_completion(&ctx->kdamond_started);
674 ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d",
676 if (IS_ERR(ctx->kdamond)) {
677 err = PTR_ERR(ctx->kdamond);
680 wait_for_completion(&ctx->kdamond_started);
683 mutex_unlock(&ctx->kdamond_lock);
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
692 * @exclusive: exclusiveness of this contexts group
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
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
701 * Return: 0 on success, negative error code otherwise.
703 int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive)
708 mutex_lock(&damon_lock);
709 if ((exclusive && nr_running_ctxs) ||
710 (!exclusive && running_exclusive_ctxs)) {
711 mutex_unlock(&damon_lock);
715 for (i = 0; i < nr_ctxs; i++) {
716 err = __damon_start(ctxs[i]);
721 if (exclusive && nr_running_ctxs)
722 running_exclusive_ctxs = true;
723 mutex_unlock(&damon_lock);
729 * __damon_stop() - Stops monitoring of a given context.
730 * @ctx: monitoring context
732 * Return: 0 on success, negative error code otherwise.
734 static int __damon_stop(struct damon_ctx *ctx)
736 struct task_struct *tsk;
738 mutex_lock(&ctx->kdamond_lock);
741 get_task_struct(tsk);
742 mutex_unlock(&ctx->kdamond_lock);
743 kthread_stop_put(tsk);
746 mutex_unlock(&ctx->kdamond_lock);
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
756 * Return: 0 on success, negative error code otherwise.
758 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
762 for (i = 0; i < nr_ctxs; i++) {
763 /* nr_running_ctxs is decremented in kdamond_fn */
764 err = __damon_stop(ctxs[i]);
772 * Reset the aggregated monitoring results ('nr_accesses' of each region).
774 static void kdamond_reset_aggregated(struct damon_ctx *c)
776 struct damon_target *t;
777 unsigned int ti = 0; /* target's index */
779 damon_for_each_target(t, c) {
780 struct damon_region *r;
782 damon_for_each_region(r, t) {
783 trace_damon_aggregated(ti, r, damon_nr_regions(t));
784 r->last_nr_accesses = r->nr_accesses;
791 static void damon_split_region_at(struct damon_target *t,
792 struct damon_region *r, unsigned long sz_r);
794 static bool __damos_valid_target(struct damon_region *r, struct damos *s)
797 unsigned int nr_accesses = r->nr_accesses_bp / 10000;
799 sz = damon_sz_region(r);
800 return s->pattern.min_sz_region <= sz &&
801 sz <= s->pattern.max_sz_region &&
802 s->pattern.min_nr_accesses <= nr_accesses &&
803 nr_accesses <= s->pattern.max_nr_accesses &&
804 s->pattern.min_age_region <= r->age &&
805 r->age <= s->pattern.max_age_region;
808 static bool damos_valid_target(struct damon_ctx *c, struct damon_target *t,
809 struct damon_region *r, struct damos *s)
811 bool ret = __damos_valid_target(r, s);
813 if (!ret || !s->quota.esz || !c->ops.get_scheme_score)
816 return c->ops.get_scheme_score(c, t, r, s) >= s->quota.min_score;
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.
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.
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
839 * Return: true if the region should be entirely skipped, false otherwise.
841 static bool damos_skip_charged_region(struct damon_target *t,
842 struct damon_region **rp, struct damos *s)
844 struct damon_region *r = *rp;
845 struct damos_quota *quota = &s->quota;
846 unsigned long sz_to_skip;
848 /* Skip previously charged regions */
849 if (quota->charge_target_from) {
850 if (t != quota->charge_target_from)
852 if (r == damon_last_region(t)) {
853 quota->charge_target_from = NULL;
854 quota->charge_addr_from = 0;
857 if (quota->charge_addr_from &&
858 r->ar.end <= quota->charge_addr_from)
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);
866 if (damon_sz_region(r) <= DAMON_MIN_REGION)
868 sz_to_skip = DAMON_MIN_REGION;
870 damon_split_region_at(t, r, sz_to_skip);
871 r = damon_next_region(r);
874 quota->charge_target_from = NULL;
875 quota->charge_addr_from = 0;
880 static void damos_update_stat(struct damos *s,
881 unsigned long sz_tried, unsigned long sz_applied)
884 s->stat.sz_tried += sz_tried;
886 s->stat.nr_applied++;
887 s->stat.sz_applied += sz_applied;
890 static bool __damos_filter_out(struct damon_ctx *ctx, struct damon_target *t,
891 struct damon_region *r, struct damos_filter *filter)
893 bool matched = false;
894 struct damon_target *ti;
896 unsigned long start, end;
898 switch (filter->type) {
899 case DAMOS_FILTER_TYPE_TARGET:
900 damon_for_each_target(ti, ctx) {
905 matched = target_idx == filter->target_idx;
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);
911 /* inside the range */
912 if (start <= r->ar.start && r->ar.end <= end) {
916 /* outside of the range */
917 if (r->ar.end <= start || end <= r->ar.start) {
921 /* start before the range and overlap */
922 if (r->ar.start < start) {
923 damon_split_region_at(t, r, start - r->ar.start);
927 /* start inside the range */
928 damon_split_region_at(t, r, end - r->ar.start);
935 return matched == filter->matching;
938 static bool damos_filter_out(struct damon_ctx *ctx, struct damon_target *t,
939 struct damon_region *r, struct damos *s)
941 struct damos_filter *filter;
943 damos_for_each_filter(filter, s) {
944 if (__damos_filter_out(ctx, t, r, filter))
950 static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t,
951 struct damon_region *r, struct damos *s)
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;
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
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;
971 /* get indices for trace_damos_before_apply() */
972 if (trace_damos_before_apply_enabled()) {
973 damon_for_each_scheme(siter, c) {
978 damon_for_each_target(titer, c) {
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,
992 damon_split_region_at(t, r, sz);
994 if (damos_filter_out(c, t, r, s))
996 ktime_get_coarse_ts64(&begin);
997 if (c->callback.before_damos_apply)
998 err = c->callback.before_damos_apply(c, t, r, s);
1000 trace_damos_before_apply(cidx, sidx, tidx, r,
1001 damon_nr_regions(t), do_trace);
1002 sz_applied = c->ops.apply_scheme(c, t, r, s);
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;
1013 if (s->action != DAMOS_STAT)
1017 damos_update_stat(s, sz, sz_applied);
1020 static void damon_do_apply_schemes(struct damon_ctx *c,
1021 struct damon_target *t,
1022 struct damon_region *r)
1026 damon_for_each_scheme(s, c) {
1027 struct damos_quota *quota = &s->quota;
1029 if (!s->wmarks.activated)
1032 /* Check the quota */
1033 if (quota->esz && quota->charged_sz >= quota->esz)
1036 if (damos_skip_charged_region(t, &r, s))
1039 if (!damos_valid_target(c, t, r, s))
1042 damos_apply_scheme(c, t, r, s);
1047 * damon_feed_loop_next_input() - get next input to achieve a target score.
1048 * @last_input The last input.
1049 * @score Current score that made with @last_input.
1051 * Calculate next input to achieve the target score, based on the last input
1052 * and current score. Assuming the input and the score are positively
1053 * proportional, calculate how much compensation should be added to or
1054 * subtracted from the last input as a proportion of the last input. Avoid
1055 * next input always being zero by setting it non-zero always. In short form
1056 * (assuming support of float and signed calculations), the algorithm is as
1059 * next_input = max(last_input * ((goal - current) / goal + 1), 1)
1061 * For simple implementation, we assume the target score is always 10,000. The
1062 * caller should adjust @score for this.
1064 * Returns next input that assumed to achieve the target score.
1066 static unsigned long damon_feed_loop_next_input(unsigned long last_input,
1067 unsigned long score)
1069 const unsigned long goal = 10000;
1070 unsigned long score_goal_diff = max(goal, score) - min(goal, score);
1071 unsigned long score_goal_diff_bp = score_goal_diff * 10000 / goal;
1072 unsigned long compensation = last_input * score_goal_diff_bp / 10000;
1073 /* Set minimum input as 10000 to avoid compensation be zero */
1074 const unsigned long min_input = 10000;
1077 return last_input + compensation;
1078 if (last_input > compensation + min_input)
1079 return last_input - compensation;
1083 /* Shouldn't be called if quota->ms, quota->sz, and quota->get_score unset */
1084 static void damos_set_effective_quota(struct damos_quota *quota)
1086 unsigned long throughput;
1089 if (!quota->ms && !quota->get_score) {
1090 quota->esz = quota->sz;
1094 if (quota->get_score) {
1095 quota->esz_bp = damon_feed_loop_next_input(
1096 max(quota->esz_bp, 10000UL),
1097 quota->get_score(quota->get_score_arg));
1098 esz = quota->esz_bp / 10000;
1102 if (quota->total_charged_ns)
1103 throughput = quota->total_charged_sz * 1000000 /
1104 quota->total_charged_ns;
1106 throughput = PAGE_SIZE * 1024;
1107 if (quota->get_score)
1108 esz = min(throughput * quota->ms, esz);
1110 esz = throughput * quota->ms;
1113 if (quota->sz && quota->sz < esz)
1119 static void damos_adjust_quota(struct damon_ctx *c, struct damos *s)
1121 struct damos_quota *quota = &s->quota;
1122 struct damon_target *t;
1123 struct damon_region *r;
1124 unsigned long cumulated_sz;
1125 unsigned int score, max_score = 0;
1127 if (!quota->ms && !quota->sz && !quota->get_score)
1130 /* New charge window starts */
1131 if (time_after_eq(jiffies, quota->charged_from +
1132 msecs_to_jiffies(quota->reset_interval))) {
1133 if (quota->esz && quota->charged_sz >= quota->esz)
1134 s->stat.qt_exceeds++;
1135 quota->total_charged_sz += quota->charged_sz;
1136 quota->charged_from = jiffies;
1137 quota->charged_sz = 0;
1138 damos_set_effective_quota(quota);
1141 if (!c->ops.get_scheme_score)
1144 /* Fill up the score histogram */
1145 memset(quota->histogram, 0, sizeof(quota->histogram));
1146 damon_for_each_target(t, c) {
1147 damon_for_each_region(r, t) {
1148 if (!__damos_valid_target(r, s))
1150 score = c->ops.get_scheme_score(c, t, r, s);
1151 quota->histogram[score] += damon_sz_region(r);
1152 if (score > max_score)
1157 /* Set the min score limit */
1158 for (cumulated_sz = 0, score = max_score; ; score--) {
1159 cumulated_sz += quota->histogram[score];
1160 if (cumulated_sz >= quota->esz || !score)
1163 quota->min_score = score;
1166 static void kdamond_apply_schemes(struct damon_ctx *c)
1168 struct damon_target *t;
1169 struct damon_region *r, *next_r;
1171 unsigned long sample_interval = c->attrs.sample_interval ?
1172 c->attrs.sample_interval : 1;
1173 bool has_schemes_to_apply = false;
1175 damon_for_each_scheme(s, c) {
1176 if (c->passed_sample_intervals != s->next_apply_sis)
1179 s->next_apply_sis +=
1180 (s->apply_interval_us ? s->apply_interval_us :
1181 c->attrs.aggr_interval) / sample_interval;
1183 if (!s->wmarks.activated)
1186 has_schemes_to_apply = true;
1188 damos_adjust_quota(c, s);
1191 if (!has_schemes_to_apply)
1194 damon_for_each_target(t, c) {
1195 damon_for_each_region_safe(r, next_r, t)
1196 damon_do_apply_schemes(c, t, r);
1201 * Merge two adjacent regions into one region
1203 static void damon_merge_two_regions(struct damon_target *t,
1204 struct damon_region *l, struct damon_region *r)
1206 unsigned long sz_l = damon_sz_region(l), sz_r = damon_sz_region(r);
1208 l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) /
1210 l->nr_accesses_bp = l->nr_accesses * 10000;
1211 l->age = (l->age * sz_l + r->age * sz_r) / (sz_l + sz_r);
1212 l->ar.end = r->ar.end;
1213 damon_destroy_region(r, t);
1217 * Merge adjacent regions having similar access frequencies
1219 * t target affected by this merge operation
1220 * thres '->nr_accesses' diff threshold for the merge
1221 * sz_limit size upper limit of each region
1223 static void damon_merge_regions_of(struct damon_target *t, unsigned int thres,
1224 unsigned long sz_limit)
1226 struct damon_region *r, *prev = NULL, *next;
1228 damon_for_each_region_safe(r, next, t) {
1229 if (abs(r->nr_accesses - r->last_nr_accesses) > thres)
1234 if (prev && prev->ar.end == r->ar.start &&
1235 abs(prev->nr_accesses - r->nr_accesses) <= thres &&
1236 damon_sz_region(prev) + damon_sz_region(r) <= sz_limit)
1237 damon_merge_two_regions(t, prev, r);
1244 * Merge adjacent regions having similar access frequencies
1246 * threshold '->nr_accesses' diff threshold for the merge
1247 * sz_limit size upper limit of each region
1249 * This function merges monitoring target regions which are adjacent and their
1250 * access frequencies are similar. This is for minimizing the monitoring
1251 * overhead under the dynamically changeable access pattern. If a merge was
1252 * unnecessarily made, later 'kdamond_split_regions()' will revert it.
1254 static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
1255 unsigned long sz_limit)
1257 struct damon_target *t;
1259 damon_for_each_target(t, c)
1260 damon_merge_regions_of(t, threshold, sz_limit);
1264 * Split a region in two
1266 * r the region to be split
1267 * sz_r size of the first sub-region that will be made
1269 static void damon_split_region_at(struct damon_target *t,
1270 struct damon_region *r, unsigned long sz_r)
1272 struct damon_region *new;
1274 new = damon_new_region(r->ar.start + sz_r, r->ar.end);
1278 r->ar.end = new->ar.start;
1281 new->last_nr_accesses = r->last_nr_accesses;
1282 new->nr_accesses_bp = r->nr_accesses_bp;
1283 new->nr_accesses = r->nr_accesses;
1285 damon_insert_region(new, r, damon_next_region(r), t);
1288 /* Split every region in the given target into 'nr_subs' regions */
1289 static void damon_split_regions_of(struct damon_target *t, int nr_subs)
1291 struct damon_region *r, *next;
1292 unsigned long sz_region, sz_sub = 0;
1295 damon_for_each_region_safe(r, next, t) {
1296 sz_region = damon_sz_region(r);
1298 for (i = 0; i < nr_subs - 1 &&
1299 sz_region > 2 * DAMON_MIN_REGION; i++) {
1301 * Randomly select size of left sub-region to be at
1302 * least 10 percent and at most 90% of original region
1304 sz_sub = ALIGN_DOWN(damon_rand(1, 10) *
1305 sz_region / 10, DAMON_MIN_REGION);
1306 /* Do not allow blank region */
1307 if (sz_sub == 0 || sz_sub >= sz_region)
1310 damon_split_region_at(t, r, sz_sub);
1317 * Split every target region into randomly-sized small regions
1319 * This function splits every target region into random-sized small regions if
1320 * current total number of the regions is equal or smaller than half of the
1321 * user-specified maximum number of regions. This is for maximizing the
1322 * monitoring accuracy under the dynamically changeable access patterns. If a
1323 * split was unnecessarily made, later 'kdamond_merge_regions()' will revert
1326 static void kdamond_split_regions(struct damon_ctx *ctx)
1328 struct damon_target *t;
1329 unsigned int nr_regions = 0;
1330 static unsigned int last_nr_regions;
1331 int nr_subregions = 2;
1333 damon_for_each_target(t, ctx)
1334 nr_regions += damon_nr_regions(t);
1336 if (nr_regions > ctx->attrs.max_nr_regions / 2)
1339 /* Maybe the middle of the region has different access frequency */
1340 if (last_nr_regions == nr_regions &&
1341 nr_regions < ctx->attrs.max_nr_regions / 3)
1344 damon_for_each_target(t, ctx)
1345 damon_split_regions_of(t, nr_subregions);
1347 last_nr_regions = nr_regions;
1351 * Check whether current monitoring should be stopped
1353 * The monitoring is stopped when either the user requested to stop, or all
1354 * monitoring targets are invalid.
1356 * Returns true if need to stop current monitoring.
1358 static bool kdamond_need_stop(struct damon_ctx *ctx)
1360 struct damon_target *t;
1362 if (kthread_should_stop())
1365 if (!ctx->ops.target_valid)
1368 damon_for_each_target(t, ctx) {
1369 if (ctx->ops.target_valid(t))
1376 static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
1379 case DAMOS_WMARK_FREE_MEM_RATE:
1380 return global_zone_page_state(NR_FREE_PAGES) * 1000 /
1389 * Returns zero if the scheme is active. Else, returns time to wait for next
1390 * watermark check in micro-seconds.
1392 static unsigned long damos_wmark_wait_us(struct damos *scheme)
1394 unsigned long metric;
1396 if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
1399 metric = damos_wmark_metric_value(scheme->wmarks.metric);
1400 /* higher than high watermark or lower than low watermark */
1401 if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
1402 if (scheme->wmarks.activated)
1403 pr_debug("deactivate a scheme (%d) for %s wmark\n",
1405 metric > scheme->wmarks.high ?
1407 scheme->wmarks.activated = false;
1408 return scheme->wmarks.interval;
1411 /* inactive and higher than middle watermark */
1412 if ((scheme->wmarks.high >= metric && metric >= scheme->wmarks.mid) &&
1413 !scheme->wmarks.activated)
1414 return scheme->wmarks.interval;
1416 if (!scheme->wmarks.activated)
1417 pr_debug("activate a scheme (%d)\n", scheme->action);
1418 scheme->wmarks.activated = true;
1422 static void kdamond_usleep(unsigned long usecs)
1424 /* See Documentation/timers/timers-howto.rst for the thresholds */
1425 if (usecs > 20 * USEC_PER_MSEC)
1426 schedule_timeout_idle(usecs_to_jiffies(usecs));
1428 usleep_idle_range(usecs, usecs + 1);
1431 /* Returns negative error code if it's not activated but should return */
1432 static int kdamond_wait_activation(struct damon_ctx *ctx)
1435 unsigned long wait_time;
1436 unsigned long min_wait_time = 0;
1437 bool init_wait_time = false;
1439 while (!kdamond_need_stop(ctx)) {
1440 damon_for_each_scheme(s, ctx) {
1441 wait_time = damos_wmark_wait_us(s);
1442 if (!init_wait_time || wait_time < min_wait_time) {
1443 init_wait_time = true;
1444 min_wait_time = wait_time;
1450 kdamond_usleep(min_wait_time);
1452 if (ctx->callback.after_wmarks_check &&
1453 ctx->callback.after_wmarks_check(ctx))
1459 static void kdamond_init_intervals_sis(struct damon_ctx *ctx)
1461 unsigned long sample_interval = ctx->attrs.sample_interval ?
1462 ctx->attrs.sample_interval : 1;
1463 unsigned long apply_interval;
1464 struct damos *scheme;
1466 ctx->passed_sample_intervals = 0;
1467 ctx->next_aggregation_sis = ctx->attrs.aggr_interval / sample_interval;
1468 ctx->next_ops_update_sis = ctx->attrs.ops_update_interval /
1471 damon_for_each_scheme(scheme, ctx) {
1472 apply_interval = scheme->apply_interval_us ?
1473 scheme->apply_interval_us : ctx->attrs.aggr_interval;
1474 scheme->next_apply_sis = apply_interval / sample_interval;
1479 * The monitoring daemon that runs as a kernel thread
1481 static int kdamond_fn(void *data)
1483 struct damon_ctx *ctx = data;
1484 struct damon_target *t;
1485 struct damon_region *r, *next;
1486 unsigned int max_nr_accesses = 0;
1487 unsigned long sz_limit = 0;
1489 pr_debug("kdamond (%d) starts\n", current->pid);
1491 complete(&ctx->kdamond_started);
1492 kdamond_init_intervals_sis(ctx);
1496 if (ctx->callback.before_start && ctx->callback.before_start(ctx))
1499 sz_limit = damon_region_sz_limit(ctx);
1501 while (!kdamond_need_stop(ctx)) {
1503 * ctx->attrs and ctx->next_{aggregation,ops_update}_sis could
1504 * be changed from after_wmarks_check() or after_aggregation()
1505 * callbacks. Read the values here, and use those for this
1506 * iteration. That is, damon_set_attrs() updated new values
1507 * are respected from next iteration.
1509 unsigned long next_aggregation_sis = ctx->next_aggregation_sis;
1510 unsigned long next_ops_update_sis = ctx->next_ops_update_sis;
1511 unsigned long sample_interval = ctx->attrs.sample_interval;
1513 if (kdamond_wait_activation(ctx))
1516 if (ctx->ops.prepare_access_checks)
1517 ctx->ops.prepare_access_checks(ctx);
1518 if (ctx->callback.after_sampling &&
1519 ctx->callback.after_sampling(ctx))
1522 kdamond_usleep(sample_interval);
1523 ctx->passed_sample_intervals++;
1525 if (ctx->ops.check_accesses)
1526 max_nr_accesses = ctx->ops.check_accesses(ctx);
1528 if (ctx->passed_sample_intervals == next_aggregation_sis) {
1529 kdamond_merge_regions(ctx,
1530 max_nr_accesses / 10,
1532 if (ctx->callback.after_aggregation &&
1533 ctx->callback.after_aggregation(ctx))
1538 * do kdamond_apply_schemes() after kdamond_merge_regions() if
1539 * possible, to reduce overhead
1541 if (!list_empty(&ctx->schemes))
1542 kdamond_apply_schemes(ctx);
1544 sample_interval = ctx->attrs.sample_interval ?
1545 ctx->attrs.sample_interval : 1;
1546 if (ctx->passed_sample_intervals == next_aggregation_sis) {
1547 ctx->next_aggregation_sis = next_aggregation_sis +
1548 ctx->attrs.aggr_interval / sample_interval;
1550 kdamond_reset_aggregated(ctx);
1551 kdamond_split_regions(ctx);
1552 if (ctx->ops.reset_aggregated)
1553 ctx->ops.reset_aggregated(ctx);
1556 if (ctx->passed_sample_intervals == next_ops_update_sis) {
1557 ctx->next_ops_update_sis = next_ops_update_sis +
1558 ctx->attrs.ops_update_interval /
1560 if (ctx->ops.update)
1561 ctx->ops.update(ctx);
1562 sz_limit = damon_region_sz_limit(ctx);
1566 damon_for_each_target(t, ctx) {
1567 damon_for_each_region_safe(r, next, t)
1568 damon_destroy_region(r, t);
1571 if (ctx->callback.before_terminate)
1572 ctx->callback.before_terminate(ctx);
1573 if (ctx->ops.cleanup)
1574 ctx->ops.cleanup(ctx);
1576 pr_debug("kdamond (%d) finishes\n", current->pid);
1577 mutex_lock(&ctx->kdamond_lock);
1578 ctx->kdamond = NULL;
1579 mutex_unlock(&ctx->kdamond_lock);
1581 mutex_lock(&damon_lock);
1583 if (!nr_running_ctxs && running_exclusive_ctxs)
1584 running_exclusive_ctxs = false;
1585 mutex_unlock(&damon_lock);
1591 * struct damon_system_ram_region - System RAM resource address region of
1593 * @start: Start address of the region (inclusive).
1594 * @end: End address of the region (exclusive).
1596 struct damon_system_ram_region {
1597 unsigned long start;
1601 static int walk_system_ram(struct resource *res, void *arg)
1603 struct damon_system_ram_region *a = arg;
1605 if (a->end - a->start < resource_size(res)) {
1606 a->start = res->start;
1613 * Find biggest 'System RAM' resource and store its start and end address in
1614 * @start and @end, respectively. If no System RAM is found, returns false.
1616 static bool damon_find_biggest_system_ram(unsigned long *start,
1620 struct damon_system_ram_region arg = {};
1622 walk_system_ram_res(0, ULONG_MAX, &arg, walk_system_ram);
1623 if (arg.end <= arg.start)
1632 * damon_set_region_biggest_system_ram_default() - Set the region of the given
1633 * monitoring target as requested, or biggest 'System RAM'.
1634 * @t: The monitoring target to set the region.
1635 * @start: The pointer to the start address of the region.
1636 * @end: The pointer to the end address of the region.
1638 * This function sets the region of @t as requested by @start and @end. If the
1639 * values of @start and @end are zero, however, this function finds the biggest
1640 * 'System RAM' resource and sets the region to cover the resource. In the
1641 * latter case, this function saves the start and end addresses of the resource
1642 * in @start and @end, respectively.
1644 * Return: 0 on success, negative error code otherwise.
1646 int damon_set_region_biggest_system_ram_default(struct damon_target *t,
1647 unsigned long *start, unsigned long *end)
1649 struct damon_addr_range addr_range;
1654 if (!*start && !*end &&
1655 !damon_find_biggest_system_ram(start, end))
1658 addr_range.start = *start;
1659 addr_range.end = *end;
1660 return damon_set_regions(t, &addr_range, 1);
1664 * damon_moving_sum() - Calculate an inferred moving sum value.
1665 * @mvsum: Inferred sum of the last @len_window values.
1666 * @nomvsum: Non-moving sum of the last discrete @len_window window values.
1667 * @len_window: The number of last values to take care of.
1668 * @new_value: New value that will be added to the pseudo moving sum.
1670 * Moving sum (moving average * window size) is good for handling noise, but
1671 * the cost of keeping past values can be high for arbitrary window size. This
1672 * function implements a lightweight pseudo moving sum function that doesn't
1673 * keep the past window values.
1675 * It simply assumes there was no noise in the past, and get the no-noise
1676 * assumed past value to drop from @nomvsum and @len_window. @nomvsum is a
1677 * non-moving sum of the last window. For example, if @len_window is 10 and we
1678 * have 25 values, @nomvsum is the sum of the 11th to 20th values of the 25
1679 * values. Hence, this function simply drops @nomvsum / @len_window from
1680 * given @mvsum and add @new_value.
1682 * For example, if @len_window is 10 and @nomvsum is 50, the last 10 values for
1683 * the last window could be vary, e.g., 0, 10, 0, 10, 0, 10, 0, 0, 0, 20. For
1684 * calculating next moving sum with a new value, we should drop 0 from 50 and
1685 * add the new value. However, this function assumes it got value 5 for each
1686 * of the last ten times. Based on the assumption, when the next value is
1687 * measured, it drops the assumed past value, 5 from the current sum, and add
1688 * the new value to get the updated pseduo-moving average.
1690 * This means the value could have errors, but the errors will be disappeared
1691 * for every @len_window aligned calls. For example, if @len_window is 10, the
1692 * pseudo moving sum with 11th value to 19th value would have an error. But
1693 * the sum with 20th value will not have the error.
1695 * Return: Pseudo-moving average after getting the @new_value.
1697 static unsigned int damon_moving_sum(unsigned int mvsum, unsigned int nomvsum,
1698 unsigned int len_window, unsigned int new_value)
1700 return mvsum - nomvsum / len_window + new_value;
1704 * damon_update_region_access_rate() - Update the access rate of a region.
1705 * @r: The DAMON region to update for its access check result.
1706 * @accessed: Whether the region has accessed during last sampling interval.
1707 * @attrs: The damon_attrs of the DAMON context.
1709 * Update the access rate of a region with the region's last sampling interval
1710 * access check result.
1712 * Usually this will be called by &damon_operations->check_accesses callback.
1714 void damon_update_region_access_rate(struct damon_region *r, bool accessed,
1715 struct damon_attrs *attrs)
1717 unsigned int len_window = 1;
1720 * sample_interval can be zero, but cannot be larger than
1721 * aggr_interval, owing to validation of damon_set_attrs().
1723 if (attrs->sample_interval)
1724 len_window = damon_max_nr_accesses(attrs);
1725 r->nr_accesses_bp = damon_moving_sum(r->nr_accesses_bp,
1726 r->last_nr_accesses * 10000, len_window,
1727 accessed ? 10000 : 0);
1733 static int __init damon_init(void)
1735 damon_region_cache = KMEM_CACHE(damon_region, 0);
1736 if (unlikely(!damon_region_cache)) {
1737 pr_err("creating damon_region_cache fails\n");
1744 subsys_initcall(damon_init);
1746 #include "core-test.h"