Skip to contentSkip to conversation

mm/damon/core: reset invalid quota->charge_target_from

PATCH Applied SJ Park 2 participants 3 messages Sep 10

Subsystems (3)
lore
Share & tools
lore

SJ Park

author maintainer
#0 lore raw

PATCH v2 1/1 mm/damon/core: reset invalid quota->charge_target_from

DAMOS can suddenly stop working if a target process that the quota is just fully charged on 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.

When the invalid target is found, further check the corner case and reset the pointer.

This issue was discovered [1] by Sashiko.

[1] https://lore.kernel.org/[email protected]

Fixes 6e4930e33329 ("mm/damon/core: fix wasteful CPU calls by skipping non-existent targets")
Cc <[email protected]> # 7.0.x

---
Changes from RFC v2
- RFC v2: https://lore.kernel.org/[email protected]
- Drop RFC tag again.
Changes from v1
- v1: https://lore.kernel.org/[email protected]
- Move the corner case check to invalid target found case, to handle dynamic quota uninstall case.
- Add RFC tag again.
Changes from RFC v1
- RFC v1: https://lore.kernel.org/[email protected]
- Drop RFC tag.

 mm/damon/core.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/damon/core.c +8 -1
index 645cb367019ae..06cf2ab7e97d7 100644
@@ -3393,8 +3393,15 @@ static void kdamond_apply_schemes(struct damon_ctx *c)
 	max_region_sz = damon_region_sz_limit(c);
 	mutex_lock(&c->walk_control_lock);
 	damon_for_each_target(t, c) {
-		if (c->ops.target_valid && c->ops.target_valid(t) == false)
+		if (c->ops.target_valid && c->ops.target_valid(t) == false) {
+			damon_for_each_scheme(s, c) {
+				if (s->quota.charge_target_from != t)
+					continue;
+				s->quota.charge_target_from = NULL;
+				s->quota.charge_addr_from = 0;
+			}
 			continue;
+		}
 		damos_apply_target(c, t, max_region_sz);
 	}
 

base-commit: bd699d8a4388cda037f01797294c834fb422ebe6

Signature · 1 line
2.47.3
#1 lore raw
Thank 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] DAMOS scheme processing permanently stalls if a valid target has zero regions, reproducing the exact lockup the patch intended to fix. --

commit be23af6d135629bfa902e51163989385be75b34b
Author: SJ Park <[email protected]>
One-line subject: mm/damon/core: reset invalid quota->charge_target_from

This commit fixes an issue where DAMOS can suddenly stop working if a target process that the quota is fully charged on is terminated. It handles this corner case by catching the invalid target and resetting the charge target pointer.

Quoted 9 lines
mm/damon/core.c +0 -1
index 645cb367019ae..06cf2ab7e97d7 100644
@@ -3393,8 +3393,15 @@ static void kdamond_apply_schemes(struct damon_ctx *c)
 	max_region_sz = damon_region_sz_limit(c);
 	mutex_lock(&c->walk_control_lock);
 	damon_for_each_target(t, c) {
-		if (c->ops.target_valid && c->ops.target_valid(t) == false)

mm/damon/core.c static void kdamond_apply_schemes(struct damon_ctx *c) · line 3405

Quoted 10 lines
mm/damon/core.c +8 -0
+		if (c->ops.target_valid && c->ops.target_valid(t) == false) {
+			damon_for_each_scheme(s, c) {
+				if (s->quota.charge_target_from != t)
+					continue;
+				s->quota.charge_target_from = NULL;
+				s->quota.charge_addr_from = 0;
+			}
 			continue;
+		}
 		damos_apply_target(c, t, max_region_sz);

[Severity: High]
This is a pre-existing issue, but does this incomplete fix leave DAMOS vulnerable to permanently stalling when a valid target has zero regions, or when using operations where target_valid() is not implemented?

If a DAMOS quota is exhausted while applying actions to a target, saving its pointer to s->quota.charge_target_from, and before the next apply interval all regions of that target are dynamically removed but the target itself is kept valid, kdamond_apply_schemes() will process the target here.

Because the target has zero regions, the loop inside damos_apply_target() is bypassed:

mm/damon/core.c:damos_apply_target() {
	...
	damon_for_each_region(r, t) {
		...
		damon_do_apply_schemes(c, t, r);
		...
	}
}

Consequently, damos_skip_charged_region() is never executed to clear charge_target_from.

When evaluating all other targets, damos_skip_charged_region() will check the stale charge_target_from pointer and unconditionally return true, skipping all actions and permanently stalling scheme processing.

Could we ensure charge_target_from is cleared even if the target has no regions, or if the target_valid() operation is missing?

Quoted 1 line
mm/damon/core.c +0 -0
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#2 lore raw
Quoted 60 lines · On Thu, 10 Sep 2026 14:43:34 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] DAMOS scheme processing permanently stalls if a valid target has zero regions, reproducing the exact lockup the patch intended to fix. --

commit be23af6d135629bfa902e51163989385be75b34b
Author: SJ Park <[email protected]>
One-line subject: mm/damon/core: reset invalid quota->charge_target_from

This commit fixes an issue where DAMOS can suddenly stop working if a target process that the quota is fully charged on is terminated. It handles this corner case by catching the invalid target and resetting the charge target pointer.

Quoted 9 lines
mm/damon/core.c +0 -1
index 645cb367019ae..06cf2ab7e97d7 100644
@@ -3393,8 +3393,15 @@ static void kdamond_apply_schemes(struct damon_ctx *c)
 	max_region_sz = damon_region_sz_limit(c);
 	mutex_lock(&c->walk_control_lock);
 	damon_for_each_target(t, c) {
-		if (c->ops.target_valid && c->ops.target_valid(t) == false)

mm/damon/core.c static void kdamond_apply_schemes(struct damon_ctx *c) · line 3405

Quoted 10 lines
mm/damon/core.c +8 -0
+		if (c->ops.target_valid && c->ops.target_valid(t) == false) {
+			damon_for_each_scheme(s, c) {
+				if (s->quota.charge_target_from != t)
+					continue;
+				s->quota.charge_target_from = NULL;
+				s->quota.charge_addr_from = 0;
+			}
 			continue;
+		}
 		damos_apply_target(c, t, max_region_sz);

[Severity: High]
This is a pre-existing issue, but does this incomplete fix leave DAMOS vulnerable to permanently stalling when a valid target has zero regions, or when using operations where target_valid() is not implemented?

If a DAMOS quota is exhausted while applying actions to a target, saving its pointer to s->quota.charge_target_from, and before the next apply interval all regions of that target are dynamically removed but the target itself is kept valid, kdamond_apply_schemes() will process the target here.

Because the target has zero regions, the loop inside damos_apply_target() is bypassed:

mm/damon/core.c:damos_apply_target() {
	...
	damon_for_each_region(r, t) {
		...
		damon_do_apply_schemes(c, t, r);
		...
	}
}

Consequently, damos_skip_charged_region() is never executed to clear charge_target_from.

Quoted 6 lines

When evaluating all other targets, damos_skip_charged_region() will check the stale charge_target_from pointer and unconditionally return true, skipping all actions and permanently stalling scheme processing.

Could we ensure charge_target_from is cleared even if the target has no regions, or if the target_valid() operation is missing?

No, the issue cannot happen.

If the target has zero region, quota couldn't be fully charged on the target. So charge_target_from couldn't point the empty region target.

For all operation set that could have invalid target, target_valid() operation is set.

Quoted 5 lines
Quoted 2 lines
mm/damon/core.c +0 -0
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

Thanks,
SJ