mm/damon: validate all parameters in the core
Subsystems (2)
SJ Park
maintainercover mm/damon: validate all parameters in the core
DAMON has a number of parameters. Some of the parameters are validated by DAMON, while some are validated by DAMON API callers. Each caller has their own set of parameters that are exposed to users. Hence each caller has their own ways to do the validation. There is no clear lines for the responsibilities. It is confusing and easy to make mistakes at validations. Actually we found a few bugs in the class.
Define DAMON core as the place to do all the validations and implement those. damon_set_regions(), damon_start() and damon_commit_ctx() are the three main DAMON core API functions for setup of DAMON parameters. Make the three functions to do safe and holistic parameter checks. The first one is already providing the validation, so changes are only for the last two functions.
This might add unnecessary validations for some use cases. The overhead should be negligible as parameters update is expected to only rarely happen. It reduces the number of places to check and fix for bugs of the class from all callers to the single component. The maintenance efficiency gain is obvious.
Further cleanup documents and caller code. Make the lines of validation duties clearer. Remove validations that are no more needed owing to the core validations.
Patches Sequence
================
Patch 1 implements the core holistic parameters validation for damon_commit_ctx(). Patch 2 extends the validation to damon_start(). Patch 3 removes the duplicated holistic parameters validation in DAMON sysfs interface, which is now embedded into the core layer. Patches 4 and 5 remove duplicated min_region_sz validation in DAMON modules. Patches 6 updates kernel doc to clarify damon_set_regions() is doing the range validation. Patches 7 and 8 remove monitoring target range validations that doesn't necessary thanks to the validation in damon_set_regions().
Note that the patch for providing the validation from damon_set_regions() is separately posted as a hotfix [1] but not yet picked into mm.git as of this writing.
[1] https://lore.kernel.org/[email protected]
Changes from RFC v1.1
- RFC v1.1: https://lore.kernel.org/[email protected]
- Drop RFC tag.
- Rebase to latest mm-new.
Changes from RFC v1
- RFC v1: https://lore.kernel.org/[email protected]
- Fix wrong dst testing of damon_commit_ctx().
- Use fresh test_ctx for each ctx in damon_start().
SJ Park (8):
mm/damon/core: safely validate src on damon_commit_ctx() mm/damon/core: do parameter testing commit on damon_start() mm/damon/sysfs: remove duplicated commit input validity check mm/damon/reclaim: remove duplicated min_region_sz power of 2 check mm/damon/lru_sort: remove duplicated min_region_sz power_of_2() check mm/damon: document region size validation in damon_set_regions() mm/damon/core: remove start, end check in
damon_set_region_system_rams()
mm/damon/sysfs: remove region size validation
include/linux/damon.h | 3 +- mm/damon/core.c | 78 ++++++++++++++++++++++++++++++++----------- mm/damon/lru_sort.c | 5 --- mm/damon/reclaim.c | 5 --- mm/damon/sysfs.c | 35 +------------------ 5 files changed, 62 insertions(+), 64 deletions(-)
base-commit: 372482a3e74023a5c23b427ef5c6e7a057b46e6d
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH 1/8 mm/damon/core: safely validate src on damon_commit_ctx()
damon_commit_ctx() does its holistic parameter set validation while applying the new parameter in the set one by one. If it finds a parameter is invalid, because some invalid parameters may already be committed (it is called "commit" but not atomic and irreversable), it stops the running DAMON context.
The callers of the function therefore have to validate the parameters before calling it. Because the function already embeds holistic validation, DAMON_SYSFS reuses it in a safe way. It creates a test-purpose context that is not running but mimics the running one, and calls damon_commit_ctx() against the test purpose context. If it succeeds, the parameters are considered valid, and a real damon_commit_ctx() call against the running context is made with those.
Other callers such as DAMON_RECLAIM and DAMON_LRU_SORT do not expose full parameters to users. For efficiency, they validate only the known set of parameters. The efficiency gain is arguably small and doubtful, though. Meanwhile the maintenance overhead of the multiple different validations is clearly high. We actually found and fixed a few bugs in the class.
Update damon_commit_ctx() to embed DAMON_SYSFS' safe and holistic validation approach. Callers can simply call damon_commit_ctx() without worrying if their parameters are invalid.
Note that damon_commit_ctx() can still cause an unexpected stop of the running context, if internal memory allocation fails. It is arguably unlikely since those internal allocations are too small to fail, but theoretically possible. It should also be better addressed, but not necessarily a blocker of this small and incremental improvement effort.
--- mm/damon/core.c | 61 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 14 deletions(-)
mm/damon/core.c +47 -14
@@ -1664,20 +1664,7 @@ static int damon_commit_probes(struct damon_ctx *dst, struct damon_ctx *src)return 0 ; } -/** -* damon_commit_ctx() - Commit parameters of a DAMON context to another. -* @dst: The commit destination DAMON context. -* @src: The commit source DAMON context. -* -* This function copies user-specified parameters from @src to @dst and update -* the internal status and results accordingly. Users should use this function -* for context-level parameters update of running context, instead of manual -* in-place updates. -* -* This function should be called from parameters-update safe context, like -* damon_call(). -*/ -int damon_commit_ctx (struct damon_ctx *dst,struct damon_ctx *src) +static int __damon_commit_ctx (struct damon_ctx *dst,struct damon_ctx *src) {int err;struct damos *scheme;
@@ -1732,6 +1719,52 @@ int damon_commit_ctx(struct damon_ctx *dst, struct damon_ctx *src)return 0 ; } +static struct damon_ctx *damon_new_test_ctx (struct damon_ctx *dst) +{ +struct damon_ctx *test_ctx; +int err; + + test_ctx =damon_new_ctx (); +if (!test_ctx) +return NULL ; + err =__damon_commit_ctx (test_ctx, dst); +if (err) { +damon_destroy_ctx (test_ctx); +return NULL ; + } +return test_ctx; +} + +/** +* damon_commit_ctx() - Commit parameters of a DAMON context to another. +* @dst: The commit destination DAMON context. +* @src: The commit source DAMON context. +* +* This function copies user-specified parameters from @src to @dst and update +* the internal status and results accordingly. Users should use this function +* for context-level parameters update of running context, instead of manual +* in-place updates. +* +* This function should be called from parameters-update safe context, like +* damon_call(). +*/ +int damon_commit_ctx (struct damon_ctx *dst,struct damon_ctx *src) +{ +struct damon_ctx *test_ctx; +int err; + + test_ctx =damon_new_test_ctx (dst); +if (!test_ctx) +return -ENOMEM ; + err =__damon_commit_ctx (test_ctx, src); +if (err) + goto out; + err =__damon_commit_ctx (dst, src); +out: +damon_destroy_ctx (test_ctx); +return err; +} +/** * damon_nr_running_ctxs() - Return number of currently running contexts. */
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH 2/8 mm/damon/core: do parameter testing commit on damon_start()
In reply to SJ Parkdamon_start() and damon_commit_ctx() are two main DAMON core API functions for setting whole DAMON parameters. While damon_commit_ctx() does holistic parameters testing, damon_start() just believes the caller validated the whole thing. Embed the holistic parameter check that is already in damon_commit_ctx() into damon_start(). After this change, the callers can safely call damon_start() without validating the parameters.
--- mm/damon/core.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
mm/damon/core.c +12 -2
@@ -1865,6 +1865,8 @@ static int __damon_start(struct damon_ctx *ctx)return err; } +static int __damon_commit_ctx (struct damon_ctx *dst,struct damon_ctx *src); +/** * damon_start() - Starts the monitorings for a given group of contexts. * @ctxs: an array of the pointers for contexts to start monitoring
@@ -1886,8 +1888,16 @@ int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive)int err =0 ;for (i =0 ; i < nr_ctxs; i++) { -if (!is_power_of_2 (ctxs[i]->min_region_sz)) -return -EINVAL ; +struct damon_ctx *test_ctx; + + test_ctx =damon_new_ctx (); +if (!test_ctx) +return -ENOMEM ; + + err =__damon_commit_ctx (test_ctx, ctxs[i]); +damon_destroy_ctx (test_ctx); +if (err) +return err; }mutex_lock (&damon_lock);
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH 3/8 mm/damon/sysfs: remove duplicated commit input validity check
In reply to SJ ParkDAMON sysfs interface does parameters validation-purpose damon_commit_ctx() calls for parameters update. Now the same logic is embedded inside damon_commit_ctx() itself. Hence, the validation in DAMON sysfs interface is just an unnecessary duplicate. Remove it.
--- mm/damon/sysfs.c | 32 +------------------------------- 1 file changed, 1 insertion(+), 31 deletions(-)
mm/damon/sysfs.c +1 -31
@@ -2098,26 +2098,6 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,static struct damon_ctx *damon_sysfs_build_ctx (struct damon_sysfs_context *sys_ctx); -/* -* Return a new damon_ctx for testing new parameters to commit. -*/ -static struct damon_ctx *damon_sysfs_new_test_ctx ( -struct damon_ctx *running_ctx) -{ -struct damon_ctx *test_ctx; -int err; - - test_ctx =damon_new_ctx (); -if (!test_ctx) -return NULL ; - err =damon_commit_ctx (test_ctx, running_ctx); -if (err) { -damon_destroy_ctx (test_ctx); -return NULL ; - } -return test_ctx; -} -/* * damon_sysfs_commit_input() - Commit user inputs to a running kdamond. * @kdamond: The kobject wrapper for the associated kdamond.
@@ -2127,7 +2107,7 @@ static struct damon_ctx *damon_sysfs_new_test_ctx(static int damon_sysfs_commit_input (void *data) {struct damon_sysfs_kdamond *kdamond = data; -struct damon_ctx *param_ctx, *test_ctx; +struct damon_ctx *param_ctx;int err;if (!damon_sysfs_kdamond_running (kdamond))
@@ -2139,17 +2119,7 @@ static int damon_sysfs_commit_input(void *data) param_ctx =damon_sysfs_build_ctx (kdamond->contexts->contexts_arr[0 ]);if (IS_ERR (param_ctx))return PTR_ERR (param_ctx); - test_ctx =damon_sysfs_new_test_ctx (kdamond->damon_ctx); -if (!test_ctx) { -damon_destroy_ctx (param_ctx); -return -ENOMEM ; - } - err =damon_commit_ctx (test_ctx, param_ctx); -if (err) - goto out; err =damon_commit_ctx (kdamond->damon_ctx, param_ctx); -out: -damon_destroy_ctx (test_ctx);damon_destroy_ctx (param_ctx);return err; }
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH 4/8 mm/damon/reclaim: remove duplicated min_region_sz power of 2 check
In reply to SJ ParkDAMON_RECLAIM validates the user input for min_region_sz. The same validation is done inside damon_start() and damon_commit_ctx(). Remove the duplicate.
--- mm/damon/reclaim.c | 5 ----- 1 file changed, 5 deletions(-)
mm/damon/reclaim.c +0 -5
@@ -208,11 +208,6 @@ static int damon_reclaim_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz =max (DAMON_MIN_REGION_SZ / addr_unit,1 ); -if (!is_power_of_2 (param_ctx->min_region_sz)) { - err = -EINVAL ; - goto out; - } -if (!damon_reclaim_mon_attrs.aggr_interval) { err = -EINVAL ; goto out;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 4/8] mm/damon/reclaim: remove duplicated min_region_sz power of 2 check
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] State Corruption on Error Path: invalid `addr_unit` permanently corrupts global monitoring region…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] State Corruption on Error Path: invalid addr_unit permanently corrupts global monitoring region parameters.
- [Medium] API Contract Violation: ALIGN macros are invoked with non-power-of-two inputs, computing garbage values. --
commit f2a479c4ed9e6eb22e5e61615eb62f684e1703b1
Author: SJ Park <[email protected]>
mm/damon/reclaim: remove duplicated min_region_sz power of 2 check
This commit removes the power-of-two validation for min_region_sz in damon_reclaim_apply_parameters(). The change relies on the assumption that identical validation in damon_start() and damon_commit_ctx() is sufficient to catch invalid parameters.
mm/damon/reclaim.c static int damon_reclaim_apply_parameters(void) · line 215
Quoted 13 lines
mm/damon/reclaim.c +0
-5@@ -208,11 +208,6 @@ static int damon_reclaim_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz =max (DAMON_MIN_REGION_SZ / addr_unit,1 ); -if (!is_power_of_2 (param_ctx->min_region_sz)) { - err = -EINVAL ; - goto out; - } -
[Severity: High]
By removing this early check, does an invalid addr_unit now proceed to call damon_set_region_system_rams_default() before damon_commit_ctx() rejects it?
If so, could this permanently corrupt the global monitor_region_start and monitor_region_end parameters on the error path?
For example, damon_find_system_rams_range() scales and overwrites these global pointers before the context validation has a chance to fail:
mm/damon/core.c:damon_find_system_rams_range() {
...
*start = damon_res_to_core_addr(arg.res.start, addr_unit);
*end = damon_res_to_core_addr(arg.res.end + 1, addr_unit);
...
}[Severity: Medium]
Additionally, if we allow a non-power-of-two min_region_sz to be passed downwards, does this violate the requirements of the ALIGN macros?
When damon_set_regions() processes the temporary target structure:
mm/damon/core.c:damon_set_regions() {
...
start = ALIGN_DOWN(ranges[i].start, min_region_sz);
end = ALIGN(ranges[i].end, min_region_sz);
...
}Since ALIGN relies on bitwise operations, won't passing a non-power-of-two min_region_sz result in garbage address calculations?
Quoted 3 lines
mm/damon/reclaim.c +0
-0if (!damon_reclaim_mon_attrs.aggr_interval) { err = -EINVAL ; goto out;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
SJ Park
maintainerRe: [PATCH 4/8] mm/damon/reclaim: remove duplicated min_region_sz power of 2 check
Quoted 3 lines · On Sun, 05 Jul 2026 16:12:09 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] State Corruption on Error Path: invalidaddr_unitpermanently corrupts global monitoring region parameters.
- [Medium] API Contract Violation:ALIGNmacros are invoked with non-power-of-two inputs, computing garbage values.
No blockers. Please read below for more details.
Quoted 36 lines
--
commit
f2a479c4ed9e6eb22e5e61615eb62f684e1703b1
Author: SJ Park <[email protected]>
mm/damon/reclaim: remove duplicatedmin_region_szpower of 2 checkThis commit removes the power-of-two validation for
min_region_szindamon_reclaim_apply_parameters(). The change relies on the assumption that identical validation indamon_start()anddamon_commit_ctx()is sufficient to catch invalid parameters.mm/damon/reclaim.c static int damon_reclaim_apply_parameters(void) · line 215
Quoted 13 lines
mm/damon/reclaim.c +0
-5@@ -208,11 +208,6 @@ static int damon_reclaim_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz =max (DAMON_MIN_REGION_SZ / addr_unit,1 ); -if (!is_power_of_2 (param_ctx->min_region_sz)) { - err = -EINVAL ; - goto out; - } -[Severity: High]
By removing this early check, does an invalidaddr_unitnow proceed to calldamon_set_region_system_rams_default()beforedamon_commit_ctx()rejects it?If so, could this permanently corrupt the global
monitor_region_startandmonitor_region_endparameters on the error path?For example,
damon_find_system_rams_range()scales and overwrites these global pointers before the context validation has a chance to fail:
Quoted 6 lines
mm/damon/core.c:damon_find_system_rams_range() {
...
*start = damon_res_to_core_addr(arg.res.start, addr_unit);
*end = damon_res_to_core_addr(arg.res.end + 1, addr_unit);
...
}Users can show and correct it. And this happens only when the user entered an invalid parameter. No bad consequence like kernel panic happens. So, this is just a minor user experience issue. And I want to make validation mminimum for simplicity. This minor issue doesn't deserve to add the complexity in my opinion. I will keep this as is unless others chime in.
Quoted 14 lines
[Severity: Medium]
Additionally, if we allow a non-power-of-twomin_region_szto be passed downwards, does this violate the requirements of the ALIGN macros?When
damon_set_regions()processes the temporary target structure:mm/damon/core.c:damon_set_regions() { ... start = ALIGN_DOWN(ranges[i].start, min_region_sz); end = ALIGN(ranges[i].end, min_region_sz); ... }
Quoted 2 lines
Since ALIGN relies on bitwise operations, won't passing a non-power-of-two
min_region_szresult in garbage address calculations?
Yes, but anyway it cannot be committed due to the validation in the commit. No bad consequene like kernel ppanic happens. So no issue.
Quoted 7 lines
Quoted 4 lines
mm/damon/reclaim.c +0
-0if (!damon_reclaim_mon_attrs.aggr_interval) { err = -EINVAL ; goto out;Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Thanks,
SJ
[...]
SJ Park
maintainerPATCH 5/8 mm/damon/lru_sort: remove duplicated min_region_sz power_of_2() check
In reply to SJ ParkDAMON_LRU_SORT validates the user input for min_region_sz. The same validation is done inside damon_start() and damon_commit_ctx(). Remove the unnecessary duplicate.
--- mm/damon/lru_sort.c | 5 ----- 1 file changed, 5 deletions(-)
mm/damon/lru_sort.c +0 -5
@@ -284,11 +284,6 @@ static int damon_lru_sort_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz =max (DAMON_MIN_REGION_SZ / addr_unit,1 ); -if (!is_power_of_2 (param_ctx->min_region_sz)) { - err = -EINVAL ; - goto out; - } -if (!damon_lru_sort_mon_attrs.sample_interval) { err = -EINVAL ; goto out;
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH 6/8 mm/damon: document region size validation in damon_set_regions()
In reply to SJ ParkThe kernel doc comment of damon_region clearly specifies every region should have positive size. But it is unclear who should verify it. damon_set_regions() is the recommended DAMON core function for setting regions from the callers, and has the verification. Update the comment to clarify the callers should be ok to pass any values for region addresses, as long as they use damon_set_regions().
--- include/linux/damon.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/damon.h +2 -1
@@ -49,7 +49,8 @@ struct damon_size_range {* @list: List head for siblings. * @age: Age of this region. * -* For any use case, @ar should be non-zero positive size. +* For any use case, @ar should be non-zero positive size. damon_set_regions() +* does the validation. * * @nr_accesses is reset to zero for every &damon_attrs->aggr_interval and be * increased for every &damon_attrs->sample_interval if an access to the region
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH 7/8 mm/damon/core: remove start, end check in damon_set_region_system_rams()
In reply to SJ Parkdamon_set_region_system_rams() validates user inputs to avoid creating a negative size region. But DAMON core avoids zero size, too. The check is incomplete. The complete check is done inside damon_set_regions(), which is eventually called from damon_set_region_system_rams_default(). Drop the incomplete and unnecessary check.
--- mm/damon/core.c | 3 --- 1 file changed, 3 deletions(-)
mm/damon/core.c +0 -3
@@ -3846,9 +3846,6 @@ int damon_set_region_system_rams_default(struct damon_target *t, {struct damon_addr_range addr_range; -if (*start > *end) -return -EINVAL ; -if (!*start && !*end && !damon_find_system_rams_range (start, end, addr_unit))return -EINVAL ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 7/8] mm/damon/core: remove start, end check in damon_set_region_system_rams()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Removing the explicit `*start > *end` check causes invalid, inverted memory ranges to be…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Removing the explicit *start > *end check causes invalid, inverted memory ranges to be inconsistently accepted due to post-alignment bounds checking in damon_set_regions(). --
commit f561b43e79bb90540b8053e4d95593cfb96f6d1d
Author: SJ Park <[email protected]>
mm/damon/core: remove start, end check in damon_set_region_system_rams()
This commit removes the explicit start and end validation in damon_set_region_system_rams_default(), relying instead on the bounds check inside damon_set_regions() to catch invalid ranges.
mm/damon/core.c int damon_set_region_system_rams_default(struct damon_target *t, · line 3851
Quoted 10 lines
mm/damon/core.c +0
-3@@ -3846,9 +3846,6 @@ int damon_set_region_system_rams_default(struct damon_target *t, {struct damon_addr_range addr_range; -if (*start > *end) -return -EINVAL ; -
[Severity: Medium]
Does removing this check allow inverted ranges to bypass validation?
Looking at damon_set_regions() in mm/damon/core.c, the fallback check evaluates the values post-alignment:
mm/damon/core.c:damon_set_regions() {
for (i = 0; i < nr_ranges; i++) {
unsigned long start, end;
start = ALIGN_DOWN(ranges[i].start, min_region_sz);
end = ALIGN(ranges[i].end, min_region_sz);
if (start >= end)
return -EINVAL;
...
}If an administrator configures an unaligned, inverted memory range (for example, start=8191, end=4097), the post-alignment check might still evaluate to ALIGN_DOWN(start) < ALIGN(end) and accept the invalid inputs.
Quoted 3 lines
mm/damon/core.c +0
-0if (!*start && !*end && !damon_find_system_rams_range (start, end, addr_unit))return -EINVAL ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
SJ Park
maintainerRe: [PATCH 7/8] mm/damon/core: remove start, end check in damon_set_region_system_rams()
Quoted 2 lines · On Sun, 05 Jul 2026 16:06:50 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Removing the explicit*start > *endcheck causes invalid, inverted memory ranges to be inconsistently accepted due to post-alignment bounds checking indamon_set_regions().
No blocker. Read below for details, please.
Quoted 39 lines
--
commit
f561b43e79bb90540b8053e4d95593cfb96f6d1d
Author: SJ Park <[email protected]>
mm/damon/core: remove start, end check indamon_set_region_system_rams()This commit removes the explicit start and end validation in
damon_set_region_system_rams_default(), relying instead on the bounds check insidedamon_set_regions()to catch invalid ranges.mm/damon/core.c int damon_set_region_system_rams_default(struct damon_target *t, · line 3851
Quoted 10 lines
mm/damon/core.c +0
-3@@ -3846,9 +3846,6 @@ int damon_set_region_system_rams_default(struct damon_target *t, {struct damon_addr_range addr_range; -if (*start > *end) -return -EINVAL ; -[Severity: Medium]
Does removing this check allow inverted ranges to bypass validation?Looking at
damon_set_regions()inmm/damon/core.c, the fallback check evaluates the values post-alignment:mm/damon/core.c:damon_set_regions() { for (i = 0; i < nr_ranges; i++) { unsigned long start, end; start = ALIGN_DOWN(ranges[i].start, min_region_sz); end = ALIGN(ranges[i].end, min_region_sz); if (start >= end) return -EINVAL; ... }
Quoted 3 lines
If an administrator configures an unaligned, inverted memory range (for example, start=8191, end=4097), the post-alignment check might still evaluate to
ALIGN_DOWN(start)< ALIGN(end) and accept the invalid inputs.
Yes, that's how it works, and I find no problem here. No bad consequences like kernel panic happenns. It it might be confusing and better to improve for user experience. However, I don't think this corner case experience deserves the additional complexity. I want to keep the validation simple and minimum.
So I will keep this as is unless others chime in.
Quoted 7 lines
Quoted 4 lines
mm/damon/core.c +0
-0if (!*start && !*end && !damon_find_system_rams_range (start, end, addr_unit))return -EINVAL ;Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Thankss,
SJ
SJ Park
maintainerPATCH 8/8 mm/damon/sysfs: remove region size validation
In reply to SJ ParkDAMON_SYSFS validates user inputs for monitoring target regions to disallow negative size regions. DAMON core assumes only positive size regions, though. The validation is incomplete. Fortunately damon_set_regions(), which is eventually used by DAMON_SYSFS, does the complete validation. Remove the incomplete and unnecessary validation.
--- mm/damon/sysfs.c | 3 --- 1 file changed, 3 deletions(-)
mm/damon/sysfs.c +0 -3
@@ -1987,9 +1987,6 @@ static int damon_sysfs_set_regions(struct damon_target *t,struct damon_sysfs_region *sys_region = sysfs_regions->regions_arr[i]; -if (sys_region->ar.start > sys_region->ar.end) - goto out; - ranges[i].start = sys_region->ar.start; ranges[i].end = sys_region->ar.end;if (i ==0 )
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 8/8] mm/damon/sysfs: remove region size validation
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Removal of region size validation allows accepting negative size regions which are then silently…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Removal of region size validation allows accepting negative size regions which are then silently transformed into valid positive size regions. --
commit 9f8b624e84e757e30369d491c2b454f3896c87c7
Author: SJ Park <[email protected]>
mm/damon/sysfs: remove region size validation
This commit removes the region size validation in DAMON sysfs, relying on the assumption that damon_set_regions() performs complete validation.
mm/damon/sysfs.c static int damon_sysfs_set_regions(struct damon_target *t, · line 1992
Quoted 11 lines
mm/damon/sysfs.c +0
-3@@ -1987,9 +1987,6 @@ static int damon_sysfs_set_regions(struct damon_target *t,struct damon_sysfs_region *sys_region = sysfs_regions->regions_arr[i]; -if (sys_region->ar.start > sys_region->ar.end) - goto out; -
[Severity: Medium]
Does removing this check allow invalid input to be silently accepted?
If a user configures a region via sysfs where start is greater than end, damon_set_regions() might still accept it because the alignment happens before the size validation:
mm/damon/core.c:damon_set_regions() {
...
start = ALIGN_DOWN(ranges[i].start, min_region_sz);
end = ALIGN(ranges[i].end, min_region_sz);
if (start >= end)
return -EINVAL;
...
}If the user provides start = 100 and end = 50, and min_region_sz is 4096, the alignment step evaluates start to 0 and end to 4096.
This causes the subsequent check to see 0 >= 4096 (which is false), silently mutating the invalid negative-sized region into a valid positive size region instead of rejecting it.
Quoted 3 lines
mm/damon/sysfs.c +0
-0ranges[i].start = sys_region->ar.start; ranges[i].end = sys_region->ar.end;if (i ==0 )
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
SJ Park
maintainerRe: [PATCH 8/8] mm/damon/sysfs: remove region size validation
Quoted 2 lines · On Sun, 05 Jul 2026 16:05:59 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Removal of region size validation allows accepting negative size regions which are then silently transformed into valid positive size regions.
No issue. Read my reply to Sashiko comment for patch 7 for more details.
Thanks,
SJ
[...]
SJ Park
maintainerQuoted 20 lines · On Sun, 5 Jul 2026 08:55:51 -0700 SJ Park <[email protected]> wrote:
DAMON has a number of parameters. Some of the parameters are validated by DAMON, while some are validated by DAMON API callers. Each caller has their own set of parameters that are exposed to users. Hence each caller has their own ways to do the validation. There is no clear lines for the responsibilities. It is confusing and easy to make mistakes at validations. Actually we found a few bugs in the class.
Define DAMON core as the place to do all the validations and implement those.
damon_set_regions(),damon_start()anddamon_commit_ctx()are the three main DAMON core API functions for setup of DAMON parameters. Make the three functions to do safe and holistic parameter checks. The first one is already providing the validation, so changes are only for the last two functions.This might add unnecessary validations for some use cases. The overhead should be negligible as parameters update is expected to only rarely happen. It reduces the number of places to check and fix for bugs of the class from all callers to the single component. The maintenance efficiency gain is obvious.
Quoted 3 lines
Further cleanup documents and caller code. Make the lines of validation duties clearer. Remove validations that are no more needed owing to the core validations.
Sashiko found a few things but those are not real issues in my opinion. Please read my replies to Sashiko comments on DAMON mailing list thread [1].
DAMON mailing list is opted in to Sashiko finding mail delivery to the author and the mailing list. I record my opinions to those by replying to the thread. Hence you can show the detailed opinions from DAMON mailing list thread.
[1] https://lore.kernel.org/damon/[email protected]/
Thanks,
SJ
[...]
Andrew Morton
maintainerQuoted 5 lines · On Sun, 5 Jul 2026 08:55:51 -0700 SJ Park <[email protected]> wrote:
Note that the patch for providing the validation from
damon_set_regions()is separately posted as a hotfix [1] but not yet picked into mm.git as of this writing.
Actually this was included in mm-hotfixes-unstable on July 1(?)
SJ Park
maintainerQuoted 9 lines · On Sun, 5 Jul 2026 12:16:18 -0700 Andrew Morton <[email protected]>…
Quoted 5 lines · On Sun, 5 Jul 2026 08:55:51 -0700 SJ Park <[email protected]> wrote:
Note that the patch for providing the validation from
damon_set_regions()is separately posted as a hotfix [1] but not yet picked into mm.git as of this writing.Actually this was included in mm-hotfixes-unstable on July 1(?)
You're correct. I dropped the RFC tag of this patch series because I found it is merged. But I forgot dropping this note. Sorry for causing confusion.
Thanks,
SJ