[RFC PATCH v1.2 2/2] mm/damon/ops-common: prevent >DAMON_MAX_SUBSCORE freq_subscore

From: SeongJae Park

Date: Mon Jun 22 2026 - 10:15:40 EST


When a zero sampling interval and a zero aggregation interval are
online-committed, damon_max_nr_accesses() will return 1 right after the
update. damon_update_monitoring_results() skips updating nr_accesses of
regions for zero intervals, though. As a result, some regions could
have nr_acceses values that are larger than damon_max_nr_accesses() for
the remaining aggregation window. Note that the remaining aggregation
window will be quite short. It is just the remaining execution of the
kdamond_fn() main loop body, since the aggregation interval is zero.

If damon_hot_score() is called during the remaining aggregation window,
the function can calculate freq_subscore that is larger than
DAMON_MAX_SUBSCORE. Depending on the score weights and age/size scores,
damon_hot_score() can now return a score that is higher than
DAMOS_MAX_SCORE.

damos_adjust_quota(), which is an indirect caller of damon_hot_score()
uses the score as an index to regions_score_histogram array. The
array's size is set to only DAMOS_MAX_SCORE + 1. As a result, an
out-of-bound array access can happen.

The issue is expected to happen only rarely in the real world. After
all, zero aggregation interval is not supposed to be common. Also, the
online commit of zero intervals should be made on exactly when the DAMOS
scheme will be triggered. I was unable to trigger this on my own.
Nonetheless, it is possible in theory and the consequence is bad.

Fix the problem by applying an upper bound of the freq_subscore. This
is a short term fix. In the long term,
damon_update_monitoring_results() should be modified to update all
monitoring results even in case of zero aggregation interval. Add that
as a TODO.

The issue was discovered [1] by Sashiko.

[1] https://lore.kernel.org/20260621175849.91990-1-sj@xxxxxxxxxx

Fixes: 2f5bef5a590b ("mm/damon/core: update monitoring results for new monitoring attributes")
Cc: <stable@xxxxxxxxxxxxxxx> # 6.3.x
Signed-off-by: SeongJae Park <sj@xxxxxxxxxx>
---
mm/damon/ops-common.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/mm/damon/ops-common.c b/mm/damon/ops-common.c
index 5c93ef2bb8a97..8d516851a69e4 100644
--- a/mm/damon/ops-common.c
+++ b/mm/damon/ops-common.c
@@ -115,6 +115,9 @@ int damon_hot_score(struct damon_ctx *c, struct damon_region *r,

freq_subscore = r->nr_accesses * DAMON_MAX_SUBSCORE /
damon_max_nr_accesses(&c->attrs);
+ /* TODO: update monitoring results always to avoid this. */
+ if (freq_subscore > DAMON_MAX_SUBSCORE)
+ freq_subscore = DAMON_MAX_SUBSCORE;

age_in_sec = (unsigned long)r->age * c->attrs.aggr_interval / 1000000;
if (age_in_sec)
--
2.47.3