Linux Perf Users
 help / color / mirror / Atom feed
From: Tengda Wu <wutengda@huaweicloud.com>
To: sashiko-reviews@lists.linux.dev
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v4 23/23] perf annotate-arm64: Support 'mrs' instruction to track 'current' pointer
Date: Tue, 8 Sep 2026 20:54:06 +0800	[thread overview]
Message-ID: <8a17cd45-388b-4bdd-9389-018e53fe4957@huaweicloud.com> (raw)
In-Reply-To: <20260808132051.7F4261F000E9@smtp.kernel.org>



On 2026/8/8 21:20, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
> - [High] The global `task_struct_cache` may permanently cache a DWARF offset for an incomplete forward declaration of `struct task_struct`, breaking task tracking globally.
> - [High] The globally static `task_struct_cache` caches a raw `Dwarf *` pointer as a validation key, which is susceptible to an ABA problem when DWARF handles are recycled, leading to corrupt DWARF parsing.
> - [Medium] Calling `die_find_inlinefunc()` with a Compilation Unit DIE (`cu_die`) instead of a Subprogram DIE (`sp_die`) causes a severe O(N) recursive DWARF tree traversal.
> - [Medium] Caching the DWARF offset of `struct task_struct *` relies on a brittle non-deterministic order dependency, silently failing for compiler-generated code like stack canaries.
> - [Low] Missing explicit inclusion of `<strings.h>` for `strcasecmp()`, which violates subsystem guidelines and causes build failures on musl libc.
> --
> 
> --- Patch [23]: [PATCH v4 23/23] perf annotate-arm64: Support 'mrs' instruction to track 'current' pointer ---
> commit 9ea98019ec4e55f722077c51c12557f82e4d5f60
> Author: Tengda Wu <wutengda@huaweicloud.com>
> 
> perf annotate-arm64: Support 'mrs' instruction to track 'current' pointer
> 
> This commit extends the arm64 instruction state tracker to handle mrs
> instructions reading from sp_el0. It attempts to track the current
> task pointer by locating the get_current() inline function and caching
> the struct task_struct DWARF offset for subsequent resolution.
> 
>> diff --git a/tools/perf/util/annotate-arch/annotate-arm64.c b/tools/perf/util/annotate-arch/annotate-arm64.c
>> index b01fa6cf865ff..9d1f315dcb280 100644
>> --- a/tools/perf/util/annotate-arch/annotate-arm64.c
>> +++ b/tools/perf/util/annotate-arch/annotate-arm64.c
> 
> [ ... ]
> 
>> @@ -889,6 +890,96 @@ static void update_adrp_insn_state(struct type_state *state,
> 
> [ ... ]
> 
>> +	/* 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")) {
> 
> [Severity: Low]
> Is the <strings.h> header missing for strcasecmp()?
> 
> Building perf with musl libc requires explicit inclusion of POSIX headers,
> and omitting <strings.h> can cause musl build failures.
> 
>> +		invalidate_reg_state(tsr);
>> +		return;
>> +	}
> 
> [ ... ]
> 
>> +	/*
>> +	 * Invalidate cached DIE offset if the debug info handle changed
>> +	 * (e.g. switching between vmlinux and a kernel module).
>> +	 */
>> +	if (task_struct_cache.dbg != dloc->di->dbg) {
>> +		task_struct_cache.dieoff = 0;
>> +		task_struct_cache.dbg = dloc->di->dbg;
>> +	}
> 
> [Severity: High]
> Does checking the raw Dwarf pointer expose an ABA problem?
> 
> If debug info handles are freed and reallocated for different DSOs during
> analysis, the memory address could be recycled. This would bypass the cache
> invalidation and cause perf to use a stale DWARF offset from the old DSO
> on the new DSO, potentially leading to out-of-bounds reads in libdw.
> 
>> +
>> +	if (!task_struct_cache.dieoff ||
>> +	    !dwarf_offdie(dloc->di->dbg, task_struct_cache.dieoff, &type_die)) {
>> +		struct strbuf sb;
>> +		char *type_name;
>> +		/*
>> +		 * Find the inline function 'get_current()' Dwarf_Die
> 
> [Severity: Medium]
> Can this cache mechanism create a non-deterministic order dependency?
> 
> If the first mrs instruction processed is a stack canary, which does not
> belong to get_current(), the search will fail and the state is invalidated.
> 
> However, if a true get_current() is processed first, the cache is populated
> and the function check is bypassed for subsequent stack canaries, allowing
> them to succeed.
> 
>> +		 * and obtain its return value data type, which should
>> +		 * be 'struct task_struct*'.
>> +		 */
>> +		if (!die_find_inlinefunc(cu_die, pc, &func_die) ||
> 
> [Severity: Medium]
> Will passing the Compilation Unit DIE (cu_die) instead of a Subprogram
> DIE cause a severe performance regression?
> 
> Because the underlying callback returns DIE_FIND_CB_CONTINUE, passing
> the entire compilation unit forces die_find_inlinefunc() to perform a
> full, inefficient O(N) recursive DWARF AST walk across every node in the
> file when the cache is empty.
> 
>> +		    !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);
>> +			free(type_name);
>> +			return;
>> +		}
>> +
>> +		/*
>> +		 * Cache the 'struct task_struct*' die offset globally.
>> +		 * This allows us to resolve stack canary accesses even
>> +		 * in CUs that lack a full task_struct definition (e.g.,
>> +		 * compiler-generated entry/exit code).
>> +		 */
>> +		task_struct_cache.dieoff = dwarf_dieoffset(&type_die);
> 
> [Severity: High]
> Could this code permanently cache incomplete forward declarations of
> struct task_struct?
> 
> The string comparison matches "struct task_struct*", but there is no check
> to verify if the DIE is a complete definition (e.g., by checking
> DW_AT_declaration or dwarf_aggregate_size()).
> 
> If the first processed CU only has a forward declaration, its incomplete
> offset will be cached globally, causing all subsequent member offset
> lookups to fail since an incomplete type has no children.
> 
>> +		free(type_name);
>> +	}
> 

The cache was originally introduced to address stack_canary data type
identification, but it still has many issues at this point, so I are removing
it and will not support it for now.

Thanks,
Tengda


      reply	other threads:[~2026-09-08 12:54 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-08 12:23 [PATCH v4 00/23] perf arm64: Support data type profiling Tengda Wu
2026-08-08 12:23 ` [PATCH v4 01/23] perf capstone: Fix arm64 jump/adrp disassembly mismatch with objdump Tengda Wu
2026-08-10 13:08   ` Shuai Xue
2026-08-11  2:27     ` Tengda Wu
2026-08-11  3:25       ` Shuai Xue
2026-08-08 12:23 ` [PATCH v4 02/23] perf llvm: Fix arm64 adrp instruction " Tengda Wu
2026-08-08 13:03   ` sashiko-bot
2026-08-11  2:37     ` Tengda Wu
2026-08-08 12:23 ` [PATCH v4 03/23] perf annotate-arm64: Generalize arm64_mov__parse to support more instructions Tengda Wu
2026-08-08 13:05   ` sashiko-bot
2026-08-11  3:19     ` Tengda Wu
2026-08-08 12:23 ` [PATCH v4 04/23] perf annotate-arm64: Handle load and store instructions Tengda Wu
2026-08-08 13:07   ` sashiko-bot
2026-08-11  7:07     ` Tengda Wu
2026-08-08 12:23 ` [PATCH v4 05/23] perf dwarf-regs: Adapt get_dwarf_regnum() for arm64 Tengda Wu
2026-08-08 13:12   ` sashiko-bot
2026-08-11  7:18     ` Tengda Wu
2026-08-24 21:36       ` Namhyung Kim
2026-08-08 12:23 ` [PATCH v4 06/23] perf annotate: Adapt arch__dwarf_regnum() " Tengda Wu
2026-08-08 13:07   ` sashiko-bot
2026-08-11  8:10     ` Tengda Wu
2026-08-11  6:33   ` Shuai Xue
2026-08-11  8:16     ` Tengda Wu
2026-08-24 21:39       ` Namhyung Kim
2026-08-08 12:23 ` [PATCH v4 07/23] perf annotate: Introduce extract_op_location callback for arch-specific parsing Tengda Wu
2026-08-08 13:11   ` sashiko-bot
2026-08-12  1:44     ` Tengda Wu
2026-08-08 12:23 ` [PATCH v4 08/23] perf annotate-arm64: Implement extract_op_location() callback Tengda Wu
2026-08-11  6:50   ` Shuai Xue
2026-08-08 12:23 ` [PATCH v4 09/23] perf annotate: Deduplicate overlapping ARM SPE events for data type profiling Tengda Wu
2026-08-10  6:57   ` Adrian Hunter
2026-08-12  2:28     ` Tengda Wu
2026-08-12  6:07       ` Adrian Hunter
2026-08-12  8:53         ` Tengda Wu
2026-08-08 12:23 ` [PATCH v4 10/23] perf arm-spe: Set default synthesized event period to 1 Tengda Wu
2026-08-08 12:23 ` [PATCH v4 11/23] perf annotate-data: Extract invalidate_reg_state() as a common helper Tengda Wu
2026-08-11  7:09   ` Shuai Xue
2026-08-08 12:23 ` [PATCH v4 12/23] perf annotate-arm64: Enable instruction tracking support Tengda Wu
2026-08-08 13:22   ` sashiko-bot
2026-08-12  3:09     ` Tengda Wu
2026-08-08 12:23 ` [PATCH v4 13/23] perf annotate-arm64: Track return type after call instructions Tengda Wu
2026-08-08 13:05   ` sashiko-bot
2026-08-08 12:23 ` [PATCH v4 14/23] perf annotate-arm64: Support load instruction tracking Tengda Wu
2026-08-08 13:08   ` sashiko-bot
2026-08-13 14:44     ` Tengda Wu
2026-08-11  7:36   ` Shuai Xue
2026-08-13 14:54     ` Tengda Wu
2026-08-24 21:44       ` Namhyung Kim
2026-08-08 12:23 ` [PATCH v4 15/23] perf annotate-arm64: Support store " Tengda Wu
2026-08-08 13:11   ` sashiko-bot
2026-08-14  1:37     ` Tengda Wu
2026-08-11  8:00   ` Shuai Xue
2026-08-14  1:26     ` Tengda Wu
2026-08-08 12:23 ` [PATCH v4 16/23] perf annotate-data: Expand type_state_reg imm_value to u64 Tengda Wu
2026-08-08 13:17   ` sashiko-bot
2026-08-11  8:10   ` Shuai Xue
2026-08-14  7:58     ` Tengda Wu
2026-08-24 21:48       ` Namhyung Kim
2026-08-08 12:23 ` [PATCH v4 17/23] perf annotate-data: Track imm_value for stack variables Tengda Wu
2026-08-11  8:16   ` Shuai Xue
2026-08-08 12:23 ` [PATCH v4 18/23] perf annotate-arm64: Support stack variable tracking Tengda Wu
2026-08-08 13:25   ` sashiko-bot
2026-08-11  8:24   ` Shuai Xue
2026-08-14 11:44     ` Tengda Wu
2026-08-08 12:23 ` [PATCH v4 19/23] perf annotate-arm64: Support 'mov' instruction tracking Tengda Wu
2026-08-08 13:20   ` sashiko-bot
2026-08-11  8:37   ` Shuai Xue
2026-08-18  8:59     ` Tengda Wu
2026-08-08 12:23 ` [PATCH v4 20/23] perf annotate-arm64: Support 'add' " Tengda Wu
2026-08-08 13:14   ` sashiko-bot
2026-08-11  8:45   ` Shuai Xue
2026-08-19  3:04     ` Tengda Wu
2026-08-24 21:58       ` Namhyung Kim
2026-08-28  7:41         ` Tengda Wu
2026-08-08 12:23 ` [PATCH v4 21/23] perf annotate-arm64: Support 'adrp' instruction to track global variables Tengda Wu
2026-08-11  8:50   ` Shuai Xue
2026-08-08 12:23 ` [PATCH v4 22/23] perf annotate-arm64: Support per-cpu variable access tracking Tengda Wu
2026-08-08 13:18   ` sashiko-bot
2026-08-31 13:07     ` Tengda Wu
2026-08-08 12:24 ` [PATCH v4 23/23] perf annotate-arm64: Support 'mrs' instruction to track 'current' pointer Tengda Wu
2026-08-08 13:20   ` sashiko-bot
2026-09-08 12:54     ` Tengda Wu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8a17cd45-388b-4bdd-9389-018e53fe4957@huaweicloud.com \
    --to=wutengda@huaweicloud.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox