mm/damon: move damos filter range arguments validation to core
Subsystems (4)
SJ Park
maintainercover mm/damon: move damos filter range arguments validation to core
DAMOS filter range arguments are being validated in the DAMON sysfs interface. Some of those have minor time-of-check to time-of-use (TOCTOU) bugs. In future, other callers might have duplicated validations with similar bugs.
Fix the bugs and further refactor the code to move the validation to the core layer. Also add kunit test cases for the validations.
Patches 1 and 2 fix the existing TOCTOU bugs. Patches 3 and 4 adds the validation in the core layer. Patch 5 drops the replicated validation in DAMON sysfs interface. Patch 6 further cleanup the code. Patches 7 and 8 extends kunit tests to test the validation.
SJ Park (8):
mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once mm/damon/sysfs-schemes: read sysfs_filter->sz_range only once mm/damon/core: return an error from damos_commit_filter_arg() mm/damon/core: disallow max < min damos filter range arguments commit mm/damon/sysfs-schemes: drop centralized filter range arg validations mm/damon/sysfs-schemes: use switch-case in add_scheme_filters() mm/damon/core-kunit: extend damos_commit_filter_for() for wrong input mm/damon/core-kunit: test invalid damos filter commits
mm/damon/core.c | 24 ++++++++++++++++------- mm/damon/sysfs-schemes.c | 32 +++++++++++++------------------ mm/damon/tests/core-kunit.h | 38 ++++++++++++++++++++++++++++--------- 3 files changed, 59 insertions(+), 35 deletions(-)
base-commit: ec1b9fab95fbfd9c5dbdfb841d91929c2cb63518
Signature · 1 line
2.47.3
SJ Park
maintainerRFC PATCH 1/8 mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once
DAMON sysfs interface reads the user-provided address range arguments for addr type DAMOS filter twice. Once for validation, and once again for assignments to the variable that will be passed to the core layer. If the user updates the argument in parallel, an invalid address range could be passed to the core layer. Avoid it by doing the assignments first, and then validating the assigned variables before passing those to the core layer.
User impact of the bug should be trivial. From the core layer's perspective, the invalid address range is not really invalid. It just works as having a weird address range. No critical issues such as a crash or a leak could happen. And sane users ain't do such parallel arguments update anyway. If they do, such racy behavior is arguably somewhat expected and deserved. That said, there is no reason to keep such races.
--- mm/damon/sysfs-schemes.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/damon/sysfs-schemes.c +3 -3
@@ -2831,12 +2831,12 @@ static int damon_sysfs_add_scheme_filters(struct damos *scheme,return err; } }else if (filter->type ==DAMOS_FILTER_TYPE_ADDR ) { -if (sysfs_filter->addr_range.end < - sysfs_filter->addr_range.start) { + filter->addr_range = sysfs_filter->addr_range; +if (filter->addr_range.end < + filter->addr_range.start) {damos_destroy_filter (filter);return -EINVAL ; } - filter->addr_range = sysfs_filter->addr_range; }else if (filter->type ==DAMOS_FILTER_TYPE_TARGET ) { filter->target_idx = sysfs_filter->target_idx; }else if (filter->type ==DAMOS_FILTER_TYPE_HUGEPAGE_SIZE ) {
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 1/8] mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once
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] A double-read of sysfs_goal->metric in damos_sysfs_add_quota_score() causes uninitialized memory use in the core DAMOS quota auto-tuning logic. --
commit 4b90a6724a684c24da0cc60a260ec280b6d479ac
Author: SJ Park <[email protected]>
Subject: mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once
This commit fixes a potential race in the DAMON sysfs interface where sysfs_filter->addr_range is read twice. By copying the address range first and validating the copy, it prevents invalid configurations from reaching the core layer.
Quoted 10 lines
mm/damon/sysfs-schemes.c +0
-2@@ -2831,12 +2831,12 @@ static int damon_sysfs_add_scheme_filters(struct damos *scheme,return err; } }else if (filter->type ==DAMOS_FILTER_TYPE_ADDR ) { -if (sysfs_filter->addr_range.end < - sysfs_filter->addr_range.start) {
mm/damon/sysfs-schemes.c static int damon_sysfs_add_scheme_filters(struct damos *scheme, · line 2842
Quoted 10 lines
mm/damon/sysfs-schemes.c +3
-1+ filter->addr_range = sysfs_filter->addr_range; +if (filter->addr_range.end < + filter->addr_range.start) {damos_destroy_filter (filter);return -EINVAL ; } - filter->addr_range = sysfs_filter->addr_range; }else if (filter->type ==DAMOS_FILTER_TYPE_TARGET ) { filter->target_idx = sysfs_filter->target_idx; }else if (filter->type ==DAMOS_FILTER_TYPE_HUGEPAGE_SIZE ) {
[ ... ]
While reviewing this fix for the double-read of addr_range, I noticed a similar pattern in the same file:
mm/damon/sysfs-schemes.c:damos_sysfs_add_quota_score() {
...
goal = damos_new_quota_goal(sysfs_goal->metric,
sysfs_goal->target_value);
if (!goal)
return -ENOMEM;
switch (sysfs_goal->metric) {
case DAMOS_QUOTA_USER_INPUT:
goal->current_value = sysfs_goal->current_value;
break;
...
}[Severity: High]
This is a pre-existing issue, but does this code double-read sysfs_goal->metric?
The sysfs_goal->metric value is read first to allocate and initialize goal->metric via damos_new_quota_goal(), and then read a second time in the switch statement.
If the metric changes concurrently from DAMOS_QUOTA_USER_INPUT to another value between these two reads, could the switch statement branch away and skip the initialization of goal->current_value?
Since kmalloc does not zero memory, wouldn't goal->current_value be left uninitialized, causing the core auto-tuning logic to read garbage data?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
SJ Park
maintainerRFC PATCH 2/8 mm/damon/sysfs-schemes: read sysfs_filter->sz_range only once
In reply to SJ ParkDAMON sysfs interface reads the user-provided size range arguments for hugepage_size type DAMOS filter twice. Once for validation, and once again for assignments to the variable that will be passed to the core layer. If the user updates the arguments in parallel, an invalid size range could be passed to the core layer. Avoid it by doing the assignments first, and then validating the assigned variables before passing those to the core layer.
User impact of the bug should be trivial. From the core layer's perspective, the invalid size range is not really invalid. It just works as having a weird size range. No critical issues such as a crash or a leak could happen. And sane users ain't do such parallel arguments update anyway. If they do, such racy behavior is arguably somewhat expected and deserved. That said, there is no reason to keep such races.
--- mm/damon/sysfs-schemes.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
mm/damon/sysfs-schemes.c +3 -4
@@ -2840,13 +2840,12 @@ static int damon_sysfs_add_scheme_filters(struct damos *scheme, }else if (filter->type ==DAMOS_FILTER_TYPE_TARGET ) { filter->target_idx = sysfs_filter->target_idx; }else if (filter->type ==DAMOS_FILTER_TYPE_HUGEPAGE_SIZE ) { -if (sysfs_filter->range_min > - sysfs_filter->range_max) { + filter->sz_range.min = sysfs_filter->range_min; + filter->sz_range.max = sysfs_filter->range_max; +if (filter->range_min > filter->range_max) {damos_destroy_filter (filter);return -EINVAL ; } - filter->sz_range.min = sysfs_filter->range_min; - filter->sz_range.max = sysfs_filter->range_max; }else if (filter->type ==DAMOS_FILTER_TYPE_PROBE_HITS_WSUM ) { filter->range_min = sysfs_filter->range_min; filter->range_max = sysfs_filter->range_max;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 2/8] mm/damon/sysfs-schemes: read sysfs_filter->sz_range only once
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 16 lines
DAMON sysfs interface reads the user-provided size range arguments for
hugepage_sizetype DAMOS filter twice. Once for validation, and once again for assignments to the variable that will be passed to the core layer. If the user updates the arguments in parallel, an invalid size range could be passed to the core layer. Avoid it by doing the assignments first, and then validating the assigned variables before passing those to the core layer.User impact of the bug should be trivial. From the core layer's perspective, the invalid size range is not really invalid. It just works as having a weird size range. No critical issues such as a crash or a leak could happen. And sane users ain't do such parallel arguments update anyway. If they do, such racy behavior is arguably somewhat expected and deserved. That said, there is no reason to keep such races.
Quoted 3 lines
Fixes ea1f204ba29a ("mm/damon/sysfs-schemes: add files for setting damos_filter->sz_range")Cc <[email protected]> # 6.15.xSigned-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
SJ Park
maintainerRFC PATCH 3/8 mm/damon/core: return an error from damos_commit_filter_arg()
In reply to SJ Parkdamos_commit_filter_arg() is supposed to always succeed. It may not in future, for example, if the given filter is invalid. Prepare the case by modifying its signature to return an error when it failed. Also pipe the return value to its callers and let them handle the error.
--- mm/damon/core.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
mm/damon/core.c +11 -7
@@ -1317,7 +1317,7 @@ static struct damos_filter *damos_nth_ops_filter(int n, struct damos *s)return NULL ; } -static void damos_commit_filter_arg ( +static int damos_commit_filter_arg (struct damos_filter *dst,struct damos_filter *src) {switch (dst->type) {
@@ -1340,28 +1340,32 @@ static void damos_commit_filter_arg(default :break ; } +return 0 ; } -static void damos_commit_filter ( +static int damos_commit_filter (struct damos_filter *dst,struct damos_filter *src) { dst->type = src->type; dst->matching = src->matching; dst->allow = src->allow; -damos_commit_filter_arg (dst, src); +return damos_commit_filter_arg (dst, src); }static int damos_commit_core_filters (struct damos *dst,struct damos *src) {struct damos_filter *dst_filter, *next, *src_filter, *new_filter; -int i =0 , j =0 ; +int i =0 , j =0 , err;damos_for_each_core_filter_safe (dst_filter, next, dst) { src_filter =damos_nth_core_filter (i++, src); -if (src_filter) -damos_commit_filter (dst_filter, src_filter); -else +if (src_filter) { + err =damos_commit_filter (dst_filter, src_filter); +if (err) +return err; + }else {damos_destroy_filter (dst_filter); + } }damos_for_each_core_filter_safe (src_filter, next, src) {
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 3/8] mm/damon/core: return an error from damos_commit_filter_arg()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Unhandled error returns in DAMOS filter commit functions lead to uninitialized memory use and type…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unhandled error returns in DAMOS filter commit functions lead to uninitialized memory use and type confusion. --
commit f2946ab5f9d35b397a925510dc71e57f307f11d2
Author: SJ Park <[email protected]>
mm/damon/core: return an error from damos_commit_filter_arg()
damos_commit_filter_arg() is supposed to always succeed. It may not in future, for example, if the given filter is invalid. Prepare the case by modifying its signature to return an error when it failed. Also pipe the return value to its callers and let them handle the error.
Quoted 4 lines
mm/damon/core.c +0
-0
[ ... ]
mm/damon/core.c static void damos_commit_filter_arg( · line 1353
Quoted 10 lines
mm/damon/core.c +2
-2-static void damos_commit_filter ( +static int damos_commit_filter (struct damos_filter *dst,struct damos_filter *src) { dst->type = src->type; dst->matching = src->matching; dst->allow = src->allow; -damos_commit_filter_arg (dst, src); +return damos_commit_filter_arg (dst, src); }
[Severity: High]
Does this code leave the target filter in a type-confused state if damos_commit_filter_arg() fails?
Since dst->type is modified before the arguments are committed, if an error is returned and subsequently ignored by the caller, the filter could have a new type but mismatched union arguments from the old type.
Quoted 11 lines
static int
damos_commit_core_filters(struct damos *dst, struct damos *src){mm/damon/core.c +1
-4struct damos_filter *dst_filter, *next, *src_filter, *new_filter; -int i =0 , j =0 ; +int i =0 , j =0 , err;damos_for_each_core_filter_safe (dst_filter, next, dst) { src_filter =damos_nth_core_filter (i++, src); -if (src_filter) -damos_commit_filter (dst_filter, src_filter); -else
mm/damon/core.c static void damos_commit_filter_arg( · line 1371
Quoted 10 lines
mm/damon/core.c +6
-0+if (src_filter) { + err =damos_commit_filter (dst_filter, src_filter); +if (err) +return err; + }else {damos_destroy_filter (dst_filter); + } }damos_for_each_core_filter_safe (src_filter, next, src) {
[Severity: High]
Does this code ignore the return value of damos_commit_filter_arg() when creating new core filters?
Further down in this loop:
mm/damon/core.c:damos_commit_core_filters() {
...
new_filter = damos_new_filter(
src_filter->type, src_filter->matching,
src_filter->allow);
if (!new_filter)
return -ENOMEM;
damos_commit_filter_arg(new_filter, src_filter);
damos_add_filter(dst, new_filter);
...
}If damos_commit_filter_arg() fails, does this silently add an uninitialized filter with garbage union arguments to the destination scheme?
[Severity: High]
Are there also missing error checks in damos_commit_ops_filters()?
It appears damos_commit_ops_filters() wasn't updated in this patch to check the new integer return values from damos_commit_filter() or damos_commit_filter_arg():
mm/damon/core.c:damos_commit_ops_filters() {
...
damos_for_each_ops_filter_safe(dst_filter, next, dst) {
src_filter = damos_nth_ops_filter(i++, src);
if (src_filter)
damos_commit_filter(dst_filter, src_filter);
...
damos_for_each_ops_filter_safe(src_filter, next, src) {
...
damos_commit_filter_arg(new_filter, src_filter);
damos_add_filter(dst, new_filter);
...
}Can ignoring these errors leave the ops filter in a type-confused state or silently add uninitialized filters to the destination scheme?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
SJ Park
maintainerRe: [RFC PATCH 3/8] mm/damon/core: return an error from damos_commit_filter_arg()
Quoted 34 lines · On Sat, 12 Sep 2026 19:29:16 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unhandled error returns in DAMOS filter commit functions lead to uninitialized memory use and type confusion. --commit
f2946ab5f9d35b397a925510dc71e57f307f11d2
Author: SJ Park <[email protected]>
mm/damon/core: return an error fromdamos_commit_filter_arg()
damos_commit_filter_arg()is supposed to always succeed. It may not in future, for example, if the given filter is invalid. Prepare the case by modifying its signature to return an error when it failed. Also pipe the return value to its callers and let them handle the error.Quoted 4 lines
mm/damon/core.c +0
-0[ ... ]
mm/damon/core.c static void damos_commit_filter_arg( · line 1353
Quoted 10 lines
mm/damon/core.c +2
-2-static void damos_commit_filter ( +static int damos_commit_filter (struct damos_filter *dst,struct damos_filter *src) { dst->type = src->type; dst->matching = src->matching; dst->allow = src->allow; -damos_commit_filter_arg (dst, src); +return damos_commit_filter_arg (dst, src); }[Severity: High]
Does this code leave the target filter in a type-confused state ifdamos_commit_filter_arg()fails?
Quoted 3 lines
Since
dst->typeis modified before the arguments are committed, if an error is returned and subsequently ignored by the caller, the filter could have a new type but mismatched union arguments from the old type.
Yes, and the caller will destroy the filter, so no problem.
Quoted 41 lines
Quoted 12 lines
static int
damos_commit_core_filters(struct damos *dst, struct damos *src){mm/damon/core.c +1
-4struct damos_filter *dst_filter, *next, *src_filter, *new_filter; -int i =0 , j =0 ; +int i =0 , j =0 , err;damos_for_each_core_filter_safe (dst_filter, next, dst) { src_filter =damos_nth_core_filter (i++, src); -if (src_filter) -damos_commit_filter (dst_filter, src_filter); -else mm/damon/core.c static void damos_commit_filter_arg( · line 1371
Quoted 10 lines
mm/damon/core.c +6
-0+if (src_filter) { + err =damos_commit_filter (dst_filter, src_filter); +if (err) +return err; + }else {damos_destroy_filter (dst_filter); + } }damos_for_each_core_filter_safe (src_filter, next, src) {[Severity: High]
Does this code ignore the return value ofdamos_commit_filter_arg()when creating new core filters?Further down in this loop:
mm/damon/core.c:damos_commit_core_filters() { ... new_filter = damos_new_filter( src_filter->type, src_filter->matching, src_filter->allow); if (!new_filter) return -ENOMEM; damos_commit_filter_arg(new_filter, src_filter); damos_add_filter(dst, new_filter); ... }
Quoted 2 lines
If
damos_commit_filter_arg()fails, does this silently add an uninitialized filter with garbage union arguments to the destination scheme?
Good finding, I will fix this in the next spin.
Quoted 22 lines
[Severity: High]
Are there also missing error checks indamos_commit_ops_filters()?It appears
damos_commit_ops_filters()wasn't updated in this patch to check the new integer return values fromdamos_commit_filter()ordamos_commit_filter_arg():mm/damon/core.c:damos_commit_ops_filters() { ... damos_for_each_ops_filter_safe(dst_filter, next, dst) { src_filter = damos_nth_ops_filter(i++, src); if (src_filter) damos_commit_filter(dst_filter, src_filter); ... damos_for_each_ops_filter_safe(src_filter, next, src) { ... damos_commit_filter_arg(new_filter, src_filter); damos_add_filter(dst, new_filter); ... }
Quoted 2 lines
Can ignoring these errors leave the ops filter in a type-confused state or silently add uninitialized filters to the destination scheme?
Good finding, I will fix this in the next spin.
Quoted 3 lines
Signature · 2 lines
-- Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Thanks,
SJ
SJ Park
maintainerRFC PATCH 4/8 mm/damon/core: disallow max < min damos filter range arguments commit
In reply to SJ Parkdamos_commit_filter_arg() receives range arguments for a few types of DAMOS filters. It allows any range including max < min range. It is fine for the logic, but makes no sense to support it. Actually DAMON sysfs interface is doing the validation on its own. To avoid duplicated validations in multiple DAMON API callers, it would be better to do the validation in the core layer. Add a validation of the given range.
--- mm/damon/core.c | 6 ++++++ 1 file changed, 6 insertions(+)
mm/damon/core.c +6 -0
@@ -1325,15 +1325,21 @@ static int damos_commit_filter_arg( dst->memcg_id = src->memcg_id;break ;case DAMOS_FILTER_TYPE_ADDR : +if (src->addr_range.end < src->addr_range.start) +return -EINVAL ; dst->addr_range = src->addr_range;break ;case DAMOS_FILTER_TYPE_TARGET : dst->target_idx = src->target_idx;break ;case DAMOS_FILTER_TYPE_HUGEPAGE_SIZE : +if (src->sz_range.max < src->sz_range.min) +return -EINVAL ; dst->sz_range = src->sz_range;break ;case DAMOS_FILTER_TYPE_PROBE_HITS_WSUM : +if (src->range_max < src->range_min) +return -EINVAL ; dst->range_min = src->range_min; dst->range_max = src->range_max;break ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 4/8] mm/damon/core: disallow max < min damos filter range arguments commit
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The newly introduced validation in `damos_commit_filter_arg()` returns `-EINVAL` on failure, but…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The newly introduced validation in damos_commit_filter_arg() returns -EINVAL on failure, but callers damos_commit_core_filters and damos_commit_ops_filters ignore this return value for new filters, adding uninitialized memory to active DAMON context.
- [High] damos_commit_filter() mutates the target filter's state (type, matching, allow) before validating its arguments, leaving the live filter in a corrupted state if validation subsequently fails.
--
commit b7aac655c61845e48301bed4669cfcf294cf84cb
Author: SJ Park <[email protected]>
mm/damon/core: disallow max < min damos filter range arguments commit
This commit adds validation to damos_commit_filter_arg() to reject invalid ranges where max is less than min. It returns -EINVAL when an invalid range is provided, avoiding duplicated validation across different DAMON API callers.
mm/damon/core.c static int damos_commit_filter_arg( · line 1329
Quoted 10 lines
mm/damon/core.c +2
-0@@ -1325,15 +1325,21 @@ static int damos_commit_filter_arg( dst->memcg_id = src->memcg_id;break ;case DAMOS_FILTER_TYPE_ADDR : +if (src->addr_range.end < src->addr_range.start) +return -EINVAL ;
[Severity: High]
Does this code leave the target filter in a corrupted state on error?
Looking at damos_commit_filter(), the state is mutated before the newly introduced validation has a chance to execute:
mm/damon/core.c:damos_commit_filter() {
dst->type = src->type;
dst->matching = src->matching;
dst->allow = src->allow;
return damos_commit_filter_arg(dst, src);
}If damos_commit_filter_arg() returns -EINVAL here, dst->type, dst->matching, and dst->allow have already been modified. If an existing ops filter receives an invalid range update, its type will be updated but its union arguments will belong to the previous type. When the filter executes, it might misinterpret the old union memory according to the new type, causing unintended matching behavior.
[Severity: High]
Can this validation error cause uninitialized memory to be added to the active DAMON context?
Looking at damos_commit_core_filters() and damos_commit_ops_filters(), the return value of damos_commit_filter_arg() is ignored when creating new filters:
mm/damon/core.c:damos_commit_core_filters() {
...
new_filter = damos_new_filter( src_filter->type, src_filter->matching, src_filter->allow); if (!new_filter) return -ENOMEM; damos_commit_filter_arg(new_filter, src_filter); damos_add_filter(dst, new_filter); }
Because damos_new_filter() allocates memory without zero-initializing the union arguments, returning -EINVAL from damos_commit_filter_arg() means the union payload (like addr_range or sz_range) is left uninitialized. The callers ignore this error and add the filter to the system anyway, which could result in using kmalloc garbage and triggering undefined matching behavior.
Additionally, damos_commit_ops_filters() ignores the return value of damos_commit_filter() for existing ops filters:
mm/damon/core.c:damos_commit_ops_filters() {
...
damos_for_each_ops_filter_safe(dst_filter, next, dst) {
src_filter = damos_nth_ops_filter(i++, src);
if (src_filter)
damos_commit_filter(dst_filter, src_filter);...
If an existing ops filter receives an invalid update, the validation error is swallowed entirely.
Quoted 2 lines
mm/damon/core.c +0
-0dst->addr_range = src->addr_range;break ;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
SJ Park
maintainerRe: [RFC PATCH 4/8] mm/damon/core: disallow max < min damos filter range arguments commit
Quoted 38 lines · On Sat, 12 Sep 2026 19:29:42 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The newly introduced validation indamos_commit_filter_arg()returns-EINVALon failure, but callersdamos_commit_core_filtersanddamos_commit_ops_filtersignore this return value for new filters, adding uninitialized memory to active DAMON context.
- [High]damos_commit_filter()mutates the target filter's state (type, matching, allow) before validating its arguments, leaving the live filter in a corrupted state if validation subsequently fails.
--commit
b7aac655c61845e48301bed4669cfcf294cf84cb
Author: SJ Park <[email protected]>
mm/damon/core: disallow max < min damos filter range arguments commitThis commit adds validation to
damos_commit_filter_arg()to reject invalid ranges where max is less than min. It returns-EINVALwhen an invalid range is provided, avoiding duplicated validation across different DAMON API callers.mm/damon/core.c static int damos_commit_filter_arg( · line 1329
Quoted 10 lines
mm/damon/core.c +2
-0@@ -1325,15 +1325,21 @@ static int damos_commit_filter_arg( dst->memcg_id = src->memcg_id;break ;case DAMOS_FILTER_TYPE_ADDR : +if (src->addr_range.end < src->addr_range.start) +return -EINVAL ;[Severity: High]
Does this code leave the target filter in a corrupted state on error?Looking at
damos_commit_filter(), the state is mutated before the newly introduced validation has a chance to execute:mm/damon/core.c:damos_commit_filter() { dst->type = src->type; dst->matching = src->matching; dst->allow = src->allow; return damos_commit_filter_arg(dst, src); }
Quoted 6 lines
If
damos_commit_filter_arg()returns-EINVALhere,dst->type,dst->matching, anddst->allowhave already been modified. If an existing ops filter receives an invalid range update, its type will be updated but its union arguments will belong to the previous type. When the filter executes, it might misinterpret the old union memory according to the new type, causing unintended matching behavior.
Yes, and the caller will destroy it, so no issue.
Quoted 31 lines
[Severity: High]
Can this validation error cause uninitialized memory to be added to the active DAMON context?Looking at
damos_commit_core_filters()anddamos_commit_ops_filters(), the return value ofdamos_commit_filter_arg()is ignored when creating new filters:mm/damon/core.c:damos_commit_core_filters() {
...new_filter = damos_new_filter( src_filter->type, src_filter->matching, src_filter->allow); if (!new_filter) return -ENOMEM; damos_commit_filter_arg(new_filter, src_filter); damos_add_filter(dst, new_filter); }Because
damos_new_filter()allocates memory without zero-initializing the union arguments, returning-EINVALfromdamos_commit_filter_arg()means the union payload (likeaddr_rangeorsz_range) is left uninitialized. The callers ignore this error and add the filter to the system anyway, which could result in using kmalloc garbage and triggering undefined matching behavior.Additionally,
damos_commit_ops_filters()ignores the return value ofdamos_commit_filter()for existing ops filters:mm/damon/core.c:damos_commit_ops_filters() {
...
Quoted 8 lines
damos_for_each_ops_filter_safe(dst_filter, next, dst) { src_filter = damos_nth_ops_filter(i++, src); if (src_filter) damos_commit_filter(dst_filter, src_filter);...
If an existing ops filter receives an invalid update, the validation error is swallowed entirely.
Good findings, I will fix this in the next spin.
Quoted 8 lines
Quoted 3 lines
mm/damon/core.c +0
-0dst->addr_range = src->addr_range;break ;[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Thanks,
SJ
SJ Park
maintainerRFC PATCH 5/8 mm/damon/sysfs-schemes: drop centralized filter range arg validations
In reply to SJ ParkDAMON sysfs interface is validating wrong range arguments for DAMOS filters. Now the core layer is doing the same validation. Drop the duplicated validation in DAMON sysfs interface.
--- mm/damon/sysfs-schemes.c | 13 ------------- 1 file changed, 13 deletions(-)
mm/damon/sysfs-schemes.c +0 -13
@@ -2832,27 +2832,14 @@ static int damon_sysfs_add_scheme_filters(struct damos *scheme, } }else if (filter->type ==DAMOS_FILTER_TYPE_ADDR ) { filter->addr_range = sysfs_filter->addr_range; -if (filter->addr_range.end < - filter->addr_range.start) { -damos_destroy_filter (filter); -return -EINVAL ; - } }else if (filter->type ==DAMOS_FILTER_TYPE_TARGET ) { filter->target_idx = sysfs_filter->target_idx; }else if (filter->type ==DAMOS_FILTER_TYPE_HUGEPAGE_SIZE ) { filter->sz_range.min = sysfs_filter->range_min; filter->sz_range.max = sysfs_filter->range_max; -if (filter->range_min > filter->range_max) { -damos_destroy_filter (filter); -return -EINVAL ; - } }else if (filter->type ==DAMOS_FILTER_TYPE_PROBE_HITS_WSUM ) { filter->range_min = sysfs_filter->range_min; filter->range_max = sysfs_filter->range_max; -if (filter->range_min > filter->range_max) { -damos_destroy_filter (filter); -return -EINVAL ; - } }damos_add_filter (scheme, filter);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 5/8] mm/damon/sysfs-schemes: drop centralized filter range arg validations
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Removing the filter range argument validation in `damon_sysfs_add_scheme_filters()` relies on the…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Removing the filter range argument validation in damon_sysfs_add_scheme_filters() relies on the core layer to perform this validation. However, the core layer's commit functions (damos_commit_core_filters() and damos_commit_ops_filters()) silently ignore the return value of damos_commit_filter_arg() when adding new filters. When an invalid range is provided via sysfs, damos_commit_filter_arg() returns -EINVAL and aborts before copying the arguments. Because damos_new_filter() uses kmalloc_obj() (which does not zero memory), the new filter's union arguments (e.g., addr_range) are left completely uninitialized. The filter is then added to the active DAMON context, resulting in the scheme acting on an unpredictable memory range derived from uninitialized heap garbage. --
commit a45f05608fa3c7f9050ea4eac0fd8f19b22ee509
Author: SJ Park <[email protected]>
mm/damon/sysfs-schemes: drop centralized filter range arg validations
This patch drops the centralized filter range argument validations in the DAMON sysfs interface, relying on the core layer to perform this validation.
Quoted 4 lines
mm/damon/sysfs-schemes.c +0
-0
[ ... ]
Quoted 8 lines
mm/damon/sysfs-schemes.c +0
-4@@ -2832,27 +2832,14 @@ static int damon_sysfs_add_scheme_filters(struct damos *scheme, } }else if (filter->type ==DAMOS_FILTER_TYPE_ADDR ) { filter->addr_range = sysfs_filter->addr_range; -if (filter->addr_range.end < - filter->addr_range.start) { -damos_destroy_filter (filter); -return -EINVAL ;
mm/damon/sysfs-schemes.c static int damon_sysfs_add_scheme_filters(struct damos *scheme, · line 2848
Quoted 10 lines
mm/damon/sysfs-schemes.c +0
-5- } }else if (filter->type ==DAMOS_FILTER_TYPE_TARGET ) { filter->target_idx = sysfs_filter->target_idx; }else if (filter->type ==DAMOS_FILTER_TYPE_HUGEPAGE_SIZE ) { filter->sz_range.min = sysfs_filter->range_min; filter->sz_range.max = sysfs_filter->range_max; -if (filter->range_min > filter->range_max) { -damos_destroy_filter (filter); -return -EINVAL ; - }
[Severity: High]
Does removing this validation allow invalid ranges to bypass sysfs and reach the core layer logic where errors are silently ignored?
Looking at damos_commit_core_filters() in mm/damon/core.c, the return value of damos_commit_filter_arg() is not checked when adding new filters:
mm/damon/core.c:damos_commit_core_filters() {
...
damos_commit_filter_arg(new_filter, src_filter);
damos_add_filter(dst, new_filter);
...
}A similar pattern exists for ops filters in damos_commit_ops_filters():
mm/damon/core.c:damos_commit_ops_filters() {
...
if (src_filter)
damos_commit_filter(dst_filter, src_filter);
...
damos_commit_filter_arg(new_filter, src_filter);
damos_add_filter(dst, new_filter);
...
}When an invalid range is provided via sysfs, damos_commit_filter_arg() returns -EINVAL and aborts before copying the arguments. Because the new filter is allocated without zeroing its memory, the filter's union arguments (like addr_range) are left completely uninitialized.
Since the error is ignored and damos_add_filter() is still called, won't this result in the scheme acting on an unpredictable memory range derived from uninitialized heap garbage?
Quoted 10 lines
mm/damon/sysfs-schemes.c +0
-4}else if (filter->type ==DAMOS_FILTER_TYPE_PROBE_HITS_WSUM ) { filter->range_min = sysfs_filter->range_min; filter->range_max = sysfs_filter->range_max; -if (filter->range_min > filter->range_max) { -damos_destroy_filter (filter); -return -EINVAL ; - } }damos_add_filter (scheme, filter);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
SJ Park
maintainerRe: [RFC PATCH 5/8] mm/damon/sysfs-schemes: drop centralized filter range arg validations
Quoted 64 lines · On Sat, 12 Sep 2026 19:30:02 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Removing the filter range argument validation indamon_sysfs_add_scheme_filters()relies on the core layer to perform this validation. However, the core layer's commit functions (damos_commit_core_filters()anddamos_commit_ops_filters()) silently ignore the return value ofdamos_commit_filter_arg()when adding new filters. When an invalid range is provided via sysfs,damos_commit_filter_arg()returns-EINVALand aborts before copying the arguments. Becausedamos_new_filter()useskmalloc_obj()(which does not zero memory), the new filter's union arguments (e.g.,addr_range) are left completely uninitialized. The filter is then added to the active DAMON context, resulting in the scheme acting on an unpredictable memory range derived from uninitialized heap garbage. --commit
a45f05608fa3c7f9050ea4eac0fd8f19b22ee509
Author: SJ Park <[email protected]>
mm/damon/sysfs-schemes: drop centralized filter range arg validationsThis patch drops the centralized filter range argument validations in the DAMON sysfs interface, relying on the core layer to perform this validation.
Quoted 4 lines
mm/damon/sysfs-schemes.c +0
-0[ ... ]
Quoted 8 lines
mm/damon/sysfs-schemes.c +0
-4@@ -2832,27 +2832,14 @@ static int damon_sysfs_add_scheme_filters(struct damos *scheme, } }else if (filter->type ==DAMOS_FILTER_TYPE_ADDR ) { filter->addr_range = sysfs_filter->addr_range; -if (filter->addr_range.end < - filter->addr_range.start) { -damos_destroy_filter (filter); -return -EINVAL ;mm/damon/sysfs-schemes.c static int damon_sysfs_add_scheme_filters(struct damos *scheme, · line 2848
Quoted 10 lines
mm/damon/sysfs-schemes.c +0
-5- } }else if (filter->type ==DAMOS_FILTER_TYPE_TARGET ) { filter->target_idx = sysfs_filter->target_idx; }else if (filter->type ==DAMOS_FILTER_TYPE_HUGEPAGE_SIZE ) { filter->sz_range.min = sysfs_filter->range_min; filter->sz_range.max = sysfs_filter->range_max; -if (filter->range_min > filter->range_max) { -damos_destroy_filter (filter); -return -EINVAL ; - }[Severity: High]
Does removing this validation allow invalid ranges to bypass sysfs and reach the core layer logic where errors are silently ignored?Looking at
damos_commit_core_filters()inmm/damon/core.c, the return value ofdamos_commit_filter_arg()is not checked when adding new filters:mm/damon/core.c:damos_commit_core_filters() { ... damos_commit_filter_arg(new_filter, src_filter); damos_add_filter(dst, new_filter); ... }A similar pattern exists for ops filters in
damos_commit_ops_filters():mm/damon/core.c:damos_commit_ops_filters() { ... if (src_filter) damos_commit_filter(dst_filter, src_filter); ... damos_commit_filter_arg(new_filter, src_filter); damos_add_filter(dst, new_filter); ... }
Quoted 8 lines
When an invalid range is provided via sysfs,
damos_commit_filter_arg()returns-EINVALand aborts before copying the arguments. Because the new filter is allocated without zeroing its memory, the filter's union arguments (likeaddr_range) are left completely uninitialized.Since the error is ignored and
damos_add_filter()is still called, won't this result in the scheme acting on an unpredictable memory range derived from uninitialized heap garbage?
Yes, I will fix those in the next spin.
Quoted 10 lines
Quoted 10 lines
mm/damon/sysfs-schemes.c +0
-4}else if (filter->type ==DAMOS_FILTER_TYPE_PROBE_HITS_WSUM ) { filter->range_min = sysfs_filter->range_min; filter->range_max = sysfs_filter->range_max; -if (filter->range_min > filter->range_max) { -damos_destroy_filter (filter); -return -EINVAL ; - } }
Quoted 4 lines
Quoted 1 line
mm/damon/sysfs-schemes.c +0
-0damos_add_filter (scheme, filter);Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Thanks,
SJ
SJ Park
maintainerRFC PATCH 6/8 mm/damon/sysfs-schemes: use switch-case in add_scheme_filters()
In reply to SJ Parkdamon_sysfs_add_scheeme_filters() has long if-else chains for DAMOS filter types. Convert the code to use switch-case, which would be cleaner and more efficient.
--- mm/damon/sysfs-schemes.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
mm/damon/sysfs-schemes.c +13 -5
@@ -2822,7 +2822,8 @@ static int damon_sysfs_add_scheme_filters(struct damos *scheme,if (!filter)return -ENOMEM ; -if (filter->type ==DAMOS_FILTER_TYPE_MEMCG ) { +switch (filter->type) { +case DAMOS_FILTER_TYPE_MEMCG : err =damon_sysfs_memcg_path_to_id ( sysfs_filter->memcg_path, &filter->memcg_id);
@@ -2830,16 +2831,23 @@ static int damon_sysfs_add_scheme_filters(struct damos *scheme,damos_destroy_filter (filter);return err; } - }else if (filter->type ==DAMOS_FILTER_TYPE_ADDR ) { +break ; +case DAMOS_FILTER_TYPE_ADDR : filter->addr_range = sysfs_filter->addr_range; - }else if (filter->type ==DAMOS_FILTER_TYPE_TARGET ) { +break ; +case DAMOS_FILTER_TYPE_TARGET : filter->target_idx = sysfs_filter->target_idx; - }else if (filter->type ==DAMOS_FILTER_TYPE_HUGEPAGE_SIZE ) { +break ; +case DAMOS_FILTER_TYPE_HUGEPAGE_SIZE : filter->sz_range.min = sysfs_filter->range_min; filter->sz_range.max = sysfs_filter->range_max; - }else if (filter->type ==DAMOS_FILTER_TYPE_PROBE_HITS_WSUM ) { +break ; +case DAMOS_FILTER_TYPE_PROBE_HITS_WSUM : filter->range_min = sysfs_filter->range_min; filter->range_max = sysfs_filter->range_max; +break ; +default : +break ; }damos_add_filter (scheme, filter);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 6/8] mm/damon/sysfs-schemes: use switch-case in add_scheme_filters()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines
damon_sysfs_add_scheeme_filters()has long if-else chains for DAMOS filter types. Convert the code to use switch-case, which would be cleaner and more efficient.Signed-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
SJ Park
maintainerRFC PATCH 7/8 mm/damon/core-kunit: extend damos_commit_filter_for() for wrong input
In reply to SJ Parkdamos_commit_filter_for() supposes damos_commit_filter() to always succeed with given inputs. damos_commit_filter() could return an error for invalid inputs. Existing callers always pass only valid inputs, but they may pass invalid inputs in future, for test purposes. Extend the function to be able to be used for wrong inputs-caused error testing.
--- mm/damon/tests/core-kunit.h | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
mm/damon/tests/core-kunit.h +15 -9
@@ -1127,9 +1127,15 @@ static void damos_test_commit_dests(struct kunit *test) }static void damos_test_commit_filter_for (struct kunit *test, -struct damos_filter *dst,struct damos_filter *src) +struct damos_filter *dst,struct damos_filter *src, +bool expect_fail) { -damos_commit_filter (dst, src); +int err; + + err =damos_commit_filter (dst, src); +KUNIT_EXPECT_EQ (test, err !=0 , expect_fail); +if (expect_fail) +return ;KUNIT_EXPECT_EQ (test, dst->type, src->type);KUNIT_EXPECT_EQ (test, dst->matching, src->matching);KUNIT_EXPECT_EQ (test, dst->allow, src->allow);
@@ -1168,47 +1174,47 @@ static void damos_test_commit_filter(struct kunit *test) .type = DAMOS_FILTER_TYPE_ANON, .matching = true, .allow = true, - }); + }, false);damos_test_commit_filter_for (test, &dst, &(struct damos_filter ){ .type =DAMOS_FILTER_TYPE_MEMCG , .matching = false, .allow = false, .memcg_id =123 , - }); + }, false);damos_test_commit_filter_for (test, &dst, &(struct damos_filter ){ .type =DAMOS_FILTER_TYPE_YOUNG , .matching = true, .allow = true, - }); + }, false);damos_test_commit_filter_for (test, &dst, &(struct damos_filter ){ .type =DAMOS_FILTER_TYPE_HUGEPAGE_SIZE , .matching = false, .allow = false, .sz_range = {.min =234 , .max =345 }, - }); + }, false);damos_test_commit_filter_for (test, &dst, &(struct damos_filter ){ .type =DAMOS_FILTER_TYPE_UNMAPPED , .matching = true, .allow = true, - }); + }, false);damos_test_commit_filter_for (test, &dst, &(struct damos_filter ){ .type =DAMOS_FILTER_TYPE_ADDR , .matching = false, .allow = false, .addr_range = {.start =456 , .end =567 }, - }); + }, false);damos_test_commit_filter_for (test, &dst, &(struct damos_filter ){ .type =DAMOS_FILTER_TYPE_TARGET , .matching = true, .allow = true, .target_idx =6 , - }); + }, false); }static void damos_test_help_initailize_scheme (struct damos *scheme)
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 7/8] mm/damon/core-kunit: extend damos_commit_filter_for() for wrong input
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
damos_commit_filter_for()supposesdamos_commit_filter()to always succeed with given inputs.damos_commit_filter()could return an error for invalid inputs. Existing callers always pass only valid inputs, but they may pass invalid inputs in future, for test purposes. Extend the function to be able to be used for wrong inputs-caused error testing.Signed-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
SJ Park
maintainerRFC PATCH 8/8 mm/damon/core-kunit: test invalid damos filter commits
In reply to SJ ParkAdd test cases for testing the validation of damos filter arguments in commit time.
--- mm/damon/tests/core-kunit.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
mm/damon/tests/core-kunit.h +14 -0
@@ -1195,6 +1195,13 @@ static void damos_test_commit_filter(struct kunit *test) .allow = false, .sz_range = {.min = 234, .max = 345}, }, false); +damos_test_commit_filter_for (test, &dst, + &(struct damos_filter ){ + .type =DAMOS_FILTER_TYPE_HUGEPAGE_SIZE , + .matching = false, + .allow = false, + .sz_range = {.min =456 , .max =123 }, + }, true); damos_test_commit_filter_for(test, &dst, &(struct damos_filter){ .type = DAMOS_FILTER_TYPE_UNMAPPED,
@@ -1208,6 +1215,13 @@ static void damos_test_commit_filter(struct kunit *test) .allow = false, .addr_range = {.start = 456, .end = 567}, }, false); +damos_test_commit_filter_for (test, &dst, + &(struct damos_filter ){ + .type =DAMOS_FILTER_TYPE_ADDR , + .matching = false, + .allow = false, + .addr_range = {.start =567 , .end =456 }, + }, true); damos_test_commit_filter_for(test, &dst, &(struct damos_filter){ .type = DAMOS_FILTER_TYPE_TARGET,
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 8/8] mm/damon/core-kunit: test invalid damos filter commits
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Missing error handling for damos_commit_filter_arg() and damos_commit_filter() leads to uninitialized memory use for DAMON filters.
Pre-existing issues:
- [Critical] Memory allocation failure during damos_commit_dests() leaves the active scheme object partially torn down, leading to a deterministic NULL pointer dereference. --
commit 0ffd278b1f3947b2729c57dd026fc9283304d3af
Author: SJ Park <[email protected]>
mm/damon/core-kunit: test invalid damos filter commits
Add test cases for testing the validation of damos filter arguments in commit time.
Quoted 5 lines
mm/damon/tests/core-kunit.h +0
-0
mm/damon/tests/core-kunit.h static void damos_test_commit_filter(struct kunit *test) · line 1193
Quoted 10 lines
mm/damon/tests/core-kunit.h +7
-0@@ -1184,6 +1184,13 @@ static void damos_test_commit_filter(struct kunit *test) .allow = false, .sz_range = {.min = 234, .max = 345}, }, false); +damos_test_commit_filter_for (test, &dst, + &(struct damos_filter ){ + .type =DAMOS_FILTER_TYPE_HUGEPAGE_SIZE , + .matching = false, + .allow = false, + .sz_range = {.min =456 , .max =123 }, + }, true);
[Severity: High]
Does this patch series leave uninitialized memory in the payload of DAMON filters if an invalid range is provided?
While this patch adds tests for invalid filter ranges, it appears the error handling in earlier patches of the series might be incomplete. If a user provides an invalid range (e.g., max < min) via DAMON sysfs, the core validation returns -EINVAL, but this error is ignored when allocating a new core filter:
mm/damon/core.c:damos_commit_core_filters() {
...
damos_for_each_core_filter_safe(src_filter, next, src) {
if (j++ < i)
continue;
new_filter = damos_new_filter(
src_filter->type, src_filter->matching,
src_filter->allow);
if (!new_filter)
return -ENOMEM;
damos_commit_filter_arg(new_filter, src_filter);
damos_add_filter(dst, new_filter);
}
...
}Similarly, the return values of both damos_commit_filter() and damos_commit_filter_arg() are ignored when updating or allocating an ops filter:
mm/damon/core.c:damos_commit_ops_filters() {
...
damos_for_each_ops_filter_safe(dst_filter, next, dst) {
src_filter = damos_nth_ops_filter(i++, src);
if (src_filter)
damos_commit_filter(dst_filter, src_filter);
else
damos_destroy_filter(dst_filter);
}
damos_for_each_ops_filter_safe(src_filter, next, src) {
if (j++ < i)
continue;
new_filter = damos_new_filter(
src_filter->type, src_filter->matching,
src_filter->allow);
if (!new_filter)
return -ENOMEM;
damos_commit_filter_arg(new_filter, src_filter);
damos_add_filter(dst, new_filter);
}
...
}Since damos_new_filter() allocates with kmalloc_obj() (which does not zero memory), ignoring the return value of damos_commit_filter_arg() could lead to the uninitialized heap data remaining in the filter's payload (e.g. sz_range or addr_range) when it is added to the active DAMON context.
[Severity: Critical]
This is a pre-existing issue, but can memory allocation failure during damos_commit_dests() leave the active scheme partially torn down and lead to a NULL pointer dereference?
If memory allocation fails for dst->weight_arr, damos_commit_dests() leaves dst->nr_dests with its old non-zero value, but assigns NULL to the array:
mm/damon/core.c:damos_commit_dests() {
...
dst->weight_arr = kmalloc_array(src->nr_dests,
sizeof(*dst->weight_arr), GFP_KERNEL);
if (!dst->weight_arr) {
/* ->node_id_arr will be freed by scheme destruction */
return -ENOMEM;
}
...
}Because damon_commit_schemes() breaks its loop on -ENOMEM and returns, the partially modified scheme is left physically linked in the live context's scheme list:
mm/damon/core.c:damon_commit_schemes() {
...
src_scheme = damon_nth_scheme(i++, src);
if (src_scheme) {
err = damos_commit(dst_scheme, src_scheme);
if (err)
return err;
}
...
}When the kdamond worker thread later executes damos_va_migrate_dests_add() on this corrupted scheme, it appears it will iterate up to dests->nr_dests and dereference the NULL weight_arr pointer:
mm/damon/vaddr.c:damos_va_migrate_dests_add() {
...
for (i = 0; i < dests->nr_dests; i++)
weight_total += dests->weight_arr[i];
...
}Is there a mechanism preventing this kernel panic if memory allocation fails on the live destination context?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
SJ Park
maintainerRe: [RFC PATCH 8/8] mm/damon/core-kunit: test invalid damos filter commits
Quoted 89 lines · On Sat, 12 Sep 2026 19:36:40 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Missing error handling fordamos_commit_filter_arg()anddamos_commit_filter()leads to uninitialized memory use for DAMON filters.Pre-existing issues:
- [Critical] Memory allocation failure duringdamos_commit_dests()leaves the active scheme object partially torn down, leading to a deterministic NULL pointer dereference. --commit
0ffd278b1f3947b2729c57dd026fc9283304d3af
Author: SJ Park <[email protected]>
mm/damon/core-kunit: test invalid damos filter commitsAdd test cases for testing the validation of damos filter arguments in commit time.
Quoted 5 lines
mm/damon/tests/core-kunit.h +0
-0mm/damon/tests/core-kunit.h static void damos_test_commit_filter(struct kunit *test) · line 1193
Quoted 10 lines
mm/damon/tests/core-kunit.h +7
-0@@ -1184,6 +1184,13 @@ static void damos_test_commit_filter(struct kunit *test) .allow = false, .sz_range = {.min = 234, .max = 345}, }, false); +damos_test_commit_filter_for (test, &dst, + &(struct damos_filter ){ + .type =DAMOS_FILTER_TYPE_HUGEPAGE_SIZE , + .matching = false, + .allow = false, + .sz_range = {.min =456 , .max =123 }, + }, true);[Severity: High]
Does this patch series leave uninitialized memory in the payload of DAMON filters if an invalid range is provided?While this patch adds tests for invalid filter ranges, it appears the error handling in earlier patches of the series might be incomplete. If a user provides an invalid range (e.g., max < min) via DAMON sysfs, the core validation returns
-EINVAL, but this error is ignored when allocating a new core filter:mm/damon/core.c:damos_commit_core_filters() { ... damos_for_each_core_filter_safe(src_filter, next, src) { if (j++ < i) continue; new_filter = damos_new_filter( src_filter->type, src_filter->matching, src_filter->allow); if (!new_filter) return -ENOMEM; damos_commit_filter_arg(new_filter, src_filter); damos_add_filter(dst, new_filter); } ... }Similarly, the return values of both
damos_commit_filter()anddamos_commit_filter_arg()are ignored when updating or allocating an ops filter:mm/damon/core.c:damos_commit_ops_filters() { ... damos_for_each_ops_filter_safe(dst_filter, next, dst) { src_filter = damos_nth_ops_filter(i++, src); if (src_filter) damos_commit_filter(dst_filter, src_filter); else damos_destroy_filter(dst_filter); } damos_for_each_ops_filter_safe(src_filter, next, src) { if (j++ < i) continue; new_filter = damos_new_filter( src_filter->type, src_filter->matching, src_filter->allow); if (!new_filter) return -ENOMEM; damos_commit_filter_arg(new_filter, src_filter); damos_add_filter(dst, new_filter); } ... }
Quoted 4 lines
Since
damos_new_filter()allocates withkmalloc_obj()(which does not zero memory), ignoring the return value ofdamos_commit_filter_arg()could lead to the uninitialized heap data remaining in the filter's payload (e.g.sz_rangeoraddr_range) when it is added to the active DAMON context.
Yes, I will fix in the next spin.
Quoted 47 lines
[Severity: Critical]
This is a pre-existing issue, but can memory allocation failure duringdamos_commit_dests()leave the active scheme partially torn down and lead to a NULL pointer dereference?If memory allocation fails for
dst->weight_arr,damos_commit_dests()leavesdst->nr_destswith its old non-zero value, but assigns NULL to the array:mm/damon/core.c:damos_commit_dests() { ... dst->weight_arr = kmalloc_array(src->nr_dests, sizeof(*dst->weight_arr), GFP_KERNEL); if (!dst->weight_arr) { /* ->node_id_arr will be freed by scheme destruction */ return -ENOMEM; } ... }Because
damon_commit_schemes()breaks its loop on-ENOMEMand returns, the partially modified scheme is left physically linked in the live context's scheme list:mm/damon/core.c:damon_commit_schemes() { ... src_scheme = damon_nth_scheme(i++, src); if (src_scheme) { err = damos_commit(dst_scheme, src_scheme); if (err) return err; } ... }When the kdamond worker thread later executes
damos_va_migrate_dests_add()on this corrupted scheme, it appears it will iterate up todests->nr_destsand dereference the NULLweight_arrpointer:mm/damon/vaddr.c:damos_va_migrate_dests_add() { ... for (i = 0; i < dests->nr_dests; i++) weight_total += dests->weight_arr[i]; ... }
Quoted 2 lines
Is there a mechanism preventing this kernel panic if memory allocation fails on the live destination context?
Yes, we have damon_ctx->maybe_corrupted to prevent this. So, no issue.
Quoted 6 lines
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Thanks,
SJ
Re: [RFC PATCH 8/8] mm/damon/core-kunit: test invalid damos filter commits
In reply to SJ ParkQuoted 27 lines · On Sat, 12 Sep 2026 12:18:06 -0700 SJ Park <[email protected]> wrote:
Add test cases for testing the validation of damos filter arguments in commit time.
Signed-off-by SJ Park <[email protected]>--- mm/damon/tests/core-kunit.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)mm/damon/tests/core-kunit.h +7
-0@@ -1195,6 +1195,13 @@ static void damos_test_commit_filter(struct kunit *test) .allow = false, .sz_range = {.min = 234, .max = 345}, }, false); +damos_test_commit_filter_for (test, &dst, + &(struct damos_filter ){ + .type =DAMOS_FILTER_TYPE_HUGEPAGE_SIZE , + .matching = false, + .allow = false, + .sz_range = {.min =456 , .max =123 }, + }, true); damos_test_commit_filter_for(test, &dst, &(struct damos_filter){ .type = DAMOS_FILTER_TYPE_UNMAPPED,
mm/damon/tests/core-kunit.h static void damos_test_commit_filter(struct kunit *test) · line 1224
Quoted 10 lines
mm/damon/tests/core-kunit.h +7
-0@@ -1208,6 +1215,13 @@ static void damos_test_commit_filter(struct kunit *test) .allow = false, .addr_range = {.start = 456, .end = 567}, }, false); +damos_test_commit_filter_for (test, &dst, + &(struct damos_filter ){ + .type =DAMOS_FILTER_TYPE_ADDR , + .matching = false, + .allow = false, + .addr_range = {.start =567 , .end =456 }, + }, true);
Could we also test an invalid PROBE_HITS_WSUM range here?
Patch 4 adds the same max < min validation for DAMOS_FILTER_TYPE_PROBE_HITS_WSUM, but this test only covers ADDR and HUGEPAGE_SIZE.
Thanks,
Kunwu
Quoted 6 lines
mm/damon/tests/core-kunit.h +0
-0damos_test_commit_filter_for(test, &dst, &(struct damos_filter){ .type = DAMOS_FILTER_TYPE_TARGET,Signature · 1 line
2.47.3
Sent using hkml (https://github.com/sjp38/hackermail)
SJ Park
maintainerRe: [RFC PATCH 8/8] mm/damon/core-kunit: test invalid damos filter commits
Hi Kunwu,
Quoted 4 lines · On Mon, 14 Sep 2026 23:57:27 +0800 Kunwu Chan <[email protected]> wrote:
Quoted 2 lines · On Sat, 12 Sep 2026 12:18:06 -0700 SJ Park <[email protected]> wrote:
Add test cases for testing the validation of damos filter arguments in commit time.
[...]
Quoted 5 lines
Could we also test an invalid
PROBE_HITS_WSUMrange here?Patch 4 adds the same max < min validation for
DAMOS_FILTER_TYPE_PROBE_HITS_WSUM, but this test only covers ADDR andHUGEPAGE_SIZE.
I agree it is better to add a test. But not necessarily together with this series, in my humble opinion. I am separately working on the test.
Thanks,
SJ
[...]
Re: [RFC PATCH 8/8] mm/damon/core-kunit: test invalid damos filter commits
Quoted 10 lines · SJ Park
Hi Kunwu,
Quoted 4 lines · On Mon, 14 Sep 2026 23:57:27 +0800 Kunwu Chan <[email protected]> wrote:
Quoted 2 lines · On Sat, 12 Sep 2026 12:18:06 -0700 SJ Park <[email protected]> wrote:
Add test cases for testing the validation of damos filter arguments in commit time.
[...]
Quoted 8 lines
Quoted 5 lines
Could we also test an invalid
PROBE_HITS_WSUMrange here?Patch 4 adds the same max < min validation for
DAMOS_FILTER_TYPE_PROBE_HITS_WSUM, but this test only covers ADDR andHUGEPAGE_SIZE.I agree it is better to add a test. But not necessarily together with this series, in my humble opinion. I am separately working on the test.
Thanks, SJ. Sounds good to me.
Quoted 6 lines
Thanks,
SJ[...]