Commit 1c7777fe authored by Shyam Saini's avatar Shyam Saini Committed by Petr Pavlu
Browse files

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: default avatarThomas Weißschuh <linux@weissschuh.net>
Suggested-by: default avatarRasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: default avatarShyam Saini <shyamsaini@linux.microsoft.com>
Link: https://lore.kernel.org/r/20250227184930.34163-3-shyamsaini@linux.microsoft.com


Signed-off-by: default avatarPetr Pavlu <petr.pavlu@suse.com>
parent bbc9462f
Loading
Loading
Loading
Loading
+19 −22
Original line number Diff line number Diff line
@@ -767,20 +767,18 @@ static struct module_kobject * __init lookup_or_create_module_kobject(const char
	int err;

	kobj = kset_find_obj(module_kset, name);
	if (kobj) {
		mk = to_module_kobject(kobj);
	} else {
	if (kobj)
		return to_module_kobject(kobj);

	mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
		BUG_ON(!mk);
	if (!mk)
		return NULL;

	mk->mod = THIS_MODULE;
	mk->kobj.kset = module_kset;
		err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
					   "%s", name);
#ifdef CONFIG_MODULES
		if (!err)
	err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL, "%s", name);
	if (IS_ENABLED(CONFIG_MODULES) && !err)
		err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
#endif
	if (err) {
		kobject_put(&mk->kobj);
		pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
@@ -790,7 +788,6 @@ static struct module_kobject * __init lookup_or_create_module_kobject(const char

	/* So that we hold reference in both cases. */
	kobject_get(&mk->kobj);
	}

	return mk;
}