Commit c698eaeb authored by Rong Tao's avatar Rong Tao Committed by Andrii Nakryiko

selftests/bpf: trace_helpers.c: Optimize kallsyms cache

Static ksyms often have problems because the number of symbols exceeds the
MAX_SYMS limit. Like changing the MAX_SYMS from 300000 to 400000 in
commit e76a0143("selftests/bpf: Bump and validate MAX_SYMS") solves
the problem somewhat, but it's not the perfect way.

This commit uses dynamic memory allocation, which completely solves the
problem caused by the limitation of the number of kallsyms. At the same
time, add APIs:

    load_kallsyms_local()
    ksym_search_local()
    ksym_get_addr_local()
    free_kallsyms_local()

There are used to solve the problem of selftests/bpf updating kallsyms
after attach new symbols during testmod testing.
Signed-off-by: default avatarRong Tao <rongtao@cestc.cn>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
Acked-by: default avatarStanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/bpf/tencent_C9BDA68F9221F21BE4081566A55D66A9700A@qq.com
parent 9bc86925
...@@ -175,6 +175,7 @@ TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/ ...@@ -175,6 +175,7 @@ TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
TPROGS_CFLAGS += -I$(LIBBPF_INCLUDE) TPROGS_CFLAGS += -I$(LIBBPF_INCLUDE)
TPROGS_CFLAGS += -I$(srctree)/tools/include TPROGS_CFLAGS += -I$(srctree)/tools/include
TPROGS_CFLAGS += -I$(srctree)/tools/perf TPROGS_CFLAGS += -I$(srctree)/tools/perf
TPROGS_CFLAGS += -I$(srctree)/tools/lib
TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0 TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0
ifdef SYSROOT ifdef SYSROOT
...@@ -314,6 +315,9 @@ XDP_SAMPLE_CFLAGS += -Wall -O2 \ ...@@ -314,6 +315,9 @@ XDP_SAMPLE_CFLAGS += -Wall -O2 \
$(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS) $(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS)
$(obj)/$(XDP_SAMPLE): $(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h $(obj)/$(XDP_SAMPLE): $(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h
# Override includes for trace_helpers.o because __must_check won't be defined
# in our include path.
$(obj)/$(TRACE_HELPERS): TPROGS_CFLAGS := $(TPROGS_CFLAGS) -D__must_check=
-include $(BPF_SAMPLES_PATH)/Makefile.target -include $(BPF_SAMPLES_PATH)/Makefile.target
......
...@@ -308,7 +308,7 @@ void test_fill_link_info(void) ...@@ -308,7 +308,7 @@ void test_fill_link_info(void)
return; return;
/* load kallsyms to compare the addr */ /* load kallsyms to compare the addr */
if (!ASSERT_OK(load_kallsyms_refresh(), "load_kallsyms_refresh")) if (!ASSERT_OK(load_kallsyms(), "load_kallsyms"))
goto cleanup; goto cleanup;
kprobe_addr = ksym_get_addr(KPROBE_FUNC); kprobe_addr = ksym_get_addr(KPROBE_FUNC);
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "trace_helpers.h" #include "trace_helpers.h"
#include "bpf/libbpf_internal.h" #include "bpf/libbpf_internal.h"
static struct ksyms *ksyms;
static void kprobe_multi_testmod_check(struct kprobe_multi *skel) static void kprobe_multi_testmod_check(struct kprobe_multi *skel)
{ {
ASSERT_EQ(skel->bss->kprobe_testmod_test1_result, 1, "kprobe_test1_result"); ASSERT_EQ(skel->bss->kprobe_testmod_test1_result, 1, "kprobe_test1_result");
...@@ -50,12 +52,12 @@ static void test_testmod_attach_api_addrs(void) ...@@ -50,12 +52,12 @@ static void test_testmod_attach_api_addrs(void)
LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
unsigned long long addrs[3]; unsigned long long addrs[3];
addrs[0] = ksym_get_addr("bpf_testmod_fentry_test1"); addrs[0] = ksym_get_addr_local(ksyms, "bpf_testmod_fentry_test1");
ASSERT_NEQ(addrs[0], 0, "ksym_get_addr"); ASSERT_NEQ(addrs[0], 0, "ksym_get_addr_local");
addrs[1] = ksym_get_addr("bpf_testmod_fentry_test2"); addrs[1] = ksym_get_addr_local(ksyms, "bpf_testmod_fentry_test2");
ASSERT_NEQ(addrs[1], 0, "ksym_get_addr"); ASSERT_NEQ(addrs[1], 0, "ksym_get_addr_local");
addrs[2] = ksym_get_addr("bpf_testmod_fentry_test3"); addrs[2] = ksym_get_addr_local(ksyms, "bpf_testmod_fentry_test3");
ASSERT_NEQ(addrs[2], 0, "ksym_get_addr"); ASSERT_NEQ(addrs[2], 0, "ksym_get_addr_local");
opts.addrs = (const unsigned long *) addrs; opts.addrs = (const unsigned long *) addrs;
opts.cnt = ARRAY_SIZE(addrs); opts.cnt = ARRAY_SIZE(addrs);
...@@ -79,11 +81,15 @@ static void test_testmod_attach_api_syms(void) ...@@ -79,11 +81,15 @@ static void test_testmod_attach_api_syms(void)
void serial_test_kprobe_multi_testmod_test(void) void serial_test_kprobe_multi_testmod_test(void)
{ {
if (!ASSERT_OK(load_kallsyms_refresh(), "load_kallsyms_refresh")) ksyms = load_kallsyms_local();
if (!ASSERT_OK_PTR(ksyms, "load_kallsyms_local"))
return; return;
if (test__start_subtest("testmod_attach_api_syms")) if (test__start_subtest("testmod_attach_api_syms"))
test_testmod_attach_api_syms(); test_testmod_attach_api_syms();
if (test__start_subtest("testmod_attach_api_addrs")) if (test__start_subtest("testmod_attach_api_addrs"))
test_testmod_attach_api_addrs(); test_testmod_attach_api_addrs();
free_kallsyms_local(ksyms);
} }
...@@ -14,104 +14,162 @@ ...@@ -14,104 +14,162 @@
#include <linux/limits.h> #include <linux/limits.h>
#include <libelf.h> #include <libelf.h>
#include <gelf.h> #include <gelf.h>
#include "bpf/libbpf_internal.h"
#define TRACEFS_PIPE "/sys/kernel/tracing/trace_pipe" #define TRACEFS_PIPE "/sys/kernel/tracing/trace_pipe"
#define DEBUGFS_PIPE "/sys/kernel/debug/tracing/trace_pipe" #define DEBUGFS_PIPE "/sys/kernel/debug/tracing/trace_pipe"
#define MAX_SYMS 400000 struct ksyms {
static struct ksym syms[MAX_SYMS]; struct ksym *syms;
static int sym_cnt; size_t sym_cap;
size_t sym_cnt;
};
static struct ksyms *ksyms;
static int ksyms__add_symbol(struct ksyms *ksyms, const char *name,
unsigned long addr)
{
void *tmp;
tmp = strdup(name);
if (!tmp)
return -ENOMEM;
ksyms->syms[ksyms->sym_cnt].addr = addr;
ksyms->syms[ksyms->sym_cnt].name = tmp;
ksyms->sym_cnt++;
return 0;
}
void free_kallsyms_local(struct ksyms *ksyms)
{
unsigned int i;
if (!ksyms)
return;
if (!ksyms->syms) {
free(ksyms);
return;
}
for (i = 0; i < ksyms->sym_cnt; i++)
free(ksyms->syms[i].name);
free(ksyms->syms);
free(ksyms);
}
static int ksym_cmp(const void *p1, const void *p2) static int ksym_cmp(const void *p1, const void *p2)
{ {
return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr; return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr;
} }
int load_kallsyms_refresh(void) struct ksyms *load_kallsyms_local(void)
{ {
FILE *f; FILE *f;
char func[256], buf[256]; char func[256], buf[256];
char symbol; char symbol;
void *addr; void *addr;
int i = 0; int ret;
struct ksyms *ksyms;
sym_cnt = 0;
f = fopen("/proc/kallsyms", "r"); f = fopen("/proc/kallsyms", "r");
if (!f) if (!f)
return -ENOENT; return NULL;
ksyms = calloc(1, sizeof(struct ksyms));
if (!ksyms) {
fclose(f);
return NULL;
}
while (fgets(buf, sizeof(buf), f)) { while (fgets(buf, sizeof(buf), f)) {
if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3) if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3)
break; break;
if (!addr) if (!addr)
continue; continue;
if (i >= MAX_SYMS)
return -EFBIG;
syms[i].addr = (long) addr; ret = libbpf_ensure_mem((void **) &ksyms->syms, &ksyms->sym_cap,
syms[i].name = strdup(func); sizeof(struct ksym), ksyms->sym_cnt + 1);
i++; if (ret)
goto error;
ret = ksyms__add_symbol(ksyms, func, (unsigned long)addr);
if (ret)
goto error;
} }
fclose(f); fclose(f);
sym_cnt = i; qsort(ksyms->syms, ksyms->sym_cnt, sizeof(struct ksym), ksym_cmp);
qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp); return ksyms;
return 0;
error:
fclose(f);
free_kallsyms_local(ksyms);
return NULL;
} }
int load_kallsyms(void) int load_kallsyms(void)
{ {
/* if (!ksyms)
* This is called/used from multiplace places, ksyms = load_kallsyms_local();
* load symbols just once. return ksyms ? 0 : 1;
*/
if (sym_cnt)
return 0;
return load_kallsyms_refresh();
} }
struct ksym *ksym_search(long key) struct ksym *ksym_search_local(struct ksyms *ksyms, long key)
{ {
int start = 0, end = sym_cnt; int start = 0, end = ksyms->sym_cnt;
int result; int result;
/* kallsyms not loaded. return NULL */ /* kallsyms not loaded. return NULL */
if (sym_cnt <= 0) if (ksyms->sym_cnt <= 0)
return NULL; return NULL;
while (start < end) { while (start < end) {
size_t mid = start + (end - start) / 2; size_t mid = start + (end - start) / 2;
result = key - syms[mid].addr; result = key - ksyms->syms[mid].addr;
if (result < 0) if (result < 0)
end = mid; end = mid;
else if (result > 0) else if (result > 0)
start = mid + 1; start = mid + 1;
else else
return &syms[mid]; return &ksyms->syms[mid];
} }
if (start >= 1 && syms[start - 1].addr < key && if (start >= 1 && ksyms->syms[start - 1].addr < key &&
key < syms[start].addr) key < ksyms->syms[start].addr)
/* valid ksym */ /* valid ksym */
return &syms[start - 1]; return &ksyms->syms[start - 1];
/* out of range. return _stext */ /* out of range. return _stext */
return &syms[0]; return &ksyms->syms[0];
} }
long ksym_get_addr(const char *name) struct ksym *ksym_search(long key)
{
if (!ksyms)
return NULL;
return ksym_search_local(ksyms, key);
}
long ksym_get_addr_local(struct ksyms *ksyms, const char *name)
{ {
int i; int i;
for (i = 0; i < sym_cnt; i++) { for (i = 0; i < ksyms->sym_cnt; i++) {
if (strcmp(syms[i].name, name) == 0) if (strcmp(ksyms->syms[i].name, name) == 0)
return syms[i].addr; return ksyms->syms[i].addr;
} }
return 0; return 0;
} }
long ksym_get_addr(const char *name)
{
if (!ksyms)
return 0;
return ksym_get_addr_local(ksyms, name);
}
/* open kallsyms and read symbol addresses on the fly. Without caching all symbols, /* open kallsyms and read symbol addresses on the fly. Without caching all symbols,
* this is faster than load + find. * this is faster than load + find.
*/ */
......
...@@ -11,13 +11,17 @@ struct ksym { ...@@ -11,13 +11,17 @@ struct ksym {
long addr; long addr;
char *name; char *name;
}; };
struct ksyms;
int load_kallsyms(void); int load_kallsyms(void);
int load_kallsyms_refresh(void);
struct ksym *ksym_search(long key); struct ksym *ksym_search(long key);
long ksym_get_addr(const char *name); long ksym_get_addr(const char *name);
struct ksyms *load_kallsyms_local(void);
struct ksym *ksym_search_local(struct ksyms *ksyms, long key);
long ksym_get_addr_local(struct ksyms *ksyms, const char *name);
void free_kallsyms_local(struct ksyms *ksyms);
/* open kallsyms and find addresses on the fly, faster than load + search. */ /* open kallsyms and find addresses on the fly, faster than load + search. */
int kallsyms_find(const char *sym, unsigned long long *addr); int kallsyms_find(const char *sym, unsigned long long *addr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment