FAILED: patch "[PATCH] mm/damon/core: fix unconditionally skip last region" failed to apply to 6.1-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x b3723b596b548c837a766aae3553c14a7b15af2b
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026092949-surpass-thimble-02a8@gregkh' --subject-prefix 'PATCH 6.1.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From b3723b596b548c837a766aae3553c14a7b15af2b Mon Sep 17 00:00:00 2001
From: Liew Rui Yan <[email protected]>
Date: Tue, 8 Sep 2026 06:47:38 -0700
Subject: [PATCH] mm/damon/core: fix unconditionally skip last region
Once quota set, the charge_{target,addr}_from unconditionally skips and resets at the last region of the tracked target, so the last region can be skipped even when it has not been processed.
Example:
1. Target has 2 regions: R1 (0-100 bytes) and R2 (100-200 bytes).
2. Quota is configured to process only 100 bytes per window.
3. Window 1: Processes R1 (0-100). Quota is full. charge_{target, addr}_from is saved at (Target, 100).
4. Window 2: The loop reaches R2. Because R2 is damon_last_region(t), the old code unconditionally returns true, skipping R2 entirely and resetting the charge_{target,addr}_from.
Result: R2 is permanently skipped even though it has never been processed.
However, it is important to note that this is a very minor issue. This is because it is triggered only when the previous window saved/kept charge_{target,addr}_from, and in the next window, all regions except the last region were skipped by damos_skip_charged_region().
Fix this by only resetting the charge_{target,addr}_from when last region is reached, only skipping when it is applied or cannot split.
mm/damon/core.c +14 -11
@@ -2342,36 +2342,39 @@ static bool damos_skip_charged_region(struct damon_target *t, {struct damos_quota *quota = &s->quota;unsigned long sz_to_skip; +bool skip = false;/* Skip previously charged regions */ if (quota->charge_target_from) {if (t != quota->charge_target_from)return true; -if (r ==damon_last_region (t)) { - quota->charge_target_from =NULL ; - quota->charge_addr_from =0 ; -return true; - }if (quota->charge_addr_from && - r->ar.end <= quota->charge_addr_from) -return true; + r->ar.end <= quota->charge_addr_from) { + skip = true; + goto out; + }if (quota->charge_addr_from && r->ar.start < quota->charge_addr_from) { sz_to_skip =ALIGN_DOWN (quota->charge_addr_from - r->ar.start, min_region_sz);if (!sz_to_skip) { -if (damon_sz_region (r) <= min_region_sz) -return true; +if (damon_sz_region (r) <= min_region_sz) { + skip = true; + goto out; + } sz_to_skip = min_region_sz; }damon_split_region_at (t, r, sz_to_skip); -return true; + skip = true; } + } +out: +if (r ==damon_last_region (t)) { quota->charge_target_from =NULL ; quota->charge_addr_from =0 ; } -return false; +return skip; } static void damos_update_stat(struct damos *s,
SJ Park
maintainerPATCH 1/1 mm/damon/core: fix unconditionally skip last region
From: Liew Rui Yan <[email protected]>
Once quota set, the charge_{target,addr}_from unconditionally skips and resets at the last region of the tracked target, so the last region can be skipped even when it has not been processed.
Example:
1. Target has 2 regions: R1 (0-100 bytes) and R2 (100-200 bytes).
2. Quota is configured to process only 100 bytes per window.
3. Window 1: Processes R1 (0-100). Quota is full. charge_{target, addr}_from is saved at (Target, 100).
4. Window 2: The loop reaches R2. Because R2 is damon_last_region(t), the old code unconditionally returns true, skipping R2 entirely and resetting the charge_{target,addr}_from.
Result: R2 is permanently skipped even though it has never been processed.
However, it is important to note that this is a very minor issue. This is because it is triggered only when the previous window saved/kept charge_{target,addr}_from, and in the next window, all regions except the last region were skipped by damos_skip_charged_region().
Fix this by only resetting the charge_{target,addr}_from when last region is reached, only skipping when it is applied or cannot split.
--- mm/damon/core.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
mm/damon/core.c +15 -9
@@ -756,6 +756,7 @@ static void damon_do_apply_schemes(struct damon_ctx *c,unsigned long sz =damon_sz_region (r);struct timespec64 begin, end;unsigned long sz_applied =0 ; +bool skip = false;if (!s->wmarks.activated)continue ;
@@ -768,14 +769,11 @@ static void damon_do_apply_schemes(struct damon_ctx *c,if (quota->charge_target_from) {if (t != quota->charge_target_from)continue ; -if (r ==damon_last_region (t)) { - quota->charge_target_from =NULL ; - quota->charge_addr_from =0 ; -continue ; - }if (quota->charge_addr_from && - r->ar.end <= quota->charge_addr_from) -continue ; + r->ar.end <= quota->charge_addr_from) { + skip = true; + goto clean_charge_from_skip; + }if (quota->charge_addr_from && r->ar.start < quota->charge_addr_from) {
@@ -783,17 +781,25 @@ static void damon_do_apply_schemes(struct damon_ctx *c, r->ar.start, DAMON_MIN_REGION);if (!sz) {if (damon_sz_region (r) <= -DAMON_MIN_REGION ) -continue ; +DAMON_MIN_REGION ) { + skip = true; + goto clean_charge_from_skip; + } sz =DAMON_MIN_REGION ; }damon_split_region_at (t, r, sz); r =damon_next_region (r); sz =damon_sz_region (r); + skip = false; } + } +clean_charge_from_skip: +if (r ==damon_last_region (t)) { quota->charge_target_from =NULL ; quota->charge_addr_from =0 ; } +if (skip) +continue ;if (!damos_valid_target (c, t, r, s))continue ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 6.1.y] mm/damon/core: fix unconditionally skip last region
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] mm/damon: out-of-bounds memory access when region split fails on last region --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm/damon: out-of-bounds memory access when region split fails on last region --
commit e8c0d519ccfab8bb3783274c1139b24ab75f8aa1
Author: Liew Rui Yan <[email protected]>
mm/damon/core: fix unconditionally skip last region
This commit fixes an issue where the last region of a target is unconditionally skipped and reset when a quota is set, even if it hasn't been processed.
Quoted 13 lines
mm/damon/core.c +0
-5@@ -768,14 +769,11 @@ static void damon_do_apply_schemes(struct damon_ctx *c,if (quota->charge_target_from) {if (t != quota->charge_target_from)continue ; -if (r ==damon_last_region (t)) { - quota->charge_target_from =NULL ; - quota->charge_addr_from =0 ; -continue ; - }
[ ... ]
Quoted 5 lines
mm/damon/core.c +0
-1@@ -783,17 +781,25 @@ static void damon_do_apply_schemes(struct damon_ctx *c, r->ar.start, DAMON_MIN_REGION);if (!sz) { if (damon_sz_region(r) <= -DAMON_MIN_REGION )
mm/damon/core.c static void damon_do_apply_schemes(struct damon_ctx *c, · line 792
Quoted 10 lines
mm/damon/core.c +4
-1-continue ; + DAMON_MIN_REGION) { + skip = true; + goto clean_charge_from_skip; + } sz =DAMON_MIN_REGION ; }damon_split_region_at (t, r, sz); r =damon_next_region (r); sz =damon_sz_region (r);
[Severity: High]
Since this patch removes the early check for the last region, what happens in damon_do_apply_schemes() if damon_split_region_at() fails on the last region?
If damon_split_region_at() fails under memory pressure, it returns silently without splitting the region. The code then unconditionally calls r = damon_next_region(r).
Because r was the last region and was not split, its list.next points to the list head. damon_next_region() then returns an invalid pointer.
The next line calls damon_sz_region(r), which dereferences this invalid pointer and causes an out-of-bounds memory access.
Does this code need a check to ensure the region was successfully split before advancing the pointer?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
SJ Park
maintainerRe: [PATCH 6.1.y] mm/damon/core: fix unconditionally skip last region
Quoted 56 lines · On Wed, 30 Sep 2026 10:05:10 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm/damon: out-of-bounds memory access when region split fails on last region --commit
e8c0d519ccfab8bb3783274c1139b24ab75f8aa1
Author: Liew Rui Yan <[email protected]>
mm/damon/core: fix unconditionally skip last regionThis commit fixes an issue where the last region of a target is unconditionally skipped and reset when a quota is set, even if it hasn't been processed.
Quoted 13 lines
mm/damon/core.c +0
-5@@ -768,14 +769,11 @@ static void damon_do_apply_schemes(struct damon_ctx *c,if (quota->charge_target_from) {if (t != quota->charge_target_from)continue ; -if (r ==damon_last_region (t)) { - quota->charge_target_from =NULL ; - quota->charge_addr_from =0 ; -continue ; - }[ ... ]
Quoted 5 lines
mm/damon/core.c +0
-1@@ -783,17 +781,25 @@ static void damon_do_apply_schemes(struct damon_ctx *c, r->ar.start, DAMON_MIN_REGION);if (!sz) { if (damon_sz_region(r) <= -DAMON_MIN_REGION )mm/damon/core.c static void damon_do_apply_schemes(struct damon_ctx *c, · line 792
Quoted 10 lines
mm/damon/core.c +4
-1-continue ; + DAMON_MIN_REGION) { + skip = true; + goto clean_charge_from_skip; + } sz =DAMON_MIN_REGION ; }damon_split_region_at (t, r, sz); r =damon_next_region (r); sz =damon_sz_region (r);[Severity: High]
Since this patch removes the early check for the last region, what happens indamon_do_apply_schemes()ifdamon_split_region_at()fails on the last region?If
damon_split_region_at()fails under memory pressure, it returns silently without splitting the region. The code then unconditionally calls r =damon_next_region(r).
Quoted 8 lines
Because r was the last region and was not split, its list.next points to the list head.
damon_next_region()then returns an invalid pointer.The next line calls
damon_sz_region(r), which dereferences this invalid pointer and causes an out-of-bounds memory access.Does this code need a check to ensure the region was successfully split before advancing the pointer?
The out-of-bounds memory access is indeed occurs due to this patch. But the lack of damon_split_region_at() error handling was anyway a bug. So, in a sense, pre-existing issue. I will work on fixing it separately.
Quoted 3 lines
Signature · 2 lines
-- Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thanks,
SJ
Sasha Levin
maintainerRe: [PATCH 6.1.y] mm/damon/core: fix unconditionally skip last region
In reply to SJ ParkQuoted 2 lines
Fix this by only resetting the charge_{target,addr}_from when last region is reached, only skipping when it is applied or cannot split.
I'll hold this until the damon_split_region_at() failure handling fix you mentioned is posted, and then take 6.12, 6.6 and 6.1 together with it.
Signature · 2 lines
Thanks, Sasha
SJ Park
maintainerv2 cover mm/damon/core: fix last region handling of damos_skip_charged_regions()
In reply to [email protected]Patch 1 is a dependency of patch 2. Without it, patch 2 introduces out-of-bounds memory access bug that was found by Sashiko. Patch 2 fixes a bug that categorized to be backported to stable@.
Changes from v1
- v1: https://lore.kernel.org/[email protected]
- Fix out-of-bounds memory access bug by adding patch 1.
Liew Rui Yan (1):
mm/damon/core: fix unconditionally skip last region
SeongJae Park (1):
mm/damon/core: do non-safe region walk on kdamond_apply_schemes()
mm/damon/core.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)
base-commit: af13aea0793685297faa77d75b825f0e5a99319c
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH v2 1/2 mm/damon/core: do non-safe region walk on kdamond_apply_schemes()
From: SeongJae Park <[email protected]>
kdamond_apply_schemes() is using damon_for_each_region_safe(), which is safe for deallocation of the region inside the loop. However, the loop internal logic does not deallocate regions. Hence it is only wasting the next pointer. Also, it causes a problem.
When an address filter is applied, and there is a region that intersects with the filter, the filter splits the region on the filter boundary. The intention is to let DAMOS apply action to only filtered-in address ranges. However, it is using damon_for_each_region_safe(), which sets the next region before the execution of the iteration. Hence, the region that split and now will be next to the previous region, is simply ignored. As a result, DAMOS applies the action to target regions bit slower than expected, when the address filter is used. Shouldn't be a big problem but definitely better to be fixed. damos_skip_charged_region() was working around the issue using a double pointer hack.
Use damon_for_each_region(), which is safe for this use case. And drop the work around in damos_skip_charged_region().
--- mm/damon/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/damon/core.c +2 -2
@@ -858,7 +858,7 @@ static void damos_set_effective_quota(struct damos_quota *quota)static void kdamond_apply_schemes (struct damon_ctx *c) {struct damon_target *t; -struct damon_region *r, *next_r; +struct damon_region *r;struct damos *s;damon_for_each_scheme (s, c) {
@@ -912,7 +912,7 @@ static void kdamond_apply_schemes(struct damon_ctx *c) }damon_for_each_target (t, c) { -damon_for_each_region_safe (r, next_r, t) +damon_for_each_region (r, t)damon_do_apply_schemes (c, t, r); } }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 6.1.y v2 1/2] mm/damon/core: do non-safe region walk on kdamond_apply_schemes()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] mm/damon: double processing and skipped regions due to broken backport --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm/damon: double processing and skipped regions due to broken backport --
commit 18797502eb0b5cf3a96dbf53e3e238e6ea4158ce
Author: SeongJae Park <[email protected]>
mm/damon/core: do non-safe region walk on kdamond_apply_schemes()
This commit replaces the damon_for_each_region_safe iterator with damon_for_each_region in kdamond_apply_schemes. This change ensures that newly split regions from address filters are properly processed during the iteration.
Quoted 4 lines
mm/damon/core.c +0
-0
[ ... ]
mm/damon/core.c static void kdamond_apply_schemes(struct damon_ctx *c) · line 915
Quoted 6 lines
mm/damon/core.c +1
-1@@ -912,7 +912,7 @@ static void kdamond_apply_schemes(struct damon_ctx *c) }damon_for_each_target (t, c) { -damon_for_each_region_safe (r, next_r, t) +damon_for_each_region (r, t)
[Severity: High]
Does this change result in regions being skipped and double-processed?
The commit message mentions dropping the workaround in damos_skip_charged_region(), but in this stable kernel version, that helper does not exist yet and the workaround is still inlined in damon_do_apply_schemes(). Since the inline workaround wasn't removed in this backport, it appears to conflict with the new loop iterator.
If a scheme's quota->charge_addr_from check triggers a region split in damon_do_apply_schemes(), the local pointer is advanced:
mm/damon/core.c:damon_do_apply_schemes() {
...
if (quota->charge_addr_from && r->ar.start <
quota->charge_addr_from) {
sz = ALIGN_DOWN(quota->charge_addr_from -
r->ar.start, DAMON_MIN_REGION);
if (!sz) {
if (damon_sz_region(r) <=
DAMON_MIN_REGION)
continue;
sz = DAMON_MIN_REGION;
}
damon_split_region_at(t, r, sz);
r = damon_next_region(r);
sz = damon_sz_region(r);
}
...
}Because r is reassigned locally within the inner scheme loop, subsequent schemes will apply their actions to the newly created second-half region, completely skipping the first half.
Then, when damon_do_apply_schemes() returns to the outer loop in kdamond_apply_schemes():
damon_for_each_region(r, t) damon_do_apply_schemes(c, t, r);
The iterator will advance naturally to the newly split second-half region. This causes damon_do_apply_schemes() to be called a second time on the exact same region, leading to DAMON actions being applied multiple times to the same memory, double-charged quotas, and corrupted statistics.
Quoted 3 lines
mm/damon/core.c +0
-0damon_do_apply_schemes (c, t, r); } }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
SJ Park
maintainerRe: [PATCH 6.1.y v2 1/2] mm/damon/core: do non-safe region walk on kdamond_apply_schemes()
Quoted 31 lines · On Fri, 09 Oct 2026 14:56:56 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm/damon: double processing and skipped regions due to broken backport --commit
18797502eb0b5cf3a96dbf53e3e238e6ea4158ce
Author: SeongJae Park <[email protected]>
mm/damon/core: do non-safe region walk onkdamond_apply_schemes()This commit replaces the
damon_for_each_region_safeiterator withdamon_for_each_regioninkdamond_apply_schemes. This change ensures that newly split regions from address filters are properly processed during the iteration.Quoted 4 lines
mm/damon/core.c +0
-0[ ... ]
mm/damon/core.c static void kdamond_apply_schemes(struct damon_ctx *c) · line 915
Quoted 6 lines
mm/damon/core.c +1
-1@@ -912,7 +912,7 @@ static void kdamond_apply_schemes(struct damon_ctx *c) }damon_for_each_target (t, c) { -damon_for_each_region_safe (r, next_r, t) +damon_for_each_region (r, t)[Severity: High]
Does this change result in regions being skipped and double-processed?
Quoted 5 lines
The commit message mentions dropping the workaround in
damos_skip_charged_region(), but in this stable kernel version, that helper does not exist yet and the workaround is still inlined indamon_do_apply_schemes(). Since the inline workaround wasn't removed in this backport, it appears to conflict with the new loop iterator.
Good catch. I will fix this in the next spin.
Quoted 30 lines
If a scheme's
quota->charge_addr_fromcheck triggers a region split indamon_do_apply_schemes(), the local pointer is advanced:mm/damon/core.c:damon_do_apply_schemes() { ... if (quota->charge_addr_from && r->ar.start < quota->charge_addr_from) { sz = ALIGN_DOWN(quota->charge_addr_from - r->ar.start, DAMON_MIN_REGION); if (!sz) { if (damon_sz_region(r) <= DAMON_MIN_REGION) continue; sz = DAMON_MIN_REGION; } damon_split_region_at(t, r, sz); r = damon_next_region(r); sz = damon_sz_region(r); } ... }Because r is reassigned locally within the inner scheme loop, subsequent schemes will apply their actions to the newly created second-half region, completely skipping the first half.
Then, when
damon_do_apply_schemes()returns to the outer loop inkdamond_apply_schemes():
Quoted 7 lines
damon_for_each_region(r, t) damon_do_apply_schemes(c, t, r);The iterator will advance naturally to the newly split second-half region. This causes
damon_do_apply_schemes()to be called a second time on the exact same region, leading to DAMON actions being applied multiple times to the same memory, double-charged quotas, and corrupted statistics.
This is an issue that exist in the mainline, too. I will separately work on it.
Quoted 7 lines
Quoted 4 lines
mm/damon/core.c +0
-0damon_do_apply_schemes (c, t, r); } }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thanks,
SJ
SJ Park
maintainerPATCH v2 2/2 mm/damon/core: fix unconditionally skip last region
In reply to SJ ParkFrom: Liew Rui Yan <[email protected]>
Once quota set, the charge_{target,addr}_from unconditionally skips and resets at the last region of the tracked target, so the last region can be skipped even when it has not been processed.
Example:
1. Target has 2 regions: R1 (0-100 bytes) and R2 (100-200 bytes).
2. Quota is configured to process only 100 bytes per window.
3. Window 1: Processes R1 (0-100). Quota is full. charge_{target, addr}_from is saved at (Target, 100).
4. Window 2: The loop reaches R2. Because R2 is damon_last_region(t), the old code unconditionally returns true, skipping R2 entirely and resetting the charge_{target,addr}_from.
Result: R2 is permanently skipped even though it has never been processed.
However, it is important to note that this is a very minor issue. This is because it is triggered only when the previous window saved/kept charge_{target,addr}_from, and in the next window, all regions except the last region were skipped by damos_skip_charged_region().
Fix this by only resetting the charge_{target,addr}_from when last region is reached, only skipping when it is applied or cannot split.
--- mm/damon/core.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
mm/damon/core.c +15 -9
@@ -756,6 +756,7 @@ static void damon_do_apply_schemes(struct damon_ctx *c,unsigned long sz =damon_sz_region (r);struct timespec64 begin, end;unsigned long sz_applied =0 ; +bool skip = false;if (!s->wmarks.activated)continue ;
@@ -768,14 +769,11 @@ static void damon_do_apply_schemes(struct damon_ctx *c,if (quota->charge_target_from) {if (t != quota->charge_target_from)continue ; -if (r ==damon_last_region (t)) { - quota->charge_target_from =NULL ; - quota->charge_addr_from =0 ; -continue ; - }if (quota->charge_addr_from && - r->ar.end <= quota->charge_addr_from) -continue ; + r->ar.end <= quota->charge_addr_from) { + skip = true; + goto clean_charge_from_skip; + }if (quota->charge_addr_from && r->ar.start < quota->charge_addr_from) {
@@ -783,17 +781,25 @@ static void damon_do_apply_schemes(struct damon_ctx *c, r->ar.start, DAMON_MIN_REGION);if (!sz) {if (damon_sz_region (r) <= -DAMON_MIN_REGION ) -continue ; +DAMON_MIN_REGION ) { + skip = true; + goto clean_charge_from_skip; + } sz =DAMON_MIN_REGION ; }damon_split_region_at (t, r, sz); r =damon_next_region (r); sz =damon_sz_region (r); + skip = false; } + } +clean_charge_from_skip: +if (r ==damon_last_region (t)) { quota->charge_target_from =NULL ; quota->charge_addr_from =0 ; } +if (skip) +continue ;if (!damos_valid_target (c, t, r, s))continue ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 6.1.y v2 2/2] mm/damon/core: fix unconditionally skip last region
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] mm/damon: Out-of-bounds access due to unchecked split failure on last region --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm/damon: Out-of-bounds access due to unchecked split failure on last region --
commit aacf3c387b1624c2944e2e5044dad18623db3d5f
Author: Liew Rui Yan <[email protected]>
mm/damon/core: fix unconditionally skip last region
This commit modifies the quota mechanism in DAMON to only reset the charge target and address from when the last region is reached. It ensures the last region is not skipped if it has not been processed.
Quoted 4 lines
mm/damon/core.c +0
-0
[ ... ]
Quoted 24 lines
mm/damon/core.c +4
-8@@ -768,14 +769,11 @@ static void damon_do_apply_schemes(struct damon_ctx *c,if (quota->charge_target_from) {if (t != quota->charge_target_from)continue ; -if (r ==damon_last_region (t)) { - quota->charge_target_from =NULL ; - quota->charge_addr_from =0 ; -continue ; - }if (quota->charge_addr_from && - r->ar.end <= quota->charge_addr_from) -continue ; + r->ar.end <= quota->charge_addr_from) { + skip = true; + goto clean_charge_from_skip; + }if (quota->charge_addr_from && r->ar.start < quota->charge_addr_from) {@@ -783,17 +781,25 @@ static void damon_do_apply_schemes(struct damon_ctx *c, r->ar.start, DAMON_MIN_REGION);if (!sz) { if (damon_sz_region(r) <= -DAMON_MIN_REGION )
mm/damon/core.c static void damon_do_apply_schemes(struct damon_ctx *c, · line 792
Quoted 10 lines
mm/damon/core.c +4
-1-continue ; + DAMON_MIN_REGION) { + skip = true; + goto clean_charge_from_skip; + } sz =DAMON_MIN_REGION ; }damon_split_region_at (t, r, sz); r =damon_next_region (r); sz =damon_sz_region (r);
[Severity: High]
Since this commit removes the check that bypassed damon_split_region_at() for the target's last region, does this introduce an out-of-bounds access if memory allocation fails?
If the last region overlaps the charge boundary, it is now passed to damon_split_region_at(). This function performs a non-blocking memory allocation that can fail under memory pressure and return silently without splitting the region:
mm/damon/core.c:damon_split_region_at() {
new = damon_new_region(r->ar.start + sz_r, r->ar.end);
if (!new)
return;
...
}If the allocation fails when processing the last region, the region is not split. The code then unconditionally executes:
r = damon_next_region(r);
sz = damon_sz_region(r);Because r was the last region and wasn't split, damon_next_region() reads the list.next pointer which points back to the list head:
include/linux/damon.h:damon_next_region() {
return container_of(r->list.next, struct damon_region, list);
}This produces an out-of-bounds pointer derived from the list head. When this pointer is immediately dereferenced by damon_sz_region(r), could this regression result in out-of-bounds memory reads?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
SJ Park
maintainerRe: [PATCH 6.1.y v2 2/2] mm/damon/core: fix unconditionally skip last region
Quoted 81 lines · On Fri, 09 Oct 2026 15:04:56 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm/damon: Out-of-bounds access due to unchecked split failure on last region --commit
aacf3c387b1624c2944e2e5044dad18623db3d5f
Author: Liew Rui Yan <[email protected]>
mm/damon/core: fix unconditionally skip last regionThis commit modifies the quota mechanism in DAMON to only reset the charge target and address from when the last region is reached. It ensures the last region is not skipped if it has not been processed.
Quoted 4 lines
mm/damon/core.c +0
-0[ ... ]
Quoted 24 lines
mm/damon/core.c +4
-8@@ -768,14 +769,11 @@ static void damon_do_apply_schemes(struct damon_ctx *c,if (quota->charge_target_from) {if (t != quota->charge_target_from)continue ; -if (r ==damon_last_region (t)) { - quota->charge_target_from =NULL ; - quota->charge_addr_from =0 ; -continue ; - }if (quota->charge_addr_from && - r->ar.end <= quota->charge_addr_from) -continue ; + r->ar.end <= quota->charge_addr_from) { + skip = true; + goto clean_charge_from_skip; + }if (quota->charge_addr_from && r->ar.start < quota->charge_addr_from) {@@ -783,17 +781,25 @@ static void damon_do_apply_schemes(struct damon_ctx *c, r->ar.start, DAMON_MIN_REGION);if (!sz) { if (damon_sz_region(r) <= -DAMON_MIN_REGION )mm/damon/core.c static void damon_do_apply_schemes(struct damon_ctx *c, · line 792
Quoted 10 lines
mm/damon/core.c +4
-1-continue ; + DAMON_MIN_REGION) { + skip = true; + goto clean_charge_from_skip; + } sz =DAMON_MIN_REGION ; }damon_split_region_at (t, r, sz); r =damon_next_region (r); sz =damon_sz_region (r);[Severity: High]
Since this commit removes the check that bypasseddamon_split_region_at()for the target's last region, does this introduce an out-of-bounds access if memory allocation fails?If the last region overlaps the charge boundary, it is now passed to
damon_split_region_at(). This function performs a non-blocking memory allocation that can fail under memory pressure and return silently without splitting the region:mm/damon/core.c:damon_split_region_at() { new = damon_new_region(r->ar.start + sz_r, r->ar.end); if (!new) return; ... }If the allocation fails when processing the last region, the region is not split. The code then unconditionally executes:
r = damon_next_region(r); sz = damon_sz_region(r);Because r was the last region and wasn't split,
damon_next_region()reads the list.next pointer which points back to the list head:
Quoted 7 lines
include/linux/damon.h:damon_next_region() { return container_of(r->list.next, struct damon_region, list); }This produces an out-of-bounds pointer derived from the list head. When this pointer is immediately dereferenced by
damon_sz_region(r), could this regression result in out-of-bounds memory reads?
Good finding. Patch 1 should avoided this issue by removing the safe iterator workaround, but I mistakenly didn't remove the workaround. I will fix it in the next spin.
Quoted 3 lines
Signature · 2 lines
-- Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Thanks,
SJ
SJ Park
maintainerv3 cover mm/damon/core: fix last region handling of damos_skip_charged_regions()
In reply to [email protected]Patch 1 is a dependency of patch 2. Without it, patch 2 introduces out-of-bounds memory access bug that was found by Sashiko. Patch 2 fixes a bug that categorized to be backported to stable@.
Changes from v2
- v2: https://lore.kernel.org/[email protected]
- Drop the safe region traversal workaround in patch 1. Changes from v1
- v1: https://lore.kernel.org/[email protected]
- Fix out-of-bounds memory access bug by adding patch 1.
Liew Rui Yan (1):
mm/damon/core: fix unconditionally skip last region
SeongJae Park (1):
mm/damon/core: do non-safe region walk on kdamond_apply_schemes()
mm/damon/core.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-)
base-commit: 5f9615e6a3b5f768df8b69ab622454fcb3af0a34
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH v3 1/2 mm/damon/core: do non-safe region walk on kdamond_apply_schemes()
From: SeongJae Park <[email protected]>
kdamond_apply_schemes() is using damon_for_each_region_safe(), which is safe for deallocation of the region inside the loop. However, the loop internal logic does not deallocate regions. Hence it is only wasting the next pointer. Also, it causes a problem.
When an address filter is applied, and there is a region that intersects with the filter, the filter splits the region on the filter boundary. The intention is to let DAMOS apply action to only filtered-in address ranges. However, it is using damon_for_each_region_safe(), which sets the next region before the execution of the iteration. Hence, the region that split and now will be next to the previous region, is simply ignored. As a result, DAMOS applies the action to target regions bit slower than expected, when the address filter is used. Shouldn't be a big problem but definitely better to be fixed. damos_skip_charged_region() was working around the issue using a double pointer hack.
Use damon_for_each_region(), which is safe for this use case. And drop the work around in damos_skip_charged_region().
--- mm/damon/core.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
mm/damon/core.c +3 -4
@@ -788,8 +788,7 @@ static void damon_do_apply_schemes(struct damon_ctx *c, sz =DAMON_MIN_REGION ; }damon_split_region_at (t, r, sz); - r =damon_next_region (r); - sz =damon_sz_region (r); +continue ; } quota->charge_target_from =NULL ; quota->charge_addr_from =0 ;
@@ -858,7 +857,7 @@ static void damos_set_effective_quota(struct damos_quota *quota)static void kdamond_apply_schemes (struct damon_ctx *c) {struct damon_target *t; -struct damon_region *r, *next_r; +struct damon_region *r;struct damos *s;damon_for_each_scheme (s, c) {
@@ -912,7 +911,7 @@ static void kdamond_apply_schemes(struct damon_ctx *c) }damon_for_each_target (t, c) { -damon_for_each_region_safe (r, next_r, t) +damon_for_each_region (r, t)damon_do_apply_schemes (c, t, r); } }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 6.1.y v3 1/2] mm/damon/core: do non-safe region walk on kdamond_apply_schemes()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] mm/damon: double-application of earlier schemes to newly split regions --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm/damon: double-application of earlier schemes to newly split regions --
commit 01d768c4d318527360ef5913771d20f22a99984a
Author: SeongJae Park <[email protected]>
mm/damon/core: do non-safe region walk on kdamond_apply_schemes()
kdamond_apply_schemes() is using damon_for_each_region_safe(), which is safe for deallocation of the region inside the loop. However, the loop internal logic does not deallocate regions. Hence it is only wasting the next pointer.
Quoted 4 lines
mm/damon/core.c +0
-0
[ ... ]
mm/damon/core.c static void kdamond_apply_schemes(struct damon_ctx *c) · line 915
Quoted 7 lines
mm/damon/core.c +1
-1@@ -912,7 +911,7 @@ static void kdamond_apply_schemes(struct damon_ctx *c) }damon_for_each_target (t, c) { -damon_for_each_region_safe (r, next_r, t) +damon_for_each_region (r, t)damon_do_apply_schemes (c, t, r);
[Severity: High]
Does this code cause earlier schemes to be applied twice to newly split regions?
If multiple schemes are configured, an earlier scheme (e.g. Scheme 0) might apply its action to a full, un-split region. If a later scheme (e.g. Scheme 1) then decides to split the region, damon_split_region_at() inserts the newly split part into the list immediately after the current region.
Because this loop in kdamond_apply_schemes() now uses damon_for_each_region() instead of the safe variant, the outer loop will advance to this newly split region and call damon_do_apply_schemes() on it again.
Will this cause damon_do_apply_schemes() to evaluate the new region from the beginning of the scheme list, resulting in Scheme 0 executing its memory management actions (like madvise) and counting statistics a second time on the same memory range?
Quoted 2 lines
mm/damon/core.c +0
-0} }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
SJ Park
maintainerRe: [PATCH 6.1.y v3 1/2] mm/damon/core: do non-safe region walk on kdamond_apply_schemes()
Quoted 45 lines · On Sat, 10 Oct 2026 06:41:56 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm/damon: double-application of earlier schemes to newly split regions --commit
01d768c4d318527360ef5913771d20f22a99984a
Author: SeongJae Park <[email protected]>
mm/damon/core: do non-safe region walk onkdamond_apply_schemes()
kdamond_apply_schemes()is usingdamon_for_each_region_safe(), which is safe for deallocation of the region inside the loop. However, the loop internal logic does not deallocate regions. Hence it is only wasting the next pointer.Quoted 4 lines
mm/damon/core.c +0
-0[ ... ]
mm/damon/core.c static void kdamond_apply_schemes(struct damon_ctx *c) · line 915
Quoted 7 lines
mm/damon/core.c +1
-1@@ -912,7 +911,7 @@ static void kdamond_apply_schemes(struct damon_ctx *c) }damon_for_each_target (t, c) { -damon_for_each_region_safe (r, next_r, t) +damon_for_each_region (r, t)damon_do_apply_schemes (c, t, r);[Severity: High]
Does this code cause earlier schemes to be applied twice to newly split regions?If multiple schemes are configured, an earlier scheme (e.g. Scheme 0) might apply its action to a full, un-split region. If a later scheme (e.g. Scheme 1) then decides to split the region,
damon_split_region_at()inserts the newly split part into the list immediately after the current region.Because this loop in
kdamond_apply_schemes()now usesdamon_for_each_region()instead of the safe variant, the outer loop will advance to this newly split region and calldamon_do_apply_schemes()on it again.
Quoted 4 lines
Will this cause
damon_do_apply_schemes()to evaluate the new region from the beginning of the scheme list, resulting in Scheme 0 executing its memory management actions (like madvise) and counting statistics a second time on the same memory range?
The same issue exists in the mainline. The user impact is minor. No blocker of this patch in my opinion. I will separately work on it.
Quoted 6 lines
Quoted 3 lines
mm/damon/core.c +0
-0} }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thanks,
SJ
SJ Park
maintainerPATCH v3 2/2 mm/damon/core: fix unconditionally skip last region
In reply to SJ ParkFrom: Liew Rui Yan <[email protected]>
Once quota set, the charge_{target,addr}_from unconditionally skips and resets at the last region of the tracked target, so the last region can be skipped even when it has not been processed.
Example:
1. Target has 2 regions: R1 (0-100 bytes) and R2 (100-200 bytes).
2. Quota is configured to process only 100 bytes per window.
3. Window 1: Processes R1 (0-100). Quota is full. charge_{target, addr}_from is saved at (Target, 100).
4. Window 2: The loop reaches R2. Because R2 is damon_last_region(t), the old code unconditionally returns true, skipping R2 entirely and resetting the charge_{target,addr}_from.
Result: R2 is permanently skipped even though it has never been processed.
However, it is important to note that this is a very minor issue. This is because it is triggered only when the previous window saved/kept charge_{target,addr}_from, and in the next window, all regions except the last region were skipped by damos_skip_charged_region().
Fix this by only resetting the charge_{target,addr}_from when last region is reached, only skipping when it is applied or cannot split.
--- mm/damon/core.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
mm/damon/core.c +15 -10
@@ -756,6 +756,7 @@ static void damon_do_apply_schemes(struct damon_ctx *c,unsigned long sz =damon_sz_region (r);struct timespec64 begin, end;unsigned long sz_applied =0 ; +bool skip = false;if (!s->wmarks.activated)continue ;
@@ -768,14 +769,11 @@ static void damon_do_apply_schemes(struct damon_ctx *c,if (quota->charge_target_from) {if (t != quota->charge_target_from)continue ; -if (r ==damon_last_region (t)) { - quota->charge_target_from =NULL ; - quota->charge_addr_from =0 ; -continue ; - }if (quota->charge_addr_from && - r->ar.end <= quota->charge_addr_from) -continue ; + r->ar.end <= quota->charge_addr_from) { + skip = true; + goto clean_charge_from_skip; + }if (quota->charge_addr_from && r->ar.start < quota->charge_addr_from) {
@@ -783,16 +781,23 @@ static void damon_do_apply_schemes(struct damon_ctx *c, r->ar.start, DAMON_MIN_REGION);if (!sz) {if (damon_sz_region (r) <= -DAMON_MIN_REGION ) -continue ; +DAMON_MIN_REGION ) { + skip = true; + goto clean_charge_from_skip; + } sz =DAMON_MIN_REGION ; }damon_split_region_at (t, r, sz); -continue ; + skip = true; } + } +clean_charge_from_skip: +if (r ==damon_last_region (t)) { quota->charge_target_from =NULL ; quota->charge_addr_from =0 ; } +if (skip) +continue ;if (!damos_valid_target (c, t, r, s))continue ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 6.1.y v3 2/2] mm/damon/core: fix unconditionally skip last region
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] mm: damon: use-after-free and permanent scheme disablement on target destroy --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm: damon: use-after-free and permanent scheme disablement on target destroy --
commit c97d3ff78284ee81310d99deee85b79f32b561d5
Author: Liew Rui Yan <[email protected]>
mm/damon/core: fix unconditionally skip last region
Once quota set, the charge_{target,addr}_from unconditionally skips and resets at the last region of the tracked target, so the last region can be skipped even when it has not been processed.
mm/damon/core.c static void damon_do_apply_schemes(struct damon_ctx *c, · line 771
Quoted 8 lines
mm/damon/core.c +0
-0@@ -768,14 +769,11 @@ static void damon_do_apply_schemes(struct damon_ctx *c,if (quota->charge_target_from) {if (t != quota->charge_target_from)continue ;
[Severity: High]
Does this code have a use-after-free and permanent scheme disablement issue when a target is destroyed?
If the user dynamically removes a target via sysfs, damon_sysfs_set_targets() calls damon_destroy_target() which frees the target memory:
mm/damon/sysfs.c:damon_sysfs_set_targets() {
...
} else {
if (damon_target_has_pid(ctx))
put_pid(t->pid);
damon_destroy_target(t);
}
...
}mm/damon/core.c:damon_destroy_target() {
damon_del_target(t);
damon_free_target(t);
}Existing schemes are updated in-place via damon_sysfs_update_scheme(), which does not clear the charge_target_from pointer, leaving a dangling pointer in the scheme.
Immediately after, when kdamond_apply_schemes() runs in the same thread, damon_do_apply_schemes() compares the valid remaining targets against this dangling pointer (if (t != quota->charge_target_from)). Since it does not match the remaining targets, it unconditionally executes continue, skipping the scheme for all regions.
Because a matching target is never reached, the pointer is never reset (the code cannot reach the clean_charge_from_skip label). This results in permanent disablement of the scheme and persistent use-after-free comparisons against the dangling pointer.
Quoted 12 lines
mm/damon/core.c +4
-7-if (r ==damon_last_region (t)) { - quota->charge_target_from =NULL ; - quota->charge_addr_from =0 ; -continue ; - }if (quota->charge_addr_from && - r->ar.end <= quota->charge_addr_from) -continue ; + r->ar.end <= quota->charge_addr_from) { + skip = true; + goto clean_charge_from_skip; + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
SJ Park
maintainerRe: [PATCH 6.1.y v3 2/2] mm/damon/core: fix unconditionally skip last region
Quoted 56 lines · On Sat, 10 Oct 2026 06:46:00 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm: damon: use-after-free and permanent scheme disablement on target destroy --commit
c97d3ff78284ee81310d99deee85b79f32b561d5
Author: Liew Rui Yan <[email protected]>
mm/damon/core: fix unconditionally skip last regionOnce quota set, the charge_{target,addr}_from unconditionally skips and resets at the last region of the tracked target, so the last region can be skipped even when it has not been processed.
mm/damon/core.c static void damon_do_apply_schemes(struct damon_ctx *c, · line 771
Quoted 8 lines
mm/damon/core.c +0
-0@@ -768,14 +769,11 @@ static void damon_do_apply_schemes(struct damon_ctx *c,if (quota->charge_target_from) {if (t != quota->charge_target_from)continue ;[Severity: High]
Does this code have a use-after-free and permanent scheme disablement issue when a target is destroyed?If the user dynamically removes a target via sysfs,
damon_sysfs_set_targets()callsdamon_destroy_target()which frees the target memory:mm/damon/sysfs.c:damon_sysfs_set_targets() { ... } else { if (damon_target_has_pid(ctx)) put_pid(t->pid); damon_destroy_target(t); } ... }mm/damon/core.c:damon_destroy_target() { damon_del_target(t); damon_free_target(t); }Existing schemes are updated in-place via
damon_sysfs_update_scheme(), which does not clear thecharge_target_frompointer, leaving a dangling pointer in the scheme.Immediately after, when
kdamond_apply_schemes()runs in the same thread,damon_do_apply_schemes()compares the valid remaining targets against this dangling pointer (if (t !=quota->charge_target_from)). Since it does not match the remaining targets, it unconditionally executes continue, skipping the scheme for all regions.
Quoted 4 lines
Because a matching target is never reached, the pointer is never reset (the code cannot reach the
clean_charge_from_skiplabel). This results in permanent disablement of the scheme and persistent use-after-free comparisons against the dangling pointer.
This is a pre-existing issue that also exists in the mainline. I will separately work on this.
Quoted 16 lines
Quoted 13 lines
mm/damon/core.c +4
-7-if (r ==damon_last_region (t)) { - quota->charge_target_from =NULL ; - quota->charge_addr_from =0 ; -continue ; - }if (quota->charge_addr_from && - r->ar.end <= quota->charge_addr_from) -continue ; + r->ar.end <= quota->charge_addr_from) { + skip = true; + goto clean_charge_from_skip; + }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Thanks,
SJ
Re: [PATCH 6.1.y v3 0/2] mm/damon/core: fix last region handling of damos_skip_charged_regions()
In reply to SJ ParkQuoted 3 lines
Patch 1 is a dependency of patch 2. Without it, patch 2 introduces out-of-bounds memory access bug that was found by Sashiko. Patch 2 fixes a bug that categorized to be backported to stable@.
Queued the series for 6.1, thanks.
Signature · 2 lines
Thanks, Sasha