selftests/bpf: trace_helpers.c: optimize kallsyms cache
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v11 1/2 selftests/bpf: trace_helpers.c: optimize kallsyms cache
From: Rong Tao <[email protected]>
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 e76a014334a6("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.
---
v11: Remove useless load_kallsyms_refresh() and modify code some format
v10: https://lore.kernel.org/lkml/[email protected]/
Keep the original load_kallsyms().
v9: https://lore.kernel.org/lkml/[email protected]/
Add load_kallsyms_local,ksym_search_local,ksym_get_addr_local functions.
v8: https://lore.kernel.org/lkml/[email protected]/
Resolves inter-thread contention for ksyms global variables.
v7: https://lore.kernel.org/lkml/[email protected]/
Fix __must_check macro.
v6: https://lore.kernel.org/lkml/[email protected]/
Apply libbpf_ensure_mem()
v5: https://lore.kernel.org/lkml/[email protected]/
Release the allocated memory once the load_kallsyms_refresh() upon error given it's dynamically allocated.
v4: https://lore.kernel.org/lkml/[email protected]/
Make sure most cases we don't need the realloc() path to begin with, and check strdup() return value.
v3: https://lore.kernel.org/lkml/[email protected]/
Do not use structs and judge ksyms__add_symbol function return value.
v2: https://lore.kernel.org/lkml/[email protected]/
Do the usual len/capacity scheme here to amortize the cost of realloc, and don't free symbols.
v1: https://lore.kernel.org/lkml/[email protected]/
--- samples/bpf/Makefile | 4 + .../selftests/bpf/prog_tests/fill_link_info.c | 2 +- .../prog_tests/kprobe_multi_testmod_test.c | 20 ++- tools/testing/selftests/bpf/trace_helpers.c | 132 +++++++++++++----- tools/testing/selftests/bpf/trace_helpers.h | 9 +- 5 files changed, 122 insertions(+), 45 deletions(-)
samples/bpf/Makefile +4 -0
@@ -175,6 +175,7 @@ TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/ TPROGS_CFLAGS += -I$(LIBBPF_INCLUDE) TPROGS_CFLAGS += -I$(srctree)/tools/include TPROGS_CFLAGS += -I$(srctree)/tools/perf +TPROGS_CFLAGS += -I$(srctree)/tools/lib TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0 ifdef SYSROOT
@@ -314,6 +315,9 @@ XDP_SAMPLE_CFLAGS += -Wall -O2 \ $(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS) $(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
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +1 -1
@@ -308,7 +308,7 @@ void test_fill_link_info(void)return ;/* load kallsyms to compare the addr */ -if (!ASSERT_OK (load_kallsyms_refresh (),"load_kallsyms_refresh" )) +if (!ASSERT_OK (load_kallsyms (),"load_kallsyms" )) goto cleanup; kprobe_addr =ksym_get_addr (KPROBE_FUNC );
tools/testing/selftests/bpf/prog_tests/kprobe_multi_testmod_test.c +13 -7
@@ -4,6 +4,8 @@#include "trace_helpers.h" #include "bpf/libbpf_internal.h" +static struct ksyms *ksyms; +static void kprobe_multi_testmod_check (struct kprobe_multi *skel) {ASSERT_EQ (skel->bss->kprobe_testmod_test1_result,1 ,"kprobe_test1_result" );
@@ -50,12 +52,12 @@ static void test_testmod_attach_api_addrs(void)LIBBPF_OPTS (bpf_kprobe_multi_opts, opts);unsigned long long addrs[3 ]; - addrs[0 ] =ksym_get_addr ("bpf_testmod_fentry_test1" ); -ASSERT_NEQ (addrs[0 ],0 ,"ksym_get_addr" ); - addrs[1 ] =ksym_get_addr ("bpf_testmod_fentry_test2" ); -ASSERT_NEQ (addrs[1 ],0 ,"ksym_get_addr" ); - addrs[2 ] =ksym_get_addr ("bpf_testmod_fentry_test3" ); -ASSERT_NEQ (addrs[2 ],0 ,"ksym_get_addr" ); + addrs[0 ] =ksym_get_addr_local (ksyms,"bpf_testmod_fentry_test1" ); +ASSERT_NEQ (addrs[0 ],0 ,"ksym_get_addr_local" ); + addrs[1 ] =ksym_get_addr_local (ksyms,"bpf_testmod_fentry_test2" ); +ASSERT_NEQ (addrs[1 ],0 ,"ksym_get_addr_local" ); + addrs[2 ] =ksym_get_addr_local (ksyms,"bpf_testmod_fentry_test3" ); +ASSERT_NEQ (addrs[2 ],0 ,"ksym_get_addr_local" ); opts.addrs = (const unsigned long *) addrs; opts.cnt =ARRAY_SIZE (addrs);
@@ -79,11 +81,15 @@ static void test_testmod_attach_api_syms(void)void serial_test_kprobe_multi_testmod_test (void ) { -if (!ASSERT_OK (load_kallsyms_refresh (),"load_kallsyms_refresh" )) + ksyms =load_kallsyms_local (NULL ); +if (!ASSERT_OK_PTR (ksyms,"load_kallsyms_local" ))return ;if (test__start_subtest ("testmod_attach_api_syms" ))test_testmod_attach_api_syms (); +if (test__start_subtest ("testmod_attach_api_addrs" ))test_testmod_attach_api_addrs (); + +free_kallsyms_local (ksyms); }
tools/testing/selftests/bpf/trace_helpers.c +97 -35
201 lines not on this page · raw message
tools/testing/selftests/bpf/trace_helpers.h +7 -2
@@ -11,13 +11,18 @@ struct ksym {long addr;char *name; }; +struct ksyms ;int load_kallsyms (void ); -int load_kallsyms_refresh (void ); -struct ksym *ksym_search (long key);long ksym_get_addr (const char *name); +struct ksyms *load_kallsyms_local (struct ksyms *ksyms); +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. */ int kallsyms_find (const char *sym,unsigned long long *addr);
Signature · 1 line
2.41.0
Quoted 16 lines · Rong Tao
From: Rong Tao <[email protected]>
Static ksyms often have problems because the number of symbols exceeds the
MAX_SYMSlimit. Like changing theMAX_SYMSfrom 300000 to 400000 in commite76a014334a6("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()
Quoted 5 lines
There are used to solve the problem of selftests/bpf updating kallsyms after attach new symbols during testmod testing.
Signed-off-by Rong Tao <[email protected]>
looks good, I added few more coments, with them addressed you can add my
thanks,
jirka
Quoted 24 lines
---
v11: Remove uselessload_kallsyms_refresh()and modify code some format
v10: https://lore.kernel.org/lkml/[email protected]/
Keep the originalload_kallsyms().
v9: https://lore.kernel.org/lkml/[email protected]/
Add load_kallsyms_local,ksym_search_local,ksym_get_addr_local functions.
v8: https://lore.kernel.org/lkml/[email protected]/
Resolves inter-thread contention for ksyms global variables.
v7: https://lore.kernel.org/lkml/[email protected]/
Fix__must_checkmacro.
v6: https://lore.kernel.org/lkml/[email protected]/
Applylibbpf_ensure_mem()
v5: https://lore.kernel.org/lkml/[email protected]/
Release the allocated memory once theload_kallsyms_refresh()upon error given it's dynamically allocated.
v4: https://lore.kernel.org/lkml/[email protected]/
Make sure most cases we don't need therealloc()path to begin with, and checkstrdup()return value.
v3: https://lore.kernel.org/lkml/[email protected]/
Do not use structs and judgeksyms__add_symbolfunction return value.
v2: https://lore.kernel.org/lkml/[email protected]/
Do the usual len/capacity scheme here to amortize the cost of realloc, and don't free symbols.
v1: https://lore.kernel.org/lkml/[email protected]/
SNIP
Quoted 4 lines
tools/testing/selftests/bpf/trace_helpers.c +4
-0+static int ksyms__add_symbol (struct ksyms *ksyms,const char *name, +unsigned long addr) +{ +void *tmp;
tools/testing/selftests/bpf/trace_helpers.c line 43
Quoted 10 lines
tools/testing/selftests/bpf/trace_helpers.c +10
-0+ + 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 ;
nit, extra new lines above
Quoted 28 lines
tools/testing/selftests/bpf/trace_helpers.c +20
-1+} + +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) {return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr; } -int load_kallsyms_refresh (void ) +struct ksyms *load_kallsyms_local (struct ksyms *ksyms) {
tools/testing/selftests/bpf/trace_helpers.c line 78
Quoted 10 lines
tools/testing/selftests/bpf/trace_helpers.c +3
-2FILE *f;char func[256 ], buf[256 ];char symbol;void *addr; -int i =0 ; +int ret; - sym_cnt =0 ; +/* flush kallsyms, free the previously allocated dynamic memory */ +free_kallsyms_local (ksyms);
with the removal of the refresh function (from last version) there's no need now for ksyms argument in load_kallsyms_local
all the current users of load_kallsyms_local are passing NULL arg
tools/testing/selftests/bpf/trace_helpers.c line 85
Quoted 8 lines
tools/testing/selftests/bpf/trace_helpers.c +4
-1f =fopen ("/proc/kallsyms" ,"r" );if (!f) -return -ENOENT ; +return NULL ; + + ksyms =calloc (1 ,sizeof (struct ksyms )); +if (!ksyms)
missing fclose(f);
Quoted 20 lines
tools/testing/selftests/bpf/trace_helpers.c +8
-5+return NULL ;while (fgets (buf,sizeof (buf), f)) {if (sscanf (buf,"%p %c %s" , &addr, &symbol, func) !=3 )break ;if (!addr)continue ; -if (i >=MAX_SYMS ) -return -EFBIG ; - syms[i].addr = (long ) addr; - syms[i].name =strdup (func); - i++; + ret =libbpf_ensure_mem ((void **) &ksyms->syms, &ksyms->sym_cap, +sizeof (struct ksym ), ksyms->sym_cnt +1 ); +if (ret) + goto error; + ret =ksyms__add_symbol (ksyms, func, (unsigned long )addr); +if (ret) + goto error;
tools/testing/selftests/bpf/trace_helpers.c line 107
Quoted 10 lines
tools/testing/selftests/bpf/trace_helpers.c +5
-3}fclose (f); - sym_cnt = i; -qsort (syms, sym_cnt,sizeof (struct ksym ), ksym_cmp); -return 0 ; +qsort (ksyms->syms, ksyms->sym_cnt,sizeof (struct ksym ), ksym_cmp); +return ksyms; + +error: +free_kallsyms_local (ksyms);
missing fclose(f);
Quoted 16 lines
tools/testing/selftests/bpf/trace_helpers.c +4
-7+return NULL ; }int load_kallsyms (void ) { -/* -* This is called/used from multiplace places, -* load symbols just once. -*/ -if (sym_cnt) -return 0 ; -return load_kallsyms_refresh (); +if (!ksyms) + ksyms =load_kallsyms_local (NULL ); +return ksyms ?0 :1 ; }
tools/testing/selftests/bpf/trace_helpers.c line 124
Quoted 10 lines
tools/testing/selftests/bpf/trace_helpers.c +4
-2-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; +if (!ksyms) +return NULL ;
I don't think we need to check !ksyms in here, you don't do that check in ksym_get_addr_local and I think it's fine
Quoted 7 lines
tools/testing/selftests/bpf/trace_helpers.c +2
-1+/* kallsyms not loaded. return NULL */ -if (sym_cnt <=0 ) +if (ksyms->sym_cnt <=0 )return NULL ;while (start < end) {
SNIP
Quoted 10 lines
tools/testing/selftests/bpf/trace_helpers.h +1
-0@@ -11,13 +11,18 @@ struct ksym {long addr;char *name; }; +struct ksyms ;
tools/testing/selftests/bpf/trace_helpers.h struct ksym { · line 23
Quoted 10 lines
tools/testing/selftests/bpf/trace_helpers.h +4
-2int load_kallsyms (void ); -int load_kallsyms_refresh (void ); -struct ksym *ksym_search (long key);long ksym_get_addr (const char *name); +struct ksyms *load_kallsyms_local (struct ksyms *ksyms); +struct ksym *ksym_search_local (struct ksyms *ksyms,long key); +long ksym_get_addr_local (struct ksyms *ksyms,const char *name); +
nit, extra newline
Quoted 8 lines
tools/testing/selftests/bpf/trace_helpers.h +2
-0+void free_kallsyms_local (struct ksyms *ksyms); +/* open kallsyms and find addresses on the fly, faster than load + search. */ int kallsyms_find (const char *sym,unsigned long long *addr);Signature · 1 line
2.41.0
PATCH v11 2/2 selftests/bpf: trace_helpers.c: Add a global ksyms initialization mutex
In reply to a message not in the archiveFrom: Rong Tao <[email protected]>
As Jirka said [0], we just need to make sure that global ksyms initialization won't race.
[0] https://lore.kernel.org/lkml/ZPCbAs3ItjRd8XVh@krava/
--- tools/testing/selftests/bpf/trace_helpers.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/testing/selftests/bpf/trace_helpers.c +4 -0
@@ -7,6 +7,7 @@#include <errno.h> #include <fcntl.h> #include <poll.h> +#include <pthread.h> #include <unistd.h> #include <linux/perf_event.h> #include <sys/mman.h>
@@ -26,6 +27,7 @@ struct ksyms { };static struct ksyms *ksyms; +static pthread_mutex_t ksyms_mutex =PTHREAD_MUTEX_INITIALIZER ;static int ksyms__add_symbol (struct ksyms *ksyms,const char *name,unsigned long addr)
@@ -110,8 +112,10 @@ struct ksyms *load_kallsyms_local(struct ksyms *ksyms)int load_kallsyms (void ) { +pthread_mutex_lock (&ksyms_mutex);if (!ksyms) ksyms =load_kallsyms_local (NULL ); +pthread_mutex_unlock (&ksyms_mutex);return ksyms ?0 :1 ; }
Signature · 1 line
2.41.0