From nobody Fri Sep 25 10:37:42 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 CB52F3793B3; Mon, 14 Sep 2026 06:45:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789368340; cv=none; b=FnEW4W2uz9uzntmaLYgkwz0dO7Fa/U/0/kpjQ4JtLLhkOUZ57X1NgdqrRSXdA67visnn74adI4Odwq7SNYTYWnQ0jVUACFtUa4hdcDMBHTdKAm/Spfpoaf+GTn1FGdyeU1cUzfpp4e8rWJ/MBsUpyw9MV4VuMMjO4Jgj3x9N3gY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789368340; c=relaxed/simple; bh=Mttz1aZ/SxLb5zaGr/Ll+RW1mzydNrOt5oRqQc66Zq4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=me8z1DNMBC057bB/YukYb+RjJFGnFJXhyZ+b8OGp/LwGS3Q7HJpGBYoS7WW4aPKlR/fhxF0AS1xSLKiS6WoaUn7wZhtj6kHQx0leK3p/pTQjLP1HdqYc4H+Tw3a6eDj0FT226Z9HHqOvbvWRO5W3SCAJpi0AQ91/wW0j1/v7UlI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QoC/KYvm; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QoC/KYvm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 478341F00893; Mon, 14 Sep 2026 06:45:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789368338; bh=VXQG48QPP2TU0Wh8rrcVmUbq1RqmviBSiisLHe3B1oA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QoC/KYvmimHw+ayEqJ2DLN442U7uazFLe3ypDgqdVCSUYkzMOFjALJInpcAxkJ4R5 1rlRgSPU0Zub8pIMRRhO9UnzJMDvrHo3egW8dd1ml/25iFB3o0S+x4qfEYK06HkRht E6QOo0W8lF5ZFAompj7w0il56MbOdBoJHOKjCeNOrTldb+MxigB/JiRE/I7owPOEmu LmBzNOu7Dtye+t82oW48+Fk9hUfdTxtAtTVMF+79Q+ptUfFYtPPjSuctJK2MA9dLVs kcfkU29yGAhkNkcblV0Ct73oThcQworiu65z3DtTSTzrXNIDr9c4fHeEtoKOwQ4594 639GTk4P8hgDw== From: Namhyung Kim To: Arnaldo Carvalho de Melo Cc: Ian Rogers , Jiri Olsa , Adrian Hunter , James Clark , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Zecheng Li , Yanbo Zhao , Tengda Wu , Shuai Xue Subject: [PATCH v2 1/4] perf annotate-data: Convert type histogram to hashmap Date: Sun, 13 Sep 2026 23:45:32 -0700 Message-ID: <20260914064535.1671939-2-namhyung@kernel.org> X-Mailer: git-send-email 2.55.0.1032.g73a4cd73de-goog In-Reply-To: <20260914064535.1671939-1-namhyung@kernel.org> References: <20260914064535.1671939-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 type histogram maintains sample counts and periods per offset. Use a hashmap instead of an array to reduce the memory overhead. No functional changes intended. Signed-off-by: Namhyung Kim Reviewed-by: Tengda Wu --- tools/perf/ui/browsers/annotate-data.c | 10 ++-- tools/perf/util/annotate-data.c | 72 ++++++++++++++++---------- tools/perf/util/annotate-data.h | 8 +-- 3 files changed, 57 insertions(+), 33 deletions(-) diff --git a/tools/perf/ui/browsers/annotate-data.c b/tools/perf/ui/browser= s/annotate-data.c index aa8c89fe2e82c1c5..c6e07a9b64089ab5 100644 --- a/tools/perf/ui/browsers/annotate-data.c +++ b/tools/perf/ui/browsers/annotate-data.c @@ -62,12 +62,16 @@ static int get_member_overhead(struct annotated_data_ty= pe *adt, =20 k =3D 0; for_each_group_evsel(evsel, leader) { + struct type_hist_entry *hist; + if (symbol_conf.skip_empty && evsel__hists(evsel)->stats.nr_samples =3D=3D 0) continue; =20 - h =3D adt->histograms[evsel->core.idx]; - update_hist_entry(&entry->hists[k++], &h->addr[offset]); + h =3D &adt->histograms[evsel->core.idx]; + if (hashmap__find(&h->samples, offset, &hist)) + update_hist_entry(&entry->hists[k], hist); + k++; } } return 0; @@ -416,7 +420,7 @@ static void browser__write(struct ui_browser *uib, void= *entry, int row) =20 /* print the number */ for_each_group_evsel(evsel, leader) { - struct type_hist *h =3D adt->histograms[evsel->core.idx]; + struct type_hist *h =3D &adt->histograms[evsel->core.idx]; =20 if (symbol_conf.skip_empty && evsel__hists(evsel)->stats.nr_samples =3D=3D 0) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 4e4c587640823c81..aff60a630fd05b01 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -1750,42 +1750,45 @@ struct annotated_data_type *find_data_type(struct d= ata_loc_info *dloc) return dso__findnew_data_type(dso, &type_die); } =20 +static size_t data_type_hash(long key, void *ctx __maybe_unused) +{ + return key; +} + +static bool data_type_equal(long key1, long key2, void *ctx __maybe_unused) +{ + return key1 =3D=3D key2; +} + static int alloc_data_type_histograms(struct annotated_data_type *adt, int= nr_entries) { int i; - size_t sz =3D sizeof(struct type_hist); =20 - sz +=3D sizeof(struct type_hist_entry) * adt->self.size; - - /* Allocate a table of pointers for each event */ + /* Allocate a histogram for each event */ adt->histograms =3D calloc(nr_entries, sizeof(*adt->histograms)); if (adt->histograms =3D=3D NULL) return -ENOMEM; =20 - /* - * Each histogram is allocated for the whole size of the type. - * TODO: Probably we can move the histogram to members. - */ for (i =3D 0; i < nr_entries; i++) { - adt->histograms[i] =3D zalloc(sz); - if (adt->histograms[i] =3D=3D NULL) - goto err; + hashmap__init(&adt->histograms[i].samples, data_type_hash, + data_type_equal, /*ctx=3D*/NULL); } =20 adt->nr_histograms =3D nr_entries; return 0; - -err: - while (--i >=3D 0) - zfree(&(adt->histograms[i])); - zfree(&adt->histograms); - return -ENOMEM; } =20 static void delete_data_type_histograms(struct annotated_data_type *adt) { - for (int i =3D 0; i < adt->nr_histograms; i++) - zfree(&(adt->histograms[i])); + for (int i =3D 0; i < adt->nr_histograms; i++) { + struct hashmap *map =3D &adt->histograms[i].samples; + struct hashmap_entry *pos, *tmp; + size_t bkt; + + hashmap__for_each_entry_safe(map, pos, tmp, bkt) + free(pos->pvalue); + hashmap__clear(map); + } =20 zfree(&adt->histograms); adt->nr_histograms =3D 0; @@ -1824,6 +1827,7 @@ int annotated_data_type__update_samples(struct annota= ted_data_type *adt, int nr_samples, u64 period) { struct type_hist *h; + struct type_hist_entry *entry; =20 if (adt =3D=3D NULL) return 0; @@ -1838,12 +1842,23 @@ int annotated_data_type__update_samples(struct anno= tated_data_type *adt, if (offset < 0 || offset >=3D adt->self.size) return -1; =20 - h =3D adt->histograms[evsel->core.idx]; + h =3D &adt->histograms[evsel->core.idx]; =20 h->nr_samples +=3D nr_samples; - h->addr[offset].nr_samples +=3D nr_samples; h->period +=3D period; - h->addr[offset].period +=3D period; + + if (!hashmap__find(&h->samples, offset, &entry)) { + entry =3D zalloc(sizeof(*entry)); + if (entry =3D=3D NULL) + return -1; + + if (hashmap__append(&h->samples, offset, entry) < 0) { + free(entry); + return -1; + } + } + entry->nr_samples +=3D nr_samples; + entry->period +=3D period; return 0; } =20 @@ -1911,14 +1926,14 @@ static void print_annotated_data_type(struct annota= ted_data_type *mem_type, struct evsel *evsel, int indent) { struct annotated_member *child; - struct type_hist *h =3D mem_type->histograms[evsel->core.idx]; + struct type_hist *h; int i, nr_events =3D 0, samples =3D 0; u64 period =3D 0; int width =3D symbol_conf.show_total_period ? 11 : 7; struct evsel *pos; =20 for_each_group_evsel(pos, evsel) { - h =3D mem_type->histograms[pos->core.idx]; + h =3D &mem_type->histograms[pos->core.idx]; =20 if (symbol_conf.skip_empty && evsel__hists(pos)->stats.nr_samples =3D=3D 0) @@ -1927,8 +1942,13 @@ static void print_annotated_data_type(struct annotat= ed_data_type *mem_type, samples =3D 0; period =3D 0; for (i =3D 0; i < member->size; i++) { - samples +=3D h->addr[member->offset + i].nr_samples; - period +=3D h->addr[member->offset + i].period; + struct type_hist_entry *entry; + + if (!hashmap__find(&h->samples, member->offset + i, &entry)) + continue; + + samples +=3D entry->nr_samples; + period +=3D entry->period; } print_annotated_data_value(h, period, samples); nr_events++; diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-dat= a.h index c26130744260955f..ca2096a9ee62cbfe 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -73,12 +73,12 @@ struct type_hist_entry { * struct type_hist - Type histogram for each event * @nr_samples: Total number of samples in this data type * @period: Total count of the event in this data type - * @offset: Array of histogram entry + * @samples: Hashmap of (offset, type_hist_entry) */ struct type_hist { u64 nr_samples; u64 period; - struct type_hist_entry addr[]; + struct hashmap samples; }; =20 /** @@ -86,7 +86,7 @@ struct type_hist { * @node: RB-tree node for dso->type_tree * @self: Actual type information * @nr_histogram: Number of histogram entries - * @histograms: An array of pointers to histograms + * @histograms: An array of histograms * * This represents a data type accessed by samples in the profile data. */ @@ -94,7 +94,7 @@ struct annotated_data_type { struct rb_node node; struct annotated_member self; int nr_histograms; - struct type_hist **histograms; + struct type_hist *histograms; }; =20 extern struct annotated_data_type unknown_type; --=20 2.55.0.1032.g73a4cd73de-goog From nobody Fri Sep 25 10:37:42 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 B942537DE9B; Mon, 14 Sep 2026 06:45:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789368340; cv=none; b=ksCrapEQYs2Hx6T6wX0jgmjaLq+vms4TNHxvYz4C4BTqTRLTAzkrMYzY1wxEeO/QkUFOFZ0L3NutgSOlVlSVny/MUkkAfQLVHSXjXOw4kKs2G9CWsIiwBQtGyYvWHnb5Blat+msEVxTZcXaxVkJ3BC81sjRdSS97E2GWwexWrog= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789368340; c=relaxed/simple; bh=MGJ9Ucc4PWEmh8U7ndSvxUkdhbYFs9vV1DhMZpv+ssM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Fn6lqpKHZhbpLHDV01MUU4yTvHQj60hzobenn4GUqXa3Uy11vxaX010F1TjeqgZU5tkmX9dBkPfGBJJti6B41Q72IqXRDnDYXCymiNbnSHKVm0ywFtMuS9HCS4T4OeNJrCrtH+vPYetf8fItI0IK+iJhn3XLNImCpiam+Ue4WVg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lw1GWC4H; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lw1GWC4H" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D82071F00898; Mon, 14 Sep 2026 06:45:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789368339; bh=ymdYKBbVAY1WyUZy4e/FNcP10X3wEv3fOcy73P04PFw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lw1GWC4HGoazzLOzbso/NpzFMV8qeociYKYcm8AGThhuX24PlapEs2eAVRCVOGeOk eMOsdbIMF3MSLk6/MBwgyB3yDTHU+6ZSCI/I4d8sBrFgFoJeQM6yu2x8a54Ew8xRU0 Zbj9H1QWOjiinrN5kYahZMneIFCPk2pItDagnCXpLmpiVbmlFkKhBqrW9IjslSPzqi Rg6FRv5RxhzRAUSljbONzeWZAUgl6H2MRkN+wKx11wqf7warTeEsZDcWn+huDPwD9a gIpP0GLTrBB4uzW4IvieSA842bG+YhnmJQy3Lkzqr79OSzJ1gH24Nc+/SkdXYCM1SW RqmVVIxMZ/T7g== From: Namhyung Kim To: Arnaldo Carvalho de Melo Cc: Ian Rogers , Jiri Olsa , Adrian Hunter , James Clark , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Zecheng Li , Yanbo Zhao , Tengda Wu , Shuai Xue , Masami Hiramatsu Subject: [PATCH v2 2/4] perf dwarf-aux: Add die_has_flex_array() helper Date: Sun, 13 Sep 2026 23:45:33 -0700 Message-ID: <20260914064535.1671939-3-namhyung@kernel.org> X-Mailer: git-send-email 2.55.0.1032.g73a4cd73de-goog In-Reply-To: <20260914064535.1671939-1-namhyung@kernel.org> References: <20260914064535.1671939-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 die_has_flex_array() returns true when the given type is a compound type and contains an array at the end. To prevent an infinite recursion add a depth field to the internal function. Cc: Masami Hiramatsu Signed-off-by: Namhyung Kim --- tools/perf/util/dwarf-aux.c | 77 +++++++++++++++++++++++++++++++++++++ tools/perf/util/dwarf-aux.h | 3 ++ 2 files changed, 80 insertions(+) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index d7160f87ac7d7ab3..465824e6513eb0ac 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -2180,3 +2180,80 @@ Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, in= t offset, =20 return die_get_member_type(&type_die, offset, die_mem); } + +static bool is_flex_array_member(Dwarf_Die *mb_die) +{ + Dwarf_Die type_die; + Dwarf_Word size; + + /* get the type of the member */ + if (die_get_real_type(mb_die, &type_die) =3D=3D NULL) + return false; + + if (dwarf_tag(&type_die) !=3D DW_TAG_array_type) + return false; + + return dwarf_aggregate_size(&type_die, &size) < 0 || size =3D=3D 0; +} + +#define MAX_FLEX_ARRAY_RECURSION 256 /* arbitrary */ + +static bool die_has_flex_array_recurse(Dwarf_Die *parent_die, int depth) +{ + Dwarf_Die die_mem, last_mb; + int tag =3D dwarf_tag(parent_die); + + if (tag !=3D DW_TAG_structure_type && tag !=3D DW_TAG_union_type) + return false; + + /* prevent infinite recursion */ + if (depth > MAX_FLEX_ARRAY_RECURSION) + return false; + + if (dwarf_child(parent_die, &die_mem)) + return false; + + do { + if (dwarf_tag(&die_mem) !=3D DW_TAG_member) + continue; + + if (tag =3D=3D DW_TAG_union_type) { + if (is_flex_array_member(&die_mem)) + return true; + + if (die_get_real_type(&die_mem, &last_mb) && + die_has_flex_array_recurse(&last_mb, depth + 1)) + return true; + } + + if (tag =3D=3D DW_TAG_structure_type) + memcpy(&last_mb, &die_mem, sizeof(last_mb)); + + } while (dwarf_siblingof(&die_mem, &die_mem) =3D=3D 0); + + if (tag =3D=3D DW_TAG_structure_type) { + if (is_flex_array_member(&last_mb)) + return true; + + if (die_get_real_type(&last_mb, &die_mem)) + return die_has_flex_array_recurse(&die_mem, depth + 1); + } + + return false; +} + +/** + * die_has_flex_array - Check if the given type has a flex-array at the end + * @parent_die: a pointer to type DIE + * + * This function returns %true iff @parent_die is a struct type and has an + * array at the end. Note that the flex-array has no element, it should h= ave + * no size and the parent size doesn't include the flex-array. So it shou= ld + * locate at the offset of the parent size. + * + * For simplicity, it assumes the parent size of aligned with the flex-arr= ay. + */ +bool die_has_flex_array(Dwarf_Die *parent_die) +{ + return die_has_flex_array_recurse(parent_die, 0); +} diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index 161f0bf980b6ee6a..9b662db710220522 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -189,4 +189,7 @@ void die_collect_global_vars(Dwarf_Die *cu_die, struct = die_var_type **var_types) /* Get the frame base information from CFA */ int die_get_cfa(Dwarf *dwarf, u64 pc, int *preg, int *poffset); =20 +/* Check whether given type has a flex array */ +bool die_has_flex_array(Dwarf_Die *parent_die); + #endif /* _DWARF_AUX_H */ --=20 2.55.0.1032.g73a4cd73de-goog From nobody Fri Sep 25 10:37:42 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 6515C380FDF; Mon, 14 Sep 2026 06:45:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789368341; cv=none; b=j4oD/klA/oxujH16YdLUhg3LKY0nzczmTvIE2LimlxK1QNDjuH7ApZ5Ty9xfJtJPbyL7d7MrwJKOVF6wSpuRiomKrQDeRdU49f+u+ymrh52h8io2EzqvlJF7SNlk4zJxZwsaGrFJN/tEeGLGaKKjKMiMsYwYYcyKEEOraGeldDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789368341; c=relaxed/simple; bh=8L3Xi2JZtbV64DEaag63/pchEoZ4oo8EegXOBp5L5Z8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uLDGzr+kc6wHZlhs1VOvwRaYUzMHRiUHdgaof8qMjX3VbE3GSblts27/hYFyjixWIAWgzruMgQG32U4xXiHuvb2GBNTnGqlVsphw+vDXziQxSp7g8n1oah98na3X2zH8SW/kKG7b7gzr/Ys/M8prJ3WZl0xiVeWxHaIHThIKGfI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TTTC7v8T; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TTTC7v8T" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7DC2B1F00899; Mon, 14 Sep 2026 06:45:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789368340; bh=/O6Fs7ytd6i1gURERsPHv6GrvClVL/uDJNVlPhWfnuM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TTTC7v8TxoYc1nuz2LdDvbNiJLibhDlViNLxwzSxarRxd0XVIKynoImuIjgIX6loQ /Gq6NRoc5skXhiLkS/2fEDGD0sGUlnQt6iGyeDFMF10juR7CcbRU/kXX8ncrKpjTGO AfwtXwk++qbov3PKIRWMC8Ag2Seq5N43uwkaxGveTRdfsAp0sUGOyEglUU3gdLdgaR 9CEYIcbsem4WVUsFXWjMtYeZQAuPLChPjwdBq2JFd+WXSvE+sUpJqA0LFEUGv5KacX 0p7cf6qfEpA/dN3Al6+yLiNHuwzEK4VZ9A/BfLUjsDdcy9yiaWM8HsOTybEnLjZovu z1mlLZVkuGzPA== From: Namhyung Kim To: Arnaldo Carvalho de Melo Cc: Ian Rogers , Jiri Olsa , Adrian Hunter , James Clark , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Zecheng Li , Yanbo Zhao , Tengda Wu , Shuai Xue Subject: [PATCH v2 3/4] perf annotate-date: Allow out-of-size access for flex-array types Date: Sun, 13 Sep 2026 23:45:34 -0700 Message-ID: <20260914064535.1671939-4-namhyung@kernel.org> X-Mailer: git-send-email 2.55.0.1032.g73a4cd73de-goog In-Reply-To: <20260914064535.1671939-1-namhyung@kernel.org> References: <20260914064535.1671939-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" Structs that have a flex array will have accesses beyond its original size as the array was declared as 0 sized. For now, it just allow any offset bigger than the size. It could be refined later. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 63 ++++++++++++++++++--------------- tools/perf/util/annotate-data.h | 2 ++ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index aff60a630fd05b01..e8aff6a916c0afe0 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include =20 @@ -248,8 +249,15 @@ static int __add_member_cb(Dwarf_Die *die, void *arg) else die_mem =3D member_type; =20 - if (dwarf_aggregate_size(&die_mem, &size) < 0) - size =3D 0; + if (dwarf_aggregate_size(&die_mem, &size) < 0) { + if (dwarf_tag(&die_mem) =3D=3D DW_TAG_array_type) { /* flex-array? */ + die_get_real_type(&die_mem, &die_mem); + if (dwarf_aggregate_size(&die_mem, &size) < 0) + size =3D 0; + } else { + size =3D 0; + } + } =20 if (dwarf_attr_integrate(die, DW_AT_data_member_location, &attr)) { if (dwarf_formudata(&attr, &loc) !=3D 0) { @@ -399,6 +407,7 @@ static struct annotated_data_type *dso__findnew_data_ty= pe(struct dso *dso, result->self.type_name =3D type_name; result->self.size =3D size; INIT_LIST_HEAD(&result->self.children); + result->flex_array =3D die_has_flex_array(type_die); =20 if (symbol_conf.annotate_data_member) add_member_types(result, type_die); @@ -517,13 +526,30 @@ static bool is_better_type(Dwarf_Die *type_a, Dwarf_D= ie *type_b) return false; } =20 +static enum type_match_result check_type_offset(Dwarf_Die *type_die, int o= ffset) +{ + Dwarf_Word size; + + /* Get the size of the actual type */ + if (dwarf_aggregate_size(type_die, &size) < 0) + return PERF_TMR_NO_SIZE; + + /* Minimal sanity check */ + if (offset < 0) + return PERF_TMR_BAD_OFFSET; + + if ((unsigned)offset >=3D size && !die_has_flex_array(type_die)) + return PERF_TMR_BAD_OFFSET; + + return PERF_TMR_OK; +} + /* The type info will be saved in @type_die */ static enum type_match_result check_variable(struct data_loc_info *dloc, Dwarf_Die *var_die, Dwarf_Die *type_die, int reg, int offset, bool is_fbreg) { - Dwarf_Word size; bool needs_pointer =3D true; Dwarf_Die sized_type; =20 @@ -554,15 +580,7 @@ static enum type_match_result check_variable(struct da= ta_loc_info *dloc, else sized_type =3D *type_die; =20 - /* Get the size of the actual type */ - if (dwarf_aggregate_size(&sized_type, &size) < 0) - return PERF_TMR_NO_SIZE; - - /* Minimal sanity check */ - if ((unsigned)offset >=3D size) - return PERF_TMR_BAD_OFFSET; - - return PERF_TMR_OK; + return check_type_offset(type_die, offset); } =20 struct type_state_stack *find_stack_state(struct type_state *state, @@ -1112,7 +1130,6 @@ static enum type_match_result check_matching_type(str= uct type_state *state, struct disasm_line *dl, Dwarf_Die *type_die) { - Dwarf_Word size; u32 insn_offset =3D dl->al.offset; int reg =3D dloc->op->reg1; int offset =3D dloc->op->offset; @@ -1166,12 +1183,7 @@ static enum type_match_result check_matching_type(st= ruct type_state *state, else sized_type =3D *type_die; =20 - /* Get the size of the actual type */ - if (dwarf_aggregate_size(&sized_type, &size) < 0 || - (unsigned)dloc->type_offset >=3D size) - return PERF_TMR_BAD_OFFSET; - - return PERF_TMR_OK; + return check_type_offset(&sized_type, dloc->type_offset); } =20 if (state->regs[reg].kind =3D=3D TSR_KIND_POINTER) { @@ -1190,12 +1202,7 @@ static enum type_match_result check_matching_type(st= ruct type_state *state, =20 dloc->type_offset =3D dloc->op->offset + state->regs[reg].offset; =20 - /* Get the size of the actual type */ - if (dwarf_aggregate_size(type_die, &size) < 0 || - (unsigned)dloc->type_offset >=3D size) - return PERF_TMR_BAD_OFFSET; - - return PERF_TMR_OK; + return check_type_offset(type_die, dloc->type_offset); } =20 if (state->regs[reg].kind =3D=3D TSR_KIND_PERCPU_POINTER) { @@ -1209,9 +1216,7 @@ static enum type_match_result check_matching_type(str= uct type_state *state, =20 dloc->type_offset =3D dloc->op->offset; =20 - /* Get the size of the actual type */ - if (dwarf_aggregate_size(type_die, &size) < 0 || - (unsigned)dloc->type_offset >=3D size) + if (check_type_offset(type_die, dloc->type_offset) !=3D PERF_TMR_OK) return PERF_TMR_BAIL_OUT; =20 return PERF_TMR_OK; @@ -1839,7 +1844,7 @@ int annotated_data_type__update_samples(struct annota= ted_data_type *adt, return -1; } =20 - if (offset < 0 || offset >=3D adt->self.size) + if (offset < 0 || (offset >=3D adt->self.size && !adt->flex_array)) return -1; =20 h =3D &adt->histograms[evsel->core.idx]; diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-dat= a.h index ca2096a9ee62cbfe..27b7148b64f60350 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -85,6 +85,7 @@ struct type_hist { * struct annotated_data_type - Data type to profile * @node: RB-tree node for dso->type_tree * @self: Actual type information + * @flex_array: Whethere it has a flex array * @nr_histogram: Number of histogram entries * @histograms: An array of histograms * @@ -93,6 +94,7 @@ struct type_hist { struct annotated_data_type { struct rb_node node; struct annotated_member self; + bool flex_array; int nr_histograms; struct type_hist *histograms; }; --=20 2.55.0.1032.g73a4cd73de-goog From nobody Fri Sep 25 10:37:42 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 A023B3822A3; Mon, 14 Sep 2026 06:45:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789368342; cv=none; b=PdE1x1ywIG0QfDavsXbatMiripB/lFNAJ/d+fhOCfh4+xBJa2gs/2WeYUgDZb48LzihAVFLHfW78CJSTuUH8/fatn5vmtfGWs7wWDfkd5KejBRa0N/M57rCJBbV4M21BJcpvEI8V0ShDcrGstX+YNf6ORmRkab8I9dhhClPCZQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789368342; c=relaxed/simple; bh=xcvF5DwxsirA0Wfh3x+KUC6C6aLKVTKzqBD70+B19Mo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r1JmiItrGDPykkAYypf6at8vdd3WavV1l2Le1fjQyl2p/XLc4auCtmKRiZDme8HbGDyTUX+TdE0JWtUeLcPaMPbXtz8ynHN2Hrl9PNR8+rucO2h6ETOVeyJY6Q2d5CU3BV5hFOR1cXqqXxHu0G+ttZGUD3p3Fq49RkGn53Qa6zw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kzuvEsaJ; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kzuvEsaJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1CAE61F0089C; Mon, 14 Sep 2026 06:45:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789368340; bh=4nt8l8m6QQOaS7ZxE7D5yfx1hTVC4vG/+Gwe9/9l1Rc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kzuvEsaJSHnu31fLmLun+nxtrRZFv95G/UikjkDyOuJqMlp3olu6q6LVLIbmHpCeO idMeU3tWuG27fGpdVmtgRbdnNVShEz925/BJXAm96xwBdKUocbuUGjTYCkmSIMKaGd 4WVw3Hl3rti0KR2lUDD3lN8d60fDnNVO+DW1QlVwpXY4YM+LZkB5isigyLdLkYlxhR VzNxoih8+tAMhgsAibB0lnmfRr8O9pocuTrT3rAiiSICbA5aNEJUMnfRZniuamIsgO dnbR5cb4raIVF1DEqaLETaHjXWiQSEKPxNtU+Aa2AnaYbzxM13FNIVUUE/VJ6Yfx6J jsDWqTfKEZEbQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo Cc: Ian Rogers , Jiri Olsa , Adrian Hunter , James Clark , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Zecheng Li , Yanbo Zhao , Tengda Wu , Shuai Xue Subject: [PATCH v2 4/4] perf annotate-data: Adjust type offset for flex-array Date: Sun, 13 Sep 2026 23:45:35 -0700 Message-ID: <20260914064535.1671939-5-namhyung@kernel.org> X-Mailer: git-send-email 2.55.0.1032.g73a4cd73de-goog In-Reply-To: <20260914064535.1671939-1-namhyung@kernel.org> References: <20260914064535.1671939-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 flex array members are located beyond the original type size. Also it needs to adjust the offset in an array to find a corresponding element using module operation. Note that we focus on access to type and field, so array index is not important. Make sure to find a field name for flex arrays. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 74 ++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index e8aff6a916c0afe0..a66b72feb6e2f227 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -333,19 +333,40 @@ static void delete_members(struct annotated_member *m= ember) } =20 static int fill_member_name(char *buf, size_t sz, struct annotated_member = *m, - int offset, bool first) + int offset, bool first, bool has_flex_array) { struct annotated_member *child; + bool found =3D false; + int len; =20 if (list_empty(&m->children)) return 0; =20 list_for_each_entry(child, &m->children, node) { - int len; - if (offset < child->offset || offset >=3D child->offset + child->size) continue; =20 + found =3D true; + break; + } + + if (!found && has_flex_array) { + /* + * It may have an intermediate struct that has another struct that + * contains a flex array. In that case, the outer struct itself is + * has no array and the size is less than the offset so the above + * logic won't find the outer struct at the offset. Let's use the + * last struct if it couldn't find a member for the flex array. + */ + child =3D list_last_entry(&m->children, struct annotated_member, node); + + if (offset < child->offset) + return 0; + + found =3D true; + } + + if (found) { /* It can have anonymous struct/union members */ if (child->var_name) { len =3D scnprintf(buf, sz, "%s%s", @@ -355,15 +376,37 @@ static int fill_member_name(char *buf, size_t sz, str= uct annotated_member *m, len =3D 0; } =20 - return fill_member_name(buf + len, sz - len, child, offset, first) + len; + return fill_member_name(buf + len, sz - len, child, offset, first, + has_flex_array) + len; } + return 0; } =20 int annotated_data_type__get_member_name(struct annotated_data_type *adt, char *buf, size_t sz, int member_offset) { - return fill_member_name(buf, sz, &adt->self, member_offset, /*first=3D*/t= rue); + return fill_member_name(buf, sz, &adt->self, member_offset, /*first=3D*/t= rue, + adt->flex_array); +} + +static struct annotated_member *find_flex_array(struct annotated_member *m) +{ + struct annotated_member *child; + + if (list_empty(&m->children)) + return NULL; + + child =3D list_last_entry(&m->children, struct annotated_member, node); + if (strstr(child->type_name, "[]")) + return child; + + return find_flex_array(child); +} + +static struct annotated_member *get_flex_array_member(struct annotated_dat= a_type *adt) +{ + return find_flex_array(&adt->self); } =20 static struct annotated_data_type *dso__findnew_data_type(struct dso *dso, @@ -1740,6 +1783,7 @@ struct annotated_data_type *find_data_type(struct dat= a_loc_info *dloc) { struct dso *dso =3D map__dso(dloc->ms->map); Dwarf_Die type_die; + struct annotated_data_type *result; =20 /* * The type offset is the same as instruction offset by default. @@ -1752,7 +1796,25 @@ struct annotated_data_type *find_data_type(struct da= ta_loc_info *dloc) if (find_data_type_die(dloc, &type_die) < 0) return NULL; =20 - return dso__findnew_data_type(dso, &type_die); + result =3D dso__findnew_data_type(dso, &type_die); + if (result =3D=3D NULL) + return NULL; + + if (result->flex_array && dloc->type_offset > result->self.size) { + struct annotated_member *flex_array =3D get_flex_array_member(result); + + if (flex_array && flex_array->size > 0) { + int offset =3D dloc->type_offset; + + /* adjust offset in the flex array */ + offset -=3D flex_array->offset; + offset %=3D flex_array->size; + offset +=3D flex_array->offset; + + dloc->type_offset =3D offset; + } + } + return result; } =20 static size_t data_type_hash(long key, void *ctx __maybe_unused) --=20 2.55.0.1032.g73a4cd73de-goog