Subject: Re: [PATCH v4 1/4] kernel: param: initialize module_kset before do_initcalls()

From: "Gary Guo" <[email-masked]>
Date: Mon, 27 Apr 2026 14:29:55 +0100

On Mon Apr 27, 2026 at 3:41 AM BST, Shashank Balaji wrote:
> module_kset is initialized in param_sysfs_init(), a subsys_initcall. A number
> of platform drivers register themselves prior to subsys_initcalls
> (tegra194_cbb_driver registers in a pure_initcall, for example). With an
> upcoming patch ("driver core: platform: set mod_name in driver registration")
> that sets their mod_name in struct device_driver, lookup_or_create_module_kobject()
> will be called for those drivers, which calls kset_find_obj(module_kset, mod_name).
> This causes a null deref because module_kset isn't alive yet.
>
> Fix this by initializing module_kset in do_basic_setup() before do_initcalls().
> Modernize the pr_warn while we're at it.
>
> Suggested-by: Greg Kroah-Hartman <[email-masked]>
> Suggested-by: Gary Guo <[email-masked]>

I didn't suggest this change :)

I suggested `pure_initcall`, which is just a one line change.

diff --git a/kernel/params.c b/kernel/params.c
index 74d620bc2521..ac088d4b09a9 100644
--- a/kernel/params.c
+++ b/kernel/params.c
@@ -957,7 +957,7 @@ static int __init param_sysfs_init(void)
 
 	return 0;
 }
-subsys_initcall(param_sysfs_init);
+pure_initcall(param_sysfs_init);
 
 /*
  * param_sysfs_builtin_init - add sysfs version and parameter

pure_initcall is level 0 so it happens before all other init calls. Does it not
work?

Best,
Gary

> Co-developed-by: Rahul Bukte <[email-masked]>
> Signed-off-by: Rahul Bukte <[email-masked]>
> Signed-off-by: Shashank Balaji <[email-masked]>
> ---
>  include/linux/module.h |  4 ++++
>  init/main.c            |  1 +
>  kernel/params.c        | 21 +++++++++------------
>  3 files changed, 14 insertions(+), 12 deletions(-)
>
> diff --git a/include/linux/module.h b/include/linux/module.h
> index 7566815fabbe..6478596e8f9f 100644
> --- a/include/linux/module.h
> +++ b/include/linux/module.h
> @@ -886,6 +886,10 @@ static inline void module_for_each_mod(int(*func)(struct module *mod, void *data
>  #ifdef CONFIG_SYSFS
>  extern struct kset *module_kset;
>  extern const struct kobj_type module_ktype;
> +
> +void param_sysfs_init(void);
> +#else
> +static inline void param_sysfs_init(void) {}
>  #endif /* CONFIG_SYSFS */
>  
>  #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
> diff --git a/init/main.c b/init/main.c
> index 96f93bb06c49..01552c6b62ff 100644
> --- a/init/main.c
> +++ b/init/main.c
> @@ -1486,6 +1486,7 @@ static void __init do_basic_setup(void)
>  	ksysfs_init();
>  	driver_init();
>  	init_irq_proc();
> +	param_sysfs_init();
>  	do_ctors();
>  	do_initcalls();
>  }
> diff --git a/kernel/params.c b/kernel/params.c
> index 74d620bc2521..d1e3934fb3a7 100644
> --- a/kernel/params.c
> +++ b/kernel/params.c
> @@ -942,22 +942,19 @@ const struct kobj_type module_ktype = {
>  /*
>   * param_sysfs_init - create "module" kset
>   *
> - * This must be done before the initramfs is unpacked and
> - * request_module() thus becomes possible, because otherwise the
> - * module load would fail in mod_sysfs_init.
> + * Must run before:
> + * - do_initcalls(): some drivers register during initcalls and rely on
> + *   module_kset existing for their sysfs module symlink.
> + * - rootfs_initcall (initramfs unpack): request_module() becomes possible.
> + *   But if module_kset is null, module load would fail in mod_sysfs_init(),
> + *   causing request_module() to fail.
>   */
> -static int __init param_sysfs_init(void)
> +void __init param_sysfs_init(void)
>  {
>  	module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
> -	if (!module_kset) {
> -		printk(KERN_WARNING "%s (%d): error creating kset\n",
> -			__FILE__, __LINE__);
> -		return -ENOMEM;
> -	}
> -
> -	return 0;
> +	if (!module_kset)
> +		pr_warn("Error creating module kset\n");
>  }
> -subsys_initcall(param_sysfs_init);
>  
>  /*
>   * param_sysfs_builtin_init - add sysfs version and parameter