14bc05954SSeongJae Park // SPDX-License-Identifier: GPL-2.0 24bc05954SSeongJae Park /* 34bc05954SSeongJae Park * DAMON Debugfs Interface 44bc05954SSeongJae Park * 54bc05954SSeongJae Park * Author: SeongJae Park <sjpark@amazon.de> 64bc05954SSeongJae Park */ 74bc05954SSeongJae Park 84bc05954SSeongJae Park #define pr_fmt(fmt) "damon-dbgfs: " fmt 94bc05954SSeongJae Park 104bc05954SSeongJae Park #include <linux/damon.h> 114bc05954SSeongJae Park #include <linux/debugfs.h> 124bc05954SSeongJae Park #include <linux/file.h> 134bc05954SSeongJae Park #include <linux/mm.h> 144bc05954SSeongJae Park #include <linux/module.h> 154bc05954SSeongJae Park #include <linux/page_idle.h> 164bc05954SSeongJae Park #include <linux/slab.h> 174bc05954SSeongJae Park 184bc05954SSeongJae Park static struct damon_ctx **dbgfs_ctxs; 194bc05954SSeongJae Park static int dbgfs_nr_ctxs; 204bc05954SSeongJae Park static struct dentry **dbgfs_dirs; 2175c1c2b5SSeongJae Park static DEFINE_MUTEX(damon_dbgfs_lock); 224bc05954SSeongJae Park 234bc05954SSeongJae Park /* 244bc05954SSeongJae Park * Returns non-empty string on success, negative error code otherwise. 254bc05954SSeongJae Park */ 264bc05954SSeongJae Park static char *user_input_str(const char __user *buf, size_t count, loff_t *ppos) 274bc05954SSeongJae Park { 284bc05954SSeongJae Park char *kbuf; 294bc05954SSeongJae Park ssize_t ret; 304bc05954SSeongJae Park 314bc05954SSeongJae Park /* We do not accept continuous write */ 324bc05954SSeongJae Park if (*ppos) 334bc05954SSeongJae Park return ERR_PTR(-EINVAL); 344bc05954SSeongJae Park 35db7a347bSSeongJae Park kbuf = kmalloc(count + 1, GFP_KERNEL | __GFP_NOWARN); 364bc05954SSeongJae Park if (!kbuf) 374bc05954SSeongJae Park return ERR_PTR(-ENOMEM); 384bc05954SSeongJae Park 394bc05954SSeongJae Park ret = simple_write_to_buffer(kbuf, count + 1, ppos, buf, count); 404bc05954SSeongJae Park if (ret != count) { 414bc05954SSeongJae Park kfree(kbuf); 424bc05954SSeongJae Park return ERR_PTR(-EIO); 434bc05954SSeongJae Park } 444bc05954SSeongJae Park kbuf[ret] = '\0'; 454bc05954SSeongJae Park 464bc05954SSeongJae Park return kbuf; 474bc05954SSeongJae Park } 484bc05954SSeongJae Park 494bc05954SSeongJae Park static ssize_t dbgfs_attrs_read(struct file *file, 504bc05954SSeongJae Park char __user *buf, size_t count, loff_t *ppos) 514bc05954SSeongJae Park { 524bc05954SSeongJae Park struct damon_ctx *ctx = file->private_data; 534bc05954SSeongJae Park char kbuf[128]; 544bc05954SSeongJae Park int ret; 554bc05954SSeongJae Park 564bc05954SSeongJae Park mutex_lock(&ctx->kdamond_lock); 574bc05954SSeongJae Park ret = scnprintf(kbuf, ARRAY_SIZE(kbuf), "%lu %lu %lu %lu %lu\n", 584bc05954SSeongJae Park ctx->sample_interval, ctx->aggr_interval, 59f7d911c3SSeongJae Park ctx->ops_update_interval, ctx->min_nr_regions, 604bc05954SSeongJae Park ctx->max_nr_regions); 614bc05954SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 624bc05954SSeongJae Park 634bc05954SSeongJae Park return simple_read_from_buffer(buf, count, ppos, kbuf, ret); 644bc05954SSeongJae Park } 654bc05954SSeongJae Park 664bc05954SSeongJae Park static ssize_t dbgfs_attrs_write(struct file *file, 674bc05954SSeongJae Park const char __user *buf, size_t count, loff_t *ppos) 684bc05954SSeongJae Park { 694bc05954SSeongJae Park struct damon_ctx *ctx = file->private_data; 704bc05954SSeongJae Park unsigned long s, a, r, minr, maxr; 714bc05954SSeongJae Park char *kbuf; 729210622aSRongwei Wang ssize_t ret; 734bc05954SSeongJae Park 744bc05954SSeongJae Park kbuf = user_input_str(buf, count, ppos); 754bc05954SSeongJae Park if (IS_ERR(kbuf)) 764bc05954SSeongJae Park return PTR_ERR(kbuf); 774bc05954SSeongJae Park 784bc05954SSeongJae Park if (sscanf(kbuf, "%lu %lu %lu %lu %lu", 794bc05954SSeongJae Park &s, &a, &r, &minr, &maxr) != 5) { 804bc05954SSeongJae Park ret = -EINVAL; 814bc05954SSeongJae Park goto out; 824bc05954SSeongJae Park } 834bc05954SSeongJae Park 844bc05954SSeongJae Park mutex_lock(&ctx->kdamond_lock); 854bc05954SSeongJae Park if (ctx->kdamond) { 864bc05954SSeongJae Park ret = -EBUSY; 874bc05954SSeongJae Park goto unlock_out; 884bc05954SSeongJae Park } 894bc05954SSeongJae Park 909210622aSRongwei Wang ret = damon_set_attrs(ctx, s, a, r, minr, maxr); 919210622aSRongwei Wang if (!ret) 929210622aSRongwei Wang ret = count; 934bc05954SSeongJae Park unlock_out: 944bc05954SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 954bc05954SSeongJae Park out: 964bc05954SSeongJae Park kfree(kbuf); 974bc05954SSeongJae Park return ret; 984bc05954SSeongJae Park } 994bc05954SSeongJae Park 100c364f9afSSeongJae Park /* 101c364f9afSSeongJae Park * Return corresponding dbgfs' scheme action value (int) for the given 102c364f9afSSeongJae Park * damos_action if the given damos_action value is valid and supported by 103c364f9afSSeongJae Park * dbgfs, negative error code otherwise. 104c364f9afSSeongJae Park */ 105c364f9afSSeongJae Park static int damos_action_to_dbgfs_scheme_action(enum damos_action action) 106c364f9afSSeongJae Park { 107c364f9afSSeongJae Park switch (action) { 108c364f9afSSeongJae Park case DAMOS_WILLNEED: 109c364f9afSSeongJae Park return 0; 110c364f9afSSeongJae Park case DAMOS_COLD: 111c364f9afSSeongJae Park return 1; 112c364f9afSSeongJae Park case DAMOS_PAGEOUT: 113c364f9afSSeongJae Park return 2; 114c364f9afSSeongJae Park case DAMOS_HUGEPAGE: 115c364f9afSSeongJae Park return 3; 116c364f9afSSeongJae Park case DAMOS_NOHUGEPAGE: 117c364f9afSSeongJae Park return 4; 118c364f9afSSeongJae Park case DAMOS_STAT: 119c364f9afSSeongJae Park return 5; 120c364f9afSSeongJae Park default: 121c364f9afSSeongJae Park return -EINVAL; 122c364f9afSSeongJae Park } 123c364f9afSSeongJae Park } 124c364f9afSSeongJae Park 125af122dd8SSeongJae Park static ssize_t sprint_schemes(struct damon_ctx *c, char *buf, ssize_t len) 126af122dd8SSeongJae Park { 127af122dd8SSeongJae Park struct damos *s; 128af122dd8SSeongJae Park int written = 0; 129af122dd8SSeongJae Park int rc; 130af122dd8SSeongJae Park 131af122dd8SSeongJae Park damon_for_each_scheme(s, c) { 132af122dd8SSeongJae Park rc = scnprintf(&buf[written], len - written, 1333a619fdbSSeongJae Park "%lu %lu %u %u %u %u %d %lu %lu %lu %u %u %u %d %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", 134*f5a79d7cSYajun Deng s->pattern.min_sz_region, 135*f5a79d7cSYajun Deng s->pattern.max_sz_region, 136*f5a79d7cSYajun Deng s->pattern.min_nr_accesses, 137*f5a79d7cSYajun Deng s->pattern.max_nr_accesses, 138*f5a79d7cSYajun Deng s->pattern.min_age_region, 139*f5a79d7cSYajun Deng s->pattern.max_age_region, 140c364f9afSSeongJae Park damos_action_to_dbgfs_scheme_action(s->action), 141d7d0ec85SSeongJae Park s->quota.ms, s->quota.sz, 142d7d0ec85SSeongJae Park s->quota.reset_interval, 143f4a68b4aSSeongJae Park s->quota.weight_sz, 144f4a68b4aSSeongJae Park s->quota.weight_nr_accesses, 145f4a68b4aSSeongJae Park s->quota.weight_age, 146ae666a6dSSeongJae Park s->wmarks.metric, s->wmarks.interval, 147ae666a6dSSeongJae Park s->wmarks.high, s->wmarks.mid, s->wmarks.low, 1483a619fdbSSeongJae Park s->stat.nr_tried, s->stat.sz_tried, 1493a619fdbSSeongJae Park s->stat.nr_applied, s->stat.sz_applied, 1503a619fdbSSeongJae Park s->stat.qt_exceeds); 151af122dd8SSeongJae Park if (!rc) 152af122dd8SSeongJae Park return -ENOMEM; 153af122dd8SSeongJae Park 154af122dd8SSeongJae Park written += rc; 155af122dd8SSeongJae Park } 156af122dd8SSeongJae Park return written; 157af122dd8SSeongJae Park } 158af122dd8SSeongJae Park 159af122dd8SSeongJae Park static ssize_t dbgfs_schemes_read(struct file *file, char __user *buf, 160af122dd8SSeongJae Park size_t count, loff_t *ppos) 161af122dd8SSeongJae Park { 162af122dd8SSeongJae Park struct damon_ctx *ctx = file->private_data; 163af122dd8SSeongJae Park char *kbuf; 164af122dd8SSeongJae Park ssize_t len; 165af122dd8SSeongJae Park 166db7a347bSSeongJae Park kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN); 167af122dd8SSeongJae Park if (!kbuf) 168af122dd8SSeongJae Park return -ENOMEM; 169af122dd8SSeongJae Park 170af122dd8SSeongJae Park mutex_lock(&ctx->kdamond_lock); 171af122dd8SSeongJae Park len = sprint_schemes(ctx, kbuf, count); 172af122dd8SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 173af122dd8SSeongJae Park if (len < 0) 174af122dd8SSeongJae Park goto out; 175af122dd8SSeongJae Park len = simple_read_from_buffer(buf, count, ppos, kbuf, len); 176af122dd8SSeongJae Park 177af122dd8SSeongJae Park out: 178af122dd8SSeongJae Park kfree(kbuf); 179af122dd8SSeongJae Park return len; 180af122dd8SSeongJae Park } 181af122dd8SSeongJae Park 182af122dd8SSeongJae Park static void free_schemes_arr(struct damos **schemes, ssize_t nr_schemes) 183af122dd8SSeongJae Park { 184af122dd8SSeongJae Park ssize_t i; 185af122dd8SSeongJae Park 186af122dd8SSeongJae Park for (i = 0; i < nr_schemes; i++) 187af122dd8SSeongJae Park kfree(schemes[i]); 188af122dd8SSeongJae Park kfree(schemes); 189af122dd8SSeongJae Park } 190af122dd8SSeongJae Park 191c364f9afSSeongJae Park /* 192c364f9afSSeongJae Park * Return corresponding damos_action for the given dbgfs input for a scheme 193c364f9afSSeongJae Park * action if the input is valid, negative error code otherwise. 194c364f9afSSeongJae Park */ 195c364f9afSSeongJae Park static enum damos_action dbgfs_scheme_action_to_damos_action(int dbgfs_action) 196af122dd8SSeongJae Park { 197c364f9afSSeongJae Park switch (dbgfs_action) { 198c364f9afSSeongJae Park case 0: 199c364f9afSSeongJae Park return DAMOS_WILLNEED; 200c364f9afSSeongJae Park case 1: 201c364f9afSSeongJae Park return DAMOS_COLD; 202c364f9afSSeongJae Park case 2: 203c364f9afSSeongJae Park return DAMOS_PAGEOUT; 204c364f9afSSeongJae Park case 3: 205c364f9afSSeongJae Park return DAMOS_HUGEPAGE; 206c364f9afSSeongJae Park case 4: 207c364f9afSSeongJae Park return DAMOS_NOHUGEPAGE; 208c364f9afSSeongJae Park case 5: 209c364f9afSSeongJae Park return DAMOS_STAT; 210af122dd8SSeongJae Park default: 211c364f9afSSeongJae Park return -EINVAL; 212af122dd8SSeongJae Park } 213af122dd8SSeongJae Park } 214af122dd8SSeongJae Park 215af122dd8SSeongJae Park /* 216af122dd8SSeongJae Park * Converts a string into an array of struct damos pointers 217af122dd8SSeongJae Park * 218af122dd8SSeongJae Park * Returns an array of struct damos pointers that converted if the conversion 219af122dd8SSeongJae Park * success, or NULL otherwise. 220af122dd8SSeongJae Park */ 221af122dd8SSeongJae Park static struct damos **str_to_schemes(const char *str, ssize_t len, 222af122dd8SSeongJae Park ssize_t *nr_schemes) 223af122dd8SSeongJae Park { 224af122dd8SSeongJae Park struct damos *scheme, **schemes; 225af122dd8SSeongJae Park const int max_nr_schemes = 256; 226af122dd8SSeongJae Park int pos = 0, parsed, ret; 227c364f9afSSeongJae Park unsigned int action_input; 228c364f9afSSeongJae Park enum damos_action action; 229af122dd8SSeongJae Park 230af122dd8SSeongJae Park schemes = kmalloc_array(max_nr_schemes, sizeof(scheme), 231af122dd8SSeongJae Park GFP_KERNEL); 232af122dd8SSeongJae Park if (!schemes) 233af122dd8SSeongJae Park return NULL; 234af122dd8SSeongJae Park 235af122dd8SSeongJae Park *nr_schemes = 0; 236af122dd8SSeongJae Park while (pos < len && *nr_schemes < max_nr_schemes) { 237*f5a79d7cSYajun Deng struct damos_access_pattern pattern = {}; 2382b8a248dSSeongJae Park struct damos_quota quota = {}; 239ae666a6dSSeongJae Park struct damos_watermarks wmarks; 2402b8a248dSSeongJae Park 241f4a68b4aSSeongJae Park ret = sscanf(&str[pos], 242ae666a6dSSeongJae Park "%lu %lu %u %u %u %u %u %lu %lu %lu %u %u %u %u %lu %lu %lu %lu%n", 243*f5a79d7cSYajun Deng &pattern.min_sz_region, &pattern.max_sz_region, 244*f5a79d7cSYajun Deng &pattern.min_nr_accesses, 245*f5a79d7cSYajun Deng &pattern.max_nr_accesses, 246*f5a79d7cSYajun Deng &pattern.min_age_region, 247*f5a79d7cSYajun Deng &pattern.max_age_region, 248*f5a79d7cSYajun Deng &action_input, "a.ms, 249f4a68b4aSSeongJae Park "a.sz, "a.reset_interval, 250f4a68b4aSSeongJae Park "a.weight_sz, "a.weight_nr_accesses, 251ae666a6dSSeongJae Park "a.weight_age, &wmarks.metric, 252ae666a6dSSeongJae Park &wmarks.interval, &wmarks.high, &wmarks.mid, 253ae666a6dSSeongJae Park &wmarks.low, &parsed); 254ae666a6dSSeongJae Park if (ret != 18) 255af122dd8SSeongJae Park break; 256c364f9afSSeongJae Park action = dbgfs_scheme_action_to_damos_action(action_input); 257c364f9afSSeongJae Park if ((int)action < 0) 258af122dd8SSeongJae Park goto fail; 259af122dd8SSeongJae Park 260*f5a79d7cSYajun Deng if (pattern.min_sz_region > pattern.max_sz_region || 261*f5a79d7cSYajun Deng pattern.min_nr_accesses > pattern.max_nr_accesses || 262*f5a79d7cSYajun Deng pattern.min_age_region > pattern.max_age_region) 263c89ae63eSXin Hao goto fail; 264c89ae63eSXin Hao 265c89ae63eSXin Hao if (wmarks.high < wmarks.mid || wmarks.high < wmarks.low || 266c89ae63eSXin Hao wmarks.mid < wmarks.low) 267c89ae63eSXin Hao goto fail; 268c89ae63eSXin Hao 269af122dd8SSeongJae Park pos += parsed; 270*f5a79d7cSYajun Deng scheme = damon_new_scheme(&pattern, action, "a, &wmarks); 271af122dd8SSeongJae Park if (!scheme) 272af122dd8SSeongJae Park goto fail; 273af122dd8SSeongJae Park 274af122dd8SSeongJae Park schemes[*nr_schemes] = scheme; 275af122dd8SSeongJae Park *nr_schemes += 1; 276af122dd8SSeongJae Park } 277af122dd8SSeongJae Park return schemes; 278af122dd8SSeongJae Park fail: 279af122dd8SSeongJae Park free_schemes_arr(schemes, *nr_schemes); 280af122dd8SSeongJae Park return NULL; 281af122dd8SSeongJae Park } 282af122dd8SSeongJae Park 283af122dd8SSeongJae Park static ssize_t dbgfs_schemes_write(struct file *file, const char __user *buf, 284af122dd8SSeongJae Park size_t count, loff_t *ppos) 285af122dd8SSeongJae Park { 286af122dd8SSeongJae Park struct damon_ctx *ctx = file->private_data; 287af122dd8SSeongJae Park char *kbuf; 288af122dd8SSeongJae Park struct damos **schemes; 2899210622aSRongwei Wang ssize_t nr_schemes = 0, ret; 290af122dd8SSeongJae Park 291af122dd8SSeongJae Park kbuf = user_input_str(buf, count, ppos); 292af122dd8SSeongJae Park if (IS_ERR(kbuf)) 293af122dd8SSeongJae Park return PTR_ERR(kbuf); 294af122dd8SSeongJae Park 2959210622aSRongwei Wang schemes = str_to_schemes(kbuf, count, &nr_schemes); 296af122dd8SSeongJae Park if (!schemes) { 297af122dd8SSeongJae Park ret = -EINVAL; 298af122dd8SSeongJae Park goto out; 299af122dd8SSeongJae Park } 300af122dd8SSeongJae Park 301af122dd8SSeongJae Park mutex_lock(&ctx->kdamond_lock); 302af122dd8SSeongJae Park if (ctx->kdamond) { 303af122dd8SSeongJae Park ret = -EBUSY; 304af122dd8SSeongJae Park goto unlock_out; 305af122dd8SSeongJae Park } 306af122dd8SSeongJae Park 3079210622aSRongwei Wang ret = damon_set_schemes(ctx, schemes, nr_schemes); 3089210622aSRongwei Wang if (!ret) { 3099210622aSRongwei Wang ret = count; 310af122dd8SSeongJae Park nr_schemes = 0; 3119210622aSRongwei Wang } 3129210622aSRongwei Wang 313af122dd8SSeongJae Park unlock_out: 314af122dd8SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 315af122dd8SSeongJae Park free_schemes_arr(schemes, nr_schemes); 316af122dd8SSeongJae Park out: 317af122dd8SSeongJae Park kfree(kbuf); 318af122dd8SSeongJae Park return ret; 319af122dd8SSeongJae Park } 320af122dd8SSeongJae Park 3214bc05954SSeongJae Park static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len) 3224bc05954SSeongJae Park { 3234bc05954SSeongJae Park struct damon_target *t; 3241971bd63SSeongJae Park int id; 3254bc05954SSeongJae Park int written = 0; 3264bc05954SSeongJae Park int rc; 3274bc05954SSeongJae Park 3284bc05954SSeongJae Park damon_for_each_target(t, ctx) { 329c9e124e0SSeongJae Park if (damon_target_has_pid(ctx)) 3304bc05954SSeongJae Park /* Show pid numbers to debugfs users */ 3311971bd63SSeongJae Park id = pid_vnr(t->pid); 3321971bd63SSeongJae Park else 3331971bd63SSeongJae Park /* Show 42 for physical address space, just for fun */ 3341971bd63SSeongJae Park id = 42; 3354bc05954SSeongJae Park 3361971bd63SSeongJae Park rc = scnprintf(&buf[written], len - written, "%d ", id); 3374bc05954SSeongJae Park if (!rc) 3384bc05954SSeongJae Park return -ENOMEM; 3394bc05954SSeongJae Park written += rc; 3404bc05954SSeongJae Park } 3414bc05954SSeongJae Park if (written) 3424bc05954SSeongJae Park written -= 1; 3434bc05954SSeongJae Park written += scnprintf(&buf[written], len - written, "\n"); 3444bc05954SSeongJae Park return written; 3454bc05954SSeongJae Park } 3464bc05954SSeongJae Park 3474bc05954SSeongJae Park static ssize_t dbgfs_target_ids_read(struct file *file, 3484bc05954SSeongJae Park char __user *buf, size_t count, loff_t *ppos) 3494bc05954SSeongJae Park { 3504bc05954SSeongJae Park struct damon_ctx *ctx = file->private_data; 3514bc05954SSeongJae Park ssize_t len; 3524bc05954SSeongJae Park char ids_buf[320]; 3534bc05954SSeongJae Park 3544bc05954SSeongJae Park mutex_lock(&ctx->kdamond_lock); 3554bc05954SSeongJae Park len = sprint_target_ids(ctx, ids_buf, 320); 3564bc05954SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 3574bc05954SSeongJae Park if (len < 0) 3584bc05954SSeongJae Park return len; 3594bc05954SSeongJae Park 3604bc05954SSeongJae Park return simple_read_from_buffer(buf, count, ppos, ids_buf, len); 3614bc05954SSeongJae Park } 3624bc05954SSeongJae Park 3634bc05954SSeongJae Park /* 3641971bd63SSeongJae Park * Converts a string into an integers array 3654bc05954SSeongJae Park * 3661971bd63SSeongJae Park * Returns an array of integers array if the conversion success, or NULL 3671971bd63SSeongJae Park * otherwise. 3684bc05954SSeongJae Park */ 3691971bd63SSeongJae Park static int *str_to_ints(const char *str, ssize_t len, ssize_t *nr_ints) 3704bc05954SSeongJae Park { 3711971bd63SSeongJae Park int *array; 3721971bd63SSeongJae Park const int max_nr_ints = 32; 3731971bd63SSeongJae Park int nr; 3744bc05954SSeongJae Park int pos = 0, parsed, ret; 3754bc05954SSeongJae Park 3761971bd63SSeongJae Park *nr_ints = 0; 3771971bd63SSeongJae Park array = kmalloc_array(max_nr_ints, sizeof(*array), GFP_KERNEL); 3781971bd63SSeongJae Park if (!array) 3794bc05954SSeongJae Park return NULL; 3801971bd63SSeongJae Park while (*nr_ints < max_nr_ints && pos < len) { 3811971bd63SSeongJae Park ret = sscanf(&str[pos], "%d%n", &nr, &parsed); 3824bc05954SSeongJae Park pos += parsed; 3834bc05954SSeongJae Park if (ret != 1) 3844bc05954SSeongJae Park break; 3851971bd63SSeongJae Park array[*nr_ints] = nr; 3861971bd63SSeongJae Park *nr_ints += 1; 3874bc05954SSeongJae Park } 3884bc05954SSeongJae Park 3891971bd63SSeongJae Park return array; 3904bc05954SSeongJae Park } 3914bc05954SSeongJae Park 3921971bd63SSeongJae Park static void dbgfs_put_pids(struct pid **pids, int nr_pids) 3934bc05954SSeongJae Park { 3944bc05954SSeongJae Park int i; 3954bc05954SSeongJae Park 3961971bd63SSeongJae Park for (i = 0; i < nr_pids; i++) 3971971bd63SSeongJae Park put_pid(pids[i]); 3981971bd63SSeongJae Park } 3991971bd63SSeongJae Park 4001971bd63SSeongJae Park /* 4011971bd63SSeongJae Park * Converts a string into an struct pid pointers array 4021971bd63SSeongJae Park * 4031971bd63SSeongJae Park * Returns an array of struct pid pointers if the conversion success, or NULL 4041971bd63SSeongJae Park * otherwise. 4051971bd63SSeongJae Park */ 4061971bd63SSeongJae Park static struct pid **str_to_pids(const char *str, ssize_t len, ssize_t *nr_pids) 4071971bd63SSeongJae Park { 4081971bd63SSeongJae Park int *ints; 4091971bd63SSeongJae Park ssize_t nr_ints; 4101971bd63SSeongJae Park struct pid **pids; 4111971bd63SSeongJae Park 4121971bd63SSeongJae Park *nr_pids = 0; 4131971bd63SSeongJae Park 4141971bd63SSeongJae Park ints = str_to_ints(str, len, &nr_ints); 4151971bd63SSeongJae Park if (!ints) 4161971bd63SSeongJae Park return NULL; 4171971bd63SSeongJae Park 4181971bd63SSeongJae Park pids = kmalloc_array(nr_ints, sizeof(*pids), GFP_KERNEL); 4191971bd63SSeongJae Park if (!pids) 4201971bd63SSeongJae Park goto out; 4211971bd63SSeongJae Park 4221971bd63SSeongJae Park for (; *nr_pids < nr_ints; (*nr_pids)++) { 4231971bd63SSeongJae Park pids[*nr_pids] = find_get_pid(ints[*nr_pids]); 4241971bd63SSeongJae Park if (!pids[*nr_pids]) { 4251971bd63SSeongJae Park dbgfs_put_pids(pids, *nr_pids); 4261971bd63SSeongJae Park kfree(ints); 4271971bd63SSeongJae Park kfree(pids); 4281971bd63SSeongJae Park return NULL; 4291971bd63SSeongJae Park } 4301971bd63SSeongJae Park } 4311971bd63SSeongJae Park 4321971bd63SSeongJae Park out: 4331971bd63SSeongJae Park kfree(ints); 4341971bd63SSeongJae Park return pids; 4354bc05954SSeongJae Park } 4364bc05954SSeongJae Park 43743642825SSeongJae Park /* 43843642825SSeongJae Park * dbgfs_set_targets() - Set monitoring targets. 43943642825SSeongJae Park * @ctx: monitoring context 4401971bd63SSeongJae Park * @nr_targets: number of targets 4411971bd63SSeongJae Park * @pids: array of target pids (size is same to @nr_targets) 44243642825SSeongJae Park * 4431971bd63SSeongJae Park * This function should not be called while the kdamond is running. @pids is 4441971bd63SSeongJae Park * ignored if the context is not configured to have pid in each target. On 4451971bd63SSeongJae Park * failure, reference counts of all pids in @pids are decremented. 44643642825SSeongJae Park * 44743642825SSeongJae Park * Return: 0 on success, negative error code otherwise. 44843642825SSeongJae Park */ 4491971bd63SSeongJae Park static int dbgfs_set_targets(struct damon_ctx *ctx, ssize_t nr_targets, 4501971bd63SSeongJae Park struct pid **pids) 45143642825SSeongJae Park { 45243642825SSeongJae Park ssize_t i; 45343642825SSeongJae Park struct damon_target *t, *next; 45443642825SSeongJae Park 45543642825SSeongJae Park damon_for_each_target_safe(t, next, ctx) { 456c9e124e0SSeongJae Park if (damon_target_has_pid(ctx)) 4571971bd63SSeongJae Park put_pid(t->pid); 45843642825SSeongJae Park damon_destroy_target(t); 45943642825SSeongJae Park } 46043642825SSeongJae Park 4611971bd63SSeongJae Park for (i = 0; i < nr_targets; i++) { 4621971bd63SSeongJae Park t = damon_new_target(); 46343642825SSeongJae Park if (!t) { 46443642825SSeongJae Park damon_for_each_target_safe(t, next, ctx) 46543642825SSeongJae Park damon_destroy_target(t); 466c9e124e0SSeongJae Park if (damon_target_has_pid(ctx)) 4671971bd63SSeongJae Park dbgfs_put_pids(pids, nr_targets); 46843642825SSeongJae Park return -ENOMEM; 46943642825SSeongJae Park } 470c9e124e0SSeongJae Park if (damon_target_has_pid(ctx)) 4711971bd63SSeongJae Park t->pid = pids[i]; 47243642825SSeongJae Park damon_add_target(ctx, t); 47343642825SSeongJae Park } 47443642825SSeongJae Park 47543642825SSeongJae Park return 0; 47643642825SSeongJae Park } 47743642825SSeongJae Park 4784bc05954SSeongJae Park static ssize_t dbgfs_target_ids_write(struct file *file, 4794bc05954SSeongJae Park const char __user *buf, size_t count, loff_t *ppos) 4804bc05954SSeongJae Park { 4814bc05954SSeongJae Park struct damon_ctx *ctx = file->private_data; 482c026291aSSeongJae Park bool id_is_pid = true; 48370b84808SSeongJae Park char *kbuf; 4841971bd63SSeongJae Park struct pid **target_pids = NULL; 4854bc05954SSeongJae Park ssize_t nr_targets; 4869210622aSRongwei Wang ssize_t ret; 4874bc05954SSeongJae Park 4884bc05954SSeongJae Park kbuf = user_input_str(buf, count, ppos); 4894bc05954SSeongJae Park if (IS_ERR(kbuf)) 4904bc05954SSeongJae Park return PTR_ERR(kbuf); 4914bc05954SSeongJae Park 492c026291aSSeongJae Park if (!strncmp(kbuf, "paddr\n", count)) { 493c026291aSSeongJae Park id_is_pid = false; 4941971bd63SSeongJae Park nr_targets = 1; 4954bc05954SSeongJae Park } 4964bc05954SSeongJae Park 497c026291aSSeongJae Park if (id_is_pid) { 4981971bd63SSeongJae Park target_pids = str_to_pids(kbuf, count, &nr_targets); 4991971bd63SSeongJae Park if (!target_pids) { 5001971bd63SSeongJae Park ret = -ENOMEM; 5011971bd63SSeongJae Park goto out; 5024bc05954SSeongJae Park } 5034bc05954SSeongJae Park } 5044bc05954SSeongJae Park 5054bc05954SSeongJae Park mutex_lock(&ctx->kdamond_lock); 5064bc05954SSeongJae Park if (ctx->kdamond) { 507c026291aSSeongJae Park if (id_is_pid) 5081971bd63SSeongJae Park dbgfs_put_pids(target_pids, nr_targets); 5094bc05954SSeongJae Park ret = -EBUSY; 5104bc05954SSeongJae Park goto unlock_out; 5114bc05954SSeongJae Park } 5124bc05954SSeongJae Park 513ebb3f994SSeongJae Park /* remove previously set targets */ 5141971bd63SSeongJae Park dbgfs_set_targets(ctx, 0, NULL); 515da7aaca0SSeongJae Park if (!nr_targets) { 516da7aaca0SSeongJae Park ret = count; 517da7aaca0SSeongJae Park goto unlock_out; 518da7aaca0SSeongJae Park } 519c026291aSSeongJae Park 520c026291aSSeongJae Park /* Configure the context for the address space type */ 521c026291aSSeongJae Park if (id_is_pid) 522da7aaca0SSeongJae Park ret = damon_select_ops(ctx, DAMON_OPS_VADDR); 523c026291aSSeongJae Park else 524da7aaca0SSeongJae Park ret = damon_select_ops(ctx, DAMON_OPS_PADDR); 525da7aaca0SSeongJae Park if (ret) 526da7aaca0SSeongJae Park goto unlock_out; 527c026291aSSeongJae Park 5281971bd63SSeongJae Park ret = dbgfs_set_targets(ctx, nr_targets, target_pids); 52943642825SSeongJae Park if (!ret) 5309210622aSRongwei Wang ret = count; 5314bc05954SSeongJae Park 5324bc05954SSeongJae Park unlock_out: 5334bc05954SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 5341971bd63SSeongJae Park kfree(target_pids); 5354bc05954SSeongJae Park out: 5364bc05954SSeongJae Park kfree(kbuf); 5374bc05954SSeongJae Park return ret; 5384bc05954SSeongJae Park } 5394bc05954SSeongJae Park 54090bebce9SSeongJae Park static ssize_t sprint_init_regions(struct damon_ctx *c, char *buf, ssize_t len) 54190bebce9SSeongJae Park { 54290bebce9SSeongJae Park struct damon_target *t; 54390bebce9SSeongJae Park struct damon_region *r; 544144760f8SSeongJae Park int target_idx = 0; 54590bebce9SSeongJae Park int written = 0; 54690bebce9SSeongJae Park int rc; 54790bebce9SSeongJae Park 54890bebce9SSeongJae Park damon_for_each_target(t, c) { 54990bebce9SSeongJae Park damon_for_each_region(r, t) { 55090bebce9SSeongJae Park rc = scnprintf(&buf[written], len - written, 551144760f8SSeongJae Park "%d %lu %lu\n", 552144760f8SSeongJae Park target_idx, r->ar.start, r->ar.end); 55390bebce9SSeongJae Park if (!rc) 55490bebce9SSeongJae Park return -ENOMEM; 55590bebce9SSeongJae Park written += rc; 55690bebce9SSeongJae Park } 557144760f8SSeongJae Park target_idx++; 55890bebce9SSeongJae Park } 55990bebce9SSeongJae Park return written; 56090bebce9SSeongJae Park } 56190bebce9SSeongJae Park 56290bebce9SSeongJae Park static ssize_t dbgfs_init_regions_read(struct file *file, char __user *buf, 56390bebce9SSeongJae Park size_t count, loff_t *ppos) 56490bebce9SSeongJae Park { 56590bebce9SSeongJae Park struct damon_ctx *ctx = file->private_data; 56690bebce9SSeongJae Park char *kbuf; 56790bebce9SSeongJae Park ssize_t len; 56890bebce9SSeongJae Park 569db7a347bSSeongJae Park kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN); 57090bebce9SSeongJae Park if (!kbuf) 57190bebce9SSeongJae Park return -ENOMEM; 57290bebce9SSeongJae Park 57390bebce9SSeongJae Park mutex_lock(&ctx->kdamond_lock); 57490bebce9SSeongJae Park if (ctx->kdamond) { 57590bebce9SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 57690bebce9SSeongJae Park len = -EBUSY; 57790bebce9SSeongJae Park goto out; 57890bebce9SSeongJae Park } 57990bebce9SSeongJae Park 58090bebce9SSeongJae Park len = sprint_init_regions(ctx, kbuf, count); 58190bebce9SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 58290bebce9SSeongJae Park if (len < 0) 58390bebce9SSeongJae Park goto out; 58490bebce9SSeongJae Park len = simple_read_from_buffer(buf, count, ppos, kbuf, len); 58590bebce9SSeongJae Park 58690bebce9SSeongJae Park out: 58790bebce9SSeongJae Park kfree(kbuf); 58890bebce9SSeongJae Park return len; 58990bebce9SSeongJae Park } 59090bebce9SSeongJae Park 591144760f8SSeongJae Park static int add_init_region(struct damon_ctx *c, int target_idx, 592144760f8SSeongJae Park struct damon_addr_range *ar) 59390bebce9SSeongJae Park { 59490bebce9SSeongJae Park struct damon_target *t; 59590bebce9SSeongJae Park struct damon_region *r, *prev; 596144760f8SSeongJae Park unsigned long idx = 0; 59790bebce9SSeongJae Park int rc = -EINVAL; 59890bebce9SSeongJae Park 59990bebce9SSeongJae Park if (ar->start >= ar->end) 60090bebce9SSeongJae Park return -EINVAL; 60190bebce9SSeongJae Park 60290bebce9SSeongJae Park damon_for_each_target(t, c) { 603144760f8SSeongJae Park if (idx++ == target_idx) { 60490bebce9SSeongJae Park r = damon_new_region(ar->start, ar->end); 60590bebce9SSeongJae Park if (!r) 60690bebce9SSeongJae Park return -ENOMEM; 60790bebce9SSeongJae Park damon_add_region(r, t); 60890bebce9SSeongJae Park if (damon_nr_regions(t) > 1) { 60990bebce9SSeongJae Park prev = damon_prev_region(r); 61090bebce9SSeongJae Park if (prev->ar.end > r->ar.start) { 61190bebce9SSeongJae Park damon_destroy_region(r, t); 61290bebce9SSeongJae Park return -EINVAL; 61390bebce9SSeongJae Park } 61490bebce9SSeongJae Park } 61590bebce9SSeongJae Park rc = 0; 61690bebce9SSeongJae Park } 61790bebce9SSeongJae Park } 61890bebce9SSeongJae Park return rc; 61990bebce9SSeongJae Park } 62090bebce9SSeongJae Park 62190bebce9SSeongJae Park static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len) 62290bebce9SSeongJae Park { 62390bebce9SSeongJae Park struct damon_target *t; 62490bebce9SSeongJae Park struct damon_region *r, *next; 62590bebce9SSeongJae Park int pos = 0, parsed, ret; 626144760f8SSeongJae Park int target_idx; 62790bebce9SSeongJae Park struct damon_addr_range ar; 62890bebce9SSeongJae Park int err; 62990bebce9SSeongJae Park 63090bebce9SSeongJae Park damon_for_each_target(t, c) { 63190bebce9SSeongJae Park damon_for_each_region_safe(r, next, t) 63290bebce9SSeongJae Park damon_destroy_region(r, t); 63390bebce9SSeongJae Park } 63490bebce9SSeongJae Park 63590bebce9SSeongJae Park while (pos < len) { 636144760f8SSeongJae Park ret = sscanf(&str[pos], "%d %lu %lu%n", 637144760f8SSeongJae Park &target_idx, &ar.start, &ar.end, &parsed); 63890bebce9SSeongJae Park if (ret != 3) 63990bebce9SSeongJae Park break; 640144760f8SSeongJae Park err = add_init_region(c, target_idx, &ar); 64190bebce9SSeongJae Park if (err) 64290bebce9SSeongJae Park goto fail; 64390bebce9SSeongJae Park pos += parsed; 64490bebce9SSeongJae Park } 64590bebce9SSeongJae Park 64690bebce9SSeongJae Park return 0; 64790bebce9SSeongJae Park 64890bebce9SSeongJae Park fail: 64990bebce9SSeongJae Park damon_for_each_target(t, c) { 65090bebce9SSeongJae Park damon_for_each_region_safe(r, next, t) 65190bebce9SSeongJae Park damon_destroy_region(r, t); 65290bebce9SSeongJae Park } 65390bebce9SSeongJae Park return err; 65490bebce9SSeongJae Park } 65590bebce9SSeongJae Park 65690bebce9SSeongJae Park static ssize_t dbgfs_init_regions_write(struct file *file, 65790bebce9SSeongJae Park const char __user *buf, size_t count, 65890bebce9SSeongJae Park loff_t *ppos) 65990bebce9SSeongJae Park { 66090bebce9SSeongJae Park struct damon_ctx *ctx = file->private_data; 66190bebce9SSeongJae Park char *kbuf; 66290bebce9SSeongJae Park ssize_t ret = count; 66390bebce9SSeongJae Park int err; 66490bebce9SSeongJae Park 66590bebce9SSeongJae Park kbuf = user_input_str(buf, count, ppos); 66690bebce9SSeongJae Park if (IS_ERR(kbuf)) 66790bebce9SSeongJae Park return PTR_ERR(kbuf); 66890bebce9SSeongJae Park 66990bebce9SSeongJae Park mutex_lock(&ctx->kdamond_lock); 67090bebce9SSeongJae Park if (ctx->kdamond) { 67190bebce9SSeongJae Park ret = -EBUSY; 67290bebce9SSeongJae Park goto unlock_out; 67390bebce9SSeongJae Park } 67490bebce9SSeongJae Park 67590bebce9SSeongJae Park err = set_init_regions(ctx, kbuf, ret); 67690bebce9SSeongJae Park if (err) 67790bebce9SSeongJae Park ret = err; 67890bebce9SSeongJae Park 67990bebce9SSeongJae Park unlock_out: 68090bebce9SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 68190bebce9SSeongJae Park kfree(kbuf); 68290bebce9SSeongJae Park return ret; 68390bebce9SSeongJae Park } 68490bebce9SSeongJae Park 685429538e8SSeongJae Park static ssize_t dbgfs_kdamond_pid_read(struct file *file, 686429538e8SSeongJae Park char __user *buf, size_t count, loff_t *ppos) 687429538e8SSeongJae Park { 688429538e8SSeongJae Park struct damon_ctx *ctx = file->private_data; 689429538e8SSeongJae Park char *kbuf; 690429538e8SSeongJae Park ssize_t len; 691429538e8SSeongJae Park 692db7a347bSSeongJae Park kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN); 693429538e8SSeongJae Park if (!kbuf) 694429538e8SSeongJae Park return -ENOMEM; 695429538e8SSeongJae Park 696429538e8SSeongJae Park mutex_lock(&ctx->kdamond_lock); 697429538e8SSeongJae Park if (ctx->kdamond) 698429538e8SSeongJae Park len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid); 699429538e8SSeongJae Park else 700429538e8SSeongJae Park len = scnprintf(kbuf, count, "none\n"); 701429538e8SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 702429538e8SSeongJae Park if (!len) 703429538e8SSeongJae Park goto out; 704429538e8SSeongJae Park len = simple_read_from_buffer(buf, count, ppos, kbuf, len); 705429538e8SSeongJae Park 706429538e8SSeongJae Park out: 707429538e8SSeongJae Park kfree(kbuf); 708429538e8SSeongJae Park return len; 709429538e8SSeongJae Park } 710429538e8SSeongJae Park 7114bc05954SSeongJae Park static int damon_dbgfs_open(struct inode *inode, struct file *file) 7124bc05954SSeongJae Park { 7134bc05954SSeongJae Park file->private_data = inode->i_private; 7144bc05954SSeongJae Park 7154bc05954SSeongJae Park return nonseekable_open(inode, file); 7164bc05954SSeongJae Park } 7174bc05954SSeongJae Park 7184bc05954SSeongJae Park static const struct file_operations attrs_fops = { 7194bc05954SSeongJae Park .open = damon_dbgfs_open, 7204bc05954SSeongJae Park .read = dbgfs_attrs_read, 7214bc05954SSeongJae Park .write = dbgfs_attrs_write, 7224bc05954SSeongJae Park }; 7234bc05954SSeongJae Park 724af122dd8SSeongJae Park static const struct file_operations schemes_fops = { 725af122dd8SSeongJae Park .open = damon_dbgfs_open, 726af122dd8SSeongJae Park .read = dbgfs_schemes_read, 727af122dd8SSeongJae Park .write = dbgfs_schemes_write, 728af122dd8SSeongJae Park }; 729af122dd8SSeongJae Park 7304bc05954SSeongJae Park static const struct file_operations target_ids_fops = { 7314bc05954SSeongJae Park .open = damon_dbgfs_open, 7324bc05954SSeongJae Park .read = dbgfs_target_ids_read, 7334bc05954SSeongJae Park .write = dbgfs_target_ids_write, 7344bc05954SSeongJae Park }; 7354bc05954SSeongJae Park 73690bebce9SSeongJae Park static const struct file_operations init_regions_fops = { 73790bebce9SSeongJae Park .open = damon_dbgfs_open, 73890bebce9SSeongJae Park .read = dbgfs_init_regions_read, 73990bebce9SSeongJae Park .write = dbgfs_init_regions_write, 74090bebce9SSeongJae Park }; 74190bebce9SSeongJae Park 742429538e8SSeongJae Park static const struct file_operations kdamond_pid_fops = { 743429538e8SSeongJae Park .open = damon_dbgfs_open, 744429538e8SSeongJae Park .read = dbgfs_kdamond_pid_read, 745429538e8SSeongJae Park }; 746429538e8SSeongJae Park 7474bc05954SSeongJae Park static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx) 7484bc05954SSeongJae Park { 749af122dd8SSeongJae Park const char * const file_names[] = {"attrs", "schemes", "target_ids", 75090bebce9SSeongJae Park "init_regions", "kdamond_pid"}; 751af122dd8SSeongJae Park const struct file_operations *fops[] = {&attrs_fops, &schemes_fops, 75290bebce9SSeongJae Park &target_ids_fops, &init_regions_fops, &kdamond_pid_fops}; 7534bc05954SSeongJae Park int i; 7544bc05954SSeongJae Park 7554bc05954SSeongJae Park for (i = 0; i < ARRAY_SIZE(file_names); i++) 7564bc05954SSeongJae Park debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]); 7574bc05954SSeongJae Park } 7584bc05954SSeongJae Park 759658f9ae7SChangbin Du static void dbgfs_before_terminate(struct damon_ctx *ctx) 7604bc05954SSeongJae Park { 7614bc05954SSeongJae Park struct damon_target *t, *next; 7624bc05954SSeongJae Park 763c9e124e0SSeongJae Park if (!damon_target_has_pid(ctx)) 764658f9ae7SChangbin Du return; 7654bc05954SSeongJae Park 76634796417SSeongJae Park mutex_lock(&ctx->kdamond_lock); 7674bc05954SSeongJae Park damon_for_each_target_safe(t, next, ctx) { 7681971bd63SSeongJae Park put_pid(t->pid); 7694bc05954SSeongJae Park damon_destroy_target(t); 7704bc05954SSeongJae Park } 77134796417SSeongJae Park mutex_unlock(&ctx->kdamond_lock); 7724bc05954SSeongJae Park } 7734bc05954SSeongJae Park 7744bc05954SSeongJae Park static struct damon_ctx *dbgfs_new_ctx(void) 7754bc05954SSeongJae Park { 7764bc05954SSeongJae Park struct damon_ctx *ctx; 7774bc05954SSeongJae Park 7784bc05954SSeongJae Park ctx = damon_new_ctx(); 7794bc05954SSeongJae Park if (!ctx) 7804bc05954SSeongJae Park return NULL; 7814bc05954SSeongJae Park 7824a20865bSSeongJae Park if (damon_select_ops(ctx, DAMON_OPS_VADDR) && 7834a20865bSSeongJae Park damon_select_ops(ctx, DAMON_OPS_PADDR)) { 784da7aaca0SSeongJae Park damon_destroy_ctx(ctx); 785da7aaca0SSeongJae Park return NULL; 786da7aaca0SSeongJae Park } 7874bc05954SSeongJae Park ctx->callback.before_terminate = dbgfs_before_terminate; 7884bc05954SSeongJae Park return ctx; 7894bc05954SSeongJae Park } 7904bc05954SSeongJae Park 79175c1c2b5SSeongJae Park static void dbgfs_destroy_ctx(struct damon_ctx *ctx) 79275c1c2b5SSeongJae Park { 79375c1c2b5SSeongJae Park damon_destroy_ctx(ctx); 79475c1c2b5SSeongJae Park } 79575c1c2b5SSeongJae Park 79675c1c2b5SSeongJae Park /* 79775c1c2b5SSeongJae Park * Make a context of @name and create a debugfs directory for it. 79875c1c2b5SSeongJae Park * 79975c1c2b5SSeongJae Park * This function should be called while holding damon_dbgfs_lock. 80075c1c2b5SSeongJae Park * 80175c1c2b5SSeongJae Park * Returns 0 on success, negative error code otherwise. 80275c1c2b5SSeongJae Park */ 80375c1c2b5SSeongJae Park static int dbgfs_mk_context(char *name) 80475c1c2b5SSeongJae Park { 80575c1c2b5SSeongJae Park struct dentry *root, **new_dirs, *new_dir; 80675c1c2b5SSeongJae Park struct damon_ctx **new_ctxs, *new_ctx; 80775c1c2b5SSeongJae Park 80875c1c2b5SSeongJae Park if (damon_nr_running_ctxs()) 80975c1c2b5SSeongJae Park return -EBUSY; 81075c1c2b5SSeongJae Park 81175c1c2b5SSeongJae Park new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) * 81275c1c2b5SSeongJae Park (dbgfs_nr_ctxs + 1), GFP_KERNEL); 81375c1c2b5SSeongJae Park if (!new_ctxs) 81475c1c2b5SSeongJae Park return -ENOMEM; 81575c1c2b5SSeongJae Park dbgfs_ctxs = new_ctxs; 81675c1c2b5SSeongJae Park 81775c1c2b5SSeongJae Park new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) * 81875c1c2b5SSeongJae Park (dbgfs_nr_ctxs + 1), GFP_KERNEL); 81975c1c2b5SSeongJae Park if (!new_dirs) 82075c1c2b5SSeongJae Park return -ENOMEM; 82175c1c2b5SSeongJae Park dbgfs_dirs = new_dirs; 82275c1c2b5SSeongJae Park 82375c1c2b5SSeongJae Park root = dbgfs_dirs[0]; 82475c1c2b5SSeongJae Park if (!root) 82575c1c2b5SSeongJae Park return -ENOENT; 82675c1c2b5SSeongJae Park 82775c1c2b5SSeongJae Park new_dir = debugfs_create_dir(name, root); 828d26f6070SBadari Pulavarty /* Below check is required for a potential duplicated name case */ 829d26f6070SBadari Pulavarty if (IS_ERR(new_dir)) 830d26f6070SBadari Pulavarty return PTR_ERR(new_dir); 83175c1c2b5SSeongJae Park dbgfs_dirs[dbgfs_nr_ctxs] = new_dir; 83275c1c2b5SSeongJae Park 83375c1c2b5SSeongJae Park new_ctx = dbgfs_new_ctx(); 83475c1c2b5SSeongJae Park if (!new_ctx) { 83575c1c2b5SSeongJae Park debugfs_remove(new_dir); 83675c1c2b5SSeongJae Park dbgfs_dirs[dbgfs_nr_ctxs] = NULL; 83775c1c2b5SSeongJae Park return -ENOMEM; 83875c1c2b5SSeongJae Park } 83975c1c2b5SSeongJae Park 84075c1c2b5SSeongJae Park dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx; 84175c1c2b5SSeongJae Park dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs], 84275c1c2b5SSeongJae Park dbgfs_ctxs[dbgfs_nr_ctxs]); 84375c1c2b5SSeongJae Park dbgfs_nr_ctxs++; 84475c1c2b5SSeongJae Park 84575c1c2b5SSeongJae Park return 0; 84675c1c2b5SSeongJae Park } 84775c1c2b5SSeongJae Park 84875c1c2b5SSeongJae Park static ssize_t dbgfs_mk_context_write(struct file *file, 84975c1c2b5SSeongJae Park const char __user *buf, size_t count, loff_t *ppos) 85075c1c2b5SSeongJae Park { 85175c1c2b5SSeongJae Park char *kbuf; 85275c1c2b5SSeongJae Park char *ctx_name; 8539210622aSRongwei Wang ssize_t ret; 85475c1c2b5SSeongJae Park 85575c1c2b5SSeongJae Park kbuf = user_input_str(buf, count, ppos); 85675c1c2b5SSeongJae Park if (IS_ERR(kbuf)) 85775c1c2b5SSeongJae Park return PTR_ERR(kbuf); 85875c1c2b5SSeongJae Park ctx_name = kmalloc(count + 1, GFP_KERNEL); 85975c1c2b5SSeongJae Park if (!ctx_name) { 86075c1c2b5SSeongJae Park kfree(kbuf); 86175c1c2b5SSeongJae Park return -ENOMEM; 86275c1c2b5SSeongJae Park } 86375c1c2b5SSeongJae Park 86475c1c2b5SSeongJae Park /* Trim white space */ 86575c1c2b5SSeongJae Park if (sscanf(kbuf, "%s", ctx_name) != 1) { 86675c1c2b5SSeongJae Park ret = -EINVAL; 86775c1c2b5SSeongJae Park goto out; 86875c1c2b5SSeongJae Park } 86975c1c2b5SSeongJae Park 87075c1c2b5SSeongJae Park mutex_lock(&damon_dbgfs_lock); 8719210622aSRongwei Wang ret = dbgfs_mk_context(ctx_name); 8729210622aSRongwei Wang if (!ret) 8739210622aSRongwei Wang ret = count; 87475c1c2b5SSeongJae Park mutex_unlock(&damon_dbgfs_lock); 87575c1c2b5SSeongJae Park 87675c1c2b5SSeongJae Park out: 87775c1c2b5SSeongJae Park kfree(kbuf); 87875c1c2b5SSeongJae Park kfree(ctx_name); 87975c1c2b5SSeongJae Park return ret; 88075c1c2b5SSeongJae Park } 88175c1c2b5SSeongJae Park 88275c1c2b5SSeongJae Park /* 88375c1c2b5SSeongJae Park * Remove a context of @name and its debugfs directory. 88475c1c2b5SSeongJae Park * 88575c1c2b5SSeongJae Park * This function should be called while holding damon_dbgfs_lock. 88675c1c2b5SSeongJae Park * 88775c1c2b5SSeongJae Park * Return 0 on success, negative error code otherwise. 88875c1c2b5SSeongJae Park */ 88975c1c2b5SSeongJae Park static int dbgfs_rm_context(char *name) 89075c1c2b5SSeongJae Park { 89175c1c2b5SSeongJae Park struct dentry *root, *dir, **new_dirs; 89275c1c2b5SSeongJae Park struct damon_ctx **new_ctxs; 89375c1c2b5SSeongJae Park int i, j; 8941552fd3eSGreg Kroah-Hartman int ret = 0; 89575c1c2b5SSeongJae Park 89675c1c2b5SSeongJae Park if (damon_nr_running_ctxs()) 89775c1c2b5SSeongJae Park return -EBUSY; 89875c1c2b5SSeongJae Park 89975c1c2b5SSeongJae Park root = dbgfs_dirs[0]; 90075c1c2b5SSeongJae Park if (!root) 90175c1c2b5SSeongJae Park return -ENOENT; 90275c1c2b5SSeongJae Park 90375c1c2b5SSeongJae Park dir = debugfs_lookup(name, root); 90475c1c2b5SSeongJae Park if (!dir) 90575c1c2b5SSeongJae Park return -ENOENT; 90675c1c2b5SSeongJae Park 90775c1c2b5SSeongJae Park new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs), 90875c1c2b5SSeongJae Park GFP_KERNEL); 9091552fd3eSGreg Kroah-Hartman if (!new_dirs) { 9101552fd3eSGreg Kroah-Hartman ret = -ENOMEM; 9111552fd3eSGreg Kroah-Hartman goto out_dput; 9121552fd3eSGreg Kroah-Hartman } 91375c1c2b5SSeongJae Park 91475c1c2b5SSeongJae Park new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs), 91575c1c2b5SSeongJae Park GFP_KERNEL); 91675c1c2b5SSeongJae Park if (!new_ctxs) { 9171552fd3eSGreg Kroah-Hartman ret = -ENOMEM; 9181552fd3eSGreg Kroah-Hartman goto out_new_dirs; 91975c1c2b5SSeongJae Park } 92075c1c2b5SSeongJae Park 92175c1c2b5SSeongJae Park for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) { 92275c1c2b5SSeongJae Park if (dbgfs_dirs[i] == dir) { 92375c1c2b5SSeongJae Park debugfs_remove(dbgfs_dirs[i]); 92475c1c2b5SSeongJae Park dbgfs_destroy_ctx(dbgfs_ctxs[i]); 92575c1c2b5SSeongJae Park continue; 92675c1c2b5SSeongJae Park } 92775c1c2b5SSeongJae Park new_dirs[j] = dbgfs_dirs[i]; 92875c1c2b5SSeongJae Park new_ctxs[j++] = dbgfs_ctxs[i]; 92975c1c2b5SSeongJae Park } 93075c1c2b5SSeongJae Park 93175c1c2b5SSeongJae Park kfree(dbgfs_dirs); 93275c1c2b5SSeongJae Park kfree(dbgfs_ctxs); 93375c1c2b5SSeongJae Park 93475c1c2b5SSeongJae Park dbgfs_dirs = new_dirs; 93575c1c2b5SSeongJae Park dbgfs_ctxs = new_ctxs; 93675c1c2b5SSeongJae Park dbgfs_nr_ctxs--; 93775c1c2b5SSeongJae Park 9381552fd3eSGreg Kroah-Hartman goto out_dput; 9391552fd3eSGreg Kroah-Hartman 9401552fd3eSGreg Kroah-Hartman out_new_dirs: 9411552fd3eSGreg Kroah-Hartman kfree(new_dirs); 9421552fd3eSGreg Kroah-Hartman out_dput: 9431552fd3eSGreg Kroah-Hartman dput(dir); 9441552fd3eSGreg Kroah-Hartman return ret; 94575c1c2b5SSeongJae Park } 94675c1c2b5SSeongJae Park 94775c1c2b5SSeongJae Park static ssize_t dbgfs_rm_context_write(struct file *file, 94875c1c2b5SSeongJae Park const char __user *buf, size_t count, loff_t *ppos) 94975c1c2b5SSeongJae Park { 95075c1c2b5SSeongJae Park char *kbuf; 9519210622aSRongwei Wang ssize_t ret; 95275c1c2b5SSeongJae Park char *ctx_name; 95375c1c2b5SSeongJae Park 95475c1c2b5SSeongJae Park kbuf = user_input_str(buf, count, ppos); 95575c1c2b5SSeongJae Park if (IS_ERR(kbuf)) 95675c1c2b5SSeongJae Park return PTR_ERR(kbuf); 95775c1c2b5SSeongJae Park ctx_name = kmalloc(count + 1, GFP_KERNEL); 95875c1c2b5SSeongJae Park if (!ctx_name) { 95975c1c2b5SSeongJae Park kfree(kbuf); 96075c1c2b5SSeongJae Park return -ENOMEM; 96175c1c2b5SSeongJae Park } 96275c1c2b5SSeongJae Park 96375c1c2b5SSeongJae Park /* Trim white space */ 96475c1c2b5SSeongJae Park if (sscanf(kbuf, "%s", ctx_name) != 1) { 96575c1c2b5SSeongJae Park ret = -EINVAL; 96675c1c2b5SSeongJae Park goto out; 96775c1c2b5SSeongJae Park } 96875c1c2b5SSeongJae Park 96975c1c2b5SSeongJae Park mutex_lock(&damon_dbgfs_lock); 9709210622aSRongwei Wang ret = dbgfs_rm_context(ctx_name); 9719210622aSRongwei Wang if (!ret) 9729210622aSRongwei Wang ret = count; 97375c1c2b5SSeongJae Park mutex_unlock(&damon_dbgfs_lock); 97475c1c2b5SSeongJae Park 97575c1c2b5SSeongJae Park out: 97675c1c2b5SSeongJae Park kfree(kbuf); 97775c1c2b5SSeongJae Park kfree(ctx_name); 97875c1c2b5SSeongJae Park return ret; 97975c1c2b5SSeongJae Park } 98075c1c2b5SSeongJae Park 9814bc05954SSeongJae Park static ssize_t dbgfs_monitor_on_read(struct file *file, 9824bc05954SSeongJae Park char __user *buf, size_t count, loff_t *ppos) 9834bc05954SSeongJae Park { 9844bc05954SSeongJae Park char monitor_on_buf[5]; 9854bc05954SSeongJae Park bool monitor_on = damon_nr_running_ctxs() != 0; 9864bc05954SSeongJae Park int len; 9874bc05954SSeongJae Park 9884bc05954SSeongJae Park len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n"); 9894bc05954SSeongJae Park 9904bc05954SSeongJae Park return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len); 9914bc05954SSeongJae Park } 9924bc05954SSeongJae Park 9934bc05954SSeongJae Park static ssize_t dbgfs_monitor_on_write(struct file *file, 9944bc05954SSeongJae Park const char __user *buf, size_t count, loff_t *ppos) 9954bc05954SSeongJae Park { 9969210622aSRongwei Wang ssize_t ret; 9974bc05954SSeongJae Park char *kbuf; 9984bc05954SSeongJae Park 9994bc05954SSeongJae Park kbuf = user_input_str(buf, count, ppos); 10004bc05954SSeongJae Park if (IS_ERR(kbuf)) 10014bc05954SSeongJae Park return PTR_ERR(kbuf); 10024bc05954SSeongJae Park 10034bc05954SSeongJae Park /* Remove white space */ 10044bc05954SSeongJae Park if (sscanf(kbuf, "%s", kbuf) != 1) { 10054bc05954SSeongJae Park kfree(kbuf); 10064bc05954SSeongJae Park return -EINVAL; 10074bc05954SSeongJae Park } 10084bc05954SSeongJae Park 1009d78f3853SSeongJae Park mutex_lock(&damon_dbgfs_lock); 1010b5ca3e83SXin Hao if (!strncmp(kbuf, "on", count)) { 1011b5ca3e83SXin Hao int i; 1012b5ca3e83SXin Hao 1013b5ca3e83SXin Hao for (i = 0; i < dbgfs_nr_ctxs; i++) { 1014b5ca3e83SXin Hao if (damon_targets_empty(dbgfs_ctxs[i])) { 1015b5ca3e83SXin Hao kfree(kbuf); 1016d78f3853SSeongJae Park mutex_unlock(&damon_dbgfs_lock); 1017b5ca3e83SXin Hao return -EINVAL; 1018b5ca3e83SXin Hao } 1019b5ca3e83SXin Hao } 10208b9b0d33SSeongJae Park ret = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs, true); 1021b5ca3e83SXin Hao } else if (!strncmp(kbuf, "off", count)) { 10229210622aSRongwei Wang ret = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs); 1023b5ca3e83SXin Hao } else { 10249210622aSRongwei Wang ret = -EINVAL; 1025b5ca3e83SXin Hao } 1026d78f3853SSeongJae Park mutex_unlock(&damon_dbgfs_lock); 10274bc05954SSeongJae Park 10289210622aSRongwei Wang if (!ret) 10299210622aSRongwei Wang ret = count; 10304bc05954SSeongJae Park kfree(kbuf); 10314bc05954SSeongJae Park return ret; 10324bc05954SSeongJae Park } 10334bc05954SSeongJae Park 103475c1c2b5SSeongJae Park static const struct file_operations mk_contexts_fops = { 103575c1c2b5SSeongJae Park .write = dbgfs_mk_context_write, 103675c1c2b5SSeongJae Park }; 103775c1c2b5SSeongJae Park 103875c1c2b5SSeongJae Park static const struct file_operations rm_contexts_fops = { 103975c1c2b5SSeongJae Park .write = dbgfs_rm_context_write, 104075c1c2b5SSeongJae Park }; 104175c1c2b5SSeongJae Park 10424bc05954SSeongJae Park static const struct file_operations monitor_on_fops = { 10434bc05954SSeongJae Park .read = dbgfs_monitor_on_read, 10444bc05954SSeongJae Park .write = dbgfs_monitor_on_write, 10454bc05954SSeongJae Park }; 10464bc05954SSeongJae Park 10474bc05954SSeongJae Park static int __init __damon_dbgfs_init(void) 10484bc05954SSeongJae Park { 10494bc05954SSeongJae Park struct dentry *dbgfs_root; 105075c1c2b5SSeongJae Park const char * const file_names[] = {"mk_contexts", "rm_contexts", 105175c1c2b5SSeongJae Park "monitor_on"}; 105275c1c2b5SSeongJae Park const struct file_operations *fops[] = {&mk_contexts_fops, 105375c1c2b5SSeongJae Park &rm_contexts_fops, &monitor_on_fops}; 10544bc05954SSeongJae Park int i; 10554bc05954SSeongJae Park 10564bc05954SSeongJae Park dbgfs_root = debugfs_create_dir("damon", NULL); 10574bc05954SSeongJae Park 10584bc05954SSeongJae Park for (i = 0; i < ARRAY_SIZE(file_names); i++) 10594bc05954SSeongJae Park debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL, 10604bc05954SSeongJae Park fops[i]); 10614bc05954SSeongJae Park dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]); 10624bc05954SSeongJae Park 1063b2d4c646SKenneth Lee dbgfs_dirs = kmalloc(sizeof(dbgfs_root), GFP_KERNEL); 10644bc05954SSeongJae Park if (!dbgfs_dirs) { 10654bc05954SSeongJae Park debugfs_remove(dbgfs_root); 10664bc05954SSeongJae Park return -ENOMEM; 10674bc05954SSeongJae Park } 10684bc05954SSeongJae Park dbgfs_dirs[0] = dbgfs_root; 10694bc05954SSeongJae Park 10704bc05954SSeongJae Park return 0; 10714bc05954SSeongJae Park } 10724bc05954SSeongJae Park 10734bc05954SSeongJae Park /* 10744bc05954SSeongJae Park * Functions for the initialization 10754bc05954SSeongJae Park */ 10764bc05954SSeongJae Park 10774bc05954SSeongJae Park static int __init damon_dbgfs_init(void) 10784bc05954SSeongJae Park { 1079d78f3853SSeongJae Park int rc = -ENOMEM; 10804bc05954SSeongJae Park 1081d78f3853SSeongJae Park mutex_lock(&damon_dbgfs_lock); 10824bc05954SSeongJae Park dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL); 10834bc05954SSeongJae Park if (!dbgfs_ctxs) 1084d78f3853SSeongJae Park goto out; 10854bc05954SSeongJae Park dbgfs_ctxs[0] = dbgfs_new_ctx(); 10864bc05954SSeongJae Park if (!dbgfs_ctxs[0]) { 10874bc05954SSeongJae Park kfree(dbgfs_ctxs); 1088d78f3853SSeongJae Park goto out; 10894bc05954SSeongJae Park } 10904bc05954SSeongJae Park dbgfs_nr_ctxs = 1; 10914bc05954SSeongJae Park 10924bc05954SSeongJae Park rc = __damon_dbgfs_init(); 10934bc05954SSeongJae Park if (rc) { 10944bc05954SSeongJae Park kfree(dbgfs_ctxs[0]); 10954bc05954SSeongJae Park kfree(dbgfs_ctxs); 10964bc05954SSeongJae Park pr_err("%s: dbgfs init failed\n", __func__); 10974bc05954SSeongJae Park } 10984bc05954SSeongJae Park 1099d78f3853SSeongJae Park out: 1100d78f3853SSeongJae Park mutex_unlock(&damon_dbgfs_lock); 11014bc05954SSeongJae Park return rc; 11024bc05954SSeongJae Park } 11034bc05954SSeongJae Park 11044bc05954SSeongJae Park module_init(damon_dbgfs_init); 110517ccae8bSSeongJae Park 110617ccae8bSSeongJae Park #include "dbgfs-test.h" 1107