[PATCH v4 6/7] modpost: Add modname to mod_device_table alias

Alexey Gladkov posted 7 patches 3 months, 2 weeks ago
There is a newer version of this series
[PATCH v4 6/7] modpost: Add modname to mod_device_table alias
Posted by Alexey Gladkov 3 months, 2 weeks ago
At this point, if a symbol is compiled as part of the kernel,
information about which module the symbol belongs to is lost.

To save this it is possible to add the module name to the alias name.
It's not very pretty, but it's possible for now.

Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Alex Gaynor <alex.gaynor@gmail.com> 
Signed-off-by: Alexey Gladkov <legion@kernel.org>
---
 include/linux/module.h   | 14 +++++++++++---
 rust/kernel/device_id.rs |  8 ++++----
 scripts/mod/file2alias.c | 18 ++++++++++++++----
 3 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/include/linux/module.h b/include/linux/module.h
index 24fe6b865e9c..e0f826fab2ac 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -243,11 +243,19 @@ struct module_kobject *lookup_or_create_module_kobject(const char *name);
 /* What your module does. */
 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
 
+/* Format: __mod_device_table__kmod_<modname>__<type>__<name> */
+#define __mod_device_table(type, name)	\
+	__PASTE(__mod_device_table__,	\
+	__PASTE(__KBUILD_MODNAME,	\
+	__PASTE(__,			\
+	__PASTE(type,			\
+	__PASTE(__, name)))))
+
 #ifdef MODULE
 /* Creates an alias so file2alias.c can find device table. */
-#define MODULE_DEVICE_TABLE(type, name)					\
-extern typeof(name) __mod_device_table__##type##__##name		\
-  __attribute__ ((unused, alias(__stringify(name))))
+#define MODULE_DEVICE_TABLE(type, name)			\
+static typeof(name) __mod_device_table(type, name)	\
+  __attribute__ ((used, alias(__stringify(name))))
 #else  /* !MODULE */
 #define MODULE_DEVICE_TABLE(type, name)
 #endif
diff --git a/rust/kernel/device_id.rs b/rust/kernel/device_id.rs
index 0a4eb56d98f2..365d8f544844 100644
--- a/rust/kernel/device_id.rs
+++ b/rust/kernel/device_id.rs
@@ -154,10 +154,10 @@ macro_rules! module_device_table {
     ($table_type: literal, $module_table_name:ident, $table_name:ident) => {
         #[rustfmt::skip]
         #[export_name =
-            concat!("__mod_device_table__", $table_type,
-                    "__", module_path!(),
-                    "_", line!(),
-                    "_", stringify!($table_name))
+            concat!("__mod_device_table__", line!(),
+                    "__kmod_", module_path!(),
+                    "__", $table_type,
+                    "__", stringify!($table_name))
         ]
         static $module_table_name: [::core::mem::MaybeUninit<u8>; $table_name.raw_ids().size()] =
             unsafe { ::core::mem::transmute_copy($table_name.raw_ids()) };
diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
index 00586119a25b..13021266a18f 100644
--- a/scripts/mod/file2alias.c
+++ b/scripts/mod/file2alias.c
@@ -1476,8 +1476,8 @@ void handle_moddevtable(struct module *mod, struct elf_info *info,
 {
 	void *symval;
 	char *zeros = NULL;
-	const char *type, *name;
-	size_t typelen;
+	const char *type, *name, *modname;
+	size_t typelen, modnamelen;
 	static const char *prefix = "__mod_device_table__";
 
 	/* We're looking for a section relative symbol */
@@ -1488,10 +1488,20 @@ void handle_moddevtable(struct module *mod, struct elf_info *info,
 	if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
 		return;
 
-	/* All our symbols are of form __mod_device_table__<type>__<name>. */
+	/* All our symbols are of form __mod_device_table__kmod_<modname>__<type>__<name>. */
 	if (!strstarts(symname, prefix))
 		return;
-	type = symname + strlen(prefix);
+
+	modname = strstr(symname, "__kmod_");
+	if (!modname)
+		return;
+	modname += strlen("__kmod_");
+
+	type = strstr(modname, "__");
+	if (!type)
+		return;
+	modnamelen = type - modname;
+	type += strlen("__");
 
 	name = strstr(type, "__");
 	if (!name)
-- 
2.49.0
Re: [PATCH v4 6/7] modpost: Add modname to mod_device_table alias
Posted by Miguel Ojeda 3 months, 2 weeks ago
On Sat, Jun 21, 2025 at 3:57 PM Alexey Gladkov <legion@kernel.org> wrote:
>
> +/* Format: __mod_device_table__kmod_<modname>__<type>__<name> */

Should we mention that `__kmod_` and `__` will be the search strings,
or otherwise the Rust formatting (i.e. that is carries a line etc.)?

Cc'ing Tomo: do we need an update on `rust/kernel/net/phy.rs`? Should
we factor out the formatting?

Thanks!

Cheers,
Miguel
Re: [PATCH v4 6/7] modpost: Add modname to mod_device_table alias
Posted by FUJITA Tomonori 3 months, 2 weeks ago
On Sat, 21 Jun 2025 17:20:49 +0200
Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:

> On Sat, Jun 21, 2025 at 3:57 PM Alexey Gladkov <legion@kernel.org> wrote:
>>
>> +/* Format: __mod_device_table__kmod_<modname>__<type>__<name> */
> 
> Should we mention that `__kmod_` and `__` will be the search strings,
> or otherwise the Rust formatting (i.e. that is carries a line etc.)?
> 
> Cc'ing Tomo: do we need an update on `rust/kernel/net/phy.rs`? Should
> we factor out the formatting?

Yeah, looks like the update is necessary. Thanks for the heads-up.

I've just send a patchset to convert the PHY abstractions to use
device_id crate:

https://lore.kernel.org/lkml/20250623060951.118564-1-fujita.tomonori@gmail.com/
Re: [PATCH v4 6/7] modpost: Add modname to mod_device_table alias
Posted by Alexey Gladkov 3 months, 2 weeks ago
On Sat, Jun 21, 2025 at 05:20:49PM +0200, Miguel Ojeda wrote:
> On Sat, Jun 21, 2025 at 3:57 PM Alexey Gladkov <legion@kernel.org> wrote:
> >
> > +/* Format: __mod_device_table__kmod_<modname>__<type>__<name> */
> 
> Should we mention that `__kmod_` and `__` will be the search strings,
> or otherwise the Rust formatting (i.e. that is carries a line etc.)?

Ok. Make sense.

Basically there's a comment for the MODULE_DEVICE_TABLE macro that tells
where and how that symbol is parsed.

> Cc'ing Tomo: do we need an update on `rust/kernel/net/phy.rs`? Should
> we factor out the formatting?
> 
> Thanks!
> 
> Cheers,
> Miguel
> 

-- 
Rgrds, legion

Re: [PATCH v4 6/7] modpost: Add modname to mod_device_table alias
Posted by Miguel Ojeda 3 months, 2 weeks ago
On Sat, Jun 21, 2025 at 3:57 PM Alexey Gladkov <legion@kernel.org> wrote:
>
>  rust/kernel/device_id.rs |  8 ++++----

Cc'ing maintainers and list.

Cheers,
Miguel