From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-23.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87E24C433E0 for ; Mon, 1 Feb 2021 17:38:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4FEC464E8F for ; Mon, 1 Feb 2021 17:38:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232305AbhBARir (ORCPT ); Mon, 1 Feb 2021 12:38:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232276AbhBARic (ORCPT ); Mon, 1 Feb 2021 12:38:32 -0500 Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F19BC06174A for ; Mon, 1 Feb 2021 09:37:52 -0800 (PST) Received: by mail-lj1-x229.google.com with SMTP id b20so12790945ljo.1 for ; Mon, 01 Feb 2021 09:37:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=cW1imzr0TE5kJBE/ANXfpVmRke4mNpObHrkdxIRCbXg=; b=ai2rcrwcWCB9dWRt2sM9bDSwC7sf4hg7HgivyaczH3CBJtKKyAXlGkSd0gZjMGXWK/ Z9sGPqU4mzxc0xCKWDnQhYvyBiEKfinD4Zug2ohoNkedI0rpzl04Qpkloaicy7EAClXc rt6GHla/RJzlR8vT/5m1Hz7jr9xHeRCE/4u88IvA3v+9hl4Xi9bimgKMrI9jhvZKNimP 12OjzB6rOHpaItiUAmPx3Zi6pmfr1yRbXzXAh9fbyfXJJnxXpzNRArGgLfNyPiWI4fwR vZFdm358CADWxOiKVy5J4F6WMz+Hyn8asaTc7LtgUGICN/VjopdSslXBEDWLP+TcYGLn R12Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=cW1imzr0TE5kJBE/ANXfpVmRke4mNpObHrkdxIRCbXg=; b=gSGFbaalRpnp5rzvwXha/IUPNERWOVy4KmfDGIykyx2wkQNup0flSyR/pURY31jvPP NNohhLNN1IHKCgIbBRQnbIneADi0xhoD9jpBAYL/9kE3vJEQc+cLD7ebBj6YBlDoDcTX Sd6TC1vKKJWFlpKqaMRFRLi40zJy9cHqZkIwT2jhfYXCMdJ3a20cjoEY3Npcd0qHrUmM KQCHSorM5KcWhwrr0ZN0rcg5HJ1Z6ydaQxyR/KDxOhZfFksJk/yWFWM2v8kFNX+UFEo/ jIEH3ylxPQmlO3JiHm2QeaPa6ciW6sUHyrSijwJ2Rdr2cwE6PhZLsj3Pea5xlFlFW9bV 5aGA== X-Gm-Message-State: AOAM5334ua2+nw4Spkq3zVIQyZ4FYq76T5pd99WlT0WqclkINsup0GZV MBNfzkVV6T22xrXKHKbwC5npacS5SE0cZUS85jROaA== X-Google-Smtp-Source: ABdhPJy0OyAIx4kblAXsY93R/7eLdNt1LO2BhxLYqqyuQwrXUxL3u8PhllFMRMP8Ro6wEzbqGBbtQ0Z1mxdqTQt48Hs= X-Received: by 2002:a2e:8541:: with SMTP id u1mr11033467ljj.0.1612201070184; Mon, 01 Feb 2021 09:37:50 -0800 (PST) MIME-Version: 1.0 References: <20201215115448.25633-1-sjpark@amazon.com> <20201215115448.25633-8-sjpark@amazon.com> In-Reply-To: <20201215115448.25633-8-sjpark@amazon.com> From: Shakeel Butt Date: Mon, 1 Feb 2021 09:37:39 -0800 Message-ID: Subject: Re: [PATCH v23 07/15] mm/damon: Implement a debugfs-based user space interface To: SeongJae Park Cc: Andrew Morton , SeongJae Park , Jonathan.Cameron@huawei.com, Andrea Arcangeli , acme@kernel.org, alexander.shishkin@linux.intel.com, amit@kernel.org, benh@kernel.crashing.org, brendan.d.gregg@gmail.com, Brendan Higgins , Qian Cai , Colin Ian King , Jonathan Corbet , David Hildenbrand , dwmw@amazon.com, Marco Elver , "Du, Fan" , foersleo@amazon.de, Greg Thelen , Ian Rogers , jolsa@redhat.com, "Kirill A. Shutemov" , Mark Rutland , Mel Gorman , Minchan Kim , Ingo Molnar , namhyung@kernel.org, "Peter Zijlstra (Intel)" , Randy Dunlap , Rik van Riel , David Rientjes , Steven Rostedt , Mike Rapoport , sblbir@amazon.com, Shuah Khan , sj38.park@gmail.com, snu@amazon.de, Vlastimil Babka , Vladimir Davydov , Yang Shi , Huang Ying , zgf574564920@gmail.com, linux-damon@amazon.com, Linux MM , linux-doc@vger.kernel.org, LKML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 15, 2020 at 3:59 AM SeongJae Park wrote: > > From: SeongJae Park > > DAMON is designed to be used by kernel space code such as the memory > management subsystems, and therefore it provides only kernel space API. Which kernel space APIs are being referred here? > That said, letting the user space control DAMON could provide some > benefits to them. For example, it will allow user space to analyze > their specific workloads and make their own special optimizations. > > For such cases, this commit implements a simple DAMON application kernel > module, namely 'damon-dbgfs', which merely wraps the DAMON api and > exports those to the user space via the debugfs. > > 'damon-dbgfs' exports three files, ``attrs``, ``target_ids``, and > ``monitor_on`` under its debugfs directory, ``/damon/``. > > Attributes > ---------- > > Users can read and write the ``sampling interval``, ``aggregation > interval``, ``regions update interval``, and min/max number of > monitoring target regions by reading from and writing to the ``attrs`` > file. For example, below commands set those values to 5 ms, 100 ms, > 1,000 ms, 10, 1000 and check it again:: > > # cd /damon > # echo 5000 100000 1000000 10 1000 > attrs > # cat attrs > 5000 100000 1000000 10 1000 > > Target IDs > ---------- > > Some types of address spaces supports multiple monitoring target. For > example, the virtual memory address spaces monitoring can have multiple > processes as the monitoring targets. Users can set the targets by > writing relevant id values of the targets to, and get the ids of the > current targets by reading from the ``target_ids`` file. In case of the > virtual address spaces monitoring, the values should be pids of the > monitoring target processes. For example, below commands set processes > having pids 42 and 4242 as the monitoring targets and check it again:: > > # cd /damon > # echo 42 4242 > target_ids > # cat target_ids > 42 4242 > > Note that setting the target ids doesn't start the monitoring. > > Turning On/Off > -------------- > > Setting the files as described above doesn't incur effect unless you > explicitly start the monitoring. You can start, stop, and check the > current status of the monitoring by writing to and reading from the > ``monitor_on`` file. Writing ``on`` to the file starts the monitoring > of the targets with the attributes. Writing ``off`` to the file stops > those. DAMON also stops if every targets are invalidated (in case of > the virtual memory monitoring, target processes are invalidated when > terminated). Below example commands turn on, off, and check the status > of DAMON:: > > # cd /damon > # echo on > monitor_on > # echo off > monitor_on > # cat monitor_on > off > > Please note that you cannot write to the above-mentioned debugfs files > while the monitoring is turned on. If you write to the files while > DAMON is running, an error code such as ``-EBUSY`` will be returned. > > Signed-off-by: SeongJae Park > Reviewed-by: Leonard Foerster > --- > include/linux/damon.h | 3 + > mm/damon/Kconfig | 9 ++ > mm/damon/Makefile | 1 + > mm/damon/core.c | 45 ++++++ > mm/damon/dbgfs.c | 366 ++++++++++++++++++++++++++++++++++++++++++ > 5 files changed, 424 insertions(+) > create mode 100644 mm/damon/dbgfs.c > > diff --git a/include/linux/damon.h b/include/linux/damon.h > index 39b4d6d3ddee..f9e0d4349352 100644 > --- a/include/linux/damon.h > +++ b/include/linux/damon.h > @@ -265,9 +265,12 @@ unsigned int damon_nr_regions(struct damon_target *t); > > struct damon_ctx *damon_new_ctx(enum damon_target_type type); > void damon_destroy_ctx(struct damon_ctx *ctx); > +int damon_set_targets(struct damon_ctx *ctx, > + unsigned long *ids, ssize_t nr_ids); > int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, > unsigned long aggr_int, unsigned long regions_update_int, > unsigned long min_nr_reg, unsigned long max_nr_reg); > +int damon_nr_running_ctxs(void); > > int damon_start(struct damon_ctx **ctxs, int nr_ctxs); > int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); > diff --git a/mm/damon/Kconfig b/mm/damon/Kconfig > index 8ae080c52950..72f1683ba0ee 100644 > --- a/mm/damon/Kconfig > +++ b/mm/damon/Kconfig > @@ -21,4 +21,13 @@ config DAMON_VADDR > This builds the default data access monitoring primitives for DAMON > that works for virtual address spaces. > > +config DAMON_DBGFS > + bool "DAMON debugfs interface" > + depends on DAMON_VADDR && DEBUG_FS > + help > + This builds the debugfs interface for DAMON. The user space admins > + can use the interface for arbitrary data access monitoring. > + > + If unsure, say N. > + > endmenu > diff --git a/mm/damon/Makefile b/mm/damon/Makefile > index 6ebbd08aed67..fed4be3bace3 100644 > --- a/mm/damon/Makefile > +++ b/mm/damon/Makefile > @@ -2,3 +2,4 @@ > > obj-$(CONFIG_DAMON) := core.o > obj-$(CONFIG_DAMON_VADDR) += vaddr.o > +obj-$(CONFIG_DAMON_DBGFS) += dbgfs.o > diff --git a/mm/damon/core.c b/mm/damon/core.c > index 5ca9f79ccbb6..b9575a6bebff 100644 > --- a/mm/damon/core.c > +++ b/mm/damon/core.c > @@ -166,6 +166,37 @@ void damon_destroy_ctx(struct damon_ctx *ctx) > kfree(ctx); > } > > +/** > + * damon_set_targets() - Set monitoring targets. > + * @ctx: monitoring context > + * @ids: array of target ids > + * @nr_ids: number of entries in @ids > + * > + * This function should not be called while the kdamond is running. > + * > + * Return: 0 on success, negative error code otherwise. > + */ > +int damon_set_targets(struct damon_ctx *ctx, > + unsigned long *ids, ssize_t nr_ids) > +{ > + ssize_t i; > + struct damon_target *t, *next; > + > + damon_for_each_target_safe(t, next, ctx) > + damon_destroy_target(t); You need to put the reference on the target before destroying. > + > + for (i = 0; i < nr_ids; i++) { > + t = damon_new_target(ids[i]); > + if (!t) { > + pr_err("Failed to alloc damon_target\n"); > + return -ENOMEM; > + } > + damon_add_target(ctx, t); > + } > + > + return 0; > +} > + > /** > * damon_set_attrs() - Set attributes for the monitoring. > * @ctx: monitoring context > @@ -206,6 +237,20 @@ int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, > return 0; > } > > +/** > + * damon_nr_running_ctxs() - Return number of currently running contexts. > + */ > +int damon_nr_running_ctxs(void) > +{ > + int nr_ctxs; > + > + mutex_lock(&damon_lock); > + nr_ctxs = nr_running_ctxs; > + mutex_unlock(&damon_lock); READ_ONCE(nr_running_ctxs) ? > + > + return nr_ctxs; > +} > + > /* Returns the size upper limit for each monitoring region */ > static unsigned long damon_region_sz_limit(struct damon_ctx *ctx) > { > diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c > new file mode 100644 > index 000000000000..fd1665a183c2 > --- /dev/null > +++ b/mm/damon/dbgfs.c > @@ -0,0 +1,366 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * DAMON Debugfs Interface > + * > + * Author: SeongJae Park > + */ > + > +#define pr_fmt(fmt) "damon-dbgfs: " fmt > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +static struct damon_ctx **dbgfs_ctxs; > +static int dbgfs_nr_ctxs; > +static struct dentry **dbgfs_dirs; > + > +/* > + * Returns non-empty string on success, negarive error code otherwise. > + */ > +static char *user_input_str(const char __user *buf, size_t count, loff_t *ppos) > +{ > + char *kbuf; > + ssize_t ret; > + > + /* We do not accept continuous write */ > + if (*ppos) > + return ERR_PTR(-EINVAL); > + > + kbuf = kmalloc(count + 1, GFP_KERNEL); > + if (!kbuf) > + return ERR_PTR(-ENOMEM); > + > + ret = simple_write_to_buffer(kbuf, count + 1, ppos, buf, count); > + if (ret != count) { > + kfree(kbuf); > + return ERR_PTR(-EIO); > + } > + kbuf[ret] = '\0'; > + > + return kbuf; > +} > + > +static ssize_t dbgfs_attrs_read(struct file *file, > + char __user *buf, size_t count, loff_t *ppos) > +{ > + struct damon_ctx *ctx = file->private_data; > + char kbuf[128]; > + int ret; > + > + mutex_lock(&ctx->kdamond_lock); > + ret = scnprintf(kbuf, ARRAY_SIZE(kbuf), "%lu %lu %lu %lu %lu\n", > + ctx->sample_interval, ctx->aggr_interval, > + ctx->regions_update_interval, ctx->min_nr_regions, > + ctx->max_nr_regions); > + mutex_unlock(&ctx->kdamond_lock); > + > + return simple_read_from_buffer(buf, count, ppos, kbuf, ret); > +} > + > +static ssize_t dbgfs_attrs_write(struct file *file, > + const char __user *buf, size_t count, loff_t *ppos) > +{ > + struct damon_ctx *ctx = file->private_data; > + unsigned long s, a, r, minr, maxr; > + char *kbuf; > + ssize_t ret = count; > + int err; > + > + kbuf = user_input_str(buf, count, ppos); > + if (IS_ERR(kbuf)) > + return PTR_ERR(kbuf); > + > + if (sscanf(kbuf, "%lu %lu %lu %lu %lu", > + &s, &a, &r, &minr, &maxr) != 5) { > + ret = -EINVAL; > + goto out; > + } > + > + mutex_lock(&ctx->kdamond_lock); > + if (ctx->kdamond) { > + ret = -EBUSY; > + goto unlock_out; > + } > + > + err = damon_set_attrs(ctx, s, a, r, minr, maxr); > + if (err) > + ret = err; > +unlock_out: > + mutex_unlock(&ctx->kdamond_lock); > +out: > + kfree(kbuf); > + return ret; > +} > + > +#define targetid_is_pid(ctx) \ > + (ctx->primitive.target_valid == damon_va_target_valid) > + > +static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len) > +{ > + struct damon_target *t; > + unsigned long id; > + int written = 0; > + int rc; > + > + damon_for_each_target(t, ctx) { > + id = t->id; > + if (targetid_is_pid(ctx)) > + /* Show pid numbers to debugfs users */ > + id = (unsigned long)pid_vnr((struct pid *)id); > + > + rc = scnprintf(&buf[written], len - written, "%lu ", id); > + if (!rc) > + return -ENOMEM; > + written += rc; > + } > + if (written) > + written -= 1; > + written += scnprintf(&buf[written], len - written, "\n"); > + return written; > +} > + > +static ssize_t dbgfs_target_ids_read(struct file *file, > + char __user *buf, size_t count, loff_t *ppos) > +{ > + struct damon_ctx *ctx = file->private_data; > + ssize_t len; > + char ids_buf[320]; > + > + mutex_lock(&ctx->kdamond_lock); > + len = sprint_target_ids(ctx, ids_buf, 320); > + mutex_unlock(&ctx->kdamond_lock); > + if (len < 0) > + return len; > + > + return simple_read_from_buffer(buf, count, ppos, ids_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 unsigned long *str_to_target_ids(const char *str, ssize_t len, > + ssize_t *nr_ids) > +{ > + unsigned long *ids; > + const int max_nr_ids = 32; > + unsigned long id; > + int pos = 0, parsed, ret; > + > + *nr_ids = 0; > + ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL); > + if (!ids) > + return NULL; > + while (*nr_ids < max_nr_ids && pos < len) { > + ret = sscanf(&str[pos], "%lu%n", &id, &parsed); > + pos += parsed; > + if (ret != 1) > + break; > + ids[*nr_ids] = id; > + *nr_ids += 1; > + } > + > + return ids; > +} > + > +static ssize_t dbgfs_target_ids_write(struct file *file, > + const char __user *buf, size_t count, loff_t *ppos) > +{ > + struct damon_ctx *ctx = file->private_data; > + char *kbuf, *nrs; > + unsigned long *targets; > + ssize_t nr_targets; > + ssize_t ret = count; > + int i; > + int err; > + > + kbuf = user_input_str(buf, count, ppos); > + if (IS_ERR(kbuf)) > + return PTR_ERR(kbuf); > + > + nrs = kbuf; > + > + targets = str_to_target_ids(nrs, ret, &nr_targets); > + if (!targets) { > + ret = -ENOMEM; > + goto out; > + } > + > + if (targetid_is_pid(ctx)) { > + for (i = 0; i < nr_targets; i++) > + targets[i] = (unsigned long)find_get_pid( > + (int)targets[i]); > + } > + > + mutex_lock(&ctx->kdamond_lock); > + if (ctx->kdamond) { > + ret = -EINVAL; > + goto unlock_out; You need to put_pid on the targets array. > + } > + > + err = damon_set_targets(ctx, targets, nr_targets); > + if (err) > + ret = err; You need to handle the partial failure from damon_set_targets(). > +unlock_out: > + mutex_unlock(&ctx->kdamond_lock); > + kfree(targets); > +out: > + kfree(kbuf); > + return ret; > +} > + > +static int damon_dbgfs_open(struct inode *inode, struct file *file) > +{ > + file->private_data = inode->i_private; > + > + return nonseekable_open(inode, file); > +} > + > +static const struct file_operations attrs_fops = { > + .owner = THIS_MODULE, > + .open = damon_dbgfs_open, > + .read = dbgfs_attrs_read, > + .write = dbgfs_attrs_write, > +}; > + > +static const struct file_operations target_ids_fops = { > + .owner = THIS_MODULE, > + .open = damon_dbgfs_open, > + .read = dbgfs_target_ids_read, > + .write = dbgfs_target_ids_write, > +}; > + > +static int dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx) > +{ > + const char * const file_names[] = {"attrs", "target_ids"}; > + const struct file_operations *fops[] = {&attrs_fops, &target_ids_fops}; > + int i; > + > + for (i = 0; i < ARRAY_SIZE(file_names); i++) { > + if (!debugfs_create_file(file_names[i], 0600, dir, > + ctx, fops[i])) { > + pr_err("failed to create %s file\n", file_names[i]); > + return -ENOMEM; > + } > + } > + > + return 0; > +} > + > +static struct damon_ctx *dbgfs_new_ctx(void) > +{ > + struct damon_ctx *ctx; > + > + ctx = damon_new_ctx(DAMON_ADAPTIVE_TARGET); > + if (!ctx) > + return NULL; > + > + damon_va_set_primitives(ctx); > + return ctx; > +} > + > +static ssize_t dbgfs_monitor_on_read(struct file *file, > + char __user *buf, size_t count, loff_t *ppos) > +{ > + char monitor_on_buf[5]; > + bool monitor_on = damon_nr_running_ctxs() != 0; > + int len; > + > + len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n"); > + > + return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len); > +} > + > +static ssize_t dbgfs_monitor_on_write(struct file *file, > + const char __user *buf, size_t count, loff_t *ppos) > +{ > + ssize_t ret = count; > + char *kbuf; > + int err; > + > + kbuf = user_input_str(buf, count, ppos); > + if (IS_ERR(kbuf)) > + return PTR_ERR(kbuf); > + > + /* Remove white space */ > + if (sscanf(kbuf, "%s", kbuf) != 1) { > + kfree(kbuf); > + return -EINVAL; > + } > + > + if (!strncmp(kbuf, "on", count)) > + err = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs); > + else if (!strncmp(kbuf, "off", count)) > + err = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs); > + else > + err = -EINVAL; > + > + if (err) > + ret = err; > + kfree(kbuf); > + return ret; > +} > + > +static const struct file_operations monitor_on_fops = { > + .owner = THIS_MODULE, > + .read = dbgfs_monitor_on_read, > + .write = dbgfs_monitor_on_write, > +}; > + > +static int __init __damon_dbgfs_init(void) > +{ > + struct dentry *dbgfs_root; > + const char * const file_names[] = {"monitor_on"}; > + const struct file_operations *fops[] = {&monitor_on_fops}; > + int i; > + > + dbgfs_root = debugfs_create_dir("damon", NULL); > + if (IS_ERR(dbgfs_root)) { > + pr_err("failed to create the dbgfs dir\n"); > + return PTR_ERR(dbgfs_root); > + } > + > + for (i = 0; i < ARRAY_SIZE(file_names); i++) { > + if (!debugfs_create_file(file_names[i], 0600, dbgfs_root, > + NULL, fops[i])) { > + pr_err("failed to create %s file\n", file_names[i]); > + return -ENOMEM; > + } > + } > + dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]); > + > + dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL); > + dbgfs_dirs[0] = dbgfs_root; > + > + return 0; > +} > + > +/* > + * Functions for the initialization > + */ > + > +static int __init damon_dbgfs_init(void) > +{ > + int rc; > + > + dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL); > + dbgfs_ctxs[0] = dbgfs_new_ctx(); > + if (!dbgfs_ctxs[0]) > + return -ENOMEM; > + dbgfs_nr_ctxs = 1; > + > + rc = __damon_dbgfs_init(); > + if (rc) > + pr_err("%s: dbgfs init failed\n", __func__); > + > + return rc; > +} > + > +module_init(damon_dbgfs_init); > -- > 2.17.1 > From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-23.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14BA8C433DB for ; Mon, 1 Feb 2021 17:37:54 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6AB5F64E8F for ; Mon, 1 Feb 2021 17:37:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6AB5F64E8F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 03C736B006C; Mon, 1 Feb 2021 12:37:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 011C36B006E; Mon, 1 Feb 2021 12:37:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E432B6B0070; Mon, 1 Feb 2021 12:37:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0089.hostedemail.com [216.40.44.89]) by kanga.kvack.org (Postfix) with ESMTP id C839F6B006C for ; Mon, 1 Feb 2021 12:37:52 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 900E0181AEF09 for ; Mon, 1 Feb 2021 17:37:52 +0000 (UTC) X-FDA: 77770406784.05.crook06_1f07fa7275c4 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id 7532C1802CCC8 for ; Mon, 1 Feb 2021 17:37:52 +0000 (UTC) X-HE-Tag: crook06_1f07fa7275c4 X-Filterd-Recvd-Size: 23181 Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) by imf18.hostedemail.com (Postfix) with ESMTP for ; Mon, 1 Feb 2021 17:37:51 +0000 (UTC) Received: by mail-lj1-f174.google.com with SMTP id f2so20637407ljp.11 for ; Mon, 01 Feb 2021 09:37:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=cW1imzr0TE5kJBE/ANXfpVmRke4mNpObHrkdxIRCbXg=; b=ai2rcrwcWCB9dWRt2sM9bDSwC7sf4hg7HgivyaczH3CBJtKKyAXlGkSd0gZjMGXWK/ Z9sGPqU4mzxc0xCKWDnQhYvyBiEKfinD4Zug2ohoNkedI0rpzl04Qpkloaicy7EAClXc rt6GHla/RJzlR8vT/5m1Hz7jr9xHeRCE/4u88IvA3v+9hl4Xi9bimgKMrI9jhvZKNimP 12OjzB6rOHpaItiUAmPx3Zi6pmfr1yRbXzXAh9fbyfXJJnxXpzNRArGgLfNyPiWI4fwR vZFdm358CADWxOiKVy5J4F6WMz+Hyn8asaTc7LtgUGICN/VjopdSslXBEDWLP+TcYGLn R12Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=cW1imzr0TE5kJBE/ANXfpVmRke4mNpObHrkdxIRCbXg=; b=FLoAhVxLhaEXwBOv+vozONlxhkwA7fr/HlNoOkKO917Mr+lIPH/ObAo8YmTddxj+lI Jta+7gVryzZAJkxxXVtz1QFMRPwM3xDTbhQSpbFgYhCUiWRN92G0mlb3avHJNk2Vha3J JYaJ8G3vXTJv3c5xljtJCHhxgOk44+LRzQtwV9mgecVLG4YhZ1kFLRWE4gOdBOxKSizE lyt8+2WXGJCeZoShKhI/270dMfggK12moh96XE0bWmssGTWSWH09RtqeHGojk7KtYYkn hWtK0xDgtTLWuyL4aJw8Vxjlw8M9+oNINo4ZxFokRoZJF/d18jshlhh3r2yJtK0AuM3i wC6A== X-Gm-Message-State: AOAM530uNQ3dxDq32g/HyMjXFDx6/HgFsGnwtnWqpnwgtDsCz6qNE9ZF eN6Ca2tXuF8y2cmJ4FeACTNCA1rv4gBPqbHNJK4/bw== X-Google-Smtp-Source: ABdhPJy0OyAIx4kblAXsY93R/7eLdNt1LO2BhxLYqqyuQwrXUxL3u8PhllFMRMP8Ro6wEzbqGBbtQ0Z1mxdqTQt48Hs= X-Received: by 2002:a2e:8541:: with SMTP id u1mr11033467ljj.0.1612201070184; Mon, 01 Feb 2021 09:37:50 -0800 (PST) MIME-Version: 1.0 References: <20201215115448.25633-1-sjpark@amazon.com> <20201215115448.25633-8-sjpark@amazon.com> In-Reply-To: <20201215115448.25633-8-sjpark@amazon.com> From: Shakeel Butt Date: Mon, 1 Feb 2021 09:37:39 -0800 Message-ID: Subject: Re: [PATCH v23 07/15] mm/damon: Implement a debugfs-based user space interface To: SeongJae Park Cc: Andrew Morton , SeongJae Park , Jonathan.Cameron@huawei.com, Andrea Arcangeli , acme@kernel.org, alexander.shishkin@linux.intel.com, amit@kernel.org, benh@kernel.crashing.org, brendan.d.gregg@gmail.com, Brendan Higgins , Qian Cai , Colin Ian King , Jonathan Corbet , David Hildenbrand , dwmw@amazon.com, Marco Elver , "Du, Fan" , foersleo@amazon.de, Greg Thelen , Ian Rogers , jolsa@redhat.com, "Kirill A. Shutemov" , Mark Rutland , Mel Gorman , Minchan Kim , Ingo Molnar , namhyung@kernel.org, "Peter Zijlstra (Intel)" , Randy Dunlap , Rik van Riel , David Rientjes , Steven Rostedt , Mike Rapoport , sblbir@amazon.com, Shuah Khan , sj38.park@gmail.com, snu@amazon.de, Vlastimil Babka , Vladimir Davydov , Yang Shi , Huang Ying , zgf574564920@gmail.com, linux-damon@amazon.com, Linux MM , linux-doc@vger.kernel.org, LKML Content-Type: text/plain; charset="UTF-8" X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Dec 15, 2020 at 3:59 AM SeongJae Park wrote: > > From: SeongJae Park > > DAMON is designed to be used by kernel space code such as the memory > management subsystems, and therefore it provides only kernel space API. Which kernel space APIs are being referred here? > That said, letting the user space control DAMON could provide some > benefits to them. For example, it will allow user space to analyze > their specific workloads and make their own special optimizations. > > For such cases, this commit implements a simple DAMON application kernel > module, namely 'damon-dbgfs', which merely wraps the DAMON api and > exports those to the user space via the debugfs. > > 'damon-dbgfs' exports three files, ``attrs``, ``target_ids``, and > ``monitor_on`` under its debugfs directory, ``/damon/``. > > Attributes > ---------- > > Users can read and write the ``sampling interval``, ``aggregation > interval``, ``regions update interval``, and min/max number of > monitoring target regions by reading from and writing to the ``attrs`` > file. For example, below commands set those values to 5 ms, 100 ms, > 1,000 ms, 10, 1000 and check it again:: > > # cd /damon > # echo 5000 100000 1000000 10 1000 > attrs > # cat attrs > 5000 100000 1000000 10 1000 > > Target IDs > ---------- > > Some types of address spaces supports multiple monitoring target. For > example, the virtual memory address spaces monitoring can have multiple > processes as the monitoring targets. Users can set the targets by > writing relevant id values of the targets to, and get the ids of the > current targets by reading from the ``target_ids`` file. In case of the > virtual address spaces monitoring, the values should be pids of the > monitoring target processes. For example, below commands set processes > having pids 42 and 4242 as the monitoring targets and check it again:: > > # cd /damon > # echo 42 4242 > target_ids > # cat target_ids > 42 4242 > > Note that setting the target ids doesn't start the monitoring. > > Turning On/Off > -------------- > > Setting the files as described above doesn't incur effect unless you > explicitly start the monitoring. You can start, stop, and check the > current status of the monitoring by writing to and reading from the > ``monitor_on`` file. Writing ``on`` to the file starts the monitoring > of the targets with the attributes. Writing ``off`` to the file stops > those. DAMON also stops if every targets are invalidated (in case of > the virtual memory monitoring, target processes are invalidated when > terminated). Below example commands turn on, off, and check the status > of DAMON:: > > # cd /damon > # echo on > monitor_on > # echo off > monitor_on > # cat monitor_on > off > > Please note that you cannot write to the above-mentioned debugfs files > while the monitoring is turned on. If you write to the files while > DAMON is running, an error code such as ``-EBUSY`` will be returned. > > Signed-off-by: SeongJae Park > Reviewed-by: Leonard Foerster > --- > include/linux/damon.h | 3 + > mm/damon/Kconfig | 9 ++ > mm/damon/Makefile | 1 + > mm/damon/core.c | 45 ++++++ > mm/damon/dbgfs.c | 366 ++++++++++++++++++++++++++++++++++++++++++ > 5 files changed, 424 insertions(+) > create mode 100644 mm/damon/dbgfs.c > > diff --git a/include/linux/damon.h b/include/linux/damon.h > index 39b4d6d3ddee..f9e0d4349352 100644 > --- a/include/linux/damon.h > +++ b/include/linux/damon.h > @@ -265,9 +265,12 @@ unsigned int damon_nr_regions(struct damon_target *t); > > struct damon_ctx *damon_new_ctx(enum damon_target_type type); > void damon_destroy_ctx(struct damon_ctx *ctx); > +int damon_set_targets(struct damon_ctx *ctx, > + unsigned long *ids, ssize_t nr_ids); > int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, > unsigned long aggr_int, unsigned long regions_update_int, > unsigned long min_nr_reg, unsigned long max_nr_reg); > +int damon_nr_running_ctxs(void); > > int damon_start(struct damon_ctx **ctxs, int nr_ctxs); > int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); > diff --git a/mm/damon/Kconfig b/mm/damon/Kconfig > index 8ae080c52950..72f1683ba0ee 100644 > --- a/mm/damon/Kconfig > +++ b/mm/damon/Kconfig > @@ -21,4 +21,13 @@ config DAMON_VADDR > This builds the default data access monitoring primitives for DAMON > that works for virtual address spaces. > > +config DAMON_DBGFS > + bool "DAMON debugfs interface" > + depends on DAMON_VADDR && DEBUG_FS > + help > + This builds the debugfs interface for DAMON. The user space admins > + can use the interface for arbitrary data access monitoring. > + > + If unsure, say N. > + > endmenu > diff --git a/mm/damon/Makefile b/mm/damon/Makefile > index 6ebbd08aed67..fed4be3bace3 100644 > --- a/mm/damon/Makefile > +++ b/mm/damon/Makefile > @@ -2,3 +2,4 @@ > > obj-$(CONFIG_DAMON) := core.o > obj-$(CONFIG_DAMON_VADDR) += vaddr.o > +obj-$(CONFIG_DAMON_DBGFS) += dbgfs.o > diff --git a/mm/damon/core.c b/mm/damon/core.c > index 5ca9f79ccbb6..b9575a6bebff 100644 > --- a/mm/damon/core.c > +++ b/mm/damon/core.c > @@ -166,6 +166,37 @@ void damon_destroy_ctx(struct damon_ctx *ctx) > kfree(ctx); > } > > +/** > + * damon_set_targets() - Set monitoring targets. > + * @ctx: monitoring context > + * @ids: array of target ids > + * @nr_ids: number of entries in @ids > + * > + * This function should not be called while the kdamond is running. > + * > + * Return: 0 on success, negative error code otherwise. > + */ > +int damon_set_targets(struct damon_ctx *ctx, > + unsigned long *ids, ssize_t nr_ids) > +{ > + ssize_t i; > + struct damon_target *t, *next; > + > + damon_for_each_target_safe(t, next, ctx) > + damon_destroy_target(t); You need to put the reference on the target before destroying. > + > + for (i = 0; i < nr_ids; i++) { > + t = damon_new_target(ids[i]); > + if (!t) { > + pr_err("Failed to alloc damon_target\n"); > + return -ENOMEM; > + } > + damon_add_target(ctx, t); > + } > + > + return 0; > +} > + > /** > * damon_set_attrs() - Set attributes for the monitoring. > * @ctx: monitoring context > @@ -206,6 +237,20 @@ int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, > return 0; > } > > +/** > + * damon_nr_running_ctxs() - Return number of currently running contexts. > + */ > +int damon_nr_running_ctxs(void) > +{ > + int nr_ctxs; > + > + mutex_lock(&damon_lock); > + nr_ctxs = nr_running_ctxs; > + mutex_unlock(&damon_lock); READ_ONCE(nr_running_ctxs) ? > + > + return nr_ctxs; > +} > + > /* Returns the size upper limit for each monitoring region */ > static unsigned long damon_region_sz_limit(struct damon_ctx *ctx) > { > diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c > new file mode 100644 > index 000000000000..fd1665a183c2 > --- /dev/null > +++ b/mm/damon/dbgfs.c > @@ -0,0 +1,366 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * DAMON Debugfs Interface > + * > + * Author: SeongJae Park > + */ > + > +#define pr_fmt(fmt) "damon-dbgfs: " fmt > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +static struct damon_ctx **dbgfs_ctxs; > +static int dbgfs_nr_ctxs; > +static struct dentry **dbgfs_dirs; > + > +/* > + * Returns non-empty string on success, negarive error code otherwise. > + */ > +static char *user_input_str(const char __user *buf, size_t count, loff_t *ppos) > +{ > + char *kbuf; > + ssize_t ret; > + > + /* We do not accept continuous write */ > + if (*ppos) > + return ERR_PTR(-EINVAL); > + > + kbuf = kmalloc(count + 1, GFP_KERNEL); > + if (!kbuf) > + return ERR_PTR(-ENOMEM); > + > + ret = simple_write_to_buffer(kbuf, count + 1, ppos, buf, count); > + if (ret != count) { > + kfree(kbuf); > + return ERR_PTR(-EIO); > + } > + kbuf[ret] = '\0'; > + > + return kbuf; > +} > + > +static ssize_t dbgfs_attrs_read(struct file *file, > + char __user *buf, size_t count, loff_t *ppos) > +{ > + struct damon_ctx *ctx = file->private_data; > + char kbuf[128]; > + int ret; > + > + mutex_lock(&ctx->kdamond_lock); > + ret = scnprintf(kbuf, ARRAY_SIZE(kbuf), "%lu %lu %lu %lu %lu\n", > + ctx->sample_interval, ctx->aggr_interval, > + ctx->regions_update_interval, ctx->min_nr_regions, > + ctx->max_nr_regions); > + mutex_unlock(&ctx->kdamond_lock); > + > + return simple_read_from_buffer(buf, count, ppos, kbuf, ret); > +} > + > +static ssize_t dbgfs_attrs_write(struct file *file, > + const char __user *buf, size_t count, loff_t *ppos) > +{ > + struct damon_ctx *ctx = file->private_data; > + unsigned long s, a, r, minr, maxr; > + char *kbuf; > + ssize_t ret = count; > + int err; > + > + kbuf = user_input_str(buf, count, ppos); > + if (IS_ERR(kbuf)) > + return PTR_ERR(kbuf); > + > + if (sscanf(kbuf, "%lu %lu %lu %lu %lu", > + &s, &a, &r, &minr, &maxr) != 5) { > + ret = -EINVAL; > + goto out; > + } > + > + mutex_lock(&ctx->kdamond_lock); > + if (ctx->kdamond) { > + ret = -EBUSY; > + goto unlock_out; > + } > + > + err = damon_set_attrs(ctx, s, a, r, minr, maxr); > + if (err) > + ret = err; > +unlock_out: > + mutex_unlock(&ctx->kdamond_lock); > +out: > + kfree(kbuf); > + return ret; > +} > + > +#define targetid_is_pid(ctx) \ > + (ctx->primitive.target_valid == damon_va_target_valid) > + > +static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len) > +{ > + struct damon_target *t; > + unsigned long id; > + int written = 0; > + int rc; > + > + damon_for_each_target(t, ctx) { > + id = t->id; > + if (targetid_is_pid(ctx)) > + /* Show pid numbers to debugfs users */ > + id = (unsigned long)pid_vnr((struct pid *)id); > + > + rc = scnprintf(&buf[written], len - written, "%lu ", id); > + if (!rc) > + return -ENOMEM; > + written += rc; > + } > + if (written) > + written -= 1; > + written += scnprintf(&buf[written], len - written, "\n"); > + return written; > +} > + > +static ssize_t dbgfs_target_ids_read(struct file *file, > + char __user *buf, size_t count, loff_t *ppos) > +{ > + struct damon_ctx *ctx = file->private_data; > + ssize_t len; > + char ids_buf[320]; > + > + mutex_lock(&ctx->kdamond_lock); > + len = sprint_target_ids(ctx, ids_buf, 320); > + mutex_unlock(&ctx->kdamond_lock); > + if (len < 0) > + return len; > + > + return simple_read_from_buffer(buf, count, ppos, ids_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 unsigned long *str_to_target_ids(const char *str, ssize_t len, > + ssize_t *nr_ids) > +{ > + unsigned long *ids; > + const int max_nr_ids = 32; > + unsigned long id; > + int pos = 0, parsed, ret; > + > + *nr_ids = 0; > + ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL); > + if (!ids) > + return NULL; > + while (*nr_ids < max_nr_ids && pos < len) { > + ret = sscanf(&str[pos], "%lu%n", &id, &parsed); > + pos += parsed; > + if (ret != 1) > + break; > + ids[*nr_ids] = id; > + *nr_ids += 1; > + } > + > + return ids; > +} > + > +static ssize_t dbgfs_target_ids_write(struct file *file, > + const char __user *buf, size_t count, loff_t *ppos) > +{ > + struct damon_ctx *ctx = file->private_data; > + char *kbuf, *nrs; > + unsigned long *targets; > + ssize_t nr_targets; > + ssize_t ret = count; > + int i; > + int err; > + > + kbuf = user_input_str(buf, count, ppos); > + if (IS_ERR(kbuf)) > + return PTR_ERR(kbuf); > + > + nrs = kbuf; > + > + targets = str_to_target_ids(nrs, ret, &nr_targets); > + if (!targets) { > + ret = -ENOMEM; > + goto out; > + } > + > + if (targetid_is_pid(ctx)) { > + for (i = 0; i < nr_targets; i++) > + targets[i] = (unsigned long)find_get_pid( > + (int)targets[i]); > + } > + > + mutex_lock(&ctx->kdamond_lock); > + if (ctx->kdamond) { > + ret = -EINVAL; > + goto unlock_out; You need to put_pid on the targets array. > + } > + > + err = damon_set_targets(ctx, targets, nr_targets); > + if (err) > + ret = err; You need to handle the partial failure from damon_set_targets(). > +unlock_out: > + mutex_unlock(&ctx->kdamond_lock); > + kfree(targets); > +out: > + kfree(kbuf); > + return ret; > +} > + > +static int damon_dbgfs_open(struct inode *inode, struct file *file) > +{ > + file->private_data = inode->i_private; > + > + return nonseekable_open(inode, file); > +} > + > +static const struct file_operations attrs_fops = { > + .owner = THIS_MODULE, > + .open = damon_dbgfs_open, > + .read = dbgfs_attrs_read, > + .write = dbgfs_attrs_write, > +}; > + > +static const struct file_operations target_ids_fops = { > + .owner = THIS_MODULE, > + .open = damon_dbgfs_open, > + .read = dbgfs_target_ids_read, > + .write = dbgfs_target_ids_write, > +}; > + > +static int dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx) > +{ > + const char * const file_names[] = {"attrs", "target_ids"}; > + const struct file_operations *fops[] = {&attrs_fops, &target_ids_fops}; > + int i; > + > + for (i = 0; i < ARRAY_SIZE(file_names); i++) { > + if (!debugfs_create_file(file_names[i], 0600, dir, > + ctx, fops[i])) { > + pr_err("failed to create %s file\n", file_names[i]); > + return -ENOMEM; > + } > + } > + > + return 0; > +} > + > +static struct damon_ctx *dbgfs_new_ctx(void) > +{ > + struct damon_ctx *ctx; > + > + ctx = damon_new_ctx(DAMON_ADAPTIVE_TARGET); > + if (!ctx) > + return NULL; > + > + damon_va_set_primitives(ctx); > + return ctx; > +} > + > +static ssize_t dbgfs_monitor_on_read(struct file *file, > + char __user *buf, size_t count, loff_t *ppos) > +{ > + char monitor_on_buf[5]; > + bool monitor_on = damon_nr_running_ctxs() != 0; > + int len; > + > + len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n"); > + > + return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len); > +} > + > +static ssize_t dbgfs_monitor_on_write(struct file *file, > + const char __user *buf, size_t count, loff_t *ppos) > +{ > + ssize_t ret = count; > + char *kbuf; > + int err; > + > + kbuf = user_input_str(buf, count, ppos); > + if (IS_ERR(kbuf)) > + return PTR_ERR(kbuf); > + > + /* Remove white space */ > + if (sscanf(kbuf, "%s", kbuf) != 1) { > + kfree(kbuf); > + return -EINVAL; > + } > + > + if (!strncmp(kbuf, "on", count)) > + err = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs); > + else if (!strncmp(kbuf, "off", count)) > + err = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs); > + else > + err = -EINVAL; > + > + if (err) > + ret = err; > + kfree(kbuf); > + return ret; > +} > + > +static const struct file_operations monitor_on_fops = { > + .owner = THIS_MODULE, > + .read = dbgfs_monitor_on_read, > + .write = dbgfs_monitor_on_write, > +}; > + > +static int __init __damon_dbgfs_init(void) > +{ > + struct dentry *dbgfs_root; > + const char * const file_names[] = {"monitor_on"}; > + const struct file_operations *fops[] = {&monitor_on_fops}; > + int i; > + > + dbgfs_root = debugfs_create_dir("damon", NULL); > + if (IS_ERR(dbgfs_root)) { > + pr_err("failed to create the dbgfs dir\n"); > + return PTR_ERR(dbgfs_root); > + } > + > + for (i = 0; i < ARRAY_SIZE(file_names); i++) { > + if (!debugfs_create_file(file_names[i], 0600, dbgfs_root, > + NULL, fops[i])) { > + pr_err("failed to create %s file\n", file_names[i]); > + return -ENOMEM; > + } > + } > + dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]); > + > + dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL); > + dbgfs_dirs[0] = dbgfs_root; > + > + return 0; > +} > + > +/* > + * Functions for the initialization > + */ > + > +static int __init damon_dbgfs_init(void) > +{ > + int rc; > + > + dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL); > + dbgfs_ctxs[0] = dbgfs_new_ctx(); > + if (!dbgfs_ctxs[0]) > + return -ENOMEM; > + dbgfs_nr_ctxs = 1; > + > + rc = __damon_dbgfs_init(); > + if (rc) > + pr_err("%s: dbgfs init failed\n", __func__); > + > + return rc; > +} > + > +module_init(damon_dbgfs_init); > -- > 2.17.1 >