Skip to content

Commit 7b5ab04

Browse files
Malaya Kumar RoutKAGA-KOKO
authored andcommitted
timekeeping: Fix resource leak in tk_aux_sysfs_init() error paths
tk_aux_sysfs_init() returns immediately on error during the auxiliary clock initialization loop without cleaning up previously allocated kobjects and sysfs groups. If kobject_create_and_add() or sysfs_create_group() fails during loop iteration, the parent kobjects (tko and auxo) and any previously created child kobjects are leaked. Fix this by adding proper error handling with goto labels to ensure all allocated resources are cleaned up on failure. kobject_put() on the parent kobjects will handle cleanup of their children. Fixes: 7b95663 ("timekeeping: Provide interface to control auxiliary clocks") Signed-off-by: Malaya Kumar Rout <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 807e0d1 commit 7b5ab04

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

kernel/time/timekeeping.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,29 +3060,32 @@ static const struct attribute_group aux_clock_enable_attr_group = {
30603060
static int __init tk_aux_sysfs_init(void)
30613061
{
30623062
struct kobject *auxo, *tko = kobject_create_and_add("time", kernel_kobj);
3063+
int ret = -ENOMEM;
30633064

30643065
if (!tko)
3065-
return -ENOMEM;
3066+
return ret;
30663067

30673068
auxo = kobject_create_and_add("aux_clocks", tko);
3068-
if (!auxo) {
3069-
kobject_put(tko);
3070-
return -ENOMEM;
3071-
}
3069+
if (!auxo)
3070+
goto err_clean;
30723071

30733072
for (int i = 0; i < MAX_AUX_CLOCKS; i++) {
30743073
char id[2] = { [0] = '0' + i, };
30753074
struct kobject *clk = kobject_create_and_add(id, auxo);
30763075

30773076
if (!clk)
3078-
return -ENOMEM;
3079-
3080-
int ret = sysfs_create_group(clk, &aux_clock_enable_attr_group);
3077+
goto err_clean;
30813078

3079+
ret = sysfs_create_group(clk, &aux_clock_enable_attr_group);
30823080
if (ret)
3083-
return ret;
3081+
goto err_clean;
30843082
}
30853083
return 0;
3084+
3085+
err_clean:
3086+
kobject_put(auxo);
3087+
kobject_put(tko);
3088+
return ret;
30863089
}
30873090
late_initcall(tk_aux_sysfs_init);
30883091

0 commit comments

Comments
 (0)