Add module_sect_addr() that returns the runtime address of the loaded module section with given name, or 0 if there's no such section or the section attributes are not available (they need CONFIG_KALLSYMS and CONFIG_SYSFS). The caller needs to make sure the module is live and hold a reference to it, which keeps the section attributes stable, so no other locking is needed. It will be used to resolve addresses of inlined instances in modules, which are stored in inline BTF as section relative offsets. Signed-off-by: Jiri Olsa --- include/linux/module.h | 9 +++++++++ kernel/module/sysfs.c | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/linux/module.h b/include/linux/module.h index 76f4f3596477..e2209a8953f9 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -952,6 +952,15 @@ static inline bool module_sig_ok(struct module *module) } #endif /* CONFIG_MODULE_SIG */ +#if defined(CONFIG_MODULES) && defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS) +unsigned long module_sect_addr(const struct module *mod, const char *name); +#else +static inline unsigned long module_sect_addr(const struct module *mod, const char *name) +{ + return 0; +} +#endif + #if defined(CONFIG_MODULES) && defined(CONFIG_KALLSYMS) int module_kallsyms_on_each_symbol(const char *modname, int (*fn)(void *, const char *, unsigned long), diff --git a/kernel/module/sysfs.c b/kernel/module/sysfs.c index 01c65d608873..f6a08fad503e 100644 --- a/kernel/module/sysfs.c +++ b/kernel/module/sysfs.c @@ -133,6 +133,26 @@ static void remove_sect_attrs(struct module *mod) } } +/* + * Returns address of the loaded section @name or 0 if there's no such + * section. The caller needs to make sure the module is live + * (MODULE_STATE_LIVE) and hold a reference to it, which keeps + * mod->sect_attrs stable; no other locking is needed. + */ +unsigned long module_sect_addr(const struct module *mod, const char *name) +{ + const struct bin_attribute *const *bin_attr; + + if (!mod->sect_attrs) + return 0; + + for (bin_attr = mod->sect_attrs->grp.bin_attrs; *bin_attr; bin_attr++) { + if (!strcmp((*bin_attr)->attr.name, name)) + return (unsigned long)(*bin_attr)->private; + } + return 0; +} + /* * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections. */ -- 2.56.1