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->primitive_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 targetid_is_pid(const struct damon_ctx *ctx)
280 return ctx->primitive.target_valid == damon_va_target_valid;
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) {
292 if (targetid_is_pid(ctx))
293 /* Show pid numbers to debugfs users */
294 id = (unsigned long)pid_vnr((struct pid *)id);
296 rc = scnprintf(&buf[written], len - written, "%lu ", id);
303 written += scnprintf(&buf[written], len - written, "\n");
307 static ssize_t dbgfs_target_ids_read(struct file *file,
308 char __user *buf, size_t count, loff_t *ppos)
310 struct damon_ctx *ctx = file->private_data;
314 mutex_lock(&ctx->kdamond_lock);
315 len = sprint_target_ids(ctx, ids_buf, 320);
316 mutex_unlock(&ctx->kdamond_lock);
320 return simple_read_from_buffer(buf, count, ppos, ids_buf, len);
324 * Converts a string into an array of unsigned long integers
326 * Returns an array of unsigned long integers if the conversion success, or
329 static unsigned long *str_to_target_ids(const char *str, ssize_t len,
333 const int max_nr_ids = 32;
335 int pos = 0, parsed, ret;
338 ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL);
341 while (*nr_ids < max_nr_ids && pos < len) {
342 ret = sscanf(&str[pos], "%lu%n", &id, &parsed);
353 static void dbgfs_put_pids(unsigned long *ids, int nr_ids)
357 for (i = 0; i < nr_ids; i++)
358 put_pid((struct pid *)ids[i]);
361 static ssize_t dbgfs_target_ids_write(struct file *file,
362 const char __user *buf, size_t count, loff_t *ppos)
364 struct damon_ctx *ctx = file->private_data;
365 struct damon_target *t, *next_t;
366 bool id_is_pid = true;
368 unsigned long *targets;
373 kbuf = user_input_str(buf, count, ppos);
375 return PTR_ERR(kbuf);
377 if (!strncmp(kbuf, "paddr\n", count)) {
379 /* target id is meaningless here, but we set it just for fun */
380 scnprintf(kbuf, count, "42 ");
383 targets = str_to_target_ids(kbuf, count, &nr_targets);
390 for (i = 0; i < nr_targets; i++) {
391 targets[i] = (unsigned long)find_get_pid(
394 dbgfs_put_pids(targets, i);
396 goto free_targets_out;
401 mutex_lock(&ctx->kdamond_lock);
404 dbgfs_put_pids(targets, nr_targets);
409 /* remove previously set targets */
410 damon_for_each_target_safe(t, next_t, ctx) {
411 if (targetid_is_pid(ctx))
412 put_pid((struct pid *)t->id);
413 damon_destroy_target(t);
416 /* Configure the context for the address space type */
418 damon_va_set_primitives(ctx);
420 damon_pa_set_primitives(ctx);
422 ret = damon_set_targets(ctx, targets, nr_targets);
425 dbgfs_put_pids(targets, nr_targets);
431 mutex_unlock(&ctx->kdamond_lock);
439 static ssize_t sprint_init_regions(struct damon_ctx *c, char *buf, ssize_t len)
441 struct damon_target *t;
442 struct damon_region *r;
446 damon_for_each_target(t, c) {
447 damon_for_each_region(r, t) {
448 rc = scnprintf(&buf[written], len - written,
450 t->id, r->ar.start, r->ar.end);
459 static ssize_t dbgfs_init_regions_read(struct file *file, char __user *buf,
460 size_t count, loff_t *ppos)
462 struct damon_ctx *ctx = file->private_data;
466 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
470 mutex_lock(&ctx->kdamond_lock);
472 mutex_unlock(&ctx->kdamond_lock);
477 len = sprint_init_regions(ctx, kbuf, count);
478 mutex_unlock(&ctx->kdamond_lock);
481 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
488 static int add_init_region(struct damon_ctx *c,
489 unsigned long target_id, struct damon_addr_range *ar)
491 struct damon_target *t;
492 struct damon_region *r, *prev;
496 if (ar->start >= ar->end)
499 damon_for_each_target(t, c) {
501 if (targetid_is_pid(c))
502 id = (unsigned long)pid_vnr((struct pid *)id);
503 if (id == target_id) {
504 r = damon_new_region(ar->start, ar->end);
507 damon_add_region(r, t);
508 if (damon_nr_regions(t) > 1) {
509 prev = damon_prev_region(r);
510 if (prev->ar.end > r->ar.start) {
511 damon_destroy_region(r, t);
521 static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len)
523 struct damon_target *t;
524 struct damon_region *r, *next;
525 int pos = 0, parsed, ret;
526 unsigned long target_id;
527 struct damon_addr_range ar;
530 damon_for_each_target(t, c) {
531 damon_for_each_region_safe(r, next, t)
532 damon_destroy_region(r, t);
536 ret = sscanf(&str[pos], "%lu %lu %lu%n",
537 &target_id, &ar.start, &ar.end, &parsed);
540 err = add_init_region(c, target_id, &ar);
549 damon_for_each_target(t, c) {
550 damon_for_each_region_safe(r, next, t)
551 damon_destroy_region(r, t);
556 static ssize_t dbgfs_init_regions_write(struct file *file,
557 const char __user *buf, size_t count,
560 struct damon_ctx *ctx = file->private_data;
565 kbuf = user_input_str(buf, count, ppos);
567 return PTR_ERR(kbuf);
569 mutex_lock(&ctx->kdamond_lock);
575 err = set_init_regions(ctx, kbuf, ret);
580 mutex_unlock(&ctx->kdamond_lock);
585 static ssize_t dbgfs_kdamond_pid_read(struct file *file,
586 char __user *buf, size_t count, loff_t *ppos)
588 struct damon_ctx *ctx = file->private_data;
592 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
596 mutex_lock(&ctx->kdamond_lock);
598 len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
600 len = scnprintf(kbuf, count, "none\n");
601 mutex_unlock(&ctx->kdamond_lock);
604 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
611 static int damon_dbgfs_open(struct inode *inode, struct file *file)
613 file->private_data = inode->i_private;
615 return nonseekable_open(inode, file);
618 static const struct file_operations attrs_fops = {
619 .open = damon_dbgfs_open,
620 .read = dbgfs_attrs_read,
621 .write = dbgfs_attrs_write,
624 static const struct file_operations schemes_fops = {
625 .open = damon_dbgfs_open,
626 .read = dbgfs_schemes_read,
627 .write = dbgfs_schemes_write,
630 static const struct file_operations target_ids_fops = {
631 .open = damon_dbgfs_open,
632 .read = dbgfs_target_ids_read,
633 .write = dbgfs_target_ids_write,
636 static const struct file_operations init_regions_fops = {
637 .open = damon_dbgfs_open,
638 .read = dbgfs_init_regions_read,
639 .write = dbgfs_init_regions_write,
642 static const struct file_operations kdamond_pid_fops = {
643 .open = damon_dbgfs_open,
644 .read = dbgfs_kdamond_pid_read,
647 static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
649 const char * const file_names[] = {"attrs", "schemes", "target_ids",
650 "init_regions", "kdamond_pid"};
651 const struct file_operations *fops[] = {&attrs_fops, &schemes_fops,
652 &target_ids_fops, &init_regions_fops, &kdamond_pid_fops};
655 for (i = 0; i < ARRAY_SIZE(file_names); i++)
656 debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
659 static void dbgfs_before_terminate(struct damon_ctx *ctx)
661 struct damon_target *t, *next;
663 if (!targetid_is_pid(ctx))
666 mutex_lock(&ctx->kdamond_lock);
667 damon_for_each_target_safe(t, next, ctx) {
668 put_pid((struct pid *)t->id);
669 damon_destroy_target(t);
671 mutex_unlock(&ctx->kdamond_lock);
674 static struct damon_ctx *dbgfs_new_ctx(void)
676 struct damon_ctx *ctx;
678 ctx = damon_new_ctx();
682 damon_va_set_primitives(ctx);
683 ctx->callback.before_terminate = dbgfs_before_terminate;
687 static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
689 damon_destroy_ctx(ctx);
693 * Make a context of @name and create a debugfs directory for it.
695 * This function should be called while holding damon_dbgfs_lock.
697 * Returns 0 on success, negative error code otherwise.
699 static int dbgfs_mk_context(char *name)
701 struct dentry *root, **new_dirs, *new_dir;
702 struct damon_ctx **new_ctxs, *new_ctx;
704 if (damon_nr_running_ctxs())
707 new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
708 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
711 dbgfs_ctxs = new_ctxs;
713 new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
714 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
717 dbgfs_dirs = new_dirs;
719 root = dbgfs_dirs[0];
723 new_dir = debugfs_create_dir(name, root);
724 dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
726 new_ctx = dbgfs_new_ctx();
728 debugfs_remove(new_dir);
729 dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
733 dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
734 dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
735 dbgfs_ctxs[dbgfs_nr_ctxs]);
741 static ssize_t dbgfs_mk_context_write(struct file *file,
742 const char __user *buf, size_t count, loff_t *ppos)
748 kbuf = user_input_str(buf, count, ppos);
750 return PTR_ERR(kbuf);
751 ctx_name = kmalloc(count + 1, GFP_KERNEL);
757 /* Trim white space */
758 if (sscanf(kbuf, "%s", ctx_name) != 1) {
763 mutex_lock(&damon_dbgfs_lock);
764 ret = dbgfs_mk_context(ctx_name);
767 mutex_unlock(&damon_dbgfs_lock);
776 * Remove a context of @name and its debugfs directory.
778 * This function should be called while holding damon_dbgfs_lock.
780 * Return 0 on success, negative error code otherwise.
782 static int dbgfs_rm_context(char *name)
784 struct dentry *root, *dir, **new_dirs;
785 struct damon_ctx **new_ctxs;
788 if (damon_nr_running_ctxs())
791 root = dbgfs_dirs[0];
795 dir = debugfs_lookup(name, root);
799 new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
804 new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
811 for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
812 if (dbgfs_dirs[i] == dir) {
813 debugfs_remove(dbgfs_dirs[i]);
814 dbgfs_destroy_ctx(dbgfs_ctxs[i]);
817 new_dirs[j] = dbgfs_dirs[i];
818 new_ctxs[j++] = dbgfs_ctxs[i];
824 dbgfs_dirs = new_dirs;
825 dbgfs_ctxs = new_ctxs;
831 static ssize_t dbgfs_rm_context_write(struct file *file,
832 const char __user *buf, size_t count, loff_t *ppos)
838 kbuf = user_input_str(buf, count, ppos);
840 return PTR_ERR(kbuf);
841 ctx_name = kmalloc(count + 1, GFP_KERNEL);
847 /* Trim white space */
848 if (sscanf(kbuf, "%s", ctx_name) != 1) {
853 mutex_lock(&damon_dbgfs_lock);
854 ret = dbgfs_rm_context(ctx_name);
857 mutex_unlock(&damon_dbgfs_lock);
865 static ssize_t dbgfs_monitor_on_read(struct file *file,
866 char __user *buf, size_t count, loff_t *ppos)
868 char monitor_on_buf[5];
869 bool monitor_on = damon_nr_running_ctxs() != 0;
872 len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
874 return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
877 static ssize_t dbgfs_monitor_on_write(struct file *file,
878 const char __user *buf, size_t count, loff_t *ppos)
883 kbuf = user_input_str(buf, count, ppos);
885 return PTR_ERR(kbuf);
887 /* Remove white space */
888 if (sscanf(kbuf, "%s", kbuf) != 1) {
893 mutex_lock(&damon_dbgfs_lock);
894 if (!strncmp(kbuf, "on", count)) {
897 for (i = 0; i < dbgfs_nr_ctxs; i++) {
898 if (damon_targets_empty(dbgfs_ctxs[i])) {
900 mutex_unlock(&damon_dbgfs_lock);
904 ret = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs);
905 } else if (!strncmp(kbuf, "off", count)) {
906 ret = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
910 mutex_unlock(&damon_dbgfs_lock);
918 static const struct file_operations mk_contexts_fops = {
919 .write = dbgfs_mk_context_write,
922 static const struct file_operations rm_contexts_fops = {
923 .write = dbgfs_rm_context_write,
926 static const struct file_operations monitor_on_fops = {
927 .read = dbgfs_monitor_on_read,
928 .write = dbgfs_monitor_on_write,
931 static int __init __damon_dbgfs_init(void)
933 struct dentry *dbgfs_root;
934 const char * const file_names[] = {"mk_contexts", "rm_contexts",
936 const struct file_operations *fops[] = {&mk_contexts_fops,
937 &rm_contexts_fops, &monitor_on_fops};
940 dbgfs_root = debugfs_create_dir("damon", NULL);
942 for (i = 0; i < ARRAY_SIZE(file_names); i++)
943 debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
945 dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
947 dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
949 debugfs_remove(dbgfs_root);
952 dbgfs_dirs[0] = dbgfs_root;
958 * Functions for the initialization
961 static int __init damon_dbgfs_init(void)
965 mutex_lock(&damon_dbgfs_lock);
966 dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
969 dbgfs_ctxs[0] = dbgfs_new_ctx();
970 if (!dbgfs_ctxs[0]) {
976 rc = __damon_dbgfs_init();
978 kfree(dbgfs_ctxs[0]);
980 pr_err("%s: dbgfs init failed\n", __func__);
984 mutex_unlock(&damon_dbgfs_lock);
988 module_init(damon_dbgfs_init);
990 #include "dbgfs-test.h"