btf_encoder: fix BTF variable generation for kernel modules

Fix pahole's logic for determining per-CPU variables. For vmlinux,
btfe->percpu_base_addr is always 0, so it didn't matter at which point to
subtract it to get offset that later was matched against corresponding ELF
symbol.

For kernel module, though, the situation is different. Kernel module's per-CPU
data section has non-zero offset, which is taken into account in all DWARF
variable addresses calculation. For such cases, it's important to subtract
section offset (btfe->percpu_base_addr) before ELF symbol look up is
performed.

This patch also records per-CPU data section size and uses it for early
filtering of non-per-CPU variables by their address.

Fixes: 2e719cca66 ("btf_encoder: revamp how per-CPU variables are encoded")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: kernel-team@fb.com
Cc: Hao Luo <haoluo@google.com>
Link: https://lore.kernel.org/r/20201211041139.589692-2-andrii@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Andrii Nakryiko 2020-12-10 20:11:37 -08:00 committed by Arnaldo Carvalho de Melo
parent b94e97e015
commit 8c009d6ce7
3 changed files with 18 additions and 5 deletions

View File

@ -776,7 +776,7 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
printf("search cu '%s' for percpu global variables.\n", cu->name);
cu__for_each_variable(cu, core_id, pos) {
uint32_t size, type, linkage, offset;
uint32_t size, type, linkage;
const char *name;
uint64_t addr;
int id;
@ -790,12 +790,24 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
/* addr has to be recorded before we follow spec */
addr = var->ip.addr;
if (var->spec)
var = var->spec;
/* DWARF takes into account .data..percpu section offset
* within its segment, which for vmlinux is 0, but for kernel
* modules is >0. ELF symbols, on the other hand, don't take
* into account these offsets (as they are relative to the
* section start), so to match DWARF and ELF symbols we need
* to negate the section base address here.
*/
if (addr < btfe->percpu_base_addr || addr >= btfe->percpu_base_addr + btfe->percpu_sec_sz)
continue;
addr -= btfe->percpu_base_addr;
if (!percpu_var_exists(addr, &size, &name))
continue; /* not a per-CPU variable */
if (var->spec)
var = var->spec;
if (var->ip.tag.type == 0) {
fprintf(stderr, "error: found variable '%s' in CU '%s' that has void type\n",
name, cu->name);
@ -826,8 +838,7 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
* add a BTF_VAR_SECINFO in btfe->percpu_secinfo, which will be added into
* btfe->types later when we add BTF_VAR_DATASEC.
*/
offset = addr - btfe->percpu_base_addr;
id = btf_elf__add_var_secinfo(&btfe->percpu_secinfo, id, offset, size);
id = btf_elf__add_var_secinfo(&btfe->percpu_secinfo, id, addr, size);
if (id < 0) {
err = -1;
fprintf(stderr, "error: failed to encode section info for variable '%s' at addr 0x%" PRIx64 "\n",

View File

@ -170,6 +170,7 @@ try_as_raw_btf:
}
btfe->percpu_shndx = elf_ndxscn(sec);
btfe->percpu_base_addr = shdr.sh_addr;
btfe->percpu_sec_sz = shdr.sh_size;
return btfe;

View File

@ -26,6 +26,7 @@ struct btf_elf {
bool raw_btf; // "/sys/kernel/btf/vmlinux"
uint32_t percpu_shndx;
uint64_t percpu_base_addr;
uint64_t percpu_sec_sz;
struct btf *btf;
struct btf *base_btf;
};