Commit da878780 authored by SeongJae Park's avatar SeongJae Park Committed by Andrew Morton

mm/damon/sysfs: support online inputs update

Currently, DAMON sysfs interface doesn't provide a way for adjusting DAMON
input parameters while it is turned on.  Therefore, users who want to
reconfigure DAMON need to stop DAMON and restart.  This means all the
monitoring results that accumulated so far, which could be useful, should
be flushed.  This would be inefficient for many cases.

For an example, let's suppose a sysadmin was running a DAMON-based
Operation Scheme to find memory regions not accessed for more than 5 mins
and page out the regions.  If it turns out the 5 mins threshold was too
long and therefore the sysadmin wants to reduce it to 4 mins, the sysadmin
should turn off DAMON, restart it, and wait for at least 4 more minutes so
that DAMON can find the cold memory regions, even though DAMON was knowing
there are regions that not accessed for 4 mins at the time of shutdown.

This commit makes DAMON sysfs interface to support online DAMON input
parameters updates by adding a new input keyword for the 'state' DAMON
sysfs file, 'commit'.  Writing the keyword to the 'state' file while the
corresponding kdamond is running makes the kdamond to read the sysfs file
values again and update the DAMON context.

Link: https://lkml.kernel.org/r/20220429160606.127307-12-sj@kernel.orgSigned-off-by: default avatarSeongJae Park <sj@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 01538719
...@@ -2061,6 +2061,8 @@ enum damon_sysfs_cmd { ...@@ -2061,6 +2061,8 @@ enum damon_sysfs_cmd {
DAMON_SYSFS_CMD_ON, DAMON_SYSFS_CMD_ON,
/* @DAMON_SYSFS_CMD_OFF: Turn the kdamond off. */ /* @DAMON_SYSFS_CMD_OFF: Turn the kdamond off. */
DAMON_SYSFS_CMD_OFF, DAMON_SYSFS_CMD_OFF,
/* @DAMON_SYSFS_CMD_COMMIT: Update kdamond inputs. */
DAMON_SYSFS_CMD_COMMIT,
/* /*
* @DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: Update scheme stats sysfs * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: Update scheme stats sysfs
* files. * files.
...@@ -2076,6 +2078,7 @@ enum damon_sysfs_cmd { ...@@ -2076,6 +2078,7 @@ enum damon_sysfs_cmd {
static const char * const damon_sysfs_cmd_strs[] = { static const char * const damon_sysfs_cmd_strs[] = {
"on", "on",
"off", "off",
"commit",
"update_schemes_stats", "update_schemes_stats",
}; };
...@@ -2195,6 +2198,39 @@ static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target, ...@@ -2195,6 +2198,39 @@ static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target,
return err; return err;
} }
/*
* Search a target in a context that corresponds to the sysfs target input.
*
* Return: pointer to the target if found, NULL if not found, or negative
* error code if the search failed.
*/
static struct damon_target *damon_sysfs_existing_target(
struct damon_sysfs_target *sys_target, struct damon_ctx *ctx)
{
struct pid *pid;
struct damon_target *t;
if (ctx->ops.id == DAMON_OPS_PADDR) {
/* Up to only one target for paddr could exist */
damon_for_each_target(t, ctx)
return t;
return NULL;
}
/* ops.id should be DAMON_OPS_VADDR or DAMON_OPS_FVADDR */
pid = find_get_pid(sys_target->pid);
if (!pid)
return ERR_PTR(-EINVAL);
damon_for_each_target(t, ctx) {
if (t->pid == pid) {
put_pid(pid);
return t;
}
}
put_pid(pid);
return NULL;
}
static int damon_sysfs_set_targets(struct damon_ctx *ctx, static int damon_sysfs_set_targets(struct damon_ctx *ctx,
struct damon_sysfs_targets *sysfs_targets) struct damon_sysfs_targets *sysfs_targets)
{ {
...@@ -2205,8 +2241,15 @@ static int damon_sysfs_set_targets(struct damon_ctx *ctx, ...@@ -2205,8 +2241,15 @@ static int damon_sysfs_set_targets(struct damon_ctx *ctx,
return -EINVAL; return -EINVAL;
for (i = 0; i < sysfs_targets->nr; i++) { for (i = 0; i < sysfs_targets->nr; i++) {
err = damon_sysfs_add_target( struct damon_sysfs_target *st = sysfs_targets->targets_arr[i];
sysfs_targets->targets_arr[i], ctx); struct damon_target *t = damon_sysfs_existing_target(st, ctx);
if (IS_ERR(t))
return PTR_ERR(t);
if (!t)
err = damon_sysfs_add_target(st, ctx);
else
err = damon_sysfs_set_regions(t, st->regions);
if (err) if (err)
return err; return err;
} }
...@@ -2309,6 +2352,48 @@ static int damon_sysfs_upd_schemes_stats(struct damon_sysfs_kdamond *kdamond) ...@@ -2309,6 +2352,48 @@ static int damon_sysfs_upd_schemes_stats(struct damon_sysfs_kdamond *kdamond)
return 0; return 0;
} }
static inline bool damon_sysfs_kdamond_running(
struct damon_sysfs_kdamond *kdamond)
{
return kdamond->damon_ctx &&
damon_sysfs_ctx_running(kdamond->damon_ctx);
}
/*
* damon_sysfs_commit_input() - Commit user inputs to a running kdamond.
* @kdamond: The kobject wrapper for the associated kdamond.
*
* If the sysfs input is wrong, the kdamond will be terminated.
*/
static int damon_sysfs_commit_input(struct damon_sysfs_kdamond *kdamond)
{
struct damon_ctx *ctx = kdamond->damon_ctx;
struct damon_sysfs_context *sys_ctx;
int err = 0;
if (!damon_sysfs_kdamond_running(kdamond))
return -EINVAL;
/* TODO: Support multiple contexts per kdamond */
if (kdamond->contexts->nr != 1)
return -EINVAL;
sys_ctx = kdamond->contexts->contexts_arr[0];
err = damon_select_ops(ctx, sys_ctx->ops_id);
if (err)
return err;
err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
if (err)
return err;
err = damon_sysfs_set_targets(ctx, sys_ctx->targets);
if (err)
return err;
err = damon_sysfs_set_schemes(ctx, sys_ctx->schemes);
if (err)
return err;
return err;
}
/* /*
* damon_sysfs_cmd_request_callback() - DAMON callback for handling requests. * damon_sysfs_cmd_request_callback() - DAMON callback for handling requests.
* @c: The DAMON context of the callback. * @c: The DAMON context of the callback.
...@@ -2331,6 +2416,9 @@ static int damon_sysfs_cmd_request_callback(struct damon_ctx *c) ...@@ -2331,6 +2416,9 @@ static int damon_sysfs_cmd_request_callback(struct damon_ctx *c)
case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS:
err = damon_sysfs_upd_schemes_stats(kdamond); err = damon_sysfs_upd_schemes_stats(kdamond);
break; break;
case DAMON_SYSFS_CMD_COMMIT:
err = damon_sysfs_commit_input(kdamond);
break;
default: default:
break; break;
} }
...@@ -2415,13 +2503,6 @@ static int damon_sysfs_turn_damon_off(struct damon_sysfs_kdamond *kdamond) ...@@ -2415,13 +2503,6 @@ static int damon_sysfs_turn_damon_off(struct damon_sysfs_kdamond *kdamond)
*/ */
} }
static inline bool damon_sysfs_kdamond_running(
struct damon_sysfs_kdamond *kdamond)
{
return kdamond->damon_ctx &&
damon_sysfs_ctx_running(kdamond->damon_ctx);
}
/* /*
* damon_sysfs_handle_cmd() - Handle a command for a specific kdamond. * damon_sysfs_handle_cmd() - Handle a command for a specific kdamond.
* @cmd: The command to handle. * @cmd: The command to handle.
......
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