1 // SPDX-License-Identifier: GPL-2.0
3 * DAMON Debugfs Interface
8 #define pr_fmt(fmt) "damon-dbgfs: " fmt
10 #include <linux/damon.h>
11 #include <linux/debugfs.h>
12 #include <linux/file.h>
14 #include <linux/module.h>
15 #include <linux/page_idle.h>
16 #include <linux/slab.h>
18 static struct damon_ctx **dbgfs_ctxs;
19 static int dbgfs_nr_ctxs;
20 static struct dentry **dbgfs_dirs;
21 static DEFINE_MUTEX(damon_dbgfs_lock);
24 * Returns non-empty string on success, negative error code otherwise.
26 static char *user_input_str(const char __user *buf, size_t count, loff_t *ppos)
31 /* We do not accept continuous write */
33 return ERR_PTR(-EINVAL);
35 kbuf = kmalloc(count + 1, GFP_KERNEL | __GFP_NOWARN);
37 return ERR_PTR(-ENOMEM);
39 ret = simple_write_to_buffer(kbuf, count + 1, ppos, buf, count);
49 static ssize_t dbgfs_attrs_read(struct file *file,
50 char __user *buf, size_t count, loff_t *ppos)
52 struct damon_ctx *ctx = file->private_data;
56 mutex_lock(&ctx->kdamond_lock);
57 ret = scnprintf(kbuf, ARRAY_SIZE(kbuf), "%lu %lu %lu %lu %lu\n",
58 ctx->sample_interval, ctx->aggr_interval,
59 ctx->ops_update_interval, ctx->min_nr_regions,
61 mutex_unlock(&ctx->kdamond_lock);
63 return simple_read_from_buffer(buf, count, ppos, kbuf, ret);
66 static ssize_t dbgfs_attrs_write(struct file *file,
67 const char __user *buf, size_t count, loff_t *ppos)
69 struct damon_ctx *ctx = file->private_data;
70 unsigned long s, a, r, minr, maxr;
74 kbuf = user_input_str(buf, count, ppos);
78 if (sscanf(kbuf, "%lu %lu %lu %lu %lu",
79 &s, &a, &r, &minr, &maxr) != 5) {
84 mutex_lock(&ctx->kdamond_lock);
90 ret = damon_set_attrs(ctx, s, a, r, minr, maxr);
94 mutex_unlock(&ctx->kdamond_lock);
100 static ssize_t sprint_schemes(struct damon_ctx *c, char *buf, ssize_t len)
106 damon_for_each_scheme(s, c) {
107 rc = scnprintf(&buf[written], len - written,
108 "%lu %lu %u %u %u %u %d %lu %lu %lu %u %u %u %d %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
109 s->min_sz_region, s->max_sz_region,
110 s->min_nr_accesses, s->max_nr_accesses,
111 s->min_age_region, s->max_age_region,
113 s->quota.ms, s->quota.sz,
114 s->quota.reset_interval,
116 s->quota.weight_nr_accesses,
118 s->wmarks.metric, s->wmarks.interval,
119 s->wmarks.high, s->wmarks.mid, s->wmarks.low,
120 s->stat.nr_tried, s->stat.sz_tried,
121 s->stat.nr_applied, s->stat.sz_applied,
131 static ssize_t dbgfs_schemes_read(struct file *file, char __user *buf,
132 size_t count, loff_t *ppos)
134 struct damon_ctx *ctx = file->private_data;
138 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
142 mutex_lock(&ctx->kdamond_lock);
143 len = sprint_schemes(ctx, kbuf, count);
144 mutex_unlock(&ctx->kdamond_lock);
147 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
154 static void free_schemes_arr(struct damos **schemes, ssize_t nr_schemes)
158 for (i = 0; i < nr_schemes; i++)
163 static bool damos_action_valid(int action)
170 case DAMOS_NOHUGEPAGE:
179 * Converts a string into an array of struct damos pointers
181 * Returns an array of struct damos pointers that converted if the conversion
182 * success, or NULL otherwise.
184 static struct damos **str_to_schemes(const char *str, ssize_t len,
187 struct damos *scheme, **schemes;
188 const int max_nr_schemes = 256;
189 int pos = 0, parsed, ret;
190 unsigned long min_sz, max_sz;
191 unsigned int min_nr_a, max_nr_a, min_age, max_age;
194 schemes = kmalloc_array(max_nr_schemes, sizeof(scheme),
200 while (pos < len && *nr_schemes < max_nr_schemes) {
201 struct damos_quota quota = {};
202 struct damos_watermarks wmarks;
204 ret = sscanf(&str[pos],
205 "%lu %lu %u %u %u %u %u %lu %lu %lu %u %u %u %u %lu %lu %lu %lu%n",
206 &min_sz, &max_sz, &min_nr_a, &max_nr_a,
207 &min_age, &max_age, &action, "a.ms,
208 "a.sz, "a.reset_interval,
209 "a.weight_sz, "a.weight_nr_accesses,
210 "a.weight_age, &wmarks.metric,
211 &wmarks.interval, &wmarks.high, &wmarks.mid,
212 &wmarks.low, &parsed);
215 if (!damos_action_valid(action))
218 if (min_sz > max_sz || min_nr_a > max_nr_a || min_age > max_age)
221 if (wmarks.high < wmarks.mid || wmarks.high < wmarks.low ||
222 wmarks.mid < wmarks.low)
226 scheme = damon_new_scheme(min_sz, max_sz, min_nr_a, max_nr_a,
227 min_age, max_age, action, "a, &wmarks);
231 schemes[*nr_schemes] = scheme;
236 free_schemes_arr(schemes, *nr_schemes);
240 static ssize_t dbgfs_schemes_write(struct file *file, const char __user *buf,
241 size_t count, loff_t *ppos)
243 struct damon_ctx *ctx = file->private_data;
245 struct damos **schemes;
246 ssize_t nr_schemes = 0, ret;
248 kbuf = user_input_str(buf, count, ppos);
250 return PTR_ERR(kbuf);
252 schemes = str_to_schemes(kbuf, count, &nr_schemes);
258 mutex_lock(&ctx->kdamond_lock);
264 ret = damon_set_schemes(ctx, schemes, nr_schemes);
271 mutex_unlock(&ctx->kdamond_lock);
272 free_schemes_arr(schemes, nr_schemes);
278 static inline bool target_has_pid(const struct damon_ctx *ctx)
280 return ctx->ops.id == DAMON_OPS_VADDR;
283 static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len)
285 struct damon_target *t;
290 damon_for_each_target(t, ctx) {
291 if (target_has_pid(ctx))
292 /* Show pid numbers to debugfs users */
293 id = pid_vnr(t->pid);
295 /* Show 42 for physical address space, just for fun */
298 rc = scnprintf(&buf[written], len - written, "%d ", id);
305 written += scnprintf(&buf[written], len - written, "\n");
309 static ssize_t dbgfs_target_ids_read(struct file *file,
310 char __user *buf, size_t count, loff_t *ppos)
312 struct damon_ctx *ctx = file->private_data;
316 mutex_lock(&ctx->kdamond_lock);
317 len = sprint_target_ids(ctx, ids_buf, 320);
318 mutex_unlock(&ctx->kdamond_lock);
322 return simple_read_from_buffer(buf, count, ppos, ids_buf, len);
326 * Converts a string into an integers array
328 * Returns an array of integers array if the conversion success, or NULL
331 static int *str_to_ints(const char *str, ssize_t len, ssize_t *nr_ints)
334 const int max_nr_ints = 32;
336 int pos = 0, parsed, ret;
339 array = kmalloc_array(max_nr_ints, sizeof(*array), GFP_KERNEL);
342 while (*nr_ints < max_nr_ints && pos < len) {
343 ret = sscanf(&str[pos], "%d%n", &nr, &parsed);
347 array[*nr_ints] = nr;
354 static void dbgfs_put_pids(struct pid **pids, int nr_pids)
358 for (i = 0; i < nr_pids; i++)
363 * Converts a string into an struct pid pointers array
365 * Returns an array of struct pid pointers if the conversion success, or NULL
368 static struct pid **str_to_pids(const char *str, ssize_t len, ssize_t *nr_pids)
376 ints = str_to_ints(str, len, &nr_ints);
380 pids = kmalloc_array(nr_ints, sizeof(*pids), GFP_KERNEL);
384 for (; *nr_pids < nr_ints; (*nr_pids)++) {
385 pids[*nr_pids] = find_get_pid(ints[*nr_pids]);
386 if (!pids[*nr_pids]) {
387 dbgfs_put_pids(pids, *nr_pids);
400 * dbgfs_set_targets() - Set monitoring targets.
401 * @ctx: monitoring context
402 * @nr_targets: number of targets
403 * @pids: array of target pids (size is same to @nr_targets)
405 * This function should not be called while the kdamond is running. @pids is
406 * ignored if the context is not configured to have pid in each target. On
407 * failure, reference counts of all pids in @pids are decremented.
409 * Return: 0 on success, negative error code otherwise.
411 static int dbgfs_set_targets(struct damon_ctx *ctx, ssize_t nr_targets,
415 struct damon_target *t, *next;
417 damon_for_each_target_safe(t, next, ctx) {
418 if (target_has_pid(ctx))
420 damon_destroy_target(t);
423 for (i = 0; i < nr_targets; i++) {
424 t = damon_new_target();
426 damon_for_each_target_safe(t, next, ctx)
427 damon_destroy_target(t);
428 if (target_has_pid(ctx))
429 dbgfs_put_pids(pids, nr_targets);
432 if (target_has_pid(ctx))
434 damon_add_target(ctx, t);
440 static ssize_t dbgfs_target_ids_write(struct file *file,
441 const char __user *buf, size_t count, loff_t *ppos)
443 struct damon_ctx *ctx = file->private_data;
444 bool id_is_pid = true;
446 struct pid **target_pids = NULL;
450 kbuf = user_input_str(buf, count, ppos);
452 return PTR_ERR(kbuf);
454 if (!strncmp(kbuf, "paddr\n", count)) {
460 target_pids = str_to_pids(kbuf, count, &nr_targets);
467 mutex_lock(&ctx->kdamond_lock);
470 dbgfs_put_pids(target_pids, nr_targets);
475 /* remove previously set targets */
476 dbgfs_set_targets(ctx, 0, NULL);
482 /* Configure the context for the address space type */
484 ret = damon_select_ops(ctx, DAMON_OPS_VADDR);
486 ret = damon_select_ops(ctx, DAMON_OPS_PADDR);
490 ret = dbgfs_set_targets(ctx, nr_targets, target_pids);
495 mutex_unlock(&ctx->kdamond_lock);
502 static ssize_t sprint_init_regions(struct damon_ctx *c, char *buf, ssize_t len)
504 struct damon_target *t;
505 struct damon_region *r;
510 damon_for_each_target(t, c) {
511 damon_for_each_region(r, t) {
512 rc = scnprintf(&buf[written], len - written,
514 target_idx, r->ar.start, r->ar.end);
524 static ssize_t dbgfs_init_regions_read(struct file *file, char __user *buf,
525 size_t count, loff_t *ppos)
527 struct damon_ctx *ctx = file->private_data;
531 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
535 mutex_lock(&ctx->kdamond_lock);
537 mutex_unlock(&ctx->kdamond_lock);
542 len = sprint_init_regions(ctx, kbuf, count);
543 mutex_unlock(&ctx->kdamond_lock);
546 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
553 static int add_init_region(struct damon_ctx *c, int target_idx,
554 struct damon_addr_range *ar)
556 struct damon_target *t;
557 struct damon_region *r, *prev;
558 unsigned long idx = 0;
561 if (ar->start >= ar->end)
564 damon_for_each_target(t, c) {
565 if (idx++ == target_idx) {
566 r = damon_new_region(ar->start, ar->end);
569 damon_add_region(r, t);
570 if (damon_nr_regions(t) > 1) {
571 prev = damon_prev_region(r);
572 if (prev->ar.end > r->ar.start) {
573 damon_destroy_region(r, t);
583 static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len)
585 struct damon_target *t;
586 struct damon_region *r, *next;
587 int pos = 0, parsed, ret;
589 struct damon_addr_range ar;
592 damon_for_each_target(t, c) {
593 damon_for_each_region_safe(r, next, t)
594 damon_destroy_region(r, t);
598 ret = sscanf(&str[pos], "%d %lu %lu%n",
599 &target_idx, &ar.start, &ar.end, &parsed);
602 err = add_init_region(c, target_idx, &ar);
611 damon_for_each_target(t, c) {
612 damon_for_each_region_safe(r, next, t)
613 damon_destroy_region(r, t);
618 static ssize_t dbgfs_init_regions_write(struct file *file,
619 const char __user *buf, size_t count,
622 struct damon_ctx *ctx = file->private_data;
627 kbuf = user_input_str(buf, count, ppos);
629 return PTR_ERR(kbuf);
631 mutex_lock(&ctx->kdamond_lock);
637 err = set_init_regions(ctx, kbuf, ret);
642 mutex_unlock(&ctx->kdamond_lock);
647 static ssize_t dbgfs_kdamond_pid_read(struct file *file,
648 char __user *buf, size_t count, loff_t *ppos)
650 struct damon_ctx *ctx = file->private_data;
654 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
658 mutex_lock(&ctx->kdamond_lock);
660 len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
662 len = scnprintf(kbuf, count, "none\n");
663 mutex_unlock(&ctx->kdamond_lock);
666 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
673 static int damon_dbgfs_open(struct inode *inode, struct file *file)
675 file->private_data = inode->i_private;
677 return nonseekable_open(inode, file);
680 static const struct file_operations attrs_fops = {
681 .open = damon_dbgfs_open,
682 .read = dbgfs_attrs_read,
683 .write = dbgfs_attrs_write,
686 static const struct file_operations schemes_fops = {
687 .open = damon_dbgfs_open,
688 .read = dbgfs_schemes_read,
689 .write = dbgfs_schemes_write,
692 static const struct file_operations target_ids_fops = {
693 .open = damon_dbgfs_open,
694 .read = dbgfs_target_ids_read,
695 .write = dbgfs_target_ids_write,
698 static const struct file_operations init_regions_fops = {
699 .open = damon_dbgfs_open,
700 .read = dbgfs_init_regions_read,
701 .write = dbgfs_init_regions_write,
704 static const struct file_operations kdamond_pid_fops = {
705 .open = damon_dbgfs_open,
706 .read = dbgfs_kdamond_pid_read,
709 static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
711 const char * const file_names[] = {"attrs", "schemes", "target_ids",
712 "init_regions", "kdamond_pid"};
713 const struct file_operations *fops[] = {&attrs_fops, &schemes_fops,
714 &target_ids_fops, &init_regions_fops, &kdamond_pid_fops};
717 for (i = 0; i < ARRAY_SIZE(file_names); i++)
718 debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
721 static void dbgfs_before_terminate(struct damon_ctx *ctx)
723 struct damon_target *t, *next;
725 if (!target_has_pid(ctx))
728 mutex_lock(&ctx->kdamond_lock);
729 damon_for_each_target_safe(t, next, ctx) {
731 damon_destroy_target(t);
733 mutex_unlock(&ctx->kdamond_lock);
736 static struct damon_ctx *dbgfs_new_ctx(void)
738 struct damon_ctx *ctx;
740 ctx = damon_new_ctx();
744 if (damon_select_ops(ctx, DAMON_OPS_VADDR) &&
745 damon_select_ops(ctx, DAMON_OPS_PADDR)) {
746 damon_destroy_ctx(ctx);
749 ctx->callback.before_terminate = dbgfs_before_terminate;
753 static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
755 damon_destroy_ctx(ctx);
759 * Make a context of @name and create a debugfs directory for it.
761 * This function should be called while holding damon_dbgfs_lock.
763 * Returns 0 on success, negative error code otherwise.
765 static int dbgfs_mk_context(char *name)
767 struct dentry *root, **new_dirs, *new_dir;
768 struct damon_ctx **new_ctxs, *new_ctx;
770 if (damon_nr_running_ctxs())
773 new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
774 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
777 dbgfs_ctxs = new_ctxs;
779 new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
780 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
783 dbgfs_dirs = new_dirs;
785 root = dbgfs_dirs[0];
789 new_dir = debugfs_create_dir(name, root);
790 dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
792 new_ctx = dbgfs_new_ctx();
794 debugfs_remove(new_dir);
795 dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
799 dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
800 dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
801 dbgfs_ctxs[dbgfs_nr_ctxs]);
807 static ssize_t dbgfs_mk_context_write(struct file *file,
808 const char __user *buf, size_t count, loff_t *ppos)
814 kbuf = user_input_str(buf, count, ppos);
816 return PTR_ERR(kbuf);
817 ctx_name = kmalloc(count + 1, GFP_KERNEL);
823 /* Trim white space */
824 if (sscanf(kbuf, "%s", ctx_name) != 1) {
829 mutex_lock(&damon_dbgfs_lock);
830 ret = dbgfs_mk_context(ctx_name);
833 mutex_unlock(&damon_dbgfs_lock);
842 * Remove a context of @name and its debugfs directory.
844 * This function should be called while holding damon_dbgfs_lock.
846 * Return 0 on success, negative error code otherwise.
848 static int dbgfs_rm_context(char *name)
850 struct dentry *root, *dir, **new_dirs;
851 struct damon_ctx **new_ctxs;
854 if (damon_nr_running_ctxs())
857 root = dbgfs_dirs[0];
861 dir = debugfs_lookup(name, root);
865 new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
870 new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
877 for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
878 if (dbgfs_dirs[i] == dir) {
879 debugfs_remove(dbgfs_dirs[i]);
880 dbgfs_destroy_ctx(dbgfs_ctxs[i]);
883 new_dirs[j] = dbgfs_dirs[i];
884 new_ctxs[j++] = dbgfs_ctxs[i];
890 dbgfs_dirs = new_dirs;
891 dbgfs_ctxs = new_ctxs;
897 static ssize_t dbgfs_rm_context_write(struct file *file,
898 const char __user *buf, size_t count, loff_t *ppos)
904 kbuf = user_input_str(buf, count, ppos);
906 return PTR_ERR(kbuf);
907 ctx_name = kmalloc(count + 1, GFP_KERNEL);
913 /* Trim white space */
914 if (sscanf(kbuf, "%s", ctx_name) != 1) {
919 mutex_lock(&damon_dbgfs_lock);
920 ret = dbgfs_rm_context(ctx_name);
923 mutex_unlock(&damon_dbgfs_lock);
931 static ssize_t dbgfs_monitor_on_read(struct file *file,
932 char __user *buf, size_t count, loff_t *ppos)
934 char monitor_on_buf[5];
935 bool monitor_on = damon_nr_running_ctxs() != 0;
938 len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
940 return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
943 static ssize_t dbgfs_monitor_on_write(struct file *file,
944 const char __user *buf, size_t count, loff_t *ppos)
949 kbuf = user_input_str(buf, count, ppos);
951 return PTR_ERR(kbuf);
953 /* Remove white space */
954 if (sscanf(kbuf, "%s", kbuf) != 1) {
959 mutex_lock(&damon_dbgfs_lock);
960 if (!strncmp(kbuf, "on", count)) {
963 for (i = 0; i < dbgfs_nr_ctxs; i++) {
964 if (damon_targets_empty(dbgfs_ctxs[i])) {
966 mutex_unlock(&damon_dbgfs_lock);
970 ret = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs, true);
971 } else if (!strncmp(kbuf, "off", count)) {
972 ret = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
976 mutex_unlock(&damon_dbgfs_lock);
984 static const struct file_operations mk_contexts_fops = {
985 .write = dbgfs_mk_context_write,
988 static const struct file_operations rm_contexts_fops = {
989 .write = dbgfs_rm_context_write,
992 static const struct file_operations monitor_on_fops = {
993 .read = dbgfs_monitor_on_read,
994 .write = dbgfs_monitor_on_write,
997 static int __init __damon_dbgfs_init(void)
999 struct dentry *dbgfs_root;
1000 const char * const file_names[] = {"mk_contexts", "rm_contexts",
1002 const struct file_operations *fops[] = {&mk_contexts_fops,
1003 &rm_contexts_fops, &monitor_on_fops};
1006 dbgfs_root = debugfs_create_dir("damon", NULL);
1008 for (i = 0; i < ARRAY_SIZE(file_names); i++)
1009 debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
1011 dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
1013 dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
1015 debugfs_remove(dbgfs_root);
1018 dbgfs_dirs[0] = dbgfs_root;
1024 * Functions for the initialization
1027 static int __init damon_dbgfs_init(void)
1031 mutex_lock(&damon_dbgfs_lock);
1032 dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
1035 dbgfs_ctxs[0] = dbgfs_new_ctx();
1036 if (!dbgfs_ctxs[0]) {
1042 rc = __damon_dbgfs_init();
1044 kfree(dbgfs_ctxs[0]);
1046 pr_err("%s: dbgfs init failed\n", __func__);
1050 mutex_unlock(&damon_dbgfs_lock);
1054 module_init(damon_dbgfs_init);
1056 #include "dbgfs-test.h"