From: SeongJae Park <sj@kernel.org>
To: akpm@linux-foundation.org
Cc: rientjes@google.com, xhao@linux.alibaba.com,
linux-damon@amazon.com, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, SeongJae Park <sj@kernel.org>
Subject: [RFC PATCH 3/4] mm/damon/sysfs: Link DAMON to 'state' file read/write functions
Date: Thu, 17 Feb 2022 16:19:37 +0000 [thread overview]
Message-ID: <20220217161938.8874-4-sj@kernel.org> (raw)
In-Reply-To: <20220217161938.8874-1-sj@kernel.org>
This commit implements 'state' DAMON sysfs interface file read/write
functions. In detail, writing 'on' or 'off' to the 'state' file turns
DAMON for the context on or off, accordingly. Reading the file shows
the state.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
mm/damon/sysfs.c | 166 +++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 162 insertions(+), 4 deletions(-)
diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index 59bdc7268dc6..721ee086265f 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -793,18 +793,176 @@ static void damon_sysfs_kdamond_rm_dirs(struct damon_sysfs_kdamond *kdamond)
kobject_put(&kdamond->contexts->kobj);
}
+static bool damon_sysfs_ctx_running(struct damon_ctx *ctx)
+{
+ bool running;
+
+ mutex_lock(&ctx->kdamond_lock);
+ running = ctx->kdamond != NULL;
+ mutex_unlock(&ctx->kdamond_lock);
+ return running;
+}
+
static ssize_t damon_sysfs_kdamond_state_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
- /* TODO: Link with DAMON */
- return sysfs_emit(buf, "off\n");
+ struct damon_sysfs_kdamond *kdamond = container_of(kobj,
+ struct damon_sysfs_kdamond, kobj);
+ struct damon_ctx *ctx = kdamond->damon_ctx;
+ bool running;
+
+ if (!ctx)
+ running = false;
+ else
+ running = damon_sysfs_ctx_running(ctx);
+
+ return sysfs_emit(buf, "%s\n", running ? "on" : "off");
+}
+
+static void damon_sysfs_destroy_targets(struct damon_ctx *ctx)
+{
+ struct damon_target *t, *next;
+
+ damon_for_each_target_safe(t, next, ctx) {
+ if (ctx->ops.id == DAMON_OPS_VADDR)
+ put_pid(t->pid);
+ damon_destroy_target(t);
+ }
+}
+
+static int damon_sysfs_set_targets(struct damon_ctx *ctx,
+ struct damon_sysfs_context *sysfs_ctx)
+{
+ struct damon_sysfs_targets *targets = sysfs_ctx->targets;
+ int i;
+
+ for (i = 0; i < targets->nr_targets; i++) {
+ struct damon_target *t;
+
+ t = damon_new_target();
+ if (!t) {
+ damon_sysfs_destroy_targets(ctx);
+ return -ENOMEM;
+ }
+ if (ctx->ops.id == DAMON_OPS_VADDR) {
+ t->pid = find_get_pid(targets->targets_arr[i]->pid);
+ if (!t->pid) {
+ damon_sysfs_destroy_targets(ctx);
+ return -EINVAL;
+ }
+ }
+ damon_add_target(ctx, t);
+ }
+ return 0;
+}
+
+static inline bool target_has_pid(const struct damon_ctx *ctx)
+{
+ return ctx->ops.id == DAMON_OPS_VADDR;
+}
+
+static void damon_sysfs_before_terminate(struct damon_ctx *ctx)
+{
+ struct damon_target *t, *next;
+
+ if (!target_has_pid(ctx))
+ return;
+
+ mutex_lock(&ctx->kdamond_lock);
+ damon_for_each_target_safe(t, next, ctx) {
+ put_pid(t->pid);
+ damon_destroy_target(t);
+ }
+ mutex_unlock(&ctx->kdamond_lock);
+}
+
+static struct damon_ctx *damon_sysfs_build_ctx(
+ struct damon_sysfs_context *sys_ctx)
+{
+ struct damon_ctx *ctx = damon_new_ctx();
+ struct damon_sysfs_attrs *sys_attrs = sys_ctx->attrs;
+ struct damon_sysfs_ul_range *sys_nr_regions = sys_attrs->nr_regions;
+ struct damon_sysfs_intervals *sys_intervals = sys_attrs->intervals;
+ int err;
+
+ if (!ctx)
+ return ERR_PTR(-ENOMEM);
+
+ err = damon_select_ops(ctx, sys_ctx->ops_id);
+ if (err)
+ goto out;
+
+ err = damon_set_attrs(ctx, sys_intervals->sample_us,
+ sys_intervals->aggr_us, sys_intervals->update_us,
+ sys_nr_regions->min, sys_nr_regions->max);
+ if (err)
+ goto out;
+ err = damon_sysfs_set_targets(ctx, sys_ctx);
+ if (err)
+ goto out;
+ ctx->callback.before_terminate = damon_sysfs_before_terminate;
+ return ctx;
+
+out:
+ damon_destroy_ctx(ctx);
+ return ERR_PTR(err);
}
static ssize_t damon_sysfs_kdamond_state_store(struct kobject *kobj,
struct kobj_attribute *attr, const char *buf, size_t count)
{
- /* TODO: Link with DAMON */
- return count;
+ struct damon_sysfs_kdamond *kdamond = container_of(kobj,
+ struct damon_sysfs_kdamond, kobj);
+ struct damon_ctx *ctx;
+ ssize_t ret;
+
+ if (!mutex_trylock(&damon_sysfs_lock))
+ return -EBUSY;
+ if (!strncmp(buf, "on\n", count)) {
+ if (kdamond->damon_ctx &&
+ damon_sysfs_ctx_running(kdamond->damon_ctx)) {
+ ret = -EBUSY;
+ goto out;
+ }
+ if (kdamond->contexts->nr != 1) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ if (kdamond->damon_ctx)
+ damon_destroy_ctx(kdamond->damon_ctx);
+ kdamond->damon_ctx = NULL;
+
+ ctx = damon_sysfs_build_ctx(
+ kdamond->contexts->contexts_arr[0]);
+ if (IS_ERR(ctx)) {
+ ret = PTR_ERR(ctx);
+ goto out;
+ }
+ ret = damon_start(&ctx, 1, false);
+ if (ret) {
+ damon_destroy_ctx(ctx);
+ goto out;
+ }
+ kdamond->damon_ctx = ctx;
+ } else if (!strncmp(buf, "off\n", count)) {
+ if (!kdamond->damon_ctx) {
+ ret = -EINVAL;
+ goto out;
+ }
+ ret = damon_stop(&kdamond->damon_ctx, 1);
+ /*
+ * kdamond->damon_ctx will be freed in next on, or
+ * kdamonds_nr_store()
+ */
+ } else {
+ ret = -EINVAL;
+ }
+out:
+ mutex_unlock(&damon_sysfs_lock);
+ if (!ret)
+ ret = count;
+ return ret;
}
static ssize_t damon_sysfs_kdamond_pid_show(struct kobject *kobj,
--
2.17.1
next prev parent reply other threads:[~2022-02-17 16:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-17 16:19 [RFC PATCH 0/4] Introduce DAMON sysfs interface SeongJae Park
2022-02-17 16:19 ` [RFC PATCH 1/4] mm/damon: Implement a sysfs-based DAMON user interface SeongJae Park
2022-02-17 16:19 ` [RFC PATCH 2/4] mm/damon/core: Allow non-exclusive DAMON start/stop SeongJae Park
2022-02-17 16:19 ` SeongJae Park [this message]
2022-02-17 16:19 ` [RFC PATCH 4/4] selftests/damon: Add a test for DAMON sysfs interface SeongJae Park
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220217161938.8874-4-sj@kernel.org \
--to=sj@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=linux-damon@amazon.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rientjes@google.com \
--cc=xhao@linux.alibaba.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).