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/random.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/damon.h>
21 #ifdef CONFIG_DAMON_KUNIT_TEST
22 #undef DAMON_MIN_REGION
23 #define DAMON_MIN_REGION 1
26 /* Get a random number in [l, r) */
27 #define damon_rand(l, r) (l + prandom_u32_max(r - l))
29 static DEFINE_MUTEX(damon_lock);
30 static int nr_running_ctxs;
33 * Construct a damon_region struct
35 * Returns the pointer to the new struct if success, or NULL otherwise
37 struct damon_region *damon_new_region(unsigned long start, unsigned long end)
39 struct damon_region *region;
41 region = kmalloc(sizeof(*region), GFP_KERNEL);
45 region->ar.start = start;
47 region->nr_accesses = 0;
48 INIT_LIST_HEAD(®ion->list);
51 region->last_nr_accesses = 0;
57 * Add a region between two other regions
59 inline void damon_insert_region(struct damon_region *r,
60 struct damon_region *prev, struct damon_region *next,
61 struct damon_target *t)
63 __list_add(&r->list, &prev->list, &next->list);
67 void damon_add_region(struct damon_region *r, struct damon_target *t)
69 list_add_tail(&r->list, &t->regions_list);
73 static void damon_del_region(struct damon_region *r, struct damon_target *t)
79 static void damon_free_region(struct damon_region *r)
84 void damon_destroy_region(struct damon_region *r, struct damon_target *t)
86 damon_del_region(r, t);
90 struct damos *damon_new_scheme(
91 unsigned long min_sz_region, unsigned long max_sz_region,
92 unsigned int min_nr_accesses, unsigned int max_nr_accesses,
93 unsigned int min_age_region, unsigned int max_age_region,
94 enum damos_action action, struct damos_quota *quota,
95 struct damos_watermarks *wmarks)
99 scheme = kmalloc(sizeof(*scheme), GFP_KERNEL);
102 scheme->min_sz_region = min_sz_region;
103 scheme->max_sz_region = max_sz_region;
104 scheme->min_nr_accesses = min_nr_accesses;
105 scheme->max_nr_accesses = max_nr_accesses;
106 scheme->min_age_region = min_age_region;
107 scheme->max_age_region = max_age_region;
108 scheme->action = action;
109 scheme->stat_count = 0;
111 INIT_LIST_HEAD(&scheme->list);
113 scheme->quota.ms = quota->ms;
114 scheme->quota.sz = quota->sz;
115 scheme->quota.reset_interval = quota->reset_interval;
116 scheme->quota.weight_sz = quota->weight_sz;
117 scheme->quota.weight_nr_accesses = quota->weight_nr_accesses;
118 scheme->quota.weight_age = quota->weight_age;
119 scheme->quota.total_charged_sz = 0;
120 scheme->quota.total_charged_ns = 0;
121 scheme->quota.esz = 0;
122 scheme->quota.charged_sz = 0;
123 scheme->quota.charged_from = 0;
124 scheme->quota.charge_target_from = NULL;
125 scheme->quota.charge_addr_from = 0;
127 scheme->wmarks.metric = wmarks->metric;
128 scheme->wmarks.interval = wmarks->interval;
129 scheme->wmarks.high = wmarks->high;
130 scheme->wmarks.mid = wmarks->mid;
131 scheme->wmarks.low = wmarks->low;
132 scheme->wmarks.activated = true;
137 void damon_add_scheme(struct damon_ctx *ctx, struct damos *s)
139 list_add_tail(&s->list, &ctx->schemes);
142 static void damon_del_scheme(struct damos *s)
147 static void damon_free_scheme(struct damos *s)
152 void damon_destroy_scheme(struct damos *s)
155 damon_free_scheme(s);
159 * Construct a damon_target struct
161 * Returns the pointer to the new struct if success, or NULL otherwise
163 struct damon_target *damon_new_target(unsigned long id)
165 struct damon_target *t;
167 t = kmalloc(sizeof(*t), GFP_KERNEL);
173 INIT_LIST_HEAD(&t->regions_list);
178 void damon_add_target(struct damon_ctx *ctx, struct damon_target *t)
180 list_add_tail(&t->list, &ctx->adaptive_targets);
183 bool damon_targets_empty(struct damon_ctx *ctx)
185 return list_empty(&ctx->adaptive_targets);
188 static void damon_del_target(struct damon_target *t)
193 void damon_free_target(struct damon_target *t)
195 struct damon_region *r, *next;
197 damon_for_each_region_safe(r, next, t)
198 damon_free_region(r);
202 void damon_destroy_target(struct damon_target *t)
205 damon_free_target(t);
208 unsigned int damon_nr_regions(struct damon_target *t)
210 return t->nr_regions;
213 struct damon_ctx *damon_new_ctx(void)
215 struct damon_ctx *ctx;
217 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
221 ctx->sample_interval = 5 * 1000;
222 ctx->aggr_interval = 100 * 1000;
223 ctx->primitive_update_interval = 60 * 1000 * 1000;
225 ktime_get_coarse_ts64(&ctx->last_aggregation);
226 ctx->last_primitive_update = ctx->last_aggregation;
228 mutex_init(&ctx->kdamond_lock);
230 ctx->min_nr_regions = 10;
231 ctx->max_nr_regions = 1000;
233 INIT_LIST_HEAD(&ctx->adaptive_targets);
234 INIT_LIST_HEAD(&ctx->schemes);
239 static void damon_destroy_targets(struct damon_ctx *ctx)
241 struct damon_target *t, *next_t;
243 if (ctx->primitive.cleanup) {
244 ctx->primitive.cleanup(ctx);
248 damon_for_each_target_safe(t, next_t, ctx)
249 damon_destroy_target(t);
252 void damon_destroy_ctx(struct damon_ctx *ctx)
254 struct damos *s, *next_s;
256 damon_destroy_targets(ctx);
258 damon_for_each_scheme_safe(s, next_s, ctx)
259 damon_destroy_scheme(s);
265 * damon_set_targets() - Set monitoring targets.
266 * @ctx: monitoring context
267 * @ids: array of target ids
268 * @nr_ids: number of entries in @ids
270 * This function should not be called while the kdamond is running.
272 * Return: 0 on success, negative error code otherwise.
274 int damon_set_targets(struct damon_ctx *ctx,
275 unsigned long *ids, ssize_t nr_ids)
278 struct damon_target *t, *next;
280 damon_destroy_targets(ctx);
282 for (i = 0; i < nr_ids; i++) {
283 t = damon_new_target(ids[i]);
285 pr_err("Failed to alloc damon_target\n");
286 /* The caller should do cleanup of the ids itself */
287 damon_for_each_target_safe(t, next, ctx)
288 damon_destroy_target(t);
291 damon_add_target(ctx, t);
298 * damon_set_attrs() - Set attributes for the monitoring.
299 * @ctx: monitoring context
300 * @sample_int: time interval between samplings
301 * @aggr_int: time interval between aggregations
302 * @primitive_upd_int: time interval between monitoring primitive updates
303 * @min_nr_reg: minimal number of regions
304 * @max_nr_reg: maximum number of regions
306 * This function should not be called while the kdamond is running.
307 * Every time interval is in micro-seconds.
309 * Return: 0 on success, negative error code otherwise.
311 int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
312 unsigned long aggr_int, unsigned long primitive_upd_int,
313 unsigned long min_nr_reg, unsigned long max_nr_reg)
315 if (min_nr_reg < 3) {
316 pr_err("min_nr_regions (%lu) must be at least 3\n",
320 if (min_nr_reg > max_nr_reg) {
321 pr_err("invalid nr_regions. min (%lu) > max (%lu)\n",
322 min_nr_reg, max_nr_reg);
326 ctx->sample_interval = sample_int;
327 ctx->aggr_interval = aggr_int;
328 ctx->primitive_update_interval = primitive_upd_int;
329 ctx->min_nr_regions = min_nr_reg;
330 ctx->max_nr_regions = max_nr_reg;
336 * damon_set_schemes() - Set data access monitoring based operation schemes.
337 * @ctx: monitoring context
338 * @schemes: array of the schemes
339 * @nr_schemes: number of entries in @schemes
341 * This function should not be called while the kdamond of the context is
344 * Return: 0 if success, or negative error code otherwise.
346 int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes,
349 struct damos *s, *next;
352 damon_for_each_scheme_safe(s, next, ctx)
353 damon_destroy_scheme(s);
354 for (i = 0; i < nr_schemes; i++)
355 damon_add_scheme(ctx, schemes[i]);
360 * damon_nr_running_ctxs() - Return number of currently running contexts.
362 int damon_nr_running_ctxs(void)
366 mutex_lock(&damon_lock);
367 nr_ctxs = nr_running_ctxs;
368 mutex_unlock(&damon_lock);
373 /* Returns the size upper limit for each monitoring region */
374 static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
376 struct damon_target *t;
377 struct damon_region *r;
378 unsigned long sz = 0;
380 damon_for_each_target(t, ctx) {
381 damon_for_each_region(r, t)
382 sz += r->ar.end - r->ar.start;
385 if (ctx->min_nr_regions)
386 sz /= ctx->min_nr_regions;
387 if (sz < DAMON_MIN_REGION)
388 sz = DAMON_MIN_REGION;
393 static int kdamond_fn(void *data);
396 * __damon_start() - Starts monitoring with given context.
397 * @ctx: monitoring context
399 * This function should be called while damon_lock is hold.
401 * Return: 0 on success, negative error code otherwise.
403 static int __damon_start(struct damon_ctx *ctx)
407 mutex_lock(&ctx->kdamond_lock);
410 ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d",
412 if (IS_ERR(ctx->kdamond)) {
413 err = PTR_ERR(ctx->kdamond);
417 mutex_unlock(&ctx->kdamond_lock);
423 * damon_start() - Starts the monitorings for a given group of contexts.
424 * @ctxs: an array of the pointers for contexts to start monitoring
425 * @nr_ctxs: size of @ctxs
427 * This function starts a group of monitoring threads for a group of monitoring
428 * contexts. One thread per each context is created and run in parallel. The
429 * caller should handle synchronization between the threads by itself. If a
430 * group of threads that created by other 'damon_start()' call is currently
431 * running, this function does nothing but returns -EBUSY.
433 * Return: 0 on success, negative error code otherwise.
435 int damon_start(struct damon_ctx **ctxs, int nr_ctxs)
440 mutex_lock(&damon_lock);
441 if (nr_running_ctxs) {
442 mutex_unlock(&damon_lock);
446 for (i = 0; i < nr_ctxs; i++) {
447 err = __damon_start(ctxs[i]);
452 mutex_unlock(&damon_lock);
458 * __damon_stop() - Stops monitoring of given context.
459 * @ctx: monitoring context
461 * Return: 0 on success, negative error code otherwise.
463 static int __damon_stop(struct damon_ctx *ctx)
465 struct task_struct *tsk;
467 mutex_lock(&ctx->kdamond_lock);
470 get_task_struct(tsk);
471 mutex_unlock(&ctx->kdamond_lock);
473 put_task_struct(tsk);
476 mutex_unlock(&ctx->kdamond_lock);
482 * damon_stop() - Stops the monitorings for a given group of contexts.
483 * @ctxs: an array of the pointers for contexts to stop monitoring
484 * @nr_ctxs: size of @ctxs
486 * Return: 0 on success, negative error code otherwise.
488 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
492 for (i = 0; i < nr_ctxs; i++) {
493 /* nr_running_ctxs is decremented in kdamond_fn */
494 err = __damon_stop(ctxs[i]);
503 * damon_check_reset_time_interval() - Check if a time interval is elapsed.
504 * @baseline: the time to check whether the interval has elapsed since
505 * @interval: the time interval (microseconds)
507 * See whether the given time interval has passed since the given baseline
508 * time. If so, it also updates the baseline to current time for next check.
510 * Return: true if the time interval has passed, or false otherwise.
512 static bool damon_check_reset_time_interval(struct timespec64 *baseline,
513 unsigned long interval)
515 struct timespec64 now;
517 ktime_get_coarse_ts64(&now);
518 if ((timespec64_to_ns(&now) - timespec64_to_ns(baseline)) <
526 * Check whether it is time to flush the aggregated information
528 static bool kdamond_aggregate_interval_passed(struct damon_ctx *ctx)
530 return damon_check_reset_time_interval(&ctx->last_aggregation,
535 * Reset the aggregated monitoring results ('nr_accesses' of each region).
537 static void kdamond_reset_aggregated(struct damon_ctx *c)
539 struct damon_target *t;
541 damon_for_each_target(t, c) {
542 struct damon_region *r;
544 damon_for_each_region(r, t) {
545 trace_damon_aggregated(t, r, damon_nr_regions(t));
546 r->last_nr_accesses = r->nr_accesses;
552 static void damon_split_region_at(struct damon_ctx *ctx,
553 struct damon_target *t, struct damon_region *r,
556 static bool __damos_valid_target(struct damon_region *r, struct damos *s)
560 sz = r->ar.end - r->ar.start;
561 return s->min_sz_region <= sz && sz <= s->max_sz_region &&
562 s->min_nr_accesses <= r->nr_accesses &&
563 r->nr_accesses <= s->max_nr_accesses &&
564 s->min_age_region <= r->age && r->age <= s->max_age_region;
567 static bool damos_valid_target(struct damon_ctx *c, struct damon_target *t,
568 struct damon_region *r, struct damos *s)
570 bool ret = __damos_valid_target(r, s);
572 if (!ret || !s->quota.esz || !c->primitive.get_scheme_score)
575 return c->primitive.get_scheme_score(c, t, r, s) >= s->quota.min_score;
578 static void damon_do_apply_schemes(struct damon_ctx *c,
579 struct damon_target *t,
580 struct damon_region *r)
584 damon_for_each_scheme(s, c) {
585 struct damos_quota *quota = &s->quota;
586 unsigned long sz = r->ar.end - r->ar.start;
587 struct timespec64 begin, end;
589 if (!s->wmarks.activated)
592 /* Check the quota */
593 if (quota->esz && quota->charged_sz >= quota->esz)
596 /* Skip previously charged regions */
597 if (quota->charge_target_from) {
598 if (t != quota->charge_target_from)
600 if (r == damon_last_region(t)) {
601 quota->charge_target_from = NULL;
602 quota->charge_addr_from = 0;
605 if (quota->charge_addr_from &&
606 r->ar.end <= quota->charge_addr_from)
609 if (quota->charge_addr_from && r->ar.start <
610 quota->charge_addr_from) {
611 sz = ALIGN_DOWN(quota->charge_addr_from -
612 r->ar.start, DAMON_MIN_REGION);
614 if (r->ar.end - r->ar.start <=
617 sz = DAMON_MIN_REGION;
619 damon_split_region_at(c, t, r, sz);
620 r = damon_next_region(r);
621 sz = r->ar.end - r->ar.start;
623 quota->charge_target_from = NULL;
624 quota->charge_addr_from = 0;
627 if (!damos_valid_target(c, t, r, s))
630 /* Apply the scheme */
631 if (c->primitive.apply_scheme) {
633 quota->charged_sz + sz > quota->esz) {
634 sz = ALIGN_DOWN(quota->esz - quota->charged_sz,
638 damon_split_region_at(c, t, r, sz);
640 ktime_get_coarse_ts64(&begin);
641 c->primitive.apply_scheme(c, t, r, s);
642 ktime_get_coarse_ts64(&end);
643 quota->total_charged_ns += timespec64_to_ns(&end) -
644 timespec64_to_ns(&begin);
645 quota->charged_sz += sz;
646 if (quota->esz && quota->charged_sz >= quota->esz) {
647 quota->charge_target_from = t;
648 quota->charge_addr_from = r->ar.end + 1;
651 if (s->action != DAMOS_STAT)
660 /* Shouldn't be called if quota->ms and quota->sz are zero */
661 static void damos_set_effective_quota(struct damos_quota *quota)
663 unsigned long throughput;
667 quota->esz = quota->sz;
671 if (quota->total_charged_ns)
672 throughput = quota->total_charged_sz * 1000000 /
673 quota->total_charged_ns;
675 throughput = PAGE_SIZE * 1024;
676 esz = throughput * quota->ms;
678 if (quota->sz && quota->sz < esz)
683 static void kdamond_apply_schemes(struct damon_ctx *c)
685 struct damon_target *t;
686 struct damon_region *r, *next_r;
689 damon_for_each_scheme(s, c) {
690 struct damos_quota *quota = &s->quota;
691 unsigned long cumulated_sz;
692 unsigned int score, max_score = 0;
694 if (!s->wmarks.activated)
697 if (!quota->ms && !quota->sz)
700 /* New charge window starts */
701 if (time_after_eq(jiffies, quota->charged_from +
703 quota->reset_interval))) {
704 quota->total_charged_sz += quota->charged_sz;
705 quota->charged_from = jiffies;
706 quota->charged_sz = 0;
707 damos_set_effective_quota(quota);
710 if (!c->primitive.get_scheme_score)
713 /* Fill up the score histogram */
714 memset(quota->histogram, 0, sizeof(quota->histogram));
715 damon_for_each_target(t, c) {
716 damon_for_each_region(r, t) {
717 if (!__damos_valid_target(r, s))
719 score = c->primitive.get_scheme_score(
721 quota->histogram[score] +=
722 r->ar.end - r->ar.start;
723 if (score > max_score)
728 /* Set the min score limit */
729 for (cumulated_sz = 0, score = max_score; ; score--) {
730 cumulated_sz += quota->histogram[score];
731 if (cumulated_sz >= quota->esz || !score)
734 quota->min_score = score;
737 damon_for_each_target(t, c) {
738 damon_for_each_region_safe(r, next_r, t)
739 damon_do_apply_schemes(c, t, r);
743 #define sz_damon_region(r) (r->ar.end - r->ar.start)
746 * Merge two adjacent regions into one region
748 static void damon_merge_two_regions(struct damon_target *t,
749 struct damon_region *l, struct damon_region *r)
751 unsigned long sz_l = sz_damon_region(l), sz_r = sz_damon_region(r);
753 l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) /
755 l->age = (l->age * sz_l + r->age * sz_r) / (sz_l + sz_r);
756 l->ar.end = r->ar.end;
757 damon_destroy_region(r, t);
760 #define diff_of(a, b) (a > b ? a - b : b - a)
763 * Merge adjacent regions having similar access frequencies
765 * t target affected by this merge operation
766 * thres '->nr_accesses' diff threshold for the merge
767 * sz_limit size upper limit of each region
769 static void damon_merge_regions_of(struct damon_target *t, unsigned int thres,
770 unsigned long sz_limit)
772 struct damon_region *r, *prev = NULL, *next;
774 damon_for_each_region_safe(r, next, t) {
775 if (diff_of(r->nr_accesses, r->last_nr_accesses) > thres)
780 if (prev && prev->ar.end == r->ar.start &&
781 diff_of(prev->nr_accesses, r->nr_accesses) <= thres &&
782 sz_damon_region(prev) + sz_damon_region(r) <= sz_limit)
783 damon_merge_two_regions(t, prev, r);
790 * Merge adjacent regions having similar access frequencies
792 * threshold '->nr_accesses' diff threshold for the merge
793 * sz_limit size upper limit of each region
795 * This function merges monitoring target regions which are adjacent and their
796 * access frequencies are similar. This is for minimizing the monitoring
797 * overhead under the dynamically changeable access pattern. If a merge was
798 * unnecessarily made, later 'kdamond_split_regions()' will revert it.
800 static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
801 unsigned long sz_limit)
803 struct damon_target *t;
805 damon_for_each_target(t, c)
806 damon_merge_regions_of(t, threshold, sz_limit);
810 * Split a region in two
812 * r the region to be split
813 * sz_r size of the first sub-region that will be made
815 static void damon_split_region_at(struct damon_ctx *ctx,
816 struct damon_target *t, struct damon_region *r,
819 struct damon_region *new;
821 new = damon_new_region(r->ar.start + sz_r, r->ar.end);
825 r->ar.end = new->ar.start;
828 new->last_nr_accesses = r->last_nr_accesses;
830 damon_insert_region(new, r, damon_next_region(r), t);
833 /* Split every region in the given target into 'nr_subs' regions */
834 static void damon_split_regions_of(struct damon_ctx *ctx,
835 struct damon_target *t, int nr_subs)
837 struct damon_region *r, *next;
838 unsigned long sz_region, sz_sub = 0;
841 damon_for_each_region_safe(r, next, t) {
842 sz_region = r->ar.end - r->ar.start;
844 for (i = 0; i < nr_subs - 1 &&
845 sz_region > 2 * DAMON_MIN_REGION; i++) {
847 * Randomly select size of left sub-region to be at
848 * least 10 percent and at most 90% of original region
850 sz_sub = ALIGN_DOWN(damon_rand(1, 10) *
851 sz_region / 10, DAMON_MIN_REGION);
852 /* Do not allow blank region */
853 if (sz_sub == 0 || sz_sub >= sz_region)
856 damon_split_region_at(ctx, t, r, sz_sub);
863 * Split every target region into randomly-sized small regions
865 * This function splits every target region into random-sized small regions if
866 * current total number of the regions is equal or smaller than half of the
867 * user-specified maximum number of regions. This is for maximizing the
868 * monitoring accuracy under the dynamically changeable access patterns. If a
869 * split was unnecessarily made, later 'kdamond_merge_regions()' will revert
872 static void kdamond_split_regions(struct damon_ctx *ctx)
874 struct damon_target *t;
875 unsigned int nr_regions = 0;
876 static unsigned int last_nr_regions;
877 int nr_subregions = 2;
879 damon_for_each_target(t, ctx)
880 nr_regions += damon_nr_regions(t);
882 if (nr_regions > ctx->max_nr_regions / 2)
885 /* Maybe the middle of the region has different access frequency */
886 if (last_nr_regions == nr_regions &&
887 nr_regions < ctx->max_nr_regions / 3)
890 damon_for_each_target(t, ctx)
891 damon_split_regions_of(ctx, t, nr_subregions);
893 last_nr_regions = nr_regions;
897 * Check whether it is time to check and apply the target monitoring regions
899 * Returns true if it is.
901 static bool kdamond_need_update_primitive(struct damon_ctx *ctx)
903 return damon_check_reset_time_interval(&ctx->last_primitive_update,
904 ctx->primitive_update_interval);
908 * Check whether current monitoring should be stopped
910 * The monitoring is stopped when either the user requested to stop, or all
911 * monitoring targets are invalid.
913 * Returns true if need to stop current monitoring.
915 static bool kdamond_need_stop(struct damon_ctx *ctx)
917 struct damon_target *t;
919 if (kthread_should_stop())
922 if (!ctx->primitive.target_valid)
925 damon_for_each_target(t, ctx) {
926 if (ctx->primitive.target_valid(t))
933 static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
938 case DAMOS_WMARK_FREE_MEM_RATE:
940 return i.freeram * 1000 / i.totalram;
948 * Returns zero if the scheme is active. Else, returns time to wait for next
949 * watermark check in micro-seconds.
951 static unsigned long damos_wmark_wait_us(struct damos *scheme)
953 unsigned long metric;
955 if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
958 metric = damos_wmark_metric_value(scheme->wmarks.metric);
959 /* higher than high watermark or lower than low watermark */
960 if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
961 if (scheme->wmarks.activated)
962 pr_debug("deactivate a scheme (%d) for %s wmark\n",
964 metric > scheme->wmarks.high ?
966 scheme->wmarks.activated = false;
967 return scheme->wmarks.interval;
970 /* inactive and higher than middle watermark */
971 if ((scheme->wmarks.high >= metric && metric >= scheme->wmarks.mid) &&
972 !scheme->wmarks.activated)
973 return scheme->wmarks.interval;
975 if (!scheme->wmarks.activated)
976 pr_debug("activate a scheme (%d)\n", scheme->action);
977 scheme->wmarks.activated = true;
981 static void kdamond_usleep(unsigned long usecs)
983 if (usecs > 100 * 1000)
984 schedule_timeout_interruptible(usecs_to_jiffies(usecs));
986 usleep_range(usecs, usecs + 1);
989 /* Returns negative error code if it's not activated but should return */
990 static int kdamond_wait_activation(struct damon_ctx *ctx)
993 unsigned long wait_time;
994 unsigned long min_wait_time = 0;
996 while (!kdamond_need_stop(ctx)) {
997 damon_for_each_scheme(s, ctx) {
998 wait_time = damos_wmark_wait_us(s);
999 if (!min_wait_time || wait_time < min_wait_time)
1000 min_wait_time = wait_time;
1005 kdamond_usleep(min_wait_time);
1011 * The monitoring daemon that runs as a kernel thread
1013 static int kdamond_fn(void *data)
1015 struct damon_ctx *ctx = (struct damon_ctx *)data;
1016 struct damon_target *t;
1017 struct damon_region *r, *next;
1018 unsigned int max_nr_accesses = 0;
1019 unsigned long sz_limit = 0;
1022 pr_debug("kdamond (%d) starts\n", current->pid);
1024 if (ctx->primitive.init)
1025 ctx->primitive.init(ctx);
1026 if (ctx->callback.before_start && ctx->callback.before_start(ctx))
1029 sz_limit = damon_region_sz_limit(ctx);
1031 while (!kdamond_need_stop(ctx) && !done) {
1032 if (kdamond_wait_activation(ctx))
1035 if (ctx->primitive.prepare_access_checks)
1036 ctx->primitive.prepare_access_checks(ctx);
1037 if (ctx->callback.after_sampling &&
1038 ctx->callback.after_sampling(ctx))
1041 usleep_range(ctx->sample_interval, ctx->sample_interval + 1);
1043 if (ctx->primitive.check_accesses)
1044 max_nr_accesses = ctx->primitive.check_accesses(ctx);
1046 if (kdamond_aggregate_interval_passed(ctx)) {
1047 kdamond_merge_regions(ctx,
1048 max_nr_accesses / 10,
1050 if (ctx->callback.after_aggregation &&
1051 ctx->callback.after_aggregation(ctx))
1053 kdamond_apply_schemes(ctx);
1054 kdamond_reset_aggregated(ctx);
1055 kdamond_split_regions(ctx);
1056 if (ctx->primitive.reset_aggregated)
1057 ctx->primitive.reset_aggregated(ctx);
1060 if (kdamond_need_update_primitive(ctx)) {
1061 if (ctx->primitive.update)
1062 ctx->primitive.update(ctx);
1063 sz_limit = damon_region_sz_limit(ctx);
1066 damon_for_each_target(t, ctx) {
1067 damon_for_each_region_safe(r, next, t)
1068 damon_destroy_region(r, t);
1071 if (ctx->callback.before_terminate)
1072 ctx->callback.before_terminate(ctx);
1073 if (ctx->primitive.cleanup)
1074 ctx->primitive.cleanup(ctx);
1076 pr_debug("kdamond (%d) finishes\n", current->pid);
1077 mutex_lock(&ctx->kdamond_lock);
1078 ctx->kdamond = NULL;
1079 mutex_unlock(&ctx->kdamond_lock);
1081 mutex_lock(&damon_lock);
1083 mutex_unlock(&damon_lock);
1088 #include "core-test.h"