From 8e1f8c904e303d5d142d24e820f01b47a35bb72a Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Sat, 13 Feb 2021 17:46:48 +0100 Subject: [PATCH] btf_encoder: Match ftrace addresses within ELF functions Currently when processing a DWARF function, we check its entrypoint against ftrace addresses, assuming that the ftrace address matches with the function's entrypoint. This is not the case on some architectures as reported by Nathan when building kernel on arm [1]. Fix the check to take into account the whole function, not just the entrypoint. Most of the is_ftrace_func code was contributed by Andrii. [1] https://lore.kernel.org/bpf/20210209034416.GA1669105@ubuntu-m3-large-x86/ Committer notes: Test comments by Nathan: "I did several builds with CONFIG_DEBUG_INFO_BTF enabled (arm64, ppc64le, and x86_64) and saw no build errors. I did not do any runtime testing." Test comments by Sedat: Linux v5.11-rc7+ and LLVM/Clang v12.0.0-rc1 on x86 (64bit) Reported-by: Nathan Chancellor Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko Tested-by: Nathan Chancellor Tested-by: Sedat Dilek Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Daniel Borkmann Cc: Hao Luo Cc: John Fastabend Cc: KP Singh Cc: Martin KaFai Lau Cc: Song Liu Cc: Yonghong Song Cc: bpf@vger.kernel.org Cc: dwarves@vger.kernel.org Cc: netdev@vger.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- btf_encoder.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/btf_encoder.c b/btf_encoder.c index b124ec2..80e8969 100644 --- a/btf_encoder.c +++ b/btf_encoder.c @@ -36,6 +36,7 @@ struct funcs_layout { struct elf_function { const char *name; unsigned long addr; + unsigned long size; unsigned long sh_addr; bool generated; }; @@ -98,6 +99,7 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym, functions[functions_cnt].name = name; functions[functions_cnt].addr = elf_sym__value(sym); + functions[functions_cnt].size = elf_sym__size(sym); functions[functions_cnt].sh_addr = sh.sh_addr; functions[functions_cnt].generated = false; functions_cnt++; @@ -236,6 +238,39 @@ get_kmod_addrs(struct btf_elf *btfe, __u64 **paddrs, __u64 *pcount) return 0; } +static int is_ftrace_func(struct elf_function *func, __u64 *addrs, __u64 count) +{ + __u64 start = func->addr; + __u64 addr, end = func->addr + func->size; + + /* + * The invariant here is addr[r] that is the smallest address + * that is >= than function start addr. Except the corner case + * where there is no such r, but for that we have a final check + * in the return. + */ + size_t l = 0, r = count - 1, m; + + /* make sure we don't use invalid r */ + if (count == 0) + return false; + + while (l < r) { + m = l + (r - l) / 2; + addr = addrs[m]; + + if (addr >= start) { + /* we satisfy invariant, so tighten r */ + r = m; + } else { + /* m is not good enough as l, maybe m + 1 will be */ + l = m + 1; + } + } + + return start <= addrs[r] && addrs[r] < end; +} + static int setup_functions(struct btf_elf *btfe, struct funcs_layout *fl) { __u64 *addrs, count, i; @@ -283,10 +318,11 @@ static int setup_functions(struct btf_elf *btfe, struct funcs_layout *fl) * functions[x]::addr is relative address within section * and needs to be relocated by adding sh_addr. */ - __u64 addr = kmod ? func->addr + func->sh_addr : func->addr; + if (kmod) + func->addr += func->sh_addr; /* Make sure function is within ftrace addresses. */ - if (bsearch(&addr, addrs, count, sizeof(addrs[0]), addrs_cmp)) { + if (is_ftrace_func(func, addrs, count)) { /* * We iterate over sorted array, so we can easily skip * not valid item and move following valid field into