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);
101 * Return corresponding dbgfs' scheme action value (int) for the given
102 * damos_action if the given damos_action value is valid and supported by
103 * dbgfs, negative error code otherwise.
105 static int damos_action_to_dbgfs_scheme_action(enum damos_action action)
116 case DAMOS_NOHUGEPAGE:
125 static ssize_t sprint_schemes(struct damon_ctx *c, char *buf, ssize_t len)
131 damon_for_each_scheme(s, c) {
132 rc = scnprintf(&buf[written], len - written,
133 "%lu %lu %u %u %u %u %d %lu %lu %lu %u %u %u %d %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
134 s->min_sz_region, s->max_sz_region,
135 s->min_nr_accesses, s->max_nr_accesses,
136 s->min_age_region, s->max_age_region,
137 damos_action_to_dbgfs_scheme_action(s->action),
138 s->quota.ms, s->quota.sz,
139 s->quota.reset_interval,
141 s->quota.weight_nr_accesses,
143 s->wmarks.metric, s->wmarks.interval,
144 s->wmarks.high, s->wmarks.mid, s->wmarks.low,
145 s->stat.nr_tried, s->stat.sz_tried,
146 s->stat.nr_applied, s->stat.sz_applied,
156 static ssize_t dbgfs_schemes_read(struct file *file, char __user *buf,
157 size_t count, loff_t *ppos)
159 struct damon_ctx *ctx = file->private_data;
163 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
167 mutex_lock(&ctx->kdamond_lock);
168 len = sprint_schemes(ctx, kbuf, count);
169 mutex_unlock(&ctx->kdamond_lock);
172 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
179 static void free_schemes_arr(struct damos **schemes, ssize_t nr_schemes)
183 for (i = 0; i < nr_schemes; i++)
189 * Return corresponding damos_action for the given dbgfs input for a scheme
190 * action if the input is valid, negative error code otherwise.
192 static enum damos_action dbgfs_scheme_action_to_damos_action(int dbgfs_action)
194 switch (dbgfs_action) {
196 return DAMOS_WILLNEED;
200 return DAMOS_PAGEOUT;
202 return DAMOS_HUGEPAGE;
204 return DAMOS_NOHUGEPAGE;
213 * Converts a string into an array of struct damos pointers
215 * Returns an array of struct damos pointers that converted if the conversion
216 * success, or NULL otherwise.
218 static struct damos **str_to_schemes(const char *str, ssize_t len,
221 struct damos *scheme, **schemes;
222 const int max_nr_schemes = 256;
223 int pos = 0, parsed, ret;
224 unsigned long min_sz, max_sz;
225 unsigned int min_nr_a, max_nr_a, min_age, max_age;
226 unsigned int action_input;
227 enum damos_action action;
229 schemes = kmalloc_array(max_nr_schemes, sizeof(scheme),
235 while (pos < len && *nr_schemes < max_nr_schemes) {
236 struct damos_quota quota = {};
237 struct damos_watermarks wmarks;
239 ret = sscanf(&str[pos],
240 "%lu %lu %u %u %u %u %u %lu %lu %lu %u %u %u %u %lu %lu %lu %lu%n",
241 &min_sz, &max_sz, &min_nr_a, &max_nr_a,
242 &min_age, &max_age, &action_input, "a.ms,
243 "a.sz, "a.reset_interval,
244 "a.weight_sz, "a.weight_nr_accesses,
245 "a.weight_age, &wmarks.metric,
246 &wmarks.interval, &wmarks.high, &wmarks.mid,
247 &wmarks.low, &parsed);
250 action = dbgfs_scheme_action_to_damos_action(action_input);
254 if (min_sz > max_sz || min_nr_a > max_nr_a || min_age > max_age)
257 if (wmarks.high < wmarks.mid || wmarks.high < wmarks.low ||
258 wmarks.mid < wmarks.low)
262 scheme = damon_new_scheme(min_sz, max_sz, min_nr_a, max_nr_a,
263 min_age, max_age, action, "a, &wmarks);
267 schemes[*nr_schemes] = scheme;
272 free_schemes_arr(schemes, *nr_schemes);
276 static ssize_t dbgfs_schemes_write(struct file *file, const char __user *buf,
277 size_t count, loff_t *ppos)
279 struct damon_ctx *ctx = file->private_data;
281 struct damos **schemes;
282 ssize_t nr_schemes = 0, ret;
284 kbuf = user_input_str(buf, count, ppos);
286 return PTR_ERR(kbuf);
288 schemes = str_to_schemes(kbuf, count, &nr_schemes);
294 mutex_lock(&ctx->kdamond_lock);
300 ret = damon_set_schemes(ctx, schemes, nr_schemes);
307 mutex_unlock(&ctx->kdamond_lock);
308 free_schemes_arr(schemes, nr_schemes);
314 static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len)
316 struct damon_target *t;
321 damon_for_each_target(t, ctx) {
322 if (damon_target_has_pid(ctx))
323 /* Show pid numbers to debugfs users */
324 id = pid_vnr(t->pid);
326 /* Show 42 for physical address space, just for fun */
329 rc = scnprintf(&buf[written], len - written, "%d ", id);
336 written += scnprintf(&buf[written], len - written, "\n");
340 static ssize_t dbgfs_target_ids_read(struct file *file,
341 char __user *buf, size_t count, loff_t *ppos)
343 struct damon_ctx *ctx = file->private_data;
347 mutex_lock(&ctx->kdamond_lock);
348 len = sprint_target_ids(ctx, ids_buf, 320);
349 mutex_unlock(&ctx->kdamond_lock);
353 return simple_read_from_buffer(buf, count, ppos, ids_buf, len);
357 * Converts a string into an integers array
359 * Returns an array of integers array if the conversion success, or NULL
362 static int *str_to_ints(const char *str, ssize_t len, ssize_t *nr_ints)
365 const int max_nr_ints = 32;
367 int pos = 0, parsed, ret;
370 array = kmalloc_array(max_nr_ints, sizeof(*array), GFP_KERNEL);
373 while (*nr_ints < max_nr_ints && pos < len) {
374 ret = sscanf(&str[pos], "%d%n", &nr, &parsed);
378 array[*nr_ints] = nr;
385 static void dbgfs_put_pids(struct pid **pids, int nr_pids)
389 for (i = 0; i < nr_pids; i++)
394 * Converts a string into an struct pid pointers array
396 * Returns an array of struct pid pointers if the conversion success, or NULL
399 static struct pid **str_to_pids(const char *str, ssize_t len, ssize_t *nr_pids)
407 ints = str_to_ints(str, len, &nr_ints);
411 pids = kmalloc_array(nr_ints, sizeof(*pids), GFP_KERNEL);
415 for (; *nr_pids < nr_ints; (*nr_pids)++) {
416 pids[*nr_pids] = find_get_pid(ints[*nr_pids]);
417 if (!pids[*nr_pids]) {
418 dbgfs_put_pids(pids, *nr_pids);
431 * dbgfs_set_targets() - Set monitoring targets.
432 * @ctx: monitoring context
433 * @nr_targets: number of targets
434 * @pids: array of target pids (size is same to @nr_targets)
436 * This function should not be called while the kdamond is running. @pids is
437 * ignored if the context is not configured to have pid in each target. On
438 * failure, reference counts of all pids in @pids are decremented.
440 * Return: 0 on success, negative error code otherwise.
442 static int dbgfs_set_targets(struct damon_ctx *ctx, ssize_t nr_targets,
446 struct damon_target *t, *next;
448 damon_for_each_target_safe(t, next, ctx) {
449 if (damon_target_has_pid(ctx))
451 damon_destroy_target(t);
454 for (i = 0; i < nr_targets; i++) {
455 t = damon_new_target();
457 damon_for_each_target_safe(t, next, ctx)
458 damon_destroy_target(t);
459 if (damon_target_has_pid(ctx))
460 dbgfs_put_pids(pids, nr_targets);
463 if (damon_target_has_pid(ctx))
465 damon_add_target(ctx, t);
471 static ssize_t dbgfs_target_ids_write(struct file *file,
472 const char __user *buf, size_t count, loff_t *ppos)
474 struct damon_ctx *ctx = file->private_data;
475 bool id_is_pid = true;
477 struct pid **target_pids = NULL;
481 kbuf = user_input_str(buf, count, ppos);
483 return PTR_ERR(kbuf);
485 if (!strncmp(kbuf, "paddr\n", count)) {
491 target_pids = str_to_pids(kbuf, count, &nr_targets);
498 mutex_lock(&ctx->kdamond_lock);
501 dbgfs_put_pids(target_pids, nr_targets);
506 /* remove previously set targets */
507 dbgfs_set_targets(ctx, 0, NULL);
513 /* Configure the context for the address space type */
515 ret = damon_select_ops(ctx, DAMON_OPS_VADDR);
517 ret = damon_select_ops(ctx, DAMON_OPS_PADDR);
521 ret = dbgfs_set_targets(ctx, nr_targets, target_pids);
526 mutex_unlock(&ctx->kdamond_lock);
533 static ssize_t sprint_init_regions(struct damon_ctx *c, char *buf, ssize_t len)
535 struct damon_target *t;
536 struct damon_region *r;
541 damon_for_each_target(t, c) {
542 damon_for_each_region(r, t) {
543 rc = scnprintf(&buf[written], len - written,
545 target_idx, r->ar.start, r->ar.end);
555 static ssize_t dbgfs_init_regions_read(struct file *file, char __user *buf,
556 size_t count, loff_t *ppos)
558 struct damon_ctx *ctx = file->private_data;
562 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
566 mutex_lock(&ctx->kdamond_lock);
568 mutex_unlock(&ctx->kdamond_lock);
573 len = sprint_init_regions(ctx, kbuf, count);
574 mutex_unlock(&ctx->kdamond_lock);
577 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
584 static int add_init_region(struct damon_ctx *c, int target_idx,
585 struct damon_addr_range *ar)
587 struct damon_target *t;
588 struct damon_region *r, *prev;
589 unsigned long idx = 0;
592 if (ar->start >= ar->end)
595 damon_for_each_target(t, c) {
596 if (idx++ == target_idx) {
597 r = damon_new_region(ar->start, ar->end);
600 damon_add_region(r, t);
601 if (damon_nr_regions(t) > 1) {
602 prev = damon_prev_region(r);
603 if (prev->ar.end > r->ar.start) {
604 damon_destroy_region(r, t);
614 static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len)
616 struct damon_target *t;
617 struct damon_region *r, *next;
618 int pos = 0, parsed, ret;
620 struct damon_addr_range ar;
623 damon_for_each_target(t, c) {
624 damon_for_each_region_safe(r, next, t)
625 damon_destroy_region(r, t);
629 ret = sscanf(&str[pos], "%d %lu %lu%n",
630 &target_idx, &ar.start, &ar.end, &parsed);
633 err = add_init_region(c, target_idx, &ar);
642 damon_for_each_target(t, c) {
643 damon_for_each_region_safe(r, next, t)
644 damon_destroy_region(r, t);
649 static ssize_t dbgfs_init_regions_write(struct file *file,
650 const char __user *buf, size_t count,
653 struct damon_ctx *ctx = file->private_data;
658 kbuf = user_input_str(buf, count, ppos);
660 return PTR_ERR(kbuf);
662 mutex_lock(&ctx->kdamond_lock);
668 err = set_init_regions(ctx, kbuf, ret);
673 mutex_unlock(&ctx->kdamond_lock);
678 static ssize_t dbgfs_kdamond_pid_read(struct file *file,
679 char __user *buf, size_t count, loff_t *ppos)
681 struct damon_ctx *ctx = file->private_data;
685 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
689 mutex_lock(&ctx->kdamond_lock);
691 len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
693 len = scnprintf(kbuf, count, "none\n");
694 mutex_unlock(&ctx->kdamond_lock);
697 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
704 static int damon_dbgfs_open(struct inode *inode, struct file *file)
706 file->private_data = inode->i_private;
708 return nonseekable_open(inode, file);
711 static const struct file_operations attrs_fops = {
712 .open = damon_dbgfs_open,
713 .read = dbgfs_attrs_read,
714 .write = dbgfs_attrs_write,
717 static const struct file_operations schemes_fops = {
718 .open = damon_dbgfs_open,
719 .read = dbgfs_schemes_read,
720 .write = dbgfs_schemes_write,
723 static const struct file_operations target_ids_fops = {
724 .open = damon_dbgfs_open,
725 .read = dbgfs_target_ids_read,
726 .write = dbgfs_target_ids_write,
729 static const struct file_operations init_regions_fops = {
730 .open = damon_dbgfs_open,
731 .read = dbgfs_init_regions_read,
732 .write = dbgfs_init_regions_write,
735 static const struct file_operations kdamond_pid_fops = {
736 .open = damon_dbgfs_open,
737 .read = dbgfs_kdamond_pid_read,
740 static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
742 const char * const file_names[] = {"attrs", "schemes", "target_ids",
743 "init_regions", "kdamond_pid"};
744 const struct file_operations *fops[] = {&attrs_fops, &schemes_fops,
745 &target_ids_fops, &init_regions_fops, &kdamond_pid_fops};
748 for (i = 0; i < ARRAY_SIZE(file_names); i++)
749 debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
752 static void dbgfs_before_terminate(struct damon_ctx *ctx)
754 struct damon_target *t, *next;
756 if (!damon_target_has_pid(ctx))
759 mutex_lock(&ctx->kdamond_lock);
760 damon_for_each_target_safe(t, next, ctx) {
762 damon_destroy_target(t);
764 mutex_unlock(&ctx->kdamond_lock);
767 static struct damon_ctx *dbgfs_new_ctx(void)
769 struct damon_ctx *ctx;
771 ctx = damon_new_ctx();
775 if (damon_select_ops(ctx, DAMON_OPS_VADDR) &&
776 damon_select_ops(ctx, DAMON_OPS_PADDR)) {
777 damon_destroy_ctx(ctx);
780 ctx->callback.before_terminate = dbgfs_before_terminate;
784 static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
786 damon_destroy_ctx(ctx);
790 * Make a context of @name and create a debugfs directory for it.
792 * This function should be called while holding damon_dbgfs_lock.
794 * Returns 0 on success, negative error code otherwise.
796 static int dbgfs_mk_context(char *name)
798 struct dentry *root, **new_dirs, *new_dir;
799 struct damon_ctx **new_ctxs, *new_ctx;
801 if (damon_nr_running_ctxs())
804 new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
805 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
808 dbgfs_ctxs = new_ctxs;
810 new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
811 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
814 dbgfs_dirs = new_dirs;
816 root = dbgfs_dirs[0];
820 new_dir = debugfs_create_dir(name, root);
821 /* Below check is required for a potential duplicated name case */
823 return PTR_ERR(new_dir);
824 dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
826 new_ctx = dbgfs_new_ctx();
828 debugfs_remove(new_dir);
829 dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
833 dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
834 dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
835 dbgfs_ctxs[dbgfs_nr_ctxs]);
841 static ssize_t dbgfs_mk_context_write(struct file *file,
842 const char __user *buf, size_t count, loff_t *ppos)
848 kbuf = user_input_str(buf, count, ppos);
850 return PTR_ERR(kbuf);
851 ctx_name = kmalloc(count + 1, GFP_KERNEL);
857 /* Trim white space */
858 if (sscanf(kbuf, "%s", ctx_name) != 1) {
863 mutex_lock(&damon_dbgfs_lock);
864 ret = dbgfs_mk_context(ctx_name);
867 mutex_unlock(&damon_dbgfs_lock);
876 * Remove a context of @name and its debugfs directory.
878 * This function should be called while holding damon_dbgfs_lock.
880 * Return 0 on success, negative error code otherwise.
882 static int dbgfs_rm_context(char *name)
884 struct dentry *root, *dir, **new_dirs;
885 struct damon_ctx **new_ctxs;
889 if (damon_nr_running_ctxs())
892 root = dbgfs_dirs[0];
896 dir = debugfs_lookup(name, root);
900 new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
907 new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
914 for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
915 if (dbgfs_dirs[i] == dir) {
916 debugfs_remove(dbgfs_dirs[i]);
917 dbgfs_destroy_ctx(dbgfs_ctxs[i]);
920 new_dirs[j] = dbgfs_dirs[i];
921 new_ctxs[j++] = dbgfs_ctxs[i];
927 dbgfs_dirs = new_dirs;
928 dbgfs_ctxs = new_ctxs;
940 static ssize_t dbgfs_rm_context_write(struct file *file,
941 const char __user *buf, size_t count, loff_t *ppos)
947 kbuf = user_input_str(buf, count, ppos);
949 return PTR_ERR(kbuf);
950 ctx_name = kmalloc(count + 1, GFP_KERNEL);
956 /* Trim white space */
957 if (sscanf(kbuf, "%s", ctx_name) != 1) {
962 mutex_lock(&damon_dbgfs_lock);
963 ret = dbgfs_rm_context(ctx_name);
966 mutex_unlock(&damon_dbgfs_lock);
974 static ssize_t dbgfs_monitor_on_read(struct file *file,
975 char __user *buf, size_t count, loff_t *ppos)
977 char monitor_on_buf[5];
978 bool monitor_on = damon_nr_running_ctxs() != 0;
981 len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
983 return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
986 static ssize_t dbgfs_monitor_on_write(struct file *file,
987 const char __user *buf, size_t count, loff_t *ppos)
992 kbuf = user_input_str(buf, count, ppos);
994 return PTR_ERR(kbuf);
996 /* Remove white space */
997 if (sscanf(kbuf, "%s", kbuf) != 1) {
1002 mutex_lock(&damon_dbgfs_lock);
1003 if (!strncmp(kbuf, "on", count)) {
1006 for (i = 0; i < dbgfs_nr_ctxs; i++) {
1007 if (damon_targets_empty(dbgfs_ctxs[i])) {
1009 mutex_unlock(&damon_dbgfs_lock);
1013 ret = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs, true);
1014 } else if (!strncmp(kbuf, "off", count)) {
1015 ret = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
1019 mutex_unlock(&damon_dbgfs_lock);
1027 static const struct file_operations mk_contexts_fops = {
1028 .write = dbgfs_mk_context_write,
1031 static const struct file_operations rm_contexts_fops = {
1032 .write = dbgfs_rm_context_write,
1035 static const struct file_operations monitor_on_fops = {
1036 .read = dbgfs_monitor_on_read,
1037 .write = dbgfs_monitor_on_write,
1040 static int __init __damon_dbgfs_init(void)
1042 struct dentry *dbgfs_root;
1043 const char * const file_names[] = {"mk_contexts", "rm_contexts",
1045 const struct file_operations *fops[] = {&mk_contexts_fops,
1046 &rm_contexts_fops, &monitor_on_fops};
1049 dbgfs_root = debugfs_create_dir("damon", NULL);
1051 for (i = 0; i < ARRAY_SIZE(file_names); i++)
1052 debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
1054 dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
1056 dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
1058 debugfs_remove(dbgfs_root);
1061 dbgfs_dirs[0] = dbgfs_root;
1067 * Functions for the initialization
1070 static int __init damon_dbgfs_init(void)
1074 mutex_lock(&damon_dbgfs_lock);
1075 dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
1078 dbgfs_ctxs[0] = dbgfs_new_ctx();
1079 if (!dbgfs_ctxs[0]) {
1085 rc = __damon_dbgfs_init();
1087 kfree(dbgfs_ctxs[0]);
1089 pr_err("%s: dbgfs init failed\n", __func__);
1093 mutex_unlock(&damon_dbgfs_lock);
1097 module_init(damon_dbgfs_init);
1099 #include "dbgfs-test.h"