mm/damon/core: reset invalid quota->charge_target_from
Subsystems (3)
SJ Park
maintainerPATCH 1/1 mm/damon/core: reset invalid quota->charge_target_from
DAMOS can suddenly stop working if a target process that quota just fully charged in it is terminated. Fix by catching and processing the corner case.
When DAMOS quota is fully charged, the target and the region to continue applying the action in the next round is saved in damos_quota->charge_{target,addr}_from. In the next round, DAMOS iterates targets and regions from the beginning. It skips applying the action to the regions until it visits and skips the saved target/region.
Virtual address space targets become invalid if the process is terminated. Trying to apply the scheme to invalid target is just a waste of time. Hence commit 6e4930e33329 ("mm/damon/core: fix wasteful CPU calls by skipping non-existent targets") made the logic to skip invalid targets. However, it does skip before the charged target/region skipping/updating.
Let's suppose the user runs DAMOS for multiple virtual address spaces with a quota. The quota exceeded in the middle of a virtual address space. And the process of the address space is terminated. Then the charge_target_from points to the invalid target. The pointer update logic is skipped for the invalid target, so the charge_target_from is never updated. DAMOS action to every target/region is skipped. From the user's perspective, it would look like suddenly DAMOS has stopped working.
No critical leak or crash can happen. The user could reinstall the scheme. But this makes use of DAMOS under certain setups quite unreliable.
Catch the corner case and reset the pointer in damos_adjust_quota(), which is called for adjustment of the quota for every round.
This issue was discovered [1] by Sashiko.
[1] https://lore.kernel.org/[email protected]
Changes from RFC v1
- RFC v1: https://lore.kernel.org/[email protected]
- Drop RFC tag.
--- mm/damon/core.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
mm/damon/core.c +18 -0
@@ -3283,6 +3283,22 @@ static void damos_trace_esz(struct damon_ctx *c, struct damos *s,trace_damos_esz (cidx, sidx, quota->esz); } +static void damos_reset_invalid_charge_target_from (struct damos_quota *quota, +struct damon_ctx *c) +{ +struct damon_target *t; + + t = quota->charge_target_from; +if (!t) +return ; +if (!c->ops.target_valid) +return ; +if (c->ops.target_valid (t)) +return ; + quota->charge_target_from =NULL ; + quota->charge_addr_from =0 ; +} +static void damos_adjust_quota (struct damon_ctx *c,struct damos *s) {struct damos_quota *quota = &s->quota;
@@ -3318,6 +3334,8 @@ static void damos_adjust_quota(struct damon_ctx *c, struct damos *s)damos_trace_esz (c, s, quota); } +damos_reset_invalid_charge_target_from (quota, c); +if (!c->ops.get_scheme_score)return ;
base-commit: 6ce2570ad1f1f4195c8d8b1a460fec2fc276dd65
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The newly added fix for invalid charge_target_from pointers is bypassed if a scheme's quota is dynamically disabled, leaving the scheme permanently broken. --
commit f88287bb2670346a5987d086e0731c7ba6d0b19e
Author: SJ Park <[email protected]>
mm/damon/core: reset invalid quota->charge_target_from
DAMOS can suddenly stop working if a target process that quota just fully charged in it is terminated. Fix by catching and processing the corner case.
Quoted 4 lines
mm/damon/core.c +0
-0
[ ... ]
mm/damon/core.c static void damos_adjust_quota(struct damon_ctx *c, struct damos *s) · line 3341
Quoted 9 lines
mm/damon/core.c +2
-0@@ -3318,6 +3334,8 @@ static void damos_adjust_quota(struct damon_ctx *c, struct damos *s)damos_trace_esz (c, s, quota); } +damos_reset_invalid_charge_target_from (quota, c); +if (!c->ops.get_scheme_score)return ;
[Severity: High]
This is a pre-existing issue, but does this bypass the newly added reset if a scheme's quota is dynamically disabled?
If a user hits the corner case where charge_target_from becomes invalid, and then dynamically disables the scheme's quota via sysfs (setting ms=0 and sz=0), damos_adjust_quota() will return early before the reset is reached:
mm/damon/core.c:damos_adjust_quota() {
if (!quota->ms && !quota->sz && list_empty("a->goals))
return;
...
}Because damos_skip_charged_region() does not check if the quota is enabled before evaluating charge_target_from, it will continue to skip all regions forever:
mm/damon/core.c:damos_skip_charged_region() {
...
if (t != quota->charge_target_from)
return true;
...
}Could the pointer reset be moved before the early return in damos_adjust_quota(), or could the skipping logic be updated to handle disabled quotas?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
SJ Park
maintainerQuoted 55 lines · On Mon, 07 Sep 2026 17:13:46 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The newly added fix for invalidcharge_target_frompointers is bypassed if a scheme's quota is dynamically disabled, leaving the scheme permanently broken. --commit
f88287bb2670346a5987d086e0731c7ba6d0b19e
Author: SJ Park <[email protected]>
mm/damon/core: reset invalidquota->charge_target_fromDAMOS can suddenly stop working if a target process that quota just fully charged in it is terminated. Fix by catching and processing the corner case.
Quoted 4 lines
mm/damon/core.c +0
-0[ ... ]
mm/damon/core.c static void damos_adjust_quota(struct damon_ctx *c, struct damos *s) · line 3341
Quoted 9 lines
mm/damon/core.c +2
-0@@ -3318,6 +3334,8 @@ static void damos_adjust_quota(struct damon_ctx *c, struct damos *s)damos_trace_esz (c, s, quota); } +damos_reset_invalid_charge_target_from (quota, c); +if (!c->ops.get_scheme_score)return ;[Severity: High]
This is a pre-existing issue, but does this bypass the newly added reset if a scheme's quota is dynamically disabled?If a user hits the corner case where
charge_target_frombecomes invalid, and then dynamically disables the scheme's quota via sysfs (setting ms=0 and sz=0),damos_adjust_quota()will return early before the reset is reached:mm/damon/core.c:damos_adjust_quota() { if (!quota->ms && !quota->sz && list_empty("a->goals)) return; ... }Because
damos_skip_charged_region()does not check if the quota is enabled before evaluatingcharge_target_from, it will continue to skip all regions forever:mm/damon/core.c:damos_skip_charged_region() { ... if (t != quota->charge_target_from) return true; ... }
Quoted 3 lines
Could the pointer reset be moved before the early return in
damos_adjust_quota(), or could the skipping logic be updated to handle disabled quotas?
Good catch. I think we should unset the charge_{target,addr}_from in damos_commit_quota(). I will do so in the next version.
Quoted 3 lines
Signature · 2 lines
-- Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thanks,
SJ
SJ Park
maintainerQuoted 3 lines · On Mon, 7 Sep 2026 09:55:27 -0700 SJ Park <[email protected]> wrote:
DAMOS can suddenly stop working if a target process that quota just fully charged in it is terminated. Fix by catching and processing the corner case.
Sashiko found this fix is not complete. I will post a new version.
Thanks,
SJ
[...]