From nobody Wed Dec 17 23:08:47 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75DAC200C1; Thu, 2 May 2024 06:00:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629613; cv=none; b=KUu/4MxVDMi40qtfEROUgXwFW0HgJNBfHzyCqjboK9Pmppmg7AWYLhTl8EI7SHcXfkaATE+4Pb9LVNiRa/+D9kTCYDve5iRemgz6+ZbQ3XfaRuyx326mQNZ/hjp3fU219VQ4Uuq1oDo/M5rdo1lgxREF0pcKZfeTwfEbYAEB/18= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629613; c=relaxed/simple; bh=zamPJCSjSG+pCfLqayGyCmEr6GJWt/RSUPZolnFgaGU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e3Xt3uj05gyVcrdM3EEMgkLB82Z0Guoj7aZIxN1oUq/BmuuZlYBOTW377ihW2r4yCK+xatQctfbXf+TiPRCZ0G+6+mUdrvJoM55Wuz/NE4TU3Wx/QCBS1/TGsOobLxxWwSoj7hjzNhna96rCBK9ZLa5JKbk0d1wr2qrScw4tF6U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mf2zs8bG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Mf2zs8bG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE07CC4AF1B; Thu, 2 May 2024 06:00:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714629613; bh=zamPJCSjSG+pCfLqayGyCmEr6GJWt/RSUPZolnFgaGU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mf2zs8bGiCmtcXgOJkeMSCdHVIVT415uotH3XYA+24ZYUoz+tYMvw71BANcQ1/Vae BMNGfbb2y00lYg2yot8ZWh9eYODHM4h72jktERY4ypljt00HdQbFHaSie3bYeZc2Vs xjnB5GupsH9RMPLkqKUPDktbQMzBx0PS/koYUf1QJdHIbu/FO5fW+cQ07XHdC2kZGp w3HwNfY5h3lboobASJTP8hezJHY8kDAjWAhlB6wV7/u4aUANypM6GjkDKmkjky5jbW PA6xK2DFUnaKPCgSCBDrjNe/+uQccWGfdpVpnnaJgWNesVPoi+F1WEygWVxpQOthLa +KFa7F4dRYr4g== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Masami Hiramatsu Subject: [PATCH 1/6] perf dwarf-aux: Add die_collect_global_vars() Date: Wed, 1 May 2024 23:00:06 -0700 Message-ID: <20240502060011.1838090-2-namhyung@kernel.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240502060011.1838090-1-namhyung@kernel.org> References: <20240502060011.1838090-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This function is to search all global variables in the CU. We want to have the list of global variables at once and match them later. Cc: Masami Hiramatsu Signed-off-by: Namhyung Kim --- tools/perf/util/dwarf-aux.c | 62 +++++++++++++++++++++++++++++++++++++ tools/perf/util/dwarf-aux.h | 8 +++++ 2 files changed, 70 insertions(+) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 40cfbdfe2d75..c0a492e65388 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1630,6 +1630,68 @@ void die_collect_vars(Dwarf_Die *sc_die, struct die_= var_type **var_types) =20 die_find_child(sc_die, __die_collect_vars_cb, (void *)var_types, &die_mem= ); } + +static int __die_collect_global_vars_cb(Dwarf_Die *die_mem, void *arg) +{ + struct die_var_type **var_types =3D arg; + Dwarf_Die type_die; + int tag =3D dwarf_tag(die_mem); + Dwarf_Attribute attr; + Dwarf_Addr base, start, end; + Dwarf_Op *ops; + size_t nops; + struct die_var_type *vt; + + if (tag !=3D DW_TAG_variable) + return DIE_FIND_CB_SIBLING; + + if (dwarf_attr(die_mem, DW_AT_location, &attr) =3D=3D NULL) + return DIE_FIND_CB_SIBLING; + + /* Only collect the location with an absolute address. */ + if (dwarf_getlocations(&attr, 0, &base, &start, &end, &ops, &nops) <=3D 0) + return DIE_FIND_CB_SIBLING; + + if (ops->atom !=3D DW_OP_addr) + return DIE_FIND_CB_SIBLING; + + if (!check_allowed_ops(ops, nops)) + return DIE_FIND_CB_SIBLING; + + if (die_get_real_type(die_mem, &type_die) =3D=3D NULL) + return DIE_FIND_CB_SIBLING; + + vt =3D malloc(sizeof(*vt)); + if (vt =3D=3D NULL) + return DIE_FIND_CB_END; + + vt->die_off =3D dwarf_dieoffset(&type_die); + vt->addr =3D ops->number; + vt->reg =3D -1; + vt->offset =3D 0; + vt->next =3D *var_types; + *var_types =3D vt; + + return DIE_FIND_CB_SIBLING; +} + +/** + * die_collect_global_vars - Save all global variables + * @cu_die: a CU DIE + * @var_types: a pointer to save the resulting list + * + * Save all global variables in the @cu_die and save them to @var_types. + * The @var_types is a singly-linked list containing type and location inf= o. + * Actual type can be retrieved using dwarf_offdie() with 'die_off' later. + * + * Callers should free @var_types. + */ +void die_collect_global_vars(Dwarf_Die *cu_die, struct die_var_type **var_= types) +{ + Dwarf_Die die_mem; + + die_find_child(cu_die, __die_collect_global_vars_cb, (void *)var_types, &= die_mem); +} #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ =20 #ifdef HAVE_DWARF_CFI_SUPPORT diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index b0f25fbf9668..24446412b869 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -171,6 +171,9 @@ Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die,= Dwarf_Addr addr, /* Save all variables and parameters in this scope */ void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types); =20 +/* Save all global variables in this CU */ +void die_collect_global_vars(Dwarf_Die *cu_die, struct die_var_type **var_= types); + #else /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ =20 static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused, @@ -203,6 +206,11 @@ static inline void die_collect_vars(Dwarf_Die *sc_die = __maybe_unused, { } =20 +static inline void die_collect_global_vars(Dwarf_Die *cu_die __maybe_unuse= d, + struct die_var_type **var_types __maybe_unused) +{ +} + #endif /* HAVE_DWARF_GETLOCATIONS_SUPPORT */ =20 #ifdef HAVE_DWARF_CFI_SUPPORT --=20 2.45.0.rc1.225.g2a3ae87e7f-goog From nobody Wed Dec 17 23:08:47 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3611E21A04; Thu, 2 May 2024 06:00:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629614; cv=none; b=i/K0PLBrQ0DpmvuQUogUPZNbuPx0R57MrzmhzBKj1/5is3sEueX9YoXggRu5cA/fNV1EgEST5MclTS0kwt2QlGAVwR+nKLuUt7wufFf4bJVxWRQxtCmXr0GxuykuvrJH5ErqJkwH+EEkLlCZljPBGKblkHs8JY5qErCU03J4pOY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629614; c=relaxed/simple; bh=h6exK3w1cm8ykwfB731qAJf7BoUGS3DGiwQPGvQefUI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u5FXmSt5nGmF0wtDnkV4XdAYHBn8JbDC/H8m1YPevHyu/aRvd52JEuLzWpeQUD/WRLokGAlYYEv9kGIRbjf9EMec9Jmp3Ys4TkOUHQnCOUffTUcp1mZkvd+ka+R3lA+QMH2ntxcsF7VNuDdLytdOr/Bpovb4MwY9XtZCRrW9IfU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=adJBniXS; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="adJBniXS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A06CC4AF1D; Thu, 2 May 2024 06:00:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714629613; bh=h6exK3w1cm8ykwfB731qAJf7BoUGS3DGiwQPGvQefUI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=adJBniXSqe9CqNaEDowdyCnnQxX74cujjSFodDuirMe2YrAtlbg8rZpzuaubIz4VZ QwmdKxU2WrCOCtKVEqVChvXrcODU0FJBupEylSxdIFXs4Yhm7xNXr9wpNxjlaQ8rBB PHWRgs1D+C9R4R8kksySAbGVo6lRl1JeJTaQjHgOWgPf2lOj/ZA8N3HtFN24CRtO/t zimC6Fv0Oz8zRVsyVX6RlcNZexBeIsPpeLB2K7PdUuLRdkIGPDHBTqbaP66A+3xZ8P RvAI4KvrDnXHKlmD816XHICJsviqG621fud7QRWXz6eQ7Vk1g2CP1wViXKiDLCgoVE CZG7/tN062ycQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org Subject: [PATCH 2/6] perf annotate-data: Collect global variables in advance Date: Wed, 1 May 2024 23:00:07 -0700 Message-ID: <20240502060011.1838090-3-namhyung@kernel.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240502060011.1838090-1-namhyung@kernel.org> References: <20240502060011.1838090-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently it looks up global variables from the current CU using address and name. But it sometimes fails to find a variable as the variable can come from a different CU - but it's still strange it failed to find a declaration for some reason. Anyway, it can collect all global variables from all CU once and then lookup them later on. This slightly improves the success rate of my test data set. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 57 +++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 12d5faff3b7a..4dd0911904f2 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -28,6 +28,8 @@ /* register number of the stack pointer */ #define X86_REG_SP 7 =20 +static void delete_var_types(struct die_var_type *var_types); + enum type_state_kind { TSR_KIND_INVALID =3D 0, TSR_KIND_TYPE, @@ -557,8 +559,8 @@ static bool global_var__add(struct data_loc_info *dloc,= u64 addr, if (gvar =3D=3D NULL) return false; =20 - gvar->name =3D strdup(name); - if (gvar->name =3D=3D NULL) { + gvar->name =3D name ? strdup(name) : NULL; + if (name && gvar->name =3D=3D NULL) { free(gvar); return false; } @@ -612,6 +614,53 @@ static bool get_global_var_info(struct data_loc_info *= dloc, u64 addr, return true; } =20 +static void global_var__collect(struct data_loc_info *dloc) +{ + Dwarf *dwarf =3D dloc->di->dbg; + Dwarf_Off off, next_off; + Dwarf_Die cu_die, type_die; + size_t header_size; + + /* Iterate all CU and collect global variables that have no location in a= register. */ + off =3D 0; + while (dwarf_nextcu(dwarf, off, &next_off, &header_size, + NULL, NULL, NULL) =3D=3D 0) { + struct die_var_type *var_types =3D NULL; + struct die_var_type *pos; + + if (dwarf_offdie(dwarf, off + header_size, &cu_die) =3D=3D NULL) { + off =3D next_off; + continue; + } + + die_collect_global_vars(&cu_die, &var_types); + + for (pos =3D var_types; pos; pos =3D pos->next) { + const char *var_name =3D NULL; + int var_offset =3D 0; + + if (pos->reg !=3D -1) + continue; + + if (!dwarf_offdie(dwarf, pos->die_off, &type_die)) + continue; + + if (!get_global_var_info(dloc, pos->addr, &var_name, + &var_offset)) + continue; + + if (var_offset !=3D 0) + continue; + + global_var__add(dloc, pos->addr, var_name, &type_die); + } + + delete_var_types(var_types); + + off =3D next_off; + } +} + static bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *d= loc, u64 ip, u64 var_addr, int *var_offset, Dwarf_Die *type_die) @@ -620,8 +669,12 @@ static bool get_global_var_type(Dwarf_Die *cu_die, str= uct data_loc_info *dloc, int offset; const char *var_name =3D NULL; struct global_var_entry *gvar; + struct dso *dso =3D map__dso(dloc->ms->map); Dwarf_Die var_die; =20 + if (RB_EMPTY_ROOT(&dso->global_vars)) + global_var__collect(dloc); + gvar =3D global_var__find(dloc, var_addr); if (gvar) { if (!dwarf_offdie(dloc->di->dbg, gvar->die_offset, type_die)) --=20 2.45.0.rc1.225.g2a3ae87e7f-goog From nobody Wed Dec 17 23:08:47 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8401E224F2; Thu, 2 May 2024 06:00:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629614; cv=none; b=gQun2ZmXeew5thr/XJtJfmCrcCWIZfcWpUyyiO12fdMXdIqOLIqHIHnN0X7eog3jJEKsAr9uGL5ewaPgJReCY7VPPZ0ltnwsnwlpPoG9OZcT9IJTY/gUksaG66nftOaG4CzsflCp1R5bjHdKv2jXfbI+KhCLIz3I5IFOoU6Y1dc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629614; c=relaxed/simple; bh=SSyYuDfAIqFwr0E0S0Wr/ukwLpvby2PoPSUWIQ8hahY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tlX4QOvqQw8HKhLAeg6HopJoEjJ7lIE9yZL+4Qx3nHclZJMe/jyKKFt+oz3xLRc0PbNOMDVtpZmo9IR/LUkGnNJuuwwU2DIetmJfymxblaog8mNFTM5mfejcsBAmmiyuj3tdmmV8hxBOvhBU1cKlYxAGsgx1+OBWLFHy2IRP+Uk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BldDkGuw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BldDkGuw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 09099C4AF49; Thu, 2 May 2024 06:00:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714629614; bh=SSyYuDfAIqFwr0E0S0Wr/ukwLpvby2PoPSUWIQ8hahY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BldDkGuwbfyaEA5Fxssr4aaABm0kVEWzAu8kv5Y2r8J/bvNwhvx4Eohi0O0wCcWr7 Dtymvpr3xI1XO7qki2JuNYMcT0ZNEXPjjHUsrbIy78bcXpxSZ+iOInr8TXXAbMddEU sF31DmxWeRjlsUMZRsfB34LvJhgnUW/uGaCS5w1jieRnUUgs1kImZyjRYdH1j4gR2S U9TZwG+xFL8WNKLspg0nyWUY1luxvEWs9sK41dj/4GUR/hysVKVsTznxDzxA8ZO5AI 3Xx1h4E1T6pyABWHxQ7XP920YTnJSUQxM6mKrUsKlkM4GQv4r7Y82kqxEcIl+VUW3+ GFLhzH0US3UsQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org Subject: [PATCH 3/6] perf annotate-data: Handle direct global variable access Date: Wed, 1 May 2024 23:00:08 -0700 Message-ID: <20240502060011.1838090-4-namhyung@kernel.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240502060011.1838090-1-namhyung@kernel.org> References: <20240502060011.1838090-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Like per-cpu base offset array, sometimes it accesses the global variable directly using the offset. Allow this type of instructions as long as it finds a global variable for the address. movslq %edi, %rcx mov -0x7dc94ae0(,%rcx,8), %rcx <<<--- here As %rcx has a valid type (i.e. array index) from the first instruction, it will be checked by the first case in check_matching_type(). But as it's not a pointer type, the match will fail. But in this case, it should check if it accesses the kernel global array variable. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 4dd0911904f2..f1e52a531563 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -1256,14 +1256,19 @@ static int check_matching_type(struct type_state *s= tate, if (state->regs[reg].ok && state->regs[reg].kind =3D=3D TSR_KIND_TYPE) { int tag =3D dwarf_tag(&state->regs[reg].type); =20 - pr_debug_dtp("\n"); - /* * Normal registers should hold a pointer (or array) to * dereference a memory location. */ - if (tag !=3D DW_TAG_pointer_type && tag !=3D DW_TAG_array_type) + if (tag !=3D DW_TAG_pointer_type && tag !=3D DW_TAG_array_type) { + if (dloc->op->offset < 0 && reg !=3D state->stack_reg) + goto check_kernel; + + pr_debug_dtp("\n"); return -1; + } + + pr_debug_dtp("\n"); =20 /* Remove the pointer and get the target type */ if (die_get_real_type(&state->regs[reg].type, type_die) =3D=3D NULL) @@ -1376,12 +1381,14 @@ static int check_matching_type(struct type_state *s= tate, return -1; } =20 - if (map__dso(dloc->ms->map)->kernel && arch__is(dloc->arch, "x86")) { +check_kernel: + if (map__dso(dloc->ms->map)->kernel) { u64 addr; int offset; =20 /* Direct this-cpu access like "%gs:0x34740" */ - if (dloc->op->segment =3D=3D INSN_SEG_X86_GS && dloc->op->imm) { + if (dloc->op->segment =3D=3D INSN_SEG_X86_GS && dloc->op->imm && + arch__is(dloc->arch, "x86")) { pr_debug_dtp(" this-cpu var\n"); =20 addr =3D dloc->op->offset; @@ -1394,17 +1401,13 @@ static int check_matching_type(struct type_state *s= tate, return -1; } =20 - /* Access to per-cpu base like "-0x7dcf0500(,%rdx,8)" */ + /* Access to global variable like "-0x7dcf0500(,%rdx,8)" */ if (dloc->op->offset < 0 && reg !=3D state->stack_reg) { - const char *var_name =3D NULL; - addr =3D (s64) dloc->op->offset; =20 - if (get_global_var_info(dloc, addr, &var_name, &offset) && - !strcmp(var_name, "__per_cpu_offset") && offset =3D=3D 0 && - get_global_var_type(cu_die, dloc, dloc->ip, addr, + if (get_global_var_type(cu_die, dloc, dloc->ip, addr, &offset, type_die)) { - pr_debug_dtp(" percpu base\n"); + pr_debug_dtp(" global var\n"); =20 dloc->type_offset =3D offset; return 1; --=20 2.45.0.rc1.225.g2a3ae87e7f-goog From nobody Wed Dec 17 23:08:47 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 055C823775; Thu, 2 May 2024 06:00:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629615; cv=none; b=lfAiPHirvpjOGLRwTE8C5YYFc3aOh223xlCgd/d7tf6WVnQWEHxIPSNX1b+lkgJ1pBYbT4O7Pyw7v54WD3++9OMwOOVj8sl00w8/y7Zu8DbdJhqK87uQstEJoBihFUjknBIRIrOduar6L24aEkcL9cBLXRlRBmZGNsTql6N5oMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629615; c=relaxed/simple; bh=FD83vOLGqIEiapuFz0V+SOOdVptlx7pB58xGzLzESqw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NFPgEoPQLam8N9Ba/4J2FiMfm6qHuATgb11nKLsiveNCFd3WDy70cRelXOB2+k39Fwq+LMzaOqdI8HUKZZ9bceEHcPLTk0ERR6X1IWMX3LIljM+uyu9fn5Ptkf0pdUj3xuMF4fwRKe1qeioyn0sYLwOoLr+2KPTxW4WgZtj75iE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rHZFhw/W; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rHZFhw/W" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82139C4AF50; Thu, 2 May 2024 06:00:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714629614; bh=FD83vOLGqIEiapuFz0V+SOOdVptlx7pB58xGzLzESqw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rHZFhw/WFZWr/010GbTf+vBwDGHAWj5xhRUIq/VH4Il3w2dgRjA56xG6KjqNz0oBH ftDTLJCYVInzFIKFhRvuQ9FIvDt/y2D03TYyh1ApemaDCdpv16e051gQqe34K84I7E x4O5WQX+/N/7Z9aLYfJmDZ7mvGMQYXxzKjKq89UKvuGjq0h5LfRvGIxFDy6yYsBOA8 TJN0Q/Re+wd4V/GhaknVD19obidf60RkkPDYEQ3E2x6fJV15sMC/hwnkjT7DM55O7u YG6qA5ETnadqEwLwbF8sYZfAkKBM42T5xdraDPKWyM69pTyrJID6TkR4aScJ6DRpTU fd51LCLJkhzJQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org Subject: [PATCH 4/6] perf annotate-data: Check memory access with two registers Date: Wed, 1 May 2024 23:00:09 -0700 Message-ID: <20240502060011.1838090-5-namhyung@kernel.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240502060011.1838090-1-namhyung@kernel.org> References: <20240502060011.1838090-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The following instruction pattern is used to access a global variable. mov $0x231c0, %rax movsql %edi, %rcx mov -0x7dc94ae0(,%rcx,8), %rcx cmpl $0x0, 0xa60(%rcx,%rax,1) <<<--- here The first instruction set the address of the per-cpu variable (here, it is 'runqueus' of struct rq). The second instruction seems like a cpu number of the per-cpu base. The third instruction get the base offset of per-cpu area for that cpu. The last instruction compares the value of the per-cpu variable at the offset of 0xa60. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 44 +++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index f1e52a531563..245e3ef3e2ff 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -1031,22 +1031,37 @@ static void update_insn_state_x86(struct type_state= *state, else if (has_reg_type(state, sreg) && state->regs[sreg].kind =3D=3D TSR_KIND_PERCPU_BASE) { u64 ip =3D dloc->ms->sym->start + dl->al.offset; + u64 var_addr =3D src->offset; int offset; =20 + if (src->multi_regs) { + int reg2 =3D (sreg =3D=3D src->reg1) ? src->reg2 : src->reg1; + + if (has_reg_type(state, reg2) && state->regs[reg2].ok && + state->regs[reg2].kind =3D=3D TSR_KIND_CONST) + var_addr +=3D state->regs[reg2].imm_value; + } + /* * In kernel, %gs points to a per-cpu region for the * current CPU. Access with a constant offset should * be treated as a global variable access. */ - if (get_global_var_type(cu_die, dloc, ip, src->offset, + if (get_global_var_type(cu_die, dloc, ip, var_addr, &offset, &type_die) && die_get_member_type(&type_die, offset, &type_die)) { tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; tsr->ok =3D true; =20 - pr_debug_dtp("mov [%x] percpu %#x(reg%d) -> reg%d", - insn_offset, src->offset, sreg, dst->reg1); + if (src->multi_regs) { + pr_debug_dtp("mov [%x] percpu %#x(reg%d,reg%d) -> reg%d", + insn_offset, src->offset, src->reg1, + src->reg2, dst->reg1); + } else { + pr_debug_dtp("mov [%x] percpu %#x(reg%d) -> reg%d", + insn_offset, src->offset, sreg, dst->reg1); + } pr_debug_type_name(&tsr->type, tsr->kind); } else { tsr->ok =3D false; @@ -1340,6 +1355,17 @@ static int check_matching_type(struct type_state *st= ate, =20 pr_debug_dtp(" percpu var\n"); =20 + if (dloc->op->multi_regs) { + int reg2 =3D dloc->op->reg2; + + if (dloc->op->reg2 =3D=3D reg) + reg2 =3D dloc->op->reg1; + + if (has_reg_type(state, reg2) && state->regs[reg2].ok && + state->regs[reg2].kind =3D=3D TSR_KIND_CONST) + var_addr +=3D state->regs[reg2].imm_value; + } + if (get_global_var_type(cu_die, dloc, dloc->ip, var_addr, &var_offset, type_die)) { dloc->type_offset =3D var_offset; @@ -1527,8 +1553,16 @@ static int find_data_type_block(struct data_loc_info= *dloc, int reg, found =3D find_data_type_insn(dloc, reg, &basic_blocks, var_types, cu_die, type_die); if (found > 0) { - pr_debug_dtp("found by insn track: %#x(reg%d) type-offset=3D%#x\n", - dloc->op->offset, reg, dloc->type_offset); + char buf[64]; + + if (dloc->op->multi_regs) + snprintf(buf, sizeof(buf), "reg%d, reg%d", + dloc->op->reg1, dloc->op->reg2); + else + snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1); + + pr_debug_dtp("found by insn track: %#x(%s) type-offset=3D%#x\n", + dloc->op->offset, buf, dloc->type_offset); pr_debug_type_name(type_die, TSR_KIND_TYPE); ret =3D 0; break; --=20 2.45.0.rc1.225.g2a3ae87e7f-goog From nobody Wed Dec 17 23:08:47 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE58E2E85A; Thu, 2 May 2024 06:00:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629616; cv=none; b=QiCxMvYyjpUypR2kJ9jCh+hIj3LJUmSkB/kzKMxP0nt0A2EsH3MAtLPjoNJ9/y6bfWqfO7TmdDEl0vpP1oHV5skaj4yV3hKt3FymRcDn2LIHNp5SpvmYgWqcVdDBlyuN64iQl07rPu1EiTgRQjXnZikQwnvClIzV3V7JZdBEAW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629616; c=relaxed/simple; bh=bBO0Iqelj8fza63egsoXJuy9RT4wZZVSdAOcjGFLV54=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=paoekKwzETUW5bQdITTdUx6dEwEbXB+s+REXMX71wrh8u16Lson5nREFpzgfSS6YSsEujeDXp8T1DY2z5aGc8HVMg8XCFhRUaL2K85/Qxdx0UDuN9ZjAcmEOxa5vFGB3SvFnsPe7Fh9NGYZzw+WIGozLCBkyjS5kxx+GfR/HovM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KcVhjvN2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KcVhjvN2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05AD4C4AF61; Thu, 2 May 2024 06:00:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714629615; bh=bBO0Iqelj8fza63egsoXJuy9RT4wZZVSdAOcjGFLV54=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KcVhjvN2Q/IIksoC2T2K7yei0Za/gHqV7wMwhTWBGaCBSyeoaG57Ii30mBmJ9WbwV Y4Y16Sazd9ilTIic/WJW3Gyfxoc3iU4pnXI8R4WRVDMkXwVUQpRw456ILHTP2PsL/S 3L4I5LA3SmBEJF+aC+ifJZUsUArPtf3Nia4Ye8ITNgvvJ0T/UsuQnWWS1+kVkTtz96 T6Pvq5XBxjDzCYombLd5K6dzK2Gklq45Ah7JhUV0uZ3pfT91/2tB8RGv6QQEfzMuBK n+HbjKb6VbbkMgO8PoFiN5vE4q/G0MMmu4rEm4ELFRd/yULipOt3soCKtmYsVhjM6Y TveN+kZdfXZuA== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org Subject: [PATCH 5/6] perf annotate-data: Handle multi regs in find_data_type_block() Date: Wed, 1 May 2024 23:00:10 -0700 Message-ID: <20240502060011.1838090-6-namhyung@kernel.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240502060011.1838090-1-namhyung@kernel.org> References: <20240502060011.1838090-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The instruction tracking should be the same for the both registers. Just do it once and compare the result with multi regs as with the previous patches. Then we don't need to call find_data_type_block() separately for each reg. Let's remove the 'reg' argument from the relevant functions. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 245e3ef3e2ff..68fe7999f033 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -1258,11 +1258,12 @@ static void setup_stack_canary(struct data_loc_info= *dloc) * are similar to global variables and no additional info is needed. */ static int check_matching_type(struct type_state *state, - struct data_loc_info *dloc, int reg, + struct data_loc_info *dloc, Dwarf_Die *cu_die, Dwarf_Die *type_die) { Dwarf_Word size; u32 insn_offset =3D dloc->ip - dloc->ms->sym->start; + int reg =3D dloc->op->reg1; =20 pr_debug_dtp("chk [%x] reg%d offset=3D%#x ok=3D%d kind=3D%d", insn_offset, reg, dloc->op->offset, @@ -1448,7 +1449,7 @@ static int check_matching_type(struct type_state *sta= te, } =20 /* Iterate instructions in basic blocks and update type table */ -static int find_data_type_insn(struct data_loc_info *dloc, int reg, +static int find_data_type_insn(struct data_loc_info *dloc, struct list_head *basic_blocks, struct die_var_type *var_types, Dwarf_Die *cu_die, Dwarf_Die *type_die) @@ -1481,7 +1482,7 @@ static int find_data_type_insn(struct data_loc_info *= dloc, int reg, update_var_state(&state, dloc, addr, dl->al.offset, var_types); =20 if (this_ip =3D=3D dloc->ip) { - ret =3D check_matching_type(&state, dloc, reg, + ret =3D check_matching_type(&state, dloc, cu_die, type_die); goto out; } @@ -1502,7 +1503,7 @@ static int find_data_type_insn(struct data_loc_info *= dloc, int reg, * Construct a list of basic blocks for each scope with variables and try = to find * the data type by updating a type state table through instructions. */ -static int find_data_type_block(struct data_loc_info *dloc, int reg, +static int find_data_type_block(struct data_loc_info *dloc, Dwarf_Die *cu_die, Dwarf_Die *scopes, int nr_scopes, Dwarf_Die *type_die) { @@ -1550,7 +1551,7 @@ static int find_data_type_block(struct data_loc_info = *dloc, int reg, fixup_var_address(var_types, start); =20 /* Find from start of this scope to the target instruction */ - found =3D find_data_type_insn(dloc, reg, &basic_blocks, var_types, + found =3D find_data_type_insn(dloc, &basic_blocks, var_types, cu_die, type_die); if (found > 0) { char buf[64]; @@ -1716,8 +1717,13 @@ static int find_data_type_die(struct data_loc_info *= dloc, Dwarf_Die *type_die) goto out; } =20 + if (loc->multi_regs && reg =3D=3D loc->reg1 && loc->reg1 !=3D loc->reg2) { + reg =3D loc->reg2; + goto retry; + } + if (reg !=3D DWARF_REG_PC) { - ret =3D find_data_type_block(dloc, reg, &cu_die, scopes, + ret =3D find_data_type_block(dloc, &cu_die, scopes, nr_scopes, type_die); if (ret =3D=3D 0) { ann_data_stat.insn_track++; @@ -1725,11 +1731,6 @@ static int find_data_type_die(struct data_loc_info *= dloc, Dwarf_Die *type_die) } } =20 - if (loc->multi_regs && reg =3D=3D loc->reg1 && loc->reg1 !=3D loc->reg2) { - reg =3D loc->reg2; - goto retry; - } - if (ret < 0) { pr_debug_dtp("no variable found\n"); ann_data_stat.no_var++; --=20 2.45.0.rc1.225.g2a3ae87e7f-goog From nobody Wed Dec 17 23:08:47 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42A9C36AFE; Thu, 2 May 2024 06:00:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629616; cv=none; b=XRQywEQcnbZcTlY+BnwepoJZFxpt6qYd3qDwmI9zhmbn5A9N/nLQRTTwSooIlHHaPmKY6Q/paqEXQDcU5mT0RUt1DQ+YwRpTyjviuZojyhboKeMcXGhwXUBcUa4k6YszaNEOJ7qyWksGMLWM2MMHnZWRnHtVVesU5557Hw5KW+w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714629616; c=relaxed/simple; bh=wBhNx/ENJ3dtDGzKbOpBhB0k32mIboB/9iNKvY4VhfM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cM1Ow5beb7dNMbXmVOtzdWG+ju13aoggIW+wunG7cF2BPdj8ffHdcK7csp/mgs78mNHvm4uhOtj2460/VRhhvmGMsiEoDweV3+SfTugYaqtNyMQs95yr4ngMyv+OtOOiVp4nNQl8/roGzBw/4ehLOGdIDNl1WbjZP/PT0e7iyyU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Hs5/Y1gp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Hs5/Y1gp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E0E4C4AF51; Thu, 2 May 2024 06:00:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714629615; bh=wBhNx/ENJ3dtDGzKbOpBhB0k32mIboB/9iNKvY4VhfM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hs5/Y1gpP3uEtaJBKNPkmeW54B0HZW29fmbLYQfgRdTIzAlU7pwW5SYG/ob3egHyH gXmEt7gK/0KqbWJZuWM5FFiglpXX4ajDpzzhlJI976gdpCh8LZGKxYCyu+3qFb53Ah GNCOSziTp/RR80ifewaD9o6rjkLPzZvr0UGYeUVus9lMv5jOSNowvgTwI9WDNgtIL8 OfU16zQwbIkxo0WpKdQ/W9axKErlQyoGuinKDK63TOyLLqgQArVx9UOpA8UXqJf1qy bkYuz4uSF9hfUBEjXKd5v0+18n6SQIkMFNaFud5kOXFGx/vk9aKHqQY9Hij/5Zols8 KFv90TiSXgUfg== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org Subject: [PATCH 6/6] perf annotate-data: Check kind of stack variables Date: Wed, 1 May 2024 23:00:11 -0700 Message-ID: <20240502060011.1838090-7-namhyung@kernel.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240502060011.1838090-1-namhyung@kernel.org> References: <20240502060011.1838090-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" I sometimes see ("unknown type") in the result and it was because it didn't check the type of stack variables properly during the instruction tracking. The stack can carry constant values (without type info) and if the target instruction is accessing the stack location, it resulted in the "unknown type". Maybe we could pick one of integer types for the constant, but it doesn't really mean anything useful. Let's just drop the stack slot if it doesn't have a valid type info. Here's an example how it got the unknown type. Note that 0xffffff48 =3D -0xb8. ----------------------------------------------------------- find data type for 0xffffff48(reg6) at ... CU for ... frame base: cfa=3D0 fbreg=3D6 scope: [2/2] (die:11cb97f) bb: [37 - 3a] var [37] reg15 type=3D'int' size=3D0x4 (die:0x1180633) bb: [40 - 4b] mov [40] imm=3D0x1 -> reg13 var [45] reg8 type=3D'sigset_t*' size=3D0x8 (die:0x11a39ee) mov [45] imm=3D0x1 -> reg2 <--- here reg2 has a cons= tant bb: [215 - 237] mov [218] reg2 -> -0xb8(stack) constant <--- and save it to the sta= ck mov [225] reg13 -> -0xc4(stack) constant call [22f] find_task_by_vgpid call [22f] return -> reg0 type=3D'struct task_struct*' size=3D0x8 (die:0x= 11881e8) bb: [5c8 - 5cf] bb: [2fb - 302] mov [2fb] -0xc4(stack) -> reg13 constant bb: [13b - 14d] mov [143] 0xd50(reg3) -> reg5 type=3D'struct task_struct*' size=3D0x8 (di= e:0xa31f3c) bb: [153 - 153] chk [153] reg6 offset=3D0xffffff48 ok=3D0 kind=3D0 fbreg <--- access h= ere found by insn track: 0xffffff48(reg6) type-offset=3D0 type=3D'G^KU' size=3D0 (die:0xffffffffffffffff) Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 68fe7999f033..2c98813f95cd 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -1314,6 +1314,9 @@ static int check_matching_type(struct type_state *sta= te, return -1; } =20 + if (stack->kind !=3D TSR_KIND_TYPE) + return 0; + *type_die =3D stack->type; /* Update the type offset from the start of slot */ dloc->type_offset -=3D stack->offset; @@ -1343,6 +1346,9 @@ static int check_matching_type(struct type_state *sta= te, return -1; } =20 + if (stack->kind !=3D TSR_KIND_TYPE) + return 0; + *type_die =3D stack->type; /* Update the type offset from the start of slot */ dloc->type_offset -=3D fboff + stack->offset; --=20 2.45.0.rc1.225.g2a3ae87e7f-goog