Commit 9f7b053a authored by SeongJae Park's avatar SeongJae Park Committed by Linus Torvalds

mm/damon: let monitoring operations can be registered and selected

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.

Link: https://lkml.kernel.org/r/20220215184603.1479-3-sj@kernel.orgSigned-off-by: default avatarSeongJae Park <sj@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Xin Hao <xhao@linux.alibaba.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent f7d911c3
...@@ -253,11 +253,24 @@ struct damos { ...@@ -253,11 +253,24 @@ struct damos {
struct list_head list; 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_ctx;
/** /**
* struct damon_operations - Monitoring operations for given use cases. * struct damon_operations - Monitoring operations for given use cases.
* *
* @id: Identifier of this operations set.
* @init: Initialize operations-related data structures. * @init: Initialize operations-related data structures.
* @update: Update operations-related data structures. * @update: Update operations-related data structures.
* @prepare_access_checks: Prepare next access check of target regions. * @prepare_access_checks: Prepare next access check of target regions.
...@@ -277,6 +290,8 @@ struct damon_ctx; ...@@ -277,6 +290,8 @@ struct damon_ctx;
* &damon_ctx.sample_interval. Finally, @reset_aggregated is called after each * &damon_ctx.sample_interval. Finally, @reset_aggregated is called after each
* &damon_ctx.aggr_interval. * &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, * @init should initialize operations-related data structures. For example,
* this could be used to construct proper monitoring target regions and link * this could be used to construct proper monitoring target regions and link
* those to @damon_ctx.adaptive_targets. * those to @damon_ctx.adaptive_targets.
...@@ -301,6 +316,7 @@ struct damon_ctx; ...@@ -301,6 +316,7 @@ struct damon_ctx;
* @cleanup is called from @kdamond just before its termination. * @cleanup is called from @kdamond just before its termination.
*/ */
struct damon_operations { struct damon_operations {
enum damon_ops_id id;
void (*init)(struct damon_ctx *context); void (*init)(struct damon_ctx *context);
void (*update)(struct damon_ctx *context); void (*update)(struct damon_ctx *context);
void (*prepare_access_checks)(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, ...@@ -489,6 +505,8 @@ int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
int damon_set_schemes(struct damon_ctx *ctx, int damon_set_schemes(struct damon_ctx *ctx,
struct damos **schemes, ssize_t nr_schemes); struct damos **schemes, ssize_t nr_schemes);
int damon_nr_running_ctxs(void); 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_start(struct damon_ctx **ctxs, int nr_ctxs);
int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
......
...@@ -25,6 +25,72 @@ ...@@ -25,6 +25,72 @@
static DEFINE_MUTEX(damon_lock); static DEFINE_MUTEX(damon_lock);
static int nr_running_ctxs; 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 * Construct a damon_region struct
* *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment