Skip to content

Commit 1c7777f

Browse files
Shyam Sainipetrpavlu
authored andcommitted
kernel: refactor lookup_or_create_module_kobject()
In the unlikely event of the allocation failing, it is better to let the machine boot with a not fully populated sysfs than to kill it with this BUG_ON(). All callers are already prepared for lookup_or_create_module_kobject() returning NULL. This is also preparation for calling this function from non __init code, where using BUG_ON for allocation failure handling is not acceptable. Since we are here, also start using IS_ENABLED instead of #ifdef construct. Suggested-by: Thomas Weißschuh <linux@weissschuh.net> Suggested-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> Signed-off-by: Shyam Saini <shyamsaini@linux.microsoft.com> Link: https://lore.kernel.org/r/20250227184930.34163-3-shyamsaini@linux.microsoft.com Signed-off-by: Petr Pavlu <petr.pavlu@suse.com>
1 parent bbc9462 commit 1c7777f

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

kernel/params.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -767,31 +767,28 @@ static struct module_kobject * __init lookup_or_create_module_kobject(const char
767767
int err;
768768

769769
kobj = kset_find_obj(module_kset, name);
770-
if (kobj) {
771-
mk = to_module_kobject(kobj);
772-
} else {
773-
mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
774-
BUG_ON(!mk);
775-
776-
mk->mod = THIS_MODULE;
777-
mk->kobj.kset = module_kset;
778-
err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
779-
"%s", name);
780-
#ifdef CONFIG_MODULES
781-
if (!err)
782-
err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
783-
#endif
784-
if (err) {
785-
kobject_put(&mk->kobj);
786-
pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
787-
name, err);
788-
return NULL;
789-
}
770+
if (kobj)
771+
return to_module_kobject(kobj);
790772

791-
/* So that we hold reference in both cases. */
792-
kobject_get(&mk->kobj);
773+
mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
774+
if (!mk)
775+
return NULL;
776+
777+
mk->mod = THIS_MODULE;
778+
mk->kobj.kset = module_kset;
779+
err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL, "%s", name);
780+
if (IS_ENABLED(CONFIG_MODULES) && !err)
781+
err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
782+
if (err) {
783+
kobject_put(&mk->kobj);
784+
pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
785+
name, err);
786+
return NULL;
793787
}
794788

789+
/* So that we hold reference in both cases. */
790+
kobject_get(&mk->kobj);
791+
795792
return mk;
796793
}
797794

0 commit comments

Comments
 (0)