perf annotate-data: Fix percpu pointer check
authorNamhyung Kim <namhyung@kernel.org>
Wed, 21 Aug 2024 06:54:08 +0000 (23:54 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 21 Aug 2024 14:30:38 +0000 (11:30 -0300)
In check_matching_type(), it checks the type state of the register in a
wrong order.  When it's the percpu pointer, it should check the type for
the pointer, but it checks the CFA bit first and thought it has no type
in the stack slot.  This resulted in no type info.

  -----------------------------------------------------------
  find data type for 0x28(reg1) at hrtimer_reprogram+0x88
  CU for kernel/time/hrtimer.c (die:0x18f219f)
  frame base: cfa=1 fbreg=7
  ...
  add [72] percpu 0x24500 -> reg1 pointer type='struct hrtimer_cpu_base' size=0x240 (die:0x18f6d46)
  bb: [7a - 7e]
  bb: [80 - 86]                        (here)
  bb: [88 - 88]                         vvv
  chk [88] reg1 offset=0x28 ok=1 kind=4 cfa : no type information
  no type information

Here, instruction at 0x72 found reg1 has a (percpu) pointer and got the
correct type.  But when it checks the final result, it wrongly thought
it was stack variable because it checks the cfa bit first.

After changing the order of state check:
  -----------------------------------------------------------
  find data type for 0x28(reg1) at hrtimer_reprogram+0x88
  CU for kernel/time/hrtimer.c (die:0x18f219f)
  frame base: cfa=1 fbreg=7
  ...                                     (here)
                                        vvvvvvvvvv
  chk [88] reg1 offset=0x28 ok=1 kind=4 percpu ptr : Good!
  found by insn track: 0x28(reg1) type-offset=0x28
  final type: type='struct hrtimer_cpu_base' size=0x240 (die:0x18f6d46)

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240821065408.285548-3-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/arch/x86/annotate/instructions.c
tools/perf/util/annotate-data.c

index 88b5bcf2116fc8ab8d6799f147990bc587e883ee..15dfc2988e24dcea80e237945aee38746790ff37 100644 (file)
@@ -282,6 +282,7 @@ static void update_insn_state_x86(struct type_state *state,
                            !strcmp(var_name, "this_cpu_off") &&
                            tsr->kind == TSR_KIND_CONST) {
                                tsr->kind = TSR_KIND_PERCPU_BASE;
+                               tsr->ok = true;
                                imm_value = tsr->imm_value;
                        }
                }
@@ -533,9 +534,11 @@ retry:
                                                        &var_name, &offset) &&
                                    !strcmp(var_name, "__per_cpu_offset")) {
                                        tsr->kind = TSR_KIND_PERCPU_BASE;
+                                       tsr->ok = true;
 
                                        pr_debug_dtp("mov [%x] percpu base reg%d\n",
                                                     insn_offset, dst->reg1);
+                                       return;
                                }
                        }
 
index bc65264084d851ebbb1f5204bb04653809991dd5..f5eefcb71c4f13058e21955d86cdd5cc010c9ff0 100644 (file)
@@ -965,7 +965,10 @@ static enum type_match_result check_matching_type(struct type_state *state,
                     insn_offset, reg, dloc->op->offset,
                     state->regs[reg].ok, state->regs[reg].kind);
 
-       if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_TYPE) {
+       if (!state->regs[reg].ok)
+               goto check_non_register;
+
+       if (state->regs[reg].kind == TSR_KIND_TYPE) {
                Dwarf_Die sized_type;
 
                /*
@@ -998,6 +1001,65 @@ static enum type_match_result check_matching_type(struct type_state *state,
                return PERF_TMR_OK;
        }
 
+       if (state->regs[reg].kind == TSR_KIND_POINTER) {
+               pr_debug_dtp("percpu ptr");
+
+               /*
+                * It's actaully pointer but the address was calculated using
+                * some arithmetic.  So it points to the actual type already.
+                */
+               *type_die = state->regs[reg].type;
+
+               dloc->type_offset = dloc->op->offset;
+
+               /* Get the size of the actual type */
+               if (dwarf_aggregate_size(type_die, &size) < 0 ||
+                   (unsigned)dloc->type_offset >= size)
+                       return PERF_TMR_BAIL_OUT;
+
+               return PERF_TMR_OK;
+       }
+
+       if (state->regs[reg].kind == TSR_KIND_CANARY) {
+               pr_debug_dtp("stack canary");
+
+               /*
+                * This is a saved value of the stack canary which will be handled
+                * in the outer logic when it returns failure here.  Pretend it's
+                * from the stack canary directly.
+                */
+               setup_stack_canary(dloc);
+
+               return PERF_TMR_BAIL_OUT;
+       }
+
+       if (state->regs[reg].kind == TSR_KIND_PERCPU_BASE) {
+               u64 var_addr = dloc->op->offset;
+               int var_offset;
+
+               pr_debug_dtp("percpu var");
+
+               if (dloc->op->multi_regs) {
+                       int reg2 = dloc->op->reg2;
+
+                       if (dloc->op->reg2 == reg)
+                               reg2 = dloc->op->reg1;
+
+                       if (has_reg_type(state, reg2) && state->regs[reg2].ok &&
+                           state->regs[reg2].kind == TSR_KIND_CONST)
+                               var_addr += state->regs[reg2].imm_value;
+               }
+
+               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 per-cpu (global) variables */
+               return PERF_TMR_BAIL_OUT;
+       }
+
+check_non_register:
        if (reg == dloc->fbreg) {
                struct type_state_stack *stack;
 
@@ -1054,64 +1116,6 @@ static enum type_match_result check_matching_type(struct type_state *state,
                return PERF_TMR_OK;
        }
 
-       if (state->regs[reg].kind == TSR_KIND_PERCPU_BASE) {
-               u64 var_addr = dloc->op->offset;
-               int var_offset;
-
-               pr_debug_dtp("percpu var");
-
-               if (dloc->op->multi_regs) {
-                       int reg2 = dloc->op->reg2;
-
-                       if (dloc->op->reg2 == reg)
-                               reg2 = dloc->op->reg1;
-
-                       if (has_reg_type(state, reg2) && state->regs[reg2].ok &&
-                           state->regs[reg2].kind == TSR_KIND_CONST)
-                               var_addr += state->regs[reg2].imm_value;
-               }
-
-               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 per-cpu (global) variables */
-               return PERF_TMR_BAIL_OUT;
-       }
-
-       if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_POINTER) {
-               pr_debug_dtp("percpu ptr");
-
-               /*
-                * It's actaully pointer but the address was calculated using
-                * some arithmetic.  So it points to the actual type already.
-                */
-               *type_die = state->regs[reg].type;
-
-               dloc->type_offset = dloc->op->offset;
-
-               /* Get the size of the actual type */
-               if (dwarf_aggregate_size(type_die, &size) < 0 ||
-                   (unsigned)dloc->type_offset >= size)
-                       return PERF_TMR_BAIL_OUT;
-
-               return PERF_TMR_OK;
-       }
-
-       if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_CANARY) {
-               pr_debug_dtp("stack canary");
-
-               /*
-                * This is a saved value of the stack canary which will be handled
-                * in the outer logic when it returns failure here.  Pretend it's
-                * from the stack canary directly.
-                */
-               setup_stack_canary(dloc);
-
-               return PERF_TMR_BAIL_OUT;
-       }
-
 check_kernel:
        if (dso__kernel(map__dso(dloc->ms->map))) {
                u64 addr;