From: SJ Park <sj@kernel.org>
Cc: SJ Park <sj@kernel.org>,
stable@vger.kernel.org, Andrew Morton <akpm@linux-foundation.org>,
damon@lists.linux.dev, linux-kernel@vger.kernel.org,
linux-mm@kvack.org
Subject: [RFC PATCH 1/3] mm/damon/sysfs-schemes: hold damon_sysfs_lock in target_metric_store()
Date: Sat, 24 Oct 2026 12:33:47 -0700 [thread overview]
Message-ID: <20261024193351.87130-2-sj@kernel.org> (raw)
In-Reply-To: <20261024193351.87130-1-sj@kernel.org>
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/20261021005234.94107-1-sj@kernel.org
Fixes: 85fcf0ffc460 ("mm/damon/sysfs-schemes: connect damos_quota_goal nid with core layer")
Cc: <stable@vger.kernel.org> # 6.16.x
Signed-off-by: SJ Park <sj@kernel.org>
---
mm/damon/sysfs-schemes.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c
index 8f083611741fd..637f1bd0c950b 100644
--- a/mm/damon/sysfs-schemes.c
+++ b/mm/damon/sysfs-schemes.c
@@ -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;
}
}
--
2.47.3
next prev parent reply other threads:[~2026-10-24 19:33 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-24 19:33 [RFC PATCH 0/3] mm/damon: fix three race bugs in sysfs and core SJ Park
2026-10-24 19:33 ` SJ Park [this message]
2026-10-24 19:33 ` [RFC PATCH 2/3] mm/damon/core: move static variable last_nr_regions to damon_ctx SJ Park
2026-10-24 19:33 ` [RFC PATCH 3/3] mm/damon/core: move static variable rotation " SJ Park
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261024193351.87130-2-sj@kernel.org \
--to=sj@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=damon@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®