linux-modules.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/3] modpost: allow "make nsdeps" to skip module-specific symbol namespace
       [not found] <20250522071744.2362563-1-masahiroy@kernel.org>
@ 2025-05-22  7:17 ` Masahiro Yamada
  2025-05-27  7:55   ` Petr Pavlu
  0 siblings, 1 reply; 2+ messages in thread
From: Masahiro Yamada @ 2025-05-22  7:17 UTC (permalink / raw)
  To: linux-kbuild
  Cc: linux-kernel, Masahiro Yamada, Daniel Gomez, Luis Chamberlain,
	Nathan Chancellor, Nicolas Schier, Peter Zijlstra, Petr Pavlu,
	Sami Tolvanen, linux-modules

When MODULE_IMPORT_NS() is missing, "make nsdeps" runs the Coccinelle
script to automatically add MODULE_IMPORT_NS() to each module.

This should not occur for users of EXPORT_SYMBOL_GPL_FOR_MODULES(), which
is intended to export a symbol to a specific module only. In such cases,
explicitly adding MODULE_IMPORT_NS("module:...") is disallowed.

This commit handles the latter case separately in order not to trigger
the Coccinelle, and displays the error message:

  ERROR: modpost: module "foo" uses symbol "bar", which is exported only for module "baz"

Apply the same logic for kernel space as well.

Fixes: 092a4f5985f2 ("module: Add module specific symbol namespace support")
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 kernel/module/main.c  | 37 ++++++++++++++++++++-----------------
 scripts/mod/modpost.c | 35 ++++++++++++++++++-----------------
 2 files changed, 38 insertions(+), 34 deletions(-)

diff --git a/kernel/module/main.c b/kernel/module/main.c
index 81035f6552ec..642f790c47e7 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -65,6 +65,8 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/module.h>
 
+#define MODULE_NS_PREFIX "module:"
+
 /*
  * Mutex protects:
  * 1) List of modules (also safely readable within RCU read section),
@@ -1108,28 +1110,21 @@ static char *get_modinfo(const struct load_info *info, const char *tag)
 }
 
 /**
- * verify_module_namespace() - does @modname have access to this symbol's @namespace
- * @namespace: export symbol namespace
+ * module_match() - check if @modname matches @patterns
  * @modname: module name
+ * @patterns: comma separated patterns
  *
- * If @namespace is prefixed with "module:" to indicate it is a module namespace
- * then test if @modname matches any of the comma separated patterns.
- *
- * The patterns only support tail-glob.
+ * The @patterns only supports tail-glob.
  */
-static bool verify_module_namespace(const char *namespace, const char *modname)
+static bool module_match(const char *modname, const char *patterns)
 {
 	size_t len, modlen = strlen(modname);
-	const char *prefix = "module:";
 	const char *sep;
 	bool glob;
 
-	if (!strstarts(namespace, prefix))
-		return false;
-
-	for (namespace += strlen(prefix); *namespace; namespace = sep) {
-		sep = strchrnul(namespace, ',');
-		len = sep - namespace;
+	for (; *patterns; patterns = sep) {
+		sep = strchrnul(patterns, ',');
+		len = sep - patterns;
 
 		glob = false;
 		if (sep[-1] == '*') {
@@ -1140,7 +1135,7 @@ static bool verify_module_namespace(const char *namespace, const char *modname)
 		if (*sep)
 			sep++;
 
-		if (mod_strncmp(namespace, modname, len) == 0 && (glob || len == modlen))
+		if (mod_strncmp(patterns, modname, len) == 0 && (glob || len == modlen))
 			return true;
 	}
 
@@ -1157,8 +1152,16 @@ static int verify_namespace_is_imported(const struct load_info *info,
 	namespace = kernel_symbol_namespace(sym);
 	if (namespace && namespace[0]) {
 
-		if (verify_module_namespace(namespace, mod->name))
+		if (strstarts(namespace, MODULE_NS_PREFIX)) {
+			namespace += strlen(MODULE_NS_PREFIX);
+
+			if (!module_match(mod->name, namespace)) {
+				pr_err("module \"%s\" uses symbol \"%s\", which is exported only for module \"%s\"\n",
+				       mod->name, kernel_symbol_name(sym), namespace);
+				return -EINVAL;
+			}
 			return 0;
+		}
 
 		for_each_modinfo_entry(imported_namespace, info, "import_ns") {
 			if (strcmp(namespace, imported_namespace) == 0)
@@ -1743,7 +1746,7 @@ static int setup_modinfo(struct module *mod, struct load_info *info)
 		 * 'module:' prefixed namespaces are implicit, disallow
 		 * explicit imports.
 		 */
-		if (strstarts(imported_namespace, "module:")) {
+		if (strstarts(imported_namespace, MODULE_NS_PREFIX)) {
 			pr_err("%s: module tries to import module namespace: %s\n",
 			       mod->name, imported_namespace);
 			return -EPERM;
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 5ca7c268294e..3948a4bc41b3 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1690,28 +1690,21 @@ void buf_write(struct buffer *buf, const char *s, int len)
 }
 
 /**
- * verify_module_namespace() - does @modname have access to this symbol's @namespace
- * @namespace: export symbol namespace
+ * module_match() - check if @modname matches @patterns
  * @modname: module name
+ * @patterns: comma-separated list of module names
  *
- * If @namespace is prefixed with "module:" to indicate it is a module namespace
- * then test if @modname matches any of the comma separated patterns.
- *
- * The patterns only support tail-glob.
+ * The @patterns only supports tail-glob.
  */
-static bool verify_module_namespace(const char *namespace, const char *modname)
+static bool module_match(const char *modname, const char *patterns)
 {
 	size_t len, modlen = strlen(modname);
-	const char *prefix = "module:";
 	const char *sep;
 	bool glob;
 
-	if (!strstarts(namespace, prefix))
-		return false;
-
-	for (namespace += strlen(prefix); *namespace; namespace = sep) {
-		sep = strchrnul(namespace, ',');
-		len = sep - namespace;
+	for (; *patterns; patterns = sep) {
+		sep = strchrnul(patterns, ',');
+		len = sep - patterns;
 
 		glob = false;
 		if (sep[-1] == '*') {
@@ -1722,7 +1715,7 @@ static bool verify_module_namespace(const char *namespace, const char *modname)
 		if (*sep)
 			sep++;
 
-		if (strncmp(namespace, modname, len) == 0 && (glob || len == modlen))
+		if (strncmp(patterns, modname, len) == 0 && (glob || len == modlen))
 			return true;
 	}
 
@@ -1756,8 +1749,16 @@ static void check_exports(struct module *mod)
 
 		basename = get_basename(mod->name);
 
-		if (!verify_module_namespace(exp->namespace, basename) &&
-		    !contains_namespace(&mod->imported_namespaces, exp->namespace)) {
+		if (strstarts(exp->namespace, MODULE_NS_PREFIX)) {
+			const char *ns_patterns = exp->namespace +
+						strlen(MODULE_NS_PREFIX);
+
+			if (!module_match(basename, ns_patterns))
+			    error("module \"%s\" uses symbol \"%s\", which is exported only for module \"%s\"\n",
+				  basename, exp->name, ns_patterns);
+
+		} else if (!contains_namespace(&mod->imported_namespaces,
+					     exp->namespace)) {
 			modpost_log(!allow_missing_ns_imports,
 				    "module %s uses symbol %s from namespace %s, but does not import it.\n",
 				    basename, exp->name, exp->namespace);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 2/3] modpost: allow "make nsdeps" to skip module-specific symbol namespace
  2025-05-22  7:17 ` [PATCH 2/3] modpost: allow "make nsdeps" to skip module-specific symbol namespace Masahiro Yamada
@ 2025-05-27  7:55   ` Petr Pavlu
  0 siblings, 0 replies; 2+ messages in thread
From: Petr Pavlu @ 2025-05-27  7:55 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, linux-kernel, Daniel Gomez, Luis Chamberlain,
	Nathan Chancellor, Nicolas Schier, Peter Zijlstra, Sami Tolvanen,
	linux-modules

On 5/22/25 09:17, Masahiro Yamada wrote:
> When MODULE_IMPORT_NS() is missing, "make nsdeps" runs the Coccinelle
> script to automatically add MODULE_IMPORT_NS() to each module.
> 
> This should not occur for users of EXPORT_SYMBOL_GPL_FOR_MODULES(), which
> is intended to export a symbol to a specific module only. In such cases,
> explicitly adding MODULE_IMPORT_NS("module:...") is disallowed.
> 
> This commit handles the latter case separately in order not to trigger
> the Coccinelle, and displays the error message:
> 
>   ERROR: modpost: module "foo" uses symbol "bar", which is exported only for module "baz"
> 
> Apply the same logic for kernel space as well.
> 
> Fixes: 092a4f5985f2 ("module: Add module specific symbol namespace support")
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Looks ok to me.

Reviewed-by: Petr Pavlu <petr.pavlu@suse.com>

Does this patch make the following note about nsdeps in
Documentation/core-api/symbol-namespaces.rst (currently only in
linux-next) obsolete and can it now be removed?

"""
Note: it will happily generate an import statement for the module namespace;
which will not work and generates build and runtime failures.
"""

-- 
Thanks,
Petr

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-05-27  7:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20250522071744.2362563-1-masahiroy@kernel.org>
2025-05-22  7:17 ` [PATCH 2/3] modpost: allow "make nsdeps" to skip module-specific symbol namespace Masahiro Yamada
2025-05-27  7:55   ` Petr Pavlu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).