xref: /openbmc/linux/mm/damon/dbgfs.c (revision 3a619fdb)
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;
73 
74 	kbuf = user_input_str(buf, count, ppos);
75 	if (IS_ERR(kbuf))
76 		return PTR_ERR(kbuf);
77 
78 	if (sscanf(kbuf, "%lu %lu %lu %lu %lu",
79 				&s, &a, &r, &minr, &maxr) != 5) {
80 		ret = -EINVAL;
81 		goto out;
82 	}
83 
84 	mutex_lock(&ctx->kdamond_lock);
85 	if (ctx->kdamond) {
86 		ret = -EBUSY;
87 		goto unlock_out;
88 	}
89 
90 	ret = damon_set_attrs(ctx, s, a, r, minr, maxr);
91 	if (!ret)
92 		ret = count;
93 unlock_out:
94 	mutex_unlock(&ctx->kdamond_lock);
95 out:
96 	kfree(kbuf);
97 	return ret;
98 }
99 
100 static ssize_t sprint_schemes(struct damon_ctx *c, char *buf, ssize_t len)
101 {
102 	struct damos *s;
103 	int written = 0;
104 	int rc;
105 
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 %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,
112 				s->action,
113 				s->quota.ms, s->quota.sz,
114 				s->quota.reset_interval,
115 				s->quota.weight_sz,
116 				s->quota.weight_nr_accesses,
117 				s->quota.weight_age,
118 				s->wmarks.metric, s->wmarks.interval,
119 				s->wmarks.high, s->wmarks.mid, s->wmarks.low,
120 				s->stat.nr_tried, s->stat.sz_tried,
121 				s->stat.nr_applied, s->stat.sz_applied,
122 				s->stat.qt_exceeds);
123 		if (!rc)
124 			return -ENOMEM;
125 
126 		written += rc;
127 	}
128 	return written;
129 }
130 
131 static ssize_t dbgfs_schemes_read(struct file *file, char __user *buf,
132 		size_t count, loff_t *ppos)
133 {
134 	struct damon_ctx *ctx = file->private_data;
135 	char *kbuf;
136 	ssize_t len;
137 
138 	kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
139 	if (!kbuf)
140 		return -ENOMEM;
141 
142 	mutex_lock(&ctx->kdamond_lock);
143 	len = sprint_schemes(ctx, kbuf, count);
144 	mutex_unlock(&ctx->kdamond_lock);
145 	if (len < 0)
146 		goto out;
147 	len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
148 
149 out:
150 	kfree(kbuf);
151 	return len;
152 }
153 
154 static void free_schemes_arr(struct damos **schemes, ssize_t nr_schemes)
155 {
156 	ssize_t i;
157 
158 	for (i = 0; i < nr_schemes; i++)
159 		kfree(schemes[i]);
160 	kfree(schemes);
161 }
162 
163 static bool damos_action_valid(int action)
164 {
165 	switch (action) {
166 	case DAMOS_WILLNEED:
167 	case DAMOS_COLD:
168 	case DAMOS_PAGEOUT:
169 	case DAMOS_HUGEPAGE:
170 	case DAMOS_NOHUGEPAGE:
171 	case DAMOS_STAT:
172 		return true;
173 	default:
174 		return false;
175 	}
176 }
177 
178 /*
179  * Converts a string into an array of struct damos pointers
180  *
181  * Returns an array of struct damos pointers that converted if the conversion
182  * success, or NULL otherwise.
183  */
184 static struct damos **str_to_schemes(const char *str, ssize_t len,
185 				ssize_t *nr_schemes)
186 {
187 	struct damos *scheme, **schemes;
188 	const int max_nr_schemes = 256;
189 	int pos = 0, parsed, ret;
190 	unsigned long min_sz, max_sz;
191 	unsigned int min_nr_a, max_nr_a, min_age, max_age;
192 	unsigned int action;
193 
194 	schemes = kmalloc_array(max_nr_schemes, sizeof(scheme),
195 			GFP_KERNEL);
196 	if (!schemes)
197 		return NULL;
198 
199 	*nr_schemes = 0;
200 	while (pos < len && *nr_schemes < max_nr_schemes) {
201 		struct damos_quota quota = {};
202 		struct damos_watermarks wmarks;
203 
204 		ret = sscanf(&str[pos],
205 				"%lu %lu %u %u %u %u %u %lu %lu %lu %u %u %u %u %lu %lu %lu %lu%n",
206 				&min_sz, &max_sz, &min_nr_a, &max_nr_a,
207 				&min_age, &max_age, &action, &quota.ms,
208 				&quota.sz, &quota.reset_interval,
209 				&quota.weight_sz, &quota.weight_nr_accesses,
210 				&quota.weight_age, &wmarks.metric,
211 				&wmarks.interval, &wmarks.high, &wmarks.mid,
212 				&wmarks.low, &parsed);
213 		if (ret != 18)
214 			break;
215 		if (!damos_action_valid(action))
216 			goto fail;
217 
218 		if (min_sz > max_sz || min_nr_a > max_nr_a || min_age > max_age)
219 			goto fail;
220 
221 		if (wmarks.high < wmarks.mid || wmarks.high < wmarks.low ||
222 		    wmarks.mid <  wmarks.low)
223 			goto fail;
224 
225 		pos += parsed;
226 		scheme = damon_new_scheme(min_sz, max_sz, min_nr_a, max_nr_a,
227 				min_age, max_age, action, &quota, &wmarks);
228 		if (!scheme)
229 			goto fail;
230 
231 		schemes[*nr_schemes] = scheme;
232 		*nr_schemes += 1;
233 	}
234 	return schemes;
235 fail:
236 	free_schemes_arr(schemes, *nr_schemes);
237 	return NULL;
238 }
239 
240 static ssize_t dbgfs_schemes_write(struct file *file, const char __user *buf,
241 		size_t count, loff_t *ppos)
242 {
243 	struct damon_ctx *ctx = file->private_data;
244 	char *kbuf;
245 	struct damos **schemes;
246 	ssize_t nr_schemes = 0, ret;
247 
248 	kbuf = user_input_str(buf, count, ppos);
249 	if (IS_ERR(kbuf))
250 		return PTR_ERR(kbuf);
251 
252 	schemes = str_to_schemes(kbuf, count, &nr_schemes);
253 	if (!schemes) {
254 		ret = -EINVAL;
255 		goto out;
256 	}
257 
258 	mutex_lock(&ctx->kdamond_lock);
259 	if (ctx->kdamond) {
260 		ret = -EBUSY;
261 		goto unlock_out;
262 	}
263 
264 	ret = damon_set_schemes(ctx, schemes, nr_schemes);
265 	if (!ret) {
266 		ret = count;
267 		nr_schemes = 0;
268 	}
269 
270 unlock_out:
271 	mutex_unlock(&ctx->kdamond_lock);
272 	free_schemes_arr(schemes, nr_schemes);
273 out:
274 	kfree(kbuf);
275 	return ret;
276 }
277 
278 static inline bool targetid_is_pid(const struct damon_ctx *ctx)
279 {
280 	return ctx->primitive.target_valid == damon_va_target_valid;
281 }
282 
283 static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len)
284 {
285 	struct damon_target *t;
286 	unsigned long id;
287 	int written = 0;
288 	int rc;
289 
290 	damon_for_each_target(t, ctx) {
291 		id = t->id;
292 		if (targetid_is_pid(ctx))
293 			/* Show pid numbers to debugfs users */
294 			id = (unsigned long)pid_vnr((struct pid *)id);
295 
296 		rc = scnprintf(&buf[written], len - written, "%lu ", id);
297 		if (!rc)
298 			return -ENOMEM;
299 		written += rc;
300 	}
301 	if (written)
302 		written -= 1;
303 	written += scnprintf(&buf[written], len - written, "\n");
304 	return written;
305 }
306 
307 static ssize_t dbgfs_target_ids_read(struct file *file,
308 		char __user *buf, size_t count, loff_t *ppos)
309 {
310 	struct damon_ctx *ctx = file->private_data;
311 	ssize_t len;
312 	char ids_buf[320];
313 
314 	mutex_lock(&ctx->kdamond_lock);
315 	len = sprint_target_ids(ctx, ids_buf, 320);
316 	mutex_unlock(&ctx->kdamond_lock);
317 	if (len < 0)
318 		return len;
319 
320 	return simple_read_from_buffer(buf, count, ppos, ids_buf, len);
321 }
322 
323 /*
324  * Converts a string into an array of unsigned long integers
325  *
326  * Returns an array of unsigned long integers if the conversion success, or
327  * NULL otherwise.
328  */
329 static unsigned long *str_to_target_ids(const char *str, ssize_t len,
330 					ssize_t *nr_ids)
331 {
332 	unsigned long *ids;
333 	const int max_nr_ids = 32;
334 	unsigned long id;
335 	int pos = 0, parsed, ret;
336 
337 	*nr_ids = 0;
338 	ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL);
339 	if (!ids)
340 		return NULL;
341 	while (*nr_ids < max_nr_ids && pos < len) {
342 		ret = sscanf(&str[pos], "%lu%n", &id, &parsed);
343 		pos += parsed;
344 		if (ret != 1)
345 			break;
346 		ids[*nr_ids] = id;
347 		*nr_ids += 1;
348 	}
349 
350 	return ids;
351 }
352 
353 static void dbgfs_put_pids(unsigned long *ids, int nr_ids)
354 {
355 	int i;
356 
357 	for (i = 0; i < nr_ids; i++)
358 		put_pid((struct pid *)ids[i]);
359 }
360 
361 static ssize_t dbgfs_target_ids_write(struct file *file,
362 		const char __user *buf, size_t count, loff_t *ppos)
363 {
364 	struct damon_ctx *ctx = file->private_data;
365 	struct damon_target *t, *next_t;
366 	bool id_is_pid = true;
367 	char *kbuf, *nrs;
368 	unsigned long *targets;
369 	ssize_t nr_targets;
370 	ssize_t ret;
371 	int i;
372 
373 	kbuf = user_input_str(buf, count, ppos);
374 	if (IS_ERR(kbuf))
375 		return PTR_ERR(kbuf);
376 
377 	nrs = kbuf;
378 	if (!strncmp(kbuf, "paddr\n", count)) {
379 		id_is_pid = false;
380 		/* target id is meaningless here, but we set it just for fun */
381 		scnprintf(kbuf, count, "42    ");
382 	}
383 
384 	targets = str_to_target_ids(nrs, count, &nr_targets);
385 	if (!targets) {
386 		ret = -ENOMEM;
387 		goto out;
388 	}
389 
390 	if (id_is_pid) {
391 		for (i = 0; i < nr_targets; i++) {
392 			targets[i] = (unsigned long)find_get_pid(
393 					(int)targets[i]);
394 			if (!targets[i]) {
395 				dbgfs_put_pids(targets, i);
396 				ret = -EINVAL;
397 				goto free_targets_out;
398 			}
399 		}
400 	}
401 
402 	mutex_lock(&ctx->kdamond_lock);
403 	if (ctx->kdamond) {
404 		if (id_is_pid)
405 			dbgfs_put_pids(targets, nr_targets);
406 		ret = -EBUSY;
407 		goto unlock_out;
408 	}
409 
410 	/* remove previously set targets */
411 	damon_for_each_target_safe(t, next_t, ctx) {
412 		if (targetid_is_pid(ctx))
413 			put_pid((struct pid *)t->id);
414 		damon_destroy_target(t);
415 	}
416 
417 	/* Configure the context for the address space type */
418 	if (id_is_pid)
419 		damon_va_set_primitives(ctx);
420 	else
421 		damon_pa_set_primitives(ctx);
422 
423 	ret = damon_set_targets(ctx, targets, nr_targets);
424 	if (ret) {
425 		if (id_is_pid)
426 			dbgfs_put_pids(targets, nr_targets);
427 	} else {
428 		ret = count;
429 	}
430 
431 unlock_out:
432 	mutex_unlock(&ctx->kdamond_lock);
433 free_targets_out:
434 	kfree(targets);
435 out:
436 	kfree(kbuf);
437 	return ret;
438 }
439 
440 static ssize_t sprint_init_regions(struct damon_ctx *c, char *buf, ssize_t len)
441 {
442 	struct damon_target *t;
443 	struct damon_region *r;
444 	int written = 0;
445 	int rc;
446 
447 	damon_for_each_target(t, c) {
448 		damon_for_each_region(r, t) {
449 			rc = scnprintf(&buf[written], len - written,
450 					"%lu %lu %lu\n",
451 					t->id, r->ar.start, r->ar.end);
452 			if (!rc)
453 				return -ENOMEM;
454 			written += rc;
455 		}
456 	}
457 	return written;
458 }
459 
460 static ssize_t dbgfs_init_regions_read(struct file *file, char __user *buf,
461 		size_t count, loff_t *ppos)
462 {
463 	struct damon_ctx *ctx = file->private_data;
464 	char *kbuf;
465 	ssize_t len;
466 
467 	kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
468 	if (!kbuf)
469 		return -ENOMEM;
470 
471 	mutex_lock(&ctx->kdamond_lock);
472 	if (ctx->kdamond) {
473 		mutex_unlock(&ctx->kdamond_lock);
474 		len = -EBUSY;
475 		goto out;
476 	}
477 
478 	len = sprint_init_regions(ctx, kbuf, count);
479 	mutex_unlock(&ctx->kdamond_lock);
480 	if (len < 0)
481 		goto out;
482 	len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
483 
484 out:
485 	kfree(kbuf);
486 	return len;
487 }
488 
489 static int add_init_region(struct damon_ctx *c,
490 			 unsigned long target_id, struct damon_addr_range *ar)
491 {
492 	struct damon_target *t;
493 	struct damon_region *r, *prev;
494 	unsigned long id;
495 	int rc = -EINVAL;
496 
497 	if (ar->start >= ar->end)
498 		return -EINVAL;
499 
500 	damon_for_each_target(t, c) {
501 		id = t->id;
502 		if (targetid_is_pid(c))
503 			id = (unsigned long)pid_vnr((struct pid *)id);
504 		if (id == target_id) {
505 			r = damon_new_region(ar->start, ar->end);
506 			if (!r)
507 				return -ENOMEM;
508 			damon_add_region(r, t);
509 			if (damon_nr_regions(t) > 1) {
510 				prev = damon_prev_region(r);
511 				if (prev->ar.end > r->ar.start) {
512 					damon_destroy_region(r, t);
513 					return -EINVAL;
514 				}
515 			}
516 			rc = 0;
517 		}
518 	}
519 	return rc;
520 }
521 
522 static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len)
523 {
524 	struct damon_target *t;
525 	struct damon_region *r, *next;
526 	int pos = 0, parsed, ret;
527 	unsigned long target_id;
528 	struct damon_addr_range ar;
529 	int err;
530 
531 	damon_for_each_target(t, c) {
532 		damon_for_each_region_safe(r, next, t)
533 			damon_destroy_region(r, t);
534 	}
535 
536 	while (pos < len) {
537 		ret = sscanf(&str[pos], "%lu %lu %lu%n",
538 				&target_id, &ar.start, &ar.end, &parsed);
539 		if (ret != 3)
540 			break;
541 		err = add_init_region(c, target_id, &ar);
542 		if (err)
543 			goto fail;
544 		pos += parsed;
545 	}
546 
547 	return 0;
548 
549 fail:
550 	damon_for_each_target(t, c) {
551 		damon_for_each_region_safe(r, next, t)
552 			damon_destroy_region(r, t);
553 	}
554 	return err;
555 }
556 
557 static ssize_t dbgfs_init_regions_write(struct file *file,
558 					  const char __user *buf, size_t count,
559 					  loff_t *ppos)
560 {
561 	struct damon_ctx *ctx = file->private_data;
562 	char *kbuf;
563 	ssize_t ret = count;
564 	int err;
565 
566 	kbuf = user_input_str(buf, count, ppos);
567 	if (IS_ERR(kbuf))
568 		return PTR_ERR(kbuf);
569 
570 	mutex_lock(&ctx->kdamond_lock);
571 	if (ctx->kdamond) {
572 		ret = -EBUSY;
573 		goto unlock_out;
574 	}
575 
576 	err = set_init_regions(ctx, kbuf, ret);
577 	if (err)
578 		ret = err;
579 
580 unlock_out:
581 	mutex_unlock(&ctx->kdamond_lock);
582 	kfree(kbuf);
583 	return ret;
584 }
585 
586 static ssize_t dbgfs_kdamond_pid_read(struct file *file,
587 		char __user *buf, size_t count, loff_t *ppos)
588 {
589 	struct damon_ctx *ctx = file->private_data;
590 	char *kbuf;
591 	ssize_t len;
592 
593 	kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
594 	if (!kbuf)
595 		return -ENOMEM;
596 
597 	mutex_lock(&ctx->kdamond_lock);
598 	if (ctx->kdamond)
599 		len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
600 	else
601 		len = scnprintf(kbuf, count, "none\n");
602 	mutex_unlock(&ctx->kdamond_lock);
603 	if (!len)
604 		goto out;
605 	len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
606 
607 out:
608 	kfree(kbuf);
609 	return len;
610 }
611 
612 static int damon_dbgfs_open(struct inode *inode, struct file *file)
613 {
614 	file->private_data = inode->i_private;
615 
616 	return nonseekable_open(inode, file);
617 }
618 
619 static const struct file_operations attrs_fops = {
620 	.open = damon_dbgfs_open,
621 	.read = dbgfs_attrs_read,
622 	.write = dbgfs_attrs_write,
623 };
624 
625 static const struct file_operations schemes_fops = {
626 	.open = damon_dbgfs_open,
627 	.read = dbgfs_schemes_read,
628 	.write = dbgfs_schemes_write,
629 };
630 
631 static const struct file_operations target_ids_fops = {
632 	.open = damon_dbgfs_open,
633 	.read = dbgfs_target_ids_read,
634 	.write = dbgfs_target_ids_write,
635 };
636 
637 static const struct file_operations init_regions_fops = {
638 	.open = damon_dbgfs_open,
639 	.read = dbgfs_init_regions_read,
640 	.write = dbgfs_init_regions_write,
641 };
642 
643 static const struct file_operations kdamond_pid_fops = {
644 	.open = damon_dbgfs_open,
645 	.read = dbgfs_kdamond_pid_read,
646 };
647 
648 static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
649 {
650 	const char * const file_names[] = {"attrs", "schemes", "target_ids",
651 		"init_regions", "kdamond_pid"};
652 	const struct file_operations *fops[] = {&attrs_fops, &schemes_fops,
653 		&target_ids_fops, &init_regions_fops, &kdamond_pid_fops};
654 	int i;
655 
656 	for (i = 0; i < ARRAY_SIZE(file_names); i++)
657 		debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
658 }
659 
660 static void dbgfs_before_terminate(struct damon_ctx *ctx)
661 {
662 	struct damon_target *t, *next;
663 
664 	if (!targetid_is_pid(ctx))
665 		return;
666 
667 	mutex_lock(&ctx->kdamond_lock);
668 	damon_for_each_target_safe(t, next, ctx) {
669 		put_pid((struct pid *)t->id);
670 		damon_destroy_target(t);
671 	}
672 	mutex_unlock(&ctx->kdamond_lock);
673 }
674 
675 static struct damon_ctx *dbgfs_new_ctx(void)
676 {
677 	struct damon_ctx *ctx;
678 
679 	ctx = damon_new_ctx();
680 	if (!ctx)
681 		return NULL;
682 
683 	damon_va_set_primitives(ctx);
684 	ctx->callback.before_terminate = dbgfs_before_terminate;
685 	return ctx;
686 }
687 
688 static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
689 {
690 	damon_destroy_ctx(ctx);
691 }
692 
693 /*
694  * Make a context of @name and create a debugfs directory for it.
695  *
696  * This function should be called while holding damon_dbgfs_lock.
697  *
698  * Returns 0 on success, negative error code otherwise.
699  */
700 static int dbgfs_mk_context(char *name)
701 {
702 	struct dentry *root, **new_dirs, *new_dir;
703 	struct damon_ctx **new_ctxs, *new_ctx;
704 
705 	if (damon_nr_running_ctxs())
706 		return -EBUSY;
707 
708 	new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
709 			(dbgfs_nr_ctxs + 1), GFP_KERNEL);
710 	if (!new_ctxs)
711 		return -ENOMEM;
712 	dbgfs_ctxs = new_ctxs;
713 
714 	new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
715 			(dbgfs_nr_ctxs + 1), GFP_KERNEL);
716 	if (!new_dirs)
717 		return -ENOMEM;
718 	dbgfs_dirs = new_dirs;
719 
720 	root = dbgfs_dirs[0];
721 	if (!root)
722 		return -ENOENT;
723 
724 	new_dir = debugfs_create_dir(name, root);
725 	dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
726 
727 	new_ctx = dbgfs_new_ctx();
728 	if (!new_ctx) {
729 		debugfs_remove(new_dir);
730 		dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
731 		return -ENOMEM;
732 	}
733 
734 	dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
735 	dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
736 			dbgfs_ctxs[dbgfs_nr_ctxs]);
737 	dbgfs_nr_ctxs++;
738 
739 	return 0;
740 }
741 
742 static ssize_t dbgfs_mk_context_write(struct file *file,
743 		const char __user *buf, size_t count, loff_t *ppos)
744 {
745 	char *kbuf;
746 	char *ctx_name;
747 	ssize_t ret;
748 
749 	kbuf = user_input_str(buf, count, ppos);
750 	if (IS_ERR(kbuf))
751 		return PTR_ERR(kbuf);
752 	ctx_name = kmalloc(count + 1, GFP_KERNEL);
753 	if (!ctx_name) {
754 		kfree(kbuf);
755 		return -ENOMEM;
756 	}
757 
758 	/* Trim white space */
759 	if (sscanf(kbuf, "%s", ctx_name) != 1) {
760 		ret = -EINVAL;
761 		goto out;
762 	}
763 
764 	mutex_lock(&damon_dbgfs_lock);
765 	ret = dbgfs_mk_context(ctx_name);
766 	if (!ret)
767 		ret = count;
768 	mutex_unlock(&damon_dbgfs_lock);
769 
770 out:
771 	kfree(kbuf);
772 	kfree(ctx_name);
773 	return ret;
774 }
775 
776 /*
777  * Remove a context of @name and its debugfs directory.
778  *
779  * This function should be called while holding damon_dbgfs_lock.
780  *
781  * Return 0 on success, negative error code otherwise.
782  */
783 static int dbgfs_rm_context(char *name)
784 {
785 	struct dentry *root, *dir, **new_dirs;
786 	struct damon_ctx **new_ctxs;
787 	int i, j;
788 
789 	if (damon_nr_running_ctxs())
790 		return -EBUSY;
791 
792 	root = dbgfs_dirs[0];
793 	if (!root)
794 		return -ENOENT;
795 
796 	dir = debugfs_lookup(name, root);
797 	if (!dir)
798 		return -ENOENT;
799 
800 	new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
801 			GFP_KERNEL);
802 	if (!new_dirs)
803 		return -ENOMEM;
804 
805 	new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
806 			GFP_KERNEL);
807 	if (!new_ctxs) {
808 		kfree(new_dirs);
809 		return -ENOMEM;
810 	}
811 
812 	for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
813 		if (dbgfs_dirs[i] == dir) {
814 			debugfs_remove(dbgfs_dirs[i]);
815 			dbgfs_destroy_ctx(dbgfs_ctxs[i]);
816 			continue;
817 		}
818 		new_dirs[j] = dbgfs_dirs[i];
819 		new_ctxs[j++] = dbgfs_ctxs[i];
820 	}
821 
822 	kfree(dbgfs_dirs);
823 	kfree(dbgfs_ctxs);
824 
825 	dbgfs_dirs = new_dirs;
826 	dbgfs_ctxs = new_ctxs;
827 	dbgfs_nr_ctxs--;
828 
829 	return 0;
830 }
831 
832 static ssize_t dbgfs_rm_context_write(struct file *file,
833 		const char __user *buf, size_t count, loff_t *ppos)
834 {
835 	char *kbuf;
836 	ssize_t ret;
837 	char *ctx_name;
838 
839 	kbuf = user_input_str(buf, count, ppos);
840 	if (IS_ERR(kbuf))
841 		return PTR_ERR(kbuf);
842 	ctx_name = kmalloc(count + 1, GFP_KERNEL);
843 	if (!ctx_name) {
844 		kfree(kbuf);
845 		return -ENOMEM;
846 	}
847 
848 	/* Trim white space */
849 	if (sscanf(kbuf, "%s", ctx_name) != 1) {
850 		ret = -EINVAL;
851 		goto out;
852 	}
853 
854 	mutex_lock(&damon_dbgfs_lock);
855 	ret = dbgfs_rm_context(ctx_name);
856 	if (!ret)
857 		ret = count;
858 	mutex_unlock(&damon_dbgfs_lock);
859 
860 out:
861 	kfree(kbuf);
862 	kfree(ctx_name);
863 	return ret;
864 }
865 
866 static ssize_t dbgfs_monitor_on_read(struct file *file,
867 		char __user *buf, size_t count, loff_t *ppos)
868 {
869 	char monitor_on_buf[5];
870 	bool monitor_on = damon_nr_running_ctxs() != 0;
871 	int len;
872 
873 	len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
874 
875 	return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
876 }
877 
878 static ssize_t dbgfs_monitor_on_write(struct file *file,
879 		const char __user *buf, size_t count, loff_t *ppos)
880 {
881 	ssize_t ret;
882 	char *kbuf;
883 
884 	kbuf = user_input_str(buf, count, ppos);
885 	if (IS_ERR(kbuf))
886 		return PTR_ERR(kbuf);
887 
888 	/* Remove white space */
889 	if (sscanf(kbuf, "%s", kbuf) != 1) {
890 		kfree(kbuf);
891 		return -EINVAL;
892 	}
893 
894 	mutex_lock(&damon_dbgfs_lock);
895 	if (!strncmp(kbuf, "on", count)) {
896 		int i;
897 
898 		for (i = 0; i < dbgfs_nr_ctxs; i++) {
899 			if (damon_targets_empty(dbgfs_ctxs[i])) {
900 				kfree(kbuf);
901 				mutex_unlock(&damon_dbgfs_lock);
902 				return -EINVAL;
903 			}
904 		}
905 		ret = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs);
906 	} else if (!strncmp(kbuf, "off", count)) {
907 		ret = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
908 	} else {
909 		ret = -EINVAL;
910 	}
911 	mutex_unlock(&damon_dbgfs_lock);
912 
913 	if (!ret)
914 		ret = count;
915 	kfree(kbuf);
916 	return ret;
917 }
918 
919 static const struct file_operations mk_contexts_fops = {
920 	.write = dbgfs_mk_context_write,
921 };
922 
923 static const struct file_operations rm_contexts_fops = {
924 	.write = dbgfs_rm_context_write,
925 };
926 
927 static const struct file_operations monitor_on_fops = {
928 	.read = dbgfs_monitor_on_read,
929 	.write = dbgfs_monitor_on_write,
930 };
931 
932 static int __init __damon_dbgfs_init(void)
933 {
934 	struct dentry *dbgfs_root;
935 	const char * const file_names[] = {"mk_contexts", "rm_contexts",
936 		"monitor_on"};
937 	const struct file_operations *fops[] = {&mk_contexts_fops,
938 		&rm_contexts_fops, &monitor_on_fops};
939 	int i;
940 
941 	dbgfs_root = debugfs_create_dir("damon", NULL);
942 
943 	for (i = 0; i < ARRAY_SIZE(file_names); i++)
944 		debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
945 				fops[i]);
946 	dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
947 
948 	dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
949 	if (!dbgfs_dirs) {
950 		debugfs_remove(dbgfs_root);
951 		return -ENOMEM;
952 	}
953 	dbgfs_dirs[0] = dbgfs_root;
954 
955 	return 0;
956 }
957 
958 /*
959  * Functions for the initialization
960  */
961 
962 static int __init damon_dbgfs_init(void)
963 {
964 	int rc = -ENOMEM;
965 
966 	mutex_lock(&damon_dbgfs_lock);
967 	dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
968 	if (!dbgfs_ctxs)
969 		goto out;
970 	dbgfs_ctxs[0] = dbgfs_new_ctx();
971 	if (!dbgfs_ctxs[0]) {
972 		kfree(dbgfs_ctxs);
973 		goto out;
974 	}
975 	dbgfs_nr_ctxs = 1;
976 
977 	rc = __damon_dbgfs_init();
978 	if (rc) {
979 		kfree(dbgfs_ctxs[0]);
980 		kfree(dbgfs_ctxs);
981 		pr_err("%s: dbgfs init failed\n", __func__);
982 	}
983 
984 out:
985 	mutex_unlock(&damon_dbgfs_lock);
986 	return rc;
987 }
988 
989 module_init(damon_dbgfs_init);
990 
991 #include "dbgfs-test.h"
992