xref: /openbmc/linux/mm/damon/dbgfs.c (revision af122dd8)
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);
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 ssize_t sprint_schemes(struct damon_ctx *c, char *buf, ssize_t len)
102 {
103 	struct damos *s;
104 	int written = 0;
105 	int rc;
106 
107 	damon_for_each_scheme(s, c) {
108 		rc = scnprintf(&buf[written], len - written,
109 				"%lu %lu %u %u %u %u %d\n",
110 				s->min_sz_region, s->max_sz_region,
111 				s->min_nr_accesses, s->max_nr_accesses,
112 				s->min_age_region, s->max_age_region,
113 				s->action);
114 		if (!rc)
115 			return -ENOMEM;
116 
117 		written += rc;
118 	}
119 	return written;
120 }
121 
122 static ssize_t dbgfs_schemes_read(struct file *file, char __user *buf,
123 		size_t count, loff_t *ppos)
124 {
125 	struct damon_ctx *ctx = file->private_data;
126 	char *kbuf;
127 	ssize_t len;
128 
129 	kbuf = kmalloc(count, GFP_KERNEL);
130 	if (!kbuf)
131 		return -ENOMEM;
132 
133 	mutex_lock(&ctx->kdamond_lock);
134 	len = sprint_schemes(ctx, kbuf, count);
135 	mutex_unlock(&ctx->kdamond_lock);
136 	if (len < 0)
137 		goto out;
138 	len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
139 
140 out:
141 	kfree(kbuf);
142 	return len;
143 }
144 
145 static void free_schemes_arr(struct damos **schemes, ssize_t nr_schemes)
146 {
147 	ssize_t i;
148 
149 	for (i = 0; i < nr_schemes; i++)
150 		kfree(schemes[i]);
151 	kfree(schemes);
152 }
153 
154 static bool damos_action_valid(int action)
155 {
156 	switch (action) {
157 	case DAMOS_WILLNEED:
158 	case DAMOS_COLD:
159 	case DAMOS_PAGEOUT:
160 	case DAMOS_HUGEPAGE:
161 	case DAMOS_NOHUGEPAGE:
162 		return true;
163 	default:
164 		return false;
165 	}
166 }
167 
168 /*
169  * Converts a string into an array of struct damos pointers
170  *
171  * Returns an array of struct damos pointers that converted if the conversion
172  * success, or NULL otherwise.
173  */
174 static struct damos **str_to_schemes(const char *str, ssize_t len,
175 				ssize_t *nr_schemes)
176 {
177 	struct damos *scheme, **schemes;
178 	const int max_nr_schemes = 256;
179 	int pos = 0, parsed, ret;
180 	unsigned long min_sz, max_sz;
181 	unsigned int min_nr_a, max_nr_a, min_age, max_age;
182 	unsigned int action;
183 
184 	schemes = kmalloc_array(max_nr_schemes, sizeof(scheme),
185 			GFP_KERNEL);
186 	if (!schemes)
187 		return NULL;
188 
189 	*nr_schemes = 0;
190 	while (pos < len && *nr_schemes < max_nr_schemes) {
191 		ret = sscanf(&str[pos], "%lu %lu %u %u %u %u %u%n",
192 				&min_sz, &max_sz, &min_nr_a, &max_nr_a,
193 				&min_age, &max_age, &action, &parsed);
194 		if (ret != 7)
195 			break;
196 		if (!damos_action_valid(action)) {
197 			pr_err("wrong action %d\n", action);
198 			goto fail;
199 		}
200 
201 		pos += parsed;
202 		scheme = damon_new_scheme(min_sz, max_sz, min_nr_a, max_nr_a,
203 				min_age, max_age, action);
204 		if (!scheme)
205 			goto fail;
206 
207 		schemes[*nr_schemes] = scheme;
208 		*nr_schemes += 1;
209 	}
210 	return schemes;
211 fail:
212 	free_schemes_arr(schemes, *nr_schemes);
213 	return NULL;
214 }
215 
216 static ssize_t dbgfs_schemes_write(struct file *file, const char __user *buf,
217 		size_t count, loff_t *ppos)
218 {
219 	struct damon_ctx *ctx = file->private_data;
220 	char *kbuf;
221 	struct damos **schemes;
222 	ssize_t nr_schemes = 0, ret = count;
223 	int err;
224 
225 	kbuf = user_input_str(buf, count, ppos);
226 	if (IS_ERR(kbuf))
227 		return PTR_ERR(kbuf);
228 
229 	schemes = str_to_schemes(kbuf, ret, &nr_schemes);
230 	if (!schemes) {
231 		ret = -EINVAL;
232 		goto out;
233 	}
234 
235 	mutex_lock(&ctx->kdamond_lock);
236 	if (ctx->kdamond) {
237 		ret = -EBUSY;
238 		goto unlock_out;
239 	}
240 
241 	err = damon_set_schemes(ctx, schemes, nr_schemes);
242 	if (err)
243 		ret = err;
244 	else
245 		nr_schemes = 0;
246 unlock_out:
247 	mutex_unlock(&ctx->kdamond_lock);
248 	free_schemes_arr(schemes, nr_schemes);
249 out:
250 	kfree(kbuf);
251 	return ret;
252 }
253 
254 static inline bool targetid_is_pid(const struct damon_ctx *ctx)
255 {
256 	return ctx->primitive.target_valid == damon_va_target_valid;
257 }
258 
259 static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len)
260 {
261 	struct damon_target *t;
262 	unsigned long id;
263 	int written = 0;
264 	int rc;
265 
266 	damon_for_each_target(t, ctx) {
267 		id = t->id;
268 		if (targetid_is_pid(ctx))
269 			/* Show pid numbers to debugfs users */
270 			id = (unsigned long)pid_vnr((struct pid *)id);
271 
272 		rc = scnprintf(&buf[written], len - written, "%lu ", id);
273 		if (!rc)
274 			return -ENOMEM;
275 		written += rc;
276 	}
277 	if (written)
278 		written -= 1;
279 	written += scnprintf(&buf[written], len - written, "\n");
280 	return written;
281 }
282 
283 static ssize_t dbgfs_target_ids_read(struct file *file,
284 		char __user *buf, size_t count, loff_t *ppos)
285 {
286 	struct damon_ctx *ctx = file->private_data;
287 	ssize_t len;
288 	char ids_buf[320];
289 
290 	mutex_lock(&ctx->kdamond_lock);
291 	len = sprint_target_ids(ctx, ids_buf, 320);
292 	mutex_unlock(&ctx->kdamond_lock);
293 	if (len < 0)
294 		return len;
295 
296 	return simple_read_from_buffer(buf, count, ppos, ids_buf, len);
297 }
298 
299 /*
300  * Converts a string into an array of unsigned long integers
301  *
302  * Returns an array of unsigned long integers if the conversion success, or
303  * NULL otherwise.
304  */
305 static unsigned long *str_to_target_ids(const char *str, ssize_t len,
306 					ssize_t *nr_ids)
307 {
308 	unsigned long *ids;
309 	const int max_nr_ids = 32;
310 	unsigned long id;
311 	int pos = 0, parsed, ret;
312 
313 	*nr_ids = 0;
314 	ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL);
315 	if (!ids)
316 		return NULL;
317 	while (*nr_ids < max_nr_ids && pos < len) {
318 		ret = sscanf(&str[pos], "%lu%n", &id, &parsed);
319 		pos += parsed;
320 		if (ret != 1)
321 			break;
322 		ids[*nr_ids] = id;
323 		*nr_ids += 1;
324 	}
325 
326 	return ids;
327 }
328 
329 static void dbgfs_put_pids(unsigned long *ids, int nr_ids)
330 {
331 	int i;
332 
333 	for (i = 0; i < nr_ids; i++)
334 		put_pid((struct pid *)ids[i]);
335 }
336 
337 static ssize_t dbgfs_target_ids_write(struct file *file,
338 		const char __user *buf, size_t count, loff_t *ppos)
339 {
340 	struct damon_ctx *ctx = file->private_data;
341 	char *kbuf, *nrs;
342 	unsigned long *targets;
343 	ssize_t nr_targets;
344 	ssize_t ret = count;
345 	int i;
346 	int err;
347 
348 	kbuf = user_input_str(buf, count, ppos);
349 	if (IS_ERR(kbuf))
350 		return PTR_ERR(kbuf);
351 
352 	nrs = kbuf;
353 
354 	targets = str_to_target_ids(nrs, ret, &nr_targets);
355 	if (!targets) {
356 		ret = -ENOMEM;
357 		goto out;
358 	}
359 
360 	if (targetid_is_pid(ctx)) {
361 		for (i = 0; i < nr_targets; i++) {
362 			targets[i] = (unsigned long)find_get_pid(
363 					(int)targets[i]);
364 			if (!targets[i]) {
365 				dbgfs_put_pids(targets, i);
366 				ret = -EINVAL;
367 				goto free_targets_out;
368 			}
369 		}
370 	}
371 
372 	mutex_lock(&ctx->kdamond_lock);
373 	if (ctx->kdamond) {
374 		if (targetid_is_pid(ctx))
375 			dbgfs_put_pids(targets, nr_targets);
376 		ret = -EBUSY;
377 		goto unlock_out;
378 	}
379 
380 	err = damon_set_targets(ctx, targets, nr_targets);
381 	if (err) {
382 		if (targetid_is_pid(ctx))
383 			dbgfs_put_pids(targets, nr_targets);
384 		ret = err;
385 	}
386 
387 unlock_out:
388 	mutex_unlock(&ctx->kdamond_lock);
389 free_targets_out:
390 	kfree(targets);
391 out:
392 	kfree(kbuf);
393 	return ret;
394 }
395 
396 static ssize_t dbgfs_kdamond_pid_read(struct file *file,
397 		char __user *buf, size_t count, loff_t *ppos)
398 {
399 	struct damon_ctx *ctx = file->private_data;
400 	char *kbuf;
401 	ssize_t len;
402 
403 	kbuf = kmalloc(count, GFP_KERNEL);
404 	if (!kbuf)
405 		return -ENOMEM;
406 
407 	mutex_lock(&ctx->kdamond_lock);
408 	if (ctx->kdamond)
409 		len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
410 	else
411 		len = scnprintf(kbuf, count, "none\n");
412 	mutex_unlock(&ctx->kdamond_lock);
413 	if (!len)
414 		goto out;
415 	len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
416 
417 out:
418 	kfree(kbuf);
419 	return len;
420 }
421 
422 static int damon_dbgfs_open(struct inode *inode, struct file *file)
423 {
424 	file->private_data = inode->i_private;
425 
426 	return nonseekable_open(inode, file);
427 }
428 
429 static const struct file_operations attrs_fops = {
430 	.open = damon_dbgfs_open,
431 	.read = dbgfs_attrs_read,
432 	.write = dbgfs_attrs_write,
433 };
434 
435 static const struct file_operations schemes_fops = {
436 	.open = damon_dbgfs_open,
437 	.read = dbgfs_schemes_read,
438 	.write = dbgfs_schemes_write,
439 };
440 
441 static const struct file_operations target_ids_fops = {
442 	.open = damon_dbgfs_open,
443 	.read = dbgfs_target_ids_read,
444 	.write = dbgfs_target_ids_write,
445 };
446 
447 static const struct file_operations kdamond_pid_fops = {
448 	.open = damon_dbgfs_open,
449 	.read = dbgfs_kdamond_pid_read,
450 };
451 
452 static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
453 {
454 	const char * const file_names[] = {"attrs", "schemes", "target_ids",
455 		"kdamond_pid"};
456 	const struct file_operations *fops[] = {&attrs_fops, &schemes_fops,
457 		&target_ids_fops, &kdamond_pid_fops};
458 	int i;
459 
460 	for (i = 0; i < ARRAY_SIZE(file_names); i++)
461 		debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
462 }
463 
464 static int dbgfs_before_terminate(struct damon_ctx *ctx)
465 {
466 	struct damon_target *t, *next;
467 
468 	if (!targetid_is_pid(ctx))
469 		return 0;
470 
471 	damon_for_each_target_safe(t, next, ctx) {
472 		put_pid((struct pid *)t->id);
473 		damon_destroy_target(t);
474 	}
475 	return 0;
476 }
477 
478 static struct damon_ctx *dbgfs_new_ctx(void)
479 {
480 	struct damon_ctx *ctx;
481 
482 	ctx = damon_new_ctx();
483 	if (!ctx)
484 		return NULL;
485 
486 	damon_va_set_primitives(ctx);
487 	ctx->callback.before_terminate = dbgfs_before_terminate;
488 	return ctx;
489 }
490 
491 static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
492 {
493 	damon_destroy_ctx(ctx);
494 }
495 
496 /*
497  * Make a context of @name and create a debugfs directory for it.
498  *
499  * This function should be called while holding damon_dbgfs_lock.
500  *
501  * Returns 0 on success, negative error code otherwise.
502  */
503 static int dbgfs_mk_context(char *name)
504 {
505 	struct dentry *root, **new_dirs, *new_dir;
506 	struct damon_ctx **new_ctxs, *new_ctx;
507 
508 	if (damon_nr_running_ctxs())
509 		return -EBUSY;
510 
511 	new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
512 			(dbgfs_nr_ctxs + 1), GFP_KERNEL);
513 	if (!new_ctxs)
514 		return -ENOMEM;
515 	dbgfs_ctxs = new_ctxs;
516 
517 	new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
518 			(dbgfs_nr_ctxs + 1), GFP_KERNEL);
519 	if (!new_dirs)
520 		return -ENOMEM;
521 	dbgfs_dirs = new_dirs;
522 
523 	root = dbgfs_dirs[0];
524 	if (!root)
525 		return -ENOENT;
526 
527 	new_dir = debugfs_create_dir(name, root);
528 	dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
529 
530 	new_ctx = dbgfs_new_ctx();
531 	if (!new_ctx) {
532 		debugfs_remove(new_dir);
533 		dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
534 		return -ENOMEM;
535 	}
536 
537 	dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
538 	dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
539 			dbgfs_ctxs[dbgfs_nr_ctxs]);
540 	dbgfs_nr_ctxs++;
541 
542 	return 0;
543 }
544 
545 static ssize_t dbgfs_mk_context_write(struct file *file,
546 		const char __user *buf, size_t count, loff_t *ppos)
547 {
548 	char *kbuf;
549 	char *ctx_name;
550 	ssize_t ret = count;
551 	int err;
552 
553 	kbuf = user_input_str(buf, count, ppos);
554 	if (IS_ERR(kbuf))
555 		return PTR_ERR(kbuf);
556 	ctx_name = kmalloc(count + 1, GFP_KERNEL);
557 	if (!ctx_name) {
558 		kfree(kbuf);
559 		return -ENOMEM;
560 	}
561 
562 	/* Trim white space */
563 	if (sscanf(kbuf, "%s", ctx_name) != 1) {
564 		ret = -EINVAL;
565 		goto out;
566 	}
567 
568 	mutex_lock(&damon_dbgfs_lock);
569 	err = dbgfs_mk_context(ctx_name);
570 	if (err)
571 		ret = err;
572 	mutex_unlock(&damon_dbgfs_lock);
573 
574 out:
575 	kfree(kbuf);
576 	kfree(ctx_name);
577 	return ret;
578 }
579 
580 /*
581  * Remove a context of @name and its debugfs directory.
582  *
583  * This function should be called while holding damon_dbgfs_lock.
584  *
585  * Return 0 on success, negative error code otherwise.
586  */
587 static int dbgfs_rm_context(char *name)
588 {
589 	struct dentry *root, *dir, **new_dirs;
590 	struct damon_ctx **new_ctxs;
591 	int i, j;
592 
593 	if (damon_nr_running_ctxs())
594 		return -EBUSY;
595 
596 	root = dbgfs_dirs[0];
597 	if (!root)
598 		return -ENOENT;
599 
600 	dir = debugfs_lookup(name, root);
601 	if (!dir)
602 		return -ENOENT;
603 
604 	new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
605 			GFP_KERNEL);
606 	if (!new_dirs)
607 		return -ENOMEM;
608 
609 	new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
610 			GFP_KERNEL);
611 	if (!new_ctxs) {
612 		kfree(new_dirs);
613 		return -ENOMEM;
614 	}
615 
616 	for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
617 		if (dbgfs_dirs[i] == dir) {
618 			debugfs_remove(dbgfs_dirs[i]);
619 			dbgfs_destroy_ctx(dbgfs_ctxs[i]);
620 			continue;
621 		}
622 		new_dirs[j] = dbgfs_dirs[i];
623 		new_ctxs[j++] = dbgfs_ctxs[i];
624 	}
625 
626 	kfree(dbgfs_dirs);
627 	kfree(dbgfs_ctxs);
628 
629 	dbgfs_dirs = new_dirs;
630 	dbgfs_ctxs = new_ctxs;
631 	dbgfs_nr_ctxs--;
632 
633 	return 0;
634 }
635 
636 static ssize_t dbgfs_rm_context_write(struct file *file,
637 		const char __user *buf, size_t count, loff_t *ppos)
638 {
639 	char *kbuf;
640 	ssize_t ret = count;
641 	int err;
642 	char *ctx_name;
643 
644 	kbuf = user_input_str(buf, count, ppos);
645 	if (IS_ERR(kbuf))
646 		return PTR_ERR(kbuf);
647 	ctx_name = kmalloc(count + 1, GFP_KERNEL);
648 	if (!ctx_name) {
649 		kfree(kbuf);
650 		return -ENOMEM;
651 	}
652 
653 	/* Trim white space */
654 	if (sscanf(kbuf, "%s", ctx_name) != 1) {
655 		ret = -EINVAL;
656 		goto out;
657 	}
658 
659 	mutex_lock(&damon_dbgfs_lock);
660 	err = dbgfs_rm_context(ctx_name);
661 	if (err)
662 		ret = err;
663 	mutex_unlock(&damon_dbgfs_lock);
664 
665 out:
666 	kfree(kbuf);
667 	kfree(ctx_name);
668 	return ret;
669 }
670 
671 static ssize_t dbgfs_monitor_on_read(struct file *file,
672 		char __user *buf, size_t count, loff_t *ppos)
673 {
674 	char monitor_on_buf[5];
675 	bool monitor_on = damon_nr_running_ctxs() != 0;
676 	int len;
677 
678 	len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
679 
680 	return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
681 }
682 
683 static ssize_t dbgfs_monitor_on_write(struct file *file,
684 		const char __user *buf, size_t count, loff_t *ppos)
685 {
686 	ssize_t ret = count;
687 	char *kbuf;
688 	int err;
689 
690 	kbuf = user_input_str(buf, count, ppos);
691 	if (IS_ERR(kbuf))
692 		return PTR_ERR(kbuf);
693 
694 	/* Remove white space */
695 	if (sscanf(kbuf, "%s", kbuf) != 1) {
696 		kfree(kbuf);
697 		return -EINVAL;
698 	}
699 
700 	if (!strncmp(kbuf, "on", count))
701 		err = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs);
702 	else if (!strncmp(kbuf, "off", count))
703 		err = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
704 	else
705 		err = -EINVAL;
706 
707 	if (err)
708 		ret = err;
709 	kfree(kbuf);
710 	return ret;
711 }
712 
713 static const struct file_operations mk_contexts_fops = {
714 	.write = dbgfs_mk_context_write,
715 };
716 
717 static const struct file_operations rm_contexts_fops = {
718 	.write = dbgfs_rm_context_write,
719 };
720 
721 static const struct file_operations monitor_on_fops = {
722 	.read = dbgfs_monitor_on_read,
723 	.write = dbgfs_monitor_on_write,
724 };
725 
726 static int __init __damon_dbgfs_init(void)
727 {
728 	struct dentry *dbgfs_root;
729 	const char * const file_names[] = {"mk_contexts", "rm_contexts",
730 		"monitor_on"};
731 	const struct file_operations *fops[] = {&mk_contexts_fops,
732 		&rm_contexts_fops, &monitor_on_fops};
733 	int i;
734 
735 	dbgfs_root = debugfs_create_dir("damon", NULL);
736 
737 	for (i = 0; i < ARRAY_SIZE(file_names); i++)
738 		debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
739 				fops[i]);
740 	dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
741 
742 	dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
743 	if (!dbgfs_dirs) {
744 		debugfs_remove(dbgfs_root);
745 		return -ENOMEM;
746 	}
747 	dbgfs_dirs[0] = dbgfs_root;
748 
749 	return 0;
750 }
751 
752 /*
753  * Functions for the initialization
754  */
755 
756 static int __init damon_dbgfs_init(void)
757 {
758 	int rc;
759 
760 	dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
761 	if (!dbgfs_ctxs)
762 		return -ENOMEM;
763 	dbgfs_ctxs[0] = dbgfs_new_ctx();
764 	if (!dbgfs_ctxs[0]) {
765 		kfree(dbgfs_ctxs);
766 		return -ENOMEM;
767 	}
768 	dbgfs_nr_ctxs = 1;
769 
770 	rc = __damon_dbgfs_init();
771 	if (rc) {
772 		kfree(dbgfs_ctxs[0]);
773 		kfree(dbgfs_ctxs);
774 		pr_err("%s: dbgfs init failed\n", __func__);
775 	}
776 
777 	return rc;
778 }
779 
780 module_init(damon_dbgfs_init);
781 
782 #include "dbgfs-test.h"
783