lkml.org 
[lkml]   [2020]   [May]   [19]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRe: [PATCH v12 10/16] mm/damon: Add debugfs interface
Date
On Mon, 18 May 2020 12:00:12 +0200 SeongJae Park <sjpark@amazon.com> wrote:

> From: SeongJae Park <sjpark@amazon.de>
>
> This commit adds a debugfs interface for DAMON.
[...]
> diff --git a/mm/damon.c b/mm/damon.c
> index ddd78843f79a..f31310536c79 100644
> --- a/mm/damon.c
> +++ b/mm/damon.c
> @@ -10,6 +10,7 @@
> #define pr_fmt(fmt) "damon: " fmt
>
[...]
> +
> +static ssize_t damon_sprint_pids(struct damon_ctx *ctx, char *buf, ssize_t len)
> +{
> + struct damon_task *t;
> + int written = 0;
> + int rc;
> +
> + damon_for_each_task(t, ctx) {
> + rc = snprintf(&buf[written], len - written, "%d ", t->pid);
> + if (!rc)
> + return -ENOMEM;
> + written += rc;
> + }
> + if (written)
> + written -= 1;
> + written += snprintf(&buf[written], len - written, "\n");
> + return written;
> +}
> +
> +static ssize_t debugfs_pids_read(struct file *file,
> + char __user *buf, size_t count, loff_t *ppos)
> +{
> + struct damon_ctx *ctx = &damon_user_ctx;
> + ssize_t len;
> + char pids_buf[320];
> +
> + len = damon_sprint_pids(ctx, pids_buf, 320);

This could race with concurrent pids debugfs file writers. Should be
synchronized. Same to other debugfs files except 'monitor_on', which is
already synchronized with corresponding writers.

I will enclose this function call with the context mutex in next revision.


Thanks,
SeongJae Park

> + if (len < 0)
> + return len;
> +
> + return simple_read_from_buffer(buf, count, ppos, pids_buf, len);
> +}
> +
> +/*
> + * Converts a string into an array of unsigned long integers
> + *
> + * Returns an array of unsigned long integers if the conversion success, or
> + * NULL otherwise.
> + */
> +static int *str_to_pids(const char *str, ssize_t len, ssize_t *nr_pids)
> +{
> + int *pids;
> + const int max_nr_pids = 32;
> + int pid;
> + int pos = 0, parsed, ret;
> +
> + *nr_pids = 0;
> + pids = kmalloc_array(max_nr_pids, sizeof(pid), GFP_KERNEL);
> + if (!pids)
> + return NULL;
> + while (*nr_pids < max_nr_pids && pos < len) {
> + ret = sscanf(&str[pos], "%d%n", &pid, &parsed);
> + pos += parsed;
> + if (ret != 1)
> + break;
> + pids[*nr_pids] = pid;
> + *nr_pids += 1;
> + }
> + if (*nr_pids == 0) {
> + kfree(pids);
> + pids = NULL;
> + }
> +
> + return pids;
> +}
> +
> +static ssize_t debugfs_pids_write(struct file *file,
> + const char __user *buf, size_t count, loff_t *ppos)
> +{
> + struct damon_ctx *ctx = &damon_user_ctx;
> + char *kbuf;
> + int *targets;
> + ssize_t nr_targets;
> + ssize_t ret;
> + int err;
> +
> + kbuf = kmalloc(count, GFP_KERNEL);
> + if (!kbuf)
> + return -ENOMEM;
> +
> + ret = simple_write_to_buffer(kbuf, count, ppos, buf, count);
> + if (ret < 0)
> + goto out;
> +
> + targets = str_to_pids(kbuf, ret, &nr_targets);
> + if (!targets) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + mutex_lock(&ctx->kdamond_lock);
> + if (ctx->kdamond) {
> + ret = -EINVAL;
> + goto unlock_out;
> + }
> +
> + err = damon_set_pids(ctx, targets, nr_targets);
> + if (err)
> + ret = err;
> +unlock_out:
> + mutex_unlock(&ctx->kdamond_lock);
> + kfree(targets);
> +out:
> + kfree(kbuf);
> + return ret;
> +}
[...]

\
 
 \ /
  Last update: 2020-05-19 16:03    [W:0.141 / U:0.924 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site