1 // SPDX-License-Identifier: GPL-2.0
3 * DAMON sysfs Interface
8 #include <linux/damon.h>
9 #include <linux/kobject.h>
10 #include <linux/pid.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
14 static DEFINE_MUTEX(damon_sysfs_lock);
17 * unsigned long range directory
20 struct damon_sysfs_ul_range {
26 static struct damon_sysfs_ul_range *damon_sysfs_ul_range_alloc(
30 struct damon_sysfs_ul_range *range = kmalloc(sizeof(*range),
35 range->kobj = (struct kobject){};
42 static ssize_t min_show(struct kobject *kobj, struct kobj_attribute *attr,
45 struct damon_sysfs_ul_range *range = container_of(kobj,
46 struct damon_sysfs_ul_range, kobj);
48 return sysfs_emit(buf, "%lu\n", range->min);
51 static ssize_t min_store(struct kobject *kobj, struct kobj_attribute *attr,
52 const char *buf, size_t count)
54 struct damon_sysfs_ul_range *range = container_of(kobj,
55 struct damon_sysfs_ul_range, kobj);
59 err = kstrtoul(buf, 0, &min);
67 static ssize_t max_show(struct kobject *kobj, struct kobj_attribute *attr,
70 struct damon_sysfs_ul_range *range = container_of(kobj,
71 struct damon_sysfs_ul_range, kobj);
73 return sysfs_emit(buf, "%lu\n", range->max);
76 static ssize_t max_store(struct kobject *kobj, struct kobj_attribute *attr,
77 const char *buf, size_t count)
79 struct damon_sysfs_ul_range *range = container_of(kobj,
80 struct damon_sysfs_ul_range, kobj);
84 err = kstrtoul(buf, 0, &max);
92 static void damon_sysfs_ul_range_release(struct kobject *kobj)
94 kfree(container_of(kobj, struct damon_sysfs_ul_range, kobj));
97 static struct kobj_attribute damon_sysfs_ul_range_min_attr =
98 __ATTR_RW_MODE(min, 0600);
100 static struct kobj_attribute damon_sysfs_ul_range_max_attr =
101 __ATTR_RW_MODE(max, 0600);
103 static struct attribute *damon_sysfs_ul_range_attrs[] = {
104 &damon_sysfs_ul_range_min_attr.attr,
105 &damon_sysfs_ul_range_max_attr.attr,
108 ATTRIBUTE_GROUPS(damon_sysfs_ul_range);
110 static struct kobj_type damon_sysfs_ul_range_ktype = {
111 .release = damon_sysfs_ul_range_release,
112 .sysfs_ops = &kobj_sysfs_ops,
113 .default_groups = damon_sysfs_ul_range_groups,
117 * schemes/stats directory
120 struct damon_sysfs_stats {
122 unsigned long nr_tried;
123 unsigned long sz_tried;
124 unsigned long nr_applied;
125 unsigned long sz_applied;
126 unsigned long qt_exceeds;
129 static struct damon_sysfs_stats *damon_sysfs_stats_alloc(void)
131 return kzalloc(sizeof(struct damon_sysfs_stats), GFP_KERNEL);
134 static ssize_t nr_tried_show(struct kobject *kobj, struct kobj_attribute *attr,
137 struct damon_sysfs_stats *stats = container_of(kobj,
138 struct damon_sysfs_stats, kobj);
140 return sysfs_emit(buf, "%lu\n", stats->nr_tried);
143 static ssize_t sz_tried_show(struct kobject *kobj, struct kobj_attribute *attr,
146 struct damon_sysfs_stats *stats = container_of(kobj,
147 struct damon_sysfs_stats, kobj);
149 return sysfs_emit(buf, "%lu\n", stats->sz_tried);
152 static ssize_t nr_applied_show(struct kobject *kobj,
153 struct kobj_attribute *attr, char *buf)
155 struct damon_sysfs_stats *stats = container_of(kobj,
156 struct damon_sysfs_stats, kobj);
158 return sysfs_emit(buf, "%lu\n", stats->nr_applied);
161 static ssize_t sz_applied_show(struct kobject *kobj,
162 struct kobj_attribute *attr, char *buf)
164 struct damon_sysfs_stats *stats = container_of(kobj,
165 struct damon_sysfs_stats, kobj);
167 return sysfs_emit(buf, "%lu\n", stats->sz_applied);
170 static ssize_t qt_exceeds_show(struct kobject *kobj,
171 struct kobj_attribute *attr, char *buf)
173 struct damon_sysfs_stats *stats = container_of(kobj,
174 struct damon_sysfs_stats, kobj);
176 return sysfs_emit(buf, "%lu\n", stats->qt_exceeds);
179 static void damon_sysfs_stats_release(struct kobject *kobj)
181 kfree(container_of(kobj, struct damon_sysfs_stats, kobj));
184 static struct kobj_attribute damon_sysfs_stats_nr_tried_attr =
185 __ATTR_RO_MODE(nr_tried, 0400);
187 static struct kobj_attribute damon_sysfs_stats_sz_tried_attr =
188 __ATTR_RO_MODE(sz_tried, 0400);
190 static struct kobj_attribute damon_sysfs_stats_nr_applied_attr =
191 __ATTR_RO_MODE(nr_applied, 0400);
193 static struct kobj_attribute damon_sysfs_stats_sz_applied_attr =
194 __ATTR_RO_MODE(sz_applied, 0400);
196 static struct kobj_attribute damon_sysfs_stats_qt_exceeds_attr =
197 __ATTR_RO_MODE(qt_exceeds, 0400);
199 static struct attribute *damon_sysfs_stats_attrs[] = {
200 &damon_sysfs_stats_nr_tried_attr.attr,
201 &damon_sysfs_stats_sz_tried_attr.attr,
202 &damon_sysfs_stats_nr_applied_attr.attr,
203 &damon_sysfs_stats_sz_applied_attr.attr,
204 &damon_sysfs_stats_qt_exceeds_attr.attr,
207 ATTRIBUTE_GROUPS(damon_sysfs_stats);
209 static struct kobj_type damon_sysfs_stats_ktype = {
210 .release = damon_sysfs_stats_release,
211 .sysfs_ops = &kobj_sysfs_ops,
212 .default_groups = damon_sysfs_stats_groups,
216 * watermarks directory
219 struct damon_sysfs_watermarks {
221 enum damos_wmark_metric metric;
222 unsigned long interval_us;
228 static struct damon_sysfs_watermarks *damon_sysfs_watermarks_alloc(
229 enum damos_wmark_metric metric, unsigned long interval_us,
230 unsigned long high, unsigned long mid, unsigned long low)
232 struct damon_sysfs_watermarks *watermarks = kmalloc(
233 sizeof(*watermarks), GFP_KERNEL);
237 watermarks->kobj = (struct kobject){};
238 watermarks->metric = metric;
239 watermarks->interval_us = interval_us;
240 watermarks->high = high;
241 watermarks->mid = mid;
242 watermarks->low = low;
246 /* Should match with enum damos_wmark_metric */
247 static const char * const damon_sysfs_wmark_metric_strs[] = {
252 static ssize_t metric_show(struct kobject *kobj, struct kobj_attribute *attr,
255 struct damon_sysfs_watermarks *watermarks = container_of(kobj,
256 struct damon_sysfs_watermarks, kobj);
258 return sysfs_emit(buf, "%s\n",
259 damon_sysfs_wmark_metric_strs[watermarks->metric]);
262 static ssize_t metric_store(struct kobject *kobj, struct kobj_attribute *attr,
263 const char *buf, size_t count)
265 struct damon_sysfs_watermarks *watermarks = container_of(kobj,
266 struct damon_sysfs_watermarks, kobj);
267 enum damos_wmark_metric metric;
269 for (metric = 0; metric < NR_DAMOS_WMARK_METRICS; metric++) {
270 if (sysfs_streq(buf, damon_sysfs_wmark_metric_strs[metric])) {
271 watermarks->metric = metric;
278 static ssize_t interval_us_show(struct kobject *kobj,
279 struct kobj_attribute *attr, char *buf)
281 struct damon_sysfs_watermarks *watermarks = container_of(kobj,
282 struct damon_sysfs_watermarks, kobj);
284 return sysfs_emit(buf, "%lu\n", watermarks->interval_us);
287 static ssize_t interval_us_store(struct kobject *kobj,
288 struct kobj_attribute *attr, const char *buf, size_t count)
290 struct damon_sysfs_watermarks *watermarks = container_of(kobj,
291 struct damon_sysfs_watermarks, kobj);
292 int err = kstrtoul(buf, 0, &watermarks->interval_us);
299 static ssize_t high_show(struct kobject *kobj,
300 struct kobj_attribute *attr, char *buf)
302 struct damon_sysfs_watermarks *watermarks = container_of(kobj,
303 struct damon_sysfs_watermarks, kobj);
305 return sysfs_emit(buf, "%lu\n", watermarks->high);
308 static ssize_t high_store(struct kobject *kobj,
309 struct kobj_attribute *attr, const char *buf, size_t count)
311 struct damon_sysfs_watermarks *watermarks = container_of(kobj,
312 struct damon_sysfs_watermarks, kobj);
313 int err = kstrtoul(buf, 0, &watermarks->high);
320 static ssize_t mid_show(struct kobject *kobj,
321 struct kobj_attribute *attr, char *buf)
323 struct damon_sysfs_watermarks *watermarks = container_of(kobj,
324 struct damon_sysfs_watermarks, kobj);
326 return sysfs_emit(buf, "%lu\n", watermarks->mid);
329 static ssize_t mid_store(struct kobject *kobj,
330 struct kobj_attribute *attr, const char *buf, size_t count)
332 struct damon_sysfs_watermarks *watermarks = container_of(kobj,
333 struct damon_sysfs_watermarks, kobj);
334 int err = kstrtoul(buf, 0, &watermarks->mid);
341 static ssize_t low_show(struct kobject *kobj,
342 struct kobj_attribute *attr, char *buf)
344 struct damon_sysfs_watermarks *watermarks = container_of(kobj,
345 struct damon_sysfs_watermarks, kobj);
347 return sysfs_emit(buf, "%lu\n", watermarks->low);
350 static ssize_t low_store(struct kobject *kobj,
351 struct kobj_attribute *attr, const char *buf, size_t count)
353 struct damon_sysfs_watermarks *watermarks = container_of(kobj,
354 struct damon_sysfs_watermarks, kobj);
355 int err = kstrtoul(buf, 0, &watermarks->low);
362 static void damon_sysfs_watermarks_release(struct kobject *kobj)
364 kfree(container_of(kobj, struct damon_sysfs_watermarks, kobj));
367 static struct kobj_attribute damon_sysfs_watermarks_metric_attr =
368 __ATTR_RW_MODE(metric, 0600);
370 static struct kobj_attribute damon_sysfs_watermarks_interval_us_attr =
371 __ATTR_RW_MODE(interval_us, 0600);
373 static struct kobj_attribute damon_sysfs_watermarks_high_attr =
374 __ATTR_RW_MODE(high, 0600);
376 static struct kobj_attribute damon_sysfs_watermarks_mid_attr =
377 __ATTR_RW_MODE(mid, 0600);
379 static struct kobj_attribute damon_sysfs_watermarks_low_attr =
380 __ATTR_RW_MODE(low, 0600);
382 static struct attribute *damon_sysfs_watermarks_attrs[] = {
383 &damon_sysfs_watermarks_metric_attr.attr,
384 &damon_sysfs_watermarks_interval_us_attr.attr,
385 &damon_sysfs_watermarks_high_attr.attr,
386 &damon_sysfs_watermarks_mid_attr.attr,
387 &damon_sysfs_watermarks_low_attr.attr,
390 ATTRIBUTE_GROUPS(damon_sysfs_watermarks);
392 static struct kobj_type damon_sysfs_watermarks_ktype = {
393 .release = damon_sysfs_watermarks_release,
394 .sysfs_ops = &kobj_sysfs_ops,
395 .default_groups = damon_sysfs_watermarks_groups,
399 * scheme/weights directory
402 struct damon_sysfs_weights {
405 unsigned int nr_accesses;
409 static struct damon_sysfs_weights *damon_sysfs_weights_alloc(unsigned int sz,
410 unsigned int nr_accesses, unsigned int age)
412 struct damon_sysfs_weights *weights = kmalloc(sizeof(*weights),
417 weights->kobj = (struct kobject){};
419 weights->nr_accesses = nr_accesses;
424 static ssize_t sz_permil_show(struct kobject *kobj,
425 struct kobj_attribute *attr, char *buf)
427 struct damon_sysfs_weights *weights = container_of(kobj,
428 struct damon_sysfs_weights, kobj);
430 return sysfs_emit(buf, "%u\n", weights->sz);
433 static ssize_t sz_permil_store(struct kobject *kobj,
434 struct kobj_attribute *attr, const char *buf, size_t count)
436 struct damon_sysfs_weights *weights = container_of(kobj,
437 struct damon_sysfs_weights, kobj);
438 int err = kstrtouint(buf, 0, &weights->sz);
445 static ssize_t nr_accesses_permil_show(struct kobject *kobj,
446 struct kobj_attribute *attr, char *buf)
448 struct damon_sysfs_weights *weights = container_of(kobj,
449 struct damon_sysfs_weights, kobj);
451 return sysfs_emit(buf, "%u\n", weights->nr_accesses);
454 static ssize_t nr_accesses_permil_store(struct kobject *kobj,
455 struct kobj_attribute *attr, const char *buf, size_t count)
457 struct damon_sysfs_weights *weights = container_of(kobj,
458 struct damon_sysfs_weights, kobj);
459 int err = kstrtouint(buf, 0, &weights->nr_accesses);
466 static ssize_t age_permil_show(struct kobject *kobj,
467 struct kobj_attribute *attr, char *buf)
469 struct damon_sysfs_weights *weights = container_of(kobj,
470 struct damon_sysfs_weights, kobj);
472 return sysfs_emit(buf, "%u\n", weights->age);
475 static ssize_t age_permil_store(struct kobject *kobj,
476 struct kobj_attribute *attr, const char *buf, size_t count)
478 struct damon_sysfs_weights *weights = container_of(kobj,
479 struct damon_sysfs_weights, kobj);
480 int err = kstrtouint(buf, 0, &weights->age);
487 static void damon_sysfs_weights_release(struct kobject *kobj)
489 kfree(container_of(kobj, struct damon_sysfs_weights, kobj));
492 static struct kobj_attribute damon_sysfs_weights_sz_attr =
493 __ATTR_RW_MODE(sz_permil, 0600);
495 static struct kobj_attribute damon_sysfs_weights_nr_accesses_attr =
496 __ATTR_RW_MODE(nr_accesses_permil, 0600);
498 static struct kobj_attribute damon_sysfs_weights_age_attr =
499 __ATTR_RW_MODE(age_permil, 0600);
501 static struct attribute *damon_sysfs_weights_attrs[] = {
502 &damon_sysfs_weights_sz_attr.attr,
503 &damon_sysfs_weights_nr_accesses_attr.attr,
504 &damon_sysfs_weights_age_attr.attr,
507 ATTRIBUTE_GROUPS(damon_sysfs_weights);
509 static struct kobj_type damon_sysfs_weights_ktype = {
510 .release = damon_sysfs_weights_release,
511 .sysfs_ops = &kobj_sysfs_ops,
512 .default_groups = damon_sysfs_weights_groups,
519 struct damon_sysfs_quotas {
521 struct damon_sysfs_weights *weights;
524 unsigned long reset_interval_ms;
527 static struct damon_sysfs_quotas *damon_sysfs_quotas_alloc(void)
529 return kzalloc(sizeof(struct damon_sysfs_quotas), GFP_KERNEL);
532 static int damon_sysfs_quotas_add_dirs(struct damon_sysfs_quotas *quotas)
534 struct damon_sysfs_weights *weights;
537 weights = damon_sysfs_weights_alloc(0, 0, 0);
541 err = kobject_init_and_add(&weights->kobj, &damon_sysfs_weights_ktype,
542 "as->kobj, "weights");
544 kobject_put(&weights->kobj);
546 quotas->weights = weights;
550 static void damon_sysfs_quotas_rm_dirs(struct damon_sysfs_quotas *quotas)
552 kobject_put("as->weights->kobj);
555 static ssize_t ms_show(struct kobject *kobj, struct kobj_attribute *attr,
558 struct damon_sysfs_quotas *quotas = container_of(kobj,
559 struct damon_sysfs_quotas, kobj);
561 return sysfs_emit(buf, "%lu\n", quotas->ms);
564 static ssize_t ms_store(struct kobject *kobj, struct kobj_attribute *attr,
565 const char *buf, size_t count)
567 struct damon_sysfs_quotas *quotas = container_of(kobj,
568 struct damon_sysfs_quotas, kobj);
569 int err = kstrtoul(buf, 0, "as->ms);
576 static ssize_t bytes_show(struct kobject *kobj, struct kobj_attribute *attr,
579 struct damon_sysfs_quotas *quotas = container_of(kobj,
580 struct damon_sysfs_quotas, kobj);
582 return sysfs_emit(buf, "%lu\n", quotas->sz);
585 static ssize_t bytes_store(struct kobject *kobj,
586 struct kobj_attribute *attr, const char *buf, size_t count)
588 struct damon_sysfs_quotas *quotas = container_of(kobj,
589 struct damon_sysfs_quotas, kobj);
590 int err = kstrtoul(buf, 0, "as->sz);
597 static ssize_t reset_interval_ms_show(struct kobject *kobj,
598 struct kobj_attribute *attr, char *buf)
600 struct damon_sysfs_quotas *quotas = container_of(kobj,
601 struct damon_sysfs_quotas, kobj);
603 return sysfs_emit(buf, "%lu\n", quotas->reset_interval_ms);
606 static ssize_t reset_interval_ms_store(struct kobject *kobj,
607 struct kobj_attribute *attr, const char *buf, size_t count)
609 struct damon_sysfs_quotas *quotas = container_of(kobj,
610 struct damon_sysfs_quotas, kobj);
611 int err = kstrtoul(buf, 0, "as->reset_interval_ms);
618 static void damon_sysfs_quotas_release(struct kobject *kobj)
620 kfree(container_of(kobj, struct damon_sysfs_quotas, kobj));
623 static struct kobj_attribute damon_sysfs_quotas_ms_attr =
624 __ATTR_RW_MODE(ms, 0600);
626 static struct kobj_attribute damon_sysfs_quotas_sz_attr =
627 __ATTR_RW_MODE(bytes, 0600);
629 static struct kobj_attribute damon_sysfs_quotas_reset_interval_ms_attr =
630 __ATTR_RW_MODE(reset_interval_ms, 0600);
632 static struct attribute *damon_sysfs_quotas_attrs[] = {
633 &damon_sysfs_quotas_ms_attr.attr,
634 &damon_sysfs_quotas_sz_attr.attr,
635 &damon_sysfs_quotas_reset_interval_ms_attr.attr,
638 ATTRIBUTE_GROUPS(damon_sysfs_quotas);
640 static struct kobj_type damon_sysfs_quotas_ktype = {
641 .release = damon_sysfs_quotas_release,
642 .sysfs_ops = &kobj_sysfs_ops,
643 .default_groups = damon_sysfs_quotas_groups,
647 * access_pattern directory
650 struct damon_sysfs_access_pattern {
652 struct damon_sysfs_ul_range *sz;
653 struct damon_sysfs_ul_range *nr_accesses;
654 struct damon_sysfs_ul_range *age;
658 struct damon_sysfs_access_pattern *damon_sysfs_access_pattern_alloc(void)
660 struct damon_sysfs_access_pattern *access_pattern =
661 kmalloc(sizeof(*access_pattern), GFP_KERNEL);
665 access_pattern->kobj = (struct kobject){};
666 return access_pattern;
669 static int damon_sysfs_access_pattern_add_range_dir(
670 struct damon_sysfs_access_pattern *access_pattern,
671 struct damon_sysfs_ul_range **range_dir_ptr,
674 struct damon_sysfs_ul_range *range = damon_sysfs_ul_range_alloc(0, 0);
679 err = kobject_init_and_add(&range->kobj, &damon_sysfs_ul_range_ktype,
680 &access_pattern->kobj, name);
682 kobject_put(&range->kobj);
684 *range_dir_ptr = range;
688 static int damon_sysfs_access_pattern_add_dirs(
689 struct damon_sysfs_access_pattern *access_pattern)
693 err = damon_sysfs_access_pattern_add_range_dir(access_pattern,
694 &access_pattern->sz, "sz");
698 err = damon_sysfs_access_pattern_add_range_dir(access_pattern,
699 &access_pattern->nr_accesses, "nr_accesses");
701 goto put_nr_accesses_sz_out;
703 err = damon_sysfs_access_pattern_add_range_dir(access_pattern,
704 &access_pattern->age, "age");
706 goto put_age_nr_accesses_sz_out;
709 put_age_nr_accesses_sz_out:
710 kobject_put(&access_pattern->age->kobj);
711 access_pattern->age = NULL;
712 put_nr_accesses_sz_out:
713 kobject_put(&access_pattern->nr_accesses->kobj);
714 access_pattern->nr_accesses = NULL;
716 kobject_put(&access_pattern->sz->kobj);
717 access_pattern->sz = NULL;
721 static void damon_sysfs_access_pattern_rm_dirs(
722 struct damon_sysfs_access_pattern *access_pattern)
724 kobject_put(&access_pattern->sz->kobj);
725 kobject_put(&access_pattern->nr_accesses->kobj);
726 kobject_put(&access_pattern->age->kobj);
729 static void damon_sysfs_access_pattern_release(struct kobject *kobj)
731 kfree(container_of(kobj, struct damon_sysfs_access_pattern, kobj));
734 static struct attribute *damon_sysfs_access_pattern_attrs[] = {
737 ATTRIBUTE_GROUPS(damon_sysfs_access_pattern);
739 static struct kobj_type damon_sysfs_access_pattern_ktype = {
740 .release = damon_sysfs_access_pattern_release,
741 .sysfs_ops = &kobj_sysfs_ops,
742 .default_groups = damon_sysfs_access_pattern_groups,
749 struct damon_sysfs_scheme {
751 enum damos_action action;
752 struct damon_sysfs_access_pattern *access_pattern;
753 struct damon_sysfs_quotas *quotas;
754 struct damon_sysfs_watermarks *watermarks;
755 struct damon_sysfs_stats *stats;
758 /* This should match with enum damos_action */
759 static const char * const damon_sysfs_damos_action_strs[] = {
770 static struct damon_sysfs_scheme *damon_sysfs_scheme_alloc(
771 enum damos_action action)
773 struct damon_sysfs_scheme *scheme = kmalloc(sizeof(*scheme),
778 scheme->kobj = (struct kobject){};
779 scheme->action = action;
783 static int damon_sysfs_scheme_set_access_pattern(
784 struct damon_sysfs_scheme *scheme)
786 struct damon_sysfs_access_pattern *access_pattern;
789 access_pattern = damon_sysfs_access_pattern_alloc();
792 err = kobject_init_and_add(&access_pattern->kobj,
793 &damon_sysfs_access_pattern_ktype, &scheme->kobj,
797 err = damon_sysfs_access_pattern_add_dirs(access_pattern);
800 scheme->access_pattern = access_pattern;
804 kobject_put(&access_pattern->kobj);
808 static int damon_sysfs_scheme_set_quotas(struct damon_sysfs_scheme *scheme)
810 struct damon_sysfs_quotas *quotas = damon_sysfs_quotas_alloc();
815 err = kobject_init_and_add("as->kobj, &damon_sysfs_quotas_ktype,
816 &scheme->kobj, "quotas");
819 err = damon_sysfs_quotas_add_dirs(quotas);
822 scheme->quotas = quotas;
826 kobject_put("as->kobj);
830 static int damon_sysfs_scheme_set_watermarks(struct damon_sysfs_scheme *scheme)
832 struct damon_sysfs_watermarks *watermarks =
833 damon_sysfs_watermarks_alloc(DAMOS_WMARK_NONE, 0, 0, 0, 0);
838 err = kobject_init_and_add(&watermarks->kobj,
839 &damon_sysfs_watermarks_ktype, &scheme->kobj,
842 kobject_put(&watermarks->kobj);
844 scheme->watermarks = watermarks;
848 static int damon_sysfs_scheme_set_stats(struct damon_sysfs_scheme *scheme)
850 struct damon_sysfs_stats *stats = damon_sysfs_stats_alloc();
855 err = kobject_init_and_add(&stats->kobj, &damon_sysfs_stats_ktype,
856 &scheme->kobj, "stats");
858 kobject_put(&stats->kobj);
860 scheme->stats = stats;
864 static int damon_sysfs_scheme_add_dirs(struct damon_sysfs_scheme *scheme)
868 err = damon_sysfs_scheme_set_access_pattern(scheme);
871 err = damon_sysfs_scheme_set_quotas(scheme);
873 goto put_access_pattern_out;
874 err = damon_sysfs_scheme_set_watermarks(scheme);
876 goto put_quotas_access_pattern_out;
877 err = damon_sysfs_scheme_set_stats(scheme);
879 goto put_watermarks_quotas_access_pattern_out;
882 put_watermarks_quotas_access_pattern_out:
883 kobject_put(&scheme->watermarks->kobj);
884 scheme->watermarks = NULL;
885 put_quotas_access_pattern_out:
886 kobject_put(&scheme->quotas->kobj);
887 scheme->quotas = NULL;
888 put_access_pattern_out:
889 kobject_put(&scheme->access_pattern->kobj);
890 scheme->access_pattern = NULL;
894 static void damon_sysfs_scheme_rm_dirs(struct damon_sysfs_scheme *scheme)
896 damon_sysfs_access_pattern_rm_dirs(scheme->access_pattern);
897 kobject_put(&scheme->access_pattern->kobj);
898 damon_sysfs_quotas_rm_dirs(scheme->quotas);
899 kobject_put(&scheme->quotas->kobj);
900 kobject_put(&scheme->watermarks->kobj);
901 kobject_put(&scheme->stats->kobj);
904 static ssize_t action_show(struct kobject *kobj, struct kobj_attribute *attr,
907 struct damon_sysfs_scheme *scheme = container_of(kobj,
908 struct damon_sysfs_scheme, kobj);
910 return sysfs_emit(buf, "%s\n",
911 damon_sysfs_damos_action_strs[scheme->action]);
914 static ssize_t action_store(struct kobject *kobj, struct kobj_attribute *attr,
915 const char *buf, size_t count)
917 struct damon_sysfs_scheme *scheme = container_of(kobj,
918 struct damon_sysfs_scheme, kobj);
919 enum damos_action action;
921 for (action = 0; action < NR_DAMOS_ACTIONS; action++) {
922 if (sysfs_streq(buf, damon_sysfs_damos_action_strs[action])) {
923 scheme->action = action;
930 static void damon_sysfs_scheme_release(struct kobject *kobj)
932 kfree(container_of(kobj, struct damon_sysfs_scheme, kobj));
935 static struct kobj_attribute damon_sysfs_scheme_action_attr =
936 __ATTR_RW_MODE(action, 0600);
938 static struct attribute *damon_sysfs_scheme_attrs[] = {
939 &damon_sysfs_scheme_action_attr.attr,
942 ATTRIBUTE_GROUPS(damon_sysfs_scheme);
944 static struct kobj_type damon_sysfs_scheme_ktype = {
945 .release = damon_sysfs_scheme_release,
946 .sysfs_ops = &kobj_sysfs_ops,
947 .default_groups = damon_sysfs_scheme_groups,
954 struct damon_sysfs_schemes {
956 struct damon_sysfs_scheme **schemes_arr;
960 static struct damon_sysfs_schemes *damon_sysfs_schemes_alloc(void)
962 return kzalloc(sizeof(struct damon_sysfs_schemes), GFP_KERNEL);
965 static void damon_sysfs_schemes_rm_dirs(struct damon_sysfs_schemes *schemes)
967 struct damon_sysfs_scheme **schemes_arr = schemes->schemes_arr;
970 for (i = 0; i < schemes->nr; i++) {
971 damon_sysfs_scheme_rm_dirs(schemes_arr[i]);
972 kobject_put(&schemes_arr[i]->kobj);
976 schemes->schemes_arr = NULL;
979 static int damon_sysfs_schemes_add_dirs(struct damon_sysfs_schemes *schemes,
982 struct damon_sysfs_scheme **schemes_arr, *scheme;
985 damon_sysfs_schemes_rm_dirs(schemes);
989 schemes_arr = kmalloc_array(nr_schemes, sizeof(*schemes_arr),
990 GFP_KERNEL | __GFP_NOWARN);
993 schemes->schemes_arr = schemes_arr;
995 for (i = 0; i < nr_schemes; i++) {
996 scheme = damon_sysfs_scheme_alloc(DAMOS_STAT);
998 damon_sysfs_schemes_rm_dirs(schemes);
1002 err = kobject_init_and_add(&scheme->kobj,
1003 &damon_sysfs_scheme_ktype, &schemes->kobj,
1007 err = damon_sysfs_scheme_add_dirs(scheme);
1011 schemes_arr[i] = scheme;
1017 damon_sysfs_schemes_rm_dirs(schemes);
1018 kobject_put(&scheme->kobj);
1022 static ssize_t nr_schemes_show(struct kobject *kobj,
1023 struct kobj_attribute *attr, char *buf)
1025 struct damon_sysfs_schemes *schemes = container_of(kobj,
1026 struct damon_sysfs_schemes, kobj);
1028 return sysfs_emit(buf, "%d\n", schemes->nr);
1031 static ssize_t nr_schemes_store(struct kobject *kobj,
1032 struct kobj_attribute *attr, const char *buf, size_t count)
1034 struct damon_sysfs_schemes *schemes = container_of(kobj,
1035 struct damon_sysfs_schemes, kobj);
1036 int nr, err = kstrtoint(buf, 0, &nr);
1043 if (!mutex_trylock(&damon_sysfs_lock))
1045 err = damon_sysfs_schemes_add_dirs(schemes, nr);
1046 mutex_unlock(&damon_sysfs_lock);
1052 static void damon_sysfs_schemes_release(struct kobject *kobj)
1054 kfree(container_of(kobj, struct damon_sysfs_schemes, kobj));
1057 static struct kobj_attribute damon_sysfs_schemes_nr_attr =
1058 __ATTR_RW_MODE(nr_schemes, 0600);
1060 static struct attribute *damon_sysfs_schemes_attrs[] = {
1061 &damon_sysfs_schemes_nr_attr.attr,
1064 ATTRIBUTE_GROUPS(damon_sysfs_schemes);
1066 static struct kobj_type damon_sysfs_schemes_ktype = {
1067 .release = damon_sysfs_schemes_release,
1068 .sysfs_ops = &kobj_sysfs_ops,
1069 .default_groups = damon_sysfs_schemes_groups,
1073 * init region directory
1076 struct damon_sysfs_region {
1077 struct kobject kobj;
1078 unsigned long start;
1082 static struct damon_sysfs_region *damon_sysfs_region_alloc(
1083 unsigned long start,
1086 struct damon_sysfs_region *region = kmalloc(sizeof(*region),
1091 region->kobj = (struct kobject){};
1092 region->start = start;
1097 static ssize_t start_show(struct kobject *kobj, struct kobj_attribute *attr,
1100 struct damon_sysfs_region *region = container_of(kobj,
1101 struct damon_sysfs_region, kobj);
1103 return sysfs_emit(buf, "%lu\n", region->start);
1106 static ssize_t start_store(struct kobject *kobj, struct kobj_attribute *attr,
1107 const char *buf, size_t count)
1109 struct damon_sysfs_region *region = container_of(kobj,
1110 struct damon_sysfs_region, kobj);
1111 int err = kstrtoul(buf, 0, ®ion->start);
1118 static ssize_t end_show(struct kobject *kobj, struct kobj_attribute *attr,
1121 struct damon_sysfs_region *region = container_of(kobj,
1122 struct damon_sysfs_region, kobj);
1124 return sysfs_emit(buf, "%lu\n", region->end);
1127 static ssize_t end_store(struct kobject *kobj, struct kobj_attribute *attr,
1128 const char *buf, size_t count)
1130 struct damon_sysfs_region *region = container_of(kobj,
1131 struct damon_sysfs_region, kobj);
1132 int err = kstrtoul(buf, 0, ®ion->end);
1139 static void damon_sysfs_region_release(struct kobject *kobj)
1141 kfree(container_of(kobj, struct damon_sysfs_region, kobj));
1144 static struct kobj_attribute damon_sysfs_region_start_attr =
1145 __ATTR_RW_MODE(start, 0600);
1147 static struct kobj_attribute damon_sysfs_region_end_attr =
1148 __ATTR_RW_MODE(end, 0600);
1150 static struct attribute *damon_sysfs_region_attrs[] = {
1151 &damon_sysfs_region_start_attr.attr,
1152 &damon_sysfs_region_end_attr.attr,
1155 ATTRIBUTE_GROUPS(damon_sysfs_region);
1157 static struct kobj_type damon_sysfs_region_ktype = {
1158 .release = damon_sysfs_region_release,
1159 .sysfs_ops = &kobj_sysfs_ops,
1160 .default_groups = damon_sysfs_region_groups,
1164 * init_regions directory
1167 struct damon_sysfs_regions {
1168 struct kobject kobj;
1169 struct damon_sysfs_region **regions_arr;
1173 static struct damon_sysfs_regions *damon_sysfs_regions_alloc(void)
1175 return kzalloc(sizeof(struct damon_sysfs_regions), GFP_KERNEL);
1178 static void damon_sysfs_regions_rm_dirs(struct damon_sysfs_regions *regions)
1180 struct damon_sysfs_region **regions_arr = regions->regions_arr;
1183 for (i = 0; i < regions->nr; i++)
1184 kobject_put(®ions_arr[i]->kobj);
1187 regions->regions_arr = NULL;
1190 static int damon_sysfs_regions_add_dirs(struct damon_sysfs_regions *regions,
1193 struct damon_sysfs_region **regions_arr, *region;
1196 damon_sysfs_regions_rm_dirs(regions);
1200 regions_arr = kmalloc_array(nr_regions, sizeof(*regions_arr),
1201 GFP_KERNEL | __GFP_NOWARN);
1204 regions->regions_arr = regions_arr;
1206 for (i = 0; i < nr_regions; i++) {
1207 region = damon_sysfs_region_alloc(0, 0);
1209 damon_sysfs_regions_rm_dirs(regions);
1213 err = kobject_init_and_add(®ion->kobj,
1214 &damon_sysfs_region_ktype, ®ions->kobj,
1217 kobject_put(®ion->kobj);
1218 damon_sysfs_regions_rm_dirs(regions);
1222 regions_arr[i] = region;
1228 static ssize_t nr_regions_show(struct kobject *kobj,
1229 struct kobj_attribute *attr, char *buf)
1231 struct damon_sysfs_regions *regions = container_of(kobj,
1232 struct damon_sysfs_regions, kobj);
1234 return sysfs_emit(buf, "%d\n", regions->nr);
1237 static ssize_t nr_regions_store(struct kobject *kobj,
1238 struct kobj_attribute *attr, const char *buf, size_t count)
1240 struct damon_sysfs_regions *regions = container_of(kobj,
1241 struct damon_sysfs_regions, kobj);
1242 int nr, err = kstrtoint(buf, 0, &nr);
1249 if (!mutex_trylock(&damon_sysfs_lock))
1251 err = damon_sysfs_regions_add_dirs(regions, nr);
1252 mutex_unlock(&damon_sysfs_lock);
1259 static void damon_sysfs_regions_release(struct kobject *kobj)
1261 kfree(container_of(kobj, struct damon_sysfs_regions, kobj));
1264 static struct kobj_attribute damon_sysfs_regions_nr_attr =
1265 __ATTR_RW_MODE(nr_regions, 0600);
1267 static struct attribute *damon_sysfs_regions_attrs[] = {
1268 &damon_sysfs_regions_nr_attr.attr,
1271 ATTRIBUTE_GROUPS(damon_sysfs_regions);
1273 static struct kobj_type damon_sysfs_regions_ktype = {
1274 .release = damon_sysfs_regions_release,
1275 .sysfs_ops = &kobj_sysfs_ops,
1276 .default_groups = damon_sysfs_regions_groups,
1283 struct damon_sysfs_target {
1284 struct kobject kobj;
1285 struct damon_sysfs_regions *regions;
1289 static struct damon_sysfs_target *damon_sysfs_target_alloc(void)
1291 return kzalloc(sizeof(struct damon_sysfs_target), GFP_KERNEL);
1294 static int damon_sysfs_target_add_dirs(struct damon_sysfs_target *target)
1296 struct damon_sysfs_regions *regions = damon_sysfs_regions_alloc();
1302 err = kobject_init_and_add(®ions->kobj, &damon_sysfs_regions_ktype,
1303 &target->kobj, "regions");
1305 kobject_put(®ions->kobj);
1307 target->regions = regions;
1311 static void damon_sysfs_target_rm_dirs(struct damon_sysfs_target *target)
1313 damon_sysfs_regions_rm_dirs(target->regions);
1314 kobject_put(&target->regions->kobj);
1317 static ssize_t pid_target_show(struct kobject *kobj,
1318 struct kobj_attribute *attr, char *buf)
1320 struct damon_sysfs_target *target = container_of(kobj,
1321 struct damon_sysfs_target, kobj);
1323 return sysfs_emit(buf, "%d\n", target->pid);
1326 static ssize_t pid_target_store(struct kobject *kobj,
1327 struct kobj_attribute *attr, const char *buf, size_t count)
1329 struct damon_sysfs_target *target = container_of(kobj,
1330 struct damon_sysfs_target, kobj);
1331 int err = kstrtoint(buf, 0, &target->pid);
1338 static void damon_sysfs_target_release(struct kobject *kobj)
1340 kfree(container_of(kobj, struct damon_sysfs_target, kobj));
1343 static struct kobj_attribute damon_sysfs_target_pid_attr =
1344 __ATTR_RW_MODE(pid_target, 0600);
1346 static struct attribute *damon_sysfs_target_attrs[] = {
1347 &damon_sysfs_target_pid_attr.attr,
1350 ATTRIBUTE_GROUPS(damon_sysfs_target);
1352 static struct kobj_type damon_sysfs_target_ktype = {
1353 .release = damon_sysfs_target_release,
1354 .sysfs_ops = &kobj_sysfs_ops,
1355 .default_groups = damon_sysfs_target_groups,
1362 struct damon_sysfs_targets {
1363 struct kobject kobj;
1364 struct damon_sysfs_target **targets_arr;
1368 static struct damon_sysfs_targets *damon_sysfs_targets_alloc(void)
1370 return kzalloc(sizeof(struct damon_sysfs_targets), GFP_KERNEL);
1373 static void damon_sysfs_targets_rm_dirs(struct damon_sysfs_targets *targets)
1375 struct damon_sysfs_target **targets_arr = targets->targets_arr;
1378 for (i = 0; i < targets->nr; i++) {
1379 damon_sysfs_target_rm_dirs(targets_arr[i]);
1380 kobject_put(&targets_arr[i]->kobj);
1384 targets->targets_arr = NULL;
1387 static int damon_sysfs_targets_add_dirs(struct damon_sysfs_targets *targets,
1390 struct damon_sysfs_target **targets_arr, *target;
1393 damon_sysfs_targets_rm_dirs(targets);
1397 targets_arr = kmalloc_array(nr_targets, sizeof(*targets_arr),
1398 GFP_KERNEL | __GFP_NOWARN);
1401 targets->targets_arr = targets_arr;
1403 for (i = 0; i < nr_targets; i++) {
1404 target = damon_sysfs_target_alloc();
1406 damon_sysfs_targets_rm_dirs(targets);
1410 err = kobject_init_and_add(&target->kobj,
1411 &damon_sysfs_target_ktype, &targets->kobj,
1416 err = damon_sysfs_target_add_dirs(target);
1420 targets_arr[i] = target;
1426 damon_sysfs_targets_rm_dirs(targets);
1427 kobject_put(&target->kobj);
1431 static ssize_t nr_targets_show(struct kobject *kobj,
1432 struct kobj_attribute *attr, char *buf)
1434 struct damon_sysfs_targets *targets = container_of(kobj,
1435 struct damon_sysfs_targets, kobj);
1437 return sysfs_emit(buf, "%d\n", targets->nr);
1440 static ssize_t nr_targets_store(struct kobject *kobj,
1441 struct kobj_attribute *attr, const char *buf, size_t count)
1443 struct damon_sysfs_targets *targets = container_of(kobj,
1444 struct damon_sysfs_targets, kobj);
1445 int nr, err = kstrtoint(buf, 0, &nr);
1452 if (!mutex_trylock(&damon_sysfs_lock))
1454 err = damon_sysfs_targets_add_dirs(targets, nr);
1455 mutex_unlock(&damon_sysfs_lock);
1462 static void damon_sysfs_targets_release(struct kobject *kobj)
1464 kfree(container_of(kobj, struct damon_sysfs_targets, kobj));
1467 static struct kobj_attribute damon_sysfs_targets_nr_attr =
1468 __ATTR_RW_MODE(nr_targets, 0600);
1470 static struct attribute *damon_sysfs_targets_attrs[] = {
1471 &damon_sysfs_targets_nr_attr.attr,
1474 ATTRIBUTE_GROUPS(damon_sysfs_targets);
1476 static struct kobj_type damon_sysfs_targets_ktype = {
1477 .release = damon_sysfs_targets_release,
1478 .sysfs_ops = &kobj_sysfs_ops,
1479 .default_groups = damon_sysfs_targets_groups,
1483 * intervals directory
1486 struct damon_sysfs_intervals {
1487 struct kobject kobj;
1488 unsigned long sample_us;
1489 unsigned long aggr_us;
1490 unsigned long update_us;
1493 static struct damon_sysfs_intervals *damon_sysfs_intervals_alloc(
1494 unsigned long sample_us, unsigned long aggr_us,
1495 unsigned long update_us)
1497 struct damon_sysfs_intervals *intervals = kmalloc(sizeof(*intervals),
1503 intervals->kobj = (struct kobject){};
1504 intervals->sample_us = sample_us;
1505 intervals->aggr_us = aggr_us;
1506 intervals->update_us = update_us;
1510 static ssize_t sample_us_show(struct kobject *kobj,
1511 struct kobj_attribute *attr, char *buf)
1513 struct damon_sysfs_intervals *intervals = container_of(kobj,
1514 struct damon_sysfs_intervals, kobj);
1516 return sysfs_emit(buf, "%lu\n", intervals->sample_us);
1519 static ssize_t sample_us_store(struct kobject *kobj,
1520 struct kobj_attribute *attr, const char *buf, size_t count)
1522 struct damon_sysfs_intervals *intervals = container_of(kobj,
1523 struct damon_sysfs_intervals, kobj);
1525 int err = kstrtoul(buf, 0, &us);
1530 intervals->sample_us = us;
1534 static ssize_t aggr_us_show(struct kobject *kobj, struct kobj_attribute *attr,
1537 struct damon_sysfs_intervals *intervals = container_of(kobj,
1538 struct damon_sysfs_intervals, kobj);
1540 return sysfs_emit(buf, "%lu\n", intervals->aggr_us);
1543 static ssize_t aggr_us_store(struct kobject *kobj, struct kobj_attribute *attr,
1544 const char *buf, size_t count)
1546 struct damon_sysfs_intervals *intervals = container_of(kobj,
1547 struct damon_sysfs_intervals, kobj);
1549 int err = kstrtoul(buf, 0, &us);
1554 intervals->aggr_us = us;
1558 static ssize_t update_us_show(struct kobject *kobj,
1559 struct kobj_attribute *attr, char *buf)
1561 struct damon_sysfs_intervals *intervals = container_of(kobj,
1562 struct damon_sysfs_intervals, kobj);
1564 return sysfs_emit(buf, "%lu\n", intervals->update_us);
1567 static ssize_t update_us_store(struct kobject *kobj,
1568 struct kobj_attribute *attr, const char *buf, size_t count)
1570 struct damon_sysfs_intervals *intervals = container_of(kobj,
1571 struct damon_sysfs_intervals, kobj);
1573 int err = kstrtoul(buf, 0, &us);
1578 intervals->update_us = us;
1582 static void damon_sysfs_intervals_release(struct kobject *kobj)
1584 kfree(container_of(kobj, struct damon_sysfs_intervals, kobj));
1587 static struct kobj_attribute damon_sysfs_intervals_sample_us_attr =
1588 __ATTR_RW_MODE(sample_us, 0600);
1590 static struct kobj_attribute damon_sysfs_intervals_aggr_us_attr =
1591 __ATTR_RW_MODE(aggr_us, 0600);
1593 static struct kobj_attribute damon_sysfs_intervals_update_us_attr =
1594 __ATTR_RW_MODE(update_us, 0600);
1596 static struct attribute *damon_sysfs_intervals_attrs[] = {
1597 &damon_sysfs_intervals_sample_us_attr.attr,
1598 &damon_sysfs_intervals_aggr_us_attr.attr,
1599 &damon_sysfs_intervals_update_us_attr.attr,
1602 ATTRIBUTE_GROUPS(damon_sysfs_intervals);
1604 static struct kobj_type damon_sysfs_intervals_ktype = {
1605 .release = damon_sysfs_intervals_release,
1606 .sysfs_ops = &kobj_sysfs_ops,
1607 .default_groups = damon_sysfs_intervals_groups,
1611 * monitoring_attrs directory
1614 struct damon_sysfs_attrs {
1615 struct kobject kobj;
1616 struct damon_sysfs_intervals *intervals;
1617 struct damon_sysfs_ul_range *nr_regions_range;
1620 static struct damon_sysfs_attrs *damon_sysfs_attrs_alloc(void)
1622 struct damon_sysfs_attrs *attrs = kmalloc(sizeof(*attrs), GFP_KERNEL);
1626 attrs->kobj = (struct kobject){};
1630 static int damon_sysfs_attrs_add_dirs(struct damon_sysfs_attrs *attrs)
1632 struct damon_sysfs_intervals *intervals;
1633 struct damon_sysfs_ul_range *nr_regions_range;
1636 intervals = damon_sysfs_intervals_alloc(5000, 100000, 60000000);
1640 err = kobject_init_and_add(&intervals->kobj,
1641 &damon_sysfs_intervals_ktype, &attrs->kobj,
1644 goto put_intervals_out;
1645 attrs->intervals = intervals;
1647 nr_regions_range = damon_sysfs_ul_range_alloc(10, 1000);
1648 if (!nr_regions_range) {
1650 goto put_intervals_out;
1653 err = kobject_init_and_add(&nr_regions_range->kobj,
1654 &damon_sysfs_ul_range_ktype, &attrs->kobj,
1657 goto put_nr_regions_intervals_out;
1658 attrs->nr_regions_range = nr_regions_range;
1661 put_nr_regions_intervals_out:
1662 kobject_put(&nr_regions_range->kobj);
1663 attrs->nr_regions_range = NULL;
1665 kobject_put(&intervals->kobj);
1666 attrs->intervals = NULL;
1670 static void damon_sysfs_attrs_rm_dirs(struct damon_sysfs_attrs *attrs)
1672 kobject_put(&attrs->nr_regions_range->kobj);
1673 kobject_put(&attrs->intervals->kobj);
1676 static void damon_sysfs_attrs_release(struct kobject *kobj)
1678 kfree(container_of(kobj, struct damon_sysfs_attrs, kobj));
1681 static struct attribute *damon_sysfs_attrs_attrs[] = {
1684 ATTRIBUTE_GROUPS(damon_sysfs_attrs);
1686 static struct kobj_type damon_sysfs_attrs_ktype = {
1687 .release = damon_sysfs_attrs_release,
1688 .sysfs_ops = &kobj_sysfs_ops,
1689 .default_groups = damon_sysfs_attrs_groups,
1696 /* This should match with enum damon_ops_id */
1697 static const char * const damon_sysfs_ops_strs[] = {
1703 struct damon_sysfs_context {
1704 struct kobject kobj;
1705 enum damon_ops_id ops_id;
1706 struct damon_sysfs_attrs *attrs;
1707 struct damon_sysfs_targets *targets;
1708 struct damon_sysfs_schemes *schemes;
1711 static struct damon_sysfs_context *damon_sysfs_context_alloc(
1712 enum damon_ops_id ops_id)
1714 struct damon_sysfs_context *context = kmalloc(sizeof(*context),
1719 context->kobj = (struct kobject){};
1720 context->ops_id = ops_id;
1724 static int damon_sysfs_context_set_attrs(struct damon_sysfs_context *context)
1726 struct damon_sysfs_attrs *attrs = damon_sysfs_attrs_alloc();
1731 err = kobject_init_and_add(&attrs->kobj, &damon_sysfs_attrs_ktype,
1732 &context->kobj, "monitoring_attrs");
1735 err = damon_sysfs_attrs_add_dirs(attrs);
1738 context->attrs = attrs;
1742 kobject_put(&attrs->kobj);
1746 static int damon_sysfs_context_set_targets(struct damon_sysfs_context *context)
1748 struct damon_sysfs_targets *targets = damon_sysfs_targets_alloc();
1753 err = kobject_init_and_add(&targets->kobj, &damon_sysfs_targets_ktype,
1754 &context->kobj, "targets");
1756 kobject_put(&targets->kobj);
1759 context->targets = targets;
1763 static int damon_sysfs_context_set_schemes(struct damon_sysfs_context *context)
1765 struct damon_sysfs_schemes *schemes = damon_sysfs_schemes_alloc();
1770 err = kobject_init_and_add(&schemes->kobj, &damon_sysfs_schemes_ktype,
1771 &context->kobj, "schemes");
1773 kobject_put(&schemes->kobj);
1776 context->schemes = schemes;
1780 static int damon_sysfs_context_add_dirs(struct damon_sysfs_context *context)
1784 err = damon_sysfs_context_set_attrs(context);
1788 err = damon_sysfs_context_set_targets(context);
1792 err = damon_sysfs_context_set_schemes(context);
1794 goto put_targets_attrs_out;
1797 put_targets_attrs_out:
1798 kobject_put(&context->targets->kobj);
1799 context->targets = NULL;
1801 kobject_put(&context->attrs->kobj);
1802 context->attrs = NULL;
1806 static void damon_sysfs_context_rm_dirs(struct damon_sysfs_context *context)
1808 damon_sysfs_attrs_rm_dirs(context->attrs);
1809 kobject_put(&context->attrs->kobj);
1810 damon_sysfs_targets_rm_dirs(context->targets);
1811 kobject_put(&context->targets->kobj);
1812 damon_sysfs_schemes_rm_dirs(context->schemes);
1813 kobject_put(&context->schemes->kobj);
1816 static ssize_t avail_operations_show(struct kobject *kobj,
1817 struct kobj_attribute *attr, char *buf)
1819 enum damon_ops_id id;
1822 for (id = 0; id < NR_DAMON_OPS; id++) {
1823 if (!damon_is_registered_ops(id))
1825 len += sysfs_emit_at(buf, len, "%s\n",
1826 damon_sysfs_ops_strs[id]);
1831 static ssize_t operations_show(struct kobject *kobj,
1832 struct kobj_attribute *attr, char *buf)
1834 struct damon_sysfs_context *context = container_of(kobj,
1835 struct damon_sysfs_context, kobj);
1837 return sysfs_emit(buf, "%s\n", damon_sysfs_ops_strs[context->ops_id]);
1840 static ssize_t operations_store(struct kobject *kobj,
1841 struct kobj_attribute *attr, const char *buf, size_t count)
1843 struct damon_sysfs_context *context = container_of(kobj,
1844 struct damon_sysfs_context, kobj);
1845 enum damon_ops_id id;
1847 for (id = 0; id < NR_DAMON_OPS; id++) {
1848 if (sysfs_streq(buf, damon_sysfs_ops_strs[id])) {
1849 context->ops_id = id;
1856 static void damon_sysfs_context_release(struct kobject *kobj)
1858 kfree(container_of(kobj, struct damon_sysfs_context, kobj));
1861 static struct kobj_attribute damon_sysfs_context_avail_operations_attr =
1862 __ATTR_RO_MODE(avail_operations, 0400);
1864 static struct kobj_attribute damon_sysfs_context_operations_attr =
1865 __ATTR_RW_MODE(operations, 0600);
1867 static struct attribute *damon_sysfs_context_attrs[] = {
1868 &damon_sysfs_context_avail_operations_attr.attr,
1869 &damon_sysfs_context_operations_attr.attr,
1872 ATTRIBUTE_GROUPS(damon_sysfs_context);
1874 static struct kobj_type damon_sysfs_context_ktype = {
1875 .release = damon_sysfs_context_release,
1876 .sysfs_ops = &kobj_sysfs_ops,
1877 .default_groups = damon_sysfs_context_groups,
1881 * contexts directory
1884 struct damon_sysfs_contexts {
1885 struct kobject kobj;
1886 struct damon_sysfs_context **contexts_arr;
1890 static struct damon_sysfs_contexts *damon_sysfs_contexts_alloc(void)
1892 return kzalloc(sizeof(struct damon_sysfs_contexts), GFP_KERNEL);
1895 static void damon_sysfs_contexts_rm_dirs(struct damon_sysfs_contexts *contexts)
1897 struct damon_sysfs_context **contexts_arr = contexts->contexts_arr;
1900 for (i = 0; i < contexts->nr; i++) {
1901 damon_sysfs_context_rm_dirs(contexts_arr[i]);
1902 kobject_put(&contexts_arr[i]->kobj);
1905 kfree(contexts_arr);
1906 contexts->contexts_arr = NULL;
1909 static int damon_sysfs_contexts_add_dirs(struct damon_sysfs_contexts *contexts,
1912 struct damon_sysfs_context **contexts_arr, *context;
1915 damon_sysfs_contexts_rm_dirs(contexts);
1919 contexts_arr = kmalloc_array(nr_contexts, sizeof(*contexts_arr),
1920 GFP_KERNEL | __GFP_NOWARN);
1923 contexts->contexts_arr = contexts_arr;
1925 for (i = 0; i < nr_contexts; i++) {
1926 context = damon_sysfs_context_alloc(DAMON_OPS_VADDR);
1928 damon_sysfs_contexts_rm_dirs(contexts);
1932 err = kobject_init_and_add(&context->kobj,
1933 &damon_sysfs_context_ktype, &contexts->kobj,
1938 err = damon_sysfs_context_add_dirs(context);
1942 contexts_arr[i] = context;
1948 damon_sysfs_contexts_rm_dirs(contexts);
1949 kobject_put(&context->kobj);
1953 static ssize_t nr_contexts_show(struct kobject *kobj,
1954 struct kobj_attribute *attr, char *buf)
1956 struct damon_sysfs_contexts *contexts = container_of(kobj,
1957 struct damon_sysfs_contexts, kobj);
1959 return sysfs_emit(buf, "%d\n", contexts->nr);
1962 static ssize_t nr_contexts_store(struct kobject *kobj,
1963 struct kobj_attribute *attr, const char *buf, size_t count)
1965 struct damon_sysfs_contexts *contexts = container_of(kobj,
1966 struct damon_sysfs_contexts, kobj);
1969 err = kstrtoint(buf, 0, &nr);
1972 /* TODO: support multiple contexts per kdamond */
1973 if (nr < 0 || 1 < nr)
1976 if (!mutex_trylock(&damon_sysfs_lock))
1978 err = damon_sysfs_contexts_add_dirs(contexts, nr);
1979 mutex_unlock(&damon_sysfs_lock);
1986 static void damon_sysfs_contexts_release(struct kobject *kobj)
1988 kfree(container_of(kobj, struct damon_sysfs_contexts, kobj));
1991 static struct kobj_attribute damon_sysfs_contexts_nr_attr
1992 = __ATTR_RW_MODE(nr_contexts, 0600);
1994 static struct attribute *damon_sysfs_contexts_attrs[] = {
1995 &damon_sysfs_contexts_nr_attr.attr,
1998 ATTRIBUTE_GROUPS(damon_sysfs_contexts);
2000 static struct kobj_type damon_sysfs_contexts_ktype = {
2001 .release = damon_sysfs_contexts_release,
2002 .sysfs_ops = &kobj_sysfs_ops,
2003 .default_groups = damon_sysfs_contexts_groups,
2010 struct damon_sysfs_kdamond {
2011 struct kobject kobj;
2012 struct damon_sysfs_contexts *contexts;
2013 struct damon_ctx *damon_ctx;
2016 static struct damon_sysfs_kdamond *damon_sysfs_kdamond_alloc(void)
2018 return kzalloc(sizeof(struct damon_sysfs_kdamond), GFP_KERNEL);
2021 static int damon_sysfs_kdamond_add_dirs(struct damon_sysfs_kdamond *kdamond)
2023 struct damon_sysfs_contexts *contexts;
2026 contexts = damon_sysfs_contexts_alloc();
2030 err = kobject_init_and_add(&contexts->kobj,
2031 &damon_sysfs_contexts_ktype, &kdamond->kobj,
2034 kobject_put(&contexts->kobj);
2037 kdamond->contexts = contexts;
2042 static void damon_sysfs_kdamond_rm_dirs(struct damon_sysfs_kdamond *kdamond)
2044 damon_sysfs_contexts_rm_dirs(kdamond->contexts);
2045 kobject_put(&kdamond->contexts->kobj);
2048 static bool damon_sysfs_ctx_running(struct damon_ctx *ctx)
2052 mutex_lock(&ctx->kdamond_lock);
2053 running = ctx->kdamond != NULL;
2054 mutex_unlock(&ctx->kdamond_lock);
2059 * enum damon_sysfs_cmd - Commands for a specific kdamond.
2061 enum damon_sysfs_cmd {
2062 /* @DAMON_SYSFS_CMD_ON: Turn the kdamond on. */
2064 /* @DAMON_SYSFS_CMD_OFF: Turn the kdamond off. */
2065 DAMON_SYSFS_CMD_OFF,
2066 /* @DAMON_SYSFS_CMD_COMMIT: Update kdamond inputs. */
2067 DAMON_SYSFS_CMD_COMMIT,
2069 * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: Update scheme stats sysfs
2072 DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS,
2074 * @NR_DAMON_SYSFS_CMDS: Total number of DAMON sysfs commands.
2076 NR_DAMON_SYSFS_CMDS,
2079 /* Should match with enum damon_sysfs_cmd */
2080 static const char * const damon_sysfs_cmd_strs[] = {
2084 "update_schemes_stats",
2088 * struct damon_sysfs_cmd_request - A request to the DAMON callback.
2089 * @cmd: The command that needs to be handled by the callback.
2090 * @kdamond: The kobject wrapper that associated to the kdamond thread.
2092 * This structure represents a sysfs command request that need to access some
2093 * DAMON context-internal data. Because DAMON context-internal data can be
2094 * safely accessed from DAMON callbacks without additional synchronization, the
2095 * request will be handled by the DAMON callback. None-``NULL`` @kdamond means
2096 * the request is valid.
2098 struct damon_sysfs_cmd_request {
2099 enum damon_sysfs_cmd cmd;
2100 struct damon_sysfs_kdamond *kdamond;
2103 /* Current DAMON callback request. Protected by damon_sysfs_lock. */
2104 static struct damon_sysfs_cmd_request damon_sysfs_cmd_request;
2106 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
2109 struct damon_sysfs_kdamond *kdamond = container_of(kobj,
2110 struct damon_sysfs_kdamond, kobj);
2111 struct damon_ctx *ctx = kdamond->damon_ctx;
2117 running = damon_sysfs_ctx_running(ctx);
2119 return sysfs_emit(buf, "%s\n", running ?
2120 damon_sysfs_cmd_strs[DAMON_SYSFS_CMD_ON] :
2121 damon_sysfs_cmd_strs[DAMON_SYSFS_CMD_OFF]);
2124 static int damon_sysfs_set_attrs(struct damon_ctx *ctx,
2125 struct damon_sysfs_attrs *sys_attrs)
2127 struct damon_sysfs_intervals *sys_intervals = sys_attrs->intervals;
2128 struct damon_sysfs_ul_range *sys_nr_regions =
2129 sys_attrs->nr_regions_range;
2131 return damon_set_attrs(ctx, sys_intervals->sample_us,
2132 sys_intervals->aggr_us, sys_intervals->update_us,
2133 sys_nr_regions->min, sys_nr_regions->max);
2136 static void damon_sysfs_destroy_targets(struct damon_ctx *ctx)
2138 struct damon_target *t, *next;
2140 damon_for_each_target_safe(t, next, ctx) {
2141 if (damon_target_has_pid(ctx))
2143 damon_destroy_target(t);
2147 static int damon_sysfs_set_regions(struct damon_target *t,
2148 struct damon_sysfs_regions *sysfs_regions)
2150 struct damon_addr_range *ranges = kmalloc_array(sysfs_regions->nr,
2151 sizeof(*ranges), GFP_KERNEL | __GFP_NOWARN);
2152 int i, err = -EINVAL;
2156 for (i = 0; i < sysfs_regions->nr; i++) {
2157 struct damon_sysfs_region *sys_region =
2158 sysfs_regions->regions_arr[i];
2160 if (sys_region->start > sys_region->end)
2163 ranges[i].start = sys_region->start;
2164 ranges[i].end = sys_region->end;
2167 if (ranges[i - 1].end > ranges[i].start)
2170 err = damon_set_regions(t, ranges, sysfs_regions->nr);
2177 static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target,
2178 struct damon_ctx *ctx)
2180 struct damon_target *t = damon_new_target();
2185 if (damon_target_has_pid(ctx)) {
2186 t->pid = find_get_pid(sys_target->pid);
2188 goto destroy_targets_out;
2190 damon_add_target(ctx, t);
2191 err = damon_sysfs_set_regions(t, sys_target->regions);
2193 goto destroy_targets_out;
2196 destroy_targets_out:
2197 damon_sysfs_destroy_targets(ctx);
2202 * Search a target in a context that corresponds to the sysfs target input.
2204 * Return: pointer to the target if found, NULL if not found, or negative
2205 * error code if the search failed.
2207 static struct damon_target *damon_sysfs_existing_target(
2208 struct damon_sysfs_target *sys_target, struct damon_ctx *ctx)
2211 struct damon_target *t;
2213 if (!damon_target_has_pid(ctx)) {
2214 /* Up to only one target for paddr could exist */
2215 damon_for_each_target(t, ctx)
2220 /* ops.id should be DAMON_OPS_VADDR or DAMON_OPS_FVADDR */
2221 pid = find_get_pid(sys_target->pid);
2223 return ERR_PTR(-EINVAL);
2224 damon_for_each_target(t, ctx) {
2225 if (t->pid == pid) {
2234 static int damon_sysfs_set_targets(struct damon_ctx *ctx,
2235 struct damon_sysfs_targets *sysfs_targets)
2239 /* Multiple physical address space monitoring targets makes no sense */
2240 if (ctx->ops.id == DAMON_OPS_PADDR && sysfs_targets->nr > 1)
2243 for (i = 0; i < sysfs_targets->nr; i++) {
2244 struct damon_sysfs_target *st = sysfs_targets->targets_arr[i];
2245 struct damon_target *t = damon_sysfs_existing_target(st, ctx);
2250 err = damon_sysfs_add_target(st, ctx);
2252 err = damon_sysfs_set_regions(t, st->regions);
2259 static struct damos *damon_sysfs_mk_scheme(
2260 struct damon_sysfs_scheme *sysfs_scheme)
2262 struct damon_sysfs_access_pattern *pattern =
2263 sysfs_scheme->access_pattern;
2264 struct damon_sysfs_quotas *sysfs_quotas = sysfs_scheme->quotas;
2265 struct damon_sysfs_weights *sysfs_weights = sysfs_quotas->weights;
2266 struct damon_sysfs_watermarks *sysfs_wmarks = sysfs_scheme->watermarks;
2267 struct damos_quota quota = {
2268 .ms = sysfs_quotas->ms,
2269 .sz = sysfs_quotas->sz,
2270 .reset_interval = sysfs_quotas->reset_interval_ms,
2271 .weight_sz = sysfs_weights->sz,
2272 .weight_nr_accesses = sysfs_weights->nr_accesses,
2273 .weight_age = sysfs_weights->age,
2275 struct damos_watermarks wmarks = {
2276 .metric = sysfs_wmarks->metric,
2277 .interval = sysfs_wmarks->interval_us,
2278 .high = sysfs_wmarks->high,
2279 .mid = sysfs_wmarks->mid,
2280 .low = sysfs_wmarks->low,
2283 return damon_new_scheme(pattern->sz->min, pattern->sz->max,
2284 pattern->nr_accesses->min, pattern->nr_accesses->max,
2285 pattern->age->min, pattern->age->max,
2286 sysfs_scheme->action, "a, &wmarks);
2289 static int damon_sysfs_set_schemes(struct damon_ctx *ctx,
2290 struct damon_sysfs_schemes *sysfs_schemes)
2294 for (i = 0; i < sysfs_schemes->nr; i++) {
2295 struct damos *scheme, *next;
2297 scheme = damon_sysfs_mk_scheme(sysfs_schemes->schemes_arr[i]);
2299 damon_for_each_scheme_safe(scheme, next, ctx)
2300 damon_destroy_scheme(scheme);
2303 damon_add_scheme(ctx, scheme);
2308 static void damon_sysfs_before_terminate(struct damon_ctx *ctx)
2310 struct damon_target *t, *next;
2312 if (ctx->ops.id != DAMON_OPS_VADDR && ctx->ops.id != DAMON_OPS_FVADDR)
2315 mutex_lock(&ctx->kdamond_lock);
2316 damon_for_each_target_safe(t, next, ctx) {
2318 damon_destroy_target(t);
2320 mutex_unlock(&ctx->kdamond_lock);
2324 * damon_sysfs_upd_schemes_stats() - Update schemes stats sysfs files.
2325 * @kdamond: The kobject wrapper that associated to the kdamond thread.
2327 * This function reads the schemes stats of specific kdamond and update the
2328 * related values for sysfs files. This function should be called from DAMON
2329 * callbacks while holding ``damon_syfs_lock``, to safely access the DAMON
2330 * contexts-internal data and DAMON sysfs variables.
2332 static int damon_sysfs_upd_schemes_stats(struct damon_sysfs_kdamond *kdamond)
2334 struct damon_ctx *ctx = kdamond->damon_ctx;
2335 struct damon_sysfs_schemes *sysfs_schemes;
2336 struct damos *scheme;
2337 int schemes_idx = 0;
2341 sysfs_schemes = kdamond->contexts->contexts_arr[0]->schemes;
2342 damon_for_each_scheme(scheme, ctx) {
2343 struct damon_sysfs_stats *sysfs_stats;
2345 sysfs_stats = sysfs_schemes->schemes_arr[schemes_idx++]->stats;
2346 sysfs_stats->nr_tried = scheme->stat.nr_tried;
2347 sysfs_stats->sz_tried = scheme->stat.sz_tried;
2348 sysfs_stats->nr_applied = scheme->stat.nr_applied;
2349 sysfs_stats->sz_applied = scheme->stat.sz_applied;
2350 sysfs_stats->qt_exceeds = scheme->stat.qt_exceeds;
2355 static inline bool damon_sysfs_kdamond_running(
2356 struct damon_sysfs_kdamond *kdamond)
2358 return kdamond->damon_ctx &&
2359 damon_sysfs_ctx_running(kdamond->damon_ctx);
2362 static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
2363 struct damon_sysfs_context *sys_ctx)
2367 err = damon_select_ops(ctx, sys_ctx->ops_id);
2370 err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
2373 err = damon_sysfs_set_targets(ctx, sys_ctx->targets);
2376 return damon_sysfs_set_schemes(ctx, sys_ctx->schemes);
2380 * damon_sysfs_commit_input() - Commit user inputs to a running kdamond.
2381 * @kdamond: The kobject wrapper for the associated kdamond.
2383 * If the sysfs input is wrong, the kdamond will be terminated.
2385 static int damon_sysfs_commit_input(struct damon_sysfs_kdamond *kdamond)
2387 if (!damon_sysfs_kdamond_running(kdamond))
2389 /* TODO: Support multiple contexts per kdamond */
2390 if (kdamond->contexts->nr != 1)
2393 return damon_sysfs_apply_inputs(kdamond->damon_ctx,
2394 kdamond->contexts->contexts_arr[0]);
2398 * damon_sysfs_cmd_request_callback() - DAMON callback for handling requests.
2399 * @c: The DAMON context of the callback.
2401 * This function is periodically called back from the kdamond thread for @c.
2402 * Then, it checks if there is a waiting DAMON sysfs request and handles it.
2404 static int damon_sysfs_cmd_request_callback(struct damon_ctx *c)
2406 struct damon_sysfs_kdamond *kdamond;
2409 /* avoid deadlock due to concurrent state_store('off') */
2410 if (!mutex_trylock(&damon_sysfs_lock))
2412 kdamond = damon_sysfs_cmd_request.kdamond;
2413 if (!kdamond || kdamond->damon_ctx != c)
2415 switch (damon_sysfs_cmd_request.cmd) {
2416 case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS:
2417 err = damon_sysfs_upd_schemes_stats(kdamond);
2419 case DAMON_SYSFS_CMD_COMMIT:
2420 err = damon_sysfs_commit_input(kdamond);
2425 /* Mark the request as invalid now. */
2426 damon_sysfs_cmd_request.kdamond = NULL;
2428 mutex_unlock(&damon_sysfs_lock);
2432 static struct damon_ctx *damon_sysfs_build_ctx(
2433 struct damon_sysfs_context *sys_ctx)
2435 struct damon_ctx *ctx = damon_new_ctx();
2439 return ERR_PTR(-ENOMEM);
2441 err = damon_sysfs_apply_inputs(ctx, sys_ctx);
2443 damon_destroy_ctx(ctx);
2444 return ERR_PTR(err);
2447 ctx->callback.after_wmarks_check = damon_sysfs_cmd_request_callback;
2448 ctx->callback.after_aggregation = damon_sysfs_cmd_request_callback;
2449 ctx->callback.before_terminate = damon_sysfs_before_terminate;
2453 static int damon_sysfs_turn_damon_on(struct damon_sysfs_kdamond *kdamond)
2455 struct damon_ctx *ctx;
2458 if (kdamond->damon_ctx &&
2459 damon_sysfs_ctx_running(kdamond->damon_ctx))
2461 if (damon_sysfs_cmd_request.kdamond == kdamond)
2463 /* TODO: support multiple contexts per kdamond */
2464 if (kdamond->contexts->nr != 1)
2467 if (kdamond->damon_ctx)
2468 damon_destroy_ctx(kdamond->damon_ctx);
2469 kdamond->damon_ctx = NULL;
2471 ctx = damon_sysfs_build_ctx(kdamond->contexts->contexts_arr[0]);
2473 return PTR_ERR(ctx);
2474 err = damon_start(&ctx, 1, false);
2476 damon_destroy_ctx(ctx);
2479 kdamond->damon_ctx = ctx;
2483 static int damon_sysfs_turn_damon_off(struct damon_sysfs_kdamond *kdamond)
2485 if (!kdamond->damon_ctx)
2487 return damon_stop(&kdamond->damon_ctx, 1);
2489 * To allow users show final monitoring results of already turned-off
2490 * DAMON, we free kdamond->damon_ctx in next
2491 * damon_sysfs_turn_damon_on(), or kdamonds_nr_store()
2496 * damon_sysfs_handle_cmd() - Handle a command for a specific kdamond.
2497 * @cmd: The command to handle.
2498 * @kdamond: The kobject wrapper for the associated kdamond.
2500 * This function handles a DAMON sysfs command for a kdamond. For commands
2501 * that need to access running DAMON context-internal data, it requests
2502 * handling of the command to the DAMON callback
2503 * (@damon_sysfs_cmd_request_callback()) and wait until it is properly handled,
2504 * or the context is completed.
2506 * Return: 0 on success, negative error code otherwise.
2508 static int damon_sysfs_handle_cmd(enum damon_sysfs_cmd cmd,
2509 struct damon_sysfs_kdamond *kdamond)
2511 bool need_wait = true;
2513 /* Handle commands that doesn't access DAMON context-internal data */
2515 case DAMON_SYSFS_CMD_ON:
2516 return damon_sysfs_turn_damon_on(kdamond);
2517 case DAMON_SYSFS_CMD_OFF:
2518 return damon_sysfs_turn_damon_off(kdamond);
2523 /* Pass the command to DAMON callback for safe DAMON context access */
2524 if (damon_sysfs_cmd_request.kdamond)
2526 if (!damon_sysfs_kdamond_running(kdamond))
2528 damon_sysfs_cmd_request.cmd = cmd;
2529 damon_sysfs_cmd_request.kdamond = kdamond;
2532 * wait until damon_sysfs_cmd_request_callback() handles the request
2533 * from kdamond context
2535 mutex_unlock(&damon_sysfs_lock);
2537 schedule_timeout_idle(msecs_to_jiffies(100));
2538 if (!mutex_trylock(&damon_sysfs_lock))
2540 if (!damon_sysfs_cmd_request.kdamond) {
2541 /* damon_sysfs_cmd_request_callback() handled */
2543 } else if (!damon_sysfs_kdamond_running(kdamond)) {
2544 /* kdamond has already finished */
2546 damon_sysfs_cmd_request.kdamond = NULL;
2548 mutex_unlock(&damon_sysfs_lock);
2550 mutex_lock(&damon_sysfs_lock);
2554 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
2555 const char *buf, size_t count)
2557 struct damon_sysfs_kdamond *kdamond = container_of(kobj,
2558 struct damon_sysfs_kdamond, kobj);
2559 enum damon_sysfs_cmd cmd;
2560 ssize_t ret = -EINVAL;
2562 if (!mutex_trylock(&damon_sysfs_lock))
2564 for (cmd = 0; cmd < NR_DAMON_SYSFS_CMDS; cmd++) {
2565 if (sysfs_streq(buf, damon_sysfs_cmd_strs[cmd])) {
2566 ret = damon_sysfs_handle_cmd(cmd, kdamond);
2570 mutex_unlock(&damon_sysfs_lock);
2576 static ssize_t pid_show(struct kobject *kobj,
2577 struct kobj_attribute *attr, char *buf)
2579 struct damon_sysfs_kdamond *kdamond = container_of(kobj,
2580 struct damon_sysfs_kdamond, kobj);
2581 struct damon_ctx *ctx;
2584 if (!mutex_trylock(&damon_sysfs_lock))
2586 ctx = kdamond->damon_ctx;
2591 mutex_lock(&ctx->kdamond_lock);
2595 pid = ctx->kdamond->pid;
2596 mutex_unlock(&ctx->kdamond_lock);
2598 mutex_unlock(&damon_sysfs_lock);
2599 return sysfs_emit(buf, "%d\n", pid);
2602 static void damon_sysfs_kdamond_release(struct kobject *kobj)
2604 struct damon_sysfs_kdamond *kdamond = container_of(kobj,
2605 struct damon_sysfs_kdamond, kobj);
2607 if (kdamond->damon_ctx)
2608 damon_destroy_ctx(kdamond->damon_ctx);
2612 static struct kobj_attribute damon_sysfs_kdamond_state_attr =
2613 __ATTR_RW_MODE(state, 0600);
2615 static struct kobj_attribute damon_sysfs_kdamond_pid_attr =
2616 __ATTR_RO_MODE(pid, 0400);
2618 static struct attribute *damon_sysfs_kdamond_attrs[] = {
2619 &damon_sysfs_kdamond_state_attr.attr,
2620 &damon_sysfs_kdamond_pid_attr.attr,
2623 ATTRIBUTE_GROUPS(damon_sysfs_kdamond);
2625 static struct kobj_type damon_sysfs_kdamond_ktype = {
2626 .release = damon_sysfs_kdamond_release,
2627 .sysfs_ops = &kobj_sysfs_ops,
2628 .default_groups = damon_sysfs_kdamond_groups,
2632 * kdamonds directory
2635 struct damon_sysfs_kdamonds {
2636 struct kobject kobj;
2637 struct damon_sysfs_kdamond **kdamonds_arr;
2641 static struct damon_sysfs_kdamonds *damon_sysfs_kdamonds_alloc(void)
2643 return kzalloc(sizeof(struct damon_sysfs_kdamonds), GFP_KERNEL);
2646 static void damon_sysfs_kdamonds_rm_dirs(struct damon_sysfs_kdamonds *kdamonds)
2648 struct damon_sysfs_kdamond **kdamonds_arr = kdamonds->kdamonds_arr;
2651 for (i = 0; i < kdamonds->nr; i++) {
2652 damon_sysfs_kdamond_rm_dirs(kdamonds_arr[i]);
2653 kobject_put(&kdamonds_arr[i]->kobj);
2656 kfree(kdamonds_arr);
2657 kdamonds->kdamonds_arr = NULL;
2660 static int damon_sysfs_nr_running_ctxs(struct damon_sysfs_kdamond **kdamonds,
2663 int nr_running_ctxs = 0;
2666 for (i = 0; i < nr_kdamonds; i++) {
2667 struct damon_ctx *ctx = kdamonds[i]->damon_ctx;
2671 mutex_lock(&ctx->kdamond_lock);
2674 mutex_unlock(&ctx->kdamond_lock);
2676 return nr_running_ctxs;
2679 static int damon_sysfs_kdamonds_add_dirs(struct damon_sysfs_kdamonds *kdamonds,
2682 struct damon_sysfs_kdamond **kdamonds_arr, *kdamond;
2685 if (damon_sysfs_nr_running_ctxs(kdamonds->kdamonds_arr, kdamonds->nr))
2688 for (i = 0; i < kdamonds->nr; i++) {
2689 if (damon_sysfs_cmd_request.kdamond ==
2690 kdamonds->kdamonds_arr[i])
2694 damon_sysfs_kdamonds_rm_dirs(kdamonds);
2698 kdamonds_arr = kmalloc_array(nr_kdamonds, sizeof(*kdamonds_arr),
2699 GFP_KERNEL | __GFP_NOWARN);
2702 kdamonds->kdamonds_arr = kdamonds_arr;
2704 for (i = 0; i < nr_kdamonds; i++) {
2705 kdamond = damon_sysfs_kdamond_alloc();
2707 damon_sysfs_kdamonds_rm_dirs(kdamonds);
2711 err = kobject_init_and_add(&kdamond->kobj,
2712 &damon_sysfs_kdamond_ktype, &kdamonds->kobj,
2717 err = damon_sysfs_kdamond_add_dirs(kdamond);
2721 kdamonds_arr[i] = kdamond;
2727 damon_sysfs_kdamonds_rm_dirs(kdamonds);
2728 kobject_put(&kdamond->kobj);
2732 static ssize_t nr_kdamonds_show(struct kobject *kobj,
2733 struct kobj_attribute *attr, char *buf)
2735 struct damon_sysfs_kdamonds *kdamonds = container_of(kobj,
2736 struct damon_sysfs_kdamonds, kobj);
2738 return sysfs_emit(buf, "%d\n", kdamonds->nr);
2741 static ssize_t nr_kdamonds_store(struct kobject *kobj,
2742 struct kobj_attribute *attr, const char *buf, size_t count)
2744 struct damon_sysfs_kdamonds *kdamonds = container_of(kobj,
2745 struct damon_sysfs_kdamonds, kobj);
2748 err = kstrtoint(buf, 0, &nr);
2754 if (!mutex_trylock(&damon_sysfs_lock))
2756 err = damon_sysfs_kdamonds_add_dirs(kdamonds, nr);
2757 mutex_unlock(&damon_sysfs_lock);
2764 static void damon_sysfs_kdamonds_release(struct kobject *kobj)
2766 kfree(container_of(kobj, struct damon_sysfs_kdamonds, kobj));
2769 static struct kobj_attribute damon_sysfs_kdamonds_nr_attr =
2770 __ATTR_RW_MODE(nr_kdamonds, 0600);
2772 static struct attribute *damon_sysfs_kdamonds_attrs[] = {
2773 &damon_sysfs_kdamonds_nr_attr.attr,
2776 ATTRIBUTE_GROUPS(damon_sysfs_kdamonds);
2778 static struct kobj_type damon_sysfs_kdamonds_ktype = {
2779 .release = damon_sysfs_kdamonds_release,
2780 .sysfs_ops = &kobj_sysfs_ops,
2781 .default_groups = damon_sysfs_kdamonds_groups,
2785 * damon user interface directory
2788 struct damon_sysfs_ui_dir {
2789 struct kobject kobj;
2790 struct damon_sysfs_kdamonds *kdamonds;
2793 static struct damon_sysfs_ui_dir *damon_sysfs_ui_dir_alloc(void)
2795 return kzalloc(sizeof(struct damon_sysfs_ui_dir), GFP_KERNEL);
2798 static int damon_sysfs_ui_dir_add_dirs(struct damon_sysfs_ui_dir *ui_dir)
2800 struct damon_sysfs_kdamonds *kdamonds;
2803 kdamonds = damon_sysfs_kdamonds_alloc();
2807 err = kobject_init_and_add(&kdamonds->kobj,
2808 &damon_sysfs_kdamonds_ktype, &ui_dir->kobj,
2811 kobject_put(&kdamonds->kobj);
2814 ui_dir->kdamonds = kdamonds;
2818 static void damon_sysfs_ui_dir_release(struct kobject *kobj)
2820 kfree(container_of(kobj, struct damon_sysfs_ui_dir, kobj));
2823 static struct attribute *damon_sysfs_ui_dir_attrs[] = {
2826 ATTRIBUTE_GROUPS(damon_sysfs_ui_dir);
2828 static struct kobj_type damon_sysfs_ui_dir_ktype = {
2829 .release = damon_sysfs_ui_dir_release,
2830 .sysfs_ops = &kobj_sysfs_ops,
2831 .default_groups = damon_sysfs_ui_dir_groups,
2834 static int __init damon_sysfs_init(void)
2836 struct kobject *damon_sysfs_root;
2837 struct damon_sysfs_ui_dir *admin;
2840 damon_sysfs_root = kobject_create_and_add("damon", mm_kobj);
2841 if (!damon_sysfs_root)
2844 admin = damon_sysfs_ui_dir_alloc();
2846 kobject_put(damon_sysfs_root);
2849 err = kobject_init_and_add(&admin->kobj, &damon_sysfs_ui_dir_ktype,
2850 damon_sysfs_root, "admin");
2853 err = damon_sysfs_ui_dir_add_dirs(admin);
2859 kobject_put(&admin->kobj);
2860 kobject_put(damon_sysfs_root);
2863 subsys_initcall(damon_sysfs_init);