pahole: Encode true signatures in kernel BTF
Yonghong Song
reviewerv10 cover pahole: Encode true signatures in kernel BTF
Current vmlinux BTF encoding is based on the source level signatures. But the compiler may do some optimization and changed the signature. If the user tried with source level signature, their initial implementation may have wrong results and then the user need to check what is the problem and work around it, e.g. through kprobe since kprobe does not need vmlinux BTF.
Majority of changed signatures are due to dead argument elimination. The following is a more complex one. The original source signature:
typedef struct {
union {
void *kernel;
void __user *user;
};
bool is_kernel : 1; } sockptr_t;
typedef sockptr_t bpfptr_t;
static int map_create(union bpf_attr *attr, bpfptr_t uattr) { ... }
After compiler optimization, the signature becomes:
static int map_create(union bpf_attr *attr, bool uattr__is_kernel) { ... }
In the above, uattr__is_kernel corresponds to 'is_kernel' field in sockptr_t. This makes it easier for developers to understand what changed.
The new signature needs to properly follow ABI specification based on locations. Otherwise, that signature should be discarded. For example,
0x0242f1f7: DW_TAG_subprogram
DW_AT_name ("memblock_find_in_range")
DW_AT_calling_convention (DW_CC_nocall)
DW_AT_type (0x0242decc "phys_addr_t")
...
0x0242f22e: DW_TAG_formal_parameter
DW_AT_location (indexed (0x14a) loclist = 0x005595bc:
[0xffffffff87a000f9, 0xffffffff87a00178): DW_OP_reg5 RDI
[0xffffffff87a00178, 0xffffffff87a001be): DW_OP_reg14 R14
[0xffffffff87a001be, 0xffffffff87a001c7): DW_OP_entry_value(DW_OP_reg5 RDI), DW_OP_stack_value
[0xffffffff87a001c7, 0xffffffff87a00214): DW_OP_reg14 R14)
DW_AT_name ("start")
DW_AT_type (0x0242decc "phys_addr_t")
...
0x0242f239: DW_TAG_formal_parameter
DW_AT_location (indexed (0x14b) loclist = 0x005595e6:
[0xffffffff87a000f9, 0xffffffff87a00175): DW_OP_reg4 RSI
[0xffffffff87a00175, 0xffffffff87a001b8): DW_OP_reg3 RBX
[0xffffffff87a001b8, 0xffffffff87a001c7): DW_OP_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value
[0xffffffff87a001c7, 0xffffffff87a00214): DW_OP_reg3 RBX)
DW_AT_name ("end")
DW_AT_type (0x0242decc "phys_addr_t")
...
0x0242f245: DW_TAG_formal_parameter
DW_AT_location (indexed (0x14c) loclist = 0x00559610:
[0xffffffff87a001e3, 0xffffffff87a001ef): DW_OP_breg4 RSI+0)
DW_AT_name ("size")
DW_AT_type (0x0242decc "phys_addr_t")
...
0x0242f250: DW_TAG_formal_parameter
DW_AT_const_value (4096)
DW_AT_name ("align")
DW_AT_type (0x0242decc "phys_addr_t")
...The third argument should correspond to RDX for x86_64. But the location suggests that the parameter value is stored in the address with 'RSI + 0'. It is not clear whether the parameter value is stored in RDX or not. So we have to discard this funciton in vmlinux BTF to avoid incorrect true signatures.
For llvm, any function having
DW_AT_calling_convention (DW_CC_nocall)
in dwarf DW_TAG_subprogram will indicate that this function has signature changed. But for non DW_CC_nocall functions, it is possible that true signature still not available due to locations. So every functions will be checked.
I did experiment with latest bpf-next. For x86_64, there are 69103 kernel functions and 875 kernel functions having signature changed. A series of patches are intended to ensure true signatures are properly represented. Eventually, only 20 functions cannot have true signatures due to locations.
For arm64, there are 863 kernel functions having signature changed, and 108 functions cannot have true signatures due to locations. I checked those functions and look like llvm arm64 backend more relaxed to compute parameter values.
For full testing, I enabled true signature support in kernel scripts/Makefile.btf like below:
-pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes +pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes --btf_features=+true_signature
See individual patches for details.
Changelog:
v9 -> v10:
- v9: https://lore.kernel.org/bpf/[email protected]/
- Remove patch 'btf_encoder: Avoid comparing inlined and out-of-line function prototypes' as it caused btf_functions.sh failure.
v8 -> v9:
- v8: https://lore.kernel.org/bpf/[email protected]/
- v8 made a mistake where all functions go through main loop in function__analyze_parameter_locations(). This is not correct as it includes functions whose signature is not changed. See function function__match_clang_parameter_locations().
- Add a change in btf_encoder to avoid comparing inlined and out-of-line function prototypes. This allows some functions to be in btf.
- Add one more test, clang_parm_optimized_stack_2, where a global function, with many unused parameters, gets properly handling in location checking.
v7 -> v8:
- v7: https://lore.kernel.org/bpf/[email protected]/
- Both Check and Analyze phases go through all functions.
- Require true_signature in btf_encoder to have name like <paramName>__<field>.
- Remove signature_changed (from nocall). It is possible that function signatures are not changed but the location reigsters do not match.
v6 -> v7:
- v6: https://lore.kernel.org/bpf/[email protected]/
- Ensure that 'collect' and 'analyze' have the same location checking.
- In 'analyze' stage, undo true_sig_member_name if the next expected register matches the previous source type although the previous parameter may
only use half of the value.
v5 -> v6:
- v5: https://lore.kernel.org/bpf/[email protected]/
- The previous change relies on parameter__new() to collect and analyze each parameter to decide true signatures. The new one separates collecting and analyzing phase from Alan. This two-phase makes logic easy to understand.
- In btf_encoder.c, remove usage of skip_idx to simplify the code.
v4 -> v5:
- v4: https://lore.kernel.org/bpf/[email protected]/
- Check info.signature_changed only under clang.
- Fix an uninitialized varable issue (var reg_dix) for gcc.
v3 -> v4:
- v3: https://lore.kernel.org/bpf/[email protected]/
- Add simple prescan of parameter registers in order to get true signatures for those functions where optimization could happen but compiler didn't do it.
- Do not create a new name (e.g. "uattr__is_kernel") with malloc at parameter_reg() stage. Instead remember both "uattr" and "is_kernel" and later generate the name "uattr_is_kernel" in btf encoder.
- Add comments to explain how to handle parameters which may take two registers.
- Fix some test failures on aarch64.
v2 -> v3:
- v2: https://lore.kernel.org/bpf/[email protected]/
- Change tests by using newly added test_lib.sh.
- Simplify to get bool variable producer_clang.
- Try to avoid producer_clang appearance in dwarf_loader.c in order to avoid clear separation between clang and gcc.
v1 -> v2:
- v1: https://lore.kernel.org/bpf/[email protected]/
- Added producer_clang guarding in btf_encoder. Otherwise, gcc kernel build will crash pahole.
- Fix an early return in parameter__reg() which didn't do pthread_mutex_unlock() which caused the deadlock for arm64.
- Add a few more places to guard with producer_clang and conf->true_signature to maintain the previous behavior if not clang or conf->true_signature is false.
Yonghong Song (5):
dwarf_loader: Detect aggregate ABI register usage and signature
changes
dwarf_loader: Collect per-parameter information
dwarf_loader: Analyze per-parameter information for true signatures
btf_encoder: Emit true function signatures
tests: Add BTF true_signature encoding tests
btf_encoder.c | 26 +- dwarf_loader.c | 603 +++++++++++++++++++++++--- dwarves.h | 14 + tests/clang_parm_aggregate_1.sh | 85 ++++ tests/clang_parm_aggregate_2.sh | 88 ++++ tests/clang_parm_memory.sh | 77 ++++ tests/clang_parm_optimized.sh | 63 +++ tests/clang_parm_optimized_stack_1.sh | 63 +++ tests/clang_parm_optimized_stack_2.sh | 63 +++ 9 files changed, 1017 insertions(+), 65 deletions(-) create mode 100755 tests/clang_parm_aggregate_1.sh create mode 100755 tests/clang_parm_aggregate_2.sh create mode 100755 tests/clang_parm_memory.sh create mode 100755 tests/clang_parm_optimized.sh create mode 100755 tests/clang_parm_optimized_stack_1.sh create mode 100755 tests/clang_parm_optimized_stack_2.sh
Signature · 1 line
2.53.0-Meta
Yonghong Song
reviewerPATCH v10 1/5 dwarf_loader: Detect aggregate ABI register usage and signature changes
Aggregate ABI register usage applies for both clang and gcc. The signature change detection is clang only.
--- dwarf_loader.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++---- dwarves.h | 3 +++ 2 files changed, 61 insertions(+), 4 deletions(-)
dwarf_loader.c +58 -4
@@ -1137,6 +1137,16 @@ static void arch__set_register_params(const GElf_Ehdr *ehdr, struct cu *cu) } } +static bool arch__agg_use_two_regs (const GElf_Ehdr *ehdr) +{ +switch (ehdr->e_machine) { +case EM_S390 : +return false; +default : +return true; + } +} +static struct template_type_param *template_type_param__new (Dwarf_Die *die,struct cu *cu,struct conf_load *conf) {struct template_type_param *ttparm =tag__alloc (cu,sizeof (*ttparm));
@@ -1539,6 +1549,29 @@ static struct ftype *ftype__new(Dwarf_Die *die, struct cu *cu)return ftype; } +static bool function__signature_changed (struct function *func,Dwarf_Die *die) +{ +/* The inlined DW_TAG_subprogram typically has the original source type for +* abstract origin of a concrete function with address range, inlined subroutine, +* or call site. +*/ +if (func->inlined) +return false; + +if (!func->abstract_origin) +return attr_numeric (die, DW_AT_calling_convention) == DW_CC_nocall; + +Dwarf_Attribute attr; +if (dwarf_attr (die, DW_AT_abstract_origin, &attr)) { +Dwarf_Die origin; +if (dwarf_formref_die (&attr, &origin)) +return attr_numeric (&origin, DW_AT_calling_convention) == DW_CC_nocall; + } + +/* This should not happen */ +return false; +} +static struct function *function__new (Dwarf_Die *die,struct cu *cu,struct conf_load *conf) {struct function *func =tag__alloc (cu,sizeof (*func));
@@ -2491,10 +2524,17 @@ static struct tag *die__create_new_function(Dwarf_Die *die, struct cu *cu, struc {struct function *function =function__new (die, cu, conf); -if (function !=NULL && -die__process_function (die, &function->proto, &function->lexblock, cu, conf) !=0 ) { -function__delete (function, cu); - function =NULL ; +if (function !=NULL ) { +/* For clang, we determine if function signature changes via DW_AT_calling_convention +* set to DW_CC_nocall. +*/ +if (cu->producer_clang) + function->proto.signature_changed =function__signature_changed (function, die); + +if (die__process_function (die, &function->proto, &function->lexblock, cu, conf) !=0 ) { +function__delete (function, cu); + function =NULL ; + } } if (function != NULL &&
@@ -3154,6 +3194,17 @@ static unsigned long long dwarf_tag__orig_id(const struct tag *tag,return cu->extra_dbg_info ? dtag->id :0 ; } +static bool attr_producer_clang (Dwarf_Die *die) +{ +const char *producer; + + producer =attr_string (die, DW_AT_producer,NULL ); +if (!producer) +return false; + +return !!strstr (producer,"clang" ); +} +struct debug_fmt_ops dwarf__ops;static int die__process (Dwarf_Die *die,struct cu *cu,struct conf_load *conf)
@@ -3191,6 +3242,7 @@ static int die__process(Dwarf_Die *die, struct cu *cu, struct conf_load *conf) } cu->language =attr_numeric (die, DW_AT_language); + cu->producer_clang =attr_producer_clang (die);if (conf->early_cu_filter) cu = conf->early_cu_filter (cu);
@@ -3409,6 +3461,7 @@ static int cu__set_common(struct cu *cu, struct conf_load *conf, cu->little_endian = ehdr.e_ident[EI_DATA ] ==ELFDATA2LSB ; cu->nr_register_params =arch__nr_register_params (&ehdr); + cu->agg_use_two_regs =arch__agg_use_two_regs (&ehdr);arch__set_register_params (&ehdr, cu);return 0 ; }
@@ -3950,6 +4003,7 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf, cu->priv = dcu; cu->dfops = &dwarf__ops; cu->language =attr_numeric (cu_die, DW_AT_language); + cu->producer_clang =attr_producer_clang (cu_die);cus__add (cus, cu); }
dwarves.h +3 -0
@@ -306,6 +306,8 @@ struct cu { uint8_t has_addr_info:1; uint8_t uses_global_strings:1; uint8_t little_endian:1; + uint8_t producer_clang:1; + uint8_t agg_use_two_regs:1; /* An aggregate like {long a; long b;} */uint8_t nr_register_params;int register_params[ARCH_MAX_REGISTER_PARAMS ];int functions_saved;
@@ -1030,6 +1032,7 @@ struct ftype { uint8_t inconsistent_proto:1; uint8_t uncertain_parm_loc:1; uint8_t reordered_parm:1; + uint8_t signature_changed:1;struct list_head template_type_params;struct list_head template_value_params;struct template_parameter_pack *template_parameter_pack;
Signature · 1 line
2.53.0-Meta
Yonghong Song
reviewerPATCH v10 2/5 dwarf_loader: Collect per-parameter information
In reply to Yonghong SongScan all parameters and save necessary information in struct parameter and such information will be used in the next patch for analysis.
The collected per-parameter information includes
- whether the parameter is const value or not
- whether the parameter is a DW_OP_fbreg (location stack) or not
- the location register for this parameter
- the type byte size for this parameter (from parameter type)
- whether the parameter is passed in memory
- whether the parameter needs to two registers
- If the source parameter needs 2 registers but the actual parameter (after optimization) only needs 1 register and only one field is used, record true_sig_member name and type.
Such information is also propagated to abstract-origin parameters in ftype__recode_dwarf_types().
parameter__new() now only decodes this location state; the optimized and unexpected_reg decisions that parameter__reg() used to drive are made by the function-level analysis pass added in the next commit, which consumes the decoded fields.
--- dwarf_loader.c | 344 +++++++++++++++++++++++++++++++++++++++++-------- dwarves.h | 11 ++ 2 files changed, 298 insertions(+), 57 deletions(-)
dwarf_loader.c +287 -57
@@ -1237,14 +1237,231 @@ static ptrdiff_t __dwarf_getlocations(Dwarf_Attribute *attr,return ret; } -/* For DW_AT_location 'attr': -* - if first location is DW_OP_regXX with expected number, return the register; -* otherwise save the register for later return -* - if location DW_OP_entry_value(DW_OP_regXX) with expected number is in the -* list, return the register; otherwise save register for later return -* - otherwise if no register was found for locations, return -1. +#define PARAMETER_UNKNOWN_REG -1 + +static int __get_type_byte_size (Dwarf_Die *die,struct cu *cu) +{ +Dwarf_Attribute attr; +if (dwarf_attr (die, DW_AT_type, &attr) ==NULL ) +return 0 ; + +Dwarf_Die type_die; +if (dwarf_formref_die (&attr, &type_die) ==NULL ) +return 0 ; + +/* A type does not have byte_size. +* 0x000dac83: DW_TAG_formal_parameter +DW_AT_location (indexed (0x385) loclist = 0x00016175: +[0xffff800080098cb0, 0xffff800080098cb4): DW_OP_breg8 W8+0 +[0xffff800080098cb4, 0xffff800080098ff4): DW_OP_breg31 WSP+16, DW_OP_deref +[0xffff800080099054, 0xffff80008009908c): DW_OP_breg31 WSP+16, DW_OP_deref) +DW_AT_name ("ubuf") +DW_AT_decl_file ("/home/yhs/work/bpf-next/arch/arm64/kernel/ptrace.c") +DW_AT_decl_line (886) +DW_AT_type (0x000d467e "const void *") + +* 0x000d467e: DW_TAG_pointer_type +DW_AT_type (0x000c4320 "const void") + +* 0x000c4320: DW_TAG_const_type +*/ +if (dwarf_tag (&type_die) == DW_TAG_pointer_type) +return cu->addr_size; + +uint64_t bsize =attr_numeric (&type_die, DW_AT_byte_size); +if (bsize ==0 ) +return __get_type_byte_size (&type_die, cu); + +return bsize; +} + +static int get_type_byte_size (Dwarf_Die *die,struct cu *cu) +{ +int byte_size =0 ; + +Dwarf_Attribute attr; +if (dwarf_attr (die, DW_AT_abstract_origin, &attr)) { +Dwarf_Die origin; +if (dwarf_formref_die (&attr, &origin)) + byte_size =__get_type_byte_size (&origin, cu); + }else { + byte_size =__get_type_byte_size (die, cu); + } +return byte_size; +} + +/* Traverse the parameter type until finding the member type which has expected +* struct type offset. */ -static int parameter__reg (Dwarf_Attribute *attr,int expected_reg) +static Dwarf_Die *get_member_with_offset (Dwarf_Die *die,int offset,Dwarf_Die *member_die) +{ +Dwarf_Attribute attr; +if (dwarf_attr (die, DW_AT_type, &attr) ==NULL ) +return NULL ; + +Dwarf_Die type_die; +if (dwarf_formref_die (&attr, &type_die) ==NULL ) +return NULL ; + +uint64_t bsize =attr_numeric (&type_die, DW_AT_byte_size); +if (bsize ==0 ) +return get_member_with_offset (&type_die, offset, member_die); + +if (dwarf_tag (&type_die) != DW_TAG_structure_type) +return NULL ; + +if (!dwarf_haschildren (&type_die) ||dwarf_child (&type_die, member_die) !=0 ) +return NULL ; +do { +if (dwarf_tag (member_die) != DW_TAG_member) +continue ; + +Dwarf_Attribute attr; +Dwarf_Off bit_offset; + +if (dwarf_attr (member_die, DW_AT_data_bit_offset, &attr) !=NULL ) + bit_offset =__attr_offset (&attr); +else if (dwarf_attr (member_die, DW_AT_data_member_location, &attr) !=NULL ) + bit_offset =__attr_offset (&attr) *8 ; +else +continue ; + +if (bit_offset == offset *8 ) +return member_die; + }while (dwarf_siblingof (member_die, member_die) ==0 ); + +return NULL ; +} + +static bool dwarf_op__is_reg (unsigned int atom) +{ +return atom >= DW_OP_reg0 && atom <= DW_OP_reg31; +} + +static bool dwarf_expr__has_stack_value (Dwarf_Op *expr,size_t exprlen) +{ +for (size_t i =1 ; i < exprlen; i++) { +if (expr[i].atom == DW_OP_stack_value) +return true; + } +return false; +} + +static void parameter__set_loc_reg (struct parameter *parm,int reg) +{ +if (parm->loc_reg ==PARAMETER_UNKNOWN_REG ) + parm->loc_reg = reg; +} + +static void parameter__set_field_bit (unsigned long *fields,int byte_offset) +{ +if (byte_offset >=0 && byte_offset < (int )(sizeof (*fields) *8 )) + *fields |=1UL << byte_offset; +} + +static void parameter__record_true_sig_member (struct parameter *parm,Dwarf_Die *die, +int field_offset,struct conf_load *conf) +{ +Dwarf_Die member_die; + +if (parm->true_sig_member_name) +return ; +if (!parm->name) +return ; +if (!get_member_with_offset (die, field_offset, &member_die)) +return ; + + parm->true_sig_member_name =attr_string (&member_die, DW_AT_name, conf); +if (!parm->true_sig_member_name) +return ; + + parm->true_sig_type_from_types =attr_type (&member_die, DW_AT_type, &parm->true_sig_type); +if (parm->true_sig_type ==0 ) + parm->true_sig_member_name =NULL ; +} + +static void parameter__finish_piece_decode (struct parameter *parm,Dwarf_Die *die, +struct conf_load *conf,struct cu *cu) +{ +unsigned long first = parm->first_reg_fields; +unsigned long second = parm->second_reg_fields; +int field_offset; + +if (!first && !second) +return ; +if (first && second) +return ; +if (__builtin_popcountl (first) >=2 ||__builtin_popcountl (second) >=2 ) +return ; + +if (__builtin_popcountl (first) ==1 ) + field_offset =__builtin_ctzl (first); +else + field_offset = cu->addr_size +__builtin_ctzl (second); + +parameter__record_true_sig_member (parm, die, field_offset, conf); +} + +/* For aggregate parameters represented by pieces, first_reg_fields and +* second_reg_fields record the byte offsets materialized in each ABI register. +* The later function-level pass decides whether the source aggregate is still +* ABI-preserved or should be replaced by the single used member candidate. +*/ +static void parameter__multi_exprs (Dwarf_Op *expr,int loc_num,struct cu *cu, +size_t exprlen,struct parameter *parm) +{ +switch (expr[0 ].atom) { + case DW_OP_lit0 ... DW_OP_lit31: +case DW_OP_constu: +case DW_OP_consts: +if (loc_num ==0 ) + parm->loc_const_value =1 ; +return ; + } + +if (parm->type_byte_size <= cu->addr_size || !cu->agg_use_two_regs) { +switch (expr[0 ].atom) { + case DW_OP_reg0 ... DW_OP_reg31: +if (loc_num ==0 ) +parameter__set_loc_reg (parm, expr[0 ].atom); +return ; + case DW_OP_breg0 ... DW_OP_breg31: +if (loc_num ==0 &&dwarf_expr__has_stack_value (expr, exprlen)) +parameter__set_loc_reg (parm, expr[0 ].atom - DW_OP_breg0 + DW_OP_reg0); +return ; +default : +return ; + } + } + +int off =0 ; +for (size_t i =0 ; i < exprlen; i++) { +if (expr[i].atom == DW_OP_piece) { +int num = expr[i].number; + +if (i ==0 ) { + off = num; +continue ; + } + +if (off < cu->addr_size) +parameter__set_field_bit (&parm->first_reg_fields, off); +else +parameter__set_field_bit (&parm->second_reg_fields, off - cu->addr_size); + off += num; + }else if (dwarf_op__is_reg (expr[i].atom)) { +if (off < cu->addr_size || parm->loc_reg ==PARAMETER_UNKNOWN_REG ) +parameter__set_loc_reg (parm, expr[i].atom); + } +/* FIXME: not handling DW_OP_bregX pieces yet since we do not +* have a use case for it yet in the Linux kernel. +*/ + } +} + +static void parameter__decode_location (Dwarf_Attribute *attr,struct conf_load *conf, +struct cu *cu,Dwarf_Die *die, +struct parameter *parm) {Dwarf_Addr base, start, end;Dwarf_Op *expr, *entry_ops;
@@ -1252,66 +1469,76 @@ static int parameter__reg(Dwarf_Attribute *attr, int expected_reg)size_t exprlen, entry_len;ptrdiff_t offset =0 ;int loc_num =-1 ; -int ret =-1 ; -/* use libdw__lock as dwarf_getlocation(s) has concurrency issues -* when libdw is not compiled with experimental --enable-thread-safety -*/ pthread_mutex_lock (&libdw__lock);while ((offset =__dwarf_getlocations (attr, offset, &base, &start, &end, &expr, &exprlen)) >0 ) { +bool had_stack_value; + loc_num++; +if (exprlen ==0 ) +continue ; -/* Convert expression list (XX DW_OP_stack_value) -> (XX). -* DW_OP_stack_value instructs interpreter to pop current value from -* DWARF expression evaluation stack, and thus is not important here. -*/ -if (exprlen >1 && expr[exprlen -1 ].atom == DW_OP_stack_value) + had_stack_value = expr[exprlen -1 ].atom == DW_OP_stack_value; +if (exprlen ==2 && had_stack_value) exprlen--; -if (exprlen !=1 ) +if (exprlen !=1 ) { +parameter__multi_exprs (expr, loc_num, cu, exprlen, parm);continue ; + }switch (expr->atom) { -/* match DW_OP_regXX at first location */ case DW_OP_reg0 ... DW_OP_reg31: -if (loc_num !=0 ) -break ; - ret = expr->atom; -if (ret == expected_reg) - goto out; +if (loc_num ==0 ) +parameter__set_loc_reg (parm, expr->atom); +break ; + case DW_OP_breg0 ... DW_OP_breg31: +if (loc_num ==0 && had_stack_value) +parameter__set_loc_reg (parm, expr->atom - DW_OP_breg0 + DW_OP_reg0); +break ; +case DW_OP_fbreg: +if (loc_num ==0 ) + parm->loc_stack =1 ; +break ; + case DW_OP_lit0 ... DW_OP_lit31: +case DW_OP_constu: +case DW_OP_consts: +if (loc_num ==0 ) + parm->loc_const_value =1 ;break ; -/* match DW_OP_entry_value(DW_OP_regXX) at any location */ case DW_OP_entry_value:case DW_OP_GNU_entry_value:if (dwarf_getlocation_attr (attr, expr, &entry_attr) ==0 &&dwarf_getlocation (&entry_attr, &entry_ops, &entry_len) ==0 && - entry_len ==1 ) { - ret = entry_ops->atom; -if (ret == expected_reg) - goto out; - } + entry_len ==1 &&dwarf_op__is_reg (entry_ops->atom)) +parameter__set_loc_reg (parm, entry_ops->atom);break ; } } -out:pthread_mutex_unlock (&libdw__lock); -return ret; + +parameter__finish_piece_decode (parm, die, conf, cu); } -static struct parameter *parameter__new (Dwarf_Die *die,struct cu *cu, -struct conf_load *conf,int param_idx) +static struct parameter *parameter__new (Dwarf_Die *die,struct cu *cu,struct conf_load *conf, +struct ftype *ftype,int param_idx) {struct parameter *parm =tag__alloc (cu,sizeof (*parm));if (parm !=NULL ) { -bool has_const_value;Dwarf_Attribute attr;tag__init (&parm->tag, cu, die); parm->name =attr_string (die, DW_AT_name, conf); parm->idx = param_idx; -if (param_idx >= cu->nr_register_params || param_idx <0 ) + parm->loc_reg =PARAMETER_UNKNOWN_REG ; +if (!ftype)return parm; + + parm->type_byte_size =get_type_byte_size (die, cu); + parm->passed_in_memory = parm->type_byte_size > + (cu->agg_use_two_regs ?2 * cu->addr_size : cu->addr_size); +/* Parameters which use DW_AT_abstract_origin to point at * the original parameter definition (with no name in the DIE) * are the result of later DWARF generation during compilation
@@ -1345,26 +1572,10 @@ static struct parameter *parameter__new(Dwarf_Die *die, struct cu *cu,* between these parameter representations. See * ftype__recode_dwarf_types() below for how this is handled. */ - has_const_value =dwarf_attr (die, DW_AT_const_value, &attr) !=NULL ; + parm->has_const_value =dwarf_attr (die, DW_AT_const_value, &attr) !=NULL ; parm->has_loc =dwarf_attr (die, DW_AT_location, &attr) !=NULL ; - -if (parm->has_loc) { -int expected_reg = cu->register_params[param_idx]; -int actual_reg =parameter__reg (&attr, expected_reg); - -if (actual_reg <0 ) - parm->optimized =1 ; -else if (expected_reg >=0 && expected_reg != actual_reg) -/* mark parameters that use an unexpected -* register to hold a parameter; these will -* be problematic for users of BTF as they -* violate expectations about register -* contents. -*/ - parm->unexpected_reg =1 ; - }else if (has_const_value) { - parm->optimized =1 ; - } +if (parm->has_loc) +parameter__decode_location (&attr, conf, cu, die, parm); }return parm;
@@ -1384,7 +1595,7 @@ static int formal_parameter_pack__load_params(struct formal_parameter_pack *packcontinue ; } -struct parameter *param =parameter__new (die, cu, conf,-1 ); +struct parameter *param =parameter__new (die, cu, conf,NULL ,-1 );if (param ==NULL )return -1 ;
@@ -1928,7 +2139,7 @@ static struct tag *die__create_new_parameter(Dwarf_Die *die, struct cu *cu, struct conf_load *conf, int param_idx) { - struct parameter *parm = parameter__new(die, cu, conf, param_idx); + struct parameter *parm = parameter__new(die, cu, conf, ftype, param_idx);if (parm ==NULL )return NULL ;
@@ -2249,7 +2460,7 @@ out_enomem: }static int die__process_function (Dwarf_Die *die,struct ftype *ftype, -struct lexblock *lexblock,struct cu *cu,struct conf_load *conf); +struct lexblock *lexblock,struct cu *cu,struct conf_load *conf);static int die__create_new_lexblock (Dwarf_Die *die,struct cu *cu,struct lexblock *father,struct conf_load *conf)
@@ -2796,6 +3007,25 @@ static void ftype__recode_dwarf_types(struct tag *tag, struct cu *cu)*/ if (pos->has_loc) opos->has_loc = pos->has_loc; +if (pos->has_const_value) + opos->has_const_value = pos->has_const_value; +if (pos->loc_const_value) + opos->loc_const_value = pos->loc_const_value; +if (pos->loc_stack) + opos->loc_stack = pos->loc_stack; +if (pos->loc_reg !=PARAMETER_UNKNOWN_REG ) + opos->loc_reg = pos->loc_reg; +if (pos->type_byte_size !=0 ) + opos->type_byte_size = pos->type_byte_size; +if (pos->passed_in_memory) + opos->passed_in_memory = pos->passed_in_memory; + opos->first_reg_fields |= pos->first_reg_fields; + opos->second_reg_fields |= pos->second_reg_fields; +if (pos->true_sig_member_name && !opos->true_sig_member_name) { + opos->true_sig_member_name = pos->true_sig_member_name; + opos->true_sig_type = pos->true_sig_type; + opos->true_sig_type_from_types = pos->true_sig_type_from_types; + }if (pos->optimized) opos->optimized = pos->optimized;
dwarves.h +11 -0
@@ -948,9 +948,20 @@ size_t lexblock__fprintf(const struct lexblock *lexblock, const struct cu *cu,struct parameter {struct tag tag;const char *name; +const char *true_sig_member_name; +Dwarf_Off true_sig_type; +unsigned long first_reg_fields; +unsigned long second_reg_fields; +int loc_reg; +uint16_t type_byte_size; +uint8_t true_sig_type_from_types:1 ; +uint8_t has_const_value:1 ; +uint8_t loc_const_value:1 ; +uint8_t loc_stack:1 ;uint8_t optimized:1 ;uint8_t unexpected_reg:1 ;uint8_t has_loc:1 ; +uint8_t passed_in_memory:1 ;/* too large for the ABI argument registers */ uint8_t idx; };
Signature · 1 line
2.53.0-Meta
Yonghong Song
reviewerPATCH v10 3/5 dwarf_loader: Analyze per-parameter information for true signatures
In reply to Yonghong SongAdd a function-level pass, function__analyze_parameter_locations(), run from cu__resolve_func_ret_types_optimized() which walks a function's parameters in ABI argument-register order and consumes the location state decoded by parameter__decode_location() in the previous commit. Each parameter advances the expected-register index by the number of argument registers it occupies (parameter__abi_slots(), e.g. a two-eightbyte aggregate consumes two registers).
For every producer it keeps the existing bookkeeping, now driven by the decoded fields:
- a parameter with no location, a constant value, or (for non-clang) no register found is marked optimized out
- a parameter found in a register other than the expected one is marked unexpected_reg
When true_signature is enabled for a signature-changed function it reconstructs the real register-level signature:
- parameters that were optimized out are dropped from the signature
- a parameter whose location cannot be tied to its expected register, wrong register, no register found, or a non-aggregate sitting on the stack - marks the function unexpected_reg so no untrustworthy signature is emitted;
- an aggregate genuinely passed on the stack (passed_in_memory) is kept;
- an aggregate split across registers via DW_OP_piece is kept whole when it is fully used or the next parameter still lands on its expected register, otherwise it is rewritten to the single member actually passed in a register (true_sig_*).
Together with the decoding commit this replaces the previous inline, per-parameter register check in parameter__new().
--- dwarf_loader.c | 197 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 194 insertions(+), 3 deletions(-)
dwarf_loader.c +194 -3
@@ -3043,6 +3043,195 @@ static void ftype__recode_dwarf_types(struct tag *tag, struct cu *cu) } } +static struct parameter *ftype__next_parameter (struct ftype *ftype,struct parameter *parm) +{ +if (parm->tag.node.next == &ftype->parms) +return NULL ; + return list_entry(parm->tag.node.next, struct parameter, tag.node); +} + +static int parameter__abi_slots (const struct parameter *parm,const struct cu *cu) +{ +int slots; + +if (!cu->agg_use_two_regs || parm->type_byte_size <= cu->addr_size) +return 1 ; + + slots = (parm->type_byte_size + cu->addr_size -1 ) / cu->addr_size; +return slots >0 ? slots :1 ; +} + +static bool parameter__has_piece_info (const struct parameter *parm) +{ +return parm->first_reg_fields || parm->second_reg_fields; +} + +static bool parameter__uses_full_aggregate (const struct parameter *parm) +{ +return parm->first_reg_fields && parm->second_reg_fields; +} + +static bool ftype__next_parameter_preserves_slots (struct ftype *ftype,struct parameter *parm, +int reg_idx,int slots,struct cu *cu) +{ +struct parameter *next =ftype__next_parameter (ftype, parm); +int next_reg_idx; + +if (!next || next->loc_reg ==PARAMETER_UNKNOWN_REG ) +return false; + + next_reg_idx = reg_idx + slots; +return next_reg_idx < cu->nr_register_params && + next->loc_reg == cu->register_params[next_reg_idx]; +} + +static bool parameter__apply_true_sig_member (struct parameter *parm,struct cu *cu) +{ +struct dwarf_tag tmp = {}; +struct dwarf_tag *dtype; + +if (!parm->true_sig_member_name || parm->true_sig_type ==0 ) +return false; + + tmp.type = parm->true_sig_type; + tmp.from_types_section.type = parm->true_sig_type_from_types; + dtype =__dwarf_cu__find_type_by_ref (cu->priv, tmp.type, tmp.from_types_section.type); +if (!dtype) +return false; + + parm->tag.type = dtype->small_id; +return true; +} + +static bool parameter__reg_in_expected_window (const struct parameter *parm,int reg_idx, +int slots,const struct cu *cu) +{ +for (int i =0 ; i < slots; i++) { +int idx = reg_idx + i; + +if (idx >= cu->nr_register_params) +break ; +if (parm->loc_reg == cu->register_params[idx]) +return true; + } +return false; +} + +static void function__match_clang_parameter_locations (struct ftype *ftype,struct cu *cu) +{ +struct parameter *pos; +int reg_idx =0 ; + +ftype__for_each_parameter (ftype, pos) { +int slots =parameter__abi_slots (pos, cu); + +if (pos->passed_in_memory) +continue ; + +if (reg_idx >= cu->nr_register_params) +break ; + +if (pos->loc_reg !=PARAMETER_UNKNOWN_REG && + !parameter__reg_in_expected_window (pos, reg_idx, slots, cu)) + pos->unexpected_reg =1 ; + + reg_idx += slots; + } +} + +static void function__analyze_parameter_locations (struct function *fn,struct cu *cu, +struct conf_load *conf) +{ +struct ftype *ftype = &fn->proto; +struct parameter *pos; +bool true_sig_enabled = conf->true_signature && ftype->signature_changed; +bool check_locations = !cu->producer_clang || ftype->signature_changed; +int reg_idx =0 ; + +if (!check_locations) { +/* Producer is clang and the signature was not changed: match +* each parameter against its expected ABI argument register. +*/ +function__match_clang_parameter_locations (ftype, cu); +return ; + } + +ftype__for_each_parameter (ftype, pos) { +bool consumes_register = true; +bool regs_available = reg_idx < cu->nr_register_params; +int slots =parameter__abi_slots (pos, cu); +int expected_reg = regs_available ? cu->register_params[reg_idx] :-1 ; +int reg_slots = pos->passed_in_memory ?1 : slots; + +if (pos->has_loc) { +if (true_sig_enabled && pos->loc_const_value) { + pos->optimized =1 ; + consumes_register = false; + goto next; + } + +if (!regs_available) { + consumes_register = false; + goto next; + } + +if (true_sig_enabled && pos->loc_stack) { +if (pos->passed_in_memory) + consumes_register = false; +else + pos->unexpected_reg =1 ; + goto next; + } + +if (pos->loc_reg ==PARAMETER_UNKNOWN_REG ) { +if (true_sig_enabled) + pos->unexpected_reg =1 ; +else + pos->optimized =1 ; + goto next; + } + +if (expected_reg >=0 && expected_reg != pos->loc_reg) { + pos->unexpected_reg =1 ; + goto next; + } + +if (true_sig_enabled &¶meter__has_piece_info (pos)) { +if (parameter__uses_full_aggregate (pos)) { + reg_idx += slots; +continue ; + } + +if (ftype__next_parameter_preserves_slots (ftype, pos, reg_idx, slots, cu)) { + pos->true_sig_member_name =0 ; + reg_idx += slots; +continue ; + } + +if (parameter__apply_true_sig_member (pos, cu)) { + reg_idx++; +continue ; + } + } + }else if (pos->has_const_value && !cu->producer_clang) { + pos->optimized =1 ; + }else if (true_sig_enabled) { +if (regs_available && +ftype__next_parameter_preserves_slots (ftype, pos, reg_idx, slots, cu)) { + reg_idx += slots; +continue ; + } + + pos->optimized =1 ; + consumes_register = false; + } + +next: +if (consumes_register) + reg_idx += reg_slots; + } +} +static void lexblock__recode_dwarf_types (struct lexblock *tag,struct cu *cu) {struct tag *pos;
@@ -3318,7 +3507,7 @@ static bool param__is_struct(struct cu *cu, struct tag *tag) } } -static int cu__resolve_func_ret_types_optimized (struct cu *cu) +static int cu__resolve_func_ret_types_optimized (struct cu *cu,struct conf_load *conf) {struct ptr_table *pt = &cu->functions_table;uint32_t i;
@@ -3329,6 +3518,8 @@ static int cu__resolve_func_ret_types_optimized(struct cu *cu)struct function *fn =tag__function (tag);bool has_unexpected_reg = false, has_struct_param = false; +function__analyze_parameter_locations (fn, cu, conf); +/* mark function as optimized if parameter is, or * if parameter does not have a location; at this * point location presence has been marked in
@@ -3507,7 +3698,7 @@ static int die__process_and_recode(Dwarf_Die *die, struct cu *cu, struct conf_loif (ret !=0 )return ret; -return cu__resolve_func_ret_types_optimized (cu); +return cu__resolve_func_ret_types_optimized (cu, conf); } static int class_member__cache_byte_size(struct tag *tag, struct cu *cu,
@@ -4270,7 +4461,7 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,* encoded in another subprogram through abstract_origin * tag. Let us visit all subprograms again to resolve this. */ -if (cu__resolve_func_ret_types_optimized (cu) !=LSK__KEEPIT ) +if (cu__resolve_func_ret_types_optimized (cu, conf) !=LSK__KEEPIT ) goto out_abort;cu__finalize (cu, cus, conf);
Signature · 1 line
2.53.0-Meta
Yonghong Song
reviewerPATCH v10 4/5 btf_encoder: Emit true function signatures
In reply to Yonghong SongWhen true_signature is enabled, consume the per-parameter analysis the DWARF loader recorded while saving a function's BTF:
- Drop parameters that were optimized out of a signature-changed function (ftype->signature_changed && param->optimized), adjusting the saved parameter count accordingly.
- When a parameter was reduced to a single aggregate member (param->true_sig_member_name is set), emit it under the synthesized name "<parameter>__<member>" so the BTF reflects the value actually passed in the register.
--- btf_encoder.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
btf_encoder.c +25 -1
@@ -1297,14 +1297,38 @@ static int32_t btf_encoder__save_func(struct btf_encoder *encoder, struct functi state->reordered_parm = ftype->reordered_parm;ftype__for_each_parameter (ftype, param) {const char *name; +char *final_name =NULL ;/* No location info/optimized + reordered means optimized out. */ if (ftype->reordered_parm && (!param->has_loc || param->optimized)) { state->nr_parms--;continue ; } - name =parameter__name (param) ?:"" ; +if (encoder->true_signature && ftype->signature_changed && param->optimized) { + state->nr_parms--; +continue ; + } + + name =parameter__name (param); +if (!name) { + name ="" ; + }else if (encoder->true_signature && + ftype->signature_changed && + param->true_sig_member_name) { +/* Non-null param->true_sig_member_name indicates that the parameter +* name is <parameter_name>__<field_name>. +*/ +if (asprintf (&final_name,"%s__%s" , name, param->true_sig_member_name) ==-1 ) { + err = -ENOMEM ; + goto out; + } + name = final_name; + } + str_off =btf__add_str (btf, name); +if (final_name) +free (final_name); +if (str_off <0 ) { err = str_off; goto out;
Signature · 1 line
2.53.0-Meta
Yonghong Song
reviewerPATCH v10 5/5 tests: Add BTF true_signature encoding tests
In reply to Yonghong SongAdd six tests exercising true_signature BTF encoding for clang-built functions:
- clang_parm_optimized: an unused scalar parameter is dropped from the true signature.
- clang_parm_optimized_stack_1: with more arguments than argument registers, optimized-out parameters (including stack-passed ones) are dropped.
- clang_parm_optimized_stack_2: with more arguments than argument registers, global function, all parameters are live.
- clang_parm_aggregate_1: a two-register aggregate that is only partially used is rewritten to its single used member, while a fully-used aggregate is preserved.
- clang_parm_aggregate_2: like aggregate_1 but with an extra unused scalar parameter; additionally checks (on aarch64) that the first argument name is preserved.
- clang_parm_memory: a large aggregate classified MEMORY and passed on the stack is kept while an unused parameter is dropped. A union is used so the struct-parameter exception in cu__resolve_func_ret_types_optimized() does not mask a wrong unexpected_reg.
The per-arch handling differs between the tests:
- clang_parm_optimized and clang_parm_optimized_stack_1 assert the signatures differ between BTF and dwarf signature.
- clang_parm_optimzied_stack_2 asserts the signatures are the same between BTF and dwarf signature.
- clang_parm_aggregate_1 asserts the signatures differ between x86_64 and aarch64. For aarch64, final signature is the same as dwarf.
- clang_parm_aggregate_2 asserts the signatures differ between x86_64 and aarch64. For aarch64, final signature is different from dwarf.
- clang_parm_memory asserts a true signature is produced with a used big struct argument.
The following is an example to dump BTF vs. Dwarf:
$ VERBOSE=1 ./clang_parm_memory.sh
Validation of BTF encoding of true_signatures.
BTF: long foo(union big u, int x); DWARF: long foo(union big u, int dead, int x);
Test ./clang_parm_memory.sh passed--- tests/clang_parm_aggregate_1.sh | 85 ++++++++++++++++++++++++++ tests/clang_parm_aggregate_2.sh | 88 +++++++++++++++++++++++++++ tests/clang_parm_memory.sh | 77 +++++++++++++++++++++++ tests/clang_parm_optimized.sh | 63 +++++++++++++++++++ tests/clang_parm_optimized_stack_1.sh | 63 +++++++++++++++++++ tests/clang_parm_optimized_stack_2.sh | 63 +++++++++++++++++++ 6 files changed, 439 insertions(+) create mode 100755 tests/clang_parm_aggregate_1.sh create mode 100755 tests/clang_parm_aggregate_2.sh create mode 100755 tests/clang_parm_memory.sh create mode 100755 tests/clang_parm_optimized.sh create mode 100755 tests/clang_parm_optimized_stack_1.sh create mode 100755 tests/clang_parm_optimized_stack_2.sh
tests/clang_parm_aggregate_1.sh new file +85 -0
@@ -0,0 +1,85 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only + +source test_lib.sh + +outdir=$(make_tmpdir) + +# Comment this out to save test data. +trap cleanup EXIT + +title_log "Validation of BTF encoding of true_signatures." + +clang_true="${outdir}/clang_true" +CC=$(which clang 2>/dev/null) + +if [[ -z "$CC" ]]; then + info_log "skip: clang not available" + test_skip +fi + +cat > ${clang_true}.c << EOF +struct t { long f1; long f2; }; +__attribute__((noinline)) static long foo(struct t a, struct t b, int i) +{ + return a.f1 + b.f1 + b.f2 + i; +} + +struct t p1, p2; +int i; +int main() +{ + return (int)foo(p1, p2, i); +} +EOF + +CFLAGS="$CFLAGS -g -O2" +${CC} ${CFLAGS} -o $clang_true ${clang_true}.c +if [[ $? -ne 0 ]]; then + error_log "Could not compile ${clang_true}.c" + test_fail +fi +LLVM_OBJCOPY=objcopy pahole -J --btf_features=+true_signature $clang_true +if [[ $? -ne 0 ]]; then + error_log "Could not encode BTF for $clang_true" + test_fail +fi + +btf_optimized=$(pfunct --all --format_path=btf $clang_true |grep "foo") +if [[ -z "$btf_optimized" ]]; then + info_log "skip: no optimizations applied." + test_skip +fi + +btf_cmp=$btf_optimized +dwarf=$(pfunct --all $clang_true |grep "foo") + +verbose_log "BTF: $btf_optimized DWARF: $dwarf" + +arch=$(uname -m) + +if [[ "$arch" == "x86_64" ]]; then + # On x86_64, clang emits DW_CC_nocall for optimized functions, + # so pahole should detect the optimization and produce a + # different BTF signature. + if [[ "$btf_cmp" == "$dwarf" ]]; then + error_log "BTF and DWARF signatures should be different and they are not: BTF: $btf_optimized ; DWARF $dwarf" + test_fail + fi +elif [[ "$arch" == "aarch64" ]]; then + # On arm64, clang does not emit DW_CC_nocall, so pahole cannot + # detect the optimization. BTF and DWARF signatures are expected + # to be the same. + if [[ "$btf_cmp" != "$dwarf" ]]; then + error_log "On arm64, BTF and DWARF signatures should be the same but they are not: BTF: $btf_optimized ; DWARF $dwarf" + test_fail + fi +else + # On other architectures, skip if we cannot determine the + # expected behavior. + if [[ "$btf_cmp" == "$dwarf" ]]; then + info_log "skip: no optimization detected on $arch" + test_skip + fi +fi +test_pass
tests/clang_parm_aggregate_2.sh new file +88 -0
88 lines not on this page · raw message
tests/clang_parm_memory.sh new file +77 -0
77 lines not on this page · raw message
tests/clang_parm_optimized.sh new file +63 -0
63 lines not on this page · raw message
tests/clang_parm_optimized_stack_1.sh new file +63 -0
63 lines not on this page · raw message
tests/clang_parm_optimized_stack_2.sh new file +63 -0
63 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Quoted 82 lines · Yonghong Song
Current vmlinux BTF encoding is based on the source level signatures. But the compiler may do some optimization and changed the signature. If the user tried with source level signature, their initial implementation may have wrong results and then the user need to check what is the problem and work around it, e.g. through kprobe since kprobe does not need vmlinux BTF.
Majority of changed signatures are due to dead argument elimination. The following is a more complex one. The original source signature:
typedef struct { union { void *kernel; void __user *user; }; bool is_kernel : 1;}
sockptr_t;
typedefsockptr_tbpfptr_t;
static intmap_create(union bpf_attr *attr, bpfptr_t uattr){ ... }
After compiler optimization, the signature becomes:
static intmap_create(union bpf_attr *attr, bool uattr__is_kernel){ ... }
In the above,uattr__is_kernelcorresponds to 'is_kernel' field insockptr_t. This makes it easier for developers to understand what changed.The new signature needs to properly follow ABI specification based on locations. Otherwise, that signature should be discarded. For example,
0x0242f1f7: DW_TAG_subprogram DW_AT_name ("memblock_find_in_range") DW_AT_calling_convention (DW_CC_nocall) DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f22e: DW_TAG_formal_parameter DW_AT_location (indexed (0x14a) loclist = 0x005595bc: [0xffffffff87a000f9, 0xffffffff87a00178): DW_OP_reg5 RDI [0xffffffff87a00178, 0xffffffff87a001be): DW_OP_reg14 R14 [0xffffffff87a001be, 0xffffffff87a001c7): DW_OP_entry_value(DW_OP_reg5 RDI), DW_OP_stack_value [0xffffffff87a001c7, 0xffffffff87a00214): DW_OP_reg14 R14) DW_AT_name ("start") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f239: DW_TAG_formal_parameter DW_AT_location (indexed (0x14b) loclist = 0x005595e6: [0xffffffff87a000f9, 0xffffffff87a00175): DW_OP_reg4 RSI [0xffffffff87a00175, 0xffffffff87a001b8): DW_OP_reg3 RBX [0xffffffff87a001b8, 0xffffffff87a001c7): DW_OP_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value [0xffffffff87a001c7, 0xffffffff87a00214): DW_OP_reg3 RBX) DW_AT_name ("end") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f245: DW_TAG_formal_parameter DW_AT_location (indexed (0x14c) loclist = 0x00559610: [0xffffffff87a001e3, 0xffffffff87a001ef): DW_OP_breg4 RSI+0) DW_AT_name ("size") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f250: DW_TAG_formal_parameter DW_AT_const_value (4096) DW_AT_name ("align") DW_AT_type (0x0242decc "phys_addr_t") ...The third argument should correspond to RDX for
x86_64. But the location suggests that the parameter value is stored in the address with 'RSI + 0'. It is not clear whether the parameter value is stored in RDX or not. So we have to discard this funciton in vmlinux BTF to avoid incorrect true signatures.For llvm, any function having
DW_AT_calling_convention(DW_CC_nocall)
in dwarfDW_TAG_subprogramwill indicate that this function has signature changed. But for nonDW_CC_nocallfunctions, it is possible that true signature still not available due to locations. So every functions will be checked.I did experiment with latest bpf-next. For
x86_64, there are 69103 kernel functions and 875 kernel functions having signature changed. A series of patches are intended to ensure true signatures are properly represented. Eventually, only 20 functions cannot have true signatures due to locations.For arm64, there are 863 kernel functions having signature changed, and 108 functions cannot have true signatures due to locations. I checked those functions and look like llvm arm64 backend more relaxed to compute parameter values.
Quoted 5 lines
For full testing, I enabled true signature support in kernel
scripts/Makefile.btflike below:
-pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes +pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes --btf_features=+true_signatureSee individual patches for details.
hi,
I tried this version and had to choose another function, because "arp_process" suddenly started to show args registers in proper order ;-)
now I checked "stop_cpus" function which is added to btf after this change, I attached the dwarf dump below
"stop_cpus" has DW_CC_nocall set, so it has signature_changed=true and I see the function ends up in the btf_encoder with optimized_parms=1
but we do not skip such functions now, it seems like we should?
I tried the attached change below and it removes all the extra functions that were added in the btf with this change
btw I can't see any other use for optimized_parms flag, it seems to be just set and never used.. so I wonder I'm missing something
jirka
dwarfdump:
0x016f4b1b: DW_TAG_subprogram
DW_AT_low_pc (0xffffffff813e0fc0)
DW_AT_high_pc (0xffffffff813e1133)
DW_AT_frame_base (DW_OP_reg7 RSP)
DW_AT_GNU_all_call_sites (true)
DW_AT_name ("stop_cpus")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c")
DW_AT_decl_line (464)
DW_AT_prototyped (true)
DW_AT_calling_convention (DW_CC_nocall)
DW_AT_type (0x016e94c5 "int")0x016f4b36: DW_TAG_formal_parameter
DW_AT_location (0x0065ad9e:
[0xffffffff813e0fc5, 0xffffffff813e0fed): DW_OP_reg5 RDI
[0xffffffff813e0fed, 0xffffffff813e111e): DW_OP_reg14 R14
[0xffffffff813e111e, 0xffffffff813e1127): DW_OP_GNU_entry_value(DW_OP_reg5 RDI), DW_OP_stack_value
[0xffffffff813e1127, 0xffffffff813e1133): DW_OP_reg14 R14)
DW_AT_name ("cpumask")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c")
DW_AT_decl_line (464)
DW_AT_type (0x016f3242 "const cpumask *")0x016f4b46: DW_TAG_formal_parameter
DW_AT_name ("fn")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c")
DW_AT_decl_line (464)
DW_AT_type (0x016f2ca5 "cpu_stop_fn_t")0x016f4b52: DW_TAG_formal_parameter
DW_AT_location (0x0065ae0d:
[0xffffffff813e0fc5, 0xffffffff813e0ff2): DW_OP_reg4 RSI
[0xffffffff813e0ff2, 0xffffffff813e10e4): DW_OP_reg3 RBX
[0xffffffff813e10e4, 0xffffffff813e1127): DW_OP_GNU_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value
[0xffffffff813e1127, 0xffffffff813e112e): DW_OP_reg3 RBX
[0xffffffff813e112e, 0xffffffff813e1133): DW_OP_GNU_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value)
DW_AT_name ("arg")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c")
DW_AT_decl_line (464)
DW_AT_type (0x016e858f "void *")---
btf_encoder.c +4 -1
@@ -1564,7 +1564,8 @@ static int btf_encoder__add_true_signature(struct btf_encoder *encoder,*/ if (true_state->unexpected_reg || true_state->uncertain_parm_loc || - true_state->ambiguous_addr) + true_state->ambiguous_addr || + true_state->optimized_parms)continue ; err =btf_encoder__add_func (encoder, true_state);if (err <0 )
@@ -1656,6 +1657,8 @@ static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder, bool skip_e skip_reason ="reordered parameters\n" ;if (state->elf->ambiguous_addr) skip_reason ="ambiguous address\n" ; +if (state->optimized_parms) + skip_reason ="optimized parms\n" ;if (skip_reason) { btf_encoder__log_func_skip(encoder, saved_fns[i].elf,
Quoted 99 lines · Jiri Olsa
Quoted 82 lines · Yonghong Song
Current vmlinux BTF encoding is based on the source level signatures. But the compiler may do some optimization and changed the signature. If the user tried with source level signature, their initial implementation may have wrong results and then the user need to check what is the problem and work around it, e.g. through kprobe since kprobe does not need vmlinux BTF.
Majority of changed signatures are due to dead argument elimination. The following is a more complex one. The original source signature:
typedef struct { union { void *kernel; void __user *user; }; bool is_kernel : 1;}
sockptr_t;
typedefsockptr_tbpfptr_t;
static intmap_create(union bpf_attr *attr, bpfptr_t uattr){ ... }
After compiler optimization, the signature becomes:
static intmap_create(union bpf_attr *attr, bool uattr__is_kernel){ ... }
In the above,uattr__is_kernelcorresponds to 'is_kernel' field insockptr_t. This makes it easier for developers to understand what changed.The new signature needs to properly follow ABI specification based on locations. Otherwise, that signature should be discarded. For example,
0x0242f1f7: DW_TAG_subprogram DW_AT_name ("memblock_find_in_range") DW_AT_calling_convention (DW_CC_nocall) DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f22e: DW_TAG_formal_parameter DW_AT_location (indexed (0x14a) loclist = 0x005595bc: [0xffffffff87a000f9, 0xffffffff87a00178): DW_OP_reg5 RDI [0xffffffff87a00178, 0xffffffff87a001be): DW_OP_reg14 R14 [0xffffffff87a001be, 0xffffffff87a001c7): DW_OP_entry_value(DW_OP_reg5 RDI), DW_OP_stack_value [0xffffffff87a001c7, 0xffffffff87a00214): DW_OP_reg14 R14) DW_AT_name ("start") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f239: DW_TAG_formal_parameter DW_AT_location (indexed (0x14b) loclist = 0x005595e6: [0xffffffff87a000f9, 0xffffffff87a00175): DW_OP_reg4 RSI [0xffffffff87a00175, 0xffffffff87a001b8): DW_OP_reg3 RBX [0xffffffff87a001b8, 0xffffffff87a001c7): DW_OP_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value [0xffffffff87a001c7, 0xffffffff87a00214): DW_OP_reg3 RBX) DW_AT_name ("end") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f245: DW_TAG_formal_parameter DW_AT_location (indexed (0x14c) loclist = 0x00559610: [0xffffffff87a001e3, 0xffffffff87a001ef): DW_OP_breg4 RSI+0) DW_AT_name ("size") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f250: DW_TAG_formal_parameter DW_AT_const_value (4096) DW_AT_name ("align") DW_AT_type (0x0242decc "phys_addr_t") ...The third argument should correspond to RDX for
x86_64. But the location suggests that the parameter value is stored in the address with 'RSI + 0'. It is not clear whether the parameter value is stored in RDX or not. So we have to discard this funciton in vmlinux BTF to avoid incorrect true signatures.For llvm, any function having
DW_AT_calling_convention(DW_CC_nocall)
in dwarfDW_TAG_subprogramwill indicate that this function has signature changed. But for nonDW_CC_nocallfunctions, it is possible that true signature still not available due to locations. So every functions will be checked.I did experiment with latest bpf-next. For
x86_64, there are 69103 kernel functions and 875 kernel functions having signature changed. A series of patches are intended to ensure true signatures are properly represented. Eventually, only 20 functions cannot have true signatures due to locations.For arm64, there are 863 kernel functions having signature changed, and 108 functions cannot have true signatures due to locations. I checked those functions and look like llvm arm64 backend more relaxed to compute parameter values.
Quoted 5 lines
For full testing, I enabled true signature support in kernel
scripts/Makefile.btflike below:
-pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes +pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes --btf_features=+true_signatureSee individual patches for details.
hi,
I tried this version and had to choose another function, because "arp_process" suddenly started to show args registers in proper order ;-)now I checked "
stop_cpus" function which is added to btf after this change, I attached the dwarf dump below"
stop_cpus" hasDW_CC_nocallset, so it has signature_changed=true and I see the function ends up in thebtf_encoderwith optimized_parms=1
Quoted 8 lines
but we do not skip such functions now, it seems like we should?
I tried the attached change below and it removes all the extra functions that were added in the btf with this change
btw I can't see any other use for
optimized_parmsflag, it seems to be just set and never used.. so I wonder I'm missing something
Thanks for looking at this!
Yonghong can correct me if I have this wrong, but I think optimized parameters are okay, but only if true signature is enabled, so omitting functions with optimized parameters should probably only happen if !true_signature. so for the change below, the modification to btf_encoder__add_saved_funcs() is right but I don't think the modification to btf_encoder__add_true_signature() is needed.
Quoted 73 lines
jirka
dwarfdump:
0x016f4b1b:DW_TAG_subprogramDW_AT_low_pc (0xffffffff813e0fc0) DW_AT_high_pc (0xffffffff813e1133) DW_AT_frame_base (DW_OP_reg7 RSP) DW_AT_GNU_all_call_sites (true) DW_AT_name ("stop_cpus") DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464) DW_AT_prototyped (true) DW_AT_calling_convention (DW_CC_nocall) DW_AT_type (0x016e94c5 "int")
0x016f4b36:DW_TAG_formal_parameterDW_AT_location (0x0065ad9e: [0xffffffff813e0fc5, 0xffffffff813e0fed): DW_OP_reg5 RDI [0xffffffff813e0fed, 0xffffffff813e111e): DW_OP_reg14 R14 [0xffffffff813e111e, 0xffffffff813e1127): DW_OP_GNU_entry_value(DW_OP_reg5 RDI), DW_OP_stack_value [0xffffffff813e1127, 0xffffffff813e1133): DW_OP_reg14 R14) DW_AT_name ("cpumask") DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464) DW_AT_type (0x016f3242 "const cpumask *")
0x016f4b46:DW_TAG_formal_parameterDW_AT_name ("fn") DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464) DW_AT_type (0x016f2ca5 "cpu_stop_fn_t")
0x016f4b52:DW_TAG_formal_parameterDW_AT_location (0x0065ae0d: [0xffffffff813e0fc5, 0xffffffff813e0ff2): DW_OP_reg4 RSI [0xffffffff813e0ff2, 0xffffffff813e10e4): DW_OP_reg3 RBX [0xffffffff813e10e4, 0xffffffff813e1127): DW_OP_GNU_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value [0xffffffff813e1127, 0xffffffff813e112e): DW_OP_reg3 RBX [0xffffffff813e112e, 0xffffffff813e1133): DW_OP_GNU_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value) DW_AT_name ("arg") DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464) DW_AT_type (0x016e858f "void *")---
btf_encoder.c +4
-1@@ -1564,7 +1564,8 @@ static int btf_encoder__add_true_signature(struct btf_encoder *encoder,*/ if (true_state->unexpected_reg || true_state->uncertain_parm_loc || - true_state->ambiguous_addr) + true_state->ambiguous_addr || + true_state->optimized_parms)continue ; err =btf_encoder__add_func (encoder, true_state);if (err <0 )@@ -1656,6 +1657,8 @@ static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder, bool skip_e skip_reason ="reordered parameters\n" ;if (state->elf->ambiguous_addr) skip_reason ="ambiguous address\n" ; +if (state->optimized_parms) + skip_reason ="optimized parms\n" ;if (skip_reason) { btf_encoder__log_func_skip(encoder, saved_fns[i].elf,
Yonghong Song
reviewerQuoted 107 lines · Alan Maguire
Quoted 98 lines · Jiri Olsa
Quoted 82 lines · Yonghong Song
Current vmlinux BTF encoding is based on the source level signatures. But the compiler may do some optimization and changed the signature. If the user tried with source level signature, their initial implementation may have wrong results and then the user need to check what is the problem and work around it, e.g. through kprobe since kprobe does not need vmlinux BTF.
Majority of changed signatures are due to dead argument elimination. The following is a more complex one. The original source signature:
typedef struct { union { void *kernel; void __user *user; }; bool is_kernel : 1; } sockptr_t; typedef sockptr_t bpfptr_t; static int map_create(union bpf_attr *attr, bpfptr_t uattr) { ... } After compiler optimization, the signature becomes: static int map_create(union bpf_attr *attr, bool uattr__is_kernel) { ... }In the above,
uattr__is_kernelcorresponds to 'is_kernel' field insockptr_t. This makes it easier for developers to understand what changed.The new signature needs to properly follow ABI specification based on locations. Otherwise, that signature should be discarded. For example,
0x0242f1f7: DW_TAG_subprogram DW_AT_name ("memblock_find_in_range") DW_AT_calling_convention (DW_CC_nocall) DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f22e: DW_TAG_formal_parameter DW_AT_location (indexed (0x14a) loclist = 0x005595bc: [0xffffffff87a000f9, 0xffffffff87a00178): DW_OP_reg5 RDI [0xffffffff87a00178, 0xffffffff87a001be): DW_OP_reg14 R14 [0xffffffff87a001be, 0xffffffff87a001c7): DW_OP_entry_value(DW_OP_reg5 RDI), DW_OP_stack_value [0xffffffff87a001c7, 0xffffffff87a00214): DW_OP_reg14 R14) DW_AT_name ("start") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f239: DW_TAG_formal_parameter DW_AT_location (indexed (0x14b) loclist = 0x005595e6: [0xffffffff87a000f9, 0xffffffff87a00175): DW_OP_reg4 RSI [0xffffffff87a00175, 0xffffffff87a001b8): DW_OP_reg3 RBX [0xffffffff87a001b8, 0xffffffff87a001c7): DW_OP_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value [0xffffffff87a001c7, 0xffffffff87a00214): DW_OP_reg3 RBX) DW_AT_name ("end") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f245: DW_TAG_formal_parameter DW_AT_location (indexed (0x14c) loclist = 0x00559610: [0xffffffff87a001e3, 0xffffffff87a001ef): DW_OP_breg4 RSI+0) DW_AT_name ("size") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f250: DW_TAG_formal_parameter DW_AT_const_value (4096) DW_AT_name ("align") DW_AT_type (0x0242decc "phys_addr_t") ...The third argument should correspond to RDX for
x86_64. But the location suggests that the parameter value is stored in the address with 'RSI + 0'. It is not clear whether the parameter value is stored in RDX or not. So we have to discard this funciton in vmlinux BTF to avoid incorrect true signatures.For llvm, any function having
DW_AT_calling_convention(DW_CC_nocall)
in dwarfDW_TAG_subprogramwill indicate that this function has signature changed. But for nonDW_CC_nocallfunctions, it is possible that true signature still not available due to locations. So every functions will be checked.I did experiment with latest bpf-next. For
x86_64, there are 69103 kernel functions and 875 kernel functions having signature changed. A series of patches are intended to ensure true signatures are properly represented. Eventually, only 20 functions cannot have true signatures due to locations.For arm64, there are 863 kernel functions having signature changed, and 108 functions cannot have true signatures due to locations. I checked those functions and look like llvm arm64 backend more relaxed to compute parameter values.
Quoted 5 lines
For full testing, I enabled true signature support in kernel
scripts/Makefile.btflike below:-pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes +pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes --btf_features=+true_signatureSee individual patches for details.
hi,
I tried this version and had to choose another function, because "arp_process" suddenly started to show args registers in proper order ;-)now I checked "
stop_cpus" function which is added to btf after this change, I attached the dwarf dump below"
stop_cpus" hasDW_CC_nocallset, so it has signature_changed=true and I see the function ends up in thebtf_encoderwith optimized_parms=1Quoted 8 lines
but we do not skip such functions now, it seems like we should?
I tried the attached change below and it removes all the extra functions that were added in the btf with this change
btw I can't see any other use for
optimized_parmsflag, it seems to be just set and never used.. so I wonder I'm missing something
Quoted 7 lines
Thanks for looking at this!
Yonghong can correct me if I have this wrong, but I think optimized parameters are okay, but only if true signature is enabled, so omitting functions with optimized parameters should probably only happen if !true_signature. so for the change below, the modification to
btf_encoder__add_saved_funcs()is right but I don't think the modification tobtf_encoder__add_true_signature()is needed.
I agree with Alan in the above.
There is no need to make change in btf_encoder__add_true_signature(). This is for true_signature, optimized parameters have been removed:
+ /* No location info/optimized + reordered means optimized out. */
+ if (ftype->reordered_parm && (!param->has_loc || param->optimized)) {
+ state->nr_parms--;
+ continue;
+ }
so we should not add true_state->optimized_parms checking in btf_encoder__add_true_signature().
For adding state->optimized_parms in btf_encoder__add_saved_funcs(), with true_signature disabled, we already have a failure message:
stop_cpus : skipping BTF encoding of function due to unexpected register usage for parameter
The expectation is for three parameters, the register usage should be
cpumask: RDI, fn: RSI, arg: RDX
but since 'fn' is optimized away, the expected reg usage does not match the actual reg usage.
We could add:
+ if (state->optimized_parms)
+ skip_reason = "optimized parms\n";
to have another message to indicate possible reason is due to optimized parms. But it is not critical as we already have one message.
I can have a follow up for this.
Quoted 74 lines
Quoted 74 lines
jirka
dwarfdump:
0x016f4b1b:DW_TAG_subprogramDW_AT_low_pc (0xffffffff813e0fc0) DW_AT_high_pc (0xffffffff813e1133) DW_AT_frame_base (DW_OP_reg7 RSP) DW_AT_GNU_all_call_sites (true) DW_AT_name ("stop_cpus") DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464) DW_AT_prototyped (true) DW_AT_calling_convention (DW_CC_nocall) DW_AT_type (0x016e94c5 "int")
0x016f4b36:DW_TAG_formal_parameterDW_AT_location (0x0065ad9e: [0xffffffff813e0fc5, 0xffffffff813e0fed): DW_OP_reg5 RDI [0xffffffff813e0fed, 0xffffffff813e111e): DW_OP_reg14 R14 [0xffffffff813e111e, 0xffffffff813e1127): DW_OP_GNU_entry_value(DW_OP_reg5 RDI), DW_OP_stack_value [0xffffffff813e1127, 0xffffffff813e1133): DW_OP_reg14 R14) DW_AT_name ("cpumask") DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464) DW_AT_type (0x016f3242 "const cpumask *")
0x016f4b46:DW_TAG_formal_parameterDW_AT_name ("fn") DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464) DW_AT_type (0x016f2ca5 "cpu_stop_fn_t")
0x016f4b52:DW_TAG_formal_parameterDW_AT_location (0x0065ae0d: [0xffffffff813e0fc5, 0xffffffff813e0ff2): DW_OP_reg4 RSI [0xffffffff813e0ff2, 0xffffffff813e10e4): DW_OP_reg3 RBX [0xffffffff813e10e4, 0xffffffff813e1127): DW_OP_GNU_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value [0xffffffff813e1127, 0xffffffff813e112e): DW_OP_reg3 RBX [0xffffffff813e112e, 0xffffffff813e1133): DW_OP_GNU_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value) DW_AT_name ("arg") DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464) DW_AT_type (0x016e858f "void *")---
btf_encoder.c +4
-1@@ -1564,7 +1564,8 @@ static int btf_encoder__add_true_signature(struct btf_encoder *encoder,*/ if (true_state->unexpected_reg || true_state->uncertain_parm_loc || - true_state->ambiguous_addr) + true_state->ambiguous_addr || + true_state->optimized_parms)continue ; err =btf_encoder__add_func (encoder, true_state);if (err <0 )@@ -1656,6 +1657,8 @@ static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder, bool skip_e skip_reason ="reordered parameters\n" ;if (state->elf->ambiguous_addr) skip_reason ="ambiguous address\n" ; +if (state->optimized_parms) + skip_reason ="optimized parms\n" ;if (skip_reason) { btf_encoder__log_func_skip(encoder, saved_fns[i].elf,
Quoted 138 lines · Yonghong Song
Quoted 107 lines · Alan Maguire
Quoted 98 lines · Jiri Olsa
Quoted 82 lines · Yonghong Song
Current vmlinux BTF encoding is based on the source level signatures. But the compiler may do some optimization and changed the signature. If the user tried with source level signature, their initial implementation may have wrong results and then the user need to check what is the problem and work around it, e.g. through kprobe since kprobe does not need vmlinux BTF.
Majority of changed signatures are due to dead argument elimination. The following is a more complex one. The original source signature:
typedef struct {
union {
void *kernel;
void __user *user;
};
boolis_kernel: 1;
}sockptr_t;
typedefsockptr_tbpfptr_t;
static intmap_create(union bpf_attr *attr, bpfptr_t uattr){ ... } After compiler optimization, the signature becomes:
static intmap_create(union bpf_attr *attr, bool uattr__is_kernel){ ... } In the above,uattr__is_kernelcorresponds to 'is_kernel' field insockptr_t. This makes it easier for developers to understand what changed.The new signature needs to properly follow ABI specification based on locations. Otherwise, that signature should be discarded. For example,
0x0242f1f7:
DW_TAG_subprogram
DW_AT_name ("memblock_find_in_range")
DW_AT_calling_convention (DW_CC_nocall)
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f22e:DW_TAG_formal_parameter
DW_AT_location (indexed (0x14a) loclist =0x005595bc:
[0xffffffff87a000f9,0xffffffff87a00178):DW_OP_reg5RDI [0xffffffff87a00178,0xffffffff87a001be):DW_OP_reg14R14 [0xffffffff87a001be,0xffffffff87a001c7):DW_OP_entry_value(DW_OP_reg5 RDI),DW_OP_stack_value[0xffffffff87a001c7,0xffffffff87a00214):DW_OP_reg14R14)
DW_AT_name ("start")
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f239:DW_TAG_formal_parameter
DW_AT_location (indexed (0x14b) loclist =0x005595e6:
[0xffffffff87a000f9,0xffffffff87a00175):DW_OP_reg4RSI [0xffffffff87a00175,0xffffffff87a001b8):DW_OP_reg3RBX [0xffffffff87a001b8,0xffffffff87a001c7):DW_OP_entry_value(DW_OP_reg4 RSI),DW_OP_stack_value[0xffffffff87a001c7,0xffffffff87a00214):DW_OP_reg3RBX)
DW_AT_name ("end")
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f245:DW_TAG_formal_parameter
DW_AT_location (indexed (0x14c) loclist =0x00559610:
[0xffffffff87a001e3,0xffffffff87a001ef):DW_OP_breg4RSI+0)
DW_AT_name ("size")
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f250:DW_TAG_formal_parameter
DW_AT_const_value (4096)
DW_AT_name ("align")
DW_AT_type (0x0242decc"phys_addr_t")
...The third argument should correspond to RDX for
x86_64. But the location suggests that the parameter value is stored in the address with 'RSI + 0'. It is not clear whether the parameter value is stored in RDX or not. So we have to discard this funciton in vmlinux BTF to avoid incorrect true signatures.For llvm, any function having
DW_AT_calling_convention (DW_CC_nocall)
in dwarfDW_TAG_subprogramwill indicate that this function has signature changed. But for nonDW_CC_nocallfunctions, it is possible that true signature still not available due to locations. So every functions will be checked.I did experiment with latest bpf-next. For
x86_64, there are 69103 kernel functions and 875 kernel functions having signature changed. A series of patches are intended to ensure true signatures are properly represented. Eventually, only 20 functions cannot have true signatures due to locations.For arm64, there are 863 kernel functions having signature changed, and 108 functions cannot have true signatures due to locations. I checked those functions and look like llvm arm64 backend more relaxed to compute parameter values.
Quoted 5 lines
For full testing, I enabled true signature support in kernel
scripts/Makefile.btflike below:
-pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes +pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes --btf_features=+true_signatureSee individual patches for details.
hi,
I tried this version and had to choose another function, because "arp_process" suddenly started to show args registers in proper order ;-)now I checked "
stop_cpus" function which is added to btf after this change, I attached the dwarf dump below"
stop_cpus" hasDW_CC_nocallset, so it has signature_changed=true and I see the function ends up in thebtf_encoderwith optimized_parms=1Quoted 8 lines
but we do not skip such functions now, it seems like we should?
I tried the attached change below and it removes all the extra functions that were added in the btf with this change
btw I can't see any other use for
optimized_parmsflag, it seems to be just set and never used.. so I wonder I'm missing somethingQuoted 7 lines
Thanks for looking at this!
Yonghong can correct me if I have this wrong, but I think optimized parameters are okay, but only if true signature is enabled, so omitting functions with optimized parameters should probably only happen if !true_signature. so for the change below, the modification to
btf_encoder__add_saved_funcs()is right but I don't think the modification tobtf_encoder__add_true_signature()is needed.I agree with Alan in the above.
There is no need to make change in
btf_encoder__add_true_signature(). This is fortrue_signature, optimized parameters have been removed:+ /* No location info/optimized + reordered means optimized out. */ + if (
ftype->reordered_parm&& (!param->has_loc ||param->optimized)) { + state->nr_parms--;
+ continue;
+ }so we should not add
true_state->optimized_parmschecking inbtf_encoder__add_true_signature().For adding
state->optimized_parmsinbtf_encoder__add_saved_funcs(), withtrue_signaturedisabled, we already have a failure message:
stop_cpus: skipping BTF encoding of function due to unexpected register usage for parameter The expectation is for three parameters, the register usage should be
cpumask: RDI, fn: RSI, arg: RDX
but since 'fn' is optimized away, the expected reg usage does not match the actual reg usage.
Quoted 8 lines
We could add:
+ if (state->optimized_parms)
+skip_reason= "optimized parms\n";
to have another message to indicate possible reason is due to optimized parms. But it is not critical as we already have one message.I can have a follow up for this.
I tried pushing just the btf_encoder__add_saved_funcs() hunk from Jiri's diff to CI [1]; it looks like we end up omitting a lot more functions than before for aarch64 (including some kfuncs) for both gcc and clang kernel builds, and we see an x86_64 test failure:
5: clang_parm_memory.sh
Validation of BTF encoding of true_signatures.
BTF for foo missing; the stack-passed aggregate was likely rejected
Test ./clang_parm_memory.sh failed
Test data is in /tmp/clang_parm_memory.sh.DV7PyO
The optimized_parms flag is a bit too broad I think; it is set whenever we are missing location info for _any_ parameter; this may be overly aggressive for clang in particular which seems to have more missing location info.
[1] https://github.com/alan-maguire/dwarves/actions/runs/28173261168
Quoted 74 lines
Quoted 73 lines
Quoted 72 lines
jirka
dwarfdump:
0x016f4b1b:
DW_TAG_subprogram
DW_AT_low_pc (0xffffffff813e0fc0)
DW_AT_high_pc (0xffffffff813e1133)
DW_AT_frame_base (DW_OP_reg7RSP) DW_AT_GNU_all_call_sites (true)
DW_AT_name ("stop_cpus")
DW_AT_decl_file("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c")DW_AT_decl_line(464)
DW_AT_prototyped (true)
DW_AT_calling_convention (DW_CC_nocall)
DW_AT_type (0x016e94c5"int")0x016f4b36:
DW_TAG_formal_parameter
DW_AT_location (0x0065ad9e:
[0xffffffff813e0fc5,0xffffffff813e0fed):DW_OP_reg5RDI
[0xffffffff813e0fed,0xffffffff813e111e):DW_OP_reg14R14
[0xffffffff813e111e,0xffffffff813e1127):DW_OP_GNU_entry_value(DW_OP_reg5 RDI),DW_OP_stack_value[0xffffffff813e1127,0xffffffff813e1133):DW_OP_reg14R14)
DW_AT_name ("cpumask")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464)
DW_AT_type (0x016f3242"const cpumask *")0x016f4b46:
DW_TAG_formal_parameter
DW_AT_name ("fn")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464)
DW_AT_type (0x016f2ca5"cpu_stop_fn_t")0x016f4b52:
DW_TAG_formal_parameter
DW_AT_location (0x0065ae0d:
[0xffffffff813e0fc5,0xffffffff813e0ff2):DW_OP_reg4RSI
[0xffffffff813e0ff2,0xffffffff813e10e4):DW_OP_reg3RBX
[0xffffffff813e10e4,0xffffffff813e1127):DW_OP_GNU_entry_value(DW_OP_reg4 RSI),DW_OP_stack_value[0xffffffff813e1127,0xffffffff813e112e):DW_OP_reg3RBX
[0xffffffff813e112e,0xffffffff813e1133):DW_OP_GNU_entry_value(DW_OP_reg4 RSI),DW_OP_stack_value) DW_AT_name ("arg")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464)
DW_AT_type (0x016e858f"void *")---
btf_encoder.c +0
-0@@ -1564,7 +1564,8 @@ static int btf_encoder__add_true_signature(struct btf_encoder *encoder,*/ if (
true_state->unexpected_reg||true_state->uncertain_parm_loc||
-true_state->ambiguous_addr)
+true_state->ambiguous_addr||
+true_state->optimized_parms) continue;
err =btf_encoder__add_func(encoder, true_state); if (err < 0)btf_encoder.c +2
-0@@ -1656,6 +1657,8 @@ static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder, bool skip_e skip_reason = "reordered parameters\n"; if (state->elf->ambiguous_addr) skip_reason = "ambiguous address\n"; + if (state->optimized_parms) + skip_reason = "optimized parms\n"; if (skip_reason) { btf_encoder__log_func_skip(encoder, saved_fns[i].elf,
Yonghong Song
reviewerQuoted 149 lines · Alan Maguire
Quoted 136 lines · Yonghong Song
Quoted 107 lines · Alan Maguire
Quoted 98 lines · Jiri Olsa
Quoted 82 lines · Yonghong Song
Current vmlinux BTF encoding is based on the source level signatures. But the compiler may do some optimization and changed the signature. If the user tried with source level signature, their initial implementation may have wrong results and then the user need to check what is the problem and work around it, e.g. through kprobe since kprobe does not need vmlinux BTF.
Majority of changed signatures are due to dead argument elimination. The following is a more complex one. The original source signature:
typedef struct {
union {
void *kernel;
void __user *user;
};
boolis_kernel: 1;
}sockptr_t;
typedefsockptr_tbpfptr_t;
static intmap_create(union bpf_attr *attr, bpfptr_t uattr){ ... }
After compiler optimization, the signature becomes:
static intmap_create(union bpf_attr *attr, bool uattr__is_kernel){ ... }
In the above,uattr__is_kernelcorresponds to 'is_kernel' field insockptr_t. This makes it easier for developers to understand what changed.The new signature needs to properly follow ABI specification based on locations. Otherwise, that signature should be discarded. For example,
0x0242f1f7:
DW_TAG_subprogram
DW_AT_name ("memblock_find_in_range")
DW_AT_calling_convention (DW_CC_nocall)
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f22e:DW_TAG_formal_parameter
DW_AT_location (indexed (0x14a) loclist =0x005595bc:
[0xffffffff87a000f9,0xffffffff87a00178):DW_OP_reg5RDI [0xffffffff87a00178,0xffffffff87a001be):DW_OP_reg14R14 [0xffffffff87a001be,0xffffffff87a001c7):DW_OP_entry_value(DW_OP_reg5 RDI),DW_OP_stack_value[0xffffffff87a001c7,0xffffffff87a00214):DW_OP_reg14R14)
DW_AT_name ("start")
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f239:DW_TAG_formal_parameter
DW_AT_location (indexed (0x14b) loclist =0x005595e6:
[0xffffffff87a000f9,0xffffffff87a00175):DW_OP_reg4RSI [0xffffffff87a00175,0xffffffff87a001b8):DW_OP_reg3RBX [0xffffffff87a001b8,0xffffffff87a001c7):DW_OP_entry_value(DW_OP_reg4 RSI),DW_OP_stack_value[0xffffffff87a001c7,0xffffffff87a00214):DW_OP_reg3RBX)
DW_AT_name ("end")
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f245:DW_TAG_formal_parameter
DW_AT_location (indexed (0x14c) loclist =0x00559610:
[0xffffffff87a001e3,0xffffffff87a001ef):DW_OP_breg4RSI+0)
DW_AT_name ("size")
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f250:DW_TAG_formal_parameter
DW_AT_const_value (4096)
DW_AT_name ("align")
DW_AT_type (0x0242decc"phys_addr_t")
...The third argument should correspond to RDX for
x86_64. But the location suggests that the parameter value is stored in the address with 'RSI + 0'. It is not clear whether the parameter value is stored in RDX or not. So we have to discard this funciton in vmlinux BTF to avoid incorrect true signatures.For llvm, any function having
DW_AT_calling_convention (DW_CC_nocall)
in dwarfDW_TAG_subprogramwill indicate that this function has signature changed. But for nonDW_CC_nocallfunctions, it is possible that true signature still not available due to locations. So every functions will be checked.I did experiment with latest bpf-next. For
x86_64, there are 69103 kernel functions and 875 kernel functions having signature changed. A series of patches are intended to ensure true signatures are properly represented. Eventually, only 20 functions cannot have true signatures due to locations.For arm64, there are 863 kernel functions having signature changed, and 108 functions cannot have true signatures due to locations. I checked those functions and look like llvm arm64 backend more relaxed to compute parameter values.
Quoted 5 lines
For full testing, I enabled true signature support in kernel
scripts/Makefile.btflike below:
-pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes +pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes --btf_features=+true_signatureSee individual patches for details.
hi,
I tried this version and had to choose another function, because "arp_process" suddenly started to show args registers in proper order ;-)now I checked "
stop_cpus" function which is added to btf after this change, I attached the dwarf dump below"
stop_cpus" hasDW_CC_nocallset, so it has signature_changed=true and I see the function ends up in thebtf_encoderwith optimized_parms=1Quoted 8 lines
but we do not skip such functions now, it seems like we should?
I tried the attached change below and it removes all the extra functions that were added in the btf with this change
btw I can't see any other use for
optimized_parmsflag, it seems to be just set and never used.. so I wonder I'm missing somethingQuoted 7 lines
Thanks for looking at this!
Yonghong can correct me if I have this wrong, but I think optimized parameters are okay, but only if true signature is enabled, so omitting functions with optimized parameters should probably only happen if !true_signature. so for the change below, the modification to
btf_encoder__add_saved_funcs()is right but I don't think the modification tobtf_encoder__add_true_signature()is needed.I agree with Alan in the above.
There is no need to make change in
btf_encoder__add_true_signature(). This is fortrue_signature, optimized parameters have been removed:+ /* No location info/optimized + reordered means optimized out. */ + if (
ftype->reordered_parm&& (!param->has_loc ||param->optimized)) { + state->nr_parms--;
+ continue;
+ }so we should not add
true_state->optimized_parmschecking inbtf_encoder__add_true_signature().For adding
state->optimized_parmsinbtf_encoder__add_saved_funcs(), withtrue_signaturedisabled, we already have a failure message:
stop_cpus: skipping BTF encoding of function due to unexpected register usage for parameter
The expectation is for three parameters, the register usage should be
cpumask: RDI, fn: RSI, arg: RDX
but since 'fn' is optimized away, the expected reg usage does not match the actual reg usage.Quoted 8 lines
We could add:
+ if (state->optimized_parms)
+skip_reason= "optimized parms\n";
to have another message to indicate possible reason is due to optimized parms. But it is not critical as we already have one message.I can have a follow up for this.
I tried pushing just the
btf_encoder__add_saved_funcs()hunk from Jiri's diff to CI [1]; it looks like we end up omitting a lot more functions than before for aarch64 (including some kfuncs) for both gcc and clang kernel builds, and we see anx86_64test failure:
Quoted 5 lines
5:
clang_parm_memory.sh
Validation of BTF encoding oftrue_signatures.
BTF for foo missing; the stack-passed aggregate was likely rejected
Test ./clang_parm_memory.sh failed
Test data is in/tmp/clang_parm_memory.sh.DV7PyO
I actually tried clang_parm_memory.sh with clang22 and clang23. Both of them passed in my local run. Not exactly sure what is the issue. Let me debug this.
Could you share how to test with pahole CI? So I can resolve all issues in CI before submit another revision if needed.
Quoted 80 lines
The
optimized_parmsflag is a bit too broad I think; it is set whenever we are missing location info for _any_ parameter; this may be overly aggressive for clang in particular which seems to have more missing location info.[1] https://github.com/alan-maguire/dwarves/actions/runs/28173261168
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
jirka
dwarfdump:
0x016f4b1b:
DW_TAG_subprogram
DW_AT_low_pc (0xffffffff813e0fc0)
DW_AT_high_pc (0xffffffff813e1133)
DW_AT_frame_base (DW_OP_reg7RSP) DW_AT_GNU_all_call_sites (true)
DW_AT_name ("stop_cpus")
DW_AT_decl_file("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c")DW_AT_decl_line(464)
DW_AT_prototyped (true)
DW_AT_calling_convention (DW_CC_nocall)
DW_AT_type (0x016e94c5"int")0x016f4b36:
DW_TAG_formal_parameter
DW_AT_location (0x0065ad9e:
[0xffffffff813e0fc5,0xffffffff813e0fed):DW_OP_reg5RDI
[0xffffffff813e0fed,0xffffffff813e111e):DW_OP_reg14R14
[0xffffffff813e111e,0xffffffff813e1127):DW_OP_GNU_entry_value(DW_OP_reg5 RDI),DW_OP_stack_value[0xffffffff813e1127,0xffffffff813e1133):DW_OP_reg14R14)
DW_AT_name ("cpumask")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464)
DW_AT_type (0x016f3242"const cpumask *")0x016f4b46:
DW_TAG_formal_parameter
DW_AT_name ("fn")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464)
DW_AT_type (0x016f2ca5"cpu_stop_fn_t")0x016f4b52:
DW_TAG_formal_parameter
DW_AT_location (0x0065ae0d:
[0xffffffff813e0fc5,0xffffffff813e0ff2):DW_OP_reg4RSI
[0xffffffff813e0ff2,0xffffffff813e10e4):DW_OP_reg3RBX
[0xffffffff813e10e4,0xffffffff813e1127):DW_OP_GNU_entry_value(DW_OP_reg4 RSI),DW_OP_stack_value[0xffffffff813e1127,0xffffffff813e112e):DW_OP_reg3RBX
[0xffffffff813e112e,0xffffffff813e1133):DW_OP_GNU_entry_value(DW_OP_reg4 RSI),DW_OP_stack_value) DW_AT_name ("arg")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464)
DW_AT_type (0x016e858f"void *")---
btf_encoder.c +4
-1@@ -1564,7 +1564,8 @@ static int btf_encoder__add_true_signature(struct btf_encoder *encoder,*/ if (true_state->unexpected_reg || true_state->uncertain_parm_loc || - true_state->ambiguous_addr) + true_state->ambiguous_addr || + true_state->optimized_parms) continue; err = btf_encoder__add_func(encoder, true_state); if (err < 0)@@ -1656,6 +1657,8 @@ static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder, bool skip_e skip_reason = "reordered parameters\n"; if (state->elf->ambiguous_addr) skip_reason = "ambiguous address\n"; + if (state->optimized_parms) + skip_reason = "optimized parms\n"; if (skip_reason) { btf_encoder__log_func_skip(encoder, saved_fns[i].elf,
Yonghong Song
reviewerQuoted 143 lines · Alan Maguire
Quoted 142 lines · Yonghong Song
Quoted 107 lines · Alan Maguire
Quoted 98 lines · Jiri Olsa
Quoted 82 lines · Yonghong Song
Current vmlinux BTF encoding is based on the source level signatures. But the compiler may do some optimization and changed the signature. If the user tried with source level signature, their initial implementation may have wrong results and then the user need to check what is the problem and work around it, e.g. through kprobe since kprobe does not need vmlinux BTF.
Majority of changed signatures are due to dead argument elimination. The following is a more complex one. The original source signature:
typedef struct {
union {
void *kernel;
void __user *user;
};
boolis_kernel: 1;
}sockptr_t;
typedefsockptr_tbpfptr_t;
static intmap_create(union bpf_attr *attr, bpfptr_t uattr){ ... }
After compiler optimization, the signature becomes:
static intmap_create(union bpf_attr *attr, bool uattr__is_kernel){ ... }
In the above,uattr__is_kernelcorresponds to 'is_kernel' field insockptr_t. This makes it easier for developers to understand what changed.The new signature needs to properly follow ABI specification based on locations. Otherwise, that signature should be discarded. For example,
0x0242f1f7:
DW_TAG_subprogram
DW_AT_name ("memblock_find_in_range")
DW_AT_calling_convention (DW_CC_nocall)
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f22e:DW_TAG_formal_parameter
DW_AT_location (indexed (0x14a) loclist =0x005595bc:
[0xffffffff87a000f9,0xffffffff87a00178):DW_OP_reg5RDI [0xffffffff87a00178,0xffffffff87a001be):DW_OP_reg14R14 [0xffffffff87a001be,0xffffffff87a001c7):DW_OP_entry_value(DW_OP_reg5 RDI),DW_OP_stack_value[0xffffffff87a001c7,0xffffffff87a00214):DW_OP_reg14R14)
DW_AT_name ("start")
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f239:DW_TAG_formal_parameter
DW_AT_location (indexed (0x14b) loclist =0x005595e6:
[0xffffffff87a000f9,0xffffffff87a00175):DW_OP_reg4RSI [0xffffffff87a00175,0xffffffff87a001b8):DW_OP_reg3RBX [0xffffffff87a001b8,0xffffffff87a001c7):DW_OP_entry_value(DW_OP_reg4 RSI),DW_OP_stack_value[0xffffffff87a001c7,0xffffffff87a00214):DW_OP_reg3RBX)
DW_AT_name ("end")
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f245:DW_TAG_formal_parameter
DW_AT_location (indexed (0x14c) loclist =0x00559610:
[0xffffffff87a001e3,0xffffffff87a001ef):DW_OP_breg4RSI+0)
DW_AT_name ("size")
DW_AT_type (0x0242decc"phys_addr_t")
...
0x0242f250:DW_TAG_formal_parameter
DW_AT_const_value (4096)
DW_AT_name ("align")
DW_AT_type (0x0242decc"phys_addr_t")
...The third argument should correspond to RDX for
x86_64. But the location suggests that the parameter value is stored in the address with 'RSI + 0'. It is not clear whether the parameter value is stored in RDX or not. So we have to discard this funciton in vmlinux BTF to avoid incorrect true signatures.For llvm, any function having
DW_AT_calling_convention (DW_CC_nocall)
in dwarfDW_TAG_subprogramwill indicate that this function has signature changed. But for nonDW_CC_nocallfunctions, it is possible that true signature still not available due to locations. So every functions will be checked.I did experiment with latest bpf-next. For
x86_64, there are 69103 kernel functions and 875 kernel functions having signature changed. A series of patches are intended to ensure true signatures are properly represented. Eventually, only 20 functions cannot have true signatures due to locations.For arm64, there are 863 kernel functions having signature changed, and 108 functions cannot have true signatures due to locations. I checked those functions and look like llvm arm64 backend more relaxed to compute parameter values.
Quoted 5 lines
For full testing, I enabled true signature support in kernel
scripts/Makefile.btflike below:
-pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes +pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes --btf_features=+true_signatureSee individual patches for details.
hi,
I tried this version and had to choose another function, because "arp_process" suddenly started to show args registers in proper order ;-)now I checked "
stop_cpus" function which is added to btf after this change, I attached the dwarf dump below"
stop_cpus" hasDW_CC_nocallset, so it has signature_changed=true and I see the function ends up in thebtf_encoderwith optimized_parms=1Quoted 8 lines
but we do not skip such functions now, it seems like we should?
I tried the attached change below and it removes all the extra functions that were added in the btf with this change
btw I can't see any other use for
optimized_parmsflag, it seems to be just set and never used.. so I wonder I'm missing somethingQuoted 7 lines
Thanks for looking at this!
Yonghong can correct me if I have this wrong, but I think optimized parameters are okay, but only if true signature is enabled, so omitting functions with optimized parameters should probably only happen if !true_signature. so for the change below, the modification to
btf_encoder__add_saved_funcs()is right but I don't think the modification tobtf_encoder__add_true_signature()is needed.I agree with Alan in the above.
There is no need to make change in
btf_encoder__add_true_signature(). This is fortrue_signature, optimized parameters have been removed:+ /* No location info/optimized + reordered means optimized out. */ + if (
ftype->reordered_parm&& (!param->has_loc ||param->optimized)) { + state->nr_parms--;
+ continue;
+ }so we should not add
true_state->optimized_parmschecking inbtf_encoder__add_true_signature().For adding
state->optimized_parmsinbtf_encoder__add_saved_funcs(), withtrue_signaturedisabled, we already have a failure message:
stop_cpus: skipping BTF encoding of function due to unexpected register usage for parameter
The expectation is for three parameters, the register usage should be
cpumask: RDI, fn: RSI, arg: RDX
but since 'fn' is optimized away, the expected reg usage does not match the actual reg usage.We could add:
+ if (state->optimized_parms)
+skip_reason= "optimized parms\n";
to have another message to indicate possible reason is due to optimized parms. But it is not critical as we already have one message.
Quoted 5 lines
Quoted 2 lines
I can have a follow up for this.
I tried pushing just the
btf_encoder__add_saved_funcs()hunk from Jiri's diff to CI [1]; it looks like we end up omitting a lot more functions than before for aarch64 (including some kfuncs) for both gcc and clang kernel builds, and we see anx86_64test failure:
For clang, aarch64 has more werid locaitons compared to x86.
Not sure whether this could be part of readon which will cause more failures.
Quoted 6 lines
5:
clang_parm_memory.sh
Validation of BTF encoding oftrue_signatures.
BTF for foo missing; the stack-passed aggregate was likely rejected
Test ./clang_parm_memory.sh failed
Test data is in/tmp/clang_parm_memory.sh.DV7PyO
The above claims the BTF encoding is missing even with true signature.
The following is what I have in my local run with llvm22:
$ cat t.c
union big { long a; char buf[24]; };
__attribute__((noinline)) static long foo(union big u, int dead, int x)
{
return u.a + x;
}union big g;
int dead, x;
int main()
{
return (int)foo(g, dead, x);
}$ /bin/clang --version clang version 22.1.3 (CentOS 22.1.3-1.el9) Target: x86_64-redhat-linux-gnu Thread model: posix InstalledDir: /usr/bin Configuration file: /etc/clang/x86_64-redhat-linux-gnu-clang.cfg
$ /bin/clang -O2 -g -c t.c
$ /bin/llvm-dwarfdump t.o t.o: file format elf64-x86-64
.debug_info contents:0x00000000: Compile Unit: length = 0x00000117, format = DWARF32, version = 0x0004, abbr_offset = 0x0000, addr_size = 0x08 (next u nit at 0x0000011b)
0x0000000b: DW_TAG_compile_unit
DW_AT_producer ("clang version 22.1.3 (CentOS 22.1.3-1.el9)")
DW_AT_language (DW_LANG_C11)
DW_AT_name ("t.c")
DW_AT_stmt_list (0x00000000)
DW_AT_comp_dir ("/home/yhs/tmp")
DW_AT_low_pc (0x0000000000000000)
DW_AT_high_pc (0x0000000000000039)0x0000002a: DW_TAG_variable
DW_AT_name ("g")
DW_AT_type (0x0000003f "big")
DW_AT_external (true)
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (7)
DW_AT_location (DW_OP_addr 0x0)0x0000003f: DW_TAG_union_type
DW_AT_name ("big")
DW_AT_byte_size (0x18)
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (1)0x00000047: DW_TAG_member
DW_AT_name ("a")
DW_AT_type (0x00000060 "long")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (1)
DW_AT_data_member_location (0x00)0x00000053: DW_TAG_member
DW_AT_name ("buf")
DW_AT_type (0x00000067 "char[24]")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (1)
DW_AT_data_member_location (0x00)0x0000005f: NULL
0x00000060: DW_TAG_base_type
DW_AT_name ("long")
DW_AT_encoding (DW_ATE_signed)
DW_AT_byte_size (0x08)0x00000067: DW_TAG_array_type
DW_AT_type (0x00000073 "char")
0x0000006c: DW_TAG_subrange_type
DW_AT_type (0x0000007a "__ARRAY_SIZE_TYPE__")
DW_AT_count (0x18)0x00000072: NULL
0x00000073: DW_TAG_base_type
DW_AT_name ("char")
DW_AT_encoding (DW_ATE_signed_char)
DW_AT_byte_size (0x01)0x0000007a: DW_TAG_base_type
DW_AT_name ("__ARRAY_SIZE_TYPE__")
DW_AT_byte_size (0x08)
DW_AT_encoding (DW_ATE_unsigned)0x00000081: DW_TAG_variable
DW_AT_name ("dead")
DW_AT_type (0x00000096 "int")
DW_AT_external (true)
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (8)
DW_AT_location (DW_OP_addr 0x0)0x00000096: DW_TAG_base_type
DW_AT_name ("int")
DW_AT_encoding (DW_ATE_signed)
DW_AT_byte_size (0x04)0x0000009d: DW_TAG_variable
DW_AT_name ("x")
DW_AT_type (0x00000096 "int")
DW_AT_external (true)
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (8)
DW_AT_location (DW_OP_addr 0x0)0x000000b2: DW_TAG_subprogram
DW_AT_low_pc (0x0000000000000000)
DW_AT_high_pc (0x000000000000002b)
DW_AT_frame_base (DW_OP_reg7 RSP)
DW_AT_GNU_all_call_sites (true)
DW_AT_name ("main")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (9)
DW_AT_type (0x00000096 "int")
DW_AT_external (true)0x000000cb: DW_TAG_GNU_call_site
DW_AT_abstract_origin (0x000000d9 "foo")
DW_AT_low_pc (0x0000000000000026)0x000000d8: NULL
0x000000d9: DW_TAG_subprogram
DW_AT_low_pc (0x0000000000000030)
DW_AT_high_pc (0x0000000000000039)
DW_AT_frame_base (DW_OP_reg7 RSP)
DW_AT_GNU_all_call_sites (true)
DW_AT_name ("foo")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (2)
DW_AT_prototyped (true)
DW_AT_calling_convention (DW_CC_nocall)
DW_AT_type (0x00000060 "long")0x000000f3: DW_TAG_formal_parameter
DW_AT_location (DW_OP_fbreg +8)
DW_AT_name ("u")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (2)
DW_AT_type (0x0000003f "big")0x00000101: DW_TAG_formal_parameter
DW_AT_name ("dead")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (2)
DW_AT_type (0x00000096 "int")0x0000010c: DW_TAG_formal_parameter
DW_AT_location (DW_OP_reg5 RDI)
DW_AT_name ("x")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (2)
DW_AT_type (0x00000096 "int")0x00000119: NULL
0x0000011a: NULL
The following is the critical dwarf:
0x000000d9: DW_TAG_subprogram
DW_AT_low_pc (0x0000000000000030)
DW_AT_high_pc (0x0000000000000039)
DW_AT_frame_base (DW_OP_reg7 RSP)
DW_AT_GNU_all_call_sites (true)
DW_AT_name ("foo")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (2)
DW_AT_prototyped (true)
DW_AT_calling_convention (DW_CC_nocall)
DW_AT_type (0x00000060 "long")0x000000f3: DW_TAG_formal_parameter
DW_AT_location (DW_OP_fbreg +8)
DW_AT_name ("u")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (2)
DW_AT_type (0x0000003f "big")0x00000101: DW_TAG_formal_parameter
DW_AT_name ("dead")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (2)
DW_AT_type (0x00000096 "int")0x0000010c: DW_TAG_formal_parameter
DW_AT_location (DW_OP_reg5 RDI)
DW_AT_name ("x")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (2)
DW_AT_type (0x00000096 "int")0x00000119: NULL
With true_signature:
$ pahole -JV --btf_features=+true_signature t.o
btf_encoder__new: 't.o' doesn't have '.data..percpu' section
File t.o:
[1] UNION big size=24
a type_id=2 bits_offset=0
buf type_id=3 bits_offset=0
[2] INT long size=8 nr_bits=64 encoding=SIGNED
[3] ARRAY (anon) type_id=4 index_type_id=6 nr_elems=24
[4] INT char size=1 nr_bits=8 encoding=SIGNED
[5] INT __ARRAY_SIZE_TYPE__ size=8 nr_bits=64 encoding=(none)
[6] INT int size=4 nr_bits=32 encoding=SIGNED
search cu 't.c' for percpu global variables.
[7] FUNC_PROTO (anon) return=2 args=(1 u, 6 x)
[8] FUNC foo type_id=7
[9] FUNC_PROTO (anon) return=6 args=(void)
[10] FUNC main type_id=9It succeeded with proper true signature.
Without true_signature:
$ pahole -JV t.o
btf_encoder__new: 't.o' doesn't have '.data..percpu' section
File t.o:
[1] UNION big size=24
a type_id=2 bits_offset=0
buf type_id=3 bits_offset=0
[2] INT long size=8 nr_bits=64 encoding=SIGNED
[3] ARRAY (anon) type_id=4 index_type_id=6 nr_elems=24
[4] INT char size=1 nr_bits=8 encoding=SIGNED
[5] INT __ARRAY_SIZE_TYPE__ size=8 nr_bits=64 encoding=(none)
[6] INT int size=4 nr_bits=32 encoding=SIGNED
search cu 't.c' for percpu global variables.
foo : skipping BTF encoding of function due to unexpected register usage for parameter
[7] FUNC_PROTO (anon) return=6 args=(void)
[8] FUNC main type_id=7The 'foo' will be missed due to mismatch between arg's and locations.
Not exactly sure what CI has an issue for the test.
Quoted 80 lines
The
optimized_parmsflag is a bit too broad I think; it is set whenever we are missing location info for _any_ parameter; this may be overly aggressive for clang in particular which seems to have more missing location info.[1] https://github.com/alan-maguire/dwarves/actions/runs/28173261168
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
jirka
dwarfdump:
0x016f4b1b:
DW_TAG_subprogram
DW_AT_low_pc (0xffffffff813e0fc0)
DW_AT_high_pc (0xffffffff813e1133)
DW_AT_frame_base (DW_OP_reg7RSP) DW_AT_GNU_all_call_sites (true)
DW_AT_name ("stop_cpus")
DW_AT_decl_file("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c")DW_AT_decl_line(464)
DW_AT_prototyped (true)
DW_AT_calling_convention (DW_CC_nocall)
DW_AT_type (0x016e94c5"int")0x016f4b36:
DW_TAG_formal_parameter
DW_AT_location (0x0065ad9e:
[0xffffffff813e0fc5,0xffffffff813e0fed):DW_OP_reg5RDI
[0xffffffff813e0fed,0xffffffff813e111e):DW_OP_reg14R14
[0xffffffff813e111e,0xffffffff813e1127):DW_OP_GNU_entry_value(DW_OP_reg5 RDI),DW_OP_stack_value[0xffffffff813e1127,0xffffffff813e1133):DW_OP_reg14R14)
DW_AT_name ("cpumask")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464)
DW_AT_type (0x016f3242"const cpumask *")0x016f4b46:
DW_TAG_formal_parameter
DW_AT_name ("fn")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464)
DW_AT_type (0x016f2ca5"cpu_stop_fn_t")0x016f4b52:
DW_TAG_formal_parameter
DW_AT_location (0x0065ae0d:
[0xffffffff813e0fc5,0xffffffff813e0ff2):DW_OP_reg4RSI
[0xffffffff813e0ff2,0xffffffff813e10e4):DW_OP_reg3RBX
[0xffffffff813e10e4,0xffffffff813e1127):DW_OP_GNU_entry_value(DW_OP_reg4 RSI),DW_OP_stack_value[0xffffffff813e1127,0xffffffff813e112e):DW_OP_reg3RBX
[0xffffffff813e112e,0xffffffff813e1133):DW_OP_GNU_entry_value(DW_OP_reg4 RSI),DW_OP_stack_value) DW_AT_name ("arg")
DW_AT_decl_file ("/home/jolsa/kernel/linux-qemu-2/kernel/stop_machine.c") DW_AT_decl_line (464)
DW_AT_type (0x016e858f"void *")---
btf_encoder.c +4
-1@@ -1564,7 +1564,8 @@ static int btf_encoder__add_true_signature(struct btf_encoder *encoder,*/ if (true_state->unexpected_reg || true_state->uncertain_parm_loc || - true_state->ambiguous_addr) + true_state->ambiguous_addr || + true_state->optimized_parms) continue; err = btf_encoder__add_func(encoder, true_state); if (err < 0)@@ -1656,6 +1657,8 @@ static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder, bool skip_e skip_reason = "reordered parameters\n"; if (state->elf->ambiguous_addr) skip_reason = "ambiguous address\n"; + if (state->optimized_parms) + skip_reason = "optimized parms\n"; if (skip_reason) { btf_encoder__log_func_skip(encoder, saved_fns[i].elf,
Quoted 140 lines · Yonghong Song
Current vmlinux BTF encoding is based on the source level signatures. But the compiler may do some optimization and changed the signature. If the user tried with source level signature, their initial implementation may have wrong results and then the user need to check what is the problem and work around it, e.g. through kprobe since kprobe does not need vmlinux BTF.
Majority of changed signatures are due to dead argument elimination. The following is a more complex one. The original source signature:
typedef struct { union { void *kernel; void __user *user; }; bool is_kernel : 1;}
sockptr_t;
typedefsockptr_tbpfptr_t;
static intmap_create(union bpf_attr *attr, bpfptr_t uattr){ ... }
After compiler optimization, the signature becomes:
static intmap_create(union bpf_attr *attr, bool uattr__is_kernel){ ... }
In the above,uattr__is_kernelcorresponds to 'is_kernel' field insockptr_t. This makes it easier for developers to understand what changed.The new signature needs to properly follow ABI specification based on locations. Otherwise, that signature should be discarded. For example,
0x0242f1f7: DW_TAG_subprogram DW_AT_name ("memblock_find_in_range") DW_AT_calling_convention (DW_CC_nocall) DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f22e: DW_TAG_formal_parameter DW_AT_location (indexed (0x14a) loclist = 0x005595bc: [0xffffffff87a000f9, 0xffffffff87a00178): DW_OP_reg5 RDI [0xffffffff87a00178, 0xffffffff87a001be): DW_OP_reg14 R14 [0xffffffff87a001be, 0xffffffff87a001c7): DW_OP_entry_value(DW_OP_reg5 RDI), DW_OP_stack_value [0xffffffff87a001c7, 0xffffffff87a00214): DW_OP_reg14 R14) DW_AT_name ("start") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f239: DW_TAG_formal_parameter DW_AT_location (indexed (0x14b) loclist = 0x005595e6: [0xffffffff87a000f9, 0xffffffff87a00175): DW_OP_reg4 RSI [0xffffffff87a00175, 0xffffffff87a001b8): DW_OP_reg3 RBX [0xffffffff87a001b8, 0xffffffff87a001c7): DW_OP_entry_value(DW_OP_reg4 RSI), DW_OP_stack_value [0xffffffff87a001c7, 0xffffffff87a00214): DW_OP_reg3 RBX) DW_AT_name ("end") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f245: DW_TAG_formal_parameter DW_AT_location (indexed (0x14c) loclist = 0x00559610: [0xffffffff87a001e3, 0xffffffff87a001ef): DW_OP_breg4 RSI+0) DW_AT_name ("size") DW_AT_type (0x0242decc "phys_addr_t") ... 0x0242f250: DW_TAG_formal_parameter DW_AT_const_value (4096) DW_AT_name ("align") DW_AT_type (0x0242decc "phys_addr_t") ...The third argument should correspond to RDX for
x86_64. But the location suggests that the parameter value is stored in the address with 'RSI + 0'. It is not clear whether the parameter value is stored in RDX or not. So we have to discard this funciton in vmlinux BTF to avoid incorrect true signatures.For llvm, any function having
DW_AT_calling_convention(DW_CC_nocall)
in dwarfDW_TAG_subprogramwill indicate that this function has signature changed. But for nonDW_CC_nocallfunctions, it is possible that true signature still not available due to locations. So every functions will be checked.I did experiment with latest bpf-next. For
x86_64, there are 69103 kernel functions and 875 kernel functions having signature changed. A series of patches are intended to ensure true signatures are properly represented. Eventually, only 20 functions cannot have true signatures due to locations.For arm64, there are 863 kernel functions having signature changed, and 108 functions cannot have true signatures due to locations. I checked those functions and look like llvm arm64 backend more relaxed to compute parameter values.
For full testing, I enabled true signature support in kernel
scripts/Makefile.btflike below:
-pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes +pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=attributes --btf_features=+true_signatureSee individual patches for details.
Changelog:
v9 -> v10:
- v9: https://lore.kernel.org/bpf/[email protected]/
- Remove patch 'btf_encoder: Avoid comparing inlined and out-of-line function prototypes' as it causedbtf_functions.shfailure.
v8 -> v9:
- v8: https://lore.kernel.org/bpf/[email protected]/
- v8 made a mistake where all functions go through main loop infunction__analyze_parameter_locations(). This is not correct as it includes functions whose signature is not changed. See functionfunction__match_clang_parameter_locations().
- Add a change inbtf_encoderto avoid comparing inlined and out-of-line function prototypes. This allows some functions to be in btf.
- Add one more test,clang_parm_optimized_stack_2, where a global function, with many unused parameters, gets properly handling in location checking.
v7 -> v8:
- v7: https://lore.kernel.org/bpf/[email protected]/
- Both Check and Analyze phases go through all functions.
- Requiretrue_signatureinbtf_encoderto have name like <paramName>__<field>.
- Removesignature_changed(from nocall). It is possible that function signatures are not changed but the location reigsters do not match.
v6 -> v7:
- v6: https://lore.kernel.org/bpf/[email protected]/
- Ensure that 'collect' and 'analyze' have the same location checking.
- In 'analyze' stage, undotrue_sig_member_nameif the next expected register matches the previous source type although the previous parameter may
only use half of the value.
v5 -> v6:
- v5: https://lore.kernel.org/bpf/[email protected]/
- The previous change relies onparameter__new()to collect and analyze each parameter to decide true signatures. The new one separates collecting and analyzing phase from Alan. This two-phase makes logic easy to understand.
- Inbtf_encoder.c, remove usage ofskip_idxto simplify the code.
v4 -> v5:
- v4: https://lore.kernel.org/bpf/[email protected]/
- Check info.signature_changed only under clang.
- Fix an uninitialized varable issue (varreg_dix) for gcc.
v3 -> v4:
- v3: https://lore.kernel.org/bpf/[email protected]/
- Add simple prescan of parameter registers in order to get true signatures for those functions where optimization could happen but compiler didn't do it.
- Do not create a new name (e.g. "uattr__is_kernel") with malloc atparameter_reg()stage. Instead remember both "uattr" and "is_kernel" and later generate the name "uattr_is_kernel" in btf encoder.
- Add comments to explain how to handle parameters which may take two registers.
- Fix some test failures on aarch64.
v2 -> v3:
- v2: https://lore.kernel.org/bpf/[email protected]/
- Change tests by using newly addedtest_lib.sh.
- Simplify to get bool variableproducer_clang.
- Try to avoidproducer_clangappearance indwarf_loader.cin order to avoid clear separation between clang and gcc.
Quoted 9 lines
v1 -> v2:
- v1: https://lore.kernel.org/bpf/[email protected]/
- Addedproducer_clangguarding inbtf_encoder. Otherwise, gcc kernel build will crash pahole.
- Fix an early return inparameter__reg()which didn't dopthread_mutex_unlock()which caused the deadlock for arm64.
- Add a few more places to guard withproducer_clangandconf->true_signatureto maintain the previous behavior if not clang orconf->true_signatureis false.
Series applied, thanks for sticking with this, and thanks to Jiri and others for testing!
Alan
Quoted 25 lines
Yonghong Song (5):
dwarf_loader: Detect aggregate ABI register usage and signature
changes
dwarf_loader: Collect per-parameter information
dwarf_loader: Analyze per-parameter information for true signatures
btf_encoder: Emit true function signatures
tests: Add BTFtrue_signatureencoding testsbtf_encoder.c | 26 +- dwarf_loader.c | 603 +++++++++++++++++++++++--- dwarves.h | 14 + tests/clang_parm_aggregate_1.sh | 85 ++++ tests/clang_parm_aggregate_2.sh | 88 ++++ tests/clang_parm_memory.sh | 77 ++++ tests/clang_parm_optimized.sh | 63 +++ tests/clang_parm_optimized_stack_1.sh | 63 +++ tests/clang_parm_optimized_stack_2.sh | 63 +++ 9 files changed, 1017 insertions(+), 65 deletions(-) create mode 100755 tests/clang_parm_aggregate_1.sh create mode 100755 tests/clang_parm_aggregate_2.sh create mode 100755 tests/clang_parm_memory.sh create mode 100755 tests/clang_parm_optimized.sh create mode 100755 tests/clang_parm_optimized_stack_1.sh create mode 100755 tests/clang_parm_optimized_stack_2.sh
Quoted 8 lines · Alan Maguire
Quoted 5 lines · Yonghong Song
Changelog:
v9 -> v10:
- v9: https://lore.kernel.org/bpf/[email protected]/
- Remove patch 'btf_encoder: Avoid comparing inlined and out-of-line function prototypes' as it causedbtf_functions.shfailure.Series applied, thanks for sticking with this, and thanks to Jiri and others for testing!
I'll now rebase my nnext long AI assisted series and retest it over the set of build+test containers, where all the over 80 new tests are passing.
I'll use the opportunity to add more tests to cover the new code in next.
Regards,
- Arnaldo