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_query_name(), and put the shared query struct in linux/kallsyms.h, so we have a more efficient linear walk. Reviewed-by: Petr Pavlu Signed-off-by: Samuel Wu --- include/linux/kallsyms.h | 10 +++++++ include/linux/module.h | 13 +++++++++ kernel/module/kallsyms.c | 57 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h index d5dd54c53ace..58958e4ef182 100644 --- a/include/linux/kallsyms.h +++ b/include/linux/kallsyms.h @@ -24,6 +24,16 @@ struct cred; struct module; +/* + * Exact or prefix name query, optionally filtered to one module. + * Callers select "vmlinux" separately instead of passing it in modname. + */ +struct kallsyms_name_query { + const char *name; + size_t cmp_len; /* bytes to compare; includes NUL if exact */ + const char *modname; /* module name, or NULL for 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..b5ea9698cea9 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_query_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_query_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..3f0edbce43be 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_query_name - Query 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_query_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.1.911.g883d8bcb20-goog