Re: [PATCH v2 2/4] perf script: Add --max-symbol-bytes to bound ELF symbol memory
From: Namhyung Kim
Date: Sun Sep 20 2026 - 20:03:27 EST
On Sat, Sep 19, 2026 at 07:33:57PM -0700, Alireza Haghdoost via B4 Relay wrote:
> From: Alireza Haghdoost <haghdoost@xxxxxxxx>
>
> 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 <haghdoost@xxxxxxxx>
> ---
[SNIP]
> 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;
Why is this needed? Do you have symbols with a really long name?
Anyway, it should be a separate change then.
Thanks,
Namhyung
> _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);
>
> 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);
> };
>
>
> --
> Git-157)
>
>