perf symbol: Remove psw_idle() from list of idle symbols
Subsystems (2)
PATCH v2 1/1 perf symbol: Remove psw_idle() from list of idle symbols
Commit fa2ae4a377c0 ("s390/idle: Rewrite psw_idle() in C")
removes symbols psw_idle() and psw_idle_exit() from the linux kernel for s390. Remove them in perf tool's list of idle functions. They can not be detected anymore.
--- tools/perf/util/symbol.c | 2 -- 1 file changed, 2 deletions(-)
tools/perf/util/symbol.c +0 -2
@@ -752,8 +752,6 @@ static bool symbol__is_idle(const char *name)"poll_idle" ,"ppc64_runlatch_off" ,"pseries_dedicated_idle_sleep" , -"psw_idle" , -"psw_idle_exit" , NULL };int i;
Signature · 1 line
2.53.0
Quoted 8 lines · Thomas Richter
Commit
fa2ae4a377c0("s390/idle: Rewritepsw_idle()in C")removes symbols
psw_idle()andpsw_idle_exit()from the linux kernel for s390. Remove them in perf tool's list of idle functions. They can not be detected anymore.Signed-off-by Thomas Richter <[email protected]>Suggested-by Heiko Carstens <[email protected]>
Namhyung Kim
maintainerQuoted 5 lines · Thomas Richter
Commit
fa2ae4a377c0("s390/idle: Rewritepsw_idle()in C")removes symbols
psw_idle()andpsw_idle_exit()from the linux kernel for s390. Remove them in perf tool's list of idle functions. They can not be detected anymore.
But I think old kernels may still run somewhere. It seems the above commit was merged to v6.10. Maybe we should wait some more time before removing it in the tool.
Thanks,
Namhyung
Quoted 23 lines
Signed-off-by Thomas Richter <[email protected]>Suggested-by Heiko Carstens <[email protected]>--- tools/perf/util/symbol.c | 2 -- 1 file changed, 2 deletions(-)tools/perf/util/symbol.c +0
-2@@ -752,8 +752,6 @@ static bool symbol__is_idle(const char *name)"poll_idle" ,"ppc64_runlatch_off" ,"pseries_dedicated_idle_sleep" , -"psw_idle" , -"psw_idle_exit" , NULL };int i;Signature · 1 line
2.53.0
Quoted 10 lines · Namhyung Kim
Quoted 5 lines · Thomas Richter
Commit
fa2ae4a377c0("s390/idle: Rewritepsw_idle()in C")removes symbols
psw_idle()andpsw_idle_exit()from the linux kernel for s390. Remove them in perf tool's list of idle functions. They can not be detected anymore.But I think old kernels may still run somewhere. It seems the above commit was merged to v6.10. Maybe we should wait some more time before removing it in the tool.
Right, people keep asking if one can use a new version of perf on an old kernel and vice versa.
So I think we should not apply this patch.
There has been efforts in the past to try to have have some info per sample indicating the "context" for a sample, if it was in idle processing, hard/soft irq processing, etc, but that didn't come to fruition so far.
With that we could get rid of this flaky heuristic of looking at a symbol name.
- Arnaldo
Quoted 11 lines
Thanks,
NamhyungQuoted 7 lines
Signed-off-by Thomas Richter <[email protected]>Suggested-by Heiko Carstens <[email protected]>--- tools/perf/util/symbol.c | 2 -- 1 file changed, 2 deletions(-)
Quoted 16 lines
Quoted 16 lines
tools/perf/util/symbol.c +0
-2@@ -752,8 +752,6 @@ static bool symbol__is_idle(const char *name)"poll_idle" ,"ppc64_runlatch_off" ,"pseries_dedicated_idle_sleep" , -"psw_idle" , -"psw_idle_exit" , NULL };int i;Signature · 1 line
2.53.0
- Arnaldo
Arnaldo Carvalho de Melo
maintainerQuoted 10 lines · Namhyung Kim
Quoted 5 lines · Thomas Richter
Commit
fa2ae4a377c0("s390/idle: Rewritepsw_idle()in C")removes symbols
psw_idle()andpsw_idle_exit()from the linux kernel for s390. Remove them in perf tool's list of idle functions. They can not be detected anymore.But I think old kernels may still run somewhere. It seems the above commit was merged to v6.10. Maybe we should wait some more time before removing it in the tool.
Agreed, using a new perf tool, say built from the tarballs made available at:
https://www.kernel.org/pub/linux/kernel/tools/perf/v7.0.0/perf-7.0.0-rc1.tar.xz
(I will not make a rc2 available since there are no changes to the tools/perf codebase in this rc).
On older kernels should still ignore those functions.
A suggestion for work in this area instead is to get those samples into a special bucket, the "idle" one, and show it at some place in the screen.
Thanks,
- Arnaldo
Ian Rogers
reviewerQuoted 18 lines · Arnaldo Carvalho de Melo
Quoted 10 lines · Namhyung Kim
Quoted 5 lines · Thomas Richter
Commit
fa2ae4a377c0("s390/idle: Rewritepsw_idle()in C")removes symbols
psw_idle()andpsw_idle_exit()from the linux kernel for s390. Remove them in perf tool's list of idle functions. They can not be detected anymore.But I think old kernels may still run somewhere. It seems the above commit was merged to v6.10. Maybe we should wait some more time before removing it in the tool.
Agreed, using a new perf tool, say built from the tarballs made available at:
https://www.kernel.org/pub/linux/kernel/tools/perf/v7.0.0/perf-7.0.0-rc1.tar.xz
Quoted 8 lines
(I will not make a rc2 available since there are no changes to the
tools/perfcodebase in this rc).On older kernels should still ignore those functions.
A suggestion for work in this area instead is to get those samples into a special bucket, the "idle" one, and show it at some place in the screen.
Would it also be sensible to pass the perf_env:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/env.h?h=perf-tools-next#n74
into symbol__is_idle? The contents of the perf_env are shown by perf report --header:
``` # ======== # captured on : Mon Mar 2 11:34:47 2026 # header version : 1 # data offset : 904 # data size : 4268216 # feat offset : 4269120 # hostname : google.com # os release : 6.17.13-1rodete1-amd64 # perf version : 7.0.rc1.g982b63f6380b # arch : x86_64 # nrcpus online : 28 # nrcpus avail : 28 # cpudesc : Intel(R) Core(TM) i7-14700 # cpuid : GenuineIntel,6,183,1 ... # e_machine : 62 # e_flags : 0 ... ```
The kernel version is in the release and the e_machine/arch captures the CPU type.
Thanks,
Ian
Quoted 4 lines
Thanks,
- Arnaldo
Arnaldo Carvalho de Melo
maintainerQuoted 53 lines · Ian Rogers
Quoted 18 lines · Arnaldo Carvalho de Melo
Quoted 10 lines · Namhyung Kim
Quoted 5 lines · Thomas Richter
Commit
fa2ae4a377c0("s390/idle: Rewritepsw_idle()in C")removes symbols
psw_idle()andpsw_idle_exit()from the linux kernel for s390. Remove them in perf tool's list of idle functions. They can not be detected anymore.But I think old kernels may still run somewhere. It seems the above commit was merged to v6.10. Maybe we should wait some more time before removing it in the tool.
Agreed, using a new perf tool, say built from the tarballs made available at:
https://www.kernel.org/pub/linux/kernel/tools/perf/v7.0.0/perf-7.0.0-rc1.tar.xz
Quoted 8 lines
(I will not make a rc2 available since there are no changes to the
tools/perfcodebase in this rc).On older kernels should still ignore those functions.
A suggestion for work in this area instead is to get those samples into a special bucket, the "idle" one, and show it at some place in the screen.
Would it also be sensible to pass the
perf_env:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/env.h?h=perf-tools-next#n74
intosymbol__is_idle? The contents of theperf_envare shown byperf report --header:``` # ======== # captured on : Mon Mar 2 11:34:47 2026 # header version : 1 # data offset : 904 # data size : 4268216 # feat offset : 4269120 # hostname : google.com # os release : 6.17.13-1rodete1-amd64 # perf version : 7.0.rc1.g982b63f6380b # arch : x86_64 # nrcpus online : 28 # nrcpus avail : 28 # cpudesc : Intel(R) Core(TM) i7-14700 # cpuid : GenuineIntel,6,183,1 ... # e_machine : 62 # e_flags : 0 ... ```
Quoted 2 lines
The kernel version is in the release and the e_machine/arch captures the CPU type.
Yeah, I think it is a good improvement, I think you mean that we should have per-arch idle symbol lists?
- Arnaldo
Ian Rogers
reviewerPATCH 1/1 perf symbol: Lazily compute idle and use the perf_env
In reply to Thomas RichterMove the idle boolean to a helper symbol__is_idle function. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. As symbols__insert no longer needs to know if a symbol is for the kernel, remove the argument.
This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/
--- tools/perf/builtin-top.c | 6 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 106 ++++++++++++++++++++++------------- tools/perf/util/symbol.h | 15 +++-- 4 files changed, 85 insertions(+), 44 deletions(-)
tools/perf/builtin-top.c +5 -1
@@ -750,6 +750,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, { struct perf_top *top = container_of(tool, struct perf_top, tool);struct addr_location al; +struct dso *dso =NULL ;if (!machine && perf_guest) {static struct intlist *seen;
@@ -829,7 +830,10 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !al.sym->idle) { +if (al.map) + dso =map__dso (al.map); + +if (al.sym ==NULL || !symbol__is_idle (al.sym, dso, machine->env)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,
tools/perf/util/symbol-elf.c +1 -1
@@ -1725,7 +1725,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);
tools/perf/util/symbol.c +68 -38
174 lines not on this page · raw message
tools/perf/util/symbol.h +11 -4
@@ -24,6 +24,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/*
@@ -41,6 +42,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index).
@@ -56,8 +63,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle. */ + enum symbol_idle_kind idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */
@@ -184,8 +191,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);
@@ -269,5 +275,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,const struct perf_env *env);#endif /* __PERF_SYMBOL */
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Ian Rogers
reviewerRe: [PATCH v1] perf symbol: Lazily compute idle and use the perf_env
Quoted 7 lines · Ian Rogers
Move the idle boolean to a helper
symbol__is_idlefunction. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. Assymbols__insertno longer needs to know if a symbol is for the kernel, remove the argument.
Quoted 6 lines
This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/Signed-off-by Ian Rogers <[email protected]>
Ping.
Thanks,
Ian
Quoted 286 lines
--- tools/perf/builtin-top.c | 6 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 106 ++++++++++++++++++++++------------- tools/perf/util/symbol.h | 15 +++-- 4 files changed, 85 insertions(+), 44 deletions(-)tools/perf/builtin-top.c +5
-1@@ -750,6 +750,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, { struct perf_top *top = container_of(tool, struct perf_top, tool);struct addr_location al; +struct dso *dso =NULL ;if (!machine && perf_guest) {static struct intlist *seen;@@ -829,7 +830,10 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !al.sym->idle) { +if (al.map) + dso =map__dso (al.map); + +if (al.sym ==NULL || !symbol__is_idle (al.sym, dso, machine->env)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,tools/perf/util/symbol-elf.c +1
-1@@ -1725,7 +1725,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);tools/perf/util/symbol.c +68
-38@@ -25,6 +25,8 @@#include "demangle-ocaml.h" #include "demangle-rust-v0.h" #include "dso.h" +#include "dwarf-regs.h" +#include "env.h" #include "util.h" // lsdir() #include "debug.h" #include "event.h" @@ -51,7 +53,6 @@static int dso__load_kernel_sym (struct dso *dso,struct map *map);static int dso__load_guest_kernel_sym (struct dso *dso,struct map *map); -static bool symbol__is_idle (const char *name);int vmlinux_path__nr_entries;char **vmlinux_path;@@ -357,8 +358,7 @@ void symbols__delete(struct rb_root_cached *symbols) } } -void __symbols__insert (struct rb_root_cached *symbols, -struct symbol *sym,bool kernel) +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym) {struct rb_node **p = &symbols->rb_root.rb_node;struct rb_node *parent =NULL ;@@ -366,17 +366,6 @@ void __symbols__insert(struct rb_root_cached *symbols,struct symbol *s;bool leftmost = true; -if (kernel) { -const char *name = sym->name; -/* -* ppc64 uses function descriptors and appends a '.' to the -* start of every instruction address. Remove it. -*/ -if (name[0 ] =='.' ) - name++; - sym->idle =symbol__is_idle (name); - } -while (*p !=NULL ) { parent = *p; s = rb_entry(parent, struct symbol, rb_node);@@ -393,7 +382,7 @@ void __symbols__insert(struct rb_root_cached *symbols,void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym) { -__symbols__insert (symbols, sym, false); +__symbols__insert (symbols, sym); }static struct symbol *symbols__find (struct rb_root_cached *symbols,u64 ip)@@ -554,7 +543,7 @@ void dso__reset_find_symbol_cache(struct dso *dso)void dso__insert_symbol (struct dso *dso,struct symbol *sym) { -__symbols__insert (dso__symbols (dso), sym,dso__kernel (dso)); +__symbols__insert (dso__symbols (dso), sym);/* update the symbol cache if necessary */ if (dso__last_find_result_addr(dso) >= sym->start &&@@ -716,47 +705,88 @@ int modules__parse(const char *filename, void *arg,return err; } +static int sym_name_cmp (const void *a,const void *b) +{ +const char *name = a; +const char *const *sym = b; + +return strcmp (name, *sym); +} +/* * These are symbols in the kernel image, so make sure that * sym is from a kernel DSO. */ -static bool symbol__is_idle (const char *name) +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,const struct perf_env *env) { -const char *const idle_symbols[] = { +static const char *const idle_symbols[] = {"acpi_idle_do_entry" ,"acpi_processor_ffh_cstate_enter" ,"arch_cpu_idle" ,"cpu_idle" ,"cpu_startup_entry" , -"idle_cpu" , -"intel_idle" , -"intel_idle_ibrs" ,"default_idle" , -"native_safe_halt" ,"enter_idle" ,"exit_idle" , -"mwait_idle" , -"mwait_idle_with_hints" , -"mwait_idle_with_hints.constprop.0" , +"idle_cpu" , +"native_safe_halt" ,"poll_idle" , -"ppc64_runlatch_off" ,"pseries_dedicated_idle_sleep" , -"psw_idle" , -"psw_idle_exit" , -NULL }; -int i; -static struct strlist *idle_symbols_list; +const char *name = sym->name; +uint16_t e_machine = env ? env->e_machine :EM_HOST ; + +if (sym->idle) +return sym->idle ==SYMBOL_IDLE__IDLE ; + +if (!dso ||dso__kernel (dso) ==DSO_SPACE__USER ) { + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; + } -if (idle_symbols_list) -return strlist__has_entry (idle_symbols_list, name); +/* +* ppc64 uses function descriptors and appends a '.' to the +* start of every instruction address. Remove it. +*/ +if (name[0 ] =='.' ) + name++; + + +if (bsearch (name, idle_symbols,ARRAY_SIZE (idle_symbols), +sizeof (idle_symbols[0 ]), sym_name_cmp)) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + +if (e_machine ==EM_386 || e_machine ==EM_X86_64 ) { +if (strstarts (name,"mwait_idle" ) || +strstarts (name,"intel_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + +if (e_machine ==EM_PPC64 &&!strcmp (name,"ppc64_runlatch_off" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } - idle_symbols_list =strlist__new (NULL ,NULL ); +if (e_machine ==EM_S390 ) { +int major =0 , minor =0 ; +const char *release = env && env->os_release + ? env->os_release : perf_version_string; -for (i =0 ; idle_symbols[i]; i++) -strlist__add (idle_symbols_list, idle_symbols[i]); +sscanf (release,"%d.%d" , &major, &minor); -return strlist__has_entry (idle_symbols_list, name); +/* Before v6.10, s390 used psw_idle. */ +if ((major <6 || (major ==6 && minor <10 )) &&strstarts (name,"psw_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; } static int map__process_kallsym_symbol(void *arg, const char *name,@@ -785,7 +815,7 @@ static int map__process_kallsym_symbol(void *arg, const char *name,* We will pass the symbols to the filter later, in * map__split_kallsyms, when we have split the maps per module */ -__symbols__insert (root, sym, !strchr (name,'[' )); +__symbols__insert (root, sym);return 0 ; }tools/perf/util/symbol.h +11
-5@@ -24,6 +24,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/* @@ -41,6 +42,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index). @@ -56,8 +63,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle. */ + enum symbol_idle_kind idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */ @@ -184,8 +191,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);@@ -269,5 +275,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,const struct perf_env *env);#endif /* __PERF_SYMBOL */ --2.53.0.473.g4a7958ca14-goog
Namhyung Kim
maintainerRe: [PATCH v1] perf symbol: Lazily compute idle and use the perf_env
Hi Ian,
Sorry for the delay.
Quoted 1 line · Ian Rogers
On Mon, Mar 2, 2026 at 3:43 PM Ian Rogers <[email protected]> wrote:
[SNIP]
Quoted 7 lines
tools/perf/util/symbol.c int modules__parse(const char *filename, void *arg, · line 751
Quoted 7 lines
tools/perf/util/symbol.c +5
-2-if (idle_symbols_list) -return strlist__has_entry (idle_symbols_list, name); +/* +* ppc64 uses function descriptors and appends a '.' to the +* start of every instruction address. Remove it. +*/ +if (name[0 ] =='.' )
Then e_machine == EM_PPC64 can be checked here.
Quoted 3 lines
tools/perf/util/symbol.c int modules__parse(const char *filename, void *arg, · line 754
Quoted 3 lines
tools/perf/util/symbol.c +3
-0+ name++; + +
Two blank lines.
Quoted 76 lines
Quoted 76 lines
tools/perf/util/symbol.c +33
-5+if (bsearch (name, idle_symbols,ARRAY_SIZE (idle_symbols), +sizeof (idle_symbols[0 ]), sym_name_cmp)) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + +if (e_machine ==EM_386 || e_machine ==EM_X86_64 ) { +if (strstarts (name,"mwait_idle" ) || +strstarts (name,"intel_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + +if (e_machine ==EM_PPC64 &&!strcmp (name,"ppc64_runlatch_off" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } - idle_symbols_list =strlist__new (NULL ,NULL ); +if (e_machine ==EM_S390 ) { +int major =0 , minor =0 ; +const char *release = env && env->os_release + ? env->os_release : perf_version_string; -for (i =0 ; idle_symbols[i]; i++) -strlist__add (idle_symbols_list, idle_symbols[i]); +sscanf (release,"%d.%d" , &major, &minor); -return strlist__has_entry (idle_symbols_list, name); +/* Before v6.10, s390 used psw_idle. */ +if ((major <6 || (major ==6 && minor <10 )) &&strstarts (name,"psw_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; } static int map__process_kallsym_symbol(void *arg, const char *name,@@ -785,7 +815,7 @@ static int map__process_kallsym_symbol(void *arg, const char *name,* We will pass the symbols to the filter later, in * map__split_kallsyms, when we have split the maps per module */ -__symbols__insert (root, sym, !strchr (name,'[' )); +__symbols__insert (root, sym);return 0 ; }tools/perf/util/symbol.h +7
-0@@ -24,6 +24,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/* @@ -41,6 +42,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index).
Quoted 8 lines
tools/perf/util/symbol.h struct symbol { · line 67
Quoted 8 lines
tools/perf/util/symbol.h +2
-2@@ -56,8 +63,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle. */ + enum symbol_idle_kind idle:2;
I'm curious if bitfields with different types (u8 and enum) can be placed consecutively bitwise. There can be a lot of symbols so it could be a concern.
Thanks,
Namhyung
Quoted 23 lines
Quoted 23 lines
tools/perf/util/symbol.h +2
-3/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */ @@ -184,8 +191,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);@@ -269,5 +275,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,const struct perf_env *env);#endif /* __PERF_SYMBOL */ --2.53.0.473.g4a7958ca14-goog
Ian Rogers
reviewerRe: [PATCH v1] perf symbol: Lazily compute idle and use the perf_env
Quoted 16 lines · Namhyung Kim
Hi Ian,
Sorry for the delay.
Quoted 1 line · Ian Rogers
On Mon, Mar 2, 2026 at 3:43 PM Ian Rogers <[email protected]> wrote:
[SNIP]
Quoted 7 lines
tools/perf/util/symbol.c int modules__parse(const char *filename, void *arg, · line 751
Quoted 7 lines
tools/perf/util/symbol.c +5
-2-if (idle_symbols_list) -return strlist__has_entry (idle_symbols_list, name); +/* +* ppc64 uses function descriptors and appends a '.' to the +* start of every instruction address. Remove it. +*/ +if (name[0 ] =='.' )
Quoted 1 line
Then
e_machine==EM_PPC64can be checked here.
Agreed, but potentially this is load bearing for more than just PPC so I'd rather leave it as it is.
Quoted 5 lines
Quoted 3 lines
tools/perf/util/symbol.c int modules__parse(const char *filename, void *arg, · line 754
Quoted 3 lines
tools/perf/util/symbol.c +3
-0+ name++; + +Two blank lines.
Will fix in v2.
Quoted 80 lines
Quoted 80 lines
Quoted 80 lines
tools/perf/util/symbol.c +33
-5+if (bsearch (name, idle_symbols,ARRAY_SIZE (idle_symbols), +sizeof (idle_symbols[0 ]), sym_name_cmp)) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + +if (e_machine ==EM_386 || e_machine ==EM_X86_64 ) { +if (strstarts (name,"mwait_idle" ) || +strstarts (name,"intel_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + +if (e_machine ==EM_PPC64 &&!strcmp (name,"ppc64_runlatch_off" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } - idle_symbols_list =strlist__new (NULL ,NULL ); +if (e_machine ==EM_S390 ) { +int major =0 , minor =0 ; +const char *release = env && env->os_release + ? env->os_release : perf_version_string; -for (i =0 ; idle_symbols[i]; i++) -strlist__add (idle_symbols_list, idle_symbols[i]); +sscanf (release,"%d.%d" , &major, &minor); -return strlist__has_entry (idle_symbols_list, name); +/* Before v6.10, s390 used psw_idle. */ +if ((major <6 || (major ==6 && minor <10 )) &&strstarts (name,"psw_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; } static int map__process_kallsym_symbol(void *arg, const char *name,@@ -785,7 +815,7 @@ static int map__process_kallsym_symbol(void *arg, const char *name,* We will pass the symbols to the filter later, in * map__split_kallsyms, when we have split the maps per module */ -__symbols__insert (root, sym, !strchr (name,'[' )); +__symbols__insert (root, sym);return 0 ; }tools/perf/util/symbol.h +7
-0@@ -24,6 +24,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/* @@ -41,6 +42,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index). @@ -56,8 +63,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4;
Quoted 8 lines
Quoted 4 lines
tools/perf/util/symbol.h struct symbol { · line 67
Quoted 4 lines
tools/perf/util/symbol.h +2
-2-/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle. */ + enum symbol_idle_kind idle:2;I'm curious if bitfields with different types (u8 and enum) can be placed consecutively bitwise. There can be a lot of symbols so it could be a concern.
pahole says no size difference:
Before:
```
struct symbol {
struct rb_node rb_node
__attribute__((__aligned__(8))); /* 0 24 */
u64 start; /* 24 8 */
u64 end; /* 32 8 */
u16 namelen; /* 40 2 */
u8 type:4; /* 42: 0 1 */
u8 binding:4; /* 42: 4 1 */
u8 idle:1; /* 43: 0 1 */
u8 ignore:1; /* 43: 1 1 */
u8 inlined:1; /* 43: 2 1 */
u8 annotate2:1; /* 43: 3 1 */
u8 ifunc_alias:1; /* 43: 4 1 */
/* XXX 3 bits hole, try to pack */
u8 arch_sym; /* 44 1 */
char name[]; /* 45 0 */
/* size: 48, cachelines: 1, members: 13 */
/* sum members: 43 */
/* sum bitfield members: 13 bits, bit holes: 1, sum bit holes: 3 bits */
/* padding: 3 */
/* forced alignments: 1 */
/* last cacheline: 48 bytes */
} __attribute__((__aligned__(8)));
```After:
```
struct symbol {
struct rb_node rb_node
__attribute__((__aligned__(8))); /* 0 24 */
u64 start; /* 24 8 */
u64 end; /* 32 8 */
u16 namelen; /* 40 2 */
u8 type:4; /* 42: 0 1 */
u8 binding:4; /* 42: 4 1 */
/* Bitfield combined with previous fields */
enum symbol_idle_kind idle:2; /* 40:24 4 */
/* Bitfield combined with next fields */
u8 ignore:1; /* 43: 2 1 */
u8 inlined:1; /* 43: 3 1 */
u8 annotate2:1; /* 43: 4 1 */
u8 ifunc_alias:1; /* 43: 5 1 */
/* XXX 2 bits hole, try to pack */
u8 arch_sym; /* 44 1 */
char name[]; /* 45 0 */
/* size: 48, cachelines: 1, members: 13 */
/* sum members: 43 */
/* sum bitfield members: 14 bits, bit holes: 1, sum bit holes: 2 bits */
/* padding: 3 */
/* forced alignments: 1 */
/* last cacheline: 48 bytes */
} __attribute__((__aligned__(8)));
```Thanks,
Ian
Quoted 27 lines
Thanks,
NamhyungQuoted 23 lines
Quoted 23 lines
tools/perf/util/symbol.h +2
-3/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */ @@ -184,8 +191,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);@@ -269,5 +275,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,const struct perf_env *env);#endif /* __PERF_SYMBOL */ --2.53.0.473.g4a7958ca14-goog
Ian Rogers
reviewerPATCH v2 1/1 perf symbol: Lazily compute idle and use the perf_env
In reply to Ian RogersMove the idle boolean to a helper symbol__is_idle function. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. As symbols__insert no longer needs to know if a symbol is for the kernel, remove the argument.
This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/
The change switches x86 matches to use strstarts which means intel_idle_irq is matched as part of strstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/
---
v1: https://lore.kernel.org/lkml/[email protected]/
--- tools/perf/builtin-top.c | 6 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 105 ++++++++++++++++++++++------------- tools/perf/util/symbol.h | 15 +++-- 4 files changed, 84 insertions(+), 44 deletions(-)
tools/perf/builtin-top.c +5 -1
@@ -751,6 +751,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, { struct perf_top *top = container_of(tool, struct perf_top, tool);struct addr_location al; +struct dso *dso =NULL ;if (!machine && perf_guest) {static struct intlist *seen;
@@ -830,7 +831,10 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !al.sym->idle) { +if (al.map) + dso =map__dso (al.map); + +if (al.sym ==NULL || !symbol__is_idle (al.sym, dso, machine->env)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,
tools/perf/util/symbol-elf.c +1 -1
@@ -1723,7 +1723,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);
tools/perf/util/symbol.c +67 -38
173 lines not on this page · raw message
tools/perf/util/symbol.h +11 -4
@@ -25,6 +25,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/*
@@ -42,6 +43,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index).
@@ -57,8 +64,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle. */ + enum symbol_idle_kind idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */
@@ -202,8 +209,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);
@@ -286,5 +292,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,const struct perf_env *env);#endif /* __PERF_SYMBOL */
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Re: [PATCH v2] perf symbol: Lazily compute idle and use the perf_env
Hi Ian,
Quoted 198 lines · Ian Rogers
Move the idle boolean to a helper
symbol__is_idlefunction. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. Assymbols__insertno longer needs to know if a symbol is for the kernel, remove the argument.This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/The change switches x86 matches to use strstarts which means
intel_idle_irqis matched as part ofstrstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/Signed-off-by Ian Rogers <[email protected]>---
v1: https://lore.kernel.org/lkml/[email protected]/--- tools/perf/builtin-top.c | 6 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 105 ++++++++++++++++++++++------------- tools/perf/util/symbol.h | 15 +++-- 4 files changed, 84 insertions(+), 44 deletions(-)tools/perf/builtin-top.c +5
-1@@ -751,6 +751,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, { struct perf_top *top = container_of(tool, struct perf_top, tool);struct addr_location al; +struct dso *dso =NULL ;if (!machine && perf_guest) {static struct intlist *seen;@@ -830,7 +831,10 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !al.sym->idle) { +if (al.map) + dso =map__dso (al.map); + +if (al.sym ==NULL || !symbol__is_idle (al.sym, dso, machine->env)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,tools/perf/util/symbol-elf.c +1
-1@@ -1723,7 +1723,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);tools/perf/util/symbol.c +30
-36@@ -25,6 +25,8 @@#include "demangle-ocaml.h" #include "demangle-rust-v0.h" #include "dso.h" +#include "dwarf-regs.h" +#include "env.h" #include "util.h" // lsdir() #include "event.h" #include "machine.h" @@ -50,7 +52,6 @@static int dso__load_kernel_sym (struct dso *dso,struct map *map);static int dso__load_guest_kernel_sym (struct dso *dso,struct map *map); -static bool symbol__is_idle (const char *name);int vmlinux_path__nr_entries;char **vmlinux_path;@@ -357,8 +358,7 @@ void symbols__delete(struct rb_root_cached *symbols) } } -void __symbols__insert (struct rb_root_cached *symbols, -struct symbol *sym,bool kernel) +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym) {struct rb_node **p = &symbols->rb_root.rb_node;struct rb_node *parent =NULL ;@@ -366,17 +366,6 @@ void __symbols__insert(struct rb_root_cached *symbols,struct symbol *s;bool leftmost = true; -if (kernel) { -const char *name = sym->name; -/* -* ppc64 uses function descriptors and appends a '.' to the -* start of every instruction address. Remove it. -*/ -if (name[0 ] =='.' ) - name++; - sym->idle =symbol__is_idle (name); - } -while (*p !=NULL ) { parent = *p; s = rb_entry(parent, struct symbol, rb_node);@@ -393,7 +382,7 @@ void __symbols__insert(struct rb_root_cached *symbols,void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym) { -__symbols__insert (symbols, sym, false); +__symbols__insert (symbols, sym); }static struct symbol *symbols__find (struct rb_root_cached *symbols,u64 ip)@@ -554,7 +543,7 @@ void dso__reset_find_symbol_cache(struct dso *dso)void dso__insert_symbol (struct dso *dso,struct symbol *sym) { -__symbols__insert (dso__symbols (dso), sym,dso__kernel (dso)); +__symbols__insert (dso__symbols (dso), sym);/* update the symbol cache if necessary */ if (dso__last_find_result_addr(dso) >= sym->start &&@@ -716,47 +705,87 @@ int modules__parse(const char *filename, void *arg,return err; } +static int sym_name_cmp (const void *a,const void *b) +{ +const char *name = a; +const char *const *sym = b; + +return strcmp (name, *sym); +} +/* * These are symbols in the kernel image, so make sure that * sym is from a kernel DSO. */ -static bool symbol__is_idle (const char *name) +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,const struct perf_env *env) { -const char *const idle_symbols[] = { +static const char *const idle_symbols[] = {"acpi_idle_do_entry" ,"acpi_processor_ffh_cstate_enter" ,"arch_cpu_idle" ,"cpu_idle" ,"cpu_startup_entry" , -"idle_cpu" , -"intel_idle" , -"intel_idle_ibrs" ,"default_idle" , -"native_safe_halt" ,"enter_idle" ,"exit_idle" , -"mwait_idle" , -"mwait_idle_with_hints" , -"mwait_idle_with_hints.constprop.0" , +"idle_cpu" , +"native_safe_halt" ,"poll_idle" , -"ppc64_runlatch_off" ,"pseries_dedicated_idle_sleep" , -"psw_idle" , -"psw_idle_exit" , -NULL }; -int i; -static struct strlist *idle_symbols_list; +const char *name = sym->name; +uint16_t e_machine = env ? env->e_machine :EM_HOST ; -if (idle_symbols_list) -return strlist__has_entry (idle_symbols_list, name); +if (sym->idle) +return sym->idle ==SYMBOL_IDLE__IDLE ; - idle_symbols_list =strlist__new (NULL ,NULL ); +if (!dso ||dso__kernel (dso) ==DSO_SPACE__USER ) { + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; + } -for (i =0 ; idle_symbols[i]; i++) -strlist__add (idle_symbols_list, idle_symbols[i]); +/* +* ppc64 uses function descriptors and appends a '.' to the +* start of every instruction address. Remove it. +*/ +if (name[0 ] =='.' )
tools/perf/util/symbol.c int modules__parse(const char *filename, void *arg, · line 760
Quoted 10 lines
tools/perf/util/symbol.c +8
-1+ name++; -return strlist__has_entry (idle_symbols_list, name); +if (bsearch (name, idle_symbols,ARRAY_SIZE (idle_symbols), +sizeof (idle_symbols[0 ]), sym_name_cmp)) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + +if (e_machine ==EM_386 || e_machine ==EM_X86_64 ) {
As said in anther thread, intel_idle_irq was still there on my test machine. I did a bit debug and found e_machine == 0 so it couldn't run into this branch. After dig more, it should be deliver_event()->perf_session__find_machine() return a struct machine whose env->e_machine is 0. I'm still busy today to do more, wish this clue can help.
Thanks,
Honglei
Quoted 93 lines
tools/perf/util/symbol.c +29
-1+if (strstarts (name,"mwait_idle" ) || +strstarts (name,"intel_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + +if (e_machine ==EM_PPC64 && !strcmp (name,"ppc64_runlatch_off" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + +if (e_machine ==EM_S390 ) { +int major =0 , minor =0 ; +const char *release = env && env->os_release + ? env->os_release : perf_version_string; + +sscanf (release,"%d.%d" , &major, &minor); + +/* Before v6.10, s390 used psw_idle. */ +if ((major <6 || (major ==6 && minor <10 )) &&strstarts (name,"psw_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; } static int map__process_kallsym_symbol(void *arg, const char *name,@@ -785,7 +814,7 @@ static int map__process_kallsym_symbol(void *arg, const char *name,* We will pass the symbols to the filter later, in * map__split_kallsyms, when we have split the maps per module */ -__symbols__insert (root, sym, !strchr (name,'[' )); +__symbols__insert (root, sym);return 0 ; }tools/perf/util/symbol.h +11
-4@@ -25,6 +25,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/* @@ -42,6 +43,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index). @@ -57,8 +64,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle. */ + enum symbol_idle_kind idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */ @@ -202,8 +209,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);@@ -286,5 +292,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,const struct perf_env *env);#endif /* __PERF_SYMBOL */
Ian Rogers
reviewerRe: [PATCH v2] perf symbol: Lazily compute idle and use the perf_env
Quoted 204 lines · Honglei Wang
Hi Ian,
Quoted 200 lines · Ian Rogers
Move the idle boolean to a helper
symbol__is_idlefunction. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. Assymbols__insertno longer needs to know if a symbol is for the kernel, remove the argument.This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/The change switches x86 matches to use strstarts which means
intel_idle_irqis matched as part ofstrstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/Signed-off-by Ian Rogers <[email protected]>---
v1: https://lore.kernel.org/lkml/[email protected]/--- tools/perf/builtin-top.c | 6 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 105 ++++++++++++++++++++++------------- tools/perf/util/symbol.h | 15 +++-- 4 files changed, 84 insertions(+), 44 deletions(-)tools/perf/builtin-top.c +5
-1@@ -751,6 +751,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, { struct perf_top *top = container_of(tool, struct perf_top, tool);struct addr_location al; +struct dso *dso =NULL ;if (!machine && perf_guest) {static struct intlist *seen;@@ -830,7 +831,10 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !al.sym->idle) { +if (al.map) + dso =map__dso (al.map); + +if (al.sym ==NULL || !symbol__is_idle (al.sym, dso, machine->env)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,tools/perf/util/symbol-elf.c +1
-1@@ -1723,7 +1723,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);tools/perf/util/symbol.c +31
-36@@ -25,6 +25,8 @@#include "demangle-ocaml.h" #include "demangle-rust-v0.h" #include "dso.h" +#include "dwarf-regs.h" +#include "env.h" #include "util.h" // lsdir() #include "event.h" #include "machine.h" @@ -50,7 +52,6 @@static int dso__load_kernel_sym (struct dso *dso,struct map *map);static int dso__load_guest_kernel_sym (struct dso *dso,struct map *map); -static bool symbol__is_idle (const char *name);int vmlinux_path__nr_entries;char **vmlinux_path;@@ -357,8 +358,7 @@ void symbols__delete(struct rb_root_cached *symbols) } } -void __symbols__insert (struct rb_root_cached *symbols, -struct symbol *sym,bool kernel) +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym) {struct rb_node **p = &symbols->rb_root.rb_node;struct rb_node *parent =NULL ;@@ -366,17 +366,6 @@ void __symbols__insert(struct rb_root_cached *symbols,struct symbol *s;bool leftmost = true; -if (kernel) { -const char *name = sym->name; -/* -* ppc64 uses function descriptors and appends a '.' to the -* start of every instruction address. Remove it. -*/ -if (name[0 ] =='.' ) - name++; - sym->idle =symbol__is_idle (name); - } -while (*p !=NULL ) { parent = *p; s = rb_entry(parent, struct symbol, rb_node);@@ -393,7 +382,7 @@ void __symbols__insert(struct rb_root_cached *symbols,void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym) { -__symbols__insert (symbols, sym, false); +__symbols__insert (symbols, sym); }static struct symbol *symbols__find (struct rb_root_cached *symbols,u64 ip)@@ -554,7 +543,7 @@ void dso__reset_find_symbol_cache(struct dso *dso)void dso__insert_symbol (struct dso *dso,struct symbol *sym) { -__symbols__insert (dso__symbols (dso), sym,dso__kernel (dso)); +__symbols__insert (dso__symbols (dso), sym);/* update the symbol cache if necessary */ if (dso__last_find_result_addr(dso) >= sym->start &&@@ -716,47 +705,87 @@ int modules__parse(const char *filename, void *arg,return err; } +static int sym_name_cmp (const void *a,const void *b) +{ +const char *name = a; +const char *const *sym = b; + +return strcmp (name, *sym); +} +/* * These are symbols in the kernel image, so make sure that * sym is from a kernel DSO. */ -static bool symbol__is_idle (const char *name) +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,const struct perf_env *env) { -const char *const idle_symbols[] = { +static const char *const idle_symbols[] = {"acpi_idle_do_entry" ,"acpi_processor_ffh_cstate_enter" ,"arch_cpu_idle" ,"cpu_idle" ,"cpu_startup_entry" , -"idle_cpu" , -"intel_idle" , -"intel_idle_ibrs" ,"default_idle" , -"native_safe_halt" ,"enter_idle" ,"exit_idle" , -"mwait_idle" , -"mwait_idle_with_hints" , -"mwait_idle_with_hints.constprop.0" , +"idle_cpu" , +"native_safe_halt" ,"poll_idle" , -"ppc64_runlatch_off" ,"pseries_dedicated_idle_sleep" , -"psw_idle" , -"psw_idle_exit" , -NULL }; -int i; -static struct strlist *idle_symbols_list; +const char *name = sym->name; +uint16_t e_machine = env ? env->e_machine :EM_HOST ; -if (idle_symbols_list) -return strlist__has_entry (idle_symbols_list, name); +if (sym->idle) +return sym->idle ==SYMBOL_IDLE__IDLE ; - idle_symbols_list =strlist__new (NULL ,NULL ); +if (!dso ||dso__kernel (dso) ==DSO_SPACE__USER ) { + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; + } -for (i =0 ; idle_symbols[i]; i++) -strlist__add (idle_symbols_list, idle_symbols[i]); +/* +* ppc64 uses function descriptors and appends a '.' to the +* start of every instruction address. Remove it. +*/ +if (name[0 ] =='.' ) + name++;
Quoted 15 lines
tools/perf/util/symbol.c int modules__parse(const char *filename, void *arg, · line 760
Quoted 8 lines
tools/perf/util/symbol.c +7
-1-return strlist__has_entry (idle_symbols_list, name); +if (bsearch (name, idle_symbols,ARRAY_SIZE (idle_symbols), +sizeof (idle_symbols[0 ]), sym_name_cmp)) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + +if (e_machine ==EM_386 || e_machine ==EM_X86_64 ) {As said in anther thread,
intel_idle_irqwas still there on my test machine. I did a bit debug and founde_machine== 0 so it couldn't run into this branch. After dig more, it should be deliver_event()->perf_session__find_machine() return a struct machine whoseenv->e_machineis 0. I'm still busy today to do more, wish this clue can help.
I can see this, the env's e_machine isn't being lazily initialized for the host like the arch is. I'll add a patch for this.
Thanks,
Ian
Quoted 97 lines
Thanks,
HongleiQuoted 93 lines
tools/perf/util/symbol.c +29
-1+if (strstarts (name,"mwait_idle" ) || +strstarts (name,"intel_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + +if (e_machine ==EM_PPC64 && !strcmp (name,"ppc64_runlatch_off" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + +if (e_machine ==EM_S390 ) { +int major =0 , minor =0 ; +const char *release = env && env->os_release + ? env->os_release : perf_version_string; + +sscanf (release,"%d.%d" , &major, &minor); + +/* Before v6.10, s390 used psw_idle. */ +if ((major <6 || (major ==6 && minor <10 )) &&strstarts (name,"psw_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; } static int map__process_kallsym_symbol(void *arg, const char *name,@@ -785,7 +814,7 @@ static int map__process_kallsym_symbol(void *arg, const char *name,* We will pass the symbols to the filter later, in * map__split_kallsyms, when we have split the maps per module */ -__symbols__insert (root, sym, !strchr (name,'[' )); +__symbols__insert (root, sym);return 0 ; }tools/perf/util/symbol.h +11
-4@@ -25,6 +25,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/* @@ -42,6 +43,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index). @@ -57,8 +64,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle. */ + enum symbol_idle_kind idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */ @@ -202,8 +209,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);@@ -286,5 +292,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,const struct perf_env *env);#endif /* __PERF_SYMBOL */
Ian Rogers
reviewerv3 cover perf symbol/env: ELF machine clean up and lazy idle computation
Add a helper to perf_env to compute the e_machine if it is EM_NONE. Derive the value from the arch string if available. Similarly derive the arch string from the ELF machine if available, for consistency. This means perf's arch (machine type) is no longer determined by uname but set to match that of the perf ELF executable.
Switch the idle computation to the point of use and lazily compute it, rather than computing it for every symbol. The current only user is perf top. At the point of use the perf_env is available and this can be used to make sure the idle function computation is machine and kernel version dependent.
v3: Properly set up the e_machine coming from the perf_env as reported
by Honglei Wang.
v2: Some minor white space clean up:
https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
Ian Rogers (2):
perf env: Add perf_env__e_machine helper and use in perf_env__arch perf symbol: Lazily compute idle and use the perf_env
tools/perf/builtin-top.c | 6 +- tools/perf/util/env.c | 179 +++++++++++++++++++++++++++-------- tools/perf/util/env.h | 1 + tools/perf/util/session.c | 14 +-- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 105 ++++++++++++-------- tools/perf/util/symbol.h | 15 ++- 7 files changed, 235 insertions(+), 87 deletions(-)
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Ian Rogers
reviewerPATCH v3 1/2 perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back of EM_HOST. Use the perf_env's arch to compute the e_machine if available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switch perf_env__arch to be derived the e_machine for consistency. This switches arch from being uname derived to matching that of the perf binary (via EM_HOST). Update session to use the helper, which may mean using EM_HOST when no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.
--- tools/perf/util/env.c | 179 ++++++++++++++++++++++++++++++-------- tools/perf/util/env.h | 1 + tools/perf/util/session.c | 14 +-- 3 files changed, 151 insertions(+), 43 deletions(-)
tools/perf/util/env.c +142 -37
@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #include "cpumap.h" +#include "dwarf-regs.h" #include "debug.h" #include "env.h" #include "util/header.h" #include "util/rwsem.h" #include <linux/compiler.h> +#include <linux/kernel.h> #include <linux/ctype.h> #include <linux/rbtree.h> #include <linux/string.h>
@@ -588,51 +590,154 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)zfree (&cache->size); } +struct arch_to_e_machine { +const char *prefix; +uint16_t e_machine; +}; +/* -* Return architecture name in a normalized form. -* The conversion logic comes from the Makefile. +* A mapping from an arch prefix string to an ELF machine that can be used in a +* bsearch. Some arch prefixes are shared an need additional processing as +* marked next to the architecture. The prefixes handle both perf's architecture +* naming and those from uname. */ -static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch; +static const struct arch_to_e_machine prefix_to_e_machine[] = { + {"aarch64" ,EM_AARCH64 }, + {"alpha" ,EM_ALPHA }, + {"arc" ,EM_ARC }, + {"arm" ,EM_ARM },/* Check also for EM_AARCH64. */ + {"avr" ,EM_AVR },/* Check also for EM_AVR32. */ + {"bfin" ,EM_BLACKFIN }, + {"blackfin" ,EM_BLACKFIN }, + {"cris" ,EM_CRIS }, + {"csky" ,EM_CSKY }, + {"hppa" ,EM_PARISC }, + {"i386" ,EM_386 }, + {"i486" ,EM_386 }, + {"i586" ,EM_386 }, + {"i686" ,EM_386 }, + {"loongarch" ,EM_LOONGARCH }, + {"m32r" ,EM_M32R }, + {"m68k" ,EM_68K }, + {"microblaze" ,EM_MICROBLAZE }, + {"mips" ,EM_MIPS }, + {"msp430" ,EM_MSP430 }, + {"parisc" ,EM_PARISC }, + {"powerpc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"ppc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"riscv" ,EM_RISCV }, + {"sa110" ,EM_ARM }, + {"s390" ,EM_S390 }, + {"sh" ,EM_SH }, + {"sparc" ,EM_SPARC },/* Check also for EM_SPARCV9. */ + {"sun4u" ,EM_SPARC }, + {"x86" ,EM_X86_64 },/* Check also for EM_386. */ + {"xtensa" ,EM_XTENSA }, +}; + +static int compare_prefix (const void *key,const void *element) +{ +const char *search_key = key; +const struct arch_to_e_machine *map_element = element; +size_t prefix_len =strlen (map_element->prefix); + +return strncmp (search_key, map_element->prefix, prefix_len); +} + +static uint16_t perf_arch_to_e_machine (const char *perf_arch,bool is_64_bit) +{ +/* Binary search for a matching prefix. */ +const struct arch_to_e_machine *result; + +if (!perf_arch) +return EM_HOST ; + + result =bsearch (perf_arch, + prefix_to_e_machine,ARRAY_SIZE (prefix_to_e_machine), +sizeof (prefix_to_e_machine[0 ]), + compare_prefix); + +if (!result) { +pr_debug ("Unknown perf arch for ELF machine mapping: %s\n" , perf_arch); +return EM_NONE ; + } + +/* Handle conflicting prefixes. */ +switch (result->e_machine) { +case EM_ARM : +return !strcmp (perf_arch,"arm64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +return is_64_bit ||strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ; +case EM_SPARC : +return is_64_bit || !strcmp (perf_arch,"sparc64" ) ?EM_SPARCV9 :EM_SPARC ; +case EM_X86_64 : +return is_64_bit || !strcmp (perf_arch,"x86_64" ) ?EM_X86_64 :EM_386 ; +default : +return result->e_machine; + } +} + +static const char *e_machine_to_perf_arch (uint16_t e_machine) +{ +/* +* Table for if either the perf arch string differs from uname or there +* are >1 ELF machine with the prefix. +*/ +static const struct arch_to_e_machine extras[] = { + {"arm64" ,EM_AARCH64 }, + {"avr32" ,EM_AVR32 }, + {"powerpc" ,EM_PPC }, + {"powerpc" ,EM_PPC64 }, + {"sparc" ,EM_SPARCV9 }, + {"x86" ,EM_386 }, + {"x86" ,EM_X86_64 }, + {"none" ,EM_NONE }, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (extras); i++) { +if (extras[i].e_machine == e_machine) +return extras[i].prefix; + } + +for (size_t i =0 ; i <ARRAY_SIZE (prefix_to_e_machine); i++) { +if (prefix_to_e_machine[i].e_machine == e_machine) +return prefix_to_e_machine[i].prefix; + + } +return "unknown" ; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +if (!env) { +if (e_flags) + *e_flags =EF_HOST ; + +return EM_HOST ; + } +if (env->e_machine ==EM_NONE ) { + env->e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit); + +if (env->e_machine ==EM_HOST ) + env->e_flags =EF_HOST ; + } +if (e_flags) + *e_flags =EF_HOST ; + +return env->e_machine; }const char *perf_env__arch (struct perf_env *env) { -char *arch_name; +if (!env) +return e_machine_to_perf_arch (EM_HOST ); -if (!env || !env->arch) {/* Assume local operation */ -static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +if (!env->arch) + env->arch =strdup (e_machine_to_perf_arch (perf_env__e_machine (env,/*e_flags=*/ NULL ))); -return normalize_arch (arch_name); +return env->arch; }#if defined(HAVE_LIBTRACEEVENT )
tools/perf/util/env.h +1 -0
@@ -186,6 +186,7 @@ int perf_env__read_cpu_topology_map(struct perf_env *env);void cpu_cache_level__free (struct cpu_cache_level *cache); +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env);const char *perf_env__arch_strerrno (struct perf_env *env,int err);const char *perf_env__cpuid (struct perf_env *env);
tools/perf/util/session.c +8 -6
@@ -2996,14 +2996,16 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* Is the env caching an e_machine? */ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Ian Rogers
reviewerPATCH v3 2/2 perf symbol: Lazily compute idle and use the perf_env
In reply to Ian RogersMove the idle boolean to a helper symbol__is_idle function. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. As symbols__insert no longer needs to know if a symbol is for the kernel, remove the argument.
This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/
The change switches x86 matches to use strstarts which means intel_idle_irq is matched as part of strstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/
--- tools/perf/builtin-top.c | 6 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 105 ++++++++++++++++++++++------------- tools/perf/util/symbol.h | 15 +++-- 4 files changed, 84 insertions(+), 44 deletions(-)
tools/perf/builtin-top.c +5 -1
@@ -751,6 +751,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, { struct perf_top *top = container_of(tool, struct perf_top, tool);struct addr_location al; +struct dso *dso =NULL ;if (!machine && perf_guest) {static struct intlist *seen;
@@ -830,7 +831,10 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !al.sym->idle) { +if (al.map) + dso =map__dso (al.map); + +if (al.sym ==NULL || !symbol__is_idle (al.sym, dso, machine->env)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,
tools/perf/util/symbol-elf.c +1 -1
@@ -1723,7 +1723,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);
tools/perf/util/symbol.c +67 -38
173 lines not on this page · raw message
tools/perf/util/symbol.h +11 -4
@@ -25,6 +25,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/*
@@ -42,6 +43,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index).
@@ -57,8 +64,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle. */ + enum symbol_idle_kind idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */
@@ -202,8 +209,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);
@@ -286,5 +292,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,struct perf_env *env);#endif /* __PERF_SYMBOL */
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Re: [PATCH v3 2/2] perf symbol: Lazily compute idle and use the perf_env
Hi Ian,
FYI. It works on my icx machine with 'perf top'.
Thanks,
Honglei
Quoted 299 lines · Ian Rogers
Move the idle boolean to a helper
symbol__is_idlefunction. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. Assymbols__insertno longer needs to know if a symbol is for the kernel, remove the argument.This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/The change switches x86 matches to use strstarts which means
intel_idle_irqis matched as part ofstrstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/Signed-off-by Ian Rogers <[email protected]>--- tools/perf/builtin-top.c | 6 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 105 ++++++++++++++++++++++------------- tools/perf/util/symbol.h | 15 +++-- 4 files changed, 84 insertions(+), 44 deletions(-)tools/perf/builtin-top.c +5
-1@@ -751,6 +751,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, { struct perf_top *top = container_of(tool, struct perf_top, tool);struct addr_location al; +struct dso *dso =NULL ;if (!machine && perf_guest) {static struct intlist *seen;@@ -830,7 +831,10 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !al.sym->idle) { +if (al.map) + dso =map__dso (al.map); + +if (al.sym ==NULL || !symbol__is_idle (al.sym, dso, machine->env)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,tools/perf/util/symbol-elf.c +1
-1@@ -1723,7 +1723,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);tools/perf/util/symbol.c +67
-38@@ -25,6 +25,8 @@#include "demangle-ocaml.h" #include "demangle-rust-v0.h" #include "dso.h" +#include "dwarf-regs.h" +#include "env.h" #include "util.h" // lsdir() #include "event.h" #include "machine.h" @@ -50,7 +52,6 @@static int dso__load_kernel_sym (struct dso *dso,struct map *map);static int dso__load_guest_kernel_sym (struct dso *dso,struct map *map); -static bool symbol__is_idle (const char *name);int vmlinux_path__nr_entries;char **vmlinux_path;@@ -357,8 +358,7 @@ void symbols__delete(struct rb_root_cached *symbols) } } -void __symbols__insert (struct rb_root_cached *symbols, -struct symbol *sym,bool kernel) +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym) {struct rb_node **p = &symbols->rb_root.rb_node;struct rb_node *parent =NULL ;@@ -366,17 +366,6 @@ void __symbols__insert(struct rb_root_cached *symbols,struct symbol *s;bool leftmost = true; -if (kernel) { -const char *name = sym->name; -/* -* ppc64 uses function descriptors and appends a '.' to the -* start of every instruction address. Remove it. -*/ -if (name[0 ] =='.' ) - name++; - sym->idle =symbol__is_idle (name); - } -while (*p !=NULL ) { parent = *p; s = rb_entry(parent, struct symbol, rb_node);@@ -393,7 +382,7 @@ void __symbols__insert(struct rb_root_cached *symbols,void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym) { -__symbols__insert (symbols, sym, false); +__symbols__insert (symbols, sym); }static struct symbol *symbols__find (struct rb_root_cached *symbols,u64 ip)@@ -554,7 +543,7 @@ void dso__reset_find_symbol_cache(struct dso *dso)void dso__insert_symbol (struct dso *dso,struct symbol *sym) { -__symbols__insert (dso__symbols (dso), sym,dso__kernel (dso)); +__symbols__insert (dso__symbols (dso), sym);/* update the symbol cache if necessary */ if (dso__last_find_result_addr(dso) >= sym->start &&@@ -716,47 +705,87 @@ int modules__parse(const char *filename, void *arg,return err; } +static int sym_name_cmp (const void *a,const void *b) +{ +const char *name = a; +const char *const *sym = b; + +return strcmp (name, *sym); +} +/* * These are symbols in the kernel image, so make sure that * sym is from a kernel DSO. */ -static bool symbol__is_idle (const char *name) +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,struct perf_env *env) { -const char *const idle_symbols[] = { +static const char *const idle_symbols[] = {"acpi_idle_do_entry" ,"acpi_processor_ffh_cstate_enter" ,"arch_cpu_idle" ,"cpu_idle" ,"cpu_startup_entry" , -"idle_cpu" , -"intel_idle" , -"intel_idle_ibrs" ,"default_idle" , -"native_safe_halt" ,"enter_idle" ,"exit_idle" , -"mwait_idle" , -"mwait_idle_with_hints" , -"mwait_idle_with_hints.constprop.0" , +"idle_cpu" , +"native_safe_halt" ,"poll_idle" , -"ppc64_runlatch_off" ,"pseries_dedicated_idle_sleep" , -"psw_idle" , -"psw_idle_exit" , -NULL }; -int i; -static struct strlist *idle_symbols_list; +const char *name = sym->name; +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); -if (idle_symbols_list) -return strlist__has_entry (idle_symbols_list, name); +if (sym->idle) +return sym->idle ==SYMBOL_IDLE__IDLE ; - idle_symbols_list =strlist__new (NULL ,NULL ); +if (!dso ||dso__kernel (dso) ==DSO_SPACE__USER ) { + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; + } -for (i =0 ; idle_symbols[i]; i++) -strlist__add (idle_symbols_list, idle_symbols[i]); +/* +* ppc64 uses function descriptors and appends a '.' to the +* start of every instruction address. Remove it. +*/ +if (name[0 ] =='.' ) + name++; -return strlist__has_entry (idle_symbols_list, name); +if (bsearch (name, idle_symbols,ARRAY_SIZE (idle_symbols), +sizeof (idle_symbols[0 ]), sym_name_cmp)) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + +if (e_machine ==EM_386 || e_machine ==EM_X86_64 ) { +if (strstarts (name,"mwait_idle" ) || +strstarts (name,"intel_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + +if (e_machine ==EM_PPC64 && !strcmp (name,"ppc64_runlatch_off" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + +if (e_machine ==EM_S390 ) { +int major =0 , minor =0 ; +const char *release = env && env->os_release + ? env->os_release : perf_version_string; + +sscanf (release,"%d.%d" , &major, &minor); + +/* Before v6.10, s390 used psw_idle. */ +if ((major <6 || (major ==6 && minor <10 )) &&strstarts (name,"psw_idle" )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; } static int map__process_kallsym_symbol(void *arg, const char *name,@@ -785,7 +814,7 @@ static int map__process_kallsym_symbol(void *arg, const char *name,* We will pass the symbols to the filter later, in * map__split_kallsyms, when we have split the maps per module */ -__symbols__insert (root, sym, !strchr (name,'[' )); +__symbols__insert (root, sym);return 0 ; }tools/perf/util/symbol.h +11
-4@@ -25,6 +25,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/* @@ -42,6 +43,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index). @@ -57,8 +64,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle. */ + enum symbol_idle_kind idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */ @@ -202,8 +209,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);@@ -286,5 +292,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,struct perf_env *env);#endif /* __PERF_SYMBOL */
Ian Rogers
reviewerv4 cover perf symbol/env: ELF machine clean up and lazy idle computation
In reply to Ian RogersAdd a helper to perf_env to compute the e_machine if it is EM_NONE. Derive the value from the arch string if available. Similarly derive the arch string from the ELF machine if available, for consistency. This means perf's arch (machine type) is no longer determined by uname but set to match that of the perf ELF executable.
Switch the idle computation to the point of use and lazily compute it, rather than computing it for every symbol. The current only user is perf top. At the point of use the perf_env is available and this can be used to make sure the idle function computation is machine and kernel version dependent.
v4: Fix Sashiko issues where an array element wasn't sorted properly,
the e_flags weren't returned properly, the idle type is change to a u8 rather than an enum value and the s390 version check for psw_idle is slightly reordered and tweaked.
v3: Properly set up the e_machine coming from the perf_env as reported
by Honglei Wang.
https://lore.kernel.org/lkml/[email protected]/
v2: Some minor white space clean up:
https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
Ian Rogers (2):
perf env: Add perf_env__e_machine helper and use in perf_env__arch perf symbol: Lazily compute idle and use the perf_env
tools/perf/builtin-top.c | 6 +- tools/perf/util/env.c | 185 ++++++++++++++++++++++++++++------- tools/perf/util/env.h | 1 + tools/perf/util/session.c | 14 +-- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 104 +++++++++++++------- tools/perf/util/symbol.h | 15 ++- 7 files changed, 240 insertions(+), 87 deletions(-)
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Ian Rogers
reviewerPATCH v4 1/2 perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back of EM_HOST. Use the perf_env's arch to compute the e_machine if available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switch perf_env__arch to be derived the e_machine for consistency. This switches arch from being uname derived to matching that of the perf binary (via EM_HOST). Update session to use the helper, which may mean using EM_HOST when no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.
--- tools/perf/util/env.c | 185 ++++++++++++++++++++++++++++++-------- tools/perf/util/env.h | 1 + tools/perf/util/session.c | 14 +-- 3 files changed, 157 insertions(+), 43 deletions(-)
tools/perf/util/env.c +148 -37
@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #include "cpumap.h" +#include "dwarf-regs.h" #include "debug.h" #include "env.h" #include "util/header.h" #include "util/rwsem.h" #include <linux/compiler.h> +#include <linux/kernel.h> #include <linux/ctype.h> #include <linux/rbtree.h> #include <linux/string.h>
@@ -588,51 +590,160 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)zfree (&cache->size); } +struct arch_to_e_machine { +const char *prefix; +uint16_t e_machine; +}; +/* -* Return architecture name in a normalized form. -* The conversion logic comes from the Makefile. +* A mapping from an arch prefix string to an ELF machine that can be used in a +* bsearch. Some arch prefixes are shared an need additional processing as +* marked next to the architecture. The prefixes handle both perf's architecture +* naming and those from uname. */ -static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch; +static const struct arch_to_e_machine prefix_to_e_machine[] = { + {"aarch64" ,EM_AARCH64 }, + {"alpha" ,EM_ALPHA }, + {"arc" ,EM_ARC }, + {"arm" ,EM_ARM },/* Check also for EM_AARCH64. */ + {"avr" ,EM_AVR },/* Check also for EM_AVR32. */ + {"bfin" ,EM_BLACKFIN }, + {"blackfin" ,EM_BLACKFIN }, + {"cris" ,EM_CRIS }, + {"csky" ,EM_CSKY }, + {"hppa" ,EM_PARISC }, + {"i386" ,EM_386 }, + {"i486" ,EM_386 }, + {"i586" ,EM_386 }, + {"i686" ,EM_386 }, + {"loongarch" ,EM_LOONGARCH }, + {"m32r" ,EM_M32R }, + {"m68k" ,EM_68K }, + {"microblaze" ,EM_MICROBLAZE }, + {"mips" ,EM_MIPS }, + {"msp430" ,EM_MSP430 }, + {"parisc" ,EM_PARISC }, + {"powerpc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"ppc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"riscv" ,EM_RISCV }, + {"s390" ,EM_S390 }, + {"sa110" ,EM_ARM }, + {"sh" ,EM_SH }, + {"sparc" ,EM_SPARC },/* Check also for EM_SPARCV9. */ + {"sun4u" ,EM_SPARC }, + {"x86" ,EM_X86_64 },/* Check also for EM_386. */ + {"xtensa" ,EM_XTENSA }, +}; + +static int compare_prefix (const void *key,const void *element) +{ +const char *search_key = key; +const struct arch_to_e_machine *map_element = element; +size_t prefix_len =strlen (map_element->prefix); + +return strncmp (search_key, map_element->prefix, prefix_len); +} + +static uint16_t perf_arch_to_e_machine (const char *perf_arch,bool is_64_bit) +{ +/* Binary search for a matching prefix. */ +const struct arch_to_e_machine *result; + +if (!perf_arch) +return EM_HOST ; + + result =bsearch (perf_arch, + prefix_to_e_machine,ARRAY_SIZE (prefix_to_e_machine), +sizeof (prefix_to_e_machine[0 ]), + compare_prefix); + +if (!result) { +pr_debug ("Unknown perf arch for ELF machine mapping: %s\n" , perf_arch); +return EM_NONE ; + } + +/* Handle conflicting prefixes. */ +switch (result->e_machine) { +case EM_ARM : +return !strcmp (perf_arch,"arm64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +return is_64_bit ||strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ; +case EM_SPARC : +return is_64_bit || !strcmp (perf_arch,"sparc64" ) ?EM_SPARCV9 :EM_SPARC ; +case EM_X86_64 : +return is_64_bit || !strcmp (perf_arch,"x86_64" ) ?EM_X86_64 :EM_386 ; +default : +return result->e_machine; + } +} + +static const char *e_machine_to_perf_arch (uint16_t e_machine) +{ +/* +* Table for if either the perf arch string differs from uname or there +* are >1 ELF machine with the prefix. +*/ +static const struct arch_to_e_machine extras[] = { + {"arm64" ,EM_AARCH64 }, + {"avr32" ,EM_AVR32 }, + {"powerpc" ,EM_PPC }, + {"powerpc" ,EM_PPC64 }, + {"sparc" ,EM_SPARCV9 }, + {"x86" ,EM_386 }, + {"x86" ,EM_X86_64 }, + {"none" ,EM_NONE }, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (extras); i++) { +if (extras[i].e_machine == e_machine) +return extras[i].prefix; + } + +for (size_t i =0 ; i <ARRAY_SIZE (prefix_to_e_machine); i++) { +if (prefix_to_e_machine[i].e_machine == e_machine) +return prefix_to_e_machine[i].prefix; + + } +return "unknown" ; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +if (!env) { +if (e_flags) + *e_flags =EF_HOST ; + +return EM_HOST ; + } +if (env->e_machine ==EM_NONE ) { + env->e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit); + +if (env->e_machine ==EM_HOST ) + env->e_flags =EF_HOST ; + } +if (e_flags) + *e_flags = env->e_flags; + +return env->e_machine; }const char *perf_env__arch (struct perf_env *env) { -char *arch_name; +if (!env) +return e_machine_to_perf_arch (EM_HOST ); -if (!env || !env->arch) {/* Assume local operation */ -static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +if (!env->arch) { +/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); -return normalize_arch (arch_name); + env->arch =strdup (e_machine_to_perf_arch (e_machine)); + } +return env->arch; }#if defined(HAVE_LIBTRACEEVENT )
tools/perf/util/env.h +1 -0
@@ -186,6 +186,7 @@ int perf_env__read_cpu_topology_map(struct perf_env *env);void cpu_cache_level__free (struct cpu_cache_level *cache); +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env);const char *perf_env__arch_strerrno (struct perf_env *env,int err);const char *perf_env__cpuid (struct perf_env *env);
tools/perf/util/session.c +8 -6
@@ -2996,14 +2996,16 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* Is the env caching an e_machine? */ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Namhyung Kim
maintainerRe: [PATCH v4 1/2] perf env: Add perf_env__e_machine helper and use in perf_env__arch
Quoted 136 lines · Ian Rogers
Add a helper that lazily computes the
e_machineand falls back ofEM_HOST. Use the perf_env's arch to compute thee_machineif available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switchperf_env__archto be derived thee_machinefor consistency. This switches arch from being uname derived to matching that of the perf binary (viaEM_HOST). Update session to use the helper, which may mean usingEM_HOSTwhen no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/env.c | 185 ++++++++++++++++++++++++++++++-------- tools/perf/util/env.h | 1 + tools/perf/util/session.c | 14 +-- 3 files changed, 157 insertions(+), 43 deletions(-)tools/perf/util/env.c +70
-28@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #include "cpumap.h" +#include "dwarf-regs.h" #include "debug.h" #include "env.h" #include "util/header.h" #include "util/rwsem.h" #include <linux/compiler.h> +#include <linux/kernel.h> #include <linux/ctype.h> #include <linux/rbtree.h> #include <linux/string.h> @@ -588,51 +590,160 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)zfree (&cache->size); } +struct arch_to_e_machine { +const char *prefix; +uint16_t e_machine; +}; +/* -* Return architecture name in a normalized form. -* The conversion logic comes from the Makefile. +* A mapping from an arch prefix string to an ELF machine that can be used in a +* bsearch. Some arch prefixes are shared an need additional processing as +* marked next to the architecture. The prefixes handle both perf's architecture +* naming and those from uname. */ -static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch; +static const struct arch_to_e_machine prefix_to_e_machine[] = { + {"aarch64" ,EM_AARCH64 }, + {"alpha" ,EM_ALPHA }, + {"arc" ,EM_ARC }, + {"arm" ,EM_ARM },/* Check also for EM_AARCH64. */ + {"avr" ,EM_AVR },/* Check also for EM_AVR32. */ + {"bfin" ,EM_BLACKFIN }, + {"blackfin" ,EM_BLACKFIN }, + {"cris" ,EM_CRIS }, + {"csky" ,EM_CSKY }, + {"hppa" ,EM_PARISC }, + {"i386" ,EM_386 }, + {"i486" ,EM_386 }, + {"i586" ,EM_386 }, + {"i686" ,EM_386 }, + {"loongarch" ,EM_LOONGARCH }, + {"m32r" ,EM_M32R }, + {"m68k" ,EM_68K }, + {"microblaze" ,EM_MICROBLAZE }, + {"mips" ,EM_MIPS }, + {"msp430" ,EM_MSP430 }, + {"parisc" ,EM_PARISC }, + {"powerpc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"ppc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"riscv" ,EM_RISCV }, + {"s390" ,EM_S390 }, + {"sa110" ,EM_ARM }, + {"sh" ,EM_SH }, + {"sparc" ,EM_SPARC },/* Check also for EM_SPARCV9. */ + {"sun4u" ,EM_SPARC }, + {"x86" ,EM_X86_64 },/* Check also for EM_386. */ + {"xtensa" ,EM_XTENSA }, +}; + +static int compare_prefix (const void *key,const void *element) +{ +const char *search_key = key; +const struct arch_to_e_machine *map_element = element; +size_t prefix_len =strlen (map_element->prefix); + +return strncmp (search_key, map_element->prefix, prefix_len); +} + +static uint16_t perf_arch_to_e_machine (const char *perf_arch,bool is_64_bit) +{ +/* Binary search for a matching prefix. */ +const struct arch_to_e_machine *result; + +if (!perf_arch) +return EM_HOST ; + + result =bsearch (perf_arch, + prefix_to_e_machine,ARRAY_SIZE (prefix_to_e_machine), +sizeof (prefix_to_e_machine[0 ]), + compare_prefix); + +if (!result) { +pr_debug ("Unknown perf arch for ELF machine mapping: %s\n" , perf_arch); +return EM_NONE ;
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 672
Quoted 10 lines
tools/perf/util/env.c +10
-0+ } + +/* Handle conflicting prefixes. */ +switch (result->e_machine) { +case EM_ARM : +return !strcmp (perf_arch,"arm64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +return is_64_bit ||strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ;
I'm curious what's the name uname -m returns for PPC64. Is "powerpc64" possible?
Quoted 10 lines
tools/perf/util/env.c +10
-0+case EM_SPARC : +return is_64_bit || !strcmp (perf_arch,"sparc64" ) ?EM_SPARCV9 :EM_SPARC ; +case EM_X86_64 : +return is_64_bit || !strcmp (perf_arch,"x86_64" ) ?EM_X86_64 :EM_386 ; +default : +return result->e_machine; + } +} + +static const char *e_machine_to_perf_arch (uint16_t e_machine)
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 692
Quoted 10 lines
tools/perf/util/env.c +10
-0+{ +/* +* Table for if either the perf arch string differs from uname or there +* are >1 ELF machine with the prefix. +*/ +static const struct arch_to_e_machine extras[] = { + {"arm64" ,EM_AARCH64 }, + {"avr32" ,EM_AVR32 }, + {"powerpc" ,EM_PPC }, + {"powerpc" ,EM_PPC64 },
Here it returns powerpc for both.
Quoted 108 lines
tools/perf/util/env.c +48
-9+ {"sparc", EM_SPARCV9}, + {"x86", EM_386}, + {"x86", EM_X86_64}, + {"none", EM_NONE}, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (extras); i++) { +if (extras[i].e_machine == e_machine) +return extras[i].prefix; + } + +for (size_t i =0 ; i <ARRAY_SIZE (prefix_to_e_machine); i++) { +if (prefix_to_e_machine[i].e_machine == e_machine) +return prefix_to_e_machine[i].prefix; + + } +return "unknown" ; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +if (!env) { +if (e_flags) + *e_flags =EF_HOST ; + +return EM_HOST ; + } +if (env->e_machine ==EM_NONE ) { + env->e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit); + +if (env->e_machine ==EM_HOST ) + env->e_flags =EF_HOST ; + } +if (e_flags) + *e_flags = env->e_flags; + +return env->e_machine; }const char *perf_env__arch (struct perf_env *env) { -char *arch_name; +if (!env) +return e_machine_to_perf_arch (EM_HOST ); -if (!env || !env->arch) {/* Assume local operation */ -static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +if (!env->arch) { +/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); -return normalize_arch (arch_name); + env->arch =strdup (e_machine_to_perf_arch (e_machine)); + } +return env->arch; }#if defined(HAVE_LIBTRACEEVENT )tools/perf/util/env.h +1
-0@@ -186,6 +186,7 @@ int perf_env__read_cpu_topology_map(struct perf_env *env);void cpu_cache_level__free (struct cpu_cache_level *cache); +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env);const char *perf_env__arch_strerrno (struct perf_env *env,int err);const char *perf_env__cpuid (struct perf_env *env);tools/perf/util/session.c +8
-6@@ -2996,14 +2996,16 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* Is the env caching an e_machine? */ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Ian Rogers
reviewerRe: [PATCH v4 1/2] perf env: Add perf_env__e_machine helper and use in perf_env__arch
Quoted 143 lines · Namhyung Kim
Quoted 141 lines · Ian Rogers
Add a helper that lazily computes the
e_machineand falls back ofEM_HOST. Use the perf_env's arch to compute thee_machineif available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switchperf_env__archto be derived thee_machinefor consistency. This switches arch from being uname derived to matching that of the perf binary (viaEM_HOST). Update session to use the helper, which may mean usingEM_HOSTwhen no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/env.c | 185 ++++++++++++++++++++++++++++++-------- tools/perf/util/env.h | 1 + tools/perf/util/session.c | 14 +-- 3 files changed, 157 insertions(+), 43 deletions(-)tools/perf/util/env.c +75
-28@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #include "cpumap.h" +#include "dwarf-regs.h" #include "debug.h" #include "env.h" #include "util/header.h" #include "util/rwsem.h" #include <linux/compiler.h> +#include <linux/kernel.h> #include <linux/ctype.h> #include <linux/rbtree.h> #include <linux/string.h> @@ -588,51 +590,160 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)zfree (&cache->size); } +struct arch_to_e_machine { +const char *prefix; +uint16_t e_machine; +}; +/* -* Return architecture name in a normalized form. -* The conversion logic comes from the Makefile. +* A mapping from an arch prefix string to an ELF machine that can be used in a +* bsearch. Some arch prefixes are shared an need additional processing as +* marked next to the architecture. The prefixes handle both perf's architecture +* naming and those from uname. */ -static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch; +static const struct arch_to_e_machine prefix_to_e_machine[] = { + {"aarch64" ,EM_AARCH64 }, + {"alpha" ,EM_ALPHA }, + {"arc" ,EM_ARC }, + {"arm" ,EM_ARM },/* Check also for EM_AARCH64. */ + {"avr" ,EM_AVR },/* Check also for EM_AVR32. */ + {"bfin" ,EM_BLACKFIN }, + {"blackfin" ,EM_BLACKFIN }, + {"cris" ,EM_CRIS }, + {"csky" ,EM_CSKY }, + {"hppa" ,EM_PARISC }, + {"i386" ,EM_386 }, + {"i486" ,EM_386 }, + {"i586" ,EM_386 }, + {"i686" ,EM_386 }, + {"loongarch" ,EM_LOONGARCH }, + {"m32r" ,EM_M32R }, + {"m68k" ,EM_68K }, + {"microblaze" ,EM_MICROBLAZE }, + {"mips" ,EM_MIPS }, + {"msp430" ,EM_MSP430 }, + {"parisc" ,EM_PARISC }, + {"powerpc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"ppc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"riscv" ,EM_RISCV }, + {"s390" ,EM_S390 }, + {"sa110" ,EM_ARM }, + {"sh" ,EM_SH }, + {"sparc" ,EM_SPARC },/* Check also for EM_SPARCV9. */ + {"sun4u" ,EM_SPARC }, + {"x86" ,EM_X86_64 },/* Check also for EM_386. */ + {"xtensa" ,EM_XTENSA }, +}; + +static int compare_prefix (const void *key,const void *element) +{ +const char *search_key = key; +const struct arch_to_e_machine *map_element = element; +size_t prefix_len =strlen (map_element->prefix); + +return strncmp (search_key, map_element->prefix, prefix_len); +} + +static uint16_t perf_arch_to_e_machine (const char *perf_arch,bool is_64_bit) +{ +/* Binary search for a matching prefix. */ +const struct arch_to_e_machine *result; + +if (!perf_arch) +return EM_HOST ; + + result =bsearch (perf_arch, + prefix_to_e_machine,ARRAY_SIZE (prefix_to_e_machine), +sizeof (prefix_to_e_machine[0 ]), + compare_prefix); + +if (!result) { +pr_debug ("Unknown perf arch for ELF machine mapping: %s\n" , perf_arch); +return EM_NONE ; + } + +/* Handle conflicting prefixes. */ +switch (result->e_machine) { +case EM_ARM :
Quoted 8 lines
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 672
Quoted 5 lines
tools/perf/util/env.c +5
-0+return !strcmp (perf_arch,"arm64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +return is_64_bit ||strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ;I'm curious what's the name
uname -mreturns for PPC64. Is "powerpc64" possible?
It is.
Quoted 14 lines
Quoted 14 lines
tools/perf/util/env.c +14
-0+case EM_SPARC : +return is_64_bit || !strcmp (perf_arch,"sparc64" ) ?EM_SPARCV9 :EM_SPARC ; +case EM_X86_64 : +return is_64_bit || !strcmp (perf_arch,"x86_64" ) ?EM_X86_64 :EM_386 ; +default : +return result->e_machine; + } +} + +static const char *e_machine_to_perf_arch (uint16_t e_machine) +{ +/* +* Table for if either the perf arch string differs from uname or there +* are >1 ELF machine with the prefix.
Quoted 8 lines
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 692
Quoted 6 lines
tools/perf/util/env.c +6
-0+*/ +static const struct arch_to_e_machine extras[] = { + {"arm64" ,EM_AARCH64 }, + {"avr32" ,EM_AVR32 }, + {"powerpc" ,EM_PPC }, + {"powerpc" ,EM_PPC64 },Here it returns powerpc for both.
Yep. This is 100% intentional as the existing code does the same:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/env.c?h=perf-tools-next#n611
``` static const char *normalize_arch(char *arch) ... if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3)) return "powerpc"; ```
The strncmp is limited to just the prefix of the uname string, ignoring the 64. So the arch "powerpc" can be 32-bit or 64-bit, just as "x86" can be 32-bit or 64-bit. To determine which case applies, the code should really check struct perf_env's kernel_is_64_bit. I think this is generally much more painful than just using the e_machine - especially since you need to strcmp the name. For the e_machine, the problem is that on x86 we have 32-bit, x32 and x86_64. There is then also an ABI question regarding the use of SIMD registers and the newer APX registers. If there are no samples and no DSOs in play, making a choice of e_machine to set up variables with is somewhat arbitrary. I think EM_HOST, the e_machine of the current perf binary, is a good choice.
Thanks,
Ian
Quoted 108 lines
Quoted 108 lines
tools/perf/util/env.c +48
-9+ {"sparc", EM_SPARCV9}, + {"x86", EM_386}, + {"x86", EM_X86_64}, + {"none", EM_NONE}, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (extras); i++) { +if (extras[i].e_machine == e_machine) +return extras[i].prefix; + } + +for (size_t i =0 ; i <ARRAY_SIZE (prefix_to_e_machine); i++) { +if (prefix_to_e_machine[i].e_machine == e_machine) +return prefix_to_e_machine[i].prefix; + + } +return "unknown" ; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +if (!env) { +if (e_flags) + *e_flags =EF_HOST ; + +return EM_HOST ; + } +if (env->e_machine ==EM_NONE ) { + env->e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit); + +if (env->e_machine ==EM_HOST ) + env->e_flags =EF_HOST ; + } +if (e_flags) + *e_flags = env->e_flags; + +return env->e_machine; }const char *perf_env__arch (struct perf_env *env) { -char *arch_name; +if (!env) +return e_machine_to_perf_arch (EM_HOST ); -if (!env || !env->arch) {/* Assume local operation */ -static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +if (!env->arch) { +/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); -return normalize_arch (arch_name); + env->arch =strdup (e_machine_to_perf_arch (e_machine)); + } +return env->arch; }#if defined(HAVE_LIBTRACEEVENT )tools/perf/util/env.h +1
-0@@ -186,6 +186,7 @@ int perf_env__read_cpu_topology_map(struct perf_env *env);void cpu_cache_level__free (struct cpu_cache_level *cache); +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env);const char *perf_env__arch_strerrno (struct perf_env *env,int err);const char *perf_env__cpuid (struct perf_env *env);tools/perf/util/session.c +8
-7@@ -2996,14 +2996,16 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* Is the env caching an e_machine? */ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args); --2.53.0.1018.g2bb0e51243-goog
Ian Rogers
reviewerPATCH v4 2/2 perf symbol: Lazily compute idle and use the perf_env
In reply to Ian RogersMove the idle boolean to a helper symbol__is_idle function. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. As symbols__insert no longer needs to know if a symbol is for the kernel, remove the argument.
This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/
The change switches x86 matches to use strstarts which means intel_idle_irq is matched as part of strstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/
--- tools/perf/builtin-top.c | 6 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 104 ++++++++++++++++++++++------------- tools/perf/util/symbol.h | 15 +++-- 4 files changed, 83 insertions(+), 44 deletions(-)
tools/perf/builtin-top.c +5 -1
@@ -751,6 +751,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, { struct perf_top *top = container_of(tool, struct perf_top, tool);struct addr_location al; +struct dso *dso =NULL ;if (!machine && perf_guest) {static struct intlist *seen;
@@ -830,7 +831,10 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !al.sym->idle) { +if (al.map) + dso =map__dso (al.map); + +if (al.sym ==NULL || !symbol__is_idle (al.sym, dso, machine->env)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,
tools/perf/util/symbol-elf.c +1 -1
@@ -1723,7 +1723,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);
tools/perf/util/symbol.c +66 -38
172 lines not on this page · raw message
tools/perf/util/symbol.h +11 -4
@@ -25,6 +25,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/*
@@ -42,6 +43,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index).
@@ -57,8 +64,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle holding enum symbol_idle_kind values. */ + u8 idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */
@@ -202,8 +209,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);
@@ -286,5 +292,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,struct perf_env *env);#endif /* __PERF_SYMBOL */
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Namhyung Kim
maintainerRe: [PATCH v4 2/2] perf symbol: Lazily compute idle and use the perf_env
Quoted 18 lines · Ian Rogers
Move the idle boolean to a helper
symbol__is_idlefunction. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. Assymbols__insertno longer needs to know if a symbol is for the kernel, remove the argument.This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/The change switches x86 matches to use strstarts which means
intel_idle_irqis matched as part ofstrstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/Signed-off-by Ian Rogers <[email protected]>---
[SNIP]
tools/perf/util/symbol.c int modules__parse(const char *filename, void *arg, · line 776
Quoted 4 lines
tools/perf/util/symbol.c +4
-0+if (e_machine ==EM_S390 &&strstarts (name,"psw_idle" )) { +int major =0 , minor =0 ; +const char *release = env && env->os_release + ? env->os_release : perf_version_string;
I think Sashiko's review is right. You need to check the kernel version instead of perf.
Thanks,
Namhyung
Quoted 12 lines
tools/perf/util/symbol.c +11
-0+ +/* Before v6.10, s390 used psw_idle. */ +if (sscanf (release,"%d.%d" , &major, &minor) !=2 || + major <6 || (major ==6 && minor <10 )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; }
Ian Rogers
reviewerRe: [PATCH v4 2/2] perf symbol: Lazily compute idle and use the perf_env
Quoted 20 lines · Namhyung Kim
Quoted 11 lines · Ian Rogers
Move the idle boolean to a helper
symbol__is_idlefunction. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. Assymbols__insertno longer needs to know if a symbol is for the kernel, remove the argument.This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/Quoted 7 lines
The change switches x86 matches to use strstarts which means
intel_idle_irqis matched as part ofstrstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/Signed-off-by Ian Rogers <[email protected]>---
Quoted 8 lines
[SNIP]
tools/perf/util/symbol.c int modules__parse(const char *filename, void *arg, · line 776
Quoted 4 lines
tools/perf/util/symbol.c +4
-0+if (e_machine ==EM_S390 &&strstarts (name,"psw_idle" )) { +int major =0 , minor =0 ; +const char *release = env && env->os_release + ? env->os_release : perf_version_string;I think Sashiko's review is right. You need to check the kernel version instead of perf.
Doing this can create more problems and complexity than it solves. If we state that os_release can be NULL at this point, we recompute it using uname:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/header.c?h=perf-tools-next#n378
then do we cache the value in env? What happens if a data/pipe file that assigns to the env later? Ad-hoc users of env->os_release recomputing it shouldn't happen; instead, in 'live' mode, we should assign os_release using uname either when the perf_env is created or lazily with a helper function. I dislike that with a helper we could potentially have multiple notions of os_release.
I'll add a patch to refactor the use of os_release, but can we be mindful that this is clear feature creep with little benefit? We will still fall back on perf_version_string if uname fails and for all practical purposes, perf_version_string will differ little from uname in this case. I'm only going to add the patch because checking other uses of os_release suggests the change is benign.
Thanks,
Ian
Quoted 15 lines
Thanks,
NamhyungQuoted 12 lines
tools/perf/util/symbol.c +11
-0+ +/* Before v6.10, s390 used psw_idle. */ +if (sscanf (release,"%d.%d" , &major, &minor) !=2 || + major <6 || (major ==6 && minor <10 )) { + sym->idle =SYMBOL_IDLE__IDLE ; +return true; + } + } + + sym->idle =SYMBOL_IDLE__NOT_IDLE ; +return false; }
Ian Rogers
reviewerv5 cover perf symbol/env: ELF machine clean up and lazy idle computation
Add a helper to perf_env to compute the e_machine if it is EM_NONE. Derive the value from the arch string if available. Similarly derive the arch string from the ELF machine if available, for consistency. This means perf's arch (machine type) is no longer determined by uname but set to match that of the perf ELF executable.
Switch the idle computation to the point of use and lazily compute it, rather than computing it for every symbol. The current only user is perf top. At the point of use the perf_env is available and this can be used to make sure the idle function computation is machine and kernel version dependent.
v5: Add perf_env os_release helper (Namhyung/Sashiko)
v4: Fix Sashiko issues where an array element wasn't sorted properly,
the e_flags weren't returned properly, the idle type is change to a u8 rather than an enum value and the s390 version check for psw_idle is slightly reordered and tweaked.
https://lore.kernel.org/lkml/[email protected]/
v3: Properly set up the e_machine coming from the perf_env as reported
by Honglei Wang.
https://lore.kernel.org/lkml/[email protected]/
v2: Some minor white space clean up:
https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
Ian Rogers (3):
perf env: Add perf_env__e_machine helper and use in perf_env__arch perf env: Add helper to lazily compute the os_release perf symbol: Lazily compute idle and use the perf_env
tools/perf/builtin-top.c | 6 +- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/env.c | 206 ++++++++++++++++++++++++------ tools/perf/util/env.h | 2 + tools/perf/util/session.c | 14 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 107 ++++++++++------ tools/perf/util/symbol.h | 15 ++- 8 files changed, 264 insertions(+), 90 deletions(-)
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Ian Rogers
reviewerPATCH v5 1/3 perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back of EM_HOST. Use the perf_env's arch to compute the e_machine if available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switch perf_env__arch to be derived the e_machine for consistency. This switches arch from being uname derived to matching that of the perf binary (via EM_HOST). Update session to use the helper, which may mean using EM_HOST when no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.
--- tools/perf/util/env.c | 185 ++++++++++++++++++++++++++++++-------- tools/perf/util/env.h | 1 + tools/perf/util/session.c | 14 +-- 3 files changed, 157 insertions(+), 43 deletions(-)
tools/perf/util/env.c +148 -37
@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #include "cpumap.h" +#include "dwarf-regs.h" #include "debug.h" #include "env.h" #include "util/header.h" #include "util/rwsem.h" #include <linux/compiler.h> +#include <linux/kernel.h> #include <linux/ctype.h> #include <linux/rbtree.h> #include <linux/string.h>
@@ -588,51 +590,160 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)zfree (&cache->size); } +struct arch_to_e_machine { +const char *prefix; +uint16_t e_machine; +}; +/* -* Return architecture name in a normalized form. -* The conversion logic comes from the Makefile. +* A mapping from an arch prefix string to an ELF machine that can be used in a +* bsearch. Some arch prefixes are shared an need additional processing as +* marked next to the architecture. The prefixes handle both perf's architecture +* naming and those from uname. */ -static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch; +static const struct arch_to_e_machine prefix_to_e_machine[] = { + {"aarch64" ,EM_AARCH64 }, + {"alpha" ,EM_ALPHA }, + {"arc" ,EM_ARC }, + {"arm" ,EM_ARM },/* Check also for EM_AARCH64. */ + {"avr" ,EM_AVR },/* Check also for EM_AVR32. */ + {"bfin" ,EM_BLACKFIN }, + {"blackfin" ,EM_BLACKFIN }, + {"cris" ,EM_CRIS }, + {"csky" ,EM_CSKY }, + {"hppa" ,EM_PARISC }, + {"i386" ,EM_386 }, + {"i486" ,EM_386 }, + {"i586" ,EM_386 }, + {"i686" ,EM_386 }, + {"loongarch" ,EM_LOONGARCH }, + {"m32r" ,EM_M32R }, + {"m68k" ,EM_68K }, + {"microblaze" ,EM_MICROBLAZE }, + {"mips" ,EM_MIPS }, + {"msp430" ,EM_MSP430 }, + {"parisc" ,EM_PARISC }, + {"powerpc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"ppc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"riscv" ,EM_RISCV }, + {"s390" ,EM_S390 }, + {"sa110" ,EM_ARM }, + {"sh" ,EM_SH }, + {"sparc" ,EM_SPARC },/* Check also for EM_SPARCV9. */ + {"sun4u" ,EM_SPARC }, + {"x86" ,EM_X86_64 },/* Check also for EM_386. */ + {"xtensa" ,EM_XTENSA }, +}; + +static int compare_prefix (const void *key,const void *element) +{ +const char *search_key = key; +const struct arch_to_e_machine *map_element = element; +size_t prefix_len =strlen (map_element->prefix); + +return strncmp (search_key, map_element->prefix, prefix_len); +} + +static uint16_t perf_arch_to_e_machine (const char *perf_arch,bool is_64_bit) +{ +/* Binary search for a matching prefix. */ +const struct arch_to_e_machine *result; + +if (!perf_arch) +return EM_HOST ; + + result =bsearch (perf_arch, + prefix_to_e_machine,ARRAY_SIZE (prefix_to_e_machine), +sizeof (prefix_to_e_machine[0 ]), + compare_prefix); + +if (!result) { +pr_debug ("Unknown perf arch for ELF machine mapping: %s\n" , perf_arch); +return EM_NONE ; + } + +/* Handle conflicting prefixes. */ +switch (result->e_machine) { +case EM_ARM : +return !strcmp (perf_arch,"arm64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +return is_64_bit ||strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ; +case EM_SPARC : +return is_64_bit || !strcmp (perf_arch,"sparc64" ) ?EM_SPARCV9 :EM_SPARC ; +case EM_X86_64 : +return is_64_bit || !strcmp (perf_arch,"x86_64" ) ?EM_X86_64 :EM_386 ; +default : +return result->e_machine; + } +} + +static const char *e_machine_to_perf_arch (uint16_t e_machine) +{ +/* +* Table for if either the perf arch string differs from uname or there +* are >1 ELF machine with the prefix. +*/ +static const struct arch_to_e_machine extras[] = { + {"arm64" ,EM_AARCH64 }, + {"avr32" ,EM_AVR32 }, + {"powerpc" ,EM_PPC }, + {"powerpc" ,EM_PPC64 }, + {"sparc" ,EM_SPARCV9 }, + {"x86" ,EM_386 }, + {"x86" ,EM_X86_64 }, + {"none" ,EM_NONE }, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (extras); i++) { +if (extras[i].e_machine == e_machine) +return extras[i].prefix; + } + +for (size_t i =0 ; i <ARRAY_SIZE (prefix_to_e_machine); i++) { +if (prefix_to_e_machine[i].e_machine == e_machine) +return prefix_to_e_machine[i].prefix; + + } +return "unknown" ; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +if (!env) { +if (e_flags) + *e_flags =EF_HOST ; + +return EM_HOST ; + } +if (env->e_machine ==EM_NONE ) { + env->e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit); + +if (env->e_machine ==EM_HOST ) + env->e_flags =EF_HOST ; + } +if (e_flags) + *e_flags = env->e_flags; + +return env->e_machine; }const char *perf_env__arch (struct perf_env *env) { -char *arch_name; +if (!env) +return e_machine_to_perf_arch (EM_HOST ); -if (!env || !env->arch) {/* Assume local operation */ -static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +if (!env->arch) { +/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); -return normalize_arch (arch_name); + env->arch =strdup (e_machine_to_perf_arch (e_machine)); + } +return env->arch; }#if defined(HAVE_LIBTRACEEVENT )
tools/perf/util/env.h +1 -0
@@ -186,6 +186,7 @@ int perf_env__read_cpu_topology_map(struct perf_env *env);void cpu_cache_level__free (struct cpu_cache_level *cache); +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env);const char *perf_env__arch_strerrno (struct perf_env *env,int err);const char *perf_env__cpuid (struct perf_env *env);
tools/perf/util/session.c +8 -6
@@ -3009,14 +3009,16 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* Is the env caching an e_machine? */ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Ian Rogers
reviewerPATCH v5 2/3 perf env: Add helper to lazily compute the os_release
In reply to Ian RogersIn live mode the os_release isn't being initialized, make a lazy initialization helper that assumes when the os_release isn't initialized this is live mode.
--- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/env.c | 21 +++++++++++++++++++++ tools/perf/util/env.h | 1 + tools/perf/util/symbol.c | 4 ++-- 4 files changed, 25 insertions(+), 3 deletions(-)
tools/perf/util/data-convert-bt.c +1 -1
@@ -1414,7 +1414,7 @@ do { \ADD ("host" , env->hostname);ADD ("sysname" ,"Linux" ); -ADD ("release" , env->os_release); +ADD ("release" ,perf_env__os_release (env));ADD ("version" , env->version);ADD ("machine" , env->arch);ADD ("domain" ,"kernel" );
tools/perf/util/env.c +21 -0
@@ -330,6 +330,27 @@ int perf_env__kernel_is_64_bit(struct perf_env *env)return env->kernel_is_64_bit; } +const char *perf_env__os_release (struct perf_env *env) +{ +struct utsname uts; +int ret; + +if (!env) +return perf_version_string; + +if (env->os_release) +return env->os_release; + +/* +* The os_release is being accessed but wasn't initialized from a data +* file, assume this is 'live' mode and use the release from uname. If +* uname fails then use the current perf tool version. +*/ + ret =uname (&uts); + env->os_release =strdup (ret <0 ? perf_version_string : uts.release); +return env->os_release; +} +int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]) {int i;
tools/perf/util/env.h +1 -0
@@ -174,6 +174,7 @@ void free_cpu_domain_info(struct cpu_domain_map **cd_map, u32 schedstat_version,void perf_env__exit (struct perf_env *env);int perf_env__kernel_is_64_bit (struct perf_env *env); +const char *perf_env__os_release (struct perf_env *env);int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]);
tools/perf/util/symbol.c +2 -2
@@ -2208,7 +2208,7 @@ static int vmlinux_path__init(struct perf_env *env) {struct utsname uts;char bf[PATH_MAX ]; -char *kernel_version; +const char *kernel_version;unsigned int i; vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
@@ -2225,7 +2225,7 @@ static int vmlinux_path__init(struct perf_env *env)return 0 ;if (env) { - kernel_version = env->os_release; + kernel_version =perf_env__os_release (env); }else {if (uname (&uts) <0 ) goto out_fail;
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Ian Rogers
reviewerPATCH v5 3/3 perf symbol: Lazily compute idle and use the perf_env
In reply to Ian RogersMove the idle boolean to a helper symbol__is_idle function. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. As symbols__insert no longer needs to know if a symbol is for the kernel, remove the argument.
This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/
The change switches x86 matches to use strstarts which means intel_idle_irq is matched as part of strstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/
--- tools/perf/builtin-top.c | 6 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 103 ++++++++++++++++++++++------------- tools/perf/util/symbol.h | 15 +++-- 4 files changed, 82 insertions(+), 44 deletions(-)
tools/perf/builtin-top.c +5 -1
@@ -751,6 +751,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, { struct perf_top *top = container_of(tool, struct perf_top, tool);struct addr_location al; +struct dso *dso =NULL ;if (!machine && perf_guest) {static struct intlist *seen;
@@ -830,7 +831,10 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !al.sym->idle) { +if (al.map) + dso =map__dso (al.map); + +if (al.sym ==NULL || !symbol__is_idle (al.sym, dso, machine->env)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,
tools/perf/util/symbol-elf.c +1 -1
@@ -1727,7 +1727,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);
tools/perf/util/symbol.c +65 -38
171 lines not on this page · raw message
tools/perf/util/symbol.h +11 -4
@@ -25,6 +25,7 @@ struct dso;struct map ;struct maps ;struct option ; +struct perf_env ;struct build_id ;/*
@@ -42,6 +43,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index).
@@ -57,8 +64,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle holding enum symbol_idle_kind values. */ + u8 idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */
@@ -202,8 +209,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);
@@ -286,5 +292,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,struct perf_env *env);#endif /* __PERF_SYMBOL */
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Ian Rogers
reviewerv6 cover perf symbol/env: ELF machine clean up and lazy idle computation
In reply to Ian RogersAdd a helper to perf_env to compute the e_machine if it is EM_NONE. Derive the value from the arch string if available. Similarly derive the arch string from the ELF machine if available, for consistency. This means perf's arch (machine type) is no longer determined by uname but set to match that of the perf ELF executable.
Switch the idle computation to the point of use and lazily compute it, rather than computing it for every symbol. The current only user is perf top. At the point of use the perf_env is available and this can be used to make sure the idle function computation is machine and kernel version dependent.
v6: Ensure arch is canonical by going to e_machine and back (Sashiko)
v5: Add perf_env os_release helper (Namhyung/Sashiko)
https://lore.kernel.org/lkml/[email protected]/
v4: Fix Sashiko issues where an array element wasn't sorted properly,
the e_flags weren't returned properly, the idle type is change to a u8 rather than an enum value and the s390 version check for psw_idle is slightly reordered and tweaked.
https://lore.kernel.org/lkml/[email protected]/
v3: Properly set up the e_machine coming from the perf_env as reported
by Honglei Wang.
https://lore.kernel.org/lkml/[email protected]/
v2: Some minor white space clean up:
https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
Ian Rogers (3):
perf env: Add perf_env__e_machine helper and use in perf_env__arch perf env: Add helper to lazily compute the os_release perf symbol: Lazily compute idle and use the perf_env
tools/perf/builtin-top.c | 6 +- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/env.c | 206 ++++++++++++++++++++++++------ tools/perf/util/env.h | 2 + tools/perf/util/header.c | 60 ++++++--- tools/perf/util/session.c | 14 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 107 ++++++++++------ tools/perf/util/symbol.h | 15 ++- 9 files changed, 309 insertions(+), 105 deletions(-)
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Ian Rogers
reviewerPATCH v6 1/3 perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back of EM_HOST. Use the perf_env's arch to compute the e_machine if available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switch perf_env__arch to be derived the e_machine for consistency. This switches arch from being uname derived to matching that of the perf binary (via EM_HOST). Update session to use the helper, which may mean using EM_HOST when no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.
--- tools/perf/util/env.c | 185 ++++++++++++++++++++++++++++++-------- tools/perf/util/env.h | 1 + tools/perf/util/header.c | 44 ++++++--- tools/perf/util/session.c | 14 +-- 4 files changed, 191 insertions(+), 53 deletions(-)
tools/perf/util/env.c +148 -37
@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #include "cpumap.h" +#include "dwarf-regs.h" #include "debug.h" #include "env.h" #include "util/header.h" #include "util/rwsem.h" #include <linux/compiler.h> +#include <linux/kernel.h> #include <linux/ctype.h> #include <linux/rbtree.h> #include <linux/string.h>
@@ -588,51 +590,160 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)zfree (&cache->size); } +struct arch_to_e_machine { +const char *prefix; +uint16_t e_machine; +}; +/* -* Return architecture name in a normalized form. -* The conversion logic comes from the Makefile. +* A mapping from an arch prefix string to an ELF machine that can be used in a +* bsearch. Some arch prefixes are shared an need additional processing as +* marked next to the architecture. The prefixes handle both perf's architecture +* naming and those from uname. */ -static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch; +static const struct arch_to_e_machine prefix_to_e_machine[] = { + {"aarch64" ,EM_AARCH64 }, + {"alpha" ,EM_ALPHA }, + {"arc" ,EM_ARC }, + {"arm" ,EM_ARM },/* Check also for EM_AARCH64. */ + {"avr" ,EM_AVR },/* Check also for EM_AVR32. */ + {"bfin" ,EM_BLACKFIN }, + {"blackfin" ,EM_BLACKFIN }, + {"cris" ,EM_CRIS }, + {"csky" ,EM_CSKY }, + {"hppa" ,EM_PARISC }, + {"i386" ,EM_386 }, + {"i486" ,EM_386 }, + {"i586" ,EM_386 }, + {"i686" ,EM_386 }, + {"loongarch" ,EM_LOONGARCH }, + {"m32r" ,EM_M32R }, + {"m68k" ,EM_68K }, + {"microblaze" ,EM_MICROBLAZE }, + {"mips" ,EM_MIPS }, + {"msp430" ,EM_MSP430 }, + {"parisc" ,EM_PARISC }, + {"powerpc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"ppc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"riscv" ,EM_RISCV }, + {"s390" ,EM_S390 }, + {"sa110" ,EM_ARM }, + {"sh" ,EM_SH }, + {"sparc" ,EM_SPARC },/* Check also for EM_SPARCV9. */ + {"sun4u" ,EM_SPARC }, + {"x86" ,EM_X86_64 },/* Check also for EM_386. */ + {"xtensa" ,EM_XTENSA }, +}; + +static int compare_prefix (const void *key,const void *element) +{ +const char *search_key = key; +const struct arch_to_e_machine *map_element = element; +size_t prefix_len =strlen (map_element->prefix); + +return strncmp (search_key, map_element->prefix, prefix_len); +} + +static uint16_t perf_arch_to_e_machine (const char *perf_arch,bool is_64_bit) +{ +/* Binary search for a matching prefix. */ +const struct arch_to_e_machine *result; + +if (!perf_arch) +return EM_HOST ; + + result =bsearch (perf_arch, + prefix_to_e_machine,ARRAY_SIZE (prefix_to_e_machine), +sizeof (prefix_to_e_machine[0 ]), + compare_prefix); + +if (!result) { +pr_debug ("Unknown perf arch for ELF machine mapping: %s\n" , perf_arch); +return EM_NONE ; + } + +/* Handle conflicting prefixes. */ +switch (result->e_machine) { +case EM_ARM : +return !strcmp (perf_arch,"arm64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +return is_64_bit ||strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ; +case EM_SPARC : +return is_64_bit || !strcmp (perf_arch,"sparc64" ) ?EM_SPARCV9 :EM_SPARC ; +case EM_X86_64 : +return is_64_bit || !strcmp (perf_arch,"x86_64" ) ?EM_X86_64 :EM_386 ; +default : +return result->e_machine; + } +} + +static const char *e_machine_to_perf_arch (uint16_t e_machine) +{ +/* +* Table for if either the perf arch string differs from uname or there +* are >1 ELF machine with the prefix. +*/ +static const struct arch_to_e_machine extras[] = { + {"arm64" ,EM_AARCH64 }, + {"avr32" ,EM_AVR32 }, + {"powerpc" ,EM_PPC }, + {"powerpc" ,EM_PPC64 }, + {"sparc" ,EM_SPARCV9 }, + {"x86" ,EM_386 }, + {"x86" ,EM_X86_64 }, + {"none" ,EM_NONE }, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (extras); i++) { +if (extras[i].e_machine == e_machine) +return extras[i].prefix; + } + +for (size_t i =0 ; i <ARRAY_SIZE (prefix_to_e_machine); i++) { +if (prefix_to_e_machine[i].e_machine == e_machine) +return prefix_to_e_machine[i].prefix; + + } +return "unknown" ; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +if (!env) { +if (e_flags) + *e_flags =EF_HOST ; + +return EM_HOST ; + } +if (env->e_machine ==EM_NONE ) { + env->e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit); + +if (env->e_machine ==EM_HOST ) + env->e_flags =EF_HOST ; + } +if (e_flags) + *e_flags = env->e_flags; + +return env->e_machine; }const char *perf_env__arch (struct perf_env *env) { -char *arch_name; +if (!env) +return e_machine_to_perf_arch (EM_HOST ); -if (!env || !env->arch) {/* Assume local operation */ -static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +if (!env->arch) { +/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); -return normalize_arch (arch_name); + env->arch =strdup (e_machine_to_perf_arch (e_machine)); + } +return env->arch; }#if defined(HAVE_LIBTRACEEVENT )
tools/perf/util/env.h +1 -0
@@ -187,6 +187,7 @@ int perf_env__read_cpu_topology_map(struct perf_env *env);void cpu_cache_level__free (struct cpu_cache_level *cache); +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env);const char *perf_env__arch_strerrno (struct perf_env *env,int err);const char *perf_env__cpuid (struct perf_env *env);
tools/perf/util/header.c +34 -10
65 lines not on this page · raw message
tools/perf/util/session.c +8 -6
@@ -3023,14 +3023,16 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* Is the env caching an e_machine? */ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
[email protected]
AI reviewRe: [PATCH v6 1/3] perf env: Add perf_env__e_machine helper and use in perf_env__arch
Sashiko AI review found 4 potential issue(s): - [High] perf_session__e_machine() bypasses lazy evaluation of e_machine from env->arch when env->e_machine is EM_NONE, breaking cross-architecture…
Sashiko AI review found 4 potential issue(s):
- [High] perf_session__e_machine() bypasses lazy evaluation of e_machine from env->arch when env->e_machine is EM_NONE, breaking cross-architecture analysis of legacy perf.data files.
- [High] Uninitialized env->kernel_is_64_bit coerces to true, causing 32-bit architectures to be parsed as 64-bit.
- [High] write_arch() silently caches EM_HOST, permanently bypassing accurate thread-based architecture detection during perf record.
- [Medium] Unhandled OOM in process_arch() leaves env->arch as NULL, leading to a subsequent NULL pointer dereference.
--
commit a2ed4a04364ee664047eec1e47e4e540e458eff9
Author: Ian Rogers <[email protected]>
perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back of EM_HOST. Use the perf_env's arch to compute the e_machine if available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules.
Quoted 4 lines
tools/perf/util/env.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/util/env.c +0
-0@@ -588,51 +590,160 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)
[ ... ]
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 721
Quoted 10 lines
tools/perf/util/env.c +10
-0+uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +if (!env) { +if (e_flags) + *e_flags =EF_HOST ; + +return EM_HOST ; + } +if (env->e_machine ==EM_NONE ) { + env->e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit);
Is env->kernel_is_64_bit guaranteed to be a boolean here?
If env->kernel_is_64_bit is uninitialized, it defaults to -1 in perf_env__init(). In C, -1 evaluates as true, which would cause 32-bit architectures to be parsed as 64-bit (e.g., returning EM_X86_64 instead of EM_386).
Should this use the lazy evaluator perf_env__kernel_is_64_bit(env) instead?
[ ... ]
Quoted 5 lines
tools/perf/util/header.c +0
-0@@ -370,21 +370,25 @@ static int write_osrelease(struct feat_fd *ff,
[ ... ]
tools/perf/util/header.c static int write_osrelease(struct feat_fd *ff, · line 379
Quoted 13 lines
tools/perf/util/header.c +4
-6-static int write_arch (struct feat_fd *ff, -struct evlist *evlist__maybe_unused ) +static int write_arch (struct feat_fd *ff,struct evlist *evlist) {struct utsname uts; -int ret; +const char *arch =NULL ; - ret =uname (&uts); -if (ret <0 ) -return -1 ; +if (evlist->session) + arch =perf_env__arch (perf_session__env (evlist->session));
When write_arch() is called during perf record, it invokes perf_env__arch(). Since env->arch is NULL and env->e_machine is EM_NONE, this falls back to perf_arch_to_e_machine(NULL, ...), locking env->e_machine to EM_HOST.
Later, when write_e_machine() calls perf_session__e_machine(), the check for env->e_machine != EM_NONE will evaluate to true.
Will this permanently bypass the accurate thread-based architecture detection (machines__for_each_thread) designed to handle mixed 32/64-bit threads?
[ ... ]
Quoted 1 line
tools/perf/util/header.c +0
-0@@ -2675,10 +2679,30 @@ static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
[ ... ]
Quoted 6 lines
tools/perf/util/header.c +6
-0+static int process_arch (struct feat_fd *ff,void *data__maybe_unused ) +{ +uint16_t saved_e_machine = ff->ph->env.e_machine; + +free (ff->ph->env.arch); + ff->ph->env.arch =do_read_string (ff);
tools/perf/util/header.c static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \ · line 2700
Quoted 10 lines
tools/perf/util/header.c +10
-0+if (!ff->ph->env.arch) +return -ENOMEM ; +/* +* Make the arch string canonical by computing the e_machine from it, +* then turning the e_machine back into an arch string. +*/ + ff->ph->env.e_machine =EM_NONE ; +if (perf_env__e_machine (&ff->ph->env,/*e_flags=*/ NULL ) !=EM_NONE ) { +zfree (&ff->ph->env.arch); +perf_env__arch (&ff->ph->env);
If the strdup() inside perf_env__arch() fails due to memory constraints, it returns NULL and env->arch remains NULL.
Since process_arch() unconditionally returns 0 here, could this lead to a NULL pointer dereference later if strncmp() is called on the architecture string?
Quoted 8 lines
tools/perf/util/header.c +5
-0+ } + ff->ph->env.e_machine = saved_e_machine; +return 0 ; +} +static int process_e_machine (struct feat_fd *ff,void *data__maybe_unused ) {int ret;
[ ... ]
Quoted 8 lines
tools/perf/util/session.c +0
-0@@ -3023,14 +3023,16 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; }
tools/perf/util/session.c uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags · line 3029
Quoted 10 lines
tools/perf/util/session.c +3
-6+/* Is the env caching an e_machine? */ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags);
For legacy perf.data files without HEADER_E_MACHINE, process_arch() restores env->e_machine to EM_NONE.
Since the condition here explicitly requires env->e_machine != EM_NONE, won't this completely bypass the new lazy evaluator and fall back to EM_HOST, potentially breaking cross-architecture analysis for legacy files?
Quoted 9 lines
tools/perf/util/session.c +5
-0+/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerv7 cover perf symbol/env: ELF machine clean up and lazy idle computation
In reply to Ian RogersAdd a helper to perf_env to compute the e_machine if it is EM_NONE. Derive the value from the arch string if available. Similarly derive the arch string from the ELF machine if available, for consistency. This means perf's arch (machine type) is no longer determined by uname but set to match that of the perf ELF executable.
Switch the idle computation to the point of use and lazily compute it, rather than computing it for every symbol. The current only user is perf top. At the point of use the perf_env is available and this can be used to make sure the idle function computation is machine and kernel version dependent.
To avoid concurrent update issues with bitfields sharing a byte in struct symbol due to the lazy computation, introduce a global lock for updates to these fields and use setter functions. The reads remain lockless.
v7:
- Address better handling of strdup failures with arch in the header/env.
- Address concurrent update issues in struct symbol bitfields by introducing a global lock for writes.
v6: Ensure arch is canonical by going to e_machine and back (Sashiko)
https://lore.kernel.org/linux-perf-users/[email protected]/
v5: Add perf_env os_release helper (Namhyung/Sashiko)
https://lore.kernel.org/lkml/[email protected]/
v4: Fix Sashiko issues where an array element wasn't sorted properly,
the e_flags weren't returned properly, the idle type is change to a u8 rather than an enum value and the s390 version check for psw_idle is slightly reordered and tweaked.
https://lore.kernel.org/lkml/[email protected]/
v3: Properly set up the e_machine coming from the perf_env as reported
by Honglei Wang.
https://lore.kernel.org/lkml/[email protected]/
v2: Some minor white space clean up:
https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
Ian Rogers (4):
perf env: Add perf_env__e_machine helper and use in perf_env__arch perf env: Add helper to lazily compute the os_release perf symbol: Add setters for bitfields sharing a byte to avoid
concurrent update issues
perf symbol: Lazily compute idle and use a global lock for updates
tools/perf/builtin-kwork.c | 2 +- tools/perf/builtin-sched.c | 2 +- tools/perf/util/annotate.c | 2 +- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/env.c | 218 +++++++++++++++++++++++++----- tools/perf/util/env.h | 2 + tools/perf/util/header.c | 63 +++++++-- tools/perf/util/session.c | 25 ++-- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 134 ++++++++++++------ tools/perf/util/symbol.h | 17 ++- 11 files changed, 357 insertions(+), 112 deletions(-)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v7 1/4 perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back of EM_HOST. Use the perf_env's arch to compute the e_machine if available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switch perf_env__arch to be derived the e_machine for consistency. This switches arch from being uname derived to matching that of the perf binary (via EM_HOST). Update session to use the helper, which may mean using EM_HOST when no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.
--- tools/perf/util/env.c | 197 +++++++++++++++++++++++++++++++------- tools/perf/util/env.h | 1 + tools/perf/util/header.c | 47 +++++++-- tools/perf/util/session.c | 25 ++--- 4 files changed, 212 insertions(+), 58 deletions(-)
tools/perf/util/env.c +160 -37
@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #include "cpumap.h" +#include "dwarf-regs.h" #include "debug.h" #include "env.h" #include "util/header.h" #include "util/rwsem.h" #include <linux/compiler.h> +#include <linux/kernel.h> #include <linux/ctype.h> #include <linux/rbtree.h> #include <linux/string.h>
@@ -588,51 +590,172 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)zfree (&cache->size); } +struct arch_to_e_machine { +const char *prefix; +uint16_t e_machine; +}; +/* -* Return architecture name in a normalized form. -* The conversion logic comes from the Makefile. +* A mapping from an arch prefix string to an ELF machine that can be used in a +* bsearch. Some arch prefixes are shared an need additional processing as +* marked next to the architecture. The prefixes handle both perf's architecture +* naming and those from uname. */ -static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch; +static const struct arch_to_e_machine prefix_to_e_machine[] = { + {"aarch64" ,EM_AARCH64 }, + {"alpha" ,EM_ALPHA }, + {"arc" ,EM_ARC }, + {"arm" ,EM_ARM },/* Check also for EM_AARCH64. */ + {"avr" ,EM_AVR },/* Check also for EM_AVR32. */ + {"bfin" ,EM_BLACKFIN }, + {"blackfin" ,EM_BLACKFIN }, + {"cris" ,EM_CRIS }, + {"csky" ,EM_CSKY }, + {"hppa" ,EM_PARISC }, + {"i386" ,EM_386 }, + {"i486" ,EM_386 }, + {"i586" ,EM_386 }, + {"i686" ,EM_386 }, + {"loongarch" ,EM_LOONGARCH }, + {"m32r" ,EM_M32R }, + {"m68k" ,EM_68K }, + {"microblaze" ,EM_MICROBLAZE }, + {"mips" ,EM_MIPS }, + {"msp430" ,EM_MSP430 }, + {"parisc" ,EM_PARISC }, + {"powerpc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"ppc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"riscv" ,EM_RISCV }, + {"s390" ,EM_S390 }, + {"sa110" ,EM_ARM }, + {"sh" ,EM_SH }, + {"sparc" ,EM_SPARC },/* Check also for EM_SPARCV9. */ + {"sun4u" ,EM_SPARC }, + {"x86" ,EM_X86_64 },/* Check also for EM_386. */ + {"xtensa" ,EM_XTENSA }, +}; + +static int compare_prefix (const void *key,const void *element) +{ +const char *search_key = key; +const struct arch_to_e_machine *map_element = element; +size_t prefix_len =strlen (map_element->prefix); + +return strncmp (search_key, map_element->prefix, prefix_len); +} + +static uint16_t perf_arch_to_e_machine (const char *perf_arch,int is_64_bit) +{ +/* Binary search for a matching prefix. */ +const struct arch_to_e_machine *result; + +if (!perf_arch) +return EM_HOST ; + + result =bsearch (perf_arch, + prefix_to_e_machine,ARRAY_SIZE (prefix_to_e_machine), +sizeof (prefix_to_e_machine[0 ]), + compare_prefix); + +if (!result) { +pr_debug ("Unknown perf arch for ELF machine mapping: %s\n" , perf_arch); +return EM_NONE ; + } + +/* +* Handle conflicting prefixes. If the is_64_bit is unknown (-1) then +* assume 64-bit. We can't use perf_env__kernel_is_64_bit as that +* depends on the arch string. +*/ +switch (result->e_machine) { +case EM_ARM : +return !strcmp (perf_arch,"arm64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +return (is_64_bit !=0 ) ||strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ; +case EM_SPARC : +return (is_64_bit !=0 ) || !strcmp (perf_arch,"sparc64" ) ?EM_SPARCV9 :EM_SPARC ; +case EM_X86_64 : +return (is_64_bit !=0 ) || !strcmp (perf_arch,"x86_64" ) ?EM_X86_64 :EM_386 ; +default : +return result->e_machine; + } +} + +static const char *e_machine_to_perf_arch (uint16_t e_machine) +{ +/* +* Table for if either the perf arch string differs from uname or there +* are >1 ELF machine with the prefix. +*/ +static const struct arch_to_e_machine extras[] = { + {"arm64" ,EM_AARCH64 }, + {"avr32" ,EM_AVR32 }, + {"powerpc" ,EM_PPC }, + {"powerpc" ,EM_PPC64 }, + {"sparc" ,EM_SPARCV9 }, + {"x86" ,EM_386 }, + {"x86" ,EM_X86_64 }, + {"none" ,EM_NONE }, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (extras); i++) { +if (extras[i].e_machine == e_machine) +return extras[i].prefix; + } + +for (size_t i =0 ; i <ARRAY_SIZE (prefix_to_e_machine); i++) { +if (prefix_to_e_machine[i].e_machine == e_machine) +return prefix_to_e_machine[i].prefix; + + } +return "unknown" ; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +if (!env) { +if (e_flags) + *e_flags =EF_HOST ; + +return EM_HOST ; + } +if (env->e_machine ==EM_NONE ) { + env->e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit); + +if (env->e_machine ==EM_HOST ) + env->e_flags =EF_HOST ; + } +if (e_flags) + *e_flags = env->e_flags; + +return env->e_machine; }const char *perf_env__arch (struct perf_env *env) { -char *arch_name; +uint16_t e_machine; +const char *arch; -if (!env || !env->arch) {/* Assume local operation */ -static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +if (!env) +return e_machine_to_perf_arch (EM_HOST ); + +if (env->arch) +return env->arch; -return normalize_arch (arch_name); +/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ + e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); + arch =e_machine_to_perf_arch (e_machine); + env->arch =strdup (arch); +/* +* Avoid potential crashes on the arch string if memory allocation in +* strdup fails and NULL were to be returned. +*/ +return env->arch ?: arch; }#if defined(HAVE_LIBTRACEEVENT )
tools/perf/util/env.h +1 -0
@@ -187,6 +187,7 @@ int perf_env__read_cpu_topology_map(struct perf_env *env);void cpu_cache_level__free (struct cpu_cache_level *cache); +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env);const char *perf_env__arch_strerrno (struct perf_env *env,int err);const char *perf_env__cpuid (struct perf_env *env);
tools/perf/util/header.c +37 -10
68 lines not on this page · raw message
tools/perf/util/session.c +14 -11
@@ -3023,14 +3023,19 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* +* Is the env caching an e_machine? If not we want to compute from the +* more accurate threads. +*/ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);
@@ -3048,10 +3053,8 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags/* * Couldn't determine from the perf_env or current set of -* threads. Default to the host. +* threads. Potentially use logic that uses the arch string otherwise +* default to the host. */ -if (e_flags) - *e_flags =EF_HOST ; - -return EM_HOST ; +return perf_env__e_machine (env, e_flags); }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v7 1/4] perf env: Add perf_env__e_machine helper and use in perf_env__arch
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] Premature caching of fallback e_machine defeats dynamic thread-based architecture detection.…
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] Premature caching of fallback e_machine defeats dynamic thread-based architecture detection.
- [High] Canonicalization of env->arch breaks perf_env__kernel_is_64_bit(), falsely treating 64-bit traces as 32-bit.
- [High] Older 32-bit perf.data files are falsely promoted to 64-bit due to uninitialized kernel_is_64_bit state.
- [High] Inconsistent caching causes perf_env__arch() to unpredictably return unnormalized or canonical strings depending on initialization order.
- [High] Changing the format of the HEADER_ARCH string written to perf.data files breaks backward compatibility and existing tests.
- [Medium] OOM during string allocation in process_arch() causes a silent NULL pointer dereference in downstream consumers. --
commit 3dd924151546a227a61b859787ac74725a289795
Author: Ian Rogers <[email protected]>
perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back of EM_HOST. Use the perf_env's arch to compute the e_machine if available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switch perf_env__arch to be derived the e_machine for consistency. This switches arch from being uname derived to matching that of the perf binary (via EM_HOST). Update session to use the helper, which may mean using EM_HOST when no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.
Quoted 4 lines
tools/perf/util/env.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/util/env.c +0
-0@@ -627,6 +629,40 @@ static uint16_t perf_arch_to_e_machine(const char *perf_arch, int is_64_bit)
[ ... ]
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 678
Quoted 9 lines
tools/perf/util/env.c +9
-0+switch (result->e_machine) { +case EM_ARM : +return !strcmp (perf_arch,"arm64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +return (is_64_bit !=0 ) ||strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ; +case EM_SPARC : +return (is_64_bit !=0 ) || !strcmp (perf_arch,"sparc64" ) ?EM_SPARCV9 :EM_SPARC ;
Does this logic introduce a regression that inadvertently promotes older 32-bit perf.data files to 64-bit?
When process_arch() reads an older data file, it calls perf_env__e_machine() which invokes this function. At that point, env->kernel_is_64_bit is still uninitialized, holding a value of -1.
Since -1 != 0 evaluates to true, would this unconditionally return EM_PPC64 and EM_SPARCV9 for older 32-bit ppc and sparc traces?
[ ... ]
Quoted 1 line
tools/perf/util/env.c +0
-0@@ -685,15 +688,20 @@ const char *perf_env__arch(struct perf_env *env)
[ ... ]
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 753
Quoted 7 lines
tools/perf/util/env.c +7
-0+/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ + e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); + arch =e_machine_to_perf_arch (e_machine); + env->arch =strdup (arch);
Could this lead to a regression with inconsistent behavior depending on initialization order?
If perf_env__read_arch() runs first, it caches the raw uname string (like x86_64). If perf_env__arch() runs first, it calculates and caches the canonical string (like x86).
Will this unpredictability break strict string comparisons against env->arch elsewhere in the codebase?
Quoted 4 lines
tools/perf/util/header.c +0
-0
[ ... ]
Quoted 4 lines
tools/perf/util/header.c +0
-1@@ -379,21 +379,28 @@ static int write_arch(struct feat_fd *ff, struct evlist *evlist) {struct utsname uts; -int ret;
tools/perf/util/header.c static int write_arch(struct feat_fd *ff, struct evlist *evlist) · line 387
Quoted 10 lines
tools/perf/util/header.c +6
-3+const char *arch =NULL ; - ret =uname (&uts); -if (ret <0 ) -return -1 ; +if (evlist->session) { +/* Force the computation in the perf_env of the e_machine of the threads. */ +perf_session__e_machine (evlist->session,/*e_flags=*/ NULL ); + arch =perf_env__arch (perf_session__env (evlist->session)); + }
Does writing the canonicalized string here cause a regression by breaking backward compatibility with existing tests?
By calling perf_env__arch(), this writes strings like "powerpc" into the HEADER_ARCH section of the data file, rather than the raw uts.machine strings like "ppc64le".
Tests such as test__session_topology parse HEADER_ARCH and conditionally skip topology validations using checks like strncmp(env->arch, "ppc64le", 7). Will this change cause those validations to run on unsupported platforms and fail?
[ ... ]
Quoted 7 lines
tools/perf/util/header.c +6
-0@@ -2684,10 +2691,30 @@ static int process_arch(struct feat_fd *ff, void *data __maybe_unused) +{ +uint16_t saved_e_machine = ff->ph->env.e_machine; + +free (ff->ph->env.arch); + ff->ph->env.arch =do_read_string (ff); +if (!ff->ph->env.arch)
tools/perf/util/header.c static int process_arch(struct feat_fd *ff, void *data __maybe_unused) · line 2706
Quoted 10 lines
tools/perf/util/header.c +10
-0+return -ENOMEM ; +/* +* Make the arch string canonical by computing the e_machine from it, +* then turning the e_machine back into an arch string. +*/ + ff->ph->env.e_machine =EM_NONE ; +if (perf_env__e_machine (&ff->ph->env,/*e_flags=*/ NULL ) !=EM_NONE ) { +zfree (&ff->ph->env.arch); +perf_env__arch (&ff->ph->env); + }
Will replacing env->arch with the canonicalized string introduce a regression that breaks 64-bit architecture detection?
The function perf_env__init_kernel_mode() expects env->arch to contain the raw uname string, matching exactly against strings like "x86_64" or "s390x". Since env->arch now contains the canonical string (e.g., "x86"), won't env->kernel_is_64_bit erroneously evaluate to 0 for 64-bit traces?
Also, is it possible for env->arch to be left as NULL here, leading to a regression where downstream consumers crash?
If the strdup() inside perf_env__arch() fails due to memory pressure, perf_env__arch() will safely return a static string, but env->arch will remain NULL.
Since process_arch() ignores the return value of perf_env__arch(), will consumers crash when attempting to use env->arch directly?
Quoted 4 lines
tools/perf/util/session.c +0
-0
[ ... ]
Quoted 4 lines
tools/perf/util/session.c +0
-0@@ -3048,10 +3053,8 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags/* * Couldn't determine from the perf_env or current set of
tools/perf/util/session.c uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags · line 3060
Quoted 10 lines
tools/perf/util/session.c +3
-5-* threads. Default to the host. +* threads. Potentially use logic that uses the arch string otherwise +* default to the host. */ -if (e_flags) - *e_flags =EF_HOST ; - -return EM_HOST ; +return perf_env__e_machine (env, e_flags); }
Can this lead to a regression where the session architecture gets permanently locked to the host architecture?
If perf_session__e_machine() is called early (for example, from write_e_machine() before threads are synthesized), machines__for_each_thread() will find no threads.
The function will then fall back to perf_env__e_machine() here, which computes and permanently caches EM_HOST into env->e_machine.
Because env->e_machine is now cached and != EM_NONE, all future calls will take the fast-path at the top of this function, completely bypassing the dynamic thread scanning logic.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH v7 2/4 perf env: Add helper to lazily compute the os_release
In reply to Ian RogersIn live mode the os_release isn't being initialized, make a lazy initialization helper that assumes when the os_release isn't initialized this is live mode.
--- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/env.c | 21 +++++++++++++++++++++ tools/perf/util/env.h | 1 + tools/perf/util/header.c | 16 +++++++++++----- tools/perf/util/symbol.c | 4 ++-- 5 files changed, 36 insertions(+), 8 deletions(-)
tools/perf/util/data-convert-bt.c +1 -1
@@ -1414,7 +1414,7 @@ do { \ADD ("host" , env->hostname);ADD ("sysname" ,"Linux" ); -ADD ("release" , env->os_release); +ADD ("release" ,perf_env__os_release (env));ADD ("version" , env->version);ADD ("machine" , env->arch);ADD ("domain" ,"kernel" );
tools/perf/util/env.c +21 -0
@@ -330,6 +330,27 @@ int perf_env__kernel_is_64_bit(struct perf_env *env)return env->kernel_is_64_bit; } +const char *perf_env__os_release (struct perf_env *env) +{ +struct utsname uts; +int ret; + +if (!env) +return perf_version_string; + +if (env->os_release) +return env->os_release; + +/* +* The os_release is being accessed but wasn't initialized from a data +* file, assume this is 'live' mode and use the release from uname. If +* uname or strdup fails then use the current perf tool version. +*/ + ret =uname (&uts); + env->os_release =strdup (ret <0 ? perf_version_string : uts.release); +return env->os_release ?: perf_version_string; +} +int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]) {int i;
tools/perf/util/env.h +1 -0
@@ -175,6 +175,7 @@ void free_cpu_domain_info(struct cpu_domain_map **cd_map, u32 schedstat_version,void perf_env__exit (struct perf_env *env);int perf_env__kernel_is_64_bit (struct perf_env *env); +const char *perf_env__os_release (struct perf_env *env);int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]);
tools/perf/util/header.c +11 -5
@@ -370,13 +370,19 @@ static int write_osrelease(struct feat_fd *ff, struct evlist *evlist __maybe_unused) {struct utsname uts; -int ret; +const char *release =NULL ; - ret =uname (&uts); -if (ret <0 ) -return -1 ; +if (evlist->session) + release =perf_env__os_release (perf_session__env (evlist->session)); -return do_write_string (ff, uts.release); +if (!release) { +int ret =uname (&uts); + +if (ret <0 ) +return -1 ; + release = uts.release; + } +return do_write_string (ff, release); }static int write_arch (struct feat_fd *ff,struct evlist *evlist)
tools/perf/util/symbol.c +2 -2
@@ -2209,7 +2209,7 @@ static int vmlinux_path__init(struct perf_env *env) {struct utsname uts;char bf[PATH_MAX ]; -char *kernel_version; +const char *kernel_version;unsigned int i; vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
@@ -2226,7 +2226,7 @@ static int vmlinux_path__init(struct perf_env *env)return 0 ;if (env) { - kernel_version = env->os_release; + kernel_version =perf_env__os_release (env); }else {if (uname (&uts) <0 ) goto out_fail;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v7 2/4] perf env: Add helper to lazily compute the os_release
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The `perf_env__os_release()` helper corrupts offline trace metadata by incorrectly assuming an…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The perf_env__os_release() helper corrupts offline trace metadata by incorrectly assuming an uninitialized os_release implies 'live mode', lazily populating it with the analysis host's kernel version.
- [Medium] Using perf_version_string as a fallback for the OS release is semantically incorrect and pollutes trace headers.
--
commit 9cca0f2ccb3995708fd2106d23dda3d1c07d1fd9
Author: Ian Rogers <[email protected]>
perf env: Add helper to lazily compute the os_release
In live mode the os_release isn't being initialized, make a lazy initialization helper that assumes when the os_release isn't initialized this is live mode.
Quoted 4 lines
tools/perf/util/env.c +0
-0
[ ... ]
Quoted 11 lines
tools/perf/util/env.c +7
-0@@ -330,6 +330,27 @@ int perf_env__kernel_is_64_bit(struct perf_env *env)return env->kernel_is_64_bit; } +const char *perf_env__os_release (struct perf_env *env) +{ +struct utsname uts; +int ret; + +if (!env) +return perf_version_string;
tools/perf/util/env.c int perf_env__kernel_is_64_bit(struct perf_env *env) · line 349
Quoted 10 lines
tools/perf/util/env.c +10
-0+ +if (env->os_release) +return env->os_release; + +/* +* The os_release is being accessed but wasn't initialized from a data +* file, assume this is 'live' mode and use the release from uname. If +* uname or strdup fails then use the current perf tool version. +*/ + ret =uname (&uts);
If we are doing offline analysis of a perf.data file that lacks the OS release feature header, env->os_release will be NULL.
Does this helper incorrectly assume we are in live mode, and lazily populate env->os_release with the analysis host's kernel version?
If so, it appears this causes tools to use the wrong vmlinux path for the target profile in vmlinux_path__init() and potentially break symbol resolution.
It also looks like this causes ctf_writer__setup_env() to export the host kernel version into the CTF trace instead of safely skipping the missing field as it did previously.
tools/perf/util/env.c int perf_env__kernel_is_64_bit(struct perf_env *env) · line 352
Quoted 3 lines
tools/perf/util/env.c +3
-0+ env->os_release =strdup (ret <0 ? perf_version_string : uts.release); +return env->os_release ?: perf_version_string; +}
Is it appropriate to use the perf tool version (perf_version_string) as a fallback for the OS release if uname() or strdup() fails?
This string could end up permanently written into the perf.data file's OS release section, or cause bogus filesystem lookups in vmlinux_path__init() since the tool version format differs from the kernel release.
Would it be safer to return NULL or a generic unknown string here instead?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerPATCH v7 3/4 perf symbol: Add setters for bitfields sharing a byte to avoid concurrent update issues
In reply to Ian RogersA problem with putting bitfields into struct symbol is that other bits in the symbol could be updated concurrently and only one update to the underlying storage unit happen, leading to lost updates.
To avoid this, introduce a global lock symbol_bits_lock in symbol.c and helper functions to update the bits sharing a byte:symbol__set_ignore and symbol__set_annotate2.
inlined is not given a setter as it is only initialized in new_inline_sym when the symbol is under construction and not shared.
--- tools/perf/builtin-kwork.c | 2 +- tools/perf/builtin-sched.c | 2 +- tools/perf/util/annotate.c | 2 +- tools/perf/util/symbol.c | 22 ++++++++++++++++++++++ tools/perf/util/symbol.h | 3 +++ 5 files changed, 28 insertions(+), 3 deletions(-)
tools/perf/builtin-kwork.c +1 -1
@@ -725,7 +725,7 @@ static void timehist_save_callchain(struct perf_kwork *kwork,if (sym) {if (!strcmp (sym->name,"__softirqentry_text_start" ) || !strcmp (sym->name,"__do_softirq" )) - sym->ignore =1 ; +symbol__set_ignore (sym, true); }callchain_cursor_advance (cursor);
tools/perf/builtin-sched.c +1 -1
@@ -2371,7 +2371,7 @@ static void save_task_callchain(struct perf_sched *sched,if (!strcmp (sym->name,"schedule" ) || !strcmp (sym->name,"__schedule" ) || !strcmp (sym->name,"preempt_schedule" )) - sym->ignore =1 ; +symbol__set_ignore (sym, true); }callchain_cursor_advance (cursor);
tools/perf/util/annotate.c +1 -1
@@ -2224,7 +2224,7 @@ int symbol__annotate2(struct map_symbol *ms, struct evsel *evsel,annotation__init_column_widths (notes, sym);annotation__update_column_widths (notes); - sym->annotate2 =1 ; +symbol__set_annotate2 (sym, true);return 0 ; }
tools/perf/util/symbol.c +22 -0
@@ -31,6 +31,7 @@#include "map.h" #include "symbol.h" #include "map_symbol.h" +#include "mutex.h" #include "mem-events.h" #include "mem-info.h" #include "symsrc.h"
@@ -52,6 +53,8 @@ static int dso__load_kernel_sym(struct dso *dso, struct map *map);static int dso__load_guest_kernel_sym (struct dso *dso,struct map *map);static bool symbol__is_idle (const char *name); +static struct mutex symbol_bits_lock; +int vmlinux_path__nr_entries;char **vmlinux_path;
@@ -345,6 +348,20 @@ void symbol__delete(struct symbol *sym)free (((void *)sym) - symbol_conf.priv_size); } +void symbol__set_ignore (struct symbol *sym,bool ignore) +{ +mutex_lock (&symbol_bits_lock); + sym->ignore = ignore; +mutex_unlock (&symbol_bits_lock); +} + +void symbol__set_annotate2 (struct symbol *sym,bool annotate2) +{ +mutex_lock (&symbol_bits_lock); + sym->annotate2 = annotate2; +mutex_unlock (&symbol_bits_lock); +} +void symbols__delete (struct rb_root_cached *symbols) {struct symbol *pos;
@@ -2398,6 +2415,8 @@ int symbol__init(struct perf_env *env)if (symbol_conf.initialized)return 0 ; +mutex_init (&symbol_bits_lock); + symbol_conf.priv_size =PERF_ALIGN (symbol_conf.priv_size,sizeof (u64));symbol__elf_init ();
@@ -2476,6 +2495,9 @@ void symbol__exit(void) {if (!symbol_conf.initialized)return ; + +mutex_destroy (&symbol_bits_lock); +strlist__delete (symbol_conf.bt_stop_list);strlist__delete (symbol_conf.sym_list);strlist__delete (symbol_conf.dso_list);
tools/perf/util/symbol.h +3 -0
@@ -77,6 +77,9 @@ struct symbol {void symbol__delete (struct symbol *sym);void symbols__delete (struct rb_root_cached *symbols); +void symbol__set_ignore (struct symbol *sym,bool ignore); +void symbol__set_annotate2 (struct symbol *sym,bool annotate2); +/* symbols__for_each_entry - iterate over symbols (rb_root) * * @symbols: the rb_root of symbols
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v7 3/4] perf symbol: Add setters for bitfields sharing a byte to avoid concurrent update issues
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Data Race / UB on concurrent reads of bitfields - [High] Architectural flaw and severe performance…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Data Race / UB on concurrent reads of bitfields
- [High] Architectural flaw and severe performance regression due to unconditional global lock
- [High] Subsystem rule violation: Event handler completeness missing in builtin-sched.c
- [Medium] Unbalanced lifecycle and resource leak on error paths in symbol__init() --
commit eaf96d025426eab89c805716fc26dd2399ff6394
Author: Ian Rogers <[email protected]>
perf symbol: Add setters for bitfields sharing a byte to avoid concurrent update issues
This commit introduces a global mutex to serialize modifications to bitfields within the symbol structure. Helper functions are provided to update these fields safely across concurrent threads.
tools/perf/builtin-sched.c
Quoted 4 lines
tools/perf/builtin-sched.c +0
-0
This isn't a bug in the current changes, but while reviewing builtin-sched.c, it appears the sched tool registers the .mmap handler but misses .mmap2.
When a subcommand registers .mmap but not .mmap2, modern kernels generating mmap2 events will have those events silently dropped. Is it possible this leaves the machine vmlinux maps and process memory maps incomplete or null?
[ ... ]
Quoted 4 lines
tools/perf/util/symbol.c +0
-0
[ ... ]
tools/perf/util/symbol.c void symbol__delete(struct symbol *sym) · line 356
Quoted 10 lines
tools/perf/util/symbol.c +6
-0@@ -345,6 +348,20 @@ void symbol__delete(struct symbol *sym)free (((void *)sym) - symbol_conf.priv_size); } +void symbol__set_ignore (struct symbol *sym,bool ignore) +{ +mutex_lock (&symbol_bits_lock); + sym->ignore = ignore; +mutex_unlock (&symbol_bits_lock); +}
Are concurrent reads left unprotected by this implementation?
While the lock protects the read-modify-write cycle of the bitfield updates here, existing readers access these fields without holding the lock. For example:
builtin-sched.c:save_task_callchain() {
...
if (sym->ignore)
...
}Under the C11 memory model, reading a bitfield while another thread concurrently modifies an adjacent bitfield in the same byte constitutes a data race. Could this result in torn reads and undefined behavior?
Additionally, is a global lock necessary for these updates?
Unconditional acquisition of a global lock on hot paths like timehist_save_callchain() and save_task_callchain() might cause a performance regression, especially since it is acquired even if the flag is already true.
On 64-bit systems, struct symbol has 3 bytes of internal padding before the dso pointer. Converting the bitfields to separate bool or u8 fields would allow them to occupy this padding. This layout would ensure each field resides in a separate byte, allowing hardware byte-level atomicity to eliminate false sharing and the need for locking entirely.
[ ... ]
tools/perf/util/symbol.c int symbol__init(struct perf_env *env) · line 2420
Quoted 7 lines
tools/perf/util/symbol.c +2
-0@@ -2398,6 +2415,8 @@ int symbol__init(struct perf_env *env)if (symbol_conf.initialized)return 0 ; +mutex_init (&symbol_bits_lock); + symbol_conf.priv_size =PERF_ALIGN (symbol_conf.priv_size,sizeof (u64));
Could this skip destroying the mutex on error paths?
If setup_list() or other initializations fail later in symbol__init(), the function returns -1 or jumps to error labels without calling mutex_destroy(). If the caller retries initialization, calling mutex_init() on an already-initialized mutex can result in undefined behavior.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerPATCH v7 4/4 perf symbol: Lazily compute idle and use a global lock for updates
In reply to Ian RogersMove the idle boolean to a helper symbol__is_idle function. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. As symbols__insert no longer needs to know if a symbol is for the kernel, remove the argument.
This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/
The change switches x86 matches to use strstarts which means intel_idle_irq is matched as part of strstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/
To avoid concurrent update issues with other bitfields in struct symbol, this change uses the global lock symbol_bits_lock (introduced in a previous commit) for updates to the idle field. A static helper symbol__set_idle taking a boolean is used to encapsulate the lock and mapping to enum symbol_idle_kind.
--- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 108 +++++++++++++++++++++++------------ tools/perf/util/symbol.h | 14 +++-- 3 files changed, 81 insertions(+), 43 deletions(-)
tools/perf/util/symbol-elf.c +1 -1
@@ -1727,7 +1727,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);
tools/perf/util/symbol.c +70 -38
172 lines not on this page · raw message
tools/perf/util/symbol.h +10 -4
@@ -43,6 +43,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index).
@@ -58,8 +64,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle holding enum symbol_idle_kind values. */ + u8 idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */
@@ -197,8 +203,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);
@@ -281,5 +286,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,struct perf_env *env);#endif /* __PERF_SYMBOL */
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v7 4/4] perf symbol: Lazily compute idle and use a global lock for updates
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The new lazy evaluation helper `symbol__is_idle()` is never called, and the migration to a…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The new lazy evaluation helper symbol__is_idle() is never called, and the migration to a tri-state enum breaks existing boolean consumers of sym->idle.
- [High] Data race on lockless read of the sym->idle bitfield in symbol__is_idle().
--
commit 2ef259ec3239aea58dbf7c1d14b3998396292f27
Author: Ian Rogers <[email protected]>
perf symbol: Lazily compute idle and use a global lock for updates
This patch moves the idle boolean computation into a lazy helper, symbol__is_idle(), considering kernel version and architecture. It also changes the idle field to a tri-state enum and protects updates with a global lock.
Quoted 4 lines
tools/perf/util/symbol.c +0
-0
[ ... ]
Quoted 4 lines
tools/perf/util/symbol.c +0
-0@@ -384,17 +389,6 @@ void __symbols__insert(struct rb_root_cached *symbols,struct symbol *s;bool leftmost = true;
tools/perf/util/symbol.c void __symbols__insert(struct rb_root_cached *symbols, · line 396
Quoted 10 lines
tools/perf/util/symbol.c +0
-10-if (kernel) { -const char *name = sym->name; -/* -* ppc64 uses function descriptors and appends a '.' to the -* start of every instruction address. Remove it. -*/ -if (name[0 ] =='.' ) - name++; - sym->idle =symbol__is_idle (name); - }
Is the new symbol__is_idle() helper actually called anywhere to lazily initialize this field?
It looks like the call was removed here, but the new function doesn't appear to be invoked anywhere else by the consumers.
[ ... ]
Quoted 3 lines
tools/perf/util/symbol.c +3
-0+bool symbol__is_idle (struct symbol *sym,const struct dso *dso,struct perf_env *env) +{ +static const char *const idle_symbols[] = {
[ ... ]
tools/perf/util/symbol.c int modules__parse(const char *filename, void *arg, · line 763
Quoted 8 lines
tools/perf/util/symbol.c +5
-2+ }; +const char *name = sym->name; +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); -if (idle_symbols_list) -return strlist__has_entry (idle_symbols_list, name); +if (sym->idle) +return sym->idle ==SYMBOL_IDLE__IDLE ;
Does reading the sym->idle bitfield here without holding symbol_bits_lock cause a data race?
Since adjacent bitfields in struct symbol share the same memory location, a concurrent update to them performs a read-modify-write cycle that could race with this lockless read, potentially resulting in undefined behavior.
[ ... ]
Quoted 18 lines
tools/perf/util/symbol.h +6
-0@@ -43,6 +43,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index).
tools/perf/util/symbol.h struct symbol { · line 68
Quoted 7 lines
tools/perf/util/symbol.h +2
-2@@ -58,8 +64,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle holding enum symbol_idle_kind values. */ + u8 idle:2;
Are there still places evaluating sym->idle directly as a boolean?
Since idle now defaults to SYMBOL_IDLE__UNKNOWN (0), a check like !al.sym->idle would evaluate to true for all symbols initially, bypassing existing filters.
If a non-idle symbol is later evaluated and set to SYMBOL_IDLE__NOT_IDLE (1), wouldn't !al.sym->idle evaluate to false, causing valid symbols to be dropped incorrectly?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Ian Rogers
reviewerv8 cover perf symbol/env: ELF machine clean up and lazy idle computation
In reply to Ian RogersAdd a helper to perf_env to compute the e_machine if it is EM_NONE. Derive the value from the arch string if available. Similarly derive the arch string from the ELF machine if available, for consistency. This means perf's arch (machine type) is no longer determined by uname but set to match that of the perf ELF executable.
Switch the idle computation to the point of use and lazily compute it, rather than computing it for every symbol. The current only user is perf top. At the point of use the perf_env is available and this can be used to make sure the idle function computation is machine and kernel version dependent.
To avoid concurrent update issues with bitfields sharing a byte in struct symbol due to the lazy computation, introduce a global lock for updates to these fields and use setter functions. The reads remain lockless.
v8:
- Address Sashiko AI review feedback for Patch 1:
- Switch all code dependent on the arch string to use e_machine instead (e.g., in perf c2c, perf lock-contention, perf header, perf arch common, tests/topology.c, perf_env__init_kernel_mode).
- Update machine__is and machine__normalized_is to take e_machine integers instead of strings.
- Refactor arch_syscalls__strerrno_function (generated via arch_errno_names.sh) to take an e_machine instead of an arch string.
- Avoid premature caching of the host architecture in perf_session__e_machine by using a non-caching helper when threads are not yet available.
v7:
- Address better handling of strdup failures with arch in the header/env.
- Address concurrent update issues in struct symbol bitfields by introducing a global lock for writes.
https://lore.kernel.org/linux-perf-users/[email protected]/
v6: Ensure arch is canonical by going to e_machine and back (Sashiko)
https://lore.kernel.org/linux-perf-users/[email protected]/
v5: Add perf_env os_release helper (Namhyung/Sashiko)
https://lore.kernel.org/lkml/[email protected]/
v4: Fix Sashiko issues where an array element wasn't sorted properly,
the e_flags weren't returned properly, the idle type is change to a u8 rather than an enum value and the s390 version check for psw_idle is slightly reordered and tweaked.
https://lore.kernel.org/lkml/[email protected]/
v3: Properly set up the e_machine coming from the perf_env as reported
by Honglei Wang.
https://lore.kernel.org/lkml/[email protected]/
v2: Some minor white space clean up:
https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
Ian Rogers (17):
perf env: Add perf_env__e_machine helper and use in perf_env__arch perf tests topology: Switch env->arch use to env->e_machine perf capstone: Determine architecture from e_machine perf print_insn: Use e_machine for fallback IP length check perf machine: Use perf_env e_machine rather than arch perf sample-raw: Use perf_env e_machine rather than arch perf sort: Use perf_env e_machine rather than arch
perf symbol: Avoid use of machine__is
perf arch common: Use perf_env e_machine rather than arch perf header: In print_pmu_caps use perf_env e_machine perf c2c: Use perf_env e_machine rather than arch
perf lock-contention: Use perf_env e_machine rather than arch perf env: Refactor perf_env__arch_strerrno
perf env: Remove unused perf_env__raw_arch
perf env: Add helper to lazily compute the os_release perf symbol: Add setters for bitfields sharing a byte to avoid
concurrent update issues
perf symbol: Lazily compute idle and use a global lock for updates
tools/perf/arch/common.c | 55 ++-- tools/perf/builtin-c2c.c | 2 +- tools/perf/builtin-kwork.c | 2 +- tools/perf/builtin-sched.c | 2 +- tools/perf/builtin-trace.c | 5 +- tools/perf/tests/topology.c | 8 +- tools/perf/trace/beauty/arch_errno_names.sh | 40 ++- tools/perf/util/annotate.c | 2 +- tools/perf/util/capstone.c | 115 +++++--- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/env.c | 283 +++++++++++++++----- tools/perf/util/env.h | 11 +- tools/perf/util/header.c | 70 +++-- tools/perf/util/lock-contention.c | 6 +- tools/perf/util/machine.c | 25 +- tools/perf/util/machine.h | 2 - tools/perf/util/print_insn.c | 8 +- tools/perf/util/sample-raw.c | 18 +- tools/perf/util/session.c | 26 +- tools/perf/util/sort.c | 12 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 163 +++++++---- tools/perf/util/symbol.h | 17 +- 23 files changed, 612 insertions(+), 264 deletions(-)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v8 1/17 perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back to EM_HOST. Use the perf_env's arch to compute the e_machine if available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switch perf_env__arch to be derived the e_machine for consistency. This switches arch from being uname derived to matching that of the perf binary (via EM_HOST). Update session to use the helper, which may mean using EM_HOST when no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.
--- tools/perf/util/env.c | 231 +++++++++++++++++++++++++++++++------- tools/perf/util/env.h | 2 + tools/perf/util/header.c | 47 ++++++-- tools/perf/util/session.c | 26 +++-- 4 files changed, 243 insertions(+), 63 deletions(-)
tools/perf/util/env.c +189 -42
@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #include "cpumap.h" +#include "dwarf-regs.h" #include "debug.h" #include "env.h" #include "util/header.h" #include "util/rwsem.h" #include <linux/compiler.h> +#include <linux/kernel.h> #include <linux/ctype.h> #include <linux/rbtree.h> #include <linux/string.h>
@@ -309,12 +311,21 @@ void perf_env__init(struct perf_env *env)static void perf_env__init_kernel_mode (struct perf_env *env) { -const char *arch =perf_env__raw_arch (env); +uint16_t e_machine = env->e_machine; -if (!strncmp (arch,"x86_64" ,6 ) || !strncmp (arch,"aarch64" ,7 ) || - !strncmp (arch,"arm64" ,5 ) || !strncmp (arch,"mips64" ,6 ) || - !strncmp (arch,"parisc64" ,8 ) || !strncmp (arch,"riscv64" ,7 ) || - !strncmp (arch,"s390x" ,5 ) || !strncmp (arch,"sparc64" ,7 )) +if (env->arch && (e_machine ==EM_NONE || e_machine ==EM_MIPS || e_machine ==EM_RISCV )) { +if (str_ends_with (env->arch,"64" ) || !strncmp (env->arch,"s390x" ,5 )) + env->kernel_is_64_bit =1 ; +else + env->kernel_is_64_bit =0 ; +return ; + } +if (e_machine ==EM_NONE ) + e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); + +if (e_machine ==EM_X86_64 || e_machine ==EM_AARCH64 || + e_machine ==EM_PPC64 || e_machine ==EM_SPARCV9 || + e_machine ==EM_S390 ) env->kernel_is_64_bit =1 ;else env->kernel_is_64_bit =0 ;
@@ -588,51 +599,187 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)zfree (&cache->size); } +struct arch_to_e_machine { +const char *prefix; +uint16_t e_machine; +}; +/* -* Return architecture name in a normalized form. -* The conversion logic comes from the Makefile. +* A mapping from an arch prefix string to an ELF machine that can be used in a +* bsearch. Some arch prefixes are shared an need additional processing as +* marked next to the architecture. The prefixes handle both perf's architecture +* naming and those from uname. */ -static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch; +static const struct arch_to_e_machine prefix_to_e_machine[] = { + {"aarch64" ,EM_AARCH64 }, + {"alpha" ,EM_ALPHA }, + {"arc" ,EM_ARC }, + {"arm" ,EM_ARM },/* Check also for EM_AARCH64. */ + {"avr" ,EM_AVR },/* Check also for EM_AVR32. */ + {"bfin" ,EM_BLACKFIN }, + {"blackfin" ,EM_BLACKFIN }, + {"cris" ,EM_CRIS }, + {"csky" ,EM_CSKY }, + {"hppa" ,EM_PARISC }, + {"i386" ,EM_386 }, + {"i486" ,EM_386 }, + {"i586" ,EM_386 }, + {"i686" ,EM_386 }, + {"loongarch" ,EM_LOONGARCH }, + {"m32r" ,EM_M32R }, + {"m68k" ,EM_68K }, + {"microblaze" ,EM_MICROBLAZE }, + {"mips" ,EM_MIPS }, + {"msp430" ,EM_MSP430 }, + {"parisc" ,EM_PARISC }, + {"powerpc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"ppc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"riscv" ,EM_RISCV }, + {"s390" ,EM_S390 }, + {"sa110" ,EM_ARM }, + {"sh" ,EM_SH }, + {"sparc" ,EM_SPARC },/* Check also for EM_SPARCV9. */ + {"sun4u" ,EM_SPARC }, + {"x86" ,EM_X86_64 },/* Check also for EM_386. */ + {"xtensa" ,EM_XTENSA }, +}; + +static int compare_prefix (const void *key,const void *element) +{ +const char *search_key = key; +const struct arch_to_e_machine *map_element = element; +size_t prefix_len =strlen (map_element->prefix); + +return strncmp (search_key, map_element->prefix, prefix_len); +} + +static uint16_t perf_arch_to_e_machine (const char *perf_arch,int is_64_bit) +{ +/* Binary search for a matching prefix. */ +const struct arch_to_e_machine *result; + +if (!perf_arch) +return EM_HOST ; + + result =bsearch (perf_arch, + prefix_to_e_machine,ARRAY_SIZE (prefix_to_e_machine), +sizeof (prefix_to_e_machine[0 ]), + compare_prefix); + +if (!result) { +pr_debug ("Unknown perf arch for ELF machine mapping: %s\n" , perf_arch); +return EM_NONE ; + } + +/* +* Handle conflicting prefixes. If the is_64_bit is unknown (-1) then +* assume 64-bit. We can't use perf_env__kernel_is_64_bit as that +* depends on the arch string. +*/ +switch (result->e_machine) { +case EM_ARM : +return !strcmp (perf_arch,"arm64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +return (is_64_bit !=0 ) ||strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ; +case EM_SPARC : +return (is_64_bit !=0 ) || !strcmp (perf_arch,"sparc64" ) ?EM_SPARCV9 :EM_SPARC ; +case EM_X86_64 : +return (is_64_bit !=0 ) || !strcmp (perf_arch,"x86_64" ) ?EM_X86_64 :EM_386 ; +default : +return result->e_machine; + } +} + +static const char *e_machine_to_perf_arch (uint16_t e_machine) +{ +/* +* Table for if either the perf arch string differs from uname or there +* are >1 ELF machine with the prefix. +*/ +static const struct arch_to_e_machine extras[] = { + {"arm64" ,EM_AARCH64 }, + {"avr32" ,EM_AVR32 }, + {"powerpc" ,EM_PPC }, + {"powerpc" ,EM_PPC64 }, + {"sparc" ,EM_SPARCV9 }, + {"x86" ,EM_386 }, + {"x86" ,EM_X86_64 }, + {"none" ,EM_NONE }, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (extras); i++) { +if (extras[i].e_machine == e_machine) +return extras[i].prefix; + } + +for (size_t i =0 ; i <ARRAY_SIZE (prefix_to_e_machine); i++) { +if (prefix_to_e_machine[i].e_machine == e_machine) +return prefix_to_e_machine[i].prefix; + + } +return "unknown" ; +} + +uint16_t perf_env__e_machine_nocache (struct perf_env *env,uint32_t *e_flags) +{ +uint16_t e_machine =EM_HOST ; + +if (env) + e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit); + +if (e_flags && e_machine ==EM_HOST ) + *e_flags =EF_HOST ; + +return e_machine; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +uint16_t e_machine; +uint32_t local_e_flags; + +if (env && env->e_machine !=EM_NONE ) { +if (e_flags) + *e_flags = env->e_flags; + +return env->e_machine; + } + e_machine =perf_env__e_machine_nocache (env, &local_e_flags); +if (env) { + env->e_machine = e_machine; + env->e_flags = local_e_flags; + } +if (e_flags) + *e_flags = local_e_flags; + +return e_machine; }const char *perf_env__arch (struct perf_env *env) { -char *arch_name; +uint16_t e_machine; +const char *arch; -if (!env || !env->arch) {/* Assume local operation */ -static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +if (!env) +return e_machine_to_perf_arch (EM_HOST ); + +if (env->arch) +return env->arch; -return normalize_arch (arch_name); +/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ + e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); + arch =e_machine_to_perf_arch (e_machine); + env->arch =strdup (arch); +/* +* Avoid potential crashes on the arch string if memory allocation in +* strdup fails and NULL were to be returned. +*/ +return env->arch ?: arch; }#if defined(HAVE_LIBTRACEEVENT )
tools/perf/util/env.h +2 -0
@@ -187,6 +187,8 @@ int perf_env__read_cpu_topology_map(struct perf_env *env);void cpu_cache_level__free (struct cpu_cache_level *cache); +uint16_t perf_env__e_machine_nocache (struct perf_env *env,uint32_t *e_flags); +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env);const char *perf_env__arch_strerrno (struct perf_env *env,int err);const char *perf_env__cpuid (struct perf_env *env);
tools/perf/util/header.c +37 -10
68 lines not on this page · raw message
tools/perf/util/session.c +15 -11
@@ -3023,14 +3023,19 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* +* Is the env caching an e_machine? If not we want to compute from the +* more accurate threads. +*/ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);
@@ -3048,10 +3053,9 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags/* * Couldn't determine from the perf_env or current set of -* threads. Default to the host. +* threads. Potentially use logic that uses the arch string otherwise +* default to the host. Don't cache in the perf_env in case later +* threads indicate a better ELF machine type. */ -if (e_flags) - *e_flags =EF_HOST ; - -return EM_HOST ; +return perf_env__e_machine_nocache (env, e_flags); }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v8 01/17] perf env: Add perf_env__e_machine helper and use in perf_env__arch
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Uninitialized stack memory is leaked to the perf.data file and cached in the environment. - [High]…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Uninitialized stack memory is leaked to the perf.data file and cached in the environment.
- [High] Lossy canonicalization of the architecture string breaks 64-bit detection for old and new perf.data files.
- [Medium] If strdup() fails during process_arch(), env->arch is silently left as NULL.
--
commit 8073d9a9766717469f9a62174c92bb53c77a0df4
Author: Ian Rogers <[email protected]>
perf env: Add perf_env__e_machine helper and use in perf_env__arch
This commit introduces a helper to lazily compute the e_machine and update the perf_env's arch calculation. It updates the session to use the new helper and modifies how the perf data file header extracts the e_machine and e_flags.
Quoted 4 lines
tools/perf/util/env.c +0
-0
[ ... ]
Quoted 7 lines
tools/perf/util/env.c +7
-0+uint16_t perf_env__e_machine_nocache (struct perf_env *env,uint32_t *e_flags) +{ +uint16_t e_machine =EM_HOST ; + +if (env) + e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit); +
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 741
Quoted 10 lines
tools/perf/util/env.c +10
-0+if (e_flags && e_machine ==EM_HOST ) + *e_flags =EF_HOST ; + +return e_machine; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +uint16_t e_machine; +uint32_t local_e_flags;
Could local_e_flags be used uninitialized here?
If e_machine != EM_HOST in perf_env__e_machine_nocache(), *e_flags is not written to, which leaves local_e_flags uninitialized.
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 752
Quoted 11 lines
tools/perf/util/env.c +11
-0+ +if (env && env->e_machine !=EM_NONE ) { +if (e_flags) + *e_flags = env->e_flags; + +return env->e_machine; + } + e_machine =perf_env__e_machine_nocache (env, &local_e_flags); +if (env) { + env->e_machine = e_machine; + env->e_flags = local_e_flags;
If local_e_flags is uninitialized, does this leak stack memory into the environment?
Quoted 6 lines
tools/perf/util/env.c +5
-0+ } +if (e_flags) + *e_flags = local_e_flags; + +return e_machine; }
[ ... ]
Quoted 2 lines
const char *perf_env__arch(struct
perf_env*env)
{
[ ... ]
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 777
Quoted 7 lines
tools/perf/util/env.c +7
-0+/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ + e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); + arch =e_machine_to_perf_arch (e_machine); + env->arch =strdup (arch);
If strdup() fails due to memory exhaustion, could env->arch be left as NULL?
Quoted 12 lines
tools/perf/util/env.c +5
-0+/* +* Avoid potential crashes on the arch string if memory allocation in +* strdup fails and NULL were to be returned. +*/ +return env->arch ?: arch; }#if defined(HAVE_LIBTRACEEVENT )tools/perf/util/header.c +0
-0
[ ... ]
tools/perf/util/header.c static int write_osrelease(struct feat_fd *ff, · line 406
Quoted 6 lines
tools/perf/util/header.c +1
-2-static int write_e_machine (struct feat_fd *ff, -struct evlist *evlist__maybe_unused ) +static int write_e_machine (struct feat_fd *ff,struct evlist *evlist) {/* e_machine expanded from 16 to 32-bits for alignment. */ uint32_t e_flags;
Could e_flags be leaked to the perf.data file here?
If no threads supply a valid ELF machine and the fallback logic invokes perf_env__e_machine_nocache() returning e_machine != EM_HOST, e_flags may remain uninitialized stack garbage.
tools/perf/util/header.c
Quoted 10 lines
tools/perf/util/header.c +2
-2-uint32_t e_machine =EM_HOST ; +uint32_t e_machine =perf_session__e_machine (evlist->session, &e_flags);int ret; ret =do_write (ff, &e_machine,sizeof (e_machine));if (ret)return ret; -return do_write (ff, &e_machine,sizeof (e_machine)); +return do_write (ff, &e_flags,sizeof (e_flags));
Does this write the potentially uninitialized stack memory to the file header?
Quoted 1 line
}
[ ... ]
Quoted 6 lines
tools/perf/util/header.c +6
-0+static int process_arch (struct feat_fd *ff,void *data__maybe_unused ) +{ +uint16_t saved_e_machine = ff->ph->env.e_machine; + +free (ff->ph->env.arch); + ff->ph->env.arch =do_read_string (ff);
tools/perf/util/header.c static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \ · line 2712
Quoted 10 lines
tools/perf/util/header.c +10
-0+if (!ff->ph->env.arch) +return -ENOMEM ; +/* +* Make the arch string canonical by computing the e_machine from it, +* then turning the e_machine back into an arch string. +*/ + ff->ph->env.e_machine =EM_NONE ; +if (perf_env__e_machine (&ff->ph->env,/*e_flags=*/ NULL ) !=EM_NONE ) { +zfree (&ff->ph->env.arch); +perf_env__arch (&ff->ph->env);
If strdup() failed inside perf_env__arch(), could ff->ph->env.arch be left as NULL, while this function incorrectly returns 0 (success) leading to later NULL pointer dereferences?
Additionally, does this canonicalization strip architecture suffixes like "64"?
For legacy perf.data files lacking the HEADER_E_MACHINE feature, saved_e_machine would be restored to EM_NONE. If the string is canonicalized from "x86_64" to "x86" (based on e_machine_to_perf_arch()), subsequent calls to perf_env__init_kernel_mode() will check str_ends_with(env->arch, "64"). Because it's now "x86", the kernel_is_64_bit flag will be set to 0.
Does this break 64-bit detection and parsing for 64-bit recordings?
Quoted 4 lines
tools/perf/util/header.c +4
-0+ } + ff->ph->env.e_machine = saved_e_machine; +return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH v8 2/17 perf tests topology: Switch env->arch use to env->e_machine
In reply to Ian RogersSome arch string comparisons weren't normalized. Avoid potential issues with normalized names vs uname values by swtiching to using the e_machine.
--- tools/perf/tests/topology.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/tests/topology.c +6 -2
@@ -11,6 +11,7 @@#include "pmus.h" #include "target.h" #include <linux/err.h> +#include <elf.h> #define TEMPL "/tmp/perf-test-XXXXXX"#define DATA_SIZE 10
@@ -74,6 +75,7 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)struct aggr_cpu_id id;struct perf_cpu cpu;struct perf_env *env; +uint16_t e_machine; session =perf_session__new (&data,NULL );TEST_ASSERT_VAL ("can't get session" , !IS_ERR (session));
@@ -101,7 +103,9 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)* condition is true (see do_core_id_test in header.c). So always * run this test on those platforms. */ -if (!env->cpu &&strncmp (env->arch,"s390" ,4 ) &&strncmp (env->arch,"aarch64" ,7 )) + e_machine =perf_env__e_machine (env,NULL ); + +if (!env->cpu && e_machine !=EM_S390 && e_machine !=EM_AARCH64 )return TEST_SKIP ;/*
@@ -110,7 +114,7 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)* physical_package_id will be set to -1. Hence skip this * test if physical_package_id returns -1 for cpu from perf_cpu_map. */ -if (!strncmp (env->arch,"ppc64le" ,7 )) { +if (e_machine ==EM_PPC64 ) {if (cpu__get_socket_id (perf_cpu_map__cpu (map,0 )) ==-1 )return TEST_SKIP ; }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v8 3/17 perf capstone: Determine architecture from e_machine
In reply to Ian RogersAvoid the use of arch string that is imprecise and use the e_machine. Do more e_machine to capstone machine translations adding MIPS and RISCV. Remove unnecessary maybe_unused annotations.
--- tools/perf/util/capstone.c | 115 +++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 36 deletions(-)
tools/perf/util/capstone.c +79 -36
@@ -16,6 +16,7 @@#include <fcntl.h> #include <inttypes.h> #include <string.h> +#include <elf.h> #include <capstone/capstone.h>
@@ -137,37 +138,74 @@ static enum cs_err perf_cs_close(csh *handle)#endif } -static int capstone_init (struct machine *machine,csh *cs_handle,bool is64, -bool disassembler_style) +static bool e_machine_to_capstone (uint16_t e_machine,bool is64, +enum cs_arch *arch,enum cs_mode *mode) +{ +switch (e_machine) { +case EM_X86_64 : + *arch =CS_ARCH_X86 ; + *mode =CS_MODE_64 ; +return true; +case EM_386 : + *arch =CS_ARCH_X86 ; + *mode =CS_MODE_32 ; +return true; +case EM_AARCH64 : + *arch =CS_ARCH_ARM64 ; + *mode =CS_MODE_ARM ; +return true; +case EM_ARM : + *arch =CS_ARCH_ARM ; + *mode =CS_MODE_ARM |CS_MODE_V8 ; +return true; +case EM_S390 : + *arch =CS_ARCH_SYSZ ; + *mode =CS_MODE_BIG_ENDIAN ; +return true; +case EM_MIPS : + *arch =CS_ARCH_MIPS ; + *mode = is64 ?CS_MODE_MIPS64 :CS_MODE_MIPS32 ; + *mode |=CS_MODE_BIG_ENDIAN ; +return true; +case EM_PPC : + *arch =CS_ARCH_PPC ; + *mode =CS_MODE_BIG_ENDIAN |CS_MODE_32 ; +return true; +case EM_PPC64 : + *arch =CS_ARCH_PPC ; + *mode =CS_MODE_BIG_ENDIAN |CS_MODE_64 ; +return true; +case EM_SPARC : + *arch =CS_ARCH_SPARC ; + *mode =CS_MODE_BIG_ENDIAN |CS_MODE_32 ; +return true; +case EM_SPARCV9 : + *arch =CS_ARCH_SPARC ; + *mode =CS_MODE_BIG_ENDIAN |CS_MODE_V9 |CS_MODE_64 ; +return true; +case EM_RISCV : + *arch =CS_ARCH_RISCV ; + *mode = is64 ?CS_MODE_RISCV64 :CS_MODE_RISCV32 ; +return true; +default : +return false; + } +} + +static int capstone_init (uint16_t e_machine,csh *cs_handle,bool is64,bool disassembler_style) {enum cs_arch arch;enum cs_mode mode; -if (machine__is (machine,"x86_64" ) && is64) { - arch =CS_ARCH_X86 ; - mode =CS_MODE_64 ; - }else if (machine__normalized_is (machine,"x86" )) { - arch =CS_ARCH_X86 ; - mode =CS_MODE_32 ; - }else if (machine__normalized_is (machine,"arm64" )) { - arch =CS_ARCH_ARM64 ; - mode =CS_MODE_ARM ; - }else if (machine__normalized_is (machine,"arm" )) { - arch =CS_ARCH_ARM ; - mode =CS_MODE_ARM +CS_MODE_V8 ; - }else if (machine__normalized_is (machine,"s390" )) { - arch =CS_ARCH_SYSZ ; - mode =CS_MODE_BIG_ENDIAN ; - }else { +if (!e_machine_to_capstone (e_machine, is64, &arch, &mode))return -1 ; - }if (perf_cs_open (arch, mode, cs_handle) !=CS_ERR_OK ) {pr_warning_once ("cs_open failed\n" );return -1 ; } -if (machine__normalized_is (machine,"x86" )) { +if (arch ==CS_ARCH_X86 ) {/* * In case of using capstone_init while symbol__disassemble * setting CS_OPT_SYNTAX_ATT depends if disassembler_style opts
@@ -212,28 +250,31 @@ static size_t print_insn_x86(struct thread *thread, u8 cpumode, struct cs_insn * } -ssize_t capstone__fprintf_insn_asm (struct machine *machine__maybe_unused , -struct thread *thread__maybe_unused , -u8 cpumode__maybe_unused ,bool is64bit__maybe_unused , -const uint8_t *code__maybe_unused , -size_t code_size__maybe_unused , -uint64_t ip__maybe_unused ,int *lenp__maybe_unused , -int print_opts__maybe_unused ,FILE *fp__maybe_unused ) +ssize_t capstone__fprintf_insn_asm (struct machine *machine, +struct thread *thread, +u8 cpumode, +bool is64bit, +const uint8_t *code, +size_t code_size, +uint64_t ip,int *lenp, +int print_opts, +FILE *fp) {size_t printed;struct cs_insn *insn;csh cs_handle;size_t count; +uint16_t e_machine =thread__e_machine (thread, machine,/*e_flags=*/ NULL );int ret;/* TODO: Try to initiate capstone only once but need a proper place. */ - ret =capstone_init (machine, &cs_handle, is64bit, true); + ret =capstone_init (e_machine, &cs_handle, is64bit,/*disassembler_style=*/ true);if (ret <0 )return ret; count =perf_cs_disasm (cs_handle, code, code_size, ip,1 , &insn);if (count >0 ) { -if (machine__normalized_is (machine,"x86" )) +if (e_machine ==EM_X86_64 || e_machine ==EM_386 ) printed =print_insn_x86 (thread, cpumode, &insn[0 ], print_opts, fp);else printed =fprintf (fp,"%s %s" , insn[0 ].mnemonic, insn[0 ].op_str);
@@ -322,9 +363,9 @@ static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)return 0 ; } -int symbol__disassemble_capstone (const char *filename__maybe_unused , -struct symbol *sym__maybe_unused , -struct annotate_args *args__maybe_unused ) +int symbol__disassemble_capstone (const char *filename, +struct symbol *sym, +struct annotate_args *args) {struct annotation *notes =symbol__annotation (sym);struct map *map = args->ms->map;
@@ -344,6 +385,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,char disasm_buf[512 ];struct disasm_line *dl;bool disassembler_style = false; +uint16_t e_machine;if (args->options->objdump_path)return -1 ;
@@ -373,8 +415,8 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused, !strcmp(args->options->disassembler_style, "att")) disassembler_style = true; -if (capstone_init (maps__machine (thread__maps (args->ms->thread)), &handle, is_64bit, - disassembler_style) <0 ) + e_machine =thread__e_machine (args->ms->thread,/*machine=*/ NULL ,/*e_flags=*/ NULL ); +if (capstone_init (e_machine, &handle, is_64bit, disassembler_style) <0 ) goto err; needs_cs_close = true;
@@ -466,6 +508,7 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused,struct disasm_line *dl;u32 *line;bool disassembler_style = false; +uint16_t e_machine;if (args->options->objdump_path)return -1 ;
@@ -484,8 +527,8 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused, !strcmp(args->options->disassembler_style, "att")) disassembler_style = true; -if (capstone_init (maps__machine (thread__maps (args->ms->thread)), &handle, is_64bit, - disassembler_style) <0 ) + e_machine =thread__e_machine (args->ms->thread,/*machine=*/ NULL ,/*e_flags=*/ NULL ); +if (capstone_init (e_machine, &handle, is_64bit, disassembler_style) <0 ) goto err; needs_cs_close = true;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v8 03/17] perf capstone: Determine architecture from e_machine
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Hardcoded endianness in e_machine_to_capstone() breaks disassembly for bi-endian architectures.…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Hardcoded endianness in e_machine_to_capstone() breaks disassembly for bi-endian architectures.
- [High] thread__e_machine() fallback to EM_HOST breaks cross-architecture disassembly for kernel code and BPF programs. --
commit a3ff746537f2f9677ae9d6427555ff27588e61a4
Author: Ian Rogers <[email protected]>
perf capstone: Determine architecture from e_machine
This commit refactors capstone initialization to use the e_machine value from the ELF header instead of the architecture string, adding support for MIPS and RISCV translations.
Note: The format of this review has been altered to provide a free-form summary of the findings without directly quoting the patch code, due to recitation restrictions.
The new e_machine_to_capstone() function hardcodes CS_MODE_BIG_ENDIAN for EM_PPC, EM_PPC64, and EM_MIPS, and implicitly assumes little-endian for EM_AARCH64 and EM_ARM.
Does this approach break disassembly for bi-endian architectures?
Linux fully supports variants like ppc64le, mipsel, armeb, and aarch64_be under these same e_machine IDs. Because the initialization now succeeds with the hardcoded endianness rather than returning -1, it actively bypasses the functional fallback to objdump. Since objdump natively reads EI_DATA from the ELF header to determine the correct endianness, skipping it leaves these architectures with incorrect disassembly output.
Could we read EI_DATA alongside e_machine to set the endianness mode correctly?
Additionally, the updated code replaces machine__normalized_is() with thread__e_machine() to determine the target architecture.
Will this break cross-architecture disassembly for kernel code and BPF programs?
Previously, machine__normalized_is() correctly determined the architecture from machine->env->arch, which is populated by the perf.data file header. The new approach searches DSOs and falls back to dso__e_machine(), which is hardcoded to return EM_HOST (the compile-time host architecture of the perf binary) for kernel components like DSO_BINARY_TYPE__KALLSYMS, DSO_BINARY_TYPE__VMLINUX, and DSO_BINARY_TYPE__BPF_PROG_INFO.
If a user performs cross-architecture analysis, such as analyzing an ARM64 perf.data file on an x86_64 host, won't thread__e_machine() return EM_X86_64? This would cause Capstone to decode ARM64 kernel or JIT instructions as x86_64 instructions.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerPATCH v8 4/17 perf print_insn: Use e_machine for fallback IP length check
In reply to Ian RogersAvoid string comparisons with perf_env arch, switch to using the more precise ELF machine.
--- tools/perf/util/print_insn.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/util/print_insn.c +5 -3
@@ -9,6 +9,7 @@#include <stdbool.h> #include "capstone.h" #include "debug.h" +#include "env.h" #include "sample.h" #include "symbol.h" #include "machine.h"
@@ -17,6 +18,7 @@#include "dump-insn.h" #include "map.h" #include "dso.h" +#include <elf.h> size_t sample__fprintf_insn_raw (struct perf_sample *sample,FILE *fp) {
@@ -33,13 +35,13 @@ size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp)static bool is64bitip (struct machine *machine,struct addr_location *al) {const struct dso *dso = al->map ?map__dso (al->map) :NULL ; +uint16_t e_machine;if (dso)return dso__is_64_bit (dso); -return machine__is (machine,"x86_64" ) || -machine__normalized_is (machine,"arm64" ) || -machine__normalized_is (machine,"s390" ); + e_machine =perf_env__e_machine (machine->env,/*e_flags=*/ NULL ); +return e_machine ==EM_X86_64 || e_machine ==EM_AARCH64 || e_machine ==EM_S390 ; } ssize_t fprintf_insn_asm(struct machine *machine, struct thread *thread, u8 cpumode,
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v8 04/17] perf print_insn: Use e_machine for fallback IP length check
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Incorrect fallback to host architecture for guest machines breaks cross-architecture…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Incorrect fallback to host architecture for guest machines breaks cross-architecture disassembly.
- [Low] Missing #ifndef fallback for EM_AARCH64 will cause compilation failures on systems with older glibc. --
commit 3c6ce70c4a5c93a1c064a25ccf73aca73925cf03
Author: Ian Rogers <[email protected]>
perf print_insn: Use e_machine for fallback IP length check
This commit updates the instruction printer to use the ELF machine type instead of string comparisons on the perf_env architecture. This aims to provide a more precise fallback check for instruction pointer length.
tools/perf/util/print_insn.c line 21
Quoted 7 lines
tools/perf/util/print_insn.c +1
-0@@ -17,6 +18,7 @@#include "dump-insn.h" #include "map.h" #include "dso.h" +#include <elf.h>
This isn't a bug, but could compiling on older systems fail here? Other files in the perf codebase, such as tools/perf/util/symbol-elf.c, provide an explicit fallback definition for EM_AARCH64 since older system headers might not include it.
[ ... ]
tools/perf/util/print_insn.c size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp) · line 43
Quoted 13 lines
tools/perf/util/print_insn.c +2
-3@@ -33,13 +35,13 @@ size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp)static bool is64bitip (struct machine *machine,struct addr_location *al) {const struct dso *dso = al->map ?map__dso (al->map) :NULL ; +uint16_t e_machine;if (dso)return dso__is_64_bit (dso); -return machine__is (machine,"x86_64" ) || -machine__normalized_is (machine,"arm64" ) || -machine__normalized_is (machine,"s390" ); + e_machine =perf_env__e_machine (machine->env,/*e_flags=*/ NULL );
When processing traces from guest machines, machine->env is initialized as NULL. If env is NULL, perf_env__e_machine_nocache() defaults to returning EM_HOST, the compile-time ELF architecture of the perf tool.
If a 64-bit host analyzes a trace from a 32-bit guest, would e_machine evaluate to EM_X86_64 (or similar) instead of the guest's actual architecture?
The previous string comparison logic safely returned false when env was NULL.
tools/perf/util/print_insn.c size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp) · line 44
Quoted 1 line
tools/perf/util/print_insn.c +1
-0+return e_machine ==EM_X86_64 || e_machine ==EM_AARCH64 || e_machine ==EM_S390 ;
Will this regression cause Capstone to disassemble 32-bit guest instructions using the 64-bit decoder and yield garbage output for those guests?
Quoted 1 line
tools/perf/util/print_insn.c +0
-0}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Ian Rogers
reviewerPATCH v8 5/17 perf machine: Use perf_env e_machine rather than arch
In reply to Ian RogersThe arch string is derived from uname and may be normalized causing potential differences meaning the ELF machine can be more precise. Reduce the scope of machine__is as often it is better to use a thread for the e_machine rather than the machine. Switch from string to ELF machine constant comparisons.
--- tools/perf/util/machine.c | 25 ++++++++----------------- tools/perf/util/machine.h | 2 -- 2 files changed, 8 insertions(+), 19 deletions(-)
tools/perf/util/machine.c +8 -17
@@ -1611,10 +1611,15 @@ static bool machine__uses_kcore(struct machine *machine)return dsos__for_each_dso (&machine->dsos, machine__uses_kcore_cb,NULL ) !=0 ? true : false; } +static bool machine__is (struct machine *machine,uint16_t e_machine) +{ +return machine &&perf_env__e_machine (machine->env,NULL ) == e_machine; +} +static bool perf_event__is_extra_kernel_mmap (struct machine *machine,struct extra_kernel_map *xm) { -return machine__is (machine,"x86_64" ) && +return machine__is (machine,EM_X86_64 ) &&is_entry_trampoline (xm->name); }
@@ -2770,7 +2775,7 @@ static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,static u64 get_leaf_frame_caller (struct perf_sample *sample,struct thread *thread,int usr_idx) { -if (machine__normalized_is (maps__machine (thread__maps (thread)),"arm64" )) +if (thread__e_machine (thread,/*machine=*/ NULL ,/*e_flags=*/ NULL ) ==EM_AARCH64 )return get_leaf_frame_caller_aarch64 (sample, thread, usr_idx);else return 0 ;
@@ -3141,20 +3146,6 @@ int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,return 0 ; } -/* -* Compares the raw arch string. N.B. see instead perf_env__arch() or -* machine__normalized_is() if a normalized arch is needed. -*/ -bool machine__is (struct machine *machine,const char *arch) -{ -return machine && !strcmp (perf_env__raw_arch (machine->env), arch); -} - -bool machine__normalized_is (struct machine *machine,const char *arch) -{ -return machine && !strcmp (perf_env__arch (machine->env), arch); -} -int machine__nr_cpus_avail (struct machine *machine) {return machine ?perf_env__nr_cpus_avail (machine->env) :0 ;
@@ -3181,7 +3172,7 @@ int machine__get_kernel_start(struct machine *machine)* start of kernel text, but still above 2^63. So leave * kernel_start = 1ULL << 63 for x86_64. */ -if (!err && !machine__is (machine,"x86_64" )) +if (!err && !machine__is (machine,EM_X86_64 )) machine->kernel_start =map__start (map); }return err;
tools/perf/util/machine.h +0 -2
@@ -227,8 +227,6 @@ static inline bool machine__is_host(struct machine *machine) }bool machine__is_lock_function (struct machine *machine,u64 addr); -bool machine__is (struct machine *machine,const char *arch); -bool machine__normalized_is (struct machine *machine,const char *arch);int machine__nr_cpus_avail (struct machine *machine);struct thread *machine__findnew_thread (struct machine *machine,pid_t pid,pid_t tid);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v8 05/17] perf machine: Use perf_env e_machine rather than arch
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The commit changes the visibility and signature of `machine__is()` (making it static and changing…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The commit changes the visibility and signature of machine__is() (making it static and changing its second parameter from const char * to uint16_t e_machine), but fails to update callers in tools/perf/util/symbol.c (which still pass a string like "x86_64"), causing a build breakage. --
commit 43e3a53fa61bf39d387266ed813f417208baa8e5
Author: Ian Rogers <[email protected]>
perf machine: Use perf_env e_machine rather than arch
This commit modifies machine__is to use ELF machine constant comparisons instead of string comparisons and reduces its scope by making it static. It updates callers within machine.c.
Quoted 3 lines
tools/perf/util/machine.c +0
-0
[ ... ]
tools/perf/util/machine.c static bool machine__uses_kcore(struct machine *machine) · line 1618
Quoted 9 lines
tools/perf/util/machine.c +5
-0@@ -1611,10 +1611,15 @@ static bool machine__uses_kcore(struct machine *machine)return dsos__for_each_dso (&machine->dsos, machine__uses_kcore_cb,NULL ) !=0 ? true : false; } +static bool machine__is (struct machine *machine,uint16_t e_machine) +{ +return machine &&perf_env__e_machine (machine->env,NULL ) == e_machine; +} +
Does changing machine__is() to be static and altering its signature break the build?
It appears callers in tools/perf/util/symbol.c still pass a string argument like "x86_64" to this function.
I notice this is addressed later in the series by commit ae1a01cb2104 (perf symbol: Avoid use of machine__is), but leaving this intermediate state might cause bisection failures.
[ ... ]
Quoted 10 lines
tools/perf/util/machine.h +0
-2@@ -227,8 +227,6 @@ static inline bool machine__is_host(struct machine *machine) }bool machine__is_lock_function (struct machine *machine,u64 addr); -bool machine__is (struct machine *machine,const char *arch); -bool machine__normalized_is (struct machine *machine,const char *arch);int machine__nr_cpus_avail (struct machine *machine);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Ian Rogers
reviewerPATCH v8 6/17 perf sample-raw: Use perf_env e_machine rather than arch
In reply to Ian RogersUse the e_machine rather than the arch to determine S390 and x86 types.
--- tools/perf/util/sample-raw.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
tools/perf/util/sample-raw.c +9 -9
@@ -1,6 +1,5 @@/* SPDX-License-Identifier: GPL-2.0 */ - -#include <string.h> +#include <elf.h> #include <linux/string.h> #include "evlist.h" #include "env.h"
@@ -14,14 +13,15 @@*/ void evlist__init_trace_event_sample_raw (struct evlist *evlist,struct perf_env *env) { -const char *arch_pf =perf_env__arch (env); -const char *cpuid =perf_env__cpuid (env); +uint16_t e_machine =perf_env__e_machine (env,/*e_eflags=*/ NULL ); -if (arch_pf && !strcmp ("s390" , arch_pf)) +if (e_machine ==EM_S390 ) { evlist->trace_event_sample_raw = evlist__s390_sample_raw; -else if (arch_pf && !strcmp ("x86" , arch_pf) && - cpuid &&strstarts (cpuid,"AuthenticAMD" ) && -evlist__has_amd_ibs (evlist)) { - evlist->trace_event_sample_raw = evlist__amd_sample_raw; + }else if (e_machine ==EM_X86_64 || e_machine ==EM_386 ) { +const char *cpuid =perf_env__cpuid (env); + +if (cpuid &&strstarts (cpuid,"AuthenticAMD" ) && +evlist__has_amd_ibs (evlist)) + evlist->trace_event_sample_raw = evlist__amd_sample_raw; } }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v8 7/17 perf sort: Use perf_env e_machine rather than arch
In reply to Ian RogersUse the e_machine rather than the arch to determine x86 or PPC types.
--- tools/perf/util/sort.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
tools/perf/util/sort.c +7 -5
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <ctype.h> +#include <elf.h> #include <errno.h> #include <inttypes.h> #include <regex.h>
@@ -2673,9 +2674,10 @@ struct sort_dimension {static int arch_support_sort_key (const char *sort_key,struct perf_env *env) { -const char *arch =perf_env__arch (env); +uint16_t e_machine =perf_env__e_machine (env,/*e_eflags=*/ NULL ); -if (!strcmp ("x86" , arch) || !strcmp ("powerpc" , arch)) { +if (e_machine ==EM_X86_64 || e_machine ==EM_386 || + e_machine ==EM_PPC64 || e_machine ==EM_PPC ) {if (!strcmp (sort_key,"p_stage_cyc" ))return 1 ;if (!strcmp (sort_key,"local_p_stage_cyc" ))
@@ -2686,14 +2688,14 @@ static int arch_support_sort_key(const char *sort_key, struct perf_env *env)static const char *arch_perf_header_entry (const char *se_header,struct perf_env *env) { -const char *arch =perf_env__arch (env); +uint16_t e_machine =perf_env__e_machine (env,/*e_eflags=*/ NULL ); -if (!strcmp ("x86" , arch)) { +if (e_machine ==EM_X86_64 || e_machine ==EM_386 ) {if (!strcmp (se_header,"Local Pipeline Stage Cycle" ))return "Local Retire Latency" ;else if (!strcmp (se_header,"Pipeline Stage Cycle" ))return "Retire Latency" ; - }else if (!strcmp ("powerpc" , arch)) { + }else if (e_machine ==EM_PPC64 || e_machine ==EM_PPC ) {if (!strcmp (se_header,"Local INSTR Latency" ))return "Finish Cyc" ;else if (!strcmp (se_header,"INSTR Latency" ))
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v8 8/17 perf symbol: Avoid use of machine__is
In reply to Ian RogersSwitch to using the ELF machine from the dso or running machine rather than the machine perf_env arch that may fall back on EM_HOST. This also avoids potentially imprecise string comparisons.
--- tools/perf/util/symbol.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-)
tools/perf/util/symbol.c +23 -6
@@ -851,6 +851,24 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)return count; } +static uint16_t machine_or_dso_e_machine (struct machine *machine,struct dso *dso) +{ +uint16_t e_machine =EM_NONE ; + +/* Check for a cached value first. */ +if (machine && machine->env && machine->env->e_machine !=EM_NONE ) +return machine->env->e_machine; + +/* DSO should be most accurate */ +if (dso) + e_machine =dso__e_machine (dso, machine,/*e_flags=*/ NULL ); + +if (e_machine !=EM_NONE ) +return e_machine; + +return perf_env__e_machine (machine ? machine->env :NULL ,/*e_flags=*/ NULL ); +} +/* * Split the symbols into maps, making sure there are no overlaps, i.e. the * kernel range is broken in several maps, named [kernel].N, as we don't have
@@ -866,14 +884,13 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,struct rb_root_cached *root =dso__symbols (dso);struct rb_node *next =rb_first_cached (root);int kernel_range =0 ; -bool x86_64; +uint16_t e_machine =EM_NONE ;if (!kmaps)return -1 ; machine =maps__machine (kmaps); - - x86_64 =machine__is (machine,"x86_64" ); + e_machine =machine_or_dso_e_machine (machine, dso);while (next) {char *module;
@@ -925,7 +942,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,*/ pos->start =map__map_ip (curr_map, pos->start); pos->end =map__map_ip (curr_map, pos->end); - }else if (x86_64 &&is_entry_trampoline (pos->name)) { + }else if (e_machine ==EM_X86_64 &&is_entry_trampoline (pos->name)) {/* * These symbols are not needed anymore since the * trampoline maps refer to the text section and it's
@@ -1428,7 +1445,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map,free (new_node); } -if (machine__is (machine,"x86_64" )) { +if (machine_or_dso_e_machine (machine, dso) ==EM_X86_64 ) {u64 addr;/*
@@ -1716,7 +1733,7 @@ int dso__load(struct dso *dso, struct map *map) ret =dso__load_guest_kernel_sym (dso, map); machine =maps__machine (map__kmaps (map)); -if (machine__is (machine,"x86_64" )) +if (machine_or_dso_e_machine (machine, dso) ==EM_X86_64 )machine__map_x86_64_entry_trampolines (machine, dso); goto out; }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v8 08/17] perf symbol: Avoid use of machine__is
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] machine_or_dso_e_machine() incorrectly prioritizes the globally cached environment architecture…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] machine_or_dso_e_machine() incorrectly prioritizes the globally cached environment architecture over the DSO, and erroneously returns the host architecture (EM_HOST) for kernel traces. --
commit ae1a01cb2104e12d0b084739b319fb08570b49d8
Author: Ian Rogers <[email protected]>
perf symbol: Avoid use of machine__is
This commit switches from using machine__is to checking the ELF machine type from either the DSO or the running machine's environment. This aims to use a more accurate architecture representation and avoid imprecise string comparisons.
Quoted 4 lines
tools/perf/util/symbol.c +0
-0
[ ... ]
tools/perf/util/symbol.c static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso) · line 862
Quoted 13 lines
tools/perf/util/symbol.c +9
-0@@ -851,6 +851,24 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)return count; } +static uint16_t machine_or_dso_e_machine (struct machine *machine,struct dso *dso) +{ +uint16_t e_machine =EM_NONE ; + +/* Check for a cached value first. */ +if (machine && machine->env && machine->env->e_machine !=EM_NONE ) +return machine->env->e_machine; + +/* DSO should be most accurate */
Does checking the globally cached environment architecture before the DSO contradict the comment that the DSO should be most accurate?
If the environment architecture is already cached, won't this bypass the DSO entirely and potentially break analysis of mixed-architecture user binaries?
tools/perf/util/symbol.c static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso) · line 867
Quoted 5 lines
tools/perf/util/symbol.c +5
-0+if (dso) + e_machine =dso__e_machine (dso, machine,/*e_flags=*/ NULL ); + +if (e_machine !=EM_NONE ) +return e_machine;
When dso__e_machine() is called for kernel DSOs, does it return the host architecture (EM_HOST) of the machine running the perf tool?
If so, would returning this host architecture skip the fallback to perf_env__e_machine(), causing cross-architecture analysis (like reading an x86_64 trace on an ARM64 host) to fail checks like e_machine == EM_X86_64?
Quoted 3 lines
tools/perf/util/symbol.c +3
-0+ +return perf_env__e_machine (machine ? machine->env :NULL ,/*e_flags=*/ NULL ); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Ian Rogers
reviewerPATCH v8 9/17 perf arch common: Use perf_env e_machine rather than arch
In reply to Ian RogersUse the e_machine rather than arch string matching.
--- tools/perf/arch/common.c | 55 +++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 20 deletions(-)
tools/perf/arch/common.c +35 -20
@@ -1,12 +1,14 @@// SPDX-License-Identifier: GPL-2.0 +#include "common.h" +#include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> -#include "common.h" #include "../util/env.h" #include "../util/debug.h" +#include <dwarf-regs.h> #include <linux/zalloc.h> static const char *const arc_triplets[] = {
@@ -145,7 +147,8 @@ static int perf_env__lookup_binutils_path(struct perf_env *env, const char *name, char **path) { int idx; -const char *arch =perf_env__arch (env), *cross_env; +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); +const char *cross_env;const char *const *path_list;char *buf =NULL ;
@@ -153,7 +156,7 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,* We don't need to try to find objdump path for native system. * Just use default binutils path (e.g.: "objdump"). */ -if (!strcmp (perf_env__arch (NULL ), arch)) +if (e_machine ==EM_HOST ) goto out; cross_env =getenv ("CROSS_COMPILE" );
@@ -170,30 +173,42 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,zfree (&buf); } -if (!strcmp (arch,"arc" )) +switch (e_machine) { +case EM_ARC : path_list = arc_triplets; -else if (!strcmp (arch,"arm" )) +break ; +case EM_ARM : path_list = arm_triplets; -else if (!strcmp (arch,"arm64" )) +break ; +case EM_AARCH64 : path_list = arm64_triplets; -else if (!strcmp (arch,"powerpc" )) +break ; +case EM_PPC : +case EM_PPC64 : path_list = powerpc_triplets; -else if (!strcmp (arch,"riscv32" )) - path_list = riscv32_triplets; -else if (!strcmp (arch,"riscv64" )) - path_list = riscv64_triplets; -else if (!strcmp (arch,"sh" )) +break ; +case EM_RISCV : + path_list =perf_env__kernel_is_64_bit (env) ? riscv64_triplets : riscv32_triplets; +break ; +case EM_SH : path_list = sh_triplets; -else if (!strcmp (arch,"s390" )) +break ; +case EM_S390 : path_list = s390_triplets; -else if (!strcmp (arch,"sparc" )) +break ; +case EM_SPARC : +case EM_SPARCV9 : path_list = sparc_triplets; -else if (!strcmp (arch,"x86" )) +break ; +case EM_X86_64 : +case EM_386 : path_list = x86_triplets; -else if (!strcmp (arch,"mips" )) +break ; +case EM_MIPS : path_list = mips_triplets; -else { -ui__error ("binutils for %s not supported.\n" , arch); +break ; +default : +ui__error ("binutils for %s not supported.\n" ,perf_env__arch (env)); goto out_error; }
@@ -202,7 +217,7 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,ui__error ("Please install %s for %s.\n" "You can add it to PATH, set CROSS_COMPILE or " "override the default using --%s.\n" , - name, arch, name); + name,perf_env__arch (env), name); goto out_error; }
@@ -237,5 +252,5 @@ int perf_env__lookup_objdump(struct perf_env *env, char **path)*/ bool perf_env__single_address_space (struct perf_env *env) { -return strcmp (perf_env__arch (env),"sparc" ); +return perf_env__e_machine (env,/*e_flags=*/ NULL ) ==EM_SPARC ; }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v8 09/17] perf arch common: Use perf_env e_machine rather than arch
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Logic inversion in `perf_env__single_address_space` breaks kernel/user address space resolution.…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Logic inversion in perf_env__single_address_space breaks kernel/user address space resolution.
- [Medium] Exact EM_HOST check breaks native binutils fallback for 32-bit profiling data on 64-bit hosts. --
commit e2d5820d159463d4dd20c88669d8e471fefbb4c5
Author: Ian Rogers <[email protected]>
perf arch common: Use perf_env e_machine rather than arch
This patch replaces architecture string matching with ELF machine constants in the perf architecture common code. It updates the binutils path lookup and single address space checks to use the e_machine values from the perf environment.
Quoted 4 lines
tools/perf/arch/common.c +0
-0
[ ... ]
Quoted 3 lines
tools/perf/arch/common.c +0
-0@@ -145,7 +147,8 @@ static int perf_env__lookup_binutils_path(struct perf_env *env, const char *name, char **path) {
[ ... ]
tools/perf/arch/common.c static int perf_env__lookup_binutils_path(struct perf_env *env, · line 160
Quoted 7 lines
tools/perf/arch/common.c +1
-1/* * We don't need to try to find objdump path for native system. * Just use default binutils path (e.g.: "objdump"). */ -if (!strcmp (perf_env__arch (NULL ), arch)) +if (e_machine ==EM_HOST ) goto out;
The previous check used perf_env__arch() which normalized both 32-bit and 64-bit variants to a single string like "x86". With the exact EM_HOST comparison, if we are analyzing a 32-bit trace (EM_386) on a 64-bit host (EM_X86_64), will this evaluate to false and skip the native fallback?
Could this regression cause the tool to wrongly search for a cross-compiler prefix instead of successfully using the native multi-arch objdump?
[ ... ]
tools/perf/arch/common.c int perf_env__lookup_objdump(struct perf_env *env, char **path) · line 256
Quoted 7 lines
tools/perf/arch/common.c +1
-1@@ -237,5 +252,5 @@ int perf_env__lookup_objdump(struct perf_env *env, char **path)*/ bool perf_env__single_address_space (struct perf_env *env) { -return strcmp (perf_env__arch (env),"sparc" ); +return perf_env__e_machine (env,/*e_flags=*/ NULL ) ==EM_SPARC ; }
Does this introduce a logic inversion regression?
The original implementation using strcmp() evaluated to non-zero (true) for non-SPARC architectures and zero (false) for SPARC. The new implementation returns true only for SPARC and false for everything else.
Also, should this check include EM_SPARCV9 to properly handle 64-bit SPARC traces, since the previous strcmp() against "sparc" would have matched the normalized string for both?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Ian Rogers
reviewerPATCH v8 10/17 perf header: In print_pmu_caps use perf_env e_machine
In reply to Ian RogersSwitch from arch to e_machine in print_pmu_caps.
--- tools/perf/util/header.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
tools/perf/util/header.c +4 -3
@@ -2348,15 +2348,16 @@ static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp)static void print_pmu_caps (struct feat_fd *ff,FILE *fp) {struct perf_env *env = &ff->ph->env; -struct pmu_caps *pmu_caps; +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL );for (int i =0 ; i < env->nr_pmus_with_caps; i++) { - pmu_caps = &env->pmu_caps[i]; +struct pmu_caps *pmu_caps = &env->pmu_caps[i]; +__print_pmu_caps (fp, pmu_caps->nr_caps, pmu_caps->caps, pmu_caps->pmu_name); } -if (strcmp (perf_env__arch (env),"x86" ) ==0 && +if ((e_machine ==EM_X86_64 || e_machine ==EM_386 ) &&perf_env__has_pmu_mapping (env,"ibs_op" )) {char *max_precise =perf_env__find_pmu_cap (env,"cpu" ,"max_precise" );
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v8 11/17 perf c2c: Use perf_env e_machine rather than arch
In reply to Ian RogersUse the e_machine rather than arch string matching for AARCH64.
--- tools/perf/builtin-c2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-c2c.c +1 -1
@@ -3202,7 +3202,7 @@ static int perf_c2c__report(int argc, const char **argv)* default display type. */ if (!display) { -if (!strcmp (perf_env__arch (env),"arm64" )) +if (perf_env__e_machine (env,/*e_flags=*/ NULL ) ==EM_AARCH64 ) display ="peer" ;else display ="tot" ;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v8 11/17] perf c2c: Use perf_env e_machine rather than arch
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Build failure on older toolchains due to missing fallback definition for `EM_AARCH64`. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Build failure on older toolchains due to missing fallback definition for EM_AARCH64. --
commit 5745fb586646dd63f1bc0598424b6a0b707af964
Author: Ian Rogers <[email protected]>
perf c2c: Use perf_env e_machine rather than arch
This commit updates perf c2c to use the e_machine value from the perf_env environment instead of performing string matching on the architecture name for AARCH64 detection.
tools/perf/builtin-c2c.c static int perf_c2c__report(int argc, const char **argv) · line 3205
Quoted 10 lines
tools/perf/builtin-c2c.c +1
-1@@ -3202,7 +3202,7 @@ static int perf_c2c__report(int argc, const char **argv)* default display type. */ if (!display) { -if (!strcmp (perf_env__arch (env),"arm64" )) +if (perf_env__e_machine (env,/*e_flags=*/ NULL ) ==EM_AARCH64 )
Will this cause a build failure on older toolchains where the system <elf.h> does not yet define EM_AARCH64?
On older systems (for example, those with glibc earlier than 2.17), EM_AARCH64 might not be declared. Other files in the perf codebase provide a manual fallback definition for it, but builtin-c2c.c does not appear to include one.
Quoted 3 lines
tools/perf/builtin-c2c.c +0
-0display ="peer" ; else display ="tot" ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Ian Rogers
reviewerPATCH v8 12/17 perf lock-contention: Use perf_env e_machine rather than arch
In reply to Ian RogersUse the e_machine rather than arch string matching for powerpc.
--- tools/perf/util/lock-contention.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/lock-contention.c +3 -3
@@ -104,7 +104,8 @@ bool match_callstack_filter(struct machine *machine, u64 *callstack, int max_stastruct map *kmap;struct symbol *sym;u64 ip; -const char *arch =perf_env__arch (machine->env); +uint16_t e_machine =perf_env__e_machine (machine->env,/*e_flags=*/ NULL ); +bool is_powerpc = e_machine ==EM_PPC64 || e_machine ==EM_PPC ;if (list_empty (&callstack_filters))return true;
@@ -125,8 +126,7 @@ bool match_callstack_filter(struct machine *machine, u64 *callstack, int max_sta* incase first or second callstack index entry has 0 * address for powerpc. */ -if (!callstack || (!callstack[i] && (strcmp (arch,"powerpc" ) || - (i !=1 && i !=2 )))) +if (!callstack || (!callstack[i] && (!is_powerpc || (i !=1 && i !=2 ))))break ; ip = callstack[i];
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v8 13/17 perf env: Refactor perf_env__arch_strerrno
In reply to Ian Rogersperf_env__arch_strerrno is only available with libtraceevent so hide the declaration if no libtraceevent.
The previous approach maps an architecture string to a function pointer to a function that takes an int errno values and returns a string. The new approach takes an e_machine and an errno value and returns a string.
As the only call site is in builtin-trace.c, the e_machine is already present and potentially more specific than the perf_env arch string that is a single global value.
The major complication in this approach is having the shell script that generates the C code map a linux directory name to the matching ELF machine constants.
--- tools/perf/builtin-trace.c | 5 ++- tools/perf/trace/beauty/arch_errno_names.sh | 40 ++++++++++++++++++--- tools/perf/util/env.c | 13 +++---- tools/perf/util/env.h | 7 ++-- 4 files changed, 44 insertions(+), 21 deletions(-)
tools/perf/builtin-trace.c +2 -3
@@ -3008,9 +3008,8 @@ static int trace__sys_exit(struct trace *trace, struct evsel *evsel, }else if (ret <0 ) { errno_print: {char bf[STRERR_BUFSIZE ]; -struct perf_env *env =evsel__env (evsel) ?: &trace->host_env;const char *emsg =str_error_r (-ret, bf,sizeof (bf)); -const char *e =perf_env__arch_strerrno (env, err); +const char *e =perf_env__arch_strerrno (e_machine, err);fprintf (trace->output,"-1 %s (%s)" , e, emsg); }
@@ -4890,7 +4889,7 @@ static size_t syscall__dump_stats(struct trace *trace, int e_machine, FILE *fp,for (e =0 ; e < stats->max_errno; ++e) {if (stats->errnos[e] !=0 ) -fprintf (fp,"\t\t\t\t%s: %d\n" ,perf_env__arch_strerrno (trace->host->env, e +1 ), stats->errnos[e]); +fprintf (fp,"\t\t\t\t%s: %d\n" ,perf_env__arch_strerrno (e_machine, e +1 ), stats->errnos[e]); } } lines++;
tools/perf/trace/beauty/arch_errno_names.sh +35 -5
@@ -52,21 +52,49 @@ process_arch() |IFS=, create_errno_lookup_func "$arch" } +arch_to_e_machine() +{ + case "$1" in + alpha) printf '\tcase EM_ALPHA:\n' ;; + arc) printf '\tcase EM_ARC:\n' ;; + arm) printf '\tcase EM_ARM:\n' ;; + arm64) printf '\tcase EM_AARCH64:\n' ;; + csky) printf '\tcase EM_CSKY:\n' ;; + hexagon) printf '\tcase EM_HEXAGON:\n' ;; + loongarch) printf '\tcase EM_LOONGARCH:\n' ;; + microblaze) printf '\tcase EM_MICROBLAZE:\n' ;; + mips) printf '\tcase EM_MIPS:\n' ;; + parisc) printf '\tcase EM_PARISC:\n' ;; + powerpc) printf '\tcase EM_PPC:\n\tcase EM_PPC64:\n' ;; + riscv) printf '\tcase EM_RISCV:\n' ;; + s390) printf '\tcase EM_S390:\n' ;; + sh) printf '\tcase EM_SH:\n' ;; + sparc) printf '\tcase EM_SPARC:\n\tcase EM_SPARCV9:\n' ;; + x86) printf '\tcase EM_386:\n\tcase EM_X86_64:\n' ;; + xtensa) printf '\tcase EM_XTENSA:\n' ;; + esac +} + create_arch_errno_table_func() { archlist="$1" default="$2" - printf 'static arch_syscalls__strerrno_t *\n' - printf 'arch_syscalls__strerrno_function(const char *arch)\n' + printf 'static const char *\n' + printf 'arch_syscalls__strerrno(uint16_t e_machine, int err)\n' printf '{\n' + printf '\tswitch (e_machine) {\n' for arch in $archlist; do arch_str=$(arch_string "$arch") - printf '\tif (!strcmp(arch, "%s"))\n' "$arch_str" - printf '\t\treturn errno_to_name__%s;\n' "$arch_str" + ems=$(arch_to_e_machine "$arch_str") + if [ -n "$ems" ]; then + printf '%s\n' "$ems" + printf '\t\treturn errno_to_name__%s(err);\n' "$arch_str" + fi done arch_str=$(arch_string "$default") - printf '\treturn errno_to_name__%s;\n' "$arch_str" + printf '\tdefault:\n\t\treturn errno_to_name__%s(err);\n' "$arch_str" + printf '\t}\n' printf '}\n' }
@@ -74,6 +102,8 @@ cat <<EoHEADER /* SPDX-License-Identifier: GPL-2.0 */ #include <string.h> +#include <stdint.h> +#include <elf.h> EoHEADER
tools/perf/util/env.c +4 -9
@@ -786,17 +786,12 @@ const char *perf_env__arch(struct perf_env *env)#include "trace/beauty/arch_errno_names.c" #endif -const char *perf_env__arch_strerrno (struct perf_env *env__maybe_unused ,int err__maybe_unused ) -{#if defined(HAVE_LIBTRACEEVENT ) -if (env->arch_strerrno ==NULL ) - env->arch_strerrno =arch_syscalls__strerrno_function (perf_env__arch (env)); - -return env->arch_strerrno ? env->arch_strerrno (err) :"no arch specific strerrno function" ; -#else -return "!HAVE_LIBTRACEEVENT" ; -#endif +const char *perf_env__arch_strerrno (uint16_t e_machine,int err) +{ +return arch_syscalls__strerrno (e_machine, err); } +#endif const char *perf_env__cpuid (struct perf_env *env) {
tools/perf/util/env.h +3 -4
@@ -67,8 +67,6 @@ struct cpu_domain_map {struct domain_info **domains; }; -typedef const char *(arch_syscalls__strerrno_t )(int err); -struct perf_env {char *hostname;char *os_release;
@@ -158,7 +156,6 @@ struct perf_env {*/ bool enabled; } clock; -arch_syscalls__strerrno_t *arch_strerrno; };enum perf_compress_type {
@@ -190,7 +187,9 @@ void cpu_cache_level__free(struct cpu_cache_level *cache);uint16_t perf_env__e_machine_nocache (struct perf_env *env,uint32_t *e_flags);uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env); -const char *perf_env__arch_strerrno (struct perf_env *env,int err); +#if defined(HAVE_LIBTRACEEVENT ) +const char *perf_env__arch_strerrno (uint16_t e_machine,int err); +#endif const char *perf_env__cpuid (struct perf_env *env);const char *perf_env__raw_arch (struct perf_env *env);int perf_env__nr_cpus_avail (struct perf_env *env);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v8 14/17 perf env: Remove unused perf_env__raw_arch
In reply to Ian RogersThe switch to using e_machine has made the perf_env__raw_arch function unused so remove it.
--- tools/perf/util/env.c | 18 ------------------ tools/perf/util/env.h | 1 - 2 files changed, 19 deletions(-)
tools/perf/util/env.c +0 -18
@@ -451,19 +451,6 @@ int perf_env__read_cpuid(struct perf_env *env)return 0 ; } -static int perf_env__read_arch (struct perf_env *env) -{ -struct utsname uts; - -if (env->arch) -return 0 ; - -if (!uname (&uts)) - env->arch =strdup (uts.machine); - -return env->arch ?0 : -ENOMEM ; -} -static int perf_env__read_nr_cpus_avail (struct perf_env *env) {if (env->nr_cpus_avail ==0 )
@@ -582,11 +569,6 @@ int perf_env__read_core_pmu_caps(struct perf_env *env)return ret; } -const char *perf_env__raw_arch (struct perf_env *env) -{ -return env && !perf_env__read_arch (env) ? env->arch :"unknown" ; -} -int perf_env__nr_cpus_avail (struct perf_env *env) {return env && !perf_env__read_nr_cpus_avail (env) ? env->nr_cpus_avail :0 ;
tools/perf/util/env.h +0 -1
@@ -191,7 +191,6 @@ const char *perf_env__arch(struct perf_env *env);const char *perf_env__arch_strerrno (uint16_t e_machine,int err);#endif const char *perf_env__cpuid (struct perf_env *env); -const char *perf_env__raw_arch (struct perf_env *env);int perf_env__nr_cpus_avail (struct perf_env *env);void perf_env__init (struct perf_env *env);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v8 15/17 perf env: Add helper to lazily compute the os_release
In reply to Ian RogersIn live mode the os_release isn't being initialized, make a lazy initialization helper that assumes when the os_release isn't initialized this is live mode.
--- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/env.c | 21 +++++++++++++++++++++ tools/perf/util/env.h | 1 + tools/perf/util/header.c | 16 +++++++++++----- tools/perf/util/symbol.c | 4 ++-- 5 files changed, 36 insertions(+), 8 deletions(-)
tools/perf/util/data-convert-bt.c +1 -1
@@ -1414,7 +1414,7 @@ do { \ADD ("host" , env->hostname);ADD ("sysname" ,"Linux" ); -ADD ("release" , env->os_release); +ADD ("release" ,perf_env__os_release (env));ADD ("version" , env->version);ADD ("machine" , env->arch);ADD ("domain" ,"kernel" );
tools/perf/util/env.c +21 -0
@@ -339,6 +339,27 @@ int perf_env__kernel_is_64_bit(struct perf_env *env)return env->kernel_is_64_bit; } +const char *perf_env__os_release (struct perf_env *env) +{ +struct utsname uts; +int ret; + +if (!env) +return perf_version_string; + +if (env->os_release) +return env->os_release; + +/* +* The os_release is being accessed but wasn't initialized from a data +* file, assume this is 'live' mode and use the release from uname. If +* uname or strdup fails then use the current perf tool version. +*/ + ret =uname (&uts); + env->os_release =strdup (ret <0 ? perf_version_string : uts.release); +return env->os_release ?: perf_version_string; +} +int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]) {int i;
tools/perf/util/env.h +1 -0
@@ -172,6 +172,7 @@ void free_cpu_domain_info(struct cpu_domain_map **cd_map, u32 schedstat_version,void perf_env__exit (struct perf_env *env);int perf_env__kernel_is_64_bit (struct perf_env *env); +const char *perf_env__os_release (struct perf_env *env);int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]);
tools/perf/util/header.c +11 -5
@@ -370,13 +370,19 @@ static int write_osrelease(struct feat_fd *ff, struct evlist *evlist __maybe_unused) {struct utsname uts; -int ret; +const char *release =NULL ; - ret =uname (&uts); -if (ret <0 ) -return -1 ; +if (evlist->session) + release =perf_env__os_release (perf_session__env (evlist->session)); -return do_write_string (ff, uts.release); +if (!release) { +int ret =uname (&uts); + +if (ret <0 ) +return -1 ; + release = uts.release; + } +return do_write_string (ff, release); }static int write_arch (struct feat_fd *ff,struct evlist *evlist)
tools/perf/util/symbol.c +2 -2
@@ -2226,7 +2226,7 @@ static int vmlinux_path__init(struct perf_env *env) {struct utsname uts;char bf[PATH_MAX ]; -char *kernel_version; +const char *kernel_version;unsigned int i; vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
@@ -2243,7 +2243,7 @@ static int vmlinux_path__init(struct perf_env *env)return 0 ;if (env) { - kernel_version = env->os_release; + kernel_version =perf_env__os_release (env); }else {if (uname (&uts) <0 ) goto out_fail;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v8 15/17] perf env: Add helper to lazily compute the os_release
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Incomplete refactoring: The JSON data converter was missed and does not use the new lazy…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Incomplete refactoring: The JSON data converter was missed and does not use the new lazy initialization helper.
- [Medium] Context leak: Assuming uninitialized os_release always implies live mode incorrectly pollutes offline trace conversions with the host's kernel version. --
commit a08c8b8c77a407cf0d562bc6f755d7e0c00f4156
Author: Ian Rogers <[email protected]>
perf env: Add helper to lazily compute the os_release
This commit introduces a helper function, perf_env__os_release(), to lazily initialize the OS release string when it is not populated. It updates the CTF data converter and other sites to use this helper. The helper assumes that if os_release isn't initialized, perf is running in live mode and uses uname() as a fallback.
tools/perf/util/data-convert-bt.c do { \ · line 1417
Quoted 10 lines
tools/perf/util/data-convert-bt.c +1
-1@@ -1414,7 +1414,7 @@ do { \ADD ("host" , env->hostname);ADD ("sysname" ,"Linux" ); -ADD ("release" , env->os_release); +ADD ("release" ,perf_env__os_release (env));
Was the JSON data converter missed in this refactoring?
The CTF converter was updated to use the new helper, but the JSON converter in tools/perf/util/data-convert-json.c still accesses the field directly:
tools/perf/util/data-convert-json.c:
...
output_json_key_string(out, true, 2, "hostname", env->hostname);
output_json_key_string(out, true, 2, "os-release", env->os_release);
output_json_key_string(out, true, 2, "arch", env->arch);
...This leads to inconsistent behavior where the JSON converter outputs an empty string or null if os_release is uninitialized, while the CTF converter outputs the host's release string.
Quoted 3 lines
tools/perf/util/data-convert-bt.c +0
-0ADD ("version" , env->version);ADD ("machine" , env->arch);ADD ("domain" ,"kernel" );
[ ... ]
Quoted 18 lines
tools/perf/util/env.c +10
-0@@ -339,6 +339,27 @@ int perf_env__kernel_is_64_bit(struct perf_env *env)return env->kernel_is_64_bit; } +const char *perf_env__os_release (struct perf_env *env) +{ +struct utsname uts; +int ret; + +if (!env) +return perf_version_string; + +if (env->os_release) +return env->os_release;
tools/perf/util/env.c int perf_env__kernel_is_64_bit(struct perf_env *env) · line 361
Quoted 10 lines
tools/perf/util/env.c +10
-0+ +/* +* The os_release is being accessed but wasn't initialized from a data +* file, assume this is 'live' mode and use the release from uname. If +* uname or strdup fails then use the current perf tool version. +*/ + ret =uname (&uts); + env->os_release =strdup (ret <0 ? perf_version_string : uts.release); +return env->os_release ?: perf_version_string; +}
Does this fallback to uname() incorrectly pollute offline trace conversions with the host's kernel version?
If a perf.data file was recorded without the HEADER_OSRELEASE feature, env->os_release will be NULL.
When running perf data convert --to-ctf on such a file, this helper will execute uname() and write the host machine's OS release into the trace environment.
Prior to this patch, a NULL os_release was safely omitted from the CTF export.
Injecting the converter's host kernel version into offline trace metadata falsifies the environment in which the trace was originally recorded.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
Ian Rogers
reviewerPATCH v8 16/17 perf symbol: Add setters for bitfields sharing a byte to avoid concurrent update issues
In reply to Ian RogersA problem with putting bitfields into struct symbol is that other bits in the symbol could be updated concurrently and only one update to the underlying storage unit happen, leading to lost updates.
To avoid this, introduce a global lock symbol_bits_lock in symbol.c and helper functions to update the bits sharing a byte:symbol__set_ignore and symbol__set_annotate2.
inlined is not given a setter as it is only initialized in new_inline_sym when the symbol is under construction and not shared.
--- tools/perf/builtin-kwork.c | 2 +- tools/perf/builtin-sched.c | 2 +- tools/perf/util/annotate.c | 2 +- tools/perf/util/symbol.c | 22 ++++++++++++++++++++++ tools/perf/util/symbol.h | 3 +++ 5 files changed, 28 insertions(+), 3 deletions(-)
tools/perf/builtin-kwork.c +1 -1
@@ -725,7 +725,7 @@ static void timehist_save_callchain(struct perf_kwork *kwork,if (sym) {if (!strcmp (sym->name,"__softirqentry_text_start" ) || !strcmp (sym->name,"__do_softirq" )) - sym->ignore =1 ; +symbol__set_ignore (sym, true); }callchain_cursor_advance (cursor);
tools/perf/builtin-sched.c +1 -1
@@ -2371,7 +2371,7 @@ static void save_task_callchain(struct perf_sched *sched,if (!strcmp (sym->name,"schedule" ) || !strcmp (sym->name,"__schedule" ) || !strcmp (sym->name,"preempt_schedule" )) - sym->ignore =1 ; +symbol__set_ignore (sym, true); }callchain_cursor_advance (cursor);
tools/perf/util/annotate.c +1 -1
@@ -2224,7 +2224,7 @@ int symbol__annotate2(struct map_symbol *ms, struct evsel *evsel,annotation__init_column_widths (notes, sym);annotation__update_column_widths (notes); - sym->annotate2 =1 ; +symbol__set_annotate2 (sym, true);return 0 ; }
tools/perf/util/symbol.c +22 -0
@@ -31,6 +31,7 @@#include "map.h" #include "symbol.h" #include "map_symbol.h" +#include "mutex.h" #include "mem-events.h" #include "mem-info.h" #include "symsrc.h"
@@ -52,6 +53,8 @@ static int dso__load_kernel_sym(struct dso *dso, struct map *map);static int dso__load_guest_kernel_sym (struct dso *dso,struct map *map);static bool symbol__is_idle (const char *name); +static struct mutex symbol_bits_lock; +int vmlinux_path__nr_entries;char **vmlinux_path;
@@ -345,6 +348,20 @@ void symbol__delete(struct symbol *sym)free (((void *)sym) - symbol_conf.priv_size); } +void symbol__set_ignore (struct symbol *sym,bool ignore) +{ +mutex_lock (&symbol_bits_lock); + sym->ignore = ignore; +mutex_unlock (&symbol_bits_lock); +} + +void symbol__set_annotate2 (struct symbol *sym,bool annotate2) +{ +mutex_lock (&symbol_bits_lock); + sym->annotate2 = annotate2; +mutex_unlock (&symbol_bits_lock); +} +void symbols__delete (struct rb_root_cached *symbols) {struct symbol *pos;
@@ -2415,6 +2432,8 @@ int symbol__init(struct perf_env *env)if (symbol_conf.initialized)return 0 ; +mutex_init (&symbol_bits_lock); + symbol_conf.priv_size =PERF_ALIGN (symbol_conf.priv_size,sizeof (u64));symbol__elf_init ();
@@ -2493,6 +2512,9 @@ void symbol__exit(void) {if (!symbol_conf.initialized)return ; + +mutex_destroy (&symbol_bits_lock); +strlist__delete (symbol_conf.bt_stop_list);strlist__delete (symbol_conf.sym_list);strlist__delete (symbol_conf.dso_list);
tools/perf/util/symbol.h +3 -0
@@ -77,6 +77,9 @@ struct symbol {void symbol__delete (struct symbol *sym);void symbols__delete (struct rb_root_cached *symbols); +void symbol__set_ignore (struct symbol *sym,bool ignore); +void symbol__set_annotate2 (struct symbol *sym,bool annotate2); +/* symbols__for_each_entry - iterate over symbols (rb_root) * * @symbols: the rb_root of symbols
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v8 16/17] perf symbol: Add setters for bitfields sharing a byte to avoid concurrent update issues
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Unconditional acquisition of a global mutex in high-frequency per-sample fast paths causes severe…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Unconditional acquisition of a global mutex in high-frequency per-sample fast paths causes severe performance regression.
- [High] Unlocked reads of bitfields concurrent with locked writes cause data races and undefined behavior.
- [Medium] The symbol_bits_lock mutex is leaked and potentially re-initialized if symbol__init() fails. --
commit 587302f6555ca9ddcc54f4761c10004005eaa78f
Author: Ian Rogers <[email protected]>
perf symbol: Add setters for bitfields sharing a byte to avoid concurrent update issues
This commit introduces a global symbol_bits_lock and helper functions to serialize updates to the ignore and annotate2 bitfields in struct symbol. This aims to prevent lost updates caused by concurrent modifications to bitfields sharing the same byte.
Quoted 4 lines
tools/perf/builtin-kwork.c +0
-0
[ ... ]
tools/perf/builtin-kwork.c static void timehist_save_callchain(struct perf_kwork *kwork, · line 731
Quoted 9 lines
tools/perf/builtin-kwork.c +1
-1@@ -725,7 +725,7 @@ static void timehist_save_callchain(struct perf_kwork *kwork,if (sym) {if (!strcmp (sym->name,"__softirqentry_text_start" ) || !strcmp (sym->name,"__do_softirq" )) - sym->ignore =1 ; +symbol__set_ignore (sym, true); }callchain_cursor_advance (cursor);
Since timehist_save_callchain is a per-sample fast path that can be called millions of times per run, could this unconditional acquisition of a global mutex cause severe lock contention and performance regression?
Would it be better to add a lockless check like if (!sym->ignore) to bypass the lock once the flag is already set?
Quoted 4 lines
tools/perf/builtin-sched.c +0
-0
[ ... ]
tools/perf/builtin-sched.c static void save_task_callchain(struct perf_sched *sched, · line 2375
Quoted 7 lines
tools/perf/builtin-sched.c +1
-1@@ -2371,7 +2371,7 @@ static void save_task_callchain(struct perf_sched *sched,if (!strcmp (sym->name,"schedule" ) || !strcmp (sym->name,"__schedule" ) || !strcmp (sym->name,"preempt_schedule" )) - sym->ignore =1 ; +symbol__set_ignore (sym, true); }
Could the same fast-path lock contention happen here during save_task_callchain?
Quoted 4 lines
tools/perf/util/symbol.c +0
-0
[ ... ]
Quoted 7 lines
tools/perf/util/symbol.c +3
-0@@ -345,6 +348,20 @@ void symbol__delete(struct symbol *sym)free (((void *)sym) - symbol_conf.priv_size); } +void symbol__set_ignore (struct symbol *sym,bool ignore) +{ +mutex_lock (&symbol_bits_lock);
tools/perf/util/symbol.c void symbol__delete(struct symbol *sym) · line 363
Quoted 10 lines
tools/perf/util/symbol.c +10
-0+ sym->ignore = ignore; +mutex_unlock (&symbol_bits_lock); +} + +void symbol__set_annotate2 (struct symbol *sym,bool annotate2) +{ +mutex_lock (&symbol_bits_lock); + sym->annotate2 = annotate2; +mutex_unlock (&symbol_bits_lock); +}
While the new symbol_bits_lock serializes writes to these bitfields, readers of sym->ignore and sym->annotate2 remain unlocked throughout the codebase.
In C, adjacent bitfields share the same underlying memory location. Under the C11 memory model, does reading a bitfield without synchronization while another thread performs a read-modify-write on that same memory location constitute a data race?
Could this lead to torn reads or undefined behavior during concurrent access?
tools/perf/util/symbol.c int symbol__init(struct perf_env *env) · line 2439
Quoted 9 lines
tools/perf/util/symbol.c +2
-0@@ -2415,6 +2432,8 @@ int symbol__init(struct perf_env *env)if (symbol_conf.initialized)return 0 ; +mutex_init (&symbol_bits_lock); + symbol_conf.priv_size =PERF_ALIGN (symbol_conf.priv_size,sizeof (u64));symbol__elf_init ();
If a subsequent initialization step fails, like setup_parallelism_bitmap, this function returns -1 without calling mutex_destroy on the error paths.
Because symbol_conf.initialized remains false, a subsequent call to symbol__exit will also return early and fail to clean the mutex up.
Additionally, if a caller attempts to retry symbol__init after a failure, could this cause mutex_init to be invoked on an already-initialized mutex?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
Ian Rogers
reviewerPATCH v8 17/17 perf symbol: Lazily compute idle and use a global lock for updates
In reply to Ian RogersMove the idle boolean to a helper symbol__is_idle function. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. As symbols__insert no longer needs to know if a symbol is for the kernel, remove the argument.
This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/
The change switches x86 matches to use strstarts which means intel_idle_irq is matched as part of strstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/
To avoid concurrent update issues with other bitfields in struct symbol, this change uses the global lock symbol_bits_lock (introduced in a previous commit) for updates to the idle field. A static helper symbol__set_idle taking a boolean is used to encapsulate the lock and mapping to enum symbol_idle_kind.
--- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 108 +++++++++++++++++++++++------------ tools/perf/util/symbol.h | 14 +++-- 3 files changed, 81 insertions(+), 43 deletions(-)
tools/perf/util/symbol-elf.c +1 -1
@@ -1727,7 +1727,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);
tools/perf/util/symbol.c +70 -38
172 lines not on this page · raw message
tools/perf/util/symbol.h +10 -4
@@ -43,6 +43,12 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx);#endif +enum symbol_idle_kind { +SYMBOL_IDLE__UNKNOWN =0 , +SYMBOL_IDLE__NOT_IDLE =1 , +SYMBOL_IDLE__IDLE =2 , +}; +/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index).
@@ -58,8 +64,8 @@ struct symbol { u8 type:4;/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */ u8 binding:4; -/** Set true for kernel symbols of idle routines. */ - u8 idle:1; +/** Cache for symbol__is_idle holding enum symbol_idle_kind values. */ + u8 idle:2;/** Resolvable but tools ignore it (e.g. idle routines). */ u8 ignore:1;/** Symbol for an inlined function. */
@@ -197,8 +203,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);
@@ -281,5 +286,6 @@ enum { };int symbol__validate_sym_arguments (void ); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,struct perf_env *env);#endif /* __PERF_SYMBOL */
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerv9 cover perf symbol/env: ELF machine clean up and lazy idle computation
In reply to Ian RogersAdd a helper to perf_env to compute the e_machine if it is EM_NONE. Derive the value from the arch string if available. Similarly derive the arch string from the ELF machine if available, for consistency. This means perf's arch (machine type) is no longer determined by uname but set to match that of the perf ELF executable.
Migrate code away from strcmp on env->arch to using the e_machine comparisons that are more accurate and not prone to uname and other naming differences. While cleaning this up, also clean up the capstone initialization code to cover more architectures and to set the big endian flag based on ELF header information.
Switch the idle computation to the point of use and lazily compute it, rather than computing it for every symbol. The current only user is perf top. At the point of use the perf_env is available and this can be used to make sure the idle function computation is machine and kernel version dependent.
To avoid concurrent update issues with bitfields sharing a byte in struct symbol switch to using C11 atomics.
v9:
- Key changes in v9:
- **C11 Atomics for struct symbol**: Dropped the global symbol_bits_lock introduced in v7/v8. Replaced unsafe bitfields with a thread-safe _Atomic uint16_t flags and lockless atomic helpers (e.g., symbol__type(), symbol__set_inlined()).
- **Bi-endianness Support**: Added *_endian variants for dso and thread helpers to ensure Capstone correctly disassembles cross-endian binaries.
- **Architecture Hardening**:
- Fixed inverted SPARC logic in perf_env__single_address_space().
- Prioritized DSO architecture over global environment in machine_or_dso_e_machine().
- Fixed an uninitialized memory leak in perf_env__e_machine().
- Removed lossy normalize_arch() canonicalization in process_arch().
- Review Feedback Status:
- **Addressed**: C11 atomics migration, bi-endianness, SPARC logic, DSO prioritization, and uninitialized memory fixes.
- **Not Addressed / Dropped**:
- Patch 15 OS Release: The concern regarding the uname() fallback during offline analysis was determined to be incorrect for these uninitialized states; the original lazy assumption is retained.
- Patch 04/11: The EM_AARCH64 fallbacks were dropped as the definition should come from dwarf-regs.h when necessary.
v8:
- Address Sashiko AI review feedback for Patch 1:
- Switch all code dependent on the arch string to use e_machine instead.
- Update machine__is and machine__normalized_is to take e_machine integers instead of strings.
- Refactor arch_syscalls__strerrno_function to take an e_machine.
- Avoid premature caching of the host architecture in
`perf_session__e_machine`.
v7:
- Address better handling of strdup failures with arch in the header/env.
- Address concurrent update issues in struct symbol bitfields by introducing a global lock for writes.
v6: Ensure arch is canonical by going to e_machine and back (Sashiko)
v5: Add perf_env os_release helper (Namhyung/Sashiko)
v4: Fix Sashiko issues where an array element wasn't sorted properly,
the e_flags weren't returned properly, the idle type is change to a u8 rather than an enum value and the s390 version check for psw_idle is slightly reordered and tweaked.
v3: Properly set up the e_machine coming from the perf_env as reported
by Honglei Wang.
v2: Some minor white space clean up.
v1: Initial release.
Ian Rogers (18):
perf env: Add perf_env__e_machine helper and use in perf_env__arch perf tests topology: Switch env->arch use to env->e_machine perf env, dso, thread: Add _endian variants for e_machine helpers perf capstone: Determine architecture from e_machine perf print_insn: Use e_machine for fallback IP length check perf symbol: Avoid use of machine__is
perf machine: Use perf_env e_machine rather than arch perf sample-raw: Use perf_env e_machine rather than arch perf sort: Use perf_env e_machine rather than arch
perf arch common: Use perf_env e_machine rather than arch perf header: In print_pmu_caps use perf_env e_machine perf c2c: Use perf_env e_machine rather than arch
perf lock-contention: Use perf_env e_machine rather than arch perf env: Refactor perf_env__arch_strerrno
perf env: Remove unused perf_env__raw_arch
perf env: Add helper to lazily compute the os_release perf symbol: Add setters for bitfields sharing a byte to avoid
concurrent update issues
perf symbol: Lazily compute idle
tools/perf/arch/common.c | 62 ++-- tools/perf/builtin-c2c.c | 40 +-- tools/perf/builtin-inject.c | 6 +- tools/perf/builtin-kwork.c | 2 +- tools/perf/builtin-report.c | 2 +- tools/perf/builtin-sched.c | 4 +- tools/perf/builtin-top.c | 7 +- tools/perf/builtin-trace.c | 7 +- tools/perf/tests/symbols.c | 2 +- tools/perf/tests/topology.c | 8 +- tools/perf/tests/vmlinux-kallsyms.c | 2 +- tools/perf/trace/beauty/arch_errno_names.sh | 40 ++- tools/perf/ui/browsers/annotate.c | 2 +- tools/perf/ui/browsers/map.c | 4 +- tools/perf/util/annotate.c | 5 +- tools/perf/util/auxtrace.c | 6 +- tools/perf/util/callchain.c | 4 +- tools/perf/util/capstone.c | 129 +++++--- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/dlfilter.c | 2 +- tools/perf/util/dso.c | 19 +- tools/perf/util/dso.h | 14 +- tools/perf/util/env.c | 295 ++++++++++++++---- tools/perf/util/env.h | 12 +- tools/perf/util/evsel_fprintf.c | 6 +- tools/perf/util/header.c | 58 ++-- tools/perf/util/intel-pt.c | 2 +- tools/perf/util/lock-contention.c | 6 +- tools/perf/util/machine.c | 27 +- tools/perf/util/machine.h | 2 - tools/perf/util/print_insn.c | 23 +- tools/perf/util/print_insn.h | 3 + tools/perf/util/probe-event.c | 4 +- tools/perf/util/sample-raw.c | 21 +- tools/perf/util/sample-raw.h | 6 +- .../util/scripting-engines/trace-event-perl.c | 2 +- .../scripting-engines/trace-event-python.c | 4 +- tools/perf/util/session.c | 26 +- tools/perf/util/sort.c | 66 ++-- tools/perf/util/srcline.c | 10 +- tools/perf/util/symbol-elf.c | 5 +- tools/perf/util/symbol.c | 208 ++++++++---- tools/perf/util/symbol.h | 74 ++++- tools/perf/util/symbol_fprintf.c | 4 +- tools/perf/util/thread.c | 22 +- tools/perf/util/thread.h | 8 +- 46 files changed, 860 insertions(+), 403 deletions(-)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v9 1/18 perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back to EM_HOST. Use the perf_env's arch to compute the e_machine if available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switch perf_env__arch to be derived the e_machine for consistency. This switches arch from being uname derived to matching that of the perf binary (via EM_HOST). Update session to use the helper, which may mean using EM_HOST when no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.
--- tools/perf/util/env.c | 231 +++++++++++++++++++++++++++++++------- tools/perf/util/env.h | 2 + tools/perf/util/header.c | 35 ++++-- tools/perf/util/session.c | 26 +++-- 4 files changed, 231 insertions(+), 63 deletions(-)
tools/perf/util/env.c +189 -42
@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #include "cpumap.h" +#include "dwarf-regs.h" #include "debug.h" #include "env.h" #include "util/header.h" #include "util/rwsem.h" #include <linux/compiler.h> +#include <linux/kernel.h> #include <linux/ctype.h> #include <linux/rbtree.h> #include <linux/string.h>
@@ -309,12 +311,21 @@ void perf_env__init(struct perf_env *env)static void perf_env__init_kernel_mode (struct perf_env *env) { -const char *arch =perf_env__raw_arch (env); +uint16_t e_machine = env->e_machine; -if (!strncmp (arch,"x86_64" ,6 ) || !strncmp (arch,"aarch64" ,7 ) || - !strncmp (arch,"arm64" ,5 ) || !strncmp (arch,"mips64" ,6 ) || - !strncmp (arch,"parisc64" ,8 ) || !strncmp (arch,"riscv64" ,7 ) || - !strncmp (arch,"s390x" ,5 ) || !strncmp (arch,"sparc64" ,7 )) +if (env->arch && (e_machine ==EM_NONE || e_machine ==EM_MIPS || e_machine ==EM_RISCV )) { +if (str_ends_with (env->arch,"64" ) || !strncmp (env->arch,"s390x" ,5 )) + env->kernel_is_64_bit =1 ; +else + env->kernel_is_64_bit =0 ; +return ; + } +if (e_machine ==EM_NONE ) + e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); + +if (e_machine ==EM_X86_64 || e_machine ==EM_AARCH64 || + e_machine ==EM_PPC64 || e_machine ==EM_SPARCV9 || + e_machine ==EM_S390 ) env->kernel_is_64_bit =1 ;else env->kernel_is_64_bit =0 ;
@@ -588,51 +599,187 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)zfree (&cache->size); } +struct arch_to_e_machine { +const char *prefix; +uint16_t e_machine; +}; +/* -* Return architecture name in a normalized form. -* The conversion logic comes from the Makefile. +* A mapping from an arch prefix string to an ELF machine that can be used in a +* bsearch. Some arch prefixes are shared an need additional processing as +* marked next to the architecture. The prefixes handle both perf's architecture +* naming and those from uname. */ -static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch; +static const struct arch_to_e_machine prefix_to_e_machine[] = { + {"aarch64" ,EM_AARCH64 }, + {"alpha" ,EM_ALPHA }, + {"arc" ,EM_ARC }, + {"arm" ,EM_ARM },/* Check also for EM_AARCH64. */ + {"avr" ,EM_AVR },/* Check also for EM_AVR32. */ + {"bfin" ,EM_BLACKFIN }, + {"blackfin" ,EM_BLACKFIN }, + {"cris" ,EM_CRIS }, + {"csky" ,EM_CSKY }, + {"hppa" ,EM_PARISC }, + {"i386" ,EM_386 }, + {"i486" ,EM_386 }, + {"i586" ,EM_386 }, + {"i686" ,EM_386 }, + {"loongarch" ,EM_LOONGARCH }, + {"m32r" ,EM_M32R }, + {"m68k" ,EM_68K }, + {"microblaze" ,EM_MICROBLAZE }, + {"mips" ,EM_MIPS }, + {"msp430" ,EM_MSP430 }, + {"parisc" ,EM_PARISC }, + {"powerpc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"ppc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"riscv" ,EM_RISCV }, + {"s390" ,EM_S390 }, + {"sa110" ,EM_ARM }, + {"sh" ,EM_SH }, + {"sparc" ,EM_SPARC },/* Check also for EM_SPARCV9. */ + {"sun4u" ,EM_SPARC }, + {"x86" ,EM_X86_64 },/* Check also for EM_386. */ + {"xtensa" ,EM_XTENSA }, +}; + +static int compare_prefix (const void *key,const void *element) +{ +const char *search_key = key; +const struct arch_to_e_machine *map_element = element; +size_t prefix_len =strlen (map_element->prefix); + +return strncmp (search_key, map_element->prefix, prefix_len); +} + +static uint16_t perf_arch_to_e_machine (const char *perf_arch,int is_64_bit) +{ +/* Binary search for a matching prefix. */ +const struct arch_to_e_machine *result; + +if (!perf_arch) +return EM_HOST ; + + result =bsearch (perf_arch, + prefix_to_e_machine,ARRAY_SIZE (prefix_to_e_machine), +sizeof (prefix_to_e_machine[0 ]), + compare_prefix); + +if (!result) { +pr_debug ("Unknown perf arch for ELF machine mapping: %s\n" , perf_arch); +return EM_NONE ; + } + +/* +* Handle conflicting prefixes. If the is_64_bit is unknown (-1) then +* assume 64-bit. We can't use perf_env__kernel_is_64_bit as that +* depends on the arch string. +*/ +switch (result->e_machine) { +case EM_ARM : +return !strcmp (perf_arch,"arm64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +return (is_64_bit !=0 ) ||strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ; +case EM_SPARC : +return (is_64_bit !=0 ) || !strcmp (perf_arch,"sparc64" ) ?EM_SPARCV9 :EM_SPARC ; +case EM_X86_64 : +return (is_64_bit !=0 ) || !strcmp (perf_arch,"x86_64" ) ?EM_X86_64 :EM_386 ; +default : +return result->e_machine; + } +} + +static const char *e_machine_to_perf_arch (uint16_t e_machine) +{ +/* +* Table for if either the perf arch string differs from uname or there +* are >1 ELF machine with the prefix. +*/ +static const struct arch_to_e_machine extras[] = { + {"arm64" ,EM_AARCH64 }, + {"avr32" ,EM_AVR32 }, + {"powerpc" ,EM_PPC }, + {"powerpc" ,EM_PPC64 }, + {"sparc" ,EM_SPARCV9 }, + {"x86" ,EM_386 }, + {"x86" ,EM_X86_64 }, + {"none" ,EM_NONE }, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (extras); i++) { +if (extras[i].e_machine == e_machine) +return extras[i].prefix; + } + +for (size_t i =0 ; i <ARRAY_SIZE (prefix_to_e_machine); i++) { +if (prefix_to_e_machine[i].e_machine == e_machine) +return prefix_to_e_machine[i].prefix; + + } +return "unknown" ; +} + +uint16_t perf_env__e_machine_nocache (struct perf_env *env,uint32_t *e_flags) +{ +uint16_t e_machine =EM_HOST ; + +if (env) + e_machine =perf_arch_to_e_machine (env->arch, env->kernel_is_64_bit); + +if (e_flags) + *e_flags = (e_machine ==EM_HOST ) ?EF_HOST :0 ; + +return e_machine; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +uint16_t e_machine; +uint32_t local_e_flags =0 ; + +if (env && env->e_machine !=EM_NONE ) { +if (e_flags) + *e_flags = env->e_flags; + +return env->e_machine; + } + e_machine =perf_env__e_machine_nocache (env, &local_e_flags); +if (env) { + env->e_machine = e_machine; + env->e_flags = local_e_flags; + } +if (e_flags) + *e_flags = local_e_flags; + +return e_machine; }const char *perf_env__arch (struct perf_env *env) { -char *arch_name; +uint16_t e_machine; +const char *arch; -if (!env || !env->arch) {/* Assume local operation */ -static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +if (!env) +return e_machine_to_perf_arch (EM_HOST ); + +if (env->arch) +return env->arch; -return normalize_arch (arch_name); +/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ + e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); + arch =e_machine_to_perf_arch (e_machine); + env->arch =strdup (arch); +/* +* Avoid potential crashes on the arch string if memory allocation in +* strdup fails and NULL were to be returned. +*/ +return env->arch ?: arch; }#if defined(HAVE_LIBTRACEEVENT )
tools/perf/util/env.h +2 -0
@@ -187,6 +187,8 @@ int perf_env__read_cpu_topology_map(struct perf_env *env);void cpu_cache_level__free (struct cpu_cache_level *cache); +uint16_t perf_env__e_machine_nocache (struct perf_env *env,uint32_t *e_flags); +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env);const char *perf_env__arch_strerrno (struct perf_env *env,int err);const char *perf_env__cpuid (struct perf_env *env);
tools/perf/util/header.c +25 -10
@@ -379,21 +379,28 @@ static int write_osrelease(struct feat_fd *ff,return do_write_string (ff, uts.release); } -static int write_arch (struct feat_fd *ff, -struct evlist *evlist__maybe_unused ) +static int write_arch (struct feat_fd *ff,struct evlist *evlist) {struct utsname uts; -int ret; +const char *arch =NULL ; - ret =uname (&uts); -if (ret <0 ) -return -1 ; +if (evlist->session) { +/* Force the computation in the perf_env of the e_machine of the threads. */ +perf_session__e_machine (evlist->session,/*e_flags=*/ NULL ); + arch =perf_env__arch (perf_session__env (evlist->session)); + } -return do_write_string (ff, uts.machine); +if (!arch) { +int ret =uname (&uts); + +if (ret <0 ) +return -1 ; + arch = uts.machine; + } +return do_write_string (ff, arch); } -static int write_e_machine (struct feat_fd *ff, -struct evlist *evlist__maybe_unused ) +static int write_e_machine (struct feat_fd *ff,struct evlist *evlist) {/* e_machine expanded from 16 to 32-bits for alignment. */ uint32_t e_flags;
@@ -2684,10 +2691,18 @@ static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \FEAT_PROCESS_STR_FUN (hostname, hostname);FEAT_PROCESS_STR_FUN (osrelease, os_release);FEAT_PROCESS_STR_FUN (version, version); -FEAT_PROCESS_STR_FUN (arch, arch);FEAT_PROCESS_STR_FUN (cpudesc, cpu_desc);FEAT_PROCESS_STR_FUN (cpuid, cpuid); +static int process_arch (struct feat_fd *ff,void *data__maybe_unused ) +{ +free (ff->ph->env.arch); + ff->ph->env.arch =do_read_string (ff); +if (!ff->ph->env.arch) +return -ENOMEM ; +return 0 ; +} +static int process_e_machine (struct feat_fd *ff,void *data__maybe_unused ) {int ret;
tools/perf/util/session.c +15 -11
@@ -3023,14 +3023,19 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* +* Is the env caching an e_machine? If not we want to compute from the +* more accurate threads. +*/ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);
@@ -3048,10 +3053,9 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags/* * Couldn't determine from the perf_env or current set of -* threads. Default to the host. +* threads. Potentially use logic that uses the arch string otherwise +* default to the host. Don't cache in the perf_env in case later +* threads indicate a better ELF machine type. */ -if (e_flags) - *e_flags =EF_HOST ; - -return EM_HOST ; +return perf_env__e_machine_nocache (env, e_flags); }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v9 01/18] perf env: Add perf_env__e_machine helper and use in perf_env__arch
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Backwards and forwards compatibility breakage via architecture normalization. - [High]…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Backwards and forwards compatibility breakage via architecture normalization.
- [High] Architecture misclassification and data loss due to guaranteed EM_HOST fallback.
- [High] Premature caching of the fallback architecture disables thread-based evaluation.
- [High] Flawed 64-bit kernel detection for MIPS, RISC-V, and legacy endianness suffixes. --
commit 745bee191a4ba7e972076983225158e66679b6ae
Author: Ian Rogers <[email protected]>
perf env: Add perf_env__e_machine helper and use in perf_env__arch
This commit introduces a helper to lazily compute the e_machine from the perf environment and uses the session's e_machine to derive the architecture string. It updates the data file header writing to fetch the e_machine and arch from the session if available.
Quoted 4 lines
tools/perf/util/env.c +0
-0
[ ... ]
Quoted 8 lines
tools/perf/util/env.c +1
-2@@ -309,12 +311,21 @@ void perf_env__init(struct perf_env *env)static void perf_env__init_kernel_mode (struct perf_env *env) { -const char *arch =perf_env__raw_arch (env); +uint16_t e_machine = env->e_machine; - if (!strncmp(arch, "x86_64", 6) || !strncmp(arch, "aarch64", 7) ||
tools/perf/util/env.c void perf_env__init(struct perf_env *env) · line 322
Quoted 10 lines
tools/perf/util/env.c +7
-3- !strncmp (arch,"arm64" ,5 ) || !strncmp (arch,"mips64" ,6 ) || - !strncmp (arch,"parisc64" ,8 ) || !strncmp (arch,"riscv64" ,7 ) || - !strncmp(arch, "s390x", 5) || !strncmp(arch, "sparc64", 7)) +if (env->arch && (e_machine ==EM_NONE || e_machine ==EM_MIPS || e_machine ==EM_RISCV )) { +if (str_ends_with (env->arch,"64" ) || !strncmp (env->arch,"s390x" ,5 )) + env->kernel_is_64_bit =1 ; +else + env->kernel_is_64_bit =0 ; +return ; + }
e_machine_to_perf_arch() returns base strings like "mips" or "riscv" without a "64" suffix. Does this cause new 64-bit MIPS and RISC-V traces to be misclassified as 32-bit since str_ends_with() will return false?
Additionally, older perf.data files may contain endianness suffixes like "mips64el" which correctly matched the previous strncmp() logic. Will these legacy files fail the new str_ends_with("64") check and corrupt 64-bit traces?
[ ... ]
Quoted 1 line
tools/perf/util/env.c +0
-0@@ -588,51 +599,187 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)
[ ... ]
Quoted 26 lines
tools/perf/util/env.c +0
-26-static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch;
[ ... ]
Quoted 23 lines
const char *perf_env__arch(struct
perf_env*env)
{tools/perf/util/env.c +10
-9-char *arch_name; +uint16_t e_machine; +const char *arch; -if (!env || !env->arch) {/* Assume local operation */ -static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +if (!env) +return e_machine_to_perf_arch (EM_HOST ); + +if (env->arch) +return env->arch; -return normalize_arch (arch_name); +/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST.
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 783
Quoted 10 lines
tools/perf/util/env.c +9
-0+*/ + e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); + arch =e_machine_to_perf_arch (e_machine); + env->arch =strdup (arch); +/* +* Avoid potential crashes on the arch string if memory allocation in +* strdup fails and NULL were to be returned. +*/ +return env->arch ?: arch; }
The removal of normalize_arch() appears to break both backward and forward compatibility.
When reading older perf.data files, env->arch is populated with the raw string from the file (e.g., "x86_64"). Without normalize_arch(), perf_env__arch() will return "x86_64", potentially breaking internal logic that expects the normalized "x86".
Conversely, for new files, e_machine_to_perf_arch() generates normalized names like "x86". Older versions of the perf tool expect the raw string for 64-bit detection:
perf_env__init_kernel_mode() {
const char *arch = perf_env__raw_arch(env);
if (!strncmp(arch, "x86_64", 6) || ...
}Does this mean older perf binaries will incorrectly parse new 64-bit perf.data files as 32-bit traces?
Quoted 4 lines
tools/perf/util/header.c +0
-0
[ ... ]
Quoted 6 lines
tools/perf/util/header.c +1
-3-static int write_arch (struct feat_fd *ff, -struct evlist *evlist__maybe_unused ) +static int write_arch (struct feat_fd *ff,struct evlist *evlist) {struct utsname uts; -int ret;
tools/perf/util/header.c static int write_osrelease(struct feat_fd *ff, · line 391
Quoted 10 lines
tools/perf/util/header.c +6
-3+const char *arch =NULL ; - ret =uname (&uts); -if (ret <0 ) -return -1 ; +if (evlist->session) { +/* Force the computation in the perf_env of the e_machine of the threads. */ +perf_session__e_machine (evlist->session,/*e_flags=*/ NULL ); + arch =perf_env__arch (perf_session__env (evlist->session)); + }
Is it intended that perf_env__arch() permanently caches the fallback architecture here?
perf_session__e_machine() avoids caching the fallback EM_HOST when no threads are present, noting: "Don't cache in the perf_env in case later threads indicate a better ELF machine type."
However, immediately afterward, perf_env__arch() calls perf_env__e_machine(), which populates env->e_machine = EM_HOST. Because it gets cached, will all subsequent evaluations for the session short-circuit and return EM_HOST, ignoring the true thread architecture once threads are added?
tools/perf/util/header.c static int write_osrelease(struct feat_fd *ff, · line 401
Quoted 11 lines
tools/perf/util/header.c +8
-1-return do_write_string (ff, uts.machine); +if (!arch) { +int ret =uname (&uts); + +if (ret <0 ) +return -1 ; + arch = uts.machine; + } +return do_write_string (ff, arch); }
Can the if (!arch) block ever be reached?
perf_env__arch() is guaranteed to return a valid string (or "unknown"), meaning arch will never be NULL. This skips the uname(&uts) fallback entirely.
As a result, if it falls back to EM_HOST, won't a 32-bit perf binary running on a 64-bit kernel incorrectly record a 32-bit system architecture?
Additionally, for system architectures missing from the prefix_to_e_machine table (e.g., hexagon, openrisc), won't it permanently record "unknown" in the header instead of the correct uname information?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Namhyung Kim
maintainerRe: [PATCH v9 01/18] perf env: Add perf_env__e_machine helper and use in perf_env__arch
In reply to Ian RogersQuoted 12 lines · Ian Rogers
Add a helper that lazily computes the
e_machineand falls back toEM_HOST. Use the perf_env's arch to compute thee_machineif available. Use a binary search for some efficiency in this, but handle somewhat complex duplicate rules. Switchperf_env__archto be derived thee_machinefor consistency. This switches arch from being uname derived to matching that of the perf binary (viaEM_HOST). Update session to use the helper, which may mean usingEM_HOSTwhen no threads are available. This also updates the perf data file header that gets the e_machine/e_flags from the session.Signed-off-by Ian Rogers <[email protected]>---
[SNIP]
Quoted 48 lines
tools/perf/util/header.c +16
-10@@ -379,21 +379,28 @@ static int write_osrelease(struct feat_fd *ff,return do_write_string (ff, uts.release); } -static int write_arch (struct feat_fd *ff, -struct evlist *evlist__maybe_unused ) +static int write_arch (struct feat_fd *ff,struct evlist *evlist) {struct utsname uts; -int ret; +const char *arch =NULL ; - ret =uname (&uts); -if (ret <0 ) -return -1 ; +if (evlist->session) { +/* Force the computation in the perf_env of the e_machine of the threads. */ +perf_session__e_machine (evlist->session,/*e_flags=*/ NULL ); + arch =perf_env__arch (perf_session__env (evlist->session)); + } -return do_write_string (ff, uts.machine); +if (!arch) { +int ret =uname (&uts); + +if (ret <0 ) +return -1 ; + arch = uts.machine; + } +return do_write_string (ff, arch); } -static int write_e_machine (struct feat_fd *ff, -struct evlist *evlist__maybe_unused ) +static int write_e_machine (struct feat_fd *ff,struct evlist *evlist) {/* e_machine expanded from 16 to 32-bits for alignment. */ uint32_t e_flags;@@ -2684,10 +2691,18 @@ static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \FEAT_PROCESS_STR_FUN (hostname, hostname);FEAT_PROCESS_STR_FUN (osrelease, os_release);FEAT_PROCESS_STR_FUN (version, version); -FEAT_PROCESS_STR_FUN (arch, arch);FEAT_PROCESS_STR_FUN (cpudesc, cpu_desc);
tools/perf/util/header.c static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \ · line 2704
Quoted 10 lines
tools/perf/util/header.c +8
-0FEAT_PROCESS_STR_FUN (cpuid, cpuid); +static int process_arch (struct feat_fd *ff,void *data__maybe_unused ) +{ +free (ff->ph->env.arch); + ff->ph->env.arch =do_read_string (ff); +if (!ff->ph->env.arch) +return -ENOMEM ; +return 0 ; +}
Isn't it same as FEAT_PROCESS_STR_FUN()?
Quoted 21 lines
tools/perf/util/header.c +1
-0+static int process_e_machine (struct feat_fd *ff,void *data__maybe_unused ) {int ret;tools/perf/util/session.c +4
-4@@ -3023,14 +3023,19 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* +* Is the env caching an e_machine? If not we want to compute from the +* more accurate threads. +*/ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; -
tools/perf/util/session.c uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags · line 3038
Quoted 10 lines
tools/perf/util/session.c +7
-2-return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/
I'm curious if it's always better. If EM_HOST is 64-bit and the first thread in a session happens to be 32-bit. Then resulting e_machine would be 32- bit, right? Is that what we want?
Thanks,
Namhyung
Quoted 21 lines
tools/perf/util/session.c +4
-5machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);@@ -3048,10 +3053,9 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags/* * Couldn't determine from the perf_env or current set of -* threads. Default to the host. +* threads. Potentially use logic that uses the arch string otherwise +* default to the host. Don't cache in the perf_env in case later +* threads indicate a better ELF machine type. */ -if (e_flags) - *e_flags =EF_HOST ; - -return EM_HOST ; +return perf_env__e_machine_nocache (env, e_flags); }Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v9 2/18 perf tests topology: Switch env->arch use to env->e_machine
In reply to Ian RogersSome arch string comparisons weren't normalized. Avoid potential issues with normalized names vs uname values by swtiching to using the e_machine.
--- tools/perf/tests/topology.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/tests/topology.c +6 -2
@@ -11,6 +11,7 @@#include "pmus.h" #include "target.h" #include <linux/err.h> +#include <elf.h> #define TEMPL "/tmp/perf-test-XXXXXX"#define DATA_SIZE 10
@@ -74,6 +75,7 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)struct aggr_cpu_id id;struct perf_cpu cpu;struct perf_env *env; +uint16_t e_machine; session =perf_session__new (&data,NULL );TEST_ASSERT_VAL ("can't get session" , !IS_ERR (session));
@@ -101,7 +103,9 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)* condition is true (see do_core_id_test in header.c). So always * run this test on those platforms. */ -if (!env->cpu &&strncmp (env->arch,"s390" ,4 ) &&strncmp (env->arch,"aarch64" ,7 )) + e_machine =perf_env__e_machine (env,NULL ); + +if (!env->cpu && e_machine !=EM_S390 && e_machine !=EM_AARCH64 )return TEST_SKIP ;/*
@@ -110,7 +114,7 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)* physical_package_id will be set to -1. Hence skip this * test if physical_package_id returns -1 for cpu from perf_cpu_map. */ -if (!strncmp (env->arch,"ppc64le" ,7 )) { +if (e_machine ==EM_PPC64 ) {if (cpu__get_socket_id (perf_cpu_map__cpu (map,0 )) ==-1 )return TEST_SKIP ; }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v9 3/18 perf env, dso, thread: Add _endian variants for e_machine helpers
In reply to Ian RogersAdd perf_arch_is_big_endian(), dso__read_e_machine_endian(), dso__e_machine_endian(), and thread__e_machine_endian() to support bi-endianness and cross-architecture analysis without breaking the existing API.
These helpers allow querying the absolute endianness of a DSO or thread, which is required for tools like Capstone that need to set the correct disassembly mode.
--- tools/perf/util/dso.c | 19 +++++++++++++------ tools/perf/util/dso.h | 14 ++++++++++++-- tools/perf/util/env.c | 12 ++++++++++++ tools/perf/util/env.h | 1 + tools/perf/util/thread.c | 22 ++++++++++++++++------ tools/perf/util/thread.h | 8 +++++++- 6 files changed, 61 insertions(+), 15 deletions(-)
tools/perf/util/dso.c +13 -6
@@ -1220,7 +1220,8 @@ static enum dso_swap_type dso_swap_type__from_elf_data(unsigned char eidata) }/* Reads e_machine from fd, optionally caching data in dso. */ -uint16_t dso__read_e_machine (struct dso *optional_dso,int fd,uint32_t *e_flags) +uint16_t dso__read_e_machine_endian (struct dso *optional_dso,int fd,uint32_t *e_flags, +bool *is_big_endian) {uint16_t e_machine =EM_NONE ;unsigned char e_ident[EI_NIDENT ];
@@ -1250,6 +1251,9 @@ uint16_t dso__read_e_machine(struct dso *optional_dso, int fd, uint32_t *e_flagsif (swap_type ==DSO_SWAP__UNSET )return EM_NONE ;// Bad ELF data encoding. +if (is_big_endian) + *is_big_endian = (e_ident[EI_DATA ] ==ELFDATA2MSB ); +/* Cache the need for swapping. */ if (optional_dso) { assert(dso__needs_swap(optional_dso) == DSO_SWAP__UNSET ||
@@ -1288,7 +1292,8 @@ uint16_t dso__read_e_machine(struct dso *optional_dso, int fd, uint32_t *e_flagsreturn e_machine; } -uint16_t dso__e_machine (struct dso *dso,struct machine *machine,uint32_t *e_flags) +uint16_t dso__e_machine_endian (struct dso *dso,struct machine *machine,uint32_t *e_flags, +bool *is_big_endian) {uint16_t e_machine =EM_NONE ;int fd;
@@ -1308,9 +1313,11 @@ uint16_t dso__e_machine(struct dso *dso, struct machine *machine, uint32_t *e_flcase DSO_BINARY_TYPE__BPF_IMAGE :case DSO_BINARY_TYPE__OOL :case DSO_BINARY_TYPE__JAVA_JIT : -if (e_flags) - *e_flags =EF_HOST ; -return EM_HOST ; +if (is_big_endian) { + *is_big_endian =perf_arch_is_big_endian ( + machine && machine->env ? machine->env->arch :NULL ); + } +return perf_env__e_machine (machine ? machine->env :NULL , e_flags);case DSO_BINARY_TYPE__DEBUGLINK :case DSO_BINARY_TYPE__BUILD_ID_CACHE :case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO :
@@ -1338,7 +1345,7 @@ uint16_t dso__e_machine(struct dso *dso, struct machine *machine, uint32_t *e_fltry_to_open_dso (dso, machine); fd =dso__data (dso)->fd;if (fd >=0 ) - e_machine =dso__read_e_machine (dso, fd, e_flags); + e_machine =dso__read_e_machine_endian (dso, fd, e_flags, is_big_endian);else if (e_flags) *e_flags =0 ;
tools/perf/util/dso.h +12 -2
@@ -866,8 +866,18 @@ int dso__data_file_size(struct dso *dso, struct machine *machine);off_t dso__data_size (struct dso *dso,struct machine *machine);ssize_t dso__data_read_offset (struct dso *dso,struct machine *machine,u64 offset,u8 *data,ssize_t size); -uint16_t dso__read_e_machine (struct dso *optional_dso,int fd,uint32_t *e_flags); -uint16_t dso__e_machine (struct dso *dso,struct machine *machine,uint32_t *e_flags); +uint16_t dso__read_e_machine_endian (struct dso *optional_dso,int fd,uint32_t *e_flags, +bool *is_big_endian); +static inline uint16_t dso__read_e_machine (struct dso *optional_dso,int fd,uint32_t *e_flags) +{ +return dso__read_e_machine_endian (optional_dso, fd, e_flags,NULL ); +} +uint16_t dso__e_machine_endian (struct dso *dso,struct machine *machine,uint32_t *e_flags, +bool *is_big_endian); +static inline uint16_t dso__e_machine (struct dso *dso,struct machine *machine,uint32_t *e_flags) +{ +return dso__e_machine_endian (dso, machine, e_flags,NULL ); +}ssize_t dso__data_read_addr (struct dso *dso,struct map *map,struct machine *machine,u64 addr,u8 *data,ssize_t size);
tools/perf/util/env.c +12 -0
@@ -339,6 +339,18 @@ int perf_env__kernel_is_64_bit(struct perf_env *env)return env->kernel_is_64_bit; } +bool perf_arch_is_big_endian (const char *arch) +{ +if (!arch) +return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; + +if (str_ends_with (arch,"_be" ) || !strcmp (arch,"sparc" ) || !strcmp (arch,"sparc64" ) || + !strcmp (arch,"s390" ) || !strcmp (arch,"s390x" ) || !strcmp (arch,"powerpc" )) +return true; + +return false; +} +int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]) {int i;
tools/perf/util/env.h +1 -0
@@ -175,6 +175,7 @@ void free_cpu_domain_info(struct cpu_domain_map **cd_map, u32 schedstat_version,void perf_env__exit (struct perf_env *env);int perf_env__kernel_is_64_bit (struct perf_env *env); +bool perf_arch_is_big_endian (const char *arch);int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]);
tools/perf/util/thread.c +16 -6
@@ -449,7 +449,7 @@ void thread__find_cpumode_addr_location(struct thread *thread, u64 addr, } } -static uint16_t read_proc_e_machine_for_pid (pid_t pid,uint32_t *e_flags) +static uint16_t read_proc_e_machine_for_pid (pid_t pid,uint32_t *e_flags,bool *is_big_endian) {char path[6 /* "/proc/" */ +11 /* max length of pid */ +5 /* "/exe\0" */ ];int fd;
@@ -458,7 +458,8 @@ static uint16_t read_proc_e_machine_for_pid(pid_t pid, uint32_t *e_flags)snprintf (path,sizeof (path),"/proc/%d/exe" , pid); fd =open (path,O_RDONLY );if (fd >=0 ) { - e_machine =dso__read_e_machine (/*optional_dso=*/ NULL , fd, e_flags); + e_machine =dso__read_e_machine_endian (/*optional_dso=*/ NULL , fd, e_flags, + is_big_endian);close (fd); }return e_machine;
@@ -468,6 +469,7 @@ struct thread__e_machine_callback_args {struct machine *machine;uint32_t e_flags;uint16_t e_machine; +bool is_big_endian; };static int thread__e_machine_callback (struct map *map,void *_args)
@@ -478,11 +480,13 @@ static int thread__e_machine_callback(struct map *map, void *_args)if (!dso)return 0 ;// No dso, continue search. - args->e_machine =dso__e_machine (dso, args->machine, &args->e_flags); + args->e_machine = +dso__e_machine_endian (dso, args->machine, &args->e_flags, &args->is_big_endian);return args->e_machine !=EM_NONE ?1 /* stop search */ :0 /* continue search */ ; } -uint16_t thread__e_machine (struct thread *thread,struct machine *machine,uint32_t *e_flags) +uint16_t thread__e_machine_endian (struct thread *thread,struct machine *machine,uint32_t *e_flags, +bool *is_big_endian) {pid_t tid, pid;uint16_t e_machine =RC_CHK_ACCESS (thread)->e_machine;
@@ -491,6 +495,7 @@ uint16_t thread__e_machine(struct thread *thread, struct machine *machine, uint3 .machine = machine, .e_flags = 0, .e_machine = EM_NONE, + .is_big_endian = false, };if (e_machine !=EM_NONE ) {
@@ -510,7 +515,8 @@ uint16_t thread__e_machine(struct thread *thread, struct machine *machine, uint3struct thread *parent =machine__findnew_thread (machine, pid, pid);if (parent) { - e_machine =thread__e_machine (parent, machine, &local_e_flags); + e_machine =thread__e_machine_endian (parent, machine, &local_e_flags, + is_big_endian);thread__put (parent); goto out; }
@@ -522,6 +528,8 @@ uint16_t thread__e_machine(struct thread *thread, struct machine *machine, uint3if (args.e_machine !=EM_NONE ) { e_machine = args.e_machine; local_e_flags = args.e_flags; +if (is_big_endian) + *is_big_endian = args.is_big_endian; }else {/* Maps failed, perhaps we're live with map events disabled. */ bool is_live = machine->machines ==NULL ;
@@ -536,7 +544,7 @@ uint16_t thread__e_machine(struct thread *thread, struct machine *machine, uint3 }/* Read from /proc/pid/exe if live. */ if (is_live) - e_machine =read_proc_e_machine_for_pid (pid, &local_e_flags); + e_machine =read_proc_e_machine_for_pid (pid, &local_e_flags, is_big_endian); } out:if (e_machine !=EM_NONE ) {
@@ -545,6 +553,8 @@ uint16_t thread__e_machine(struct thread *thread, struct machine *machine, uint3 }else { e_machine =EM_HOST ; local_e_flags =EF_HOST ; +if (is_big_endian) + *is_big_endian = (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__); }if (e_flags) *e_flags = local_e_flags;
tools/perf/util/thread.h +7 -1
@@ -311,7 +311,13 @@ static inline void thread__set_filter_entry_depth(struct thread *thread, int depRC_CHK_ACCESS (thread)->filter_entry_depth = depth; } -uint16_t thread__e_machine (struct thread *thread,struct machine *machine,uint32_t *e_flags); +uint16_t thread__e_machine_endian (struct thread *thread,struct machine *machine,uint32_t *e_flags, +bool *is_big_endian); +static inline uint16_t thread__e_machine (struct thread *thread,struct machine *machine, +uint32_t *e_flags) +{ +return thread__e_machine_endian (thread, machine, e_flags,NULL ); +}static inline void thread__set_e_machine (struct thread *thread,uint16_t e_machine) {
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v9 03/18] perf env, dso, thread: Add _endian variants for e_machine helpers
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] thread__e_machine_endian() fast-path returns successfully without initializing the is_big_endian…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] thread__e_machine_endian() fast-path returns successfully without initializing the is_big_endian output parameter.
- [High] dso__e_machine_endian() bypasses lazy evaluation of env->arch, silently falling back to the host's endianness.
- [High] perf_arch_is_big_endian() falsely categorizes standard big-endian architectures (e.g., MIPS, m68k) as little-endian.
- [High] read_proc_e_machine_for_pid() uses a blocking open() on user-controlled /proc paths, risking an indefinite hang. --
commit 34bedc0fb8b78f200595dbcc97e6be1af183988e
Author: Ian Rogers <[email protected]>
perf env, dso, thread: Add _endian variants for e_machine helpers
Add perf_arch_is_big_endian(), dso__read_e_machine_endian(), dso__e_machine_endian(), and thread__e_machine_endian() to support bi-endianness and cross-architecture analysis without breaking the existing API.
Quoted 4 lines
tools/perf/util/dso.c +0
-0
[ ... ]
tools/perf/util/dso.c uint16_t dso__e_machine(struct dso *dso, struct machine *machine, uint32_t *e_fl · line 1318
Quoted 10 lines
tools/perf/util/dso.c +3
-3@@ -1308,9 +1313,11 @@ uint16_t dso__e_machine(struct dso *dso, struct machine *machine, uint32_t *e_flcase DSO_BINARY_TYPE__BPF_IMAGE :case DSO_BINARY_TYPE__OOL :case DSO_BINARY_TYPE__JAVA_JIT : -if (e_flags) - *e_flags =EF_HOST ; -return EM_HOST ; +if (is_big_endian) { + *is_big_endian =perf_arch_is_big_endian ( + machine && machine->env ? machine->env->arch :NULL );
Could this direct access to machine->env->arch read a NULL pointer if the architecture hasn't been lazily computed yet?
The arch string inside struct perf_env is lazily populated via perf_env__arch(env). If NULL is passed to perf_arch_is_big_endian() here, it will default to the host's byte order.
Quoted 3 lines
tools/perf/util/dso.c +2
-0+ } +return perf_env__e_machine (machine ? machine->env :NULL , e_flags);case DSO_BINARY_TYPE__DEBUGLINK :
[ ... ]
Quoted 4 lines
tools/perf/util/env.c +0
-0
[ ... ]
Quoted 5 lines
tools/perf/util/env.c +1
-0@@ -339,6 +339,18 @@ int perf_env__kernel_is_64_bit(struct perf_env *env)return env->kernel_is_64_bit; } +bool perf_arch_is_big_endian (const char *arch)
tools/perf/util/env.c int perf_env__kernel_is_64_bit(struct perf_env *env) · line 352
Quoted 10 lines
tools/perf/util/env.c +10
-0+{ +if (!arch) +return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; + +if (str_ends_with (arch,"_be" ) || !strcmp (arch,"sparc" ) || !strcmp (arch,"sparc64" ) || + !strcmp (arch,"s390" ) || !strcmp (arch,"s390x" ) || !strcmp (arch,"powerpc" )) +return true; + +return false; +}
Does this check omit standard big-endian architectures like mips, m68k, and parisc?
Because they aren't in the allowlist, cross-architecture analysis on a MIPS kernel might return false, causing downstream consumers to be incorrectly configured in little-endian mode.
Quoted 3 lines
tools/perf/util/env.c +1
-0+int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]) {
[ ... ]
Quoted 4 lines
tools/perf/util/thread.c +0
-0
[ ... ]
tools/perf/util/thread.c static uint16_t read_proc_e_machine_for_pid(pid_t pid, uint32_t *e_flags) · line 459
Quoted 12 lines
tools/perf/util/thread.c +1
-1@@ -449,7 +449,7 @@ void thread__find_cpumode_addr_location(struct thread *thread, u64 addr, } } -static uint16_t read_proc_e_machine_for_pid (pid_t pid,uint32_t *e_flags) +static uint16_t read_proc_e_machine_for_pid (pid_t pid,uint32_t *e_flags,bool *is_big_endian) {char path[6 /* "/proc/" */ +11 /* max length of pid */ +5 /* "/exe\0" */ ];int fd;@@ -458,7 +458,8 @@ static uint16_t read_proc_e_machine_for_pid(pid_t pid, uint32_t *e_flags)snprintf (path,sizeof (path),"/proc/%d/exe" , pid); fd =open (path,O_RDONLY );
Is it possible for this open() to hang indefinitely if the /proc/%d/exe path points to a blocking FUSE filesystem or an unresponsive network mount?
Opening it without O_NONBLOCK risks hanging the perf daemon.
Quoted 6 lines
tools/perf/util/thread.c +2
-1if (fd >=0 ) { - e_machine =dso__read_e_machine (/*optional_dso=*/ NULL , fd, e_flags); + e_machine =dso__read_e_machine_endian (/*optional_dso=*/ NULL , fd, e_flags, + is_big_endian);close (fd); }
[ ... ]
tools/perf/util/thread.c uint16_t thread__e_machine(struct thread *thread, struct machine *machine, uint3 · line 501
Quoted 8 lines
tools/perf/util/thread.c +1
-0@@ -491,6 +495,7 @@ uint16_t thread__e_machine(struct thread *thread, struct machine *machine, uint3 .machine = machine, .e_flags = 0, .e_machine = EM_NONE, + .is_big_endian = false, };if (e_machine !=EM_NONE ) {
Does this fast-path return successfully without initializing the is_big_endian output parameter?
Because struct thread does not cache the endianness, any caller relying on is_big_endian might read uninitialized memory or a default value from their stack.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerPATCH v9 4/18 perf capstone: Determine architecture from e_machine
In reply to Ian RogersAvoid the use of arch string that is imprecise and use the e_machine. Do more e_machine to capstone machine translations adding MIPS and RISCV. Remove unnecessary maybe_unused annotations.
--- tools/perf/util/capstone.c | 129 ++++++++++++++++++++++++------------- 1 file changed, 85 insertions(+), 44 deletions(-)
tools/perf/util/capstone.c +85 -44
@@ -1,7 +1,19 @@// SPDX-License-Identifier: GPL-2.0 #include "capstone.h" -#include "annotate.h" + +#include <errno.h> +#include <inttypes.h> +#include <string.h> + +#include <dlfcn.h> +#include <elf.h> +#include <fcntl.h> +#include <linux/ctype.h> + +#include <capstone/capstone.h> +#include "addr_location.h" +#include "annotate.h" #include "debug.h" #include "disasm.h" #include "dso.h"
@@ -11,13 +23,6 @@#include "print_insn.h" #include "symbol.h" #include "thread.h" -#include <dlfcn.h> -#include <errno.h> -#include <fcntl.h> -#include <inttypes.h> -#include <string.h> - -#include <capstone/capstone.h> #ifdef LIBCAPSTONE_DLOPEN static void *perf_cs_dll_handle (void )
@@ -137,37 +142,67 @@ static enum cs_err perf_cs_close(csh *handle)#endif } -static int capstone_init (struct machine *machine,csh *cs_handle,bool is64, +static bool e_machine_to_capstone (uint16_t e_machine,bool is64,bool is_big_endian, +enum cs_arch *arch,enum cs_mode *mode) +{ + *mode = is_big_endian ?CS_MODE_BIG_ENDIAN :CS_MODE_LITTLE_ENDIAN ; + *mode |= is64 ?CS_MODE_64 :CS_MODE_32 ; + +switch (e_machine) { +case EM_X86_64 : +case EM_386 : + *arch =CS_ARCH_X86 ; +return true; +case EM_AARCH64 : + *arch =CS_ARCH_ARM64 ; + *mode |=CS_MODE_ARM ; +return true; +case EM_ARM : + *arch =CS_ARCH_ARM ; + *mode |=CS_MODE_ARM |CS_MODE_V8 ; +return true; +case EM_S390 : + *arch =CS_ARCH_SYSZ ; +return true; +case EM_MIPS : + *arch =CS_ARCH_MIPS ; + *mode |= is64 ?CS_MODE_MIPS64 :CS_MODE_MIPS32 ; +return true; +case EM_PPC : +case EM_PPC64 : + *arch =CS_ARCH_PPC ; +return true; +case EM_SPARC : + *arch =CS_ARCH_SPARC ; +return true; +case EM_SPARCV9 : + *arch =CS_ARCH_SPARC ; + *mode |=CS_MODE_V9 ; +return true; +case EM_RISCV : + *arch =CS_ARCH_RISCV ; + *mode |= is64 ?CS_MODE_RISCV64 :CS_MODE_RISCV32 ; +return true; +default : +return false; + } +} + +static int capstone_init (uint16_t e_machine,csh *cs_handle,bool is64,bool is_big_endian,bool disassembler_style) {enum cs_arch arch;enum cs_mode mode; -if (machine__is (machine,"x86_64" ) && is64) { - arch =CS_ARCH_X86 ; - mode =CS_MODE_64 ; - }else if (machine__normalized_is (machine,"x86" )) { - arch =CS_ARCH_X86 ; - mode =CS_MODE_32 ; - }else if (machine__normalized_is (machine,"arm64" )) { - arch =CS_ARCH_ARM64 ; - mode =CS_MODE_ARM ; - }else if (machine__normalized_is (machine,"arm" )) { - arch =CS_ARCH_ARM ; - mode =CS_MODE_ARM +CS_MODE_V8 ; - }else if (machine__normalized_is (machine,"s390" )) { - arch =CS_ARCH_SYSZ ; - mode =CS_MODE_BIG_ENDIAN ; - }else { +if (!e_machine_to_capstone (e_machine, is64, is_big_endian, &arch, &mode))return -1 ; - }if (perf_cs_open (arch, mode, cs_handle) !=CS_ERR_OK ) {pr_warning_once ("cs_open failed\n" );return -1 ; } -if (machine__normalized_is (machine,"x86" )) { +if (arch ==CS_ARCH_X86 ) {/* * In case of using capstone_init while symbol__disassemble * setting CS_OPT_SYNTAX_ATT depends if disassembler_style opts
@@ -211,29 +246,28 @@ static size_t print_insn_x86(struct thread *thread, u8 cpumode, struct cs_insn *return printed; } - -ssize_t capstone__fprintf_insn_asm (struct machine *machine__maybe_unused , -struct thread *thread__maybe_unused , -u8 cpumode__maybe_unused ,bool is64bit__maybe_unused , -const uint8_t *code__maybe_unused , -size_t code_size__maybe_unused , -uint64_t ip__maybe_unused ,int *lenp__maybe_unused , -int print_opts__maybe_unused ,FILE *fp__maybe_unused ) +ssize_t capstone__fprintf_insn_asm (struct machine *machine,struct thread *thread,u8 cpumode, +bool is64bit,const uint8_t *code,size_t code_size,uint64_t ip, +int *lenp,int print_opts,FILE *fp) {size_t printed;struct cs_insn *insn;csh cs_handle;size_t count; +bool is_big_endian = false; +uint16_t e_machine =thread__e_machine_endian (thread, machine, +/*e_flags=*/ NULL , &is_big_endian);int ret;/* TODO: Try to initiate capstone only once but need a proper place. */ - ret =capstone_init (machine, &cs_handle, is64bit, true); + ret =capstone_init (e_machine, &cs_handle, is64bit, is_big_endian, +/*disassembler_style=*/ true);if (ret <0 )return ret; count =perf_cs_disasm (cs_handle, code, code_size, ip,1 , &insn);if (count >0 ) { -if (machine__normalized_is (machine,"x86" )) +if (e_machine ==EM_X86_64 || e_machine ==EM_386 ) printed =print_insn_x86 (thread, cpumode, &insn[0 ], print_opts, fp);else printed =fprintf (fp,"%s %s" , insn[0 ].mnemonic, insn[0 ].op_str);
@@ -322,9 +356,8 @@ static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)return 0 ; } -int symbol__disassemble_capstone (const char *filename__maybe_unused , -struct symbol *sym__maybe_unused , -struct annotate_args *args__maybe_unused ) +int symbol__disassemble_capstone (const char *filename,struct symbol *sym, +struct annotate_args *args) {struct annotation *notes =symbol__annotation (sym);struct map *map = args->ms->map;
@@ -344,6 +377,8 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,char disasm_buf[512 ];struct disasm_line *dl;bool disassembler_style = false; +uint16_t e_machine; +bool is_big_endian = false;if (args->options->objdump_path)return -1 ;
@@ -373,8 +408,10 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused, !strcmp(args->options->disassembler_style, "att")) disassembler_style = true; -if (capstone_init (maps__machine (thread__maps (args->ms->thread)), &handle, is_64bit, - disassembler_style) <0 ) + e_machine =thread__e_machine_endian (args->ms->thread, +/*machine=*/ NULL , +/*e_flags=*/ NULL , &is_big_endian); +if (capstone_init (e_machine, &handle, is_64bit, is_big_endian, disassembler_style) <0 ) goto err; needs_cs_close = true;
@@ -466,6 +503,8 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused,struct disasm_line *dl;u32 *line;bool disassembler_style = false; +uint16_t e_machine; +bool is_big_endian = false;if (args->options->objdump_path)return -1 ;
@@ -484,8 +523,10 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused, !strcmp(args->options->disassembler_style, "att")) disassembler_style = true; -if (capstone_init (maps__machine (thread__maps (args->ms->thread)), &handle, is_64bit, - disassembler_style) <0 ) + e_machine =thread__e_machine_endian (args->ms->thread, +/*machine=*/ NULL , +/*e_flags=*/ NULL , &is_big_endian); +if (capstone_init (e_machine, &handle, is_64bit, is_big_endian, disassembler_style) <0 ) goto err; needs_cs_close = true;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v9 04/18] perf capstone: Determine architecture from e_machine
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Unconditional application of CS_MODE_64 and CS_MODE_32 breaks Capstone initialization for…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Unconditional application of CS_MODE_64 and CS_MODE_32 breaks Capstone initialization for architectures like ARM, ARM64, and RISC-V.
- [High] Uninitialized is_big_endian on thread cache hits causes big-endian architectures to be disassembled as little-endian.
- [Medium] Lockless multi-variable data race when caching thread e_machine and e_flags.
--
commit 438f1b068410120794eb1080db889c82f269a06e
Author: Ian Rogers <[email protected]>
perf capstone: Determine architecture from e_machine
This commit updates Capstone initialization to use e_machine instead of arch strings to determine the architecture. It also adds e_machine translations for MIPS and RISCV, and removes unnecessary maybe_unused annotations.
[Note: The report format has been altered to provide a free-form summary of the findings instead of inline diff quoting due to recitation restrictions.]
Regarding the implementation of e_machine_to_capstone(), is it intended to unconditionally set CS_MODE_64 or CS_MODE_32 in the mode parameter for all architectures?
Capstone defines CS_MODE_32 as 4 and CS_MODE_64 as 8. While these mode flags are valid for architectures like x86 and MIPS, they might not be supported for others. For instance, RISC-V uses CS_MODE_RISCV32 or CS_MODE_RISCV64, and ARM expects CS_MODE_ARM.
If CS_MODE_64 or CS_MODE_32 is combined with the architecture-specific flags for ARM, ARM64, or RISC-V, could it pass an invalid bitmask to Capstone and cause cs_open() to fail with CS_ERR_MODE?
In functions like capstone__fprintf_insn_asm(), the is_big_endian variable is initialized to false and passed by reference to thread__e_machine_endian(). If thread__e_machine_endian() finds that the architecture is already cached, it takes an early return path.
Does this early return path bypass updating the is_big_endian out-parameter? Since the thread cache only stores e_machine and e_flags, could this cause big-endian architectures to be disassembled as little-endian on cache hits because is_big_endian remains false?
Additionally, when thread__e_machine_endian() caches the architecture details, it appears to update e_machine and e_flags sequentially without a lock or memory barriers.
Could a concurrent thread observe the newly updated e_machine, take the early return path, and read a stale or uninitialized e_flags value before the first thread has completed its update?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Ian Rogers
reviewerPATCH v9 5/18 perf print_insn: Use e_machine for fallback IP length check
In reply to Ian RogersAvoid string comparisons with perf_env arch, switch to using the more precise ELF machine.
Sort header files and fix missing definitions.
--- tools/perf/util/print_insn.c | 23 ++++++++++++++--------- tools/perf/util/print_insn.h | 3 +++ 2 files changed, 17 insertions(+), 9 deletions(-)
tools/perf/util/print_insn.c +14 -9
@@ -4,19 +4,24 @@* * Author(s): Changbin Du <[email protected]> */ +#include "print_insn.h" +#include <inttypes.h> -#include <string.h> #include <stdbool.h> +#include <string.h> + +#include <dwarf-regs.h> +#include "capstone.h" #include "debug.h" +#include "dso.h" +#include "dump-insn.h" +#include "env.h" +#include "machine.h" +#include "map.h" #include "sample.h" #include "symbol.h" -#include "machine.h" #include "thread.h" -#include "print_insn.h" -#include "dump-insn.h" -#include "map.h" -#include "dso.h" size_t sample__fprintf_insn_raw (struct perf_sample *sample,FILE *fp) {
@@ -33,13 +38,13 @@ size_t sample__fprintf_insn_raw(struct perf_sample *sample, FILE *fp)static bool is64bitip (struct machine *machine,struct addr_location *al) {const struct dso *dso = al->map ?map__dso (al->map) :NULL ; +uint16_t e_machine;if (dso)return dso__is_64_bit (dso); -return machine__is (machine,"x86_64" ) || -machine__normalized_is (machine,"arm64" ) || -machine__normalized_is (machine,"s390" ); + e_machine =perf_env__e_machine (machine->env,/*e_flags=*/ NULL ); +return e_machine ==EM_X86_64 || e_machine ==EM_AARCH64 || e_machine ==EM_S390 ; } ssize_t fprintf_insn_asm(struct machine *machine, struct thread *thread, u8 cpumode,
tools/perf/util/print_insn.h +3 -0
@@ -5,6 +5,9 @@#include <stddef.h> #include <stdio.h> +#include <linux/types.h> + +struct addr_location ;struct perf_sample ;struct thread ;struct machine ;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v9 6/18 perf symbol: Avoid use of machine__is
In reply to Ian RogersSwitch to using the ELF machine from the dso or running machine rather than the machine perf_env arch that may fall back on EM_HOST. This also avoids potentially imprecise string comparisons.
--- tools/perf/util/symbol.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
tools/perf/util/symbol.c +22 -6
@@ -851,6 +851,23 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)return count; } +static uint16_t machine_or_dso_e_machine (struct machine *machine,struct dso *dso) +{ +uint16_t e_machine =EM_NONE ; +/* DSO should be most accurate */ +if (dso) + e_machine =dso__e_machine (dso, machine,/*e_flags=*/ NULL ); + +if (e_machine !=EM_NONE ) +return e_machine; + +/* Check the global environment next. */ +if (machine && machine->env && machine->env->e_machine !=EM_NONE ) +return machine->env->e_machine; + +return perf_env__e_machine (machine ? machine->env :NULL ,/*e_flags=*/ NULL ); +} +/* * Split the symbols into maps, making sure there are no overlaps, i.e. the * kernel range is broken in several maps, named [kernel].N, as we don't have
@@ -866,14 +883,13 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,struct rb_root_cached *root =dso__symbols (dso);struct rb_node *next =rb_first_cached (root);int kernel_range =0 ; -bool x86_64; +uint16_t e_machine =EM_NONE ;if (!kmaps)return -1 ; machine =maps__machine (kmaps); - - x86_64 =machine__is (machine,"x86_64" ); + e_machine =machine_or_dso_e_machine (machine, dso);while (next) {char *module;
@@ -925,7 +941,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,*/ pos->start =map__map_ip (curr_map, pos->start); pos->end =map__map_ip (curr_map, pos->end); - }else if (x86_64 &&is_entry_trampoline (pos->name)) { + }else if (e_machine ==EM_X86_64 &&is_entry_trampoline (pos->name)) {/* * These symbols are not needed anymore since the * trampoline maps refer to the text section and it's
@@ -1428,7 +1444,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map,free (new_node); } -if (machine__is (machine,"x86_64" )) { +if (machine_or_dso_e_machine (machine, dso) ==EM_X86_64 ) {u64 addr;/*
@@ -1716,7 +1732,7 @@ int dso__load(struct dso *dso, struct map *map) ret =dso__load_guest_kernel_sym (dso, map); machine =maps__machine (map__kmaps (map)); -if (machine__is (machine,"x86_64" )) +if (machine_or_dso_e_machine (machine, dso) ==EM_X86_64 )machine__map_x86_64_entry_trampolines (machine, dso); goto out; }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v9 06/18] perf symbol: Avoid use of machine__is
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The patch completely fails to read the ELF machine architecture from the DSO for kernel objects…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch completely fails to read the ELF machine architecture from the DSO for kernel objects, introducing a cross-architecture analysis regression. --
commit 01fba2f5fba1a6782bc236fbef6a297cb4c04daf
Author: Ian Rogers <[email protected]>
perf symbol: Avoid use of machine__is
This commit switches to using the ELF machine from the dso or running machine rather than the machine perf_env arch to avoid falling back on EM_HOST and imprecise string comparisons.
Quoted 4 lines
tools/perf/util/symbol.c +0
-0
[ ... ]
tools/perf/util/symbol.c static int dso__load_kcore(struct dso *dso, struct map *map, · line 1447
Quoted 6 lines
tools/perf/util/symbol.c +1
-1@@ -1428,7 +1444,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map,free (new_node); } -if (machine__is (machine,"x86_64" )) { +if (machine_or_dso_e_machine (machine, dso) ==EM_X86_64 ) {
Does this actually avoid falling back to EM_HOST for kernel objects?
Looking at dso__e_machine_endian(), kernel DSOs (like DSO_BINARY_TYPE__VMLINUX, KALLSYMS, and KCORE) skip reading the ELF header entirely and immediately return perf_env__e_machine(machine->env).
Additionally, at this point in dso__load_kcore(), the DSO binary type has not been set yet. It is set later in the function via dso__set_binary_type(). Because of this, it returns EM_NONE, causing machine_or_dso_e_machine() to also fall back to perf_env__e_machine().
Since perf_env__e_machine_nocache() defaults to EM_HOST if the architecture is missing, analyzing a non-x86 perf.data file missing an architecture string on an x86_64 host will evaluate to EM_X86_64.
Previously, machine__is() used perf_env__raw_arch(), which safely returned "unknown" if the architecture was missing, preventing incorrect matches.
Could this change incorrectly trigger x86_64-specific trampoline mappings for non-x86 kernel objects?
Quoted 4 lines
tools/perf/util/symbol.c +0
-0u64 addr;/* * If one of the corresponding symbols is there, assume the
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Ian Rogers
reviewerPATCH v9 7/18 perf machine: Use perf_env e_machine rather than arch
In reply to Ian RogersThe arch string is derived from uname and may be normalized causing potential differences meaning the ELF machine can be more precise. Reduce the scope of machine__is as often it is better to use a thread for the e_machine rather than the machine. Switch from string to ELF machine constant comparisons.
--- tools/perf/util/machine.c | 25 ++++++++----------------- tools/perf/util/machine.h | 2 -- 2 files changed, 8 insertions(+), 19 deletions(-)
tools/perf/util/machine.c +8 -17
@@ -1611,10 +1611,15 @@ static bool machine__uses_kcore(struct machine *machine)return dsos__for_each_dso (&machine->dsos, machine__uses_kcore_cb,NULL ) !=0 ? true : false; } +static bool machine__is (struct machine *machine,uint16_t e_machine) +{ +return machine &&perf_env__e_machine (machine->env,NULL ) == e_machine; +} +static bool perf_event__is_extra_kernel_mmap (struct machine *machine,struct extra_kernel_map *xm) { -return machine__is (machine,"x86_64" ) && +return machine__is (machine,EM_X86_64 ) &&is_entry_trampoline (xm->name); }
@@ -2770,7 +2775,7 @@ static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,static u64 get_leaf_frame_caller (struct perf_sample *sample,struct thread *thread,int usr_idx) { -if (machine__normalized_is (maps__machine (thread__maps (thread)),"arm64" )) +if (thread__e_machine (thread,/*machine=*/ NULL ,/*e_flags=*/ NULL ) ==EM_AARCH64 )return get_leaf_frame_caller_aarch64 (sample, thread, usr_idx);else return 0 ;
@@ -3141,20 +3146,6 @@ int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,return 0 ; } -/* -* Compares the raw arch string. N.B. see instead perf_env__arch() or -* machine__normalized_is() if a normalized arch is needed. -*/ -bool machine__is (struct machine *machine,const char *arch) -{ -return machine && !strcmp (perf_env__raw_arch (machine->env), arch); -} - -bool machine__normalized_is (struct machine *machine,const char *arch) -{ -return machine && !strcmp (perf_env__arch (machine->env), arch); -} -int machine__nr_cpus_avail (struct machine *machine) {return machine ?perf_env__nr_cpus_avail (machine->env) :0 ;
@@ -3181,7 +3172,7 @@ int machine__get_kernel_start(struct machine *machine)* start of kernel text, but still above 2^63. So leave * kernel_start = 1ULL << 63 for x86_64. */ -if (!err && !machine__is (machine,"x86_64" )) +if (!err && !machine__is (machine,EM_X86_64 )) machine->kernel_start =map__start (map); }return err;
tools/perf/util/machine.h +0 -2
@@ -227,8 +227,6 @@ static inline bool machine__is_host(struct machine *machine) }bool machine__is_lock_function (struct machine *machine,u64 addr); -bool machine__is (struct machine *machine,const char *arch); -bool machine__normalized_is (struct machine *machine,const char *arch);int machine__nr_cpus_avail (struct machine *machine);struct thread *machine__findnew_thread (struct machine *machine,pid_t pid,pid_t tid);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v9 07/18] perf machine: Use perf_env e_machine rather than arch
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] machine__is() incorrectly falls back to the compile-time host architecture when the machine's…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] machine__is() incorrectly falls back to the compile-time host architecture when the machine's environment is NULL, breaking non-x86_64 guest kernel memory map handling.
- [High] get_leaf_frame_caller() delegates to thread__e_machine(), which unconditionally falls back to the host architecture if maps fail, breaking offline cross-architecture callchain analysis. --
commit e3337f1222c33fd5dc5c1fda6cc5b1ac0b73b28e
Author: Ian Rogers <[email protected]>
perf machine: Use perf_env e_machine rather than arch
This commit modifies perf's machine parsing to use ELF machine constants instead of architecture strings. This is intended to provide more precise comparisons by using the thread's e_machine and avoids issues with normalized architecture strings.
tools/perf/util/machine.c static bool machine__uses_kcore(struct machine *machine) · line 1617
Quoted 12 lines
tools/perf/util/machine.c +4
-0@@ -1611,10 +1611,15 @@ static bool machine__uses_kcore(struct machine *machine)return dsos__for_each_dso (&machine->dsos, machine__uses_kcore_cb,NULL ) !=0 ? true : false; } +static bool machine__is (struct machine *machine,uint16_t e_machine) +{ +return machine &&perf_env__e_machine (machine->env,NULL ) == e_machine; +}
Could this fallback to the host architecture when dealing with guest machines that don't have their environment populated?
Guest machines initialized via machines__add do not have their machine->env populated (machine->env == NULL). Previously, machine__is(..., "x86_64") used perf_env__raw_arch(NULL), which returned "unknown", safely evaluating to false.
The new implementation uses perf_env__e_machine(NULL, NULL), which evaluates to perf_env__e_machine_nocache() and unconditionally returns EM_HOST (the architecture the perf binary was compiled for).
When analyzing non-x86_64 guests on an x86_64 host, machine__is(guest, EM_X86_64) evaluates to true.
[ ... ]
tools/perf/util/machine.c static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread, · line 2781
Quoted 9 lines
tools/perf/util/machine.c +1
-1@@ -2770,7 +2775,7 @@ static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,static u64 get_leaf_frame_caller (struct perf_sample *sample,struct thread *thread,int usr_idx) { -if (machine__normalized_is (maps__machine (thread__maps (thread)),"arm64" )) +if (thread__e_machine (thread,/*machine=*/ NULL ,/*e_flags=*/ NULL ) ==EM_AARCH64 )return get_leaf_frame_caller_aarch64 (sample, thread, usr_idx);else return 0 ;
Will this break offline cross-architecture callchain analysis for AArch64?
Previously, get_leaf_frame_caller() used machine__normalized_is(), which correctly checked the session's recorded perf_env (derived from the perf.data header) to determine the architecture.
The new code relies on thread__e_machine() returning EM_AARCH64. If thread__e_machine() cannot determine the architecture from the thread's DSOs (for example, due to stripped binaries, kernel threads, or JIT code), it falls back to read_proc_e_machine_for_pid().
Since offline perf.data files are not part of a live session, this bypasses /proc reads and unconditionally defaults to EM_HOST (the compile-time architecture of the perf binary). As a result, analyzing an AArch64 perf.data file on an x86_64 host will silently fall back to EM_X86_64, bypassing get_leaf_frame_caller_aarch64() and preventing leaf frame unwinding.
[ ... ]
tools/perf/util/machine.c int machine__get_kernel_start(struct machine *machine) · line 3178
Quoted 9 lines
tools/perf/util/machine.c +1
-1@@ -3181,7 +3172,7 @@ int machine__get_kernel_start(struct machine *machine)* start of kernel text, but still above 2^63. So leave * kernel_start = 1ULL << 63 for x86_64. */ -if (!err && !machine__is (machine,"x86_64" )) +if (!err && !machine__is (machine,EM_X86_64 )) machine->kernel_start =map__start (map); }return err;
If machine__is() erroneously returns true for non-x86_64 guests (as noted above), could this leave machine->kernel_start at 1ULL << 63 and corrupt kernel address resolution for cross-architecture guest analysis?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Ian Rogers
reviewerPATCH v9 8/18 perf sample-raw: Use perf_env e_machine rather than arch
In reply to Ian RogersUse the e_machine rather than the arch to determine S390 and x86 types.
--- tools/perf/util/sample-raw.c | 21 +++++++++++---------- tools/perf/util/sample-raw.h | 6 +++++- 2 files changed, 16 insertions(+), 11 deletions(-)
tools/perf/util/sample-raw.c +11 -10
@@ -1,11 +1,12 @@/* SPDX-License-Identifier: GPL-2.0 */ +#include "sample-raw.h" -#include <string.h> +#include <elf.h> #include <linux/string.h> -#include "evlist.h" +#include "env.h" +#include "evlist.h" #include "header.h" -#include "sample-raw.h" #include "session.h" /*
@@ -14,14 +15,14 @@*/ void evlist__init_trace_event_sample_raw (struct evlist *evlist,struct perf_env *env) { -const char *arch_pf =perf_env__arch (env); -const char *cpuid =perf_env__cpuid (env); +uint16_t e_machine =perf_env__e_machine (env,/*e_eflags=*/ NULL ); -if (arch_pf && !strcmp ("s390" , arch_pf)) +if (e_machine ==EM_S390 ) { evlist->trace_event_sample_raw = evlist__s390_sample_raw; -else if (arch_pf && !strcmp ("x86" , arch_pf) && - cpuid &&strstarts (cpuid,"AuthenticAMD" ) && -evlist__has_amd_ibs (evlist)) { - evlist->trace_event_sample_raw = evlist__amd_sample_raw; + }else if (e_machine ==EM_X86_64 || e_machine ==EM_386 ) { +const char *cpuid =perf_env__cpuid (env); + +if (cpuid &&strstarts (cpuid,"AuthenticAMD" ) &&evlist__has_amd_ibs (evlist)) + evlist->trace_event_sample_raw = evlist__amd_sample_raw; } }
tools/perf/util/sample-raw.h +5 -1
@@ -2,7 +2,10 @@#ifndef __SAMPLE_RAW_H#define __SAMPLE_RAW_H 1 +#include <stdbool.h> +struct evlist ; +struct perf_env ;union perf_event ;struct perf_sample ;
@@ -12,4 +15,5 @@ bool evlist__has_amd_ibs(struct evlist *evlist);void evlist__amd_sample_raw (struct evlist *evlist,union perf_event *event,struct perf_sample *sample);void evlist__init_trace_event_sample_raw (struct evlist *evlist,struct perf_env *env); -#endif /* __PERF_EVLIST_H */ + +#endif /* __SAMPLE_RAW_H */
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v9 9/18 perf sort: Use perf_env e_machine rather than arch
In reply to Ian RogersUse the e_machine rather than the arch to determine x86 or PPC types.
--- tools/perf/util/sort.c | 58 +++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 26 deletions(-)
tools/perf/util/sort.c +32 -26
@@ -1,40 +1,45 @@// SPDX-License-Identifier: GPL-2.0 +#include "sort.h" +#include <ctype.h> #include <errno.h> #include <inttypes.h> -#include <regex.h> #include <stdlib.h> + +#include <elf.h> +#include <linux/kernel.h> #include <linux/mman.h> +#include <linux/string.h> #include <linux/time64.h> + +#include <regex.h> + +#include "annotate-data.h" +#include "annotate.h" +#include "branch.h" +#include "cacheline.h" +#include "cgroup.h" +#include "comm.h" #include "debug.h" #include "dso.h" -#include "sort.h" +#include "event.h" +#include "evlist.h" +#include "evsel.h" #include "hist.h" -#include "cacheline.h" -#include "comm.h" +#include "machine.h" #include "map.h" -#include "maps.h" -#include "symbol.h" #include "map_symbol.h" -#include "branch.h" -#include "thread.h" -#include "evsel.h" -#include "evlist.h" -#include "srcline.h" -#include "strlist.h" -#include "strbuf.h" +#include "maps.h" #include "mem-events.h" #include "mem-info.h" -#include "annotate.h" -#include "annotate-data.h" -#include "event.h" -#include "time-utils.h" -#include "cgroup.h" -#include "machine.h" #include "session.h" +#include "srcline.h" +#include "strbuf.h" +#include "strlist.h" +#include "symbol.h" +#include "thread.h" +#include "time-utils.h" #include "trace-event.h" -#include <linux/kernel.h> -#include <linux/string.h> #ifdef HAVE_LIBTRACEEVENT #include <event-parse.h>
@@ -2673,9 +2678,10 @@ struct sort_dimension {static int arch_support_sort_key (const char *sort_key,struct perf_env *env) { -const char *arch =perf_env__arch (env); +uint16_t e_machine =perf_env__e_machine (env,/*e_eflags=*/ NULL ); -if (!strcmp ("x86" , arch) || !strcmp ("powerpc" , arch)) { +if (e_machine ==EM_X86_64 || e_machine ==EM_386 || e_machine ==EM_PPC64 || + e_machine ==EM_PPC ) {if (!strcmp (sort_key,"p_stage_cyc" ))return 1 ;if (!strcmp (sort_key,"local_p_stage_cyc" ))
@@ -2686,14 +2692,14 @@ static int arch_support_sort_key(const char *sort_key, struct perf_env *env)static const char *arch_perf_header_entry (const char *se_header,struct perf_env *env) { -const char *arch =perf_env__arch (env); +uint16_t e_machine =perf_env__e_machine (env,/*e_eflags=*/ NULL ); -if (!strcmp ("x86" , arch)) { +if (e_machine ==EM_X86_64 || e_machine ==EM_386 ) {if (!strcmp (se_header,"Local Pipeline Stage Cycle" ))return "Local Retire Latency" ;else if (!strcmp (se_header,"Pipeline Stage Cycle" ))return "Retire Latency" ; - }else if (!strcmp ("powerpc" , arch)) { + }else if (e_machine ==EM_PPC64 || e_machine ==EM_PPC ) {if (!strcmp (se_header,"Local INSTR Latency" ))return "Finish Cyc" ;else if (!strcmp (se_header,"INSTR Latency" ))
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v9 10/18 perf arch common: Use perf_env e_machine rather than arch
In reply to Ian RogersUse the e_machine rather than arch string matching.
--- tools/perf/arch/common.c | 62 ++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 22 deletions(-)
tools/perf/arch/common.c +40 -22
@@ -1,13 +1,18 @@// SPDX-License-Identifier: GPL-2.0 +#include "common.h" +#include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> + +#include <linux/zalloc.h> #include <unistd.h> -#include "common.h" -#include "../util/env.h" + +#include <dwarf-regs.h> +#include "../util/debug.h" -#include <linux/zalloc.h> +#include "../util/env.h" static const char *const arc_triplets[] = {"arc-linux-" ,
@@ -145,7 +150,8 @@ static int perf_env__lookup_binutils_path(struct perf_env *env, const char *name, char **path) { int idx; -const char *arch =perf_env__arch (env), *cross_env; +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); +const char *cross_env;const char *const *path_list;char *buf =NULL ;
@@ -153,7 +159,7 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,* We don't need to try to find objdump path for native system. * Just use default binutils path (e.g.: "objdump"). */ -if (!strcmp (perf_env__arch (NULL ), arch)) +if (e_machine ==EM_HOST ) goto out; cross_env =getenv ("CROSS_COMPILE" );
@@ -170,30 +176,42 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,zfree (&buf); } -if (!strcmp (arch,"arc" )) +switch (e_machine) { +case EM_ARC : path_list = arc_triplets; -else if (!strcmp (arch,"arm" )) +break ; +case EM_ARM : path_list = arm_triplets; -else if (!strcmp (arch,"arm64" )) +break ; +case EM_AARCH64 : path_list = arm64_triplets; -else if (!strcmp (arch,"powerpc" )) +break ; +case EM_PPC : +case EM_PPC64 : path_list = powerpc_triplets; -else if (!strcmp (arch,"riscv32" )) - path_list = riscv32_triplets; -else if (!strcmp (arch,"riscv64" )) - path_list = riscv64_triplets; -else if (!strcmp (arch,"sh" )) +break ; +case EM_RISCV : + path_list =perf_env__kernel_is_64_bit (env) ? riscv64_triplets : riscv32_triplets; +break ; +case EM_SH : path_list = sh_triplets; -else if (!strcmp (arch,"s390" )) +break ; +case EM_S390 : path_list = s390_triplets; -else if (!strcmp (arch,"sparc" )) +break ; +case EM_SPARC : +case EM_SPARCV9 : path_list = sparc_triplets; -else if (!strcmp (arch,"x86" )) +break ; +case EM_X86_64 : +case EM_386 : path_list = x86_triplets; -else if (!strcmp (arch,"mips" )) +break ; +case EM_MIPS : path_list = mips_triplets; -else { -ui__error ("binutils for %s not supported.\n" , arch); +break ; +default : +ui__error ("binutils for %s not supported.\n" ,perf_env__arch (env)); goto out_error; }
@@ -202,7 +220,7 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,ui__error ("Please install %s for %s.\n" "You can add it to PATH, set CROSS_COMPILE or " "override the default using --%s.\n" , - name, arch, name); + name,perf_env__arch (env), name); goto out_error; }
@@ -237,5 +255,5 @@ int perf_env__lookup_objdump(struct perf_env *env, char **path)*/ bool perf_env__single_address_space (struct perf_env *env) { -return strcmp (perf_env__arch (env),"sparc" ); +return perf_env__e_machine (env,/*e_flags=*/ NULL ) !=EM_SPARCV9 ; }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v9 10/18] perf arch common: Use perf_env e_machine rather than arch
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Native objdump fallback breaks for cross-bitness profiling (e.g., analyzing a 32-bit profile on…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Native objdump fallback breaks for cross-bitness profiling (e.g., analyzing a 32-bit profile on a 64-bit host).
- [Medium] Unintended address space handling change for 32-bit SPARC.
--
commit ba683ab91ed0ab22877903f737b0057971f914b1
Author: Ian Rogers <[email protected]>
perf arch common: Use perf_env e_machine rather than arch
This commit updates the perf architecture matching logic to use e_machine values instead of architecture strings.
Quoted 4 lines
tools/perf/arch/common.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/arch/common.c +0
-0@@ -145,7 +150,8 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,
[ ... ]
tools/perf/arch/common.c static int perf_env__lookup_binutils_path(struct perf_env *env, · line 163
Quoted 7 lines
tools/perf/arch/common.c +1
-1/* * We don't need to try to find objdump path for native system. * Just use default binutils path (e.g.: "objdump"). */ -if (!strcmp (perf_env__arch (NULL ), arch)) +if (e_machine ==EM_HOST ) goto out;
Does this change break cross-bitness profiling on the same architecture family?
Previously, perf_env__arch() normalized both 32-bit (EM_386) and 64-bit (EM_X86_64) ELF machine types into a single family string like x86. This allowed a 64-bit host to fall back to the native objdump when analyzing a 32-bit profile.
With the new check, EM_386 is not equal to EM_X86_64 for a 32-bit profile on a 64-bit host, causing the check to fail. The code then attempts a triplet lookup, such as i686-pc-linux-gnu-objdump.
If these specific prefixed symlinks are not installed, will this trigger a fatal ui__error and abort perf annotate, even though the native objdump inherently supports multilib and 32-bit binaries?
[ ... ]
tools/perf/arch/common.c int perf_env__lookup_objdump(struct perf_env *env, char **path) · line 259
Quoted 7 lines
tools/perf/arch/common.c +1
-1@@ -237,5 +255,5 @@ int perf_env__lookup_objdump(struct perf_env *env, char **path)*/ bool perf_env__single_address_space (struct perf_env *env) { -return strcmp (perf_env__arch (env),"sparc" ); +return perf_env__e_machine (env,/*e_flags=*/ NULL ) !=EM_SPARCV9 ; }
Is this an intended logic change for 32-bit SPARC?
Before this patch, perf_env__arch() mapped both 32-bit (EM_SPARC) and 64-bit (EM_SPARCV9) to sparc, so the strcmp returned 0 (false) for both variants.
Now, the function evaluates to false for 64-bit SPARC (EM_SPARCV9) but evaluates to true for 32-bit SPARC (EM_SPARC).
Could this unexpectedly alter address space separation for 32-bit SPARC profiles?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Ian Rogers
reviewerPATCH v9 11/18 perf header: In print_pmu_caps use perf_env e_machine
In reply to Ian RogersSwitch from arch to e_machine in print_pmu_caps.
--- tools/perf/util/header.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
tools/perf/util/header.c +4 -3
@@ -2348,15 +2348,16 @@ static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp)static void print_pmu_caps (struct feat_fd *ff,FILE *fp) {struct perf_env *env = &ff->ph->env; -struct pmu_caps *pmu_caps; +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL );for (int i =0 ; i < env->nr_pmus_with_caps; i++) { - pmu_caps = &env->pmu_caps[i]; +struct pmu_caps *pmu_caps = &env->pmu_caps[i]; +__print_pmu_caps (fp, pmu_caps->nr_caps, pmu_caps->caps, pmu_caps->pmu_name); } -if (strcmp (perf_env__arch (env),"x86" ) ==0 && +if ((e_machine ==EM_X86_64 || e_machine ==EM_386 ) &&perf_env__has_pmu_mapping (env,"ibs_op" )) {char *max_precise =perf_env__find_pmu_cap (env,"cpu" ,"max_precise" );
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v9 12/18 perf c2c: Use perf_env e_machine rather than arch
In reply to Ian RogersUse the e_machine rather than arch string matching for AARCH64.
Add include of dwarf-regs.h in case the EM_AARCH64 isn't defined, sort the headers given this include.
--- tools/perf/builtin-c2c.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-)
tools/perf/builtin-c2c.c +22 -18
@@ -12,41 +12,45 @@*/ #include <errno.h> #include <inttypes.h> + +#include <asm/bug.h> #include <linux/compiler.h> #include <linux/err.h> #include <linux/kernel.h> #include <linux/stringify.h> #include <linux/zalloc.h> -#include <asm/bug.h> #include <sys/param.h> -#include "debug.h" -#include "builtin.h" + +#include <dwarf-regs.h> #include <perf/cpumap.h> #include <subcmd/pager.h> #include <subcmd/parse-options.h> -#include "map_symbol.h" -#include "mem-events.h" -#include "session.h" -#include "hist.h" -#include "sort.h" -#include "tool.h" + +#include "builtin.h" #include "cacheline.h" #include "data.h" +#include "debug.h" #include "event.h" #include "evlist.h" #include "evsel.h" -#include "ui/browsers/hists.h" -#include "thread.h" -#include "mem2node.h" +#include "hist.h" +#include "map_symbol.h" +#include "mem-events.h" #include "mem-info.h" -#include "symbol.h" -#include "ui/ui.h" -#include "ui/progress.h" +#include "mem2node.h" #include "pmus.h" +#include "session.h" +#include "sort.h" #include "string2.h" -#include "util/util.h" -#include "util/symbol.h" +#include "symbol.h" +#include "thread.h" +#include "tool.h" +#include "ui/browsers/hists.h" +#include "ui/progress.h" +#include "ui/ui.h" #include "util/annotate.h" +#include "util/symbol.h" +#include "util/util.h" struct c2c_hists {struct hists hists;
@@ -3202,7 +3206,7 @@ static int perf_c2c__report(int argc, const char **argv)* default display type. */ if (!display) { -if (!strcmp (perf_env__arch (env),"arm64" )) +if (perf_env__e_machine (env,/*e_flags=*/ NULL ) ==EM_AARCH64 ) display ="peer" ;else display ="tot" ;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v9 13/18 perf lock-contention: Use perf_env e_machine rather than arch
In reply to Ian RogersUse the e_machine rather than arch string matching for powerpc.
--- tools/perf/util/lock-contention.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/lock-contention.c +3 -3
@@ -104,7 +104,8 @@ bool match_callstack_filter(struct machine *machine, u64 *callstack, int max_stastruct map *kmap;struct symbol *sym;u64 ip; -const char *arch =perf_env__arch (machine->env); +uint16_t e_machine =perf_env__e_machine (machine->env,/*e_flags=*/ NULL ); +bool is_powerpc = e_machine ==EM_PPC64 || e_machine ==EM_PPC ;if (list_empty (&callstack_filters))return true;
@@ -125,8 +126,7 @@ bool match_callstack_filter(struct machine *machine, u64 *callstack, int max_sta* incase first or second callstack index entry has 0 * address for powerpc. */ -if (!callstack || (!callstack[i] && (strcmp (arch,"powerpc" ) || - (i !=1 && i !=2 )))) +if (!callstack || (!callstack[i] && (!is_powerpc || (i !=1 && i !=2 ))))break ; ip = callstack[i];
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v9 14/18 perf env: Refactor perf_env__arch_strerrno
In reply to Ian Rogersperf_env__arch_strerrno is only available with libtraceevent so hide the declaration if no libtraceevent.
The previous approach maps an architecture string to a function pointer to a function that takes an int errno values and returns a string. The new approach takes an e_machine and an errno value and returns a string.
As the only call site is in builtin-trace.c, the e_machine is already present and potentially more specific than the perf_env arch string that is a single global value.
The major complication in this approach is having the shell script that generates the C code map a linux directory name to the matching ELF machine constants.
--- tools/perf/builtin-trace.c | 7 ++-- tools/perf/trace/beauty/arch_errno_names.sh | 40 ++++++++++++++++++--- tools/perf/util/env.c | 13 +++---- tools/perf/util/env.h | 7 ++-- 4 files changed, 46 insertions(+), 21 deletions(-)
tools/perf/builtin-trace.c +4 -3
@@ -3008,9 +3008,8 @@ static int trace__sys_exit(struct trace *trace, struct evsel *evsel, }else if (ret <0 ) { errno_print: {char bf[STRERR_BUFSIZE ]; -struct perf_env *env =evsel__env (evsel) ?: &trace->host_env;const char *emsg =str_error_r (-ret, bf,sizeof (bf)); -const char *e =perf_env__arch_strerrno (env, err); +const char *e =perf_env__arch_strerrno (e_machine, err);fprintf (trace->output,"-1 %s (%s)" , e, emsg); }
@@ -4890,7 +4889,9 @@ static size_t syscall__dump_stats(struct trace *trace, int e_machine, FILE *fp,for (e =0 ; e < stats->max_errno; ++e) {if (stats->errnos[e] !=0 ) -fprintf (fp,"\t\t\t\t%s: %d\n" ,perf_env__arch_strerrno (trace->host->env, e +1 ), stats->errnos[e]); +fprintf (fp,"\t\t\t\t%s: %d\n" , +perf_env__arch_strerrno (e_machine, e +1 ), + stats->errnos[e]); } } lines++;
tools/perf/trace/beauty/arch_errno_names.sh +35 -5
@@ -52,21 +52,49 @@ process_arch() |IFS=, create_errno_lookup_func "$arch" } +arch_to_e_machine() +{ + case "$1" in + alpha) printf '\tcase EM_ALPHA:\n' ;; + arc) printf '\tcase EM_ARC:\n' ;; + arm) printf '\tcase EM_ARM:\n' ;; + arm64) printf '\tcase EM_AARCH64:\n' ;; + csky) printf '\tcase EM_CSKY:\n' ;; + hexagon) printf '\tcase EM_HEXAGON:\n' ;; + loongarch) printf '\tcase EM_LOONGARCH:\n' ;; + microblaze) printf '\tcase EM_MICROBLAZE:\n' ;; + mips) printf '\tcase EM_MIPS:\n' ;; + parisc) printf '\tcase EM_PARISC:\n' ;; + powerpc) printf '\tcase EM_PPC:\n\tcase EM_PPC64:\n' ;; + riscv) printf '\tcase EM_RISCV:\n' ;; + s390) printf '\tcase EM_S390:\n' ;; + sh) printf '\tcase EM_SH:\n' ;; + sparc) printf '\tcase EM_SPARC:\n\tcase EM_SPARCV9:\n' ;; + x86) printf '\tcase EM_386:\n\tcase EM_X86_64:\n' ;; + xtensa) printf '\tcase EM_XTENSA:\n' ;; + esac +} + create_arch_errno_table_func() { archlist="$1" default="$2" - printf 'static arch_syscalls__strerrno_t *\n' - printf 'arch_syscalls__strerrno_function(const char *arch)\n' + printf 'static const char *\n' + printf 'arch_syscalls__strerrno(uint16_t e_machine, int err)\n' printf '{\n' + printf '\tswitch (e_machine) {\n' for arch in $archlist; do arch_str=$(arch_string "$arch") - printf '\tif (!strcmp(arch, "%s"))\n' "$arch_str" - printf '\t\treturn errno_to_name__%s;\n' "$arch_str" + ems=$(arch_to_e_machine "$arch_str") + if [ -n "$ems" ]; then + printf '%s\n' "$ems" + printf '\t\treturn errno_to_name__%s(err);\n' "$arch_str" + fi done arch_str=$(arch_string "$default") - printf '\treturn errno_to_name__%s;\n' "$arch_str" + printf '\tdefault:\n\t\treturn errno_to_name__%s(err);\n' "$arch_str" + printf '\t}\n' printf '}\n' }
@@ -74,6 +102,8 @@ cat <<EoHEADER /* SPDX-License-Identifier: GPL-2.0 */ #include <string.h> +#include <stdint.h> +#include <elf.h> EoHEADER
tools/perf/util/env.c +4 -9
@@ -798,17 +798,12 @@ const char *perf_env__arch(struct perf_env *env)#include "trace/beauty/arch_errno_names.c" #endif -const char *perf_env__arch_strerrno (struct perf_env *env__maybe_unused ,int err__maybe_unused ) -{#if defined(HAVE_LIBTRACEEVENT ) -if (env->arch_strerrno ==NULL ) - env->arch_strerrno =arch_syscalls__strerrno_function (perf_env__arch (env)); - -return env->arch_strerrno ? env->arch_strerrno (err) :"no arch specific strerrno function" ; -#else -return "!HAVE_LIBTRACEEVENT" ; -#endif +const char *perf_env__arch_strerrno (uint16_t e_machine,int err) +{ +return arch_syscalls__strerrno (e_machine, err); } +#endif const char *perf_env__cpuid (struct perf_env *env) {
tools/perf/util/env.h +3 -4
@@ -67,8 +67,6 @@ struct cpu_domain_map {struct domain_info **domains; }; -typedef const char *(arch_syscalls__strerrno_t )(int err); -struct perf_env {char *hostname;char *os_release;
@@ -158,7 +156,6 @@ struct perf_env {*/ bool enabled; } clock; -arch_syscalls__strerrno_t *arch_strerrno; };enum perf_compress_type {
@@ -191,7 +188,9 @@ void cpu_cache_level__free(struct cpu_cache_level *cache);uint16_t perf_env__e_machine_nocache (struct perf_env *env,uint32_t *e_flags);uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env); -const char *perf_env__arch_strerrno (struct perf_env *env,int err); +#if defined(HAVE_LIBTRACEEVENT ) +const char *perf_env__arch_strerrno (uint16_t e_machine,int err); +#endif const char *perf_env__cpuid (struct perf_env *env);const char *perf_env__raw_arch (struct perf_env *env);int perf_env__nr_cpus_avail (struct perf_env *env);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v9 14/18] perf env: Refactor perf_env__arch_strerrno
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Cross-architecture analysis regression in errno string decoding for global system summaries. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Cross-architecture analysis regression in errno string decoding for global system summaries. --
commit db5c373c1c19f2552fd69fd048980560d992a842
Author: Ian Rogers <[email protected]>
perf env: Refactor perf_env__arch_strerrno
This commit modifies perf_env__arch_strerrno to take an e_machine constant instead of an architecture string and updates callers in builtin-trace.c. It also adds a helper script to map Linux directory names to ELF machine constants.
Quoted 4 lines
tools/perf/builtin-trace.c +0
-0
[ ... ]
tools/perf/builtin-trace.c static size_t syscall__dump_stats(struct trace *trace, int e_machine, FILE *fp, · line 4897
Quoted 11 lines
tools/perf/builtin-trace.c +3
-1@@ -4890,7 +4889,9 @@ static size_t syscall__dump_stats(struct trace *trace, int e_machine, FILE *fp,for (e =0 ; e < stats->max_errno; ++e) {if (stats->errnos[e] !=0 ) -fprintf (fp,"\t\t\t\t%s: %d\n" ,perf_env__arch_strerrno (trace->host->env, e +1 ), stats->errnos[e]); +fprintf (fp,"\t\t\t\t%s: %d\n" , +perf_env__arch_strerrno (e_machine, e +1 ), + stats->errnos[e]); } } lines++;
How does this impact cross-architecture analysis for global system summaries?
Prior to this patch, syscall__dump_stats() used the target environment (trace->host->env) to decode errno strings.
By switching to the e_machine argument, if the caller for global system summaries passes a hardcoded EM_HOST (or an equivalent host-specific default), will cross-architecture global summaries now incorrectly decode errnos using the host architecture's mappings instead of the target's?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Ian Rogers
reviewerPATCH v9 15/18 perf env: Remove unused perf_env__raw_arch
In reply to Ian RogersThe switch to using e_machine has made the perf_env__raw_arch function unused so remove it.
--- tools/perf/util/env.c | 18 ------------------ tools/perf/util/env.h | 1 - 2 files changed, 19 deletions(-)
tools/perf/util/env.c +0 -18
@@ -463,19 +463,6 @@ int perf_env__read_cpuid(struct perf_env *env)return 0 ; } -static int perf_env__read_arch (struct perf_env *env) -{ -struct utsname uts; - -if (env->arch) -return 0 ; - -if (!uname (&uts)) - env->arch =strdup (uts.machine); - -return env->arch ?0 : -ENOMEM ; -} -static int perf_env__read_nr_cpus_avail (struct perf_env *env) {if (env->nr_cpus_avail ==0 )
@@ -594,11 +581,6 @@ int perf_env__read_core_pmu_caps(struct perf_env *env)return ret; } -const char *perf_env__raw_arch (struct perf_env *env) -{ -return env && !perf_env__read_arch (env) ? env->arch :"unknown" ; -} -int perf_env__nr_cpus_avail (struct perf_env *env) {return env && !perf_env__read_nr_cpus_avail (env) ? env->nr_cpus_avail :0 ;
tools/perf/util/env.h +0 -1
@@ -192,7 +192,6 @@ const char *perf_env__arch(struct perf_env *env);const char *perf_env__arch_strerrno (uint16_t e_machine,int err);#endif const char *perf_env__cpuid (struct perf_env *env); -const char *perf_env__raw_arch (struct perf_env *env);int perf_env__nr_cpus_avail (struct perf_env *env);void perf_env__init (struct perf_env *env);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Ian Rogers
reviewerPATCH v9 16/18 perf env: Add helper to lazily compute the os_release
In reply to Ian RogersIn live mode the os_release isn't being initialized, make a lazy initialization helper that assumes when the os_release isn't initialized this is live mode.
--- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/env.c | 21 +++++++++++++++++++++ tools/perf/util/env.h | 1 + tools/perf/util/header.c | 16 +++++++++++----- tools/perf/util/symbol.c | 4 ++-- 5 files changed, 36 insertions(+), 8 deletions(-)
tools/perf/util/data-convert-bt.c +1 -1
@@ -1414,7 +1414,7 @@ do { \ADD ("host" , env->hostname);ADD ("sysname" ,"Linux" ); -ADD ("release" , env->os_release); +ADD ("release" ,perf_env__os_release (env));ADD ("version" , env->version);ADD ("machine" , env->arch);ADD ("domain" ,"kernel" );
tools/perf/util/env.c +21 -0
@@ -351,6 +351,27 @@ bool perf_arch_is_big_endian(const char *arch)return false; } +const char *perf_env__os_release (struct perf_env *env) +{ +struct utsname uts; +int ret; + +if (!env) +return perf_version_string; + +if (env->os_release) +return env->os_release; + +/* +* The os_release is being accessed but wasn't initialized from a data +* file, assume this is 'live' mode and use the release from uname. If +* uname or strdup fails then use the current perf tool version. +*/ + ret =uname (&uts); + env->os_release =strdup (ret <0 ? perf_version_string : uts.release); +return env->os_release ?: perf_version_string; +} +int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]) {int i;
tools/perf/util/env.h +1 -0
@@ -173,6 +173,7 @@ void perf_env__exit(struct perf_env *env);int perf_env__kernel_is_64_bit (struct perf_env *env);bool perf_arch_is_big_endian (const char *arch); +const char *perf_env__os_release (struct perf_env *env);int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]);
tools/perf/util/header.c +11 -5
@@ -370,13 +370,19 @@ static int write_osrelease(struct feat_fd *ff, struct evlist *evlist __maybe_unused) {struct utsname uts; -int ret; +const char *release =NULL ; - ret =uname (&uts); -if (ret <0 ) -return -1 ; +if (evlist->session) + release =perf_env__os_release (perf_session__env (evlist->session)); -return do_write_string (ff, uts.release); +if (!release) { +int ret =uname (&uts); + +if (ret <0 ) +return -1 ; + release = uts.release; + } +return do_write_string (ff, release); }static int write_arch (struct feat_fd *ff,struct evlist *evlist)
tools/perf/util/symbol.c +2 -2
@@ -2225,7 +2225,7 @@ static int vmlinux_path__init(struct perf_env *env) {struct utsname uts;char bf[PATH_MAX ]; -char *kernel_version; +const char *kernel_version;unsigned int i; vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
@@ -2242,7 +2242,7 @@ static int vmlinux_path__init(struct perf_env *env)return 0 ;if (env) { - kernel_version = env->os_release; + kernel_version =perf_env__os_release (env); }else {if (uname (&uts) <0 ) goto out_fail;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v9 16/18] perf env: Add helper to lazily compute the os_release
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Lazy initialization in `perf_env__os_release` assumes a NULL `os_release` implies 'live mode'…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Lazy initialization in perf_env__os_release assumes a NULL os_release implies 'live mode', permanently polluting offline trace environments with the analysis host's kernel version. --
commit d62418e881e036f2b1905f5ed81dd67b105e5fb8
Author: Ian Rogers <[email protected]>
perf env: Add helper to lazily compute the os_release
This commit introduces a lazy initialization helper for os_release. It assumes that if os_release is uninitialized, the tool is running in live mode, and populates it using uname().
[ ... ]
Quoted 16 lines
tools/perf/util/env.c +8
-0@@ -351,6 +351,27 @@ bool perf_arch_is_big_endian(const char *arch)return false; } +const char *perf_env__os_release (struct perf_env *env) +{ +struct utsname uts; +int ret; + +if (!env) +return perf_version_string; +
tools/perf/util/env.c bool perf_arch_is_big_endian(const char *arch) · line 371
Quoted 10 lines
tools/perf/util/env.c +10
-0+if (env->os_release) +return env->os_release; + +/* +* The os_release is being accessed but wasn't initialized from a data +* file, assume this is 'live' mode and use the release from uname. If +* uname or strdup fails then use the current perf tool version. +*/ + ret =uname (&uts); + env->os_release =strdup (ret <0 ? perf_version_string : uts.release);
If an offline perf.data file was recorded without the HEADER_OSRELEASE feature (for example, using --no-feature osrelease), won't env->os_release be NULL?
If so, does this permanently overwrite the target's environment metadata with the analysis host's kernel version?
If this occurs, it seems vmlinux_path__init() will search for the analysis host's kernel path instead of the target's. If the offline trace also lacks build-ids, could perf silently load the host's kernel symbol table and apply it to the target's profile, corrupting symbol resolution?
Similarly, could tools like perf data convert silently export the host's OS release into the new trace data as if it belonged to the target system?
Quoted 6 lines
tools/perf/util/env.c +3
-0+return env->os_release ?: perf_version_string; +} +int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]) {int i;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
Ian Rogers
reviewerPATCH v9 17/18 perf symbol: Add setters for bitfields sharing a byte to avoid concurrent update issues
In reply to Ian RogersA problem with putting bitfields into struct symbol is that other bits in the symbol could be updated concurrently and only one update to the underlying storage unit happen, leading to lost updates.
To avoid this, use atomics to atomically read or set part of 16-bits of flags in the symbol. Add accessors to simplify this.
The idle value has 3 values in preparation for a later change that will lazily update it.
--- tools/perf/builtin-inject.c | 6 +- tools/perf/builtin-kwork.c | 2 +- tools/perf/builtin-report.c | 2 +- tools/perf/builtin-sched.c | 4 +- tools/perf/builtin-top.c | 6 +- tools/perf/tests/symbols.c | 2 +- tools/perf/tests/vmlinux-kallsyms.c | 2 +- tools/perf/ui/browsers/annotate.c | 2 +- tools/perf/ui/browsers/map.c | 4 +- tools/perf/util/annotate.c | 5 +- tools/perf/util/auxtrace.c | 6 +- tools/perf/util/callchain.c | 4 +- tools/perf/util/dlfilter.c | 2 +- tools/perf/util/evsel_fprintf.c | 6 +- tools/perf/util/intel-pt.c | 2 +- tools/perf/util/machine.c | 2 +- tools/perf/util/probe-event.c | 4 +- .../util/scripting-engines/trace-event-perl.c | 2 +- .../scripting-engines/trace-event-python.c | 4 +- tools/perf/util/sort.c | 8 +- tools/perf/util/srcline.c | 10 +-- tools/perf/util/symbol-elf.c | 3 +- tools/perf/util/symbol.c | 84 +++++++++++++++---- tools/perf/util/symbol.h | 70 ++++++++++++---- tools/perf/util/symbol_fprintf.c | 4 +- 25 files changed, 171 insertions(+), 75 deletions(-)
tools/perf/builtin-inject.c +3 -3
@@ -439,9 +439,9 @@ static int perf_event__convert_sample_callchain(const struct perf_tool *tool, node = cursor->first;for (k =0 ; k < cursor->nr && i <PERF_MAX_STACK_DEPTH ; k++) {if (machine__kernel_ip (machine, node->ip)) -/* kernel IPs were added already */ ; -else if (node->ms.sym && node->ms.sym->inlined) -/* we can't handle inlined callchains */ ; + ;/* kernel IPs were added already */ +else if (node->ms.sym &&symbol__inlined (node->ms.sym)) + ;/* we can't handle inlined callchains */ else inject->raw_callchain->ips[i++] = node->ip;
tools/perf/builtin-kwork.c +1 -1
@@ -725,7 +725,7 @@ static void timehist_save_callchain(struct perf_kwork *kwork,if (sym) {if (!strcmp (sym->name,"__softirqentry_text_start" ) || !strcmp (sym->name,"__do_softirq" )) - sym->ignore =1 ; +symbol__set_ignore (sym, true); }callchain_cursor_advance (cursor);
tools/perf/builtin-report.c +1 -1
@@ -753,7 +753,7 @@ static int hists__resort_cb(struct hist_entry *he, void *arg)struct report *rep = arg;struct symbol *sym = he->ms.sym; -if (rep->symbol_ipc && sym && !sym->annotate2) { +if (rep->symbol_ipc && sym && !symbol__is_annotate2 (sym)) {struct evsel *evsel =hists_to_evsel (he->hists);symbol__annotate2 (&he->ms, evsel,NULL );
tools/perf/builtin-sched.c +2 -2
@@ -2371,7 +2371,7 @@ static void save_task_callchain(struct perf_sched *sched,if (!strcmp (sym->name,"schedule" ) || !strcmp (sym->name,"__schedule" ) || !strcmp (sym->name,"preempt_schedule" )) - sym->ignore =1 ; +symbol__set_ignore (sym, true); }callchain_cursor_advance (cursor);
@@ -3035,7 +3035,7 @@ static size_t callchain__fprintf_folded(FILE *fp, struct callchain_node *node)list_for_each_entry (chain, &node->val, list) {if (chain->ip >=PERF_CONTEXT_MAX )continue ; -if (chain->ms.sym && chain->ms.sym->ignore) +if (chain->ms.sym &&symbol__ignore (chain->ms.sym))continue ; ret += fprintf(fp, "%s%s", first ? "" : sep, callchain_list__sym_name(chain, bf, sizeof(bf),
tools/perf/builtin-top.c +3 -3
@@ -186,8 +186,8 @@ static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)"Please report to [email protected]\n" , ip,dso__long_name (dso),dso__symtab_origin (dso),map__start (map),map__end (map), sym->start, sym->end, - sym->binding ==STB_GLOBAL ?'g' : - sym->binding ==STB_LOCAL ?'l' :'w' , sym->name, +symbol__binding (sym) ==STB_GLOBAL ?'g' : +symbol__binding (sym) ==STB_LOCAL ?'l' :'w' , sym->name, err ?"[unknown]" : uts.machine, err ? "[unknown]" : uts.release, perf_version_string);if (use_browser <=0 )
@@ -830,7 +830,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !al.sym->idle) { +if (al.sym ==NULL || !symbol__is_idle (al.sym)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,
tools/perf/tests/symbols.c +1 -1
@@ -125,7 +125,7 @@ static int test_dso(struct dso *dso)for (nd =rb_first_cached (dso__symbols (dso)); nd; nd =rb_next (nd)) { struct symbol *sym = rb_entry(nd, struct symbol, rb_node); -if (sym->type !=STT_FUNC && sym->type !=STT_GNU_IFUNC ) +if (symbol__type (sym) !=STT_FUNC &&symbol__type (sym) !=STT_GNU_IFUNC )continue ;/* Check for overlapping function symbols */
tools/perf/tests/vmlinux-kallsyms.c +1 -1
@@ -346,7 +346,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused* such as __indirect_thunk_end. */ continue ; - }else if (is_ignored_symbol (sym->name, sym->type)) { + }else if (is_ignored_symbol (sym->name,symbol__type (sym))) {/* * Ignore hidden symbols, see scripts/kallsyms.c for the details */
tools/perf/ui/browsers/annotate.c +1 -1
@@ -1185,7 +1185,7 @@ int __hist_entry__tui_annotate(struct hist_entry *he, struct map_symbol *ms,if (dso__annotate_warned (dso))return -1 ; -if (not_annotated || !sym->annotate2) { +if (not_annotated || !symbol__is_annotate2 (sym)) { err =symbol__annotate2 (ms, evsel, &browser.arch);if (err) {annotate_browser__symbol_annotate_error (&browser, err);
tools/perf/ui/browsers/map.c +2 -2
@@ -32,8 +32,8 @@ static void map_browser__write(struct ui_browser *browser, void *nd, int row)ui_browser__set_percent_color (browser,0 , current_entry);ui_browser__printf (browser,"%*" PRIx64" %*" PRIx64" %c " , mb->addrlen, sym->start, mb->addrlen, sym->end, - sym->binding ==STB_GLOBAL ?'g' : - sym->binding ==STB_LOCAL ?'l' :'w' ); +symbol__binding (sym) ==STB_GLOBAL ?'g' : +symbol__binding (sym) ==STB_LOCAL ?'l' :'w' ); width = browser->width - ((mb->addrlen *2 ) +4 );if (width >0 )ui_browser__write_nstring (browser, sym->name, width);
tools/perf/util/annotate.c +3 -2
@@ -235,7 +235,8 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms, h =annotated_source__histogram (src, evsel);if (h ==NULL ) {pr_debug ("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64", addr=%#" PRIx64", end=%#" PRIx64", func: %d\n" , - __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type ==STT_FUNC ); + __func__, __LINE__, sym->name, sym->start, addr, sym->end, +symbol__type (sym) ==STT_FUNC );return -ENOMEM ; }
@@ -2224,7 +2225,7 @@ int symbol__annotate2(struct map_symbol *ms, struct evsel *evsel,annotation__init_column_widths (notes, sym);annotation__update_column_widths (notes); - sym->annotate2 =1 ; +symbol__set_annotate2 (sym, true);return 0 ; }
tools/perf/util/auxtrace.c +3 -3
@@ -2663,7 +2663,7 @@ static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt, {/* Same name, and global or the n'th found or any */ return !arch__compare_symbol_names (name, sym->name) && - ((!idx && sym->binding ==STB_GLOBAL ) || + ((!idx &&symbol__binding (sym) ==STB_GLOBAL ) || (idx >0 && ++*cnt == idx) || idx <0 ); }
@@ -2681,8 +2681,8 @@ static void print_duplicate_syms(struct dso *dso, const char *sym_name)if (dso_sym_match (sym, sym_name, &cnt,-1 )) {pr_err ("#%d\t0x%" PRIx64"\t%c\t%s\n" , ++cnt, sym->start, - sym->binding ==STB_GLOBAL ?'g' : - sym->binding ==STB_LOCAL ?'l' :'w' , +symbol__binding (sym) ==STB_GLOBAL ?'g' : +symbol__binding (sym) ==STB_LOCAL ?'l' :'w' , sym->name); near = true; }else if (near) {
tools/perf/util/callchain.c +2 -2
@@ -801,7 +801,7 @@ static enum match_result match_chain(struct callchain_cursor_node *node,* symbol start. Otherwise do a faster comparison based * on the symbol start address. */ -if (cnode->ms.sym->inlined || node->ms.sym->inlined) { +if (symbol__inlined (cnode->ms.sym) ||symbol__inlined (node->ms.sym)) { match =match_chain_strings (cnode->ms.sym->name, node->ms.sym->name);if (match !=MATCH_ERROR )
@@ -1245,7 +1245,7 @@ char *callchain_list__sym_name(struct callchain_list *cl,int printed;if (cl->ms.sym) { -const char *inlined = cl->ms.sym->inlined ?" (inlined)" :"" ; +const char *inlined =symbol__inlined (cl->ms.sym) ?" (inlined)" :"" ;if (show_srcline && cl->srcline) printed = scnprintf(bf, bfsize, "%s %s%s",
tools/perf/util/dlfilter.c +1 -1
@@ -56,7 +56,7 @@ static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al) d_al->symoff = al->addr -map__start (al->map) - sym->start; else d_al->symoff = 0; - d_al->sym_binding = sym->binding; + d_al->sym_binding =symbol__binding (sym); }else { d_al->sym =NULL ; d_al->sym_start =0 ;
tools/perf/util/evsel_fprintf.c +3 -3
@@ -146,7 +146,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment, sym = node->ms.sym; map = node->ms.map; -if (sym && sym->ignore && print_skip_ignored) +if (sym &&symbol__ignore (sym) && print_skip_ignored) goto next; printed +=fprintf (fp,"%-*.*s" , left_alignment, left_alignment," " );
@@ -182,7 +182,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment,addr_location__exit (&node_al); } -if (print_dso && (!sym || !sym->inlined)) +if (print_dso && (!sym || !symbol__inlined (sym))) printed +=map__fprintf_dsoname_dsoff (map, print_dsoff, addr, fp);if (print_srcline) {
@@ -192,7 +192,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment, printed +=map__fprintf_srcline (map, addr,"\n " , fp); } -if (sym && sym->inlined) +if (sym &&symbol__inlined (sym)) printed +=fprintf (fp," (inlined)" );if (!print_oneline)
tools/perf/util/intel-pt.c +1 -1
@@ -2979,7 +2979,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip) start =dso__first_symbol (map__dso (map));for (sym = start; sym; sym =dso__next_symbol (sym)) { -if (sym->binding ==STB_GLOBAL && +if (symbol__binding (sym) ==STB_GLOBAL && !strcmp (sym->name,"__switch_to" )) { ip =map__unmap_ip (map, sym->start);if (ip >=map__start (map) && ip <map__end (map)) {
tools/perf/util/machine.c +1 -1
@@ -1078,7 +1078,7 @@ static u64 find_entry_trampoline(struct dso *dso)unsigned int i;for (; sym; sym =dso__next_symbol (sym)) { -if (sym->binding !=STB_GLOBAL ) +if (symbol__binding (sym) !=STB_GLOBAL )continue ;for (i =0 ; i <ARRAY_SIZE (syms); i++) {if (!strcmp (sym->name, syms[i]))
tools/perf/util/probe-event.c +2 -2
@@ -416,7 +416,7 @@ static int find_alternative_probe_point(struct debuginfo *dinfo,map__for_each_symbol_by_name (map, pp->function, sym, idx) {if (uprobes) { address = sym->start; -if (sym->type ==STT_GNU_IFUNC ) +if (symbol__type (sym) ==STT_GNU_IFUNC )pr_warning ("Warning: The probe function (%s) is a GNU indirect function.\n" "Consider identifying the final function used at run time and set the probe directly on that.\n" , pp->function);
@@ -3189,7 +3189,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,for (j =0 ; j < num_matched_functions; j++) { sym = syms[j]; -if (sym->type !=STT_FUNC ) +if (symbol__type (sym) !=STT_FUNC )continue ;/* There can be duplicated symbols in the map */
tools/perf/util/scripting-engines/trace-event-perl.c +1 -1
@@ -304,7 +304,7 @@ static SV *perl_process_callchain(struct perf_sample *sample, }if (!hv_stores (sym,"start" ,newSVuv (node->ms.sym->start)) || !hv_stores (sym,"end" ,newSVuv (node->ms.sym->end)) || - !hv_stores (sym,"binding" ,newSVuv (node->ms.sym->binding)) || + !hv_stores (sym,"binding" ,newSVuv (symbol__binding (node->ms.sym))) || !hv_stores (sym,"name" ,newSVpvn (node->ms.sym->name, node->ms.sym->namelen)) || !hv_stores (elem,"sym" ,newRV_noinc ((SV *)sym))) {
tools/perf/util/scripting-engines/trace-event-python.c +2 -2
@@ -437,7 +437,7 @@ static PyObject *python_process_callchain(struct perf_sample *sample,pydict_set_item_string_decref (pysym,"end" ,PyLong_FromUnsignedLongLong (node->ms.sym->end));pydict_set_item_string_decref (pysym,"binding" , -_PyLong_FromLong (node->ms.sym->binding)); +_PyLong_FromLong (symbol__binding (node->ms.sym)));pydict_set_item_string_decref (pysym,"name" ,_PyUnicode_FromStringAndSize (node->ms.sym->name, node->ms.sym->namelen));
@@ -1275,7 +1275,7 @@ static int python_export_symbol(struct db_export *dbe, struct symbol *sym,tuple_set_d64 (t,1 ,dso__db_id (dso));tuple_set_d64 (t,2 , sym->start);tuple_set_d64 (t,3 , sym->end); -tuple_set_s32 (t,4 , sym->binding); +tuple_set_s32 (t,4 ,symbol__binding (sym));tuple_set_string (t,5 , sym->name);call_object (tables->symbol_handler, t,"symbol_table" );
tools/perf/util/sort.c +4 -4
@@ -469,7 +469,7 @@ int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)if (sym_l == sym_r)return 0 ; -if (sym_l->inlined || sym_r->inlined) { +if (symbol__inlined (sym_l) ||symbol__inlined (sym_r)) {int ret =strcmp (sym_l->name, sym_r->name);if (ret)
@@ -536,7 +536,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms, ret +=repsep_snprintf (bf + ret, size - ret,"[%c] " , level);if (sym && map) { -if (sym->type ==STT_OBJECT ) { +if (symbol__type (sym) ==STT_OBJECT ) { ret +=repsep_snprintf (bf + ret, size - ret,"%s" , sym->name); ret +=repsep_snprintf (bf + ret, size - ret,"+0x%llx" , ip -map__unmap_ip (map, sym->start));
@@ -544,7 +544,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms, ret +=repsep_snprintf (bf + ret, size - ret,"%.*s" , width - ret, sym->name); -if (sym->inlined) +if (symbol__inlined (sym)) ret +=repsep_snprintf (bf + ret, size - ret," (inlined)" ); }
@@ -1483,7 +1483,7 @@ static int _hist_entry__addr_snprintf(struct map_symbol *ms, ret +=repsep_snprintf (bf + ret, size - ret,"[%c] " , level);if (sym && map) { -if (sym->type ==STT_OBJECT ) { +if (symbol__type (sym) ==STT_OBJECT ) { ret +=repsep_snprintf (bf + ret, size - ret,"%s" , sym->name); ret +=repsep_snprintf (bf + ret, size - ret,"+0x%llx" , ip -map__unmap_ip (map, sym->start));
tools/perf/util/srcline.c +5 -5
@@ -113,16 +113,16 @@ struct symbol *new_inline_sym(struct dso *dso,/* ensure that we don't alias an inlined symbol, which could * lead to double frees in inline_node__delete */ -assert (!base_sym->inlined); +assert (!symbol__inlined (base_sym)); }else {/* create a fake symbol for the inline frame */ inline_sym =symbol__new (base_sym ? base_sym->start :0 , base_sym ? (base_sym->end - base_sym->start) :0 , - base_sym ? base_sym->binding :0 , - base_sym ? base_sym->type :0 , + base_sym ?symbol__binding (base_sym) :0 , + base_sym ?symbol__type (base_sym) :0 , funcname);if (inline_sym) - inline_sym->inlined =1 ; +symbol__set_inlined (inline_sym, true); }free (demangled);
@@ -437,7 +437,7 @@ void inline_node__delete(struct inline_node *node)list_del_init (&ilist->list);zfree_srcline (&ilist->srcline);/* only the inlined symbols are owned by the list */ -if (ilist->symbol && ilist->symbol->inlined) +if (ilist->symbol &&symbol__inlined (ilist->symbol))symbol__delete (ilist->symbol);free (ilist); }
tools/perf/util/symbol-elf.c +2 -1
@@ -350,7 +350,8 @@ static bool get_ifunc_name(Elf *elf, struct dso *dso, GElf_Ehdr *ehdr, sym =dso__find_symbol_nocache (dso, addr);/* Expecting the address to be an IFUNC or IFUNC alias */ -if (!sym || sym->start != addr || (sym->type !=STT_GNU_IFUNC && !sym->ifunc_alias)) +if (!sym || sym->start != addr || + (symbol__type (sym) !=STT_GNU_IFUNC && !symbol__ifunc_alias (sym)))return false;snprintf (buf, buf_sz,"%s@plt" , sym->name);
tools/perf/util/symbol.c +68 -16
151 lines not on this page · raw message
tools/perf/util/symbol.h +56 -14
94 lines not on this page · raw message
tools/perf/util/symbol_fprintf.c +2 -2
@@ -11,8 +11,8 @@ size_t symbol__fprintf(struct symbol *sym, FILE *fp) {return fprintf (fp," %" PRIx64"-%" PRIx64" %c %s\n" , sym->start, sym->end, - sym->binding ==STB_GLOBAL ?'g' : - sym->binding ==STB_LOCAL ?'l' :'w' , +symbol__binding (sym) ==STB_GLOBAL ?'g' : +symbol__binding (sym) ==STB_LOCAL ?'l' :'w' , sym->name); }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v9 17/18] perf symbol: Add setters for bitfields sharing a byte to avoid concurrent update issues
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Performance regression due to `atomic_load` enforcing strict sequential consistency in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Performance regression due to atomic_load enforcing strict sequential consistency in frequently called struct symbol getters. --
commit f3d51ff8771487f327675b2cba0003cf43b91f8a
Author: Ian Rogers <[email protected]>
perf symbol: Add setters for bitfields sharing a byte to avoid concurrent update issues
This commit addresses concurrent update issues by replacing bitfields in struct symbol with a 16-bit atomic flags variable. It introduces atomic accessors to read and modify these flags safely.
Quoted 3 lines
tools/perf/util/symbol.h +0
-0
[ ... ]
Quoted 13 lines
tools/perf/util/symbol.h +9
-0@@ -77,6 +82,43 @@ struct symbol {void symbol__delete (struct symbol *sym);void symbols__delete (struct rb_root_cached *symbols); +static inline u8 symbol__type (const struct symbol *sym) +{ +return (atomic_load (&sym->flags) &SYMBOL_FLAG_TYPE_MASK ) >>SYMBOL_FLAG_TYPE_SHIFT ; +} + +static inline u8 symbol__binding (const struct symbol *sym) +{ +return (atomic_load (&sym->flags) &SYMBOL_FLAG_BINDING_MASK ) >>SYMBOL_FLAG_BINDING_SHIFT ; +}
[ ... ]
tools/perf/util/symbol.h struct symbol { · line 103
Quoted 4 lines
tools/perf/util/symbol.h +4
-0+static inline bool symbol__inlined (const struct symbol *sym) +{ +return (atomic_load (&sym->flags) &SYMBOL_FLAG_INLINED ) !=0 ; +}
Could the use of atomic_load() here cause a performance regression on weakly-ordered architectures?
By default, C11 atomic_load() implies memory_order_seq_cst. On architectures like ARM64 or PowerPC, this generates memory barriers or load-acquire instructions.
These getters are evaluated millions of times in extremely hot paths, such as the qsort comparison function _sort__sym_cmp() in tools/perf/util/sort.c:
int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
{
...
if (symbol__inlined(sym_l) || symbol__inlined(sym_r)) {
int ret = strcmp(sym_l->name, sym_r->name);
...
}Emitting strict memory ordering instructions inside sorting loops prevents CPU pipeline instruction reordering, which might significantly slow down sample processing during perf report and perf top.
Since the read operations only require atomicity to prevent tearing, would it be better to use atomic_load_explicit(&sym->flags, memory_order_relaxed) instead?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
Ian Rogers
reviewerPATCH v9 18/18 perf symbol: Lazily compute idle
In reply to Ian RogersSwitch from an idle boolean to a helper symbol__is_idle function. In the function lazily compute whether a symbol is an idle function taking into consideration the kernel version and architecture of the machine. As symbols__insert no longer needs to know if a symbol is for the kernel, remove the argument.
This change is inspired by mailing list discussion, particularly from Thomas Richter <[email protected]> and Heiko Carstens <[email protected]>:
https://lore.kernel.org/lkml/[email protected]/
The change switches x86 matches to use strstarts which means intel_idle_irq is matched as part of strstarts(name, "intel_idle"), a change suggested by Honglei Wang <[email protected]> in:
https://lore.kernel.org/lkml/[email protected]/
--- tools/perf/builtin-top.c | 3 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 114 +++++++++++++++++++++-------------- tools/perf/util/symbol.h | 8 +-- 4 files changed, 74 insertions(+), 53 deletions(-)
tools/perf/builtin-top.c +2 -1
@@ -830,7 +830,8 @@ static void perf_event__process_sample(const struct perf_tool *tool, } } -if (al.sym ==NULL || !symbol__is_idle (al.sym)) { +if (al.sym ==NULL || + !symbol__is_idle (al.sym, al.map ?map__dso (al.map) :NULL , machine->env)) {struct hists *hists =evsel__hists (evsel);struct hist_entry_iter iter = { .evsel = evsel,
tools/perf/util/symbol-elf.c +1 -1
@@ -1728,7 +1728,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,arch__sym_update (f, &sym); -__symbols__insert (dso__symbols (curr_dso), f,dso__kernel (dso)); +__symbols__insert (dso__symbols (curr_dso), f); nr++; }dso__put (curr_dso);
tools/perf/util/symbol.c +68 -46
176 lines not on this page · raw message
tools/perf/util/symbol.h +3 -5
@@ -28,6 +28,7 @@ struct map;struct maps ;struct option ;struct build_id ; +struct perf_env ;/* * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
@@ -60,7 +61,6 @@ enum symbol_idle_kind {#define SYMBOL_FLAG_INLINED (1 << 11)#define SYMBOL_FLAG_ANNOTATE2 (1 << 12)#define SYMBOL_FLAG_IFUNC_ALIAS (1 << 13) -/** * A symtab entry. When allocated this may be preceded by an annotation (see * symbol__annotation) and/or a browser_index (see symbol__browser_index).
@@ -112,7 +112,7 @@ static inline bool symbol__ifunc_alias(const struct symbol *sym)return (atomic_load (&sym->flags) &SYMBOL_FLAG_IFUNC_ALIAS ) !=0 ; } -bool symbol__is_idle (const struct symbol *sym); +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,struct perf_env *env);void symbol__set_ignore (struct symbol *sym,bool ignore);void symbol__set_annotate2 (struct symbol *sym,bool annotate2);
@@ -196,7 +196,6 @@ int filename__read_debuglink(const char *filename, char *debuglink, size_t size);bool filename__has_section (const char *filename,const char *sec); -struct perf_env ;int symbol__init (struct perf_env *env);void symbol__exit (void );void symbol__elf_init (void );
@@ -236,8 +235,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name); -void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym, -bool kernel); +void __symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__insert (struct rb_root_cached *symbols,struct symbol *sym);void symbols__fixup_duplicate (struct rb_root_cached *symbols);void symbols__fixup_end (struct rb_root_cached *symbols,bool is_kallsyms);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewRe: [PATCH v9 18/18] perf symbol: Lazily compute idle
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Unnecessary out-of-line function call `perf_env__e_machine` before the fast-path cache check in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unnecessary out-of-line function call perf_env__e_machine before the fast-path cache check in symbol__is_idle introduces measurable overhead on the perf top hot path. --
commit 786c2d40f855657d57b68d17dcb788ab5ff93929
Author: Ian Rogers <[email protected]>
perf symbol: Lazily compute idle
This commit switches from an idle boolean to a helper function, symbol__is_idle, to lazily compute whether a symbol is an idle function. The computation now takes the machine's architecture and kernel version into consideration.
[ ... ]
Quoted 4 lines
tools/perf/util/symbol.c +0
-0
[ ... ]
Quoted 27 lines
tools/perf/util/symbol.c +9
-9@@ -760,55 +747,90 @@ int modules__parse(const char *filename, void *arg,return err; } -bool symbol__is_idle (const struct symbol *sym) -{ -uint16_t flags =atomic_load (&sym->flags); -uint16_t idle_val = (flags &SYMBOL_FLAG_IDLE_MASK ) >>SYMBOL_FLAG_IDLE_SHIFT ; - -return idle_val ==SYMBOL_IDLE__IDLE ; -} -/* * These are symbols in the kernel image, so make sure that * sym is from a kernel DSO. */ -static bool symbol__compute_is_idle (const char *name) +static int sym_name_cmp (const void *a,const void *b) +{ +const char *name = a; +const char *const *sym = b; + +return strcmp (name, *sym); +} + +bool symbol__is_idle (struct symbol *sym,const struct dso *dso,struct perf_env *env) {
[ ... ]
tools/perf/util/symbol.c int modules__parse(const char *filename, void *arg, · line 779
Quoted 2 lines
tools/perf/util/symbol.c +2
-0+const char *name = sym->name; +uint16_t e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL );
Does calling perf_env__e_machine() before checking the cached idle_val introduce unnecessary overhead on the perf top hot path?
Delaying the e_machine initialization until after the cache check below might help avoid this out-of-line function call when the idle state is already known.
Quoted 13 lines
tools/perf/util/symbol.c +13
-0+ + { +uint16_t flags =atomic_load (&sym->flags); +uint16_t idle_val = (flags &SYMBOL_FLAG_IDLE_MASK ) >>SYMBOL_FLAG_IDLE_SHIFT ; + +if (idle_val !=SYMBOL_IDLE__UNKNOWN ) +return idle_val ==SYMBOL_IDLE__IDLE ; + } + +if (!dso ||dso__kernel (dso) ==DSO_SPACE__USER ) { +symbol__set_idle (sym,/*idle=*/ false); +return false; + }
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
Ian Rogers
reviewerv10 cover Add perf_env__e_machine and migrate arch string comparisons to e_machine
In reply to Ian RogersAdd a helper to perf_env to compute the e_machine if it is EM_NONE. Derive the value from the arch string if available. Similarly derive the arch string from the ELF machine if available, for consistency. This means perf's arch (machine type) is no longer determined by uname but set to match that of the perf ELF executable.
Migrate code away from strcmp on env->arch to using the e_machine comparisons that are more accurate and not prone to uname and other naming differences. While cleaning this up, also clean up the capstone initialization code to cover more architectures and to set the big endian flag based on ELF header information.
Switch the idle computation to the point of use and lazily compute it, rather than computing it for every symbol. The current only user is perf top. At the point of use the perf_env is available and this can be used to make sure the idle function computation is machine and kernel version dependent.
To avoid concurrent update issues with bitfields sharing a byte in struct symbol switch to using C11 atomics.
v10:
- Key changes in v10:
- **Memory Safety & Robustness Hardening**: Added explicit machine NULL-pointer checks in dso__load() and thread guards in thread__e_machine_endian() to guarantee segmentation fault protection during out-of-tree Make builds, OOMs, or orphaned map group trace loadings.
- **Mixed-Bitness Platform Resilience**: Fully overhauled is_native_compatible() to be environment-bitness-aware. This ensures 32-bit hosts correctly utilize cross-compiler objdump lookups when analyzing 64-bit traces sharing identical ELF machine constants (EM_RISCV).
- **Live Session Architecture Mappings**: Redesigned perf_env__init_kernel_mode() to query runtime uname(&uts) and map live env->arch states dynamically, removing fragile hardcoded allowlists and guaranteeing correct 64-bit classifications for riscv64, mips64, and parisc64. Integrated bitness-aware 64-bit string overrides for these shared enums inside perf_env__arch().
- **Compiler Optimization Suffix Support**: Replaced strict strcmp() logic with an asymmetric compiler-suffix-aware prefix helper match_x86_idle_routine() to safely allowlist hot idle run-loops containing .constprop.* or .isra.* extensions while shielding against active hotplug/management functions dropping.
- **Spectre-mitigated Handler & Cross-Architecture Fallbacks**: Restored the missing intel_idle_ibrs Spectre mitigation idle handler to the allowlist. Configured symbol__is_idle() to safely fall back to reading dso__e_machine() if the environment context is missing, preserving cross-architecture offline trace evaluations.
- **Out-of-tree (O=) Make Builds Compatibility**: Refactored generated API translation units and Make Build dependency rules to eliminate -Wunused-function and -Wmissing-prototypes errors under strict WERROR=1 out-of-tree build parameters.
v9:
- Key changes in v9:
- **C11 Atomics for struct symbol**: Dropped the global symbol_bits_lock introduced in v7/v8. Replaced unsafe bitfields with a thread-safe _Atomic uint16_t flags and lockless atomic helpers (e.g., symbol__type(), symbol__set_inlined()).
- **Bi-endianness Support**: Added *_endian variants for dso and thread helpers to ensure Capstone correctly disassembles cross-endian binaries.
- **Architecture Hardening**:
- Fixed inverted SPARC logic in perf_env__single_address_space().
- Prioritized DSO architecture over global environment in machine_or_dso_e_machine().
- Fixed an uninitialized memory leak in perf_env__e_machine().
- Removed lossy normalize_arch() canonicalization in process_arch().
- Review Feedback Status:
- **Addressed**: C11 atomics migration, bi-endianness, SPARC logic, DSO prioritization, and uninitialized memory fixes.
- **Not Addressed / Dropped**:
- Patch 15 OS Release: The concern regarding the uname() fallback during offline analysis was determined to be incorrect for these uninitialized states; the original lazy assumption is retained.
- Patch 04/11: The EM_AARCH64 fallbacks were dropped as the definition should come from dwarf-regs.h when necessary.
v8:
- Address Sashiko AI review feedback for Patch 1:
- Switch all code dependent on the arch string to use e_machine instead.
- Update machine__is and machine__normalized_is to take e_machine integers instead of strings.
- Refactor arch_syscalls__strerrno_function to take an e_machine.
- Avoid premature caching of the host architecture in
`perf_session__e_machine`.
v7:
- Address better handling of strdup failures with arch in the header/env.
- Address concurrent update issues in struct symbol bitfields by introducing a global lock for writes.
v6: Ensure arch is canonical by going to e_machine and back (Sashiko)
v5: Add perf_env os_release helper (Namhyung/Sashiko)
v4: Fix Sashiko issues where an array element wasn't sorted properly,
the e_flags weren't returned properly, the idle type is change to a u8 rather than an enum value and the s390 version check for psw_idle is slightly reordered and tweaked.
v3: Properly set up the e_machine coming from the perf_env as reported
by Honglei Wang.
v2: Some minor white space clean up.
v1: Initial release.
Ian Rogers (18):
perf env: Add perf_env__e_machine helper and use in perf_env__arch perf tests topology: Switch env->arch use to env->e_machine perf env, dso, thread: Add _endian variants for e_machine helpers perf capstone: Determine architecture from e_machine perf print_insn: Use e_machine for fallback IP length check perf symbol: Avoid use of machine__is
perf machine: Use perf_env e_machine rather than arch perf sample-raw: Use perf_env e_machine rather than arch perf sort: Use perf_env e_machine rather than arch
perf arch common: Use perf_env e_machine rather than arch perf header: In print_pmu_caps use perf_env e_machine perf c2c: Use perf_env e_machine rather than arch
perf lock-contention: Use perf_env e_machine rather than arch perf env: Refactor perf_env__arch_strerrno
perf env: Remove unused perf_env__raw_arch
perf env: Add helper to lazily compute the os_release perf symbol: Add setters for bitfields sharing a byte to avoid
concurrent update issues
perf symbol: Lazily compute idle
tools/perf/arch/common.c | 92 +++-- tools/perf/builtin-c2c.c | 40 +- tools/perf/builtin-inject.c | 2 +- tools/perf/builtin-kwork.c | 2 +- tools/perf/builtin-report.c | 2 +- tools/perf/builtin-sched.c | 4 +- tools/perf/builtin-top.c | 6 +- tools/perf/builtin-trace.c | 7 +- tools/perf/tests/symbols.c | 2 +- tools/perf/tests/topology.c | 8 +- tools/perf/tests/vmlinux-kallsyms.c | 2 +- tools/perf/trace/beauty/Build | 1 + tools/perf/trace/beauty/arch_errno_names.sh | 41 +- tools/perf/ui/browsers/annotate.c | 2 +- tools/perf/ui/browsers/map.c | 4 +- tools/perf/util/annotate.c | 5 +- tools/perf/util/auxtrace.c | 6 +- tools/perf/util/callchain.c | 4 +- tools/perf/util/capstone.c | 132 ++++--- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/data-convert-json.c | 6 +- tools/perf/util/dlfilter.c | 2 +- tools/perf/util/dso.c | 19 +- tools/perf/util/dso.h | 14 +- tools/perf/util/env.c | 370 ++++++++++++++---- tools/perf/util/env.h | 13 +- tools/perf/util/evsel_fprintf.c | 6 +- tools/perf/util/header.c | 55 ++- tools/perf/util/intel-pt.c | 2 +- tools/perf/util/libdw.c | 2 +- tools/perf/util/lock-contention.c | 6 +- tools/perf/util/machine.c | 37 +- tools/perf/util/machine.h | 2 - tools/perf/util/print_insn.c | 23 +- tools/perf/util/print_insn.h | 2 + tools/perf/util/probe-event.c | 4 +- tools/perf/util/sample-raw.c | 21 +- tools/perf/util/sample-raw.h | 6 +- .../util/scripting-engines/trace-event-perl.c | 2 +- .../scripting-engines/trace-event-python.c | 4 +- tools/perf/util/session.c | 26 +- tools/perf/util/sort.c | 66 ++-- tools/perf/util/srcline.c | 10 +- tools/perf/util/symbol-elf.c | 5 +- tools/perf/util/symbol.c | 235 ++++++++--- tools/perf/util/symbol.h | 80 +++- tools/perf/util/symbol_fprintf.c | 4 +- tools/perf/util/thread.c | 54 ++- tools/perf/util/thread.h | 23 +- 49 files changed, 1048 insertions(+), 415 deletions(-)
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
Ian Rogers
reviewerPATCH v10 1/18 perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back to EM_HOST. Use the perf_env's arch to compute the e_machine if available, using a binary search for efficiency while handling duplicate rules.
Switch perf_env__arch to be derived from e_machine for consistency. To support 32-bit compat binaries on 64-bit hosts during dynamic local or live operations, unpopulated arch fallback paths query uname() at runtime to dynamically resolve the correct host e_machine, safely preventing bitness misclassification regressions.
Update session and header to use the helper to safely record e_machine and flags without forcing premature thread scanning.
--- tools/perf/util/env.c | 300 ++++++++++++++++++++++++++++++++------ tools/perf/util/env.h | 2 + tools/perf/util/header.c | 32 ++-- tools/perf/util/session.c | 26 ++-- 4 files changed, 295 insertions(+), 65 deletions(-)
tools/perf/util/env.c +256 -44
@@ -1,10 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #include "cpumap.h" +#include "dwarf-regs.h" #include "debug.h" #include "env.h" #include "util/header.h" #include "util/rwsem.h" #include <linux/compiler.h> +#include <linux/kernel.h> #include <linux/ctype.h> #include <linux/rbtree.h> #include <linux/string.h>
@@ -309,15 +311,27 @@ void perf_env__init(struct perf_env *env)static void perf_env__init_kernel_mode (struct perf_env *env) { -const char *arch =perf_env__raw_arch (env); +const char *arch = env->arch; -if (!strncmp (arch,"x86_64" ,6 ) || !strncmp (arch,"aarch64" ,7 ) || - !strncmp (arch,"arm64" ,5 ) || !strncmp (arch,"mips64" ,6 ) || - !strncmp (arch,"parisc64" ,8 ) || !strncmp (arch,"riscv64" ,7 ) || - !strncmp (arch,"s390x" ,5 ) || !strncmp (arch,"sparc64" ,7 )) - env->kernel_is_64_bit =1 ; -else - env->kernel_is_64_bit =0 ; +if (!arch) { +static struct utsname uts = { .machine[0 ] ='\0' , }; + +if (uts.machine[0 ] =='\0' ) +uname (&uts); +if (uts.machine[0 ] !='\0' ) + arch = uts.machine; + } + +if (arch) { +if (str_ends_with (arch,"64" ) || !strncmp (arch,"s390x" ,5 )) + env->kernel_is_64_bit =1 ; +else + env->kernel_is_64_bit =0 ; +return ; + } + +/* Fallback if completely unresolvable (assume host-bitness) */ + env->kernel_is_64_bit = (sizeof (void *) ==8 ) ?1 :0 ; }int perf_env__kernel_is_64_bit (struct perf_env *env)
@@ -588,51 +602,249 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)zfree (&cache->size); } +struct arch_to_e_machine { +const char *prefix; +uint16_t e_machine; +}; +/* -* Return architecture name in a normalized form. -* The conversion logic comes from the Makefile. +* A mapping from an arch prefix string to an ELF machine that can be used in a +* bsearch. Some arch prefixes are shared an need additional processing as +* marked next to the architecture. The prefixes handle both perf's architecture +* naming and those from uname. */ -static const char *normalize_arch (char *arch) -{ -if (!strcmp (arch,"x86_64" )) -return "x86" ; -if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; -if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; -if (!strncmp (arch,"aarch64" ,7 ) || !strncmp (arch,"arm64" ,5 )) -return "arm64" ; -if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; -if (!strncmp (arch,"s390" ,4 )) -return "s390" ; -if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; -if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; -if (!strncmp (arch,"mips" ,4 )) -return "mips" ; -if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; -if (!strncmp (arch,"loongarch" ,9 )) -return "loongarch" ; - -return arch; +static const struct arch_to_e_machine prefix_to_e_machine[] = { + {"aarch64" ,EM_AARCH64 }, + {"alpha" ,EM_ALPHA }, + {"arc" ,EM_ARC }, + {"arm" ,EM_ARM },/* Check also for EM_AARCH64. */ + {"avr" ,EM_AVR },/* Check also for EM_AVR32. */ + {"bfin" ,EM_BLACKFIN }, + {"blackfin" ,EM_BLACKFIN }, + {"cris" ,EM_CRIS }, + {"csky" ,EM_CSKY }, + {"hppa" ,EM_PARISC }, + {"i386" ,EM_386 }, + {"i486" ,EM_386 }, + {"i586" ,EM_386 }, + {"i686" ,EM_386 }, + {"loongarch" ,EM_LOONGARCH }, + {"m32r" ,EM_M32R }, + {"m68k" ,EM_68K }, + {"microblaze" ,EM_MICROBLAZE }, + {"mips" ,EM_MIPS }, + {"msp430" ,EM_MSP430 }, + {"parisc" ,EM_PARISC }, + {"powerpc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"ppc" ,EM_PPC },/* Check also for EM_PPC64. */ + {"riscv" ,EM_RISCV }, + {"s390" ,EM_S390 }, + {"sa110" ,EM_ARM }, + {"sh" ,EM_SH }, + {"sparc" ,EM_SPARC },/* Check also for EM_SPARCV9. */ + {"sun4u" ,EM_SPARC }, + {"x86" ,EM_X86_64 },/* Check also for EM_386. */ + {"xtensa" ,EM_XTENSA }, +}; + +static int compare_prefix (const void *key,const void *element) +{ +const char *search_key = key; +const struct arch_to_e_machine *map_element = element; +size_t prefix_len =strlen (map_element->prefix); + +return strncmp (search_key, map_element->prefix, prefix_len); +} + +static uint16_t perf_arch_to_e_machine (const char *perf_arch,int is_64_bit) +{ +/* Binary search for a matching prefix. */ +const struct arch_to_e_machine *result; + +if (!perf_arch) +return EM_HOST ; + + result =bsearch (perf_arch, + prefix_to_e_machine,ARRAY_SIZE (prefix_to_e_machine), +sizeof (prefix_to_e_machine[0 ]), + compare_prefix); + +if (!result) { +pr_debug ("Unknown perf arch for ELF machine mapping: %s\n" , perf_arch); +return EM_NONE ; + } + +/* +* Handle conflicting prefixes. If the is_64_bit is unknown (-1) then +* assume 64-bit. We can't use perf_env__kernel_is_64_bit as that +* depends on the arch string. +*/ +switch (result->e_machine) { +case EM_ARM : +return !strcmp (perf_arch,"arm64" ) || !strcmp (perf_arch,"aarch64" ) ?EM_AARCH64 :EM_ARM ; +case EM_AVR : +return !strcmp (perf_arch,"avr32" ) ?EM_AVR32 :EM_AVR ; +case EM_PPC : +if (is_64_bit ==1 ) +return EM_PPC64 ; +if (is_64_bit ==0 ) +return EM_PPC ; +return strstarts (perf_arch,"ppc64" ) ?EM_PPC64 :EM_PPC ; +case EM_SPARC : +if (is_64_bit ==1 ) +return EM_SPARCV9 ; +if (is_64_bit ==0 ) +return EM_SPARC ; +return !strcmp (perf_arch,"sparc64" ) || !strcmp (perf_arch,"sun4u" ) ?EM_SPARCV9 :EM_SPARC ; +case EM_X86_64 : +if (is_64_bit ==1 ) +return EM_X86_64 ; +if (is_64_bit ==0 ) +return EM_386 ; +return !strcmp (perf_arch,"x86_64" ) || !strcmp (perf_arch,"x86" ) ?EM_X86_64 :EM_386 ; +default : +return result->e_machine; + } +} + +static const char *e_machine_to_perf_arch (uint16_t e_machine) +{ +/* +* Table for if either the perf arch string differs from uname or there +* are >1 ELF machine with the prefix. +*/ +static const struct arch_to_e_machine extras[] = { + {"arm64" ,EM_AARCH64 }, + {"avr32" ,EM_AVR32 }, + {"powerpc" ,EM_PPC }, + {"powerpc" ,EM_PPC64 }, + {"sparc" ,EM_SPARCV9 }, + {"x86" ,EM_386 }, + {"x86" ,EM_X86_64 }, + {"none" ,EM_NONE }, + }; + +for (size_t i =0 ; i <ARRAY_SIZE (extras); i++) { +if (extras[i].e_machine == e_machine) +return extras[i].prefix; + } + +for (size_t i =0 ; i <ARRAY_SIZE (prefix_to_e_machine); i++) { +if (prefix_to_e_machine[i].e_machine == e_machine) +return prefix_to_e_machine[i].prefix; + + } +return "unknown" ; +} + +uint16_t perf_env__e_machine_nocache (struct perf_env *env,uint32_t *e_flags) +{ +uint16_t e_machine =EM_NONE ; +const char *arch =NULL ; +int is_64_bit =-1 ; + +if (e_flags) + *e_flags =0 ; + +if (env) { + arch = env->arch; + is_64_bit = env->kernel_is_64_bit; + } + +if (!arch) { +static struct utsname uts = { .machine[0 ] ='\0' , }; + +if (uts.machine[0 ] =='\0' ) +uname (&uts); +if (uts.machine[0 ] !='\0' ) + arch = uts.machine; + } + + e_machine =perf_arch_to_e_machine (arch, is_64_bit); + +if (e_flags) + *e_flags = (e_machine ==EM_HOST ) ?EF_HOST :0 ; + +return e_machine; +} + +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags) +{ +uint16_t e_machine; +uint32_t local_e_flags =0 ; + +if (env && env->e_machine !=EM_NONE ) { +if (e_flags) + *e_flags = env->e_flags; + +return env->e_machine; + } + e_machine =perf_env__e_machine_nocache (env, &local_e_flags); +/* +* Only cache the e_machine in perf_env if env->arch is not NULL. +* If env->arch is NULL, the e_machine is just a fallback to EM_HOST. +* Caching it permanently would prevent dynamic, more accurate +* thread-based session e_machine scanning later in +* perf_session__e_machine(). +*/ +if (env && env->arch) { + env->e_machine = e_machine; + env->e_flags = local_e_flags; + } +if (e_flags) + *e_flags = local_e_flags; + +return e_machine; }const char *perf_env__arch (struct perf_env *env) { -char *arch_name; +uint16_t e_machine; +const char *arch; -if (!env || !env->arch) {/* Assume local operation */ +if (!env) {static struct utsname uts = { .machine[0 ] ='\0' , }; -if (uts.machine[0 ] =='\0' &&uname (&uts) <0 ) -return NULL ; - arch_name = uts.machine; - }else - arch_name = env->arch; +uint16_t host_e_machine; + +if (uts.machine[0 ] =='\0' ) +uname (&uts); +if (uts.machine[0 ] !='\0' ) { + host_e_machine =perf_arch_to_e_machine (uts.machine,-1 ); +return e_machine_to_perf_arch (host_e_machine); + } +return e_machine_to_perf_arch (EM_HOST ); + } + +if (env->arch) +return env->arch; -return normalize_arch (arch_name); +/* +* Lazily compute/allocate arch. The e_machine may have been +* read from a data file and so may not be EM_HOST. +*/ + e_machine =perf_env__e_machine (env,/*e_flags=*/ NULL ); + arch =e_machine_to_perf_arch (e_machine); + +if (e_machine ==EM_RISCV &&perf_env__kernel_is_64_bit (env) ==1 ) + arch ="riscv64" ; +else if (e_machine ==EM_MIPS &&perf_env__kernel_is_64_bit (env) ==1 ) + arch ="mips64" ; +else if (e_machine ==EM_PARISC &&perf_env__kernel_is_64_bit (env) ==1 ) + arch ="parisc64" ; + +/* +* Only cache the normalized arch string in env->arch if the +* e_machine was successfully resolved (i.e. not the unpopulated +* fallback to EM_HOST), allowing threads to dynamically indicate +* a better ELF machine type later. +*/ +if (env && env->e_machine !=EM_NONE ) + env->arch =strdup (arch); +/* +* Avoid potential crashes on the arch string if memory allocation in +* strdup fails and NULL were to be returned. +*/ +return env->arch ?: arch; }const char *perf_env__arch_strerrno (struct perf_env *env__maybe_unused ,int err__maybe_unused )
tools/perf/util/env.h +2 -0
@@ -187,6 +187,8 @@ int perf_env__read_cpu_topology_map(struct perf_env *env);void cpu_cache_level__free (struct cpu_cache_level *cache); +uint16_t perf_env__e_machine_nocache (struct perf_env *env,uint32_t *e_flags); +uint16_t perf_env__e_machine (struct perf_env *env,uint32_t *e_flags);const char *perf_env__arch (struct perf_env *env);const char *perf_env__arch_strerrno (struct perf_env *env,int err);arch_syscalls__strerrno_t *arch_syscalls__strerrno_function (const char *arch);
tools/perf/util/header.c +22 -10
@@ -441,21 +441,25 @@ static int write_osrelease(struct feat_fd *ff,return do_write_string (ff, uts.release); } -static int write_arch (struct feat_fd *ff, -struct evlist *evlist__maybe_unused ) +static int write_arch (struct feat_fd *ff,struct evlist *evlist) {struct utsname uts; -int ret; +const char *arch =NULL ; - ret =uname (&uts); -if (ret <0 ) -return -1 ; +if (evlist->session) + arch =perf_env__arch (perf_session__env (evlist->session)); + +if (!arch) { +int ret =uname (&uts); -return do_write_string (ff, uts.machine); +if (ret <0 ) +return -1 ; + arch = uts.machine; + } +return do_write_string (ff, arch); } -static int write_e_machine (struct feat_fd *ff, -struct evlist *evlist__maybe_unused ) +static int write_e_machine (struct feat_fd *ff,struct evlist *evlist) {/* e_machine expanded from 16 to 32-bits for alignment. */ uint32_t e_flags;
@@ -2841,10 +2845,18 @@ static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \FEAT_PROCESS_STR_FUN (hostname, hostname);FEAT_PROCESS_STR_FUN (osrelease, os_release);FEAT_PROCESS_STR_FUN (version, version); -FEAT_PROCESS_STR_FUN (arch, arch);FEAT_PROCESS_STR_FUN (cpudesc, cpu_desc);FEAT_PROCESS_STR_FUN (cpuid, cpuid); +static int process_arch (struct feat_fd *ff,void *data__maybe_unused ) +{ +free (ff->ph->env.arch); + ff->ph->env.arch =do_read_string (ff); +if (!ff->ph->env.arch) +return -ENOMEM ; +return 0 ; +} +static int process_e_machine (struct feat_fd *ff,void *data__maybe_unused ) {int ret;
tools/perf/util/session.c +15 -11
@@ -4080,14 +4080,19 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flagsreturn EM_HOST ; } +/* +* Is the env caching an e_machine? If not we want to compute from the +* more accurate threads. +*/ env =perf_session__env (session); -if (env && env->e_machine !=EM_NONE ) { -if (e_flags) - *e_flags = env->e_flags; - -return env->e_machine; - } +if (env && env->e_machine !=EM_NONE ) +return perf_env__e_machine (env, e_flags); +/* +* Compute from threads, note this is more accurate than +* perf_env__e_machine that falls back on EM_HOST and doesn't consider +* mixed 32-bit and 64-bit threads. +*/ machines__for_each_thread (&session->machines, perf_session__e_machine_cb, &args);
@@ -4105,10 +4110,9 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags/* * Couldn't determine from the perf_env or current set of -* threads. Default to the host. +* threads. Potentially use logic that uses the arch string otherwise +* default to the host. Don't cache in the perf_env in case later +* threads indicate a better ELF machine type. */ -if (e_flags) - *e_flags =EF_HOST ; - -return EM_HOST ; +return perf_env__e_machine_nocache (env, e_flags); }
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
[email protected]
AI reviewRe: [PATCH v10 01/18] perf env: Add perf_env__e_machine helper and use in perf_env__arch
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] x86_64 is severely misclassified as 32-bit locally and in newly recorded `perf.data` files due…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] x86_64 is severely misclassified as 32-bit locally and in newly recorded perf.data files due to normalization overwriting the architecture string.
- [High] Removing normalize_arch breaks the parsing of older perf.data files by returning unnormalized architecture strings.
- [High] Kernel 64-bit detection fails for architectures with endianness or variant suffixes due to a strict str_ends_with check.
- [High] Unsynchronized lazy initialization of perf_env fields (arch, e_machine, e_flags) causes data races and memory leaks. --
commit a1c7d037273069adc911ec02be28b6159d9a6b8e
Author: Ian Rogers <[email protected]>
perf env: Add perf_env__e_machine helper and use in perf_env__arch
Add a helper that lazily computes the e_machine and falls back to EM_HOST. Use the perf_env's arch to compute the e_machine if available, using a binary search for efficiency while handling duplicate rules.
Quoted 4 lines
tools/perf/util/env.c +0
-0
[ ... ]
Quoted 6 lines
tools/perf/util/env.c +1
-1@@ -309,15 +311,27 @@ void perf_env__init(struct perf_env *env)static void perf_env__init_kernel_mode (struct perf_env *env) { -const char *arch =perf_env__raw_arch (env); +const char *arch = env->arch;
[ ... ]
tools/perf/util/env.c void perf_env__init(struct perf_env *env) · line 326
Quoted 2 lines
tools/perf/util/env.c +2
-0+if (arch) { +if (str_ends_with (arch,"64" ) || !strncmp (arch,"s390x" ,5 ))
[Severity: High]
Does this strict str_ends_with(arch, "64") check reject valid 64-bit architectures that possess trailing variant suffixes?
For example, architectures like "aarch64_be", "mips64el", or "riscv64gc" do not end exactly in "64". Will they fail this check and be incorrectly classified as 32-bit (env->kernel_is_64_bit = 0), which could break unwinding and BPF parsing on those platforms?
[Severity: Critical]
Additionally, could this str_ends_with() check cause standard x86_64 systems to be misclassified as 32-bit locally and in newly recorded perf.data files?
When recording a new perf.data file on x86_64, perf_env__arch() maps the host machine to the normalized string "x86" and caches it in env->arch. Later, when perf_env__init_kernel_mode() evaluates the cached "x86" string here, str_ends_with("x86", "64") will evaluate to false, erroneously setting env->kernel_is_64_bit to 0.
Quoted 5 lines
tools/perf/util/env.c +5
-0+ env->kernel_is_64_bit =1 ; + else + env->kernel_is_64_bit = 0; +return ; + }
[ ... ]
Quoted 1 line
tools/perf/util/env.c +0
-0@@ -588,51 +602,249 @@ void cpu_cache_level__free(struct cpu_cache_level *cache)
[ ... ]
Quoted 8 lines
const char *perf_env__arch(struct
perf_env*env)
{tools/perf/util/env.c +3
-2-char *arch_name; +uint16_t e_machine; +const char *arch; -if (!env || !env->arch) {/* Assume local operation */ +if (!env) {
[ ... ]
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 819
Quoted 2 lines
tools/perf/util/env.c +2
-0+if (env->arch) +return env->arch;
[Severity: High]
By returning env->arch directly and removing the normalize_arch() call, does this break the parsing of older perf.data files?
When reading from older files, env->arch contains the raw unnormalized string (e.g., "x86_64") from the HEADER_ARCH section. Returning this unnormalized string directly bypasses standard perf architecture aliases, which could break callers expecting canonical names, such as arch_syscalls__strerrno_function().
Quoted 2 lines
tools/perf/util/env.c +0
-1-return normalize_arch (arch_name);
[ ... ]
tools/perf/util/env.c void cpu_cache_level__free(struct cpu_cache_level *cache) · line 842
Quoted 8 lines
tools/perf/util/env.c +8
-0+/* +* Only cache the normalized arch string in env->arch if the +* e_machine was successfully resolved (i.e. not the unpopulated +* fallback to EM_HOST), allowing threads to dynamically indicate +* a better ELF machine type later. +*/ +if (env && env->e_machine !=EM_NONE ) + env->arch =strdup (arch);
[Severity: High]
Since perf_env can be shared across multiple threads during concurrent event processing (e.g., in perf report), could this unsynchronized lazy caching of env->arch via strdup() lead to data races and memory leaks?
Multiple threads observing env->arch as NULL could concurrently allocate and overwrite the same pointer. Additionally, without memory barriers, concurrent readers might observe a non-NULL env->arch pointer but read uninitialized memory. Similar unsynchronized lazy writes seem to happen for env->e_machine and env->e_flags in perf_env__e_machine().
Quoted 6 lines
tools/perf/util/env.c +5
-0+/* +* Avoid potential crashes on the arch string if memory allocation in +* strdup fails and NULL were to be returned. +*/ +return env->arch ?: arch; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH v10 2/18 perf tests topology: Switch env->arch use to env->e_machine
In reply to Ian RogersSome arch string comparisons weren't normalized. Avoid potential issues with normalized names vs uname values by swtiching to using the e_machine.
--- tools/perf/tests/topology.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/tests/topology.c +6 -2
@@ -11,6 +11,7 @@#include "pmus.h" #include "target.h" #include <linux/err.h> +#include "dwarf-regs.h" #define TEMPL "/tmp/perf-test-XXXXXX"#define DATA_SIZE 10
@@ -74,6 +75,7 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)struct aggr_cpu_id id;struct perf_cpu cpu;struct perf_env *env; +uint16_t e_machine; session =perf_session__new (&data,NULL );TEST_ASSERT_VAL ("can't get session" , !IS_ERR (session));
@@ -101,7 +103,9 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)* condition is true (see do_core_id_test in header.c). So always * run this test on those platforms. */ -if (!env->cpu &&strncmp (env->arch,"s390" ,4 ) &&strncmp (env->arch,"aarch64" ,7 )) + e_machine =perf_env__e_machine (env,NULL ); + +if (!env->cpu && e_machine !=EM_S390 && e_machine !=EM_AARCH64 )return TEST_SKIP ;/*
@@ -110,7 +114,7 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)* physical_package_id will be set to -1. Hence skip this * test if physical_package_id returns -1 for cpu from perf_cpu_map. */ -if (!strncmp (env->arch,"ppc64le" ,7 )) { +if (e_machine ==EM_PPC64 ) {if (cpu__get_socket_id (perf_cpu_map__cpu (map,0 )) ==-1 )return TEST_SKIP ; }
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
Ian Rogers
reviewerPATCH v10 3/18 perf env, dso, thread: Add _endian variants for e_machine helpers
In reply to Ian RogersAdd perf_arch_is_big_endian(), dso__read_e_machine_endian(), dso__e_machine_endian(), and thread__e_machine_endian() to support bi-endianness and cross-architecture analysis without breaking the existing API.
These helpers allow querying the absolute endianness of a DSO or thread, which is required for tools like Capstone that need to set the correct disassembly mode.
--- tools/perf/util/dso.c | 19 +++++++++----- tools/perf/util/dso.h | 14 +++++++++-- tools/perf/util/env.c | 16 ++++++++++++ tools/perf/util/env.h | 1 + tools/perf/util/thread.c | 54 ++++++++++++++++++++++++++++++---------- tools/perf/util/thread.h | 23 ++++++++++++++++- 6 files changed, 105 insertions(+), 22 deletions(-)
tools/perf/util/dso.c +13 -6
@@ -1220,7 +1220,8 @@ static enum dso_swap_type dso_swap_type__from_elf_data(unsigned char eidata) }/* Reads e_machine from fd, optionally caching data in dso. */ -uint16_t dso__read_e_machine (struct dso *optional_dso,int fd,uint32_t *e_flags) +uint16_t dso__read_e_machine_endian (struct dso *optional_dso,int fd,uint32_t *e_flags, +bool *is_big_endian) {uint16_t e_machine =EM_NONE ;unsigned char e_ident[EI_NIDENT ];
@@ -1250,6 +1251,9 @@ uint16_t dso__read_e_machine(struct dso *optional_dso, int fd, uint32_t *e_flagsif (swap_type ==DSO_SWAP__UNSET )return EM_NONE ;// Bad ELF data encoding. +if (is_big_endian) + *is_big_endian = (e_ident[EI_DATA ] ==ELFDATA2MSB ); +/* Cache the need for swapping. */ if (optional_dso) { assert(dso__needs_swap(optional_dso) == DSO_SWAP__UNSET ||
@@ -1288,7 +1292,8 @@ uint16_t dso__read_e_machine(struct dso *optional_dso, int fd, uint32_t *e_flagsreturn e_machine; } -uint16_t dso__e_machine (struct dso *dso,struct machine *machine,uint32_t *e_flags) +uint16_t dso__e_machine_endian (struct dso *dso,struct machine *machine,uint32_t *e_flags, +bool *is_big_endian) {uint16_t e_machine =EM_NONE ;int fd;
@@ -1308,9 +1313,11 @@ uint16_t dso__e_machine(struct dso *dso, struct machine *machine, uint32_t *e_flcase DSO_BINARY_TYPE__BPF_IMAGE :case DSO_BINARY_TYPE__OOL :case DSO_BINARY_TYPE__JAVA_JIT : -if (e_flags) - *e_flags =EF_HOST ; -return EM_HOST ; +if (is_big_endian) { + *is_big_endian =perf_arch_is_big_endian ( + machine && machine->env ?perf_env__arch (machine->env) :NULL ); + } +return perf_env__e_machine (machine ? machine->env :NULL , e_flags);case DSO_BINARY_TYPE__DEBUGLINK :case DSO_BINARY_TYPE__BUILD_ID_CACHE :case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO :
@@ -1338,7 +1345,7 @@ uint16_t dso__e_machine(struct dso *dso, struct machine *machine, uint32_t *e_fltry_to_open_dso (dso, machine); fd =dso__data (dso)->fd;if (fd >=0 ) - e_machine =dso__read_e_machine (dso, fd, e_flags); + e_machine =dso__read_e_machine_endian (dso, fd, e_flags, is_big_endian);else if (e_flags) *e_flags =0 ;
tools/perf/util/dso.h +12 -2
@@ -866,8 +866,18 @@ int dso__data_file_size(struct dso *dso, struct machine *machine);off_t dso__data_size (struct dso *dso,struct machine *machine);ssize_t dso__data_read_offset (struct dso *dso,struct machine *machine,u64 offset,u8 *data,ssize_t size); -uint16_t dso__read_e_machine (struct dso *optional_dso,int fd,uint32_t *e_flags); -uint16_t dso__e_machine (struct dso *dso,struct machine *machine,uint32_t *e_flags); +uint16_t dso__read_e_machine_endian (struct dso *optional_dso,int fd,uint32_t *e_flags, +bool *is_big_endian); +static inline uint16_t dso__read_e_machine (struct dso *optional_dso,int fd,uint32_t *e_flags) +{ +return dso__read_e_machine_endian (optional_dso, fd, e_flags,NULL ); +} +uint16_t dso__e_machine_endian (struct dso *dso,struct machine *machine,uint32_t *e_flags, +bool *is_big_endian); +static inline uint16_t dso__e_machine (struct dso *dso,struct machine *machine,uint32_t *e_flags) +{ +return dso__e_machine_endian (dso, machine, e_flags,NULL ); +}ssize_t dso__data_read_addr (struct dso *dso,struct map *map,struct machine *machine,u64 addr,u8 *data,ssize_t size);
tools/perf/util/env.c +16 -0
@@ -342,6 +342,22 @@ int perf_env__kernel_is_64_bit(struct perf_env *env)return env->kernel_is_64_bit; } +bool perf_arch_is_big_endian (const char *arch) +{ +if (!arch) +return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; + +if (str_ends_with (arch,"_be" ) || !strcmp (arch,"sparc" ) || !strcmp (arch,"sparc64" ) || + !strcmp (arch,"s390" ) || !strcmp (arch,"s390x" ) || !strcmp (arch,"powerpc" ) || + !strcmp (arch,"ppc" ) || !strcmp (arch,"ppc64" ) || + !strcmp (arch,"mips" ) || !strcmp (arch,"mips64" ) || !strcmp (arch,"parisc" ) || + !strcmp (arch,"parisc64" ) || !strcmp (arch,"m68k" ) || + !strcmp (arch,"armeb" ) || !strcmp (arch,"mipseb" ) || !strcmp (arch,"mips64eb" )) +return true; + +return false; +} +int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]) {int i;
tools/perf/util/env.h +1 -0
@@ -175,6 +175,7 @@ void free_cpu_domain_info(struct cpu_domain_map **cd_map, u32 schedstat_version,void perf_env__exit (struct perf_env *env);int perf_env__kernel_is_64_bit (struct perf_env *env); +bool perf_arch_is_big_endian (const char *arch);int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]);
tools/perf/util/thread.c +41 -13
120 lines not on this page · raw message
tools/perf/util/thread.h +22 -1
@@ -69,6 +69,11 @@ DECLARE_RC_STRUCT(thread) {* computed. */ uint16_t e_machine; +/** +* @e_is_big_endian: True if the ELF architecture of the thread is big endian. +* Valid if e_machine != EM_NONE. +*/ +bool e_is_big_endian;/* LBR call stack stitch */ bool lbr_stitch_enable;struct lbr_stitch *lbr_stitch;
@@ -311,7 +316,13 @@ static inline void thread__set_filter_entry_depth(struct thread *thread, int depRC_CHK_ACCESS (thread)->filter_entry_depth = depth; } -uint16_t thread__e_machine (struct thread *thread,struct machine *machine,uint32_t *e_flags); +uint16_t thread__e_machine_endian (struct thread *thread,struct machine *machine,uint32_t *e_flags, +bool *is_big_endian); +static inline uint16_t thread__e_machine (struct thread *thread,struct machine *machine, +uint32_t *e_flags) +{ +return thread__e_machine_endian (thread, machine, e_flags,NULL ); +}static inline void thread__set_e_machine (struct thread *thread,uint16_t e_machine) {
@@ -328,6 +339,16 @@ static inline void thread__set_e_flags(struct thread *thread, uint32_t e_flags)RC_CHK_ACCESS (thread)->e_flags = e_flags; } +static inline bool thread__e_is_big_endian (const struct thread *thread) +{ +return RC_CHK_ACCESS (thread)->e_is_big_endian; +} + +static inline void thread__set_e_is_big_endian (struct thread *thread,bool is_big_endian) +{ +RC_CHK_ACCESS (thread)->e_is_big_endian = is_big_endian; +} +static inline bool thread__lbr_stitch_enable (const struct thread *thread) {
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
[email protected]
AI reviewRe: [PATCH v10 03/18] perf env, dso, thread: Add _endian variants for e_machine helpers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Incorrect host endianness fallback in `thread__e_machine_endian` for NULL thread queries. - [High]…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Incorrect host endianness fallback in thread__e_machine_endian for NULL thread queries.
- [High] Data race and TOCTOU vulnerability due to concurrent reads and lockless updates of thread architecture properties. --
commit eb24af8119780afad944b6a5f56753d733d7d010
Author: Ian Rogers <[email protected]>
perf env, dso, thread: Add _endian variants for e_machine helpers
This commit adds _endian variants for ELF architecture helpers to support bi-endianness and cross-architecture analysis. It introduces perf_arch_is_big_endian() and updates the dso and thread structures to query and cache the absolute endianness.
Quoted 3 lines
tools/perf/util/thread.c +0
-0
[ ... ]
Quoted 8 lines
tools/perf/util/thread.c +3
-2-uint16_t thread__e_machine (struct thread *thread,struct machine *machine,uint32_t *e_flags) +uint16_t thread__e_machine_endian (struct thread *thread,struct machine *machine,uint32_t *e_flags, +bool *is_big_endian) {pid_t tid, pid; -uint16_t e_machine =RC_CHK_ACCESS (thread)->e_machine; +uint16_t e_machine;uint32_t local_e_flags =0 ;
tools/perf/util/thread.c static int thread__e_machine_callback(struct map *map, void *_args) · line 479
Quoted 10 lines
tools/perf/util/thread.c +5
-5-struct thread__e_machine_callback_args args = { - .machine = machine, - .e_flags =0 , - .e_machine =EM_NONE , - }; +struct thread__e_machine_callback_args args; + +if (!thread) { +if (is_big_endian) + *is_big_endian =perf_arch_is_big_endian (NULL );
[Severity: High]
Does this fallback path incorrectly use the host endianness instead of the target architecture? By passing NULL to perf_arch_is_big_endian(), it looks like this resolves to the host's __BYTE_ORDER__ even when performing offline cross-architecture analysis.
Should this dynamically resolve the endianness from the machine environment, similar to how it is handled in dso__e_machine_endian() by passing the architecture from machine->env?
Quoted 16 lines
tools/perf/util/thread.c +10
-0+return perf_env__e_machine (machine ? machine->env :NULL , e_flags); + } + + e_machine =RC_CHK_ACCESS (thread)->e_machine; + args.machine = machine; + args.e_flags =0 ; + args.e_machine =EM_NONE ; + args.is_big_endian = false;if (e_machine !=EM_NONE ) {if (e_flags) *e_flags =thread__e_flags (thread); +if (is_big_endian) + *is_big_endian =thread__e_is_big_endian (thread);return e_machine; }
[ ... ]
tools/perf/util/thread.c uint16_t thread__e_machine(struct thread *thread, struct machine *machine, uint3 · line 548
Quoted 5 lines
out:
tools/perf/util/thread.c +1
-0if (e_machine !=EM_NONE ) {thread__set_e_machine (thread, e_machine);thread__set_e_flags (thread, local_e_flags); +thread__set_e_is_big_endian (thread, args.is_big_endian);
[Severity: High]
Is there a potential data race here when initializing the thread architecture properties?
Since struct thread objects can be accessed concurrently, a second thread could observe e_machine != EM_NONE in the fast-path above immediately after thread__set_e_machine() is called, but before thread__set_e_flags() or thread__set_e_is_big_endian() complete.
Could this cause the second thread to read uninitialized or stale metadata? Would it be safer to update the payload properties before setting the e_machine indicator, possibly with memory barriers?
Quoted 4 lines
tools/perf/util/thread.c +2
-0+if (is_big_endian) + *is_big_endian = args.is_big_endian; }else { e_machine =EM_HOST ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3