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;
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;
189 unsigned long *targets;
195 kbuf = user_input_str(buf, count, ppos);
197 return PTR_ERR(kbuf);
201 targets = str_to_target_ids(nrs, ret, &nr_targets);
207 if (targetid_is_pid(ctx)) {
208 for (i = 0; i < nr_targets; i++) {
209 targets[i] = (unsigned long)find_get_pid(
212 dbgfs_put_pids(targets, i);
214 goto free_targets_out;
219 mutex_lock(&ctx->kdamond_lock);
221 if (targetid_is_pid(ctx))
222 dbgfs_put_pids(targets, nr_targets);
227 err = damon_set_targets(ctx, targets, nr_targets);
229 if (targetid_is_pid(ctx))
230 dbgfs_put_pids(targets, nr_targets);
235 mutex_unlock(&ctx->kdamond_lock);
243 static ssize_t dbgfs_kdamond_pid_read(struct file *file,
244 char __user *buf, size_t count, loff_t *ppos)
246 struct damon_ctx *ctx = file->private_data;
250 kbuf = kmalloc(count, GFP_KERNEL);
254 mutex_lock(&ctx->kdamond_lock);
256 len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
258 len = scnprintf(kbuf, count, "none\n");
259 mutex_unlock(&ctx->kdamond_lock);
262 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
269 static int damon_dbgfs_open(struct inode *inode, struct file *file)
271 file->private_data = inode->i_private;
273 return nonseekable_open(inode, file);
276 static const struct file_operations attrs_fops = {
277 .open = damon_dbgfs_open,
278 .read = dbgfs_attrs_read,
279 .write = dbgfs_attrs_write,
282 static const struct file_operations target_ids_fops = {
283 .open = damon_dbgfs_open,
284 .read = dbgfs_target_ids_read,
285 .write = dbgfs_target_ids_write,
288 static const struct file_operations kdamond_pid_fops = {
289 .open = damon_dbgfs_open,
290 .read = dbgfs_kdamond_pid_read,
293 static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
295 const char * const file_names[] = {"attrs", "target_ids",
297 const struct file_operations *fops[] = {&attrs_fops, &target_ids_fops,
301 for (i = 0; i < ARRAY_SIZE(file_names); i++)
302 debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
305 static int dbgfs_before_terminate(struct damon_ctx *ctx)
307 struct damon_target *t, *next;
309 if (!targetid_is_pid(ctx))
312 damon_for_each_target_safe(t, next, ctx) {
313 put_pid((struct pid *)t->id);
314 damon_destroy_target(t);
319 static struct damon_ctx *dbgfs_new_ctx(void)
321 struct damon_ctx *ctx;
323 ctx = damon_new_ctx();
327 damon_va_set_primitives(ctx);
328 ctx->callback.before_terminate = dbgfs_before_terminate;
332 static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
334 damon_destroy_ctx(ctx);
338 * Make a context of @name and create a debugfs directory for it.
340 * This function should be called while holding damon_dbgfs_lock.
342 * Returns 0 on success, negative error code otherwise.
344 static int dbgfs_mk_context(char *name)
346 struct dentry *root, **new_dirs, *new_dir;
347 struct damon_ctx **new_ctxs, *new_ctx;
349 if (damon_nr_running_ctxs())
352 new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
353 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
356 dbgfs_ctxs = new_ctxs;
358 new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
359 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
362 dbgfs_dirs = new_dirs;
364 root = dbgfs_dirs[0];
368 new_dir = debugfs_create_dir(name, root);
369 dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
371 new_ctx = dbgfs_new_ctx();
373 debugfs_remove(new_dir);
374 dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
378 dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
379 dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
380 dbgfs_ctxs[dbgfs_nr_ctxs]);
386 static ssize_t dbgfs_mk_context_write(struct file *file,
387 const char __user *buf, size_t count, loff_t *ppos)
394 kbuf = user_input_str(buf, count, ppos);
396 return PTR_ERR(kbuf);
397 ctx_name = kmalloc(count + 1, GFP_KERNEL);
403 /* Trim white space */
404 if (sscanf(kbuf, "%s", ctx_name) != 1) {
409 mutex_lock(&damon_dbgfs_lock);
410 err = dbgfs_mk_context(ctx_name);
413 mutex_unlock(&damon_dbgfs_lock);
422 * Remove a context of @name and its debugfs directory.
424 * This function should be called while holding damon_dbgfs_lock.
426 * Return 0 on success, negative error code otherwise.
428 static int dbgfs_rm_context(char *name)
430 struct dentry *root, *dir, **new_dirs;
431 struct damon_ctx **new_ctxs;
434 if (damon_nr_running_ctxs())
437 root = dbgfs_dirs[0];
441 dir = debugfs_lookup(name, root);
445 new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
450 new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
457 for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
458 if (dbgfs_dirs[i] == dir) {
459 debugfs_remove(dbgfs_dirs[i]);
460 dbgfs_destroy_ctx(dbgfs_ctxs[i]);
463 new_dirs[j] = dbgfs_dirs[i];
464 new_ctxs[j++] = dbgfs_ctxs[i];
470 dbgfs_dirs = new_dirs;
471 dbgfs_ctxs = new_ctxs;
477 static ssize_t dbgfs_rm_context_write(struct file *file,
478 const char __user *buf, size_t count, loff_t *ppos)
485 kbuf = user_input_str(buf, count, ppos);
487 return PTR_ERR(kbuf);
488 ctx_name = kmalloc(count + 1, GFP_KERNEL);
494 /* Trim white space */
495 if (sscanf(kbuf, "%s", ctx_name) != 1) {
500 mutex_lock(&damon_dbgfs_lock);
501 err = dbgfs_rm_context(ctx_name);
504 mutex_unlock(&damon_dbgfs_lock);
512 static ssize_t dbgfs_monitor_on_read(struct file *file,
513 char __user *buf, size_t count, loff_t *ppos)
515 char monitor_on_buf[5];
516 bool monitor_on = damon_nr_running_ctxs() != 0;
519 len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
521 return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
524 static ssize_t dbgfs_monitor_on_write(struct file *file,
525 const char __user *buf, size_t count, loff_t *ppos)
531 kbuf = user_input_str(buf, count, ppos);
533 return PTR_ERR(kbuf);
535 /* Remove white space */
536 if (sscanf(kbuf, "%s", kbuf) != 1) {
541 if (!strncmp(kbuf, "on", count))
542 err = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs);
543 else if (!strncmp(kbuf, "off", count))
544 err = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
554 static const struct file_operations mk_contexts_fops = {
555 .write = dbgfs_mk_context_write,
558 static const struct file_operations rm_contexts_fops = {
559 .write = dbgfs_rm_context_write,
562 static const struct file_operations monitor_on_fops = {
563 .read = dbgfs_monitor_on_read,
564 .write = dbgfs_monitor_on_write,
567 static int __init __damon_dbgfs_init(void)
569 struct dentry *dbgfs_root;
570 const char * const file_names[] = {"mk_contexts", "rm_contexts",
572 const struct file_operations *fops[] = {&mk_contexts_fops,
573 &rm_contexts_fops, &monitor_on_fops};
576 dbgfs_root = debugfs_create_dir("damon", NULL);
578 for (i = 0; i < ARRAY_SIZE(file_names); i++)
579 debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
581 dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
583 dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
585 debugfs_remove(dbgfs_root);
588 dbgfs_dirs[0] = dbgfs_root;
594 * Functions for the initialization
597 static int __init damon_dbgfs_init(void)
601 dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
604 dbgfs_ctxs[0] = dbgfs_new_ctx();
605 if (!dbgfs_ctxs[0]) {
611 rc = __damon_dbgfs_init();
613 kfree(dbgfs_ctxs[0]);
615 pr_err("%s: dbgfs init failed\n", __func__);
621 module_init(damon_dbgfs_init);
623 #include "dbgfs-test.h"