Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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