[PATCH] selftests/cgroup: Fix error path leaks in test_percpu_basic

Yu Miao posted 1 patch 1 month ago
tools/testing/selftests/cgroup/test_kmem.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
[PATCH] selftests/cgroup: Fix error path leaks in test_percpu_basic
Posted by Yu Miao 1 month ago
When cg_name_indexed() returns NULL partway through the child creation
loop, the code returned -1 without running cleanup_children and cleanup.
That left the `parent` pathname allocation unreleased and did not remove
child cgroup directories already created under the parent. Fix by jumping
to cleanup_children instead of returning.

When cg_create() fails, `child` (the pathname from cg_name_indexed())
was not freed before cleanup_children. Fix by freeing `child` before
branching to cleanup_children.

Signed-off-by: Yu Miao <yumiao@kylinos.cn>
---
 tools/testing/selftests/cgroup/test_kmem.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/cgroup/test_kmem.c b/tools/testing/selftests/cgroup/test_kmem.c
index eeabd34bf083..12f59925500b 100644
--- a/tools/testing/selftests/cgroup/test_kmem.c
+++ b/tools/testing/selftests/cgroup/test_kmem.c
@@ -368,11 +368,15 @@ static int test_percpu_basic(const char *root)
 
 	for (i = 0; i < 1000; i++) {
 		child = cg_name_indexed(parent, "child", i);
-		if (!child)
-			return -1;
+		if (!child) {
+			ret = -1;
+			goto cleanup_children;
+		}
 
-		if (cg_create(child))
+		if (cg_create(child)) {
+			free(child);
 			goto cleanup_children;
+		}
 
 		free(child);
 	}
-- 
2.43.0
Re: [PATCH] selftests/cgroup: Fix error path leaks in test_percpu_basic
Posted by Tejun Heo 4 weeks, 1 day ago
Hello,

On Wed, May 13, 2026 at 10:39:07AM +0800, Yu Miao wrote:
> [PATCH] selftests/cgroup: Fix error path leaks in test_percpu_basic

Applied to cgroup/for-7.1-fixes with the following Fixes: tag added:

  Fixes: 90631e1dea55 ("kselftests: cgroup: add perpcu memory accounting test")

Thanks.

--
tejun