Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 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.

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Stanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/bpf/tencent_C9BDA68F9221F21BE4081566A55D66A9700A@qq.com

authored by

Rong Tao and committed by
Andrii Nakryiko
c698eaeb 9bc86925

+122 -50
+4
samples/bpf/Makefile
··· 175 175 TPROGS_CFLAGS += -I$(LIBBPF_INCLUDE) 176 176 TPROGS_CFLAGS += -I$(srctree)/tools/include 177 177 TPROGS_CFLAGS += -I$(srctree)/tools/perf 178 + TPROGS_CFLAGS += -I$(srctree)/tools/lib 178 179 TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0 179 180 180 181 ifdef SYSROOT ··· 315 314 316 315 $(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS) 317 316 $(obj)/$(XDP_SAMPLE): $(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h 317 + # Override includes for trace_helpers.o because __must_check won't be defined 318 + # in our include path. 319 + $(obj)/$(TRACE_HELPERS): TPROGS_CFLAGS := $(TPROGS_CFLAGS) -D__must_check= 318 320 319 321 -include $(BPF_SAMPLES_PATH)/Makefile.target 320 322
+13 -7
tools/testing/selftests/bpf/prog_tests/kprobe_multi_testmod_test.c
··· 4 4 #include "trace_helpers.h" 5 5 #include "bpf/libbpf_internal.h" 6 6 7 + static struct ksyms *ksyms; 8 + 7 9 static void kprobe_multi_testmod_check(struct kprobe_multi *skel) 8 10 { 9 11 ASSERT_EQ(skel->bss->kprobe_testmod_test1_result, 1, "kprobe_test1_result"); ··· 52 50 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); 53 51 unsigned long long addrs[3]; 54 52 55 - addrs[0] = ksym_get_addr("bpf_testmod_fentry_test1"); 56 - ASSERT_NEQ(addrs[0], 0, "ksym_get_addr"); 57 - addrs[1] = ksym_get_addr("bpf_testmod_fentry_test2"); 58 - ASSERT_NEQ(addrs[1], 0, "ksym_get_addr"); 59 - addrs[2] = ksym_get_addr("bpf_testmod_fentry_test3"); 60 - ASSERT_NEQ(addrs[2], 0, "ksym_get_addr"); 53 + addrs[0] = ksym_get_addr_local(ksyms, "bpf_testmod_fentry_test1"); 54 + ASSERT_NEQ(addrs[0], 0, "ksym_get_addr_local"); 55 + addrs[1] = ksym_get_addr_local(ksyms, "bpf_testmod_fentry_test2"); 56 + ASSERT_NEQ(addrs[1], 0, "ksym_get_addr_local"); 57 + addrs[2] = ksym_get_addr_local(ksyms, "bpf_testmod_fentry_test3"); 58 + ASSERT_NEQ(addrs[2], 0, "ksym_get_addr_local"); 61 59 62 60 opts.addrs = (const unsigned long *) addrs; 63 61 opts.cnt = ARRAY_SIZE(addrs); ··· 81 79 82 80 void serial_test_kprobe_multi_testmod_test(void) 83 81 { 84 - if (!ASSERT_OK(load_kallsyms_refresh(), "load_kallsyms_refresh")) 82 + ksyms = load_kallsyms_local(); 83 + if (!ASSERT_OK_PTR(ksyms, "load_kallsyms_local")) 85 84 return; 86 85 87 86 if (test__start_subtest("testmod_attach_api_syms")) 88 87 test_testmod_attach_api_syms(); 88 + 89 89 if (test__start_subtest("testmod_attach_api_addrs")) 90 90 test_testmod_attach_api_addrs(); 91 + 92 + free_kallsyms_local(ksyms); 91 93 }
+98 -40
tools/testing/selftests/bpf/trace_helpers.c
··· 14 14 #include <linux/limits.h> 15 15 #include <libelf.h> 16 16 #include <gelf.h> 17 + #include "bpf/libbpf_internal.h" 17 18 18 19 #define TRACEFS_PIPE "/sys/kernel/tracing/trace_pipe" 19 20 #define DEBUGFS_PIPE "/sys/kernel/debug/tracing/trace_pipe" 20 21 21 - #define MAX_SYMS 400000 22 - static struct ksym syms[MAX_SYMS]; 23 - static int sym_cnt; 22 + struct ksyms { 23 + struct ksym *syms; 24 + size_t sym_cap; 25 + size_t sym_cnt; 26 + }; 27 + 28 + static struct ksyms *ksyms; 29 + 30 + static int ksyms__add_symbol(struct ksyms *ksyms, const char *name, 31 + unsigned long addr) 32 + { 33 + void *tmp; 34 + 35 + tmp = strdup(name); 36 + if (!tmp) 37 + return -ENOMEM; 38 + ksyms->syms[ksyms->sym_cnt].addr = addr; 39 + ksyms->syms[ksyms->sym_cnt].name = tmp; 40 + ksyms->sym_cnt++; 41 + return 0; 42 + } 43 + 44 + void free_kallsyms_local(struct ksyms *ksyms) 45 + { 46 + unsigned int i; 47 + 48 + if (!ksyms) 49 + return; 50 + 51 + if (!ksyms->syms) { 52 + free(ksyms); 53 + return; 54 + } 55 + 56 + for (i = 0; i < ksyms->sym_cnt; i++) 57 + free(ksyms->syms[i].name); 58 + free(ksyms->syms); 59 + free(ksyms); 60 + } 24 61 25 62 static int ksym_cmp(const void *p1, const void *p2) 26 63 { 27 64 return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr; 28 65 } 29 66 30 - int load_kallsyms_refresh(void) 67 + struct ksyms *load_kallsyms_local(void) 31 68 { 32 69 FILE *f; 33 70 char func[256], buf[256]; 34 71 char symbol; 35 72 void *addr; 36 - int i = 0; 37 - 38 - sym_cnt = 0; 73 + int ret; 74 + struct ksyms *ksyms; 39 75 40 76 f = fopen("/proc/kallsyms", "r"); 41 77 if (!f) 42 - return -ENOENT; 78 + return NULL; 79 + 80 + ksyms = calloc(1, sizeof(struct ksyms)); 81 + if (!ksyms) { 82 + fclose(f); 83 + return NULL; 84 + } 43 85 44 86 while (fgets(buf, sizeof(buf), f)) { 45 87 if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3) 46 88 break; 47 89 if (!addr) 48 90 continue; 49 - if (i >= MAX_SYMS) 50 - return -EFBIG; 51 91 52 - syms[i].addr = (long) addr; 53 - syms[i].name = strdup(func); 54 - i++; 92 + ret = libbpf_ensure_mem((void **) &ksyms->syms, &ksyms->sym_cap, 93 + sizeof(struct ksym), ksyms->sym_cnt + 1); 94 + if (ret) 95 + goto error; 96 + ret = ksyms__add_symbol(ksyms, func, (unsigned long)addr); 97 + if (ret) 98 + goto error; 55 99 } 56 100 fclose(f); 57 - sym_cnt = i; 58 - qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp); 59 - return 0; 101 + qsort(ksyms->syms, ksyms->sym_cnt, sizeof(struct ksym), ksym_cmp); 102 + return ksyms; 103 + 104 + error: 105 + fclose(f); 106 + free_kallsyms_local(ksyms); 107 + return NULL; 60 108 } 61 109 62 110 int load_kallsyms(void) 63 111 { 64 - /* 65 - * This is called/used from multiplace places, 66 - * load symbols just once. 67 - */ 68 - if (sym_cnt) 69 - return 0; 70 - return load_kallsyms_refresh(); 112 + if (!ksyms) 113 + ksyms = load_kallsyms_local(); 114 + return ksyms ? 0 : 1; 71 115 } 72 116 73 - struct ksym *ksym_search(long key) 117 + struct ksym *ksym_search_local(struct ksyms *ksyms, long key) 74 118 { 75 - int start = 0, end = sym_cnt; 119 + int start = 0, end = ksyms->sym_cnt; 76 120 int result; 77 121 78 122 /* kallsyms not loaded. return NULL */ 79 - if (sym_cnt <= 0) 123 + if (ksyms->sym_cnt <= 0) 80 124 return NULL; 81 125 82 126 while (start < end) { 83 127 size_t mid = start + (end - start) / 2; 84 128 85 - result = key - syms[mid].addr; 129 + result = key - ksyms->syms[mid].addr; 86 130 if (result < 0) 87 131 end = mid; 88 132 else if (result > 0) 89 133 start = mid + 1; 90 134 else 91 - return &syms[mid]; 135 + return &ksyms->syms[mid]; 92 136 } 93 137 94 - if (start >= 1 && syms[start - 1].addr < key && 95 - key < syms[start].addr) 138 + if (start >= 1 && ksyms->syms[start - 1].addr < key && 139 + key < ksyms->syms[start].addr) 96 140 /* valid ksym */ 97 - return &syms[start - 1]; 141 + return &ksyms->syms[start - 1]; 98 142 99 143 /* out of range. return _stext */ 100 - return &syms[0]; 144 + return &ksyms->syms[0]; 145 + } 146 + 147 + struct ksym *ksym_search(long key) 148 + { 149 + if (!ksyms) 150 + return NULL; 151 + return ksym_search_local(ksyms, key); 152 + } 153 + 154 + long ksym_get_addr_local(struct ksyms *ksyms, const char *name) 155 + { 156 + int i; 157 + 158 + for (i = 0; i < ksyms->sym_cnt; i++) { 159 + if (strcmp(ksyms->syms[i].name, name) == 0) 160 + return ksyms->syms[i].addr; 161 + } 162 + 163 + return 0; 101 164 } 102 165 103 166 long ksym_get_addr(const char *name) 104 167 { 105 - int i; 106 - 107 - for (i = 0; i < sym_cnt; i++) { 108 - if (strcmp(syms[i].name, name) == 0) 109 - return syms[i].addr; 110 - } 111 - 112 - return 0; 168 + if (!ksyms) 169 + return 0; 170 + return ksym_get_addr_local(ksyms, name); 113 171 } 114 172 115 173 /* open kallsyms and read symbol addresses on the fly. Without caching all symbols,
+6 -2
tools/testing/selftests/bpf/trace_helpers.h
··· 11 11 long addr; 12 12 char *name; 13 13 }; 14 + struct ksyms; 14 15 15 16 int load_kallsyms(void); 16 - int load_kallsyms_refresh(void); 17 - 18 17 struct ksym *ksym_search(long key); 19 18 long ksym_get_addr(const char *name); 19 + 20 + struct ksyms *load_kallsyms_local(void); 21 + struct ksym *ksym_search_local(struct ksyms *ksyms, long key); 22 + long ksym_get_addr_local(struct ksyms *ksyms, const char *name); 23 + void free_kallsyms_local(struct ksyms *ksyms); 20 24 21 25 /* open kallsyms and find addresses on the fly, faster than load + search. */ 22 26 int kallsyms_find(const char *sym, unsigned long long *addr);