Skip to content

Commit 1af5e8b

Browse files
Zheng Yejiangregkh
authored andcommitted
mm/damon/vaddr: fix issue in damon_va_evenly_split_region()
commit f3c7a1e upstream. Patch series "mm/damon/vaddr: Fix issue in damon_va_evenly_split_region()". v2. According to the logic of damon_va_evenly_split_region(), currently following split case would not meet the expectation: Suppose DAMON_MIN_REGION=0x1000, Case: Split [0x0, 0x3000) into 2 pieces, then the result would be acutually 3 regions: [0x0, 0x1000), [0x1000, 0x2000), [0x2000, 0x3000) but NOT the expected 2 regions: [0x0, 0x1000), [0x1000, 0x3000) !!! The root cause is that when calculating size of each split piece in damon_va_evenly_split_region(): `sz_piece = ALIGN_DOWN(sz_orig / nr_pieces, DAMON_MIN_REGION);` both the dividing and the ALIGN_DOWN may cause loss of precision, then each time split one piece of size 'sz_piece' from origin 'start' to 'end' would cause more pieces are split out than expected!!! To fix it, count for each piece split and make sure no more than 'nr_pieces'. In addition, add above case into damon_test_split_evenly(). And add 'nr_piece == 1' check in damon_va_evenly_split_region() for better code readability and add a corresponding kunit testcase. This patch (of 2): According to the logic of damon_va_evenly_split_region(), currently following split case would not meet the expectation: Suppose DAMON_MIN_REGION=0x1000, Case: Split [0x0, 0x3000) into 2 pieces, then the result would be acutually 3 regions: [0x0, 0x1000), [0x1000, 0x2000), [0x2000, 0x3000) but NOT the expected 2 regions: [0x0, 0x1000), [0x1000, 0x3000) !!! The root cause is that when calculating size of each split piece in damon_va_evenly_split_region(): `sz_piece = ALIGN_DOWN(sz_orig / nr_pieces, DAMON_MIN_REGION);` both the dividing and the ALIGN_DOWN may cause loss of precision, then each time split one piece of size 'sz_piece' from origin 'start' to 'end' would cause more pieces are split out than expected!!! To fix it, count for each piece split and make sure no more than 'nr_pieces'. In addition, add above case into damon_test_split_evenly(). After this patch, damon-operations test passed: # ./tools/testing/kunit/kunit.py run damon-operations [...] ============== damon-operations (6 subtests) =============== [PASSED] damon_test_three_regions_in_vmas [PASSED] damon_test_apply_three_regions1 [PASSED] damon_test_apply_three_regions2 [PASSED] damon_test_apply_three_regions3 [PASSED] damon_test_apply_three_regions4 [PASSED] damon_test_split_evenly ================ [PASSED] damon-operations ================= Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: 3f49584 ("mm/damon: implement primitives for the virtual memory address spaces") Signed-off-by: Zheng Yejian <[email protected]> Reviewed-by: SeongJae Park <[email protected]> Cc: Fernand Sieber <[email protected]> Cc: Leonard Foerster <[email protected]> Cc: Shakeel Butt <[email protected]> Cc: Ye Weihua <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4358f24 commit 1af5e8b

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

mm/damon/vaddr-test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ static void damon_test_split_evenly(struct kunit *test)
300300
damon_test_split_evenly_fail(test, 0, 100, 0);
301301
damon_test_split_evenly_succ(test, 0, 100, 10);
302302
damon_test_split_evenly_succ(test, 5, 59, 5);
303+
damon_test_split_evenly_succ(test, 0, 3, 2);
303304
damon_test_split_evenly_fail(test, 5, 6, 2);
304305
}
305306

mm/damon/vaddr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static int damon_va_evenly_split_region(struct damon_target *t,
6767
unsigned long sz_orig, sz_piece, orig_end;
6868
struct damon_region *n = NULL, *next;
6969
unsigned long start;
70+
unsigned int i;
7071

7172
if (!r || !nr_pieces)
7273
return -EINVAL;
@@ -80,8 +81,7 @@ static int damon_va_evenly_split_region(struct damon_target *t,
8081

8182
r->ar.end = r->ar.start + sz_piece;
8283
next = damon_next_region(r);
83-
for (start = r->ar.end; start + sz_piece <= orig_end;
84-
start += sz_piece) {
84+
for (start = r->ar.end, i = 1; i < nr_pieces; start += sz_piece, i++) {
8585
n = damon_new_region(start, start + sz_piece);
8686
if (!n)
8787
return -ENOMEM;

0 commit comments

Comments
 (0)