Skip to content

Commit 2b26b1e

Browse files
sjp38gregkh
authored andcommitted
mm/damon/lru_sort: detect and use fresh enabled and kdamond_pid values
commit b98b7ff upstream. DAMON_LRU_SORT updates 'enabled' and 'kdamond_pid' parameter values, which represents the running status of its kdamond, when the user explicitly requests start/stop of the kdamond. The kdamond can, however, be stopped in events other than the explicit user request in the following three events. 1. ctx->regions_score_histogram allocation failure at beginning of the execution, 2. damon_commit_ctx() failure due to invalid user input, and 3. damon_commit_ctx() failure due to its internal allocation failures. Hence, if the kdamond is stopped by the above three events, the values of the status parameters can be stale. Users could show the stale values and be confused. This is already bad, but the real consequence is worse. DAMON_LRU_SORT avoids unnecessary damon_start() and damon_stop() calls based on the 'enabled' parameter value. And the update of 'enabled' parameter value depends on the damon_start() and damon_stop() call results. Hence, once the kdamond has stopped by the unintentional events, the user cannot restart the kdamond before the system reboot. For example, the issue can be reproduced via below steps. # cd /sys/module/damon_lru_sort/parameters # # # start DAMON_LRU_SORT # echo Y > enabled # ps -ef | grep kdamond root 806 2 0 17:53 ? 00:00:00 [kdamond.0] root 808 803 0 17:53 pts/4 00:00:00 grep kdamond # # # commit wrong input to stop kdamond withou explicit stop request # echo 3 > addr_unit # echo Y > commit_inputs bash: echo: write error: Invalid argument # # # confirm kdamond is stopped # ps -ef | grep kdamond root 811 803 0 17:53 pts/4 00:00:00 grep kdamond # # # users casn now show stable status # cat enabled Y # cat kdamond_pid 806 # # # even after fixing the wrong parameter, # # kdamond cannot be restarted. # echo 1 > addr_unit # echo Y > enabled # ps -ef | grep kdamond root 815 803 0 17:54 pts/4 00:00:00 grep kdamond The problem will only rarely happen in real and common setups for the following reasons. The allocation failures are unlikely in such setups since those allocations are arguably too small to fail. Also sane users on real production environments may not commit wrong input parameters. But once it happens, the consequence is quite bad. And the bug is a bug. The issue stems from the fact that there are multiple events that can change the status, and following all the events is challenging. Dynamically detect and use the fresh status for the parameters when those are requested. Link: https://lore.kernel.org/20260419161003.79176-3-sj@kernel.org Fixes: 40e983c ("mm/damon: introduce DAMON-based LRU-lists Sorting") Co-developed-by: Liew Rui Yan <aethernet65535@gmail.com> Signed-off-by: Liew Rui Yan <aethernet65535@gmail.com> Signed-off-by: SeongJae Park <sj@kernel.org> Cc: <stable@vger.kernel.org> # 6.0.x Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: SeongJae Park <sj@kernel.org> (port parts of 42b7491 ("mm/damon/core: introduce damon_call()") and d2b5be7 ("mm/damon/sysfs: use DAMON core API damon_is_running()") for damon_is_running() dependency) Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 0dd8917 commit 2b26b1e

3 files changed

Lines changed: 73 additions & 32 deletions

File tree

include/linux/damon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ static inline unsigned int damon_max_nr_accesses(const struct damon_attrs *attrs
677677

678678
int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
679679
int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
680+
bool damon_is_running(struct damon_ctx *ctx);
680681
int damon_kdamond_pid(struct damon_ctx *ctx);
681682

682683
int damon_set_region_biggest_system_ram_default(struct damon_target *t,

mm/damon/core.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,22 @@ int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
762762
return err;
763763
}
764764

765+
/**
766+
* damon_is_running() - Returns if a given DAMON context is running.
767+
* @ctx: The DAMON context to see if running.
768+
*
769+
* Return: true if @ctx is running, false otherwise.
770+
*/
771+
bool damon_is_running(struct damon_ctx *ctx)
772+
{
773+
bool running;
774+
775+
mutex_lock(&ctx->kdamond_lock);
776+
running = ctx->kdamond != NULL;
777+
mutex_unlock(&ctx->kdamond_lock);
778+
return running;
779+
}
780+
765781
/**
766782
* damon_kdamond_pid() - Return pid of a given DAMON context's worker thread.
767783
* @ctx: The DAMON context of the question.

mm/damon/lru_sort.c

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,6 @@ module_param(monitor_region_start, ulong, 0600);
111111
static unsigned long monitor_region_end __read_mostly;
112112
module_param(monitor_region_end, ulong, 0600);
113113

114-
/*
115-
* PID of the DAMON thread
116-
*
117-
* If DAMON_LRU_SORT is enabled, this becomes the PID of the worker thread.
118-
* Else, -1.
119-
*/
120-
static int kdamond_pid __read_mostly = -1;
121-
module_param(kdamond_pid, int, 0400);
122-
123114
static struct damos_stat damon_lru_sort_hot_stat;
124115
DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_hot_stat,
125116
lru_sort_tried_hot_regions, lru_sorted_hot_regions,
@@ -249,60 +240,93 @@ static int damon_lru_sort_turn(bool on)
249240
{
250241
int err;
251242

252-
if (!on) {
253-
err = damon_stop(&ctx, 1);
254-
if (!err)
255-
kdamond_pid = -1;
256-
return err;
257-
}
243+
if (!on)
244+
return damon_stop(&ctx, 1);
258245

259246
err = damon_lru_sort_apply_parameters();
260247
if (err)
261248
return err;
262249

263-
err = damon_start(&ctx, 1, true);
264-
if (err)
265-
return err;
266-
kdamond_pid = ctx->kdamond->pid;
267-
return 0;
250+
return damon_start(&ctx, 1, true);
251+
}
252+
253+
static bool damon_lru_sort_enabled(void)
254+
{
255+
if (!ctx)
256+
return false;
257+
return damon_is_running(ctx);
268258
}
269259

270260
static int damon_lru_sort_enabled_store(const char *val,
271261
const struct kernel_param *kp)
272262
{
273-
bool is_enabled = enabled;
274-
bool enable;
275263
int err;
276264

277-
err = kstrtobool(val, &enable);
265+
err = kstrtobool(val, &enabled);
278266
if (err)
279267
return err;
280268

281-
if (is_enabled == enable)
269+
if (damon_lru_sort_enabled() == enabled)
282270
return 0;
283271

284272
/* Called before init function. The function will handle this. */
285273
if (!ctx)
286-
goto set_param_out;
274+
return 0;
287275

288-
err = damon_lru_sort_turn(enable);
289-
if (err)
290-
return err;
276+
return damon_lru_sort_turn(enabled);
277+
}
291278

292-
set_param_out:
293-
enabled = enable;
294-
return err;
279+
static int damon_lru_sort_enabled_load(char *buffer,
280+
const struct kernel_param *kp)
281+
{
282+
return sprintf(buffer, "%c\n", damon_lru_sort_enabled() ? 'Y' : 'N');
295283
}
296284

297285
static const struct kernel_param_ops enabled_param_ops = {
298286
.set = damon_lru_sort_enabled_store,
299-
.get = param_get_bool,
287+
.get = damon_lru_sort_enabled_load,
300288
};
301289

302290
module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
303291
MODULE_PARM_DESC(enabled,
304292
"Enable or disable DAMON_LRU_SORT (default: disabled)");
305293

294+
static int damon_lru_sort_kdamond_pid_store(const char *val,
295+
const struct kernel_param *kp)
296+
{
297+
/*
298+
* kdamond_pid is read-only, but kernel command line could write it.
299+
* Do nothing here.
300+
*/
301+
return 0;
302+
}
303+
304+
static int damon_lru_sort_kdamond_pid_load(char *buffer,
305+
const struct kernel_param *kp)
306+
{
307+
int kdamond_pid = -1;
308+
309+
if (ctx) {
310+
kdamond_pid = damon_kdamond_pid(ctx);
311+
if (kdamond_pid < 0)
312+
kdamond_pid = -1;
313+
}
314+
return sprintf(buffer, "%d\n", kdamond_pid);
315+
}
316+
317+
static const struct kernel_param_ops kdamond_pid_param_ops = {
318+
.set = damon_lru_sort_kdamond_pid_store,
319+
.get = damon_lru_sort_kdamond_pid_load,
320+
};
321+
322+
/*
323+
* PID of the DAMON thread
324+
*
325+
* If DAMON_LRU_SORT is enabled, this becomes the PID of the worker thread.
326+
* Else, -1.
327+
*/
328+
module_param_cb(kdamond_pid, &kdamond_pid_param_ops, NULL, 0400);
329+
306330
static int damon_lru_sort_handle_commit_inputs(void)
307331
{
308332
int err;

0 commit comments

Comments
 (0)