mm/damon: move zero damos quota target_value handling to the core layer
Subsystems (2)
SJ Park
maintainercover mm/damon: move zero damos quota target_value handling to the core layer
Having zero DAMOS quota target value can cause division by zero. DAMON API callers are checking the target value parameters to avoid that. It is easy to make mistakes in some of the multiple API callers. Move that to the core layer.
Patch 1 adds the corner case handling into the core layer DAMON parameters validation logic. Patches 2 and 3 remove no more needed DAMON API callers side handling of the corner case in DAMON_LRU_SORT and DMON_SAMPLIE_MTIER, respectively.
Changes from RFC
- RFC: https://lore.kernel.org/[email protected]
- Drop RFC tag.
- Rebase to latest mm-new.
SJ Park (3):
mm/damon/core: error damos_commit_quota_goal() for zero target_value Revert "mm/damon/lru_sort: error out for >10000 active_mem_bp" Revert "samples/damon/mtier: error out for zero quota goal target
values"
mm/damon/core.c | 22 ++++++++++++++++------ mm/damon/lru_sort.c | 2 -- samples/damon/mtier.c | 3 --- 3 files changed, 16 insertions(+), 11 deletions(-)
base-commit: e5bdf22abf33923f4571fae61ef1e184afc7ddf6
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH 1/3 mm/damon/core: error damos_commit_quota_goal() for zero target_value
If a DAMOS scheme has a damos_quota_goal of zero target_value, damos_quota_goal() could trigger division-by-zero error. Hence each DAMON API callers should do the zero target_value validation. It is easy to make mistakes. Actually such bugs in DAMON_LRU_SORT and DAMON_SAMPLE_MTIER were found and fixed [1].
It is better to handle the corner case only once in the core layer, instead of multiple places in all DAMON API callers. One straightforward option is using an alternative denominator for the corner case in the damos_quota_goal(). However, the zero target_value is meaningless. In this case, the quota goal is always evaluated as achieved or over-achieved. The quota will only keep being reduced.
Simply avoid using zero target_value by adding a check in the core layer DAMOS quota goal parameters validation/commit path, damos_commit_quota_goal(). Update it to return an error in the case. Also update its caller to propagate the error.
[1] https://lore.kernel.org/[email protected]
--- mm/damon/core.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
mm/damon/core.c +16 -6
@@ -1212,14 +1212,17 @@ static void damos_commit_quota_goal_union( } } -static void damos_commit_quota_goal ( +static int damos_commit_quota_goal (struct damos_quota_goal *dst,struct damos_quota_goal *src) { +if (!src->target_value) +return -EINVAL ; dst->metric = src->metric; dst->target_value = src->target_value;if (dst->metric ==DAMOS_QUOTA_USER_INPUT ) dst->current_value = src->current_value;damos_commit_quota_goal_union (dst, src); +return 0 ; }/**
@@ -1237,14 +1240,17 @@ static void damos_commit_quota_goal(int damos_commit_quota_goals (struct damos_quota *dst,struct damos_quota *src) {struct damos_quota_goal *dst_goal, *next, *src_goal, *new_goal; -int i =0 , j =0 ; +int i =0 , j =0 , err;damos_for_each_quota_goal_safe (dst_goal, next, dst) { src_goal =damos_nth_quota_goal (i++, src); -if (src_goal) -damos_commit_quota_goal (dst_goal, src_goal); -else +if (src_goal) { + err =damos_commit_quota_goal (dst_goal, src_goal); +if (err) +return err; + }else {damos_destroy_quota_goal (dst_goal); + } }damos_for_each_quota_goal_safe (src_goal, next, src) {if (j++ < i)
@@ -1253,7 +1259,11 @@ int damos_commit_quota_goals(struct damos_quota *dst, struct damos_quota *src) src_goal->metric, src_goal->target_value);if (!new_goal)return -ENOMEM ; -damos_commit_quota_goal (new_goal, src_goal); + err =damos_commit_quota_goal (new_goal, src_goal); +if (err) { +damos_free_quota_goal (new_goal); +return err; + }damos_add_quota_goal (dst, new_goal); }return 0 ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 1/3] mm/damon/core: error damos_commit_quota_goal() for zero target_value
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
If a DAMOS scheme has a
damos_quota_goalof zerotarget_value,damos_quota_goal()could trigger division-by-zero error. Hence each DAMON API callers should do the zerotarget_valuevalidation. It is easy to make mistakes. Actually such bugs inDAMON_LRU_SORTandDAMON_SAMPLE_MTIERwere found and fixed [1].It is better to handle the corner case only once in the core layer, instead of multiple places in all DAMON API callers. One straightforward option is using an alternative denominator for the corner case in the
damos_quota_goal(). However, the zerotarget_valueis meaningless. In this case, the quota goal is always evaluated as achieved or over-achieved. The quota will only keep being reduced.
Quoted 8 lines
Simply avoid using zero
target_valueby adding a check in the core layer DAMOS quota goal parameters validation/commit path,damos_commit_quota_goal(). Update it to return an error in the case. Also update its caller to propagate the error.[1] https://lore.kernel.org/[email protected]
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=1
SJ Park
maintainerPATCH 2/3 Revert "mm/damon/lru_sort: error out for >10000 active_mem_bp"
In reply to SJ ParkThis reverts commit 06befa61c427e74319781e6f35a364cfc32dbae8.
The commit was made to avoid zero damos quota goal target value, because it can trigger division-by-zero. Now the core layer handles the corner case. It returns an error for any attempt setting the aero target_value. The corner case handling in DAMON_LRU_SORT is hence no more needed. Remove it.
Note that this slightly changes the user behavior. It still disallows active_mem_bp of 10,002. But now it allows other >10,000 active_mem_bp values. Setting >10,000 active_mem_bp makes not much sense. But it doesn't cause critical problems such as memory leak or crash, either. Arguably that doesn't deserve additional code complexity. Just allow it.
--- mm/damon/lru_sort.c | 2 -- 1 file changed, 2 deletions(-)
mm/damon/lru_sort.c +0 -2
@@ -233,8 +233,6 @@ static int damon_lru_sort_add_quota_goals(struct damos *hot_scheme,if (!active_mem_bp)return 0 ; -if (10000 < active_mem_bp) -return -EINVAL ; goal =damos_new_quota_goal (DAMOS_QUOTA_ACTIVE_MEM_BP , active_mem_bp);if (!goal)return -ENOMEM ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 2/3] Revert "mm/damon/lru_sort: error out for >10000 active_mem_bp"
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
This reverts commit
06befa61c427e74319781e6f35a364cfc32dbae8.The commit was made to avoid zero damos quota goal target value, because it can trigger division-by-zero. Now the core layer handles the corner case. It returns an error for any attempt setting the aero
target_value. The corner case handling inDAMON_LRU_SORTis hence no more needed. Remove it.
Quoted 8 lines
Note that this slightly changes the user behavior. It still disallows
active_mem_bpof 10,002. But now it allows other >10,000active_mem_bpvalues. Setting >10,000active_mem_bpmakes not much sense. But it doesn't cause critical problems such as memory leak or crash, either. Arguably that doesn't deserve additional code complexity. Just allow it.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=2
SJ Park
maintainerPATCH 3/3 Revert "samples/damon/mtier: error out for zero quota goal target values"
In reply to SJ ParkThis reverts commit a16fd3ad9d89b05475864da97327870464611736.
The commit was made to avoid zero damos quota goal target value, because it can trigger division-by-zero. Now the core layer handles the corner case. It returns an error for any attempt setting the aero target_value. The corner case handling in DAMON_SAMPLE_MTIER is hence no more needed. Remove it.
--- samples/damon/mtier.c | 3 --- 1 file changed, 3 deletions(-)
samples/damon/mtier.c +0 -3
@@ -161,9 +161,6 @@ static struct damon_ctx *damon_sample_mtier_build_ctx(bool promote)if (!scheme) goto free_out;damon_set_schemes (ctx, &scheme,1 ); -/* zero target value causes division by zero in damos_quota_store() */ -if (!node0_mem_used_bp || !node0_mem_free_bp) - goto free_out; quota_goal = damos_new_quota_goal( promote ? DAMOS_QUOTA_NODE_MEM_USED_BP : DAMOS_QUOTA_NODE_MEM_FREE_BP,
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 3/3] Revert "samples/damon/mtier: error out for zero quota goal target values"
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
This reverts commit
a16fd3ad9d89b05475864da97327870464611736.The commit was made to avoid zero damos quota goal target value, because it can trigger division-by-zero. Now the core layer handles the corner case. It returns an error for any attempt setting the aero
target_value. The corner case handling inDAMON_SAMPLE_MTIERis hence no more needed. Remove it.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=3