xref: /openbmc/linux/mm/damon/dbgfs.c (revision 48998c17)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DAMON Debugfs Interface
4  *
5  * Author: SeongJae Park <sjpark@amazon.de>
6  */
7 
8 #define pr_fmt(fmt) "damon-dbgfs: " fmt
9 
10 #include <linux/damon.h>
11 #include <linux/debugfs.h>
12 #include <linux/file.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/page_idle.h>
16 #include <linux/slab.h>
17 
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);
22 
23 /*
24  * Returns non-empty string on success, negative error code otherwise.
25  */
26 static char *user_input_str(const char __user *buf, size_t count, loff_t *ppos)
27 {
28 	char *kbuf;
29 	ssize_t ret;
30 
31 	/* We do not accept continuous write */
32 	if (*ppos)
33 		return ERR_PTR(-EINVAL);
34 
35 	kbuf = kmalloc(count + 1, GFP_KERNEL | __GFP_NOWARN);
36 	if (!kbuf)
37 		return ERR_PTR(-ENOMEM);
38 
39 	ret = simple_write_to_buffer(kbuf, count + 1, ppos, buf, count);
40 	if (ret != count) {
41 		kfree(kbuf);
42 		return ERR_PTR(-EIO);
43 	}
44 	kbuf[ret] = '\0';
45 
46 	return kbuf;
47 }
48 
49 static ssize_t dbgfs_attrs_read(struct file *file,
50 		char __user *buf, size_t count, loff_t *ppos)
51 {
52 	struct damon_ctx *ctx = file->private_data;
53 	char kbuf[128];
54 	int ret;
55 
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,
60 			ctx->max_nr_regions);
61 	mutex_unlock(&ctx->kdamond_lock);
62 
63 	return simple_read_from_buffer(buf, count, ppos, kbuf, ret);
64 }
65 
66 static ssize_t dbgfs_attrs_write(struct file *file,
67 		const char __user *buf, size_t count, loff_t *ppos)
68 {
69 	struct damon_ctx *ctx = file->private_data;
70 	unsigned long s, a, r, minr, maxr;
71 	char *kbuf;
72 	ssize_t ret = count;
73 	int err;
74 
75 	kbuf = user_input_str(buf, count, ppos);
76 	if (IS_ERR(kbuf))
77 		return PTR_ERR(kbuf);
78 
79 	if (sscanf(kbuf, "%lu %lu %lu %lu %lu",
80 				&s, &a, &r, &minr, &maxr) != 5) {
81 		ret = -EINVAL;
82 		goto out;
83 	}
84 
85 	mutex_lock(&ctx->kdamond_lock);
86 	if (ctx->kdamond) {
87 		ret = -EBUSY;
88 		goto unlock_out;
89 	}
90 
91 	err = damon_set_attrs(ctx, s, a, r, minr, maxr);
92 	if (err)
93 		ret = err;
94 unlock_out:
95 	mutex_unlock(&ctx->kdamond_lock);
96 out:
97 	kfree(kbuf);
98 	return ret;
99 }
100 
101 static inline bool targetid_is_pid(const struct damon_ctx *ctx)
102 {
103 	return ctx->primitive.target_valid == damon_va_target_valid;
104 }
105 
106 static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len)
107 {
108 	struct damon_target *t;
109 	unsigned long id;
110 	int written = 0;
111 	int rc;
112 
113 	damon_for_each_target(t, ctx) {
114 		id = t->id;
115 		if (targetid_is_pid(ctx))
116 			/* Show pid numbers to debugfs users */
117 			id = (unsigned long)pid_vnr((struct pid *)id);
118 
119 		rc = scnprintf(&buf[written], len - written, "%lu ", id);
120 		if (!rc)
121 			return -ENOMEM;
122 		written += rc;
123 	}
124 	if (written)
125 		written -= 1;
126 	written += scnprintf(&buf[written], len - written, "\n");
127 	return written;
128 }
129 
130 static ssize_t dbgfs_target_ids_read(struct file *file,
131 		char __user *buf, size_t count, loff_t *ppos)
132 {
133 	struct damon_ctx *ctx = file->private_data;
134 	ssize_t len;
135 	char ids_buf[320];
136 
137 	mutex_lock(&ctx->kdamond_lock);
138 	len = sprint_target_ids(ctx, ids_buf, 320);
139 	mutex_unlock(&ctx->kdamond_lock);
140 	if (len < 0)
141 		return len;
142 
143 	return simple_read_from_buffer(buf, count, ppos, ids_buf, len);
144 }
145 
146 /*
147  * Converts a string into an array of unsigned long integers
148  *
149  * Returns an array of unsigned long integers if the conversion success, or
150  * NULL otherwise.
151  */
152 static unsigned long *str_to_target_ids(const char *str, ssize_t len,
153 					ssize_t *nr_ids)
154 {
155 	unsigned long *ids;
156 	const int max_nr_ids = 32;
157 	unsigned long id;
158 	int pos = 0, parsed, ret;
159 
160 	*nr_ids = 0;
161 	ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL);
162 	if (!ids)
163 		return NULL;
164 	while (*nr_ids < max_nr_ids && pos < len) {
165 		ret = sscanf(&str[pos], "%lu%n", &id, &parsed);
166 		pos += parsed;
167 		if (ret != 1)
168 			break;
169 		ids[*nr_ids] = id;
170 		*nr_ids += 1;
171 	}
172 
173 	return ids;
174 }
175 
176 static void dbgfs_put_pids(unsigned long *ids, int nr_ids)
177 {
178 	int i;
179 
180 	for (i = 0; i < nr_ids; i++)
181 		put_pid((struct pid *)ids[i]);
182 }
183 
184 static ssize_t dbgfs_target_ids_write(struct file *file,
185 		const char __user *buf, size_t count, loff_t *ppos)
186 {
187 	struct damon_ctx *ctx = file->private_data;
188 	struct damon_target *t, *next_t;
189 	char *kbuf, *nrs;
190 	unsigned long *targets;
191 	ssize_t nr_targets;
192 	ssize_t ret = count;
193 	int i;
194 	int err;
195 
196 	kbuf = user_input_str(buf, count, ppos);
197 	if (IS_ERR(kbuf))
198 		return PTR_ERR(kbuf);
199 
200 	nrs = kbuf;
201 
202 	targets = str_to_target_ids(nrs, ret, &nr_targets);
203 	if (!targets) {
204 		ret = -ENOMEM;
205 		goto out;
206 	}
207 
208 	if (targetid_is_pid(ctx)) {
209 		for (i = 0; i < nr_targets; i++) {
210 			targets[i] = (unsigned long)find_get_pid(
211 					(int)targets[i]);
212 			if (!targets[i]) {
213 				dbgfs_put_pids(targets, i);
214 				ret = -EINVAL;
215 				goto free_targets_out;
216 			}
217 		}
218 	}
219 
220 	mutex_lock(&ctx->kdamond_lock);
221 	if (ctx->kdamond) {
222 		if (targetid_is_pid(ctx))
223 			dbgfs_put_pids(targets, nr_targets);
224 		ret = -EBUSY;
225 		goto unlock_out;
226 	}
227 
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);
233 	}
234 
235 	err = damon_set_targets(ctx, targets, nr_targets);
236 	if (err) {
237 		if (targetid_is_pid(ctx))
238 			dbgfs_put_pids(targets, nr_targets);
239 		ret = err;
240 	}
241 
242 unlock_out:
243 	mutex_unlock(&ctx->kdamond_lock);
244 free_targets_out:
245 	kfree(targets);
246 out:
247 	kfree(kbuf);
248 	return ret;
249 }
250 
251 static ssize_t dbgfs_kdamond_pid_read(struct file *file,
252 		char __user *buf, size_t count, loff_t *ppos)
253 {
254 	struct damon_ctx *ctx = file->private_data;
255 	char *kbuf;
256 	ssize_t len;
257 
258 	kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
259 	if (!kbuf)
260 		return -ENOMEM;
261 
262 	mutex_lock(&ctx->kdamond_lock);
263 	if (ctx->kdamond)
264 		len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
265 	else
266 		len = scnprintf(kbuf, count, "none\n");
267 	mutex_unlock(&ctx->kdamond_lock);
268 	if (!len)
269 		goto out;
270 	len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
271 
272 out:
273 	kfree(kbuf);
274 	return len;
275 }
276 
277 static int damon_dbgfs_open(struct inode *inode, struct file *file)
278 {
279 	file->private_data = inode->i_private;
280 
281 	return nonseekable_open(inode, file);
282 }
283 
284 static const struct file_operations attrs_fops = {
285 	.open = damon_dbgfs_open,
286 	.read = dbgfs_attrs_read,
287 	.write = dbgfs_attrs_write,
288 };
289 
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,
294 };
295 
296 static const struct file_operations kdamond_pid_fops = {
297 	.open = damon_dbgfs_open,
298 	.read = dbgfs_kdamond_pid_read,
299 };
300 
301 static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
302 {
303 	const char * const file_names[] = {"attrs", "target_ids",
304 		"kdamond_pid"};
305 	const struct file_operations *fops[] = {&attrs_fops, &target_ids_fops,
306 		&kdamond_pid_fops};
307 	int i;
308 
309 	for (i = 0; i < ARRAY_SIZE(file_names); i++)
310 		debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
311 }
312 
313 static int dbgfs_before_terminate(struct damon_ctx *ctx)
314 {
315 	struct damon_target *t, *next;
316 
317 	if (!targetid_is_pid(ctx))
318 		return 0;
319 
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);
324 	}
325 	mutex_unlock(&ctx->kdamond_lock);
326 	return 0;
327 }
328 
329 static struct damon_ctx *dbgfs_new_ctx(void)
330 {
331 	struct damon_ctx *ctx;
332 
333 	ctx = damon_new_ctx();
334 	if (!ctx)
335 		return NULL;
336 
337 	damon_va_set_primitives(ctx);
338 	ctx->callback.before_terminate = dbgfs_before_terminate;
339 	return ctx;
340 }
341 
342 static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
343 {
344 	damon_destroy_ctx(ctx);
345 }
346 
347 /*
348  * Make a context of @name and create a debugfs directory for it.
349  *
350  * This function should be called while holding damon_dbgfs_lock.
351  *
352  * Returns 0 on success, negative error code otherwise.
353  */
354 static int dbgfs_mk_context(char *name)
355 {
356 	struct dentry *root, **new_dirs, *new_dir;
357 	struct damon_ctx **new_ctxs, *new_ctx;
358 
359 	if (damon_nr_running_ctxs())
360 		return -EBUSY;
361 
362 	new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
363 			(dbgfs_nr_ctxs + 1), GFP_KERNEL);
364 	if (!new_ctxs)
365 		return -ENOMEM;
366 	dbgfs_ctxs = new_ctxs;
367 
368 	new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
369 			(dbgfs_nr_ctxs + 1), GFP_KERNEL);
370 	if (!new_dirs)
371 		return -ENOMEM;
372 	dbgfs_dirs = new_dirs;
373 
374 	root = dbgfs_dirs[0];
375 	if (!root)
376 		return -ENOENT;
377 
378 	new_dir = debugfs_create_dir(name, root);
379 	/* Below check is required for a potential duplicated name case */
380 	if (IS_ERR(new_dir))
381 		return PTR_ERR(new_dir);
382 	dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
383 
384 	new_ctx = dbgfs_new_ctx();
385 	if (!new_ctx) {
386 		debugfs_remove(new_dir);
387 		dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
388 		return -ENOMEM;
389 	}
390 
391 	dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
392 	dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
393 			dbgfs_ctxs[dbgfs_nr_ctxs]);
394 	dbgfs_nr_ctxs++;
395 
396 	return 0;
397 }
398 
399 static ssize_t dbgfs_mk_context_write(struct file *file,
400 		const char __user *buf, size_t count, loff_t *ppos)
401 {
402 	char *kbuf;
403 	char *ctx_name;
404 	ssize_t ret = count;
405 	int err;
406 
407 	kbuf = user_input_str(buf, count, ppos);
408 	if (IS_ERR(kbuf))
409 		return PTR_ERR(kbuf);
410 	ctx_name = kmalloc(count + 1, GFP_KERNEL);
411 	if (!ctx_name) {
412 		kfree(kbuf);
413 		return -ENOMEM;
414 	}
415 
416 	/* Trim white space */
417 	if (sscanf(kbuf, "%s", ctx_name) != 1) {
418 		ret = -EINVAL;
419 		goto out;
420 	}
421 
422 	mutex_lock(&damon_dbgfs_lock);
423 	err = dbgfs_mk_context(ctx_name);
424 	if (err)
425 		ret = err;
426 	mutex_unlock(&damon_dbgfs_lock);
427 
428 out:
429 	kfree(kbuf);
430 	kfree(ctx_name);
431 	return ret;
432 }
433 
434 /*
435  * Remove a context of @name and its debugfs directory.
436  *
437  * This function should be called while holding damon_dbgfs_lock.
438  *
439  * Return 0 on success, negative error code otherwise.
440  */
441 static int dbgfs_rm_context(char *name)
442 {
443 	struct dentry *root, *dir, **new_dirs;
444 	struct inode *inode;
445 	struct damon_ctx **new_ctxs;
446 	int i, j;
447 	int ret = 0;
448 
449 	if (damon_nr_running_ctxs())
450 		return -EBUSY;
451 
452 	root = dbgfs_dirs[0];
453 	if (!root)
454 		return -ENOENT;
455 
456 	dir = debugfs_lookup(name, root);
457 	if (!dir)
458 		return -ENOENT;
459 
460 	inode = d_inode(dir);
461 	if (!S_ISDIR(inode->i_mode)) {
462 		ret = -EINVAL;
463 		goto out_dput;
464 	}
465 
466 	new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
467 			GFP_KERNEL);
468 	if (!new_dirs) {
469 		ret = -ENOMEM;
470 		goto out_dput;
471 	}
472 
473 	new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
474 			GFP_KERNEL);
475 	if (!new_ctxs) {
476 		ret = -ENOMEM;
477 		goto out_new_dirs;
478 	}
479 
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]);
484 			continue;
485 		}
486 		new_dirs[j] = dbgfs_dirs[i];
487 		new_ctxs[j++] = dbgfs_ctxs[i];
488 	}
489 
490 	kfree(dbgfs_dirs);
491 	kfree(dbgfs_ctxs);
492 
493 	dbgfs_dirs = new_dirs;
494 	dbgfs_ctxs = new_ctxs;
495 	dbgfs_nr_ctxs--;
496 
497 	goto out_dput;
498 
499 out_new_dirs:
500 	kfree(new_dirs);
501 out_dput:
502 	dput(dir);
503 	return ret;
504 }
505 
506 static ssize_t dbgfs_rm_context_write(struct file *file,
507 		const char __user *buf, size_t count, loff_t *ppos)
508 {
509 	char *kbuf;
510 	ssize_t ret = count;
511 	int err;
512 	char *ctx_name;
513 
514 	kbuf = user_input_str(buf, count, ppos);
515 	if (IS_ERR(kbuf))
516 		return PTR_ERR(kbuf);
517 	ctx_name = kmalloc(count + 1, GFP_KERNEL);
518 	if (!ctx_name) {
519 		kfree(kbuf);
520 		return -ENOMEM;
521 	}
522 
523 	/* Trim white space */
524 	if (sscanf(kbuf, "%s", ctx_name) != 1) {
525 		ret = -EINVAL;
526 		goto out;
527 	}
528 
529 	mutex_lock(&damon_dbgfs_lock);
530 	err = dbgfs_rm_context(ctx_name);
531 	if (err)
532 		ret = err;
533 	mutex_unlock(&damon_dbgfs_lock);
534 
535 out:
536 	kfree(kbuf);
537 	kfree(ctx_name);
538 	return ret;
539 }
540 
541 static ssize_t dbgfs_monitor_on_read(struct file *file,
542 		char __user *buf, size_t count, loff_t *ppos)
543 {
544 	char monitor_on_buf[5];
545 	bool monitor_on = damon_nr_running_ctxs() != 0;
546 	int len;
547 
548 	len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
549 
550 	return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
551 }
552 
553 static ssize_t dbgfs_monitor_on_write(struct file *file,
554 		const char __user *buf, size_t count, loff_t *ppos)
555 {
556 	ssize_t ret = count;
557 	char *kbuf;
558 	int err;
559 
560 	kbuf = user_input_str(buf, count, ppos);
561 	if (IS_ERR(kbuf))
562 		return PTR_ERR(kbuf);
563 
564 	/* Remove white space */
565 	if (sscanf(kbuf, "%s", kbuf) != 1) {
566 		kfree(kbuf);
567 		return -EINVAL;
568 	}
569 
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);
575 	else
576 		err = -EINVAL;
577 	mutex_unlock(&damon_dbgfs_lock);
578 
579 	if (err)
580 		ret = err;
581 	kfree(kbuf);
582 	return ret;
583 }
584 
585 static const struct file_operations mk_contexts_fops = {
586 	.write = dbgfs_mk_context_write,
587 };
588 
589 static const struct file_operations rm_contexts_fops = {
590 	.write = dbgfs_rm_context_write,
591 };
592 
593 static const struct file_operations monitor_on_fops = {
594 	.read = dbgfs_monitor_on_read,
595 	.write = dbgfs_monitor_on_write,
596 };
597 
598 static int __init __damon_dbgfs_init(void)
599 {
600 	struct dentry *dbgfs_root;
601 	const char * const file_names[] = {"mk_contexts", "rm_contexts",
602 		"monitor_on"};
603 	const struct file_operations *fops[] = {&mk_contexts_fops,
604 		&rm_contexts_fops, &monitor_on_fops};
605 	int i;
606 
607 	dbgfs_root = debugfs_create_dir("damon", NULL);
608 
609 	for (i = 0; i < ARRAY_SIZE(file_names); i++)
610 		debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
611 				fops[i]);
612 	dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
613 
614 	dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
615 	if (!dbgfs_dirs) {
616 		debugfs_remove(dbgfs_root);
617 		return -ENOMEM;
618 	}
619 	dbgfs_dirs[0] = dbgfs_root;
620 
621 	return 0;
622 }
623 
624 /*
625  * Functions for the initialization
626  */
627 
628 static int __init damon_dbgfs_init(void)
629 {
630 	int rc = -ENOMEM;
631 
632 	mutex_lock(&damon_dbgfs_lock);
633 	dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
634 	if (!dbgfs_ctxs)
635 		goto out;
636 	dbgfs_ctxs[0] = dbgfs_new_ctx();
637 	if (!dbgfs_ctxs[0]) {
638 		kfree(dbgfs_ctxs);
639 		goto out;
640 	}
641 	dbgfs_nr_ctxs = 1;
642 
643 	rc = __damon_dbgfs_init();
644 	if (rc) {
645 		kfree(dbgfs_ctxs[0]);
646 		kfree(dbgfs_ctxs);
647 		pr_err("%s: dbgfs init failed\n", __func__);
648 	}
649 
650 out:
651 	mutex_unlock(&damon_dbgfs_lock);
652 	return rc;
653 }
654 
655 module_init(damon_dbgfs_init);
656 
657 #include "dbgfs-test.h"
658