mm/damon: fix three race bugs in sysfs and core
Subsystems (3)
SJ Park
maintainercover mm/damon: fix three race bugs in sysfs and core
There are three race condition bugs in DAMON sysfs interface and the core layer. User impacts are not critical but better to fix sooner than later.
Patch 1 fixes a toctou bug in DAMON sysfs interface that can mess up DAMOS auto-tuning. Patches 2 and 3 move static variables that can be wrongly shared with multiple kdamonds and therefore make DAMON monitoring results be a little more indeterministic.
Note for Andrew. These are not urgent but better to take the hotfix track in my humble opinion.
SJ Park (3):
mm/damon/sysfs-schemes: hold damon_sysfs_lock in target_metric_store() mm/damon/core: move static variable last_nr_regions to damon_ctx mm/damon/core: move static variable rotation to damon_ctx
include/linux/damon.h | 10 ++++++++++ mm/damon/core.c | 10 +++++----- mm/damon/sysfs-schemes.c | 3 +++ 3 files changed, 18 insertions(+), 5 deletions(-)
base-commit: f390255d9de653f4e7a9bae8c9a5ad881ad25a87
Signature · 1 line
2.47.3
SJ Park
maintainerRFC PATCH 1/3 mm/damon/sysfs-schemes: hold damon_sysfs_lock in target_metric_store()
damon_sysfs_add_quota_score() reads damos_sysfs_quota_goal->metric twice, once to set damos_quota_goal->metric to the value, and once again to set the metric-dependent damos_quota_goal unions based on the value.
target_metrics_score() can update damos_sysfs_quota_goal->metric between the two reads. This time-of-check to time-of-use (toctou) race can make the union fields incorrectly set. The incorrect damos_quota_goal will be passed to the core layer and make DAMOS incorrectly behave.
Users could reproduce the race by writing a new value to the target_metric sysfs file while DAMON is starting, or online parameters update is ongoing. The issue will only rarely happen in the real use cases. It requires sysfs write permission. The race window is short. Sane users wouldn't do such writes while DAMON enablement and/or online parameters update is ongoing.
The user impact is not very critical. The race is not easy to unintentionally trigger. No leak or crash happens. Nonetheless, DAMOS could be unexpectedly more or less aggressive and does not achieve the auto-tune goal. Because of the nature of races, investigating will be difficult. Users can control the impact from unexpected aggressiveness using additional quotas and resolve the issue in runtime by re-installing the quota goal, though.
Avoid the race by protecting damos_sysfs_quota_goal->metric write in target_metric_store() with damon_sysfs_lock. Because damon_sysfs_add_quota_score() is called while holding the same lock, the race is removed.
The issue was discovered as a part of a bug report [1] from Sashiko, which is partially outdated. In more detail, the Sashiko report [1] was mainly for an outdated bug [2]. But it was also mentioning the issue of this patch as a similar class bug.
[1] https://sashiko.dev/#/bug/linux-7f814c9a-986f-4103-a55a-ef9537dcf62b
[2] https://lore.kernel.org/[email protected]
--- mm/damon/sysfs-schemes.c | 3 +++ 1 file changed, 3 insertions(+)
mm/damon/sysfs-schemes.c +3 -0
@@ -1310,7 +1310,10 @@ static ssize_t target_metric_store(struct kobject *kobj, metric_name = &damos_sysfs_qgoal_metric_names[i];if (sysfs_streq (buf, metric_name->name)) { +if (!mutex_trylock (&damon_sysfs_lock)) +return -EBUSY ; goal->metric = metric_name->metric; +mutex_unlock (&damon_sysfs_lock);return count; } }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 1/3] mm/damon/sysfs-schemes: hold damon_sysfs_lock in target_metric_store()
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines
damon_sysfs_add_quota_score()readsdamos_sysfs_quota_goal->metrictwice, once to setdamos_quota_goal->metricto the value, and once again to set the metric-dependentdamos_quota_goalunions based on the value.
target_metrics_score()can updatedamos_sysfs_quota_goal->metricbetween the two reads. This time-of-check to time-of-use (toctou) race can make the union fields incorrectly set. The incorrectdamos_quota_goalwill be passed to the core layer and make DAMOS incorrectly behave.Users could reproduce the race by writing a new value to the
target_metricsysfs file while DAMON is starting, or online parameters update is ongoing. The issue will only rarely happen in the real use cases. It requires sysfs write permission. The race window is short. Sane users wouldn't do such writes while DAMON enablement and/or online parameters update is ongoing.
[ ... ]
[2] https://lore.kernel.org/[email protected]
Quoted 3 lines
Fixes 85fcf0ffc460 ("mm/damon/sysfs-schemes: connect damos_quota_goal nid with core layer")Cc <[email protected]> # 6.16.xSigned-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
SJ Park
maintainerRFC PATCH 2/3 mm/damon/core: move static variable last_nr_regions to damon_ctx
In reply to SJ Parkkdamond_split_regions() uses a static variable last_nr_regions, to infer if the region boundaries are too stable. If it looks too stable, it does a more aggressive region split because it might be a fake stability coming from non-aggressive split. Because the variable is static, the inference can be racy and wrong when multiple kdamonds are running in parallel.
The user impact is not critical. No crash, no leak. DAMON's best-effort monitoring accuracy can be degraded when multiple kdamonds are used in parallel and the workload has certain access patterns. Still it is a bug that is better to fix.
Move the variable into the damon_ctx struct to fix the problem.
--- include/linux/damon.h | 5 +++++ mm/damon/core.c | 5 ++--- 2 files changed, 7 insertions(+), 3 deletions(-)
include/linux/damon.h +5 -0
@@ -958,6 +958,11 @@ struct damon_ctx {/* @rnd_state: Per-ctx PRNG state for damon_rand(). */ struct rnd_state rnd_state; +/* +* number of regions after last merge. Read kdamond_split_regions() +* for more details. +*/ +unsigned long last_nr_regions; };/* Get a random number in [@l, @r) using @ctx's lockless PRNG. */
mm/damon/core.c +2 -3
@@ -3795,7 +3795,6 @@ static noinline_for_stack void kdamond_split_regions(struct damon_ctx *ctx)struct damon_target *t;unsigned long nr_regions =0 ;unsigned long max_nr_regions = ctx->attrs.max_nr_regions; -static unsigned long last_nr_regions;int nr_subregions =2 ;damon_for_each_target (t, ctx)
@@ -3811,7 +3810,7 @@ static noinline_for_stack void kdamond_split_regions(struct damon_ctx *ctx) }/* Maybe the middle of the region has different access frequency */ -if (last_nr_regions == nr_regions && +if (ctx->last_nr_regions == nr_regions && nr_regions < max_nr_regions /3 ) nr_subregions =3 ;
@@ -3820,7 +3819,7 @@ static noinline_for_stack void kdamond_split_regions(struct damon_ctx *ctx) ctx->min_region_sz); done: - last_nr_regions = nr_regions; + ctx->last_nr_regions = nr_regions; }/*
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 2/3] mm/damon/core: move static variable last_nr_regions to damon_ctx
Sashiko has reviewed this patch and found no issues.
Quoted 12 lines
kdamond_split_regions()uses a static variablelast_nr_regions, to infer if the region boundaries are too stable. If it looks too stable, it does a more aggressive region split because it might be a fake stability coming from non-aggressive split. Because the variable is static, the inference can be racy and wrong when multiple kdamonds are running in parallel.The user impact is not critical. No crash, no leak. DAMON's best-effort monitoring accuracy can be degraded when multiple kdamonds are used in parallel and the workload has certain access patterns. Still it is a bug that is better to fix.
Quoted 6 lines
Move the variable into the
damon_ctxstruct to fix the problem.Fixes 75c1c2b53c78 ("mm/damon/dbgfs: support multiple contexts")Cc <[email protected]> # 5.15.xSigned-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
SJ Park
maintainerRFC PATCH 3/3 mm/damon/core: move static variable rotation to damon_ctx
In reply to SJ Parkdamon_split_some_reigons() uses a static variable, rotation, to remember how many times it was called. It is again used to determine fair selection of regions to split. Since it is a static variable, it can be indeterministic and therefore less fair.
The user impact is not critical. No crash, no leak. DAMON's best-effort and indeterministic monitoring accuracy could be slightly more indeterministic when multiple kdamonds are used in parallel.
Fix the problem by moving the static variable into the damon_ctx struct.
--- include/linux/damon.h | 5 +++++ mm/damon/core.c | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-)
include/linux/damon.h +5 -0
@@ -963,6 +963,11 @@ struct damon_ctx {* for more details. */ unsigned long last_nr_regions; +/* +* number of damon_split_some_regions() invocations for this context. +* Read damon_split_some_regions() for more details. +*/ +unsigned long region_split_rotation; };/* Get a random number in [@l, @r) using @ctx's lockless PRNG. */
mm/damon/core.c +3 -2
@@ -3753,12 +3753,13 @@ static void damon_split_regions_of(struct damon_ctx *ctx,static void damon_split_some_regions (struct damon_ctx *ctx,unsigned long split_step) { -static unsigned long rotation;struct damon_target *t;struct damon_region *r, *next; -unsigned long offset = rotation++ % split_step; +unsigned long offset;unsigned long idx =0 ; + offset = ctx->region_split_rotation++ % split_step; +damon_for_each_target (t, ctx) {damon_for_each_region_safe (r, next, t) {unsigned long sz_region, sz_sub;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 3/3] mm/damon/core: move static variable rotation to damon_ctx
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines
damon_split_some_reigons()uses a static variable, rotation, to remember how many times it was called. It is again used to determine fair selection of regions to split. Since it is a static variable, it can be indeterministic and therefore less fair.The user impact is not critical. No crash, no leak. DAMON's best-effort and indeterministic monitoring accuracy could be slightly more indeterministic when multiple kdamonds are used in parallel.
Quoted 6 lines
Fix the problem by moving the static variable into the
damon_ctxstruct.Fixes 1a582e6e0174 ("mm/damon/core: split a fraction of regions when nr_regions exceeds max/2")Cc <[email protected]> # 7.3.xSigned-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3