mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Namhyung Kim <namhyung@kernel.org>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	 Alireza Haghdoost <haghdoost@uber.com>,
	Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Jiri Olsa <jolsa@kernel.org>,
	 Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	 linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v10 01/12] perf symbol: Fix symbols__fixup_end() for equal-start zero-sized aliases
Date: Tue, 24 Nov 2026 14:30:58 -0800	[thread overview]
Message-ID: <20261124223109.820609-2-irogers@google.com> (raw)
In-Reply-To: <20261124223109.820609-1-irogers@google.com>

When multiple zero-sized symbols share the same start address (such as
STB_GLOBAL and STB_WEAK aliases in /proc/kallsyms), symbols__fixup_end()
previously skipped earlier entries in the equal-start group and only
updated end on the final entry before the next distinct address.

Because __dso__load_kallsyms() and ELF loaders call symbols__fixup_end()
before symbols__fixup_duplicate(), inflating only the last entry gave it
a non-zero length while leaving earlier aliases at the same address with
zero length. In choose_best_symbol(), the initial "Prefer a symbol with
non zero length" check then unconditionally selected whichever alias
happened to appear last, bypassing the STT_NOTYPE, STB_WEAK, STB_GLOBAL,
underscore-count, and arch__choose_best_symbol() heuristics (for example
dropping a global symbol when followed by a weak alias at the same
address).

Track group_start across each equal-start group in symbols__fixup_end():
when no symbol at that start address already has a non-zero size
(!addr_has_sized), synthesize end for every symbol in the group so
subsequent deduplication compares equal-length aliases using binding and
name heuristics (and so allow_aliases callers do not retain zero-length
earlier aliases). When any symbol at that address already has an
explicit non-zero size (addr_has_sized), leave zero-sized aliases at
zero as before so choose_best_symbol() continues to prefer explicitly
sized symbols.

Fixes: 694bf407b061 ("perf symbols: Add some heuristics for choosing the best duplicate symbol")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/symbol.c | 74 +++++++++++++++++++++++-----------------
 1 file changed, 42 insertions(+), 32 deletions(-)

diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 35b08c05a2fd..920951aaa5be 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -518,7 +518,7 @@ void symbols__fixup_duplicate(struct symbols *symbols)
 /* Update zero-sized symbols using the address of the next symbol */
 void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms)
 {
-	struct symbol *curr, *prev;
+	unsigned int group_start = 0;
 	bool addr_has_sized;
 
 	down_write(&symbols->lock);
@@ -529,14 +529,12 @@ void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms)
 		return;
 	}
 
-	curr = symbols->symbols[0];
-	addr_has_sized = curr->end > curr->start;
+	addr_has_sized = symbols->symbols[0]->end > symbols->symbols[0]->start;
 
 	for (unsigned int i = 1; i < symbols->cnt; i++) {
-		prev = curr;
-		curr = symbols->symbols[i];
+		struct symbol *curr = symbols->symbols[i];
 
-		if (curr->start == prev->start) {
+		if (curr->start == symbols->symbols[group_start]->start) {
 			if (curr->end > curr->start)
 				addr_has_sized = true;
 			continue;
@@ -558,40 +556,52 @@ void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms)
 		 * like in:
 		 *   ffffffffc1937000 T hdmi_driver_init  [snd_hda_codec_hdmi]
 		 */
-		if (!addr_has_sized && prev->end == prev->start) {
-			const char *prev_mod;
-			const char *curr_mod;
+		if (!addr_has_sized) {
+			const char *curr_mod = is_kallsyms ? strchr(curr->name, '[') : NULL;
 
-			if (!is_kallsyms) {
-				prev->end = curr->start;
-			} else {
-				prev_mod = strchr(prev->name, '[');
-				curr_mod = strchr(curr->name, '[');
-
-				/* Last kernel/module symbol mapped to end of page */
-				if (!prev_mod != !curr_mod) {
-					prev->end = roundup(prev->end + 4096, 4096);
-				/* Last symbol in the previous module */
-				} else if (prev_mod && strcmp(prev_mod, curr_mod)) {
-					prev->end = roundup(prev->end + 4096, 4096);
-				} else {
-					prev->end = curr->start;
-				}
+			for (unsigned int j = group_start; j < i; j++) {
+				struct symbol *prev = symbols->symbols[j];
 
-				/* The next module can start within that page */
-				if (prev->end > curr->start)
-					prev->end = curr->start;
+				if (prev->end != prev->start)
+					continue;
 
-				pr_debug4("%s sym:%s end:%#" PRIx64 "\n",
-					  __func__, prev->name, prev->end);
+				if (!is_kallsyms) {
+					prev->end = curr->start;
+				} else {
+					const char *prev_mod = strchr(prev->name, '[');
+
+					/* Last kernel/module symbol mapped to end of page */
+					if (!prev_mod != !curr_mod) {
+						prev->end = roundup(prev->end + 4096, 4096);
+					/* Last symbol in the previous module */
+					} else if (prev_mod && strcmp(prev_mod, curr_mod)) {
+						prev->end = roundup(prev->end + 4096, 4096);
+					} else {
+						prev->end = curr->start;
+					}
+
+					/* The next module can start within that page */
+					if (prev->end > curr->start)
+						prev->end = curr->start;
+
+					pr_debug4("%s sym:%s end:%#" PRIx64 "\n",
+						  __func__, prev->name, prev->end);
+				}
 			}
 		}
+		group_start = i;
 		addr_has_sized = curr->end > curr->start;
 	}
 
-	/* Last entry */
-	if (!addr_has_sized && curr->end == curr->start)
-		curr->end = roundup(curr->start, 4096) + 4096;
+	/* Last equal-start group */
+	if (!addr_has_sized) {
+		for (unsigned int j = group_start; j < symbols->cnt; j++) {
+			struct symbol *curr = symbols->symbols[j];
+
+			if (curr->end == curr->start)
+				curr->end = roundup(curr->start, 4096) + 4096;
+		}
+	}
 
 	up_write(&symbols->lock);
 }

base-commit: 148690b95cc425950dbba79d73eb4f7938347fce
-- 
2.56.1.831.g720f3f5937-goog


  reply	other threads:[~2026-11-24 22:31 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 22:30 [PATCH v10 00/12] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
2026-11-24 22:30 ` Ian Rogers [this message]
2026-11-24 22:30 ` [PATCH v10 02/12] perf symbol: Drop num_by_name and sorted_by_name from struct symbols Ian Rogers
2026-11-24 22:31 ` [PATCH v10 03/12] perf symbol: Replace sorted boolean with sorted_cnt prefix length Ian Rogers
2026-11-24 22:31 ` [PATCH v10 04/12] perf symbol: Add secondary sorted run for dso__insert_symbol() Ian Rogers
2026-11-24 22:31 ` [PATCH v10 05/12] perf test symbols: Add sorted insertion and equal-start alias tests Ian Rogers
2026-11-24 22:31 ` [PATCH v10 06/12] perf dso: Synchronize dso_cache access and make dso->loaded atomic Ian Rogers
2026-11-24 22:31 ` [PATCH v10 07/12] perf symbol: Add symbols__shrink() and symbols__try_delete_unannotated() Ian Rogers
2026-11-24 22:31 ` [PATCH v10 08/12] perf dso: Add dso__shrink() and dso__try_evict() Ian Rogers
2026-11-24 22:31 ` [PATCH v10 09/12] perf session: Add dsos, machine, and perf_session shrinking Ian Rogers
2026-11-24 22:31 ` [PATCH v10 10/12] perf test symbols: Add symbol and DSO shrinking tests Ian Rogers
2026-11-24 22:31 ` [PATCH v10 11/12] perf session: Add dso__session() and session-scoped allocation wrappers Ian Rogers
2026-11-24 23:55   ` Alireza Haghdoost
2026-11-24 22:31 ` [PATCH v10 12/12] perf test symbols: Add session allocation wrapper and lock contention tests Ian Rogers

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=20261124223109.820609-2-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=haghdoost@uber.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /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

all inboxes | Powered by JetHome®