blob: 1efac0022e9a46b2a94ba0329bbe157a33fc83d5 [file] [log] [blame]
SeongJae Park4bc05952021-09-07 19:56:53 -07001// 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;
SeongJae Park75c1c2b2021-09-07 19:57:01 -070021static DEFINE_MUTEX(damon_dbgfs_lock);
SeongJae Park4bc05952021-09-07 19:56:53 -070022
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
SeongJae Parkdb7a3472021-11-19 16:43:49 -080035 kbuf = kmalloc(count + 1, GFP_KERNEL | __GFP_NOWARN);
SeongJae Park4bc05952021-09-07 19:56:53 -070036 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,
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
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;
Rongwei Wang92106222021-11-05 13:47:09 -070072 ssize_t ret;
SeongJae Park4bc05952021-09-07 19:56:53 -070073
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
Rongwei Wang92106222021-11-05 13:47:09 -070090 ret = damon_set_attrs(ctx, s, a, r, minr, maxr);
91 if (!ret)
92 ret = count;
SeongJae Park4bc05952021-09-07 19:56:53 -070093unlock_out:
94 mutex_unlock(&ctx->kdamond_lock);
95out:
96 kfree(kbuf);
97 return ret;
98}
99
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700100static 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,
SeongJae Parkae666a62021-11-05 13:47:50 -0700108 "%lu %lu %u %u %u %u %d %lu %lu %lu %u %u %u %d %lu %lu %lu %lu %lu %lu\n",
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700109 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,
SeongJae Parkd7d0ec82021-11-05 13:47:27 -0700112 s->action,
113 s->quota.ms, s->quota.sz,
114 s->quota.reset_interval,
SeongJae Parkf4a68b42021-11-05 13:47:40 -0700115 s->quota.weight_sz,
116 s->quota.weight_nr_accesses,
117 s->quota.weight_age,
SeongJae Parkae666a62021-11-05 13:47:50 -0700118 s->wmarks.metric, s->wmarks.interval,
119 s->wmarks.high, s->wmarks.mid, s->wmarks.low,
SeongJae Parkd7d0ec82021-11-05 13:47:27 -0700120 s->stat_count, s->stat_sz);
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700121 if (!rc)
122 return -ENOMEM;
123
124 written += rc;
125 }
126 return written;
127}
128
129static ssize_t dbgfs_schemes_read(struct file *file, char __user *buf,
130 size_t count, loff_t *ppos)
131{
132 struct damon_ctx *ctx = file->private_data;
133 char *kbuf;
134 ssize_t len;
135
SeongJae Parkdb7a3472021-11-19 16:43:49 -0800136 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700137 if (!kbuf)
138 return -ENOMEM;
139
140 mutex_lock(&ctx->kdamond_lock);
141 len = sprint_schemes(ctx, kbuf, count);
142 mutex_unlock(&ctx->kdamond_lock);
143 if (len < 0)
144 goto out;
145 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
146
147out:
148 kfree(kbuf);
149 return len;
150}
151
152static void free_schemes_arr(struct damos **schemes, ssize_t nr_schemes)
153{
154 ssize_t i;
155
156 for (i = 0; i < nr_schemes; i++)
157 kfree(schemes[i]);
158 kfree(schemes);
159}
160
161static bool damos_action_valid(int action)
162{
163 switch (action) {
164 case DAMOS_WILLNEED:
165 case DAMOS_COLD:
166 case DAMOS_PAGEOUT:
167 case DAMOS_HUGEPAGE:
168 case DAMOS_NOHUGEPAGE:
SeongJae Park2f0b5482021-11-05 13:46:32 -0700169 case DAMOS_STAT:
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700170 return true;
171 default:
172 return false;
173 }
174}
175
176/*
177 * Converts a string into an array of struct damos pointers
178 *
179 * Returns an array of struct damos pointers that converted if the conversion
180 * success, or NULL otherwise.
181 */
182static struct damos **str_to_schemes(const char *str, ssize_t len,
183 ssize_t *nr_schemes)
184{
185 struct damos *scheme, **schemes;
186 const int max_nr_schemes = 256;
187 int pos = 0, parsed, ret;
188 unsigned long min_sz, max_sz;
189 unsigned int min_nr_a, max_nr_a, min_age, max_age;
190 unsigned int action;
191
192 schemes = kmalloc_array(max_nr_schemes, sizeof(scheme),
193 GFP_KERNEL);
194 if (!schemes)
195 return NULL;
196
197 *nr_schemes = 0;
198 while (pos < len && *nr_schemes < max_nr_schemes) {
SeongJae Park2b8a2482021-11-05 13:47:16 -0700199 struct damos_quota quota = {};
SeongJae Parkae666a62021-11-05 13:47:50 -0700200 struct damos_watermarks wmarks;
SeongJae Park2b8a2482021-11-05 13:47:16 -0700201
SeongJae Parkf4a68b42021-11-05 13:47:40 -0700202 ret = sscanf(&str[pos],
SeongJae Parkae666a62021-11-05 13:47:50 -0700203 "%lu %lu %u %u %u %u %u %lu %lu %lu %u %u %u %u %lu %lu %lu %lu%n",
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700204 &min_sz, &max_sz, &min_nr_a, &max_nr_a,
SeongJae Parkd7d0ec82021-11-05 13:47:27 -0700205 &min_age, &max_age, &action, &quota.ms,
SeongJae Parkf4a68b42021-11-05 13:47:40 -0700206 &quota.sz, &quota.reset_interval,
207 &quota.weight_sz, &quota.weight_nr_accesses,
SeongJae Parkae666a62021-11-05 13:47:50 -0700208 &quota.weight_age, &wmarks.metric,
209 &wmarks.interval, &wmarks.high, &wmarks.mid,
210 &wmarks.low, &parsed);
211 if (ret != 18)
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700212 break;
SeongJae Park0bceffa2021-12-10 14:46:31 -0800213 if (!damos_action_valid(action))
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700214 goto fail;
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700215
216 pos += parsed;
217 scheme = damon_new_scheme(min_sz, max_sz, min_nr_a, max_nr_a,
SeongJae Parkee801b72021-11-05 13:47:47 -0700218 min_age, max_age, action, &quota, &wmarks);
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700219 if (!scheme)
220 goto fail;
221
222 schemes[*nr_schemes] = scheme;
223 *nr_schemes += 1;
224 }
225 return schemes;
226fail:
227 free_schemes_arr(schemes, *nr_schemes);
228 return NULL;
229}
230
231static ssize_t dbgfs_schemes_write(struct file *file, const char __user *buf,
232 size_t count, loff_t *ppos)
233{
234 struct damon_ctx *ctx = file->private_data;
235 char *kbuf;
236 struct damos **schemes;
Rongwei Wang92106222021-11-05 13:47:09 -0700237 ssize_t nr_schemes = 0, ret;
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700238
239 kbuf = user_input_str(buf, count, ppos);
240 if (IS_ERR(kbuf))
241 return PTR_ERR(kbuf);
242
Rongwei Wang92106222021-11-05 13:47:09 -0700243 schemes = str_to_schemes(kbuf, count, &nr_schemes);
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700244 if (!schemes) {
245 ret = -EINVAL;
246 goto out;
247 }
248
249 mutex_lock(&ctx->kdamond_lock);
250 if (ctx->kdamond) {
251 ret = -EBUSY;
252 goto unlock_out;
253 }
254
Rongwei Wang92106222021-11-05 13:47:09 -0700255 ret = damon_set_schemes(ctx, schemes, nr_schemes);
256 if (!ret) {
257 ret = count;
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700258 nr_schemes = 0;
Rongwei Wang92106222021-11-05 13:47:09 -0700259 }
260
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700261unlock_out:
262 mutex_unlock(&ctx->kdamond_lock);
263 free_schemes_arr(schemes, nr_schemes);
264out:
265 kfree(kbuf);
266 return ret;
267}
268
SeongJae Park4bc05952021-09-07 19:56:53 -0700269static inline bool targetid_is_pid(const struct damon_ctx *ctx)
270{
271 return ctx->primitive.target_valid == damon_va_target_valid;
272}
273
274static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len)
275{
276 struct damon_target *t;
277 unsigned long id;
278 int written = 0;
279 int rc;
280
281 damon_for_each_target(t, ctx) {
282 id = t->id;
283 if (targetid_is_pid(ctx))
284 /* Show pid numbers to debugfs users */
285 id = (unsigned long)pid_vnr((struct pid *)id);
286
287 rc = scnprintf(&buf[written], len - written, "%lu ", id);
288 if (!rc)
289 return -ENOMEM;
290 written += rc;
291 }
292 if (written)
293 written -= 1;
294 written += scnprintf(&buf[written], len - written, "\n");
295 return written;
296}
297
298static ssize_t dbgfs_target_ids_read(struct file *file,
299 char __user *buf, size_t count, loff_t *ppos)
300{
301 struct damon_ctx *ctx = file->private_data;
302 ssize_t len;
303 char ids_buf[320];
304
305 mutex_lock(&ctx->kdamond_lock);
306 len = sprint_target_ids(ctx, ids_buf, 320);
307 mutex_unlock(&ctx->kdamond_lock);
308 if (len < 0)
309 return len;
310
311 return simple_read_from_buffer(buf, count, ppos, ids_buf, len);
312}
313
314/*
315 * Converts a string into an array of unsigned long integers
316 *
317 * Returns an array of unsigned long integers if the conversion success, or
318 * NULL otherwise.
319 */
320static unsigned long *str_to_target_ids(const char *str, ssize_t len,
321 ssize_t *nr_ids)
322{
323 unsigned long *ids;
324 const int max_nr_ids = 32;
325 unsigned long id;
326 int pos = 0, parsed, ret;
327
328 *nr_ids = 0;
329 ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL);
330 if (!ids)
331 return NULL;
332 while (*nr_ids < max_nr_ids && pos < len) {
333 ret = sscanf(&str[pos], "%lu%n", &id, &parsed);
334 pos += parsed;
335 if (ret != 1)
336 break;
337 ids[*nr_ids] = id;
338 *nr_ids += 1;
339 }
340
341 return ids;
342}
343
344static void dbgfs_put_pids(unsigned long *ids, int nr_ids)
345{
346 int i;
347
348 for (i = 0; i < nr_ids; i++)
349 put_pid((struct pid *)ids[i]);
350}
351
352static ssize_t dbgfs_target_ids_write(struct file *file,
353 const char __user *buf, size_t count, loff_t *ppos)
354{
355 struct damon_ctx *ctx = file->private_data;
SeongJae Parkc026291a2021-11-05 13:47:00 -0700356 bool id_is_pid = true;
SeongJae Park4bc05952021-09-07 19:56:53 -0700357 char *kbuf, *nrs;
358 unsigned long *targets;
359 ssize_t nr_targets;
Rongwei Wang92106222021-11-05 13:47:09 -0700360 ssize_t ret;
SeongJae Park4bc05952021-09-07 19:56:53 -0700361 int i;
SeongJae Park4bc05952021-09-07 19:56:53 -0700362
363 kbuf = user_input_str(buf, count, ppos);
364 if (IS_ERR(kbuf))
365 return PTR_ERR(kbuf);
366
367 nrs = kbuf;
SeongJae Parkc026291a2021-11-05 13:47:00 -0700368 if (!strncmp(kbuf, "paddr\n", count)) {
369 id_is_pid = false;
370 /* target id is meaningless here, but we set it just for fun */
371 scnprintf(kbuf, count, "42 ");
372 }
SeongJae Park4bc05952021-09-07 19:56:53 -0700373
Rongwei Wang92106222021-11-05 13:47:09 -0700374 targets = str_to_target_ids(nrs, count, &nr_targets);
SeongJae Park4bc05952021-09-07 19:56:53 -0700375 if (!targets) {
376 ret = -ENOMEM;
377 goto out;
378 }
379
SeongJae Parkc026291a2021-11-05 13:47:00 -0700380 if (id_is_pid) {
SeongJae Park4bc05952021-09-07 19:56:53 -0700381 for (i = 0; i < nr_targets; i++) {
382 targets[i] = (unsigned long)find_get_pid(
383 (int)targets[i]);
384 if (!targets[i]) {
385 dbgfs_put_pids(targets, i);
386 ret = -EINVAL;
387 goto free_targets_out;
388 }
389 }
390 }
391
392 mutex_lock(&ctx->kdamond_lock);
393 if (ctx->kdamond) {
SeongJae Parkc026291a2021-11-05 13:47:00 -0700394 if (id_is_pid)
SeongJae Park4bc05952021-09-07 19:56:53 -0700395 dbgfs_put_pids(targets, nr_targets);
396 ret = -EBUSY;
397 goto unlock_out;
398 }
399
SeongJae Parkc026291a2021-11-05 13:47:00 -0700400 /* remove targets with previously-set primitive */
401 damon_set_targets(ctx, NULL, 0);
402
403 /* Configure the context for the address space type */
404 if (id_is_pid)
405 damon_va_set_primitives(ctx);
406 else
407 damon_pa_set_primitives(ctx);
408
Rongwei Wang92106222021-11-05 13:47:09 -0700409 ret = damon_set_targets(ctx, targets, nr_targets);
410 if (ret) {
SeongJae Parkc026291a2021-11-05 13:47:00 -0700411 if (id_is_pid)
SeongJae Park4bc05952021-09-07 19:56:53 -0700412 dbgfs_put_pids(targets, nr_targets);
Rongwei Wang92106222021-11-05 13:47:09 -0700413 } else {
414 ret = count;
SeongJae Park4bc05952021-09-07 19:56:53 -0700415 }
416
417unlock_out:
418 mutex_unlock(&ctx->kdamond_lock);
419free_targets_out:
420 kfree(targets);
421out:
422 kfree(kbuf);
423 return ret;
424}
425
SeongJae Park90bebce2021-11-05 13:46:42 -0700426static ssize_t sprint_init_regions(struct damon_ctx *c, char *buf, ssize_t len)
427{
428 struct damon_target *t;
429 struct damon_region *r;
430 int written = 0;
431 int rc;
432
433 damon_for_each_target(t, c) {
434 damon_for_each_region(r, t) {
435 rc = scnprintf(&buf[written], len - written,
436 "%lu %lu %lu\n",
437 t->id, r->ar.start, r->ar.end);
438 if (!rc)
439 return -ENOMEM;
440 written += rc;
441 }
442 }
443 return written;
444}
445
446static ssize_t dbgfs_init_regions_read(struct file *file, char __user *buf,
447 size_t count, loff_t *ppos)
448{
449 struct damon_ctx *ctx = file->private_data;
450 char *kbuf;
451 ssize_t len;
452
SeongJae Parkdb7a3472021-11-19 16:43:49 -0800453 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
SeongJae Park90bebce2021-11-05 13:46:42 -0700454 if (!kbuf)
455 return -ENOMEM;
456
457 mutex_lock(&ctx->kdamond_lock);
458 if (ctx->kdamond) {
459 mutex_unlock(&ctx->kdamond_lock);
460 len = -EBUSY;
461 goto out;
462 }
463
464 len = sprint_init_regions(ctx, kbuf, count);
465 mutex_unlock(&ctx->kdamond_lock);
466 if (len < 0)
467 goto out;
468 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
469
470out:
471 kfree(kbuf);
472 return len;
473}
474
475static int add_init_region(struct damon_ctx *c,
476 unsigned long target_id, struct damon_addr_range *ar)
477{
478 struct damon_target *t;
479 struct damon_region *r, *prev;
480 unsigned long id;
481 int rc = -EINVAL;
482
483 if (ar->start >= ar->end)
484 return -EINVAL;
485
486 damon_for_each_target(t, c) {
487 id = t->id;
488 if (targetid_is_pid(c))
489 id = (unsigned long)pid_vnr((struct pid *)id);
490 if (id == target_id) {
491 r = damon_new_region(ar->start, ar->end);
492 if (!r)
493 return -ENOMEM;
494 damon_add_region(r, t);
495 if (damon_nr_regions(t) > 1) {
496 prev = damon_prev_region(r);
497 if (prev->ar.end > r->ar.start) {
498 damon_destroy_region(r, t);
499 return -EINVAL;
500 }
501 }
502 rc = 0;
503 }
504 }
505 return rc;
506}
507
508static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len)
509{
510 struct damon_target *t;
511 struct damon_region *r, *next;
512 int pos = 0, parsed, ret;
513 unsigned long target_id;
514 struct damon_addr_range ar;
515 int err;
516
517 damon_for_each_target(t, c) {
518 damon_for_each_region_safe(r, next, t)
519 damon_destroy_region(r, t);
520 }
521
522 while (pos < len) {
523 ret = sscanf(&str[pos], "%lu %lu %lu%n",
524 &target_id, &ar.start, &ar.end, &parsed);
525 if (ret != 3)
526 break;
527 err = add_init_region(c, target_id, &ar);
528 if (err)
529 goto fail;
530 pos += parsed;
531 }
532
533 return 0;
534
535fail:
536 damon_for_each_target(t, c) {
537 damon_for_each_region_safe(r, next, t)
538 damon_destroy_region(r, t);
539 }
540 return err;
541}
542
543static ssize_t dbgfs_init_regions_write(struct file *file,
544 const char __user *buf, size_t count,
545 loff_t *ppos)
546{
547 struct damon_ctx *ctx = file->private_data;
548 char *kbuf;
549 ssize_t ret = count;
550 int err;
551
552 kbuf = user_input_str(buf, count, ppos);
553 if (IS_ERR(kbuf))
554 return PTR_ERR(kbuf);
555
556 mutex_lock(&ctx->kdamond_lock);
557 if (ctx->kdamond) {
558 ret = -EBUSY;
559 goto unlock_out;
560 }
561
562 err = set_init_regions(ctx, kbuf, ret);
563 if (err)
564 ret = err;
565
566unlock_out:
567 mutex_unlock(&ctx->kdamond_lock);
568 kfree(kbuf);
569 return ret;
570}
571
SeongJae Park429538e2021-09-07 19:56:57 -0700572static ssize_t dbgfs_kdamond_pid_read(struct file *file,
573 char __user *buf, size_t count, loff_t *ppos)
574{
575 struct damon_ctx *ctx = file->private_data;
576 char *kbuf;
577 ssize_t len;
578
SeongJae Parkdb7a3472021-11-19 16:43:49 -0800579 kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
SeongJae Park429538e2021-09-07 19:56:57 -0700580 if (!kbuf)
581 return -ENOMEM;
582
583 mutex_lock(&ctx->kdamond_lock);
584 if (ctx->kdamond)
585 len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
586 else
587 len = scnprintf(kbuf, count, "none\n");
588 mutex_unlock(&ctx->kdamond_lock);
589 if (!len)
590 goto out;
591 len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
592
593out:
594 kfree(kbuf);
595 return len;
596}
597
SeongJae Park4bc05952021-09-07 19:56:53 -0700598static int damon_dbgfs_open(struct inode *inode, struct file *file)
599{
600 file->private_data = inode->i_private;
601
602 return nonseekable_open(inode, file);
603}
604
605static const struct file_operations attrs_fops = {
606 .open = damon_dbgfs_open,
607 .read = dbgfs_attrs_read,
608 .write = dbgfs_attrs_write,
609};
610
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700611static const struct file_operations schemes_fops = {
612 .open = damon_dbgfs_open,
613 .read = dbgfs_schemes_read,
614 .write = dbgfs_schemes_write,
615};
616
SeongJae Park4bc05952021-09-07 19:56:53 -0700617static const struct file_operations target_ids_fops = {
618 .open = damon_dbgfs_open,
619 .read = dbgfs_target_ids_read,
620 .write = dbgfs_target_ids_write,
621};
622
SeongJae Park90bebce2021-11-05 13:46:42 -0700623static const struct file_operations init_regions_fops = {
624 .open = damon_dbgfs_open,
625 .read = dbgfs_init_regions_read,
626 .write = dbgfs_init_regions_write,
627};
628
SeongJae Park429538e2021-09-07 19:56:57 -0700629static const struct file_operations kdamond_pid_fops = {
630 .open = damon_dbgfs_open,
631 .read = dbgfs_kdamond_pid_read,
632};
633
SeongJae Park4bc05952021-09-07 19:56:53 -0700634static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
635{
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700636 const char * const file_names[] = {"attrs", "schemes", "target_ids",
SeongJae Park90bebce2021-11-05 13:46:42 -0700637 "init_regions", "kdamond_pid"};
SeongJae Parkaf122dd2021-11-05 13:46:29 -0700638 const struct file_operations *fops[] = {&attrs_fops, &schemes_fops,
SeongJae Park90bebce2021-11-05 13:46:42 -0700639 &target_ids_fops, &init_regions_fops, &kdamond_pid_fops};
SeongJae Park4bc05952021-09-07 19:56:53 -0700640 int i;
641
642 for (i = 0; i < ARRAY_SIZE(file_names); i++)
643 debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
644}
645
Changbin Du658f9ae2021-11-05 13:48:27 -0700646static void dbgfs_before_terminate(struct damon_ctx *ctx)
SeongJae Park4bc05952021-09-07 19:56:53 -0700647{
648 struct damon_target *t, *next;
649
650 if (!targetid_is_pid(ctx))
Changbin Du658f9ae2021-11-05 13:48:27 -0700651 return;
SeongJae Park4bc05952021-09-07 19:56:53 -0700652
653 damon_for_each_target_safe(t, next, ctx) {
654 put_pid((struct pid *)t->id);
655 damon_destroy_target(t);
656 }
SeongJae Park4bc05952021-09-07 19:56:53 -0700657}
658
659static struct damon_ctx *dbgfs_new_ctx(void)
660{
661 struct damon_ctx *ctx;
662
663 ctx = damon_new_ctx();
664 if (!ctx)
665 return NULL;
666
667 damon_va_set_primitives(ctx);
668 ctx->callback.before_terminate = dbgfs_before_terminate;
669 return ctx;
670}
671
SeongJae Park75c1c2b2021-09-07 19:57:01 -0700672static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
673{
674 damon_destroy_ctx(ctx);
675}
676
677/*
678 * Make a context of @name and create a debugfs directory for it.
679 *
680 * This function should be called while holding damon_dbgfs_lock.
681 *
682 * Returns 0 on success, negative error code otherwise.
683 */
684static int dbgfs_mk_context(char *name)
685{
686 struct dentry *root, **new_dirs, *new_dir;
687 struct damon_ctx **new_ctxs, *new_ctx;
688
689 if (damon_nr_running_ctxs())
690 return -EBUSY;
691
692 new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
693 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
694 if (!new_ctxs)
695 return -ENOMEM;
696 dbgfs_ctxs = new_ctxs;
697
698 new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
699 (dbgfs_nr_ctxs + 1), GFP_KERNEL);
700 if (!new_dirs)
701 return -ENOMEM;
702 dbgfs_dirs = new_dirs;
703
704 root = dbgfs_dirs[0];
705 if (!root)
706 return -ENOENT;
707
708 new_dir = debugfs_create_dir(name, root);
709 dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
710
711 new_ctx = dbgfs_new_ctx();
712 if (!new_ctx) {
713 debugfs_remove(new_dir);
714 dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
715 return -ENOMEM;
716 }
717
718 dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
719 dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
720 dbgfs_ctxs[dbgfs_nr_ctxs]);
721 dbgfs_nr_ctxs++;
722
723 return 0;
724}
725
726static ssize_t dbgfs_mk_context_write(struct file *file,
727 const char __user *buf, size_t count, loff_t *ppos)
728{
729 char *kbuf;
730 char *ctx_name;
Rongwei Wang92106222021-11-05 13:47:09 -0700731 ssize_t ret;
SeongJae Park75c1c2b2021-09-07 19:57:01 -0700732
733 kbuf = user_input_str(buf, count, ppos);
734 if (IS_ERR(kbuf))
735 return PTR_ERR(kbuf);
736 ctx_name = kmalloc(count + 1, GFP_KERNEL);
737 if (!ctx_name) {
738 kfree(kbuf);
739 return -ENOMEM;
740 }
741
742 /* Trim white space */
743 if (sscanf(kbuf, "%s", ctx_name) != 1) {
744 ret = -EINVAL;
745 goto out;
746 }
747
748 mutex_lock(&damon_dbgfs_lock);
Rongwei Wang92106222021-11-05 13:47:09 -0700749 ret = dbgfs_mk_context(ctx_name);
750 if (!ret)
751 ret = count;
SeongJae Park75c1c2b2021-09-07 19:57:01 -0700752 mutex_unlock(&damon_dbgfs_lock);
753
754out:
755 kfree(kbuf);
756 kfree(ctx_name);
757 return ret;
758}
759
760/*
761 * Remove a context of @name and its debugfs directory.
762 *
763 * This function should be called while holding damon_dbgfs_lock.
764 *
765 * Return 0 on success, negative error code otherwise.
766 */
767static int dbgfs_rm_context(char *name)
768{
769 struct dentry *root, *dir, **new_dirs;
770 struct damon_ctx **new_ctxs;
771 int i, j;
772
773 if (damon_nr_running_ctxs())
774 return -EBUSY;
775
776 root = dbgfs_dirs[0];
777 if (!root)
778 return -ENOENT;
779
780 dir = debugfs_lookup(name, root);
781 if (!dir)
782 return -ENOENT;
783
784 new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
785 GFP_KERNEL);
786 if (!new_dirs)
787 return -ENOMEM;
788
789 new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
790 GFP_KERNEL);
791 if (!new_ctxs) {
792 kfree(new_dirs);
793 return -ENOMEM;
794 }
795
796 for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
797 if (dbgfs_dirs[i] == dir) {
798 debugfs_remove(dbgfs_dirs[i]);
799 dbgfs_destroy_ctx(dbgfs_ctxs[i]);
800 continue;
801 }
802 new_dirs[j] = dbgfs_dirs[i];
803 new_ctxs[j++] = dbgfs_ctxs[i];
804 }
805
806 kfree(dbgfs_dirs);
807 kfree(dbgfs_ctxs);
808
809 dbgfs_dirs = new_dirs;
810 dbgfs_ctxs = new_ctxs;
811 dbgfs_nr_ctxs--;
812
813 return 0;
814}
815
816static ssize_t dbgfs_rm_context_write(struct file *file,
817 const char __user *buf, size_t count, loff_t *ppos)
818{
819 char *kbuf;
Rongwei Wang92106222021-11-05 13:47:09 -0700820 ssize_t ret;
SeongJae Park75c1c2b2021-09-07 19:57:01 -0700821 char *ctx_name;
822
823 kbuf = user_input_str(buf, count, ppos);
824 if (IS_ERR(kbuf))
825 return PTR_ERR(kbuf);
826 ctx_name = kmalloc(count + 1, GFP_KERNEL);
827 if (!ctx_name) {
828 kfree(kbuf);
829 return -ENOMEM;
830 }
831
832 /* Trim white space */
833 if (sscanf(kbuf, "%s", ctx_name) != 1) {
834 ret = -EINVAL;
835 goto out;
836 }
837
838 mutex_lock(&damon_dbgfs_lock);
Rongwei Wang92106222021-11-05 13:47:09 -0700839 ret = dbgfs_rm_context(ctx_name);
840 if (!ret)
841 ret = count;
SeongJae Park75c1c2b2021-09-07 19:57:01 -0700842 mutex_unlock(&damon_dbgfs_lock);
843
844out:
845 kfree(kbuf);
846 kfree(ctx_name);
847 return ret;
848}
849
SeongJae Park4bc05952021-09-07 19:56:53 -0700850static ssize_t dbgfs_monitor_on_read(struct file *file,
851 char __user *buf, size_t count, loff_t *ppos)
852{
853 char monitor_on_buf[5];
854 bool monitor_on = damon_nr_running_ctxs() != 0;
855 int len;
856
857 len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
858
859 return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
860}
861
862static ssize_t dbgfs_monitor_on_write(struct file *file,
863 const char __user *buf, size_t count, loff_t *ppos)
864{
Rongwei Wang92106222021-11-05 13:47:09 -0700865 ssize_t ret;
SeongJae Park4bc05952021-09-07 19:56:53 -0700866 char *kbuf;
SeongJae Park4bc05952021-09-07 19:56:53 -0700867
868 kbuf = user_input_str(buf, count, ppos);
869 if (IS_ERR(kbuf))
870 return PTR_ERR(kbuf);
871
872 /* Remove white space */
873 if (sscanf(kbuf, "%s", kbuf) != 1) {
874 kfree(kbuf);
875 return -EINVAL;
876 }
877
SeongJae Parkd78f38532021-11-19 16:43:52 -0800878 mutex_lock(&damon_dbgfs_lock);
Xin Haob5ca3e82021-11-05 13:48:07 -0700879 if (!strncmp(kbuf, "on", count)) {
880 int i;
881
882 for (i = 0; i < dbgfs_nr_ctxs; i++) {
883 if (damon_targets_empty(dbgfs_ctxs[i])) {
884 kfree(kbuf);
SeongJae Parkd78f38532021-11-19 16:43:52 -0800885 mutex_unlock(&damon_dbgfs_lock);
Xin Haob5ca3e82021-11-05 13:48:07 -0700886 return -EINVAL;
887 }
888 }
Rongwei Wang92106222021-11-05 13:47:09 -0700889 ret = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs);
Xin Haob5ca3e82021-11-05 13:48:07 -0700890 } else if (!strncmp(kbuf, "off", count)) {
Rongwei Wang92106222021-11-05 13:47:09 -0700891 ret = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
Xin Haob5ca3e82021-11-05 13:48:07 -0700892 } else {
Rongwei Wang92106222021-11-05 13:47:09 -0700893 ret = -EINVAL;
Xin Haob5ca3e82021-11-05 13:48:07 -0700894 }
SeongJae Parkd78f38532021-11-19 16:43:52 -0800895 mutex_unlock(&damon_dbgfs_lock);
SeongJae Park4bc05952021-09-07 19:56:53 -0700896
Rongwei Wang92106222021-11-05 13:47:09 -0700897 if (!ret)
898 ret = count;
SeongJae Park4bc05952021-09-07 19:56:53 -0700899 kfree(kbuf);
900 return ret;
901}
902
SeongJae Park75c1c2b2021-09-07 19:57:01 -0700903static const struct file_operations mk_contexts_fops = {
904 .write = dbgfs_mk_context_write,
905};
906
907static const struct file_operations rm_contexts_fops = {
908 .write = dbgfs_rm_context_write,
909};
910
SeongJae Park4bc05952021-09-07 19:56:53 -0700911static const struct file_operations monitor_on_fops = {
912 .read = dbgfs_monitor_on_read,
913 .write = dbgfs_monitor_on_write,
914};
915
916static int __init __damon_dbgfs_init(void)
917{
918 struct dentry *dbgfs_root;
SeongJae Park75c1c2b2021-09-07 19:57:01 -0700919 const char * const file_names[] = {"mk_contexts", "rm_contexts",
920 "monitor_on"};
921 const struct file_operations *fops[] = {&mk_contexts_fops,
922 &rm_contexts_fops, &monitor_on_fops};
SeongJae Park4bc05952021-09-07 19:56:53 -0700923 int i;
924
925 dbgfs_root = debugfs_create_dir("damon", NULL);
926
927 for (i = 0; i < ARRAY_SIZE(file_names); i++)
928 debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
929 fops[i]);
930 dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
931
932 dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
933 if (!dbgfs_dirs) {
934 debugfs_remove(dbgfs_root);
935 return -ENOMEM;
936 }
937 dbgfs_dirs[0] = dbgfs_root;
938
939 return 0;
940}
941
942/*
943 * Functions for the initialization
944 */
945
946static int __init damon_dbgfs_init(void)
947{
SeongJae Parkd78f38532021-11-19 16:43:52 -0800948 int rc = -ENOMEM;
SeongJae Park4bc05952021-09-07 19:56:53 -0700949
SeongJae Parkd78f38532021-11-19 16:43:52 -0800950 mutex_lock(&damon_dbgfs_lock);
SeongJae Park4bc05952021-09-07 19:56:53 -0700951 dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
952 if (!dbgfs_ctxs)
SeongJae Parkd78f38532021-11-19 16:43:52 -0800953 goto out;
SeongJae Park4bc05952021-09-07 19:56:53 -0700954 dbgfs_ctxs[0] = dbgfs_new_ctx();
955 if (!dbgfs_ctxs[0]) {
956 kfree(dbgfs_ctxs);
SeongJae Parkd78f38532021-11-19 16:43:52 -0800957 goto out;
SeongJae Park4bc05952021-09-07 19:56:53 -0700958 }
959 dbgfs_nr_ctxs = 1;
960
961 rc = __damon_dbgfs_init();
962 if (rc) {
963 kfree(dbgfs_ctxs[0]);
964 kfree(dbgfs_ctxs);
965 pr_err("%s: dbgfs init failed\n", __func__);
966 }
967
SeongJae Parkd78f38532021-11-19 16:43:52 -0800968out:
969 mutex_unlock(&damon_dbgfs_lock);
SeongJae Park4bc05952021-09-07 19:56:53 -0700970 return rc;
971}
972
973module_init(damon_dbgfs_init);
SeongJae Park17ccae82021-09-07 19:57:09 -0700974
975#include "dbgfs-test.h"