Skip to content

Commit eded254

Browse files
sjp38akpm00
authored andcommitted
mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_regions_of()
damon_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. Link: https://lkml.kernel.org/r/20251101182021.74868-9-sj@kernel.org Fixes: 17ccae8 ("mm/damon: add kunit tests") Signed-off-by: SeongJae Park <sj@kernel.org> Cc: Brendan Higgins <brendan.higgins@linux.dev> Cc: David Gow <davidgow@google.com> Cc: Kefeng Wang <wangkefeng.wang@huawei.com> Cc: <stable@vger.kernel.org> [5.15+] Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 0998d27 commit eded254

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

mm/damon/tests/core-kunit.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,35 @@ static void damon_test_split_regions_of(struct kunit *test)
278278
struct damon_target *t;
279279
struct damon_region *r;
280280

281+
if (!c)
282+
kunit_skip("ctx alloc fail");
281283
t = damon_new_target();
284+
if (!t) {
285+
damon_destroy_ctx(c);
286+
kunit_skip(test, "target alloc fail");
287+
}
282288
r = damon_new_region(0, 22);
289+
if (!r) {
290+
damon_destroy_ctx(c);
291+
damon_free_target(t);
292+
kunit_skip(test, "region alloc fail");
293+
}
283294
damon_add_region(r, t);
284295
damon_split_regions_of(t, 2, DAMON_MIN_REGION);
285296
KUNIT_EXPECT_LE(test, damon_nr_regions(t), 2u);
286297
damon_free_target(t);
287298

288299
t = damon_new_target();
300+
if (!t) {
301+
damon_destroy_ctx(c);
302+
kunit_skip(test, "second target alloc fail");
303+
}
289304
r = damon_new_region(0, 220);
305+
if (!r) {
306+
damon_destroy_ctx(c);
307+
damon_free_target(t);
308+
kunit_skip(test, "second region alloc fail");
309+
}
290310
damon_add_region(r, t);
291311
damon_split_regions_of(t, 4, DAMON_MIN_REGION);
292312
KUNIT_EXPECT_LE(test, damon_nr_regions(t), 4u);

0 commit comments

Comments
 (0)