From patchwork Tue Feb 4 05:22:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shyam Saini X-Patchwork-Id: 13958605 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DE29E25A635; Tue, 4 Feb 2025 05:23:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738646592; cv=none; b=YGOwY6boxwKYz0NCeRaRueE9Zf4n9Smv8WpM6tHGuab1QzxkLqkAg2r2iTpdNUVaeWBp5GJBETUbSCuYqWnlBxaD2cEPF8EjMw/w+xp+v96YpaWsiG5/2mngNWi4grs7yB0JdCM7ziswzumBIYQH1sVQQTpgxbM6kATQWWoy/tI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738646592; c=relaxed/simple; bh=/UDhm/vaWOmQBD2Ymv1yhU7aLHSoK2nAEDtBJRfoU58=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FkUt+oxsYc41Ro9Aqu29UsVapv/S6OmEBiuLerUURhAd8c/KQBVfTTSBfpG0tPquD9n2hg2M3Cv1uE/S68/0Y4Yu3ceyWuh5FKW8kmJ0aSvhqnOI44DUKSChslEVNakzLX8jmCvMf1+b6PZT0na55wDdrUyeaIwwWIwV2nQNrR0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=EePQux87; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="EePQux87" Received: from thinkpad-p16sg1.corp.microsoft.com (unknown [20.236.11.42]) by linux.microsoft.com (Postfix) with ESMTPSA id 567A3205721D; Mon, 3 Feb 2025 21:23:10 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 567A3205721D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1738646590; bh=csRcyVR+eazDhu8zA5aLUmc3OVGKk3oiP6AFa429sl0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EePQux87gxUelpWpxJzIu/HscLLmmNpNJAa16aRL4Mn1hZVk4e9b9v7ulwJEkS3MU 3gjMX50I8yFOv0DMOzKt2ZC9b4elPtmJpmxievXOfChkfAPiSX0mjMJZYUt5oKlG4y G5oI51QM517ucJlYn6fxBO0orAbBb9zZSm5AoMlw= From: Shyam Saini To: linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org Cc: code@tyhicks.com, linux@rasmusvillemoes.dk, mcgrof@kernel.org, frkaya@linux.microsoft.com, vijayb@linux.microsoft.com, petr.pavlu@suse.com, linux@weissschuh.net, samitolvanen@google.com, da.gomez@samsung.com, gregkh@linuxfoundation.org, rafael@kernel.org, dakr@kernel.org Subject: [v1 1/3] kernel: param: rename locate_module_kobject Date: Mon, 3 Feb 2025 21:22:20 -0800 Message-Id: <20250204052222.1611510-2-shyamsaini@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250204052222.1611510-1-shyamsaini@linux.microsoft.com> References: <20250204052222.1611510-1-shyamsaini@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 locate_module_kobject() look up for existing module_kobject For given module name locate_module_kobject() look up for corresponding module_kobject if it exists. If it coudln't find its corresponding module_kobject then it creates one for the given name. Rename locate_module_kobject() to lookup_or_create_module_kobject(), to better describe its operations. This doesn't change anything functionality wise. Suggested-by: Rasmus Villemoes Signed-off-by: Shyam Saini --- kernel/params.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/kernel/params.c b/kernel/params.c index 0074d29c9b80..4b43baaf7c83 100644 --- a/kernel/params.c +++ b/kernel/params.c @@ -763,7 +763,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; @@ -805,10 +805,9 @@ 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; - /* We need to remove old parameters before adding more. */ if (mk->mp) sysfs_remove_group(&mk->kobj, &mk->mp->grp); @@ -876,7 +875,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); From patchwork Tue Feb 4 05:22:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shyam Saini X-Patchwork-Id: 13958606 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 67881155300; Tue, 4 Feb 2025 05:23:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738646592; cv=none; b=SR74IatS44HhOjw+EAp6ds0WaZ3o8FbwpkciuGAd1+CwD7XOqPiXsSZDiwLQbbRQ87xFLpXWoP3lXU6kQQ8/1X2wwcTimarV/qruOFbHyaQiCsU9jNWoq3yZhMI4PJT1O3pmBBYBcYP0Df7IQqng5KQ9nlztkSM/TGmI9VyNy4g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738646592; c=relaxed/simple; bh=UVp817FmXr3cNnMienD40UydCcCR1Q1BOqMpQxywWiQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qFmQp3DVhIdL2a3lXoAbvjanqDeOl8HCl2FA8nAjttKWZNaB9tr8AHQbw38Ldk2RQcMKWmNUwPQRbQAW88Gu6GH/vgpB9+gE6MMPotHn3HyLk5Qys1Mx5c5tl2xq0m0zOWVbBU3NYxH/3Bk83xMaRlcICQMkKSFANLOL0eGI0Cs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=FKmo535+; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="FKmo535+" Received: from thinkpad-p16sg1.corp.microsoft.com (unknown [20.236.11.42]) by linux.microsoft.com (Postfix) with ESMTPSA id A5C5B2066C1F; Mon, 3 Feb 2025 21:23:10 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A5C5B2066C1F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1738646590; bh=cvXuNjcj9yYbFPM1G7gRfvjS+yimOvGhctKDxaPytKU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FKmo535+oDG0PQO9PCFs/4b7AZJOZNRpJJ7JU4+GfEadEltP2iruxo+NkmwsAs9Nw JvAJ4dLv5T+bT01mhafrwxMzDIr7pQqFfo3+ZztyC0OBTa8uWMvdCwOKvBT6jIAtTL t0WT+v2mpuFoXjMzuhSMz3iU326p9AMHoPGm62XE= From: Shyam Saini To: linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org Cc: code@tyhicks.com, linux@rasmusvillemoes.dk, mcgrof@kernel.org, frkaya@linux.microsoft.com, vijayb@linux.microsoft.com, petr.pavlu@suse.com, linux@weissschuh.net, samitolvanen@google.com, da.gomez@samsung.com, gregkh@linuxfoundation.org, rafael@kernel.org, dakr@kernel.org Subject: [v1 2/3] include: move lookup_or_create_module_kobject()/to_module* to module.h Date: Mon, 3 Feb 2025 21:22:21 -0800 Message-Id: <20250204052222.1611510-3-shyamsaini@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250204052222.1611510-1-shyamsaini@linux.microsoft.com> References: <20250204052222.1611510-1-shyamsaini@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Move the following to module.h to allow common usages: - lookup_or_create_module_kobject() - to_module_attr - to_module_kobject This makes lookup_or_create_module_kobject() as inline. Signed-off-by: Shyam Saini --- include/linux/module.h | 39 +++++++++++++++++++++++++++++++++++++++ kernel/params.c | 39 --------------------------------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index 12f8a7d4fc1c..ba5f5e6c3927 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -894,8 +894,47 @@ static inline void *module_writable_address(struct module *mod, void *loc) #endif /* CONFIG_MODULES */ #ifdef CONFIG_SYSFS +/* sysfs output in /sys/modules/XYZ/parameters/ */ +#define to_module_attr(n) container_of_const(n, struct module_attribute, attr) +#define to_module_kobject(n) container_of(n, struct module_kobject, kobj) extern struct kset *module_kset; extern const struct kobj_type module_ktype; + +static inline struct module_kobject * __init lookup_or_create_module_kobject(const char *name) +{ + struct module_kobject *mk; + struct kobject *kobj; + 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; + } + + /* So that we hold reference in both cases. */ + kobject_get(&mk->kobj); + } + + return mk; +} + #endif /* CONFIG_SYSFS */ #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x) diff --git a/kernel/params.c b/kernel/params.c index 4b43baaf7c83..3c0798b79f76 100644 --- a/kernel/params.c +++ b/kernel/params.c @@ -537,10 +537,6 @@ const struct kernel_param_ops param_ops_string = { }; EXPORT_SYMBOL(param_ops_string); -/* sysfs output in /sys/modules/XYZ/parameters/ */ -#define to_module_attr(n) container_of_const(n, struct module_attribute, attr) -#define to_module_kobject(n) container_of(n, struct module_kobject, kobj) - struct param_attribute { struct module_attribute mattr; @@ -763,41 +759,6 @@ 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 *mk; - struct kobject *kobj; - 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; - } - - /* So that we hold reference in both cases. */ - kobject_get(&mk->kobj); - } - - return mk; -} - static void __init kernel_add_sysfs_param(const char *name, const struct kernel_param *kparam, unsigned int name_skip) From patchwork Tue Feb 4 05:22:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shyam Saini X-Patchwork-Id: 13958607 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A182819B5A9; Tue, 4 Feb 2025 05:23:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738646593; cv=none; b=XoL9zQR3YzQd+7XNs7NjtVb0KhGF8Z1AI9bGHfmK0/x/02+f8XZc5c+9dPUu9+egTvxy+jKPgA6uLG0IaICMCFgSBnCnOJtL+y+ZMXvFjcK9YXzAzU+e/QPkVqseB5NxjvacBp6dVYodisqtuW5ev4erS+8Dw012WOel7iDYTdQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738646593; c=relaxed/simple; bh=p6Kok+qq/l2B3e2e4G34QtRJ4FvWDnVU8naLbf3/tmc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AkbGhn7b3e4Rspj91yK/zTi6DAzC1rx7P7IyM3PvP4cffoySXzkfOVPsYjd7iTnivHcA8LV0StdP/TgbOMkbkiTg93rS+HdVeRS6wPdGAQFeqAs3YJWYg0br8eQHv1dggYTE9aJ9Z5G8h9kBf6Zip0Wz2BEUKtibyzKdFtllAfk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=fqcOuSdC; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="fqcOuSdC" Received: from thinkpad-p16sg1.corp.microsoft.com (unknown [20.236.11.42]) by linux.microsoft.com (Postfix) with ESMTPSA id 14426206AB6B; Mon, 3 Feb 2025 21:23:11 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 14426206AB6B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1738646591; bh=f5DIEBK/28IXN8N2iEB0e9xXQ45c/QNvM+ed7OW3uQU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fqcOuSdCj74sxw2TfcszdnZ1SWXhXpg6VxEcNLiRP3WFIeBMFxRaU8hRpmXxXdDaH acCKueQrVP55clhraQeaXMiTZQVk108A3QzuhwQEEioNrpUJKCSdCbMraaU4PB6KpD zt7F06zXXCaAoH0FLD4YYkz5E0+3QRn1lPcSyFqk= From: Shyam Saini To: linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org Cc: code@tyhicks.com, linux@rasmusvillemoes.dk, mcgrof@kernel.org, frkaya@linux.microsoft.com, vijayb@linux.microsoft.com, petr.pavlu@suse.com, linux@weissschuh.net, samitolvanen@google.com, da.gomez@samsung.com, gregkh@linuxfoundation.org, rafael@kernel.org, dakr@kernel.org Subject: [v1 3/3] drivers: base: handle module_kboject creation Date: Mon, 3 Feb 2025 21:22:22 -0800 Message-Id: <20250204052222.1611510-4-shyamsaini@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250204052222.1611510-1-shyamsaini@linux.microsoft.com> References: <20250204052222.1611510-1-shyamsaini@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 module_add_driver() relies on module_kset list for /sys/module//drivers directory creation. Since, commit 96a1a2412acb ("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//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") Suggested-by: Rasmus Villemoes Signed-off-by: Shyam Saini --- drivers/base/module.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/drivers/base/module.c b/drivers/base/module.c index 5bc71bea883a..218aaa096455 100644 --- a/drivers/base/module.c +++ b/drivers/base/module.c @@ -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); } }