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