Linux Perf Users
 help / color / mirror / Atom feed
From: "Rui Qi" <qirui.001@bytedance.com>
To: <linux-perf-users@vger.kernel.org>
Cc: "Peter Zijlstra" <peterz@infradead.org>,
	 "Ingo Molnar" <mingo@redhat.com>,
	 "Arnaldo Carvalho de Melo" <acme@kernel.org>,
	 "Namhyung Kim" <namhyung@kernel.org>,
	"Rui Qi" <qirui.001@bytedance.com>
Subject: [PATCH v2] perf: Extract is_mapping_symbol() helper for kernel mapping symbol filtering
Date: Wed,  6 May 2026 15:38:20 +0800	[thread overview]
Message-ID: <20260506073820.2419087-1-qirui.001@bytedance.com> (raw)

The perf tool currently has ad-hoc logic to filter out ELF mapping
symbols scattered across multiple files and architectures.  ARM,
AArch64 and RISC-V each have their own inline checks in
dso__load_sym_internal(), and kallsym processing in map__process_kallsym_symbol()
has yet another check for ARM module symbols.

This is fragile: adding support for a new architecture or adjusting
which prefixes are considered mapping symbols requires touching
multiple places, and it is easy for the checks to diverge.  It also
does not match the kernel's own is_mapping_symbol() logic, which
additionally covers x86 local symbols ('.L*' and 'L0*').

Introduce a single is_mapping_symbol() inline helper in symbol.h and
convert kernel symbol handling to use it.  The helper covers the
existing '$' prefix used by ARM, AArch64 and RISC-V, and also adds
the x86 local symbol prefixes so that perf stays consistent with
the kernel.

Signed-off-by: Rui Qi <qirui.001@bytedance.com>
---

Changes in v2:
- Only apply is_mapping_symbol() filtering to kernel symbols (kallsyms
  and ksymbol events), not to user-space symbols from ELF files,
  BFD libraries, or perf map files.  This avoids incorrectly
  discarding valid user-space function names that start with '$',
  which is a legal character in identifiers for many languages
  (e.g., Java, Scala) and compilers (GCC).
- Move the mapping symbol check in machine__process_ksymbol_register()
  to the beginning of the function, before any map/dso allocation
  or insertion, to avoid leaving empty maps in the kernel map tree.

Link (v1): https://lore.kernel.org/all/20260504090609.1801880-1-qirui.001@bytedance.com/

 tools/perf/util/machine.c |  8 +++++++-
 tools/perf/util/symbol.c  |  4 ++--
 tools/perf/util/symbol.h  | 15 +++++++++++++++
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index e76f8c86e62a..e0dcf8bfb896 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -729,9 +729,15 @@ static int machine__process_ksymbol_register(struct machine *machine,
 {
 	struct symbol *sym;
 	struct dso *dso = NULL;
-	struct map *map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
+	struct map *map;
 	int err = 0;
 
+	/* Ignore mapping symbols in ksymbol events - check early before any state mutation */
+	if (is_mapping_symbol(event->ksymbol.name))
+		return 0;
+
+	map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
+
 	if (!map) {
 		dso = dso__new(event->ksymbol.name);
 
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index fcaeeddbbb6b..af03b16c17c6 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -770,8 +770,8 @@ static int map__process_kallsym_symbol(void *arg, const char *name,
 	if (!symbol_type__filter(type))
 		return 0;
 
-	/* Ignore local symbols for ARM modules */
-	if (name[0] == '$')
+	/* Ignore mapping symbols in kallsyms */
+	if (is_mapping_symbol(name))
 		return 0;
 
 	/*
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index bd6eb90c8668..27fa1b43e6f1 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -28,6 +28,21 @@ struct maps;
 struct option;
 struct build_id;
 
+/*
+ * Ignore kernel mapping symbols, matching kernel is_mapping_symbol() logic.
+ * This checks for '$' prefix (used by ARM, AArch64, RISC-V) and
+ * x86 local symbol prefixes (.L* and L0*).
+ * Only use this for kernel symbols (kallsyms, ksymbol events).
+ */
+static inline bool is_mapping_symbol(const char *str)
+{
+	if (str[0] == '.' && str[1] == 'L')
+		return true;
+	if (str[0] == 'L' && str[1] == '0')
+		return true;
+	return str[0] == '$';
+}
+
 /*
  * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
  * for newer versions we can use mmap to reduce memory usage:
-- 
2.20.1

             reply	other threads:[~2026-05-06  7:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-06  7:38 Rui Qi [this message]
2026-05-06 10:43 ` [PATCH v2] perf: Extract is_mapping_symbol() helper for kernel mapping symbol filtering sashiko-bot
2026-05-07  6:27   ` Rui Qi
2026-05-07  7:11 ` [PATCH v3] perf: Add " Rui Qi
2026-05-07 15:23   ` Ian Rogers
2026-05-07 20:38   ` sashiko-bot

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=20260506073820.2419087-1-qirui.001@bytedance.com \
    --to=qirui.001@bytedance.com \
    --cc=acme@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