Skip to contentSkip to conversation

Properly handle module_kobject creation

PATCH In review Shyam Saini 3 participants 12 messages Feb 27, 2025 – Apr 16, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover Properly handle module_kobject creation

Hi Everyone,

This patch series fixes handling of module_kobject creation. A driver expect module_kset list populated with its corresponding module_kobject to create its /sys/module/<built-in-module>/drivers directory.

Since,
[1] commit 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time") Call to populate module_kset list is deferred to save init time so that
external watchdog doesn't fireup on some boards and Linux can take
responsibility of feeding watchdog before it spuriously resets the
system. However, [1] this fix caused another issue i.e, consumers
of module_kset can't get related module_kobject during driver
initialisation and hence can't create their
/sys/module/<built-in-module>/drivers directory.

Consequently, [1] breaks user-space applications for eg: DPDK, which expects /sys/module/vfio_pci/drivers/pci:vfio-pci/new_id to be present.

The second issue was reported and the [2] revert of [1] was proposed. However, [2] the Revert doesn't address the original issue reported in [1].

This patch series addresses both issues reported in [1] and [2].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=96a1a2412acb
[2] https://lore.kernel.org/lkml/[email protected]/

Thanks,
Shyam

---
- Changes in v4;
- Restore accidentally removed empty line in kernel_add_sysfs_param()
- Prepare this patch series for stable tree inclusion

- Changes in v3:
- Split refactoring and globalisation of lookup_or_create_module_kobject() into two patches
- Fix commit message to better describe the change.
- Use IS_ENABLED instead of #ifdef macro construct
- Undo the moving of __modinit macro construct into module.h
- drop __modinit from lookup_or_create_module_kobject() declaration
- Link: https://lore.kernel.org/lkml/[email protected]/

Changes in v2:
- Undo the moving of to_module* and lookup_or_create_module_kobject() into module.h
- Refactor and globalize lookup_or_create_module_kobject()
- move __modinit macro construct to module.h
- Fix commit message typos
- Link: https://lore.kernel.org/lkml/[email protected]/

Changes in v1:
- Rename locate_module_kobject() to lookup_or_create_module_kobject() to accurately describe its operations.
- Moves lookup_or_create_module_kobject() and to_module* macros to module.h, so that driver code can use these.
- Handle module_kobject creation and population of module_kset list to fix [1] and [2] issues.
- Link: https://lore.kernel.org/lkml/[email protected]/

Changes in RFC:
Reverts commit 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time")

Shyam Saini (4):
kernel: param: rename locate_module_kobject
kernel: refactor lookup_or_create_module_kobject()
kernel: globalize lookup_or_create_module_kobject()
drivers: base: handle module_kobject creation

 drivers/base/module.c  | 13 +++++-------
 include/linux/module.h |  2 ++
 kernel/params.c        | 47 ++++++++++++++++++++----------------------
 3 files changed, 29 insertions(+), 33 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/4 kernel: param: rename locate_module_kobject

The locate_module_kobject() function looks up an existing module_kobject for a given module name. If it cannot find the corresponding module_kobject, it creates one for the given name.

This commit renames locate_module_kobject() to lookup_or_create_module_kobject() to better describe its operations.

This doesn't change anything functionality wise.

Fixes 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time")
---
 kernel/params.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/params.c +3 -3
index 2509f216c9f3..a2441ce059ae 100644
@@ -760,7 +760,7 @@ void destroy_params(const struct kernel_param *params, unsigned num)
 			params[i].ops->free(params[i].arg);
 }
 
-static struct module_kobject * __init locate_module_kobject(const char *name)
+static struct module_kobject * __init lookup_or_create_module_kobject(const char *name)
 {
 	struct module_kobject *mk;
 	struct kobject *kobj;
@@ -802,7 +802,7 @@ static void __init kernel_add_sysfs_param(const char *name,
 	struct module_kobject *mk;
 	int err;
 
-	mk = locate_module_kobject(name);
+	mk = lookup_or_create_module_kobject(name);
 	if (!mk)
 		return;
 
@@ -873,7 +873,7 @@ static void __init version_sysfs_builtin(void)
 	int err;
 
 	for (vattr = __start___modver; vattr < __stop___modver; vattr++) {
-		mk = locate_module_kobject(vattr->module_name);
+		mk = lookup_or_create_module_kobject(vattr->module_name);
 		if (mk) {
 			err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
 			WARN_ON_ONCE(err);
Signature · 1 line
2.34.1
#2 lore raw

PATCH v4 2/4 kernel: refactor lookup_or_create_module_kobject()

In reply to Shyam Saini

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.

Fixes 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time")
---
 kernel/params.c | 41 +++++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 22 deletions(-)
kernel/params.c +19 -22
index a2441ce059ae..787662663e34 100644
@@ -767,31 +767,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;
 }
 
Signature · 1 line
2.34.1
#3 lore raw

PATCH v4 3/4 kernel: globalize lookup_or_create_module_kobject()

In reply to Shyam Saini

lookup_or_create_module_kobject() is marked as static and __init, to make it global drop static keyword.
Since this function can be called from non-init code, use __modinit instead of __init, __modinit marker will make it __init if CONFIG_MODULES is not defined.

Fixes 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time")
---
 include/linux/module.h | 2 ++
 kernel/params.c        | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/module.h +2 -0
index d9a5183a9fe7..57044f2838be 100644
@@ -162,6 +162,8 @@ extern void cleanup_module(void);
 #define __INITRODATA_OR_MODULE __INITRODATA
 #endif /*CONFIG_MODULES*/
 
+struct module_kobject *lookup_or_create_module_kobject(const char *name);
+
 /* Generic info of form tag = "info" */
 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
 
kernel/params.c +1 -1
index 787662663e34..e668fc90b83e 100644
@@ -760,7 +760,7 @@ void destroy_params(const struct kernel_param *params, unsigned num)
 			params[i].ops->free(params[i].arg);
 }
 
-static struct module_kobject * __init lookup_or_create_module_kobject(const char *name)
+struct module_kobject __modinit * lookup_or_create_module_kobject(const char *name)
 {
 	struct module_kobject *mk;
 	struct kobject *kobj;
Signature · 1 line
2.34.1
#4 lore raw

PATCH v4 4/4 drivers: base: handle module_kobject creation

In reply to Shyam Saini

module_add_driver() relies on module_kset list for /sys/module/<built-in-module>/drivers directory creation.

Since,
commit 96a1a2412acba ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time") drivers which are initialized from subsys_initcall() or any other
higher precedence initcall couldn't find the related kobject entry
in the module_kset list because module_kset is not fully populated
by the time module_add_driver() refers it. As a consequence,
module_add_driver() returns early without calling make_driver_name().
Therefore, /sys/module/<built-in-module>/drivers is never created.

Fix this issue by letting module_add_driver() handle module_kobject creation itself.

Fixes 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time")
---
 drivers/base/module.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/base/module.c +5 -8
index 5bc71bea883a..218aaa096455 100644
@@ -42,16 +42,13 @@ int module_add_driver(struct module *mod, const struct device_driver *drv)
 	if (mod)
 		mk = &mod->mkobj;
 	else if (drv->mod_name) {
-		struct kobject *mkobj;
-
-		/* Lookup built-in module entry in /sys/modules */
-		mkobj = kset_find_obj(module_kset, drv->mod_name);
-		if (mkobj) {
-			mk = container_of(mkobj, struct module_kobject, kobj);
+		/* Lookup or create built-in module entry in /sys/modules */
+		mk = lookup_or_create_module_kobject(drv->mod_name);
+		if (mk) {
 			/* remember our module structure */
 			drv->p->mkobj = mk;
-			/* kset_find_obj took a reference */
-			kobject_put(mkobj);
+			/* lookup_or_create_module_kobject took a reference */
+			kobject_put(&mk->kobj);
 		}
 	}
 
Signature · 1 line
2.34.1
maintainer Acked-by on v4 4/4
#5 lore raw

Re: [PATCH v4 4/4] drivers: base: handle module_kobject creation

Quoted 51 lines · Shyam Saini

module_add_driver() relies on module_kset list for /sys/module/<built-in-module>/drivers directory creation.

Since,
commit 96a1a2412acba ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time") drivers which are initialized from subsys_initcall() or any other
higher precedence initcall couldn't find the related kobject entry
in the module_kset list because module_kset is not fully populated
by the time module_add_driver() refers it. As a consequence,
module_add_driver() returns early without calling make_driver_name().
Therefore, /sys/module/<built-in-module>/drivers is never created.

Fix this issue by letting module_add_driver() handle module_kobject creation itself.

Fixes 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time")
---
 drivers/base/module.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/base/module.c +5 -8
index 5bc71bea883a..218aaa096455 100644
@@ -42,16 +42,13 @@ int module_add_driver(struct module *mod, const struct device_driver *drv)
 	if (mod)
 		mk = &mod->mkobj;
 	else if (drv->mod_name) {
-		struct kobject *mkobj;
-
-		/* Lookup built-in module entry in /sys/modules */
-		mkobj = kset_find_obj(module_kset, drv->mod_name);
-		if (mkobj) {
-			mk = container_of(mkobj, struct module_kobject, kobj);
+		/* Lookup or create built-in module entry in /sys/modules */
+		mk = lookup_or_create_module_kobject(drv->mod_name);
+		if (mk) {
 			/* remember our module structure */
 			drv->p->mkobj = mk;
-			/* kset_find_obj took a reference */
-			kobject_put(mkobj);
+			/* lookup_or_create_module_kobject took a reference */
+			kobject_put(&mk->kobj);
 		}
 	}
 
Signature · 1 line
2.34.1
maintainer
#6 lore raw
In reply to Shyam Saini
Quoted 20 lines · Shyam Saini

Hi Everyone,

This patch series fixes handling of module_kobject creation. A driver expect module_kset list populated with its corresponding module_kobject to create its /sys/module/<built-in-module>/drivers directory.

Since,
[1] commit 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time") Call to populate module_kset list is deferred to save init time so that
external watchdog doesn't fireup on some boards and Linux can take
responsibility of feeding watchdog before it spuriously resets the
system. However, [1] this fix caused another issue i.e, consumers
of module_kset can't get related module_kobject during driver
initialisation and hence can't create their
/sys/module/<built-in-module>/drivers directory.

Consequently, [1] breaks user-space applications for eg: DPDK, which expects /sys/module/vfio_pci/drivers/pci:vfio-pci/new_id to be present.

Quoted 8 lines

The second issue was reported and the [2] revert of [1] was proposed. However, [2] the Revert doesn't address the original issue reported in [1].

This patch series addresses both issues reported in [1] and [2].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=96a1a2412acb
[2] https://lore.kernel.org/lkml/[email protected]/

This looks ok to me. I only think the Fixes: tag should have remained solely on the last patch in the series as that is the actual fix. I'll adjust it when picking up the patches.

I'm going to wait for a few days if others still would like to comment and then plan to queue this on modules-next.

@Greg, could I please get an Acked-by from you on the last patch in the series as this affects the code in the driver core?

Signature · 2 lines
Thanks,
Petr
#7 lore raw

Hi Petr,

Quoted 25 lines · Petr Pavlu
Quoted 24 lines · Shyam Saini

Hi Everyone,

This patch series fixes handling of module_kobject creation. A driver expect module_kset list populated with its corresponding module_kobject to create its /sys/module/<built-in-module>/drivers directory.

Since,
[1] commit 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time") Call to populate module_kset list is deferred to save init time so that
external watchdog doesn't fireup on some boards and Linux can take
responsibility of feeding watchdog before it spuriously resets the
system. However, [1] this fix caused another issue i.e, consumers
of module_kset can't get related module_kobject during driver
initialisation and hence can't create their
/sys/module/<built-in-module>/drivers directory.

Consequently, [1] breaks user-space applications for eg: DPDK, which expects /sys/module/vfio_pci/drivers/pci:vfio-pci/new_id to be present.

The second issue was reported and the [2] revert of [1] was proposed. However, [2] the Revert doesn't address the original issue reported in [1].

Quoted 9 lines
Quoted 4 lines

This patch series addresses both issues reported in [1] and [2].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=96a1a2412acb
[2] https://lore.kernel.org/lkml/[email protected]/

This looks ok to me. I only think the Fixes: tag should have remained solely on the last patch in the series as that is the actual fix. I'll adjust it when picking up the patches.

Thank you for pointing that out, will take care of it next time.

I makred rest of the dependent patches for stable inclusion as per this [1] doc and, there was a warning from checkpatch scripts without Fixes: tag
WARNING: The commit message has 'stable@', `perhaps it also needs a 'Fixes:' tag?

Thanks,
Shyam

[1] https://docs.kernel.org/process/stable-kernel-rules.html#option-1

#8 lore raw
In reply to Petr Pavlu

Hi Greg,

Quoted 34 lines · Shyam Saini
Quoted 20 lines

Hi Everyone,

This patch series fixes handling of module_kobject creation. A driver expect module_kset list populated with its corresponding module_kobject to create its /sys/module/<built-in-module>/drivers directory.

Since,
[1] commit 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time") Call to populate module_kset list is deferred to save init time so that
external watchdog doesn't fireup on some boards and Linux can take
responsibility of feeding watchdog before it spuriously resets the
system. However, [1] this fix caused another issue i.e, consumers
of module_kset can't get related module_kobject during driver
initialisation and hence can't create their
/sys/module/<built-in-module>/drivers directory.

Consequently, [1] breaks user-space applications for eg: DPDK, which expects /sys/module/vfio_pci/drivers/pci:vfio-pci/new_id to be present.

Quoted 8 lines

The second issue was reported and the [2] revert of [1] was proposed. However, [2] the Revert doesn't address the original issue reported in [1].

This patch series addresses both issues reported in [1] and [2].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=96a1a2412acb
[2] https://lore.kernel.org/lkml/[email protected]/

This looks ok to me. I only think the Fixes: tag should have remained solely on the last patch in the series as that is the actual fix. I'll adjust it when picking up the patches.

Quoted 6 lines

I'm going to wait for a few days if others still would like to comment and then plan to queue this on modules-next.

@Greg, could I please get an Acked-by from you on the last patch in the series as this affects the code in the driver core?

do you have any feedback on last patch of this series?

Thanks,
Shyam

#9 lore raw
In reply to Petr Pavlu

Hi Greg,

Quoted 34 lines · Shyam Saini
Quoted 20 lines

Hi Everyone,

This patch series fixes handling of module_kobject creation. A driver expect module_kset list populated with its corresponding module_kobject to create its /sys/module/<built-in-module>/drivers directory.

Since,
[1] commit 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time") Call to populate module_kset list is deferred to save init time so that
external watchdog doesn't fireup on some boards and Linux can take
responsibility of feeding watchdog before it spuriously resets the
system. However, [1] this fix caused another issue i.e, consumers
of module_kset can't get related module_kobject during driver
initialisation and hence can't create their
/sys/module/<built-in-module>/drivers directory.

Consequently, [1] breaks user-space applications for eg: DPDK, which expects /sys/module/vfio_pci/drivers/pci:vfio-pci/new_id to be present.

Quoted 8 lines

The second issue was reported and the [2] revert of [1] was proposed. However, [2] the Revert doesn't address the original issue reported in [1].

This patch series addresses both issues reported in [1] and [2].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=96a1a2412acb
[2] https://lore.kernel.org/lkml/[email protected]/

This looks ok to me. I only think the Fixes: tag should have remained solely on the last patch in the series as that is the actual fix. I'll adjust it when picking up the patches.

Quoted 6 lines

I'm going to wait for a few days if others still would like to comment and then plan to queue this on modules-next.

@Greg, could I please get an Acked-by from you on the last patch in the series as this affects the code in the driver core?

sorry for the frequent pings, could you please Ack last patch of this series or perhaps provide any feedback ?

Thanks,
Shyam

maintainer
#10 lore raw
Quoted 39 lines · Shyam Saini

Hi Greg,

Quoted 37 lines · Shyam Saini
Quoted 20 lines

Hi Everyone,

This patch series fixes handling of module_kobject creation. A driver expect module_kset list populated with its corresponding module_kobject to create its /sys/module/<built-in-module>/drivers directory.

Since,
[1] commit 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time") Call to populate module_kset list is deferred to save init time so that
external watchdog doesn't fireup on some boards and Linux can take
responsibility of feeding watchdog before it spuriously resets the
system. However, [1] this fix caused another issue i.e, consumers
of module_kset can't get related module_kobject during driver
initialisation and hence can't create their
/sys/module/<built-in-module>/drivers directory.

Consequently, [1] breaks user-space applications for eg: DPDK, which expects /sys/module/vfio_pci/drivers/pci:vfio-pci/new_id to be present.

Quoted 8 lines

The second issue was reported and the [2] revert of [1] was proposed. However, [2] the Revert doesn't address the original issue reported in [1].

This patch series addresses both issues reported in [1] and [2].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=96a1a2412acb
[2] https://lore.kernel.org/lkml/[email protected]/

This looks ok to me. I only think the Fixes: tag should have remained solely on the last patch in the series as that is the actual fix. I'll adjust it when picking up the patches.

I'm going to wait for a few days if others still would like to comment and then plan to queue this on modules-next.

Quoted 6 lines
Quoted 3 lines

@Greg, could I please get an Acked-by from you on the last patch in the series as this affects the code in the driver core?

sorry for the frequent pings, could you please Ack last patch of this series or perhaps provide any feedback ?

The merge window _just_ ended, please give us a chance to catch up...

maintainer
#11 lore raw
In reply to Shyam Saini
Quoted 20 lines · Shyam Saini

Hi Everyone,

This patch series fixes handling of module_kobject creation. A driver expect module_kset list populated with its corresponding module_kobject to create its /sys/module/<built-in-module>/drivers directory.

Since,
[1] commit 96a1a2412acb ("kernel/params.c: defer most of param_sysfs_init() to late_initcall time") Call to populate module_kset list is deferred to save init time so that
external watchdog doesn't fireup on some boards and Linux can take
responsibility of feeding watchdog before it spuriously resets the
system. However, [1] this fix caused another issue i.e, consumers
of module_kset can't get related module_kobject during driver
initialisation and hence can't create their
/sys/module/<built-in-module>/drivers directory.

Consequently, [1] breaks user-space applications for eg: DPDK, which expects /sys/module/vfio_pci/drivers/pci:vfio-pci/new_id to be present.

Quoted 8 lines

The second issue was reported and the [2] revert of [1] was proposed. However, [2] the Revert doesn't address the original issue reported in [1].

This patch series addresses both issues reported in [1] and [2].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=96a1a2412acb
[2] https://lore.kernel.org/lkml/[email protected]/

I've queued now the series on modules-fixes, with some minor cleanups to the commit messages.

I changed the "Cc: [email protected]" tag on the last patch to "Cc:
[email protected]". Documentation/process/stable-kernel-rules.rst recommends using the latter address by default, unless the patch is fixing unpublished vulnerabilities.

I've removed "Fixes: 96a1a2412acb" and "Cc: [email protected]" from the first three patches because only the final patch contains the actual fix. I suspect the stable tooling should automatically determine the dependencies in this case, but to be explicit, I added a note to [email protected] on the last patch indicating that it requires all other patches from the series.

Signature · 2 lines
Thanks,
Petr