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;
29 * Construct a damon_region struct
31 * Returns the pointer to the new struct if success, or NULL otherwise
33 struct damon_region *damon_new_region(unsigned long start, unsigned long end)
35 struct damon_region *region;
37 region = kmalloc(sizeof(*region), GFP_KERNEL);
41 region->ar.start = start;
43 region->nr_accesses = 0;
44 INIT_LIST_HEAD(®ion->list);
47 region->last_nr_accesses = 0;
52 void damon_add_region(struct damon_region *r, struct damon_target *t)
54 list_add_tail(&r->list, &t->regions_list);
58 static void damon_del_region(struct damon_region *r, struct damon_target *t)
64 static void damon_free_region(struct damon_region *r)
69 void damon_destroy_region(struct damon_region *r, struct damon_target *t)
71 damon_del_region(r, t);
75 struct damos *damon_new_scheme(
76 unsigned long min_sz_region, unsigned long max_sz_region,
77 unsigned int min_nr_accesses, unsigned int max_nr_accesses,
78 unsigned int min_age_region, unsigned int max_age_region,
79 enum damos_action action, struct damos_quota *quota,
80 struct damos_watermarks *wmarks)
84 scheme = kmalloc(sizeof(*scheme), GFP_KERNEL);
87 scheme->min_sz_region = min_sz_region;
88 scheme->max_sz_region = max_sz_region;
89 scheme->min_nr_accesses = min_nr_accesses;
90 scheme->max_nr_accesses = max_nr_accesses;
91 scheme->min_age_region = min_age_region;
92 scheme->max_age_region = max_age_region;
93 scheme->action = action;
94 scheme->stat = (struct damos_stat){};
95 INIT_LIST_HEAD(&scheme->list);
97 scheme->quota.ms = quota->ms;
98 scheme->quota.sz = quota->sz;
99 scheme->quota.reset_interval = quota->reset_interval;
100 scheme->quota.weight_sz = quota->weight_sz;
101 scheme->quota.weight_nr_accesses = quota->weight_nr_accesses;
102 scheme->quota.weight_age = quota->weight_age;
103 scheme->quota.total_charged_sz = 0;
104 scheme->quota.total_charged_ns = 0;
105 scheme->quota.esz = 0;
106 scheme->quota.charged_sz = 0;
107 scheme->quota.charged_from = 0;
108 scheme->quota.charge_target_from = NULL;
109 scheme->quota.charge_addr_from = 0;
111 scheme->wmarks.metric = wmarks->metric;
112 scheme->wmarks.interval = wmarks->interval;
113 scheme->wmarks.high = wmarks->high;
114 scheme->wmarks.mid = wmarks->mid;
115 scheme->wmarks.low = wmarks->low;
116 scheme->wmarks.activated = true;
121 void damon_add_scheme(struct damon_ctx *ctx, struct damos *s)
123 list_add_tail(&s->list, &ctx->schemes);
126 static void damon_del_scheme(struct damos *s)
131 static void damon_free_scheme(struct damos *s)
136 void damon_destroy_scheme(struct damos *s)
139 damon_free_scheme(s);
143 * Construct a damon_target struct
145 * Returns the pointer to the new struct if success, or NULL otherwise
147 struct damon_target *damon_new_target(unsigned long id)
149 struct damon_target *t;
151 t = kmalloc(sizeof(*t), GFP_KERNEL);
157 INIT_LIST_HEAD(&t->regions_list);
162 void damon_add_target(struct damon_ctx *ctx, struct damon_target *t)
164 list_add_tail(&t->list, &ctx->adaptive_targets);
167 bool damon_targets_empty(struct damon_ctx *ctx)
169 return list_empty(&ctx->adaptive_targets);
172 static void damon_del_target(struct damon_target *t)
177 void damon_free_target(struct damon_target *t)
179 struct damon_region *r, *next;
181 damon_for_each_region_safe(r, next, t)
182 damon_free_region(r);
186 void damon_destroy_target(struct damon_target *t)
189 damon_free_target(t);
192 unsigned int damon_nr_regions(struct damon_target *t)
194 return t->nr_regions;
197 struct damon_ctx *damon_new_ctx(void)
199 struct damon_ctx *ctx;
201 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
205 ctx->sample_interval = 5 * 1000;
206 ctx->aggr_interval = 100 * 1000;
207 ctx->primitive_update_interval = 60 * 1000 * 1000;
209 ktime_get_coarse_ts64(&ctx->last_aggregation);
210 ctx->last_primitive_update = ctx->last_aggregation;
212 mutex_init(&ctx->kdamond_lock);
214 ctx->min_nr_regions = 10;
215 ctx->max_nr_regions = 1000;
217 INIT_LIST_HEAD(&ctx->adaptive_targets);
218 INIT_LIST_HEAD(&ctx->schemes);
223 static void damon_destroy_targets(struct damon_ctx *ctx)
225 struct damon_target *t, *next_t;
227 if (ctx->primitive.cleanup) {
228 ctx->primitive.cleanup(ctx);
232 damon_for_each_target_safe(t, next_t, ctx)
233 damon_destroy_target(t);
236 void damon_destroy_ctx(struct damon_ctx *ctx)
238 struct damos *s, *next_s;
240 damon_destroy_targets(ctx);
242 damon_for_each_scheme_safe(s, next_s, ctx)
243 damon_destroy_scheme(s);
249 * damon_set_targets() - Set monitoring targets.
250 * @ctx: monitoring context
251 * @ids: array of target ids
252 * @nr_ids: number of entries in @ids
254 * This function should not be called while the kdamond is running.
256 * Return: 0 on success, negative error code otherwise.
258 int damon_set_targets(struct damon_ctx *ctx,
259 unsigned long *ids, ssize_t nr_ids)
262 struct damon_target *t, *next;
264 damon_destroy_targets(ctx);
266 for (i = 0; i < nr_ids; i++) {
267 t = damon_new_target(ids[i]);
269 /* The caller should do cleanup of the ids itself */
270 damon_for_each_target_safe(t, next, ctx)
271 damon_destroy_target(t);
274 damon_add_target(ctx, t);
281 * damon_set_attrs() - Set attributes for the monitoring.
282 * @ctx: monitoring context
283 * @sample_int: time interval between samplings
284 * @aggr_int: time interval between aggregations
285 * @primitive_upd_int: time interval between monitoring primitive updates
286 * @min_nr_reg: minimal number of regions
287 * @max_nr_reg: maximum number of regions
289 * This function should not be called while the kdamond is running.
290 * Every time interval is in micro-seconds.
292 * Return: 0 on success, negative error code otherwise.
294 int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
295 unsigned long aggr_int, unsigned long primitive_upd_int,
296 unsigned long min_nr_reg, unsigned long max_nr_reg)
300 if (min_nr_reg > max_nr_reg)
303 ctx->sample_interval = sample_int;
304 ctx->aggr_interval = aggr_int;
305 ctx->primitive_update_interval = primitive_upd_int;
306 ctx->min_nr_regions = min_nr_reg;
307 ctx->max_nr_regions = max_nr_reg;
313 * damon_set_schemes() - Set data access monitoring based operation schemes.
314 * @ctx: monitoring context
315 * @schemes: array of the schemes
316 * @nr_schemes: number of entries in @schemes
318 * This function should not be called while the kdamond of the context is
321 * Return: 0 if success, or negative error code otherwise.
323 int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes,
326 struct damos *s, *next;
329 damon_for_each_scheme_safe(s, next, ctx)
330 damon_destroy_scheme(s);
331 for (i = 0; i < nr_schemes; i++)
332 damon_add_scheme(ctx, schemes[i]);
337 * damon_nr_running_ctxs() - Return number of currently running contexts.
339 int damon_nr_running_ctxs(void)
343 mutex_lock(&damon_lock);
344 nr_ctxs = nr_running_ctxs;
345 mutex_unlock(&damon_lock);
350 /* Returns the size upper limit for each monitoring region */
351 static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
353 struct damon_target *t;
354 struct damon_region *r;
355 unsigned long sz = 0;
357 damon_for_each_target(t, ctx) {
358 damon_for_each_region(r, t)
359 sz += r->ar.end - r->ar.start;
362 if (ctx->min_nr_regions)
363 sz /= ctx->min_nr_regions;
364 if (sz < DAMON_MIN_REGION)
365 sz = DAMON_MIN_REGION;
370 static int kdamond_fn(void *data);
373 * __damon_start() - Starts monitoring with given context.
374 * @ctx: monitoring context
376 * This function should be called while damon_lock is hold.
378 * Return: 0 on success, negative error code otherwise.
380 static int __damon_start(struct damon_ctx *ctx)
384 mutex_lock(&ctx->kdamond_lock);
387 ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d",
389 if (IS_ERR(ctx->kdamond)) {
390 err = PTR_ERR(ctx->kdamond);
394 mutex_unlock(&ctx->kdamond_lock);
400 * damon_start() - Starts the monitorings for a given group of contexts.
401 * @ctxs: an array of the pointers for contexts to start monitoring
402 * @nr_ctxs: size of @ctxs
404 * This function starts a group of monitoring threads for a group of monitoring
405 * contexts. One thread per each context is created and run in parallel. The
406 * caller should handle synchronization between the threads by itself. If a
407 * group of threads that created by other 'damon_start()' call is currently
408 * running, this function does nothing but returns -EBUSY.
410 * Return: 0 on success, negative error code otherwise.
412 int damon_start(struct damon_ctx **ctxs, int nr_ctxs)
417 mutex_lock(&damon_lock);
418 if (nr_running_ctxs) {
419 mutex_unlock(&damon_lock);
423 for (i = 0; i < nr_ctxs; i++) {
424 err = __damon_start(ctxs[i]);
429 mutex_unlock(&damon_lock);
435 * __damon_stop() - Stops monitoring of given context.
436 * @ctx: monitoring context
438 * Return: 0 on success, negative error code otherwise.
440 static int __damon_stop(struct damon_ctx *ctx)
442 struct task_struct *tsk;
444 mutex_lock(&ctx->kdamond_lock);
447 get_task_struct(tsk);
448 mutex_unlock(&ctx->kdamond_lock);
450 put_task_struct(tsk);
453 mutex_unlock(&ctx->kdamond_lock);
459 * damon_stop() - Stops the monitorings for a given group of contexts.
460 * @ctxs: an array of the pointers for contexts to stop monitoring
461 * @nr_ctxs: size of @ctxs
463 * Return: 0 on success, negative error code otherwise.
465 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
469 for (i = 0; i < nr_ctxs; i++) {
470 /* nr_running_ctxs is decremented in kdamond_fn */
471 err = __damon_stop(ctxs[i]);
480 * damon_check_reset_time_interval() - Check if a time interval is elapsed.
481 * @baseline: the time to check whether the interval has elapsed since
482 * @interval: the time interval (microseconds)
484 * See whether the given time interval has passed since the given baseline
485 * time. If so, it also updates the baseline to current time for next check.
487 * Return: true if the time interval has passed, or false otherwise.
489 static bool damon_check_reset_time_interval(struct timespec64 *baseline,
490 unsigned long interval)
492 struct timespec64 now;
494 ktime_get_coarse_ts64(&now);
495 if ((timespec64_to_ns(&now) - timespec64_to_ns(baseline)) <
503 * Check whether it is time to flush the aggregated information
505 static bool kdamond_aggregate_interval_passed(struct damon_ctx *ctx)
507 return damon_check_reset_time_interval(&ctx->last_aggregation,
512 * Reset the aggregated monitoring results ('nr_accesses' of each region).
514 static void kdamond_reset_aggregated(struct damon_ctx *c)
516 struct damon_target *t;
517 unsigned int ti = 0; /* target's index */
519 damon_for_each_target(t, c) {
520 struct damon_region *r;
522 damon_for_each_region(r, t) {
523 trace_damon_aggregated(t, ti, r, damon_nr_regions(t));
524 r->last_nr_accesses = r->nr_accesses;
531 static void damon_split_region_at(struct damon_ctx *ctx,
532 struct damon_target *t, struct damon_region *r,
535 static bool __damos_valid_target(struct damon_region *r, struct damos *s)
539 sz = r->ar.end - r->ar.start;
540 return s->min_sz_region <= sz && sz <= s->max_sz_region &&
541 s->min_nr_accesses <= r->nr_accesses &&
542 r->nr_accesses <= s->max_nr_accesses &&
543 s->min_age_region <= r->age && r->age <= s->max_age_region;
546 static bool damos_valid_target(struct damon_ctx *c, struct damon_target *t,
547 struct damon_region *r, struct damos *s)
549 bool ret = __damos_valid_target(r, s);
551 if (!ret || !s->quota.esz || !c->primitive.get_scheme_score)
554 return c->primitive.get_scheme_score(c, t, r, s) >= s->quota.min_score;
557 static void damon_do_apply_schemes(struct damon_ctx *c,
558 struct damon_target *t,
559 struct damon_region *r)
563 damon_for_each_scheme(s, c) {
564 struct damos_quota *quota = &s->quota;
565 unsigned long sz = r->ar.end - r->ar.start;
566 struct timespec64 begin, end;
567 unsigned long sz_applied = 0;
569 if (!s->wmarks.activated)
572 /* Check the quota */
573 if (quota->esz && quota->charged_sz >= quota->esz)
576 /* Skip previously charged regions */
577 if (quota->charge_target_from) {
578 if (t != quota->charge_target_from)
580 if (r == damon_last_region(t)) {
581 quota->charge_target_from = NULL;
582 quota->charge_addr_from = 0;
585 if (quota->charge_addr_from &&
586 r->ar.end <= quota->charge_addr_from)
589 if (quota->charge_addr_from && r->ar.start <
590 quota->charge_addr_from) {
591 sz = ALIGN_DOWN(quota->charge_addr_from -
592 r->ar.start, DAMON_MIN_REGION);
594 if (r->ar.end - r->ar.start <=
597 sz = DAMON_MIN_REGION;
599 damon_split_region_at(c, t, r, sz);
600 r = damon_next_region(r);
601 sz = r->ar.end - r->ar.start;
603 quota->charge_target_from = NULL;
604 quota->charge_addr_from = 0;
607 if (!damos_valid_target(c, t, r, s))
610 /* Apply the scheme */
611 if (c->primitive.apply_scheme) {
613 quota->charged_sz + sz > quota->esz) {
614 sz = ALIGN_DOWN(quota->esz - quota->charged_sz,
618 damon_split_region_at(c, t, r, sz);
620 ktime_get_coarse_ts64(&begin);
621 sz_applied = c->primitive.apply_scheme(c, t, r, s);
622 ktime_get_coarse_ts64(&end);
623 quota->total_charged_ns += timespec64_to_ns(&end) -
624 timespec64_to_ns(&begin);
625 quota->charged_sz += sz;
626 if (quota->esz && quota->charged_sz >= quota->esz) {
627 quota->charge_target_from = t;
628 quota->charge_addr_from = r->ar.end + 1;
631 if (s->action != DAMOS_STAT)
636 s->stat.sz_tried += sz;
638 s->stat.nr_applied++;
639 s->stat.sz_applied += sz_applied;
643 /* Shouldn't be called if quota->ms and quota->sz are zero */
644 static void damos_set_effective_quota(struct damos_quota *quota)
646 unsigned long throughput;
650 quota->esz = quota->sz;
654 if (quota->total_charged_ns)
655 throughput = quota->total_charged_sz * 1000000 /
656 quota->total_charged_ns;
658 throughput = PAGE_SIZE * 1024;
659 esz = throughput * quota->ms;
661 if (quota->sz && quota->sz < esz)
666 static void kdamond_apply_schemes(struct damon_ctx *c)
668 struct damon_target *t;
669 struct damon_region *r, *next_r;
672 damon_for_each_scheme(s, c) {
673 struct damos_quota *quota = &s->quota;
674 unsigned long cumulated_sz;
675 unsigned int score, max_score = 0;
677 if (!s->wmarks.activated)
680 if (!quota->ms && !quota->sz)
683 /* New charge window starts */
684 if (time_after_eq(jiffies, quota->charged_from +
686 quota->reset_interval))) {
687 if (quota->esz && quota->charged_sz >= quota->esz)
688 s->stat.qt_exceeds++;
689 quota->total_charged_sz += quota->charged_sz;
690 quota->charged_from = jiffies;
691 quota->charged_sz = 0;
692 damos_set_effective_quota(quota);
695 if (!c->primitive.get_scheme_score)
698 /* Fill up the score histogram */
699 memset(quota->histogram, 0, sizeof(quota->histogram));
700 damon_for_each_target(t, c) {
701 damon_for_each_region(r, t) {
702 if (!__damos_valid_target(r, s))
704 score = c->primitive.get_scheme_score(
706 quota->histogram[score] +=
707 r->ar.end - r->ar.start;
708 if (score > max_score)
713 /* Set the min score limit */
714 for (cumulated_sz = 0, score = max_score; ; score--) {
715 cumulated_sz += quota->histogram[score];
716 if (cumulated_sz >= quota->esz || !score)
719 quota->min_score = score;
722 damon_for_each_target(t, c) {
723 damon_for_each_region_safe(r, next_r, t)
724 damon_do_apply_schemes(c, t, r);
728 static inline unsigned long sz_damon_region(struct damon_region *r)
730 return r->ar.end - r->ar.start;
734 * Merge two adjacent regions into one region
736 static void damon_merge_two_regions(struct damon_target *t,
737 struct damon_region *l, struct damon_region *r)
739 unsigned long sz_l = sz_damon_region(l), sz_r = sz_damon_region(r);
741 l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) /
743 l->age = (l->age * sz_l + r->age * sz_r) / (sz_l + sz_r);
744 l->ar.end = r->ar.end;
745 damon_destroy_region(r, t);
749 * Merge adjacent regions having similar access frequencies
751 * t target affected by this merge operation
752 * thres '->nr_accesses' diff threshold for the merge
753 * sz_limit size upper limit of each region
755 static void damon_merge_regions_of(struct damon_target *t, unsigned int thres,
756 unsigned long sz_limit)
758 struct damon_region *r, *prev = NULL, *next;
760 damon_for_each_region_safe(r, next, t) {
761 if (abs(r->nr_accesses - r->last_nr_accesses) > thres)
766 if (prev && prev->ar.end == r->ar.start &&
767 abs(prev->nr_accesses - r->nr_accesses) <= thres &&
768 sz_damon_region(prev) + sz_damon_region(r) <= sz_limit)
769 damon_merge_two_regions(t, prev, r);
776 * Merge adjacent regions having similar access frequencies
778 * threshold '->nr_accesses' diff threshold for the merge
779 * sz_limit size upper limit of each region
781 * This function merges monitoring target regions which are adjacent and their
782 * access frequencies are similar. This is for minimizing the monitoring
783 * overhead under the dynamically changeable access pattern. If a merge was
784 * unnecessarily made, later 'kdamond_split_regions()' will revert it.
786 static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
787 unsigned long sz_limit)
789 struct damon_target *t;
791 damon_for_each_target(t, c)
792 damon_merge_regions_of(t, threshold, sz_limit);
796 * Split a region in two
798 * r the region to be split
799 * sz_r size of the first sub-region that will be made
801 static void damon_split_region_at(struct damon_ctx *ctx,
802 struct damon_target *t, struct damon_region *r,
805 struct damon_region *new;
807 new = damon_new_region(r->ar.start + sz_r, r->ar.end);
811 r->ar.end = new->ar.start;
814 new->last_nr_accesses = r->last_nr_accesses;
816 damon_insert_region(new, r, damon_next_region(r), t);
819 /* Split every region in the given target into 'nr_subs' regions */
820 static void damon_split_regions_of(struct damon_ctx *ctx,
821 struct damon_target *t, int nr_subs)
823 struct damon_region *r, *next;
824 unsigned long sz_region, sz_sub = 0;
827 damon_for_each_region_safe(r, next, t) {
828 sz_region = r->ar.end - r->ar.start;
830 for (i = 0; i < nr_subs - 1 &&
831 sz_region > 2 * DAMON_MIN_REGION; i++) {
833 * Randomly select size of left sub-region to be at
834 * least 10 percent and at most 90% of original region
836 sz_sub = ALIGN_DOWN(damon_rand(1, 10) *
837 sz_region / 10, DAMON_MIN_REGION);
838 /* Do not allow blank region */
839 if (sz_sub == 0 || sz_sub >= sz_region)
842 damon_split_region_at(ctx, t, r, sz_sub);
849 * Split every target region into randomly-sized small regions
851 * This function splits every target region into random-sized small regions if
852 * current total number of the regions is equal or smaller than half of the
853 * user-specified maximum number of regions. This is for maximizing the
854 * monitoring accuracy under the dynamically changeable access patterns. If a
855 * split was unnecessarily made, later 'kdamond_merge_regions()' will revert
858 static void kdamond_split_regions(struct damon_ctx *ctx)
860 struct damon_target *t;
861 unsigned int nr_regions = 0;
862 static unsigned int last_nr_regions;
863 int nr_subregions = 2;
865 damon_for_each_target(t, ctx)
866 nr_regions += damon_nr_regions(t);
868 if (nr_regions > ctx->max_nr_regions / 2)
871 /* Maybe the middle of the region has different access frequency */
872 if (last_nr_regions == nr_regions &&
873 nr_regions < ctx->max_nr_regions / 3)
876 damon_for_each_target(t, ctx)
877 damon_split_regions_of(ctx, t, nr_subregions);
879 last_nr_regions = nr_regions;
883 * Check whether it is time to check and apply the target monitoring regions
885 * Returns true if it is.
887 static bool kdamond_need_update_primitive(struct damon_ctx *ctx)
889 return damon_check_reset_time_interval(&ctx->last_primitive_update,
890 ctx->primitive_update_interval);
894 * Check whether current monitoring should be stopped
896 * The monitoring is stopped when either the user requested to stop, or all
897 * monitoring targets are invalid.
899 * Returns true if need to stop current monitoring.
901 static bool kdamond_need_stop(struct damon_ctx *ctx)
903 struct damon_target *t;
905 if (kthread_should_stop())
908 if (!ctx->primitive.target_valid)
911 damon_for_each_target(t, ctx) {
912 if (ctx->primitive.target_valid(t))
919 static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
924 case DAMOS_WMARK_FREE_MEM_RATE:
926 return i.freeram * 1000 / i.totalram;
934 * Returns zero if the scheme is active. Else, returns time to wait for next
935 * watermark check in micro-seconds.
937 static unsigned long damos_wmark_wait_us(struct damos *scheme)
939 unsigned long metric;
941 if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
944 metric = damos_wmark_metric_value(scheme->wmarks.metric);
945 /* higher than high watermark or lower than low watermark */
946 if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
947 if (scheme->wmarks.activated)
948 pr_debug("deactivate a scheme (%d) for %s wmark\n",
950 metric > scheme->wmarks.high ?
952 scheme->wmarks.activated = false;
953 return scheme->wmarks.interval;
956 /* inactive and higher than middle watermark */
957 if ((scheme->wmarks.high >= metric && metric >= scheme->wmarks.mid) &&
958 !scheme->wmarks.activated)
959 return scheme->wmarks.interval;
961 if (!scheme->wmarks.activated)
962 pr_debug("activate a scheme (%d)\n", scheme->action);
963 scheme->wmarks.activated = true;
967 static void kdamond_usleep(unsigned long usecs)
969 /* See Documentation/timers/timers-howto.rst for the thresholds */
970 if (usecs > 20 * USEC_PER_MSEC)
971 schedule_timeout_idle(usecs_to_jiffies(usecs));
973 usleep_idle_range(usecs, usecs + 1);
976 /* Returns negative error code if it's not activated but should return */
977 static int kdamond_wait_activation(struct damon_ctx *ctx)
980 unsigned long wait_time;
981 unsigned long min_wait_time = 0;
983 while (!kdamond_need_stop(ctx)) {
984 damon_for_each_scheme(s, ctx) {
985 wait_time = damos_wmark_wait_us(s);
986 if (!min_wait_time || wait_time < min_wait_time)
987 min_wait_time = wait_time;
992 kdamond_usleep(min_wait_time);
998 * The monitoring daemon that runs as a kernel thread
1000 static int kdamond_fn(void *data)
1002 struct damon_ctx *ctx = (struct damon_ctx *)data;
1003 struct damon_target *t;
1004 struct damon_region *r, *next;
1005 unsigned int max_nr_accesses = 0;
1006 unsigned long sz_limit = 0;
1009 pr_debug("kdamond (%d) starts\n", current->pid);
1011 if (ctx->primitive.init)
1012 ctx->primitive.init(ctx);
1013 if (ctx->callback.before_start && ctx->callback.before_start(ctx))
1016 sz_limit = damon_region_sz_limit(ctx);
1018 while (!kdamond_need_stop(ctx) && !done) {
1019 if (kdamond_wait_activation(ctx))
1022 if (ctx->primitive.prepare_access_checks)
1023 ctx->primitive.prepare_access_checks(ctx);
1024 if (ctx->callback.after_sampling &&
1025 ctx->callback.after_sampling(ctx))
1028 kdamond_usleep(ctx->sample_interval);
1030 if (ctx->primitive.check_accesses)
1031 max_nr_accesses = ctx->primitive.check_accesses(ctx);
1033 if (kdamond_aggregate_interval_passed(ctx)) {
1034 kdamond_merge_regions(ctx,
1035 max_nr_accesses / 10,
1037 if (ctx->callback.after_aggregation &&
1038 ctx->callback.after_aggregation(ctx))
1040 kdamond_apply_schemes(ctx);
1041 kdamond_reset_aggregated(ctx);
1042 kdamond_split_regions(ctx);
1043 if (ctx->primitive.reset_aggregated)
1044 ctx->primitive.reset_aggregated(ctx);
1047 if (kdamond_need_update_primitive(ctx)) {
1048 if (ctx->primitive.update)
1049 ctx->primitive.update(ctx);
1050 sz_limit = damon_region_sz_limit(ctx);
1053 damon_for_each_target(t, ctx) {
1054 damon_for_each_region_safe(r, next, t)
1055 damon_destroy_region(r, t);
1058 if (ctx->callback.before_terminate)
1059 ctx->callback.before_terminate(ctx);
1060 if (ctx->primitive.cleanup)
1061 ctx->primitive.cleanup(ctx);
1063 pr_debug("kdamond (%d) finishes\n", current->pid);
1064 mutex_lock(&ctx->kdamond_lock);
1065 ctx->kdamond = NULL;
1066 mutex_unlock(&ctx->kdamond_lock);
1068 mutex_lock(&damon_lock);
1070 mutex_unlock(&damon_lock);
1075 #include "core-test.h"