From: SeongJae Park <sj@kernel.org>
To: akpm@linux-foundation.org
Cc: xhao@linux.alibaba.com, rientjes@google.com, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, SeongJae Park <sj@kernel.org>
Subject: [PATCH 2/8] mm/damon: Let monitoring operations can be registered and selected
Date: Tue, 15 Feb 2022 18:45:57 +0000 [thread overview]
Message-ID: <20220215184603.1479-3-sj@kernel.org> (raw)
In-Reply-To: <20220215184603.1479-1-sj@kernel.org>
In-kernel DAMON user code like DAMON debugfs interface should set
'struct damon_operations' of its 'struct damon_ctx' on its own.
Therefore, the client code should depend on all supporting monitoring
operations implementations that it could use. For example, DAMON
debugfs interface depends on both vaddr and paddr, while some of the
users are not always interested in both.
To minimize such unnecessary dependencies, this commit makes the
monitoring operations can be registered by implementing code and
then dynamically selected by the user code without build-time
dependency.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
include/linux/damon.h | 18 ++++++++++++
mm/damon/core.c | 66 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+)
diff --git a/include/linux/damon.h b/include/linux/damon.h
index 00baeb42c18e..076da277b249 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -253,11 +253,24 @@ struct damos {
struct list_head list;
};
+/**
+ * enum damon_ops_id - Identifier for each monitoring operations implementation
+ *
+ * @DAMON_OPS_VADDR: Monitoring operations for virtual address spaces
+ * @DAMON_OPS_PADDR: Monitoring operations for the physical address space
+ */
+enum damon_ops_id {
+ DAMON_OPS_VADDR,
+ DAMON_OPS_PADDR,
+ NR_DAMON_OPS,
+};
+
struct damon_ctx;
/**
* struct damon_operations - Monitoring operations for given use cases.
*
+ * @id: Identifier of this operations set.
* @init: Initialize operations-related data structures.
* @update: Update operations-related data structures.
* @prepare_access_checks: Prepare next access check of target regions.
@@ -277,6 +290,8 @@ struct damon_ctx;
* &damon_ctx.sample_interval. Finally, @reset_aggregated is called after each
* &damon_ctx.aggr_interval.
*
+ * Each &struct damon_operations instance having valid @id can be registered
+ * via damon_register_ops() and selected by damon_select_ops() later.
* @init should initialize operations-related data structures. For example,
* this could be used to construct proper monitoring target regions and link
* those to @damon_ctx.adaptive_targets.
@@ -301,6 +316,7 @@ struct damon_ctx;
* @cleanup is called from @kdamond just before its termination.
*/
struct damon_operations {
+ enum damon_ops_id id;
void (*init)(struct damon_ctx *context);
void (*update)(struct damon_ctx *context);
void (*prepare_access_checks)(struct damon_ctx *context);
@@ -489,6 +505,8 @@ int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
int damon_set_schemes(struct damon_ctx *ctx,
struct damos **schemes, ssize_t nr_schemes);
int damon_nr_running_ctxs(void);
+int damon_register_ops(struct damon_operations *ops);
+int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id);
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/core.c b/mm/damon/core.c
index be93fb1c3473..82e0a4620c4f 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -25,6 +25,72 @@
static DEFINE_MUTEX(damon_lock);
static int nr_running_ctxs;
+static DEFINE_MUTEX(damon_ops_lock);
+static struct damon_operations damon_registered_ops[NR_DAMON_OPS];
+
+/* Should be called under damon_ops_lock with id smaller than NR_DAMON_OPS */
+static bool damon_registered_ops_id(enum damon_ops_id id)
+{
+ struct damon_operations empty_ops = {};
+
+ if (!memcmp(&empty_ops, &damon_registered_ops[id], sizeof(empty_ops)))
+ return false;
+ return true;
+}
+
+/**
+ * damon_register_ops() - Register a monitoring operations set to DAMON.
+ * @ops: monitoring operations set to register.
+ *
+ * This function registers a monitoring operations set of valid &struct
+ * damon_operations->id so that others can find and use them later.
+ *
+ * Return: 0 on success, negative error code otherwise.
+ */
+int damon_register_ops(struct damon_operations *ops)
+{
+ int err = 0;
+
+ if (ops->id >= NR_DAMON_OPS)
+ return -EINVAL;
+ mutex_lock(&damon_ops_lock);
+ /* Fail for already registered ops */
+ if (damon_registered_ops_id(ops->id)) {
+ err = -EINVAL;
+ goto out;
+ }
+ damon_registered_ops[ops->id] = *ops;
+out:
+ mutex_unlock(&damon_ops_lock);
+ return err;
+}
+
+/**
+ * damon_select_ops() - Select a monitoring operations to use with the context.
+ * @ctx: monitoring context to use the operations.
+ * @id: id of the registered monitoring operations to select.
+ *
+ * This function finds registered monitoring operations set of @id and make
+ * @ctx to use it.
+ *
+ * Return: 0 on success, negative error code otherwise.
+ */
+int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id)
+{
+ int err = 0;
+
+ if (id >= NR_DAMON_OPS)
+ return -EINVAL;
+
+ mutex_lock(&damon_ops_lock);
+ if (!damon_registered_ops_id(id))
+ err = -EINVAL;
+ else
+ ctx->ops = damon_registered_ops[id];
+ mutex_unlock(&damon_ops_lock);
+ return err;
+}
+
/*
* Construct a damon_region struct
*
--
2.17.1
next prev parent reply other threads:[~2022-02-15 18:46 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-15 18:45 [PATCH 0/8] Allow DAMON user code independent of monitoring primitives SeongJae Park
2022-02-15 18:45 ` [PATCH 1/8] mm/damon: Rename damon_primitives to damon_operations SeongJae Park
2022-02-15 18:45 ` SeongJae Park [this message]
2022-02-15 18:45 ` [PATCH 3/8] mm/damon/paddr,vaddr: Register themselves to DAMON in subsys_initcall SeongJae Park
2022-02-15 18:45 ` [PATCH 4/8] mm/damon/reclaim: Use damon_select_ops() instead of damon_{v,p}a_set_operations() SeongJae Park
2022-02-15 18:46 ` [PATCH 5/8] mm/damon/dbgfs: " SeongJae Park
2022-02-15 18:46 ` [PATCH 6/8] mm/damon/dbgfs: Use operations id for knowing if the target has pid SeongJae Park
2022-02-15 18:46 ` [PATCH 7/8] mm/damon/dbgfs-test: Fix is_target_id() change SeongJae Park
2022-02-15 18:46 ` [PATCH 8/8] mm/damon/paddr,vaddr: Remove damon_{p,v}a_{target_valid,set_operations}() 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=20220215184603.1479-3-sj@kernel.org \
--to=sj@kernel.org \
--cc=akpm@linux-foundation.org \
--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