1 // SPDX-License-Identifier: GPL-2.0
3 * DAMON Debugfs Interface
5 * Author: SeongJae Park <sjpark@amazon.de>
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;
75 kbuf = user_input_str(buf, count, ppos);
79 if (sscanf(kbuf, "%lu %lu %lu %lu %lu",
80 &s, &a, &r, &minr, &maxr) != 5) {
85 mutex_lock(&ctx->kdamond_lock);
91 err = damon_set_attrs(ctx, s, a, r, minr, maxr);
95 mutex_unlock(&ctx->kdamond_lock);
101 static inline bool targetid_is_pid(const struct damon_ctx *ctx)
103 return ctx->primitive.target_valid == damon_va_target_valid;
106 static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len)
108 struct damon_target *t;
113 damon_for_each_target(t, ctx) {
115 if (targetid_is_pid(ctx))
116 /* Show pid numbers to debugfs users */
117 id = (unsigned long)pid_vnr((struct pid *)id);
119 rc = scnprintf(&buf[written], len - written, "%lu ", id);
126 written += scnprintf(&buf[written], len - written, "\n");
130 static ssize_t dbgfs_target_ids_read(struct file *file,
131 char __user *buf, size_t count, loff_t *ppos)
133 struct damon_ctx *ctx = file->private_data;
137 mutex_lock(&ctx->kdamond_lock);
138 len = sprint_target_ids(ctx, ids_buf, 320);
139 mutex_unlock(&ctx->kdamond_lock);
143 return simple_read_from_buffer(buf, count, ppos, ids_buf, len);
147 * Converts a string into an array of unsigned long integers
149 * Returns an array of unsigned long integers if the conversion success, or
152 static unsigned long *str_to_target_ids(const char *str, ssize_t len,
156 const int max_nr_ids = 32;
158 int pos = 0, parsed, ret;
161 ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL);
164 while (*nr_ids < max_nr_ids && pos < len) {
165 ret = sscanf(&str[pos], "%lu%n", &id, &parsed);
176 static void dbgfs_put_pids(unsigned long *ids, int nr_ids)
180 for (i = 0; i < nr_ids; i++)
181 put_pid((struct pid *)ids[i]);
184 static ssize_t dbgfs_target_ids_write(struct file *file,
185 const char __user *buf, size_t count, loff_t *ppos)
187 struct damon_ctx *ctx = file->private_data;
188 struct damon_target *t, *next_t;
190 unsigned long *targets;
196 kbuf = user_input_str(buf, count, ppos);
198 return PTR_ERR(kbuf);
202 targets = str_to_target_ids(nrs, ret, &nr_targets);
208 if (targetid_is_pid(ctx)) {
209 for (i = 0; i < nr_targets; i++) {
210 targets[i] = (unsigned long)find_get_pid(
213 dbgfs_put_pids(targets, i);
215 goto free_targets_out;
220 mutex_lock(&ctx->kdamond_lock);
222 if (targetid_is_pid(ctx))
223 dbgfs_put_pids(targets, nr_targets);
228 /* remove previously set targets */
229 damon_for_each_target_safe(t, next_t, ctx) {
230 if (targetid_is_pid(ctx))
231 put_pid((struct pid *)t->id);
232 damon_destroy_target(t);
235 err = damon_set_targets(ctx, targets, nr_targets);
237 if (targetid_is_pid(ctx))
238 dbgfs_put_pids(targets, nr_targets);
243 mutex_unlock(&ctx->kdamond_lock);
251 static ssize_t dbgfs_kdamond_pid_read(struct file *file,
252 char __user *buf, size_t count, loff_t *ppos)
254 struct damon_ctx *ctx = file->private_data;
258 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
262 mutex_lock(&ctx->kdamond_lock);
264 len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
266 len = scnprintf(kbuf, count, "none\n");
267 mutex_unlock(&ctx->kdamond_lock);
270 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
277 static int damon_dbgfs_open(struct inode *inode, struct file *file)
279 file->private_data = inode->i_private;
281 return nonseekable_open(inode, file);
284 static const struct file_operations attrs_fops = {
285 .open = damon_dbgfs_open,
286 .read = dbgfs_attrs_read,
287 .write = dbgfs_attrs_write,
290 static const struct file_operations target_ids_fops = {
291 .open = damon_dbgfs_open,
292 .read = dbgfs_target_ids_read,
293 .write = dbgfs_target_ids_write,
296 static const struct file_operations kdamond_pid_fops = {
297 .open = damon_dbgfs_open,
298 .read = dbgfs_kdamond_pid_read,
301 static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
303 const char * const file_names[] = {"attrs", "target_ids",
305 const struct file_operations *fops[] = {&attrs_fops, &target_ids_fops,
309 for (i = 0; i < ARRAY_SIZE(file_names); i++)
310 debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
313 static int dbgfs_before_terminate(struct damon_ctx *ctx)
315 struct damon_target *t, *next;
317 if (!targetid_is_pid(ctx))
320 mutex_lock(&ctx->kdamond_lock);
321 damon_for_each_target_safe(t, next, ctx) {
322 put_pid((struct pid *)t->id);
323 damon_destroy_target(t);
325 mutex_unlock(&ctx->kdamond_lock);
329 static struct damon_ctx *dbgfs_new_ctx(void)
331 struct damon_ctx *ctx;
333 ctx = damon_new_ctx();
337 damon_va_set_primitives(ctx);
338 ctx->callback.before_terminate = dbgfs_before_terminate;
342 static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
344 damon_destroy_ctx(ctx);
348 * Make a context of @name and create a debugfs directory for it.
350 * This function should be called while holding damon_dbgfs_lock.
352 * Returns 0 on success, negative error code otherwise.
354 static int dbgfs_mk_context(char *name)
356 struct dentry *root, **new_dirs, *new_dir;
357 struct damon_ctx **new_ctxs, *new_ctx;
359 if (damon_nr_running_ctxs())
362 new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
363 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
366 dbgfs_ctxs = new_ctxs;
368 new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
369 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
372 dbgfs_dirs = new_dirs;
374 root = dbgfs_dirs[0];
378 new_dir = debugfs_create_dir(name, root);
379 /* Below check is required for a potential duplicated name case */
381 return PTR_ERR(new_dir);
382 dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
384 new_ctx = dbgfs_new_ctx();
386 debugfs_remove(new_dir);
387 dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
391 dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
392 dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
393 dbgfs_ctxs[dbgfs_nr_ctxs]);
399 static ssize_t dbgfs_mk_context_write(struct file *file,
400 const char __user *buf, size_t count, loff_t *ppos)
407 kbuf = user_input_str(buf, count, ppos);
409 return PTR_ERR(kbuf);
410 ctx_name = kmalloc(count + 1, GFP_KERNEL);
416 /* Trim white space */
417 if (sscanf(kbuf, "%s", ctx_name) != 1) {
422 mutex_lock(&damon_dbgfs_lock);
423 err = dbgfs_mk_context(ctx_name);
426 mutex_unlock(&damon_dbgfs_lock);
435 * Remove a context of @name and its debugfs directory.
437 * This function should be called while holding damon_dbgfs_lock.
439 * Return 0 on success, negative error code otherwise.
441 static int dbgfs_rm_context(char *name)
443 struct dentry *root, *dir, **new_dirs;
445 struct damon_ctx **new_ctxs;
449 if (damon_nr_running_ctxs())
452 root = dbgfs_dirs[0];
456 dir = debugfs_lookup(name, root);
460 inode = d_inode(dir);
461 if (!S_ISDIR(inode->i_mode)) {
466 new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
473 new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
480 for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
481 if (dbgfs_dirs[i] == dir) {
482 debugfs_remove(dbgfs_dirs[i]);
483 dbgfs_destroy_ctx(dbgfs_ctxs[i]);
486 new_dirs[j] = dbgfs_dirs[i];
487 new_ctxs[j++] = dbgfs_ctxs[i];
493 dbgfs_dirs = new_dirs;
494 dbgfs_ctxs = new_ctxs;
506 static ssize_t dbgfs_rm_context_write(struct file *file,
507 const char __user *buf, size_t count, loff_t *ppos)
514 kbuf = user_input_str(buf, count, ppos);
516 return PTR_ERR(kbuf);
517 ctx_name = kmalloc(count + 1, GFP_KERNEL);
523 /* Trim white space */
524 if (sscanf(kbuf, "%s", ctx_name) != 1) {
529 mutex_lock(&damon_dbgfs_lock);
530 err = dbgfs_rm_context(ctx_name);
533 mutex_unlock(&damon_dbgfs_lock);
541 static ssize_t dbgfs_monitor_on_read(struct file *file,
542 char __user *buf, size_t count, loff_t *ppos)
544 char monitor_on_buf[5];
545 bool monitor_on = damon_nr_running_ctxs() != 0;
548 len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
550 return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
553 static ssize_t dbgfs_monitor_on_write(struct file *file,
554 const char __user *buf, size_t count, loff_t *ppos)
560 kbuf = user_input_str(buf, count, ppos);
562 return PTR_ERR(kbuf);
564 /* Remove white space */
565 if (sscanf(kbuf, "%s", kbuf) != 1) {
570 mutex_lock(&damon_dbgfs_lock);
571 if (!strncmp(kbuf, "on", count))
572 err = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs);
573 else if (!strncmp(kbuf, "off", count))
574 err = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
577 mutex_unlock(&damon_dbgfs_lock);
585 static const struct file_operations mk_contexts_fops = {
586 .write = dbgfs_mk_context_write,
589 static const struct file_operations rm_contexts_fops = {
590 .write = dbgfs_rm_context_write,
593 static const struct file_operations monitor_on_fops = {
594 .read = dbgfs_monitor_on_read,
595 .write = dbgfs_monitor_on_write,
598 static int __init __damon_dbgfs_init(void)
600 struct dentry *dbgfs_root;
601 const char * const file_names[] = {"mk_contexts", "rm_contexts",
603 const struct file_operations *fops[] = {&mk_contexts_fops,
604 &rm_contexts_fops, &monitor_on_fops};
607 dbgfs_root = debugfs_create_dir("damon", NULL);
609 for (i = 0; i < ARRAY_SIZE(file_names); i++)
610 debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
612 dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
614 dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
616 debugfs_remove(dbgfs_root);
619 dbgfs_dirs[0] = dbgfs_root;
625 * Functions for the initialization
628 static int __init damon_dbgfs_init(void)
632 mutex_lock(&damon_dbgfs_lock);
633 dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
636 dbgfs_ctxs[0] = dbgfs_new_ctx();
637 if (!dbgfs_ctxs[0]) {
643 rc = __damon_dbgfs_init();
645 kfree(dbgfs_ctxs[0]);
647 pr_err("%s: dbgfs init failed\n", __func__);
651 mutex_unlock(&damon_dbgfs_lock);
655 module_init(damon_dbgfs_init);
657 #include "dbgfs-test.h"