[RFC PATCH -next 13/16] mm/damon/sysfs: ensure valid addr_unit setting in damon_sysfs_apply_inputs()

Quanmin Yan posted 16 patches 1 month, 3 weeks ago
There is a newer version of this series
[RFC PATCH -next 13/16] mm/damon/sysfs: ensure valid addr_unit setting in damon_sysfs_apply_inputs()
Posted by Quanmin Yan 1 month, 3 weeks ago
By calling damon_sysfs_turn_damon_on(), the execution of damon_commit_ctx()
can be bypassed. Therefore, it is necessary to prevent ctx->addr_unit from
being set to 0 in damon_sysfs_apply_inputs() and update min_region to avoid
potential issues.

Signed-off-by: Quanmin Yan <yanquanmin1@huawei.com>
---
 mm/damon/sysfs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index bea782b0a711..122824776c1d 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -1422,7 +1422,8 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
 	err = damon_select_ops(ctx, sys_ctx->ops_id);
 	if (err)
 		return err;
-	ctx->addr_unit = sys_ctx->addr_unit;
+	ctx->addr_unit = sys_ctx->addr_unit ? : 1;
+	ctx->min_region = max(DAMON_MIN_REGION / ctx->addr_unit, 1);
 	err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
 	if (err)
 		return err;
-- 
2.34.1
Re: [RFC PATCH -next 13/16] mm/damon/sysfs: ensure valid addr_unit setting in damon_sysfs_apply_inputs()
Posted by SeongJae Park 1 month, 3 weeks ago
On Wed, 13 Aug 2025 13:07:03 +0800 Quanmin Yan <yanquanmin1@huawei.com> wrote:

> By calling damon_sysfs_turn_damon_on(), the execution of damon_commit_ctx()
> can be bypassed. Therefore, it is necessary to prevent ctx->addr_unit from
> being set to 0 in damon_sysfs_apply_inputs() and update min_region to avoid
> potential issues.

Nice catch!

> 
> Signed-off-by: Quanmin Yan <yanquanmin1@huawei.com>
> ---
>  mm/damon/sysfs.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
> index bea782b0a711..122824776c1d 100644
> --- a/mm/damon/sysfs.c
> +++ b/mm/damon/sysfs.c
> @@ -1422,7 +1422,8 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
>  	err = damon_select_ops(ctx, sys_ctx->ops_id);
>  	if (err)
>  		return err;
> -	ctx->addr_unit = sys_ctx->addr_unit;
> +	ctx->addr_unit = sys_ctx->addr_unit ? : 1;

So this is fixing a bug of the seventh patch ("mm/damon/sysfs: implement
addr_unit file under context dir") of this series, right?  It is better to not
add a broken patch, and then fixing it in the same series.  Let's squash the
fix of the problem into the patch.  Don't forget adding your Signed-off-by on
the patch.

Also, since sys_ctx->addr_unit is initialized as 1, the value being zero is
user's wrong input.  Let's return -EINVAL instead of making it silently
success.

> +	ctx->min_region = max(DAMON_MIN_REGION / ctx->addr_unit, 1);

Seems this is a fix of an issue in the 12th patch ("mm/damon: add
damon_ctx->min_region and damon_target->min_region") of this series?  Let's fix
it on the patch.

>  	err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
>  	if (err)
>  		return err;
> -- 
> 2.34.1


Thanks,
SJ
Re: [RFC PATCH -next 13/16] mm/damon/sysfs: ensure valid addr_unit setting in damon_sysfs_apply_inputs()
Posted by Quanmin Yan 1 month, 2 weeks ago
Hi SJ,

在 2025/8/14 1:02, SeongJae Park 写道:
> On Wed, 13 Aug 2025 13:07:03 +0800 Quanmin Yan <yanquanmin1@huawei.com> wrote:
>
>> By calling damon_sysfs_turn_damon_on(), the execution of damon_commit_ctx()
>> can be bypassed. Therefore, it is necessary to prevent ctx->addr_unit from
>> being set to 0 in damon_sysfs_apply_inputs() and update min_region to avoid
>> potential issues.
> Nice catch!
>
>> Signed-off-by: Quanmin Yan <yanquanmin1@huawei.com>
>> ---
>>   mm/damon/sysfs.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
>> index bea782b0a711..122824776c1d 100644
>> --- a/mm/damon/sysfs.c
>> +++ b/mm/damon/sysfs.c
>> @@ -1422,7 +1422,8 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
>>   	err = damon_select_ops(ctx, sys_ctx->ops_id);
>>   	if (err)
>>   		return err;
>> -	ctx->addr_unit = sys_ctx->addr_unit;
>> +	ctx->addr_unit = sys_ctx->addr_unit ? : 1;
> So this is fixing a bug of the seventh patch ("mm/damon/sysfs: implement
> addr_unit file under context dir") of this series, right?  It is better to not
> add a broken patch, and then fixing it in the same series.  Let's squash the
> fix of the problem into the patch.  Don't forget adding your Signed-off-by on
> the patch.
>
> Also, since sys_ctx->addr_unit is initialized as 1, the value being zero is
> user's wrong input.  Let's return -EINVAL instead of making it silently
> success.

Thank you for the kind reminder! The relevant changes have been integrated into
patch #7 of the v2 series[1]. It's worth noting that we've already prevented users
from inputting 0 at the source, therefore "ctx->addr_unit = sys_ctx->addr_unit;"
has been retained in the v2 version.

[1] https://lore.kernel.org/all/20250820080623.3799131-8-yanquanmin1@huawei.com/

>> +	ctx->min_region = max(DAMON_MIN_REGION / ctx->addr_unit, 1);
> Seems this is a fix of an issue in the 12th patch ("mm/damon: add
> damon_ctx->min_region and damon_target->min_region") of this series?  Let's fix
> it on the patch.

This set of changes has been integrated into patch #11 of the v2 series[2].

[2] https://lore.kernel.org/all/20250820080623.3799131-12-yanquanmin1@huawei.com/

Thanks,
Quanmin Yan