From: Lawrence Lin via B4 Relay <devnull+deduce.gmail.com@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Luis Chamberlain <mcgrof@kernel.org>,
Petr Pavlu <petr.pavlu@suse.com>,
Daniel Gomez <da.gomez@kernel.org>,
Sami Tolvanen <samitolvanen@google.com>
Cc: "Aaron Tomlin" <atomlin@atomlin.com>,
"Stanislaw Gruszka" <stf_xl@wp.pl>,
"David Laight" <david.laight.linux@gmail.com>,
"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-trace-kernel@vger.kernel.org,
"Lawrence Lin" <deduce@gmail.com>
Subject: [PATCH v2 1/2] module: Add module_kallsyms_on_each_addr()
Date: Sun, 04 Oct 2026 14:51:00 -0500 [thread overview]
Message-ID: <20261004-ftrace-mod-bsearch-v2-1-c1de73e72e73@gmail.com> (raw)
In-Reply-To: <20261004-ftrace-mod-bsearch-v2-0-c1de73e72e73@gmail.com>
From: Lawrence Lin <deduce@gmail.com>
ftrace needs the addresses of the symbols of a module while the module is
still being loaded: ftrace_module_init() runs before complete_formation(),
while the module is MODULE_STATE_UNFORMED, and
module_kallsyms_on_each_symbol() skips unformed modules. It also needs
exactly the symbols that find_kallsyms_symbol() may resolve an address
to, so that it agrees with kallsyms_lookup().
Factor the symbol filter of find_kallsyms_symbol() into is_lookup_symbol()
and add module_kallsyms_on_each_addr(), which calls a function with the
address of each such symbol of a given module. Like find_kallsyms_symbol(),
it reads mod->kallsyms under RCU, which add_kallsyms() has set up by then.
No functional change to find_kallsyms_symbol().
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Lawrence Lin <deduce@gmail.com>
---
include/linux/module.h | 10 ++++++++++
kernel/module/kallsyms.c | 44 +++++++++++++++++++++++++++++++++++---------
2 files changed, 45 insertions(+), 9 deletions(-)
diff --git a/include/linux/module.h b/include/linux/module.h
index 96cc98568eea..8b1c06d1118c 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -971,6 +971,10 @@ unsigned long module_kallsyms_lookup_name(const char *name);
unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
+void module_kallsyms_on_each_addr(struct module *mod,
+ void (*fn)(void *, unsigned long),
+ void *data);
+
#else /* CONFIG_MODULES && CONFIG_KALLSYMS */
static inline int module_kallsyms_on_each_symbol(const char *modname,
@@ -1014,6 +1018,12 @@ static inline unsigned long find_kallsyms_symbol_value(struct module *mod,
return 0;
}
+static inline void module_kallsyms_on_each_addr(struct module *mod,
+ void (*fn)(void *, unsigned long),
+ void *data)
+{
+}
+
#endif /* CONFIG_MODULES && CONFIG_KALLSYMS */
/* Define __free(module_put) macro for struct module *. */
diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
index f23126d804b2..bccee4b8294c 100644
--- a/kernel/module/kallsyms.c
+++ b/kernel/module/kallsyms.c
@@ -246,6 +246,18 @@ static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned
return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
}
+/*
+ * Whether find_kallsyms_symbol() may resolve an address to symbol @symnum.
+ * Unnamed symbols are ignored: they're uninformative and inserted at a whim.
+ */
+static bool is_lookup_symbol(struct mod_kallsyms *kallsyms, unsigned int symnum)
+{
+ const char *name = kallsyms_symbol_name(kallsyms, symnum);
+
+ return kallsyms->symtab[symnum].st_shndx != SHN_UNDEF &&
+ *name != '\0' && !is_mapping_symbol(name);
+}
+
/*
* Given a module and address, find the corresponding symbol and return its name
* while providing its size and offset if needed.
@@ -286,15 +298,7 @@ static const char *find_kallsyms_symbol(struct module *mod,
const Elf_Sym *sym = &kallsyms->symtab[i];
unsigned long thisval = kallsyms_symbol_value(sym);
- if (sym->st_shndx == SHN_UNDEF)
- continue;
-
- /*
- * We ignore unnamed symbols: they're uninformative
- * and inserted at a whim.
- */
- if (*kallsyms_symbol_name(kallsyms, i) == '\0' ||
- is_mapping_symbol(kallsyms_symbol_name(kallsyms, i)))
+ if (!is_lookup_symbol(kallsyms, i))
continue;
if (thisval <= addr && thisval > bestval) {
@@ -458,6 +462,28 @@ unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name)
return __find_kallsyms_symbol_value(mod, name);
}
+/*
+ * Call @fn with the address of each symbol of @mod that find_kallsyms_symbol()
+ * may resolve an address to. Unlike module_kallsyms_on_each_symbol(), this
+ * also works while @mod is still being loaded.
+ */
+void module_kallsyms_on_each_addr(struct module *mod,
+ void (*fn)(void *, unsigned long),
+ void *data)
+{
+ struct mod_kallsyms *kallsyms;
+ unsigned int i;
+
+ guard(rcu)();
+ kallsyms = rcu_dereference(mod->kallsyms);
+
+ /* ELF starts real symbols at 1. */
+ for (i = 1; i < kallsyms->num_symtab; i++) {
+ if (is_lookup_symbol(kallsyms, i))
+ fn(data, kallsyms_symbol_value(&kallsyms->symtab[i]));
+ }
+}
+
int module_kallsyms_on_each_symbol(const char *modname,
int (*fn)(void *, const char *, unsigned long),
void *data)
--
2.55.0
next prev parent reply other threads:[~2026-10-04 19:51 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-04 19:50 [PATCH v2 0/2] ftrace: Drop weak function locations of modules when loading them Lawrence Lin via B4 Relay
2026-10-04 19:51 ` Lawrence Lin via B4 Relay [this message]
2026-10-04 19:51 ` [PATCH v2 2/2] " Lawrence Lin via B4 Relay
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=20261004-ftrace-mod-bsearch-v2-1-c1de73e72e73@gmail.com \
--to=devnull+deduce.gmail.com@kernel.org \
--cc=atomlin@atomlin.com \
--cc=da.gomez@kernel.org \
--cc=david.laight.linux@gmail.com \
--cc=deduce@gmail.com \
--cc=kwilczynski@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mcgrof@kernel.org \
--cc=mhiramat@kernel.org \
--cc=petr.pavlu@suse.com \
--cc=rostedt@goodmis.org \
--cc=samitolvanen@google.com \
--cc=stf_xl@wp.pl \
/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
all inboxes | Powered by JetHome®