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);
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\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_count, s->stat_sz);
129 static ssize_t dbgfs_schemes_read(struct file *file, char __user *buf,
130 size_t count, loff_t *ppos)
132 struct damon_ctx *ctx = file->private_data;
136 kbuf = kmalloc(count, GFP_KERNEL);
140 mutex_lock(&ctx->kdamond_lock);
141 len = sprint_schemes(ctx, kbuf, count);
142 mutex_unlock(&ctx->kdamond_lock);
145 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
152 static void free_schemes_arr(struct damos **schemes, ssize_t nr_schemes)
156 for (i = 0; i < nr_schemes; i++)
161 static bool damos_action_valid(int action)
168 case DAMOS_NOHUGEPAGE:
177 * Converts a string into an array of struct damos pointers
179 * Returns an array of struct damos pointers that converted if the conversion
180 * success, or NULL otherwise.
182 static struct damos **str_to_schemes(const char *str, ssize_t len,
185 struct damos *scheme, **schemes;
186 const int max_nr_schemes = 256;
187 int pos = 0, parsed, ret;
188 unsigned long min_sz, max_sz;
189 unsigned int min_nr_a, max_nr_a, min_age, max_age;
192 schemes = kmalloc_array(max_nr_schemes, sizeof(scheme),
198 while (pos < len && *nr_schemes < max_nr_schemes) {
199 struct damos_quota quota = {};
200 struct damos_watermarks wmarks;
202 ret = sscanf(&str[pos],
203 "%lu %lu %u %u %u %u %u %lu %lu %lu %u %u %u %u %lu %lu %lu %lu%n",
204 &min_sz, &max_sz, &min_nr_a, &max_nr_a,
205 &min_age, &max_age, &action, "a.ms,
206 "a.sz, "a.reset_interval,
207 "a.weight_sz, "a.weight_nr_accesses,
208 "a.weight_age, &wmarks.metric,
209 &wmarks.interval, &wmarks.high, &wmarks.mid,
210 &wmarks.low, &parsed);
213 if (!damos_action_valid(action)) {
214 pr_err("wrong action %d\n", action);
219 scheme = damon_new_scheme(min_sz, max_sz, min_nr_a, max_nr_a,
220 min_age, max_age, action, "a, &wmarks);
224 schemes[*nr_schemes] = scheme;
229 free_schemes_arr(schemes, *nr_schemes);
233 static ssize_t dbgfs_schemes_write(struct file *file, const char __user *buf,
234 size_t count, loff_t *ppos)
236 struct damon_ctx *ctx = file->private_data;
238 struct damos **schemes;
239 ssize_t nr_schemes = 0, ret;
241 kbuf = user_input_str(buf, count, ppos);
243 return PTR_ERR(kbuf);
245 schemes = str_to_schemes(kbuf, count, &nr_schemes);
251 mutex_lock(&ctx->kdamond_lock);
257 ret = damon_set_schemes(ctx, schemes, nr_schemes);
264 mutex_unlock(&ctx->kdamond_lock);
265 free_schemes_arr(schemes, nr_schemes);
271 static inline bool targetid_is_pid(const struct damon_ctx *ctx)
273 return ctx->primitive.target_valid == damon_va_target_valid;
276 static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len)
278 struct damon_target *t;
283 damon_for_each_target(t, ctx) {
285 if (targetid_is_pid(ctx))
286 /* Show pid numbers to debugfs users */
287 id = (unsigned long)pid_vnr((struct pid *)id);
289 rc = scnprintf(&buf[written], len - written, "%lu ", id);
296 written += scnprintf(&buf[written], len - written, "\n");
300 static ssize_t dbgfs_target_ids_read(struct file *file,
301 char __user *buf, size_t count, loff_t *ppos)
303 struct damon_ctx *ctx = file->private_data;
307 mutex_lock(&ctx->kdamond_lock);
308 len = sprint_target_ids(ctx, ids_buf, 320);
309 mutex_unlock(&ctx->kdamond_lock);
313 return simple_read_from_buffer(buf, count, ppos, ids_buf, len);
317 * Converts a string into an array of unsigned long integers
319 * Returns an array of unsigned long integers if the conversion success, or
322 static unsigned long *str_to_target_ids(const char *str, ssize_t len,
326 const int max_nr_ids = 32;
328 int pos = 0, parsed, ret;
331 ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL);
334 while (*nr_ids < max_nr_ids && pos < len) {
335 ret = sscanf(&str[pos], "%lu%n", &id, &parsed);
346 static void dbgfs_put_pids(unsigned long *ids, int nr_ids)
350 for (i = 0; i < nr_ids; i++)
351 put_pid((struct pid *)ids[i]);
354 static ssize_t dbgfs_target_ids_write(struct file *file,
355 const char __user *buf, size_t count, loff_t *ppos)
357 struct damon_ctx *ctx = file->private_data;
358 bool id_is_pid = true;
360 unsigned long *targets;
365 kbuf = user_input_str(buf, count, ppos);
367 return PTR_ERR(kbuf);
370 if (!strncmp(kbuf, "paddr\n", count)) {
372 /* target id is meaningless here, but we set it just for fun */
373 scnprintf(kbuf, count, "42 ");
376 targets = str_to_target_ids(nrs, count, &nr_targets);
383 for (i = 0; i < nr_targets; i++) {
384 targets[i] = (unsigned long)find_get_pid(
387 dbgfs_put_pids(targets, i);
389 goto free_targets_out;
394 mutex_lock(&ctx->kdamond_lock);
397 dbgfs_put_pids(targets, nr_targets);
402 /* remove targets with previously-set primitive */
403 damon_set_targets(ctx, NULL, 0);
405 /* Configure the context for the address space type */
407 damon_va_set_primitives(ctx);
409 damon_pa_set_primitives(ctx);
411 ret = damon_set_targets(ctx, targets, nr_targets);
414 dbgfs_put_pids(targets, nr_targets);
420 mutex_unlock(&ctx->kdamond_lock);
428 static ssize_t sprint_init_regions(struct damon_ctx *c, char *buf, ssize_t len)
430 struct damon_target *t;
431 struct damon_region *r;
435 damon_for_each_target(t, c) {
436 damon_for_each_region(r, t) {
437 rc = scnprintf(&buf[written], len - written,
439 t->id, r->ar.start, r->ar.end);
448 static ssize_t dbgfs_init_regions_read(struct file *file, char __user *buf,
449 size_t count, loff_t *ppos)
451 struct damon_ctx *ctx = file->private_data;
455 kbuf = kmalloc(count, GFP_KERNEL);
459 mutex_lock(&ctx->kdamond_lock);
461 mutex_unlock(&ctx->kdamond_lock);
466 len = sprint_init_regions(ctx, kbuf, count);
467 mutex_unlock(&ctx->kdamond_lock);
470 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
477 static int add_init_region(struct damon_ctx *c,
478 unsigned long target_id, struct damon_addr_range *ar)
480 struct damon_target *t;
481 struct damon_region *r, *prev;
485 if (ar->start >= ar->end)
488 damon_for_each_target(t, c) {
490 if (targetid_is_pid(c))
491 id = (unsigned long)pid_vnr((struct pid *)id);
492 if (id == target_id) {
493 r = damon_new_region(ar->start, ar->end);
496 damon_add_region(r, t);
497 if (damon_nr_regions(t) > 1) {
498 prev = damon_prev_region(r);
499 if (prev->ar.end > r->ar.start) {
500 damon_destroy_region(r, t);
510 static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len)
512 struct damon_target *t;
513 struct damon_region *r, *next;
514 int pos = 0, parsed, ret;
515 unsigned long target_id;
516 struct damon_addr_range ar;
519 damon_for_each_target(t, c) {
520 damon_for_each_region_safe(r, next, t)
521 damon_destroy_region(r, t);
525 ret = sscanf(&str[pos], "%lu %lu %lu%n",
526 &target_id, &ar.start, &ar.end, &parsed);
529 err = add_init_region(c, target_id, &ar);
538 damon_for_each_target(t, c) {
539 damon_for_each_region_safe(r, next, t)
540 damon_destroy_region(r, t);
545 static ssize_t dbgfs_init_regions_write(struct file *file,
546 const char __user *buf, size_t count,
549 struct damon_ctx *ctx = file->private_data;
554 kbuf = user_input_str(buf, count, ppos);
556 return PTR_ERR(kbuf);
558 mutex_lock(&ctx->kdamond_lock);
564 err = set_init_regions(ctx, kbuf, ret);
569 mutex_unlock(&ctx->kdamond_lock);
574 static ssize_t dbgfs_kdamond_pid_read(struct file *file,
575 char __user *buf, size_t count, loff_t *ppos)
577 struct damon_ctx *ctx = file->private_data;
581 kbuf = kmalloc(count, GFP_KERNEL);
585 mutex_lock(&ctx->kdamond_lock);
587 len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
589 len = scnprintf(kbuf, count, "none\n");
590 mutex_unlock(&ctx->kdamond_lock);
593 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
600 static int damon_dbgfs_open(struct inode *inode, struct file *file)
602 file->private_data = inode->i_private;
604 return nonseekable_open(inode, file);
607 static const struct file_operations attrs_fops = {
608 .open = damon_dbgfs_open,
609 .read = dbgfs_attrs_read,
610 .write = dbgfs_attrs_write,
613 static const struct file_operations schemes_fops = {
614 .open = damon_dbgfs_open,
615 .read = dbgfs_schemes_read,
616 .write = dbgfs_schemes_write,
619 static const struct file_operations target_ids_fops = {
620 .open = damon_dbgfs_open,
621 .read = dbgfs_target_ids_read,
622 .write = dbgfs_target_ids_write,
625 static const struct file_operations init_regions_fops = {
626 .open = damon_dbgfs_open,
627 .read = dbgfs_init_regions_read,
628 .write = dbgfs_init_regions_write,
631 static const struct file_operations kdamond_pid_fops = {
632 .open = damon_dbgfs_open,
633 .read = dbgfs_kdamond_pid_read,
636 static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
638 const char * const file_names[] = {"attrs", "schemes", "target_ids",
639 "init_regions", "kdamond_pid"};
640 const struct file_operations *fops[] = {&attrs_fops, &schemes_fops,
641 &target_ids_fops, &init_regions_fops, &kdamond_pid_fops};
644 for (i = 0; i < ARRAY_SIZE(file_names); i++)
645 debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
648 static void dbgfs_before_terminate(struct damon_ctx *ctx)
650 struct damon_target *t, *next;
652 if (!targetid_is_pid(ctx))
655 damon_for_each_target_safe(t, next, ctx) {
656 put_pid((struct pid *)t->id);
657 damon_destroy_target(t);
661 static struct damon_ctx *dbgfs_new_ctx(void)
663 struct damon_ctx *ctx;
665 ctx = damon_new_ctx();
669 damon_va_set_primitives(ctx);
670 ctx->callback.before_terminate = dbgfs_before_terminate;
674 static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
676 damon_destroy_ctx(ctx);
680 * Make a context of @name and create a debugfs directory for it.
682 * This function should be called while holding damon_dbgfs_lock.
684 * Returns 0 on success, negative error code otherwise.
686 static int dbgfs_mk_context(char *name)
688 struct dentry *root, **new_dirs, *new_dir;
689 struct damon_ctx **new_ctxs, *new_ctx;
691 if (damon_nr_running_ctxs())
694 new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
695 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
698 dbgfs_ctxs = new_ctxs;
700 new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
701 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
704 dbgfs_dirs = new_dirs;
706 root = dbgfs_dirs[0];
710 new_dir = debugfs_create_dir(name, root);
711 dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
713 new_ctx = dbgfs_new_ctx();
715 debugfs_remove(new_dir);
716 dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
720 dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
721 dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
722 dbgfs_ctxs[dbgfs_nr_ctxs]);
728 static ssize_t dbgfs_mk_context_write(struct file *file,
729 const char __user *buf, size_t count, loff_t *ppos)
735 kbuf = user_input_str(buf, count, ppos);
737 return PTR_ERR(kbuf);
738 ctx_name = kmalloc(count + 1, GFP_KERNEL);
744 /* Trim white space */
745 if (sscanf(kbuf, "%s", ctx_name) != 1) {
750 mutex_lock(&damon_dbgfs_lock);
751 ret = dbgfs_mk_context(ctx_name);
754 mutex_unlock(&damon_dbgfs_lock);
763 * Remove a context of @name and its debugfs directory.
765 * This function should be called while holding damon_dbgfs_lock.
767 * Return 0 on success, negative error code otherwise.
769 static int dbgfs_rm_context(char *name)
771 struct dentry *root, *dir, **new_dirs;
772 struct damon_ctx **new_ctxs;
775 if (damon_nr_running_ctxs())
778 root = dbgfs_dirs[0];
782 dir = debugfs_lookup(name, root);
786 new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
791 new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
798 for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
799 if (dbgfs_dirs[i] == dir) {
800 debugfs_remove(dbgfs_dirs[i]);
801 dbgfs_destroy_ctx(dbgfs_ctxs[i]);
804 new_dirs[j] = dbgfs_dirs[i];
805 new_ctxs[j++] = dbgfs_ctxs[i];
811 dbgfs_dirs = new_dirs;
812 dbgfs_ctxs = new_ctxs;
818 static ssize_t dbgfs_rm_context_write(struct file *file,
819 const char __user *buf, size_t count, loff_t *ppos)
825 kbuf = user_input_str(buf, count, ppos);
827 return PTR_ERR(kbuf);
828 ctx_name = kmalloc(count + 1, GFP_KERNEL);
834 /* Trim white space */
835 if (sscanf(kbuf, "%s", ctx_name) != 1) {
840 mutex_lock(&damon_dbgfs_lock);
841 ret = dbgfs_rm_context(ctx_name);
844 mutex_unlock(&damon_dbgfs_lock);
852 static ssize_t dbgfs_monitor_on_read(struct file *file,
853 char __user *buf, size_t count, loff_t *ppos)
855 char monitor_on_buf[5];
856 bool monitor_on = damon_nr_running_ctxs() != 0;
859 len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
861 return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
864 static ssize_t dbgfs_monitor_on_write(struct file *file,
865 const char __user *buf, size_t count, loff_t *ppos)
870 kbuf = user_input_str(buf, count, ppos);
872 return PTR_ERR(kbuf);
874 /* Remove white space */
875 if (sscanf(kbuf, "%s", kbuf) != 1) {
880 if (!strncmp(kbuf, "on", count)) {
883 for (i = 0; i < dbgfs_nr_ctxs; i++) {
884 if (damon_targets_empty(dbgfs_ctxs[i])) {
889 ret = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs);
890 } else if (!strncmp(kbuf, "off", count)) {
891 ret = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
902 static const struct file_operations mk_contexts_fops = {
903 .write = dbgfs_mk_context_write,
906 static const struct file_operations rm_contexts_fops = {
907 .write = dbgfs_rm_context_write,
910 static const struct file_operations monitor_on_fops = {
911 .read = dbgfs_monitor_on_read,
912 .write = dbgfs_monitor_on_write,
915 static int __init __damon_dbgfs_init(void)
917 struct dentry *dbgfs_root;
918 const char * const file_names[] = {"mk_contexts", "rm_contexts",
920 const struct file_operations *fops[] = {&mk_contexts_fops,
921 &rm_contexts_fops, &monitor_on_fops};
924 dbgfs_root = debugfs_create_dir("damon", NULL);
926 for (i = 0; i < ARRAY_SIZE(file_names); i++)
927 debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
929 dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
931 dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
933 debugfs_remove(dbgfs_root);
936 dbgfs_dirs[0] = dbgfs_root;
942 * Functions for the initialization
945 static int __init damon_dbgfs_init(void)
949 dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
952 dbgfs_ctxs[0] = dbgfs_new_ctx();
953 if (!dbgfs_ctxs[0]) {
959 rc = __damon_dbgfs_init();
961 kfree(dbgfs_ctxs[0]);
963 pr_err("%s: dbgfs init failed\n", __func__);
969 module_init(damon_dbgfs_init);
971 #include "dbgfs-test.h"