[PATCH 5/7] kallsyms: Add helper kallsyms_on_each_match_symbol()

From: Zhen Lei
Date: Thu Sep 08 2022 - 09:11:13 EST


Function kallsyms_on_each_symbol() traverses all symbols and submits each
symbol to the hook 'fn' for judgment and processing. For some cases, the
hook actually only handles the matched symbol, such as livepatch.

So that, we can first compress the name being looked up and then use
it for comparison when traversing 'kallsyms_names', this greatly reduces
the time consumed by traversing.

This requires CONFIG_KALLSYMS_ALL=y, so that scripts/kallsyms does not
compress that type character of each symbol.

If CONFIG_KALLSYMS_ALL=n, the traversal of symbols is rolled back to the
mode before optimization.

The pseudo code of the test case is as follows:
static int tst_find(void *data, const char *name,
struct module *mod, unsigned long addr)
{
if (strcmp(name, "vmap") == 0)
*(unsigned long *)data = addr;
return 0;
}

static int tst_match(void *data, unsigned long addr)
{
*(unsigned long *)data = addr;
return 0;
}

start = sched_clock();
kallsyms_on_each_match_symbol(tst_match, "vmap", &addr);
end = sched_clock();

start = sched_clock();
kallsyms_on_each_symbol(tst_find, &addr);
end = sched_clock();

The test results are as follows (twice):
kallsyms_on_each_match_symbol: 1058511, 1079288
kallsyms_on_each_symbol : 26097313, 24765180

kallsyms_on_each_match_symbol() consumes only 4.2% of
kallsyms_on_each_symbol()'s time.

Signed-off-by: Zhen Lei <thunder.leizhen@xxxxxxxxxx>
---
include/linux/kallsyms.h | 8 ++++++++
kernel/kallsyms.c | 41 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 49 insertions(+)

diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h
index ad39636e0c3f122..f9f2cc084cab16b 100644
--- a/include/linux/kallsyms.h
+++ b/include/linux/kallsyms.h
@@ -69,6 +69,8 @@ static inline void *dereference_symbol_descriptor(void *ptr)
int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
unsigned long),
void *data);
+extern int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
+ const char *name, void *data);

/* Lookup the address for a symbol. Returns 0 if not found. */
unsigned long kallsyms_lookup_name(const char *name);
@@ -168,6 +170,12 @@ static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct
{
return -EOPNOTSUPP;
}
+
+static inline int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
+ const char *name, void *data)
+{
+ return -EOPNOTSUPP;
+}
#endif /*CONFIG_KALLSYMS*/

static inline void print_ip_sym(const char *loglvl, unsigned long ip)
diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
index e1cd7305aa5f548..9816a0ac30c8c48 100644
--- a/kernel/kallsyms.c
+++ b/kernel/kallsyms.c
@@ -316,6 +316,47 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
return 0;
}

+int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
+ const char *name, void *data)
+{
+ unsigned int i, off;
+ int len, ret;
+ char namebuf[KSYM_NAME_LEN];
+
+ len = kallsyms_name_to_tokens(name, namebuf);
+ if (!len)
+ goto slow_path;
+
+ for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
+ if ((i & 0xfff) == 0)
+ cond_resched();
+
+ if ((kallsyms_names[off] == len + 1) &&
+ !memcmp(&kallsyms_names[off + 2], namebuf, len)) {
+ ret = fn(data, kallsyms_sym_address(i));
+ if (ret != 0)
+ return ret;
+ cond_resched();
+ }
+ off += kallsyms_names[off] + 1;
+ }
+
+ return 0;
+
+slow_path:
+ for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
+ off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
+ if (!strcmp(name, namebuf)) {
+ ret = fn(data, kallsyms_sym_address(i));
+ if (ret != 0)
+ return ret;
+ }
+ cond_resched();
+ }
+
+ return 0;
+}
+
static unsigned long get_symbol_pos(unsigned long addr,
unsigned long *symbolsize,
unsigned long *offset)
--
2.25.1