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: 7b95663a3d96 ("timekeeping: Provide interface to control auxiliary clocks")
Signed-off-by: Malaya Kumar Rout <mrout@redhat.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://patch.msgid.link/20251120150213.246777-1-mrout@redhat.com

authored by Malaya Kumar Rout and committed by Thomas Gleixner 7b5ab04f 807e0d18

Changed files
+12 -9
kernel
+12 -9
kernel/time/timekeeping.c
··· 3060 static int __init tk_aux_sysfs_init(void) 3061 { 3062 struct kobject *auxo, *tko = kobject_create_and_add("time", kernel_kobj); 3063 3064 if (!tko) 3065 - return -ENOMEM; 3066 3067 auxo = kobject_create_and_add("aux_clocks", tko); 3068 - if (!auxo) { 3069 - kobject_put(tko); 3070 - return -ENOMEM; 3071 - } 3072 3073 for (int i = 0; i < MAX_AUX_CLOCKS; i++) { 3074 char id[2] = { [0] = '0' + i, }; 3075 struct kobject *clk = kobject_create_and_add(id, auxo); 3076 3077 if (!clk) 3078 - return -ENOMEM; 3079 3080 - int ret = sysfs_create_group(clk, &aux_clock_enable_attr_group); 3081 - 3082 if (ret) 3083 - return ret; 3084 } 3085 return 0; 3086 } 3087 late_initcall(tk_aux_sysfs_init); 3088
··· 3060 static int __init tk_aux_sysfs_init(void) 3061 { 3062 struct kobject *auxo, *tko = kobject_create_and_add("time", kernel_kobj); 3063 + int ret = -ENOMEM; 3064 3065 if (!tko) 3066 + return ret; 3067 3068 auxo = kobject_create_and_add("aux_clocks", tko); 3069 + if (!auxo) 3070 + goto err_clean; 3071 3072 for (int i = 0; i < MAX_AUX_CLOCKS; i++) { 3073 char id[2] = { [0] = '0' + i, }; 3074 struct kobject *clk = kobject_create_and_add(id, auxo); 3075 3076 if (!clk) 3077 + goto err_clean; 3078 3079 + ret = sysfs_create_group(clk, &aux_clock_enable_attr_group); 3080 if (ret) 3081 + goto err_clean; 3082 } 3083 return 0; 3084 + 3085 + err_clean: 3086 + kobject_put(auxo); 3087 + kobject_put(tko); 3088 + return ret; 3089 } 3090 late_initcall(tk_aux_sysfs_init); 3091