perf arm64: Support data type profiling
v5 cover perf arm64: Support data type profiling
This patch series implements data type profiling support for arm64, enabling 'perf annotate --data-type' to resolve memory locations and variable types on arm64 platforms.
The main changes since v4 include:
v4: https://lore.kernel.org/all/[email protected]/
* Enhanced Arm64 Instruction Decoding: Added support for parsing extension type (e.g., UXTW, SXTW) and shift amount in arm64 instructions, enabling proper identification of SIB-style indexed addressing modes.
* Unified Base Register Offset Handling: Introduced arch_get_reg_offset() to centralize offset extraction across various addressing modes, including post-index addressing, single-register addressing, SIB-style indexed addressing as well as cases where the base register is a constant type.
* Refined Instruction Tracking Policy: Adjusted instruction filtering logic. All branch and jump instructions that do not modify registers are now skipped. Special load/store instructions are also ignored (currently not supported for tracking).
* Naming Cleanup: Renamed dont_overlap to default_single_event_per_ip and integrated it into itrace_synth_opts__set_default() per Adrian Hunter's suggestion.
Patch organization
==================
The series is organized as follows:
1. Fix disassembly mismatches (Patches 01-02)
Current perf annotate supports three disassembly backends: llvm, capstone, and objdump. On arm64, inconsistencies between the output of these backends (specifically llvm/capstone vs. objdump) often prevent the tracker from correctly identifying registers and offsets. These patches resolve these mismatches, ensuring consistent instruction parsing across all supported backends.
2. Infrastructure for arm64 operand parsing (Patches 03-09) These patches establish the necessary infrastructure for arm64-specific operand handling. This includes implementing new callbacks and data structures to manage arm64's unique addressing modes and register sets. This foundation is essential for the subsequent type-tracking logic.
3. ARM SPE event handling (Patches 10-11)
Patch 09 automatically deduplicates overlapping ARM SPE events (e.g., l1d-miss, tlb-access) in 'perf annotate' by retaining only the "instructions" event when data type profiling is enabled. Patch 10 defaults the synthesized event period to 1 for ARM SPE to fix zero 'Percent' values in annotate output.
5. Core instruction tracking (Patches 12-26)
These patches implement the core logic for type tracking on arm64, covering several key types of instructions, including:
* Memory Access: ldr/str variants (including stack-based access).
* Arithmetic & Data Processing: mov, add, and adrp.
* Special Access: System register access (mrs) and per-cpu variable tracking.
The implementation draws inspiration from the existing x86 logic while adapting it to the nuances of the AArch64 ISA [2][3]. With these changes, perf annotate can successfully resolve memory locations and register types, providing basic support for data type profiling on arm64 platforms.
Example Result
==============
# perf mem record -a -K -- sleep 1
# perf annotate --data-type --stdio --type-stat
Annotate data type stats:
total 1138, ok 842 (74.0%), bad 296 (26.0%)
-----------------------------------------------------------
6 : no_sym
42 : no_var
240 : no_typeinfo
8 : bad_offset
203 : insn_trackAnnotate type: 'struct page' in [kernel.kallsyms] (66948 samples):
============================================================================
Percent offset size field
100.00 0 0x40 struct page {
9.01 0 0x8 long unsigned int flags;
57.99 0x8 0x28 union {
57.99 0x8 0x28 struct {
33.00 0x8 0x10 union {
33.00 0x8 0x10 struct list_head lru {
33.00 0x8 0x8 struct list_head* next;
0.00 0x10 0x8 struct list_head* prev;
};
33.00 0x8 0x10 struct {
33.00 0x8 0x8 void* __filler;
0.00 0x10 0x4 unsigned int mlock_count;...
Each patch's type profiling results are as follows:
Patch | Feature | no_sym | no_var | no_typeinfo | bad_offset | insn_track | ok(%)
------+----------------------------+--------+--------+-------------+------------+------------+------
0011 | base (default spe period) | 6 | 493 | - | - | - | 56.2% 0013 | enable insn tracking | 6 | 42 | 437 | 2 | 12 | 57.2% 0016 | support 'load' insn | 6 | 42 | 398 | 1 | 52 | 60.7% 0017 | support 'store' insn | 6 | 42 | 398 | 1 | 52 | 60.7% 0021 | support stack variable | 6 | 42 | 391 | 1 | 59 | 61.3% 0022 | support 'mov' insn | 6 | 42 | 372 | 3 | 76 | 62.8% 0023 | support 'add' insn | 6 | 42 | 323 | 7 | 121 | 66.8% 0024 | support 'adrp' insn | 6 | 42 | 243 | 8 | 200 | 73.7% 0025 | support per-cpu variable | 6 | 42 | 243 | 8 | 200 | 73.7% 0026 | support 'mrs' insn | 6 | 42 | 240 | 8 | 203 | 74.0%
Limitations
===========
* SIMD/FP & SVE Vector Support:
Data type profiling currently focuses on General-Purpose (GP) register operations. Vector/SIMD registers (v0-v31, d0-d31, q0-q31) and Scalable Vector Extension (SVE/SME) instructions are not tracked yet.
* Compiler Prologue/Epilogue Code:
As shown by the previous test results, approximately 14% of the failed type profiling results originate from compiler-generated prologue or epilogue code (e.g., ldp x19, x20, [sp, #16]). These instructions manage callee-saved registers across function boundaries, propagating type context through these operations requires inter-procedural (cross-function) instruction analysis, which is currently unsupported by the local backward instruction tracker.
Testing
=======
Tested on arm64 (all passed):
# perf test -v "perf data type profiling tests" 81: perf data type profiling tests : Ok
=== Test Summary ===
Passed main tests : 1
Passed subtests : 0 Skipped tests : 0 Failed tests : 0
Tested on x86. The profiling results show no change before/after applying this patch series:
before : total 880, ok 711 (80.8%), bad 169 (19.2%) after : total 880, ok 711 (80.8%), bad 169 (19.2%)
Changelog
=========
v4 -> v5:
- v4: https://lore.kernel.org/all/[email protected]/
- Introduce arch_get_reg_offset() to uniformly handle reg offset.
- Add support for parsing extension type and shift amount in arm64 instructions.
- Refine which instructions to track or skip. (Shuai Xue)
- Rename dont_overlap to default_single_event_per_ip and use it in itrace_synth_opts__set_default(). (Adrian Hunter)
- Introduce delete_stack_state() to clean up obsolete stack state. (Shuai Xue)
- Normalize arch__dwarf_regnum() error return values. (Shuai Xue)
- Drop canary support due to unresolved bugs.
- Fix various minor issues, such as name memory leaks and header includes.
v3 -> v4:
- v3: https://lore.kernel.org/all/[email protected]/
- Fix Capstone compilation failure.
- Stop adding new pcrel_adrp_addr in LLVM; reuse pcrel_load_addr instead.
- Fix parsing issue in arm64_mov__parse.
- Add PC-relative load instruction parsing logic to arm64_ldst__parse, and introduce rstrip_space_and_comment to strip comments.
- Remove wzr/xzr register parsing (not planning to handle this yet).
- Add post-index addressing mode parsing for the '[base], reg' format.
- Update built-in implementation of --itrace=i1i to deduplicate early during arm_spe_process_auxtrace_info.
- Restrict the "default period to 1" behavior to ARM SPE, instead of applying it to all architectures.
- Add register type tracking for function call instructions.
- Add dual-register type tracking for load pair and store pair instructions.
- Correct stack variable offset calculations.
- Add type invalidation upon retry failure.
- Reuse imm_value instead of introducing addr for 'adrp' instruction tracking.
- Fix potential stale type resolution errors caused by TSR_KIND_GLOBAL_ADDR and TSR_KIND_CONST during stack passing.
- Fix a strbuf memory leak during 'mrs' instruction tracking.
- Fix stale dieoff issue when debug info changes.
- Simplify add type propagation: only propagate offset/imm updates, leave type parsing to chk.
v2 -> v3:
- v2: https://lore.kernel.org/all/[email protected]/
- Instead of always parsing the left operand as src and the right operand as dst, set them based on the actual instruction definition. (Namhyung Kim)
- Fix refcount leak in print_capstone_detail().
- Remove useless '<' check when parsing 'addr <symbol>' in arm64_mov__parse().
- Add example comments in arm64_ldst__parse().
- Split arch__dwarf_regnum() changes into a separate commit.
- Rename annotated_addr_mode enum: INSN_ADDR_* -> PERF_ADDR_MODE_*.
- Set caller-saved registers in init_type_state().
- For instructions with addressing mode, always goto adjust_reg_index_state() at the end to update the src register state.
- Handle TSR_KIND_CONST registers for 'mov' and 'add' instructions.
- Invalidate dst register for all other unsupported instructions.
- Verify type DIE is task_struct pointer before caching globally.
- Enable --itrace=i1i by default for ARM SPE data type profiling in 'perf annotate' to avoid overlapping event counting for the same instruction. (James Clark)
- Fix global variable type resolving error in check_matching_type(). (James Clark)
- Address review comments from sashiko [1]:
- Fix unconditional call to arch->extract_op_location()
- Handle multi_regs correctly
- Fix invalid register state in error path
- Other misc fixes
v1 -> v2:
- v1: https://lore.kernel.org/all/[email protected]/
- Fix inconsistencies in arm64 instruction output across llvm, capstone, and objdump disassembly backends.
- Support arm64-specific addressing modes and operand formats. (Leo Yan)
- Extend instruction tracking to support mov and add instructions, along with per-cpu and stack variables.
- Include real-world examples in commit messages to demonstrate practical effects. (Namhyung Kim)
- Improve type-tracking success rate (type stat) from 64.2% to 82.1%.
Please let me know if you have any feedback.
Thanks,
Tengda
[1] https://sashiko.dev/#/patchset/20260403094800.1418825-1-wutengda%40huaweicloud.com
[2] https://developer.arm.com/documentation/102374/0103
[3] https://github.com/flynd/asmsheets/releases/tag/v8
Tengda Wu (26):
perf capstone: Symbolize address operands to match objdump on arm64 perf llvm: Fix arm64 adrp instruction disassembly mismatch with
objdump
perf annotate-arm64: Generalize arm64_mov__parse to support more
instructions
perf annotate-arm64: Handle load and store instructions perf annotate: Normalize arch__dwarf_regnum() error return values perf annotate: Introduce extract_op_location callback for
arch-specific parsing
perf dwarf-regs: Adapt get_dwarf_regnum() for arm64
perf annotate: Adapt arch__dwarf_regnum() for arm64
perf annotate-arm64: Implement extract_op_location() callback perf annotate: Default to --itrace=i1i for data type profiling perf arm-spe: Set default synthesized event period to 1 perf annotate-data: Extract invalidate_reg_state() as a common helper perf annotate-arm64: Enable instruction tracking support perf annotate-data: Add arch_get_reg_offset helper
perf annotate-arm64: Track return type after call instructions perf annotate-arm64: Support load instruction tracking perf annotate-arm64: Support store instruction tracking perf annotate-data: Expand type_state_reg imm_value to u64 perf annotate-data: Track imm_value for stack variables perf annotate-x86: Delete stale stack state on store of untracked
register
perf annotate-arm64: Support stack variable tracking
perf annotate-arm64: Support 'mov' instruction tracking perf annotate-arm64: Support 'add' instruction tracking perf annotate-arm64: Support 'adrp' instruction to track global
variables
perf annotate-arm64: Support per-cpu variable access tracking perf annotate-arm64: Support 'mrs' instruction to track 'current'
pointer
tools/perf/builtin-annotate.c | 8 + .../perf/util/annotate-arch/annotate-arm64.c | 1194 ++++++++++++++++- .../util/annotate-arch/annotate-powerpc.c | 9 + tools/perf/util/annotate-arch/annotate-x86.c | 117 +- tools/perf/util/annotate-data.c | 192 ++- tools/perf/util/annotate-data.h | 14 +- tools/perf/util/annotate.c | 108 +- tools/perf/util/annotate.h | 63 + tools/perf/util/arm-spe.c | 16 +- tools/perf/util/auxtrace.c | 12 +- tools/perf/util/auxtrace.h | 7 +- tools/perf/util/capstone.c | 185 ++- tools/perf/util/cs-etm.c | 2 +- tools/perf/util/disasm.c | 5 + tools/perf/util/disasm.h | 5 + .../util/dwarf-regs-arch/dwarf-regs-arm64.c | 22 + tools/perf/util/dwarf-regs.c | 2 +- tools/perf/util/include/dwarf-regs.h | 1 + tools/perf/util/intel-bts.c | 2 +- tools/perf/util/intel-pt.c | 3 +- tools/perf/util/llvm.c | 52 +- 21 files changed, 1799 insertions(+), 220 deletions(-)
base-commit: af5f12805e5cefa4fe68d6127c7e1fb78cd5535c
Signature · 1 line
2.34.1
PATCH v5 1/26 perf capstone: Symbolize address operands to match objdump on arm64
Capstone currently outputs address-bearing instructions with a raw hex value prefixed by '#', without symbolic resolution. This differs from objdump output and leads to parse failures in jump__parse() and arm64_mov__parse() on arm64.
Example of the mismatch:
Current: b #0xffff8000800114c8
Objdump: b ffff8000800114c8 <el0t_64_sync+0x108>
Current: adrp x18, #0xffff800081f5f000
Objdump: adrp x18, ffff800081f5f000 <this_cpu_vector>
Fix this by extending symbol__disassemble_capstone() to:
- Enable CS_OPT_DETAIL for arm64 to access instruction group info.
- Detect instructions with address operands requiring symbolization (jump/call/branch/adr/adrp) via opcode or Capstone group ID.
- Resolve target addresses to symbols via resolve_symbol_from_addr().
- Format output to match objdump's style (strip '#' prefix, add symbol and offset).
--- tools/perf/util/capstone.c | 185 +++++++++++++++++++++++++++++-------- tools/perf/util/disasm.c | 5 + tools/perf/util/disasm.h | 1 + 3 files changed, 153 insertions(+), 38 deletions(-)
tools/perf/util/capstone.c +147 -38
@@ -3,6 +3,7 @@#include <errno.h> #include <inttypes.h> +#include <stdlib.h> #include <string.h> #include <dlfcn.h>
@@ -31,6 +32,10 @@#define CS_MODE_RISCVC 4#endif +#if CS_VERSION_MAJOR <4 +#define ARM64_GRP_BRANCH_RELATIVE 7/* = CS_GRP_BRANCH_RELATIVE */ +#endif +#ifdef LIBCAPSTONE_DLOPEN static void *perf_cs_dll_handle (void ) {
@@ -225,6 +230,12 @@ static int capstone_init(uint16_t e_machine, csh *cs_handle, bool is64, bool is_* on x86 by investigating instruction details. */ perf_cs_option (*cs_handle,CS_OPT_DETAIL ,CS_OPT_ON ); + }else if (arch ==CS_ARCH_ARM64 ) { +/* +* Same as x86: arm64 needs instruction details to resolve +* symbolic addresses. +*/ +perf_cs_option (*cs_handle,CS_OPT_DETAIL ,CS_OPT_ON ); }return 0 ;
@@ -292,16 +303,44 @@ ssize_t capstone__fprintf_insn_asm(struct machine *machine, struct thread *threareturn printed; } +static int resolve_symbol_from_addr (struct map *map,u64 addr, +const char **sym_name,u64 *sym_offset) +{ +struct map *found_map =NULL ; +struct symbol *sym; +u64 al_addr; +u64 ip =map__objdump_2mem (map, addr); + +if (dso__kernel (map__dso (map))) { +/* +* The kernel maps can be split into sections, let's +* find the map first and then search the symbol. +*/ + found_map =maps__find (map__kmaps (map), ip); +if (found_map ==NULL ) +return -1 ; + map = found_map; + } + +/* convert it to map-relative address for search */ + al_addr =map__map_ip (map, ip); + + sym =map__find_symbol (map, al_addr); +map__put (found_map); + +if (sym ==NULL ) +return -1 ; + + *sym_name = sym->name; + *sym_offset = al_addr - sym->start; +return 0 ; +} +static void print_capstone_detail (struct cs_insn *insn,char *buf,size_t len,struct annotate_args *args,u64 addr) {int i;struct map *map = args->ms->map; -struct symbol *sym; - -/* TODO: support more architectures */ -if (!arch__is_x86 (args->arch)) -return ;if (insn->detail ==NULL )return ;
@@ -309,7 +348,8 @@ static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len,for (i =0 ; i < insn->detail->x86.op_count; i++) {struct cs_x86_op *op = &insn->detail->x86.operands[i];u64 orig_addr; -struct map *found_map =NULL ; +const char *sym_name =NULL ; +u64 sym_offset;if (op->type !=X86_OP_MEM )continue ;
@@ -320,40 +360,114 @@ static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len,/* get the target address */ orig_addr = addr + insn->size + op->mem.disp; - addr =map__objdump_2mem (map, orig_addr); - -if (dso__kernel (map__dso (map))) { -/* -* The kernel maps can be split into sections, let's -* find the map first and then search the symbol. -*/ - found_map =maps__find (map__kmaps (map), addr); -if (found_map ==NULL ) -continue ; - map = found_map; - } - -/* convert it to map-relative address for search */ - addr =map__map_ip (map, addr); - - sym =map__find_symbol (map, addr); -if (sym ==NULL ) { -map__put (found_map); +if (resolve_symbol_from_addr (map, orig_addr, &sym_name, &sym_offset))continue ; - } -if (addr == sym->start) { +if (sym_offset ==0 ) {scnprintf (buf, len,"\t# %" PRIx64" <%s>" , - orig_addr, sym->name); + orig_addr, sym_name); }else {scnprintf (buf, len,"\t# %" PRIx64" <%s+%#" PRIx64">" , - orig_addr, sym->name, addr - sym->start); + orig_addr, sym_name, sym_offset); } -map__put (found_map);break ; } } +static int print_default_format (struct cs_insn *insn,char *buf,size_t len) +{ +return scnprintf (buf, len," %-7s %s" , + insn->mnemonic, insn->op_str); +} + +static void format_capstone_insn_x86 (struct cs_insn *insn,char *buf, +size_t len,struct annotate_args *args, +u64 addr) +{ +int printed; + + printed =print_default_format (insn, buf, len); + buf += printed; + len -= printed; + +print_capstone_detail (insn, buf, len, args, addr); +} + +static bool needs_symbolic_address (struct cs_insn *insn) +{ +int i; + +if (insn->id ==ARM64_INS_ADR || insn->id ==ARM64_INS_ADRP ) +return true; + +if (insn->detail ==NULL ) +return false; + +for (i =0 ; i < insn->detail->groups_count; i++) { +if (insn->detail->groups[i] ==ARM64_GRP_JUMP || + insn->detail->groups[i] ==ARM64_GRP_CALL || + insn->detail->groups[i] ==ARM64_GRP_BRANCH_RELATIVE ) +return true; + } + +return false; +} + +static void format_capstone_insn_arm64 (struct cs_insn *insn,char *buf, +size_t len,struct annotate_args *args) +{ +struct map *map = args->ms->map; +char *last_imm, *endptr; +u64 addr; + +print_default_format (insn, buf, len); +/* +* Adjust instructions to keep the existing behavior with objdump. +* +* Example conversion: +* From: b #0xffff8000800114c8 +* To: b ffff8000800114c8 <el0t_64_sync+0x108> +*/ +if (needs_symbolic_address (insn)) { +const char *sym_name =NULL ; +u64 sym_offset; + +/* Extract last immediate value as address */ + last_imm =strrchr (buf,'#' ); +if (!last_imm) +return ; + + addr =strtoull (last_imm +1 , &endptr,16 ); +if (endptr == last_imm +1 ) +return ; + +if (resolve_symbol_from_addr (map, addr, &sym_name, &sym_offset)) +return ; + +/* Symbolize the resolved address */ + len = len - (last_imm - buf); +if (sym_offset ==0 ) { +scnprintf (last_imm, len,"%" PRIx64" <%s>" , + addr, sym_name); + }else { +scnprintf (last_imm, len,"%" PRIx64" <%s+%#" PRIx64">" , + addr, sym_name, sym_offset); + } + } +} + +static void format_capstone_insn (struct cs_insn *insn,char *buf,size_t len, +struct annotate_args *args,u64 addr) +{ +/* TODO: support more architectures */ +if (arch__is_x86 (args->arch)) +format_capstone_insn_x86 (insn, buf, len, args, addr); +else if (arch__is_arm64 (args->arch)) +format_capstone_insn_arm64 (insn, buf, len, args); +else +print_default_format (insn, buf, len); +} +struct find_file_offset_data {u64 ip;u64 offset;
@@ -446,14 +560,9 @@ int symbol__disassemble_capstone(const char *filename, struct symbol *sym, free_count = count =perf_cs_disasm (handle, buf, buf_len, start, buf_len, &insn);for (i =0 , offset =0 ; i < count; i++) { -int printed; - - printed =scnprintf (disasm_buf,sizeof (disasm_buf), -" %-7s %s" , - insn[i].mnemonic, insn[i].op_str); -print_capstone_detail (&insn[i], disasm_buf + printed, -sizeof (disasm_buf) - printed, args, - start + offset); +format_capstone_insn (&insn[i], disasm_buf, +sizeof (disasm_buf), args, + start + offset); args->offset = offset; args->line = disasm_buf;
tools/perf/util/disasm.c +5 -0
@@ -203,6 +203,11 @@ bool arch__is_powerpc(const struct arch *arch)return arch->id.e_machine ==EM_PPC || arch->id.e_machine ==EM_PPC64 ; } +bool arch__is_arm64 (const struct arch *arch) +{ +return arch->id.e_machine ==EM_AARCH64 ; +} +static void ins_ops__delete (struct ins_operands *ops) {if (ops ==NULL )
tools/perf/util/disasm.h +1 -0
@@ -111,6 +111,7 @@ struct annotate_args {const struct arch *arch__find (uint16_t e_machine,uint32_t e_flags,const char *cpuid);bool arch__is_x86 (const struct arch *arch);bool arch__is_powerpc (const struct arch *arch); +bool arch__is_arm64 (const struct arch *arch);extern const struct ins_ops call_ops;extern const struct ins_ops dec_ops;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 01/26] perf capstone: Symbolize address operands to match objdump on arm64
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Premature `map__put()` of `found_map` before the caller consumes `sym_name` creates a…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Premature map__put() of found_map before the caller consumes sym_name creates a Use-After-Free vulnerability. --
commit ff403b9f1717375044e9b6fbb3f09210431e386b
Author: Tengda Wu <[email protected]>
perf capstone: Symbolize address operands to match objdump on arm64
This commit extends Capstone disassembly support on arm64 to resolve target addresses to symbols. It detects instructions with address operands, maps them to symbols, and formats the output to match objdump's style.
Quoted 4 lines
tools/perf/util/capstone.c +0
-0
[ ... ]
Quoted 26 lines
tools/perf/util/capstone.c +22
-0@@ -292,16 +303,44 @@ ssize_t capstone__fprintf_insn_asm(struct machine *machine, struct thread *threareturn printed; } +static int resolve_symbol_from_addr (struct map *map,u64 addr, +const char **sym_name,u64 *sym_offset) +{ +struct map *found_map =NULL ; +struct symbol *sym; +u64 al_addr; +u64 ip =map__objdump_2mem (map, addr); + +if (dso__kernel (map__dso (map))) { +/* +* The kernel maps can be split into sections, let's +* find the map first and then search the symbol. +*/ + found_map =maps__find (map__kmaps (map), ip); +if (found_map ==NULL ) +return -1 ; + map = found_map; + } + +/* convert it to map-relative address for search */ + al_addr =map__map_ip (map, ip); +
tools/perf/util/capstone.c ssize_t capstone__fprintf_insn_asm(struct machine *machine, struct thread *threa · line 337
Quoted 10 lines
tools/perf/util/capstone.c +10
-0+ sym =map__find_symbol (map, al_addr); +map__put (found_map); + +if (sym ==NULL ) +return -1 ; + + *sym_name = sym->name; + *sym_offset = al_addr - sym->start; +return 0 ; +}
[Severity: Medium]
Is map__put(found_map) called prematurely here? If the map is concurrently removed and the refcount drops to zero, the map and its symbols could be freed. Does this leave sym->name pointing to freed memory when it is assigned to *sym_name and returned to the caller?
Quoted 4 lines
tools/perf/util/capstone.c +1
-0+static void print_capstone_detail (struct cs_insn *insn,char *buf,size_t len,struct annotate_args *args,u64 addr) {
[ ... ]
Quoted 22 lines
tools/perf/util/capstone.c +0
-18@@ -320,40 +360,114 @@ static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len,/* get the target address */ orig_addr = addr + insn->size + op->mem.disp; - addr =map__objdump_2mem (map, orig_addr); - -if (dso__kernel (map__dso (map))) { -/* -* The kernel maps can be split into sections, let's -* find the map first and then search the symbol. -*/ - found_map =maps__find (map__kmaps (map), addr); -if (found_map ==NULL ) -continue ; - map = found_map; - } - -/* convert it to map-relative address for search */ - addr =map__map_ip (map, addr); - - sym =map__find_symbol (map, addr); -if (sym ==NULL ) {
tools/perf/util/capstone.c static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len, · line 368
Quoted 10 lines
tools/perf/util/capstone.c +3
-4-map__put (found_map); +if (resolve_symbol_from_addr (map, orig_addr, &sym_name, &sym_offset))continue ; - } -if (addr == sym->start) { +if (sym_offset ==0 ) {scnprintf (buf, len,"\t# %" PRIx64" <%s>" , - orig_addr, sym->name); + orig_addr, sym_name);
[Severity: Medium]
Can this result in a Use-After-Free? If resolve_symbol_from_addr() drops the map reference too early, sym_name might point to freed memory by the time it is dereferenced here in scnprintf().
Quoted 5 lines
tools/perf/util/capstone.c +1
-1}else {scnprintf (buf, len,"\t# %" PRIx64" <%s+%#" PRIx64">" , - orig_addr, sym->name, addr - sym->start); + orig_addr, sym_name, sym_offset); }
[ ... ]
Quoted 26 lines
tools/perf/util/capstone.c +26
-0+static void format_capstone_insn_arm64 (struct cs_insn *insn,char *buf, +size_t len,struct annotate_args *args) +{ +struct map *map = args->ms->map; +char *last_imm, *endptr; +u64 addr; + +print_default_format (insn, buf, len); +/* +* Adjust instructions to keep the existing behavior with objdump. +* +* Example conversion: +* From: b #0xffff8000800114c8 +* To: b ffff8000800114c8 <el0t_64_sync+0x108> +*/ +if (needs_symbolic_address (insn)) { +const char *sym_name =NULL ; +u64 sym_offset; + +/* Extract last immediate value as address */ + last_imm =strrchr (buf,'#' ); +if (!last_imm) +return ; + + addr =strtoull (last_imm +1 , &endptr,16 ); +if (endptr == last_imm +1 )
tools/perf/util/capstone.c static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len, · line 451
Quoted 10 lines
tools/perf/util/capstone.c +10
-0+return ; + +if (resolve_symbol_from_addr (map, addr, &sym_name, &sym_offset)) +return ; + +/* Symbolize the resolved address */ + len = len - (last_imm - buf); +if (sym_offset ==0 ) { +scnprintf (last_imm, len,"%" PRIx64" <%s>" , + addr, sym_name);
[Severity: Medium]
Does this also dereference the potentially dangling sym_name pointer if the underlying map was freed in resolve_symbol_from_addr()?
Quoted 6 lines
tools/perf/util/capstone.c +6
-0+ }else { +scnprintf (last_imm, len,"%" PRIx64" <%s+%#" PRIx64">" , + addr, sym_name, sym_offset); + } + } +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Namhyung Kim
maintainerRe: [PATCH v5 01/26] perf capstone: Symbolize address operands to match objdump on arm64
Hello,
Quoted 51 lines · On Tue, Sep 08, 2026 at 01:18:03PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Prematuremap__put()offound_mapbefore the caller consumessym_namecreates a Use-After-Free vulnerability. --commit
ff403b9f1717375044e9b6fbb3f09210431e386b
Author: Tengda Wu <[email protected]>
perf capstone: Symbolize address operands to match objdump on arm64This commit extends Capstone disassembly support on arm64 to resolve target addresses to symbols. It detects instructions with address operands, maps them to symbols, and formats the output to match objdump's style.
Quoted 4 lines
tools/perf/util/capstone.c +0
-0[ ... ]
Quoted 34 lines
tools/perf/util/capstone.c +30
-0@@ -292,16 +303,44 @@ ssize_t capstone__fprintf_insn_asm(struct machine *machine, struct thread *threareturn printed; } +static int resolve_symbol_from_addr (struct map *map,u64 addr, +const char **sym_name,u64 *sym_offset) +{ +struct map *found_map =NULL ; +struct symbol *sym; +u64 al_addr; +u64 ip =map__objdump_2mem (map, addr); + +if (dso__kernel (map__dso (map))) { +/* +* The kernel maps can be split into sections, let's +* find the map first and then search the symbol. +*/ + found_map =maps__find (map__kmaps (map), ip); +if (found_map ==NULL ) +return -1 ; + map = found_map; + } + +/* convert it to map-relative address for search */ + al_addr =map__map_ip (map, ip); + + sym =map__find_symbol (map, al_addr); +map__put (found_map); + +if (sym ==NULL ) +return -1 ; + + *sym_name = sym->name; + *sym_offset = al_addr - sym->start;
Quoted 8 lines
tools/perf/util/capstone.c ssize_t capstone__fprintf_insn_asm(struct machine *machine, struct thread *threa · line 337
Quoted 2 lines
tools/perf/util/capstone.c +2
-0+return 0 ; +}[Severity: Medium]
Ismap__put(found_map)called prematurely here? If the map is concurrently removed and the refcount drops to zero, the map and its symbols could be freed. Does this leavesym->namepointing to freed memory when it is assigned to*sym_nameand returned to the caller?
Right, probably better to return a pointer to symbol instead and make the found_map as an out parameter.
Thanks,
Namhyung
Quoted 100 lines
Quoted 5 lines
tools/perf/util/capstone.c +1
-0+static void print_capstone_detail (struct cs_insn *insn,char *buf,size_t len,struct annotate_args *args,u64 addr) {[ ... ]
Quoted 22 lines
tools/perf/util/capstone.c +0
-18@@ -320,40 +360,114 @@ static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len,/* get the target address */ orig_addr = addr + insn->size + op->mem.disp; - addr =map__objdump_2mem (map, orig_addr); - -if (dso__kernel (map__dso (map))) { -/* -* The kernel maps can be split into sections, let's -* find the map first and then search the symbol. -*/ - found_map =maps__find (map__kmaps (map), addr); -if (found_map ==NULL ) -continue ; - map = found_map; - } - -/* convert it to map-relative address for search */ - addr =map__map_ip (map, addr); - - sym =map__find_symbol (map, addr); -if (sym ==NULL ) {tools/perf/util/capstone.c static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len, · line 368
Quoted 10 lines
tools/perf/util/capstone.c +3
-4-map__put (found_map); +if (resolve_symbol_from_addr (map, orig_addr, &sym_name, &sym_offset))continue ; - } -if (addr == sym->start) { +if (sym_offset ==0 ) {scnprintf (buf, len,"\t# %" PRIx64" <%s>" , - orig_addr, sym->name); + orig_addr, sym_name);[Severity: Medium]
Can this result in a Use-After-Free? Ifresolve_symbol_from_addr()drops the map reference too early,sym_namemight point to freed memory by the time it is dereferenced here inscnprintf().Quoted 5 lines
tools/perf/util/capstone.c +1
-1}else {scnprintf (buf, len,"\t# %" PRIx64" <%s+%#" PRIx64">" , - orig_addr, sym->name, addr - sym->start); + orig_addr, sym_name, sym_offset); }[ ... ]
Quoted 26 lines
tools/perf/util/capstone.c +26
-0+static void format_capstone_insn_arm64 (struct cs_insn *insn,char *buf, +size_t len,struct annotate_args *args) +{ +struct map *map = args->ms->map; +char *last_imm, *endptr; +u64 addr; + +print_default_format (insn, buf, len); +/* +* Adjust instructions to keep the existing behavior with objdump. +* +* Example conversion: +* From: b #0xffff8000800114c8 +* To: b ffff8000800114c8 <el0t_64_sync+0x108> +*/ +if (needs_symbolic_address (insn)) { +const char *sym_name =NULL ; +u64 sym_offset; + +/* Extract last immediate value as address */ + last_imm =strrchr (buf,'#' ); +if (!last_imm) +return ; + + addr =strtoull (last_imm +1 , &endptr,16 ); +if (endptr == last_imm +1 )tools/perf/util/capstone.c static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len, · line 451
Quoted 10 lines
tools/perf/util/capstone.c +10
-0+return ; + +if (resolve_symbol_from_addr (map, addr, &sym_name, &sym_offset)) +return ; + +/* Symbolize the resolved address */ + len = len - (last_imm - buf); +if (sym_offset ==0 ) { +scnprintf (last_imm, len,"%" PRIx64" <%s>" , + addr, sym_name);[Severity: Medium]
Does this also dereference the potentially danglingsym_namepointer if the underlying map was freed inresolve_symbol_from_addr()?Quoted 6 lines
tools/perf/util/capstone.c +6
-0+ }else { +scnprintf (last_imm, len,"%" PRIx64" <%s+%#" PRIx64">" , + addr, sym_name, sym_offset); + } + } +}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v5 01/26] perf capstone: Symbolize address operands to match objdump on arm64
Quoted 63 lines · Namhyung Kim
Hello,
Quoted 51 lines · On Tue, Sep 08, 2026 at 01:18:03PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Prematuremap__put()offound_mapbefore the caller consumessym_namecreates a Use-After-Free vulnerability. --commit
ff403b9f1717375044e9b6fbb3f09210431e386b
Author: Tengda Wu <[email protected]>
perf capstone: Symbolize address operands to match objdump on arm64This commit extends Capstone disassembly support on arm64 to resolve target addresses to symbols. It detects instructions with address operands, maps them to symbols, and formats the output to match objdump's style.
Quoted 4 lines
tools/perf/util/capstone.c +0
-0[ ... ]
Quoted 34 lines
tools/perf/util/capstone.c +30
-0@@ -292,16 +303,44 @@ ssize_t capstone__fprintf_insn_asm(struct machine *machine, struct thread *threareturn printed; } +static int resolve_symbol_from_addr (struct map *map,u64 addr, +const char **sym_name,u64 *sym_offset) +{ +struct map *found_map =NULL ; +struct symbol *sym; +u64 al_addr; +u64 ip =map__objdump_2mem (map, addr); + +if (dso__kernel (map__dso (map))) { +/* +* The kernel maps can be split into sections, let's +* find the map first and then search the symbol. +*/ + found_map =maps__find (map__kmaps (map), ip); +if (found_map ==NULL ) +return -1 ; + map = found_map; + } + +/* convert it to map-relative address for search */ + al_addr =map__map_ip (map, ip); + + sym =map__find_symbol (map, al_addr); +map__put (found_map); + +if (sym ==NULL ) +return -1 ; + + *sym_name = sym->name; + *sym_offset = al_addr - sym->start;Quoted 8 lines
tools/perf/util/capstone.c ssize_t capstone__fprintf_insn_asm(struct machine *machine, struct thread *threa · line 337
Quoted 2 lines
tools/perf/util/capstone.c +2
-0+return 0 ; +}[Severity: Medium]
Ismap__put(found_map)called prematurely here? If the map is concurrently removed and the refcount drops to zero, the map and its symbols could be freed. Does this leavesym->namepointing to freed memory when it is assigned to*sym_nameand returned to the caller?
Quoted 6 lines
Right, probably better to return a pointer to symbol instead and make the
found_mapas an out parameter.Thanks,
Namhyung
Agreed. We probably also need to make sym_offset an out parameter, because struct symbol does not contain offset information, and when printing we need to use sym_offset to determine whether to append the '+offset' suffix. So the function signature might be changed to something like this:
static struct symbol *find_symbol_from_addr(struct map *map, u64 addr,
struct map **found_map, u64 *sym_offset)
Is it okay to change it this way?
Thanks,
Tengda
Quoted 100 lines
Quoted 100 lines
Quoted 5 lines
tools/perf/util/capstone.c +1
-0+static void print_capstone_detail (struct cs_insn *insn,char *buf,size_t len,struct annotate_args *args,u64 addr) {[ ... ]
Quoted 22 lines
tools/perf/util/capstone.c +0
-18@@ -320,40 +360,114 @@ static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len,/* get the target address */ orig_addr = addr + insn->size + op->mem.disp; - addr =map__objdump_2mem (map, orig_addr); - -if (dso__kernel (map__dso (map))) { -/* -* The kernel maps can be split into sections, let's -* find the map first and then search the symbol. -*/ - found_map =maps__find (map__kmaps (map), addr); -if (found_map ==NULL ) -continue ; - map = found_map; - } - -/* convert it to map-relative address for search */ - addr =map__map_ip (map, addr); - - sym =map__find_symbol (map, addr); -if (sym ==NULL ) {tools/perf/util/capstone.c static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len, · line 368
Quoted 10 lines
tools/perf/util/capstone.c +3
-4-map__put (found_map); +if (resolve_symbol_from_addr (map, orig_addr, &sym_name, &sym_offset))continue ; - } -if (addr == sym->start) { +if (sym_offset ==0 ) {scnprintf (buf, len,"\t# %" PRIx64" <%s>" , - orig_addr, sym->name); + orig_addr, sym_name);[Severity: Medium]
Can this result in a Use-After-Free? Ifresolve_symbol_from_addr()drops the map reference too early,sym_namemight point to freed memory by the time it is dereferenced here inscnprintf().Quoted 5 lines
tools/perf/util/capstone.c +1
-1}else {scnprintf (buf, len,"\t# %" PRIx64" <%s+%#" PRIx64">" , - orig_addr, sym->name, addr - sym->start); + orig_addr, sym_name, sym_offset); }[ ... ]
Quoted 26 lines
tools/perf/util/capstone.c +26
-0+static void format_capstone_insn_arm64 (struct cs_insn *insn,char *buf, +size_t len,struct annotate_args *args) +{ +struct map *map = args->ms->map; +char *last_imm, *endptr; +u64 addr; + +print_default_format (insn, buf, len); +/* +* Adjust instructions to keep the existing behavior with objdump. +* +* Example conversion: +* From: b #0xffff8000800114c8 +* To: b ffff8000800114c8 <el0t_64_sync+0x108> +*/ +if (needs_symbolic_address (insn)) { +const char *sym_name =NULL ; +u64 sym_offset; + +/* Extract last immediate value as address */ + last_imm =strrchr (buf,'#' ); +if (!last_imm) +return ; + + addr =strtoull (last_imm +1 , &endptr,16 ); +if (endptr == last_imm +1 )tools/perf/util/capstone.c static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len, · line 451
Quoted 10 lines
tools/perf/util/capstone.c +10
-0+return ; + +if (resolve_symbol_from_addr (map, addr, &sym_name, &sym_offset)) +return ; + +/* Symbolize the resolved address */ + len = len - (last_imm - buf); +if (sym_offset ==0 ) { +scnprintf (last_imm, len,"%" PRIx64" <%s>" , + addr, sym_name);[Severity: Medium]
Does this also dereference the potentially danglingsym_namepointer if the underlying map was freed inresolve_symbol_from_addr()?Quoted 6 lines
tools/perf/util/capstone.c +6
-0+ }else { +scnprintf (last_imm, len,"%" PRIx64" <%s+%#" PRIx64">" , + addr, sym_name, sym_offset); + } + } +}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v5 2/26 perf llvm: Fix arm64 adrp instruction disassembly mismatch with objdump
In reply to Tengda WuThe operands of 'adrp' instructions parsed by libllvm are currently represented as raw immediates rather than the "address <symbol+offset>" format used by objdump. This inconsistency causes arm64_mov__parse() to fail when parsing these instructions during post-processing.
Example of the mismatch:
Current: adrp x18, 8014
Objdump: adrp x18, ffff800081f5f000 <this_cpu_vector>
Fix this by manually extracting the target address from the raw adrp instruction via symbol_lookup_callback(). The address is then converted to a specific symbol during symbol__disassemble_llvm() and formatted to match objdump's output, ensuring compatibility with existing parsers.
--- tools/perf/util/llvm.c | 52 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 5 deletions(-)
tools/perf/util/llvm.c +47 -5
@@ -9,8 +9,10 @@#include "symbol.h" #include <errno.h> #include <fcntl.h> +#include <string.h> #include <unistd.h> #include <linux/zalloc.h> +#include <linux/bitops.h> #ifdef HAVE_LIBLLVM_SUPPORT #include "llvm-c-helpers.h"
@@ -108,6 +110,16 @@ symbol_lookup_callback(void *disinfo, uint64_t value, storage->branch_addr = value; else if (*ref_type == LLVMDisassembler_ReferenceType_In_PCrel_Load) storage->pcrel_load_addr = value; + else if (*ref_type == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) { +uint64_t adrp_imm; + +/* immhi (bits 23:5) and immlo (bits 30:29) */ + adrp_imm = ((value &0x00ffffe0 ) >>3 ) | ((value >>29 ) &0x3 ); + adrp_imm =sign_extend64 (adrp_imm,20 ); + +/* Calculate the target page address */ + storage->pcrel_load_addr = (address & ~0xfffULL ) + (adrp_imm <<12 ); + } *ref_type = LLVMDisassembler_ReferenceType_InOut_None;return NULL ; }
@@ -228,12 +240,42 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym, } }if (storage.pcrel_load_addr !=0 ) { -char *name =llvm_name_for_data (dso, filename, +char *name; + +if (arch__is_arm64 (args->arch)) { +/* +* For arm64, replace the immediate operand with +* the resolved address to match objdump's output. +* +* Example conversion: +* From: adrp x18, 8014 +* To: adrp x18, ffff800081f5f000 +*/ +char *s =strchr (disasm_buf,',' ); + +if (s ==NULL ) + goto err; + + s++; + *s ='\0' ; + disasm_len =strlen (disasm_buf); + disasm_len +=scnprintf (disasm_buf + disasm_len, +sizeof (disasm_buf) - disasm_len, +" %" PRIx64, storage.pcrel_load_addr); - disasm_len +=scnprintf (disasm_buf + disasm_len, -sizeof (disasm_buf) - disasm_len, -" # %#" PRIx64, - storage.pcrel_load_addr); + }else { +/* +* For other archs, append the resolved address +* as an inline comment. +*/ + disasm_len +=scnprintf (disasm_buf + disasm_len, +sizeof (disasm_buf) - disasm_len, +" # %#" PRIx64, + storage.pcrel_load_addr); + } + +/* Append the resolved symbol name if available */ + name =llvm_name_for_data (dso, filename, storage.pcrel_load_addr);if (name) { disasm_len += scnprintf(disasm_buf + disasm_len, sizeof(disasm_buf) -
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 02/26] perf llvm: Fix arm64 adrp instruction disassembly mismatch with objdump
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
The operands of 'adrp' instructions parsed by libllvm are currently represented as raw immediates rather than the "address <symbol+offset>" format used by objdump. This inconsistency causes
arm64_mov__parse()to fail when parsing these instructions during post-processing.Example of the mismatch:
Current: adrp x18, 8014
Objdump: adrp x18,ffff800081f5f000<this_cpu_vector>
Quoted 7 lines
Fix this by manually extracting the target address from the raw adrp instruction via
symbol_lookup_callback(). The address is then converted to a specific symbol duringsymbol__disassemble_llvm()and formatted to match objdump's output, ensuring compatibility with existing parsers.Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v5 02/26] perf llvm: Fix arm64 adrp instruction disassembly mismatch with objdump
In reply to Tengda WuQuoted 9 lines · Tengda Wu
The operands of 'adrp' instructions parsed by libllvm are currently represented as raw immediates rather than the "address <symbol+offset>" format used by objdump. This inconsistency causes
arm64_mov__parse()to fail when parsing these instructions during post-processing.Example of the mismatch:
Current: adrp x18, 8014
Objdump: adrp x18,ffff800081f5f000<this_cpu_vector>
Quoted 7 lines
Fix this by manually extracting the target address from the raw adrp instruction via
symbol_lookup_callback(). The address is then converted to a specific symbol duringsymbol__disassemble_llvm()and formatted to match objdump's output, ensuring compatibility with existing parsers.Signed-off-by Tengda Wu <[email protected]>
Thanks,
Namhyung
PATCH v5 3/26 perf annotate-arm64: Generalize arm64_mov__parse to support more instructions
In reply to Tengda WuAs the default parser for arm64 instructions, arm64_mov__parse() currently only supports parsing instructions with the 'addr <symbol>' suffix. Other instructions are not supported, causing a lack of source and target information for most regular instructions.
Generalize arm64_mov__parse to extend its parsing capabilities:
1. Parse 'ops->target.raw' and 'ops->source.raw' by default.
2. Utilize arm64__check_multi_regs() to identify if source and target contain multiple registers.
3. Maintain backward compatibility for instructions containing the 'addr <symbol>' suffix.
--- .../perf/util/annotate-arch/annotate-arm64.c | 166 ++++++++++++++---- 1 file changed, 134 insertions(+), 32 deletions(-)
tools/perf/util/annotate-arch/annotate-arm64.c +134 -32
@@ -3,6 +3,8 @@#include <errno.h> #include <stdlib.h> #include <string.h> +#include <linux/ctype.h> +#include <linux/string.h> #include <linux/zalloc.h> #include <regex.h> #include "../annotate.h"
@@ -14,59 +16,158 @@ struct arch_arm64 {regex_t jump_insn; }; -static int arm64_mov__parse (const struct arch *arch__maybe_unused , +static bool arm64__is_reg (const char *op) +{ +if (!op || !*op) +return false; + +/* +* General-purpose registers: x0-x30, w0-w30. +* Check for 'x' or 'w' prefix followed by a numeric index. +*/ +if ((op[0 ] =='x' || op[0 ] =='w' ) &&isdigit (op[1 ])) +return true; + +/* Special-purpose registers: sp, wzr, xzr. */ +if (!strncmp (op,"sp" ,2 ) || !strncmp (op,"xzr" ,3 ) || + !strncmp (op,"wzr" ,3 )) +return true; + +/* TODO: Support more registers. */ +return false; +} + +static bool arm64__check_multi_regs (const struct arch *arch,const char *op) +{ +const char *p = op; +int reg_count =0 ; + +while (p && *p) { + p =skip_spaces (p); +if (*p == arch->objdump.memory_ref_char) + p++; + +if (arm64__is_reg (p)) + reg_count++; + +if (reg_count >=2 ) +return true; + +/* Move to next operand after comma */ + p =strchr (p,',' ); +if (p) + p++; + } + +return false; +} + +/* +* Duplicate @insn, stripping the comment and trailing whitespace. +* Returns a newly allocated string which the caller must free(), +* or NULL on allocation failure or if @insn is NULL. +*/ +static char *rstrip_space_and_comment (const char *insn,char comment_char) +{ +const char *end, *comment; +size_t len; +char *result; + +if (insn ==NULL ) +return NULL ; + + comment =strchr (insn, comment_char); +if (comment !=NULL ) + end = comment; +else + end = insn +strlen (insn); + +while (end > insn &&isspace (end[-1 ])) + --end; + + len = end - insn; + result =malloc (len +1 ); +if (result ==NULL ) +return NULL ; + +memcpy (result, insn, len); + result[len] ='\0' ; + +return result; +} + +static int arm64_mov__parse (const struct arch *arch,struct ins_operands *ops,struct map_symbol *ms__maybe_unused ,struct disasm_line *dl__maybe_unused ) { -char *s =strchr (ops->raw,',' ), *target, *endptr; +char *s =strchr (ops->raw,',' ), *source, *endptr;if (s ==NULL )return -1 ; +/* Parse target */ *s ='\0' ; - ops->source.raw =strdup (ops->raw); + ops->target.raw =strdup (ops->raw); *s =',' ; -if (ops->source.raw ==NULL ) -return -1 ; - - target = ++s; - ops->target.raw =strdup (target);if (ops->target.raw ==NULL ) - goto out_free_source; +return -1 ; - ops->target.addr =strtoull (target, &endptr,16 ); -if (endptr == target) - goto out_free_target; + ops->target.multi_regs =arm64__check_multi_regs (arch, ops->target.raw); - s =strchr (endptr,'<' ); -if (s ==NULL ) - goto out_free_target; - endptr =strchr (s +1 ,'>' ); -if (endptr ==NULL ) - goto out_free_target; - - *endptr ='\0' ; - *s =' ' ; - ops->target.name =strdup (s); - *s ='<' ; - *endptr ='>' ; -if (ops->target.name ==NULL ) - goto out_free_target; +/* Parse source, stripping comment if present */ + source =skip_spaces (++s); + ops->source.raw =rstrip_space_and_comment (source, arch->objdump.comment_char); +if (ops->source.raw ==NULL ) { +zfree (&ops->target.raw); +return -1 ; + } + + ops->source.multi_regs =arm64__check_multi_regs (arch, ops->source.raw); + +/* +* Parse 'addr <symbol>' from source (if any). +* +* A raw hex string may be a scalar immediate, not an address. +* Only validate 'source.addr' if accompanied by a '<symbol>' tag, +* otherwise reset it to 0 to avoid false positive address tracking. +*/ + ops->source.addr =strtoull (ops->source.raw, &endptr,16 ); +if (endptr != ops->source.raw) { + s =strchr (endptr,'<' ); +if (s ==NULL ) { + ops->source.addr =0 ; +return 0 ; + } + endptr =strrchr (s +1 ,'>' ); +if (endptr ==NULL ) { + ops->source.addr =0 ; +return 0 ; + } + + *endptr ='\0' ; + ops->source.name =strdup (s +1 ); + *endptr ='>' ; +if (ops->source.name ==NULL ) { + ops->source.addr =0 ; +return 0 ; + } + }return 0 ; +} -out_free_target: -zfree (&ops->target.raw); -out_free_source: -zfree (&ops->source.raw); -return -1 ; +static int arm64_mov__scnprintf (const struct ins *ins,char *bf,size_t size, +struct ins_operands *ops,int max_ins_name) +{ +return scnprintf (bf, size,"%-*s %s, %s" , max_ins_name, ins->name, + ops->target.raw, ops->source.name ?: ops->source.raw); }static const struct ins_ops arm64_mov_ops = { .parse = arm64_mov__parse, - .scnprintf = mov__scnprintf, + .scnprintf = arm64_mov__scnprintf, };static const struct ins_ops *arm64__associate_instruction_ops (struct arch *arch,const char *name)
@@ -103,6 +204,7 @@ const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id, arch->id = *id; arch->objdump.comment_char ='/' ; arch->objdump.skip_functions_char ='+' ; + arch->objdump.memory_ref_char ='[' ; arch->associate_instruction_ops = arm64__associate_instruction_ops;/* bl, blr */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 03/26] perf annotate-arm64: Generalize arm64_mov__parse to support more instructions
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines
As the default parser for arm64 instructions,
arm64_mov__parse()currently only supports parsing instructions with the 'addr <symbol>' suffix. Other instructions are not supported, causing a lack of source and target information for most regular instructions.
Quoted 8 lines
Generalize
arm64_mov__parseto extend its parsing capabilities:
1. Parse 'ops->target.raw' and 'ops->source.raw' by default.
2. Utilizearm64__check_multi_regs()to identify if source and target contain multiple registers.
3. Maintain backward compatibility for instructions containing the 'addr <symbol>' suffix.Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v5 03/26] perf annotate-arm64: Generalize arm64_mov__parse to support more instructions
In reply to Tengda WuQuoted 5 lines · Tengda Wu
As the default parser for arm64 instructions,
arm64_mov__parse()currently only supports parsing instructions with the 'addr <symbol>' suffix. Other instructions are not supported, causing a lack of source and target information for most regular instructions.
Quoted 8 lines
Generalize
arm64_mov__parseto extend its parsing capabilities:
1. Parse 'ops->target.raw' and 'ops->source.raw' by default.
2. Utilizearm64__check_multi_regs()to identify if source and target contain multiple registers.
3. Maintain backward compatibility for instructions containing the 'addr <symbol>' suffix.Signed-off-by Tengda Wu <[email protected]>
Thanks,
Namhyung
Quoted 219 lines
--- .../perf/util/annotate-arch/annotate-arm64.c | 166 ++++++++++++++---- 1 file changed, 134 insertions(+), 32 deletions(-)tools/perf/util/annotate-arch/annotate-arm64.c +134
-32@@ -3,6 +3,8 @@#include <errno.h> #include <stdlib.h> #include <string.h> +#include <linux/ctype.h> +#include <linux/string.h> #include <linux/zalloc.h> #include <regex.h> #include "../annotate.h" @@ -14,59 +16,158 @@ struct arch_arm64 {regex_t jump_insn; }; -static int arm64_mov__parse (const struct arch *arch__maybe_unused , +static bool arm64__is_reg (const char *op) +{ +if (!op || !*op) +return false; + +/* +* General-purpose registers: x0-x30, w0-w30. +* Check for 'x' or 'w' prefix followed by a numeric index. +*/ +if ((op[0 ] =='x' || op[0 ] =='w' ) &&isdigit (op[1 ])) +return true; + +/* Special-purpose registers: sp, wzr, xzr. */ +if (!strncmp (op,"sp" ,2 ) || !strncmp (op,"xzr" ,3 ) || + !strncmp (op,"wzr" ,3 )) +return true; + +/* TODO: Support more registers. */ +return false; +} + +static bool arm64__check_multi_regs (const struct arch *arch,const char *op) +{ +const char *p = op; +int reg_count =0 ; + +while (p && *p) { + p =skip_spaces (p); +if (*p == arch->objdump.memory_ref_char) + p++; + +if (arm64__is_reg (p)) + reg_count++; + +if (reg_count >=2 ) +return true; + +/* Move to next operand after comma */ + p =strchr (p,',' ); +if (p) + p++; + } + +return false; +} + +/* +* Duplicate @insn, stripping the comment and trailing whitespace. +* Returns a newly allocated string which the caller must free(), +* or NULL on allocation failure or if @insn is NULL. +*/ +static char *rstrip_space_and_comment (const char *insn,char comment_char) +{ +const char *end, *comment; +size_t len; +char *result; + +if (insn ==NULL ) +return NULL ; + + comment =strchr (insn, comment_char); +if (comment !=NULL ) + end = comment; +else + end = insn +strlen (insn); + +while (end > insn &&isspace (end[-1 ])) + --end; + + len = end - insn; + result =malloc (len +1 ); +if (result ==NULL ) +return NULL ; + +memcpy (result, insn, len); + result[len] ='\0' ; + +return result; +} + +static int arm64_mov__parse (const struct arch *arch,struct ins_operands *ops,struct map_symbol *ms__maybe_unused ,struct disasm_line *dl__maybe_unused ) { -char *s =strchr (ops->raw,',' ), *target, *endptr; +char *s =strchr (ops->raw,',' ), *source, *endptr;if (s ==NULL )return -1 ; +/* Parse target */ *s ='\0' ; - ops->source.raw =strdup (ops->raw); + ops->target.raw =strdup (ops->raw); *s =',' ; -if (ops->source.raw ==NULL ) -return -1 ; - - target = ++s; - ops->target.raw =strdup (target);if (ops->target.raw ==NULL ) - goto out_free_source; +return -1 ; - ops->target.addr =strtoull (target, &endptr,16 ); -if (endptr == target) - goto out_free_target; + ops->target.multi_regs =arm64__check_multi_regs (arch, ops->target.raw); - s =strchr (endptr,'<' ); -if (s ==NULL ) - goto out_free_target; - endptr =strchr (s +1 ,'>' ); -if (endptr ==NULL ) - goto out_free_target; - - *endptr ='\0' ; - *s =' ' ; - ops->target.name =strdup (s); - *s ='<' ; - *endptr ='>' ; -if (ops->target.name ==NULL ) - goto out_free_target; +/* Parse source, stripping comment if present */ + source =skip_spaces (++s); + ops->source.raw =rstrip_space_and_comment (source, arch->objdump.comment_char); +if (ops->source.raw ==NULL ) { +zfree (&ops->target.raw); +return -1 ; + } + + ops->source.multi_regs =arm64__check_multi_regs (arch, ops->source.raw); + +/* +* Parse 'addr <symbol>' from source (if any). +* +* A raw hex string may be a scalar immediate, not an address. +* Only validate 'source.addr' if accompanied by a '<symbol>' tag, +* otherwise reset it to 0 to avoid false positive address tracking. +*/ + ops->source.addr =strtoull (ops->source.raw, &endptr,16 ); +if (endptr != ops->source.raw) { + s =strchr (endptr,'<' ); +if (s ==NULL ) { + ops->source.addr =0 ; +return 0 ; + } + endptr =strrchr (s +1 ,'>' ); +if (endptr ==NULL ) { + ops->source.addr =0 ; +return 0 ; + } + + *endptr ='\0' ; + ops->source.name =strdup (s +1 ); + *endptr ='>' ; +if (ops->source.name ==NULL ) { + ops->source.addr =0 ; +return 0 ; + } + }return 0 ; +} -out_free_target: -zfree (&ops->target.raw); -out_free_source: -zfree (&ops->source.raw); -return -1 ; +static int arm64_mov__scnprintf (const struct ins *ins,char *bf,size_t size, +struct ins_operands *ops,int max_ins_name) +{ +return scnprintf (bf, size,"%-*s %s, %s" , max_ins_name, ins->name, + ops->target.raw, ops->source.name ?: ops->source.raw); }static const struct ins_ops arm64_mov_ops = { .parse = arm64_mov__parse, - .scnprintf = mov__scnprintf, + .scnprintf = arm64_mov__scnprintf, };static const struct ins_ops *arm64__associate_instruction_ops (struct arch *arch,const char *name)@@ -103,6 +204,7 @@ const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id, arch->id = *id; arch->objdump.comment_char ='/' ; arch->objdump.skip_functions_char ='+' ; + arch->objdump.memory_ref_char ='[' ; arch->associate_instruction_ops = arm64__associate_instruction_ops;/* bl, blr */ Signature · 1 line
2.34.1
PATCH v5 4/26 perf annotate-arm64: Handle load and store instructions
In reply to Tengda WuAdd ldst_ops to handle load and store instructions in order to parse the data types and offsets associated with PMU events for memory access instructions. There are many variants of load and store instructions in arm64, making it difficult to match all of these instruction names completely. Therefore, only the instruction prefixes are matched. The prefix 'ld|st' covers most of the memory access instructions, 'cas|swp' matches atomic instructions, and 'prf' matches memory prefetch instructions.
--- .../perf/util/annotate-arch/annotate-arm64.c | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+)
tools/perf/util/annotate-arch/annotate-arm64.c +140 -0
@@ -14,6 +14,7 @@ struct arch_arm64 {struct arch arch;regex_t call_insn;regex_t jump_insn; +regex_t ldst_insn;/* load and store instruction */ };static bool arm64__is_reg (const char *op)
@@ -170,6 +171,130 @@ static const struct ins_ops arm64_mov_ops = { .scnprintf = arm64_mov__scnprintf, }; +static bool arm64__insn_is_target_on_right (const char *ins_name) +{ +/* +* Store instructions write to the memory operand on the right, +* unlike standard syntax where the target is the left operand. +*/ +return !strncmp (ins_name,"st" ,2 ); +} + +/* +* This function is used to parse arm64 load/store instructions into +* instruction operands. +* +* Typical instructions and their parsing logic: +* +* 1. Immediate offset: +* ldr x2, [x0] -> target="x2", source="[x0]" +* ldr x2, [x0, #24] -> target="x2", source="[x0, #24]" +* ldp x19, x20, [sp, #16] -> target="x19, x20", source="[sp, #16]" +* +* 2. Pre-index addressing: +* stp x29, x30, [sp, #-64]! -> target="[sp, #-64]!", source="x29, x30" +* +* 3. Post-index addressing: +* str x1, [x0], #8 -> target="[x0], #8", source="x1" +* ldr w1, [x21], #4 -> target="w1", source="[x21], #4" +* ldp x29, x30, [sp], #32 -> target="x29, x30", source="[sp], #32" +* +* 4. Register offset / extension: +* ldr x0, [x1, w0, sxtw #3] -> target="x0", source="[x1, w0, sxtw #3]" +* ldr x0, [x1, x0, lsl #3] -> target="x0", source="[x1, x0, lsl #3]" +* +* 5. Atomic operations: +* cas w3, w1, [x0] -> target="w3, w1", source="[x0]" +* swp x3, x0, [x2] -> target="x3, x0", source="[x2]" +* +* 6. Prefetch memory: +* prfm pstl1strm, [x4] -> target="pstl1strm", source="[x4]" +* +* 7. PC-relative loads (No bracket found): +* ldr x0, ffff800080f40c68 <__kvm_nvhe_$d> -> Fallback to default parser +* +* Parsing strategy: +* Use the '[' bracket as the boundary to split the operands into left +* and right sides. For non-store instructions, the left side is the +* target and the right side is the source. For store instructions, the +* roles are reversed. +*/ +static int arm64_ldst__parse (const struct arch *arch,struct ins_operands *ops, +struct map_symbol *ms,struct disasm_line *dl) +{ +char *raw, *s, *left, *right; +int ret =-1 ; + + raw =rstrip_space_and_comment (ops->raw, arch->objdump.comment_char); +if (!raw) +return -1 ; + + s =strchr (raw, arch->objdump.memory_ref_char); +if (!s) { +/* Fallback to default parser for PC-relative loads. */ +free (raw); +return arm64_mov__parse (arch, ops, ms, dl); + } + + right =strdup (s); +if (!right) + goto out_free_raw; + +while (s > raw && *s !=',' ) + --s; + +if (s == raw) + goto out_free_right; + + *s ='\0' ; + left =strdup (raw); + *s =',' ; +if (!left) + goto out_free_right; + +free (raw); + +if (arm64__insn_is_target_on_right (dl->ins.name)) { + ops->source.raw = left; + ops->source.mem_ref = false; + + ops->target.raw = right; + ops->target.mem_ref = true; + }else { + ops->source.raw = right; + ops->source.mem_ref = true; + + ops->target.raw = left; + ops->target.mem_ref = false; + } + + ops->source.multi_regs =arm64__check_multi_regs (arch, ops->source.raw); + ops->target.multi_regs =arm64__check_multi_regs (arch, ops->target.raw); + +return 0 ; + +out_free_right: +free (right); +out_free_raw: +free (raw); +return ret; +} + +static int arm64_ldst__scnprintf (const struct ins *ins,char *bf,size_t size, +struct ins_operands *ops,int max_ins_name) +{ +if (arm64__insn_is_target_on_right (ins->name)) +return scnprintf (bf, size,"%-*s %s" , max_ins_name, ins->name, ops->raw); + +return scnprintf (bf, size,"%-*s %s, %s" , max_ins_name, ins->name, + ops->target.raw, ops->source.name ?: ops->source.raw); +} + +static struct ins_ops arm64_ldst_ops = { + .parse = arm64_ldst__parse, + .scnprintf = arm64_ldst__scnprintf, +}; +static const struct ins_ops *arm64__associate_instruction_ops (struct arch *arch,const char *name) { struct arch_arm64 *arm = container_of(arch, struct arch_arm64, arch);
@@ -180,6 +305,8 @@ static const struct ins_ops *arm64__associate_instruction_ops(struct arch *arch, ops = &jump_ops; else if (!regexec(&arm->call_insn, name, 2, match, 0)) ops = &call_ops; + else if (!regexec(&arm->ldst_insn, name, 2, match, 0)) + ops = &arm64_ldst_ops; else if (!strcmp(name, "ret")) ops = &ret_ops; else
@@ -205,6 +332,7 @@ const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id, arch->objdump.comment_char ='/' ; arch->objdump.skip_functions_char ='+' ; arch->objdump.memory_ref_char ='[' ; + arch->objdump.imm_char ='#' ; arch->associate_instruction_ops = arm64__associate_instruction_ops;/* bl, blr */
@@ -218,8 +346,20 @@ const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id,if (err) goto out_free_call; +/* +* The ARM64 architecture has many variants of load/store instructions. +* It is quite challenging to match all of them completely. Here, we +* only match the prefixes of these instructions. +*/ + err =regcomp (&arm->ldst_insn,"^(ld|st|cas|prf|swp)" , +REG_EXTENDED ); +if (err) + goto out_free_jump; +return arch; +out_free_jump: +regfree (&arm->jump_insn); out_free_call:regfree (&arm->call_insn); out_free_arm:
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 04/26] perf annotate-arm64: Handle load and store instructions
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
Add
ldst_opsto handle load and store instructions in order to parse the data types and offsets associated with PMU events for memory access instructions. There are many variants of load and store instructions in arm64, making it difficult to match all of these instruction names completely. Therefore, only the instruction prefixes are matched. The prefix 'ld|st' covers most of the memory access instructions, 'cas|swp' matches atomic instructions, and 'prf' matches memory prefetch instructions.Signed-off-by Li Huafei <[email protected]>Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v5 04/26] perf annotate-arm64: Handle load and store instructions
In reply to Tengda WuQuoted 11 lines · Tengda Wu
Add
ldst_opsto handle load and store instructions in order to parse the data types and offsets associated with PMU events for memory access instructions. There are many variants of load and store instructions in arm64, making it difficult to match all of these instruction names completely. Therefore, only the instruction prefixes are matched. The prefix 'ld|st' covers most of the memory access instructions, 'cas|swp' matches atomic instructions, and 'prf' matches memory prefetch instructions.Signed-off-by Li Huafei <[email protected]>Signed-off-by Tengda Wu <[email protected]>
Thanks,
Namhyung
Quoted 188 lines
--- .../perf/util/annotate-arch/annotate-arm64.c | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+)tools/perf/util/annotate-arch/annotate-arm64.c +140
-0@@ -14,6 +14,7 @@ struct arch_arm64 {struct arch arch;regex_t call_insn;regex_t jump_insn; +regex_t ldst_insn;/* load and store instruction */ };static bool arm64__is_reg (const char *op)@@ -170,6 +171,130 @@ static const struct ins_ops arm64_mov_ops = { .scnprintf = arm64_mov__scnprintf, }; +static bool arm64__insn_is_target_on_right (const char *ins_name) +{ +/* +* Store instructions write to the memory operand on the right, +* unlike standard syntax where the target is the left operand. +*/ +return !strncmp (ins_name,"st" ,2 ); +} + +/* +* This function is used to parse arm64 load/store instructions into +* instruction operands. +* +* Typical instructions and their parsing logic: +* +* 1. Immediate offset: +* ldr x2, [x0] -> target="x2", source="[x0]" +* ldr x2, [x0, #24] -> target="x2", source="[x0, #24]" +* ldp x19, x20, [sp, #16] -> target="x19, x20", source="[sp, #16]" +* +* 2. Pre-index addressing: +* stp x29, x30, [sp, #-64]! -> target="[sp, #-64]!", source="x29, x30" +* +* 3. Post-index addressing: +* str x1, [x0], #8 -> target="[x0], #8", source="x1" +* ldr w1, [x21], #4 -> target="w1", source="[x21], #4" +* ldp x29, x30, [sp], #32 -> target="x29, x30", source="[sp], #32" +* +* 4. Register offset / extension: +* ldr x0, [x1, w0, sxtw #3] -> target="x0", source="[x1, w0, sxtw #3]" +* ldr x0, [x1, x0, lsl #3] -> target="x0", source="[x1, x0, lsl #3]" +* +* 5. Atomic operations: +* cas w3, w1, [x0] -> target="w3, w1", source="[x0]" +* swp x3, x0, [x2] -> target="x3, x0", source="[x2]" +* +* 6. Prefetch memory: +* prfm pstl1strm, [x4] -> target="pstl1strm", source="[x4]" +* +* 7. PC-relative loads (No bracket found): +* ldr x0, ffff800080f40c68 <__kvm_nvhe_$d> -> Fallback to default parser +* +* Parsing strategy: +* Use the '[' bracket as the boundary to split the operands into left +* and right sides. For non-store instructions, the left side is the +* target and the right side is the source. For store instructions, the +* roles are reversed. +*/ +static int arm64_ldst__parse (const struct arch *arch,struct ins_operands *ops, +struct map_symbol *ms,struct disasm_line *dl) +{ +char *raw, *s, *left, *right; +int ret =-1 ; + + raw =rstrip_space_and_comment (ops->raw, arch->objdump.comment_char); +if (!raw) +return -1 ; + + s =strchr (raw, arch->objdump.memory_ref_char); +if (!s) { +/* Fallback to default parser for PC-relative loads. */ +free (raw); +return arm64_mov__parse (arch, ops, ms, dl); + } + + right =strdup (s); +if (!right) + goto out_free_raw; + +while (s > raw && *s !=',' ) + --s; + +if (s == raw) + goto out_free_right; + + *s ='\0' ; + left =strdup (raw); + *s =',' ; +if (!left) + goto out_free_right; + +free (raw); + +if (arm64__insn_is_target_on_right (dl->ins.name)) { + ops->source.raw = left; + ops->source.mem_ref = false; + + ops->target.raw = right; + ops->target.mem_ref = true; + }else { + ops->source.raw = right; + ops->source.mem_ref = true; + + ops->target.raw = left; + ops->target.mem_ref = false; + } + + ops->source.multi_regs =arm64__check_multi_regs (arch, ops->source.raw); + ops->target.multi_regs =arm64__check_multi_regs (arch, ops->target.raw); + +return 0 ; + +out_free_right: +free (right); +out_free_raw: +free (raw); +return ret; +} + +static int arm64_ldst__scnprintf (const struct ins *ins,char *bf,size_t size, +struct ins_operands *ops,int max_ins_name) +{ +if (arm64__insn_is_target_on_right (ins->name)) +return scnprintf (bf, size,"%-*s %s" , max_ins_name, ins->name, ops->raw); + +return scnprintf (bf, size,"%-*s %s, %s" , max_ins_name, ins->name, + ops->target.raw, ops->source.name ?: ops->source.raw); +} + +static struct ins_ops arm64_ldst_ops = { + .parse = arm64_ldst__parse, + .scnprintf = arm64_ldst__scnprintf, +}; +static const struct ins_ops *arm64__associate_instruction_ops (struct arch *arch,const char *name) { struct arch_arm64 *arm = container_of(arch, struct arch_arm64, arch);@@ -180,6 +305,8 @@ static const struct ins_ops *arm64__associate_instruction_ops(struct arch *arch, ops = &jump_ops; else if (!regexec(&arm->call_insn, name, 2, match, 0)) ops = &call_ops; + else if (!regexec(&arm->ldst_insn, name, 2, match, 0)) + ops = &arm64_ldst_ops; else if (!strcmp(name, "ret")) ops = &ret_ops; else@@ -205,6 +332,7 @@ const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id, arch->objdump.comment_char ='/' ; arch->objdump.skip_functions_char ='+' ; arch->objdump.memory_ref_char ='[' ; + arch->objdump.imm_char ='#' ; arch->associate_instruction_ops = arm64__associate_instruction_ops;/* bl, blr */ @@ -218,8 +346,20 @@ const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id,if (err) goto out_free_call; +/* +* The ARM64 architecture has many variants of load/store instructions. +* It is quite challenging to match all of them completely. Here, we +* only match the prefixes of these instructions. +*/ + err =regcomp (&arm->ldst_insn,"^(ld|st|cas|prf|swp)" , +REG_EXTENDED ); +if (err) + goto out_free_jump; +return arch; +out_free_jump: +regfree (&arm->jump_insn); out_free_call:regfree (&arm->call_insn); out_free_arm:Signature · 1 line
2.34.1
PATCH v5 5/26 perf annotate: Normalize arch__dwarf_regnum() error return values
In reply to Tengda Wuarch__dwarf_regnum() returns inconsistent error values: early path returns -1 while the main lookup returns -ENOENT/-EINVAL from get_dwarf_regnum(). Callers only check for -1, so genuine failures can slip through.
Normalize all failures to -1 to match the callers' expectation.
--- tools/perf/util/annotate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/annotate.c +1 -1
@@ -2492,7 +2492,7 @@ static int arch__dwarf_regnum(const struct arch *arch, const char *str) reg =get_dwarf_regnum (regname, arch->id.e_machine, arch->id.e_flags);free (regname); -return reg; +return reg <0 ?-1 : reg; }/*
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 05/26] perf annotate: Normalize arch__dwarf_regnum() error return values
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The check_matching_type() function uses dloc->op->reg1 as an array index without bounds checking, leading to an out-of-bounds stack access when a register cannot be parsed. --
commit 7d09889746335253aa4ac524e693986afc798032
Author: Tengda Wu <[email protected]>
perf annotate: Normalize arch__dwarf_regnum() error return values
This commit normalizes the error return values of arch__dwarf_regnum() so that all failures consistently return -1. This ensures callers that only check for -1 will correctly identify genuine failures.
tools/perf/util/annotate.c static int arch__dwarf_regnum(const struct arch *arch, const char *str) · line 2495
Quoted 9 lines
tools/perf/util/annotate.c +1
-1@@ -2492,7 +2492,7 @@ static int arch__dwarf_regnum(const struct arch *arch, const char *str) reg =get_dwarf_regnum (regname, arch->id.e_machine, arch->id.e_flags);free (regname); -return reg; +return reg <0 ?-1 : reg;
[Severity: High]
This is a pre-existing issue, but does consistently returning -1 here make it easier to trigger an out-of-bounds stack read downstream in check_matching_type()?
When processing an instruction with a memory reference, if register parsing fails, the register variable can remain set to -1. Because the memory reference flag (op_loc->mem_ref) remains true, the code can still proceed to find_data_type() and check_matching_type().
Inside check_matching_type(), this register value (-1) is used directly as an array index without bounds checking:
tools/perf/util/annotate-data.c:check_matching_type() {
int reg = dloc->op->reg1;
...
pr_debug_dtp("chk [%x] reg%d offset=%s%#x ok=%d kind=%d ",
insn_offset, reg, offset_sign, abs(offset),
state->regs[reg].ok, state->regs[reg].kind);
if (!state->regs[reg].ok)
...
}Could this index read uninitialized memory preceding the state->regs array? If the stack garbage is interpreted as TSR_KIND_TYPE, might it try to dereference it as a Dwarf_Die object and cause a segmentation fault when analyzing maliciously crafted perf.data or ELF files?
Quoted 1 line
tools/perf/util/annotate.c +0
-0}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH v5 05/26] perf annotate: Normalize arch__dwarf_regnum() error return values
In reply to Tengda WuQuoted 7 lines · Tengda Wu
arch__dwarf_regnum()returns inconsistent error values: early path returns -1 while the main lookup returns -ENOENT/-EINVAL fromget_dwarf_regnum(). Callers only check for -1, so genuine failures can slip through.Normalize all failures to -1 to match the callers' expectation.
This sounds like a bug in the callers but this function is local to annotate.c and is just a convenience so:
Thanks,
Ian
Quoted 22 lines
Suggested-by Shuai Xue <[email protected]>Signed-off-by Tengda Wu <[email protected]>--- tools/perf/util/annotate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/annotate.c +1
-2@@ -2492,7 +2492,7 @@ static int arch__dwarf_regnum(const struct arch *arch, const char *str) reg =get_dwarf_regnum (regname, arch->id.e_machine, arch->id.e_flags);free (regname); -return reg; +return reg <0 ?-1 : reg; }/* -- 2.34.1
Re: [PATCH v5 05/26] perf annotate: Normalize arch__dwarf_regnum() error return values
Quoted 11 lines · Ian Rogers
Quoted 7 lines · Tengda Wu
arch__dwarf_regnum()returns inconsistent error values: early path returns -1 while the main lookup returns -ENOENT/-EINVAL fromget_dwarf_regnum(). Callers only check for -1, so genuine failures can slip through.Normalize all failures to -1 to match the callers' expectation.
This sounds like a bug in the callers but this function is local to
annotate.cand is just a convenience so:
Agreed.
Thanks,
Namhyung
PATCH v5 6/26 perf annotate: Introduce extract_op_location callback for arch-specific parsing
In reply to Tengda WuAssembly syntax for operands varies significantly across different architectures, which prevents the operand location (op_loc) parsing logic in annotate_get_insn_location() from being directly reused.
To simplify the core logic and improve maintainability, move the operand parsing inside the for_each_insn_op_loc loop into arch-specific extract_op_location callbacks. This refactoring is intended to be a cleanup with no functional changes.
--- .../util/annotate-arch/annotate-powerpc.c | 9 ++ tools/perf/util/annotate-arch/annotate-x86.c | 79 ++++++++++++++++ tools/perf/util/annotate.c | 94 +------------------ tools/perf/util/annotate.h | 2 + tools/perf/util/disasm.h | 4 + 5 files changed, 99 insertions(+), 89 deletions(-)
tools/perf/util/annotate-arch/annotate-powerpc.c +9 -0
@@ -390,6 +390,14 @@ static void update_insn_state_powerpc(struct type_state *state, }#endif /* HAVE_LIBDW_SUPPORT */ +static void extract_op_location_powerpc (const struct arch *arch__maybe_unused , +struct disasm_line *dl, +const char *op_str__maybe_unused ,int op_idx, +struct annotated_op_loc *op_loc) +{ +get_powerpc_regs (dl->raw.raw_insn, !op_idx, op_loc); +} +const struct arch *arch__new_powerpc (const struct e_machine_and_e_flags *id,const char *cpuid__maybe_unused ) {
@@ -406,5 +414,6 @@ const struct arch *arch__new_powerpc(const struct e_machine_and_e_flags *id,#ifdef HAVE_LIBDW_SUPPORT arch->update_insn_state = update_insn_state_powerpc;#endif + arch->extract_op_location = extract_op_location_powerpc;return arch; }
tools/perf/util/annotate-arch/annotate-x86.c +79 -0
96 lines not on this page · raw message
tools/perf/util/annotate.c +5 -89
113 lines not on this page · raw message
tools/perf/util/annotate.h +2 -0
@@ -492,6 +492,8 @@ int annotate_parse_percent_type(const struct option *opt, const char *_str,int annotate_check_args (void ); +int arch__dwarf_regnum (const struct arch *arch,const char *str); +/** * struct annotated_op_loc - Location info of instruction operand * @reg1: First register in the operand
tools/perf/util/disasm.h +4 -0
@@ -16,6 +16,7 @@ struct symbol;struct data_loc_info ;struct type_state ;struct disasm_line ; +struct annotated_op_loc ;struct e_machine_and_e_flags {uint32_t e_flags;
@@ -49,6 +50,9 @@ struct arch { struct data_loc_info *dloc, Dwarf_Die *cu_die, struct disasm_line *dl);#endif +void (*extract_op_location)(const struct arch *arch,struct disasm_line *dl, +const char *op_str,int op_idx, +struct annotated_op_loc *op_loc); };struct ins {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 06/26] perf annotate: Introduce extract_op_location callback for arch-specific parsing
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
Assembly syntax for operands varies significantly across different architectures, which prevents the operand location (
op_loc) parsing logic inannotate_get_insn_location()from being directly reused.To simplify the core logic and improve maintainability, move the operand parsing inside the
for_each_insn_op_locloop into arch-specificextract_op_locationcallbacks. This refactoring is intended to be a cleanup with no functional changes.Signed-off-by Li Huafei <[email protected]>Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v5 06/26] perf annotate: Introduce extract_op_location callback for arch-specific parsing
In reply to Tengda WuQuoted 11 lines · Tengda Wu
Assembly syntax for operands varies significantly across different architectures, which prevents the operand location (
op_loc) parsing logic inannotate_get_insn_location()from being directly reused.To simplify the core logic and improve maintainability, move the operand parsing inside the
for_each_insn_op_locloop into arch-specificextract_op_locationcallbacks. This refactoring is intended to be a cleanup with no functional changes.Signed-off-by Li Huafei <[email protected]>Signed-off-by Tengda Wu <[email protected]>
Thanks,
Namhyung
Quoted 295 lines
--- .../util/annotate-arch/annotate-powerpc.c | 9 ++ tools/perf/util/annotate-arch/annotate-x86.c | 79 ++++++++++++++++ tools/perf/util/annotate.c | 94 +------------------ tools/perf/util/annotate.h | 2 + tools/perf/util/disasm.h | 4 + 5 files changed, 99 insertions(+), 89 deletions(-)tools/perf/util/annotate-arch/annotate-powerpc.c +9
-0@@ -390,6 +390,14 @@ static void update_insn_state_powerpc(struct type_state *state, }#endif /* HAVE_LIBDW_SUPPORT */ +static void extract_op_location_powerpc (const struct arch *arch__maybe_unused , +struct disasm_line *dl, +const char *op_str__maybe_unused ,int op_idx, +struct annotated_op_loc *op_loc) +{ +get_powerpc_regs (dl->raw.raw_insn, !op_idx, op_loc); +} +const struct arch *arch__new_powerpc (const struct e_machine_and_e_flags *id,const char *cpuid__maybe_unused ) {@@ -406,5 +414,6 @@ const struct arch *arch__new_powerpc(const struct e_machine_and_e_flags *id,#ifdef HAVE_LIBDW_SUPPORT arch->update_insn_state = update_insn_state_powerpc;#endif + arch->extract_op_location = extract_op_location_powerpc;return arch; }tools/perf/util/annotate-arch/annotate-x86.c +79
-0@@ -1,6 +1,8 @@// SPDX-License-Identifier: GPL-2.0 #include <string.h> +#include <stdlib.h> #include <linux/compiler.h> +#include <linux/ctype.h> #include <linux/zalloc.h> #include <assert.h> #include <inttypes.h> @@ -809,6 +811,82 @@ static void update_insn_state_x86(struct type_state *state, }#endif +/* +* Get register number and access offset from the given instruction. +* It assumes AT&T x86 asm format like OFFSET(REG). +* Fills @reg and @offset when return 0. +*/ +static int extract_reg_offset (const struct arch *arch,const char *str, +struct annotated_op_loc *op_loc) +{ +char *p; + +if (arch->objdump.register_char ==0 ) +return -1 ; + +/* +* It should start from offset, but it's possible to skip 0 +* in the asm. So 0(%rax) should be same as (%rax). +* +* However, it also start with a segment select register like +* %gs:0x18(%rbx). In that case it should skip the part. +*/ +if (*str == arch->objdump.register_char) { +/* FIXME: Handle other segment registers */ +if (!strncmp (str,"%gs:" ,4 )) + op_loc->segment =INSN_SEG_X86_GS ; + +while (*str && !isdigit (*str) && + *str != arch->objdump.memory_ref_char) + str++; + } + + op_loc->offset =strtol (str, &p,0 ); + op_loc->reg1 =arch__dwarf_regnum (arch, p); +if (op_loc->reg1 ==-1 ) +return -1 ; + +/* Get the second register */ +if (op_loc->multi_regs) + op_loc->reg2 =arch__dwarf_regnum (arch, p +1 ); + +return 0 ; +} + +static void extract_op_location_x86 (const struct arch *arch, +struct disasm_line *dl__maybe_unused , +const char *op_str,int op_idx__maybe_unused , +struct annotated_op_loc *op_loc) +{ +if (op_str ==NULL ) +return ; + +if (strchr (op_str, arch->objdump.memory_ref_char)) { + op_loc->mem_ref = true; +extract_reg_offset (arch, op_str, op_loc); + }else { +const char *s = op_str; +char *p =NULL ; + +/* FIXME: Handle other segment registers */ +if (!strncmp (op_str,"%gs:" ,4 )) { + op_loc->segment =INSN_SEG_X86_GS ; + op_loc->offset =strtol (op_str +4 , &p,0 ); +if (p && p != op_str +4 ) + op_loc->imm = true; +return ; + } + +if (*s == arch->objdump.register_char) { + op_loc->reg1 =arch__dwarf_regnum (arch, s); + }else if (*s == arch->objdump.imm_char) { + op_loc->offset =strtol (s +1 , &p,0 ); +if (p && p != s +1 ) + op_loc->imm = true; + } + } +} +const struct arch *arch__new_x86 (const struct e_machine_and_e_flags *id,const char *cpuid) {struct arch *arch =zalloc (sizeof (*arch));@@ -848,5 +926,6 @@ const struct arch *arch__new_x86(const struct e_machine_and_e_flags *id, const c#ifdef HAVE_LIBDW_SUPPORT arch->update_insn_state = update_insn_state_x86;#endif + arch->extract_op_location = extract_op_location_x86;return arch; }tools/perf/util/annotate.c +5
-89@@ -2472,7 +2472,7 @@ int annotate_check_args(void)return 0 ; } -static int arch__dwarf_regnum (const struct arch *arch,const char *str) +int arch__dwarf_regnum (const struct arch *arch,const char *str) {const char *p;char *regname, *q;@@ -2495,51 +2495,6 @@ static int arch__dwarf_regnum(const struct arch *arch, const char *str)return reg <0 ?-1 : reg; } -/* -* Get register number and access offset from the given instruction. -* It assumes AT&T x86 asm format like OFFSET(REG). Maybe it needs -* to revisit the format when it handles different architecture. -* Fills @reg and @offset when return 0. -*/ -static int extract_reg_offset (const struct arch *arch,const char *str, -struct annotated_op_loc *op_loc) -{ -char *p; - -if (arch->objdump.register_char ==0 ) -return -1 ; - -/* -* It should start from offset, but it's possible to skip 0 -* in the asm. So 0(%rax) should be same as (%rax). -* -* However, it also start with a segment select register like -* %gs:0x18(%rbx). In that case it should skip the part. -*/ -if (*str == arch->objdump.register_char) { -if (arch__is_x86 (arch)) { -/* FIXME: Handle other segment registers */ -if (!strncmp (str,"%gs:" ,4 )) - op_loc->segment =INSN_SEG_X86_GS ; - } - -while (*str && !isdigit (*str) && - *str != arch->objdump.memory_ref_char) - str++; - } - - op_loc->offset =strtol (str, &p,0 ); - op_loc->reg1 =arch__dwarf_regnum (arch, p); -if (op_loc->reg1 ==-1 ) -return -1 ; - -/* Get the second register */ -if (op_loc->multi_regs) - op_loc->reg2 =arch__dwarf_regnum (arch, p +1 ); - -return 0 ; -} -/** * annotate_get_insn_location - Get location of instruction * @arch: the architecture info @@ -2595,50 +2550,11 @@ int annotate_get_insn_location(const struct arch *arch, struct disasm_line *dl,/* Invalidate the register by default */ op_loc->reg1 =-1 ; op_loc->reg2 =-1 ; + op_loc->mem_ref = mem_ref; + op_loc->multi_regs = multi_regs; -if (insn_str ==NULL ) { -if (!arch__is_powerpc (arch)) -continue ; - } - -/* -* For powerpc, call get_powerpc_regs function which extracts the -* required fields for op_loc, ie reg1, reg2, offset from the -* raw instruction. -*/ -if (arch__is_powerpc (arch)) { - op_loc->mem_ref = mem_ref; - op_loc->multi_regs = multi_regs; -get_powerpc_regs (dl->raw.raw_insn, !i, op_loc); - }else if (strchr (insn_str, arch->objdump.memory_ref_char)) { - op_loc->mem_ref = true; - op_loc->multi_regs = multi_regs; -extract_reg_offset (arch, insn_str, op_loc); - }else { -const char *s = insn_str; -char *p =NULL ; - -if (arch__is_x86 (arch)) { -/* FIXME: Handle other segment registers */ -if (!strncmp (insn_str,"%gs:" ,4 )) { - op_loc->segment =INSN_SEG_X86_GS ; - op_loc->offset =strtol (insn_str +4 , - &p,0 ); -if (p && p != insn_str +4 ) - op_loc->imm = true; -continue ; - } - } - -if (*s == arch->objdump.register_char) { - op_loc->reg1 =arch__dwarf_regnum (arch, s); - } -else if (*s == arch->objdump.imm_char) { - op_loc->offset =strtol (s +1 , &p,0 ); -if (p && p != s +1 ) - op_loc->imm = true; - } - } +if (arch->extract_op_location) + arch->extract_op_location (arch, dl, insn_str, i, op_loc); }return 0 ;tools/perf/util/annotate.h +2
-0@@ -492,6 +492,8 @@ int annotate_parse_percent_type(const struct option *opt, const char *_str,int annotate_check_args (void ); +int arch__dwarf_regnum (const struct arch *arch,const char *str); +/** * struct annotated_op_loc - Location info of instruction operand * @reg1: First register in the operand tools/perf/util/disasm.h +4
-0@@ -16,6 +16,7 @@ struct symbol;struct data_loc_info ;struct type_state ;struct disasm_line ; +struct annotated_op_loc ;struct e_machine_and_e_flags {uint32_t e_flags;@@ -49,6 +50,9 @@ struct arch { struct data_loc_info *dloc, Dwarf_Die *cu_die, struct disasm_line *dl);#endif +void (*extract_op_location)(const struct arch *arch,struct disasm_line *dl, +const char *op_str,int op_idx, +struct annotated_op_loc *op_loc); };struct ins {Signature · 1 line
2.34.1
PATCH v5 7/26 perf dwarf-regs: Adapt get_dwarf_regnum() for arm64
In reply to Tengda WuThe current arm64 DWARF register lookup relies on 'aarch64_regstr_tbl', a static string table. While this works for kprobe-tracer where register names start with '%', it is insufficient for parsing register numbers directly from raw instructions (e.g., extracting '6' from 'x6' or 'w6') during annotation.
Since get_dwarf_regnum() is currently used only by 'perf annotate' and does not affect kprobe-tracer, replace the limited table-based lookup with a programmatic implementation in __get_dwarf_regnum_arm64(). This allows resolving arm64 register names (x0-x30, w0-w30, sp, etc.) directly into their corresponding DWARF register numbers.
--- .../util/dwarf-regs-arch/dwarf-regs-arm64.c | 22 +++++++++++++++++++ tools/perf/util/dwarf-regs.c | 2 +- tools/perf/util/include/dwarf-regs.h | 1 + 3 files changed, 24 insertions(+), 1 deletion(-)
tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c +22 -0
@@ -1,5 +1,8 @@// SPDX-License-Identifier: GPL-2.0 #include <errno.h> +#include <ctype.h> +#include <stdlib.h> +#include <string.h> #include <dwarf-regs.h> #include "../../../arch/arm64/include/uapi/asm/perf_regs.h"
@@ -10,3 +13,22 @@ int __get_dwarf_regnum_for_perf_regnum_arm64(int perf_regnum)return perf_regnum; } + +int __get_dwarf_regnum_arm64 (const char *name) +{ +int reg; + +if (!strcmp (name,"sp" )) +return 31 ; + +if (*name !='x' && *name !='w' ) +return -ENOENT ; + + name++; +if (!isdigit (*name)) +return -ENOENT ; + + reg =strtol (name,NULL ,10 ); + +return reg >=0 && reg <=30 ? reg : -ENOENT ; +}
tools/perf/util/dwarf-regs.c +1 -1
@@ -114,7 +114,7 @@ int get_dwarf_regnum(const char *name, unsigned int machine, unsigned int flags) reg =_get_dwarf_regnum (arm_regstr_tbl, name);break ;case EM_AARCH64 : - reg =_get_dwarf_regnum (aarch64_regstr_tbl, name); + reg =__get_dwarf_regnum_arm64 (name);break ;case EM_CSKY : reg =__get_csky_regnum (name, flags);
tools/perf/util/include/dwarf-regs.h +1 -0
@@ -105,6 +105,7 @@ int __get_dwarf_regnum_x86_64(const char *name);int __get_dwarf_regnum_for_perf_regnum_i386 (int perf_regnum);int __get_dwarf_regnum_for_perf_regnum_x86_64 (int perf_regnum); +int __get_dwarf_regnum_arm64 (const char *name);int __get_dwarf_regnum_for_perf_regnum_arm (int perf_regnum);int __get_dwarf_regnum_for_perf_regnum_arm64 (int perf_regnum);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 07/26] perf dwarf-regs: Adapt get_dwarf_regnum() for arm64
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Missing 'lr' (link register) support in arm64 DWARF register programmatic lookup. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missing 'lr' (link register) support in arm64 DWARF register programmatic lookup. --
commit 5d023c42589d9bd9061d9468194dc470f5cdc273
Author: Tengda Wu <[email protected]>
perf dwarf-regs: Adapt get_dwarf_regnum() for arm64
This commit replaces the static table lookup for arm64 DWARF registers with a programmatic parser in __get_dwarf_regnum_arm64(). This allows resolving arm64 register names directly into their corresponding DWARF register numbers for perf annotate.
Quoted 4 lines
tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c +0
-0
[ ... ]
Quoted 4 lines
tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c +0
-0@@ -10,3 +13,22 @@ int __get_dwarf_regnum_for_perf_regnum_arm64(int perf_regnum)return perf_regnum; }
tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c int __get_dwarf_regnum_for_perf_regnum_arm64(int perf_regnum) · line 25
Quoted 10 lines
tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c +10
-0+ +int __get_dwarf_regnum_arm64 (const char *name) +{ +int reg; + +if (!strcmp (name,"sp" )) +return 31 ; + +if (*name !='x' && *name !='w' ) +return -ENOENT ;
[Severity: Medium]
Does this code inadvertently drop support for the link register ('lr')?
If perf annotate processes an instruction containing 'lr' (e.g., stp x29, lr, [sp, #-16]!), arch__dwarf_regnum() extracts "lr" and calls this programmatic logic. Since "lr" does not start with 'x' or 'w' and is not "sp", it will return -ENOENT. This seems like it will prevent data type tracking for the link register, which was previously supported when mapping to DWARF register 30.
Quoted 9 lines
tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c +9
-0+ + name++; +if (!isdigit (*name)) +return -ENOENT ; + + reg =strtol (name,NULL ,10 ); + +return reg >=0 && reg <=30 ? reg : -ENOENT ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH v5 07/26] perf dwarf-regs: Adapt get_dwarf_regnum() for arm64
Quoted 39 lines · On Tue, Sep 08, 2026 at 01:21:51PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missing 'lr' (link register) support in arm64 DWARF register programmatic lookup. --commit
5d023c42589d9bd9061d9468194dc470f5cdc273
Author: Tengda Wu <[email protected]>perf dwarf-regs: Adapt
get_dwarf_regnum()for arm64This commit replaces the static table lookup for arm64 DWARF registers with a programmatic parser in
__get_dwarf_regnum_arm64(). This allows resolving arm64 register names directly into their corresponding DWARF register numbers for perf annotate.Quoted 4 lines
tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c +0
-0[ ... ]
Quoted 4 lines
tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c +0
-0@@ -10,3 +13,22 @@ int __get_dwarf_regnum_for_perf_regnum_arm64(int perf_regnum)return perf_regnum; }tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c int __get_dwarf_regnum_for_perf_regnum_arm64(int perf_regnum) · line 25
Quoted 10 lines
tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c +10
-0+ +int __get_dwarf_regnum_arm64 (const char *name) +{ +int reg; + +if (!strcmp (name,"sp" )) +return 31 ; + +if (*name !='x' && *name !='w' ) +return -ENOENT ;[Severity: Medium]
Does this code inadvertently drop support for the link register ('lr')?
Quoted 6 lines
If perf annotate processes an instruction containing 'lr' (e.g., stp x29, lr, [sp, #-16]!),
arch__dwarf_regnum()extracts "lr" and calls this programmatic logic. Since "lr" does not start with 'x' or 'w' and is not "sp", it will return-ENOENT. This seems like it will prevent data type tracking for the link register, which was previously supported when mapping to DWARF register 30.
I think we agreed to ignore 'lr' for now.
Thanks,
Namhyung
Quoted 13 lines
Quoted 10 lines
tools/perf/util/dwarf-regs-arch/dwarf-regs-arm64.c +9
-0+ + name++; +if (!isdigit (*name)) +return -ENOENT ; + + reg =strtol (name,NULL ,10 ); + +return reg >=0 && reg <=30 ? reg : -ENOENT ; +}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v5 8/26 perf annotate: Adapt arch__dwarf_regnum() for arm64
In reply to Tengda WuCurrently, arch__dwarf_regnum() assumes that all architectures use a register prefix character (e.g., '%' for x86) defined by arch->objdump.register_char, and uses it to match register names in objdump output. However, this assumption does not hold for arm64, where assembly syntax uses bare register names like 'x0', 'w1' without any prefix.
As a result, arm64 builds may fail to correctly recognize register names from objdump disassembly, leading to incomplete or incorrect annotation output.
To address this:
- Make the register prefix check optional, allowing architectures without a prefix character to be parsed correctly.
- Extend the delimiter set in strpbrk() to include the closing square bracket ']'. In arm64 assembly, memory operands often use bracketed syntax such as '[x1, #16]' or '[x2]'. Adding ']' ensures clean extraction of register names like 'x2' without trailing characters.
--- tools/perf/util/annotate.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
tools/perf/util/annotate.c +7 -5
@@ -2474,19 +2474,21 @@ int annotate_check_args(void)int arch__dwarf_regnum (const struct arch *arch,const char *str) { -const char *p; +const char *p = str;char *regname, *q;int reg; - p =strchr (str, arch->objdump.register_char); -if (p ==NULL ) -return -1 ; +if (arch->objdump.register_char) { + p =strchr (str, arch->objdump.register_char); +if (p ==NULL ) +return -1 ; + } regname =strdup (p);if (regname ==NULL )return -1 ; - q =strpbrk (regname,",) " ); + q =strpbrk (regname,",)] " );if (q) *q ='\0' ;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 08/26] perf annotate: Adapt arch__dwarf_regnum() for arm64
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 16 lines
Currently,
arch__dwarf_regnum()assumes that all architectures use a register prefix character (e.g., '%' for x86) defined byarch->objdump.register_char, and uses it to match register names in objdump output. However, this assumption does not hold for arm64, where assembly syntax uses bare register names like 'x0', 'w1' without any prefix.As a result, arm64 builds may fail to correctly recognize register names from objdump disassembly, leading to incomplete or incorrect annotation output.
To address this:
- Make the register prefix check optional, allowing architectures without a prefix character to be parsed correctly.
Quoted 6 lines
- Extend the delimiter set in
strpbrk()to include the closing square bracket ']'. In arm64 assembly, memory operands often use bracketed syntax such as '[x1, #16]' or '[x2]'. Adding ']' ensures clean extraction of register names like 'x2' without trailing characters.Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH v5 08/26] perf annotate: Adapt arch__dwarf_regnum() for arm64
In reply to Tengda WuQuoted 16 lines · Tengda Wu
Currently,
arch__dwarf_regnum()assumes that all architectures use a register prefix character (e.g., '%' for x86) defined byarch->objdump.register_char, and uses it to match register names in objdump output. However, this assumption does not hold for arm64, where assembly syntax uses bare register names like 'x0', 'w1' without any prefix.As a result, arm64 builds may fail to correctly recognize register names from objdump disassembly, leading to incomplete or incorrect annotation output.
To address this:
- Make the register prefix check optional, allowing architectures without a prefix character to be parsed correctly.
Quoted 6 lines
- Extend the delimiter set in
strpbrk()to include the closing square bracket ']'. In arm64 assembly, memory operands often use bracketed syntax such as '[x1, #16]' or '[x2]'. Adding ']' ensures clean extraction of register names like 'x2' without trailing characters.Signed-off-by Tengda Wu <[email protected]>
Thanks,
Namhyung
Quoted 38 lines
--- tools/perf/util/annotate.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)tools/perf/util/annotate.c +7
-5@@ -2474,19 +2474,21 @@ int annotate_check_args(void)int arch__dwarf_regnum (const struct arch *arch,const char *str) { -const char *p; +const char *p = str;char *regname, *q;int reg; - p =strchr (str, arch->objdump.register_char); -if (p ==NULL ) -return -1 ; +if (arch->objdump.register_char) { + p =strchr (str, arch->objdump.register_char); +if (p ==NULL ) +return -1 ; + } regname =strdup (p);if (regname ==NULL )return -1 ; - q =strpbrk (regname,",) " ); + q =strpbrk (regname,",)] " );if (q) *q ='\0' ;Signature · 1 line
2.34.1
PATCH v5 9/26 perf annotate-arm64: Implement extract_op_location() callback
In reply to Tengda WuImplement the extract_op_location() callback for the arm64 architecture to handle its specific assembly syntax and addressing modes.
The extractor handles:
1. Immediate operands (e.g., '#0x10' or '#0xc600, lsl #16').
2. Memory references with diverse addressing modes:
- Signed offset: [base{, #imm|reg}]
- Pre-index: [base, #imm]!
- Post-index: [base], #imm|reg
3. Multi-register operands and primary/secondary register extraction.
4. Register extension specifiers (e.g., uxtw, sxtx) and shift amounts for shifted/extended operands.
With this callback in place, 'perf annotate' can now resolve memory locations and register types on arm64, marking the initial enablement of data type profiling for this architecture.
--- .../perf/util/annotate-arch/annotate-arm64.c | 127 ++++++++++++++++++ tools/perf/util/annotate.h | 61 +++++++++ 2 files changed, 188 insertions(+)
tools/perf/util/annotate-arch/annotate-arm64.c +127 -0
@@ -316,6 +316,132 @@ static const struct ins_ops *arm64__associate_instruction_ops(struct arch *arch,return ops; } +static enum annotated_ext_type arm64__check_ext_type (const char *op) +{ +if (!strncmp (op,"uxtb" ,4 ))return PERF_EXT_UXTB ; +if (!strncmp (op,"uxth" ,4 ))return PERF_EXT_UXTH ; +if (!strncmp (op,"uxtw" ,4 ))return PERF_EXT_UXTW ; +if (!strncmp (op,"uxtx" ,4 ))return PERF_EXT_UXTX ; +if (!strncmp (op,"sxtb" ,4 ))return PERF_EXT_SXTB ; +if (!strncmp (op,"sxth" ,4 ))return PERF_EXT_SXTH ; +if (!strncmp (op,"sxtw" ,4 ))return PERF_EXT_SXTW ; +if (!strncmp (op,"sxtx" ,4 ))return PERF_EXT_SXTX ; + +return PERF_EXT_NONE ; +} + +static enum annotated_shift_type arm64__check_shift_type (const char *op) +{ +if (!strncmp (op,"lsl" ,3 ))return PERF_SHIFT_LSL ; +if (!strncmp (op,"lsr" ,3 ))return PERF_SHIFT_LSR ; +if (!strncmp (op,"asr" ,3 ))return PERF_SHIFT_ASR ; +if (!strncmp (op,"ror" ,3 ))return PERF_SHIFT_ROR ; + +return PERF_SHIFT_NONE ; +} + +static void extract_op_location_arm64 (const struct arch *arch, +struct disasm_line *dl__maybe_unused , +const char *op_str,int op_idx__maybe_unused , +struct annotated_op_loc *op_loc) +{ +const char *s = op_str; +char *p =NULL ; + +if (op_str ==NULL ) +return ; + +/* +* Handle immediate operand. +* e.g., "#0xc600" -> offset = 0xc600, imm = true +* +* After parsing, check for post-modifiers like "lsl #16", which +* typically occur in instructions such as movk, movz, movn, etc. +*/ +if (*s == arch->objdump.imm_char) { + op_loc->offset =strtol (s +1 , &p,0 ); +if (p && p != s +1 ) { + op_loc->imm = true; + s = p; + } + goto check_modifiers; + } + +/* +* Handle memory references, identify arm64 specific addressing modes. +* Reference: Arm Architecture Reference Manual +* (DDI 0487), Chapter C1.3.3: Load/store addressing modes. +*/ +if (*s == arch->objdump.memory_ref_char) { + op_loc->mem_ref = true; + + p =strchr (s,']' ); +if (p ==NULL ) +return ; + +/* Pre-index: [base, #imm]! */ +if (p[1 ] =='!' ) + op_loc->addr_mode =PERF_AAM_PRE_INDEX ; +/* Post-index: [base], #imm|reg */ +else if (p[1 ] ==',' && + (strchr (p +1 , arch->objdump.imm_char) || +arm64__is_reg (skip_spaces (p +2 )))) + op_loc->addr_mode =PERF_AAM_POST_INDEX ; +/* Signed offset: [base{, #imm|reg}] */ +else + op_loc->addr_mode =PERF_AAM_SIGNED_OFFSET ; + + s++; + } + +/* Extract the primary register */ + op_loc->reg1 =arch__dwarf_regnum (arch, s); +if (op_loc->reg1 ==-1 ) +return ; + +/* Move to the next operand field, if present */ + s =strchr (s,',' ); +if (s ==NULL ) +return ; + s =skip_spaces (s +1 ); + +/* Extract secondary register or immediate offset */ +if (op_loc->multi_regs) + op_loc->reg2 =arch__dwarf_regnum (arch, s); +else if (*s == arch->objdump.imm_char) + op_loc->offset =strtol (s +1 ,NULL ,0 ); + +check_modifiers: +/* +* Look for a following shift or extension modifier: +* ", lsl #3" -> extend_type = PERF_EXT_NONE, +* shift_type = PERF_SHIFT_LSL, amount = 3 +* ", uxtw #3" -> extend_type = PERF_EXT_UXTW, +* shift_type = PERF_SHIFT_LSL, amount = 3 +* ", uxtw" -> extend_type = PERF_EXT_UXTW, +* shift_type = PERF_SHIFT_LSL, amount = 0 +*/ + s =strchr (s,',' ); +if (s ==NULL ) +return ; + s =skip_spaces (s +1 ); + + op_loc->extend_type =arm64__check_ext_type (s); + op_loc->shift_type =arm64__check_shift_type (s); +/* ARM64 extended operands are implicitly shifted by LSL. */ +if (op_loc->extend_type !=PERF_EXT_NONE ) + op_loc->shift_type =PERF_SHIFT_LSL ; + +/* Parse shift amount if present */ + op_loc->amount =0 ; +if (op_loc->extend_type !=PERF_EXT_NONE || + op_loc->shift_type !=PERF_SHIFT_NONE ) { + s =strchr (s, arch->objdump.imm_char); +if (s) + op_loc->amount = (u8 )strtol (s +1 ,NULL ,0 ); + } +} +const struct arch *arch__new_arm64 (const struct e_machine_and_e_flags *id,const char *cpuid__maybe_unused ) {
@@ -334,6 +460,7 @@ const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id, arch->objdump.memory_ref_char ='[' ; arch->objdump.imm_char ='#' ; arch->associate_instruction_ops = arm64__associate_instruction_ops; + arch->extract_op_location = extract_op_location_arm64;/* bl, blr */ err =regcomp (&arm->call_insn,"^blr?$" ,REG_EXTENDED );
tools/perf/util/annotate.h +61 -0
79 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 09/26] perf annotate-arm64: Implement extract_op_location() callback
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Shift and extension modifiers are silently ignored for single-register operands in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Shift and extension modifiers are silently ignored for single-register operands in extract_op_location_arm64(). --
commit e285d74ff460ca1f5c334760a2efdd0b26f56319
Author: Tengda Wu <[email protected]>
perf annotate-arm64: Implement extract_op_location() callback
This commit implements the extract_op_location() callback for the arm64 architecture to handle its specific assembly syntax and addressing modes. This enables perf annotate to resolve memory locations and register types for data type profiling.
Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0
[ ... ]
Quoted 27 lines
tools/perf/util/annotate-arch/annotate-arm64.c +27
-0+static void extract_op_location_arm64 (const struct arch *arch, +struct disasm_line *dl__maybe_unused , +const char *op_str,int op_idx__maybe_unused , +struct annotated_op_loc *op_loc) +{ +const char *s = op_str; +char *p =NULL ; + +if (op_str ==NULL ) +return ; + +/* +* Handle immediate operand. +* e.g., "#0xc600" -> offset = 0xc600, imm = true +* +* After parsing, check for post-modifiers like "lsl #16", which +* typically occur in instructions such as movk, movz, movn, etc. +*/ +if (*s == arch->objdump.imm_char) { + op_loc->offset =strtol (s +1 , &p,0 ); +if (p && p != s +1 ) { + op_loc->imm = true; + s = p; + } + goto check_modifiers; + } +
[ ... ]
Quoted 20 lines
tools/perf/util/annotate-arch/annotate-arm64.c +20
-0+/* Extract the primary register */ + op_loc->reg1 =arch__dwarf_regnum (arch, s); +if (op_loc->reg1 ==-1 ) +return ; + +/* Move to the next operand field, if present */ + s =strchr (s,',' ); +if (s ==NULL ) +return ; + s =skip_spaces (s +1 ); + +/* Extract secondary register or immediate offset */ +if (op_loc->multi_regs) + op_loc->reg2 =arch__dwarf_regnum (arch, s); +else if (*s == arch->objdump.imm_char) + op_loc->offset =strtol (s +1 ,NULL ,0 ); + +check_modifiers: +/* +* Look for a following shift or extension modifier:
tools/perf/util/annotate-arch/annotate-arm64.c static const struct ins_ops *arm64__associate_instruction_ops(struct arch *arch, · line 426
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+* ", lsl #3" -> extend_type = PERF_EXT_NONE, +* shift_type = PERF_SHIFT_LSL, amount = 3 +* ", uxtw #3" -> extend_type = PERF_EXT_UXTW, +* shift_type = PERF_SHIFT_LSL, amount = 3 +* ", uxtw" -> extend_type = PERF_EXT_UXTW, +* shift_type = PERF_SHIFT_LSL, amount = 0 +*/ + s =strchr (s,',' ); +if (s ==NULL ) +return ;
[Severity: Medium]
Will this silently ignore shift and extension modifiers for single-register operands in extract_op_location_arm64()?
When perf annotate processes an instruction like "cmp x0, x1, lsl #3", the source operand string is parsed as "x1, lsl #3". During primary register extraction, the pointer 's' is advanced past the first comma to point directly at "lsl #3".
Because multi_regs is false and the operand is not an immediate, execution falls through to the check_modifiers block. At this point, the string is just "lsl #3". The strchr(s, ',') search will fail and the function returns early without parsing the modifier.
Could this lead to missing annotations and incomplete statistics for these instructions during data type profiling?
Quoted 7 lines
tools/perf/util/annotate-arch/annotate-arm64.c +7
-0+ s =skip_spaces (s +1 ); + + op_loc->extend_type =arm64__check_ext_type (s); + op_loc->shift_type =arm64__check_shift_type (s); +/* ARM64 extended operands are implicitly shifted by LSL. */ +if (op_loc->extend_type !=PERF_EXT_NONE ) + op_loc->shift_type =PERF_SHIFT_LSL ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Re: [PATCH v5 09/26] perf annotate-arm64: Implement extract_op_location() callback
Quoted 91 lines · On 2026/9/8 21:18, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Shift and extension modifiers are silently ignored for single-register operands inextract_op_location_arm64(). --commit
e285d74ff460ca1f5c334760a2efdd0b26f56319
Author: Tengda Wu <[email protected]>perf annotate-arm64: Implement
extract_op_location()callbackThis commit implements the
extract_op_location()callback for the arm64 architecture to handle its specific assembly syntax and addressing modes. This enables perf annotate to resolve memory locations and register types for data type profiling.Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0[ ... ]
Quoted 27 lines
tools/perf/util/annotate-arch/annotate-arm64.c +27
-0+static void extract_op_location_arm64 (const struct arch *arch, +struct disasm_line *dl__maybe_unused , +const char *op_str,int op_idx__maybe_unused , +struct annotated_op_loc *op_loc) +{ +const char *s = op_str; +char *p =NULL ; + +if (op_str ==NULL ) +return ; + +/* +* Handle immediate operand. +* e.g., "#0xc600" -> offset = 0xc600, imm = true +* +* After parsing, check for post-modifiers like "lsl #16", which +* typically occur in instructions such as movk, movz, movn, etc. +*/ +if (*s == arch->objdump.imm_char) { + op_loc->offset =strtol (s +1 , &p,0 ); +if (p && p != s +1 ) { + op_loc->imm = true; + s = p; + } + goto check_modifiers; + } +[ ... ]
Quoted 20 lines
tools/perf/util/annotate-arch/annotate-arm64.c +20
-0+/* Extract the primary register */ + op_loc->reg1 =arch__dwarf_regnum (arch, s); +if (op_loc->reg1 ==-1 ) +return ; + +/* Move to the next operand field, if present */ + s =strchr (s,',' ); +if (s ==NULL ) +return ; + s =skip_spaces (s +1 ); + +/* Extract secondary register or immediate offset */ +if (op_loc->multi_regs) + op_loc->reg2 =arch__dwarf_regnum (arch, s); +else if (*s == arch->objdump.imm_char) + op_loc->offset =strtol (s +1 ,NULL ,0 ); + +check_modifiers: +/* +* Look for a following shift or extension modifier: tools/perf/util/annotate-arch/annotate-arm64.c static const struct ins_ops *arm64__associate_instruction_ops(struct arch *arch, · line 426
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+* ", lsl #3" -> extend_type = PERF_EXT_NONE, +* shift_type = PERF_SHIFT_LSL, amount = 3 +* ", uxtw #3" -> extend_type = PERF_EXT_UXTW, +* shift_type = PERF_SHIFT_LSL, amount = 3 +* ", uxtw" -> extend_type = PERF_EXT_UXTW, +* shift_type = PERF_SHIFT_LSL, amount = 0 +*/ + s =strchr (s,',' ); +if (s ==NULL ) +return ;[Severity: Medium]
Will this silently ignore shift and extension modifiers for single-register operands inextract_op_location_arm64()?When perf annotate processes an instruction like "cmp x0, x1, lsl #3", the source operand string is parsed as "x1, lsl #3". During primary register extraction, the pointer 's' is advanced past the first comma to point directly at "lsl #3".
Quoted 8 lines
Because
multi_regsis false and the operand is not an immediate, execution falls through to thecheck_modifiersblock. At this point, the string is just "lsl #3". Thestrchr(s, ',')search will fail and the function returns early without parsing the modifier.Could this lead to missing annotations and incomplete statistics for these instructions during data type profiling?
Yes, that's indeed a problem. It doesn't affect instruction tracking for now, but it does need to be fixed. I'll fix it.
Thanks,
Tengda
Quoted 8 lines
Quoted 7 lines
tools/perf/util/annotate-arch/annotate-arm64.c +7
-0+ s =skip_spaces (s +1 ); + + op_loc->extend_type =arm64__check_ext_type (s); + op_loc->shift_type =arm64__check_shift_type (s); +/* ARM64 extended operands are implicitly shifted by LSL. */ +if (op_loc->extend_type !=PERF_EXT_NONE ) + op_loc->shift_type =PERF_SHIFT_LSL ;
PATCH v5 10/26 perf annotate: Default to --itrace=i1i for data type profiling
In reply to Tengda WuARM SPE-based sampling can synthesize multiple events on a single instruction, as shown below. This would affect data type profiling statistics:
Available samples
0 arm_spe_0/ts_enable=1,pa_enable=1,load_filter=1,store_filter=1,min_latency=30/ 0 dummy:u
84K l1d-miss
95K l1d-access
77K llc-miss
58K llc-access
9K tlb-miss
108K tlb-access
0 branch
13K remote-access
108K memory
108K instructions
While 'perf report' provides an interactive menu for users to select a specific event to prevent duplicate counting, 'perf annotate' lacks such a mechanism. Consequently, it counts all instructions across these overlapping events, which inflates the profile and distorts the data type statistics.
Although using the '--itrace' option can work around this issue (e.g.:
perf annotate --data-type --stdio --itrace=i1i), it is inconvenient for users to specify this explicitly every time.
To address this, introduce itrace_synth_opts.default_single_event_per_ip. Set this field to true when data type profiling is enabled and the user has not explicitly specified an itrace option. In itrace_synth_opts__set_default(), use this flag to default to synthesizing at most one event per IP (equivalent to --itrace=i1i) to prevent duplicate sample counts.
--- tools/perf/builtin-annotate.c | 8 ++++++++ tools/perf/util/arm-spe.c | 5 ++++- tools/perf/util/auxtrace.c | 12 ++++++++++-- tools/perf/util/auxtrace.h | 7 ++++++- tools/perf/util/cs-etm.c | 2 +- tools/perf/util/intel-bts.c | 2 +- tools/perf/util/intel-pt.c | 3 ++- 7 files changed, 32 insertions(+), 7 deletions(-)
tools/perf/builtin-annotate.c +8 -0
@@ -873,6 +873,14 @@ int cmd_annotate(int argc, const char **argv) annotate.session =perf_session__new (&data, &annotate.tool);if (IS_ERR (annotate.session))return PTR_ERR (annotate.session); +/* +* Hardware tracing (e.g., ARM SPE) may synthesize multiple events per +* instruction. When data type profiling is enabled, default to synthesizing +* at most one event (equivalent to --itrace=i1i) to prevent skewed +* statistics. +*/ +if (annotate.data_type && !itrace_synth_opts.set) + itrace_synth_opts.default_single_event_per_ip = true; annotate.session->itrace_synth_opts = &itrace_synth_opts;
tools/perf/util/arm-spe.c +4 -1
@@ -2029,7 +2029,10 @@ int arm_spe_process_auxtrace_info(union perf_event *event,if (session->itrace_synth_opts && session->itrace_synth_opts->set) { spe->synth_opts = *session->itrace_synth_opts; }else { -itrace_synth_opts__set_default (&spe->synth_opts, false); +struct itrace_synth_opts *opts = session->itrace_synth_opts; +bool single_event_per_ip = opts ? opts->default_single_event_per_ip : false; + +itrace_synth_opts__set_default (&spe->synth_opts, false, single_event_per_ip);/* Default nanoseconds period not supported */ spe->synth_opts.period_type =PERF_ITRACE_PERIOD_INSTRUCTIONS ; spe->synth_opts.period =1 ;
tools/perf/util/auxtrace.c +10 -2
@@ -1478,8 +1478,15 @@ s64 perf_event__process_auxtrace(const struct perf_tool *tool __maybe_unused,#define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024void itrace_synth_opts__set_default (struct itrace_synth_opts *synth_opts, -bool no_sample) +bool no_sample,bool single_event_per_ip) { +if (single_event_per_ip) { + synth_opts->instructions = true; + synth_opts->period_type =PERF_ITRACE_PERIOD_INSTRUCTIONS ; + synth_opts->period =1 ; +return ; + } + synth_opts->branches = true; synth_opts->transactions = true; synth_opts->ptwrites = true;
@@ -1582,7 +1589,8 @@ int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts,if (!str) {itrace_synth_opts__set_default (synth_opts, - synth_opts->default_no_sample); + synth_opts->default_no_sample, + synth_opts->default_single_event_per_ip);return 0 ; }
tools/perf/util/auxtrace.h +6 -1
@@ -65,6 +65,10 @@ enum itrace_period_type {* struct itrace_synth_opts - AUX area tracing synthesis options. * @set: indicates whether or not options have been set * @default_no_sample: Default to no sampling. +* @default_single_event_per_ip: Default to synthesizing at most 1 event per IP +* (equivalent to --itrace=i1i). Used by commands +* like 'perf annotate --data-type' to avoid duplicate +* sample counts for a single instruction. * @inject: indicates the event (not just the sample) must be fully synthesized * because 'perf inject' will write it out * @instructions: whether to synthesize 'instructions' events
@@ -117,6 +121,7 @@ enum itrace_period_type {struct itrace_synth_opts {bool set;bool default_no_sample; +bool default_single_event_per_ip;bool inject;bool instructions;bool cycles;
@@ -622,7 +627,7 @@ int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts,int itrace_parse_synth_opts (const struct option *opt,const char *str,int unset);void itrace_synth_opts__set_default (struct itrace_synth_opts *synth_opts, -bool no_sample); +bool no_sample,bool single_event_per_ip);size_t perf_event__fprintf_auxtrace_error (union perf_event *event,FILE *fp); void perf_session__auxtrace_error_inc(struct perf_session *session,
tools/perf/util/cs-etm.c +1 -1
@@ -3626,7 +3626,7 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event, etm->synth_opts = *session->itrace_synth_opts; }else {itrace_synth_opts__set_default (&etm->synth_opts, - session->itrace_synth_opts->default_no_sample); + session->itrace_synth_opts->default_no_sample, false); etm->synth_opts.callchain = false; etm->synth_opts.thread_stack = session->itrace_synth_opts->thread_stack; }
tools/perf/util/intel-bts.c +1 -1
@@ -877,7 +877,7 @@ int intel_bts_process_auxtrace_info(union perf_event *event, bts->synth_opts = *session->itrace_synth_opts; }else {itrace_synth_opts__set_default (&bts->synth_opts, - session->itrace_synth_opts->default_no_sample); + session->itrace_synth_opts->default_no_sample, false); bts->synth_opts.thread_stack = session->itrace_synth_opts->thread_stack; }
tools/perf/util/intel-pt.c +2 -1
@@ -4463,7 +4463,8 @@ int intel_pt_process_auxtrace_info(union perf_event *event, }else {struct itrace_synth_opts *opts = session->itrace_synth_opts; -itrace_synth_opts__set_default (&pt->synth_opts, opts->default_no_sample); +itrace_synth_opts__set_default (&pt->synth_opts, + opts->default_no_sample, false);if (!opts->default_no_sample && !opts->inject) { pt->synth_opts.branches = false; pt->synth_opts.callchain = true;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 10/26] perf annotate: Default to --itrace=i1i for data type profiling
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The default_single_event_per_ip option is explicitly ignored for Intel PT, Intel BTS, and CS-ETM…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The default_single_event_per_ip option is explicitly ignored for Intel PT, Intel BTS, and CS-ETM decoders, leading to skewed data type profiling statistics on those PMUs. --
commit 177095456668310745d79faec9f1ed30c358ec07
Author: Tengda Wu <[email protected]>
perf annotate: Default to --itrace=i1i for data type profiling
This commit introduces a default_single_event_per_ip option to the itrace_synth_opts structure. It aims to prevent duplicate sample counts when data type profiling is enabled by synthesizing at most one event per IP, avoiding skewed statistics on hardware tracing PMUs.
tools/perf/util/cs-etm.c int cs_etm__process_auxtrace_info_full(union perf_event *event, · line 3629
Quoted 10 lines
tools/perf/util/cs-etm.c +1
-1@@ -3626,7 +3626,7 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event, etm->synth_opts = *session->itrace_synth_opts; }else {itrace_synth_opts__set_default (&etm->synth_opts, - session->itrace_synth_opts->default_no_sample); + session->itrace_synth_opts->default_no_sample, false);
[Severity: Medium]
Does hardcoding false here defeat the purpose of the fix for CS-ETM traces?
When perf annotate --data-type runs without an explicit --itrace option, cs_etm__process_auxtrace_info_full() ignores the new default_single_event_per_ip configuration.
This causes CS-ETM to synthesize both instructions and branches for a single IP, duplicating sample counts and skewing data type profiling statistics.
Should it pass session->itrace_synth_opts->default_single_event_per_ip instead?
[ ... ]
tools/perf/util/intel-bts.c int intel_bts_process_auxtrace_info(union perf_event *event, · line 880
Quoted 10 lines
tools/perf/util/intel-bts.c +1
-1@@ -877,7 +877,7 @@ int intel_bts_process_auxtrace_info(union perf_event *event, bts->synth_opts = *session->itrace_synth_opts; }else {itrace_synth_opts__set_default (&bts->synth_opts, - session->itrace_synth_opts->default_no_sample); + session->itrace_synth_opts->default_no_sample, false);
[Severity: Medium]
Similarly, is there a specific reason to hardcode false for Intel BTS in intel_bts_process_auxtrace_info()?
This explicitly ignores the single event per IP option for BTS profiles, which may also lead to skewed statistics.
[ ... ]
tools/perf/util/intel-pt.c int intel_pt_process_auxtrace_info(union perf_event *event, · line 4467
Quoted 11 lines
tools/perf/util/intel-pt.c +2
-1@@ -4463,7 +4463,8 @@ int intel_pt_process_auxtrace_info(union perf_event *event, }else {struct itrace_synth_opts *opts = session->itrace_synth_opts; -itrace_synth_opts__set_default (&pt->synth_opts, opts->default_no_sample); +itrace_synth_opts__set_default (&pt->synth_opts, + opts->default_no_sample, false);
[Severity: Medium]
Does Intel PT also need to support this new option?
Hardcoding false here in intel_pt_process_auxtrace_info() forces Intel PT to ignore opts->default_single_event_per_ip. This allows it to synthesize multiple events per instruction (such as transactions or ptwrites), which can distort data type profiles just like ARM SPE did before this patch.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Re: [PATCH v5 10/26] perf annotate: Default to --itrace=i1i for data type profiling
In reply to Tengda WuQuoted 28 lines · Tengda Wu
ARM SPE-based sampling can synthesize multiple events on a single instruction, as shown below. This would affect data type profiling statistics:
Available samples
0 arm_spe_0/ts_enable=1,pa_enable=1,load_filter=1,store_filter=1,min_latency=30/ 0 dummy:u
84K l1d-miss
95K l1d-access
77K llc-miss
58K llc-access
9K tlb-miss
108K tlb-access
0 branch
13K remote-access
108K memory
108K instructionsWhile 'perf report' provides an interactive menu for users to select a specific event to prevent duplicate counting, 'perf annotate' lacks such a mechanism. Consequently, it counts all instructions across these overlapping events, which inflates the profile and distorts the data type statistics.
Although using the '--itrace' option can work around this issue (e.g.:
perf annotate --data-type --stdio --itrace=i1i), it is inconvenient for users to specify this explicitly every time.
Quoted 7 lines
To address this, introduce itrace_synth_opts.default_single_event_per_ip. Set this field to true when data type profiling is enabled and the user has not explicitly specified an itrace option. In
itrace_synth_opts__set_default(), use this flag to default to synthesizing at most one event per IP (equivalent to --itrace=i1i) to prevent duplicate sample counts.Signed-off-by Tengda Wu <[email protected]>
Quoted 148 lines
--- tools/perf/builtin-annotate.c | 8 ++++++++ tools/perf/util/arm-spe.c | 5 ++++- tools/perf/util/auxtrace.c | 12 ++++++++++-- tools/perf/util/auxtrace.h | 7 ++++++- tools/perf/util/cs-etm.c | 2 +- tools/perf/util/intel-bts.c | 2 +- tools/perf/util/intel-pt.c | 3 ++- 7 files changed, 32 insertions(+), 7 deletions(-)tools/perf/builtin-annotate.c +8
-0@@ -873,6 +873,14 @@ int cmd_annotate(int argc, const char **argv) annotate.session =perf_session__new (&data, &annotate.tool);if (IS_ERR (annotate.session))return PTR_ERR (annotate.session); +/* +* Hardware tracing (e.g., ARM SPE) may synthesize multiple events per +* instruction. When data type profiling is enabled, default to synthesizing +* at most one event (equivalent to --itrace=i1i) to prevent skewed +* statistics. +*/ +if (annotate.data_type && !itrace_synth_opts.set) + itrace_synth_opts.default_single_event_per_ip = true; annotate.session->itrace_synth_opts = &itrace_synth_opts;tools/perf/util/arm-spe.c +4
-1@@ -2029,7 +2029,10 @@ int arm_spe_process_auxtrace_info(union perf_event *event,if (session->itrace_synth_opts && session->itrace_synth_opts->set) { spe->synth_opts = *session->itrace_synth_opts; }else { -itrace_synth_opts__set_default (&spe->synth_opts, false); +struct itrace_synth_opts *opts = session->itrace_synth_opts; +bool single_event_per_ip = opts ? opts->default_single_event_per_ip : false; + +itrace_synth_opts__set_default (&spe->synth_opts, false, single_event_per_ip);/* Default nanoseconds period not supported */ spe->synth_opts.period_type =PERF_ITRACE_PERIOD_INSTRUCTIONS ; spe->synth_opts.period =1 ;tools/perf/util/auxtrace.c +10
-2@@ -1478,8 +1478,15 @@ s64 perf_event__process_auxtrace(const struct perf_tool *tool __maybe_unused,#define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024void itrace_synth_opts__set_default (struct itrace_synth_opts *synth_opts, -bool no_sample) +bool no_sample,bool single_event_per_ip) { +if (single_event_per_ip) { + synth_opts->instructions = true; + synth_opts->period_type =PERF_ITRACE_PERIOD_INSTRUCTIONS ; + synth_opts->period =1 ; +return ; + } + synth_opts->branches = true; synth_opts->transactions = true; synth_opts->ptwrites = true;@@ -1582,7 +1589,8 @@ int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts,if (!str) {itrace_synth_opts__set_default (synth_opts, - synth_opts->default_no_sample); + synth_opts->default_no_sample, + synth_opts->default_single_event_per_ip);return 0 ; }tools/perf/util/auxtrace.h +6
-1@@ -65,6 +65,10 @@ enum itrace_period_type {* struct itrace_synth_opts - AUX area tracing synthesis options. * @set: indicates whether or not options have been set * @default_no_sample: Default to no sampling. +* @default_single_event_per_ip: Default to synthesizing at most 1 event per IP +* (equivalent to --itrace=i1i). Used by commands +* like 'perf annotate --data-type' to avoid duplicate +* sample counts for a single instruction. * @inject: indicates the event (not just the sample) must be fully synthesized * because 'perf inject' will write it out * @instructions: whether to synthesize 'instructions' events @@ -117,6 +121,7 @@ enum itrace_period_type {struct itrace_synth_opts {bool set;bool default_no_sample; +bool default_single_event_per_ip;bool inject;bool instructions;bool cycles;@@ -622,7 +627,7 @@ int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts,int itrace_parse_synth_opts (const struct option *opt,const char *str,int unset);void itrace_synth_opts__set_default (struct itrace_synth_opts *synth_opts, -bool no_sample); +bool no_sample,bool single_event_per_ip);size_t perf_event__fprintf_auxtrace_error (union perf_event *event,FILE *fp); void perf_session__auxtrace_error_inc(struct perf_session *session,tools/perf/util/cs-etm.c +1
-1@@ -3626,7 +3626,7 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event, etm->synth_opts = *session->itrace_synth_opts; }else {itrace_synth_opts__set_default (&etm->synth_opts, - session->itrace_synth_opts->default_no_sample); + session->itrace_synth_opts->default_no_sample, false); etm->synth_opts.callchain = false; etm->synth_opts.thread_stack = session->itrace_synth_opts->thread_stack; }tools/perf/util/intel-bts.c +1
-1@@ -877,7 +877,7 @@ int intel_bts_process_auxtrace_info(union perf_event *event, bts->synth_opts = *session->itrace_synth_opts; }else {itrace_synth_opts__set_default (&bts->synth_opts, - session->itrace_synth_opts->default_no_sample); + session->itrace_synth_opts->default_no_sample, false); bts->synth_opts.thread_stack = session->itrace_synth_opts->thread_stack; }tools/perf/util/intel-pt.c +2
-1@@ -4463,7 +4463,8 @@ int intel_pt_process_auxtrace_info(union perf_event *event, }else {struct itrace_synth_opts *opts = session->itrace_synth_opts; -itrace_synth_opts__set_default (&pt->synth_opts, opts->default_no_sample); +itrace_synth_opts__set_default (&pt->synth_opts, + opts->default_no_sample, false);if (!opts->default_no_sample && !opts->inject) { pt->synth_opts.branches = false; pt->synth_opts.callchain = true;
PATCH v5 11/26 perf arm-spe: Set default synthesized event period to 1
In reply to Tengda WuWhen using --itrace=M for data type profiling on arm64, the 'Percent' values in perf annotate output are all zero:
Annotate type: 'struct mmu_gather_batch' in [kernel.kallsyms] (15 samples):
============================================================================
Percent offset size field
0.00 0 0x10 struct mmu_gather_batch {
0.00 0 0x8 struct mmu_gather_batch* next;
0.00 0x8 0x4 unsigned int nr;
0.00 0xc 0x4 unsigned int max;
0.00 0x10 0 struct encoded_page*[] encoded_pages;
};However, adding the -n option (show-nr-samples) reveals non-zero sample counts:
Annotate type: 'struct mmu_gather_batch' in [kernel.kallsyms] (15 samples):
============================================================================
Samples offset size field
15 0 0x10 struct mmu_gather_batch {
13 0 0x8 struct mmu_gather_batch* next;
2 0x8 0x4 unsigned int nr;
0 0xc 0x4 unsigned int max;
0 0x10 0 struct encoded_page*[] encoded_pages;
};The root cause is that when --itrace is used with non-i/y options (e.g., --itrace=M), the period remains zero after parsing. During subsequent propagation, the final percentage calculation results in zero:
arm_spe_prep_sample()
sample->period = spe->synth_opts.period;
__hists__add_entry()
he.stat.period = sample->period;
annotated_data_type__update_samples()
h->period += period;
h->addr[offset].period += period;
print_annotated_data_value()
double percent = h->period ? (100.0 * period / h->period) : 0;Fix this by defaulting the period to 1 in the ARM SPE auxtrace info processing path if it is not explicitly specified. This ensures that downstream tool components can correctly accumulate periods and compute event percentages for all synthesized SPE events.
With this fix applied, the result is as follows:
Annotate type: 'struct mmu_gather_batch' in [kernel.kallsyms] (15 samples):
============================================================================
Percent offset size field
100.00 0 0x10 struct mmu_gather_batch {
86.67 0 0x8 struct mmu_gather_batch* next;
13.33 0x8 0x4 unsigned int nr;
0.00 0xc 0x4 unsigned int max;
0.00 0x10 0 struct encoded_page*[] encoded_pages;
};--- tools/perf/util/arm-spe.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
tools/perf/util/arm-spe.c +11 -0
@@ -2043,6 +2043,17 @@ int arm_spe_process_auxtrace_info(union perf_event *event, err = -EINVAL ; goto err_free_queues; } + +/* +* When --itrace is used with non-i/y options (e.g., --itrace=M), +* the period remains 0 because the parser does not apply a default +* for those paths. However, synthesized SPE events such as memory +* accesses, TLB walks, and cache misses still require a valid +* sample->period to correctly accumulate periods and compute event +* percentages. Set it to 1 to ensure proper accounting. +*/ + spe->synth_opts.period = spe->synth_opts.period ?:1 ; +if (spe->synth_opts.period >1 )ui__warning ("Arm SPE has a hardware-based sampling period.\n\n" "--itrace periods > 1i downsample by an interval of n SPE samples rather than n instructions.\n" );
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 11/26] perf arm-spe: Set default synthesized event period to 1
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
When using --itrace=M for data type profiling on arm64, the 'Percent' values in perf annotate output are all zero:
Annotate type: 'struct
mmu_gather_batch' in [kernel.kallsyms] (15 samples):
============================================================================Percent offset size field 0.00 0 0x10 struct mmu_gather_batch { 0.00 0 0x8 struct mmu_gather_batch* next; 0.00 0x8 0x4 unsigned int nr; 0.00 0xc 0x4 unsigned int max; 0.00 0x10 0 struct encoded_page*[] encoded_pages; };
Quoted 8 lines
However, adding the -n option (show-nr-samples) reveals non-zero sample counts:
[ ... ]
0.00 0xc 0x4 unsigned int max; 0.00 0x10 0 struct encoded_page*[] encoded_pages; };Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
PATCH v5 12/26 perf annotate-data: Extract invalidate_reg_state() as a common helper
In reply to Tengda WuThe invalidate_reg_state() function is currently static and only used within the x86 data type profiling implementation. To prepare for adding arm64 instruction tracking support, extract it as a common helper that can be shared across architectures.
No functional change intended.
--- tools/perf/util/annotate-arch/annotate-x86.c | 9 --------- tools/perf/util/annotate-data.c | 9 +++++++++ tools/perf/util/annotate-data.h | 1 + 3 files changed, 10 insertions(+), 9 deletions(-)
tools/perf/util/annotate-arch/annotate-x86.c +0 -9
@@ -207,15 +207,6 @@ static int x86__cpuid_parse(struct arch *arch, const char *cpuid) }#ifdef HAVE_LIBDW_SUPPORT -static void invalidate_reg_state (struct type_state_reg *reg) -{ - reg->kind =TSR_KIND_INVALID ; - reg->ok = false; - reg->lifetime_active = false; - reg->lifetime_end =0 ; - reg->copied_from =-1 ; -} -static void update_insn_state_x86 (struct type_state *state,struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl)
tools/perf/util/annotate-data.c +9 -0
@@ -638,6 +638,15 @@ struct type_state_stack *findnew_stack_state(struct type_state *state,return stack; } +void invalidate_reg_state (struct type_state_reg *reg) +{ + reg->kind =TSR_KIND_INVALID ; + reg->ok = false; + reg->lifetime_active = false; + reg->lifetime_end =0 ; + reg->copied_from =-1 ; +} +/* Maintain a cache for quick global variable lookup */ struct global_var_entry {struct rb_node node;
tools/perf/util/annotate-data.h +1 -0
@@ -258,6 +258,7 @@ void set_stack_state(struct type_state_stack *stack, int offset, u8 kind, Dwarf_Die *type_die, int ptr_offset);struct type_state_stack *find_stack_state (struct type_state *state,int offset); +void invalidate_reg_state (struct type_state_reg *reg);bool get_global_var_type (Dwarf_Die *cu_die,struct data_loc_info *dloc,u64 ip,u64 var_addr,int *var_offset,Dwarf_Die *type_die);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 12/26] perf annotate-data: Extract invalidate_reg_state() as a common helper
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
The
invalidate_reg_state()function is currently static and only used within the x86 data type profiling implementation. To prepare for adding arm64 instruction tracking support, extract it as a common helper that can be shared across architectures.No functional change intended.
Reviewed-by Shuai Xue <[email protected]>Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
PATCH v5 13/26 perf annotate-arm64: Enable instruction tracking support
In reply to Tengda WuEnable basic instruction tracking for arm64 by implementing three essential functions in the find_data_type_block() call path:
find_data_type_block
-> arch_supports_insn_tracking (1)
-> find_data_type_insn
-> init_type_state (2)
-> update_var_state
-> update_insn_state (3)Changes:
* arch_supports_insn_tracking(): add arm64 to the list of supported architectures, allowing find_data_type_block() to proceed with data type analysis.
* init_type_state(): correctly identify ret_reg, stack_reg, and caller-saved registers for arm64 during type state initialization.
* update_insn_state(): add the update_insn_state_arm64() callback with conservative register invalidation for call instructions and those with destination register(s), preventing stale type propagation. Full instruction-level analysis support will be added incrementally in later patches.
With these changes, arm64 gains support for basic variable type inference during instruction tracking.
--- .../perf/util/annotate-arch/annotate-arm64.c | 78 +++++++++++++++++++ tools/perf/util/annotate-data.c | 11 ++- 2 files changed, 88 insertions(+), 1 deletion(-)
tools/perf/util/annotate-arch/annotate-arm64.c +78 -0
@@ -9,6 +9,10 @@#include <regex.h> #include "../annotate.h" #include "../disasm.h" +#include "../annotate-data.h" +#include "../debug.h" +#include "../map.h" +#include "../symbol.h" struct arch_arm64 {struct arch arch;
@@ -442,6 +446,77 @@ static void extract_op_location_arm64(const struct arch *arch, } } +#ifdef HAVE_LIBDW_SUPPORT +static bool is_readonly_branch_or_cmp (const char *name) +{ +return !strncmp (name,"cb" ,2 ) || !strncmp (name,"tb" ,2 ) || + !strcmp (name,"tst" ) || !strcmp (name,"teq" ) || + !strcmp (name,"cmp" ) || !strcmp (name,"cmn" ) || + !strcmp (name,"ccmp" ) || !strcmp (name,"ccmn" ); +} + +static void update_insn_state_arm64 (struct type_state *state, +struct data_loc_info *dloc,Dwarf_Die *cu_die__maybe_unused , +struct disasm_line *dl) +{ +struct annotated_insn_loc loc; +struct annotated_op_loc *dst = &loc.ops[INSN_OP_TARGET ]; +u32 insn_offset = dl->al.offset; + +/* Skip read-only instructions that do not affect register type state */ +if (is_readonly_branch_or_cmp (dl->ins.name)) +return ; + +if (annotate_get_insn_location (dloc->arch, dl, &loc) <0 ) +return ; + +/* +* Invalidate caller-saved registers on function calls per ARM64 AAPCS64 +* ABI, unless DWARF location info indicates the register remains valid +* beyond the call address. +*/ +if (ins__is_call (&dl->ins)) { +struct symbol *func = dl->ops.target.sym; +const char *call_name; +u64 call_addr; + + call_name = func ? func->name : dl->ops.target.name; +pr_debug_dtp ("call [%x] %s\n" , insn_offset, call_name ?:"<unknown>" ); + +/* Invalidate caller-saved registers after call */ + call_addr =map__rip_2objdump (dloc->ms->map, + dloc->ms->sym->start + dl->al.offset); +for (unsigned int i =0 ; i <ARRAY_SIZE (state->regs); i++) { +struct type_state_reg *reg = &state->regs[i]; + +if (!reg->caller_saved) +continue ; +/* Keep register valid within DWARF location lifetime */ +if (reg->lifetime_active && call_addr < reg->lifetime_end) +continue ; +invalidate_reg_state (reg); + } +return ; + } + +/* +* Invalidate destination register(s) for unsupported instructions to +* prevent stale type info from propagating to subsequent instructions. +*/ +if (has_reg_type (state, dst->reg1) && !dst->mem_ref) { +pr_debug_dtp ("%s [%x] invalidate reg%d" , + dl->ins.name, insn_offset, dst->reg1); +invalidate_reg_state (&state->regs[dst->reg1]); +if (dst->multi_regs &&has_reg_type (state, dst->reg2)) { +pr_debug_dtp (" and reg%d" , dst->reg2); +invalidate_reg_state (&state->regs[dst->reg2]); + } +pr_debug_dtp ("\n" ); +return ; + } +} +#endif +const struct arch *arch__new_arm64 (const struct e_machine_and_e_flags *id,const char *cpuid__maybe_unused ) {
@@ -461,6 +536,9 @@ const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id, arch->objdump.imm_char ='#' ; arch->associate_instruction_ops = arm64__associate_instruction_ops; arch->extract_op_location = extract_op_location_arm64; +#ifdef HAVE_LIBDW_SUPPORT + arch->update_insn_state = update_insn_state_arm64; +#endif /* bl, blr */ err =regcomp (&arm->call_insn,"^blr?$" ,REG_EXTENDED );
tools/perf/util/annotate-data.c +10 -1
@@ -28,6 +28,7 @@/* register number of the stack pointer */ #define X86_REG_SP 7 +#define ARM64_REG_SP 31static void delete_var_types (struct die_var_type *var_types);
@@ -178,6 +179,13 @@ static void init_type_state(struct type_state *state, const struct arch *arch) state->regs[11 ].caller_saved = true; state->ret_reg =0 ; state->stack_reg =X86_REG_SP ; + }else if (arch__is_arm64 (arch)) { +int i; + +for (i =0 ; i <18 ; i++) + state->regs[i].caller_saved = true; + state->ret_reg =0 ; + state->stack_reg =ARM64_REG_SP ; } }
@@ -1437,7 +1445,8 @@ static enum type_match_result find_data_type_insn(struct data_loc_info *dloc,static int arch_supports_insn_tracking (struct data_loc_info *dloc) { -if ((arch__is_x86 (dloc->arch)) || (arch__is_powerpc (dloc->arch))) +if (arch__is_x86 (dloc->arch) ||arch__is_powerpc (dloc->arch) || +arch__is_arm64 (dloc->arch))return 1 ;return 0 ; }
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 13/26] perf annotate-arm64: Enable instruction tracking support
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Incomplete initialization of ARM64 caller-saved registers in init_type_state() misses x30 (Link…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incomplete initialization of ARM64 caller-saved registers in init_type_state() misses x30 (Link Register) and x18. According to the ARM64 AAPCS64 ABI, x30 is intrinsically caller-saved (it is clobbered by function calls to store the return address), and x18 is also a temporary/caller-saved register (unless strictly reserved by a platform ABI). Because these registers are omitted during initialization, their caller_saved flags remain false. Consequently, their data type states are not invalidated across function calls, allowing stale type info to erroneously propagate. --
commit 7f29650747bf4bd656328dd491ef0f827ab60d8c
Author: Tengda Wu <[email protected]>
perf annotate-arm64: Enable instruction tracking support
Enable basic instruction tracking for arm64 by implementing three essential functions in the find_data_type_block() call path. This allows arm64 to support basic variable type inference during instruction tracking.
Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0@@ -442,6 +446,77 @@ static void extract_op_location_arm64(const struct arch *arch,
[ ... ]
Quoted 6 lines
tools/perf/util/annotate-arch/annotate-arm64.c +6
-0+if (ins__is_call (&dl->ins)) { +struct symbol *func = dl->ops.target.sym; +const char *call_name; +u64 call_addr; + + call_name = func ? func->name : dl->ops.target.name;
tools/perf/util/annotate-arch/annotate-arm64.c static void extract_op_location_arm64(const struct arch *arch, · line 493
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+pr_debug_dtp ("call [%x] %s\n" , insn_offset, call_name ?:"<unknown>" ); + +/* Invalidate caller-saved registers after call */ + call_addr =map__rip_2objdump (dloc->ms->map, + dloc->ms->sym->start + dl->al.offset); +for (unsigned int i =0 ; i <ARRAY_SIZE (state->regs); i++) { +struct type_state_reg *reg = &state->regs[i]; + +if (!reg->caller_saved) +continue ;
[Severity: Medium]
Because x30 and x18 are omitted from being marked as caller-saved during type state initialization, their caller_saved flags will be false.
Are these registers unintentionally skipped here, preventing them from being invalidated across function calls?
Quoted 7 lines
tools/perf/util/annotate-arch/annotate-arm64.c +7
-0+/* Keep register valid within DWARF location lifetime */ +if (reg->lifetime_active && call_addr < reg->lifetime_end) +continue ; +invalidate_reg_state (reg); + } +return ; + }
[ ... ]
Quoted 4 lines
tools/perf/util/annotate-data.c +0
-0
[ ... ]
tools/perf/util/annotate-data.c static void init_type_state(struct type_state *state, const struct arch *arch) · line 186
Quoted 9 lines
tools/perf/util/annotate-data.c +5
-0@@ -178,6 +179,13 @@ static void init_type_state(struct type_state *state, const struct arch *arch) state->regs[11 ].caller_saved = true; state->ret_reg =0 ; state->stack_reg =X86_REG_SP ; + }else if (arch__is_arm64 (arch)) { +int i; + +for (i =0 ; i <18 ; i++) + state->regs[i].caller_saved = true;
[Severity: Medium]
According to the ARM64 AAPCS64 ABI, x30 (Link Register) is intrinsically caller-saved since it is clobbered by function calls to store the return address.
x18 is also typically a temporary/caller-saved register unless strictly reserved by a platform ABI.
This loop halts at 17, failing to set the caller_saved flag for x18 and x30.
Could this omission allow stale data type information for these registers to erroneously propagate across function calls?
Quoted 4 lines
tools/perf/util/annotate-data.c +2
-0+ state->ret_reg =0 ; + state->stack_reg =ARM64_REG_SP ; } }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Re: [PATCH v5 13/26] perf annotate-arm64: Enable instruction tracking support
Quoted 73 lines · On 2026/9/8 21:18, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incomplete initialization of ARM64 caller-saved registers ininit_type_state()misses x30 (Link Register) and x18. According to the ARM64 AAPCS64 ABI, x30 is intrinsically caller-saved (it is clobbered by function calls to store the return address), and x18 is also a temporary/caller-saved register (unless strictly reserved by a platform ABI). Because these registers are omitted during initialization, theircaller_savedflags remainfalse. Consequently, their data type states are not invalidated across function calls, allowing stale type info to erroneously propagate. --commit
7f29650747bf4bd656328dd491ef0f827ab60d8c
Author: Tengda Wu <[email protected]>perf annotate-arm64: Enable instruction tracking support
Enable basic instruction tracking for arm64 by implementing three essential functions in the
find_data_type_block()call path. This allows arm64 to support basic variable type inference during instruction tracking.Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0[ ... ]
Quoted 1 line
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0@@ -442,6 +446,77 @@ static void extract_op_location_arm64(const struct arch *arch,[ ... ]
Quoted 6 lines
tools/perf/util/annotate-arch/annotate-arm64.c +6
-0+if (ins__is_call (&dl->ins)) { +struct symbol *func = dl->ops.target.sym; +const char *call_name; +u64 call_addr; + + call_name = func ? func->name : dl->ops.target.name;tools/perf/util/annotate-arch/annotate-arm64.c static void extract_op_location_arm64(const struct arch *arch, · line 493
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+pr_debug_dtp ("call [%x] %s\n" , insn_offset, call_name ?:"<unknown>" ); + +/* Invalidate caller-saved registers after call */ + call_addr =map__rip_2objdump (dloc->ms->map, + dloc->ms->sym->start + dl->al.offset); +for (unsigned int i =0 ; i <ARRAY_SIZE (state->regs); i++) { +struct type_state_reg *reg = &state->regs[i]; + +if (!reg->caller_saved) +continue ;[Severity: Medium]
Because x30 and x18 are omitted from being marked as caller-saved during type state initialization, theircaller_savedflags will be false.Are these registers unintentionally skipped here, preventing them from being invalidated across function calls?
Quoted 7 lines
tools/perf/util/annotate-arch/annotate-arm64.c +7
-0+/* Keep register valid within DWARF location lifetime */ +if (reg->lifetime_active && call_addr < reg->lifetime_end) +continue ; +invalidate_reg_state (reg); + } +return ; + }[ ... ]
Quoted 4 lines
tools/perf/util/annotate-data.c +0
-0[ ... ]
tools/perf/util/annotate-data.c static void init_type_state(struct type_state *state, const struct arch *arch) · line 186
Quoted 9 lines
tools/perf/util/annotate-data.c +5
-0@@ -178,6 +179,13 @@ static void init_type_state(struct type_state *state, const struct arch *arch) state->regs[11 ].caller_saved = true; state->ret_reg =0 ; state->stack_reg =X86_REG_SP ; + }else if (arch__is_arm64 (arch)) { +int i; + +for (i =0 ; i <18 ; i++) + state->regs[i].caller_saved = true;[Severity: Medium]
According to the ARM64 AAPCS64 ABI, x30 (Link Register) is intrinsically caller-saved since it is clobbered by function calls to store the return address.
Quoted 8 lines
x18 is also typically a temporary/caller-saved register unless strictly reserved by a platform ABI.
This loop halts at 17, failing to set the
caller_savedflag for x18 and x30.Could this omission allow stale data type information for these registers to erroneously propagate across function calls?
x18 does need to be set as caller-saved, but x30 is not really necessary, because it generally serves as LR and is not used to carry data type information. So I'm going to add only x18.
Thanks,
Tengda
PATCH v5 14/26 perf annotate-data: Add arch_get_reg_offset helper
In reply to Tengda WuAdd a helper to compute the effective offset from a base register for instruction operands. Currently handles arm64 addressing modes including post-indexed, single-register, SIB-style indexed with shift/extension, and constant-offset register pairs.
Fallback to instruction's raw offset for other architectures.
--- tools/perf/util/annotate-data.c | 106 +++++++++++++++++++++++++++----- tools/perf/util/annotate-data.h | 4 +- 2 files changed, 94 insertions(+), 16 deletions(-)
tools/perf/util/annotate-data.c +91 -15
@@ -1118,6 +1118,74 @@ static void setup_stack_canary(struct data_loc_info *dloc) } } +int arch_get_reg_offset (const struct arch *arch,struct annotated_op_loc *op_loc, +int breg,struct type_state *state,bool apply_index, +int *offset) +{ +if (arch__is_arm64 (arch)) { +int reg2; + +/* Post-indexed addressing: offset is 0 when not applying index */ +if (op_loc->addr_mode ==PERF_AAM_POST_INDEX && !apply_index) { + *offset =0 ; +return 0 ; + } + +/* Single-register addressing: return the stored offset directly */ +if (!op_loc->multi_regs) { + *offset = op_loc->offset; +return 0 ; + } + +/* +* SIB (Scale-Index-Base) style indexed addressing, e.g.: +* Array element load: ldr dst, [base, index, lsl #3] +* Array element load: ldr dst, [base, index, uxtw #3] +* Array address calc: add dst, base, index lsl #3 +* +* The index reg only computes element offset without modifying +* the underlying data type; the dst type is inherited from base. +* Therefore, the base reg offset can be treated as 0. +*/ +if (op_loc->extend_type || op_loc->shift_type) { +/* Memory references use the breg as the address base. */ +if (op_loc->mem_ref) { + *offset =0 ; +return 0 ; + } +/* +* Otherwise, require breg to be a known pointer type to +* avoid treating integer operations as address calculations. +*/ +if (has_reg_type (state, breg) && state->regs[breg].ok && + (state->regs[breg].kind ==TSR_KIND_POINTER || + (state->regs[breg].kind ==TSR_KIND_TYPE && +dwarf_tag (&state->regs[breg].type) == DW_TAG_pointer_type))) { + *offset =0 ; +return 0 ; + } +return -1 ; + } + +/* +* For register-based addressing without shift/extension, +* reg2 may be a constant offset. Use its value as the offset +* from the base register. +*/ + reg2 = op_loc->reg1 == breg ? op_loc->reg2 : op_loc->reg1; +if (has_reg_type (state, reg2) && state->regs[reg2].ok && + state->regs[reg2].kind ==TSR_KIND_CONST ) { + *offset = (s64 )state->regs[reg2].imm_value; +return 0 ; + } + +return -1 ; + } + + *offset = op_loc->offset; +return 0 ; +} +/* * It's at the target address, check if it has a matching type. * It returns PERF_TMR_BAIL_OUT when it looks up per-cpu variables which
@@ -1132,18 +1200,26 @@ static enum type_match_result check_matching_type(struct type_state *state,Dwarf_Word size;u32 insn_offset = dl->al.offset;int reg = dloc->op->reg1; -int offset = dloc->op->offset; +int offset;const char *offset_sign ="" ;bool retry = true; -if (offset <0 ) { - offset = -offset; - offset_sign ="-" ; +again: +if (arch_get_reg_offset (dloc->arch, dloc->op, reg, state, false, &offset)) { +/* +* Fall back to the instruction's offset. This prevents the +* register type of the instruction from being completely lost. +* Compared to dropping this type inference entirely, providing +* a rough type hint is likely more useful. +*/ + offset = dloc->op->offset; } -again: +if (offset <0 ) + offset_sign ="-" ; +pr_debug_dtp ("chk [%x] reg%d offset=%s%#x ok=%d kind=%d " , - insn_offset, reg, offset_sign, offset, + insn_offset, reg, offset_sign,abs (offset), state->regs[reg].ok, state->regs[reg].kind);if (!state->regs[reg].ok)
@@ -1166,7 +1242,7 @@ static enum type_match_result check_matching_type(struct type_state *state,*/ ptr_type =die_get_pointer_type (&state->regs[reg].type, &ptr_die);if (!ptr_type) { -if (dloc->op->offset <0 && reg != state->stack_reg) +if (offset <0 && reg != state->stack_reg) goto check_kernel;return PERF_TMR_NO_POINTER ;
@@ -1176,7 +1252,7 @@ static enum type_match_result check_matching_type(struct type_state *state,if (__die_get_real_type (ptr_type, type_die) ==NULL )return PERF_TMR_NO_POINTER ; - dloc->type_offset = dloc->op->offset + state->regs[reg].offset; + dloc->type_offset = offset + state->regs[reg].offset;if (dwarf_tag (type_die) == DW_TAG_typedef)die_get_real_type (type_die, &sized_type);
@@ -1205,7 +1281,7 @@ static enum type_match_result check_matching_type(struct type_state *state,*/ *type_die = state->regs[reg].type; - dloc->type_offset = dloc->op->offset + state->regs[reg].offset; + dloc->type_offset = offset + state->regs[reg].offset;/* Get the size of the actual type */ if (dwarf_aggregate_size(type_die, &size) < 0 ||
@@ -1224,7 +1300,7 @@ static enum type_match_result check_matching_type(struct type_state *state,*/ *type_die = state->regs[reg].type; - dloc->type_offset = dloc->op->offset; + dloc->type_offset = offset;/* Get the size of the actual type */ if (dwarf_aggregate_size(type_die, &size) < 0 ||
@@ -1248,7 +1324,7 @@ static enum type_match_result check_matching_type(struct type_state *state, }if (state->regs[reg].kind ==TSR_KIND_PERCPU_BASE ) { -u64 var_addr = dloc->op->offset; +u64 var_addr = (s64 ) offset;int var_offset;pr_debug_dtp ("percpu var" );
@@ -1275,7 +1351,7 @@ static enum type_match_result check_matching_type(struct type_state *state,if (state->regs[reg].kind ==TSR_KIND_CONST &&dso__kernel (map__dso (dloc->ms->map))) { -if (dloc->op->offset <0 && reg != state->stack_reg && reg != dloc->fbreg) +if (offset <0 && reg != state->stack_reg && reg != dloc->fbreg) goto check_kernel; } check_non_register:
@@ -1362,7 +1438,7 @@ static enum type_match_result check_matching_type(struct type_state *state, arch__is_x86(dloc->arch)) {pr_debug_dtp ("this-cpu var" ); - addr = dloc->op->offset; + addr = (s64 ) offset;if (get_global_var_type (cu_die, dloc, dloc->ip, addr, &offset, type_die)) {
@@ -1373,8 +1449,8 @@ static enum type_match_result check_matching_type(struct type_state *state, }/* Access to global variable like "-0x7dcf0500(,%rdx,8)" */ -if (dloc->op->offset <0 && reg != state->stack_reg) { - addr = (s64 ) dloc->op->offset; +if (offset <0 && reg != state->stack_reg) { + addr = (s64 ) offset;if (get_global_var_type (cu_die, dloc, dloc->ip, addr, &offset, type_die)) {
tools/perf/util/annotate-data.h +3 -1
@@ -265,7 +265,9 @@ bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc,bool get_global_var_info (struct data_loc_info *dloc,u64 addr,const char **var_name,int *var_offset);void pr_debug_type_name (Dwarf_Die *die,enum type_state_kind kind); - +int arch_get_reg_offset (const struct arch *arch,struct annotated_op_loc *op_loc, +int breg,struct type_state *state,bool apply_index, +int *offset);#else /* HAVE_LIBDW_SUPPORT */ static inline struct annotated_data_type *
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 14/26] perf annotate-data: Add arch_get_reg_offset helper
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The newly computed effective `offset` from `arch_get_reg_offset` is not propagated to…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The newly computed effective offset from arch_get_reg_offset is not propagated to dloc->type_offset before querying stack variables, breaking stack type tracking for indexed addressing modes on ARM64.
- [Low] The debug logging variable offset_sign is not reset across loop iterations, causing positive offsets to be printed as negative.
--
commit 090fc5d342fd0b37d2080a84b8472453f678a2e5
Author: Tengda Wu <[email protected]>
perf annotate-data: Add arch_get_reg_offset helper
Adds a helper to compute the effective offset from a base register for instruction operands. Currently handles arm64 addressing modes and falls back to the instruction's raw offset for other architectures.
Quoted 4 lines
tools/perf/util/annotate-data.c +0
-0
[ ... ]
Quoted 16 lines
tools/perf/util/annotate-data.c +5
-4@@ -1132,18 +1200,26 @@ static enum type_match_result check_matching_type(struct type_state *state,Dwarf_Word size;u32 insn_offset = dl->al.offset;int reg = dloc->op->reg1; -int offset = dloc->op->offset; +int offset;const char *offset_sign ="" ;bool retry = true; -if (offset <0 ) { - offset = -offset; - offset_sign ="-" ; +again: +if (arch_get_reg_offset (dloc->arch, dloc->op, reg, state, false, &offset)) { +/* +* Fall back to the instruction's offset. This prevents the
tools/perf/util/annotate-data.c static enum type_match_result check_matching_type(struct type_state *state, · line 1219
Quoted 10 lines
tools/perf/util/annotate-data.c +7
-1+* register type of the instruction from being completely lost. +* Compared to dropping this type inference entirely, providing +* a rough type hint is likely more useful. +*/ + offset = dloc->op->offset; } -again: +if (offset <0 ) + offset_sign ="-" ;
[Severity: Low]
Does this code leave offset_sign set to "-" across loop iterations?
If check_matching_type() triggers a retry (goto again) after processing a negative offset, and the new offset for the retry is positive, offset_sign will still point to "-" instead of being cleared.
[Severity: Medium]
Since this code now dynamically computes offset using arch_get_reg_offset(), is it necessary to update dloc->type_offset with this newly computed value?
Later in this function, during stack variable lookups, the code relies on dloc->type_offset:
tools/perf/util/annotate-data.c:check_matching_type() {
...
check_non_register:
if (reg == dloc->fbreg || reg == state->stack_reg) {
...
stack = find_stack_state(state, dloc->type_offset);
...
}And similarly for CFA stack lookups:
tools/perf/util/annotate-data.c:check_matching_type() {
...
stack = find_stack_state(state, dloc->type_offset - fboff);
...
}If dloc->type_offset isn't updated to reflect the true effective offset calculated by arch_get_reg_offset(), will find_stack_state() end up using a stale or incorrect offset (e.g. 0 instead of the index offset) for stack-based array accesses on ARM64?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Re: [PATCH v5 14/26] perf annotate-data: Add arch_get_reg_offset helper
Quoted 49 lines · On 2026/9/8 21:22, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The newly computed effectiveoffsetfromarch_get_reg_offsetis not propagated todloc->type_offsetbefore querying stack variables, breaking stack type tracking for indexed addressing modes on ARM64.
- [Low] The debug logging variableoffset_signis not reset across loop iterations, causing positive offsets to be printed as negative.
--commit
090fc5d342fd0b37d2080a84b8472453f678a2e5
Author: Tengda Wu <[email protected]>perf annotate-data: Add
arch_get_reg_offsethelperAdds a helper to compute the effective offset from a base register for instruction operands. Currently handles arm64 addressing modes and falls back to the instruction's raw offset for other architectures.
Quoted 4 lines
tools/perf/util/annotate-data.c +0
-0[ ... ]
Quoted 16 lines
tools/perf/util/annotate-data.c +5
-4@@ -1132,18 +1200,26 @@ static enum type_match_result check_matching_type(struct type_state *state,Dwarf_Word size;u32 insn_offset = dl->al.offset;int reg = dloc->op->reg1; -int offset = dloc->op->offset; +int offset;const char *offset_sign ="" ;bool retry = true; -if (offset <0 ) { - offset = -offset; - offset_sign ="-" ; +again: +if (arch_get_reg_offset (dloc->arch, dloc->op, reg, state, false, &offset)) { +/* +* Fall back to the instruction's offset. This prevents the tools/perf/util/annotate-data.c static enum type_match_result check_matching_type(struct type_state *state, · line 1219
Quoted 10 lines
tools/perf/util/annotate-data.c +7
-1+* register type of the instruction from being completely lost. +* Compared to dropping this type inference entirely, providing +* a rough type hint is likely more useful. +*/ + offset = dloc->op->offset; } -again: +if (offset <0 ) + offset_sign ="-" ;[Severity: Low]
Does this code leaveoffset_signset to "-" across loop iterations?
Quoted 4 lines
If
check_matching_type()triggers a retry (goto again) after processing a negative offset, and the new offset for the retry is positive,offset_signwill still point to "-" instead of being cleared.
Yes, it should be changed to:
offset_sign = offset < 0 ? "-" : "";
Quoted 24 lines
[Severity: Medium]
Since this code now dynamically computes offset usingarch_get_reg_offset(), is it necessary to updatedloc->type_offsetwith this newly computed value?Later in this function, during stack variable lookups, the code relies on
dloc->type_offset:tools/perf/util/annotate-data.c:check_matching_type() { ... check_non_register: if (reg == dloc->fbreg || reg == state->stack_reg) { ... stack = find_stack_state(state, dloc->type_offset); ... }And similarly for CFA stack lookups:
tools/perf/util/annotate-data.c:check_matching_type() { ... stack = find_stack_state(state, dloc->type_offset - fboff); ... }
Quoted 5 lines
If
dloc->type_offsetisn't updated to reflect the true effective offset calculated byarch_get_reg_offset(), willfind_stack_state()end up using a stale or incorrect offset (e.g. 0 instead of the index offset) for stack-based array accesses on ARM64?
Yes, dloc->type_offset should be replaced with the currently computed offset when calling find_stack_state.
Thanks,
Tengda
PATCH v5 15/26 perf annotate-arm64: Track return type after call instructions
In reply to Tengda WuEnable data type profiling to track function return values in arm64 annotations by looking up the DWARF return type of called functions.
--- tools/perf/util/annotate-arch/annotate-arm64.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
tools/perf/util/annotate-arch/annotate-arm64.c +17 -1
@@ -456,7 +456,7 @@ static bool is_readonly_branch_or_cmp(const char *name) }static void update_insn_state_arm64 (struct type_state *state, -struct data_loc_info *dloc,Dwarf_Die *cu_die__maybe_unused , +struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl) {struct annotated_insn_loc loc;
@@ -479,6 +479,8 @@ static void update_insn_state_arm64(struct type_state *state,struct symbol *func = dl->ops.target.sym;const char *call_name;u64 call_addr; +struct type_state_reg *tsr; +Dwarf_Die type_die; call_name = func ? func->name : dl->ops.target.name;pr_debug_dtp ("call [%x] %s\n" , insn_offset, call_name ?:"<unknown>" );
@@ -496,6 +498,20 @@ static void update_insn_state_arm64(struct type_state *state,continue ;invalidate_reg_state (reg); } + +/* Update register with the return type (if any) */ +if (call_name &&die_find_func_rettype (cu_die, call_name, &type_die)) { + tsr = &state->regs[state->ret_reg]; +invalidate_reg_state (tsr); + tsr->type = type_die; + tsr->kind =TSR_KIND_TYPE ; + tsr->offset =0 ; + tsr->ok = true; + +pr_debug_dtp ("call [%x] return -> reg%d" , + insn_offset, state->ret_reg); +pr_debug_type_name (&type_die, tsr->kind); + }return ; }
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 15/26] perf annotate-arm64: Track return type after call instructions
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines
Enable data type profiling to track function return values in arm64 annotations by looking up the DWARF return type of called functions.
Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
PATCH v5 16/26 perf annotate-arm64: Support load instruction tracking
In reply to Tengda WuExtend update_insn_state_arm64() to handle load instructions, tracking register state changes when data is loaded from memory to registers.
Two main categories are considered: standard load (e.g, ldr variants) and load pair (ldp), for example:
ldr dst, [src, #imm]
ldp dst1, dst2, [src, #imm]
For data type propagation, a load pair can be treated as two standard loads combined, first loading dst1 and then dst2. The difference is that when loading dst2, an additional memory offset 'mem_offset' after the first load must be added. This mem_offset can be derived from the instruction mnemonic and the size of the dst register. Each load operation is handled by introducing propagate_load_reg_state().
When processing a load pair, the case where dst and src registers are the same (e.g., ldp x0, x1, [x0]) also needs to be considered. Therefore, before propagating data types, a snapshot of the src register's state must be saved and then passed to propagate_load_reg_state().
Finally, handle the side effects of pre-index and post-index addressing via adjust_reg_index_state().
A real-world example is shown below:
ffff80008011f5b0 <pick_task_stop>:
ffff80008011f5b8: ldr x0, [x0, #2712] // x0: struct rq* -> task_struct* * ffff80008011f5c0: ldr w1, [x0, #104]
Before this commit, the type of x0 was incorrectly inferred as 'struct rq':
find data type for 0x68(reg0) at pick_task_stop+0x10 var [8] reg0 offset 0 type='struct rq*'
chk [10] reg0 offset=0x68 ok=1 kind=1 (struct rq*) : Good! final result: type='struct rq'
After this commit, the type of x0 is correctly inferred as 'struct task_struct':
find data type for 0x68(reg0) at pick_task_stop+0x10
var [8] reg0 offset 0 type='struct rq*'
ldr [8] 0xa98(reg0) -> reg0 type='struct task_struct*' chk [10] reg0 offset=0x68 ok=1 kind=1 (struct task_struct*) : Good! final result: type='struct task_struct'
--- .../perf/util/annotate-arch/annotate-arm64.c | 202 +++++++++++++++++- 1 file changed, 201 insertions(+), 1 deletion(-)
tools/perf/util/annotate-arch/annotate-arm64.c +201 -1
@@ -455,11 +455,206 @@ static bool is_readonly_branch_or_cmp(const char *name) !strcmp (name,"ccmp" ) || !strcmp (name,"ccmn" ); } +static int arm64__reg_size (const char *reg) +{ +if (!reg || !*reg || !arm64__is_reg (reg)) +return -1 ; + +if (reg[0 ] =='w' ) +return 4 ; + +if (reg[0 ] =='x' || !strncmp (reg,"sp" ,2 )) +return 8 ; + +return -1 ; +} + +/* Apply addressing mode (pre-index, post-index) to register state */ +static void adjust_reg_index_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl, +struct annotated_op_loc *op_loc) +{ +struct type_state_reg *tsr; +int reg = op_loc->reg1; +int offset; + +if (op_loc->addr_mode !=PERF_AAM_PRE_INDEX && + op_loc->addr_mode !=PERF_AAM_POST_INDEX ) +return ; + +if (!has_reg_type (state, reg) || !state->regs[reg].ok) +return ; + + tsr = &state->regs[reg]; + tsr->copied_from =-1 ; + +if (arch_get_reg_offset (dloc->arch, op_loc, reg, state, true, &offset)) { +invalidate_reg_state (tsr); +return ; + } + + tsr->offset += offset; + +pr_debug_dtp ("%s [%x] %s-index %#x(reg%d) -> reg%d" , dl->ins.name, + (u32 ) dl->al.offset, op_loc->addr_mode ==PERF_AAM_PRE_INDEX ? +"pre" :"post" , offset, reg, reg); +pr_debug_type_name (&tsr->type, tsr->kind); +} + +/* +* Match standard load variants (ldr, ldur, ldar, ldp) that follow +* straightforward load semantics: memory source on the right +* and target registers on the left (written). Excludes exclusive loads +* (ldxr, ldxp, etc.) and acquire/exclusive combination variants. +*/ +static bool is_standard_load_insn (const char *name) +{ +return !strncmp (name,"ldr" ,3 ) ||/* ldr, ldrb, ldrh, ldrsb, ldrsh, ldrsw */ + !strncmp (name,"ldur" ,4 ) ||/* ldur, ldurb, ldurh, ldursb, ldursh, ldursw */ + !strncmp (name,"ldar" ,4 ) ||/* ldar, ldarb, ldarh */ + !strncmp (name,"ldp" ,3 );/* ldp, ldpsw */ +} + +/* +* For load insns: propagate type from source reg state @src_states to @dreg. +* +* @mem_offset accounts for additional memory byte offset when handling multi-reg +* loads (e.g. second target reg in 'ldp'), which is added to the reg offset. +*/ +static int propagate_load_reg_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl,int dreg, +struct annotated_op_loc *src, +struct type_state_reg **src_states, +int mem_offset) +{ +struct type_state_reg *tsr; +struct type_state_reg *src_tsr = src_states[0 ]; +Dwarf_Die type_die; +u32 insn_offset = dl->al.offset; +int sreg = src->reg1; +int reg_offset; + +if (!has_reg_type (state, dreg)) +return -1 ; + + tsr = &state->regs[dreg]; + tsr->copied_from =-1 ; + +retry: +if (arch_get_reg_offset (dloc->arch, src, sreg, state, false, ®_offset)) +return -1 ; + + reg_offset += mem_offset; + +if (!src_tsr || !src_tsr->ok) +return -1 ; + +/* Dereference the pointer if it has one */ +if (src_tsr->kind ==TSR_KIND_TYPE && +die_deref_ptr_type (&src_tsr->type, + src_tsr->offset + reg_offset, &type_die)) { + tsr->type = type_die; + tsr->kind =TSR_KIND_TYPE ; + tsr->offset =0 ; + tsr->ok = true; + +if (src->multi_regs) { +pr_debug_dtp ("%s [%x] %#x(reg%d, reg%d) -> reg%d" , + dl->ins.name, insn_offset, reg_offset, + src->reg1, src->reg2, dreg); + }else { +pr_debug_dtp ("%s [%x] %#x(reg%d) -> reg%d" , + dl->ins.name, insn_offset, reg_offset, + sreg, dreg); + } +pr_debug_type_name (&tsr->type, tsr->kind); +return 0 ; + } + +/* Or try another register if any */ +if (src->multi_regs && src->reg1 != src->reg2 && sreg != src->reg2 && + !(src->extend_type || src->shift_type)) { + sreg = src->reg2; + src_tsr = src_states[1 ]; + goto retry; + } + +return -1 ; +} + +static void update_load_insn_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl, +struct annotated_op_loc *src, +struct annotated_op_loc *dst) +{ +struct type_state_reg snapshots[2 ]; +struct type_state_reg *src_states[2 ] = {}; + +if (!src->mem_ref ||/* exclude PC-relative loads */ + !has_reg_type (state, dst->reg1) || + (dst->multi_regs && !has_reg_type (state, dst->reg2))) + goto out_err_adjust; + +/* +* Snapshot source register states before updating destination registers. +* This avoids state corruption in aliased instructions (e.g., ldp x0, x1, [x0]), +* ensuring the second destination load still uses the original base +* register state. +*/ +if (has_reg_type (state, src->reg1)) { + snapshots[0 ] = state->regs[src->reg1]; + src_states[0 ] = &snapshots[0 ]; + } +if (src->multi_regs &&has_reg_type (state, src->reg2)) { + snapshots[1 ] = state->regs[src->reg2]; + src_states[1 ] = &snapshots[1 ]; + } + +/* Handle the first destination register */ +if (propagate_load_reg_state (state, dloc, dl, dst->reg1, + src, src_states,/*mem_offset=*/ 0 )) + goto out_err_adjust; + +/* Handle the second destination register (if any) */ +if (dst->multi_regs) { +int mem_offset; + +/* +* ldpsw loads two 32-bit signed words into 64-bit registers. +* Memory spacing between elements is 4 bytes, not the register size. +*/ +if (!strcmp (dl->ins.name,"ldpsw" )) + mem_offset =4 ; +else + mem_offset =arm64__reg_size (dl->ops.target.raw); + +if (mem_offset <0 || +propagate_load_reg_state (state, dloc, dl, dst->reg2, + src, src_states, mem_offset)) + goto out_err_adjust; + } + +out_adjust: +adjust_reg_index_state (state, dloc, dl, src); +return ; + +out_err_adjust: +if (has_reg_type (state, dst->reg1)) +invalidate_reg_state (&state->regs[dst->reg1]); +if (dst->multi_regs &&has_reg_type (state, dst->reg2)) +invalidate_reg_state (&state->regs[dst->reg2]); + goto out_adjust; +} +static void update_insn_state_arm64 (struct type_state *state,struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl) {struct annotated_insn_loc loc; +struct annotated_op_loc *src = &loc.ops[INSN_OP_SOURCE ];struct annotated_op_loc *dst = &loc.ops[INSN_OP_TARGET ];u32 insn_offset = dl->al.offset;
@@ -519,7 +714,8 @@ static void update_insn_state_arm64(struct type_state *state,* Invalidate destination register(s) for unsupported instructions to * prevent stale type info from propagating to subsequent instructions. */ -if (has_reg_type (state, dst->reg1) && !dst->mem_ref) { +if (has_reg_type (state, dst->reg1) && !dst->mem_ref && + !is_standard_load_insn (dl->ins.name)) {pr_debug_dtp ("%s [%x] invalidate reg%d" , dl->ins.name, insn_offset, dst->reg1);invalidate_reg_state (&state->regs[dst->reg1]);
@@ -530,6 +726,10 @@ static void update_insn_state_arm64(struct type_state *state,pr_debug_dtp ("\n" ); return; } + +/* Memory to register transfers */ +if (is_standard_load_insn (dl->ins.name)) +update_load_insn_state (state, dloc, dl, src, dst); }#endif
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 16/26] perf annotate-arm64: Support load instruction tracking
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Unintended rollback of valid type state for the first destination register in a multi-register…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Unintended rollback of valid type state for the first destination register in a multi-register load (e.g., ldp) if the second register's type cannot be resolved.
- [Medium] Missing support for TSR_KIND_POINTER in propagate_load_reg_state causes type tracking to silently fail for variables whose addresses are held in registers. --
commit 6913b761b417f151400c62773b3dd0ee4dcddbf2
Author: Tengda Wu <[email protected]>
perf annotate-arm64: Support load instruction tracking
Extend update_insn_state_arm64() to handle load instructions, tracking register state changes when data is loaded from memory to registers. Introduces propagate_load_reg_state() and update_load_insn_state() to handle standard loads and load pairs.
Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0@@ -455,11 +455,206 @@ static bool is_readonly_branch_or_cmp(const char *name)
[ ... ]
Quoted 7 lines
tools/perf/util/annotate-arch/annotate-arm64.c +7
-0+static int propagate_load_reg_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl,int dreg, +struct annotated_op_loc *src, +struct type_state_reg **src_states, +int mem_offset) +{
[ ... ]
tools/perf/util/annotate-arch/annotate-arm64.c static bool is_readonly_branch_or_cmp(const char *name) · line 557
Quoted 7 lines
tools/perf/util/annotate-arch/annotate-arm64.c +7
-0+if (!src_tsr || !src_tsr->ok) +return -1 ; + +/* Dereference the pointer if it has one */ +if (src_tsr->kind ==TSR_KIND_TYPE && +die_deref_ptr_type (&src_tsr->type, + src_tsr->offset + reg_offset, &type_die)) {
[Severity: Medium]
Will this silently fail to propagate types for pointers since it restricts dereferencing strictly to TSR_KIND_TYPE?
If the source register state is TSR_KIND_POINTER (e.g., from an earlier address calculation), it appears the pointer dereference will fail type propagation here, reducing data type profiling coverage.
Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +4
-0+ tsr->type = type_die; + tsr->kind =TSR_KIND_TYPE ; + tsr->offset =0 ; + tsr->ok = true;
[ ... ]
Quoted 6 lines
tools/perf/util/annotate-arch/annotate-arm64.c +6
-0+static void update_load_insn_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl, +struct annotated_op_loc *src, +struct annotated_op_loc *dst) +{
[ ... ]
Quoted 25 lines
tools/perf/util/annotate-arch/annotate-arm64.c +25
-0+/* Handle the first destination register */ +if (propagate_load_reg_state (state, dloc, dl, dst->reg1, + src, src_states,/*mem_offset=*/ 0 )) + goto out_err_adjust; + +/* Handle the second destination register (if any) */ +if (dst->multi_regs) { +int mem_offset; + +/* +* ldpsw loads two 32-bit signed words into 64-bit registers. +* Memory spacing between elements is 4 bytes, not the register size. +*/ +if (!strcmp (dl->ins.name,"ldpsw" )) + mem_offset =4 ; +else + mem_offset =arm64__reg_size (dl->ops.target.raw); + +if (mem_offset <0 || +propagate_load_reg_state (state, dloc, dl, dst->reg2, + src, src_states, mem_offset)) + goto out_err_adjust; + } + +out_adjust:
tools/perf/util/annotate-arch/annotate-arm64.c static bool is_readonly_branch_or_cmp(const char *name) · line 650
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+adjust_reg_index_state (state, dloc, dl, src); +return ; + +out_err_adjust: +if (has_reg_type (state, dst->reg1)) +invalidate_reg_state (&state->regs[dst->reg1]); +if (dst->multi_regs &&has_reg_type (state, dst->reg2)) +invalidate_reg_state (&state->regs[dst->reg2]); + goto out_adjust; +}
[Severity: Medium]
Does this unintentionally discard valid type state for the first destination register if the second destination register's type cannot be resolved?
If an arm64 ldp instruction is analyzed where the first member is trackable, but the second member cannot be resolved, this error path seems to unconditionally invalidate the successfully resolved state of dst->reg1.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
Re: [PATCH v5 16/26] perf annotate-arm64: Support load instruction tracking
Quoted 48 lines · On 2026/9/8 21:23, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Unintended rollback of valid type state for the first destination register in a multi-register load (e.g., ldp) if the second register's type cannot be resolved.
- [Medium] Missing support forTSR_KIND_POINTERinpropagate_load_reg_statecauses type tracking to silently fail for variables whose addresses are held in registers. --commit
6913b761b417f151400c62773b3dd0ee4dcddbf2
Author: Tengda Wu <[email protected]>perf annotate-arm64: Support load instruction tracking
Extend
update_insn_state_arm64()to handle load instructions, tracking register state changes when data is loaded from memory to registers. Introducespropagate_load_reg_state()andupdate_load_insn_state()to handle standard loads and load pairs.Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0[ ... ]
Quoted 1 line
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0@@ -455,11 +455,206 @@ static bool is_readonly_branch_or_cmp(const char *name)[ ... ]
Quoted 7 lines
tools/perf/util/annotate-arch/annotate-arm64.c +7
-0+static int propagate_load_reg_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl,int dreg, +struct annotated_op_loc *src, +struct type_state_reg **src_states, +int mem_offset) +{[ ... ]
tools/perf/util/annotate-arch/annotate-arm64.c static bool is_readonly_branch_or_cmp(const char *name) · line 557
Quoted 7 lines
tools/perf/util/annotate-arch/annotate-arm64.c +7
-0+if (!src_tsr || !src_tsr->ok) +return -1 ; + +/* Dereference the pointer if it has one */ +if (src_tsr->kind ==TSR_KIND_TYPE && +die_deref_ptr_type (&src_tsr->type, + src_tsr->offset + reg_offset, &type_die)) {[Severity: Medium]
Will this silently fail to propagate types for pointers since it restricts dereferencing strictly toTSR_KIND_TYPE?
Quoted 4 lines
If the source register state is
TSR_KIND_POINTER(e.g., from an earlier address calculation), it appears the pointer dereference will fail type propagation here, reducing data type profiling coverage.
Agreed. It is necessary to add a check for TSR_KIND_POINTER similar to what x86 does.
Quoted 56 lines
Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +4
-0+ tsr->type = type_die; + tsr->kind =TSR_KIND_TYPE ; + tsr->offset =0 ; + tsr->ok = true;[ ... ]
Quoted 6 lines
tools/perf/util/annotate-arch/annotate-arm64.c +6
-0+static void update_load_insn_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl, +struct annotated_op_loc *src, +struct annotated_op_loc *dst) +{[ ... ]
Quoted 25 lines
tools/perf/util/annotate-arch/annotate-arm64.c +25
-0+/* Handle the first destination register */ +if (propagate_load_reg_state (state, dloc, dl, dst->reg1, + src, src_states,/*mem_offset=*/ 0 )) + goto out_err_adjust; + +/* Handle the second destination register (if any) */ +if (dst->multi_regs) { +int mem_offset; + +/* +* ldpsw loads two 32-bit signed words into 64-bit registers. +* Memory spacing between elements is 4 bytes, not the register size. +*/ +if (!strcmp (dl->ins.name,"ldpsw" )) + mem_offset =4 ; +else + mem_offset =arm64__reg_size (dl->ops.target.raw); + +if (mem_offset <0 || +propagate_load_reg_state (state, dloc, dl, dst->reg2, + src, src_states, mem_offset)) + goto out_err_adjust; + } + +out_adjust:tools/perf/util/annotate-arch/annotate-arm64.c static bool is_readonly_branch_or_cmp(const char *name) · line 650
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+adjust_reg_index_state (state, dloc, dl, src); +return ; + +out_err_adjust: +if (has_reg_type (state, dst->reg1)) +invalidate_reg_state (&state->regs[dst->reg1]); +if (dst->multi_regs &&has_reg_type (state, dst->reg2)) +invalidate_reg_state (&state->regs[dst->reg2]); + goto out_adjust; +}[Severity: Medium]
Does this unintentionally discard valid type state for the first destination register if the second destination register's type cannot be resolved?
Quoted 4 lines
If an arm64 ldp instruction is analyzed where the first member is trackable, but the second member cannot be resolved, this error path seems to unconditionally invalidate the successfully resolved state of
dst->reg1.
Agreed.
Thanks,
Tengda
Re: [PATCH v5 16/26] perf annotate-arm64: Support load instruction tracking
In reply to Tengda WuQuoted 192 lines · Tengda Wu
Extend
update_insn_state_arm64()to handle load instructions, tracking register state changes when data is loaded from memory to registers.Two main categories are considered: standard load (e.g, ldr variants) and load pair (ldp), for example:
ldr dst, [src, #imm]
ldp dst1, dst2, [src, #imm]For data type propagation, a load pair can be treated as two standard loads combined, first loading dst1 and then dst2. The difference is that when loading dst2, an additional memory offset '
mem_offset' after the first load must be added. Thismem_offsetcan be derived from the instruction mnemonic and the size of the dst register. Each load operation is handled by introducingpropagate_load_reg_state().When processing a load pair, the case where dst and src registers are the same (e.g., ldp x0, x1, [x0]) also needs to be considered. Therefore, before propagating data types, a snapshot of the src register's state must be saved and then passed to
propagate_load_reg_state().Finally, handle the side effects of pre-index and post-index addressing via
adjust_reg_index_state().A real-world example is shown below:
ffff80008011f5b0<pick_task_stop>:ffff80008011f5b8: ldr x0, [x0, #2712] // x0: struct rq* -> task_struct* * ffff80008011f5c0: ldr w1, [x0, #104]Before this commit, the type of x0 was incorrectly inferred as 'struct rq':
find data type for 0x68(reg0) at
pick_task_stop+0x10var [8] reg0 offset 0 type='struct rq*'
chk [10] reg0 offset=0x68 ok=1 kind=1 (struct rq*) : Good! final result: type='struct rq'After this commit, the type of x0 is correctly inferred as 'struct
task_struct':find data type for 0x68(reg0) at
pick_task_stop+0x10
var [8] reg0 offset 0 type='struct rq*'
ldr [8] 0xa98(reg0) -> reg0 type='struct task_struct*' chk [10] reg0 offset=0x68 ok=1 kind=1 (struct task_struct*) : Good! final result: type='structtask_struct'Signed-off-by Li Huafei <[email protected]>Signed-off-by Tengda Wu <[email protected]>--- .../perf/util/annotate-arch/annotate-arm64.c | 202 +++++++++++++++++- 1 file changed, 201 insertions(+), 1 deletion(-)tools/perf/util/annotate-arch/annotate-arm64.c +133
-0@@ -455,11 +455,206 @@ static bool is_readonly_branch_or_cmp(const char *name) !strcmp (name,"ccmp" ) || !strcmp (name,"ccmn" ); } +static int arm64__reg_size (const char *reg) +{ +if (!reg || !*reg || !arm64__is_reg (reg)) +return -1 ; + +if (reg[0 ] =='w' ) +return 4 ; + +if (reg[0 ] =='x' || !strncmp (reg,"sp" ,2 )) +return 8 ; + +return -1 ; +} + +/* Apply addressing mode (pre-index, post-index) to register state */ +static void adjust_reg_index_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl, +struct annotated_op_loc *op_loc) +{ +struct type_state_reg *tsr; +int reg = op_loc->reg1; +int offset; + +if (op_loc->addr_mode !=PERF_AAM_PRE_INDEX && + op_loc->addr_mode !=PERF_AAM_POST_INDEX ) +return ; + +if (!has_reg_type (state, reg) || !state->regs[reg].ok) +return ; + + tsr = &state->regs[reg]; + tsr->copied_from =-1 ; + +if (arch_get_reg_offset (dloc->arch, op_loc, reg, state, true, &offset)) { +invalidate_reg_state (tsr); +return ; + } + + tsr->offset += offset; + +pr_debug_dtp ("%s [%x] %s-index %#x(reg%d) -> reg%d" , dl->ins.name, + (u32 ) dl->al.offset, op_loc->addr_mode ==PERF_AAM_PRE_INDEX ? +"pre" :"post" , offset, reg, reg); +pr_debug_type_name (&tsr->type, tsr->kind); +} + +/* +* Match standard load variants (ldr, ldur, ldar, ldp) that follow +* straightforward load semantics: memory source on the right +* and target registers on the left (written). Excludes exclusive loads +* (ldxr, ldxp, etc.) and acquire/exclusive combination variants. +*/ +static bool is_standard_load_insn (const char *name) +{ +return !strncmp (name,"ldr" ,3 ) ||/* ldr, ldrb, ldrh, ldrsb, ldrsh, ldrsw */ + !strncmp (name,"ldur" ,4 ) ||/* ldur, ldurb, ldurh, ldursb, ldursh, ldursw */ + !strncmp (name,"ldar" ,4 ) ||/* ldar, ldarb, ldarh */ + !strncmp (name,"ldp" ,3 );/* ldp, ldpsw */ +} + +/* +* For load insns: propagate type from source reg state @src_states to @dreg. +* +* @mem_offset accounts for additional memory byte offset when handling multi-reg +* loads (e.g. second target reg in 'ldp'), which is added to the reg offset. +*/ +static int propagate_load_reg_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl,int dreg, +struct annotated_op_loc *src, +struct type_state_reg **src_states, +int mem_offset) +{ +struct type_state_reg *tsr; +struct type_state_reg *src_tsr = src_states[0 ]; +Dwarf_Die type_die; +u32 insn_offset = dl->al.offset; +int sreg = src->reg1; +int reg_offset; + +if (!has_reg_type (state, dreg)) +return -1 ; + + tsr = &state->regs[dreg]; + tsr->copied_from =-1 ; + +retry: +if (arch_get_reg_offset (dloc->arch, src, sreg, state, false, ®_offset)) +return -1 ; + + reg_offset += mem_offset; + +if (!src_tsr || !src_tsr->ok) +return -1 ; + +/* Dereference the pointer if it has one */ +if (src_tsr->kind ==TSR_KIND_TYPE && +die_deref_ptr_type (&src_tsr->type, + src_tsr->offset + reg_offset, &type_die)) { + tsr->type = type_die; + tsr->kind =TSR_KIND_TYPE ; + tsr->offset =0 ; + tsr->ok = true; + +if (src->multi_regs) { +pr_debug_dtp ("%s [%x] %#x(reg%d, reg%d) -> reg%d" , + dl->ins.name, insn_offset, reg_offset, + src->reg1, src->reg2, dreg); + }else { +pr_debug_dtp ("%s [%x] %#x(reg%d) -> reg%d" , + dl->ins.name, insn_offset, reg_offset, + sreg, dreg); + } +pr_debug_type_name (&tsr->type, tsr->kind); +return 0 ; + } + +/* Or try another register if any */ +if (src->multi_regs && src->reg1 != src->reg2 && sreg != src->reg2 && + !(src->extend_type || src->shift_type)) { + sreg = src->reg2; + src_tsr = src_states[1 ]; + goto retry; + } + +return -1 ; +} + +static void update_load_insn_state(struct type_state *state, + struct data_loc_info *dloc, + struct disasm_line *dl, + struct annotated_op_loc *src,
tools/perf/util/annotate-arch/annotate-arm64.c static bool is_readonly_branch_or_cmp(const char *name) · line 600
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+ struct annotated_op_loc *dst) +{ +struct type_state_reg snapshots[2 ]; +struct type_state_reg *src_states[2 ] = {}; + +if (!src->mem_ref ||/* exclude PC-relative loads */ + !has_reg_type (state, dst->reg1) || + (dst->multi_regs && !has_reg_type (state, dst->reg2))) + goto out_err_adjust; +
Just realized the condition here is not quite right -- rejecting too early here leaves no chance for the type of dst->reg1 to be propagated. For example, consider the following instruction:
ldp x1, xzr, [x2]
It would be more appropriate to change it to a check on src->reg1:
if (!src->mem_ref || /* exclude PC-relative loads */ !has_reg_type(state, src->reg1)) goto out_invalidate_all;
After changing it to this, the issue raised by Sashiko in the subsequent patch 0021, where both sreg and fbreg are -1, can also be resolved.
Thanks,
Tengda
Quoted 81 lines
tools/perf/util/annotate-arch/annotate-arm64.c +58
-1+/* +* Snapshot source register states before updating destination registers. +* This avoids state corruption in aliased instructions (e.g., ldp x0, x1, [x0]), +* ensuring the second destination load still uses the original base +* register state. +*/ +if (has_reg_type (state, src->reg1)) { + snapshots[0 ] = state->regs[src->reg1]; + src_states[0 ] = &snapshots[0 ]; + } +if (src->multi_regs &&has_reg_type (state, src->reg2)) { + snapshots[1 ] = state->regs[src->reg2]; + src_states[1 ] = &snapshots[1 ]; + } + +/* Handle the first destination register */ +if (propagate_load_reg_state (state, dloc, dl, dst->reg1, + src, src_states,/*mem_offset=*/ 0 )) + goto out_err_adjust; + +/* Handle the second destination register (if any) */ +if (dst->multi_regs) { +int mem_offset; + +/* +* ldpsw loads two 32-bit signed words into 64-bit registers. +* Memory spacing between elements is 4 bytes, not the register size. +*/ +if (!strcmp (dl->ins.name,"ldpsw" )) + mem_offset =4 ; +else + mem_offset =arm64__reg_size (dl->ops.target.raw); + +if (mem_offset <0 || +propagate_load_reg_state (state, dloc, dl, dst->reg2, + src, src_states, mem_offset)) + goto out_err_adjust; + } + +out_adjust: +adjust_reg_index_state (state, dloc, dl, src); +return ; + +out_err_adjust: +if (has_reg_type (state, dst->reg1)) +invalidate_reg_state (&state->regs[dst->reg1]); +if (dst->multi_regs &&has_reg_type (state, dst->reg2)) +invalidate_reg_state (&state->regs[dst->reg2]); + goto out_adjust; +} +static void update_insn_state_arm64 (struct type_state *state,struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl) {struct annotated_insn_loc loc; +struct annotated_op_loc *src = &loc.ops[INSN_OP_SOURCE ];struct annotated_op_loc *dst = &loc.ops[INSN_OP_TARGET ];u32 insn_offset = dl->al.offset;@@ -519,7 +714,8 @@ static void update_insn_state_arm64(struct type_state *state,* Invalidate destination register(s) for unsupported instructions to * prevent stale type info from propagating to subsequent instructions. */ -if (has_reg_type (state, dst->reg1) && !dst->mem_ref) { +if (has_reg_type (state, dst->reg1) && !dst->mem_ref && + !is_standard_load_insn (dl->ins.name)) {pr_debug_dtp ("%s [%x] invalidate reg%d" , dl->ins.name, insn_offset, dst->reg1);invalidate_reg_state (&state->regs[dst->reg1]);@@ -530,6 +726,10 @@ static void update_insn_state_arm64(struct type_state *state,pr_debug_dtp ("\n" ); return; } + +/* Memory to register transfers */ +if (is_standard_load_insn (dl->ins.name)) +update_load_insn_state (state, dloc, dl, src, dst); }#endif
PATCH v5 17/26 perf annotate-arm64: Support store instruction tracking
In reply to Tengda WuExtend update_insn_state_arm64() to handle standard store instructions (str, stur, stlr, stp). Exclusive stores (e.g., stxr, stxp) are omitted as they exhibit different operand semantics.
Unlike load instructions, a store instruction sets a value in the struct within the memory where the destination register resides, and does not alter its type. Therefore, no processing is required for the transfer.
The only point to note is that store instructions support pre-index and post-index addressing modes, so calling adjust_reg_index_state() is still necessary to handle their addressing.
--- .../perf/util/annotate-arch/annotate-arm64.c | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+)
tools/perf/util/annotate-arch/annotate-arm64.c +25 -0
@@ -649,6 +649,20 @@ static void update_load_insn_state(struct type_state *state, goto out_adjust; } +/* +* Match standard store variants (str, stur, stlr, stp) that follow +* straightforward store semantics: source registers on the left (read-only) +* and memory target on the right. Excludes exclusive stores (stxr, stxp, etc.) +* which have status/result register side effects. +*/ +static bool is_standard_store_insn (const char *name) +{ +return !strncmp (name,"str" ,3 ) ||/* str, strb, strh */ + !strncmp (name,"stur" ,4 ) ||/* stur, sturb, sturh */ + !strncmp (name,"stlr" ,4 ) ||/* stlr, stlrb, stlrh */ + !strncmp (name,"stp" ,3 );/* stp */ +} +static void update_insn_state_arm64 (struct type_state *state,struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl)
@@ -730,6 +744,17 @@ static void update_insn_state_arm64(struct type_state *state,/* Memory to register transfers */ if (is_standard_load_insn (dl->ins.name))update_load_insn_state (state, dloc, dl, src, dst); +/* Register to memory transfers */ +else if (is_standard_store_insn (dl->ins.name)) { +/* +* Ignore transfers since it'd set a value in a struct +* and won't change the type. +* +* Needs to update the pre-index and post-index addressing +* modes for the destination register. +*/ +adjust_reg_index_state (state, dloc, dl, dst); + } }#endif
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 17/26] perf annotate-arm64: Support store instruction tracking
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
Extend
update_insn_state_arm64()to handle standard store instructions (str, stur, stlr, stp). Exclusive stores (e.g., stxr, stxp) are omitted as they exhibit different operand semantics.Unlike load instructions, a store instruction sets a value in the struct within the memory where the destination register resides, and does not alter its type. Therefore, no processing is required for the transfer.
Quoted 5 lines
The only point to note is that store instructions support pre-index and post-index addressing modes, so calling
adjust_reg_index_state()is still necessary to handle their addressing.Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
PATCH v5 18/26 perf annotate-data: Expand type_state_reg imm_value to u64
In reply to Tengda WuThe arm64 'adrp + add/ldr' instruction pair involves global variable address calculation and passing, which requires storing the 64-bit address in type_state_reg. Currently, there is no field in type_state_reg that can hold such an address. The most likely candidate for reuse is imm_value, but it is defined as u32, which limits the size of values it can carry.
To address this, expand imm_value from u32 to u64. Also update related compilation warnings and fix the sign-extension issue introduced by this change.
--- tools/perf/util/annotate-arch/annotate-x86.c | 4 ++-- tools/perf/util/annotate-data.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/annotate-arch/annotate-x86.c +2 -2
@@ -536,11 +536,11 @@ static void update_insn_state_x86(struct type_state *state,if (src->imm) { tsr->kind =TSR_KIND_CONST ; - tsr->imm_value = src->offset; + tsr->imm_value = (s64 )src->offset; tsr->offset =0 ; tsr->ok = true; -pr_debug_dtp ("mov [%x] imm=%#x -> reg%d\n" , +pr_debug_dtp ("mov [%x] imm=%#" PRIx64" -> reg%d\n" , insn_offset, tsr->imm_value, dst->reg1);return ; }
tools/perf/util/annotate-data.h +1 -1
@@ -173,7 +173,7 @@ extern struct annotated_data_stat ann_data_stat;*/ struct type_state_reg {Dwarf_Die type; -u32 imm_value; +u64 imm_value;/* * The offset within the struct that the register points to. * A value of 0 means the register points to the beginning.
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 18/26] perf annotate-data: Expand type_state_reg imm_value to u64
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
The arm64 'adrp + add/ldr' instruction pair involves global variable address calculation and passing, which requires storing the 64-bit address in
type_state_reg. Currently, there is no field intype_state_regthat can hold such an address. The most likely candidate for reuse isimm_value, but it is defined as u32, which limits the size of values it can carry.To address this, expand
imm_valuefrom u32 to u64. Also update related compilation warnings and fix the sign-extension issue introduced by this change.Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
PATCH v5 19/26 perf annotate-data: Track imm_value for stack variables
In reply to Tengda WuCurrently, imm_value is tracked in registers but dropped when values are saved to or loaded from stack variables during type profiling. If a register of type TSR_KIND_CONST is stored to the stack, its value will be lost.
Add an imm_value field to struct type_state_stack and update set_stack_state() and findnew_stack_state() to accept and preserve the immediate value when updating stack state.
--- tools/perf/util/annotate-arch/annotate-x86.c | 8 ++++++-- tools/perf/util/annotate-data.c | 13 +++++++------ tools/perf/util/annotate-data.h | 5 +++-- 3 files changed, 16 insertions(+), 10 deletions(-)
tools/perf/util/annotate-arch/annotate-x86.c +6 -2
@@ -591,6 +591,7 @@ static void update_insn_state_x86(struct type_state *state, tsr->type = stack->type; tsr->kind = stack->kind; tsr->offset = stack->ptr_offset; + tsr->imm_value = stack->imm_value; tsr->ok = true; } else if (die_get_member_type(&stack->type, offset - stack->offset,
@@ -598,6 +599,7 @@ static void update_insn_state_x86(struct type_state *state, tsr->type = type_die; tsr->kind =TSR_KIND_TYPE ; tsr->offset =0 ; + tsr->imm_value =0 ; tsr->ok = true; }else {invalidate_reg_state (tsr);
@@ -773,10 +775,12 @@ static void update_insn_state_x86(struct type_state *state,*/ if (!stack->compound)set_stack_state (stack, offset, tsr->kind, - &tsr->type, tsr->offset); + &tsr->type, tsr->offset, + tsr->imm_value); }else {findnew_stack_state (state, offset, tsr->kind, - &tsr->type, tsr->offset); + &tsr->type, tsr->offset, + tsr->imm_value); }if (dst->reg1 == fbreg) {
tools/perf/util/annotate-data.c +7 -6
@@ -590,7 +590,7 @@ struct type_state_stack *find_stack_state(struct type_state *state, }void set_stack_state (struct type_state_stack *stack,int offset,u8 kind, -Dwarf_Die *type_die,int ptr_offset) +Dwarf_Die *type_die,int ptr_offset,u64 imm_value) {int tag;Dwarf_Word size;
@@ -607,8 +607,9 @@ void set_stack_state(struct type_state_stack *stack, int offset, u8 kind, stack->offset = offset; stack->ptr_offset = ptr_offset; stack->kind = kind; + stack->imm_value = imm_value; -if (kind ==TSR_KIND_POINTER ) { +if (kind ==TSR_KIND_POINTER || kind ==TSR_KIND_CONST ) { stack->compound = false;return ; }
@@ -629,18 +630,18 @@ void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,struct type_state_stack *findnew_stack_state (struct type_state *state,int offset,u8 kind,Dwarf_Die *type_die, -int ptr_offset) +int ptr_offset,u64 imm_value) {struct type_state_stack *stack =find_stack_state (state, offset);if (stack) { -set_stack_state (stack, offset, kind, type_die, ptr_offset); +set_stack_state (stack, offset, kind, type_die, ptr_offset, imm_value);return stack; } stack =malloc (sizeof (*stack));if (stack) { -set_stack_state (stack, offset, kind, type_die, ptr_offset); +set_stack_state (stack, offset, kind, type_die, ptr_offset, imm_value);list_add (&stack->list, &state->stack_vars); }return stack;
@@ -935,7 +936,7 @@ static void update_var_state(struct type_state *state, struct data_loc_info *dlocontinue ;findnew_stack_state (state, offset,TSR_KIND_TYPE , - &mem_die,/*ptr_offset=*/ 0 ); + &mem_die,/*ptr_offset=*/ 0 ,/*imm_value=*/ 0 );if (var->reg == state->stack_reg) { pr_debug_dtp("var [%"PRIx64"] %#x(reg%d)",
tools/perf/util/annotate-data.h +3 -2
@@ -199,6 +199,7 @@ struct type_state_stack {int size;bool compound;u8 kind; +u64 imm_value; };/*
@@ -253,9 +254,9 @@ bool has_reg_type(struct type_state *state, int reg);struct type_state_stack *findnew_stack_state (struct type_state *state,int offset,u8 kind,Dwarf_Die *type_die, -int ptr_offset); +int ptr_offset,u64 imm_value);void set_stack_state (struct type_state_stack *stack,int offset,u8 kind, -Dwarf_Die *type_die,int ptr_offset); +Dwarf_Die *type_die,int ptr_offset,u64 imm_value);struct type_state_stack *find_stack_state (struct type_state *state,int offset);void invalidate_reg_state (struct type_state_reg *reg);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 19/26] perf annotate-data: Track imm_value for stack variables
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
Currently,
imm_valueis tracked in registers but dropped when values are saved to or loaded from stack variables during type profiling. If a register of typeTSR_KIND_CONSTis stored to the stack, its value will be lost.Add an
imm_valuefield to structtype_state_stackand updateset_stack_state()andfindnew_stack_state()to accept and preserve the immediate value when updating stack state.Reviewed-by Shuai Xue <[email protected]>Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
PATCH v5 20/26 perf annotate-x86: Delete stale stack state on store of untracked register
In reply to Tengda WuWhen a register without a tracked type is stored to a stack slot that previously held a valid type, the code currently performs an early return. This leaves the old type information intact in the stack state.
As a result, a stale type bug occurs:
1. A tracked register is stored into a stack slot.
2. The register becomes untracked/invalid (e.g., across a function call).
3. The now-untracked register is stored into the same stack slot.
4. Subsequent loads from that slot erroneously pick up the old stale type because the stack entry was never invalidated.
Fix this by introducing delete_stack_state() to search and free a stack entry for a given offset. When storing an untracked or invalid source register into a stack location, call delete_stack_state() to delete the stale entry before returning.
--- tools/perf/util/annotate-arch/annotate-x86.c | 17 +++++++++++++---- tools/perf/util/annotate-data.c | 10 ++++++++++ tools/perf/util/annotate-data.h | 1 + 3 files changed, 24 insertions(+), 4 deletions(-)
tools/perf/util/annotate-arch/annotate-x86.c +13 -4
@@ -753,15 +753,24 @@ static void update_insn_state_x86(struct type_state *state, }/* Case 3. register to memory transfers */ if (!src->mem_ref && dst->mem_ref) { -if (!has_reg_type (state, src->reg1) || - !state->regs[src->reg1].ok) -return ; -/* Check stack variables with offset */ if (dst->reg1 == fbreg || dst->reg1 == state->stack_reg) {struct type_state_stack *stack;int offset = dst->offset - fboff; +if (!has_reg_type (state, src->reg1) || + !state->regs[src->reg1].ok) { + stack =find_stack_state (state, offset); +/* +* Preserve compound states when only a member +* is overwritten. +*/ +if (stack && !stack->compound) +delete_stack_state (state, offset); + +return ; + } + tsr = &state->regs[src->reg1]; stack =find_stack_state (state, offset);
tools/perf/util/annotate-data.c +10 -0
@@ -647,6 +647,16 @@ struct type_state_stack *findnew_stack_state(struct type_state *state,return stack; } +void delete_stack_state (struct type_state *state,int offset) +{ +struct type_state_stack *stack =find_stack_state (state, offset); + +if (stack) { +list_del (&stack->list); +free (stack); + } +} +void invalidate_reg_state (struct type_state_reg *reg) { reg->kind =TSR_KIND_INVALID ;
tools/perf/util/annotate-data.h +1 -0
@@ -259,6 +259,7 @@ void set_stack_state(struct type_state_stack *stack, int offset, u8 kind, Dwarf_Die *type_die, int ptr_offset, u64 imm_value);struct type_state_stack *find_stack_state (struct type_state *state,int offset); +void delete_stack_state (struct type_state *state,int offset);void invalidate_reg_state (struct type_state_reg *reg); bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc, u64 ip, u64 var_addr, int *var_offset,
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 20/26] perf annotate-x86: Delete stale stack state on store of untracked register
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
When a register without a tracked type is stored to a stack slot that previously held a valid type, the code currently performs an early return. This leaves the old type information intact in the stack state.
As a result, a stale type bug occurs:
1. A tracked register is stored into a stack slot.
2. The register becomes untracked/invalid (e.g., across a function call).
3. The now-untracked register is stored into the same stack slot.
4. Subsequent loads from that slot erroneously pick up the old stale type because the stack entry was never invalidated.
Quoted 6 lines
Fix this by introducing
delete_stack_state()to search and free a stack entry for a given offset. When storing an untracked or invalid source register into a stack location, calldelete_stack_state()to delete the stale entry before returning.Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
PATCH v5 21/26 perf annotate-arm64: Support stack variable tracking
In reply to Tengda WuExtend update_insn_state_arm64() to track data types stored on the stack.
The implementation handles:
1. Stack Loads: Identify when a register is loaded from a stack slot and update the register's type state based on the tracked stack content or compound member types.
2. Stack Stores: Update or create new stack state entries when a tracked register type is stored to the stack.
Similar to loads, stores also support saving one or two registers (in the case of 'stp'). Therefore, propagate_store_reg_state() is introduced.
With these changes, the instruction tracker can now follow data types as they move between registers and memory, specifically for function local variables and compiler-spilled values on arm64.
--- .../perf/util/annotate-arch/annotate-arm64.c | 159 ++++++++++++++++-- 1 file changed, 149 insertions(+), 10 deletions(-)
tools/perf/util/annotate-arch/annotate-arm64.c +149 -10
@@ -535,6 +535,8 @@ static int propagate_load_reg_state(struct type_state *state,u32 insn_offset = dl->al.offset;int sreg = src->reg1;int reg_offset; +int fbreg = dloc->fbreg; +int fboff =0 ;if (!has_reg_type (state, dreg))return -1 ;
@@ -542,12 +544,57 @@ static int propagate_load_reg_state(struct type_state *state, tsr = &state->regs[dreg]; tsr->copied_from =-1 ; +if (dloc->fb_cfa) { +u64 ip = dloc->ms->sym->start + dl->al.offset; +u64 pc =map__rip_2objdump (dloc->ms->map, ip); + +if (die_get_cfa (dloc->di->dbg, pc, &fbreg, &fboff) <0 ) + fbreg =-1 ; + } + retry:if (arch_get_reg_offset (dloc->arch, src, sreg, state, false, ®_offset))return -1 ; reg_offset += mem_offset; +/* Check stack variables with offset */ +if (sreg == fbreg || sreg == state->stack_reg) { +struct type_state_stack *stack; +int offset = sreg == fbreg ? reg_offset - fboff : reg_offset; + + stack =find_stack_state (state, offset); +if (stack ==NULL ) { +return -1 ; + }else if (!stack->compound) { + tsr->type = stack->type; + tsr->kind = stack->kind; + tsr->offset = stack->ptr_offset; + tsr->imm_value = stack->imm_value; + tsr->ok = true; + }else if (die_get_member_type (&stack->type, + offset - stack->offset, + &type_die)) { + tsr->type = type_die; + tsr->kind =TSR_KIND_TYPE ; + tsr->offset =0 ; + tsr->imm_value =0 ; + tsr->ok = true; + }else { +return -1 ; + } + +if (sreg == fbreg) { +pr_debug_dtp ("%s [%x] -%#x(stack) -> reg%d" , + dl->ins.name, insn_offset, -offset, dreg); + }else { +pr_debug_dtp ("%s [%x] %#x(reg%d) -> reg%d" , + dl->ins.name, insn_offset, offset, sreg, dreg); + } +pr_debug_type_name (&tsr->type, tsr->kind); +return 0 ; + } +if (!src_tsr || !src_tsr->ok)return -1 ;
@@ -663,6 +710,106 @@ static bool is_standard_store_insn(const char *name) !strncmp (name,"stp" ,3 );/* stp */ } +/* +* For store insns: propagate type from @sreg to the memory location referenced +* by @dst, applying @mem_offset to account for multi-regs stores (e.g. stp). +*/ +static void propagate_store_reg_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl,int sreg, +struct annotated_op_loc *dst, +int mem_offset) +{ +struct type_state_reg *tsr; +u32 insn_offset = dl->al.offset; +int dreg = dst->reg1; +int reg_offset; +int fbreg = dloc->fbreg; +int fboff =0 ; + +if (arch_get_reg_offset (dloc->arch, dst, dreg, state, false, ®_offset)) +return ; + + reg_offset += mem_offset; + +if (dloc->fb_cfa) { +u64 ip = dloc->ms->sym->start + dl->al.offset; +u64 pc =map__rip_2objdump (dloc->ms->map, ip); + +if (die_get_cfa (dloc->di->dbg, pc, &fbreg, &fboff) <0 ) + fbreg =-1 ; + } + +/* Check stack variables with offset */ +if (dreg == fbreg || dreg == state->stack_reg) { +struct type_state_stack *stack; +int offset = dreg == fbreg ? reg_offset - fboff : reg_offset; + +if (!has_reg_type (state, sreg) || !state->regs[sreg].ok) { + stack =find_stack_state (state, offset); +/* Preserve compound states when only a member is overwritten. */ +if (stack && !stack->compound) +delete_stack_state (state, offset); + +return ; + } + + tsr = &state->regs[sreg]; + + stack =find_stack_state (state, offset); +if (stack) { +if (!stack->compound) +set_stack_state (stack, offset, tsr->kind, &tsr->type, + tsr->offset, tsr->imm_value); +/* +* If it's a compound type, it means attempting to +* write to a member value of the compound type without +* changing the compound type itself, so do nothing. +*/ + }else { +findnew_stack_state (state, offset, tsr->kind, &tsr->type, + tsr->offset, tsr->imm_value); + } + +if (dreg == fbreg) { +pr_debug_dtp ("%s [%x] reg%d -> -%#x(stack)" , + dl->ins.name, insn_offset, sreg, -offset); + }else { +pr_debug_dtp ("%s [%x] reg%d -> %#x(reg%d)" , + dl->ins.name, insn_offset, sreg, offset, dreg); + } +if (tsr->offset !=0 ) +pr_debug_dtp (" reg%d offset %#x ->" , sreg, tsr->offset); + +pr_debug_type_name (&tsr->type, tsr->kind); + } +/* +* Ignore other transfers since it'd set a value in a struct +* and won't change the type. +*/ +} + +static void update_store_insn_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl, +struct annotated_op_loc *src, +struct annotated_op_loc *dst) +{ +/* Handle the first source register */ +propagate_store_reg_state (state, dloc, dl, src->reg1, dst,/*mem_offset=*/ 0 ); + +/* Handle the second source register (if any) */ +if (src->multi_regs) { +int mem_offset =arm64__reg_size (dl->ops.source.raw); + +if (mem_offset >=0 ) +propagate_store_reg_state (state, dloc, dl, src->reg2, + dst, mem_offset); + } + +adjust_reg_index_state (state, dloc, dl, dst); +} +static void update_insn_state_arm64 (struct type_state *state,struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl)
@@ -745,16 +892,8 @@ static void update_insn_state_arm64(struct type_state *state,if (is_standard_load_insn (dl->ins.name))update_load_insn_state (state, dloc, dl, src, dst);/* Register to memory transfers */ -else if (is_standard_store_insn (dl->ins.name)) { -/* -* Ignore transfers since it'd set a value in a struct -* and won't change the type. -* -* Needs to update the pre-index and post-index addressing -* modes for the destination register. -*/ -adjust_reg_index_state (state, dloc, dl, dst); - } +else if (is_standard_store_insn (dl->ins.name)) + update_store_insn_state(state, dloc, dl, src, dst); }#endif
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 21/26] perf annotate-arm64: Support stack variable tracking
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Invalid register comparison causes unparsed operands to be falsely treated as stack variables. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Invalid register comparison causes unparsed operands to be falsely treated as stack variables. --
commit 7ef0e34126bb69c05756eba7378c5775c6ea2fd3
Author: Tengda Wu <[email protected]>
perf annotate-arm64: Support stack variable tracking
This commit extends update_insn_state_arm64 to track data types stored on the stack. It handles stack loads and stores, introducing propagate_store_reg_state to follow data types between registers and memory.
Quoted 5 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0@@ -542,12 +544,57 @@ static int propagate_load_reg_state(struct type_state *state,
[ ... ]
Quoted 6 lines
tools/perf/util/annotate-arch/annotate-arm64.c +6
-0+if (dloc->fb_cfa) { +u64 ip = dloc->ms->sym->start + dl->al.offset; +u64 pc =map__rip_2objdump (dloc->ms->map, ip); + +if (die_get_cfa (dloc->di->dbg, pc, &fbreg, &fboff) <0 ) + fbreg =-1 ;
tools/perf/util/annotate-arch/annotate-arm64.c static int propagate_load_reg_state(struct type_state *state, · line 562
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +4
-0+ } + retry:if (arch_get_reg_offset (dloc->arch, src, sreg, state, false, ®_offset))return -1 ; reg_offset += mem_offset; +/* Check stack variables with offset */ +if (sreg == fbreg || sreg == state->stack_reg) {
[Severity: Medium]
When an unparsed or unsupported base register leaves sreg as -1, and fbreg is also -1, does this condition evaluate to true? Could this cause unparsed source operands to be incorrectly treated as stack loads?
[ ... ]
Quoted 6 lines
tools/perf/util/annotate-arch/annotate-arm64.c +6
-0+static void propagate_store_reg_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl,int sreg, +struct annotated_op_loc *dst, +int mem_offset) +{
[ ... ]
tools/perf/util/annotate-arch/annotate-arm64.c static bool is_standard_store_insn(const char *name) · line 744
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+if (dloc->fb_cfa) { +u64 ip = dloc->ms->sym->start + dl->al.offset; +u64 pc =map__rip_2objdump (dloc->ms->map, ip); + +if (die_get_cfa (dloc->di->dbg, pc, &fbreg, &fboff) <0 ) + fbreg =-1 ; + } + +/* Check stack variables with offset */ +if (dreg == fbreg || dreg == state->stack_reg) {
[Severity: Medium]
Similarly to the load case, if the base register extraction leaves dreg as -1 and fbreg is also -1, does this check evaluate to true? Will this erroneously treat unparsed target operands as stack stores, potentially corrupting the stack type state tracking?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
Re: [PATCH v5 21/26] perf annotate-arm64: Support stack variable tracking
Quoted 35 lines · On 2026/9/8 21:36, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Invalid register comparison causes unparsed operands to be falsely treated as stack variables. --commit
7ef0e34126bb69c05756eba7378c5775c6ea2fd3
Author: Tengda Wu <[email protected]>perf annotate-arm64: Support stack variable tracking
This commit extends
update_insn_state_arm64to track data types stored on the stack. It handles stack loads and stores, introducingpropagate_store_reg_stateto follow data types between registers and memory.Quoted 5 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0@@ -542,12 +544,57 @@ static int propagate_load_reg_state(struct type_state *state,[ ... ]
Quoted 14 lines
tools/perf/util/annotate-arch/annotate-arm64.c +8
-0+if (dloc->fb_cfa) { +u64 ip = dloc->ms->sym->start + dl->al.offset; +u64 pc =map__rip_2objdump (dloc->ms->map, ip); + +if (die_get_cfa (dloc->di->dbg, pc, &fbreg, &fboff) <0 ) + fbreg =-1 ; + } + retry:if (arch_get_reg_offset (dloc->arch, src, sreg, state, false, ®_offset))return -1 ; reg_offset += mem_offset;
Quoted 8 lines
tools/perf/util/annotate-arch/annotate-arm64.c static int propagate_load_reg_state(struct type_state *state, · line 562
Quoted 2 lines
tools/perf/util/annotate-arch/annotate-arm64.c +2
-0+/* Check stack variables with offset */ +if (sreg == fbreg || sreg == state->stack_reg) {[Severity: Medium]
When an unparsed or unsupported base register leaves sreg as -1, and fbreg is also -1, does this condition evaluate to true? Could this cause unparsed source operands to be incorrectly treated as stack loads?
Agreed.
Quoted 11 lines
[ ... ]
Quoted 6 lines
tools/perf/util/annotate-arch/annotate-arm64.c +6
-0+static void propagate_store_reg_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl,int sreg, +struct annotated_op_loc *dst, +int mem_offset) +{[ ... ]
Quoted 17 lines
tools/perf/util/annotate-arch/annotate-arm64.c static bool is_standard_store_insn(const char *name) · line 744
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+if (dloc->fb_cfa) { +u64 ip = dloc->ms->sym->start + dl->al.offset; +u64 pc =map__rip_2objdump (dloc->ms->map, ip); + +if (die_get_cfa (dloc->di->dbg, pc, &fbreg, &fboff) <0 ) + fbreg =-1 ; + } + +/* Check stack variables with offset */ +if (dreg == fbreg || dreg == state->stack_reg) {[Severity: Medium]
Similarly to the load case, if the base register extraction leaves dreg as -1 and fbreg is also -1, does this check evaluate to true? Will this erroneously treat unparsed target operands as stack stores, potentially corrupting the stack type state tracking?
Agreed. It would be best to add a guard for when dreg is not -1.
Tengda
PATCH v5 22/26 perf annotate-arm64: Support 'mov' instruction tracking
In reply to Tengda WuExtend update_insn_state_arm64() to support register-to-register and immediate-to-register 'mov' instructions.
For register-to-register 'mov' (e.g., mov dreg, sreg), propagate data type information from the source register to the destination register.
For immediate-to-register 'mov' (e.g., mov dreg, #imm), store the immediate value in the destination register's imm_value field and set its kind to TSR_KIND_CONST, allowing subsequent instructions to resolve it as a constant.
A real-world example is shown below:
ffff8000803eebf8 <get_vma_policy>:
ffff8000803eec20: mov x21, x0 // x0 (struct vm_area_struct*) -> x21 ffff8000803eec28: ldr x2, [x0, #112] ffff8000803eec2c: cbz x2, ffff8000803eec94 <get_vma_policy+0x9c> * ffff8000803eec94: ldr x0, [x21, #152]
Before this commit, the type of x21 was unknown, causing the subsequent inference to fail:
var [0] reg0 offset 0 type='struct vm_area_struct*' size=0x8 chk [9c] reg21 offset=0x98 ok=0 kind=0 cfa : no type information final result: no type information
After this commit, the type of x21 is correctly inferred as 'vm_area_struct':
var [0] reg0 offset 0 type='struct vm_area_struct*' size=0x8 mov [28] reg0 -> reg21 type='struct vm_area_struct*' size=0x8 chk [9c] reg21 offset=0x98 ok=1 kind=1 (struct vm_area_struct*) : Good! found by insn track: 0x98(reg21) type-offset=0x98
final result: type='struct vm_area_struct' size=0xb0
--- .../perf/util/annotate-arch/annotate-arm64.c | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-)
tools/perf/util/annotate-arch/annotate-arm64.c +59 -1
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/compiler.h> #include <errno.h> +#include <inttypes.h> #include <stdlib.h> #include <string.h> #include <linux/ctype.h>
@@ -605,6 +606,7 @@ static int propagate_load_reg_state(struct type_state *state, tsr->type = type_die; tsr->kind =TSR_KIND_TYPE ; tsr->offset =0 ; + tsr->imm_value =0 ; tsr->ok = true;if (src->multi_regs) {
@@ -810,6 +812,58 @@ static void update_store_insn_state(struct type_state *state,adjust_reg_index_state (state, dloc, dl, dst); } +static void update_mov_insn_state (struct type_state *state, +struct disasm_line *dl, +struct annotated_op_loc *src, +struct annotated_op_loc *dst) +{ +struct type_state_reg *tsr; +u32 insn_offset = dl->al.offset; +int sreg = src->reg1; +int dreg = dst->reg1; + +if (!has_reg_type (state, dreg)) +return ; + + tsr = &state->regs[dreg]; + tsr->copied_from =-1 ; + +if (src->imm) { +invalidate_reg_state (tsr); + tsr->kind =TSR_KIND_CONST ; + tsr->imm_value = (s64 )src->offset; + tsr->offset =0 ; + tsr->ok = true; + +pr_debug_dtp ("mov [%x] imm=%#" PRIx64" -> reg%d\n" , + insn_offset, tsr->imm_value, dreg); +return ; + } + +if (!has_reg_type (state, sreg) || !state->regs[sreg].ok) { +invalidate_reg_state (tsr); +return ; + } + + tsr->type = state->regs[sreg].type; + tsr->kind = state->regs[sreg].kind; + tsr->imm_value = state->regs[sreg].imm_value; + tsr->offset = state->regs[sreg].offset; + tsr->ok = state->regs[sreg].ok; +/* +* lifetime_active and lifetime_end represent DWARF variable lifetime +* ranges tied to the source register. Copying them risks incorrectly +* invalidating the destination register after a call instruction, +* so intentionally omit them. +*/ + +if (tsr->kind ==TSR_KIND_TYPE || tsr->kind ==TSR_KIND_POINTER ) + tsr->copied_from = sreg; + +pr_debug_dtp ("mov [%x] reg%d -> reg%d" , insn_offset, sreg, dreg); +pr_debug_type_name (&tsr->type, tsr->kind); +} +static void update_insn_state_arm64 (struct type_state *state,struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl)
@@ -876,6 +930,7 @@ static void update_insn_state_arm64(struct type_state *state,* prevent stale type info from propagating to subsequent instructions. */ if (has_reg_type (state, dst->reg1) && !dst->mem_ref && +strcmp (dl->ins.name,"mov" ) && !is_standard_load_insn (dl->ins.name)) {pr_debug_dtp ("%s [%x] invalidate reg%d" , dl->ins.name, insn_offset, dst->reg1);
@@ -888,8 +943,11 @@ static void update_insn_state_arm64(struct type_state *state,return ; } +/* Register to register or imm value to register transfers */ +if (!strcmp (dl->ins.name,"mov" )) +update_mov_insn_state (state, dl, src, dst);/* Memory to register transfers */ -if (is_standard_load_insn (dl->ins.name)) +else if (is_standard_load_insn (dl->ins.name))update_load_insn_state (state, dloc, dl, src, dst);/* Register to memory transfers */ else if (is_standard_store_insn (dl->ins.name))
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 22/26] perf annotate-arm64: Support 'mov' instruction tracking
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Destination register's DWARF lifetime state is improperly preserved during register-to-register…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Destination register's DWARF lifetime state is improperly preserved during register-to-register 'mov' instruction tracking. --
commit e7b1d4e72b07c94aacdc6e7221ef41f56b7b017c
Author: Tengda Wu <[email protected]>
perf annotate-arm64: Support 'mov' instruction tracking
Extend update_insn_state_arm64() to support register-to-register and immediate-to-register 'mov' instructions. For register-to-register 'mov', it propagates data type information from the source to the destination.
Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0
[ ... ]
Quoted 19 lines
tools/perf/util/annotate-arch/annotate-arm64.c +15
-0@@ -810,6 +812,58 @@ static void update_store_insn_state(struct type_state *state,adjust_reg_index_state (state, dloc, dl, dst); } +static void update_mov_insn_state (struct type_state *state, +struct disasm_line *dl, +struct annotated_op_loc *src, +struct annotated_op_loc *dst) +{ +struct type_state_reg *tsr; +u32 insn_offset = dl->al.offset; +int sreg = src->reg1; +int dreg = dst->reg1; + +if (!has_reg_type (state, dreg)) +return ; + + tsr = &state->regs[dreg]; + tsr->copied_from =-1 ;
[ ... ]
tools/perf/util/annotate-arch/annotate-arm64.c static void update_store_insn_state(struct type_state *state, · line 858
Quoted 11 lines
tools/perf/util/annotate-arch/annotate-arm64.c +11
-0+ tsr->type = state->regs[sreg].type; + tsr->kind = state->regs[sreg].kind; + tsr->imm_value = state->regs[sreg].imm_value; + tsr->offset = state->regs[sreg].offset; + tsr->ok = state->regs[sreg].ok; +/* +* lifetime_active and lifetime_end represent DWARF variable lifetime +* ranges tied to the source register. Copying them risks incorrectly +* invalidating the destination register after a call instruction, +* so intentionally omit them. +*/
[Severity: Medium]
While avoiding copying the source's lifetime, does this leave the destination register's existing lifetime values stale?
If the destination register (dreg) previously had an active DWARF lifetime tracking state (lifetime_active = true), those fields are not cleared here since invalidate_reg_state() isn't called for this valid register transfer path.
Should lifetime_active be set to false and lifetime_end set to 0?
Otherwise, the destination register might falsely survive caller-saved register invalidations during subsequent call instructions and corrupt downstream type inference.
Quoted 7 lines
tools/perf/util/annotate-arch/annotate-arm64.c +7
-0+ +if (tsr->kind ==TSR_KIND_TYPE || tsr->kind ==TSR_KIND_POINTER ) + tsr->copied_from = sreg; + +pr_debug_dtp ("mov [%x] reg%d -> reg%d" , insn_offset, sreg, dreg); +pr_debug_type_name (&tsr->type, tsr->kind); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
Re: [PATCH v5 22/26] perf annotate-arm64: Support 'mov' instruction tracking
Quoted 63 lines · On 2026/9/8 21:27, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Destination register's DWARF lifetime state is improperly preserved during register-to-register 'mov' instruction tracking. --commit
e7b1d4e72b07c94aacdc6e7221ef41f56b7b017c
Author: Tengda Wu <[email protected]>perf annotate-arm64: Support 'mov' instruction tracking
Extend
update_insn_state_arm64()to support register-to-register and immediate-to-register 'mov' instructions. For register-to-register 'mov', it propagates data type information from the source to the destination.Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0[ ... ]
Quoted 19 lines
tools/perf/util/annotate-arch/annotate-arm64.c +15
-0@@ -810,6 +812,58 @@ static void update_store_insn_state(struct type_state *state,adjust_reg_index_state (state, dloc, dl, dst); } +static void update_mov_insn_state (struct type_state *state, +struct disasm_line *dl, +struct annotated_op_loc *src, +struct annotated_op_loc *dst) +{ +struct type_state_reg *tsr; +u32 insn_offset = dl->al.offset; +int sreg = src->reg1; +int dreg = dst->reg1; + +if (!has_reg_type (state, dreg)) +return ; + + tsr = &state->regs[dreg]; + tsr->copied_from =-1 ;[ ... ]
tools/perf/util/annotate-arch/annotate-arm64.c static void update_store_insn_state(struct type_state *state, · line 858
Quoted 11 lines
tools/perf/util/annotate-arch/annotate-arm64.c +11
-0+ tsr->type = state->regs[sreg].type; + tsr->kind = state->regs[sreg].kind; + tsr->imm_value = state->regs[sreg].imm_value; + tsr->offset = state->regs[sreg].offset; + tsr->ok = state->regs[sreg].ok; +/* +* lifetime_active and lifetime_end represent DWARF variable lifetime +* ranges tied to the source register. Copying them risks incorrectly +* invalidating the destination register after a call instruction, +* so intentionally omit them. +*/ [Severity: Medium]
While avoiding copying the source's lifetime, does this leave the destination register's existing lifetime values stale?If the destination register (dreg) previously had an active DWARF lifetime tracking state (
lifetime_active= true), those fields are not cleared here sinceinvalidate_reg_state()isn't called for this valid register transfer path.
Quoted 6 lines
Should
lifetime_activebe set to false andlifetime_endset to 0?Otherwise, the destination register might falsely survive caller-saved register invalidations during subsequent call instructions and corrupt downstream type inference.
It seems that the scenario I was originally worried about in my comment, where dreg is incorrectly invalidated, does not exist.
Consider the following three consecutive instructions:
mov dreg, x0
call xxxx // x0 lifetime end
ldr x1, [dreg, #16]
1. If dreg is a callee-saved register, such as x20, then it is not affected by the call instruction, and setting its lifetime to any value will not matter.
2. If dreg is a caller-saved register, such as x1, then it is affected by the call instruction, and its lifetime needs to be cleared.
In practice, case 2 is very rare and looks more like undefined behavior, so only case 1 needs to be considered. In that case, staying consistent with x86 may be the better choice:
tsr->lifetime_active = state->regs[src->reg1].lifetime_active; tsr->lifetime_end = state->regs[src->reg1].lifetime_end;
Given the above, I think we can safely copy the lifetime fields and drop the original concern.
Thanks,
Tengda
PATCH v5 23/26 perf annotate-arm64: Support 'add' instruction tracking
In reply to Tengda WuExtend update_insn_state_arm64() to track 'add' instructions for structure member address calculation.
Unlike x86, the arm64 'add' instruction has an extra base register among its source operands. Therefore, in terms of propagating the data type, it is essentially performing a 'mov', except that before the 'mov', it first needs to be updated by adding the offset or reg2.
A real-world example is shown below:
ffff80008001c9a8 <flush_ptrace_hw_breakpoint>:
ffff80008001c9c4: add x19, x0, #0xeb8 // x0 (task_struct*) + 0xeb8 -> x19 * ffff80008001c9d0: ldr x0, [x19]
Before this commit, the type flow broke at the 'add' instruction, leaving the subsequent load with no type information:
chk [28] reg19 offset=0 ok=0 kind=0 cfa : no type information final result: no type information
After this commit, the tracker correctly follows the member address calculation:
var [0] reg0 offset 0 type='struct task_struct*'
add [1c] address of 0xeb8(reg0) -> reg19 type='struct task_struct*' chk [28] reg19 offset=0 ok=1 kind=1 (struct task_struct*) : Good! found by insn track: 0(reg19) type-offset=0xeb8
final result: type='struct task_struct'
--- .../perf/util/annotate-arch/annotate-arm64.c | 72 ++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-)
tools/perf/util/annotate-arch/annotate-arm64.c +70 -2
@@ -864,6 +864,72 @@ static void update_mov_insn_state(struct type_state *state,pr_debug_type_name (&tsr->type, tsr->kind); } +static void update_add_insn_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl, +struct annotated_op_loc *src, +struct annotated_op_loc *dst) +{ +struct type_state_reg *tsr; +struct type_state_reg src_tsr; +u32 insn_offset = dl->al.offset; +int sreg = src->reg1; +int dreg = dst->reg1; +int reg_offset; + +if (!has_reg_type (state, dreg)) +return ; + + tsr = &state->regs[dreg]; + tsr->copied_from =-1 ; + +retry: +if (!has_reg_type (state, sreg) || !state->regs[sreg].ok) { +invalidate_reg_state (tsr); +return ; + } + + src_tsr = state->regs[sreg]; + +if (arch_get_reg_offset (dloc->arch, src, sreg, state, false, ®_offset)) + goto try_other_reg; + +if (src_tsr.kind ==TSR_KIND_CONST ) { + tsr->kind = src_tsr.kind; + tsr->imm_value = src_tsr.imm_value + reg_offset; + tsr->offset =0 ; + tsr->ok = src_tsr.ok; + +pr_debug_dtp ("add [%x] imm %#x(reg%d) -> reg%d\n" , + insn_offset, reg_offset, sreg, dreg); +return ; + } + +if (src_tsr.kind ==TSR_KIND_POINTER || + (src_tsr.kind ==TSR_KIND_TYPE && +dwarf_tag (&src_tsr.type) == DW_TAG_pointer_type)) { + tsr->type = src_tsr.type; + tsr->kind = src_tsr.kind; + tsr->imm_value = src_tsr.imm_value; + tsr->offset = src_tsr.offset + reg_offset; + tsr->ok = src_tsr.ok; + +pr_debug_dtp ("add [%x] address of %#x(reg%d) -> reg%d" , + insn_offset, reg_offset, sreg, dreg); +pr_debug_type_name (&tsr->type, tsr->kind); +return ; + } + +try_other_reg: +/* Try another register if any */ +if (src->multi_regs && src->reg1 != src->reg2 && sreg != src->reg2 && + !(src->extend_type || src->shift_type)) { + sreg = src->reg2; + goto retry; + } +invalidate_reg_state (tsr); +} +static void update_insn_state_arm64 (struct type_state *state,struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl)
@@ -930,7 +996,7 @@ static void update_insn_state_arm64(struct type_state *state,* prevent stale type info from propagating to subsequent instructions. */ if (has_reg_type (state, dst->reg1) && !dst->mem_ref && -strcmp (dl->ins.name,"mov" ) && +strcmp (dl->ins.name,"add" ) &&strcmp (dl->ins.name,"mov" ) && !is_standard_load_insn (dl->ins.name)) {pr_debug_dtp ("%s [%x] invalidate reg%d" , dl->ins.name, insn_offset, dst->reg1);
@@ -943,8 +1009,10 @@ static void update_insn_state_arm64(struct type_state *state,return ; } +if (!strcmp (dl->ins.name,"add" )) +update_add_insn_state (state, dloc, dl, src, dst);/* Register to register or imm value to register transfers */ -if (!strcmp (dl->ins.name,"mov" )) +else if (!strcmp (dl->ins.name,"mov" ))update_mov_insn_state (state, dl, src, dst);/* Memory to register transfers */ else if (is_standard_load_insn (dl->ins.name))
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 23/26] perf annotate-arm64: Support 'add' instruction tracking
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Failure to clear `lifetime_active` and `lifetime_end` in `update_add_insn_state` for the…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Failure to clear lifetime_active and lifetime_end in update_add_insn_state for the destination register. --
commit 1ca68e83139c25b5687ab59598c804f32ea92afb
Author: Tengda Wu <[email protected]>
perf annotate-arm64: Support 'add' instruction tracking
This commit extends update_insn_state_arm64() to track 'add' instructions for structure member address calculation. It enables the data type tracker to follow member address calculations and propagate types correctly instead of dropping type info on 'add'.
Quoted 16 lines
tools/perf/util/annotate-arch/annotate-arm64.c +8
-0@@ -864,6 +864,72 @@ static void update_mov_insn_state(struct type_state *state,pr_debug_type_name (&tsr->type, tsr->kind); } +static void update_add_insn_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl, +struct annotated_op_loc *src, +struct annotated_op_loc *dst) +{ +struct type_state_reg *tsr; +struct type_state_reg src_tsr;
tools/perf/util/annotate-arch/annotate-arm64.c static void update_mov_insn_state(struct type_state *state, · line 884
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+u32 insn_offset = dl->al.offset; +int sreg = src->reg1; +int dreg = dst->reg1; +int reg_offset; + +if (!has_reg_type (state, dreg)) +return ; + + tsr = &state->regs[dreg]; + tsr->copied_from =-1 ;
[Severity: Medium]
Does this code need to clear lifetime_active and lifetime_end on the destination register?
When the add instruction overwrites a destination register that previously tracked a variable with an active DWARF lifetime, it looks like the stale DWARF lifetime bounds are incorrectly retained on the destination register. Could this cause caller-saved registers to incorrectly survive function call invalidation and corrupt data type tracking during profiling?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
Re: [PATCH v5 23/26] perf annotate-arm64: Support 'add' instruction tracking
Quoted 45 lines · On 2026/9/8 21:27, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Failure to clearlifetime_activeandlifetime_endinupdate_add_insn_statefor the destination register. --commit
1ca68e83139c25b5687ab59598c804f32ea92afb
Author: Tengda Wu <[email protected]>perf annotate-arm64: Support 'add' instruction tracking
This commit extends
update_insn_state_arm64()to track 'add' instructions for structure member address calculation. It enables the data type tracker to follow member address calculations and propagate types correctly instead of dropping type info on 'add'.Quoted 16 lines
tools/perf/util/annotate-arch/annotate-arm64.c +8
-0@@ -864,6 +864,72 @@ static void update_mov_insn_state(struct type_state *state,pr_debug_type_name (&tsr->type, tsr->kind); } +static void update_add_insn_state (struct type_state *state, +struct data_loc_info *dloc, +struct disasm_line *dl, +struct annotated_op_loc *src, +struct annotated_op_loc *dst) +{ +struct type_state_reg *tsr; +struct type_state_reg src_tsr;tools/perf/util/annotate-arch/annotate-arm64.c static void update_mov_insn_state(struct type_state *state, · line 884
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+u32 insn_offset = dl->al.offset; +int sreg = src->reg1; +int dreg = dst->reg1; +int reg_offset; + +if (!has_reg_type (state, dreg)) +return ; + + tsr = &state->regs[dreg]; + tsr->copied_from =-1 ;[Severity: Medium]
Does this code need to clearlifetime_activeandlifetime_endon the destination register?
Quoted 8 lines
When the add instruction overwrites a destination register that previously tracked a variable with an active DWARF lifetime, it looks like the stale DWARF lifetime bounds are incorrectly retained on the destination register. Could this cause caller-saved registers to incorrectly survive function call invalidation and corrupt data type tracking during profiling?
[ ... ]
Agreed.
Thanks,
Tengda
PATCH v5 24/26 perf annotate-arm64: Support 'adrp' instruction to track global variables
In reply to Tengda WuExtend update_insn_state_arm64() to track global variable types calculated via page-relative addressing.
On arm64, global variables are typically accessed by first calculating the page address using 'adrp', followed by an 'add' or 'ldr' to get the specific symbol address. Without tracking 'adrp', the instruction tracker loses the base address, making it impossible to resolve global symbols and their associated DWARF types.
Introduce TSR_KIND_GLOBAL_ADDR to represent a partial global address state. When encountering 'adrp', store the page-aligned target address in the register's type state. Upon a subsequent 'add' or 'ldr' instruction that references a TSR_KIND_GLOBAL_ADDR register, combine the page address with the immediate offset.
A real-world example is shown below:
ffff80008032e008 <folios_put_refs>:
ffff80008032e048: adrp x24, ffff80008202f000 <nr_cpu_ids> ffff80008032e050: add x24, x24, #0xd40 * ffff80008032e078: ldr x0, [x24]
Before this commit, x24 was unknown, leading to no type information:
chk [70] reg24 offset=0 ok=0 kind=0 cfa : no type information final result: no type information
After this commit, the tracker correctly follows the adrp/add flow:
adrp [40] global addr=0xffff80008202f000 -> reg24 add [48] global 0xd40(reg24) -> reg24
chk [70] reg24 offset=0 ok=1 kind=7 global addr : Good! final result: type='struct folio*'
--- .../perf/util/annotate-arch/annotate-arm64.c | 83 +++++++++++++++++-- tools/perf/util/annotate-data.c | 25 +++++- tools/perf/util/annotate-data.h | 1 + 3 files changed, 99 insertions(+), 10 deletions(-)
tools/perf/util/annotate-arch/annotate-arm64.c +74 -9
@@ -524,7 +524,7 @@ static bool is_standard_load_insn(const char *name)* loads (e.g. second target reg in 'ldp'), which is added to the reg offset. */ static int propagate_load_reg_state(struct type_state *state, - struct data_loc_info *dloc, + struct data_loc_info *dloc, Dwarf_Die *cu_die, struct disasm_line *dl, int dreg, struct annotated_op_loc *src, struct type_state_reg **src_states,
@@ -622,6 +622,33 @@ static int propagate_load_reg_state(struct type_state *state,return 0 ; } +/* Or check if it's a global variable */ +if (src_tsr->kind ==TSR_KIND_GLOBAL_ADDR ) { +u64 ip = dloc->ms->sym->start + dl->al.offset; +u64 addr = src_tsr->imm_value + reg_offset; +int offset; + +if (!get_global_var_type (cu_die, dloc, ip, addr, &offset, &type_die) || + !die_get_member_type (&type_die, offset, &type_die)) +return -1 ; + + tsr->type = type_die; + tsr->kind =TSR_KIND_TYPE ; + tsr->offset =0 ; + tsr->imm_value =0 ; + tsr->ok = true; + +if (src->multi_regs) { +pr_debug_dtp ("%s [%x] global (reg%d, reg%d) -> reg%d" , + dl->ins.name, insn_offset, src->reg1, src->reg2, dreg); + }else { +pr_debug_dtp ("%s [%x] global (reg%d) -> reg%d" , + dl->ins.name, insn_offset, sreg, dreg); + } +pr_debug_type_name (&tsr->type, tsr->kind); +return 0 ; + } +/* Or try another register if any */ if (src->multi_regs && src->reg1 != src->reg2 && sreg != src->reg2 && !(src->extend_type || src->shift_type)) {
@@ -634,7 +661,7 @@ static int propagate_load_reg_state(struct type_state *state, }static void update_load_insn_state (struct type_state *state, -struct data_loc_info *dloc, +struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl,struct annotated_op_loc *src,struct annotated_op_loc *dst)
@@ -663,7 +690,7 @@ static void update_load_insn_state(struct type_state *state, }/* Handle the first destination register */ -if (propagate_load_reg_state (state, dloc, dl, dst->reg1, +if (propagate_load_reg_state (state, dloc, cu_die, dl, dst->reg1, src, src_states,/*mem_offset=*/ 0 )) goto out_err_adjust;
@@ -681,7 +708,7 @@ static void update_load_insn_state(struct type_state *state, mem_offset =arm64__reg_size (dl->ops.target.raw);if (mem_offset <0 || -propagate_load_reg_state (state, dloc, dl, dst->reg2, +propagate_load_reg_state (state, dloc, cu_die, dl, dst->reg2, src, src_states, mem_offset)) goto out_err_adjust; }
@@ -894,14 +921,16 @@ static void update_add_insn_state(struct type_state *state,if (arch_get_reg_offset (dloc->arch, src, sreg, state, false, ®_offset)) goto try_other_reg; -if (src_tsr.kind ==TSR_KIND_CONST ) { +if (src_tsr.kind ==TSR_KIND_CONST || src_tsr.kind ==TSR_KIND_GLOBAL_ADDR ) { tsr->kind = src_tsr.kind; +/* For 'adrp + add' pair: resolve the full global variable address. */ tsr->imm_value = src_tsr.imm_value + reg_offset; tsr->offset =0 ; tsr->ok = src_tsr.ok; -pr_debug_dtp ("add [%x] imm %#x(reg%d) -> reg%d\n" , - insn_offset, reg_offset, sreg, dreg); +pr_debug_dtp ("add [%x] %s %#x(reg%d) -> reg%d\n" , + insn_offset, src_tsr.kind ==TSR_KIND_CONST ? +"imm" :"global" , reg_offset, sreg, dreg);return ; }
@@ -930,6 +959,39 @@ static void update_add_insn_state(struct type_state *state,invalidate_reg_state (tsr); } +static void update_adrp_insn_state (struct type_state *state, +struct disasm_line *dl, +struct annotated_op_loc *dst) +{ +struct type_state_reg *tsr; +u32 insn_offset = dl->al.offset; +int dreg = dst->reg1; + +if (!has_reg_type (state, dreg)) +return ; + + tsr = &state->regs[dreg]; + tsr->copied_from =-1 ; + +if (!dl->ops.source.addr) { +invalidate_reg_state (tsr); +return ; + } + + tsr->kind =TSR_KIND_GLOBAL_ADDR ; +/* +* Stores a partial page-relative address. The full absolute address +* of the global variable will be resolved when a subsequent 'add' or +* 'ldr' instruction consumes this register. +*/ + tsr->imm_value = dl->ops.source.addr; + tsr->offset =0 ; + tsr->ok = true; + +pr_debug_dtp ("adrp [%x] global addr=%#" PRIx64" -> reg%d\n" , + insn_offset, tsr->imm_value, dreg); +} +static void update_insn_state_arm64 (struct type_state *state,struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl)
@@ -996,6 +1058,7 @@ static void update_insn_state_arm64(struct type_state *state,* prevent stale type info from propagating to subsequent instructions. */ if (has_reg_type (state, dst->reg1) && !dst->mem_ref && +strcmp (dl->ins.name,"adrp" ) &&strcmp (dl->ins.name,"add" ) &&strcmp (dl->ins.name,"mov" ) && !is_standard_load_insn (dl->ins.name)) { pr_debug_dtp("%s [%x] invalidate reg%d",
@@ -1009,14 +1072,16 @@ static void update_insn_state_arm64(struct type_state *state,return ; } -if (!strcmp (dl->ins.name,"add" )) +if (!strcmp (dl->ins.name,"adrp" )) +update_adrp_insn_state (state, dl, dst); +else if (!strcmp (dl->ins.name,"add" ))update_add_insn_state (state, dloc, dl, src, dst);/* Register to register or imm value to register transfers */ else if (!strcmp (dl->ins.name,"mov" ))update_mov_insn_state (state, dl, src, dst);/* Memory to register transfers */ else if (is_standard_load_insn (dl->ins.name)) -update_load_insn_state (state, dloc, dl, src, dst); +update_load_insn_state (state, dloc, cu_die, dl, src, dst);/* Register to memory transfers */ else if (is_standard_store_insn (dl->ins.name))update_store_insn_state (state, dloc, dl, src, dst);
tools/perf/util/annotate-data.c +24 -1
@@ -70,6 +70,9 @@ void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind)case TSR_KIND_CANARY :pr_info (" stack canary\n" );return ; +case TSR_KIND_GLOBAL_ADDR : +pr_info (" global address\n" ); +return ;case TSR_KIND_TYPE :default :break ;
@@ -609,7 +612,8 @@ void set_stack_state(struct type_state_stack *stack, int offset, u8 kind, stack->kind = kind; stack->imm_value = imm_value; -if (kind ==TSR_KIND_POINTER || kind ==TSR_KIND_CONST ) { +if (kind ==TSR_KIND_POINTER || kind ==TSR_KIND_CONST || + kind ==TSR_KIND_GLOBAL_ADDR ) { stack->compound = false;return ; }
@@ -1365,6 +1369,25 @@ static enum type_match_result check_matching_type(struct type_state *state,if (offset <0 && reg != state->stack_reg && reg != dloc->fbreg) goto check_kernel; } + +if (state->regs[reg].kind ==TSR_KIND_GLOBAL_ADDR ) { +u64 var_addr = state->regs[reg].imm_value + offset; +int var_offset; + +pr_debug_dtp ("global addr" ); + +/* +* The register holds the address of a global variable. Try to +* find the variable by the address and get its type. +*/ +if (get_global_var_type (cu_die, dloc, dloc->ip, var_addr, + &var_offset, type_die)) { + dloc->type_offset = var_offset; +return PERF_TMR_OK ; + } +/* No need to retry global variables */ +return PERF_TMR_BAIL_OUT ; + } check_non_register:if (reg == dloc->fbreg || reg == state->stack_reg) {struct type_state_stack *stack;
tools/perf/util/annotate-data.h +1 -0
@@ -37,6 +37,7 @@ enum type_state_kind {TSR_KIND_PERCPU_POINTER ,TSR_KIND_POINTER ,TSR_KIND_CANARY , + TSR_KIND_GLOBAL_ADDR, };/**
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 24/26] perf annotate-arm64: Support 'adrp' instruction to track global variables
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
Extend
update_insn_state_arm64()to track global variable types calculated via page-relative addressing.On arm64, global variables are typically accessed by first calculating the page address using 'adrp', followed by an 'add' or 'ldr' to get the specific symbol address. Without tracking 'adrp', the instruction tracker loses the base address, making it impossible to resolve global symbols and their associated DWARF types.
Introduce
TSR_KIND_GLOBAL_ADDRto represent a partial global address state. When encountering 'adrp', store the page-aligned target address in the register's type state. Upon a subsequent 'add' or 'ldr' instruction that references aTSR_KIND_GLOBAL_ADDRregister, combine the page address with the immediate offset.
Quoted 6 lines
[ ... ]
chk [70] reg24 offset=0 ok=1 kind=7 global addr : Good! final result: type='struct folio*'Signed-off-by Li Huafei <[email protected]>Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
PATCH v5 25/26 perf annotate-arm64: Support per-cpu variable access tracking
In reply to Tengda WuExtend update_insn_state_arm64() to handle per-cpu variable addressing.
On arm64, per-cpu variables are accessed by adding a per-cpu offset (typically from the '__per_cpu_offset' array) to the address of a global variable or a structure field with '__percpu' modifier (e.g., 's32 __percpu *counters' in struct percpu_counter). This results in instruction patterns like:
ldr x0, [x6, x5] // Pattern A: direct load per-cpu instance add x0, x6, x5 // Pattern B: compute per-cpu address
where x6 holds the per-cpu offset retrieved from:
adrp x4, <page> add x4, x4, #offset // x4 = &__per_cpu_offset ldr x6, [x4, w0, sxtw #3] // x6 = __per_cpu_offset[cpu]
and x5 is one of the following:
case 1: global variable
adrp x5, <page>
add x5, x5, #offset // x5 = &global_var
case 2: '__percpu' field embedded in a structure
ldr x5, [x25, #32] // x5 = &percpu_field
To handle such cases:
1. Identify per-cpu base initialization: Detect 'adrp + ldr' pairs that resolve to the '__per_cpu_offset' symbol and mark the destination register as TSR_KIND_PERCPU_BASE.
2. Propagate type information: During subsequent 'ldr' or 'add' instructions, if one register is TSR_KIND_PERCPU_BASE, attempt to resolve the type from the other register.
A real-world example is shown below:
ffff8000808f2d28 <cppc_set_perf>:
ffff8000808f2d38: adrp x2, ffff800082033000 ffff8000808f2d3c: add x5, x2, #0x3f8 // x5 = &__per_cpu_offset ffff8000808f2d44: adrp x2, ffff800081f73000 ffff8000808f2d48: add x2, x2, #0x6b8 // x2 = &cpu_pcc_subspace_idx ffff8000808f2d6c: ldr x5, [x5, w0, sxtw #3] // x5 = __per_cpu_offset[cpu] * ffff8000808f2d80: ldr w23, [x5, x2] // per_cpu_var(cpu_pcc_subspace_idx)
Before this commit, the tracker could not link x5 back to a per-cpu context, resulting in an incorrect data type resolution:
adrp [10] global addr=0xffff800082033000 -> reg2
add [14] global 0x3f8(reg2) -> reg5
adrp [1c] global addr=0xffff800081f73000 -> reg2
add [20] global 0x6b8(reg2) -> reg2
ldr [44] global (reg5, reg0) -> reg5 type='long unsigned int[]' size=0x1000 chk [58] reg5 offset=0 ok=1 kind=1 (long unsigned int[]) : Good! found by insn track: 0(reg5, reg2) type-offset=0
final result: type='long unsigned int' size=0x8
After this commit, the tracker correctly identifies the per-cpu flow and resolves the actual variable type:
ldr [44] global (reg5, reg0) -> reg5 percpu base chk [58] reg5 offset=0 ok=1 kind=2 percpu var : retry chk [58] reg2 offset=0 ok=1 kind=7 global addr : Good! found by insn track: 0(reg5, reg2) type-offset=0 final result: type='int' size=0x4
--- .../perf/util/annotate-arch/annotate-arm64.c | 47 ++++++++++++++++++- tools/perf/util/annotate-data.c | 22 ++++++++- 2 files changed, 67 insertions(+), 2 deletions(-)
tools/perf/util/annotate-arch/annotate-arm64.c +46 -1
@@ -12,6 +12,7 @@#include "../disasm.h" #include "../annotate-data.h" #include "../debug.h" +#include "../dso.h" #include "../map.h" #include "../symbol.h"
@@ -627,13 +628,23 @@ static int propagate_load_reg_state(struct type_state *state,u64 ip = dloc->ms->sym->start + dl->al.offset;u64 addr = src_tsr->imm_value + reg_offset;int offset; +u8 kind; +const char *var_name =NULL ; + +/* it might be per-cpu offset */ +if (dso__kernel (map__dso (dloc->ms->map)) && +get_global_var_info (dloc, addr, &var_name, &offset) && + !strcmp (var_name,"__per_cpu_offset" )) + kind =TSR_KIND_PERCPU_BASE ; +else + kind =TSR_KIND_TYPE ;if (!get_global_var_type (cu_die, dloc, ip, addr, &offset, &type_die) || !die_get_member_type (&type_die, offset, &type_die))return -1 ; tsr->type = type_die; - tsr->kind =TSR_KIND_TYPE ; + tsr->kind = kind; tsr->offset =0 ; tsr->imm_value =0 ; tsr->ok = true;
@@ -649,6 +660,12 @@ static int propagate_load_reg_state(struct type_state *state,return 0 ; } +/* +* If src_tsr->kind is TSR_KIND_PERCPU_BASE, treat it as an array. +* No special handling is needed here; just fall through to retry +* and resolve the type from the second register. +*/ +/* Or try another register if any */ if (src->multi_regs && src->reg1 != src->reg2 && sreg != src->reg2 && !(src->extend_type || src->shift_type)) {
@@ -949,6 +966,34 @@ static void update_add_insn_state(struct type_state *state,return ; } +if (src_tsr.kind ==TSR_KIND_PERCPU_BASE ) { +int reg2; + +if (!src->multi_regs) { +invalidate_reg_state (tsr); +return ; + } + + reg2 = (sreg == src->reg1) ? src->reg2 : src->reg1; + +if (!has_reg_type (state, reg2) || !state->regs[reg2].ok) { +invalidate_reg_state (tsr); +return ; + } + +/* Treat percpu as array: inherit type from reg2 */ + tsr->type = state->regs[reg2].type; + tsr->kind = state->regs[reg2].kind; + tsr->offset = state->regs[reg2].offset; + tsr->imm_value = state->regs[reg2].imm_value; + tsr->ok = state->regs[reg2].ok; + +pr_debug_dtp ("add [%x] percpu (reg%d, reg%d) -> reg%d" , + insn_offset, src->reg1, src->reg2, dreg); +pr_debug_type_name (&tsr->type, tsr->kind); +return ; + } + try_other_reg:/* Try another register if any */ if (src->multi_regs && src->reg1 != src->reg2 && sreg != src->reg2 &&
tools/perf/util/annotate-data.c +21 -1
@@ -1339,11 +1339,31 @@ static enum type_match_result check_matching_type(struct type_state *state, }if (state->regs[reg].kind ==TSR_KIND_PERCPU_BASE ) { -u64 var_addr = (s64 ) offset; +u64 var_addr;int var_offset;pr_debug_dtp ("percpu var" ); +if (arch__is_arm64 (dloc->arch)) { +int reg2; + +if (!retry || !dloc->op->multi_regs || + dloc->op->reg1 == dloc->op->reg2 || + dloc->op->extend_type || dloc->op->shift_type) +return PERF_TMR_BAIL_OUT ; + + reg2 = dloc->op->reg2; +if (!has_reg_type (state, reg2) || !state->regs[reg2].ok) +return PERF_TMR_NO_TYPE ; + +pr_debug_dtp (" : retry\n" ); + retry = false; + reg = reg2; + goto again; + } + + var_addr = (s64 ) offset; +if (dloc->op->multi_regs) {int reg2 = dloc->op->reg2;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 25/26] perf annotate-arm64: Support per-cpu variable access tracking
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The newly added logic to track per-cpu base registers in 'add' and 'ldr' instructions is…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The newly added logic to track per-cpu base registers in 'add' and 'ldr' instructions is entirely unreachable dead code because arch_get_reg_offset prematurely aborts the tracking state. --
commit e3331fba4432f08f3db300129b5060bba477930c
Author: Tengda Wu <[email protected]>
perf annotate-arm64: Support per-cpu variable access tracking
This commit extends update_insn_state_arm64() to handle per-cpu variable addressing. It detects 'adrp + ldr' pairs that resolve to the '__per_cpu_offset' symbol, and attempts to propagate this type information through subsequent 'ldr' and 'add' instructions. However, the newly added logic is entirely unreachable dead code because arch_get_reg_offset() prematurely aborts the tracking state.
Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0
[ ... ]
tools/perf/util/annotate-arch/annotate-arm64.c static int propagate_load_reg_state(struct type_state *state, · line 668
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +6
-0@@ -649,6 +660,12 @@ static int propagate_load_reg_state(struct type_state *state,return 0 ; } +/* +* If src_tsr->kind is TSR_KIND_PERCPU_BASE, treat it as an array. +* No special handling is needed here; just fall through to retry +* and resolve the type from the second register. +*/ +
[Severity: Medium]
Is this fall-through logic in propagate_load_reg_state() for intermediate Pattern A ('ldr') instructions actually reachable?
Earlier in propagate_load_reg_state(), arch_get_reg_offset() forces an early exit by returning -1 when evaluating a multi-register load where the second register isn't a constant. This happens before this new per-cpu fallback logic can be reached, making this dead code.
Quoted 3 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0/* Or try another register if any */ if (src->multi_regs && src->reg1 != src->reg2 && sreg != src->reg2 && !(src->extend_type || src->shift_type)) {
[ ... ]
Quoted 22 lines
tools/perf/util/annotate-arch/annotate-arm64.c +18
-0@@ -949,6 +966,34 @@ static void update_add_insn_state(struct type_state *state,return ; } +if (src_tsr.kind ==TSR_KIND_PERCPU_BASE ) { +int reg2; + +if (!src->multi_regs) { +invalidate_reg_state (tsr); +return ; + } + + reg2 = (sreg == src->reg1) ? src->reg2 : src->reg1; + +if (!has_reg_type (state, reg2) || !state->regs[reg2].ok) { +invalidate_reg_state (tsr); +return ; + } + +/* Treat percpu as array: inherit type from reg2 */ + tsr->type = state->regs[reg2].type; + tsr->kind = state->regs[reg2].kind;
tools/perf/util/annotate-arch/annotate-arm64.c static void update_add_insn_state(struct type_state *state, · line 996
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+ tsr->offset = state->regs[reg2].offset; + tsr->imm_value = state->regs[reg2].imm_value; + tsr->ok = state->regs[reg2].ok; + +pr_debug_dtp ("add [%x] percpu (reg%d, reg%d) -> reg%d" , + insn_offset, src->reg1, src->reg2, dreg); +pr_debug_type_name (&tsr->type, tsr->kind); +return ; + } +
[Severity: Medium]
Does this block ever execute when handling a per-cpu address calculation (Pattern B like 'add x0, x6, x5') in update_add_insn_state()?
When arch_get_reg_offset() executes earlier in update_add_insn_state(), it returns -1 for multi-register non-memory operations (like 'add') unless one register is a TSR_KIND_CONST or the base is a known TSR_KIND_POINTER.
Since the registers here are PERCPU_BASE and GLOBAL_ADDR, it returns -1 and jumps to the try_other_reg label, permanently bypassing this new tracking logic.
Quoted 3 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0try_other_reg:/* Try another register if any */ if (src->multi_regs && src->reg1 != src->reg2 && sreg != src->reg2 &&
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=25
Re: [PATCH v5 25/26] perf annotate-arm64: Support per-cpu variable access tracking
Quoted 38 lines · On 2026/9/8 21:30, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The newly added logic to track per-cpu base registers in 'add' and 'ldr' instructions is entirely unreachable dead code becausearch_get_reg_offsetprematurely aborts the tracking state. --commit
e3331fba4432f08f3db300129b5060bba477930c
Author: Tengda Wu <[email protected]>perf annotate-arm64: Support per-cpu variable access tracking
This commit extends
update_insn_state_arm64()to handle per-cpu variable addressing. It detects 'adrp + ldr' pairs that resolve to the '__per_cpu_offset' symbol, and attempts to propagate this type information through subsequent 'ldr' and 'add' instructions. However, the newly added logic is entirely unreachable dead code becausearch_get_reg_offset()prematurely aborts the tracking state.Quoted 4 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0[ ... ]
tools/perf/util/annotate-arch/annotate-arm64.c static int propagate_load_reg_state(struct type_state *state, · line 668
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +6
-0@@ -649,6 +660,12 @@ static int propagate_load_reg_state(struct type_state *state,return 0 ; } +/* +* If src_tsr->kind is TSR_KIND_PERCPU_BASE, treat it as an array. +* No special handling is needed here; just fall through to retry +* and resolve the type from the second register. +*/ +[Severity: Medium]
Is this fall-through logic inpropagate_load_reg_state()for intermediate Pattern A ('ldr') instructions actually reachable?
Quoted 5 lines
Earlier in
propagate_load_reg_state(),arch_get_reg_offset()forces an early exit by returning -1 when evaluating a multi-register load where the second register isn't a constant. This happens before this new per-cpu fallback logic can be reached, making this dead code.
Indeed. The return happens too early.
Quoted 43 lines
Quoted 3 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0/* Or try another register if any */ if (src->multi_regs && src->reg1 != src->reg2 && sreg != src->reg2 && !(src->extend_type || src->shift_type)) {[ ... ]
Quoted 22 lines
tools/perf/util/annotate-arch/annotate-arm64.c +18
-0@@ -949,6 +966,34 @@ static void update_add_insn_state(struct type_state *state,return ; } +if (src_tsr.kind ==TSR_KIND_PERCPU_BASE ) { +int reg2; + +if (!src->multi_regs) { +invalidate_reg_state (tsr); +return ; + } + + reg2 = (sreg == src->reg1) ? src->reg2 : src->reg1; + +if (!has_reg_type (state, reg2) || !state->regs[reg2].ok) { +invalidate_reg_state (tsr); +return ; + } + +/* Treat percpu as array: inherit type from reg2 */ + tsr->type = state->regs[reg2].type; + tsr->kind = state->regs[reg2].kind;tools/perf/util/annotate-arch/annotate-arm64.c static void update_add_insn_state(struct type_state *state, · line 996
Quoted 10 lines
tools/perf/util/annotate-arch/annotate-arm64.c +10
-0+ tsr->offset = state->regs[reg2].offset; + tsr->imm_value = state->regs[reg2].imm_value; + tsr->ok = state->regs[reg2].ok; + +pr_debug_dtp ("add [%x] percpu (reg%d, reg%d) -> reg%d" , + insn_offset, src->reg1, src->reg2, dreg); +pr_debug_type_name (&tsr->type, tsr->kind); +return ; + } +[Severity: Medium]
Does this block ever execute when handling a per-cpu address calculation (Pattern B like 'add x0, x6, x5') inupdate_add_insn_state()?
Quoted 8 lines
When
arch_get_reg_offset()executes earlier inupdate_add_insn_state(), it returns -1 for multi-register non-memory operations (like 'add') unless one register is aTSR_KIND_CONSTor the base is a knownTSR_KIND_POINTER.Since the registers here are
PERCPU_BASEandGLOBAL_ADDR, it returns -1 and jumps to thetry_other_reglabel, permanently bypassing this new tracking logic.
Agreed. This is the same issue as above. Will fix in v6.
Quoted 4 lines
Quoted 3 lines
tools/perf/util/annotate-arch/annotate-arm64.c +0
-0try_other_reg:/* Try another register if any */ if (src->multi_regs && src->reg1 != src->reg2 && sreg != src->reg2 &&
PATCH v5 26/26 perf annotate-arm64: Support 'mrs' instruction to track 'current' pointer
In reply to Tengda WuExtend update_insn_state_arm64() to handle the 'mrs' instruction, enabling the tracking of the 'current' task pointer in the kernel.
On arm64, the kernel uses the 'sp_el0' system register to store the address of the currently executing 'struct task_struct'. This is typically accessed via the 'get_current()' inline function, resulting in the instruction 'mrs xN, sp_el0'.
To resolve the data type of the target register, first verify the access is to 'sp_el0' within a kernel DSO. Then, locate the 'get_current()' inline function's DWARF Die at the current PC and extract its return type (which should be 'struct task_struct*').
A real-world example is shown below:
ffff8000800deee8 <kthread_blkcg>:
ffff8000800deef0: mrs x0, sp_el0 // x0 = current * ffff8000800deef4: ldr w1, [x0, #44]
Before this commit, the type flow starts with no information:
chk [c] reg0 offset=0x2c ok=0 kind=0 cfa : no type information final result: no type information
After this commit, the tracker identifies the 'current' pointer from the system register:
mrs [8] sp_el0 -> reg0 type='struct task_struct*'
chk [c] reg0 offset=0x2c ok=1 kind=1 (struct task_struct*) : Good! found by insn track: 0x2c(reg0) type-offset=0x2c
final result: type='struct task_struct'
--- .../perf/util/annotate-arch/annotate-arm64.c | 73 ++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-)
tools/perf/util/annotate-arch/annotate-arm64.c +71 -2
@@ -4,6 +4,7 @@#include <inttypes.h> #include <stdlib.h> #include <string.h> +#include <strings.h> #include <linux/ctype.h> #include <linux/string.h> #include <linux/zalloc.h>
@@ -15,6 +16,7 @@#include "../dso.h" #include "../map.h" #include "../symbol.h" +#include "../strbuf.h" struct arch_arm64 {struct arch arch;
@@ -1037,6 +1039,71 @@ static void update_adrp_insn_state(struct type_state *state, insn_offset, tsr->imm_value, dreg); } +static void update_mrs_insn_state (struct type_state *state, +struct data_loc_info *dloc,Dwarf_Die *cu_die, +struct disasm_line *dl, +struct annotated_op_loc *dst) +{ +struct type_state_reg *tsr; +Dwarf_Die type_die; +u32 insn_offset = dl->al.offset; +int dreg = dst->reg1; +Dwarf_Die func_die; +Dwarf_Attribute attr; +u64 ip, pc; +struct strbuf sb; +char *type_name; + +if (!has_reg_type (state, dreg)) +return ; + + tsr = &state->regs[dreg]; + tsr->copied_from =-1 ; + +/* Handle case difference: LLVM (SP_EL0) vs objdump (sp_el0) */ +if (!dso__kernel (map__dso (dloc->ms->map)) || +strcasecmp (dl->ops.source.raw,"sp_el0" )) { +invalidate_reg_state (tsr); +return ; + } + + ip = dloc->ms->sym->start + dl->al.offset; + pc =map__rip_2objdump (dloc->ms->map, ip); + +/* +* Find the inline function 'get_current()' Dwarf_Die and obtain its +* return value data type, which should be 'struct task_struct*'. +*/ +if (!die_find_inlinefunc (cu_die, pc, &func_die) || + !die_compare_name (&func_die,"get_current" ) || + !dwarf_attr_integrate (&func_die, DW_AT_type, &attr) || + !dwarf_formref_die (&attr, &type_die)) { +invalidate_reg_state (tsr); +return ; + } + +strbuf_init (&sb,32 ); +die_get_typename_from_type (&type_die, &sb); + type_name =strbuf_detach (&sb,NULL ); + +if (!type_name ||strcmp (type_name,"struct task_struct*" )) { +invalidate_reg_state (tsr); + goto out_free; + } + + tsr->type = type_die; + tsr->kind =TSR_KIND_TYPE ; + tsr->offset =0 ; + tsr->imm_value =0 ; + tsr->ok = true; + +pr_debug_dtp ("mrs [%x] sp_el0 -> reg%d" , insn_offset, dreg); +pr_debug_type_name (&type_die, tsr->kind); + +out_free: +free (type_name); +} +static void update_insn_state_arm64 (struct type_state *state,struct data_loc_info *dloc,Dwarf_Die *cu_die,struct disasm_line *dl)
@@ -1103,7 +1170,7 @@ static void update_insn_state_arm64(struct type_state *state,* prevent stale type info from propagating to subsequent instructions. */ if (has_reg_type (state, dst->reg1) && !dst->mem_ref && -strcmp (dl->ins.name,"adrp" ) && +strcmp (dl->ins.name,"mrs" ) &&strcmp (dl->ins.name,"adrp" ) &&strcmp (dl->ins.name,"add" ) &&strcmp (dl->ins.name,"mov" ) && !is_standard_load_insn (dl->ins.name)) { pr_debug_dtp("%s [%x] invalidate reg%d",
@@ -1117,7 +1184,9 @@ static void update_insn_state_arm64(struct type_state *state,return ; } -if (!strcmp (dl->ins.name,"adrp" )) +if (!strcmp (dl->ins.name,"mrs" )) +update_mrs_insn_state (state, dloc, cu_die, dl, dst); +else if (!strcmp (dl->ins.name,"adrp" ))update_adrp_insn_state (state, dl, dst);else if (!strcmp (dl->ins.name,"add" ))update_add_insn_state (state, dloc, dl, src, dst);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 26/26] perf annotate-arm64: Support 'mrs' instruction to track 'current' pointer
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
Extend
update_insn_state_arm64()to handle the 'mrs' instruction, enabling the tracking of the 'current' task pointer in the kernel.On arm64, the kernel uses the '
sp_el0' system register to store the address of the currently executing 'structtask_struct'. This is typically accessed via the 'get_current()' inline function, resulting in the instruction 'mrs xN,sp_el0'.To resolve the data type of the target register, first verify the access is to '
sp_el0' within a kernel DSO. Then, locate the 'get_current()' inline function's DWARF Die at the current PC and extract its return type (which should be 'struct task_struct*').
Quoted 8 lines
A real-world example is shown below:
[ ... ]
found by insn track: 0x2c(reg0) type-offset=0x2c final result: type='structtask_struct'Signed-off-by Li Huafei <[email protected]>Signed-off-by Tengda Wu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26