The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Alexey Gladkov <legion@kernel.org>
To: Masahiro Yamada <masahiroy@kernel.org>,
	Petr Pavlu <petr.pavlu@suse.com>,
	Luis Chamberlain <mcgrof@kernel.org>,
	Sami Tolvanen <samitolvanen@google.com>,
	Daniel Gomez <da.gomez@samsung.com>,
	Nathan Chancellor <nathan@kernel.org>,
	Nicolas Schier <nicolas.schier@linux.dev>
Cc: linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org,
	linux-kbuild@vger.kernel.org, Alexey Gladkov <legion@kernel.org>,
	Miguel Ojeda <ojeda@kernel.org>,
	Alex Gaynor <alex.gaynor@gmail.com>,
	rust-for-linux@vger.kernel.org
Subject: [PATCH v5 07/10] modpost: Add modname to mod_device_table alias
Date: Thu, 24 Jul 2025 15:49:44 +0200	[thread overview]
Message-ID: <e78d20afcc059cec3cd977ee74a0ec4df06de05b.1753354215.git.legion@kernel.org> (raw)
In-Reply-To: <cover.1753354215.git.legion@kernel.org>

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>
Cc: rust-for-linux@vger.kernel.org
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, 31 insertions(+), 9 deletions(-)

diff --git a/include/linux/module.h b/include/linux/module.h
index 4689852d7b18..32e98200b2f4 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -243,10 +243,22 @@ 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>
+ * Parts of the string `__kmod_` and `__` are used as delimiters when parsing
+ * a symbol in file2alias.c
+ */
+#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)					\
-static typeof(name) __mod_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)
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.50.1


  parent reply	other threads:[~2025-07-24 13:50 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-24 13:49 [PATCH v5 00/10] Add generated modalias to modules.builtin.modinfo Alexey Gladkov
2025-07-24 13:49 ` [PATCH v5 01/10] module: remove meaningless 'name' parameter from __MODULE_INFO() Alexey Gladkov
2025-07-24 13:49 ` [PATCH v5 02/10] kbuild: always create intermediate vmlinux.unstripped Alexey Gladkov
2025-07-24 13:49 ` [PATCH v5 03/10] kbuild: keep .modinfo section in vmlinux.unstripped Alexey Gladkov
2025-07-28 13:57   ` Alexander Gordeev
2025-07-28 15:15     ` Alexey Gladkov
2025-07-29 13:54     ` Masahiro Yamada
2025-07-24 13:49 ` [PATCH v5 04/10] kbuild: extract modules.builtin.modinfo from vmlinux.unstripped Alexey Gladkov
2025-07-24 13:49 ` [PATCH v5 05/10] scsi: Always define blogic_pci_tbl structure Alexey Gladkov
2025-07-24 13:49 ` [PATCH v5 06/10] pinctrl: meson: Fix typo in device table macro Alexey Gladkov
2025-07-24 14:09   ` Neil Armstrong
2025-07-24 13:49 ` Alexey Gladkov [this message]
2025-07-24 16:00   ` [PATCH v5 07/10] modpost: Add modname to mod_device_table alias Miguel Ojeda
2025-07-24 13:49 ` [PATCH v5 08/10] modpost: Create modalias for builtin modules Alexey Gladkov
2025-07-24 13:49 ` [PATCH v5 09/10] kbuild: vmlinux.unstripped should always depend on .vmlinux.export.o Alexey Gladkov
2025-07-24 13:49 ` [PATCH v5 10/10] s390: vmlinux.lds.S: Reorder sections Alexey Gladkov
2025-07-26 10:59   ` Masahiro Yamada
2025-07-28 14:01     ` Heiko Carstens
2025-07-28 15:18       ` Alexey Gladkov
2025-07-29 12:46         ` Masahiro Yamada

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e78d20afcc059cec3cd977ee74a0ec4df06de05b.1753354215.git.legion@kernel.org \
    --to=legion@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=da.gomez@samsung.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=nathan@kernel.org \
    --cc=nicolas.schier@linux.dev \
    --cc=ojeda@kernel.org \
    --cc=petr.pavlu@suse.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=samitolvanen@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox