[PATCH bpf-next v3 2/9] module: Add module_kallsyms_find_name()
From: Samuel Wu
Date: Fri Nov 13 2026 - 17:14:53 EST
KALLSYMS_QUERY_BY_NAME, added later in this series, needs to find a
symbol name or prefix across module symbols in one pass.
module_get_kallsym() walks the module list from the head and runs an
export bsearch() for every symbol index.
Add module_kallsyms_find_name(), and put the shared query struct in
linux/kallsyms.h, so we have a more efficient linear walk.
Signed-off-by: Samuel Wu <wusamuel@xxxxxxxxxx>
---
include/linux/kallsyms.h | 7 +++++
include/linux/module.h | 13 +++++++++
kernel/module/kallsyms.c | 57 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 77 insertions(+)
diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h
index d5dd54c53ace..d31f2f436159 100644
--- a/include/linux/kallsyms.h
+++ b/include/linux/kallsyms.h
@@ -24,6 +24,13 @@
struct cred;
struct module;
+/* Exact or prefix name query, optionally in one module. */
+struct kallsyms_name_query {
+ const char *name;
+ size_t cmp_len; /* bytes to compare; includes NUL if exact */
+ const char *modname; /* NULL matches any module */
+};
+
static inline int is_kernel_text(unsigned long addr)
{
if (__is_kernel_text(addr))
diff --git a/include/linux/module.h b/include/linux/module.h
index 76f4f3596477..07d5a74ecfcc 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -42,6 +42,7 @@ struct modversion_info {
struct module;
struct exception_table_entry;
+struct kallsyms_name_query;
struct module_kobject {
struct kobject kobj;
@@ -978,6 +979,10 @@ int lookup_module_symbol_attrs(unsigned long addr,
*/
int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
char *name, char *module_name, int *exported);
+int module_kallsyms_find_name(unsigned int *symnum,
+ const struct kallsyms_name_query *q,
+ unsigned long *value, char *name,
+ char *module_name);
/* Look for this name: can be of form module:name. */
unsigned long module_kallsyms_lookup_name(const char *name);
@@ -1016,6 +1021,14 @@ static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
return -ERANGE;
}
+static inline int module_kallsyms_find_name(unsigned int *symnum,
+ const struct kallsyms_name_query *q,
+ unsigned long *value, char *name,
+ char *module_name)
+{
+ return -ENOENT;
+}
+
static inline unsigned long module_kallsyms_lookup_name(const char *name)
{
return 0;
diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
index 1ff8ce857d92..72006c9bad42 100644
--- a/kernel/module/kallsyms.c
+++ b/kernel/module/kallsyms.c
@@ -406,6 +406,63 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
return -ERANGE;
}
+/**
+ * module_kallsyms_find_name - Find a module symbol by name or prefix
+ * @symnum: in: index to start at; out: index of the match. The index
+ * counts the symbols of the searched modules in list order.
+ * @q: name query; if @q->modname is set, only that module is searched
+ * @value: set to the symbol address
+ * @name: set to the symbol name (KSYM_NAME_LEN bytes)
+ * @module_name: set to the module name (MODULE_NAME_LEN bytes)
+ *
+ * Undefined (SHN_UNDEF) symbols are skipped.
+ *
+ * Return: 0 on a match, or -ENOENT.
+ */
+int module_kallsyms_find_name(unsigned int *symnum,
+ const struct kallsyms_name_query *q,
+ unsigned long *value, char *name,
+ char *module_name)
+{
+ unsigned int start = *symnum;
+ unsigned int sym_base = 0;
+ struct module *mod;
+
+ guard(rcu)();
+ list_for_each_entry_rcu(mod, &modules, list) {
+ struct mod_kallsyms *kallsyms;
+ unsigned int i = 0;
+
+ if (mod->state == MODULE_STATE_UNFORMED)
+ continue;
+ if (q->modname && strcmp(q->modname, mod->name))
+ continue;
+
+ kallsyms = rcu_dereference(mod->kallsyms);
+ if (start > sym_base)
+ i = start - sym_base;
+ for (; i < kallsyms->num_symtab; i++) {
+ const Elf_Sym *sym = &kallsyms->symtab[i];
+ const char *symname = kallsyms_symbol_name(kallsyms, i);
+
+ if (sym->st_shndx == SHN_UNDEF)
+ continue;
+ if (strncmp(symname, q->name, q->cmp_len))
+ continue;
+
+ *value = kallsyms_symbol_value(sym);
+ strscpy(name, symname, KSYM_NAME_LEN);
+ strscpy(module_name, mod->name, MODULE_NAME_LEN);
+ *symnum = sym_base + i;
+ return 0;
+ }
+ if (q->modname)
+ break;
+ sym_base += kallsyms->num_symtab;
+ }
+ return -ENOENT;
+}
+
/* Given a module and name of symbol, find and return the symbol's value */
static unsigned long __find_kallsyms_symbol_value(struct module *mod, const char *name)
{
--
2.56.0.679.gc527c65306-goog