From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
Luis Chamberlain <mcgrof@kernel.org>,
Petr Pavlu <petr.pavlu@suse.com>,
Sami Tolvanen <samitolvanen@google.com>,
Daniel Gomez <da.gomez@samsung.com>,
linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
linux-modules@vger.kernel.org
Subject: [PATCH v2 1/2] modules: Add __module_build_id() to find module by build_id
Date: Sat, 1 Feb 2025 16:23:11 +0900 [thread overview]
Message-ID: <173839459112.2009498.16498824811045520238.stgit@devnote2> (raw)
In-Reply-To: <173839458022.2009498.14495253908367838065.stgit@devnote2>
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Add __module_build_id() to find module by build_id. This also makes
module::build_id available with CONFIG_MODULE_BUILD_ID kconfig.
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
include/linux/module.h | 8 +++++++-
kernel/module/Kconfig | 3 +++
kernel/module/kallsyms.c | 4 ++--
kernel/module/main.c | 29 +++++++++++++++++++++++++++++
lib/Kconfig.debug | 1 +
5 files changed, 42 insertions(+), 3 deletions(-)
diff --git a/include/linux/module.h b/include/linux/module.h
index b3a643435357..e181056d6af6 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -416,7 +416,7 @@ struct module {
/* Unique handle for this module */
char name[MODULE_NAME_LEN];
-#ifdef CONFIG_STACKTRACE_BUILD_ID
+#ifdef CONFIG_MODULE_BUILD_ID
/* Module build ID */
unsigned char build_id[BUILD_ID_SIZE_MAX];
#endif
@@ -622,6 +622,7 @@ static inline bool module_is_coming(struct module *mod)
struct module *__module_text_address(unsigned long addr);
struct module *__module_address(unsigned long addr);
+struct module *__module_build_id(unsigned char *build_id, int size);
bool is_module_address(unsigned long addr);
bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
bool is_module_percpu_address(unsigned long addr);
@@ -791,6 +792,11 @@ static inline struct module *__module_text_address(unsigned long addr)
return NULL;
}
+static inline struct module *__module_build_id(unsigned char *build_id, int size)
+{
+ return NULL;
+}
+
static inline bool is_module_address(unsigned long addr)
{
return false;
diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig
index 7b329057997a..5e81dea1afea 100644
--- a/kernel/module/Kconfig
+++ b/kernel/module/Kconfig
@@ -26,6 +26,9 @@ if MODULES
config MODULE_DEBUGFS
bool
+config MODULE_BUILD_ID
+ bool
+
config MODULE_DEBUG
bool "Module debugging"
depends on DEBUG_FS
diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
index bf65e0c3c86f..98f2661c1da8 100644
--- a/kernel/module/kallsyms.c
+++ b/kernel/module/kallsyms.c
@@ -224,7 +224,7 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
mod->core_kallsyms.num_symtab = ndst;
}
-#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
+#if IS_ENABLED(CONFIG_MODULE_BUILD_ID)
void init_build_id(struct module *mod, const struct load_info *info)
{
const Elf_Shdr *sechdr;
@@ -338,7 +338,7 @@ int module_address_lookup(unsigned long addr,
if (modname)
*modname = mod->name;
if (modbuildid) {
-#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
+#if IS_ENABLED(CONFIG_MODULE_BUILD_ID)
*modbuildid = mod->build_id;
#else
*modbuildid = NULL;
diff --git a/kernel/module/main.c b/kernel/module/main.c
index 5399c182b3cb..fca9b6a692e3 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -3746,6 +3746,35 @@ struct module *__module_text_address(unsigned long addr)
return mod;
}
+/**
+ * __module_build_id() - get the module whose build_id start with an array.
+ * @build_id: the first part of the build_id.
+ * @size: the size of @build_id.
+ *
+ * Must be called with preempt disabled or module mutex held so that
+ * module doesn't get freed during this.
+ */
+struct module *__module_build_id(unsigned char *build_id, int size)
+{
+#ifdef CONFIG_MODULE_BUILD_ID
+ struct module *mod;
+
+ if (size < 0)
+ return NULL;
+
+ if (size > BUILD_ID_SIZE_MAX)
+ size = BUILD_ID_SIZE_MAX;
+
+ list_for_each_entry_rcu(mod, &modules, list) {
+ if (mod->state == MODULE_STATE_UNFORMED)
+ continue;
+ if (!memcmp(mod->build_id, build_id, size))
+ return mod;
+ }
+#endif
+ return NULL;
+}
+
/* Don't grab lock, we're oopsing. */
void print_modules(void)
{
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index cf2a41dc7682..2d3a2f656a86 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -38,6 +38,7 @@ config PRINTK_CALLER
config STACKTRACE_BUILD_ID
bool "Show build ID information in stacktraces"
depends on PRINTK
+ select MODULE_BUILD_ID if MODULES
help
Selecting this option adds build ID information for symbols in
stacktraces printed with the printk format '%p[SR]b'.
next prev parent reply other threads:[~2025-02-01 7:23 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-01 7:23 [PATCH v2 0/2] tracing: Introduce relative stacktrace Masami Hiramatsu (Google)
2025-02-01 7:23 ` Masami Hiramatsu (Google) [this message]
2025-02-01 7:23 ` [PATCH v2 2/2] tracing: Add relative-stacktrace option Masami Hiramatsu (Google)
2025-02-03 15:32 ` [PATCH v2 0/2] tracing: Introduce relative stacktrace Steven Rostedt
2025-02-05 12:25 ` Masami Hiramatsu
2025-02-05 13:28 ` Masami Hiramatsu
2025-02-05 14:53 ` Steven Rostedt
2025-02-05 22:52 ` Steven Rostedt
2025-02-06 0:28 ` Masami Hiramatsu
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=173839459112.2009498.16498824811045520238.stgit@devnote2 \
--to=mhiramat@kernel.org \
--cc=da.gomez@samsung.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=mcgrof@kernel.org \
--cc=petr.pavlu@suse.com \
--cc=rostedt@goodmis.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;
as well as URLs for NNTP newsgroup(s).