Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/damon/tests/sysfs-kunit: handle alloc failures on damon_sysfs_test_add_targets()

damon_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.

Link: https://lkml.kernel.org/r/20251101182021.74868-21-sj@kernel.org
Fixes: b8ee5575f763 ("mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()")
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> [6.7+]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

SeongJae Park and committed by
Andrew Morton
7d808bf1 0a63a0e7

+25
+25
mm/damon/tests/sysfs-kunit.h
··· 45 45 struct damon_ctx *ctx; 46 46 47 47 sysfs_targets = damon_sysfs_targets_alloc(); 48 + if (!sysfs_targets) 49 + kunit_skip(test, "sysfs_targets alloc fail"); 48 50 sysfs_targets->nr = 1; 49 51 sysfs_targets->targets_arr = kmalloc_array(1, 50 52 sizeof(*sysfs_targets->targets_arr), GFP_KERNEL); 53 + if (!sysfs_targets->targets_arr) { 54 + kfree(sysfs_targets); 55 + kunit_skip(test, "targets_arr alloc fail"); 56 + } 51 57 52 58 sysfs_target = damon_sysfs_target_alloc(); 59 + if (!sysfs_target) { 60 + kfree(sysfs_targets->targets_arr); 61 + kfree(sysfs_targets); 62 + kunit_skip(test, "sysfs_target alloc fail"); 63 + } 53 64 sysfs_target->pid = __damon_sysfs_test_get_any_pid(12, 100); 54 65 sysfs_target->regions = damon_sysfs_regions_alloc(); 66 + if (!sysfs_target->regions) { 67 + kfree(sysfs_targets->targets_arr); 68 + kfree(sysfs_targets); 69 + kfree(sysfs_target); 70 + kunit_skip(test, "sysfs_regions alloc fail"); 71 + } 72 + 55 73 sysfs_targets->targets_arr[0] = sysfs_target; 56 74 57 75 ctx = damon_new_ctx(); 76 + if (!ctx) { 77 + kfree(sysfs_targets->targets_arr); 78 + kfree(sysfs_targets); 79 + kfree(sysfs_target); 80 + kfree(sysfs_target->regions); 81 + kunit_skip(test, "ctx alloc fail"); 82 + } 58 83 59 84 damon_sysfs_add_targets(ctx, sysfs_targets); 60 85 KUNIT_EXPECT_EQ(test, 1u, nr_damon_targets(ctx));