mm/damon/tests: fix memory bugs in kunit tests
Subsystems (4)
SeongJae Park
maintainercover mm/damon/tests: fix memory bugs in kunit tests
DAMON kunit tests were initially written assuming those will be run on environments that are well controlled and therefore tolerant to transient test failures and bugs in the test code itself. The user-mode linux based manual run of the tests is one example of such an environment. And the test code was written for adding more test coverage as fast as possible, over making those safe and reliable.
As a result, the tests resulted in having a number of bugs including real memory leaks, theoretical unhandled memory allocation failures, and unused memory allocations. The allocation failures that are not handled well are unlikely in the real world, since those allocations are too small to fail. But in theory, it can happen and cause inappropriate memory access.
It is arguable if bugs in test code can really harm users. But, anyway bugs are bugs that need to be fixed. Fix the bugs one by one. Also Cc stable@ for the fixes of memory leak and unhandled memory allocation failures. The unused memory allocations are only a matter of memory efficiency, so not Cc-ing stable@.
The first patch fixes memory leaks in the test code for the DAMON core layer.
Following fifteen, three, and one patches respectively fix unhandled memory allocation failures in the test code for DAMON core layer, virtual address space DAMON operation set, and DAMON sysfs interface, one by one per test function.
Final two patches remove memory allocations that are correctly deallocated at the end, but not really being used by any code.
SeongJae Park (22):
mm/damon/tests/core-kunit: fix memory leak in
damon_test_set_filters_default_reject()
mm/damon/tests/core-kunit: handle allocation failures in
damon_test_regions()
mm/damon/tests/core-kunit: handle memory failure from
damon_test_target()
mm/damon/tests/core-kunit: handle memory alloc failure from
damon_test_aggregate()
mm/damon/tests/core-kunit: handle alloc failures on
damon_test_split_at()
mm/damon/tests/core-kunit: handle alloc failures on
damon_test_merge_two()
mm/damon/tests/core-kunit: handle alloc failures on
dasmon_test_merge_regions_of()
mm/damon/tests/core-kunit: handle alloc failures on
damon_test_split_regions_of()
mm/damon/tests/core-kunit: handle alloc failures in
damon_test_ops_registration()
mm/damon/tests/core-kunit: handle alloc failures in
damon_test_set_regions()
mm/damon/tests/core-kunit: handle alloc failures in
damon_test_update_monitoring_result()
mm/damon/tests/core-kunit: handle alloc failure on
damon_test_set_attrs()
mm/damon/tests/core-kunit: handle alloc failres in
damon_test_new_filter()
mm/damon/tests/core-kunit: handle alloc failure on
damos_test_commit_filter()
mm/damon/tests/core-kunit: handle alloc failures on
damos_test_filter_out()
mm/damon/tests/core-kunit: handle alloc failures on
damon_test_set_filters_default_reject()
mm/damon/tests/vaddr-kunit: handle alloc failures on
damon_do_test_apply_three_regions()
mm/damon/tests/vaddr-kunit: handle alloc failures in
damon_test_split_evenly_fail()
mm/damon/tests/vaddr-kunit: handle alloc failures on
damon_test_split_evenly_succ()
mm/damon/tests/sysfs-kunit: handle alloc failures on
damon_sysfs_test_add_targets()
mm/damon/tests/core-kunit: remove unnecessary damon_ctx variable on
damon_test_split_at()
mm/damon/tests/core-kunit: remove unused ctx in
damon_test_split_regions_of()
mm/damon/tests/core-kunit.h | 125 ++++++++++++++++++++++++++++++++--- mm/damon/tests/sysfs-kunit.h | 25 +++++++ mm/damon/tests/vaddr-kunit.h | 26 +++++++- 3 files changed, 163 insertions(+), 13 deletions(-)
base-commit: 75f0c76bb8c01fdea838a601dc3326b11177c0d8
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 1/22 mm/damon/tests/core-kunit: fix memory leak in damon_test_set_filters_default_reject()
Kunit test function for damos_set_filters_default_reject() allocates two 'struct damos_filter' objects and not deallocates those, so that the memory for the two objects are leaked for every time the test runs. Fix this by deallocating those objects at the end of the test code.
--- mm/damon/tests/core-kunit.h | 3 +++ 1 file changed, 3 insertions(+)
mm/damon/tests/core-kunit.h +3 -0
@@ -598,6 +598,9 @@ static void damon_test_set_filters_default_reject(struct kunit *test)*/ KUNIT_EXPECT_EQ (test, scheme.core_filters_default_reject, false);KUNIT_EXPECT_EQ (test, scheme.ops_filters_default_reject, true); + +damos_free_filter (anon_filter); +damos_free_filter (target_filter); }static struct kunit_case damon_test_cases[] = {
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 2/22 mm/damon/tests/core-kunit: handle allocation failures in damon_test_regions()
In reply to SeongJae Parkdamon_test_regions() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 6 ++++++ 1 file changed, 6 insertions(+)
mm/damon/tests/core-kunit.h +6 -0
@@ -20,11 +20,17 @@ static void damon_test_regions(struct kunit *test)struct damon_target *t; r =damon_new_region (1 ,2 ); +if (!r) +kunit_skip (test,"region alloc fail" );KUNIT_EXPECT_EQ (test,1ul , r->ar.start);KUNIT_EXPECT_EQ (test,2ul , r->ar.end);KUNIT_EXPECT_EQ (test,0u , r->nr_accesses); t =damon_new_target (); +if (!t) { +damon_free_region (r); +kunit_skip (test,"target alloc fail" ); + }KUNIT_EXPECT_EQ (test,0u ,damon_nr_regions (t));damon_add_region (r, t);
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 3/22 mm/damon/tests/core-kunit: handle memory failure from damon_test_target()
In reply to SeongJae Parkdamon_test_target() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 7 +++++++ 1 file changed, 7 insertions(+)
mm/damon/tests/core-kunit.h +7 -0
@@ -58,7 +58,14 @@ static void damon_test_target(struct kunit *test)struct damon_ctx *c =damon_new_ctx ();struct damon_target *t; +if (!c) +kunit_skip (test,"ctx alloc fail" ); + t =damon_new_target (); +if (!t) { +damon_destroy_ctx (c); +kunit_skip (test,"target alloc fail" ); + }KUNIT_EXPECT_EQ (test,0u ,nr_damon_targets (c));damon_add_target (c, t);
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 4/22 mm/damon/tests/core-kunit: handle memory alloc failure from damon_test_aggregate()
In reply to SeongJae Parkdamon_test_aggregate() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
mm/damon/tests/core-kunit.h +11 -0
@@ -97,8 +97,15 @@ static void damon_test_aggregate(struct kunit *test)struct damon_region *r;int it, ir; +if (!ctx) +kunit_skip (test,"ctx alloc fail" ); +for (it =0 ; it <3 ; it++) { t =damon_new_target (); +if (!t) { +damon_destroy_ctx (ctx); +kunit_skip (test,"target alloc fail" ); + }damon_add_target (ctx, t); }
@@ -106,6 +113,10 @@ static void damon_test_aggregate(struct kunit *test)damon_for_each_target (t, ctx) {for (ir =0 ; ir <3 ; ir++) { r =damon_new_region (saddr[it][ir], eaddr[it][ir]); +if (!r) { +damon_destroy_ctx (ctx); +kunit_skip (test,"region alloc fail" ); + } r->nr_accesses = accesses[it][ir]; r->nr_accesses_bp = accesses[it][ir] *10000 ;damon_add_region (r, t);
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 5/22 mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_at()
In reply to SeongJae Parkdamon_test_split_at() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
mm/damon/tests/core-kunit.h +11 -0
@@ -148,8 +148,19 @@ static void damon_test_split_at(struct kunit *test)struct damon_target *t;struct damon_region *r, *r_new; +if (!c) +kunit_skip (test,"ctx alloc fail" ); t =damon_new_target (); +if (!t) { +damon_destroy_ctx (c); +kunit_skip (test,"target alloc fail" ); + } r =damon_new_region (0 ,100 ); +if (!r) { +damon_destroy_ctx (c); +damon_free_target (t); +kunit_skip (test,"region alloc fail" ); + } r->nr_accesses_bp =420000 ; r->nr_accesses =42 ; r->last_nr_accesses =15 ;
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 6/22 mm/damon/tests/core-kunit: handle alloc failures on damon_test_merge_two()
In reply to SeongJae Parkdamon_test_merge_two() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
mm/damon/tests/core-kunit.h +10 -0
@@ -188,11 +188,21 @@ static void damon_test_merge_two(struct kunit *test)int i; t =damon_new_target (); +if (!t) +kunit_skip (test,"target alloc fail" ); r =damon_new_region (0 ,100 ); +if (!r) { +damon_free_target (t); +kunit_skip (test,"region alloc fail" ); + } r->nr_accesses =10 ; r->nr_accesses_bp =100000 ;damon_add_region (r, t); r2 =damon_new_region (100 ,300 ); +if (!r2) { +damon_free_target (t); +kunit_skip (test,"second region alloc fail" ); + } r2->nr_accesses =20 ; r2->nr_accesses_bp =200000 ;damon_add_region (r2, t);
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 7/22 mm/damon/tests/core-kunit: handle alloc failures on dasmon_test_merge_regions_of()
In reply to SeongJae Parkdamon_test_merge_regions_of() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 6 ++++++ 1 file changed, 6 insertions(+)
mm/damon/tests/core-kunit.h +6 -0
@@ -248,8 +248,14 @@ static void damon_test_merge_regions_of(struct kunit *test)int i; t =damon_new_target (); +if (!t) +kunit_skip (test,"target alloc fail" );for (i =0 ; i <ARRAY_SIZE (sa); i++) { r =damon_new_region (sa[i], ea[i]); +if (!r) { +damon_free_target (t); +kunit_skip (test,"region alloc fail" ); + } r->nr_accesses = nrs[i]; r->nr_accesses_bp = nrs[i] *10000 ;damon_add_region (r, t);
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 8/22 mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_regions_of()
In reply to SeongJae Parkdamon_test_split_regions_of() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
mm/damon/tests/core-kunit.h +20 -0
@@ -278,15 +278,35 @@ static void damon_test_split_regions_of(struct kunit *test)struct damon_target *t;struct damon_region *r; +if (!c) +kunit_skip ("ctx alloc fail" ); t =damon_new_target (); +if (!t) { +damon_destroy_ctx (c); +kunit_skip (test,"target alloc fail" ); + } r =damon_new_region (0 ,22 ); +if (!r) { +damon_destroy_ctx (c); +damon_free_target (t); +kunit_skip (test,"region alloc fail" ); + }damon_add_region (r, t);damon_split_regions_of (t,2 ,DAMON_MIN_REGION );KUNIT_EXPECT_LE (test,damon_nr_regions (t),2u );damon_free_target (t); t =damon_new_target (); +if (!t) { +damon_destroy_ctx (c); +kunit_skip (test,"second target alloc fail" ); + } r =damon_new_region (0 ,220 ); +if (!r) { +damon_destroy_ctx (c); +damon_free_target (t); +kunit_skip (test,"second region alloc fail" ); + }damon_add_region (r, t);damon_split_regions_of (t,4 ,DAMON_MIN_REGION );KUNIT_EXPECT_LE (test,damon_nr_regions (t),4u );
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 9/22 mm/damon/tests/core-kunit: handle alloc failures in damon_test_ops_registration()
In reply to SeongJae Parkdamon_test_ops_registration() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 3 +++ 1 file changed, 3 insertions(+)
mm/damon/tests/core-kunit.h +3 -0
@@ -320,6 +320,9 @@ static void damon_test_ops_registration(struct kunit *test)struct damon_operations ops = {.id =DAMON_OPS_VADDR }, bak;bool need_cleanup = false; +if (!c) +kunit_skip (test,"ctx alloc fail" ); +/* DAMON_OPS_VADDR is registered only if CONFIG_DAMON_VADDR is set */ if (!damon_is_registered_ops (DAMON_OPS_VADDR )) { bak.id =DAMON_OPS_VADDR ;
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 10/22 mm/damon/tests/core-kunit: handle alloc failures in damon_test_set_regions()
In reply to SeongJae Parkdamon_test_set_regions() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
mm/damon/tests/core-kunit.h +15 -2
@@ -368,13 +368,26 @@ static void damon_test_ops_registration(struct kunit *test)static void damon_test_set_regions (struct kunit *test) {struct damon_target *t =damon_new_target (); -struct damon_region *r1 =damon_new_region (4 ,16 ); -struct damon_region *r2 =damon_new_region (24 ,32 ); +struct damon_region *r1, *r2;struct damon_addr_range range = {.start =8 , .end =28 };unsigned long expects[] = {8 ,16 ,16 ,24 ,24 ,28 };int expect_idx =0 ;struct damon_region *r; +if (!t) +kunit_skip (test,"target alloc fail" ); + r1 =damon_new_region (4 ,16 ); +if (!r1) { +damon_free_target (t); +kunit_skip (test,"region alloc fail" ); + } + r2 =damon_new_region (24 ,32 ); +if (!r2) { +damon_free_target (t); +damon_free_region (r1); +kunit_skip (test,"second region alloc fail" ); + } +damon_add_region (r1, t);damon_add_region (r2, t);damon_set_regions (t, &range,1 ,DAMON_MIN_REGION );
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 11/22 mm/damon/tests/core-kunit: handle alloc failures in damon_test_update_monitoring_result()
In reply to SeongJae Parkdamon_test_update_monitoring_result() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 3 +++ 1 file changed, 3 insertions(+)
mm/damon/tests/core-kunit.h +3 -0
@@ -429,6 +429,9 @@ static void damon_test_update_monitoring_result(struct kunit *test)struct damon_attrs new_attrs;struct damon_region *r =damon_new_region (3 ,7 ); +if (!r) +kunit_skip (test,"region alloc fail" ); + r->nr_accesses =15 ; r->nr_accesses_bp =150000 ; r->age =20 ;
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 12/22 mm/damon/tests/core-kunit: handle alloc failure on damon_test_set_attrs()
In reply to SeongJae Parkdamon_test_set_attrs() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 3 +++ 1 file changed, 3 insertions(+)
mm/damon/tests/core-kunit.h +3 -0
@@ -465,6 +465,9 @@ static void damon_test_set_attrs(struct kunit *test) .sample_interval = 5000, .aggr_interval = 100000,};struct damon_attrs invalid_attrs; +if (!c) +kunit_skip (test,"ctx alloc fail" ); +KUNIT_EXPECT_EQ (test,damon_set_attrs (c, &valid_attrs),0 ); invalid_attrs = valid_attrs;
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 13/22 mm/damon/tests/core-kunit: handle alloc failres in damon_test_new_filter()
In reply to SeongJae Parkdamon_test_new_filter() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 2 ++ 1 file changed, 2 insertions(+)
mm/damon/tests/core-kunit.h +2 -0
@@ -505,6 +505,8 @@ static void damos_test_new_filter(struct kunit *test)struct damos_filter *filter; filter =damos_new_filter (DAMOS_FILTER_TYPE_ANON , true, false); +if (!filter) +kunit_skip (test,"filter alloc fail" );KUNIT_EXPECT_EQ (test, filter->type,DAMOS_FILTER_TYPE_ANON );KUNIT_EXPECT_EQ (test, filter->matching, true);KUNIT_EXPECT_PTR_EQ (test, filter->list.prev, &filter->list);
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 14/22 mm/damon/tests/core-kunit: handle alloc failure on damos_test_commit_filter()
In reply to SeongJae Parkdamon_test_commit_filter() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
mm/damon/tests/core-kunit.h +10 -5
@@ -516,11 +516,16 @@ static void damos_test_new_filter(struct kunit *test)static void damos_test_commit_filter (struct kunit *test) { -struct damos_filter *src_filter =damos_new_filter ( -DAMOS_FILTER_TYPE_ANON , true, true); -struct damos_filter *dst_filter =damos_new_filter ( -DAMOS_FILTER_TYPE_ACTIVE , false, false); - +struct damos_filter *src_filter, *dst_filter; + + src_filter =damos_new_filter (DAMOS_FILTER_TYPE_ANON , true, true); +if (!src_filter) +kunit_skip (test,"src filter alloc fail" ); + dst_filter =damos_new_filter (DAMOS_FILTER_TYPE_ACTIVE , false, false); +if (!dst_filter) { +damos_destroy_filter (src_filter); +kunit_skip (test,"dst filter alloc fail" ); + }damos_commit_filter (dst_filter, src_filter);KUNIT_EXPECT_EQ (test, dst_filter->type, src_filter->type);KUNIT_EXPECT_EQ (test, dst_filter->matching, src_filter->matching);
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 15/22 mm/damon/tests/core-kunit: handle alloc failures on damos_test_filter_out()
In reply to SeongJae Parkdamon_test_filter_out() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
mm/damon/tests/core-kunit.h +11 -0
@@ -542,11 +542,22 @@ static void damos_test_filter_out(struct kunit *test)struct damos_filter *f; f =damos_new_filter (DAMOS_FILTER_TYPE_ADDR , true, false); +if (!f) +kunit_skip (test,"filter alloc fail" ); f->addr_range = (struct damon_addr_range ){ .start =DAMON_MIN_REGION *2 , .end =DAMON_MIN_REGION *6 }; t =damon_new_target (); +if (!t) { +damos_destroy_filter (f); +kunit_skip (test,"target alloc fail" ); + } r =damon_new_region (DAMON_MIN_REGION *3 ,DAMON_MIN_REGION *5 ); +if (!r) { +damos_destroy_filter (f); +damon_free_target (t); +kunit_skip (test,"region alloc fail" ); + }damon_add_region (r, t);/* region in the range */
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 16/22 mm/damon/tests/core-kunit: handle alloc failures on damon_test_set_filters_default_reject()
In reply to SeongJae Parkdamon_test_set_filters_default_reject() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/core-kunit.h | 6 ++++++ 1 file changed, 6 insertions(+)
mm/damon/tests/core-kunit.h +6 -0
@@ -659,6 +659,8 @@ static void damon_test_set_filters_default_reject(struct kunit *test)KUNIT_EXPECT_EQ (test, scheme.ops_filters_default_reject, false); target_filter =damos_new_filter (DAMOS_FILTER_TYPE_TARGET , true, true); +if (!target_filter) +kunit_skip (test,"filter alloc fail" );damos_add_filter (&scheme, target_filter);damos_set_filters_default_reject (&scheme);/*
@@ -684,6 +686,10 @@ static void damon_test_set_filters_default_reject(struct kunit *test)KUNIT_EXPECT_EQ (test, scheme.ops_filters_default_reject, false); anon_filter =damos_new_filter (DAMOS_FILTER_TYPE_ANON , true, true); +if (!anon_filter) { +damos_free_filter (target_filter); +kunit_skip (test,"anon_filter alloc fail" ); + }damos_add_filter (&scheme, anon_filter);damos_set_filters_default_reject (&scheme);
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 17/22 mm/damon/tests/vaddr-kunit: handle alloc failures on damon_do_test_apply_three_regions()
In reply to SeongJae Parkdamon_do_test_apply_three_regions() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/vaddr-kunit.h | 6 ++++++ 1 file changed, 6 insertions(+)
mm/damon/tests/vaddr-kunit.h +6 -0
@@ -136,8 +136,14 @@ static void damon_do_test_apply_three_regions(struct kunit *test,int i; t =damon_new_target (); +if (!t) +kunit_skip (test,"target alloc fail" );for (i =0 ; i < nr_regions /2 ; i++) { r =damon_new_region (regions[i *2 ], regions[i *2 +1 ]); +if (!r) { +damon_destroy_target (t,NULL ); +kunit_skip (test,"region alloc fail" ); + }damon_add_region (r, t); }
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 18/22 mm/damon/tests/vaddr-kunit: handle alloc failures in damon_test_split_evenly_fail()
In reply to SeongJae Parkdamon_test_split_evenly_fail() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/vaddr-kunit.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
mm/damon/tests/vaddr-kunit.h +10 -1
@@ -256,7 +256,16 @@ static void damon_test_split_evenly_fail(struct kunit *test, unsigned long start, unsigned long end, unsigned int nr_pieces) { struct damon_target *t = damon_new_target(); -struct damon_region *r =damon_new_region (start, end); +struct damon_region *r; + +if (!t) +kunit_skip (test,"target alloc fail" ); + + r =damon_new_region (start, end); +if (!r) { +damon_free_target (t); +kunit_skip (test,"region alloc fail" ); + }damon_add_region (r, t); KUNIT_EXPECT_EQ(test,
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 19/22 mm/damon/tests/vaddr-kunit: handle alloc failures on damon_test_split_evenly_succ()
In reply to SeongJae Parkdamon_test_split_evenly_succ() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/vaddr-kunit.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
mm/damon/tests/vaddr-kunit.h +8 -1
@@ -284,10 +284,17 @@ static void damon_test_split_evenly_succ(struct kunit *test, unsigned long start, unsigned long end, unsigned int nr_pieces) { struct damon_target *t = damon_new_target(); -struct damon_region *r =damon_new_region (start, end); +struct damon_region *r;unsigned long expected_width = (end - start) / nr_pieces;unsigned long i =0 ; +if (!t) +kunit_skip (test,"target alloc fail" ); + r =damon_new_region (start, end); +if (!r) { +damon_free_target (t); +kunit_skip (test,"region alloc fail" ); + }damon_add_region (r, t);KUNIT_EXPECT_EQ (test,damon_va_evenly_split_region (t, r, nr_pieces),0 );
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 20/22 mm/damon/tests/sysfs-kunit: handle alloc failures on damon_sysfs_test_add_targets()
In reply to SeongJae Parkdamon_sysfs_test_add_targets() is assuming all dynamic memory allocation in it will succeed. Those are indeed likely in the real use cases since those allocations are too small to fail, but theoretically those could fail. In the case, inappropriate memory access can happen. Fix it by appropriately cleanup pre-allocated memory and skip the execution of the remaining tests in the failure cases.
--- mm/damon/tests/sysfs-kunit.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
mm/damon/tests/sysfs-kunit.h +25 -0
@@ -45,16 +45,41 @@ static void damon_sysfs_test_add_targets(struct kunit *test)struct damon_ctx *ctx; sysfs_targets =damon_sysfs_targets_alloc (); +if (!sysfs_targets) +kunit_skip (test,"sysfs_targets alloc fail" ); sysfs_targets->nr =1 ; sysfs_targets->targets_arr =kmalloc_array (1 ,sizeof (*sysfs_targets->targets_arr),GFP_KERNEL ); +if (!sysfs_targets->targets_arr) { +kfree (sysfs_targets); +kunit_skip (test,"targets_arr alloc fail" ); + } sysfs_target =damon_sysfs_target_alloc (); +if (!sysfs_target) { +kfree (sysfs_targets->targets_arr); +kfree (sysfs_targets); +kunit_skip (test,"sysfs_target alloc fail" ); + } sysfs_target->pid =__damon_sysfs_test_get_any_pid (12 ,100 ); sysfs_target->regions =damon_sysfs_regions_alloc (); +if (!sysfs_target->regions) { +kfree (sysfs_targets->targets_arr); +kfree (sysfs_targets); +kfree (sysfs_target); +kunit_skip (test,"sysfs_regions alloc fail" ); + } + sysfs_targets->targets_arr[0 ] = sysfs_target; ctx =damon_new_ctx (); +if (!ctx) { +kfree (sysfs_targets->targets_arr); +kfree (sysfs_targets); +kfree (sysfs_target); +kfree (sysfs_target->regions); +kunit_skip (test,"ctx alloc fail" ); + }damon_sysfs_add_targets (ctx, sysfs_targets);KUNIT_EXPECT_EQ (test,1u ,nr_damon_targets (ctx));
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 21/22 mm/damon/tests/core-kunit: remove unnecessary damon_ctx variable on damon_test_split_at()
In reply to SeongJae Parkdamon_test_split_at() dynamically allocates a 'struct damon_ctx' object, but it is not really being used in the code other than handling the allocation failure and deallocating it at the end of the function. Remove the unnecessary allocation and deallocation of the object.
--- mm/damon/tests/core-kunit.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
mm/damon/tests/core-kunit.h +1 -8
@@ -144,20 +144,14 @@ static void damon_test_aggregate(struct kunit *test)static void damon_test_split_at (struct kunit *test) { -struct damon_ctx *c =damon_new_ctx ();struct damon_target *t;struct damon_region *r, *r_new; -if (!c) -kunit_skip (test,"ctx alloc fail" ); t =damon_new_target (); -if (!t) { -damon_destroy_ctx (c); +if (!t)kunit_skip (test,"target alloc fail" ); - } r =damon_new_region (0 ,100 );if (!r) { -damon_destroy_ctx (c);damon_free_target (t);kunit_skip (test,"region alloc fail" ); }
@@ -178,7 +172,6 @@ static void damon_test_split_at(struct kunit *test)KUNIT_EXPECT_EQ (test, r->last_nr_accesses, r_new->last_nr_accesses);damon_free_target (t); -damon_destroy_ctx (c); }static void damon_test_merge_two (struct kunit *test)
Signature · 1 line
2.47.3
SeongJae Park
maintainerPATCH 22/22 mm/damon/tests/core-kunit: remove unused ctx in damon_test_split_regions_of()
In reply to SeongJae Parkdamon_test_split_regions_of() dynamically allocates a 'struct damon_ctx' object, but it is not really being used in the code other than handling the allocation failure and deallocating it at the end of the function. Remove the unnecessary allocation and deallocation of the object.
--- mm/damon/tests/core-kunit.h | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
mm/damon/tests/core-kunit.h +2 -12
@@ -267,20 +267,14 @@ static void damon_test_merge_regions_of(struct kunit *test)static void damon_test_split_regions_of (struct kunit *test) { -struct damon_ctx *c =damon_new_ctx ();struct damon_target *t;struct damon_region *r; -if (!c) -kunit_skip ("ctx alloc fail" ); t =damon_new_target (); -if (!t) { -damon_destroy_ctx (c); +if (!t)kunit_skip (test,"target alloc fail" ); - } r =damon_new_region (0 ,22 );if (!r) { -damon_destroy_ctx (c);damon_free_target (t);kunit_skip (test,"region alloc fail" ); }
@@ -290,13 +284,10 @@ static void damon_test_split_regions_of(struct kunit *test)damon_free_target (t); t =damon_new_target (); -if (!t) { -damon_destroy_ctx (c); +if (!t)kunit_skip (test,"second target alloc fail" ); - } r =damon_new_region (0 ,220 );if (!r) { -damon_destroy_ctx (c);damon_free_target (t);kunit_skip (test,"second region alloc fail" ); }
@@ -304,7 +295,6 @@ static void damon_test_split_regions_of(struct kunit *test)damon_split_regions_of (t,4 ,DAMON_MIN_REGION );KUNIT_EXPECT_LE (test,damon_nr_regions (t),4u );damon_free_target (t); -damon_destroy_ctx (c); }static void damon_test_ops_registration (struct kunit *test)
Signature · 1 line
2.47.3