Skip to content

Commit b98b7ff

Browse files
sjp38akpm00
authored andcommitted
mm/damon/lru_sort: detect and use fresh enabled and kdamond_pid values
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>
1 parent 64a140a commit b98b7ff

1 file changed

Lines changed: 55 additions & 30 deletions

File tree

mm/damon/lru_sort.c

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,6 @@ module_param(monitor_region_end, ulong, 0600);
161161
*/
162162
static unsigned long addr_unit __read_mostly = 1;
163163

164-
/*
165-
* PID of the DAMON thread
166-
*
167-
* If DAMON_LRU_SORT is enabled, this becomes the PID of the worker thread.
168-
* Else, -1.
169-
*/
170-
static int kdamond_pid __read_mostly = -1;
171-
module_param(kdamond_pid, int, 0400);
172-
173164
static struct damos_stat damon_lru_sort_hot_stat;
174165
DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_hot_stat,
175166
lru_sort_tried_hot_regions, lru_sorted_hot_regions,
@@ -386,12 +377,8 @@ static int damon_lru_sort_turn(bool on)
386377
{
387378
int err;
388379

389-
if (!on) {
390-
err = damon_stop(&ctx, 1);
391-
if (!err)
392-
kdamond_pid = -1;
393-
return err;
394-
}
380+
if (!on)
381+
return damon_stop(&ctx, 1);
395382

396383
err = damon_lru_sort_apply_parameters();
397384
if (err)
@@ -400,9 +387,6 @@ static int damon_lru_sort_turn(bool on)
400387
err = damon_start(&ctx, 1, true);
401388
if (err)
402389
return err;
403-
kdamond_pid = damon_kdamond_pid(ctx);
404-
if (kdamond_pid < 0)
405-
return kdamond_pid;
406390
return damon_call(ctx, &call_control);
407391
}
408392

@@ -430,42 +414,83 @@ module_param_cb(addr_unit, &addr_unit_param_ops, &addr_unit, 0600);
430414
MODULE_PARM_DESC(addr_unit,
431415
"Scale factor for DAMON_LRU_SORT to ops address conversion (default: 1)");
432416

417+
static bool damon_lru_sort_enabled(void)
418+
{
419+
if (!ctx)
420+
return false;
421+
return damon_is_running(ctx);
422+
}
423+
433424
static int damon_lru_sort_enabled_store(const char *val,
434425
const struct kernel_param *kp)
435426
{
436-
bool is_enabled = enabled;
437-
bool enable;
438427
int err;
439428

440-
err = kstrtobool(val, &enable);
429+
err = kstrtobool(val, &enabled);
441430
if (err)
442431
return err;
443432

444-
if (is_enabled == enable)
433+
if (damon_lru_sort_enabled() == enabled)
445434
return 0;
446435

447436
/* Called before init function. The function will handle this. */
448437
if (!damon_initialized())
449-
goto set_param_out;
438+
return 0;
450439

451-
err = damon_lru_sort_turn(enable);
452-
if (err)
453-
return err;
440+
return damon_lru_sort_turn(enabled);
441+
}
454442

455-
set_param_out:
456-
enabled = enable;
457-
return err;
443+
static int damon_lru_sort_enabled_load(char *buffer,
444+
const struct kernel_param *kp)
445+
{
446+
return sprintf(buffer, "%c\n", damon_lru_sort_enabled() ? 'Y' : 'N');
458447
}
459448

460449
static const struct kernel_param_ops enabled_param_ops = {
461450
.set = damon_lru_sort_enabled_store,
462-
.get = param_get_bool,
451+
.get = damon_lru_sort_enabled_load,
463452
};
464453

465454
module_param_cb(enabled, &enabled_param_ops, &enabled, 0600);
466455
MODULE_PARM_DESC(enabled,
467456
"Enable or disable DAMON_LRU_SORT (default: disabled)");
468457

458+
static int damon_lru_sort_kdamond_pid_store(const char *val,
459+
const struct kernel_param *kp)
460+
{
461+
/*
462+
* kdamond_pid is read-only, but kernel command line could write it.
463+
* Do nothing here.
464+
*/
465+
return 0;
466+
}
467+
468+
static int damon_lru_sort_kdamond_pid_load(char *buffer,
469+
const struct kernel_param *kp)
470+
{
471+
int kdamond_pid = -1;
472+
473+
if (ctx) {
474+
kdamond_pid = damon_kdamond_pid(ctx);
475+
if (kdamond_pid < 0)
476+
kdamond_pid = -1;
477+
}
478+
return sprintf(buffer, "%d\n", kdamond_pid);
479+
}
480+
481+
static const struct kernel_param_ops kdamond_pid_param_ops = {
482+
.set = damon_lru_sort_kdamond_pid_store,
483+
.get = damon_lru_sort_kdamond_pid_load,
484+
};
485+
486+
/*
487+
* PID of the DAMON thread
488+
*
489+
* If DAMON_LRU_SORT is enabled, this becomes the PID of the worker thread.
490+
* Else, -1.
491+
*/
492+
module_param_cb(kdamond_pid, &kdamond_pid_param_ops, NULL, 0400);
493+
469494
static int __init damon_lru_sort_init(void)
470495
{
471496
int err;

0 commit comments

Comments
 (0)