[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250211214842.1806521-3-shyamsaini@linux.microsoft.com>
Date: Tue, 11 Feb 2025 13:48:40 -0800
From: Shyam Saini <shyamsaini@...ux.microsoft.com>
To: linux-kernel@...r.kernel.org,
linux-modules@...r.kernel.org
Cc: code@...icks.com,
linux@...musvillemoes.dk,
christophe.leroy@...roup.eu,
hch@...radead.org,
mcgrof@...nel.org,
frkaya@...ux.microsoft.com,
vijayb@...ux.microsoft.com,
petr.pavlu@...e.com,
linux@...ssschuh.net,
samitolvanen@...gle.com,
da.gomez@...sung.com,
gregkh@...uxfoundation.org,
rafael@...nel.org,
dakr@...nel.org
Subject: [PATCH v3 2/4] 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@...ssschuh.net>
Suggested-by: Rasmus Villemoes <linux@...musvillemoes.dk>
Signed-off-by: Shyam Saini <shyamsaini@...ux.microsoft.com>
---
kernel/params.c | 41 +++++++++++++++++++----------------------
1 file changed, 19 insertions(+), 22 deletions(-)
diff --git a/kernel/params.c b/kernel/params.c
index 4b43baaf7c83..6e87aef876b2 100644
--- a/kernel/params.c
+++ b/kernel/params.c
@@ -770,31 +770,28 @@ 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 {
- mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
- BUG_ON(!mk);
-
- 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 = 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",
- name, err);
- return NULL;
- }
+ if (kobj)
+ return to_module_kobject(kobj);
- /* So that we hold reference in both cases. */
- kobject_get(&mk->kobj);
+ mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
+ 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);
+ if (IS_ENABLED(CONFIG_MODULES) && !err)
+ err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
+ if (err) {
+ kobject_put(&mk->kobj);
+ pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
+ name, err);
+ return NULL;
}
+ /* So that we hold reference in both cases. */
+ kobject_get(&mk->kobj);
+
return mk;
}
--
2.34.1
Powered by blists - more mailing lists