pahole: Workaround dwarf bug for function encoding
Jiri Olsa
reviewerv2 cover pahole: Workaround dwarf bug for function encoding
hi,
because of gcc bug [1] we can no longer rely on DW_AT_declaration attribute to filter out declarations and end up with just one copy of the function in the BTF data.
It seems this bug is not easy to fix, but regardless if the it's coming soon, it's probably good idea not to depend so much only on dwarf data and make some extra checks.
Thus for function encoding we are now doing following checks:
- argument names are defined for the function
- there's symbol and address defined for the function
- function address belongs to ftrace locations (new in v2)
- function is generated only once
v2 changes:
- add check ensuring functions belong to ftrace's mcount locations, this way we ensure to have in BTF only functions available for ftrace - patch 2 changelog describes all details
- use collect* function names [Andrii]
- use conventional size increase in realloc [Andrii]
- drop elf_sym__is_function check
- drop patch 3, it's not needed, because we follow ftrace locations
With v2 patchset I'm getting 38334 BTF functions, which is reasonable subset of ftrace's available_filter_functions.
thanks,
jirka
[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97060
---
Jiri Olsa (2):
btf_encoder: Move find_all_percpu_vars in generic collect_symbols
btf_encoder: Change functions check due to broken dwarf
btf_encoder.c | 338 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 283 insertions(+), 55 deletions(-)
Jiri Olsa
reviewerPATCH v2 1/2 btf_encoder: Move find_all_percpu_vars in generic collect_symbols
Moving find_all_percpu_vars under generic collect_symbols function that walks over symbols and calls collect_percpu_var.
We will add another collect function that needs to go through all the symbols, so it's better we go through them just once.
There's no functional change intended.
--- btf_encoder.c | 124 +++++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 57 deletions(-)
btf_encoder.c +67 -57
@@ -250,75 +250,85 @@ static bool percpu_var_exists(uint64_t addr, uint32_t *sz, const char **name)return true; } -static int find_all_percpu_vars (struct btf_elf *btfe) +static int collect_percpu_var (struct btf_elf *btfe,GElf_Sym *sym) { -uint32_t core_id; -GElf_Sym sym; +const char *sym_name; +uint64_t addr; +uint32_t size; -/* cache variables' addresses, preparing for searching in symtab. */ - percpu_var_cnt =0 ; +/* compare a symbol's shndx to determine if it's a percpu variable */ +if (elf_sym__section (sym) != btfe->percpu_shndx) +return 0 ; +if (elf_sym__type (sym) !=STT_OBJECT ) +return 0 ; -/* search within symtab for percpu variables */ -elf_symtab__for_each_symbol (btfe->symtab, core_id, sym) { -const char *sym_name; -uint64_t addr; -uint32_t size; + addr =elf_sym__value (sym); +/* +* Store only those symbols that have allocated space in the percpu section. +* This excludes the following three types of symbols: +* +* 1. __ADDRESSABLE(sym), which are forcely emitted as symbols. +* 2. __UNIQUE_ID(prefix), which are introduced to generate unique ids. +* 3. __exitcall(fn), functions which are labeled as exit calls. +* +* In addition, the variables defined using DEFINE_PERCPU_FIRST are +* also not included, which currently includes: +* +* 1. fixed_percpu_data +*/ +if (!addr) +return 0 ; -/* compare a symbol's shndx to determine if it's a percpu variable */ -if (elf_sym__section (&sym) != btfe->percpu_shndx) -continue ; -if (elf_sym__type (&sym) !=STT_OBJECT ) -continue ; + size =elf_sym__size (sym); +if (!size) +return 0 ;/* ignore zero-sized symbols */ - addr =elf_sym__value (&sym); -/* -* Store only those symbols that have allocated space in the percpu section. -* This excludes the following three types of symbols: -* -* 1. __ADDRESSABLE(sym), which are forcely emitted as symbols. -* 2. __UNIQUE_ID(prefix), which are introduced to generate unique ids. -* 3. __exitcall(fn), functions which are labeled as exit calls. -* -* In addition, the variables defined using DEFINE_PERCPU_FIRST are -* also not included, which currently includes: -* -* 1. fixed_percpu_data -*/ -if (!addr) -continue ; + sym_name =elf_sym__name (sym, btfe->symtab); +if (!btf_name_valid (sym_name)) { +dump_invalid_symbol ("Found symbol of invalid name when encoding btf" , + sym_name, btf_elf__verbose, btf_elf__force); +if (btf_elf__force) +return 0 ; +return -1 ; + } - size =elf_sym__size (&sym); -if (!size) -continue ;/* ignore zero-sized symbols */ +if (btf_elf__verbose) +printf ("Found per-CPU symbol '%s' at address 0x%lx\n" , sym_name, addr); - sym_name =elf_sym__name (&sym, btfe->symtab); -if (!btf_name_valid (sym_name)) { -dump_invalid_symbol ("Found symbol of invalid name when encoding btf" , - sym_name, btf_elf__verbose, btf_elf__force); -if (btf_elf__force) -continue ; -return -1 ; - } +if (percpu_var_cnt ==MAX_PERCPU_VAR_CNT ) { +fprintf (stderr,"Reached the limit of per-CPU variables: %d\n" , +MAX_PERCPU_VAR_CNT ); +return -1 ; + } + percpu_vars[percpu_var_cnt].addr = addr; + percpu_vars[percpu_var_cnt].sz = size; + percpu_vars[percpu_var_cnt].name = sym_name; + percpu_var_cnt++; -if (btf_elf__verbose) -printf ("Found per-CPU symbol '%s' at address 0x%lx\n" , sym_name, addr); +return 0 ; +} + +static int collect_symbols (struct btf_elf *btfe,bool collect_percpu_vars) +{ +uint32_t core_id; +GElf_Sym sym; -if (percpu_var_cnt ==MAX_PERCPU_VAR_CNT ) { -fprintf (stderr,"Reached the limit of per-CPU variables: %d\n" , -MAX_PERCPU_VAR_CNT ); +/* cache variables' addresses, preparing for searching in symtab. */ + percpu_var_cnt =0 ; + +/* search within symtab for percpu variables */ +elf_symtab__for_each_symbol (btfe->symtab, core_id, sym) { +if (collect_percpu_vars &&collect_percpu_var (btfe, &sym))return -1 ; - } - percpu_vars[percpu_var_cnt].addr = addr; - percpu_vars[percpu_var_cnt].sz = size; - percpu_vars[percpu_var_cnt].name = sym_name; - percpu_var_cnt++; } -if (percpu_var_cnt) -qsort (percpu_vars, percpu_var_cnt,sizeof (percpu_vars[0 ]), percpu_var_cmp); +if (collect_percpu_vars) { +if (percpu_var_cnt) +qsort (percpu_vars, percpu_var_cnt,sizeof (percpu_vars[0 ]), percpu_var_cmp); -if (btf_elf__verbose) -printf ("Found %d per-CPU variables!\n" , percpu_var_cnt); +if (btf_elf__verbose) +printf ("Found %d per-CPU variables!\n" , percpu_var_cnt); + }return 0 ; }
@@ -347,7 +357,7 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,if (!btfe)return -1 ; -if (!skip_encoding_vars &&find_all_percpu_vars (btfe)) +if (collect_symbols (btfe, !skip_encoding_vars)) goto out; has_index_type = false;
Signature · 1 line
2.26.2
Re: [PATCH 1/2] btf_encoder: Move find_all_percpu_vars in generic collect_symbols
This looks good to me. Thanks, Jiri.
Quoted 172 lines · Jiri Olsa
Moving
find_all_percpu_varsunder genericcollect_symbolsfunction that walks over symbols and callscollect_percpu_var.We will add another collect function that needs to go through all the symbols, so it's better we go through them just once.
There's no functional change intended.
Signed-off-by Jiri Olsa <[email protected]>--- btf_encoder.c | 124 +++++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 57 deletions(-)btf_encoder.c +67
-58@@ -250,75 +250,85 @@ static bool percpu_var_exists(uint64_t addr, uint32_t *sz, const char **name)return true; } -static int find_all_percpu_vars (struct btf_elf *btfe) +static int collect_percpu_var (struct btf_elf *btfe,GElf_Sym *sym) { -uint32_t core_id; -GElf_Sym sym; +const char *sym_name; +uint64_t addr; +uint32_t size; -/* cache variables' addresses, preparing for searching in symtab. */ - percpu_var_cnt =0 ; +/* compare a symbol's shndx to determine if it's a percpu variable */ +if (elf_sym__section (sym) != btfe->percpu_shndx) +return 0 ; +if (elf_sym__type (sym) !=STT_OBJECT ) +return 0 ; -/* search within symtab for percpu variables */ -elf_symtab__for_each_symbol (btfe->symtab, core_id, sym) { -const char *sym_name; -uint64_t addr; -uint32_t size; + addr =elf_sym__value (sym); +/* +* Store only those symbols that have allocated space in the percpu section. +* This excludes the following three types of symbols: +* +* 1. __ADDRESSABLE(sym), which are forcely emitted as symbols. +* 2. __UNIQUE_ID(prefix), which are introduced to generate unique ids. +* 3. __exitcall(fn), functions which are labeled as exit calls. +* +* In addition, the variables defined using DEFINE_PERCPU_FIRST are +* also not included, which currently includes: +* +* 1. fixed_percpu_data +*/ +if (!addr) +return 0 ; -/* compare a symbol's shndx to determine if it's a percpu variable */ -if (elf_sym__section (&sym) != btfe->percpu_shndx) -continue ; -if (elf_sym__type (&sym) !=STT_OBJECT ) -continue ; + size =elf_sym__size (sym); +if (!size) +return 0 ;/* ignore zero-sized symbols */ - addr =elf_sym__value (&sym); -/* -* Store only those symbols that have allocated space in the percpu section. -* This excludes the following three types of symbols: -* -* 1. __ADDRESSABLE(sym), which are forcely emitted as symbols. -* 2. __UNIQUE_ID(prefix), which are introduced to generate unique ids. -* 3. __exitcall(fn), functions which are labeled as exit calls. -* -* In addition, the variables defined using DEFINE_PERCPU_FIRST are -* also not included, which currently includes: -* -* 1. fixed_percpu_data -*/ -if (!addr) -continue ; + sym_name =elf_sym__name (sym, btfe->symtab); +if (!btf_name_valid (sym_name)) { +dump_invalid_symbol ("Found symbol of invalid name when encoding btf" , + sym_name, btf_elf__verbose, btf_elf__force); +if (btf_elf__force) +return 0 ; +return -1 ; + } - size =elf_sym__size (&sym); -if (!size) -continue ;/* ignore zero-sized symbols */ +if (btf_elf__verbose) +printf ("Found per-CPU symbol '%s' at address 0x%lx\n" , sym_name, addr); - sym_name =elf_sym__name (&sym, btfe->symtab); -if (!btf_name_valid (sym_name)) { -dump_invalid_symbol ("Found symbol of invalid name when encoding btf" , - sym_name, btf_elf__verbose, btf_elf__force); -if (btf_elf__force) -continue ; -return -1 ; - } +if (percpu_var_cnt ==MAX_PERCPU_VAR_CNT ) { +fprintf (stderr,"Reached the limit of per-CPU variables: %d\n" , +MAX_PERCPU_VAR_CNT ); +return -1 ; + } + percpu_vars[percpu_var_cnt].addr = addr; + percpu_vars[percpu_var_cnt].sz = size; + percpu_vars[percpu_var_cnt].name = sym_name; + percpu_var_cnt++; -if (btf_elf__verbose) -printf ("Found per-CPU symbol '%s' at address 0x%lx\n" , sym_name, addr); +return 0 ; +} + +static int collect_symbols (struct btf_elf *btfe,bool collect_percpu_vars) +{ +uint32_t core_id; +GElf_Sym sym; -if (percpu_var_cnt ==MAX_PERCPU_VAR_CNT ) { -fprintf (stderr,"Reached the limit of per-CPU variables: %d\n" , -MAX_PERCPU_VAR_CNT ); +/* cache variables' addresses, preparing for searching in symtab. */ + percpu_var_cnt =0 ; + +/* search within symtab for percpu variables */ +elf_symtab__for_each_symbol (btfe->symtab, core_id, sym) { +if (collect_percpu_vars &&collect_percpu_var (btfe, &sym))return -1 ; - } - percpu_vars[percpu_var_cnt].addr = addr; - percpu_vars[percpu_var_cnt].sz = size; - percpu_vars[percpu_var_cnt].name = sym_name; - percpu_var_cnt++; } -if (percpu_var_cnt) -qsort (percpu_vars, percpu_var_cnt,sizeof (percpu_vars[0 ]), percpu_var_cmp); +if (collect_percpu_vars) { +if (percpu_var_cnt) +qsort (percpu_vars, percpu_var_cnt,sizeof (percpu_vars[0 ]), percpu_var_cmp); -if (btf_elf__verbose) -printf ("Found %d per-CPU variables!\n" , percpu_var_cnt); +if (btf_elf__verbose) +printf ("Found %d per-CPU variables!\n" , percpu_var_cnt); + }return 0 ; }@@ -347,7 +357,7 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,if (!btfe)return -1 ; -if (!skip_encoding_vars &&find_all_percpu_vars (btfe)) +if (collect_symbols (btfe, !skip_encoding_vars)) goto out; has_index_type = false; --2.26.2
Arnaldo Carvalho de Melo
AppliedRe: [PATCH 1/2] btf_encoder: Move find_all_percpu_vars in generic collect_symbols
Em Mon, Nov 02, 2020 at 10:29:22AM -0800, Hao Luo escreveu:
Quoted 3 lines
This looks good to me. Thanks, Jiri.
Acked-by Hao Luo <[email protected]>
Thanks, applied, will wait for a v2 for the other.
Quoted 173 lines · Jiri Olsa
Quoted 172 lines
Moving
find_all_percpu_varsunder genericcollect_symbolsfunction that walks over symbols and callscollect_percpu_var.We will add another collect function that needs to go through all the symbols, so it's better we go through them just once.
There's no functional change intended.
Signed-off-by Jiri Olsa <[email protected]>--- btf_encoder.c | 124 +++++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 57 deletions(-)btf_encoder.c +67
-58@@ -250,75 +250,85 @@ static bool percpu_var_exists(uint64_t addr, uint32_t *sz, const char **name)return true; } -static int find_all_percpu_vars (struct btf_elf *btfe) +static int collect_percpu_var (struct btf_elf *btfe,GElf_Sym *sym) { -uint32_t core_id; -GElf_Sym sym; +const char *sym_name; +uint64_t addr; +uint32_t size; -/* cache variables' addresses, preparing for searching in symtab. */ - percpu_var_cnt =0 ; +/* compare a symbol's shndx to determine if it's a percpu variable */ +if (elf_sym__section (sym) != btfe->percpu_shndx) +return 0 ; +if (elf_sym__type (sym) !=STT_OBJECT ) +return 0 ; -/* search within symtab for percpu variables */ -elf_symtab__for_each_symbol (btfe->symtab, core_id, sym) { -const char *sym_name; -uint64_t addr; -uint32_t size; + addr =elf_sym__value (sym); +/* +* Store only those symbols that have allocated space in the percpu section. +* This excludes the following three types of symbols: +* +* 1. __ADDRESSABLE(sym), which are forcely emitted as symbols. +* 2. __UNIQUE_ID(prefix), which are introduced to generate unique ids. +* 3. __exitcall(fn), functions which are labeled as exit calls. +* +* In addition, the variables defined using DEFINE_PERCPU_FIRST are +* also not included, which currently includes: +* +* 1. fixed_percpu_data +*/ +if (!addr) +return 0 ; -/* compare a symbol's shndx to determine if it's a percpu variable */ -if (elf_sym__section (&sym) != btfe->percpu_shndx) -continue ; -if (elf_sym__type (&sym) !=STT_OBJECT ) -continue ; + size =elf_sym__size (sym); +if (!size) +return 0 ;/* ignore zero-sized symbols */ - addr =elf_sym__value (&sym); -/* -* Store only those symbols that have allocated space in the percpu section. -* This excludes the following three types of symbols: -* -* 1. __ADDRESSABLE(sym), which are forcely emitted as symbols. -* 2. __UNIQUE_ID(prefix), which are introduced to generate unique ids. -* 3. __exitcall(fn), functions which are labeled as exit calls. -* -* In addition, the variables defined using DEFINE_PERCPU_FIRST are -* also not included, which currently includes: -* -* 1. fixed_percpu_data -*/ -if (!addr) -continue ; + sym_name =elf_sym__name (sym, btfe->symtab); +if (!btf_name_valid (sym_name)) { +dump_invalid_symbol ("Found symbol of invalid name when encoding btf" , + sym_name, btf_elf__verbose, btf_elf__force); +if (btf_elf__force) +return 0 ; +return -1 ; + } - size =elf_sym__size (&sym); -if (!size) -continue ;/* ignore zero-sized symbols */ +if (btf_elf__verbose) +printf ("Found per-CPU symbol '%s' at address 0x%lx\n" , sym_name, addr); - sym_name =elf_sym__name (&sym, btfe->symtab); -if (!btf_name_valid (sym_name)) { -dump_invalid_symbol ("Found symbol of invalid name when encoding btf" , - sym_name, btf_elf__verbose, btf_elf__force); -if (btf_elf__force) -continue ; -return -1 ; - } +if (percpu_var_cnt ==MAX_PERCPU_VAR_CNT ) { +fprintf (stderr,"Reached the limit of per-CPU variables: %d\n" , +MAX_PERCPU_VAR_CNT ); +return -1 ; + } + percpu_vars[percpu_var_cnt].addr = addr; + percpu_vars[percpu_var_cnt].sz = size; + percpu_vars[percpu_var_cnt].name = sym_name; + percpu_var_cnt++; -if (btf_elf__verbose) -printf ("Found per-CPU symbol '%s' at address 0x%lx\n" , sym_name, addr); +return 0 ; +} + +static int collect_symbols (struct btf_elf *btfe,bool collect_percpu_vars) +{ +uint32_t core_id; +GElf_Sym sym; -if (percpu_var_cnt ==MAX_PERCPU_VAR_CNT ) { -fprintf (stderr,"Reached the limit of per-CPU variables: %d\n" , -MAX_PERCPU_VAR_CNT ); +/* cache variables' addresses, preparing for searching in symtab. */ + percpu_var_cnt =0 ; + +/* search within symtab for percpu variables */ +elf_symtab__for_each_symbol (btfe->symtab, core_id, sym) { +if (collect_percpu_vars &&collect_percpu_var (btfe, &sym))return -1 ; - } - percpu_vars[percpu_var_cnt].addr = addr; - percpu_vars[percpu_var_cnt].sz = size; - percpu_vars[percpu_var_cnt].name = sym_name; - percpu_var_cnt++; } -if (percpu_var_cnt) -qsort (percpu_vars, percpu_var_cnt,sizeof (percpu_vars[0 ]), percpu_var_cmp); +if (collect_percpu_vars) { +if (percpu_var_cnt) +qsort (percpu_vars, percpu_var_cnt,sizeof (percpu_vars[0 ]), percpu_var_cmp); -if (btf_elf__verbose) -printf ("Found %d per-CPU variables!\n" , percpu_var_cnt); +if (btf_elf__verbose) +printf ("Found %d per-CPU variables!\n" , percpu_var_cnt); + }return 0 ; }@@ -347,7 +357,7 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,if (!btfe)return -1 ; -if (!skip_encoding_vars &&find_all_percpu_vars (btfe)) +if (collect_symbols (btfe, !skip_encoding_vars)) goto out; has_index_type = false; --2.26.2
Signature · 1 line
- Arnaldo
Jiri Olsa
reviewerPATCH v2 2/2 btf_encoder: Change functions check due to broken dwarf
In reply to Jiri OlsaWe need to generate just single BTF instance for the function, while DWARF data contains multiple instances of DW_TAG_subprogram tag.
Unfortunately we can no longer rely on DW_AT_declaration tag (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97060)
Instead we apply following checks:
- argument names are defined for the function
- there's symbol and address defined for the function
- function is generated only once
Also because we want to follow kernel's ftrace traceable functions, this patchset is adding extra check that the function is one of the ftrace's functions.
All ftrace functions addresses are stored in vmlinux binary within symbols:
__start_mcount_loc
__stop_mcount_loc
During object preparation code we read those addresses, sort them and use them as filter for all detected dwarf functions.
We also filter out functions within .init section, ftrace is doing that in runtime.
I can still see several differences to ftrace functions in /sys/kernel/debug/tracing/available_filter_functions file:
- available_filter_functions includes modules (7086 functions)
- available_filter_functions includes functions like:
__acpi_match_device.part.0.constprop.0 acpi_ns_check_sorted_list.constprop.0 acpi_os_unmap_generic_address.part.0 acpiphp_check_bridge.part.0
which are not part of dwarf data (1164 functions)
- BTF includes multiple functions like:
__clk_register_clkdev clk_register_clkdev
which share same code so they appear just as single function in available_filter_functions, but dwarf keeps track of both of them (16 functions)
With this change I'm getting 38334 BTF functions, which when added above functions to consideration gives same amount of functions in available_filter_functions.
The patch still keeps the original function filter condition (that uses current fn->declaration check) in case the object does not contain *_mcount_loc symbol -> object is not vmlinux.
--- btf_encoder.c | 222 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 220 insertions(+), 2 deletions(-)
btf_encoder.c +220 -2
@@ -26,6 +26,151 @@*/ #define KSYM_NAME_LEN 128 +struct mcount_symbols { +unsigned long start; +unsigned long stop; +unsigned long init_begin; +unsigned long init_end; +unsigned long start_section; +}; + +struct elf_function { +const char *name; +unsigned long addr; +bool generated; +bool valid; +}; + +static struct elf_function *functions; +static int functions_alloc; +static int functions_cnt; +static int functions_valid; + +static int functions_cmp (const void *_a,const void *_b) +{ +const struct elf_function *a = _a; +const struct elf_function *b = _b; + +return strcmp (a->name, b->name); +} + +static void delete_functions (void ) +{ +free (functions); + functions_alloc = functions_cnt = functions_valid =0 ; +} + +#ifndef max +# define max (x, y) ((x) < (y) ? (y) : (x)) +#endif + +static int collect_function (struct btf_elf *btfe,GElf_Sym *sym) +{ +if (elf_sym__type (sym) !=STT_FUNC ) +return 0 ; +if (!elf_sym__value (sym)) +return 0 ; + +if (functions_cnt == functions_alloc) { + functions_alloc =max (1000 , functions_alloc *3 /2 ); + functions =realloc (functions, functions_alloc *sizeof (*functions)); +if (!functions) +return -1 ; + } + + functions[functions_cnt].name =elf_sym__name (sym, btfe->symtab); + functions[functions_cnt].addr =elf_sym__value (sym); + functions[functions_cnt].generated = false; + functions[functions_cnt].valid = false; + functions_cnt++; +return 0 ; +} + +static int addrs_cmp (const void *_a,const void *_b) +{ +const unsigned long *a = _a; +const unsigned long *b = _b; + +return *a - *b; +} + +static int filter_functions (struct btf_elf *btfe,struct mcount_symbols *ms) +{ +bool init_filter = ms->init_begin && ms->init_end; +unsigned long *addrs, count, offset, i; +Elf_Data *data; +GElf_Shdr shdr; +Elf_Scn *sec; + +/* +* Find mcount addressed marked by __start_mcount_loc +* and __stop_mcount_loc symbols and load them into +* sorted array. +*/ + sec =elf_getscn (btfe->elf, ms->start_section); +if (!sec || !gelf_getshdr (sec, &shdr)) { +fprintf (stderr,"Failed to get section(%lu) header.\n" , + ms->start_section); +return -1 ; + } + + offset = ms->start - shdr.sh_addr; + count = (ms->stop - ms->start) /8 ; + + data =elf_getdata (sec,0 ); +if (!data) { +fprintf (stderr,"Failed to section(%lu) data.\n" , + ms->start_section); +return -1 ; + } + + addrs =malloc (count *sizeof (addrs[0 ])); +if (!addrs) { +fprintf (stderr,"Failed to allocate memory for ftrace addresses.\n" ); +return -1 ; + } + +memcpy (addrs, data->d_buf + offset, count *sizeof (addrs[0 ])); +qsort (addrs, count,sizeof (addrs[0 ]), addrs_cmp); + +/* +* Let's got through all collected functions and filter +* out those that are not in ftrace and init code. +*/ +for (i =0 ; i < functions_cnt; i++) { +struct elf_function *func = &functions[i]; + +/* Do not enable .init section functions. */ +if (init_filter && + func->addr >= ms->init_begin && + func->addr < ms->init_end) +continue ; + +/* Make sure function is within mcount addresses. */ +if (bsearch (&func->addr, addrs, count,sizeof (addrs[0 ]), addrs_cmp)) { + functions_valid++; + func->valid = true; + } + } + +free (addrs); +return 0 ; +} + +static bool should_generate_function (const struct btf_elf *btfe,const char *name) +{ +struct elf_function *p; +struct elf_function key = { .name = name }; + + p =bsearch (&key, functions, functions_cnt, +sizeof (functions[0 ]), functions_cmp); +if (!p || !p->valid || p->generated) +return false; + + p->generated = true; +return true; +} +static bool btf_name_char_ok (char c,bool first) {if (c =='_' || c =='.' )
@@ -207,6 +352,7 @@ int btf_encoder__encode()btf_elf__add_datasec_type (btfe,PERCPU_SECTION , &btfe->percpu_secinfo); err =btf_elf__encode (btfe,0 ); +delete_functions ();btf_elf__delete (btfe); btfe =NULL ;
@@ -308,8 +454,27 @@ static int collect_percpu_var(struct btf_elf *btfe, GElf_Sym *sym)return 0 ; } +#define SET_SYMBOL (__sym, __var) \ + if (!ms->__var && !strcmp(__sym, elf_sym__name(sym, btfe->symtab))) \ + ms->__var = sym->st_value; \ + +static void collect_mcount_symbol (GElf_Sym *sym,struct mcount_symbols *ms) +{ +if (!ms->start && + !strcmp ("__start_mcount_loc" ,elf_sym__name (sym, btfe->symtab))) { + ms->start = sym->st_value; + ms->start_section = sym->st_shndx; + } + SET_SYMBOL("__stop_mcount_loc", stop) + SET_SYMBOL("__init_begin", init_begin) + SET_SYMBOL("__init_end", init_end) +} + +#undef SET_SYMBOL +static int collect_symbols (struct btf_elf *btfe,bool collect_percpu_vars) { +struct mcount_symbols ms = { };uint32_t core_id;GElf_Sym sym;
@@ -320,6 +485,9 @@ static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars)elf_symtab__for_each_symbol (btfe->symtab, core_id, sym) {if (collect_percpu_vars &&collect_percpu_var (btfe, &sym))return -1 ; +if (collect_function (btfe, &sym)) +return -1 ; +collect_mcount_symbol (&sym, &ms); }if (collect_percpu_vars) {
@@ -329,9 +497,34 @@ static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars)if (btf_elf__verbose)printf ("Found %d per-CPU variables!\n" , percpu_var_cnt); } + +if (functions_cnt) { +qsort (functions, functions_cnt,sizeof (functions[0 ]), functions_cmp); +if (ms.start && ms.stop && +filter_functions (btfe, &ms)) { +fprintf (stderr,"Failed to filter dwarf functions\n" ); +return -1 ; + } +if (btf_elf__verbose) +printf ("Found %d functions!\n" , functions_valid); + } +return 0 ; } +static bool has_arg_names (struct cu *cu,struct ftype *ftype) +{ +struct parameter *param; +const char *name; + +ftype__for_each_parameter (ftype, param) { + name = dwarves__active_loader->strings__ptr (cu, param->name); +if (name ==NULL ) +return false; + } +return true; +} +int cu__encode_btf (struct cu *cu,int verbose,bool force,bool skip_encoding_vars) {
@@ -407,8 +600,32 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,int btf_fnproto_id, btf_fn_id;const char *name; -if (fn->declaration || !fn->external) -continue ; +/* +* The functions_valid != 0 means we parsed all necessary +* kernel symbols and we are using mcount location filter +* for functions. If it's not available keep the current +* dwarf declaration check. +*/ +if (functions_valid) { +/* +* We need to generate just single BTF instance for the +* function, while DWARF data contains multiple instances +* of DW_TAG_subprogram tag. +* +* We check following conditions: +* - argument names are defined +* - there's symbol and address defined for the function +* - function address belongs to ftrace locations +* - function is generated only once +*/ +if (!has_arg_names (cu, &fn->proto)) +continue ; +if (!should_generate_function (btfe,function__name (fn, cu))) +continue ; + }else { +if (fn->declaration || !fn->external) +continue ; + } btf_fnproto_id =btf_elf__add_func_proto (btfe, cu, &fn->proto, type_id_off); name = dwarves__active_loader->strings__ptr (cu, fn->name);
@@ -492,6 +709,7 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force, out:if (err) { +delete_functions ();btf_elf__delete (btfe); btfe =NULL ; }
Signature · 1 line
2.26.2
Re: [PATCH 2/2] btf_encoder: Change functions check due to broken dwarf
Quoted 12 lines · Jiri Olsa
We need to generate just single BTF instance for the function, while DWARF data contains multiple instances of
DW_TAG_subprogramtag.Unfortunately we can no longer rely on
DW_AT_declarationtag (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97060)Instead we apply following checks:
- argument names are defined for the function
- there's symbol and address defined for the function
- function is generated only once
Quoted 8 lines
Also because we want to follow kernel's ftrace traceable functions, this patchset is adding extra check that the function is one of the ftrace's functions.
All ftrace functions addresses are stored in vmlinux binary within symbols:
__start_mcount_loc
__stop_mcount_loc
hum, for some reason this does not pass through bpf internal functions like bpf_iter_bpf_map.. I learned it hard way ;-) will check
jirka
Re: [PATCH 2/2] btf_encoder: Change functions check due to broken dwarf
Quoted 17 lines · Jiri Olsa
Quoted 16 lines · Jiri Olsa
We need to generate just single BTF instance for the function, while DWARF data contains multiple instances of
DW_TAG_subprogramtag.Unfortunately we can no longer rely on
DW_AT_declarationtag (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97060)Instead we apply following checks:
- argument names are defined for the function
- there's symbol and address defined for the function
- function is generated only onceAlso because we want to follow kernel's ftrace traceable functions, this patchset is adding extra check that the function is one of the ftrace's functions.
Quoted 8 lines
Quoted 4 lines
All ftrace functions addresses are stored in vmlinux binary within symbols:
__start_mcount_loc
__stop_mcount_lochum, for some reason this does not pass through bpf internal functions like
bpf_iter_bpf_map.. I learned it hard way ;-) will check
so it gets filtered out because it's __init function I'll check if the fix below catches all internal functions, but I guess we should do something more robust
jirka
---
btf_encoder.c +2 -1
@@ -143,7 +143,8 @@ static int filter_functions(struct btf_elf *btfe, struct mcount_symbols *ms)/* Do not enable .init section functions. */ if (init_filter && func->addr >= ms->init_begin && - func->addr < ms->init_end) + func->addr < ms->init_end && +strncmp ("bpf_" , func->name,4 ))continue ;/* Make sure function is within mcount addresses. */
Re: [PATCH 2/2] btf_encoder: Change functions check due to broken dwarf
Quoted 19 lines · Jiri Olsa
Quoted 17 lines · Jiri Olsa
Quoted 16 lines · Jiri Olsa
We need to generate just single BTF instance for the function, while DWARF data contains multiple instances of
DW_TAG_subprogramtag.Unfortunately we can no longer rely on
DW_AT_declarationtag (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97060)Instead we apply following checks:
- argument names are defined for the function
- there's symbol and address defined for the function
- function is generated only onceAlso because we want to follow kernel's ftrace traceable functions, this patchset is adding extra check that the function is one of the ftrace's functions.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
All ftrace functions addresses are stored in vmlinux binary within symbols:
__start_mcount_loc
__stop_mcount_lochum, for some reason this does not pass through bpf internal functions like
bpf_iter_bpf_map.. I learned it hard way ;-)
what's the exact name of the function that was missing? bpf_iter_bpf_map doesn't exist. And if it's __init function, why does it matter, it's not going to be even available at runtime, right?
Quoted 15 lines
Quoted 1 line
will check
so it gets filtered out because it's
__initfunction I'll check if the fix below catches all internal functions, but I guess we should do something more robustjirka
---
btf_encoder.c +0
-0
btf_encoder.c static int filter_functions(struct btf_elf *btfe, struct mcount_symbols *ms) · line 147
Quoted 6 lines
btf_encoder.c +2
-1@@ -143,7 +143,8 @@ static int filter_functions(struct btf_elf *btfe, struct mcount_symbols *ms)/* Do not enable .init section functions. */ if (init_filter && func->addr >= ms->init_begin && - func->addr < ms->init_end) + func->addr < ms->init_end && +strncmp ("bpf_" , func->name,4 ))
this looks like a very wrong way to do this? Can you please elaborate on what's missing and why it shouldn't be missing?
Quoted 4 lines
btf_encoder.c +0
-0continue ;/* Make sure function is within mcount addresses. */
Re: [PATCH 2/2] btf_encoder: Change functions check due to broken dwarf
Quoted 25 lines · Andrii Nakryiko
Quoted 24 lines · Jiri Olsa
Quoted 22 lines · Jiri Olsa
Quoted 20 lines · Jiri Olsa
We need to generate just single BTF instance for the function, while DWARF data contains multiple instances of
DW_TAG_subprogramtag.Unfortunately we can no longer rely on
DW_AT_declarationtag (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97060)Instead we apply following checks:
- argument names are defined for the function
- there's symbol and address defined for the function
- function is generated only onceAlso because we want to follow kernel's ftrace traceable functions, this patchset is adding extra check that the function is one of the ftrace's functions.
All ftrace functions addresses are stored in vmlinux binary within symbols:
__start_mcount_loc
__stop_mcount_loc
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
hum, for some reason this does not pass through bpf internal functions like
bpf_iter_bpf_map.. I learned it hard way ;-)what's the exact name of the function that was missing?
bpf_iter_bpf_mapdoesn't exist. And if it's__initfunction, why does it matter, it's not going to be even available at runtime, right?
bpf_map iter definition:
DEFINE_BPF_ITER_FUNC(bpf_map, struct bpf_iter_meta *meta, struct bpf_map *map)
goes to:
#define DEFINE_BPF_ITER_FUNC(target, args...) \
extern int bpf_iter_ ## target(args); \
int __init bpf_iter_ ## target(args) { return 0; }that creates __init bpf_iter_bpf_map function that will make it into BTF where it's expected when opening iterator, but the code will be freed because it's __init function
there are few iteratos functions like that, and I was going to check if there's more
Quoted 10 lines
Quoted 10 lines
Quoted 2 lines
will check
so it gets filtered out because it's
__initfunction I'll check if the fix below catches all internal functions, but I guess we should do something more robustjirka
Quoted 15 lines
btf_encoder.c static int filter_functions(struct btf_elf *btfe, struct mcount_symbols *ms) · line 147
Quoted 12 lines
---
btf_encoder.c +2
-1@@ -143,7 +143,8 @@ static int filter_functions(struct btf_elf *btfe, struct mcount_symbols *ms)/* Do not enable .init section functions. */ if (init_filter && func->addr >= ms->init_begin && - func->addr < ms->init_end) + func->addr < ms->init_end && +strncmp ("bpf_" , func->name,4 ))this looks like a very wrong way to do this? Can you please elaborate on what's missing and why it shouldn't be missing?
yes, it's just a hack, we should do something more robust as I mentioned above
it just allowed me to use iterators finaly ;-)
jirka
Re: [PATCH 2/2] btf_encoder: Change functions check due to broken dwarf
Quoted 41 lines · Jiri Olsa
Quoted 25 lines · Andrii Nakryiko
Quoted 24 lines · Jiri Olsa
Quoted 22 lines · Jiri Olsa
Quoted 20 lines · Jiri Olsa
We need to generate just single BTF instance for the function, while DWARF data contains multiple instances of
DW_TAG_subprogramtag.Unfortunately we can no longer rely on
DW_AT_declarationtag (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97060)Instead we apply following checks:
- argument names are defined for the function
- there's symbol and address defined for the function
- function is generated only onceAlso because we want to follow kernel's ftrace traceable functions, this patchset is adding extra check that the function is one of the ftrace's functions.
All ftrace functions addresses are stored in vmlinux binary within symbols:
__start_mcount_loc
__stop_mcount_locQuoted 7 lines
Quoted 2 lines
Quoted 2 lines
hum, for some reason this does not pass through bpf internal functions like
bpf_iter_bpf_map.. I learned it hard way ;-)what's the exact name of the function that was missing?
bpf_iter_bpf_mapdoesn't exist. And if it's__initfunction, why does it matter, it's not going to be even available at runtime, right?
bpf_mapiter definition:
DEFINE_BPF_ITER_FUNC(bpf_map, struct bpf_iter_meta *meta, struct bpf_map *map)goes to:
Quoted 7 lines
#define DEFINE_BPF_ITER_FUNC(target, args...) \ extern int bpf_iter_ ## target(args); \ int __init bpf_iter_ ## target(args) { return 0; }that creates
__initbpf_iter_bpf_mapfunction that will make it into BTF where it's expected when opening iterator, but the code will be freed because it's__initfunction
hm... should we just drop __init there?
Yonghong, is __init strictly necessary, or was just an optimization to save a tiny bit of space?
Quoted 30 lines
there are few iteratos functions like that, and I was going to check if there's more
Quoted 10 lines
Quoted 10 lines
Quoted 2 lines
will check
so it gets filtered out because it's
__initfunction I'll check if the fix below catches all internal functions, but I guess we should do something more robustjirka
Quoted 15 lines
btf_encoder.c static int filter_functions(struct btf_elf *btfe, struct mcount_symbols *ms) · line 147
Quoted 12 lines
---
btf_encoder.c +2
-1@@ -143,7 +143,8 @@ static int filter_functions(struct btf_elf *btfe, struct mcount_symbols *ms)/* Do not enable .init section functions. */ if (init_filter && func->addr >= ms->init_begin && - func->addr < ms->init_end) + func->addr < ms->init_end && +strncmp ("bpf_" , func->name,4 ))this looks like a very wrong way to do this? Can you please elaborate on what's missing and why it shouldn't be missing?
Quoted 4 lines
yes, it's just a hack, we should do something more robust as I mentioned above
it just allowed me to use iterators finaly ;-)
sure, I get it, I was just trying to understand why there is such a problem in the first place. Turns out we need FUNCs not just for fentry/fexit and similar, but also for bpf_iter, which is an entirely different use case (similar to raw_tp, but raw_tp is using typedef -> func_proto approach).
So I don't know, we might as well just not do mcount checks?.. As an alternative, but it's not great as well.
Quoted 3 lines
jirka
Re: [PATCH 2/2] btf_encoder: Change functions check due to broken dwarf
On Tue, Nov 03, 2020 at 11:23:33AM -0800, Andrii Nakryiko wrote:
SNIP
Quoted 7 lines
Quoted 6 lines
Quoted 1 line
on what's missing and why it shouldn't be missing?
yes, it's just a hack, we should do something more robust as I mentioned above
it just allowed me to use iterators finaly ;-)
Quoted 8 lines
sure, I get it, I was just trying to understand why there is such a problem in the first place. Turns out we need FUNCs not just for fentry/fexit and similar, but also for
bpf_iter, which is an entirely different use case (similar toraw_tp, butraw_tpis using typedef ->func_protoapproach).So I don't know, we might as well just not do mcount checks?.. As an alternative, but it's not great as well.
how about moving all such functions to separate new .init.XXX section, and pahole would make one extr acheck for that.. and it still gets freed
jirka
Re: [PATCH 2/2] btf_encoder: Change functions check due to broken dwarf
In reply to Andrii NakryikoQuoted 46 lines · Andrii Nakryiko
Quoted 45 lines · Jiri Olsa
Quoted 25 lines · Andrii Nakryiko
Quoted 24 lines · Jiri Olsa
Quoted 22 lines · Jiri Olsa
Quoted 20 lines · Jiri Olsa
We need to generate just single BTF instance for the function, while DWARF data contains multiple instances of
DW_TAG_subprogramtag.Unfortunately we can no longer rely on
DW_AT_declarationtag (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97060)Instead we apply following checks:
- argument names are defined for the function
- there's symbol and address defined for the function
- function is generated only onceAlso because we want to follow kernel's ftrace traceable functions, this patchset is adding extra check that the function is one of the ftrace's functions.
All ftrace functions addresses are stored in vmlinux binary within symbols:
__start_mcount_loc
__stop_mcount_locQuoted 7 lines
Quoted 2 lines
Quoted 2 lines
hum, for some reason this does not pass through bpf internal functions like
bpf_iter_bpf_map.. I learned it hard way ;-)what's the exact name of the function that was missing?
bpf_iter_bpf_mapdoesn't exist. And if it's__initfunction, why does it matter, it's not going to be even available at runtime, right?
bpf_mapiter definition:
DEFINE_BPF_ITER_FUNC(bpf_map, struct bpf_iter_meta *meta, struct bpf_map *map)goes to:
#define DEFINE_BPF_ITER_FUNC(target, args...) \ extern int bpf_iter_ ## target(args); \ int __init bpf_iter_ ## target(args) { return 0; }
Quoted 8 lines
Quoted 3 lines
that creates
__initbpf_iter_bpf_mapfunction that will make it into BTF where it's expected when opening iterator, but the code will be freed because it's__initfunctionhm... should we just drop
__initthere?Yonghong, is
__initstrictly necessary, or was just an optimization to save a tiny bit of space?
It is an optimization to save some space. We only need function signature, not function body, for bpf_iter.
The macro definition is in include/linux/bpf.h.
#define DEFINE_BPF_ITER_FUNC(target, args...) \
extern int bpf_iter_ ## target(args); \
int __init bpf_iter_ ## target(args) { return 0; }Maybe you could have a section, e.g., called
.init.bpf.preserve_type
which you can scan through to preserve the types.
Alternatively you can drop the above __init, the saving is indeed tiny. But this adds overhead to ksymbol lookup and may not be desirable.
Quoted 48 lines
Quoted 31 lines
there are few iteratos functions like that, and I was going to check if there's more
Quoted 10 lines
Quoted 10 lines
Quoted 2 lines
will check
so it gets filtered out because it's
__initfunction I'll check if the fix below catches all internal functions, but I guess we should do something more robustjirka
Quoted 15 lines
btf_encoder.c static int filter_functions(struct btf_elf *btfe, struct mcount_symbols *ms) · line 147
Quoted 12 lines
---
btf_encoder.c +2
-1@@ -143,7 +143,8 @@ static int filter_functions(struct btf_elf *btfe, struct mcount_symbols *ms)/* Do not enable .init section functions. */ if (init_filter && func->addr >= ms->init_begin && - func->addr < ms->init_end) + func->addr < ms->init_end && +strncmp ("bpf_" , func->name,4 ))this looks like a very wrong way to do this? Can you please elaborate on what's missing and why it shouldn't be missing?
Quoted 4 lines
yes, it's just a hack, we should do something more robust as I mentioned above
it just allowed me to use iterators finaly ;-)
sure, I get it, I was just trying to understand why there is such a problem in the first place. Turns out we need FUNCs not just for fentry/fexit and similar, but also for
bpf_iter, which is an entirely different use case (similar toraw_tp, butraw_tpis using typedef ->func_protoapproach).So I don't know, we might as well just not do mcount checks?.. As an alternative, but it's not great as well.
Quoted 3 lines
jirka
Re: [PATCH 2/2] btf_encoder: Change functions check due to broken dwarf
On Tue, Nov 03, 2020 at 12:27:56PM -0800, Yonghong Song wrote:
SNIP
Quoted 26 lines
Quoted 12 lines
Quoted 12 lines
Quoted 1 line
bpf_mapiter definition:
DEFINE_BPF_ITER_FUNC(bpf_map, struct bpf_iter_meta *meta, struct bpf_map *map)goes to:
#define DEFINE_BPF_ITER_FUNC(target, args...) \ extern int bpf_iter_ ## target(args); \ int __init bpf_iter_ ## target(args) { return 0; }Quoted 8 lines
Quoted 3 lines
that creates
__initbpf_iter_bpf_mapfunction that will make it into BTF where it's expected when opening iterator, but the code will be freed because it's__initfunctionhm... should we just drop
__initthere?Yonghong, is
__initstrictly necessary, or was just an optimization to save a tiny bit of space?It is an optimization to save some space. We only need function signature, not function body, for
bpf_iter.The macro definition is in
include/linux/bpf.h.
Quoted 7 lines
#define DEFINE_BPF_ITER_FUNC(target, args...) \ extern int bpf_iter_ ## target(args); \ int __init bpf_iter_ ## target(args) { return 0; }Maybe you could have a section, e.g., called
.init.bpf.preserve_type
which you can scan through to preserve the types.
right, sounds good, will send v3 with that
thanks,
jirka
Re: [PATCH 2/2] btf_encoder: Change functions check due to broken dwarf
In reply to Jiri OlsaQuoted 60 lines · Jiri Olsa
We need to generate just single BTF instance for the function, while DWARF data contains multiple instances of
DW_TAG_subprogramtag.Unfortunately we can no longer rely on
DW_AT_declarationtag (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97060)Instead we apply following checks:
- argument names are defined for the function
- there's symbol and address defined for the function
- function is generated only onceAlso because we want to follow kernel's ftrace traceable functions, this patchset is adding extra check that the function is one of the ftrace's functions.
All ftrace functions addresses are stored in vmlinux binary within symbols:
__start_mcount_loc
__stop_mcount_locDuring object preparation code we read those addresses, sort them and use them as filter for all detected dwarf functions.
We also filter out functions within .init section, ftrace is doing that in runtime.
I can still see several differences to ftrace functions in
/sys/kernel/debug/tracing/available_filter_functionsfile:-
available_filter_functionsincludes modules (7086 functions)
-available_filter_functionsincludes functions like:
__acpi_match_device.part.0.constprop.0 acpi_ns_check_sorted_list.constprop.0 acpi_os_unmap_generic_address.part.0 acpiphp_check_bridge.part.0which are not part of dwarf data (1164 functions)
- BTF includes multiple functions like:
__clk_register_clkdevclk_register_clkdevwhich share same code so they appear just as single function in
available_filter_functions, but dwarf keeps track of both of them (16 functions)With this change I'm getting 38334 BTF functions, which when added above functions to consideration gives same amount of functions in
available_filter_functions.The patch still keeps the original function filter condition (that uses current
fn->declarationcheck) in case the object does not contain*_mcount_locsymbol -> object is not vmlinux.Signed-off-by Jiri Olsa <[email protected]>--- btf_encoder.c | 222 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 220 insertions(+), 2 deletions(-)
[...]
btf_encoder.c line 78
Quoted 12 lines
btf_encoder.c +12
-0+static int collect_function (struct btf_elf *btfe,GElf_Sym *sym) +{ +if (elf_sym__type (sym) !=STT_FUNC ) +return 0 ; +if (!elf_sym__value (sym)) +return 0 ; + +if (functions_cnt == functions_alloc) { + functions_alloc =max (1000 , functions_alloc *3 /2 ); + functions =realloc (functions, functions_alloc *sizeof (*functions)); +if (!functions) +return -1 ;
memory leak right here. You need to use a temporary variable and check if for NULL, before overwriting functions.
Quoted 6 lines
btf_encoder.c +6
-0+ } + + functions[functions_cnt].name =elf_sym__name (sym, btfe->symtab); + functions[functions_cnt].addr =elf_sym__value (sym); + functions[functions_cnt].generated = false; + functions[functions_cnt].valid = false;
btf_encoder.c line 94
Quoted 10 lines
btf_encoder.c +10
-0+ functions_cnt++; +return 0 ; +} + +static int addrs_cmp (const void *_a,const void *_b) +{ +const unsigned long *a = _a; +const unsigned long *b = _b; + +return *a - *b;
this is cute, but is it always correct? instead of thinking how this works with overflows, maybe let's keep it simple with
if (*a == *b)
return 0;
return *a < *b ? -1 : 1;
?
Quoted 18 lines
btf_encoder.c +18
-0+} + +static int filter_functions (struct btf_elf *btfe,struct mcount_symbols *ms) +{ +bool init_filter = ms->init_begin && ms->init_end; +unsigned long *addrs, count, offset, i; +Elf_Data *data; +GElf_Shdr shdr; +Elf_Scn *sec; + +/* +* Find mcount addressed marked by __start_mcount_loc +* and __stop_mcount_loc symbols and load them into +* sorted array. +*/ + sec =elf_getscn (btfe->elf, ms->start_section); +if (!sec || !gelf_getshdr (sec, &shdr)) { + fprintf(stderr, "Failed to get section(%lu) header.\n",
btf_encoder.c line 122
Quoted 10 lines
btf_encoder.c +10
-0+ ms->start_section); +return -1 ; + } + + offset = ms->start - shdr.sh_addr; + count = (ms->stop - ms->start) /8 ; + + data =elf_getdata (sec,0 ); +if (!data) { + fprintf(stderr, "Failed to section(%lu) data.\n",
typo: failed to get?
Quoted 10 lines
btf_encoder.c +10
-0+ ms->start_section); +return -1 ; + } + + addrs =malloc (count *sizeof (addrs[0 ])); +if (!addrs) { +fprintf (stderr,"Failed to allocate memory for ftrace addresses.\n" ); +return -1 ; + } +
[...]
Quoted 5 lines
btf_encoder.c +4
-0+#define SET_SYMBOL (__sym, __var) \ + if (!ms->__var && !strcmp(__sym, elf_sym__name(sym, btfe->symtab))) \ + ms->__var = sym->st_value; \ +
btf_encoder.c static int collect_percpu_var(struct btf_elf *btfe, GElf_Sym *sym) · line 470
Quoted 10 lines
btf_encoder.c +10
-0+static void collect_mcount_symbol (GElf_Sym *sym,struct mcount_symbols *ms) +{ +if (!ms->start && + !strcmp ("__start_mcount_loc" ,elf_sym__name (sym, btfe->symtab))) { + ms->start = sym->st_value; + ms->start_section = sym->st_shndx; + } + SET_SYMBOL("__stop_mcount_loc", stop) + SET_SYMBOL("__init_begin", init_begin) +SET_SYMBOL ("__init_end" , init_end)
please don't use macro here, it doesn't save much code but complicates reading it quite significantly
Quoted 21 lines
btf_encoder.c +8
-0+} + +#undef SET_SYMBOL +static int collect_symbols (struct btf_elf *btfe,bool collect_percpu_vars) { +struct mcount_symbols ms = { };uint32_t core_id;GElf_Sym sym;@@ -320,6 +485,9 @@ static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars)elf_symtab__for_each_symbol (btfe->symtab, core_id, sym) {if (collect_percpu_vars &&collect_percpu_var (btfe, &sym))return -1 ; +if (collect_function (btfe, &sym)) +return -1 ; +collect_mcount_symbol (&sym, &ms); }if (collect_percpu_vars) {
btf_encoder.c static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars) · line 504
Quoted 8 lines
btf_encoder.c +5
-0@@ -329,9 +497,34 @@ static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars)if (btf_elf__verbose)printf ("Found %d per-CPU variables!\n" , percpu_var_cnt); } + +if (functions_cnt) { +qsort (functions, functions_cnt,sizeof (functions[0 ]), functions_cmp); +if (ms.start && ms.stop && +filter_functions (btfe, &ms)) {
nit: single line should fit well, no?
Quoted 10 lines
btf_encoder.c +7
-0+fprintf (stderr,"Failed to filter dwarf functions\n" ); +return -1 ; + } +if (btf_elf__verbose) +printf ("Found %d functions!\n" , functions_valid); + } +return 0 ; }
[...]
Re: [PATCH 2/2] btf_encoder: Change functions check due to broken dwarf
On Tue, Nov 03, 2020 at 10:55:58AM -0800, Andrii Nakryiko wrote:
SNIP
Quoted 34 lines
Quoted 31 lines
I can still see several differences to ftrace functions in
/sys/kernel/debug/tracing/available_filter_functionsfile:-
available_filter_functionsincludes modules (7086 functions)
-available_filter_functionsincludes functions like:
__acpi_match_device.part.0.constprop.0 acpi_ns_check_sorted_list.constprop.0 acpi_os_unmap_generic_address.part.0 acpiphp_check_bridge.part.0which are not part of dwarf data (1164 functions)
- BTF includes multiple functions like:
__clk_register_clkdevclk_register_clkdevwhich share same code so they appear just as single function in
available_filter_functions, but dwarf keeps track of both of them (16 functions)With this change I'm getting 38334 BTF functions, which when added above functions to consideration gives same amount of functions in
available_filter_functions.The patch still keeps the original function filter condition (that uses current
fn->declarationcheck) in case the object does not contain*_mcount_locsymbol -> object is not vmlinux.Signed-off-by Jiri Olsa <[email protected]>--- btf_encoder.c | 222 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 220 insertions(+), 2 deletions(-)[...]
Quoted 12 lines
btf_encoder.c line 78
Quoted 12 lines
btf_encoder.c +12
-0+static int collect_function (struct btf_elf *btfe,GElf_Sym *sym) +{ +if (elf_sym__type (sym) !=STT_FUNC ) +return 0 ; +if (!elf_sym__value (sym)) +return 0 ; + +if (functions_cnt == functions_alloc) { + functions_alloc =max (1000 , functions_alloc *3 /2 ); + functions =realloc (functions, functions_alloc *sizeof (*functions)); +if (!functions) +return -1 ;
ok, I thought that if we go down I don't need to,
but I did not check how pahole is handling this,
probably free everything
Quoted 24 lines
memory leak right here. You need to use a temporary variable and check if for NULL, before overwriting functions.
Quoted 6 lines
btf_encoder.c +6
-0+ } + + functions[functions_cnt].name =elf_sym__name (sym, btfe->symtab); + functions[functions_cnt].addr =elf_sym__value (sym); + functions[functions_cnt].generated = false; + functions[functions_cnt].valid = false;btf_encoder.c line 94
Quoted 10 lines
btf_encoder.c +10
-0+ functions_cnt++; +return 0 ; +} + +static int addrs_cmp (const void *_a,const void *_b) +{ +const unsigned long *a = _a; +const unsigned long *b = _b; + +return *a - *b;this is cute, but is it always correct? instead of thinking how this works with overflows, maybe let's keep it simple with
Quoted 3 lines
if (*a == *b)
return 0;
return *a < *b ? -1 : 1;
sure, will fix
Quoted 25 lines
?
Quoted 22 lines
btf_encoder.c +22
-0+} + +static int filter_functions (struct btf_elf *btfe,struct mcount_symbols *ms) +{ +bool init_filter = ms->init_begin && ms->init_end; +unsigned long *addrs, count, offset, i; +Elf_Data *data; +GElf_Shdr shdr; +Elf_Scn *sec; + +/* +* Find mcount addressed marked by __start_mcount_loc +* and __stop_mcount_loc symbols and load them into +* sorted array. +*/ + sec =elf_getscn (btfe->elf, ms->start_section); +if (!sec || !gelf_getshdr (sec, &shdr)) { +fprintf (stderr,"Failed to get section(%lu) header.\n" , + ms->start_section); +return -1 ; + } +
Quoted 8 lines
btf_encoder.c line 122
Quoted 6 lines
btf_encoder.c +6
-0+ offset = ms->start - shdr.sh_addr; + count = (ms->stop - ms->start) /8 ; + + data =elf_getdata (sec,0 ); +if (!data) { + fprintf(stderr, "Failed to section(%lu) data.\n",typo: failed to get?
yep
Quoted 24 lines
Quoted 11 lines
btf_encoder.c +10
-0+ ms->start_section); +return -1 ; + } + + addrs =malloc (count *sizeof (addrs[0 ])); +if (!addrs) { +fprintf (stderr,"Failed to allocate memory for ftrace addresses.\n" ); +return -1 ; + } +[...]
Quoted 10 lines
btf_encoder.c +9
-0+#define SET_SYMBOL (__sym, __var) \ + if (!ms->__var && !strcmp(__sym, elf_sym__name(sym, btfe->symtab))) \ + ms->__var = sym->st_value; \ + +static void collect_mcount_symbol (GElf_Sym *sym,struct mcount_symbols *ms) +{ +if (!ms->start && + !strcmp ("__start_mcount_loc" ,elf_sym__name (sym, btfe->symtab))) { + ms->start = sym->st_value;
Quoted 8 lines
btf_encoder.c static int collect_percpu_var(struct btf_elf *btfe, GElf_Sym *sym) · line 470
Quoted 5 lines
btf_encoder.c +5
-0+ ms->start_section = sym->st_shndx; + } + SET_SYMBOL("__stop_mcount_loc", stop) + SET_SYMBOL("__init_begin", init_begin) +SET_SYMBOL ("__init_end" , init_end)please don't use macro here, it doesn't save much code but complicates reading it quite significantly
ok
Quoted 20 lines
Quoted 20 lines
btf_encoder.c +8
-0+} + +#undef SET_SYMBOL +static int collect_symbols (struct btf_elf *btfe,bool collect_percpu_vars) { +struct mcount_symbols ms = { };uint32_t core_id;GElf_Sym sym;@@ -320,6 +485,9 @@ static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars)elf_symtab__for_each_symbol (btfe->symtab, core_id, sym) {if (collect_percpu_vars &&collect_percpu_var (btfe, &sym))return -1 ; +if (collect_function (btfe, &sym)) +return -1 ; +collect_mcount_symbol (&sym, &ms); }
Quoted 12 lines
btf_encoder.c static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars) · line 504
Quoted 10 lines
btf_encoder.c +5
-0if (collect_percpu_vars) {@@ -329,9 +497,34 @@ static int collect_symbols(struct btf_elf *btfe, bool collect_percpu_vars)if (btf_elf__verbose)printf ("Found %d per-CPU variables!\n" , percpu_var_cnt); } + +if (functions_cnt) { +qsort (functions, functions_cnt,sizeof (functions[0 ]), functions_cmp); +if (ms.start && ms.stop && +filter_functions (btfe, &ms)) {nit: single line should fit well, no?
ook
thanks,
jirka