Merge tag 'firewire-updates-6.8' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / mm / damon / sysfs.c
CommitLineData
c951cd3b
SP
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * DAMON sysfs Interface
4 *
5 * Copyright (c) 2022 SeongJae Park <sj@kernel.org>
6 */
7
c951cd3b
SP
8#include <linux/pid.h>
9#include <linux/sched.h>
10#include <linux/slab.h>
11
39240595 12#include "sysfs-common.h"
c951cd3b 13
2031b14e
SP
14/*
15 * init region directory
16 */
17
18struct damon_sysfs_region {
19 struct kobject kobj;
789a2306 20 struct damon_addr_range ar;
2031b14e
SP
21};
22
1f719814 23static struct damon_sysfs_region *damon_sysfs_region_alloc(void)
2031b14e 24{
1f719814 25 return kzalloc(sizeof(struct damon_sysfs_region), GFP_KERNEL);
2031b14e
SP
26}
27
28static ssize_t start_show(struct kobject *kobj, struct kobj_attribute *attr,
29 char *buf)
30{
31 struct damon_sysfs_region *region = container_of(kobj,
32 struct damon_sysfs_region, kobj);
33
789a2306 34 return sysfs_emit(buf, "%lu\n", region->ar.start);
2031b14e
SP
35}
36
37static ssize_t start_store(struct kobject *kobj, struct kobj_attribute *attr,
38 const char *buf, size_t count)
39{
40 struct damon_sysfs_region *region = container_of(kobj,
41 struct damon_sysfs_region, kobj);
789a2306 42 int err = kstrtoul(buf, 0, &region->ar.start);
2031b14e 43
30b6242c 44 return err ? err : count;
2031b14e
SP
45}
46
47static ssize_t end_show(struct kobject *kobj, struct kobj_attribute *attr,
48 char *buf)
49{
50 struct damon_sysfs_region *region = container_of(kobj,
51 struct damon_sysfs_region, kobj);
52
789a2306 53 return sysfs_emit(buf, "%lu\n", region->ar.end);
2031b14e
SP
54}
55
56static ssize_t end_store(struct kobject *kobj, struct kobj_attribute *attr,
57 const char *buf, size_t count)
58{
59 struct damon_sysfs_region *region = container_of(kobj,
60 struct damon_sysfs_region, kobj);
789a2306 61 int err = kstrtoul(buf, 0, &region->ar.end);
2031b14e 62
30b6242c 63 return err ? err : count;
2031b14e
SP
64}
65
66static void damon_sysfs_region_release(struct kobject *kobj)
67{
68 kfree(container_of(kobj, struct damon_sysfs_region, kobj));
69}
70
71static struct kobj_attribute damon_sysfs_region_start_attr =
72 __ATTR_RW_MODE(start, 0600);
73
74static struct kobj_attribute damon_sysfs_region_end_attr =
75 __ATTR_RW_MODE(end, 0600);
76
77static struct attribute *damon_sysfs_region_attrs[] = {
78 &damon_sysfs_region_start_attr.attr,
79 &damon_sysfs_region_end_attr.attr,
80 NULL,
81};
82ATTRIBUTE_GROUPS(damon_sysfs_region);
83
e56397e8 84static const struct kobj_type damon_sysfs_region_ktype = {
2031b14e
SP
85 .release = damon_sysfs_region_release,
86 .sysfs_ops = &kobj_sysfs_ops,
87 .default_groups = damon_sysfs_region_groups,
88};
89
90/*
91 * init_regions directory
92 */
93
94struct damon_sysfs_regions {
95 struct kobject kobj;
96 struct damon_sysfs_region **regions_arr;
97 int nr;
98};
99
100static struct damon_sysfs_regions *damon_sysfs_regions_alloc(void)
101{
102 return kzalloc(sizeof(struct damon_sysfs_regions), GFP_KERNEL);
103}
104
105static void damon_sysfs_regions_rm_dirs(struct damon_sysfs_regions *regions)
106{
107 struct damon_sysfs_region **regions_arr = regions->regions_arr;
108 int i;
109
110 for (i = 0; i < regions->nr; i++)
111 kobject_put(&regions_arr[i]->kobj);
112 regions->nr = 0;
113 kfree(regions_arr);
114 regions->regions_arr = NULL;
115}
116
117static int damon_sysfs_regions_add_dirs(struct damon_sysfs_regions *regions,
118 int nr_regions)
119{
120 struct damon_sysfs_region **regions_arr, *region;
121 int err, i;
122
123 damon_sysfs_regions_rm_dirs(regions);
124 if (!nr_regions)
125 return 0;
126
127 regions_arr = kmalloc_array(nr_regions, sizeof(*regions_arr),
128 GFP_KERNEL | __GFP_NOWARN);
129 if (!regions_arr)
130 return -ENOMEM;
131 regions->regions_arr = regions_arr;
132
133 for (i = 0; i < nr_regions; i++) {
1f719814 134 region = damon_sysfs_region_alloc();
2031b14e
SP
135 if (!region) {
136 damon_sysfs_regions_rm_dirs(regions);
137 return -ENOMEM;
138 }
139
140 err = kobject_init_and_add(&region->kobj,
141 &damon_sysfs_region_ktype, &regions->kobj,
142 "%d", i);
143 if (err) {
144 kobject_put(&region->kobj);
145 damon_sysfs_regions_rm_dirs(regions);
146 return err;
147 }
148
149 regions_arr[i] = region;
150 regions->nr++;
151 }
152 return 0;
153}
154
155static ssize_t nr_regions_show(struct kobject *kobj,
156 struct kobj_attribute *attr, char *buf)
157{
158 struct damon_sysfs_regions *regions = container_of(kobj,
159 struct damon_sysfs_regions, kobj);
160
161 return sysfs_emit(buf, "%d\n", regions->nr);
162}
163
164static ssize_t nr_regions_store(struct kobject *kobj,
165 struct kobj_attribute *attr, const char *buf, size_t count)
166{
a17a8b3b 167 struct damon_sysfs_regions *regions;
2031b14e
SP
168 int nr, err = kstrtoint(buf, 0, &nr);
169
170 if (err)
171 return err;
172 if (nr < 0)
173 return -EINVAL;
174
a17a8b3b
XH
175 regions = container_of(kobj, struct damon_sysfs_regions, kobj);
176
2031b14e
SP
177 if (!mutex_trylock(&damon_sysfs_lock))
178 return -EBUSY;
179 err = damon_sysfs_regions_add_dirs(regions, nr);
180 mutex_unlock(&damon_sysfs_lock);
181 if (err)
182 return err;
183
184 return count;
185}
186
187static void damon_sysfs_regions_release(struct kobject *kobj)
188{
189 kfree(container_of(kobj, struct damon_sysfs_regions, kobj));
190}
191
192static struct kobj_attribute damon_sysfs_regions_nr_attr =
193 __ATTR_RW_MODE(nr_regions, 0600);
194
195static struct attribute *damon_sysfs_regions_attrs[] = {
196 &damon_sysfs_regions_nr_attr.attr,
197 NULL,
198};
199ATTRIBUTE_GROUPS(damon_sysfs_regions);
200
e56397e8 201static const struct kobj_type damon_sysfs_regions_ktype = {
2031b14e
SP
202 .release = damon_sysfs_regions_release,
203 .sysfs_ops = &kobj_sysfs_ops,
204 .default_groups = damon_sysfs_regions_groups,
205};
206
c951cd3b
SP
207/*
208 * target directory
209 */
210
211struct damon_sysfs_target {
212 struct kobject kobj;
2031b14e 213 struct damon_sysfs_regions *regions;
c951cd3b
SP
214 int pid;
215};
216
217static struct damon_sysfs_target *damon_sysfs_target_alloc(void)
218{
219 return kzalloc(sizeof(struct damon_sysfs_target), GFP_KERNEL);
220}
221
2031b14e
SP
222static int damon_sysfs_target_add_dirs(struct damon_sysfs_target *target)
223{
224 struct damon_sysfs_regions *regions = damon_sysfs_regions_alloc();
225 int err;
226
227 if (!regions)
228 return -ENOMEM;
229
230 err = kobject_init_and_add(&regions->kobj, &damon_sysfs_regions_ktype,
231 &target->kobj, "regions");
232 if (err)
233 kobject_put(&regions->kobj);
234 else
235 target->regions = regions;
236 return err;
237}
238
239static void damon_sysfs_target_rm_dirs(struct damon_sysfs_target *target)
240{
241 damon_sysfs_regions_rm_dirs(target->regions);
242 kobject_put(&target->regions->kobj);
243}
244
c951cd3b
SP
245static ssize_t pid_target_show(struct kobject *kobj,
246 struct kobj_attribute *attr, char *buf)
247{
248 struct damon_sysfs_target *target = container_of(kobj,
249 struct damon_sysfs_target, kobj);
250
251 return sysfs_emit(buf, "%d\n", target->pid);
252}
253
254static ssize_t pid_target_store(struct kobject *kobj,
255 struct kobj_attribute *attr, const char *buf, size_t count)
256{
257 struct damon_sysfs_target *target = container_of(kobj,
258 struct damon_sysfs_target, kobj);
259 int err = kstrtoint(buf, 0, &target->pid);
260
261 if (err)
262 return -EINVAL;
263 return count;
264}
265
266static void damon_sysfs_target_release(struct kobject *kobj)
267{
268 kfree(container_of(kobj, struct damon_sysfs_target, kobj));
269}
270
271static struct kobj_attribute damon_sysfs_target_pid_attr =
272 __ATTR_RW_MODE(pid_target, 0600);
273
274static struct attribute *damon_sysfs_target_attrs[] = {
275 &damon_sysfs_target_pid_attr.attr,
276 NULL,
277};
278ATTRIBUTE_GROUPS(damon_sysfs_target);
279
e56397e8 280static const struct kobj_type damon_sysfs_target_ktype = {
c951cd3b
SP
281 .release = damon_sysfs_target_release,
282 .sysfs_ops = &kobj_sysfs_ops,
283 .default_groups = damon_sysfs_target_groups,
284};
285
286/*
287 * targets directory
288 */
289
290struct damon_sysfs_targets {
291 struct kobject kobj;
292 struct damon_sysfs_target **targets_arr;
293 int nr;
294};
295
296static struct damon_sysfs_targets *damon_sysfs_targets_alloc(void)
297{
298 return kzalloc(sizeof(struct damon_sysfs_targets), GFP_KERNEL);
299}
300
301static void damon_sysfs_targets_rm_dirs(struct damon_sysfs_targets *targets)
302{
303 struct damon_sysfs_target **targets_arr = targets->targets_arr;
304 int i;
305
2031b14e
SP
306 for (i = 0; i < targets->nr; i++) {
307 damon_sysfs_target_rm_dirs(targets_arr[i]);
c951cd3b 308 kobject_put(&targets_arr[i]->kobj);
2031b14e 309 }
c951cd3b
SP
310 targets->nr = 0;
311 kfree(targets_arr);
312 targets->targets_arr = NULL;
313}
314
315static int damon_sysfs_targets_add_dirs(struct damon_sysfs_targets *targets,
316 int nr_targets)
317{
318 struct damon_sysfs_target **targets_arr, *target;
319 int err, i;
320
321 damon_sysfs_targets_rm_dirs(targets);
322 if (!nr_targets)
323 return 0;
324
325 targets_arr = kmalloc_array(nr_targets, sizeof(*targets_arr),
326 GFP_KERNEL | __GFP_NOWARN);
327 if (!targets_arr)
328 return -ENOMEM;
329 targets->targets_arr = targets_arr;
330
331 for (i = 0; i < nr_targets; i++) {
332 target = damon_sysfs_target_alloc();
333 if (!target) {
334 damon_sysfs_targets_rm_dirs(targets);
335 return -ENOMEM;
336 }
337
338 err = kobject_init_and_add(&target->kobj,
339 &damon_sysfs_target_ktype, &targets->kobj,
340 "%d", i);
341 if (err)
342 goto out;
343
2031b14e
SP
344 err = damon_sysfs_target_add_dirs(target);
345 if (err)
346 goto out;
347
c951cd3b
SP
348 targets_arr[i] = target;
349 targets->nr++;
350 }
351 return 0;
352
353out:
354 damon_sysfs_targets_rm_dirs(targets);
355 kobject_put(&target->kobj);
356 return err;
357}
358
359static ssize_t nr_targets_show(struct kobject *kobj,
360 struct kobj_attribute *attr, char *buf)
361{
362 struct damon_sysfs_targets *targets = container_of(kobj,
363 struct damon_sysfs_targets, kobj);
364
365 return sysfs_emit(buf, "%d\n", targets->nr);
366}
367
368static ssize_t nr_targets_store(struct kobject *kobj,
369 struct kobj_attribute *attr, const char *buf, size_t count)
370{
a17a8b3b 371 struct damon_sysfs_targets *targets;
c951cd3b
SP
372 int nr, err = kstrtoint(buf, 0, &nr);
373
374 if (err)
375 return err;
376 if (nr < 0)
377 return -EINVAL;
378
a17a8b3b
XH
379 targets = container_of(kobj, struct damon_sysfs_targets, kobj);
380
c951cd3b
SP
381 if (!mutex_trylock(&damon_sysfs_lock))
382 return -EBUSY;
383 err = damon_sysfs_targets_add_dirs(targets, nr);
384 mutex_unlock(&damon_sysfs_lock);
385 if (err)
386 return err;
387
388 return count;
389}
390
391static void damon_sysfs_targets_release(struct kobject *kobj)
392{
393 kfree(container_of(kobj, struct damon_sysfs_targets, kobj));
394}
395
396static struct kobj_attribute damon_sysfs_targets_nr_attr =
397 __ATTR_RW_MODE(nr_targets, 0600);
398
399static struct attribute *damon_sysfs_targets_attrs[] = {
400 &damon_sysfs_targets_nr_attr.attr,
401 NULL,
402};
403ATTRIBUTE_GROUPS(damon_sysfs_targets);
404
e56397e8 405static const struct kobj_type damon_sysfs_targets_ktype = {
c951cd3b
SP
406 .release = damon_sysfs_targets_release,
407 .sysfs_ops = &kobj_sysfs_ops,
408 .default_groups = damon_sysfs_targets_groups,
409};
410
411/*
412 * intervals directory
413 */
414
415struct damon_sysfs_intervals {
416 struct kobject kobj;
417 unsigned long sample_us;
418 unsigned long aggr_us;
419 unsigned long update_us;
420};
421
422static struct damon_sysfs_intervals *damon_sysfs_intervals_alloc(
423 unsigned long sample_us, unsigned long aggr_us,
424 unsigned long update_us)
425{
426 struct damon_sysfs_intervals *intervals = kmalloc(sizeof(*intervals),
427 GFP_KERNEL);
428
429 if (!intervals)
430 return NULL;
431
432 intervals->kobj = (struct kobject){};
433 intervals->sample_us = sample_us;
434 intervals->aggr_us = aggr_us;
435 intervals->update_us = update_us;
436 return intervals;
437}
438
439static ssize_t sample_us_show(struct kobject *kobj,
440 struct kobj_attribute *attr, char *buf)
441{
442 struct damon_sysfs_intervals *intervals = container_of(kobj,
443 struct damon_sysfs_intervals, kobj);
444
445 return sysfs_emit(buf, "%lu\n", intervals->sample_us);
446}
447
448static ssize_t sample_us_store(struct kobject *kobj,
449 struct kobj_attribute *attr, const char *buf, size_t count)
450{
451 struct damon_sysfs_intervals *intervals = container_of(kobj,
452 struct damon_sysfs_intervals, kobj);
453 unsigned long us;
454 int err = kstrtoul(buf, 0, &us);
455
456 if (err)
30b6242c 457 return err;
c951cd3b
SP
458
459 intervals->sample_us = us;
460 return count;
461}
462
463static ssize_t aggr_us_show(struct kobject *kobj, struct kobj_attribute *attr,
464 char *buf)
465{
466 struct damon_sysfs_intervals *intervals = container_of(kobj,
467 struct damon_sysfs_intervals, kobj);
468
469 return sysfs_emit(buf, "%lu\n", intervals->aggr_us);
470}
471
472static ssize_t aggr_us_store(struct kobject *kobj, struct kobj_attribute *attr,
473 const char *buf, size_t count)
474{
475 struct damon_sysfs_intervals *intervals = container_of(kobj,
476 struct damon_sysfs_intervals, kobj);
477 unsigned long us;
478 int err = kstrtoul(buf, 0, &us);
479
480 if (err)
30b6242c 481 return err;
c951cd3b
SP
482
483 intervals->aggr_us = us;
484 return count;
485}
486
487static ssize_t update_us_show(struct kobject *kobj,
488 struct kobj_attribute *attr, char *buf)
489{
490 struct damon_sysfs_intervals *intervals = container_of(kobj,
491 struct damon_sysfs_intervals, kobj);
492
493 return sysfs_emit(buf, "%lu\n", intervals->update_us);
494}
495
496static ssize_t update_us_store(struct kobject *kobj,
497 struct kobj_attribute *attr, const char *buf, size_t count)
498{
499 struct damon_sysfs_intervals *intervals = container_of(kobj,
500 struct damon_sysfs_intervals, kobj);
501 unsigned long us;
502 int err = kstrtoul(buf, 0, &us);
503
504 if (err)
30b6242c 505 return err;
c951cd3b
SP
506
507 intervals->update_us = us;
508 return count;
509}
510
511static void damon_sysfs_intervals_release(struct kobject *kobj)
512{
513 kfree(container_of(kobj, struct damon_sysfs_intervals, kobj));
514}
515
516static struct kobj_attribute damon_sysfs_intervals_sample_us_attr =
517 __ATTR_RW_MODE(sample_us, 0600);
518
519static struct kobj_attribute damon_sysfs_intervals_aggr_us_attr =
520 __ATTR_RW_MODE(aggr_us, 0600);
521
522static struct kobj_attribute damon_sysfs_intervals_update_us_attr =
523 __ATTR_RW_MODE(update_us, 0600);
524
525static struct attribute *damon_sysfs_intervals_attrs[] = {
526 &damon_sysfs_intervals_sample_us_attr.attr,
527 &damon_sysfs_intervals_aggr_us_attr.attr,
528 &damon_sysfs_intervals_update_us_attr.attr,
529 NULL,
530};
531ATTRIBUTE_GROUPS(damon_sysfs_intervals);
532
e56397e8 533static const struct kobj_type damon_sysfs_intervals_ktype = {
c951cd3b
SP
534 .release = damon_sysfs_intervals_release,
535 .sysfs_ops = &kobj_sysfs_ops,
536 .default_groups = damon_sysfs_intervals_groups,
537};
538
539/*
540 * monitoring_attrs directory
541 */
542
543struct damon_sysfs_attrs {
544 struct kobject kobj;
545 struct damon_sysfs_intervals *intervals;
546 struct damon_sysfs_ul_range *nr_regions_range;
547};
548
549static struct damon_sysfs_attrs *damon_sysfs_attrs_alloc(void)
550{
551 struct damon_sysfs_attrs *attrs = kmalloc(sizeof(*attrs), GFP_KERNEL);
552
553 if (!attrs)
554 return NULL;
555 attrs->kobj = (struct kobject){};
556 return attrs;
557}
558
559static int damon_sysfs_attrs_add_dirs(struct damon_sysfs_attrs *attrs)
560{
561 struct damon_sysfs_intervals *intervals;
562 struct damon_sysfs_ul_range *nr_regions_range;
563 int err;
564
565 intervals = damon_sysfs_intervals_alloc(5000, 100000, 60000000);
566 if (!intervals)
567 return -ENOMEM;
568
569 err = kobject_init_and_add(&intervals->kobj,
570 &damon_sysfs_intervals_ktype, &attrs->kobj,
571 "intervals");
572 if (err)
573 goto put_intervals_out;
574 attrs->intervals = intervals;
575
576 nr_regions_range = damon_sysfs_ul_range_alloc(10, 1000);
577 if (!nr_regions_range) {
578 err = -ENOMEM;
579 goto put_intervals_out;
580 }
581
582 err = kobject_init_and_add(&nr_regions_range->kobj,
583 &damon_sysfs_ul_range_ktype, &attrs->kobj,
584 "nr_regions");
585 if (err)
586 goto put_nr_regions_intervals_out;
587 attrs->nr_regions_range = nr_regions_range;
588 return 0;
589
590put_nr_regions_intervals_out:
591 kobject_put(&nr_regions_range->kobj);
592 attrs->nr_regions_range = NULL;
593put_intervals_out:
594 kobject_put(&intervals->kobj);
595 attrs->intervals = NULL;
596 return err;
597}
598
599static void damon_sysfs_attrs_rm_dirs(struct damon_sysfs_attrs *attrs)
600{
601 kobject_put(&attrs->nr_regions_range->kobj);
602 kobject_put(&attrs->intervals->kobj);
603}
604
605static void damon_sysfs_attrs_release(struct kobject *kobj)
606{
607 kfree(container_of(kobj, struct damon_sysfs_attrs, kobj));
608}
609
610static struct attribute *damon_sysfs_attrs_attrs[] = {
611 NULL,
612};
613ATTRIBUTE_GROUPS(damon_sysfs_attrs);
614
e56397e8 615static const struct kobj_type damon_sysfs_attrs_ktype = {
c951cd3b
SP
616 .release = damon_sysfs_attrs_release,
617 .sysfs_ops = &kobj_sysfs_ops,
618 .default_groups = damon_sysfs_attrs_groups,
619};
620
621/*
622 * context directory
623 */
624
625/* This should match with enum damon_ops_id */
626static const char * const damon_sysfs_ops_strs[] = {
627 "vaddr",
b8243447 628 "fvaddr",
c951cd3b
SP
629 "paddr",
630};
631
632struct damon_sysfs_context {
633 struct kobject kobj;
634 enum damon_ops_id ops_id;
635 struct damon_sysfs_attrs *attrs;
636 struct damon_sysfs_targets *targets;
7e84b1f8 637 struct damon_sysfs_schemes *schemes;
c951cd3b
SP
638};
639
640static struct damon_sysfs_context *damon_sysfs_context_alloc(
641 enum damon_ops_id ops_id)
642{
643 struct damon_sysfs_context *context = kmalloc(sizeof(*context),
644 GFP_KERNEL);
645
646 if (!context)
647 return NULL;
648 context->kobj = (struct kobject){};
649 context->ops_id = ops_id;
650 return context;
651}
652
653static int damon_sysfs_context_set_attrs(struct damon_sysfs_context *context)
654{
655 struct damon_sysfs_attrs *attrs = damon_sysfs_attrs_alloc();
656 int err;
657
658 if (!attrs)
659 return -ENOMEM;
660 err = kobject_init_and_add(&attrs->kobj, &damon_sysfs_attrs_ktype,
661 &context->kobj, "monitoring_attrs");
662 if (err)
663 goto out;
664 err = damon_sysfs_attrs_add_dirs(attrs);
665 if (err)
666 goto out;
667 context->attrs = attrs;
668 return 0;
669
670out:
671 kobject_put(&attrs->kobj);
672 return err;
673}
674
675static int damon_sysfs_context_set_targets(struct damon_sysfs_context *context)
676{
677 struct damon_sysfs_targets *targets = damon_sysfs_targets_alloc();
678 int err;
679
680 if (!targets)
681 return -ENOMEM;
682 err = kobject_init_and_add(&targets->kobj, &damon_sysfs_targets_ktype,
683 &context->kobj, "targets");
684 if (err) {
685 kobject_put(&targets->kobj);
686 return err;
687 }
688 context->targets = targets;
689 return 0;
690}
691
7e84b1f8
SP
692static int damon_sysfs_context_set_schemes(struct damon_sysfs_context *context)
693{
694 struct damon_sysfs_schemes *schemes = damon_sysfs_schemes_alloc();
695 int err;
696
697 if (!schemes)
698 return -ENOMEM;
699 err = kobject_init_and_add(&schemes->kobj, &damon_sysfs_schemes_ktype,
700 &context->kobj, "schemes");
701 if (err) {
702 kobject_put(&schemes->kobj);
703 return err;
704 }
705 context->schemes = schemes;
706 return 0;
707}
708
c951cd3b
SP
709static int damon_sysfs_context_add_dirs(struct damon_sysfs_context *context)
710{
711 int err;
712
713 err = damon_sysfs_context_set_attrs(context);
714 if (err)
715 return err;
716
717 err = damon_sysfs_context_set_targets(context);
718 if (err)
719 goto put_attrs_out;
7e84b1f8
SP
720
721 err = damon_sysfs_context_set_schemes(context);
722 if (err)
723 goto put_targets_attrs_out;
c951cd3b
SP
724 return 0;
725
7e84b1f8
SP
726put_targets_attrs_out:
727 kobject_put(&context->targets->kobj);
728 context->targets = NULL;
c951cd3b
SP
729put_attrs_out:
730 kobject_put(&context->attrs->kobj);
731 context->attrs = NULL;
732 return err;
733}
734
735static void damon_sysfs_context_rm_dirs(struct damon_sysfs_context *context)
736{
737 damon_sysfs_attrs_rm_dirs(context->attrs);
738 kobject_put(&context->attrs->kobj);
739 damon_sysfs_targets_rm_dirs(context->targets);
740 kobject_put(&context->targets->kobj);
7e84b1f8
SP
741 damon_sysfs_schemes_rm_dirs(context->schemes);
742 kobject_put(&context->schemes->kobj);
c951cd3b
SP
743}
744
0f2cb588
SP
745static ssize_t avail_operations_show(struct kobject *kobj,
746 struct kobj_attribute *attr, char *buf)
747{
748 enum damon_ops_id id;
749 int len = 0;
750
751 for (id = 0; id < NR_DAMON_OPS; id++) {
752 if (!damon_is_registered_ops(id))
753 continue;
754 len += sysfs_emit_at(buf, len, "%s\n",
755 damon_sysfs_ops_strs[id]);
756 }
757 return len;
758}
759
c951cd3b
SP
760static ssize_t operations_show(struct kobject *kobj,
761 struct kobj_attribute *attr, char *buf)
762{
763 struct damon_sysfs_context *context = container_of(kobj,
764 struct damon_sysfs_context, kobj);
765
766 return sysfs_emit(buf, "%s\n", damon_sysfs_ops_strs[context->ops_id]);
767}
768
769static ssize_t operations_store(struct kobject *kobj,
770 struct kobj_attribute *attr, const char *buf, size_t count)
771{
772 struct damon_sysfs_context *context = container_of(kobj,
773 struct damon_sysfs_context, kobj);
774 enum damon_ops_id id;
775
776 for (id = 0; id < NR_DAMON_OPS; id++) {
777 if (sysfs_streq(buf, damon_sysfs_ops_strs[id])) {
c951cd3b
SP
778 context->ops_id = id;
779 return count;
780 }
781 }
782 return -EINVAL;
783}
784
785static void damon_sysfs_context_release(struct kobject *kobj)
786{
787 kfree(container_of(kobj, struct damon_sysfs_context, kobj));
788}
789
0f2cb588
SP
790static struct kobj_attribute damon_sysfs_context_avail_operations_attr =
791 __ATTR_RO_MODE(avail_operations, 0400);
792
c951cd3b
SP
793static struct kobj_attribute damon_sysfs_context_operations_attr =
794 __ATTR_RW_MODE(operations, 0600);
795
796static struct attribute *damon_sysfs_context_attrs[] = {
0f2cb588 797 &damon_sysfs_context_avail_operations_attr.attr,
c951cd3b
SP
798 &damon_sysfs_context_operations_attr.attr,
799 NULL,
800};
801ATTRIBUTE_GROUPS(damon_sysfs_context);
802
e56397e8 803static const struct kobj_type damon_sysfs_context_ktype = {
c951cd3b
SP
804 .release = damon_sysfs_context_release,
805 .sysfs_ops = &kobj_sysfs_ops,
806 .default_groups = damon_sysfs_context_groups,
807};
808
809/*
810 * contexts directory
811 */
812
813struct damon_sysfs_contexts {
814 struct kobject kobj;
815 struct damon_sysfs_context **contexts_arr;
816 int nr;
817};
818
819static struct damon_sysfs_contexts *damon_sysfs_contexts_alloc(void)
820{
821 return kzalloc(sizeof(struct damon_sysfs_contexts), GFP_KERNEL);
822}
823
824static void damon_sysfs_contexts_rm_dirs(struct damon_sysfs_contexts *contexts)
825{
826 struct damon_sysfs_context **contexts_arr = contexts->contexts_arr;
827 int i;
828
829 for (i = 0; i < contexts->nr; i++) {
830 damon_sysfs_context_rm_dirs(contexts_arr[i]);
831 kobject_put(&contexts_arr[i]->kobj);
832 }
833 contexts->nr = 0;
834 kfree(contexts_arr);
835 contexts->contexts_arr = NULL;
836}
837
838static int damon_sysfs_contexts_add_dirs(struct damon_sysfs_contexts *contexts,
839 int nr_contexts)
840{
841 struct damon_sysfs_context **contexts_arr, *context;
842 int err, i;
843
844 damon_sysfs_contexts_rm_dirs(contexts);
845 if (!nr_contexts)
846 return 0;
847
848 contexts_arr = kmalloc_array(nr_contexts, sizeof(*contexts_arr),
849 GFP_KERNEL | __GFP_NOWARN);
850 if (!contexts_arr)
851 return -ENOMEM;
852 contexts->contexts_arr = contexts_arr;
853
854 for (i = 0; i < nr_contexts; i++) {
855 context = damon_sysfs_context_alloc(DAMON_OPS_VADDR);
856 if (!context) {
857 damon_sysfs_contexts_rm_dirs(contexts);
858 return -ENOMEM;
859 }
860
861 err = kobject_init_and_add(&context->kobj,
862 &damon_sysfs_context_ktype, &contexts->kobj,
863 "%d", i);
864 if (err)
865 goto out;
866
867 err = damon_sysfs_context_add_dirs(context);
868 if (err)
869 goto out;
870
871 contexts_arr[i] = context;
872 contexts->nr++;
873 }
874 return 0;
875
876out:
877 damon_sysfs_contexts_rm_dirs(contexts);
878 kobject_put(&context->kobj);
879 return err;
880}
881
882static ssize_t nr_contexts_show(struct kobject *kobj,
883 struct kobj_attribute *attr, char *buf)
884{
885 struct damon_sysfs_contexts *contexts = container_of(kobj,
886 struct damon_sysfs_contexts, kobj);
887
888 return sysfs_emit(buf, "%d\n", contexts->nr);
889}
890
891static ssize_t nr_contexts_store(struct kobject *kobj,
892 struct kobj_attribute *attr, const char *buf, size_t count)
893{
a17a8b3b 894 struct damon_sysfs_contexts *contexts;
c951cd3b
SP
895 int nr, err;
896
897 err = kstrtoint(buf, 0, &nr);
898 if (err)
899 return err;
900 /* TODO: support multiple contexts per kdamond */
901 if (nr < 0 || 1 < nr)
902 return -EINVAL;
903
a17a8b3b 904 contexts = container_of(kobj, struct damon_sysfs_contexts, kobj);
c951cd3b
SP
905 if (!mutex_trylock(&damon_sysfs_lock))
906 return -EBUSY;
907 err = damon_sysfs_contexts_add_dirs(contexts, nr);
908 mutex_unlock(&damon_sysfs_lock);
909 if (err)
910 return err;
911
912 return count;
913}
914
915static void damon_sysfs_contexts_release(struct kobject *kobj)
916{
917 kfree(container_of(kobj, struct damon_sysfs_contexts, kobj));
918}
919
920static struct kobj_attribute damon_sysfs_contexts_nr_attr
921 = __ATTR_RW_MODE(nr_contexts, 0600);
922
923static struct attribute *damon_sysfs_contexts_attrs[] = {
924 &damon_sysfs_contexts_nr_attr.attr,
925 NULL,
926};
927ATTRIBUTE_GROUPS(damon_sysfs_contexts);
928
e56397e8 929static const struct kobj_type damon_sysfs_contexts_ktype = {
c951cd3b
SP
930 .release = damon_sysfs_contexts_release,
931 .sysfs_ops = &kobj_sysfs_ops,
932 .default_groups = damon_sysfs_contexts_groups,
933};
934
935/*
936 * kdamond directory
937 */
938
939struct damon_sysfs_kdamond {
940 struct kobject kobj;
941 struct damon_sysfs_contexts *contexts;
942 struct damon_ctx *damon_ctx;
943};
944
945static struct damon_sysfs_kdamond *damon_sysfs_kdamond_alloc(void)
946{
947 return kzalloc(sizeof(struct damon_sysfs_kdamond), GFP_KERNEL);
948}
949
950static int damon_sysfs_kdamond_add_dirs(struct damon_sysfs_kdamond *kdamond)
951{
952 struct damon_sysfs_contexts *contexts;
953 int err;
954
955 contexts = damon_sysfs_contexts_alloc();
956 if (!contexts)
957 return -ENOMEM;
958
959 err = kobject_init_and_add(&contexts->kobj,
960 &damon_sysfs_contexts_ktype, &kdamond->kobj,
961 "contexts");
962 if (err) {
963 kobject_put(&contexts->kobj);
964 return err;
965 }
966 kdamond->contexts = contexts;
967
968 return err;
969}
970
971static void damon_sysfs_kdamond_rm_dirs(struct damon_sysfs_kdamond *kdamond)
972{
973 damon_sysfs_contexts_rm_dirs(kdamond->contexts);
974 kobject_put(&kdamond->contexts->kobj);
975}
976
a61ea561
SP
977static bool damon_sysfs_ctx_running(struct damon_ctx *ctx)
978{
979 bool running;
980
981 mutex_lock(&ctx->kdamond_lock);
982 running = ctx->kdamond != NULL;
983 mutex_unlock(&ctx->kdamond_lock);
984 return running;
985}
986
3cbab4ca
SP
987/*
988 * enum damon_sysfs_cmd - Commands for a specific kdamond.
989 */
990enum damon_sysfs_cmd {
991 /* @DAMON_SYSFS_CMD_ON: Turn the kdamond on. */
992 DAMON_SYSFS_CMD_ON,
993 /* @DAMON_SYSFS_CMD_OFF: Turn the kdamond off. */
994 DAMON_SYSFS_CMD_OFF,
da878780
SP
995 /* @DAMON_SYSFS_CMD_COMMIT: Update kdamond inputs. */
996 DAMON_SYSFS_CMD_COMMIT,
d91beaa5
SP
997 /*
998 * @DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUOTA_GOALS: Commit the quota goals
999 * to DAMON.
1000 */
1001 DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUOTA_GOALS,
3cbab4ca
SP
1002 /*
1003 * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: Update scheme stats sysfs
1004 * files.
1005 */
1006 DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS,
6ad243b8
SP
1007 /*
1008 * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES: Update
1009 * tried_regions/total_bytes sysfs files for each scheme.
1010 */
1011 DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES,
f1d13cac
SP
1012 /*
1013 * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS: Update schemes tried
1014 * regions
1015 */
1016 DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS,
772c15e5
SP
1017 /*
1018 * @DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS: Clear schemes tried
1019 * regions
1020 */
1021 DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS,
3cbab4ca
SP
1022 /*
1023 * @NR_DAMON_SYSFS_CMDS: Total number of DAMON sysfs commands.
1024 */
1025 NR_DAMON_SYSFS_CMDS,
1026};
1027
1028/* Should match with enum damon_sysfs_cmd */
1029static const char * const damon_sysfs_cmd_strs[] = {
1030 "on",
1031 "off",
da878780 1032 "commit",
d91beaa5 1033 "commit_schemes_quota_goals",
3cbab4ca 1034 "update_schemes_stats",
6ad243b8 1035 "update_schemes_tried_bytes",
f1d13cac 1036 "update_schemes_tried_regions",
772c15e5 1037 "clear_schemes_tried_regions",
3cbab4ca
SP
1038};
1039
01538719
SP
1040/*
1041 * struct damon_sysfs_cmd_request - A request to the DAMON callback.
1042 * @cmd: The command that needs to be handled by the callback.
1043 * @kdamond: The kobject wrapper that associated to the kdamond thread.
1044 *
1045 * This structure represents a sysfs command request that need to access some
1046 * DAMON context-internal data. Because DAMON context-internal data can be
1047 * safely accessed from DAMON callbacks without additional synchronization, the
1048 * request will be handled by the DAMON callback. None-``NULL`` @kdamond means
1049 * the request is valid.
1050 */
1051struct damon_sysfs_cmd_request {
1052 enum damon_sysfs_cmd cmd;
1053 struct damon_sysfs_kdamond *kdamond;
1054};
1055
1056/* Current DAMON callback request. Protected by damon_sysfs_lock. */
1057static struct damon_sysfs_cmd_request damon_sysfs_cmd_request;
1058
c951cd3b
SP
1059static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
1060 char *buf)
1061{
a61ea561
SP
1062 struct damon_sysfs_kdamond *kdamond = container_of(kobj,
1063 struct damon_sysfs_kdamond, kobj);
1064 struct damon_ctx *ctx = kdamond->damon_ctx;
1065 bool running;
1066
1067 if (!ctx)
1068 running = false;
1069 else
1070 running = damon_sysfs_ctx_running(ctx);
1071
3cbab4ca
SP
1072 return sysfs_emit(buf, "%s\n", running ?
1073 damon_sysfs_cmd_strs[DAMON_SYSFS_CMD_ON] :
1074 damon_sysfs_cmd_strs[DAMON_SYSFS_CMD_OFF]);
a61ea561
SP
1075}
1076
1077static int damon_sysfs_set_attrs(struct damon_ctx *ctx,
1078 struct damon_sysfs_attrs *sys_attrs)
1079{
1080 struct damon_sysfs_intervals *sys_intervals = sys_attrs->intervals;
1081 struct damon_sysfs_ul_range *sys_nr_regions =
1082 sys_attrs->nr_regions_range;
bead3b00
SP
1083 struct damon_attrs attrs = {
1084 .sample_interval = sys_intervals->sample_us,
1085 .aggr_interval = sys_intervals->aggr_us,
1086 .ops_update_interval = sys_intervals->update_us,
1087 .min_nr_regions = sys_nr_regions->min,
1088 .max_nr_regions = sys_nr_regions->max,
1089 };
1090 return damon_set_attrs(ctx, &attrs);
a61ea561
SP
1091}
1092
1093static void damon_sysfs_destroy_targets(struct damon_ctx *ctx)
1094{
1095 struct damon_target *t, *next;
871f697b 1096 bool has_pid = damon_target_has_pid(ctx);
a61ea561
SP
1097
1098 damon_for_each_target_safe(t, next, ctx) {
871f697b 1099 if (has_pid)
a61ea561
SP
1100 put_pid(t->pid);
1101 damon_destroy_target(t);
1102 }
1103}
1104
2031b14e
SP
1105static int damon_sysfs_set_regions(struct damon_target *t,
1106 struct damon_sysfs_regions *sysfs_regions)
1107{
97d482f4
SP
1108 struct damon_addr_range *ranges = kmalloc_array(sysfs_regions->nr,
1109 sizeof(*ranges), GFP_KERNEL | __GFP_NOWARN);
1110 int i, err = -EINVAL;
2031b14e 1111
97d482f4
SP
1112 if (!ranges)
1113 return -ENOMEM;
2031b14e
SP
1114 for (i = 0; i < sysfs_regions->nr; i++) {
1115 struct damon_sysfs_region *sys_region =
1116 sysfs_regions->regions_arr[i];
2031b14e 1117
789a2306 1118 if (sys_region->ar.start > sys_region->ar.end)
97d482f4
SP
1119 goto out;
1120
789a2306
SP
1121 ranges[i].start = sys_region->ar.start;
1122 ranges[i].end = sys_region->ar.end;
97d482f4
SP
1123 if (i == 0)
1124 continue;
1125 if (ranges[i - 1].end > ranges[i].start)
1126 goto out;
2031b14e 1127 }
97d482f4
SP
1128 err = damon_set_regions(t, ranges, sysfs_regions->nr);
1129out:
1130 kfree(ranges);
1131 return err;
1132
2031b14e
SP
1133}
1134
74bd8b7d
SP
1135static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target,
1136 struct damon_ctx *ctx)
1137{
1138 struct damon_target *t = damon_new_target();
1139 int err = -EINVAL;
1140
1141 if (!t)
1142 return -ENOMEM;
1c8e2349 1143 damon_add_target(ctx, t);
c9e124e0 1144 if (damon_target_has_pid(ctx)) {
74bd8b7d
SP
1145 t->pid = find_get_pid(sys_target->pid);
1146 if (!t->pid)
1147 goto destroy_targets_out;
1148 }
74bd8b7d
SP
1149 err = damon_sysfs_set_regions(t, sys_target->regions);
1150 if (err)
1151 goto destroy_targets_out;
1152 return 0;
1153
1154destroy_targets_out:
1155 damon_sysfs_destroy_targets(ctx);
1156 return err;
1157}
1158
97323360 1159static int damon_sysfs_update_target_pid(struct damon_target *target, int pid)
da878780 1160{
97323360 1161 struct pid *pid_new;
da878780 1162
97323360
SP
1163 pid_new = find_get_pid(pid);
1164 if (!pid_new)
19467a95
SP
1165 return -EINVAL;
1166
97323360
SP
1167 if (pid_new == target->pid) {
1168 put_pid(pid_new);
19467a95 1169 return 0;
da878780 1170 }
19467a95 1171
19467a95 1172 put_pid(target->pid);
97323360 1173 target->pid = pid_new;
19467a95 1174 return 0;
da878780
SP
1175}
1176
97323360
SP
1177static int damon_sysfs_update_target(struct damon_target *target,
1178 struct damon_ctx *ctx,
1179 struct damon_sysfs_target *sys_target)
1180{
85c2ceaa 1181 int err = 0;
97323360
SP
1182
1183 if (damon_target_has_pid(ctx)) {
1184 err = damon_sysfs_update_target_pid(target, sys_target->pid);
1185 if (err)
1186 return err;
1187 }
1188
1189 /*
1190 * Do monitoring target region boundary update only if one or more
1191 * regions are set by the user. This is for keeping current monitoring
1192 * target results and range easier, especially for dynamic monitoring
1193 * target regions update ops like 'vaddr'.
1194 */
1195 if (sys_target->regions->nr)
1196 err = damon_sysfs_set_regions(target, sys_target->regions);
1197 return err;
1198}
1199
a61ea561
SP
1200static int damon_sysfs_set_targets(struct damon_ctx *ctx,
1201 struct damon_sysfs_targets *sysfs_targets)
1202{
19467a95
SP
1203 struct damon_target *t, *next;
1204 int i = 0, err;
a61ea561 1205
0a890a9f
SP
1206 /* Multiple physical address space monitoring targets makes no sense */
1207 if (ctx->ops.id == DAMON_OPS_PADDR && sysfs_targets->nr > 1)
1208 return -EINVAL;
1209
19467a95
SP
1210 damon_for_each_target_safe(t, next, ctx) {
1211 if (i < sysfs_targets->nr) {
b4936b54 1212 err = damon_sysfs_update_target(t, ctx,
19467a95 1213 sysfs_targets->targets_arr[i]);
b4936b54
SP
1214 if (err)
1215 return err;
19467a95
SP
1216 } else {
1217 if (damon_target_has_pid(ctx))
1218 put_pid(t->pid);
1219 damon_destroy_target(t);
1220 }
1221 i++;
1222 }
1223
1224 for (; i < sysfs_targets->nr; i++) {
da878780 1225 struct damon_sysfs_target *st = sysfs_targets->targets_arr[i];
19467a95
SP
1226
1227 err = damon_sysfs_add_target(st, ctx);
74bd8b7d 1228 if (err)
2031b14e 1229 return err;
a61ea561
SP
1230 }
1231 return 0;
1232}
1233
76b7069b
SP
1234static bool damon_sysfs_schemes_regions_updating;
1235
a61ea561
SP
1236static void damon_sysfs_before_terminate(struct damon_ctx *ctx)
1237{
1238 struct damon_target *t, *next;
f1d13cac 1239 struct damon_sysfs_kdamond *kdamond;
6ad243b8 1240 enum damon_sysfs_cmd cmd;
f1d13cac
SP
1241
1242 /* damon_sysfs_schemes_update_regions_stop() might not yet called */
1243 kdamond = damon_sysfs_cmd_request.kdamond;
6ad243b8
SP
1244 cmd = damon_sysfs_cmd_request.cmd;
1245 if (kdamond && ctx == kdamond->damon_ctx &&
1246 (cmd == DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS ||
76b7069b
SP
1247 cmd == DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES) &&
1248 damon_sysfs_schemes_regions_updating) {
f1d13cac 1249 damon_sysfs_schemes_update_regions_stop(ctx);
76b7069b 1250 damon_sysfs_schemes_regions_updating = false;
f1d13cac
SP
1251 mutex_unlock(&damon_sysfs_lock);
1252 }
a61ea561 1253
61768a1b 1254 if (!damon_target_has_pid(ctx))
a61ea561
SP
1255 return;
1256
1257 mutex_lock(&ctx->kdamond_lock);
1258 damon_for_each_target_safe(t, next, ctx) {
1259 put_pid(t->pid);
1260 damon_destroy_target(t);
1261 }
1262 mutex_unlock(&ctx->kdamond_lock);
1263}
1264
4acd715f
SP
1265/*
1266 * damon_sysfs_upd_schemes_stats() - Update schemes stats sysfs files.
1267 * @kdamond: The kobject wrapper that associated to the kdamond thread.
1268 *
1269 * This function reads the schemes stats of specific kdamond and update the
1270 * related values for sysfs files. This function should be called from DAMON
1271 * callbacks while holding ``damon_syfs_lock``, to safely access the DAMON
1272 * contexts-internal data and DAMON sysfs variables.
1273 */
1274static int damon_sysfs_upd_schemes_stats(struct damon_sysfs_kdamond *kdamond)
1275{
1276 struct damon_ctx *ctx = kdamond->damon_ctx;
1277
1278 if (!ctx)
1279 return -EINVAL;
1280 damon_sysfs_schemes_update_stats(
1281 kdamond->contexts->contexts_arr[0]->schemes, ctx);
01538719
SP
1282 return 0;
1283}
1284
f1d13cac 1285static int damon_sysfs_upd_schemes_regions_start(
6ad243b8 1286 struct damon_sysfs_kdamond *kdamond, bool total_bytes_only)
f1d13cac
SP
1287{
1288 struct damon_ctx *ctx = kdamond->damon_ctx;
1289
1290 if (!ctx)
1291 return -EINVAL;
1292 return damon_sysfs_schemes_update_regions_start(
6ad243b8
SP
1293 kdamond->contexts->contexts_arr[0]->schemes, ctx,
1294 total_bytes_only);
f1d13cac
SP
1295}
1296
1297static int damon_sysfs_upd_schemes_regions_stop(
1298 struct damon_sysfs_kdamond *kdamond)
1299{
1300 struct damon_ctx *ctx = kdamond->damon_ctx;
1301
1302 if (!ctx)
1303 return -EINVAL;
1304 return damon_sysfs_schemes_update_regions_stop(ctx);
1305}
1306
772c15e5
SP
1307static int damon_sysfs_clear_schemes_regions(
1308 struct damon_sysfs_kdamond *kdamond)
1309{
1310 struct damon_ctx *ctx = kdamond->damon_ctx;
1311
1312 if (!ctx)
1313 return -EINVAL;
1314 return damon_sysfs_schemes_clear_regions(
1315 kdamond->contexts->contexts_arr[0]->schemes, ctx);
1316}
1317
da878780
SP
1318static inline bool damon_sysfs_kdamond_running(
1319 struct damon_sysfs_kdamond *kdamond)
1320{
1321 return kdamond->damon_ctx &&
1322 damon_sysfs_ctx_running(kdamond->damon_ctx);
1323}
1324
a79b68ee
SP
1325static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
1326 struct damon_sysfs_context *sys_ctx)
1327{
1328 int err;
1329
1330 err = damon_select_ops(ctx, sys_ctx->ops_id);
1331 if (err)
1332 return err;
1333 err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
1334 if (err)
1335 return err;
1336 err = damon_sysfs_set_targets(ctx, sys_ctx->targets);
1337 if (err)
1338 return err;
1339 return damon_sysfs_set_schemes(ctx, sys_ctx->schemes);
1340}
1341
da878780
SP
1342/*
1343 * damon_sysfs_commit_input() - Commit user inputs to a running kdamond.
1344 * @kdamond: The kobject wrapper for the associated kdamond.
1345 *
1346 * If the sysfs input is wrong, the kdamond will be terminated.
1347 */
1348static int damon_sysfs_commit_input(struct damon_sysfs_kdamond *kdamond)
1349{
da878780
SP
1350 if (!damon_sysfs_kdamond_running(kdamond))
1351 return -EINVAL;
1352 /* TODO: Support multiple contexts per kdamond */
1353 if (kdamond->contexts->nr != 1)
1354 return -EINVAL;
1355
a79b68ee
SP
1356 return damon_sysfs_apply_inputs(kdamond->damon_ctx,
1357 kdamond->contexts->contexts_arr[0]);
da878780
SP
1358}
1359
d91beaa5
SP
1360static int damon_sysfs_commit_schemes_quota_goals(
1361 struct damon_sysfs_kdamond *sysfs_kdamond)
1362{
1363 struct damon_ctx *ctx;
1364 struct damon_sysfs_context *sysfs_ctx;
1365
1366 if (!damon_sysfs_kdamond_running(sysfs_kdamond))
1367 return -EINVAL;
1368 /* TODO: Support multiple contexts per kdamond */
1369 if (sysfs_kdamond->contexts->nr != 1)
1370 return -EINVAL;
1371
1372 ctx = sysfs_kdamond->damon_ctx;
1373 sysfs_ctx = sysfs_kdamond->contexts->contexts_arr[0];
1374 damos_sysfs_set_quota_scores(sysfs_ctx->schemes, ctx);
1375 return 0;
1376}
1377
01538719
SP
1378/*
1379 * damon_sysfs_cmd_request_callback() - DAMON callback for handling requests.
76126332
SP
1380 * @c: The DAMON context of the callback.
1381 * @active: Whether @c is not deactivated due to watermarks.
01538719
SP
1382 *
1383 * This function is periodically called back from the kdamond thread for @c.
1384 * Then, it checks if there is a waiting DAMON sysfs request and handles it.
1385 */
76126332 1386static int damon_sysfs_cmd_request_callback(struct damon_ctx *c, bool active)
01538719
SP
1387{
1388 struct damon_sysfs_kdamond *kdamond;
6ad243b8 1389 bool total_bytes_only = false;
01538719
SP
1390 int err = 0;
1391
1392 /* avoid deadlock due to concurrent state_store('off') */
f1d13cac
SP
1393 if (!damon_sysfs_schemes_regions_updating &&
1394 !mutex_trylock(&damon_sysfs_lock))
01538719
SP
1395 return 0;
1396 kdamond = damon_sysfs_cmd_request.kdamond;
1397 if (!kdamond || kdamond->damon_ctx != c)
1398 goto out;
1399 switch (damon_sysfs_cmd_request.cmd) {
1400 case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS:
1401 err = damon_sysfs_upd_schemes_stats(kdamond);
1402 break;
da878780
SP
1403 case DAMON_SYSFS_CMD_COMMIT:
1404 err = damon_sysfs_commit_input(kdamond);
1405 break;
d91beaa5
SP
1406 case DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUOTA_GOALS:
1407 err = damon_sysfs_commit_schemes_quota_goals(kdamond);
1408 break;
6ad243b8
SP
1409 case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES:
1410 total_bytes_only = true;
1411 fallthrough;
f1d13cac
SP
1412 case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS:
1413 if (!damon_sysfs_schemes_regions_updating) {
6ad243b8
SP
1414 err = damon_sysfs_upd_schemes_regions_start(kdamond,
1415 total_bytes_only);
f1d13cac
SP
1416 if (!err) {
1417 damon_sysfs_schemes_regions_updating = true;
1418 goto keep_lock_out;
1419 }
1420 } else {
76126332
SP
1421 /*
1422 * Continue regions updating if DAMON is till
1423 * active and the update for all schemes is not
1424 * finished.
1425 */
1426 if (active && !damos_sysfs_regions_upd_done())
1427 goto keep_lock_out;
f1d13cac
SP
1428 err = damon_sysfs_upd_schemes_regions_stop(kdamond);
1429 damon_sysfs_schemes_regions_updating = false;
1430 }
1431 break;
772c15e5
SP
1432 case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS:
1433 err = damon_sysfs_clear_schemes_regions(kdamond);
1434 break;
01538719
SP
1435 default:
1436 break;
1437 }
1438 /* Mark the request as invalid now. */
1439 damon_sysfs_cmd_request.kdamond = NULL;
1440out:
f1d13cac
SP
1441 if (!damon_sysfs_schemes_regions_updating)
1442 mutex_unlock(&damon_sysfs_lock);
1443keep_lock_out:
01538719
SP
1444 return err;
1445}
1446
76126332
SP
1447static int damon_sysfs_after_wmarks_check(struct damon_ctx *c)
1448{
1449 /*
1450 * after_wmarks_check() is called back while the context is deactivated
1451 * by watermarks.
1452 */
1453 return damon_sysfs_cmd_request_callback(c, false);
1454}
1455
1456static int damon_sysfs_after_aggregation(struct damon_ctx *c)
1457{
1458 /*
1459 * after_aggregation() is called back only while the context is not
1460 * deactivated by watermarks.
1461 */
1462 return damon_sysfs_cmd_request_callback(c, true);
1463}
1464
a61ea561
SP
1465static struct damon_ctx *damon_sysfs_build_ctx(
1466 struct damon_sysfs_context *sys_ctx)
1467{
1468 struct damon_ctx *ctx = damon_new_ctx();
1469 int err;
1470
1471 if (!ctx)
1472 return ERR_PTR(-ENOMEM);
1473
a79b68ee
SP
1474 err = damon_sysfs_apply_inputs(ctx, sys_ctx);
1475 if (err) {
1476 damon_destroy_ctx(ctx);
1477 return ERR_PTR(err);
1478 }
a61ea561 1479
76126332
SP
1480 ctx->callback.after_wmarks_check = damon_sysfs_after_wmarks_check;
1481 ctx->callback.after_aggregation = damon_sysfs_after_aggregation;
a61ea561
SP
1482 ctx->callback.before_terminate = damon_sysfs_before_terminate;
1483 return ctx;
a61ea561
SP
1484}
1485
1486static int damon_sysfs_turn_damon_on(struct damon_sysfs_kdamond *kdamond)
1487{
1488 struct damon_ctx *ctx;
1489 int err;
1490
e7fcac4c 1491 if (damon_sysfs_kdamond_running(kdamond))
a61ea561 1492 return -EBUSY;
01538719
SP
1493 if (damon_sysfs_cmd_request.kdamond == kdamond)
1494 return -EBUSY;
a61ea561
SP
1495 /* TODO: support multiple contexts per kdamond */
1496 if (kdamond->contexts->nr != 1)
1497 return -EINVAL;
1498
1499 if (kdamond->damon_ctx)
1500 damon_destroy_ctx(kdamond->damon_ctx);
1501 kdamond->damon_ctx = NULL;
1502
1503 ctx = damon_sysfs_build_ctx(kdamond->contexts->contexts_arr[0]);
1504 if (IS_ERR(ctx))
1505 return PTR_ERR(ctx);
1506 err = damon_start(&ctx, 1, false);
1507 if (err) {
1508 damon_destroy_ctx(ctx);
1509 return err;
1510 }
1511 kdamond->damon_ctx = ctx;
1512 return err;
1513}
1514
1515static int damon_sysfs_turn_damon_off(struct damon_sysfs_kdamond *kdamond)
1516{
1517 if (!kdamond->damon_ctx)
1518 return -EINVAL;
1519 return damon_stop(&kdamond->damon_ctx, 1);
1520 /*
1521 * To allow users show final monitoring results of already turned-off
1522 * DAMON, we free kdamond->damon_ctx in next
1523 * damon_sysfs_turn_damon_on(), or kdamonds_nr_store()
1524 */
c951cd3b
SP
1525}
1526
3cbab4ca
SP
1527/*
1528 * damon_sysfs_handle_cmd() - Handle a command for a specific kdamond.
1529 * @cmd: The command to handle.
1530 * @kdamond: The kobject wrapper for the associated kdamond.
1531 *
01538719
SP
1532 * This function handles a DAMON sysfs command for a kdamond. For commands
1533 * that need to access running DAMON context-internal data, it requests
1534 * handling of the command to the DAMON callback
1535 * (@damon_sysfs_cmd_request_callback()) and wait until it is properly handled,
1536 * or the context is completed.
3cbab4ca
SP
1537 *
1538 * Return: 0 on success, negative error code otherwise.
1539 */
1540static int damon_sysfs_handle_cmd(enum damon_sysfs_cmd cmd,
1541 struct damon_sysfs_kdamond *kdamond)
1542{
01538719
SP
1543 bool need_wait = true;
1544
1545 /* Handle commands that doesn't access DAMON context-internal data */
3cbab4ca
SP
1546 switch (cmd) {
1547 case DAMON_SYSFS_CMD_ON:
1548 return damon_sysfs_turn_damon_on(kdamond);
1549 case DAMON_SYSFS_CMD_OFF:
1550 return damon_sysfs_turn_damon_off(kdamond);
3cbab4ca
SP
1551 default:
1552 break;
1553 }
01538719
SP
1554
1555 /* Pass the command to DAMON callback for safe DAMON context access */
1556 if (damon_sysfs_cmd_request.kdamond)
1557 return -EBUSY;
1558 if (!damon_sysfs_kdamond_running(kdamond))
1559 return -EINVAL;
1560 damon_sysfs_cmd_request.cmd = cmd;
1561 damon_sysfs_cmd_request.kdamond = kdamond;
1562
1563 /*
1564 * wait until damon_sysfs_cmd_request_callback() handles the request
1565 * from kdamond context
1566 */
1567 mutex_unlock(&damon_sysfs_lock);
1568 while (need_wait) {
1569 schedule_timeout_idle(msecs_to_jiffies(100));
1570 if (!mutex_trylock(&damon_sysfs_lock))
1571 continue;
1572 if (!damon_sysfs_cmd_request.kdamond) {
1573 /* damon_sysfs_cmd_request_callback() handled */
1574 need_wait = false;
1575 } else if (!damon_sysfs_kdamond_running(kdamond)) {
1576 /* kdamond has already finished */
1577 need_wait = false;
1578 damon_sysfs_cmd_request.kdamond = NULL;
1579 }
1580 mutex_unlock(&damon_sysfs_lock);
1581 }
1582 mutex_lock(&damon_sysfs_lock);
1583 return 0;
3cbab4ca
SP
1584}
1585
c951cd3b
SP
1586static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
1587 const char *buf, size_t count)
1588{
a61ea561
SP
1589 struct damon_sysfs_kdamond *kdamond = container_of(kobj,
1590 struct damon_sysfs_kdamond, kobj);
3cbab4ca
SP
1591 enum damon_sysfs_cmd cmd;
1592 ssize_t ret = -EINVAL;
a61ea561
SP
1593
1594 if (!mutex_trylock(&damon_sysfs_lock))
1595 return -EBUSY;
3cbab4ca
SP
1596 for (cmd = 0; cmd < NR_DAMON_SYSFS_CMDS; cmd++) {
1597 if (sysfs_streq(buf, damon_sysfs_cmd_strs[cmd])) {
1598 ret = damon_sysfs_handle_cmd(cmd, kdamond);
1599 break;
1600 }
1601 }
a61ea561
SP
1602 mutex_unlock(&damon_sysfs_lock);
1603 if (!ret)
1604 ret = count;
1605 return ret;
c951cd3b
SP
1606}
1607
1608static ssize_t pid_show(struct kobject *kobj,
1609 struct kobj_attribute *attr, char *buf)
1610{
a61ea561
SP
1611 struct damon_sysfs_kdamond *kdamond = container_of(kobj,
1612 struct damon_sysfs_kdamond, kobj);
1613 struct damon_ctx *ctx;
f1c71c28 1614 int pid = -1;
a61ea561
SP
1615
1616 if (!mutex_trylock(&damon_sysfs_lock))
1617 return -EBUSY;
1618 ctx = kdamond->damon_ctx;
f1c71c28 1619 if (!ctx)
a61ea561 1620 goto out;
f1c71c28 1621
a61ea561 1622 mutex_lock(&ctx->kdamond_lock);
f1c71c28 1623 if (ctx->kdamond)
a61ea561
SP
1624 pid = ctx->kdamond->pid;
1625 mutex_unlock(&ctx->kdamond_lock);
1626out:
1627 mutex_unlock(&damon_sysfs_lock);
1628 return sysfs_emit(buf, "%d\n", pid);
c951cd3b
SP
1629}
1630
1631static void damon_sysfs_kdamond_release(struct kobject *kobj)
1632{
1633 struct damon_sysfs_kdamond *kdamond = container_of(kobj,
1634 struct damon_sysfs_kdamond, kobj);
1635
1636 if (kdamond->damon_ctx)
1637 damon_destroy_ctx(kdamond->damon_ctx);
15423a52 1638 kfree(kdamond);
c951cd3b
SP
1639}
1640
1641static struct kobj_attribute damon_sysfs_kdamond_state_attr =
1642 __ATTR_RW_MODE(state, 0600);
1643
1644static struct kobj_attribute damon_sysfs_kdamond_pid_attr =
1645 __ATTR_RO_MODE(pid, 0400);
1646
1647static struct attribute *damon_sysfs_kdamond_attrs[] = {
1648 &damon_sysfs_kdamond_state_attr.attr,
1649 &damon_sysfs_kdamond_pid_attr.attr,
1650 NULL,
1651};
1652ATTRIBUTE_GROUPS(damon_sysfs_kdamond);
1653
e56397e8 1654static const struct kobj_type damon_sysfs_kdamond_ktype = {
c951cd3b
SP
1655 .release = damon_sysfs_kdamond_release,
1656 .sysfs_ops = &kobj_sysfs_ops,
1657 .default_groups = damon_sysfs_kdamond_groups,
1658};
1659
1660/*
1661 * kdamonds directory
1662 */
1663
1664struct damon_sysfs_kdamonds {
1665 struct kobject kobj;
1666 struct damon_sysfs_kdamond **kdamonds_arr;
1667 int nr;
1668};
1669
1670static struct damon_sysfs_kdamonds *damon_sysfs_kdamonds_alloc(void)
1671{
1672 return kzalloc(sizeof(struct damon_sysfs_kdamonds), GFP_KERNEL);
1673}
1674
1675static void damon_sysfs_kdamonds_rm_dirs(struct damon_sysfs_kdamonds *kdamonds)
1676{
1677 struct damon_sysfs_kdamond **kdamonds_arr = kdamonds->kdamonds_arr;
1678 int i;
1679
1680 for (i = 0; i < kdamonds->nr; i++) {
1681 damon_sysfs_kdamond_rm_dirs(kdamonds_arr[i]);
1682 kobject_put(&kdamonds_arr[i]->kobj);
1683 }
1684 kdamonds->nr = 0;
1685 kfree(kdamonds_arr);
1686 kdamonds->kdamonds_arr = NULL;
1687}
1688
c274cd5c 1689static bool damon_sysfs_kdamonds_busy(struct damon_sysfs_kdamond **kdamonds,
c951cd3b
SP
1690 int nr_kdamonds)
1691{
c951cd3b
SP
1692 int i;
1693
1694 for (i = 0; i < nr_kdamonds; i++) {
c274cd5c
KX
1695 if (damon_sysfs_kdamond_running(kdamonds[i]) ||
1696 damon_sysfs_cmd_request.kdamond == kdamonds[i])
1697 return true;
c951cd3b 1698 }
c274cd5c
KX
1699
1700 return false;
c951cd3b
SP
1701}
1702
1703static int damon_sysfs_kdamonds_add_dirs(struct damon_sysfs_kdamonds *kdamonds,
1704 int nr_kdamonds)
1705{
1706 struct damon_sysfs_kdamond **kdamonds_arr, *kdamond;
1707 int err, i;
1708
c274cd5c 1709 if (damon_sysfs_kdamonds_busy(kdamonds->kdamonds_arr, kdamonds->nr))
c951cd3b
SP
1710 return -EBUSY;
1711
1712 damon_sysfs_kdamonds_rm_dirs(kdamonds);
1713 if (!nr_kdamonds)
1714 return 0;
1715
1716 kdamonds_arr = kmalloc_array(nr_kdamonds, sizeof(*kdamonds_arr),
1717 GFP_KERNEL | __GFP_NOWARN);
1718 if (!kdamonds_arr)
1719 return -ENOMEM;
1720 kdamonds->kdamonds_arr = kdamonds_arr;
1721
1722 for (i = 0; i < nr_kdamonds; i++) {
1723 kdamond = damon_sysfs_kdamond_alloc();
1724 if (!kdamond) {
1725 damon_sysfs_kdamonds_rm_dirs(kdamonds);
1726 return -ENOMEM;
1727 }
1728
1729 err = kobject_init_and_add(&kdamond->kobj,
1730 &damon_sysfs_kdamond_ktype, &kdamonds->kobj,
1731 "%d", i);
1732 if (err)
1733 goto out;
1734
1735 err = damon_sysfs_kdamond_add_dirs(kdamond);
1736 if (err)
1737 goto out;
1738
1739 kdamonds_arr[i] = kdamond;
1740 kdamonds->nr++;
1741 }
1742 return 0;
1743
1744out:
1745 damon_sysfs_kdamonds_rm_dirs(kdamonds);
1746 kobject_put(&kdamond->kobj);
1747 return err;
1748}
1749
1750static ssize_t nr_kdamonds_show(struct kobject *kobj,
1751 struct kobj_attribute *attr, char *buf)
1752{
1753 struct damon_sysfs_kdamonds *kdamonds = container_of(kobj,
1754 struct damon_sysfs_kdamonds, kobj);
1755
1756 return sysfs_emit(buf, "%d\n", kdamonds->nr);
1757}
1758
1759static ssize_t nr_kdamonds_store(struct kobject *kobj,
1760 struct kobj_attribute *attr, const char *buf, size_t count)
1761{
a17a8b3b 1762 struct damon_sysfs_kdamonds *kdamonds;
c951cd3b
SP
1763 int nr, err;
1764
1765 err = kstrtoint(buf, 0, &nr);
1766 if (err)
1767 return err;
1768 if (nr < 0)
1769 return -EINVAL;
1770
a17a8b3b
XH
1771 kdamonds = container_of(kobj, struct damon_sysfs_kdamonds, kobj);
1772
c951cd3b
SP
1773 if (!mutex_trylock(&damon_sysfs_lock))
1774 return -EBUSY;
1775 err = damon_sysfs_kdamonds_add_dirs(kdamonds, nr);
1776 mutex_unlock(&damon_sysfs_lock);
1777 if (err)
1778 return err;
1779
1780 return count;
1781}
1782
1783static void damon_sysfs_kdamonds_release(struct kobject *kobj)
1784{
1785 kfree(container_of(kobj, struct damon_sysfs_kdamonds, kobj));
1786}
1787
1788static struct kobj_attribute damon_sysfs_kdamonds_nr_attr =
1789 __ATTR_RW_MODE(nr_kdamonds, 0600);
1790
1791static struct attribute *damon_sysfs_kdamonds_attrs[] = {
1792 &damon_sysfs_kdamonds_nr_attr.attr,
1793 NULL,
1794};
1795ATTRIBUTE_GROUPS(damon_sysfs_kdamonds);
1796
e56397e8 1797static const struct kobj_type damon_sysfs_kdamonds_ktype = {
c951cd3b
SP
1798 .release = damon_sysfs_kdamonds_release,
1799 .sysfs_ops = &kobj_sysfs_ops,
1800 .default_groups = damon_sysfs_kdamonds_groups,
1801};
1802
1803/*
1804 * damon user interface directory
1805 */
1806
1807struct damon_sysfs_ui_dir {
1808 struct kobject kobj;
1809 struct damon_sysfs_kdamonds *kdamonds;
1810};
1811
1812static struct damon_sysfs_ui_dir *damon_sysfs_ui_dir_alloc(void)
1813{
1814 return kzalloc(sizeof(struct damon_sysfs_ui_dir), GFP_KERNEL);
1815}
1816
1817static int damon_sysfs_ui_dir_add_dirs(struct damon_sysfs_ui_dir *ui_dir)
1818{
1819 struct damon_sysfs_kdamonds *kdamonds;
1820 int err;
1821
1822 kdamonds = damon_sysfs_kdamonds_alloc();
1823 if (!kdamonds)
1824 return -ENOMEM;
1825
1826 err = kobject_init_and_add(&kdamonds->kobj,
1827 &damon_sysfs_kdamonds_ktype, &ui_dir->kobj,
1828 "kdamonds");
1829 if (err) {
1830 kobject_put(&kdamonds->kobj);
1831 return err;
1832 }
1833 ui_dir->kdamonds = kdamonds;
1834 return err;
1835}
1836
1837static void damon_sysfs_ui_dir_release(struct kobject *kobj)
1838{
1839 kfree(container_of(kobj, struct damon_sysfs_ui_dir, kobj));
1840}
1841
1842static struct attribute *damon_sysfs_ui_dir_attrs[] = {
1843 NULL,
1844};
1845ATTRIBUTE_GROUPS(damon_sysfs_ui_dir);
1846
e56397e8 1847static const struct kobj_type damon_sysfs_ui_dir_ktype = {
c951cd3b
SP
1848 .release = damon_sysfs_ui_dir_release,
1849 .sysfs_ops = &kobj_sysfs_ops,
1850 .default_groups = damon_sysfs_ui_dir_groups,
1851};
1852
1853static int __init damon_sysfs_init(void)
1854{
1855 struct kobject *damon_sysfs_root;
1856 struct damon_sysfs_ui_dir *admin;
1857 int err;
1858
1859 damon_sysfs_root = kobject_create_and_add("damon", mm_kobj);
1860 if (!damon_sysfs_root)
1861 return -ENOMEM;
1862
1863 admin = damon_sysfs_ui_dir_alloc();
1864 if (!admin) {
1865 kobject_put(damon_sysfs_root);
1866 return -ENOMEM;
1867 }
1868 err = kobject_init_and_add(&admin->kobj, &damon_sysfs_ui_dir_ktype,
1869 damon_sysfs_root, "admin");
1870 if (err)
1871 goto out;
1872 err = damon_sysfs_ui_dir_add_dirs(admin);
1873 if (err)
1874 goto out;
1875 return 0;
1876
1877out:
1878 kobject_put(&admin->kobj);
1879 kobject_put(damon_sysfs_root);
1880 return err;
1881}
1882subsys_initcall(damon_sysfs_init);
b8ee5575
SP
1883
1884#include "sysfs-test.h"