[PATCH v4 7/8] livepatch: Improve the search performance of module_kallsyms_on_each_symbol()

From: Zhen Lei
Date: Tue Sep 20 2022 - 03:15:00 EST


Currently we traverse all symbols of all modules to find the specified
function for the specified module. But in reality, we just need to find
the given module and then traverse all the symbols in it.

In order to achieve this purpose, split the call to hook 'fn' into two
phases:
1. Finds the given module. Pass pointer 'mod'. Hook 'fn' directly returns
the comparison result of the module name without comparing the function
name.
2. Finds the given function in that module. Pass pointer 'mod = NULL'.
Hook 'fn' skip the comparison of module name and directly compare
function names.

Phase1: mod1-->mod2..(subsequent modules do not need to be compared)
|
Phase2: -->f1-->f2-->f3

Signed-off-by: Zhen Lei <thunder.leizhen@xxxxxxxxxx>
---
kernel/livepatch/core.c | 7 ++-----
kernel/module/kallsyms.c | 13 ++++++++++++-
2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 31b57ccf908017e..98e23137e4133bc 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -130,15 +130,12 @@ static int klp_find_callback(void *data, const char *name,
{
struct klp_find_arg *args = data;

- if ((mod && !args->objname) || (!mod && args->objname))
- return 0;
+ if (mod)
+ return strcmp(args->objname, mod->name);

if (strcmp(args->name, name))
return 0;

- if (args->objname && strcmp(args->objname, mod->name))
- return 0;
-
args->addr = addr;
args->count++;

diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
index f5c5c9175333df7..b033613e6c7e3bb 100644
--- a/kernel/module/kallsyms.c
+++ b/kernel/module/kallsyms.c
@@ -510,6 +510,11 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
if (mod->state == MODULE_STATE_UNFORMED)
continue;

+ /* check mod->name first */
+ ret = fn(data, NULL, mod, 0);
+ if (ret)
+ continue;
+
/* Use rcu_dereference_sched() to remain compliant with the sparse tool */
preempt_disable();
kallsyms = rcu_dereference_sched(mod->kallsyms);
@@ -522,10 +527,16 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
continue;

ret = fn(data, kallsyms_symbol_name(kallsyms, i),
- mod, kallsyms_symbol_value(sym));
+ NULL, kallsyms_symbol_value(sym));
if (ret != 0)
goto out;
}
+
+ /*
+ * The given module is found, the subsequent modules do not
+ * need to be compared.
+ */
+ break;
}
out:
mutex_unlock(&module_mutex);
--
2.25.1