[PATCH v2] selftests/livepatch: fix resource leak in test_klp_syscall init error path

From
Rui Qi <hidden>
Date
2026-06-04 08:32:31
Also in
linux-kselftest, lkml
Reply to
Rui Qi, 2026-06-02
In livepatch_init(), if klp_enable_patch() fails, the previously
created kobject and sysfs file are never cleaned up, causing a
resource leak. Capture the return value and add proper cleanup
on the error path.

Signed-off-by: Rui Qi <redacted>
---
Changes in v2:
- Remove sysfs_remove_file() from the error path as suggested by
  Miroslav Benes. kobject_put() alone is sufficient because it
  eventually calls kobject_cleanup() -> __kobject_del() ->
  sysfs_remove_dir(), which removes the entire directory including
  all files created under it.

 .../selftests/livepatch/test_modules/test_klp_syscall.c     | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c b/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c
index 0630ffd9d9a1..08aacc0e14de 100644
--- a/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c
+++ b/tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c
@@ -109,7 +109,11 @@ static int livepatch_init(void)
 	 */
 	npids = npids_pending;
 
-	return klp_enable_patch(&patch);
+	ret = klp_enable_patch(&patch);
+	if (ret)
+		kobject_put(klp_kobj);
+
+	return ret;
 }
 
 static void livepatch_exit(void)
-- 
2.20.1
Thread (9 messages) 9 messages, 4 authors, last reply 2026-06-09
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help