From nobody Thu Sep 24 21:19:00 2026 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 87C861D5CFE; Sun, 20 Sep 2026 02:34:33 +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=1789871673; cv=none; b=CX83MbwOvdrVnd6/BGO9jEUfD2LDx19T6QTMFoDiY0qfLQ3143rZgadkLP+nOloaN/fvvThOz2mosICSOKs/ySiCBfd/NmVBuGQ/adBYeORtEvvI6YacVibu4NGzR8LstBG+lnH8s+9B/Obq6C8s0nO7/95pT8Y3zb808Bh+vKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789871673; c=relaxed/simple; bh=8R6TBAR7F78A55XcG8gJNCyOLkrXNYeRBGRE59YA2gw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KBN6VmPILBavVDAaXlIE179c8g286W1APiGY5/B+9aaOA3EjVZ32JM5jNfZjRWPB5N1WIxmzLuhB7TZ8iCWVwlgiWWJ8cQUlBCgIJpkq41MIUqBIAYEHpgGT7CWZYK/O1bI7WPWbt207uUPRAsa652115++eqQsUiUOCy1moBR8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LLZKHa4d; 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="LLZKHa4d" Received: by smtp.kernel.org (Postfix) with ESMTPS id 3386CC2BCB9; Sun, 20 Sep 2026 02:34:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789871673; bh=8R6TBAR7F78A55XcG8gJNCyOLkrXNYeRBGRE59YA2gw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=LLZKHa4dZb8/g6nzmrWFirDIPEJryYPk7iQv1cIb9LAiEKNNaYfAKY2Kx1kKNBFsw B2Zz0pDtZ8UvTex0QyOz59WoLz5onBKmdfAO1DPDJ9awAvlIUbg7f6UoTgwihx2SAR oryVKRcY5mlcb7yqV0SC+5JW0fOqjgaAuDrwtnlWDC4xDoO/1GumtrdVhHx5oCRaRH uwTXBxxBo/urgMZAp0Vxk1/4/RQNoQMKgN6A4sXId0MvYGKk4tDu3hHnUscR8f7nbB oOMwHeJiIYJ6D8PpvRZoN6r4VpLLEBgjH3eMdMCkNqCjELgi887ity7815I92R0BOG Ewi+jBnr1PPoQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D02FC982DF; Sun, 20 Sep 2026 02:34:33 +0000 (UTC) From: Alireza Haghdoost via B4 Relay Date: Sat, 19 Sep 2026 19:33:56 -0700 Subject: [PATCH v2 1/4] perf symbols: Fix broken ELF_C_READ_MMAP fallback guard Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260919-perf-symbol-memory-send-v2-1-495b8f00ad7c@uber.com> References: <20260919-perf-symbol-memory-send-v2-0-495b8f00ad7c@uber.com> In-Reply-To: <20260919-perf-symbol-memory-send-v2-0-495b8f00ad7c@uber.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Alexei Starovoitov , Andrii Nakryiko Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Alireza Haghdoost X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1452; i=haghdoost@uber.com; h=from:subject:message-id; bh=jwkKCZxWR8rzu5laHR8MpSUN65pL8rVjbdfQdF2AFOU=; b=owEBiQJ2/ZANAwAIAVNlBDxl2ALYAcsmYgBqr0Y30TXneuFp2Nqnsn7P9GUk0jF1J+LJFPOvL YZDQxyXx2+JAk8EAAEIADkWIQS5+sFL3gX/8PrA0S1TZQQ8ZdgC2AUCaq9GNxsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwwLDMACgkQU2UEPGXYAthhjA//fIXxYnl9RjKMgZTfBqkYrtJiatXJEfR kCyv0E3yuhbBn671Hwa8dZiaep3w+eRj6FigzAH19N5Ybe1of1eRWHRr3pmQKlIn8ExpiWySV0C f4Mb8VkKE3FIYJ01GyKREH6P2aVMex6UyOiU8wEeS6S1pV40bBhryDYXbG1ExejnbeB54BQmY2k gceJP+C82QuujAYoAbqXVjwBeIIg11byip0t2toPJHN7I1U7KNEEKLYOZ/wGRTHKx8K+4ii5Op0 h3LLMNFiaNYySOIJjIBNDRDANg4RN2H6Gn+6++cYvUBGlSwzJ5H3h+iih4tYQ4BCah1Xh8cj8ln cmNvSSPLJtkQJJ3Mwk7i+30UnClWJvO5C/lvVpUzt7OsUFUi/Hodf6TS/H94W6L2q5USwNLIhGy l44UIqyaPXMI5OzCvCtNrIpS89xMv/Oi6SP/tIy6KD6Isf1l7oHAvIjxcQn3ayf40NGc9CSKiEt KpPZmtpAppW3Ka6GUsgbjrdT8401eVq7tc7tP8Rwi1AIOasHmBhXmUUVXGY3VoxUoLPVjCgBWdA Husc6fqGHji/HVX740iFsVbyoVbJoAYMrIyf5BTArlBU2yn3CP4Gs+RhIqz2SGEt7gKvm92Gr0H lWtc0NgeTmyhfZTKIbHL3AmF9WxYm3fQUqdSsQoOJUd9wFI6bGOU= X-Developer-Key: i=haghdoost@uber.com; a=openpgp; fpr=B9FAC14BDE05FFF0FAC0D12D5365043C65D802D8 X-Endpoint-Received: by B4 Relay for haghdoost@uber.com/default with auth_id=764 X-Original-From: Alireza Haghdoost Reply-To: haghdoost@uber.com From: Alireza Haghdoost Commit 22dd1ac91a77 ("tools: Remove feature-libelf-mmap feature detection") replaced perf's compile-time feature test with an #ifdef on ELF_C_READ_MMAP. ELF_C_READ_MMAP is an Elf_Cmd enumerator rather than a preprocessor macro, so the condition is always false and perf silently uses ELF_C_READ. Perf already requires a sufficiently recent elfutils version that provides ELF_C_READ_MMAP. Use the enumerator directly instead of restoring a feature probe or retaining an unreachable fallback. Fixes: 22dd1ac91a77 ("tools: Remove feature-libelf-mmap feature detection") Signed-off-by: Alireza Haghdoost Reviewed-by: Namhyung Kim --- tools/perf/util/symbol.h | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index d0bac824c79c..46b1649c64fc 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -57,15 +57,7 @@ static inline bool is_livepatch_symbol(const char *str) return strstarts(str, KLP_SYM_PREFIX); } =20 -/* - * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP; - * for newer versions we can use mmap to reduce memory usage: - */ -#ifdef ELF_C_READ_MMAP -# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP -#else -# define PERF_ELF_C_READ_MMAP ELF_C_READ -#endif +#define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP =20 #ifdef HAVE_LIBELF_SUPPORT Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, --=20 Git-157) From nobody Thu Sep 24 21:19:00 2026 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 87D343C0A03; Sun, 20 Sep 2026 02:34:33 +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=1789871673; cv=none; b=XR3tXoVBvgT5mNtyhfiGO3arcV7OLuxy9QxW/gqgLVa2NktQc+F5q5RwXxFUsagLQUEH1GlkujkRVOS3C3DpvxjphWZLDTUP2UiRKoz0XmhK0SkbPWU+TygRS/LCFUTC4SiEMuPg1TZ7nF4sXRmJQAYiBr8yxhCtbugTuWs9/7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789871673; c=relaxed/simple; bh=yBCL3sN0psDdZKgR2CIVhklEjaLwr7sfDsGpbr/EiWk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UpycC+41qXTBDGgnO+tqWw1jzQ2+4ottGqspF2fZtOYILn1QlMI6RLAopdBLWbQiDuuRKSWABYhLl61xZTv3cG/AMyFudxIXmGa1aXOe9PpsYyqZ+PTOkH3J2AgpPHIJ38hb1VRdlh3Okq5QMKIs0Et5N8Eo98DYLBRECrcxPmo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DVwGsqrD; 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="DVwGsqrD" Received: by smtp.kernel.org (Postfix) with ESMTPS id 3E58DC2BCF7; Sun, 20 Sep 2026 02:34:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789871673; bh=yBCL3sN0psDdZKgR2CIVhklEjaLwr7sfDsGpbr/EiWk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=DVwGsqrD3yUVYPJxjNu2WapaUTApWXluIhWZg/HyTEoUTMhTm3xITlcXCYOZxY0vu sOe1dppPbPuxjAyVzexqw1/0usSs5I+VjujXERaU/PA5Q93g8Op5tINv7HEI1y1RKq tDkBEbyNdjG91y/IGGt2TVKspHrrxldSVZEHZWK6X9kXMMUCnPfgzcUrLFcQ0gevli y760mRznTok573V3LnvBNHtgaQSjy01dsGigtN1fiZ+icAUShOoBP7+h/AbypyLEC7 BnIFX5cF2r9dPiHwQd44xc6ZHWZyDYr1q3egXFieU52mWyGaf0Z2brYdXmQGU5o7GA tBLrYiHx1q0mg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F308C982CC; Sun, 20 Sep 2026 02:34:33 +0000 (UTC) From: Alireza Haghdoost via B4 Relay Date: Sat, 19 Sep 2026 19:33:57 -0700 Subject: [PATCH v2 2/4] perf script: Add --max-symbol-bytes to bound ELF symbol memory Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260919-perf-symbol-memory-send-v2-2-495b8f00ad7c@uber.com> References: <20260919-perf-symbol-memory-send-v2-0-495b8f00ad7c@uber.com> In-Reply-To: <20260919-perf-symbol-memory-send-v2-0-495b8f00ad7c@uber.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Alexei Starovoitov , Andrii Nakryiko Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Alireza Haghdoost X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=20153; i=haghdoost@uber.com; h=from:subject:message-id; bh=zaTt808ipaMzTUtvKvs89xlwGNQaXKsGzSGs62o5iBY=; b=owEBiQJ2/ZANAwAIAVNlBDxl2ALYAcsmYgBqr0Y3WWPtBbJTYmg1f5p02D2+nHHcT2NTQ/KIU 1WGgBYZ8PGJAk8EAAEIADkWIQS5+sFL3gX/8PrA0S1TZQQ8ZdgC2AUCaq9GNxsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwwLDMACgkQU2UEPGXYAtiakA/+Jm5ZMc+JKOnii8e0Mq3CbOSpWMIcMjR upSNQwYEY+7yn45ZL4Tq124DVWRYvf/aF5AcDVpMqEOvo5CfTe+TON+EvY/7HxyZfgb1kBs3yRK 364U1XUWMvGVCy8oOyEseT01DACMq3TuIFEKtUn7SOd5hUCNDwVnqYLV2B0qENRMvkkQvxCZisQ YVicujEFG0rxYN2A+rBrPPUvD5MzIgc1Hcy0qwhVU8w3fOCjPQ1PTxIMzdh0cXyBpXosFz+yB3l vq3+YZSWLnC2ZwX3iNLQdxHcCTD32ZniH92XeB3dGyE9nEaRRmjDW/+cT0uJr6H2/YeoV9wrIdg Y2eDCaiG+k7zvtV8d2jQ+ED04dOlGQCmJocoeAknb9oLjS8X/V/It43UW+ZYBeLlvOc1lGOm5so NDZd3ey0IP9uKImFrCwWlbKb+UrLROo5jfOXQirbRP0bMnX+b1nrQt4xzU6zCTy5D2s37/1fyHa y+QbdjrXrYI6jGKnV+JLu9dUl8xvDLvVBIp2IgB7I0eZffgG6G8C5dwFIOhMNJ6ykV/+FadoG2V Vf7iUZLcLAl1qa0KbScxbOJRVNuRfX3avYYPrbqCUlhSl7LDhNaMoqdUi3IFoEZQY30FgVTtMF0 +3tE3uybTFGbxtr8x+L1uI+JYXyzPxgXUEDW6PcqhAsU9C6l52M8= X-Developer-Key: i=haghdoost@uber.com; a=openpgp; fpr=B9FAC14BDE05FFF0FAC0D12D5365043C65D802D8 X-Endpoint-Received: by B4 Relay for haghdoost@uber.com/default with auth_id=764 X-Original-From: Alireza Haghdoost Reply-To: haghdoost@uber.com From: Alireza Haghdoost perf script eagerly materializes every ELF symbol into an rb-tree kept until process exit. Large profiles can therefore consume substantial anonymous memory, causing perf script to be OOM-killed or forcing the kernel to reclaim memory from co-located workloads. This patch adds --max-symbol-bytes to bound struct symbol allocations. Once the budget is reached, the ELF loader stops loading symbols, warns once, and lets unresolved addresses appear as [unknown]. This allows users to bound the memory footprint upfront and explicitly choose between complete symbolization and avoiding unbounded host memory pressure. perf record already provides a similar --max-size option to bound disk usage. The counter includes every symbol__new() allocation, but this patch enforces the limit only in the ELF loader, which is the source of the unbounded memory growth addressed here. In this path, reaching the limit can safely produce [unknown] symbols. Other loaders currently treat a failed symbol allocation as an error. Capping those paths would therefore require separate changes whose complexity may outweigh the potential memory savings. The cap applies to userspace DSOs, vmlinux-as-ELF, and kernel modules. Sizes require a B/K/M/G suffix, except that a bare 0 and the default mean unlimited. Reservations are atomic so concurrent loaders cannot exceed the limit; accounting retains complete name lengths and partial zero-sized symbol ranges do not cover omitted addresses. Document the option with the code that introduces it and add focused accounting and concurrent-reservation tests. Signed-off-by: Alireza Haghdoost --- tools/perf/Documentation/perf-script.txt | 10 +++ tools/perf/builtin-script.c | 42 +++++++++ tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/symbol-bytes.c | 143 +++++++++++++++++++++++++++= ++++ tools/perf/tests/tests.h | 1 + tools/perf/util/symbol-elf.c | 66 +++++++++++--- tools/perf/util/symbol.c | 82 +++++++++++++++++- tools/perf/util/symbol.h | 8 +- tools/perf/util/symbol_conf.h | 1 + 10 files changed, 340 insertions(+), 15 deletions(-) diff --git a/tools/perf/Documentation/perf-script.txt b/tools/perf/Document= ation/perf-script.txt index 200ea25891d8..217167a2e56b 100644 --- a/tools/perf/Documentation/perf-script.txt +++ b/tools/perf/Documentation/perf-script.txt @@ -412,6 +412,16 @@ include::itrace.txt[] =20 Default: 127 =20 +--max-symbol-bytes:: + Limit the bytes held in struct symbol allocations for DSOs on the + libelf symbol-loader path: userspace DSOs, vmlinux-as-ELF, and kernel + modules. This is not a cap on all symbol memory or RSS: symbols from + kallsyms, JIT maps, and libbfd are counted but not capped. Accepts a + size with a B/K/M/G suffix (e.g. 128M). When the budget is exceeded, + the ELF loader stops adding symbols; addresses not covered by symbols + already loaded are then printed as [unknown]. A warning is printed. + Default: 0 (unlimited). + --ns:: Use 9 decimal places when displaying time (i.e. show the nanoseconds) =20 diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index ad8ca08ceb5f..017b39ed6a21 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -68,6 +68,7 @@ #include "util/thread.h" #include "util/thread_map.h" #include "util/time-utils.h" +#include "util/units.h" #include "util/tool.h" #include "util/trace-event.h" #include "util/unwind.h" @@ -4035,6 +4036,44 @@ static int parse_callret_trace(const struct option *= opt __maybe_unused, return 0; } =20 +static int parse_max_symbol_bytes(const struct option *opt, + const char *str, int unset) +{ + unsigned long *max_bytes =3D (unsigned long *)opt->value; + static struct parse_tag size_tags[] =3D { + { .tag =3D 'B', .mult =3D 1 }, + { .tag =3D 'K', .mult =3D 1 << 10 }, + { .tag =3D 'M', .mult =3D 1 << 20 }, + { .tag =3D 'G', .mult =3D 1 << 30 }, + { .tag =3D 0 }, + }; + unsigned long bytes; + size_t len; + + if (unset) { + *max_bytes =3D 0; + return 0; + } + + if (!strcmp(str, "0")) { + *max_bytes =3D 0; + return 0; + } + + len =3D strlen(str); + if (len < 2 || !strchr("BKMG", str[len - 1]) || + strspn(str, "0123456789") !=3D len - 1) + return -1; + + bytes =3D parse_tag_value(str, size_tags); + if (bytes !=3D (unsigned long)-1) { + *max_bytes =3D bytes; + return 0; + } + + return -1; +} + int cmd_script(int argc, const char **argv) { bool show_full_info =3D false; @@ -4135,6 +4174,9 @@ int cmd_script(int argc, const char **argv) "Set the maximum stack depth when parsing the callchain, " "anything beyond the specified depth will be ignored. " "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STA= CK_DEPTH)), + OPT_CALLBACK(0, "max-symbol-bytes", &symbol_conf.max_symbol_bytes, + "size", "Limit bytes for ELF struct symbol (e.g. 128M; 0=3Dunlimite= d)", + parse_max_symbol_bytes), OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"), OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to pre= vious event"), OPT_BOOLEAN('I', "show-info", &show_full_info, diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 66944a4f4968..09b0f4a55c30 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -66,6 +66,7 @@ perf-test-y +=3D dlfilter-test.o perf-test-y +=3D sigtrap.o perf-test-y +=3D event_groups.o perf-test-y +=3D symbols.o +perf-test-y +=3D symbol-bytes.o perf-test-y +=3D util.o perf-test-y +=3D hwmon_pmu.o perf-test-y +=3D tool_pmu.o diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index 4d0784b16723..0bed0f4f076b 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -150,6 +150,7 @@ static struct test_suite *generic_tests[] =3D { &suite__sigtrap, &suite__event_groups, &suite__symbols, + &suite__symbol_bytes, &suite__util, &suite__subcmd_help, &suite__kallsyms_split, diff --git a/tools/perf/tests/symbol-bytes.c b/tools/perf/tests/symbol-byte= s.c new file mode 100644 index 000000000000..4a4740daa5f5 --- /dev/null +++ b/tools/perf/tests/symbol-bytes.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include + +#include "debug.h" +#include "symbol.h" +#include "symbol_conf.h" +#include "tests.h" + +static int test__symbol_bytes_long_name(struct test_suite *test __maybe_un= used, + int subtest __maybe_unused) +{ + const size_t name_len =3D 65536; + unsigned long saved_max =3D symbol_conf.max_symbol_bytes; + size_t baseline =3D symbol__bytes_used(); + struct symbol *sym =3D NULL; + size_t expected =3D symbol_conf.priv_size + sizeof(*sym) + name_len + 1; + char *name; + int ret =3D TEST_FAIL; + + symbol_conf.max_symbol_bytes =3D 0; + name =3D malloc(name_len + 1); + if (!name) + goto out; + memset(name, 'a', name_len); + name[name_len] =3D '\0'; + + sym =3D symbol__new(0, 1, 0, 0, name); + if (!sym) + goto out_free_name; + if (symbol__bytes_used() !=3D baseline + expected) { + pr_debug("long symbol name accounting mismatch\n"); + goto out_delete; + } + + /* Kallsyms splitting can shorten the stored name in place. */ + sym->name[10] =3D '\0'; + symbol__delete(sym); + sym =3D NULL; + if (symbol__bytes_used() !=3D baseline) { + pr_debug("long symbol name was not fully unaccounted\n"); + goto out_free_name; + } + ret =3D TEST_OK; + +out_delete: + if (sym) + symbol__delete(sym); +out_free_name: + free(name); +out: + symbol_conf.max_symbol_bytes =3D saved_max; + return ret; +} + +struct reserve_arg { + size_t bytes; + bool success; +}; + +static void *reserve_bytes(void *data) +{ + struct reserve_arg *arg =3D data; + + arg->success =3D symbol__try_account_bytes(arg->bytes); + return NULL; +} + +static int test__symbol_bytes_reservation(struct test_suite *test __maybe_= unused, + int subtest __maybe_unused) +{ + enum { NR_THREADS =3D 8, NR_ALLOWED =3D 4 }; + const size_t reservation =3D 1024; + unsigned long saved_max =3D symbol_conf.max_symbol_bytes; + size_t baseline =3D symbol__bytes_used(); + struct reserve_arg args[NR_THREADS]; + pthread_t threads[NR_THREADS]; + int created =3D 0, successful =3D 0; + int ret =3D TEST_FAIL; + int i; + + if (baseline > ULONG_MAX - NR_ALLOWED * reservation) + return TEST_SKIP; + + symbol_conf.max_symbol_bytes =3D baseline + NR_ALLOWED * reservation; + if (symbol__try_account_bytes(SIZE_MAX)) { + pr_debug("overflowing symbol reservation succeeded\n"); + symbol__unaccount_bytes(SIZE_MAX); + goto out; + } + + for (i =3D 0; i < NR_THREADS; i++) { + args[i].bytes =3D reservation; + args[i].success =3D false; + if (pthread_create(&threads[i], NULL, reserve_bytes, &args[i])) + goto out_join; + created++; + } + +out_join: + for (i =3D 0; i < created; i++) + pthread_join(threads[i], NULL); + for (i =3D 0; i < created; i++) { + if (args[i].success) + successful++; + } + + if (created !=3D NR_THREADS || successful !=3D NR_ALLOWED) { + pr_debug("symbol reservation count: created %d, successful %d\n", + created, successful); + goto out_release; + } + if (symbol__bytes_used() !=3D baseline + NR_ALLOWED * reservation) { + pr_debug("symbol reservation exceeded configured budget\n"); + goto out_release; + } + ret =3D TEST_OK; + +out_release: + for (i =3D 0; i < created; i++) { + if (args[i].success) + symbol__unaccount_bytes(args[i].bytes); + } +out: + symbol_conf.max_symbol_bytes =3D saved_max; + if (symbol__bytes_used() !=3D baseline) + ret =3D TEST_FAIL; + return ret; +} + +static struct test_case tests__symbol_bytes[] =3D { + TEST_CASE("Long name accounting", symbol_bytes_long_name), + TEST_CASE("Concurrent strict reservations", symbol_bytes_reservation), + { .name =3D NULL, } +}; + +struct test_suite suite__symbol_bytes =3D { + .desc =3D "Symbol memory accounting", + .test_cases =3D tests__symbol_bytes, +}; diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index cee9e6b62dcc..6dfad7ac722b 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -178,6 +178,7 @@ DECLARE_SUITE(dlfilter); DECLARE_SUITE(sigtrap); DECLARE_SUITE(event_groups); DECLARE_SUITE(symbols); +DECLARE_SUITE(symbol_bytes); DECLARE_SUITE(util); DECLARE_SUITE(uncore_event_sorting); DECLARE_SUITE(subcmd_help); diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index e955c3feddcd..2f7ea1499cbf 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -561,6 +561,13 @@ static bool get_plt_got_name(GElf_Shdr *shdr, size_t i, return result; } =20 +static void symbol_budget_warning(void) +{ + pr_warning_once("perf: symbol memory budget exceeded (%lu bytes), " + "remaining symbols will be [unknown]\n", + symbol_conf.max_symbol_bytes); +} + static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf, GElf_Ehdr *ehdr, char *buf, size_t buf_sz) @@ -580,11 +587,19 @@ static int dso__synthesize_plt_got_symbols(struct dso= *dso, Elf *elf, get_rela_dyn_info(elf, ehdr, &di, scn); =20 for (i =3D 0; i < shdr.sh_size; i +=3D shdr.sh_entsize) { + bool budget_exceeded; + if (!get_plt_got_name(&shdr, i, &di, buf, buf_sz)) snprintf(buf, buf_sz, "offset_%#" PRIx64 "@plt", (u64)shdr.sh_offset + = i); - sym =3D symbol__new(shdr.sh_offset + i, shdr.sh_entsize, STB_GLOBAL, STT= _FUNC, buf); - if (!sym) + sym =3D symbol__new_bounded(shdr.sh_offset + i, shdr.sh_entsize, + STB_GLOBAL, STT_FUNC, buf, &budget_exceeded); + if (!sym) { + if (budget_exceeded) { + symbol_budget_warning(); + err =3D 0; + } goto out; + } symbols__insert(dso__symbols(dso), sym); } err =3D 0; @@ -615,6 +630,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct= symsrc *ss) Elf *elf; int nr =3D 0, err =3D -1; struct rel_info ri =3D { .is_rela =3D false }; + bool budget_exceeded; bool lazy_plt; =20 elf =3D ss->elf; @@ -636,9 +652,16 @@ int dso__synthesize_plt_symbols(struct dso *dso, struc= t symsrc *ss) return 0; =20 /* Add a symbol for .plt header */ - plt_sym =3D symbol__new(shdr_plt.sh_offset, plt_header_size, STB_GLOBAL, = STT_FUNC, ".plt"); - if (!plt_sym) + plt_sym =3D symbol__new_bounded(shdr_plt.sh_offset, plt_header_size, + STB_GLOBAL, STT_FUNC, ".plt", + &budget_exceeded); + if (!plt_sym) { + if (budget_exceeded) { + symbol_budget_warning(); + return 0; + } goto out_elf_end; + } symbols__insert(dso__symbols(dso), plt_sym); =20 /* Only x86 has .plt.got */ @@ -756,9 +779,15 @@ int dso__synthesize_plt_symbols(struct dso *dso, struc= t symsrc *ss) "offset_%#" PRIx64 "@plt", plt_offset); free(demangled); =20 - f =3D symbol__new(plt_offset, plt_entry_size, STB_GLOBAL, STT_FUNC, symp= ltname); - if (!f) + f =3D symbol__new_bounded(plt_offset, plt_entry_size, STB_GLOBAL, + STT_FUNC, sympltname, &budget_exceeded); + if (!f) { + if (budget_exceeded) { + symbol_budget_warning(); + err =3D 0; + } goto out_elf_end; + } =20 plt_offset +=3D plt_entry_size; symbols__insert(dso__symbols(dso), f); @@ -1534,6 +1563,7 @@ dso__load_sym_internal(struct dso *dso, struct map *m= ap, struct symsrc *syms_ss, Elf *elf; int nr =3D 0; bool remap_kernel =3D false, adjust_kernel_syms =3D false; + bool budget_truncated =3D false; u64 max_text_sh_offset =3D 0; =20 if (kmap && !kmaps) @@ -1633,8 +1663,16 @@ dso__load_sym_internal(struct dso *dso, struct map *= map, struct symsrc *syms_ss, char *demangled =3D NULL; int is_label =3D elf_sym__is_label(&sym); const char *section_name; + bool budget_exceeded =3D false; bool used_opd =3D false; =20 + if (symbol_conf.max_symbol_bytes && + symbol__bytes_used() >=3D symbol_conf.max_symbol_bytes) { + symbol_budget_warning(); + budget_truncated =3D true; + break; + } + if (!is_label && !elf_sym__filter(&sym)) continue; =20 @@ -1775,10 +1813,17 @@ dso__load_sym_internal(struct dso *dso, struct map = *map, struct symsrc *syms_ss, if (demangled !=3D NULL) elf_name =3D demangled; =20 - f =3D symbol__new(sym.st_value, sym.st_size, - GELF_ST_BIND(sym.st_info), - GELF_ST_TYPE(sym.st_info), elf_name); + f =3D symbol__new_bounded(sym.st_value, sym.st_size, + GELF_ST_BIND(sym.st_info), + GELF_ST_TYPE(sym.st_info), elf_name, + &budget_exceeded); + if (!f && budget_exceeded) { + symbol_budget_warning(); + budget_truncated =3D true; + } free(demangled); + if (!f && budget_truncated) + break; if (!f) goto out_elf_end; =20 @@ -1793,7 +1838,8 @@ dso__load_sym_internal(struct dso *dso, struct map *m= ap, struct symsrc *syms_ss, * For misannotated, zeroed, ASM function sizes. */ if (nr > 0) { - symbols__fixup_end(dso__symbols(dso), false); + if (!budget_truncated) + symbols__fixup_end(dso__symbols(dso), false); symbols__fixup_duplicate(dso__symbols(dso)); if (kmap) { /* diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 3587ad243159..32eef666f748 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -310,13 +310,72 @@ void symbols__fixup_end(struct rb_root_cached *symbol= s, bool is_kallsyms) curr->end =3D roundup(curr->start, 4096) + 4096; } =20 -struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const = char *name) +static _Atomic size_t symbol_bytes_used; + +size_t symbol__bytes_used(void) +{ + return atomic_load_explicit(&symbol_bytes_used, memory_order_relaxed); +} + +void symbol__account_bytes(size_t bytes) +{ + atomic_fetch_add_explicit(&symbol_bytes_used, bytes, memory_order_relaxed= ); +} + +bool symbol__try_account_bytes(size_t bytes) +{ + size_t old =3D symbol__bytes_used(); + + for (;;) { + if (old > SIZE_MAX - bytes) + return false; + if (symbol_conf.max_symbol_bytes && + (old > symbol_conf.max_symbol_bytes || + bytes > symbol_conf.max_symbol_bytes - old)) + return false; + if (atomic_compare_exchange_weak_explicit(&symbol_bytes_used, &old, old = + bytes, + memory_order_relaxed, + memory_order_relaxed)) + return true; + } +} + +void symbol__unaccount_bytes(size_t bytes) +{ + atomic_fetch_sub_explicit(&symbol_bytes_used, bytes, memory_order_relaxed= ); +} + +static struct symbol *__symbol__new(u64 start, u64 len, u8 binding, u8 typ= e, + const char *name, bool bounded, + bool *budget_exceeded) { size_t namelen =3D strlen(name) + 1; - struct symbol *sym =3D calloc(1, (symbol_conf.priv_size + - sizeof(*sym) + namelen)); - if (sym =3D=3D NULL) + size_t alloc_size; + struct symbol *sym; + + if (budget_exceeded) + *budget_exceeded =3D false; + if (namelen - 1 > UINT32_MAX || + namelen > SIZE_MAX - sizeof(*sym) || + symbol_conf.priv_size > SIZE_MAX - sizeof(*sym) - namelen) return NULL; + alloc_size =3D symbol_conf.priv_size + sizeof(*sym) + namelen; + + if (bounded && !symbol__try_account_bytes(alloc_size)) { + if (budget_exceeded) + *budget_exceeded =3D true; + return NULL; + } + + sym =3D calloc(1, alloc_size); + if (sym =3D=3D NULL) { + if (bounded) + symbol__unaccount_bytes(alloc_size); + return NULL; + } + + if (!bounded) + symbol__account_bytes(alloc_size); =20 if (symbol_conf.priv_size) { if (symbol_conf.init_annotation) { @@ -339,8 +398,22 @@ struct symbol *symbol__new(u64 start, u64 len, u8 bind= ing, u8 type, const char * return sym; } =20 +struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const = char *name) +{ + return __symbol__new(start, len, binding, type, name, false, NULL); +} + +struct symbol *symbol__new_bounded(u64 start, u64 len, u8 binding, u8 type, + const char *name, bool *budget_exceeded) +{ + return __symbol__new(start, len, binding, type, name, true, budget_exceed= ed); +} + void symbol__delete(struct symbol *sym) { + size_t alloc_size =3D symbol_conf.priv_size + sizeof(*sym) + + sym->namelen + 1; + if (symbol_conf.priv_size) { if (symbol_conf.init_annotation) { struct annotation *notes =3D symbol__annotation(sym); @@ -348,6 +421,7 @@ void symbol__delete(struct symbol *sym) annotation__exit(notes); } } + symbol__unaccount_bytes(alloc_size); free(((void *)sym) - symbol_conf.priv_size); } =20 diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 46b1649c64fc..f7331edf0b71 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -90,7 +90,7 @@ struct symbol { u64 start; u64 end; /** Length of the string name. */ - u16 namelen; + u32 namelen; _Atomic uint16_t flags; /** Architecture specific. Unused except on PPC where it holds st_other. = */ u8 arch_sym; @@ -227,6 +227,12 @@ void symbol__elf_init(void); int symbol__annotation_init(void); =20 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const = char *name); +struct symbol *symbol__new_bounded(u64 start, u64 len, u8 binding, u8 type, + const char *name, bool *budget_exceeded); +size_t symbol__bytes_used(void); +void symbol__account_bytes(size_t bytes); +bool symbol__try_account_bytes(size_t bytes); +void symbol__unaccount_bytes(size_t bytes); size_t __symbol__fprintf_symname_offs(const struct symbol *sym, const struct addr_location *al, bool unknown_as_addr, diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h index 71f60081a85b..6a16c5badd5e 100644 --- a/tools/perf/util/symbol_conf.h +++ b/tools/perf/util/symbol_conf.h @@ -120,6 +120,7 @@ struct symbol_conf { int pad_output_len_dso; int group_sort_idx; int addr_range; + unsigned long max_symbol_bytes; DECLARE_BITMAP(parallelism_filter, MAX_NR_CPUS + 1); }; =20 --=20 Git-157) From nobody Thu Sep 24 21:19:00 2026 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 904863C3442; Sun, 20 Sep 2026 02:34:33 +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=1789871673; cv=none; b=MYqPOiw8vrw/tAPkggQ8HF6bET9qudiSAC7vM4ightY/uMAKxZnwCZQf/sRfEHakEVDdEWZwBRjKex27EN3nArjjYDP7+4prhX/VoiHpIqEKzzY8KHmeV7KXkmHGCoXvzn8fgzTT7GDqXfTtmyV/xyI820P6m7Gz4ZKgLO1n6us= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789871673; c=relaxed/simple; bh=Q5iZ/khQy1i1/ozAArw1b10QQPe6alwA+1tZTEGkf8U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FQFFKpVM5ZCh4AUb/zjmjDjX+bChFGKbCUYTt3Zw8VLequaMeep20ZVC8DHJhtSaDlz5wDwaaMxs5LAsJe+544gQpPGb5OsaQ0Ul4svAEz+VhmZ3rwomH8wZO+Xqm7tTGClZDuYDNU8DHCefWBHL7yScvi0mct7VphHQKDf2nhw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Xvy5i4zm; 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="Xvy5i4zm" Received: by smtp.kernel.org (Postfix) with ESMTPS id 4D5CEC2BCFD; Sun, 20 Sep 2026 02:34:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789871673; bh=Q5iZ/khQy1i1/ozAArw1b10QQPe6alwA+1tZTEGkf8U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Xvy5i4zmnhKA3UpbS90Vpl5bw7QeS3e3Zptek0iUwqPReZ+IfKQ8RACTd1gG4efcu /YqX4wOIbSi3zeObMXLAienvvdjMjDgqKGAgZqG2lM80d3MUAOkQ7fr9c1F7u7jrlv z1yExJG+Ra+rRPJAIzxAZgmswT2czSYj5ciAiT/7P3Yjo78175mZdI/8MtoYZKfT7v QRdqRsVtnqx0Kc8/NROvNHxI8SPpKeldudgAp8pqjWRiCBdvAnN5FTjmWlSU5AYJSG LuMRiofX+WiEeaXK/ssMjJ7kYbc5kXGhlWbWX0Isv3MR3fq9xT3/OVk5KGbovnLyBb GYWRg+fg3HU9w== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32D58C982EA; Sun, 20 Sep 2026 02:34:33 +0000 (UTC) From: Alireza Haghdoost via B4 Relay Date: Sat, 19 Sep 2026 19:33:58 -0700 Subject: [PATCH v2 3/4] perf script: Add --lazy-load-symbols for lazy symbol loading Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260919-perf-symbol-memory-send-v2-3-495b8f00ad7c@uber.com> References: <20260919-perf-symbol-memory-send-v2-0-495b8f00ad7c@uber.com> In-Reply-To: <20260919-perf-symbol-memory-send-v2-0-495b8f00ad7c@uber.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Alexei Starovoitov , Andrii Nakryiko Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Alireza Haghdoost X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=47164; i=haghdoost@uber.com; h=from:subject:message-id; bh=rCbOKMTnLxkV5HKPcltZ+lN7k6HFlJymYSQgS/dh3XI=; b=owEBiQJ2/ZANAwAIAVNlBDxl2ALYAcsmYgBqr0Y3q8b5PLoUoWdR+RpE2GMtInFd9Ulb1rV2g LHwWK2YyNuJAk8EAAEIADkWIQS5+sFL3gX/8PrA0S1TZQQ8ZdgC2AUCaq9GNxsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwwLDMACgkQU2UEPGXYAtj5+g/+KWoA2YR3wi5Irbccl6LUqs/yZQ9XdBe m4a/I/B/rBeAEf4dgfX0tjjT2JRwDPtZQj1atWED2FncMf88xVuVj1UpOO85lwLBNgY4ycXy0Bn 6T+W6ZBvDaPsdIUWemhPhOwNmKZs7UJtascQqodMMqVXwlzFFLofCGQqr5YCCrmpAC3GqEqScI6 jdVlWbQWFzSAOaPnq7LFGkfhjBjqDnNPpnqbknv8GtLvPzbUCUGFGOan/GWUQ0gVgr9/BybWSNH T+V3/sKVcuSCJbRanH4MDf6ucmYdS8gGud2N8a2KDh5zVHciZnIdgOGJ+iw9oDMXblPaw6a56NB 3U7SFepyXxfTVR5VHv3uk662a7ojZaZ4iSQNXZN/YWW9gl6mXmR5AUCHqbDwyZkRMWLKxRv4Tbx mHDJjrtJw7mSHrxRuGaeJBFd2fdaiBgPRgHe6GbNJq6zTX75PdT8kF3CcP8l7KIcMgylPkDgQVK nEmB87wckB67HqA5QzxJP6L9nHcKRPChQcVPkLrEkrW4uwcENKXzdOdUDDWRGFc0NC3uHfI1E5P Nx4R7Zm3sX+NjYM9x/nMYVYvDYl0zAxCc+LIqfniHijfmJjPYGTy+R6FElWbzZSWAkz0EKf1IGI QHwCZ1GUKJgB3N+5qRiaOL4x1O9/Bc+ltjKjsH9lTWZfdGDSZx3Q= X-Developer-Key: i=haghdoost@uber.com; a=openpgp; fpr=B9FAC14BDE05FFF0FAC0D12D5365043C65D802D8 X-Endpoint-Received: by B4 Relay for haghdoost@uber.com/default with auth_id=764 X-Original-From: Alireza Haghdoost Reply-To: haghdoost@uber.com From: Alireza Haghdoost perf script eagerly materializes eligible symbols from every DSO encountered in samples. On a production fixture, it loaded about 765k symbols to resolve about 45k distinct (DSO, symbol) frames, exceeding the memory available in a memory-constrained cgroup. This patch adds --lazy-load-symbols for userspace ELF DSOs. It builds a compact sorted index, resolves sampled addresses by binary search, reads symbol names through a private data-source DSO, and caches resolved symbols in the existing rb-tree. The private DSO uses the normal DSO data cache, so the exact split-debuginfo source can be reopened after descriptor eviction. If the source cannot be read during preflight, perf discards the index and eagerly loads that DSO instead. On the same fixture, peak RssAnon drops from 265 MiB to 39 MiB and wall time from 3.1 seconds to 1.85 seconds. Memory optimizations usually cost time; this one does not because lazy loading skips many unnecessary calloc() calls and demangling operations. Lazy loading is most effective when samples reference only a small fraction of the available symbols, such as profiles spanning many large DSOs. It still builds an index proportional to the total symbol count. Eager loading remains available for dense symbol coverage or cases requiring its broader ELF and architecture support. This does not claim full parity with the eager loader. Lazy loading supports the common userspace ELF symtab/dynsym case; .gnu_debugdata and PPC64 .opd continue through the eager loader. Materialization is serialized with the DSO lock. Name lookups materialize the remaining index before constructing the name-sorted array. Lazy loading shares eager duplicate and IFUNC selection, and clips ranges that cross .plt before synthesizing PLT symbols. Signed-off-by: Alireza Haghdoost --- tools/perf/Documentation/perf-script.txt | 34 +- tools/perf/arch/powerpc/util/sym-handling.c | 5 +- tools/perf/builtin-script.c | 4 +- tools/perf/tests/symbol-bytes.c | 139 ++++++ tools/perf/util/dso.c | 63 ++- tools/perf/util/dso.h | 49 ++ tools/perf/util/map.c | 30 +- tools/perf/util/symbol-elf.c | 667 ++++++++++++++++++++++++= ++++ tools/perf/util/symbol-minimal.c | 17 + tools/perf/util/symbol.c | 58 ++- tools/perf/util/symbol.h | 6 +- tools/perf/util/symbol_conf.h | 1 + 12 files changed, 1029 insertions(+), 44 deletions(-) diff --git a/tools/perf/Documentation/perf-script.txt b/tools/perf/Document= ation/perf-script.txt index 217167a2e56b..136d773778da 100644 --- a/tools/perf/Documentation/perf-script.txt +++ b/tools/perf/Documentation/perf-script.txt @@ -412,15 +412,33 @@ include::itrace.txt[] =20 Default: 127 =20 +--lazy-load-symbols:: + Resolve symbols lazily instead of eagerly loading the full + symbol table of every DSO that appears in a sample. A compact + sorted index is built per DSO and only the addresses that appear + in samples are materialized into symbols, with names read from the + file's string table through the DSO data cache at lookup time. This + sharply reduces memory + (and usually time) for profiles of large binaries where only a + small fraction of the symbol table is referenced. This applies only + to userspace ELF DSOs; kernel DSOs and modules always load eagerly. + Operations that look up a symbol by name materialize the remainder + of that DSO's index first to preserve name-lookup behavior. + Output may differ from the default loader for some targets + (e.g. PPC64 .opd or .gnu_debugdata). Default: off. + --max-symbol-bytes:: - Limit the bytes held in struct symbol allocations for DSOs on the - libelf symbol-loader path: userspace DSOs, vmlinux-as-ELF, and kernel - modules. This is not a cap on all symbol memory or RSS: symbols from - kallsyms, JIT maps, and libbfd are counted but not capped. Accepts a - size with a B/K/M/G suffix (e.g. 128M). When the budget is exceeded, - the ELF loader stops adding symbols; addresses not covered by symbols - already loaded are then printed as [unknown]. A warning is printed. - Default: 0 (unlimited). + Limit the bytes held in struct symbol allocations (and, with + --lazy-load-symbols, the lazy index) for DSOs on the ELF symbol + loader path -- userspace DSOs plus vmlinux-as-ELF and kernel + modules. This is not a cap on all symbol memory or RSS: symbols + from kallsyms, JIT maps, and libbfd are counted but not capped. + Accepts a size with a B/K/M/G suffix (e.g. 128M). + When the budget is exceeded, the ELF loader stops adding symbols; + addresses not covered by the symbols already loaded are then printed + as [unknown]. A warning is printed. This is a safety net independent of + --lazy-load-symbols and can be used with or without it. Default: 0 + (unlimited). =20 --ns:: Use 9 decimal places when displaying time (i.e. show the nanoseconds) diff --git a/tools/perf/arch/powerpc/util/sym-handling.c b/tools/perf/arch/= powerpc/util/sym-handling.c index 947bfad7aa59..ac8fc787e95d 100644 --- a/tools/perf/arch/powerpc/util/sym-handling.c +++ b/tools/perf/arch/powerpc/util/sym-handling.c @@ -10,10 +10,9 @@ #include "probe-event.h" #include "probe-file.h" =20 -int arch__choose_best_symbol(struct symbol *syma, - struct symbol *symb __maybe_unused) +int arch__choose_best_symbol(const char *syma_name) { - char *sym =3D syma->name; + const char *sym =3D syma_name; =20 #if !defined(_CALL_ELF) || _CALL_ELF !=3D 2 /* Skip over any initial dot */ diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 017b39ed6a21..5bdf4a7895df 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -4037,7 +4037,7 @@ static int parse_callret_trace(const struct option *o= pt __maybe_unused, } =20 static int parse_max_symbol_bytes(const struct option *opt, - const char *str, int unset) + const char *str, int unset) { unsigned long *max_bytes =3D (unsigned long *)opt->value; static struct parse_tag size_tags[] =3D { @@ -4177,6 +4177,8 @@ int cmd_script(int argc, const char **argv) OPT_CALLBACK(0, "max-symbol-bytes", &symbol_conf.max_symbol_bytes, "size", "Limit bytes for ELF struct symbol (e.g. 128M; 0=3Dunlimite= d)", parse_max_symbol_bytes), + OPT_BOOLEAN(0, "lazy-load-symbols", &symbol_conf.lazy_load_symbols, + "Resolve symbols lazily instead of loading full symtabs"), OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"), OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to pre= vious event"), OPT_BOOLEAN('I', "show-info", &show_full_info, diff --git a/tools/perf/tests/symbol-bytes.c b/tools/perf/tests/symbol-byte= s.c index 4a4740daa5f5..e5e2e6b9db97 100644 --- a/tools/perf/tests/symbol-bytes.c +++ b/tools/perf/tests/symbol-bytes.c @@ -1,11 +1,17 @@ // SPDX-License-Identifier: GPL-2.0 +#include #include #include #include #include #include +#include +#include +#include =20 #include "debug.h" +#include "dso.h" +#include "map.h" #include "symbol.h" #include "symbol_conf.h" #include "tests.h" @@ -131,9 +137,142 @@ static int test__symbol_bytes_reservation(struct test= _suite *test __maybe_unused return ret; } =20 +static int test__symbol_bytes_duplicate_selection(struct test_suite *test = __maybe_unused, + int subtest __maybe_unused) +{ + struct duplicate_case { + u64 a_size; + u8 a_type; + u8 a_binding; + const char *a_name; + u64 b_size; + u8 b_type; + u8 b_binding; + const char *b_name; + int expected; + } cases[] =3D { + { 1, STT_FUNC, STB_GLOBAL, "a", 0, STT_FUNC, STB_GLOBAL, "b", SYMBOL_A }, + { 1, STT_NOTYPE, STB_GLOBAL, "a", 1, STT_FUNC, STB_GLOBAL, "b", SYMBOL_B= }, + { 1, STT_FUNC, STB_WEAK, "a", 1, STT_FUNC, STB_GLOBAL, "b", SYMBOL_B }, + { 1, STT_FUNC, STB_GLOBAL, "a", 1, STT_FUNC, STB_LOCAL, "b", SYMBOL_A }, + { 1, STT_FUNC, STB_GLOBAL, "name", 1, STT_FUNC, STB_GLOBAL, "_name", SYM= BOL_A }, + { 1, STT_FUNC, STB_GLOBAL, "a", 1, STT_FUNC, STB_GLOBAL, "long", SYMBOL_= B }, + }; + size_t i; + + for (i =3D 0; i < ARRAY_SIZE(cases); i++) { + struct duplicate_case *c =3D &cases[i]; + + if (symbol__choose_best(c->a_size, c->a_type, c->a_binding, c->a_name, + c->b_size, c->b_type, c->b_binding, c->b_name) !=3D + c->expected) + return TEST_FAIL; + } + return TEST_OK; +} + +#ifdef HAVE_LIBELF_SUPPORT +static int test__symbol_bytes_lazy_name_lookup(struct test_suite *test __m= aybe_unused, + int subtest __maybe_unused) +{ + static const char names[] =3D "first\0second\0"; + unsigned long saved_max =3D symbol_conf.max_symbol_bytes; + size_t baseline =3D symbol__bytes_used(); + char path[] =3D "/tmp/perf-lazy-names-XXXXXX"; + struct dso_ondemand *od =3D NULL; + struct symbol *sym; + struct dso *dso =3D NULL; + struct map *map =3D NULL; + struct rb_node *node; + int nr_symbols =3D 0; + int ret =3D TEST_FAIL; + int fd =3D -1; + + symbol_conf.max_symbol_bytes =3D 0; + fd =3D mkstemp(path); + if (fd < 0 || write(fd, names, sizeof(names)) !=3D (ssize_t)sizeof(names)) + goto out; + close(fd); + fd =3D -1; + + dso =3D dso__new("/not/the/symbol/source"); + od =3D zalloc(sizeof(*od)); + if (!dso || !od) + goto out; + od->sorted =3D zalloc(2 * sizeof(*od->sorted)); + od->data_dso =3D dso__new(path); + if (!od->sorted || !od->data_dso || + dso__data_set_path(od->data_dso, path) < 0) + goto out; + dso__set_binary_type(od->data_dso, DSO_BINARY_TYPE__SYSTEM_PATH_DSO); + od->strtab_size =3D sizeof(names); + od->nr_sorted =3D 2; + od->nr_alloc =3D 2; + od->sorted[0] =3D (struct sym_idx) { + .start =3D 0x10, + .end =3D 0x20, + .name_off =3D 0, + .binding =3D STB_GLOBAL, + .type =3D STT_FUNC, + }; + od->sorted[1] =3D (struct sym_idx) { + .start =3D 0x20, + .end =3D 0x30, + .name_off =3D sizeof("first"), + .binding =3D STB_GLOBAL, + .type =3D STT_FUNC, + }; + if (!symbol__try_account_bytes(od->nr_alloc * sizeof(*od->sorted))) + goto out; + dso__set_ondemand(dso, od); + od =3D NULL; + dso__set_loaded(dso); + map =3D map__new2(0, dso); + if (!map) + goto out; + + sym =3D map__find_symbol(map, 0x11); + if (!sym || strcmp(sym->name, "first")) + goto out; + + dso__data_close(dso__ondemand(dso)->data_dso); + sym =3D map__find_symbol_by_name(map, "second"); + if (!sym || strcmp(sym->name, "second") || dso__ondemand(dso)) + goto out; + + for (node =3D rb_first_cached(dso__symbols(dso)); node; node =3D rb_next(= node)) + nr_symbols++; + if (nr_symbols !=3D 2) + goto out; + ret =3D TEST_OK; +out: + if (fd >=3D 0) + close(fd); + if (map) + map__put(map); + if (dso) + dso__put(dso); + if (od) { + if (od->data_dso) + dso__put(od->data_dso); + free(od->sorted); + free(od); + } + unlink(path); + symbol_conf.max_symbol_bytes =3D saved_max; + if (symbol__bytes_used() !=3D baseline) + ret =3D TEST_FAIL; + return ret; +} +#endif + static struct test_case tests__symbol_bytes[] =3D { TEST_CASE("Long name accounting", symbol_bytes_long_name), TEST_CASE("Concurrent strict reservations", symbol_bytes_reservation), + TEST_CASE("Shared duplicate selection", symbol_bytes_duplicate_selection), +#ifdef HAVE_LIBELF_SUPPORT + TEST_CASE("Lazy address and name lookup", symbol_bytes_lazy_name_lookup), +#endif { .name =3D NULL, } }; =20 diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 42bfe30a3b51..af0440cb8449 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -526,8 +526,11 @@ static void dso__list_add(struct dso *dso) EXCLUSIVE_L= OCKS_REQUIRED(_dso__data_o #ifdef REFCNT_CHECKING dso__data(dso)->dso =3D dso__get(dso); #endif - /* Assume the dso is part of dsos, hence the optional reference count abo= ve. */ - assert(dso__dsos(dso)); + /* + * Most data DSOs belong to a dsos collection. Private data sources, + * such as a lazy symbol index's split-debuginfo file, are instead kept + * alive by their owner. + */ dso__data_open_cnt++; } =20 @@ -572,16 +575,22 @@ char *dso__filename_with_chroot(const struct dso *dso= , const char *filename) static char *dso__get_filename(struct dso *dso, const char *root_dir, bool *decomp) { - char *name =3D malloc(PATH_MAX); + char *name; =20 *decomp =3D false; =20 - if (name =3D=3D NULL) - return NULL; - - if (dso__read_binary_type_filename(dso, dso__binary_type(dso), - root_dir, name, PATH_MAX)) - goto out; + if (dso__data(dso)->path) { + name =3D strdup(dso__data(dso)->path); + if (!name) + return NULL; + } else { + name =3D malloc(PATH_MAX); + if (!name) + return NULL; + if (dso__read_binary_type_filename(dso, dso__binary_type(dso), + root_dir, name, PATH_MAX)) + goto out; + } =20 if (!is_regular_file(name)) { struct stat st; @@ -807,6 +816,17 @@ void dso__data_close(struct dso *dso) mutex_unlock(dso__data_open_lock()); } =20 +int dso__data_set_path(struct dso *dso, const char *path) +{ + char *new_path =3D strdup(path); + + if (!new_path) + return -ENOMEM; + free(dso__data(dso)->path); + dso__data(dso)->path =3D new_path; + return 0; +} + static void try_to_open_dso(struct dso *dso, struct machine *machine) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) { @@ -1674,6 +1694,27 @@ void dso__set_sorted_by_name(struct dso *dso) RC_CHK_ACCESS(dso)->sorted_by_name =3D true; } =20 +void dso__reset_symbol_names(struct dso *dso) +{ + zfree(&RC_CHK_ACCESS(dso)->symbol_names); + RC_CHK_ACCESS(dso)->symbol_names_len =3D 0; + RC_CHK_ACCESS(dso)->sorted_by_name =3D false; +} + +void dso__free_ondemand(struct dso *dso) +{ + struct dso_ondemand *od =3D RC_CHK_ACCESS(dso)->ondemand; + + if (!od) + return; + RC_CHK_ACCESS(dso)->ondemand =3D NULL; + free(od->sorted); + symbol__unaccount_bytes(od->nr_alloc * sizeof(*od->sorted)); + dso__data_close(od->data_dso); + dso__put(od->data_dso); + free(od); +} + struct dso *dso__new_id(const char *name, const struct dso_id *id) { RC_STRUCT(dso) *dso =3D zalloc(sizeof(*dso) + strlen(name) + 1); @@ -1755,7 +1796,11 @@ void dso__delete(struct dso *dso) =20 dso__data_close(dso); auxtrace_cache__free(RC_CHK_ACCESS(dso)->auxtrace_cache); + mutex_lock(dso__lock(dso)); + dso__free_ondemand(dso); + mutex_unlock(dso__lock(dso)); dso_cache__free(dso); + zfree(&RC_CHK_ACCESS(dso)->data.path); dso__free_a2l(dso); dso__free_libdw(dso); dso__free_symsrc_filename(dso); diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index 55c4aaa53c38..301997642e6a 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -264,6 +264,7 @@ struct dso_data { #ifdef REFCNT_CHECKING struct dso *dso; #endif + char *path; int fd; int status; u32 status_seen; @@ -282,6 +283,27 @@ struct dso_bpf_prog { struct perf_env *env; }; =20 +struct sym_idx { + u64 start; /* adjusted st_value (same space as sym->start) */ + u64 end; /* start + st_size, or next start if st_size=3D=3D0 */ + u32 name_off; /* symbol's st_name: offset into the strtab */ + u8 binding; + u8 type; + u8 flags; +}; + +#define SYM_IDX_FLAG_IFUNC_ALIAS (1 << 0) +#define SYM_IDX_FLAG_MATERIALIZED (1 << 1) + +struct dso_ondemand { + struct dso *data_dso; /* exact symbol source, using the DSO data cache */ + u64 strtab_offset; /* file offset of strtab section */ + u64 strtab_size; + struct sym_idx *sorted; + u32 nr_sorted; /* deduped count */ + u32 nr_alloc; /* allocated count, for accounting */ +}; + struct auxtrace_cache; =20 DECLARE_RC_STRUCT(dso) { @@ -308,6 +330,7 @@ DECLARE_RC_STRUCT(dso) { char *symsrc_filename; struct nsinfo *nsinfo; struct auxtrace_cache *auxtrace_cache; + struct dso_ondemand *ondemand; union { /* Tool specific area */ void *priv; u64 db_id; @@ -448,6 +471,16 @@ static inline void dso__set_auxtrace_cache(struct dso = *dso, struct auxtrace_cach RC_CHK_ACCESS(dso)->auxtrace_cache =3D cache; } =20 +static inline struct dso_ondemand *dso__ondemand(struct dso *dso) +{ + return RC_CHK_ACCESS(dso)->ondemand; +} + +static inline void dso__set_ondemand(struct dso *dso, struct dso_ondemand = *od) +{ + RC_CHK_ACCESS(dso)->ondemand =3D od; +} + static inline struct dso_bpf_prog *dso__bpf_prog(struct dso *dso) { return &RC_CHK_ACCESS(dso)->bpf_prog; @@ -823,6 +856,21 @@ int dso__read_binary_type_filename(const struct dso *d= so, enum dso_binary_type t const char *root_dir, char *filename, size_t size); bool is_kernel_module(const char *pathname, int cpumode); bool dso__needs_decompress(struct dso *dso); +struct symbol *dso__find_symbol_ondemand(struct dso *dso, u64 addr) + EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)); +struct symbol *dso__find_symbol_ondemand_exact(struct dso *dso, u64 addr) + EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)); +int dso__materialize_symbols_ondemand(struct dso *dso) + EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)); +const char *dso__read_ondemand_symbol_name(struct dso *data_dso, + u64 strtab_offset, u64 strtab_size, + u64 name_off, char *buf, + size_t buflen, char **to_free, + unsigned int *nr_reads); +void dso__free_ondemand(struct dso *dso) + EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)); +void dso__reset_symbol_names(struct dso *dso) + EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)); int dso__decompress_kmodule_fd(struct dso *dso, const char *name); int dso__decompress_kmodule_path(struct dso *dso, const char *name, char *pathname, size_t len); @@ -896,6 +944,7 @@ bool dso__data_get_fd(struct dso *dso, struct machine *= machine, int *fd) EXCLUSIVE_TRYLOCK_FUNCTION(true, _dso__data_open_lock); void dso__data_put_fd(struct dso *dso) UNLOCK_FUNCTION(_dso__data_open_loc= k); void dso__data_close(struct dso *dso) LOCKS_EXCLUDED(_dso__data_open_lock); +int dso__data_set_path(struct dso *dso, const char *path); =20 int dso__data_file_size(struct dso *dso, struct machine *machine); off_t dso__data_size(struct dso *dso, struct machine *machine); diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 41cdddc987ee..c44698bc3a65 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -382,10 +382,28 @@ int map__load(struct map *map) =20 struct symbol *map__find_symbol(struct map *map, u64 addr) { + struct dso *dso; + struct symbol *sym; + if (map__load(map) < 0) return NULL; =20 - return dso__find_symbol(map__dso(map), addr); + dso =3D map__dso(map); + if (dso__ondemand(dso)) { + /* + * On-demand lookup may materialize and insert a symbol. Keep + * both the lookup and insertion under the DSO lock so another + * thread cannot traverse or modify the rb-tree concurrently. + */ + mutex_lock(dso__lock(dso)); + sym =3D dso__find_symbol(dso, addr); + if (!sym) + sym =3D dso__find_symbol_ondemand(dso, addr); + mutex_unlock(dso__lock(dso)); + } else { + sym =3D dso__find_symbol(dso, addr); + } + return sym; } =20 struct symbol *map__find_symbol_by_name_idx(struct map *map, const char *n= ame, size_t *idx) @@ -396,6 +414,16 @@ struct symbol *map__find_symbol_by_name_idx(struct map= *map, const char *name, s return NULL; =20 dso =3D map__dso(map); + if (dso__ondemand(dso)) { + mutex_lock(dso__lock(dso)); + /* + * Name lookup requires a complete name-sorted array. Preserve + * that API by materializing the remaining address index first. + * An explicit symbol-byte limit can leave a partial set. + */ + dso__materialize_symbols_ondemand(dso); + mutex_unlock(dso__lock(dso)); + } dso__sort_by_name(dso); =20 return dso__find_symbol_by_name(dso, name, idx); diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 2f7ea1499cbf..094d5ffaa1f0 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -12,6 +13,7 @@ #include "libbfd.h" #include "map.h" #include "maps.h" +#include "namespaces.h" #include "symbol.h" #include "symsrc.h" #include "machine.h" @@ -334,6 +336,7 @@ static bool addend_may_be_ifunc(GElf_Ehdr *ehdr, struct= rel_info *ri) =20 static bool get_ifunc_name(Elf *elf, struct dso *dso, GElf_Ehdr *ehdr, struct rel_info *ri, char *buf, size_t buf_sz) + EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)) { u64 addr =3D ri->rela.r_addend; struct symbol *sym; @@ -348,6 +351,8 @@ static bool get_ifunc_name(Elf *elf, struct dso *dso, G= Elf_Ehdr *ehdr, addr -=3D phdr.p_vaddr - phdr.p_offset; =20 sym =3D dso__find_symbol_nocache(dso, addr); + if (!sym && dso__ondemand(dso)) + sym =3D dso__find_symbol_ondemand_exact(dso, addr); =20 /* Expecting the address to be an IFUNC or IFUNC alias */ if (!sym || sym->start !=3D addr || @@ -608,6 +613,26 @@ static int dso__synthesize_plt_got_symbols(struct dso = *dso, Elf *elf, return err; } =20 +static u32 sym_idx__lower_bound(const struct dso_ondemand *od, u64 addr); + +static void dso__clip_ondemand_symbols_at(struct dso *dso, u64 addr) +{ + struct dso_ondemand *od =3D dso__ondemand(dso); + u32 lo, i; + + if (!od) + return; + + lo =3D sym_idx__lower_bound(od, addr); + if (!lo) + return; + + for (i =3D 0; i < lo; i++) { + if (od->sorted[i].end > addr) + od->sorted[i].end =3D addr; + } +} + /* * We need to check if we have a .dynsym, so that we can handle the * .plt, synthesizing its symbols, that aren't on the symtabs (be it @@ -616,6 +641,7 @@ static int dso__synthesize_plt_got_symbols(struct dso *= dso, Elf *elf, * have the PLT data stripped out (shdr_rel_plt.sh_type =3D=3D SHT_NOBITS). */ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) + EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)) { uint32_t idx; GElf_Sym sym; @@ -639,6 +665,13 @@ int dso__synthesize_plt_symbols(struct dso *dso, struc= t symsrc *ss) if (!elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL)) return 0; =20 + /* + * Zero-sized or oversized ELF symbols can have been extended across + * .plt. Clip the index first so lookups cannot attribute PLT addresses + * to a preceding symbol before the synthesized PLT symbols are added. + */ + dso__clip_ondemand_symbols_at(dso, shdr_plt.sh_offset); + /* * A symbol from a previous section (e.g. .init) can have been expanded * by symbols__fixup_end() to overlap .plt. Truncate it before adding @@ -1544,6 +1577,612 @@ static int dso__process_kernel_symbol(struct dso *d= so, struct map *map, return 0; } =20 +static int cmp_sym_idx(const void *a, const void *b) +{ + const struct sym_idx *sa =3D a, *sb =3D b; + + if (sa->start !=3D sb->start) + return sa->start < sb->start ? -1 : 1; + /* + * qsort is not stable. During sorting name_off temporarily holds + * the fill ordinal, preserving eager's symtab insertion order for + * equal-start aliases. It is restored to st_name afterwards. + */ + if (sa->name_off !=3D sb->name_off) + return sa->name_off < sb->name_off ? -1 : 1; + return 0; +} + +/* + * Return the first entry whose start is not less than @addr. ISO C bsearc= h() + * does not provide an insertion point or guarantee the first equal entry,= so + * clipping and exact-start alias lookup use this helper. + */ +static u32 sym_idx__lower_bound(const struct dso_ondemand *od, u64 addr) +{ + u32 lo =3D 0, hi =3D od->nr_sorted; + + while (lo < hi) { + u32 mid =3D lo + (hi - lo) / 2; + + if (od->sorted[mid].start < addr) + lo =3D mid + 1; + else + hi =3D mid; + } + return lo; +} + +static int cmp_addr_to_sym_idx(const void *key, const void *entry) +{ + u64 addr =3D *(const u64 *)key; + const struct sym_idx *idx =3D entry; + + if (addr < idx->start) + return -1; + if (addr >=3D idx->end) + return 1; + return 0; +} + +static bool ondemand_sym_ok(Elf *elf, Elf_Data *secstrs, + const GElf_Sym *sym, u32 sh_link, + uint16_t e_machine) +{ + Elf_Scn *sym_sec; + GElf_Shdr sym_shdr; + int is_label =3D elf_sym__is_label(sym); + const char *name; + + if (!is_label && !elf_sym__filter((GElf_Sym *)sym)) + return false; + + if (sym->st_shndx =3D=3D SHN_ABS) + return false; + + sym_sec =3D elf_getscn(elf, sym->st_shndx); + if (!sym_sec) + return false; + if (!gelf_getshdr(sym_sec, &sym_shdr)) + return false; + if (!(sym_shdr.sh_flags & SHF_ALLOC)) + return false; + + if (is_label && (!secstrs || !elf_sec__filter(&sym_shdr, secstrs))) + return false; + + name =3D elf_strptr(elf, sh_link, sym->st_name); + if (!name) + return false; + + /* + * Reject ARM/AArch64/RISC-V "mapping symbols" ($a/$d/$t/$x), as + * the eager loop does. They are zero-size STT_NOTYPE labels in + * allocated sections that would otherwise be indexed and fill + * forward over real functions, misattributing everything after + * them. + */ + if (e_machine =3D=3D EM_ARM || e_machine =3D=3D EM_AARCH64) { + if (name[0] =3D=3D '$' && strchr("adtx", name[1]) && + (name[2] =3D=3D '\0' || name[2] =3D=3D '.')) + return false; + } + if (e_machine =3D=3D EM_RISCV) { + if (name[0] =3D=3D '$' && strchr("dx", name[1])) + return false; + } + + return true; +} + +static int dso__build_ondemand_index(struct dso *dso, struct symsrc *syms_= ss, + struct symsrc *runtime_ss, + int dynsym) +{ + struct dso_ondemand *od; + Elf *elf =3D syms_ss->elf; + GElf_Ehdr ehdr =3D syms_ss->ehdr; + GElf_Shdr shdr; + GElf_Shdr strshdr; + Elf_Scn *strscn, *sec_strndx; + Elf_Data *syms; + GElf_Sym sym; + Elf_Data *secstrs =3D NULL; + size_t i, index_bytes, reservation_peak; + u32 count =3D 0, j; + u32 *name_offsets; + u64 nr_entries, strtab_offset; + u64 probe_off; + u8 probe; + + /* + * GNU debugdata is backed by a temporary decompressed fd rather than a + * reopenable source path. Keep using the eager loader for that case. + */ + if (syms_ss->type =3D=3D DSO_BINARY_TYPE__GNU_DEBUGDATA) + return 0; + + if (dynsym) + shdr =3D syms_ss->dynshdr; + else + shdr =3D syms_ss->symshdr; + + syms =3D elf_getdata(dynsym ? syms_ss->dynsym : syms_ss->symtab, NULL); + if (!syms) + return -1; + + if (!shdr.sh_entsize) + return 0; + + nr_entries =3D shdr.sh_size / shdr.sh_entsize; + if (nr_entries > UINT32_MAX) + return -EOVERFLOW; + + strscn =3D elf_getscn(elf, shdr.sh_link); + if (!strscn || !gelf_getshdr(strscn, &strshdr)) + return -1; + strtab_offset =3D strshdr.sh_offset; + + /* + * Section name string table, used to match the eager path's + * elf_sec__filter() (text/data section check for STT_NOTYPE labels). + */ + sec_strndx =3D elf_getscn(elf, ehdr.e_shstrndx); + if (sec_strndx) + secstrs =3D elf_getdata(sec_strndx, NULL); + + for (i =3D 0; i < nr_entries; i++) { + if (!gelf_getsym(syms, i, &sym)) + continue; + if (ondemand_sym_ok(elf, secstrs, &sym, shdr.sh_link, + ehdr.e_machine)) + count++; + } + + if (!count) + return 0; + if (check_mul_overflow((size_t)count, sizeof(*od->sorted), + &index_bytes)) + return -EOVERFLOW; + + /* + * Account the index against the symbol memory budget: at 24 + * bytes/symbol it is the dominant on-demand cost and must count + * toward --max-symbol-bytes just like struct symbol allocations do. + */ + if (!symbol__try_account_bytes(index_bytes)) { + symbol_budget_warning(); + return 0; /* fall back to the eager loader's per-symbol budget */ + } + reservation_peak =3D symbol__bytes_used(); + + od =3D zalloc(sizeof(*od)); + if (!od) { + symbol__unaccount_bytes(index_bytes); + return -1; + } + + od->sorted =3D zalloc(index_bytes); + if (!od->sorted) { + symbol__unaccount_bytes(index_bytes); + free(od); + return -1; + } + od->nr_alloc =3D count; /* allocated; the deduped count may shrink */ + name_offsets =3D malloc(count * sizeof(*name_offsets)); + if (!name_offsets) { + symbol__unaccount_bytes(index_bytes); + free(od->sorted); + free(od); + return -1; + } + + j =3D 0; + for (i =3D 0; i < nr_entries; i++) { + u64 adjusted; + GElf_Phdr phdr; + + if (!gelf_getsym(syms, i, &sym)) + continue; + if (!ondemand_sym_ok(elf, secstrs, &sym, shdr.sh_link, + ehdr.e_machine)) + continue; + + adjusted =3D sym.st_value; + + if ((ehdr.e_machine =3D=3D EM_ARM) && + (GELF_ST_TYPE(sym.st_info) =3D=3D STT_FUNC) && + (adjusted & 1)) + --adjusted; + + /* + * Program header adjustment, identical to the eager loop: + * read the PT_LOAD containing the symbol from the runtime + * ELF (the debug-info file may have zeroed p_offset), and + * fall back to the section-header bias when no program + * header matches -- exactly what the eager path does when + * elf_read_program_header fails. + */ + if (elf_read_program_header(runtime_ss->elf, adjusted, + &phdr) =3D=3D 0) { + adjusted -=3D phdr.p_vaddr - phdr.p_offset; + } else { + Elf_Scn *sym_sec =3D elf_getscn(elf, sym.st_shndx); + GElf_Shdr sym_shdr; + + if (sym_sec && gelf_getshdr(sym_sec, &sym_shdr)) + adjusted -=3D sym_shdr.sh_addr - sym_shdr.sh_offset; + } + + od->sorted[j].start =3D adjusted; + od->sorted[j].end =3D sym.st_size; /* st_size for now, converted later */ + /* + * Sort equal-start aliases in original symtab order to match + * rb-tree insertion order. Restore st_name after sorting. + */ + name_offsets[j] =3D sym.st_name; + od->sorted[j].name_off =3D j; + od->sorted[j].binding =3D GELF_ST_BIND(sym.st_info); + od->sorted[j].type =3D GELF_ST_TYPE(sym.st_info); + j++; + } + count =3D j; + if (!count) { + symbol__unaccount_bytes(index_bytes); + free(name_offsets); + free(od->sorted); + free(od); + return 0; + } + + qsort(od->sorted, count, sizeof(*od->sorted), cmp_sym_idx); + for (i =3D 0; i < count; i++) + od->sorted[i].name_off =3D name_offsets[od->sorted[i].name_off]; + free(name_offsets); + + /* + * Match the eager loader's ordering: fill zero-sized ranges before + * choosing among equal-start aliases, so the size preference sees + * the same synthesized lengths as symbols__fixup_duplicate(). + */ + for (i =3D 0; i < count; i++) { + u64 size =3D od->sorted[i].end; /* was st_size */ + + if (size > 0) + od->sorted[i].end =3D od->sorted[i].start + size; + else if (i + 1 < count) + od->sorted[i].end =3D od->sorted[i + 1].start; + else + od->sorted[i].end =3D roundup(od->sorted[i].start, 4096) + 4096; + } + + if (!symbol_conf.allow_aliases) { + u32 out =3D 0; + + for (i =3D 0; i < count; i++) { + u32 best =3D i; + const char *na =3D NULL, *nb; + char *da =3D NULL, *db; + bool has_ifunc =3D od->sorted[i].type =3D=3D STT_GNU_IFUNC; + + na =3D elf_strptr(elf, shdr.sh_link, + od->sorted[best].name_off); + if (na) { + da =3D dso__demangle_sym(dso, 0, na); + if (da) + na =3D da; + } + + for (j =3D i + 1; j < count && + od->sorted[j].start =3D=3D od->sorted[i].start; j++) { + int choice; + + has_ifunc |=3D od->sorted[j].type =3D=3D STT_GNU_IFUNC; + nb =3D elf_strptr(elf, shdr.sh_link, + od->sorted[j].name_off); + if (!na || !nb) + continue; + + db =3D dso__demangle_sym(dso, 0, nb); + if (db) + nb =3D db; + + choice =3D symbol__choose_best( + od->sorted[best].end - + od->sorted[best].start, + od->sorted[best].type, + od->sorted[best].binding, na, + od->sorted[j].end - + od->sorted[j].start, + od->sorted[j].type, + od->sorted[j].binding, nb); + if (choice =3D=3D SYMBOL_B) { + best =3D j; + free(da); + da =3D db; + na =3D nb; + } else { + free(db); + } + } + + free(da); + od->sorted[out++] =3D od->sorted[best]; + if (has_ifunc && od->sorted[out - 1].type !=3D STT_GNU_IFUNC) + od->sorted[out - 1].flags |=3D SYM_IDX_FLAG_IFUNC_ALIAS; + i =3D j - 1; /* skip past all aliases of this start */ + } + + if (out < count) { + struct sym_idx *shrunk; + + shrunk =3D realloc(od->sorted, out * sizeof(*od->sorted)); + if (shrunk) { + od->sorted =3D shrunk; + symbol__unaccount_bytes((od->nr_alloc - out) * + sizeof(*od->sorted)); + od->nr_alloc =3D out; + } + } + count =3D out; + } + + /* + * The interval binary search requires non-overlapping ranges. In + * the default deduplicated mode, prefer the symbol with the nearest + * preceding start when an ELF st_size overlaps the next symbol. + */ + if (!symbol_conf.allow_aliases) { + for (i =3D 0; i + 1 < count; i++) { + if (od->sorted[i].end > od->sorted[i + 1].start) + od->sorted[i].end =3D od->sorted[i + 1].start; + } + } + + /* + * Keep an exact-path data DSO for the symbol source. This may differ + * from the runtime image (for example, split debuginfo), so using the + * primary DSO's data cache could read an unrelated string-table offset. + * The standard DSO data cache manages descriptor eviction and reopening. + */ + od->data_dso =3D dso__new(syms_ss->name); + if (!od->data_dso || + dso__data_set_path(od->data_dso, syms_ss->name) < 0) + goto out_decline_source; + dso__set_binary_type(od->data_dso, DSO_BINARY_TYPE__SYSTEM_PATH_DSO); + dso__set_nsinfo(od->data_dso, nsinfo__get(dso__nsinfo(dso))); + + /* + * Open the managed source while eager fallback is still possible. + * A later failure would otherwise turn materialization into a miss. + */ + if (od->sorted[0].name_off >=3D strshdr.sh_size) + goto out_decline_source; + if (check_add_overflow(strtab_offset, + (u64)od->sorted[0].name_off, &probe_off)) + goto out_decline_source; + if (dso__data_read_offset(od->data_dso, NULL, probe_off, &probe, 1) !=3D = 1) + goto out_decline_source; + + od->strtab_offset =3D strtab_offset; + od->strtab_size =3D strshdr.sh_size; + od->nr_sorted =3D count; + + dso__set_ondemand(dso, od); + + pr_debug("%s: on-demand index: %u symbols (%zu bytes, %zu bytes total) bu= dget=3D%zu\n", + dso__long_name(dso), count, + od->nr_alloc * sizeof(*od->sorted), symbol__bytes_used(), + reservation_peak); + + return 1; + +out_decline_source: + if (od->data_dso) { + dso__data_close(od->data_dso); + dso__put(od->data_dso); + } + symbol__unaccount_bytes(od->nr_alloc * sizeof(*od->sorted)); + free(od->sorted); + free(od); + return 0; +} + +const char *dso__read_ondemand_symbol_name(struct dso *data_dso, + u64 strtab_offset, u64 strtab_size, + u64 name_off, char *buf, + size_t buflen, char **to_free, + unsigned int *nr_reads) +{ + ssize_t n; + u64 remain; + u64 file_off; + size_t cap, want; + + *to_free =3D NULL; + + if (name_off >=3D strtab_size) + return NULL; + if (check_add_overflow(strtab_offset, name_off, &file_off)) + return NULL; + remain =3D strtab_size - name_off; + if (nr_reads) + *nr_reads =3D 0; + + want =3D min((u64)(buflen - 1), remain); + if (nr_reads) + (*nr_reads)++; + n =3D dso__data_read_offset(data_dso, NULL, file_off, (u8 *)buf, want); + if (n <=3D 0) + return NULL; + buf[n] =3D '\0'; + if (memchr(buf, '\0', n)) + return buf; + if ((size_t)n < want) + return NULL; + + cap =3D 4096; + for (;;) { + char *tmp; + + want =3D cap; + if (want > remain) + want =3D remain; + if (want =3D=3D 0) + break; + + tmp =3D *to_free ? realloc(*to_free, want + 1) : malloc(want + 1); + if (!tmp) { + free(*to_free); + *to_free =3D NULL; + return NULL; + } + *to_free =3D tmp; + + if (nr_reads) + (*nr_reads)++; + n =3D dso__data_read_offset(data_dso, NULL, file_off, + (u8 *)*to_free, want); + if (n <=3D 0) { + free(*to_free); + *to_free =3D NULL; + return NULL; + } + (*to_free)[n] =3D '\0'; + + if (memchr(*to_free, '\0', n)) + return *to_free; + if ((size_t)n < want) + break; + + if (want >=3D remain || (u64)n >=3D remain) + break; + + if (cap > SIZE_MAX / 2) + break; + cap *=3D 2; + } + + free(*to_free); + *to_free =3D NULL; + return NULL; +} + +static struct symbol *dso__materialize_symbol_ondemand(struct dso *dso, u3= 2 pos) + EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)) +{ + struct dso_ondemand *od =3D dso__ondemand(dso); + struct sym_idx *idx =3D &od->sorted[pos]; + const char *name; + char namebuf[1024]; + char *name_heap =3D NULL; + char *demangled; + bool budget_exceeded; + struct symbol *s =3D NULL; + + /* + * Check the budget before doing any name I/O or demangling, so an + * over-budget DSO stops paying pread+demangle on every later miss. + */ + if (symbol_conf.max_symbol_bytes && + symbol__bytes_used() >=3D symbol_conf.max_symbol_bytes) { + symbol_budget_warning(); + return NULL; + } + + name =3D dso__read_ondemand_symbol_name(od->data_dso, od->strtab_offset, + od->strtab_size, idx->name_off, + namebuf, sizeof(namebuf), + &name_heap, NULL); + if (!name) + return NULL; + + demangled =3D dso__demangle_sym(dso, 0, name); + if (demangled) + name =3D demangled; + + s =3D symbol__new_bounded(idx->start, idx->end - idx->start, + idx->binding, idx->type, name, &budget_exceeded); + free(demangled); + free(name_heap); + if (!s && budget_exceeded) + symbol_budget_warning(); + if (s) { + if (idx->flags & SYM_IDX_FLAG_IFUNC_ALIAS) + symbol__set_ifunc_alias(s, true); + dso__reset_symbol_names(dso); + __symbols__insert(dso__symbols(dso), s); + idx->flags |=3D SYM_IDX_FLAG_MATERIALIZED; + } + return s; +} + +int dso__materialize_symbols_ondemand(struct dso *dso) +{ + struct dso_ondemand *od =3D dso__ondemand(dso); + u32 i; + + if (!od) + return 0; + for (i =3D 0; i < od->nr_sorted; i++) { + if (od->sorted[i].flags & SYM_IDX_FLAG_MATERIALIZED) + continue; + if (!dso__materialize_symbol_ondemand(dso, i)) + return -1; + } + dso__free_ondemand(dso); + return 0; +} + +struct symbol *dso__find_symbol_ondemand(struct dso *dso, u64 addr) +{ + struct dso_ondemand *od =3D dso__ondemand(dso); + const struct sym_idx *idx; + u32 lo, hi, mid; + + if (!od || !od->sorted || !od->data_dso) + return NULL; + + if (!symbol_conf.allow_aliases) { + idx =3D bsearch(&addr, od->sorted, od->nr_sorted, + sizeof(*od->sorted), cmp_addr_to_sym_idx); + return idx ? dso__materialize_symbol_ondemand(dso, idx - od->sorted) : N= ULL; + } + + lo =3D 0; + hi =3D od->nr_sorted; + while (lo < hi) { + mid =3D (lo + hi) / 2; + if (addr < od->sorted[mid].start) + hi =3D mid; + else if (addr >=3D od->sorted[mid].end) + lo =3D mid + 1; + else + return dso__materialize_symbol_ondemand(dso, mid); + } + return NULL; +} + +struct symbol *dso__find_symbol_ondemand_exact(struct dso *dso, u64 addr) +{ + struct dso_ondemand *od =3D dso__ondemand(dso); + u32 lo, mid; + + if (!od || !od->sorted || !od->data_dso) + return NULL; + + lo =3D sym_idx__lower_bound(od, addr); + if (lo >=3D od->nr_sorted || od->sorted[lo].start !=3D addr) + return NULL; + for (mid =3D lo; mid < od->nr_sorted && + od->sorted[mid].start =3D=3D addr; mid++) { + if (od->sorted[mid].type =3D=3D STT_GNU_IFUNC || + od->sorted[mid].flags & SYM_IDX_FLAG_IFUNC_ALIAS) + return dso__materialize_symbol_ondemand(dso, mid); + } + return dso__materialize_symbol_ondemand(dso, lo); +} + static int dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *sy= ms_ss, struct symsrc *runtime_ss, int kmodule, int dynsym) @@ -1656,6 +2295,34 @@ dso__load_sym_internal(struct dso *dso, struct map *= map, struct symsrc *syms_ss, if (kmodule && adjust_kernel_syms) max_text_sh_offset =3D max_text_section(runtime_ss->elf, &runtime_ss->eh= dr); =20 + /* + * PPC64 ELFv1 function symbols need the eager loop's .opd descriptor + * translation. For symtabs, the selected and runtime sources can differ. + */ + if (symbol_conf.lazy_load_symbols && !dso__kernel(dso) && !kmodule && + !syms_ss->opdsec && (dynsym || !runtime_ss->opdsec)) { + int oret =3D 0; + + if (!dynsym && syms_ss->symtab) + oret =3D dso__build_ondemand_index(dso, syms_ss, + runtime_ss, 0); + else if (dynsym && !dso__ondemand(dso) && syms_ss->dynsym) + oret =3D dso__build_ondemand_index(dso, syms_ss, + runtime_ss, 1); + + /* + * On hard error, propagate it. If an index was built, the + * DSO resolves on demand; skip the eager loop below. If the + * build declined (oret =3D=3D 0, no index -- e.g. no usable + * symbols, or no reopenable data source), continue with the eager + * loop so the DSO still gets symbols. + */ + if (oret < 0) + return oret; + if (dso__ondemand(dso)) + return 1; + } + curr_dso =3D dso__get(dso); elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { struct symbol *f; diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-mini= mal.c index 0a71d1463952..245aed6b2788 100644 --- a/tools/perf/util/symbol-minimal.c +++ b/tools/perf/util/symbol-minimal.c @@ -373,6 +373,23 @@ void symbol__elf_init(void) { } =20 +struct symbol *dso__find_symbol_ondemand(struct dso *dso __maybe_unused, + u64 addr __maybe_unused) +{ + return NULL; +} + +struct symbol *dso__find_symbol_ondemand_exact(struct dso *dso __maybe_unu= sed, + u64 addr __maybe_unused) +{ + return NULL; +} + +int dso__materialize_symbols_ondemand(struct dso *dso __maybe_unused) +{ + return 0; +} + bool filename__has_section(const char *filename __maybe_unused, const char= *sec __maybe_unused) { return false; diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 32eef666f748..3ca9655c36fd 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -143,72 +143,80 @@ int __weak arch__compare_symbol_names_n(const char *n= amea, const char *nameb, return strncmp(namea, nameb, n); } =20 -int __weak arch__choose_best_symbol(struct symbol *syma, - struct symbol *symb __maybe_unused) +int __weak arch__choose_best_symbol(const char *syma_name) { /* Avoid "SyS" kernel syscall aliases */ - if (strlen(syma->name) >=3D 3 && !strncmp(syma->name, "SyS", 3)) + if (strlen(syma_name) >=3D 3 && !strncmp(syma_name, "SyS", 3)) return SYMBOL_B; - if (strlen(syma->name) >=3D 10 && !strncmp(syma->name, "compat_SyS", 10)) + if (strlen(syma_name) >=3D 10 && !strncmp(syma_name, "compat_SyS", 10)) return SYMBOL_B; =20 return SYMBOL_A; } =20 -static int choose_best_symbol(struct symbol *syma, struct symbol *symb) +int symbol__choose_best(u64 a_size, u8 a_type, u8 a_binding, + const char *a_name, + u64 b_size, u8 b_type, u8 b_binding, + const char *b_name) { s64 a; s64 b; size_t na, nb; =20 /* Prefer a symbol with non zero length */ - a =3D syma->end - syma->start; - b =3D symb->end - symb->start; - if ((b =3D=3D 0) && (a > 0)) + if ((b_size =3D=3D 0) && (a_size > 0)) return SYMBOL_A; - else if ((a =3D=3D 0) && (b > 0)) + else if ((a_size =3D=3D 0) && (b_size > 0)) return SYMBOL_B; =20 - if (symbol__type(syma) !=3D symbol__type(symb)) { - if (symbol__type(syma) =3D=3D STT_NOTYPE) + if (a_type !=3D b_type) { + if (a_type =3D=3D STT_NOTYPE) return SYMBOL_B; - if (symbol__type(symb) =3D=3D STT_NOTYPE) + if (b_type =3D=3D STT_NOTYPE) return SYMBOL_A; } =20 /* Prefer a non weak symbol over a weak one */ - a =3D symbol__binding(syma) =3D=3D STB_WEAK; - b =3D symbol__binding(symb) =3D=3D STB_WEAK; + a =3D a_binding =3D=3D STB_WEAK; + b =3D b_binding =3D=3D STB_WEAK; if (b && !a) return SYMBOL_A; if (a && !b) return SYMBOL_B; =20 /* Prefer a global symbol over a non global one */ - a =3D symbol__binding(syma) =3D=3D STB_GLOBAL; - b =3D symbol__binding(symb) =3D=3D STB_GLOBAL; + a =3D a_binding =3D=3D STB_GLOBAL; + b =3D b_binding =3D=3D STB_GLOBAL; if (a && !b) return SYMBOL_A; if (b && !a) return SYMBOL_B; =20 /* Prefer a symbol with less underscores */ - a =3D prefix_underscores_count(syma->name); - b =3D prefix_underscores_count(symb->name); + a =3D prefix_underscores_count(a_name); + b =3D prefix_underscores_count(b_name); if (b > a) return SYMBOL_A; else if (a > b) return SYMBOL_B; =20 /* Choose the symbol with the longest name */ - na =3D strlen(syma->name); - nb =3D strlen(symb->name); + na =3D strlen(a_name); + nb =3D strlen(b_name); if (na > nb) return SYMBOL_A; else if (na < nb) return SYMBOL_B; =20 - return arch__choose_best_symbol(syma, symb); + return arch__choose_best_symbol(a_name); +} + +static int choose_best_symbol(struct symbol *syma, struct symbol *symb) +{ + return symbol__choose_best(syma->end - syma->start, + symbol__type(syma), symbol__binding(syma), syma->name, + symb->end - symb->start, + symbol__type(symb), symbol__binding(symb), symb->name); } =20 void symbols__fixup_duplicate(struct rb_root_cached *symbols) @@ -1989,11 +1997,19 @@ int dso__load(struct dso *dso, struct map *map) } =20 #ifdef HAVE_LIBBFD_SUPPORT +#ifdef HAVE_LIBELF_SUPPORT + if (is_reg && !symbol_conf.lazy_load_symbols) +#else if (is_reg) +#endif bfdrc =3D dso__load_bfd_symbols(dso, name); #endif if (is_reg && bfdrc < 0) sirc =3D symsrc__init(ss, dso, name, symtab_type); +#if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBELF_SUPPORT) + if (is_reg && symbol_conf.lazy_load_symbols && sirc < 0) + bfdrc =3D dso__load_bfd_symbols(dso, name); +#endif =20 if (nsexit) nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index f7331edf0b71..99563c9ff610 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -233,6 +233,10 @@ size_t symbol__bytes_used(void); void symbol__account_bytes(size_t bytes); bool symbol__try_account_bytes(size_t bytes); void symbol__unaccount_bytes(size_t bytes); +int symbol__choose_best(u64 a_size, u8 a_type, u8 a_binding, + const char *a_name, + u64 b_size, u8 b_type, u8 b_binding, + const char *b_name); size_t __symbol__fprintf_symname_offs(const struct symbol *sym, const struct addr_location *al, bool unknown_as_addr, @@ -308,7 +312,7 @@ const char *arch__normalize_symbol_name(const char *nam= e); int arch__compare_symbol_names(const char *namea, const char *nameb); int arch__compare_symbol_names_n(const char *namea, const char *nameb, unsigned int n); -int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb); +int arch__choose_best_symbol(const char *syma_name); =20 enum symbol_tag_include { SYMBOL_TAG_INCLUDE__NONE =3D 0, diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h index 6a16c5badd5e..0f8d044eba20 100644 --- a/tools/perf/util/symbol_conf.h +++ b/tools/perf/util/symbol_conf.h @@ -74,6 +74,7 @@ struct symbol_conf { no_buildid_mmap2, guest_code, lazy_load_kernel_maps, + lazy_load_symbols, keep_exited_threads, annotate_data_member, annotate_data_sample, --=20 Git-157) From nobody Thu Sep 24 21:19:00 2026 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 87E833C2BB0; Sun, 20 Sep 2026 02:34:33 +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=1789871673; cv=none; b=Hu5F+lpt/z7/vXoVcCNSzlrXLKlwkrG7/vc0tyaW/+UrL6HLjvxhsGtq7vPWhqSEffhaOb1Szxs/wgqKEXYNqwa0pXrMjH9BfYqO95DZ4AMNUToashIFN0wUC5WYMo5n83pJIs7vbPNpeVClxzZy1zl5PjrTWvzdeKxeVsF6x8c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789871673; c=relaxed/simple; bh=qhzvN6rhkiv5CQVXBL+/SbtmyGuRfGRdbWsgrCSzPLQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ctSo1m97DXpD5XeA0gg+Qnke3NTlr9D8bqObretbW3nuugaHgzweI1V0wCLeSHZ6T6yim1fmTbHT/SrUCQslR0z51+3C4Ok802ti800qgH6g9VrqRcF0Ru3cLdJfow6o2JHRMLktDfXudnc8pqtwF1DMaE5f4Qnl55bI6JJnr3c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HLOUwGQ/; 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="HLOUwGQ/" Received: by smtp.kernel.org (Postfix) with ESMTPS id 56A03C2BCFF; Sun, 20 Sep 2026 02:34:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789871673; bh=qhzvN6rhkiv5CQVXBL+/SbtmyGuRfGRdbWsgrCSzPLQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=HLOUwGQ/iCxctnPhOXStS9MQ1nLYhCfpZpTdQlI/7iutKDRL0KTP/8J15iM6EN9Dg L3KzIukndA72DFTl52bGOnXABNYJFIt2lNF7Mxhu7zUAAFP2qh8dZUt6S2sBw4E3p5 Hkoy5c8uy7TU44Nb426yG88VyYQnr8jSssAeIJ6xlWU/fJKyNs6M5i24ZJvMZnV973 L9HoQdadJ1c6b7YXJlYC6wkzEtOXHomRMiNAJ5/mbp7XTzJnIDLsROzBixUXsGAEC0 LXMfJowfH/CFx390SACe+XeYnsxWLmjhBbNKhYNLUdj5dyoTuxSPHcA3vw9UQ32keN 09jIzauhgg+QQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43F2DC982E1; Sun, 20 Sep 2026 02:34:33 +0000 (UTC) From: Alireza Haghdoost via B4 Relay Date: Sat, 19 Sep 2026 19:33:59 -0700 Subject: [PATCH v2 4/4] perf test: Test lazy symbol loading and symbol memory limits Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260919-perf-symbol-memory-send-v2-4-495b8f00ad7c@uber.com> References: <20260919-perf-symbol-memory-send-v2-0-495b8f00ad7c@uber.com> In-Reply-To: <20260919-perf-symbol-memory-send-v2-0-495b8f00ad7c@uber.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Alexei Starovoitov , Andrii Nakryiko Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Alireza Haghdoost X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=12656; i=haghdoost@uber.com; h=from:subject:message-id; bh=FHwdij3JAUd6sWksHc6Bee53/mbFuKWZXUTNqN4o0n0=; b=owEBiQJ2/ZANAwAIAVNlBDxl2ALYAcsmYgBqr0Y4pOLzWKOSXtxuTKXzVN6uWYDVyAMpjXaoR x/w08pB4D6JAk8EAAEIADkWIQS5+sFL3gX/8PrA0S1TZQQ8ZdgC2AUCaq9GOBsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwwLDMACgkQU2UEPGXYAthQqA//amo4LdKmAdSEWwtTJxTpy8OIw+diTme c61XgyXDh5EPuHm72HvxozcVFLAaLgfrwgChq+OdnoYcqWxIq97WECN6aFcErglpVIhaQa9BWrR 6g3677Dv8v2iYlON9jda21zSvXSHw13b6VIIbLG2vsApF429qWXj4wmm8880DtPWgjE+N7ryfhv dlk6Y1vQy2MCpaSuNL5DA07NJq1FxK7FADAWlM6A1Jhtnr6DzAm7g06raisQC28oMC34qu9XusJ uLo4fBEpN5MEwpSQ4un4BsH391IUpgIgC5tbmRsuEsFL3MdFjoGTv8DnWZ1HqEE6alhwP1pibsz MGsU9IJA3h97IZuwa1hejDHYqPaxjoE3MP5jgELfUUfHBIpXGJQIxxv8rPViXixAb/z24vwwbuS s7hCT3RB9cXWfrmK6bTgpg0cpEyqyB1plvOQxLezkfFK164ZLD4+kfbmHDQ0KFtxpNIX115cNL8 myzmWpxHk2nKeWKHh27dUT9Zyi6F0UL/U03IlGWvI3NGUM9BrtDDTM5nkgxiVM9JpsvuehRvk+1 jXhgx4KfyWmLHSc5OgTBBZ0CyM+fpJve4k93JNHsTg5yO4KgpXAcIJL7EKy4AS2gZkDNnt4tRTS RWY/Ld3ORFHgLQxhHpMsPDZ62xv4yjSNUBa8LRdm/Cz5tbxL+6oA= X-Developer-Key: i=haghdoost@uber.com; a=openpgp; fpr=B9FAC14BDE05FFF0FAC0D12D5365043C65D802D8 X-Endpoint-Received: by B4 Relay for haghdoost@uber.com/default with auth_id=764 X-Original-From: Alireza Haghdoost Reply-To: haghdoost@uber.com From: Alireza Haghdoost Add a perf script shell test for --lazy-load-symbols and --max-symbol-bytes. Record a small callchain fixture, require evidence that the controlled perf DSO built an on-demand index, and compare only extracted occurrences of the controlled test_loop symbol. This avoids coupling the test to addresses, diagnostics, or architecture-specific symbols for which the eager and lazy loaders have documented differences. Derive the constrained lazy budget from the unlimited run's index-reservation peak. This gives a deterministic boundary where the index fits and later materialization reaches the limit. Also verify eager limiting, malformed size rejection, and one-time warning behavior. Report unsupported recording, missing controlled output, unavailable libelf, and unavailable dependent data as skips without replacing a prior failure. Keep helper returns safe under set -e. Add focused coverage for skip-status preservation and truncated string-table reads in the lazy materialization path. Signed-off-by: Alireza Haghdoost --- tools/perf/tests/shell/script_lazy_load_symbols.sh | 278 +++++++++++++++++= ++++ .../tests/shell/script_lazy_load_symbols_skip.sh | 26 ++ tools/perf/tests/symbol-bytes.c | 58 +++++ 3 files changed, 362 insertions(+) diff --git a/tools/perf/tests/shell/script_lazy_load_symbols.sh b/tools/per= f/tests/shell/script_lazy_load_symbols.sh new file mode 100755 index 000000000000..7e193c1c3d1f --- /dev/null +++ b/tools/perf/tests/shell/script_lazy_load_symbols.sh @@ -0,0 +1,278 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# perf script lazy symbol loading tests (exclusive) +# +# Verifies that --lazy-load-symbols matches the default eager loader for a +# controlled symbol, and that --max-symbol-bytes caps symbol allocations +# (emitting [unknown] plus a warning) without crashing. + +mark_skip() { + if [ "${err}" -eq 0 ]; then + err=3D2 + fi + return 0 +} + +if [ "${PERF_LAZY_LOAD_SYMBOLS_TEST_HELPERS:-}" =3D 1 ]; then + return 0 +fi + +set -e + +shelldir=3D$(dirname "$0") +# shellcheck source=3Dlib/perf_has_symbol.sh +. "${shelldir}"/lib/perf_has_symbol.sh + +testsym=3D"test_loop" +perf_path=3D$(readlink -f "$(command -v perf)") +lazy_index_budget=3D + +skip_test_missing_symbol ${testsym} + +if ! perf check feature -q libelf +then + echo "Lazy symbol loading [Skipped no libelf support]" + exit 2 +fi + +err=3D0 +temp_dir=3D$(mktemp -d /tmp/__perf_test.lazy_load.XXXXX) +perfdata=3D"${temp_dir}/perf.data" +eager_out=3D"${temp_dir}/eager.out" +lazy_out=3D"${temp_dir}/lazy.out" +lazy_err=3D"${temp_dir}/lazy.err" +eager_sym_out=3D"${temp_dir}/eager.sym.out" +lazy_sym_out=3D"${temp_dir}/lazy.sym.out" + +cleanup() { + rm -rf "${temp_dir}" + trap - EXIT TERM INT +} + +trap_cleanup() { + echo "Unexpected signal in ${FUNCNAME[1]}" + cleanup + exit 1 +} +trap trap_cleanup EXIT TERM INT + +test_lazy_load_identical() { + echo "Lazy-load output matches eager loader" + + # Record a small profile with callchains so symbol resolution runs. + if ! perf record -o "${perfdata}" -g -- perf test -w thloop 2> /dev/null + then + echo "Lazy-load identical [Skipped record not supported]" + mark_skip + return 0 + fi + + if ! perf script -i "${perfdata}" 2> /dev/null > "${eager_out}" || \ + ! perf script -v --lazy-load-symbols -i "${perfdata}" \ + 2> "${lazy_err}" > "${lazy_out}" + then + echo "Lazy-load identical [Failed perf script error]" + err=3D1 + return + fi + if ! grep -q "on-demand index:" "${lazy_err}" + then + echo "Lazy-load identical [Failed lazy loader fell back to eager]" + err=3D1 + return + fi + lazy_index_budget=3D$(awk -v dso=3D"${perf_path}: on-demand index:" \ + 'index($0, dso) { sub(/^.* budget=3D/, ""); print; exit }' \ + "${lazy_err}") + case "${lazy_index_budget}" in + ''|*[!0-9]*) + echo "Lazy-load identical [Failed controlled DSO has no index]" + err=3D1 + return + ;; + esac + + # The comparison is only meaningful if something actually resolved; + # two all-[unknown] outputs would also match. + if ! grep -q "${testsym}" "${eager_out}" + then + echo "Lazy-load identical [Skipped no ${testsym} resolved]" + mark_skip + return 0 + fi + + grep -w -o "${testsym}" "${eager_out}" > "${eager_sym_out}" + if ! grep -w -o "${testsym}" "${lazy_out}" > "${lazy_sym_out}" + then + echo "Lazy-load identical [Failed no lazy ${testsym} resolved]" + err=3D1 + return + fi + + if ! cmp -s "${eager_sym_out}" "${lazy_sym_out}" + then + echo "Lazy-load identical [Failed ${testsym} output differs]" + err=3D1 + return + fi + echo "Lazy-load identical [Success]" +} + +test_max_symbol_bytes() { + echo "--max-symbol-bytes budget enforcement" + + # Depends on ${perfdata} from test_lazy_load_identical. + if [ ! -s "${perfdata}" ] + then + echo "--max-symbol-bytes budget [Skipped record not supported]" + mark_skip + return 0 + fi + + # A tiny budget forces most symbols to be dropped as [unknown], + # with a single warning, and must not crash. + if ! perf script --max-symbol-bytes=3D1K -i "${perfdata}" > /dev/null \ + 2> "${temp_dir}/budget.err" + then + echo "--max-symbol-bytes budget [Failed nonzero exit]" + err=3D1 + return + fi + if ! grep -q "symbol memory budget exceeded" "${temp_dir}/budget.err" + then + echo "--max-symbol-bytes budget [Failed missing warning]" + err=3D1 + return + fi + if perf script --max-symbol-bytes=3D1Kjunk -i "${perfdata}" \ + > /dev/null 2>&1 + then + echo "--max-symbol-bytes budget [Failed malformed size accepted]" + err=3D1 + return + fi + if ! perf script --max-symbol-bytes=3D0 -i "${perfdata}" \ + > /dev/null 2>&1 + then + echo "--max-symbol-bytes budget [Failed zero not accepted]" + err=3D1 + return + fi + + # The unlimited run logged the peak accounted bytes at the controlled + # DSO's index reservation, before alias dedup may have released bytes. + # Reuse that peak as the budget: deterministic index construction fits, + # while subsequent materialization must hit the limit. + if ! perf script -v --lazy-load-symbols \ + --max-symbol-bytes=3D"${lazy_index_budget}B" \ + -i "${perfdata}" > /dev/null 2> "${temp_dir}/lazy-budget.err" + then + echo "--max-symbol-bytes lazy budget [Failed nonzero exit]" + err=3D1 + return + fi + if ! grep -Fq "${perf_path}: on-demand index:" \ + "${temp_dir}/lazy-budget.err" || + ! grep -q "symbol memory budget exceeded" "${temp_dir}/lazy-budget.err" + then + echo "--max-symbol-bytes lazy budget [Failed no indexed budget case]" + err=3D1 + return + fi + warnings=3D$(grep -c "symbol memory budget exceeded" \ + "${temp_dir}/lazy-budget.err" || true) + if [ "${warnings}" -ne 1 ] + then + echo "--max-symbol-bytes lazy budget [Failed warning count: ${warnings}]" + err=3D1 + return + fi + echo "--max-symbol-bytes budget [Success]" +} + +test_budget_truncation_range() { + local longsym + local first_symbol + local trunc_source=3D"${temp_dir}/truncation.S" + local trunc_binary=3D"${temp_dir}/truncation" + local trunc_data=3D"${temp_dir}/truncation.data" + local trunc_out=3D"${temp_dir}/truncation.out" + local trunc_err=3D"${temp_dir}/truncation.err" + + echo "--max-symbol-bytes truncation range" + + if [ "$(uname -m)" !=3D x86_64 ]; then + echo "--max-symbol-bytes truncation range [Skipped x86_64 only]" + mark_skip + return 0 + fi + + longsym=3D$(printf 's%.0s' {1..900}) + cat > "${trunc_source}" < /dev/null + then + echo "--max-symbol-bytes truncation range [Skipped record not supported]" + mark_skip + return 0 + fi + if ! perf script --max-symbol-bytes=3D1K -i "${trunc_data}" -F ip,sym,dso= \ + > "${trunc_out}" 2> "${trunc_err}" + then + echo "--max-symbol-bytes truncation range [Failed perf script error]" + err=3D1 + return + fi + + if ! grep -q "symbol memory budget exceeded" "${trunc_err}" || + ! grep -F "${trunc_binary}" "${trunc_out}" | grep -q '\[unknown\]' || + grep -Fq "${longsym}" "${trunc_out}" + then + echo "--max-symbol-bytes truncation range [Failed omitted range resolved= ]" + err=3D1 + return + fi + echo "--max-symbol-bytes truncation range [Success]" +} + +test_lazy_load_identical +test_max_symbol_bytes +test_budget_truncation_range + +cleanup +exit $err diff --git a/tools/perf/tests/shell/script_lazy_load_symbols_skip.sh b/tool= s/perf/tests/shell/script_lazy_load_symbols_skip.sh new file mode 100755 index 000000000000..136503863fdd --- /dev/null +++ b/tools/perf/tests/shell/script_lazy_load_symbols_skip.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# perf script lazy symbol loading skip status + +set -e + +shelldir=3D$(dirname "$0") +PERF_LAZY_LOAD_SYMBOLS_TEST_HELPERS=3D1 +. "${shelldir}"/script_lazy_load_symbols.sh +unset PERF_LAZY_LOAD_SYMBOLS_TEST_HELPERS + +err=3D0 +mark_skip +if [ "${err}" -ne 2 ]; then + echo "Lazy-load skip status [Failed expected 2, got ${err}]" + exit 1 +fi + +err=3D1 +mark_skip +if [ "${err}" -ne 1 ]; then + echo "Lazy-load skip status [Failed skip overwrote failure: ${err}]" + exit 1 +fi + +echo "Lazy-load skip status [Success]" diff --git a/tools/perf/tests/symbol-bytes.c b/tools/perf/tests/symbol-byte= s.c index e5e2e6b9db97..0c9424fe4d1c 100644 --- a/tools/perf/tests/symbol-bytes.c +++ b/tools/perf/tests/symbol-bytes.c @@ -172,6 +172,63 @@ static int test__symbol_bytes_duplicate_selection(stru= ct test_suite *test __mayb } =20 #ifdef HAVE_LIBELF_SUPPORT +static int truncated_name_case(size_t file_size, unsigned int expected_rea= ds) +{ + char path[] =3D "/tmp/perf-lazy-truncated-XXXXXX"; + struct dso *data_dso =3D NULL; + char *contents =3D NULL; + char *name_heap =3D NULL; + char namebuf[1024]; + const char *name; + unsigned int nr_reads; + int ret =3D TEST_FAIL; + int fd =3D -1; + + contents =3D malloc(file_size); + if (!contents) + goto out; + memset(contents, 'a', file_size); + + fd =3D mkstemp(path); + if (fd < 0 || write(fd, contents, file_size) !=3D (ssize_t)file_size) + goto out; + close(fd); + fd =3D -1; + + data_dso =3D dso__new(path); + if (!data_dso || dso__data_set_path(data_dso, path) < 0) + goto out; + dso__set_binary_type(data_dso, DSO_BINARY_TYPE__SYSTEM_PATH_DSO); + name =3D dso__read_ondemand_symbol_name(data_dso, 0, 8192, 0, + namebuf, sizeof(namebuf), + &name_heap, &nr_reads); + if (name || name_heap || nr_reads !=3D expected_reads) + goto out; + ret =3D TEST_OK; +out: + if (fd >=3D 0) + close(fd); + if (data_dso) + dso__put(data_dso); + unlink(path); + free(name_heap); + free(contents); + return ret; +} + +static int test__symbol_bytes_truncated_name(struct test_suite *test __may= be_unused, + int subtest __maybe_unused) +{ + /* + * One byte is short in the stack-buffer read. 1023 bytes fills it + * exactly, so the following read exercises the heap-buffer path. + */ + if (truncated_name_case(1, 1) !=3D TEST_OK || + truncated_name_case(1023, 2) !=3D TEST_OK) + return TEST_FAIL; + return TEST_OK; +} + static int test__symbol_bytes_lazy_name_lookup(struct test_suite *test __m= aybe_unused, int subtest __maybe_unused) { @@ -271,6 +328,7 @@ static struct test_case tests__symbol_bytes[] =3D { TEST_CASE("Concurrent strict reservations", symbol_bytes_reservation), TEST_CASE("Shared duplicate selection", symbol_bytes_duplicate_selection), #ifdef HAVE_LIBELF_SUPPORT + TEST_CASE("Truncated lazy symbol names", symbol_bytes_truncated_name), TEST_CASE("Lazy address and name lookup", symbol_bytes_lazy_name_lookup), #endif { .name =3D NULL, } --=20 Git-157)