Reference count checker and related fixes
Ian Rogers
reviewerv6 cover Reference count checker and related fixes
The perf tool has a class of memory problems where reference counts are used incorrectly. Memory/address sanitizers and valgrind don't provide useful ways to debug these problems, you see a memory leak where the only pertinent information is the original allocation site. What would be more useful is knowing where a get fails to have a corresponding put, where there are double puts, etc.
This work was motivated by the roll-back of:
https://lore.kernel.org/linux-perf-users/[email protected]/
where fixing a missed put resulted in a use-after-free in a different context. There was a sense in fixing the issue that a game of wac-a-mole had been embarked upon in adding missed gets and puts.
The basic approach of the change is to add a level of indirection at the get and put calls. Get allocates a level of indirection that, if no corresponding put is called, becomes a memory leak (and associated stack trace) that leak sanitizer can report. Similarly if two puts are called for the same get, then a double free can be detected by address sanitizer. This can also detect the use after put, which should also yield a segv without a sanitizer.
Adding reference count checking to cpu map was done as a proof of concept, it yielded little other than a location where the use of get could be cleaner by using its result. Reference count checking on nsinfo identified a double free of the indirection layer and the related threads, thereby identifying a data race as discussed here:
https://lore.kernel.org/linux-perf-users/CAP-5=fWZH20L4kv-BwVtGLwR=Em3AOOT+Q4QGivvQuYn5AsPRg@mail.gmail.com/
Accordingly the dso->lock was extended and use to cover the race.
The v3 version addresses problems in v2, in particular using macros to avoid #ifdefs. The v3 version applies the reference count checking approach to two more data structures, maps and map. While maps was straightforward, struct map showed a problem where reference counted thing can be on lists and rb-trees that are oblivious to the reference count. To sanitize this, struct map is changed so that it is referenced by either a list or rb-tree node and not part of it. This simplifies the reference count and the patches have caught and fixed a number of missed or mismatched reference counts relating to struct map.
The patches are arranged so that API refactors and bug fixes appear first, then the reference count checker itself appears. This allows for the refactor and fixes to be applied upstream first, as has already happened with cpumap.
A wider discussion of the approach is on the mailing list:
https://lore.kernel.org/linux-perf-users/[email protected]/T/#mf25ccd7a2e03de92cec29d36e2999a8ab5ec7f88
Comparing it to a past approach:
https://lore.kernel.org/all/[email protected]/
and to ref_tracker:
https://lwn.net/Articles/877603/
v6. rebase removing 5 merged changes. Fix missed issues with libunwind. v5. rebase removing 5 merged changes. Add map_list_node__new to the
1st patch (perf map: Move map list node into symbol) as suggested by Arnaldo. Remove unnecessary map__puts from patch 12 (perf map:
Changes to reference counting) as suggested by Adrian.
v4. rebases on to acme's perf-tools-next, fixes more issues with
map/maps and breaks apart the accessor functions to reduce individual patch sizes. The accessor functions are mechanical changes where the single biggest one is refactoring use of map->dso to be map__dso(map).
The v3 change is available here:
https://lore.kernel.org/lkml/[email protected]/
Ian Rogers (12):
perf map: Rename map_ip and unmap_ip
perf map: Add helper for map_ip and unmap_ip
perf map: Add accessors for prot, priv and flags perf map: Add accessors for pgoff and reloc
perf test: Add extra diagnostics to maps test perf maps: Modify maps_by_name to hold a reference to a map perf map: Changes to reference counting
libperf: Add reference count checking macros. perf cpumap: Add reference count checking
perf namespaces: Add reference count checking perf maps: Add reference count checking.
perf map: Add reference count checking
tools/lib/perf/Makefile | 2 +- tools/lib/perf/cpumap.c | 94 +++++++------ tools/lib/perf/include/internal/cpumap.h | 4 +- tools/lib/perf/include/internal/rc_check.h | 94 +++++++++++++ tools/perf/arch/s390/annotate/instructions.c | 4 +- tools/perf/arch/x86/util/event.c | 2 +- tools/perf/builtin-inject.c | 4 +- tools/perf/builtin-kallsyms.c | 2 +- tools/perf/builtin-kmem.c | 4 +- tools/perf/builtin-lock.c | 4 +- tools/perf/builtin-report.c | 11 +- tools/perf/builtin-script.c | 6 +- tools/perf/builtin-top.c | 4 +- tools/perf/tests/code-reading.c | 1 + tools/perf/tests/cpumap.c | 4 +- tools/perf/tests/hists_cumulate.c | 10 ++ tools/perf/tests/hists_filter.c | 10 ++ tools/perf/tests/hists_link.c | 18 ++- tools/perf/tests/hists_output.c | 10 ++ tools/perf/tests/maps.c | 67 ++++++--- tools/perf/tests/mmap-thread-lookup.c | 1 + tools/perf/tests/thread-maps-share.c | 29 ++-- tools/perf/tests/vmlinux-kallsyms.c | 22 +-- tools/perf/util/annotate.c | 20 +-- tools/perf/util/bpf_lock_contention.c | 4 +- tools/perf/util/callchain.c | 9 +- tools/perf/util/cpumap.c | 40 +++--- tools/perf/util/dlfilter.c | 2 +- tools/perf/util/dso.c | 8 +- tools/perf/util/dsos.c | 2 +- tools/perf/util/event.c | 14 +- tools/perf/util/evsel_fprintf.c | 2 +- tools/perf/util/hist.c | 10 +- tools/perf/util/intel-pt.c | 10 +- tools/perf/util/machine.c | 126 ++++++++++------- tools/perf/util/map.c | 95 +++++++------ tools/perf/util/map.h | 57 ++++++-- tools/perf/util/maps.c | 100 +++++++------ tools/perf/util/maps.h | 17 +-- tools/perf/util/namespaces.c | 132 ++++++++++-------- tools/perf/util/namespaces.h | 3 +- tools/perf/util/pmu.c | 8 +- tools/perf/util/probe-event.c | 14 +- .../scripting-engines/trace-event-python.c | 2 +- tools/perf/util/sort.c | 18 +-- tools/perf/util/symbol-elf.c | 26 ++-- tools/perf/util/symbol.c | 88 +++++++----- tools/perf/util/thread.c | 2 +- tools/perf/util/unwind-libdw.c | 10 +- tools/perf/util/unwind-libunwind-local.c | 4 +- tools/perf/util/unwind-libunwind.c | 2 +- 51 files changed, 766 insertions(+), 466 deletions(-) create mode 100644 tools/lib/perf/include/internal/rc_check.h
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH v6 1/12 perf map: Rename map_ip and unmap_ip
Add dso to match comment. This avoids a naming conflict with later added accessor functions for variables in struct map.
--- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-script.c | 4 ++-- tools/perf/util/machine.c | 4 ++-- tools/perf/util/map.c | 8 ++++---- tools/perf/util/map.h | 4 ++-- tools/perf/util/symbol-elf.c | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-)
tools/perf/builtin-kmem.c +1 -1
@@ -423,7 +423,7 @@ static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)if (!caller) {/* found */ if (node->ms.map) - addr =map__unmap_ip (node->ms.map, node->ip); + addr =map__dso_unmap_ip (node->ms.map, node->ip);else addr = node->ip;
tools/perf/builtin-script.c +2 -2
@@ -1012,11 +1012,11 @@ static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,if (thread__find_map_fb (thread, sample->cpumode, from, &alf) && !map__dso (alf.map)->adjust_symbols) - from =map__map_ip (alf.map, from); + from =map__dso_map_ip (alf.map, from);if (thread__find_map_fb (thread, sample->cpumode, to, &alt) && !map__dso (alt.map)->adjust_symbols) - to =map__map_ip (alt.map, to); + to =map__dso_map_ip (alt.map, to); printed +=fprintf (fp," 0x%" PRIx64, from);if (PRINT_FIELD (DSO )) {
tools/perf/util/machine.c +2 -2
@@ -3058,7 +3058,7 @@ static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *msif (!symbol_conf.inline_name || !map || !sym)return ret; - addr =map__map_ip (map, ip); + addr =map__dso_map_ip (map, ip); addr =map__rip_2objdump (map, addr); dso =map__dso (map);
@@ -3103,7 +3103,7 @@ static int unwind_entry(struct unwind_entry *entry, void *arg)* its corresponding binary. */ if (entry->ms.map) - addr =map__map_ip (entry->ms.map, entry->ip); + addr =map__dso_map_ip (entry->ms.map, entry->ip); srcline =callchain_srcline (&entry->ms, addr); return callchain_cursor_append(cursor, entry->ip, &entry->ms,
tools/perf/util/map.c +4 -4
@@ -109,8 +109,8 @@ void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso) map->pgoff = pgoff; map->reloc =0 ; map->dso =dso__get (dso); - map->map_ip = map__map_ip; - map->unmap_ip = map__unmap_ip; + map->map_ip = map__dso_map_ip; + map->unmap_ip = map__dso_unmap_ip; map->erange_warned = false;refcount_set (&map->refcnt,1 ); }
@@ -590,12 +590,12 @@ struct maps *map__kmaps(struct map *map)return kmap->kmaps; } -u64 map__map_ip (const struct map *map,u64 ip) +u64 map__dso_map_ip (const struct map *map,u64 ip) {return ip -map__start (map) + map->pgoff; } -u64 map__unmap_ip (const struct map *map,u64 ip) +u64 map__dso_unmap_ip (const struct map *map,u64 ip) {return ip +map__start (map) - map->pgoff; }
tools/perf/util/map.h +2 -2
@@ -41,9 +41,9 @@ struct kmap *map__kmap(struct map *map);struct maps *map__kmaps (struct map *map);/* ip -> dso rip */ -u64 map__map_ip (const struct map *map,u64 ip); +u64 map__dso_map_ip (const struct map *map,u64 ip);/* dso rip -> ip */ -u64 map__unmap_ip (const struct map *map,u64 ip); +u64 map__dso_unmap_ip (const struct map *map,u64 ip);/* Returns ip */ u64 identity__map_ip (const struct map *map__maybe_unused ,u64 ip);
tools/perf/util/symbol-elf.c +2 -2
@@ -1357,8 +1357,8 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map, map->start = shdr->sh_addr +ref_reloc (kmap); map->end =map__start (map) + shdr->sh_size; map->pgoff = shdr->sh_offset; - map->map_ip = map__map_ip; - map->unmap_ip = map__unmap_ip; + map->map_ip = map__dso_map_ip; + map->unmap_ip = map__dso_unmap_ip;/* Ensure maps are correctly ordered */ if (kmaps) {int err;
Signature · 1 line
2.40.0.348.gf938b09366-goog
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v6 01/12] perf map: Rename map_ip and unmap_ip
Em Tue, Apr 04, 2023 at 01:59:43PM -0700, Ian Rogers escreveu:
Quoted 18 lines
Add dso to match comment. This avoids a naming conflict with later added accessor functions for variables in struct map.
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-script.c | 4 ++-- tools/perf/util/machine.c | 4 ++-- tools/perf/util/map.c | 8 ++++---- tools/perf/util/map.h | 4 ++-- tools/perf/util/symbol-elf.c | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-)tools/perf/builtin-kmem.c +0
-0
tools/perf/builtin-kmem.c static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample) · line 426
Quoted 5 lines
tools/perf/builtin-kmem.c +1
-1@@ -423,7 +423,7 @@ static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)if (!caller) {/* found */ if (node->ms.map) - addr =map__unmap_ip (node->ms.map, node->ip); + addr =map__dso_unmap_ip (node->ms.map, node->ip);
As we chatted, I think this is a good opportunity to make this more clear, perhaps rename map__unmap_ip() to map__addr_offset_to_virt() and map__map_ip() to map__addr_virt_to_offset()?
- Arnaldo
Quoted 107 lines
tools/perf/builtin-kmem.c +0
-0else addr = node->ip;tools/perf/builtin-script.c +2
-2@@ -1012,11 +1012,11 @@ static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,if (thread__find_map_fb (thread, sample->cpumode, from, &alf) && !map__dso (alf.map)->adjust_symbols) - from =map__map_ip (alf.map, from); + from =map__dso_map_ip (alf.map, from);if (thread__find_map_fb (thread, sample->cpumode, to, &alt) && !map__dso (alt.map)->adjust_symbols) - to =map__map_ip (alt.map, to); + to =map__dso_map_ip (alt.map, to); printed +=fprintf (fp," 0x%" PRIx64, from);if (PRINT_FIELD (DSO )) {tools/perf/util/machine.c +2
-2@@ -3058,7 +3058,7 @@ static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *msif (!symbol_conf.inline_name || !map || !sym)return ret; - addr =map__map_ip (map, ip); + addr =map__dso_map_ip (map, ip); addr =map__rip_2objdump (map, addr); dso =map__dso (map);@@ -3103,7 +3103,7 @@ static int unwind_entry(struct unwind_entry *entry, void *arg)* its corresponding binary. */ if (entry->ms.map) - addr =map__map_ip (entry->ms.map, entry->ip); + addr =map__dso_map_ip (entry->ms.map, entry->ip); srcline =callchain_srcline (&entry->ms, addr); return callchain_cursor_append(cursor, entry->ip, &entry->ms,tools/perf/util/map.c +4
-4@@ -109,8 +109,8 @@ void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso) map->pgoff = pgoff; map->reloc =0 ; map->dso =dso__get (dso); - map->map_ip = map__map_ip; - map->unmap_ip = map__unmap_ip; + map->map_ip = map__dso_map_ip; + map->unmap_ip = map__dso_unmap_ip; map->erange_warned = false;refcount_set (&map->refcnt,1 ); }@@ -590,12 +590,12 @@ struct maps *map__kmaps(struct map *map)return kmap->kmaps; } -u64 map__map_ip (const struct map *map,u64 ip) +u64 map__dso_map_ip (const struct map *map,u64 ip) {return ip -map__start (map) + map->pgoff; } -u64 map__unmap_ip (const struct map *map,u64 ip) +u64 map__dso_unmap_ip (const struct map *map,u64 ip) {return ip +map__start (map) - map->pgoff; }tools/perf/util/map.h +2
-2@@ -41,9 +41,9 @@ struct kmap *map__kmap(struct map *map);struct maps *map__kmaps (struct map *map);/* ip -> dso rip */ -u64 map__map_ip (const struct map *map,u64 ip); +u64 map__dso_map_ip (const struct map *map,u64 ip);/* dso rip -> ip */ -u64 map__unmap_ip (const struct map *map,u64 ip); +u64 map__dso_unmap_ip (const struct map *map,u64 ip);/* Returns ip */ u64 identity__map_ip (const struct map *map__maybe_unused ,u64 ip);tools/perf/util/symbol-elf.c +2
-2@@ -1357,8 +1357,8 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map, map->start = shdr->sh_addr +ref_reloc (kmap); map->end =map__start (map) + shdr->sh_size; map->pgoff = shdr->sh_offset; - map->map_ip = map__map_ip; - map->unmap_ip = map__unmap_ip; + map->map_ip = map__dso_map_ip; + map->unmap_ip = map__dso_unmap_ip;/* Ensure maps are correctly ordered */ if (kmaps) {int err;Signature · 1 line
2.40.0.348.gf938b09366-goog
Signature · 1 line
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v6 01/12] perf map: Rename map_ip and unmap_ip
Em Wed, Apr 05, 2023 at 10:25:06AM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 14 lines
Em Tue, Apr 04, 2023 at 01:59:43PM -0700, Ian Rogers escreveu:
Quoted 13 lines
Add dso to match comment. This avoids a naming conflict with later added accessor functions for variables in struct map.
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-script.c | 4 ++-- tools/perf/util/machine.c | 4 ++-- tools/perf/util/map.c | 8 ++++---- tools/perf/util/map.h | 4 ++-- tools/perf/util/symbol-elf.c | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-)
Quoted 14 lines
tools/perf/builtin-kmem.c static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample) · line 426
Quoted 10 lines
tools/perf/builtin-kmem.c +1
-1@@ -423,7 +423,7 @@ static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)if (!caller) {/* found */ if (node->ms.map) - addr =map__unmap_ip (node->ms.map, node->ip); + addr =map__dso_unmap_ip (node->ms.map, node->ip);As we chatted, I think this is a good opportunity to make this more clear, perhaps rename
map__unmap_ip()tomap__addr_offset_to_virt()andmap__map_ip()tomap__addr_virt_to_offset()?
Maybe prefix both with __, i.e. __map__addr_offset_to_virt() and __map__addr_virt_to_offset(), since then you need to wrap map->unmap_ip() tomap__map__addr_offset_to_virt(), that in some cases will map to __map__addr_offset_to_virt(), modulo in identity maps, like with the
kernel where we use identify__map_ip().
- Arnaldo
Quoted 113 lines
- Arnaldo
Quoted 107 lines
tools/perf/builtin-kmem.c +0
-0else addr = node->ip;tools/perf/builtin-script.c +2
-2@@ -1012,11 +1012,11 @@ static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,if (thread__find_map_fb (thread, sample->cpumode, from, &alf) && !map__dso (alf.map)->adjust_symbols) - from =map__map_ip (alf.map, from); + from =map__dso_map_ip (alf.map, from);if (thread__find_map_fb (thread, sample->cpumode, to, &alt) && !map__dso (alt.map)->adjust_symbols) - to =map__map_ip (alt.map, to); + to =map__dso_map_ip (alt.map, to); printed +=fprintf (fp," 0x%" PRIx64, from);if (PRINT_FIELD (DSO )) {tools/perf/util/machine.c +2
-2@@ -3058,7 +3058,7 @@ static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *msif (!symbol_conf.inline_name || !map || !sym)return ret; - addr =map__map_ip (map, ip); + addr =map__dso_map_ip (map, ip); addr =map__rip_2objdump (map, addr); dso =map__dso (map);@@ -3103,7 +3103,7 @@ static int unwind_entry(struct unwind_entry *entry, void *arg)* its corresponding binary. */ if (entry->ms.map) - addr =map__map_ip (entry->ms.map, entry->ip); + addr =map__dso_map_ip (entry->ms.map, entry->ip); srcline =callchain_srcline (&entry->ms, addr); return callchain_cursor_append(cursor, entry->ip, &entry->ms,tools/perf/util/map.c +4
-4@@ -109,8 +109,8 @@ void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso) map->pgoff = pgoff; map->reloc =0 ; map->dso =dso__get (dso); - map->map_ip = map__map_ip; - map->unmap_ip = map__unmap_ip; + map->map_ip = map__dso_map_ip; + map->unmap_ip = map__dso_unmap_ip; map->erange_warned = false;refcount_set (&map->refcnt,1 ); }@@ -590,12 +590,12 @@ struct maps *map__kmaps(struct map *map)return kmap->kmaps; } -u64 map__map_ip (const struct map *map,u64 ip) +u64 map__dso_map_ip (const struct map *map,u64 ip) {return ip -map__start (map) + map->pgoff; } -u64 map__unmap_ip (const struct map *map,u64 ip) +u64 map__dso_unmap_ip (const struct map *map,u64 ip) {return ip +map__start (map) - map->pgoff; }tools/perf/util/map.h +2
-2@@ -41,9 +41,9 @@ struct kmap *map__kmap(struct map *map);struct maps *map__kmaps (struct map *map);/* ip -> dso rip */ -u64 map__map_ip (const struct map *map,u64 ip); +u64 map__dso_map_ip (const struct map *map,u64 ip);/* dso rip -> ip */ -u64 map__unmap_ip (const struct map *map,u64 ip); +u64 map__dso_unmap_ip (const struct map *map,u64 ip);/* Returns ip */ u64 identity__map_ip (const struct map *map__maybe_unused ,u64 ip);tools/perf/util/symbol-elf.c +2
-2@@ -1357,8 +1357,8 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map, map->start = shdr->sh_addr +ref_reloc (kmap); map->end =map__start (map) + shdr->sh_size; map->pgoff = shdr->sh_offset; - map->map_ip = map__map_ip; - map->unmap_ip = map__unmap_ip; + map->map_ip = map__dso_map_ip; + map->unmap_ip = map__dso_unmap_ip;/* Ensure maps are correctly ordered */ if (kmaps) {int err;Signature · 1 line
2.40.0.348.gf938b09366-googSignature · 1 line
- Arnaldo
Signature · 1 line
- Arnaldo
Ian Rogers
reviewerRe: [PATCH v6 01/12] perf map: Rename map_ip and unmap_ip
Quoted 31 lines · Arnaldo Carvalho de Melo
Em Wed, Apr 05, 2023 at 10:25:06AM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 14 lines
Em Tue, Apr 04, 2023 at 01:59:43PM -0700, Ian Rogers escreveu:
Quoted 13 lines
Add dso to match comment. This avoids a naming conflict with later added accessor functions for variables in struct map.
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-script.c | 4 ++-- tools/perf/util/machine.c | 4 ++-- tools/perf/util/map.c | 8 ++++---- tools/perf/util/map.h | 4 ++-- tools/perf/util/symbol-elf.c | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-)Quoted 14 lines
tools/perf/builtin-kmem.c static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample) · line 426
Quoted 10 lines
tools/perf/builtin-kmem.c +1
-1@@ -423,7 +423,7 @@ static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)if (!caller) {/* found */ if (node->ms.map) - addr =map__unmap_ip (node->ms.map, node->ip); + addr =map__dso_unmap_ip (node->ms.map, node->ip);As we chatted, I think this is a good opportunity to make this more clear, perhaps rename
map__unmap_ip()tomap__addr_offset_to_virt()andmap__map_ip()tomap__addr_virt_to_offset()?
Quoted 5 lines
Maybe prefix both with __, i.e.
__map__addr_offset_to_virt()and__map__addr_virt_to_offset(), since then you need to wrapmap->unmap_ip()tomap__map__addr_offset_to_virt(), that in some cases will map to__map__addr_offset_to_virt(), modulo in identity maps, like with the
kernel where we useidentify__map_ip().
Thanks Arnaldo, I'm not following what you are asking here. As it is a refactor unrelated to reference count checking, could you follow up this patch set with it?
Ian
Quoted 119 lines
- Arnaldo
Quoted 112 lines
- Arnaldo
Quoted 105 lines
tools/perf/builtin-kmem.c +0
-0else addr = node->ip;tools/perf/builtin-script.c +2
-2@@ -1012,11 +1012,11 @@ static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,if (thread__find_map_fb (thread, sample->cpumode, from, &alf) && !map__dso (alf.map)->adjust_symbols) - from =map__map_ip (alf.map, from); + from =map__dso_map_ip (alf.map, from);if (thread__find_map_fb (thread, sample->cpumode, to, &alt) && !map__dso (alt.map)->adjust_symbols) - to =map__map_ip (alt.map, to); + to =map__dso_map_ip (alt.map, to); printed +=fprintf (fp," 0x%" PRIx64, from);if (PRINT_FIELD (DSO )) {tools/perf/util/machine.c +2
-2@@ -3058,7 +3058,7 @@ static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *msif (!symbol_conf.inline_name || !map || !sym)return ret; - addr =map__map_ip (map, ip); + addr =map__dso_map_ip (map, ip); addr =map__rip_2objdump (map, addr); dso =map__dso (map);@@ -3103,7 +3103,7 @@ static int unwind_entry(struct unwind_entry *entry, void *arg)* its corresponding binary. */ if (entry->ms.map) - addr =map__map_ip (entry->ms.map, entry->ip); + addr =map__dso_map_ip (entry->ms.map, entry->ip); srcline =callchain_srcline (&entry->ms, addr); return callchain_cursor_append(cursor, entry->ip, &entry->ms,tools/perf/util/map.c +4
-4@@ -109,8 +109,8 @@ void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso) map->pgoff = pgoff; map->reloc =0 ; map->dso =dso__get (dso); - map->map_ip = map__map_ip; - map->unmap_ip = map__unmap_ip; + map->map_ip = map__dso_map_ip; + map->unmap_ip = map__dso_unmap_ip; map->erange_warned = false;refcount_set (&map->refcnt,1 ); }@@ -590,12 +590,12 @@ struct maps *map__kmaps(struct map *map)return kmap->kmaps; } -u64 map__map_ip (const struct map *map,u64 ip) +u64 map__dso_map_ip (const struct map *map,u64 ip) {return ip -map__start (map) + map->pgoff; } -u64 map__unmap_ip (const struct map *map,u64 ip) +u64 map__dso_unmap_ip (const struct map *map,u64 ip) {return ip +map__start (map) - map->pgoff; }tools/perf/util/map.h +2
-2@@ -41,9 +41,9 @@ struct kmap *map__kmap(struct map *map);struct maps *map__kmaps (struct map *map);/* ip -> dso rip */ -u64 map__map_ip (const struct map *map,u64 ip); +u64 map__dso_map_ip (const struct map *map,u64 ip);/* dso rip -> ip */ -u64 map__unmap_ip (const struct map *map,u64 ip); +u64 map__dso_unmap_ip (const struct map *map,u64 ip);/* Returns ip */ u64 identity__map_ip (const struct map *map__maybe_unused ,u64 ip);tools/perf/util/symbol-elf.c +2
-3@@ -1357,8 +1357,8 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map, map->start = shdr->sh_addr +ref_reloc (kmap); map->end =map__start (map) + shdr->sh_size; map->pgoff = shdr->sh_offset; - map->map_ip = map__map_ip; - map->unmap_ip = map__unmap_ip; + map->map_ip = map__dso_map_ip; + map->unmap_ip = map__dso_unmap_ip;/* Ensure maps are correctly ordered */ if (kmaps) {int err; --Quoted 2 lines
2.40.0.348.gf938b09366-goog
--
Quoted 1 line
- Arnaldo
--
- Arnaldo
Ian Rogers
reviewerPATCH v6 2/12 perf map: Add helper for map_ip and unmap_ip
In reply to Ian RogersLater changes will add reference count checking for struct map, add a helper function to invoke the map_ip and unmap_ip function pointers. The helper allows the reference count check to be in fewer places.
--- tools/perf/arch/s390/annotate/instructions.c | 3 ++- tools/perf/builtin-kallsyms.c | 2 +- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-lock.c | 4 ++-- tools/perf/builtin-script.c | 2 +- tools/perf/tests/vmlinux-kallsyms.c | 10 +++++----- tools/perf/util/annotate.c | 16 +++++++++------- tools/perf/util/bpf_lock_contention.c | 4 ++-- tools/perf/util/dlfilter.c | 2 +- tools/perf/util/dso.c | 6 ++++-- tools/perf/util/event.c | 8 ++++---- tools/perf/util/evsel_fprintf.c | 2 +- tools/perf/util/intel-pt.c | 10 +++++----- tools/perf/util/machine.c | 16 ++++++++-------- tools/perf/util/map.c | 10 +++++----- tools/perf/util/map.h | 10 ++++++++++ tools/perf/util/maps.c | 8 ++++---- tools/perf/util/probe-event.c | 8 ++++---- .../util/scripting-engines/trace-event-python.c | 2 +- tools/perf/util/sort.c | 12 ++++++------ tools/perf/util/symbol.c | 4 ++-- tools/perf/util/thread.c | 2 +- tools/perf/util/unwind-libdw.c | 2 +- tools/perf/util/unwind-libunwind-local.c | 2 +- 24 files changed, 81 insertions(+), 66 deletions(-)
tools/perf/arch/s390/annotate/instructions.c +2 -1
@@ -39,7 +39,8 @@ static int s390_call__parse(struct arch *arch, struct ins_operands *ops, target.addr =map__objdump_2mem (map, ops->target.addr);if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;return 0 ;
tools/perf/builtin-kallsyms.c +1 -1
@@ -39,7 +39,7 @@ static int __cmd_kallsyms(int argc, const char **argv) dso =map__dso (map);printf ("%s: %s %s %#" PRIx64"-%#" PRIx64" (%#" PRIx64"-%#" PRIx64")\n" , symbol->name, dso->short_name, dso->long_name, - map->unmap_ip (map, symbol->start), map->unmap_ip (map, symbol->end), +map__unmap_ip (map, symbol->start),map__unmap_ip (map, symbol->end), symbol->start, symbol->end); }
tools/perf/builtin-kmem.c +1 -1
@@ -1024,7 +1024,7 @@ static void __print_slab_result(struct rb_root *root,if (sym !=NULL )snprintf (buf,sizeof (buf),"%s+%" PRIx64"" , sym->name, - addr - map->unmap_ip (map, sym->start)); + addr -map__unmap_ip (map, sym->start));else snprintf (buf,sizeof (buf),"%#" PRIx64"" , addr);printf (" %-34s |" , buf);
tools/perf/builtin-lock.c +2 -2
@@ -900,7 +900,7 @@ static int get_symbol_name_offset(struct map *map, struct symbol *sym, u64 ip,return 0 ; } - offset = map->map_ip (map, ip) - sym->start; + offset =map__map_ip (map, ip) - sym->start;if (offset)return scnprintf (buf, size,"%s+%#lx" , sym->name, offset);
@@ -1070,7 +1070,7 @@ static int report_lock_contention_begin_event(struct evsel *evsel,return -ENOMEM ; } - addrs[filters.nr_addrs++] = kmap->unmap_ip (kmap, sym->start); + addrs[filters.nr_addrs++] =map__unmap_ip (kmap, sym->start); filters.addrs = addrs; } }
tools/perf/builtin-script.c +1 -1
@@ -1088,7 +1088,7 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,/* Load maps to ensure dso->is_64_bit has been updated */ map__load (al.map); - offset = al.map->map_ip (al.map, start); + offset =map__map_ip (al.map, start); len =dso__data_read_offset (dso, machine, offset, (u8 *)buffer, end - start +MAXINSN );
tools/perf/tests/vmlinux-kallsyms.c +5 -5
@@ -13,7 +13,7 @@#include "debug.h" #include "machine.h" -#define UM (x) kallsyms_map->unmap_ip(kallsyms_map, (x)) +#define UM (x) map__unmap_ip(kallsyms_map, (x))static bool is_ignored_symbol (const char *name,char type) {
@@ -221,8 +221,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unusedif (sym->start == sym->end)continue ; - mem_start = vmlinux_map->unmap_ip (vmlinux_map, sym->start); - mem_end = vmlinux_map->unmap_ip (vmlinux_map, sym->end); + mem_start =map__unmap_ip (vmlinux_map, sym->start); + mem_end =map__unmap_ip (vmlinux_map, sym->end); first_pair =machine__find_kernel_symbol (&kallsyms, mem_start,NULL ); pair = first_pair;
@@ -319,8 +319,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unusedmaps__for_each_entry (maps, rb_node) {struct map *pair, *map = rb_node->map; - mem_start = vmlinux_map->unmap_ip (vmlinux_map,map__start (map)); - mem_end = vmlinux_map->unmap_ip (vmlinux_map,map__end (map)); + mem_start =map__unmap_ip (vmlinux_map,map__start (map)); + mem_end =map__unmap_ip (vmlinux_map,map__end (map)); pair =maps__find (kallsyms.kmaps, mem_start);if (pair ==NULL || pair->priv)
tools/perf/util/annotate.c +9 -7
@@ -272,7 +272,8 @@ static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_s target.addr =map__objdump_2mem (map, ops->target.addr);if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;return 0 ;
@@ -376,8 +377,8 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s } target.addr =map__objdump_2mem (map, ops->target.addr); - start = map->unmap_ip (map, sym->start), - end = map->unmap_ip (map, sym->end); + start =map__unmap_ip (map, sym->start); + end =map__unmap_ip (map, sym->end); ops->target.outside = target.addr < start || target.addr > end;
@@ -400,7 +401,8 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s* the symbol searching and disassembly should be done. */ if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;if (!ops->target.outside) {
@@ -881,7 +883,7 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms,unsigned offset;struct sym_hist *h; -pr_debug3 ("%s: addr=%#" PRIx64"\n" , __func__, ms->map->unmap_ip (ms->map, addr)); +pr_debug3 ("%s: addr=%#" PRIx64"\n" , __func__,map__unmap_ip (ms->map, addr));if ((addr < sym->start || addr >= sym->end) && (addr != sym->end || sym->start != sym->end)) {
@@ -1977,8 +1979,8 @@ static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)return err;pr_debug ("%s: filename=%s, sym=%s, start=%#" PRIx64", end=%#" PRIx64"\n" , __func__, - symfs_filename, sym->name, map->unmap_ip (map, sym->start), - map->unmap_ip (map, sym->end)); + symfs_filename, sym->name,map__unmap_ip (map, sym->start), +map__unmap_ip (map, sym->end));pr_debug ("annotating [%p] %30s : [%p] %30s\n" , dso, dso->long_name, sym, sym->name);
tools/perf/util/bpf_lock_contention.c +2 -2
@@ -74,7 +74,7 @@ int lock_contention_prepare(struct lock_contention *con)continue ; } - addrs[con->filters->nr_addrs++] = kmap->unmap_ip (kmap, sym->start); + addrs[con->filters->nr_addrs++] =map__unmap_ip (kmap, sym->start); con->filters->addrs = addrs; } naddrs = con->filters->nr_addrs;
@@ -233,7 +233,7 @@ static const char *lock_contention_get_name(struct lock_contention *con,if (sym) {unsigned long offset; - offset = kmap->map_ip (kmap, addr) - sym->start; + offset =map__map_ip (kmap, addr) - sym->start;if (offset ==0 )return sym->name;
tools/perf/util/dlfilter.c +1 -1
@@ -278,7 +278,7 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len) map = a.map; have_map: - offset = map->map_ip (map, ip); + offset =map__map_ip (map, ip);if (ip + len >=map__end (map)) len =map__end (map) - ip;return dso__data_read_offset (map__dso (map), d->machine, offset, buf, len);
tools/perf/util/dso.c +4 -2
@@ -1122,7 +1122,8 @@ ssize_t dso__data_read_addr(struct dso *dso, struct map *map, struct machine *machine, u64 addr, u8 *data, ssize_t size) { - u64 offset = map->map_ip(map, addr); + u64 offset = map__map_ip(map, addr); +return dso__data_read_offset (dso, machine, offset, data, size); }
@@ -1162,7 +1163,8 @@ ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map, struct machine *machine, u64 addr, const u8 *data, ssize_t size) { - u64 offset = map->map_ip(map, addr); + u64 offset = map__map_ip(map, addr); +return dso__data_write_cache_offs (dso, machine, offset, data, size); }
tools/perf/util/event.c +4 -4
@@ -487,7 +487,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *ma al.map =maps__find (machine__kernel_maps (machine), tp->addr);if (al.map &&map__load (al.map) >=0 ) { - al.addr = al.map->map_ip (al.map, tp->addr); + al.addr =map__map_ip (al.map, tp->addr); al.sym =map__find_symbol (al.map, al.addr);if (al.sym) ret +=symbol__fprintf_symname_offs (al.sym, &al, fp);
@@ -622,7 +622,7 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,*/ if (load_map)map__load (al->map); - al->addr = al->map->map_ip (al->map, al->addr); + al->addr =map__map_ip (al->map, al->addr); }return al->map;
@@ -743,12 +743,12 @@ int machine__resolve(struct machine *machine, struct addr_location *al, }if (!ret && al->sym) {snprintf (al_addr_str, sz,"0x%" PRIx64, - al->map->unmap_ip (al->map, al->sym->start)); +map__unmap_ip (al->map, al->sym->start)); ret =strlist__has_entry (symbol_conf.sym_list, al_addr_str); }if (!ret && symbol_conf.addr_list && al->map) { -unsigned long addr = al->map->unmap_ip (al->map, al->addr); +unsigned long addr =map__unmap_ip (al->map, al->addr); ret =intlist__has_entry (symbol_conf.addr_list, addr);if (!ret && symbol_conf.addr_range) {
tools/perf/util/evsel_fprintf.c +1 -1
@@ -151,7 +151,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment, printed +=fprintf (fp," <-" );if (map) - addr = map->map_ip (map, node->ip); + addr =map__map_ip (map, node->ip);if (print_ip) {/* Show binary offset for userspace addr */
tools/perf/util/intel-pt.c +5 -5
@@ -816,7 +816,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn, dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE))return -ENOENT ; - offset = al.map->map_ip (al.map, *ip); + offset =map__map_ip (al.map, *ip);if (!to_ip && one_map) {struct intel_pt_cache_entry *e;
@@ -987,7 +987,7 @@ static int __intel_pt_pgd_ip(uint64_t ip, void *data)if (!thread__find_map (thread, cpumode, ip, &al) || !map__dso (al.map))return -EINVAL ; - offset = al.map->map_ip (al.map, ip); + offset =map__map_ip (al.map, ip);return intel_pt_match_pgd_ip (ptq->pt, ip, offset,map__dso (al.map)->long_name); }
@@ -2749,7 +2749,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)for (sym = start; sym; sym =dso__next_symbol (sym)) {if (sym->binding ==STB_GLOBAL && !strcmp (sym->name,"__switch_to" )) { - ip = map->unmap_ip (map, sym->start); + ip =map__unmap_ip (map, sym->start);if (ip >=map__start (map) && ip <map__end (map)) { switch_ip = ip;break ;
@@ -2767,7 +2767,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)for (sym = start; sym; sym =dso__next_symbol (sym)) {if (!strcmp (sym->name, ptss)) { - ip = map->unmap_ip (map, sym->start); + ip =map__unmap_ip (map, sym->start);if (ip >=map__start (map) && ip <map__end (map)) { *ptss_ip = ip;break ;
@@ -3393,7 +3393,7 @@ static int intel_pt_text_poke(struct intel_pt *pt, union perf_event *event)if (!dso || !dso->auxtrace_cache)continue ; - offset = al.map->map_ip (al.map, addr); + offset =map__map_ip (al.map, addr); e =intel_pt_cache_lookup (dso, machine, offset);if (!e)
tools/perf/util/machine.c +8 -8
@@ -919,7 +919,7 @@ static int machine__process_ksymbol_register(struct machine *machine, dso =map__dso (map); } - sym =symbol__new (map->map_ip (map,map__start (map)), + sym =symbol__new (map__map_ip (map,map__start (map)), event->ksymbol.len,0 ,0 , event->ksymbol.name);if (!sym)
@@ -944,7 +944,7 @@ static int machine__process_ksymbol_unregister(struct machine *machine,else {struct dso *dso =map__dso (map); - sym =dso__find_symbol (dso, map->map_ip (map,map__start (map))); + sym =dso__find_symbol (dso,map__map_ip (map,map__start (map)));if (sym)dso__delete_symbol (dso, sym); }
@@ -1279,7 +1279,7 @@ int machine__map_x86_64_entry_trampolines(struct machine *machine, dest_map =maps__find (kmaps, map->pgoff);if (dest_map != map) - map->pgoff = dest_map->map_ip (dest_map, map->pgoff); + map->pgoff =map__map_ip (dest_map, map->pgoff); found = true; }if (found || machine->trampolines_mapped)
@@ -3345,7 +3345,7 @@ char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, chreturn NULL ; *modp =__map__is_kmodule (map) ? (char *)map__dso (map)->short_name :NULL ; - *addrp = map->unmap_ip (map, sym->start); + *addrp =map__unmap_ip (map, sym->start);return sym->name; }
@@ -3388,17 +3388,17 @@ bool machine__is_lock_function(struct machine *machine, u64 addr)return false; } - machine->sched.text_start = kmap->unmap_ip (kmap, sym->start); + machine->sched.text_start =map__unmap_ip (kmap, sym->start);/* should not fail from here */ sym =machine__find_kernel_symbol_by_name (machine,"__sched_text_end" , &kmap); - machine->sched.text_end = kmap->unmap_ip (kmap, sym->start); + machine->sched.text_end =map__unmap_ip (kmap, sym->start); sym =machine__find_kernel_symbol_by_name (machine,"__lock_text_start" , &kmap); - machine->lock.text_start = kmap->unmap_ip (kmap, sym->start); + machine->lock.text_start =map__unmap_ip (kmap, sym->start); sym =machine__find_kernel_symbol_by_name (machine,"__lock_text_end" , &kmap); - machine->lock.text_end = kmap->unmap_ip (kmap, sym->start); + machine->lock.text_end =map__unmap_ip (kmap, sym->start); }/* failed to get kernel symbols */
tools/perf/util/map.c +5 -5
@@ -519,7 +519,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip)if (dso->kernel ==DSO_SPACE__USER )return rip + dso->text_offset; -return map->unmap_ip (map, rip) - map->reloc; +return map__unmap_ip (map, rip) - map->reloc; }/**
@@ -539,24 +539,24 @@ u64 map__objdump_2mem(struct map *map, u64 ip)const struct dso *dso =map__dso (map);if (!dso->adjust_symbols) -return map->unmap_ip (map, ip); +return map__unmap_ip (map, ip);if (dso->rel) -return map->unmap_ip (map, ip + map->pgoff); +return map__unmap_ip (map, ip + map->pgoff);/* * kernel modules also have DSO_TYPE_USER in dso->kernel, * but all kernel modules are ET_REL, so won't get here. */ if (dso->kernel ==DSO_SPACE__USER ) -return map->unmap_ip (map, ip - dso->text_offset); +return map__unmap_ip (map, ip - dso->text_offset);return ip + map->reloc; }bool map__contains_symbol (const struct map *map,const struct symbol *sym) { -u64 ip = map->unmap_ip (map, sym->start); +u64 ip =map__unmap_ip (map, sym->start);return ip >=map__start (map) && ip <map__end (map); }
tools/perf/util/map.h +10 -0
@@ -52,6 +52,16 @@ static inline struct dso *map__dso(const struct map *map)return map->dso; } +static inline u64 map__map_ip (const struct map *map,u64 ip) +{ +return map->map_ip (map, ip); +} + +static inline u64 map__unmap_ip (const struct map *map,u64 ip) +{ +return map->unmap_ip (map, ip); +} +static inline u64 map__start (const struct map *map) {return map->start;
tools/perf/util/maps.c +4 -4
@@ -194,7 +194,7 @@ struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp)if (map !=NULL &&map__load (map) >=0 ) {if (mapp !=NULL ) *mapp = map; -return map__find_symbol (map, map->map_ip (map, addr)); +return map__find_symbol (map,map__map_ip (map, addr)); }return NULL ;
@@ -237,7 +237,7 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams)return -1 ; } - ams->al_addr = ams->ms.map->map_ip (ams->ms.map, ams->addr); + ams->al_addr =map__map_ip (ams->ms.map, ams->addr); ams->ms.sym =map__find_symbol (ams->ms.map, ams->al_addr);return ams->ms.sym ?0 :-1 ;
@@ -349,8 +349,8 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) after->start =map__end (map); after->pgoff +=map__end (map) -map__start (pos->map); -assert (pos->map->map_ip (pos->map,map__end (map)) == - after->map_ip (after,map__end (map))); +assert (map__map_ip (pos->map,map__end (map)) == +map__map_ip (after,map__end (map))); err =__maps__insert (maps, after);if (err) goto put_map;
tools/perf/util/probe-event.c +4 -4
@@ -141,7 +141,7 @@ static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, sym =machine__find_kernel_symbol_by_name (host_machine, name, &map);if (!sym)return -ENOENT ; - *addr = map->unmap_ip (map, sym->start) - + *addr =map__unmap_ip (map, sym->start) - ((reloc) ?0 : map->reloc) - ((reladdr) ?map__start (map) :0 ); }
@@ -400,7 +400,7 @@ static int find_alternative_probe_point(struct debuginfo *dinfo,"Consider identifying the final function used at run time and set the probe directly on that.\n" , pp->function); }else - address = map->unmap_ip (map, sym->start) - map->reloc; + address =map__unmap_ip (map, sym->start) - map->reloc;break ; }if (!address) {
@@ -2249,7 +2249,7 @@ static int find_perf_probe_point_from_map(struct probe_trace_point *tp, goto out; pp->retprobe = tp->retprobe; - pp->offset = addr - map->unmap_ip (map, sym->start); + pp->offset = addr -map__unmap_ip (map, sym->start); pp->function =strdup (sym->name); ret = pp->function ?0 : -ENOMEM ;
@@ -3123,7 +3123,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev, goto err_out; }/* Add one probe point */ - tp->address = map->unmap_ip (map, sym->start) + pp->offset; + tp->address =map__unmap_ip (map, sym->start) + pp->offset;/* Check the kprobe (not in module) is within .text */ if (!pev->uprobes && !pev->target &&
tools/perf/util/scripting-engines/trace-event-python.c +1 -1
@@ -471,7 +471,7 @@ static PyObject *python_process_callchain(struct perf_sample *sample,struct addr_location node_al;unsigned long offset; - node_al.addr = map->map_ip (map, node->ip); + node_al.addr =map__map_ip (map, node->ip); node_al.map = map; offset =get_offset (node->ms.sym, &node_al);
tools/perf/util/sort.c +6 -6
@@ -364,7 +364,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms,u64 rip = ip;if (dso && dso->kernel && dso->adjust_symbols) - rip = map->unmap_ip (map, ip); + rip =map__unmap_ip (map, ip); ret +=repsep_snprintf (bf, size,"%-#*llx %c " ,BITS_PER_LONG /4 +2 , rip, o);
@@ -375,7 +375,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms,if (sym->type ==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)); + ip -map__unmap_ip (map, sym->start)); }else { ret += repsep_snprintf(bf + ret, size - ret, "%.*s", width - ret,
@@ -1147,7 +1147,7 @@ static int _hist_entry__addr_snprintf(struct map_symbol *ms,if (sym->type ==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)); + ip -map__unmap_ip (map, sym->start)); }else { ret += repsep_snprintf(bf + ret, size - ret, "%.*s", width - ret,
@@ -2104,9 +2104,9 @@ sort__addr_cmp(struct hist_entry *left, struct hist_entry *right)struct map *right_map = right->ms.map;if (left_map) - left_ip = left_map->unmap_ip (left_map, left_ip); + left_ip =map__unmap_ip (left_map, left_ip);if (right_map) - right_ip = right_map->unmap_ip (right_map, right_ip); + right_ip =map__unmap_ip (right_map, right_ip);return _sort__addr_cmp (left_ip, right_ip); }
@@ -2118,7 +2118,7 @@ static int hist_entry__addr_snprintf(struct hist_entry *he, char *bf,struct map *map = he->ms.map;if (map) - ip = map->unmap_ip (map, ip); + ip =map__unmap_ip (map, ip);return repsep_snprintf (bf, size,"%-#*llx" , width, ip); }
tools/perf/util/symbol.c +2 -2
@@ -896,8 +896,8 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,* So that we look just like we get from .ko files, * i.e. not prelinked, relative to initial_map->start. */ - pos->start = curr_map->map_ip (curr_map, pos->start); - pos->end = curr_map->map_ip (curr_map, pos->end); + 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)) {/* * These symbols are not needed anymore since the
tools/perf/util/thread.c +1 -1
@@ -464,7 +464,7 @@ int thread__memcpy(struct thread *thread, struct machine *machine,if ( !dso || dso->data.status ==DSO_DATA_STATUS_ERROR ||map__load (al.map) <0 )return -1 ; - offset = al.map->map_ip (al.map, ip); + offset =map__map_ip (al.map, ip);if (is64bit) *is64bit = dso->is_64_bit;
tools/perf/util/unwind-libdw.c +1 -1
@@ -115,7 +115,7 @@ static int entry(u64 ip, struct unwind_info *ui)pr_debug ("unwind: %s:ip = 0x%" PRIx64" (0x%" PRIx64")\n" , al.sym ? al.sym->name :"''" , ip, - al.map ? al.map->map_ip (al.map, ip) : (u64 )0 ); + al.map ?map__map_ip (al.map, ip) : (u64 )0 );return 0 ; }
tools/perf/util/unwind-libunwind-local.c +1 -1
@@ -640,7 +640,7 @@ static int entry(u64 ip, struct thread *thread,pr_debug ("unwind: %s:ip = 0x%" PRIx64" (0x%" PRIx64")\n" , al.sym ? al.sym->name :"''" , ip, - al.map ? al.map->map_ip (al.map, ip) : (u64 )0 ); + al.map ?map__map_ip (al.map, ip) : (u64 )0 );return cb (&e, arg); }
Signature · 1 line
2.40.0.348.gf938b09366-goog
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v6 02/12] perf map: Add helper for map_ip and unmap_ip
Em Tue, Apr 04, 2023 at 01:59:44PM -0700, Ian Rogers escreveu:
Quoted 4 lines
Later changes will add reference count checking for struct map, add a helper function to invoke the
map_ipandunmap_ipfunction pointers. The helper allows the reference count check to be in fewer places.
Fixing these:
⬢[acme@toolbox perf-tools-next]$ find tools/perf/ -name "*.[ch]" | xargs grep -- '->map_ip('tools/perf/arch/s390/annotate/instructions.c: map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr)) ==tools/perf/util/cs-etm.c: offset = al.map->map_ip(al.map, address);tools/perf/util/annotate.c: map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr)) ==tools/perf/util/annotate.c: map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr)) ==tools/perf/util/map.c: * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == iptools/perf/util/map.h: return map->map_ip(map, ip);
⬢[acme@toolbox perf-tools-next]$
⬢[acme@toolbox perf-tools-next]$ find tools/perf/ -name "*.[ch]" | xargs grep -- '->unmap_ip('tools/perf/arch/powerpc/util/sym-handling.c: if (map->unmap_ip(map, sym->start) == tev->point.address) {tools/perf/util/map.h: return map->unmap_ip(map, ip);
⬢[acme@toolbox perf-tools-next]$
Quoted 671 lines
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/arch/s390/annotate/instructions.c | 3 ++- tools/perf/builtin-kallsyms.c | 2 +- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-lock.c | 4 ++-- tools/perf/builtin-script.c | 2 +- tools/perf/tests/vmlinux-kallsyms.c | 10 +++++----- tools/perf/util/annotate.c | 16 +++++++++------- tools/perf/util/bpf_lock_contention.c | 4 ++-- tools/perf/util/dlfilter.c | 2 +- tools/perf/util/dso.c | 6 ++++-- tools/perf/util/event.c | 8 ++++---- tools/perf/util/evsel_fprintf.c | 2 +- tools/perf/util/intel-pt.c | 10 +++++----- tools/perf/util/machine.c | 16 ++++++++-------- tools/perf/util/map.c | 10 +++++----- tools/perf/util/map.h | 10 ++++++++++ tools/perf/util/maps.c | 8 ++++---- tools/perf/util/probe-event.c | 8 ++++---- .../util/scripting-engines/trace-event-python.c | 2 +- tools/perf/util/sort.c | 12 ++++++------ tools/perf/util/symbol.c | 4 ++-- tools/perf/util/thread.c | 2 +- tools/perf/util/unwind-libdw.c | 2 +- tools/perf/util/unwind-libunwind-local.c | 2 +- 24 files changed, 81 insertions(+), 66 deletions(-)tools/perf/arch/s390/annotate/instructions.c +2
-1@@ -39,7 +39,8 @@ static int s390_call__parse(struct arch *arch, struct ins_operands *ops, target.addr =map__objdump_2mem (map, ops->target.addr);if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;return 0 ;tools/perf/builtin-kallsyms.c +1
-1@@ -39,7 +39,7 @@ static int __cmd_kallsyms(int argc, const char **argv) dso =map__dso (map);printf ("%s: %s %s %#" PRIx64"-%#" PRIx64" (%#" PRIx64"-%#" PRIx64")\n" , symbol->name, dso->short_name, dso->long_name, - map->unmap_ip (map, symbol->start), map->unmap_ip (map, symbol->end), +map__unmap_ip (map, symbol->start),map__unmap_ip (map, symbol->end), symbol->start, symbol->end); }tools/perf/builtin-kmem.c +1
-1@@ -1024,7 +1024,7 @@ static void __print_slab_result(struct rb_root *root,if (sym !=NULL )snprintf (buf,sizeof (buf),"%s+%" PRIx64"" , sym->name, - addr - map->unmap_ip (map, sym->start)); + addr -map__unmap_ip (map, sym->start));else snprintf (buf,sizeof (buf),"%#" PRIx64"" , addr);printf (" %-34s |" , buf);tools/perf/builtin-lock.c +2
-2@@ -900,7 +900,7 @@ static int get_symbol_name_offset(struct map *map, struct symbol *sym, u64 ip,return 0 ; } - offset = map->map_ip (map, ip) - sym->start; + offset =map__map_ip (map, ip) - sym->start;if (offset)return scnprintf (buf, size,"%s+%#lx" , sym->name, offset);@@ -1070,7 +1070,7 @@ static int report_lock_contention_begin_event(struct evsel *evsel,return -ENOMEM ; } - addrs[filters.nr_addrs++] = kmap->unmap_ip (kmap, sym->start); + addrs[filters.nr_addrs++] =map__unmap_ip (kmap, sym->start); filters.addrs = addrs; } }tools/perf/builtin-script.c +1
-1@@ -1088,7 +1088,7 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,/* Load maps to ensure dso->is_64_bit has been updated */ map__load (al.map); - offset = al.map->map_ip (al.map, start); + offset =map__map_ip (al.map, start); len =dso__data_read_offset (dso, machine, offset, (u8 *)buffer, end - start +MAXINSN );tools/perf/tests/vmlinux-kallsyms.c +5
-5@@ -13,7 +13,7 @@#include "debug.h" #include "machine.h" -#define UM (x) kallsyms_map->unmap_ip(kallsyms_map, (x)) +#define UM (x) map__unmap_ip(kallsyms_map, (x))static bool is_ignored_symbol (const char *name,char type) {@@ -221,8 +221,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unusedif (sym->start == sym->end)continue ; - mem_start = vmlinux_map->unmap_ip (vmlinux_map, sym->start); - mem_end = vmlinux_map->unmap_ip (vmlinux_map, sym->end); + mem_start =map__unmap_ip (vmlinux_map, sym->start); + mem_end =map__unmap_ip (vmlinux_map, sym->end); first_pair =machine__find_kernel_symbol (&kallsyms, mem_start,NULL ); pair = first_pair;@@ -319,8 +319,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unusedmaps__for_each_entry (maps, rb_node) {struct map *pair, *map = rb_node->map; - mem_start = vmlinux_map->unmap_ip (vmlinux_map,map__start (map)); - mem_end = vmlinux_map->unmap_ip (vmlinux_map,map__end (map)); + mem_start =map__unmap_ip (vmlinux_map,map__start (map)); + mem_end =map__unmap_ip (vmlinux_map,map__end (map)); pair =maps__find (kallsyms.kmaps, mem_start);if (pair ==NULL || pair->priv)tools/perf/util/annotate.c +9
-7@@ -272,7 +272,8 @@ static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_s target.addr =map__objdump_2mem (map, ops->target.addr);if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;return 0 ;@@ -376,8 +377,8 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s } target.addr =map__objdump_2mem (map, ops->target.addr); - start = map->unmap_ip (map, sym->start), - end = map->unmap_ip (map, sym->end); + start =map__unmap_ip (map, sym->start); + end =map__unmap_ip (map, sym->end); ops->target.outside = target.addr < start || target.addr > end;@@ -400,7 +401,8 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s* the symbol searching and disassembly should be done. */ if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;if (!ops->target.outside) {@@ -881,7 +883,7 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms,unsigned offset;struct sym_hist *h; -pr_debug3 ("%s: addr=%#" PRIx64"\n" , __func__, ms->map->unmap_ip (ms->map, addr)); +pr_debug3 ("%s: addr=%#" PRIx64"\n" , __func__,map__unmap_ip (ms->map, addr));if ((addr < sym->start || addr >= sym->end) && (addr != sym->end || sym->start != sym->end)) {@@ -1977,8 +1979,8 @@ static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)return err;pr_debug ("%s: filename=%s, sym=%s, start=%#" PRIx64", end=%#" PRIx64"\n" , __func__, - symfs_filename, sym->name, map->unmap_ip (map, sym->start), - map->unmap_ip (map, sym->end)); + symfs_filename, sym->name,map__unmap_ip (map, sym->start), +map__unmap_ip (map, sym->end));pr_debug ("annotating [%p] %30s : [%p] %30s\n" , dso, dso->long_name, sym, sym->name);tools/perf/util/bpf_lock_contention.c +2
-2@@ -74,7 +74,7 @@ int lock_contention_prepare(struct lock_contention *con)continue ; } - addrs[con->filters->nr_addrs++] = kmap->unmap_ip (kmap, sym->start); + addrs[con->filters->nr_addrs++] =map__unmap_ip (kmap, sym->start); con->filters->addrs = addrs; } naddrs = con->filters->nr_addrs;@@ -233,7 +233,7 @@ static const char *lock_contention_get_name(struct lock_contention *con,if (sym) {unsigned long offset; - offset = kmap->map_ip (kmap, addr) - sym->start; + offset =map__map_ip (kmap, addr) - sym->start;if (offset ==0 )return sym->name;tools/perf/util/dlfilter.c +1
-1@@ -278,7 +278,7 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len) map = a.map; have_map: - offset = map->map_ip (map, ip); + offset =map__map_ip (map, ip);if (ip + len >=map__end (map)) len =map__end (map) - ip;return dso__data_read_offset (map__dso (map), d->machine, offset, buf, len);tools/perf/util/dso.c +4
-2@@ -1122,7 +1122,8 @@ ssize_t dso__data_read_addr(struct dso *dso, struct map *map, struct machine *machine, u64 addr, u8 *data, ssize_t size) { - u64 offset = map->map_ip(map, addr); + u64 offset = map__map_ip(map, addr); +return dso__data_read_offset (dso, machine, offset, data, size); }@@ -1162,7 +1163,8 @@ ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map, struct machine *machine, u64 addr, const u8 *data, ssize_t size) { - u64 offset = map->map_ip(map, addr); + u64 offset = map__map_ip(map, addr); +return dso__data_write_cache_offs (dso, machine, offset, data, size); }tools/perf/util/event.c +4
-4@@ -487,7 +487,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *ma al.map =maps__find (machine__kernel_maps (machine), tp->addr);if (al.map &&map__load (al.map) >=0 ) { - al.addr = al.map->map_ip (al.map, tp->addr); + al.addr =map__map_ip (al.map, tp->addr); al.sym =map__find_symbol (al.map, al.addr);if (al.sym) ret +=symbol__fprintf_symname_offs (al.sym, &al, fp);@@ -622,7 +622,7 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,*/ if (load_map)map__load (al->map); - al->addr = al->map->map_ip (al->map, al->addr); + al->addr =map__map_ip (al->map, al->addr); }return al->map;@@ -743,12 +743,12 @@ int machine__resolve(struct machine *machine, struct addr_location *al, }if (!ret && al->sym) {snprintf (al_addr_str, sz,"0x%" PRIx64, - al->map->unmap_ip (al->map, al->sym->start)); +map__unmap_ip (al->map, al->sym->start)); ret =strlist__has_entry (symbol_conf.sym_list, al_addr_str); }if (!ret && symbol_conf.addr_list && al->map) { -unsigned long addr = al->map->unmap_ip (al->map, al->addr); +unsigned long addr =map__unmap_ip (al->map, al->addr); ret =intlist__has_entry (symbol_conf.addr_list, addr);if (!ret && symbol_conf.addr_range) {tools/perf/util/evsel_fprintf.c +1
-1@@ -151,7 +151,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment, printed +=fprintf (fp," <-" );if (map) - addr = map->map_ip (map, node->ip); + addr =map__map_ip (map, node->ip);if (print_ip) {/* Show binary offset for userspace addr */ tools/perf/util/intel-pt.c +5
-5@@ -816,7 +816,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn, dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE))return -ENOENT ; - offset = al.map->map_ip (al.map, *ip); + offset =map__map_ip (al.map, *ip);if (!to_ip && one_map) {struct intel_pt_cache_entry *e;@@ -987,7 +987,7 @@ static int __intel_pt_pgd_ip(uint64_t ip, void *data)if (!thread__find_map (thread, cpumode, ip, &al) || !map__dso (al.map))return -EINVAL ; - offset = al.map->map_ip (al.map, ip); + offset =map__map_ip (al.map, ip);return intel_pt_match_pgd_ip (ptq->pt, ip, offset,map__dso (al.map)->long_name); }@@ -2749,7 +2749,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)for (sym = start; sym; sym =dso__next_symbol (sym)) {if (sym->binding ==STB_GLOBAL && !strcmp (sym->name,"__switch_to" )) { - ip = map->unmap_ip (map, sym->start); + ip =map__unmap_ip (map, sym->start);if (ip >=map__start (map) && ip <map__end (map)) { switch_ip = ip;break ;@@ -2767,7 +2767,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)for (sym = start; sym; sym =dso__next_symbol (sym)) {if (!strcmp (sym->name, ptss)) { - ip = map->unmap_ip (map, sym->start); + ip =map__unmap_ip (map, sym->start);if (ip >=map__start (map) && ip <map__end (map)) { *ptss_ip = ip;break ;@@ -3393,7 +3393,7 @@ static int intel_pt_text_poke(struct intel_pt *pt, union perf_event *event)if (!dso || !dso->auxtrace_cache)continue ; - offset = al.map->map_ip (al.map, addr); + offset =map__map_ip (al.map, addr); e =intel_pt_cache_lookup (dso, machine, offset);if (!e)tools/perf/util/machine.c +8
-8@@ -919,7 +919,7 @@ static int machine__process_ksymbol_register(struct machine *machine, dso =map__dso (map); } - sym =symbol__new (map->map_ip (map,map__start (map)), + sym =symbol__new (map__map_ip (map,map__start (map)), event->ksymbol.len,0 ,0 , event->ksymbol.name);if (!sym)@@ -944,7 +944,7 @@ static int machine__process_ksymbol_unregister(struct machine *machine,else {struct dso *dso =map__dso (map); - sym =dso__find_symbol (dso, map->map_ip (map,map__start (map))); + sym =dso__find_symbol (dso,map__map_ip (map,map__start (map)));if (sym)dso__delete_symbol (dso, sym); }@@ -1279,7 +1279,7 @@ int machine__map_x86_64_entry_trampolines(struct machine *machine, dest_map =maps__find (kmaps, map->pgoff);if (dest_map != map) - map->pgoff = dest_map->map_ip (dest_map, map->pgoff); + map->pgoff =map__map_ip (dest_map, map->pgoff); found = true; }if (found || machine->trampolines_mapped)@@ -3345,7 +3345,7 @@ char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, chreturn NULL ; *modp =__map__is_kmodule (map) ? (char *)map__dso (map)->short_name :NULL ; - *addrp = map->unmap_ip (map, sym->start); + *addrp =map__unmap_ip (map, sym->start);return sym->name; }@@ -3388,17 +3388,17 @@ bool machine__is_lock_function(struct machine *machine, u64 addr)return false; } - machine->sched.text_start = kmap->unmap_ip (kmap, sym->start); + machine->sched.text_start =map__unmap_ip (kmap, sym->start);/* should not fail from here */ sym =machine__find_kernel_symbol_by_name (machine,"__sched_text_end" , &kmap); - machine->sched.text_end = kmap->unmap_ip (kmap, sym->start); + machine->sched.text_end =map__unmap_ip (kmap, sym->start); sym =machine__find_kernel_symbol_by_name (machine,"__lock_text_start" , &kmap); - machine->lock.text_start = kmap->unmap_ip (kmap, sym->start); + machine->lock.text_start =map__unmap_ip (kmap, sym->start); sym =machine__find_kernel_symbol_by_name (machine,"__lock_text_end" , &kmap); - machine->lock.text_end = kmap->unmap_ip (kmap, sym->start); + machine->lock.text_end =map__unmap_ip (kmap, sym->start); }/* failed to get kernel symbols */ tools/perf/util/map.c +5
-5@@ -519,7 +519,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip)if (dso->kernel ==DSO_SPACE__USER )return rip + dso->text_offset; -return map->unmap_ip (map, rip) - map->reloc; +return map__unmap_ip (map, rip) - map->reloc; }/** @@ -539,24 +539,24 @@ u64 map__objdump_2mem(struct map *map, u64 ip)const struct dso *dso =map__dso (map);if (!dso->adjust_symbols) -return map->unmap_ip (map, ip); +return map__unmap_ip (map, ip);if (dso->rel) -return map->unmap_ip (map, ip + map->pgoff); +return map__unmap_ip (map, ip + map->pgoff);/* * kernel modules also have DSO_TYPE_USER in dso->kernel, * but all kernel modules are ET_REL, so won't get here. */ if (dso->kernel ==DSO_SPACE__USER ) -return map->unmap_ip (map, ip - dso->text_offset); +return map__unmap_ip (map, ip - dso->text_offset);return ip + map->reloc; }bool map__contains_symbol (const struct map *map,const struct symbol *sym) { -u64 ip = map->unmap_ip (map, sym->start); +u64 ip =map__unmap_ip (map, sym->start);return ip >=map__start (map) && ip <map__end (map); }tools/perf/util/map.h +10
-0@@ -52,6 +52,16 @@ static inline struct dso *map__dso(const struct map *map)return map->dso; } +static inline u64 map__map_ip (const struct map *map,u64 ip) +{ +return map->map_ip (map, ip); +} + +static inline u64 map__unmap_ip (const struct map *map,u64 ip) +{ +return map->unmap_ip (map, ip); +} +static inline u64 map__start (const struct map *map) {return map->start;tools/perf/util/maps.c +4
-4@@ -194,7 +194,7 @@ struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp)if (map !=NULL &&map__load (map) >=0 ) {if (mapp !=NULL ) *mapp = map; -return map__find_symbol (map, map->map_ip (map, addr)); +return map__find_symbol (map,map__map_ip (map, addr)); }return NULL ;@@ -237,7 +237,7 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams)return -1 ; } - ams->al_addr = ams->ms.map->map_ip (ams->ms.map, ams->addr); + ams->al_addr =map__map_ip (ams->ms.map, ams->addr); ams->ms.sym =map__find_symbol (ams->ms.map, ams->al_addr);return ams->ms.sym ?0 :-1 ;@@ -349,8 +349,8 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) after->start =map__end (map); after->pgoff +=map__end (map) -map__start (pos->map); -assert (pos->map->map_ip (pos->map,map__end (map)) == - after->map_ip (after,map__end (map))); +assert (map__map_ip (pos->map,map__end (map)) == +map__map_ip (after,map__end (map))); err =__maps__insert (maps, after);if (err) goto put_map;tools/perf/util/probe-event.c +4
-4@@ -141,7 +141,7 @@ static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, sym =machine__find_kernel_symbol_by_name (host_machine, name, &map);if (!sym)return -ENOENT ; - *addr = map->unmap_ip (map, sym->start) - + *addr =map__unmap_ip (map, sym->start) - ((reloc) ?0 : map->reloc) - ((reladdr) ?map__start (map) :0 ); }@@ -400,7 +400,7 @@ static int find_alternative_probe_point(struct debuginfo *dinfo,"Consider identifying the final function used at run time and set the probe directly on that.\n" , pp->function); }else - address = map->unmap_ip (map, sym->start) - map->reloc; + address =map__unmap_ip (map, sym->start) - map->reloc;break ; }if (!address) {@@ -2249,7 +2249,7 @@ static int find_perf_probe_point_from_map(struct probe_trace_point *tp, goto out; pp->retprobe = tp->retprobe; - pp->offset = addr - map->unmap_ip (map, sym->start); + pp->offset = addr -map__unmap_ip (map, sym->start); pp->function =strdup (sym->name); ret = pp->function ?0 : -ENOMEM ;@@ -3123,7 +3123,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev, goto err_out; }/* Add one probe point */ - tp->address = map->unmap_ip (map, sym->start) + pp->offset; + tp->address =map__unmap_ip (map, sym->start) + pp->offset;/* Check the kprobe (not in module) is within .text */ if (!pev->uprobes && !pev->target &&tools/perf/util/scripting-engines/trace-event-python.c +1
-1@@ -471,7 +471,7 @@ static PyObject *python_process_callchain(struct perf_sample *sample,struct addr_location node_al;unsigned long offset; - node_al.addr = map->map_ip (map, node->ip); + node_al.addr =map__map_ip (map, node->ip); node_al.map = map; offset =get_offset (node->ms.sym, &node_al);tools/perf/util/sort.c +6
-6@@ -364,7 +364,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms,u64 rip = ip;if (dso && dso->kernel && dso->adjust_symbols) - rip = map->unmap_ip (map, ip); + rip =map__unmap_ip (map, ip); ret +=repsep_snprintf (bf, size,"%-#*llx %c " ,BITS_PER_LONG /4 +2 , rip, o);@@ -375,7 +375,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms,if (sym->type ==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)); + ip -map__unmap_ip (map, sym->start)); }else { ret += repsep_snprintf(bf + ret, size - ret, "%.*s", width - ret,@@ -1147,7 +1147,7 @@ static int _hist_entry__addr_snprintf(struct map_symbol *ms,if (sym->type ==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)); + ip -map__unmap_ip (map, sym->start)); }else { ret += repsep_snprintf(bf + ret, size - ret, "%.*s", width - ret,@@ -2104,9 +2104,9 @@ sort__addr_cmp(struct hist_entry *left, struct hist_entry *right)struct map *right_map = right->ms.map;if (left_map) - left_ip = left_map->unmap_ip (left_map, left_ip); + left_ip =map__unmap_ip (left_map, left_ip);if (right_map) - right_ip = right_map->unmap_ip (right_map, right_ip); + right_ip =map__unmap_ip (right_map, right_ip);return _sort__addr_cmp (left_ip, right_ip); }@@ -2118,7 +2118,7 @@ static int hist_entry__addr_snprintf(struct hist_entry *he, char *bf,struct map *map = he->ms.map;if (map) - ip = map->unmap_ip (map, ip); + ip =map__unmap_ip (map, ip);return repsep_snprintf (bf, size,"%-#*llx" , width, ip); }tools/perf/util/symbol.c +2
-2@@ -896,8 +896,8 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,* So that we look just like we get from .ko files, * i.e. not prelinked, relative to initial_map->start. */ - pos->start = curr_map->map_ip (curr_map, pos->start); - pos->end = curr_map->map_ip (curr_map, pos->end); + 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)) {/* * These symbols are not needed anymore since the tools/perf/util/thread.c +1
-1@@ -464,7 +464,7 @@ int thread__memcpy(struct thread *thread, struct machine *machine,if ( !dso || dso->data.status ==DSO_DATA_STATUS_ERROR ||map__load (al.map) <0 )return -1 ; - offset = al.map->map_ip (al.map, ip); + offset =map__map_ip (al.map, ip);if (is64bit) *is64bit = dso->is_64_bit;tools/perf/util/unwind-libdw.c +1
-1@@ -115,7 +115,7 @@ static int entry(u64 ip, struct unwind_info *ui)pr_debug ("unwind: %s:ip = 0x%" PRIx64" (0x%" PRIx64")\n" , al.sym ? al.sym->name :"''" , ip, - al.map ? al.map->map_ip (al.map, ip) : (u64 )0 ); + al.map ?map__map_ip (al.map, ip) : (u64 )0 );return 0 ; }tools/perf/util/unwind-libunwind-local.c +1
-1@@ -640,7 +640,7 @@ static int entry(u64 ip, struct thread *thread,pr_debug ("unwind: %s:ip = 0x%" PRIx64" (0x%" PRIx64")\n" , al.sym ? al.sym->name :"''" , ip, - al.map ? al.map->map_ip (al.map, ip) : (u64 )0 ); + al.map ?map__map_ip (al.map, ip) : (u64 )0 );return cb (&e, arg); }Signature · 1 line
2.40.0.348.gf938b09366-goog
Signature · 1 line
- Arnaldo
Ian Rogers
reviewerRe: [PATCH v6 02/12] perf map: Add helper for map_ip and unmap_ip
Quoted 7 lines · Arnaldo Carvalho de Melo
Em Tue, Apr 04, 2023 at 01:59:44PM -0700, Ian Rogers escreveu:
Quoted 4 lines
Later changes will add reference count checking for struct map, add a helper function to invoke the
map_ipandunmap_ipfunction pointers. The helper allows the reference count check to be in fewer places.
Quoted 5 lines
Fixing these:
⬢[acme@toolbox perf-tools-next]$ find
tools/perf/-name "*.[ch]" | xargs grep -- '->map_ip('tools/perf/arch/s390/annotate/instructions.c:map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr))==tools/perf/util/cs-etm.c: offset =al.map->map_ip(al.map, address);
Agreed on fixing these.
Quoted 2 lines
tools/perf/util/annotate.c: map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr)) == tools/perf/util/annotate.c: map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr)) ==
Here the dispatched function pointer is from map and the first argument is target.ms.map, so the helper function doesn't apply.
Quoted 2 lines
tools/perf/util/map.c: * e.g.map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip)))== iptools/perf/util/map.h: returnmap->map_ip(map, ip);
This is a comment and the helper definition.
Quoted 4 lines
⬢[acme@toolbox perf-tools-next]$
⬢[acme@toolbox perf-tools-next]$ find
tools/perf/-name "*.[ch]" | xargs grep -- '->unmap_ip('tools/perf/arch/powerpc/util/sym-handling.c: if (map->unmap_ip(map, sym->start)==tev->point.address) {
Agreed on fixing this.
Thanks,
Ian
Quoted 678 lines
tools/perf/util/map.h: returnmap->unmap_ip(map, ip); ⬢[acme@toolbox perf-tools-next]$Quoted 669 lines
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/arch/s390/annotate/instructions.c | 3 ++- tools/perf/builtin-kallsyms.c | 2 +- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-lock.c | 4 ++-- tools/perf/builtin-script.c | 2 +- tools/perf/tests/vmlinux-kallsyms.c | 10 +++++----- tools/perf/util/annotate.c | 16 +++++++++------- tools/perf/util/bpf_lock_contention.c | 4 ++-- tools/perf/util/dlfilter.c | 2 +- tools/perf/util/dso.c | 6 ++++-- tools/perf/util/event.c | 8 ++++---- tools/perf/util/evsel_fprintf.c | 2 +- tools/perf/util/intel-pt.c | 10 +++++----- tools/perf/util/machine.c | 16 ++++++++-------- tools/perf/util/map.c | 10 +++++----- tools/perf/util/map.h | 10 ++++++++++ tools/perf/util/maps.c | 8 ++++---- tools/perf/util/probe-event.c | 8 ++++---- .../util/scripting-engines/trace-event-python.c | 2 +- tools/perf/util/sort.c | 12 ++++++------ tools/perf/util/symbol.c | 4 ++-- tools/perf/util/thread.c | 2 +- tools/perf/util/unwind-libdw.c | 2 +- tools/perf/util/unwind-libunwind-local.c | 2 +- 24 files changed, 81 insertions(+), 66 deletions(-)tools/perf/arch/s390/annotate/instructions.c +2
-1@@ -39,7 +39,8 @@ static int s390_call__parse(struct arch *arch, struct ins_operands *ops, target.addr =map__objdump_2mem (map, ops->target.addr);if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;return 0 ;tools/perf/builtin-kallsyms.c +1
-1@@ -39,7 +39,7 @@ static int __cmd_kallsyms(int argc, const char **argv) dso =map__dso (map);printf ("%s: %s %s %#" PRIx64"-%#" PRIx64" (%#" PRIx64"-%#" PRIx64")\n" , symbol->name, dso->short_name, dso->long_name, - map->unmap_ip (map, symbol->start), map->unmap_ip (map, symbol->end), +map__unmap_ip (map, symbol->start),map__unmap_ip (map, symbol->end), symbol->start, symbol->end); }tools/perf/builtin-kmem.c +1
-1@@ -1024,7 +1024,7 @@ static void __print_slab_result(struct rb_root *root,if (sym !=NULL )snprintf (buf,sizeof (buf),"%s+%" PRIx64"" , sym->name, - addr - map->unmap_ip (map, sym->start)); + addr -map__unmap_ip (map, sym->start));else snprintf (buf,sizeof (buf),"%#" PRIx64"" , addr);printf (" %-34s |" , buf);tools/perf/builtin-lock.c +2
-2@@ -900,7 +900,7 @@ static int get_symbol_name_offset(struct map *map, struct symbol *sym, u64 ip,return 0 ; } - offset = map->map_ip (map, ip) - sym->start; + offset =map__map_ip (map, ip) - sym->start;if (offset)return scnprintf (buf, size,"%s+%#lx" , sym->name, offset);@@ -1070,7 +1070,7 @@ static int report_lock_contention_begin_event(struct evsel *evsel,return -ENOMEM ; } - addrs[filters.nr_addrs++] = kmap->unmap_ip (kmap, sym->start); + addrs[filters.nr_addrs++] =map__unmap_ip (kmap, sym->start); filters.addrs = addrs; } }tools/perf/builtin-script.c +1
-1@@ -1088,7 +1088,7 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,/* Load maps to ensure dso->is_64_bit has been updated */ map__load (al.map); - offset = al.map->map_ip (al.map, start); + offset =map__map_ip (al.map, start); len =dso__data_read_offset (dso, machine, offset, (u8 *)buffer, end - start +MAXINSN );tools/perf/tests/vmlinux-kallsyms.c +5
-5@@ -13,7 +13,7 @@#include "debug.h" #include "machine.h" -#define UM (x) kallsyms_map->unmap_ip(kallsyms_map, (x)) +#define UM (x) map__unmap_ip(kallsyms_map, (x))static bool is_ignored_symbol (const char *name,char type) {@@ -221,8 +221,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unusedif (sym->start == sym->end)continue ; - mem_start = vmlinux_map->unmap_ip (vmlinux_map, sym->start); - mem_end = vmlinux_map->unmap_ip (vmlinux_map, sym->end); + mem_start =map__unmap_ip (vmlinux_map, sym->start); + mem_end =map__unmap_ip (vmlinux_map, sym->end); first_pair =machine__find_kernel_symbol (&kallsyms, mem_start,NULL ); pair = first_pair;@@ -319,8 +319,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unusedmaps__for_each_entry (maps, rb_node) {struct map *pair, *map = rb_node->map; - mem_start = vmlinux_map->unmap_ip (vmlinux_map,map__start (map)); - mem_end = vmlinux_map->unmap_ip (vmlinux_map,map__end (map)); + mem_start =map__unmap_ip (vmlinux_map,map__start (map)); + mem_end =map__unmap_ip (vmlinux_map,map__end (map)); pair =maps__find (kallsyms.kmaps, mem_start);if (pair ==NULL || pair->priv)tools/perf/util/annotate.c +9
-7@@ -272,7 +272,8 @@ static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_s target.addr =map__objdump_2mem (map, ops->target.addr);if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;return 0 ;@@ -376,8 +377,8 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s } target.addr =map__objdump_2mem (map, ops->target.addr); - start = map->unmap_ip (map, sym->start), - end = map->unmap_ip (map, sym->end); + start =map__unmap_ip (map, sym->start); + end =map__unmap_ip (map, sym->end); ops->target.outside = target.addr < start || target.addr > end;@@ -400,7 +401,8 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s* the symbol searching and disassembly should be done. */ if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;if (!ops->target.outside) {@@ -881,7 +883,7 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms,unsigned offset;struct sym_hist *h; -pr_debug3 ("%s: addr=%#" PRIx64"\n" , __func__, ms->map->unmap_ip (ms->map, addr)); +pr_debug3 ("%s: addr=%#" PRIx64"\n" , __func__,map__unmap_ip (ms->map, addr));if ((addr < sym->start || addr >= sym->end) && (addr != sym->end || sym->start != sym->end)) {@@ -1977,8 +1979,8 @@ static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)return err;pr_debug ("%s: filename=%s, sym=%s, start=%#" PRIx64", end=%#" PRIx64"\n" , __func__, - symfs_filename, sym->name, map->unmap_ip (map, sym->start), - map->unmap_ip (map, sym->end)); + symfs_filename, sym->name,map__unmap_ip (map, sym->start), +map__unmap_ip (map, sym->end));pr_debug ("annotating [%p] %30s : [%p] %30s\n" , dso, dso->long_name, sym, sym->name);tools/perf/util/bpf_lock_contention.c +2
-2@@ -74,7 +74,7 @@ int lock_contention_prepare(struct lock_contention *con)continue ; } - addrs[con->filters->nr_addrs++] = kmap->unmap_ip (kmap, sym->start); + addrs[con->filters->nr_addrs++] =map__unmap_ip (kmap, sym->start); con->filters->addrs = addrs; } naddrs = con->filters->nr_addrs;@@ -233,7 +233,7 @@ static const char *lock_contention_get_name(struct lock_contention *con,if (sym) {unsigned long offset; - offset = kmap->map_ip (kmap, addr) - sym->start; + offset =map__map_ip (kmap, addr) - sym->start;if (offset ==0 )return sym->name;tools/perf/util/dlfilter.c +1
-1@@ -278,7 +278,7 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len) map = a.map; have_map: - offset = map->map_ip (map, ip); + offset =map__map_ip (map, ip);if (ip + len >=map__end (map)) len =map__end (map) - ip;return dso__data_read_offset (map__dso (map), d->machine, offset, buf, len);tools/perf/util/dso.c +4
-2@@ -1122,7 +1122,8 @@ ssize_t dso__data_read_addr(struct dso *dso, struct map *map, struct machine *machine, u64 addr, u8 *data, ssize_t size) { - u64 offset = map->map_ip(map, addr); + u64 offset = map__map_ip(map, addr); +return dso__data_read_offset (dso, machine, offset, data, size); }@@ -1162,7 +1163,8 @@ ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map, struct machine *machine, u64 addr, const u8 *data, ssize_t size) { - u64 offset = map->map_ip(map, addr); + u64 offset = map__map_ip(map, addr); +return dso__data_write_cache_offs (dso, machine, offset, data, size); }tools/perf/util/event.c +4
-4@@ -487,7 +487,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *ma al.map =maps__find (machine__kernel_maps (machine), tp->addr);if (al.map &&map__load (al.map) >=0 ) { - al.addr = al.map->map_ip (al.map, tp->addr); + al.addr =map__map_ip (al.map, tp->addr); al.sym =map__find_symbol (al.map, al.addr);if (al.sym) ret +=symbol__fprintf_symname_offs (al.sym, &al, fp);@@ -622,7 +622,7 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,*/ if (load_map)map__load (al->map); - al->addr = al->map->map_ip (al->map, al->addr); + al->addr =map__map_ip (al->map, al->addr); }return al->map;@@ -743,12 +743,12 @@ int machine__resolve(struct machine *machine, struct addr_location *al, }if (!ret && al->sym) {snprintf (al_addr_str, sz,"0x%" PRIx64, - al->map->unmap_ip (al->map, al->sym->start)); +map__unmap_ip (al->map, al->sym->start)); ret =strlist__has_entry (symbol_conf.sym_list, al_addr_str); }if (!ret && symbol_conf.addr_list && al->map) { -unsigned long addr = al->map->unmap_ip (al->map, al->addr); +unsigned long addr =map__unmap_ip (al->map, al->addr); ret =intlist__has_entry (symbol_conf.addr_list, addr);if (!ret && symbol_conf.addr_range) {tools/perf/util/evsel_fprintf.c +1
-1@@ -151,7 +151,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment, printed +=fprintf (fp," <-" );if (map) - addr = map->map_ip (map, node->ip); + addr =map__map_ip (map, node->ip);if (print_ip) {/* Show binary offset for userspace addr */ tools/perf/util/intel-pt.c +5
-5@@ -816,7 +816,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn, dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE))return -ENOENT ; - offset = al.map->map_ip (al.map, *ip); + offset =map__map_ip (al.map, *ip);if (!to_ip && one_map) {struct intel_pt_cache_entry *e;@@ -987,7 +987,7 @@ static int __intel_pt_pgd_ip(uint64_t ip, void *data)if (!thread__find_map (thread, cpumode, ip, &al) || !map__dso (al.map))return -EINVAL ; - offset = al.map->map_ip (al.map, ip); + offset =map__map_ip (al.map, ip);return intel_pt_match_pgd_ip (ptq->pt, ip, offset,map__dso (al.map)->long_name); }@@ -2749,7 +2749,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)for (sym = start; sym; sym =dso__next_symbol (sym)) {if (sym->binding ==STB_GLOBAL && !strcmp (sym->name,"__switch_to" )) { - ip = map->unmap_ip (map, sym->start); + ip =map__unmap_ip (map, sym->start);if (ip >=map__start (map) && ip <map__end (map)) { switch_ip = ip;break ;@@ -2767,7 +2767,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)for (sym = start; sym; sym =dso__next_symbol (sym)) {if (!strcmp (sym->name, ptss)) { - ip = map->unmap_ip (map, sym->start); + ip =map__unmap_ip (map, sym->start);if (ip >=map__start (map) && ip <map__end (map)) { *ptss_ip = ip;break ;@@ -3393,7 +3393,7 @@ static int intel_pt_text_poke(struct intel_pt *pt, union perf_event *event)if (!dso || !dso->auxtrace_cache)continue ; - offset = al.map->map_ip (al.map, addr); + offset =map__map_ip (al.map, addr); e =intel_pt_cache_lookup (dso, machine, offset);if (!e)tools/perf/util/machine.c +8
-8@@ -919,7 +919,7 @@ static int machine__process_ksymbol_register(struct machine *machine, dso =map__dso (map); } - sym =symbol__new (map->map_ip (map,map__start (map)), + sym =symbol__new (map__map_ip (map,map__start (map)), event->ksymbol.len,0 ,0 , event->ksymbol.name);if (!sym)@@ -944,7 +944,7 @@ static int machine__process_ksymbol_unregister(struct machine *machine,else {struct dso *dso =map__dso (map); - sym =dso__find_symbol (dso, map->map_ip (map,map__start (map))); + sym =dso__find_symbol (dso,map__map_ip (map,map__start (map)));if (sym)dso__delete_symbol (dso, sym); }@@ -1279,7 +1279,7 @@ int machine__map_x86_64_entry_trampolines(struct machine *machine, dest_map =maps__find (kmaps, map->pgoff);if (dest_map != map) - map->pgoff = dest_map->map_ip (dest_map, map->pgoff); + map->pgoff =map__map_ip (dest_map, map->pgoff); found = true; }if (found || machine->trampolines_mapped)@@ -3345,7 +3345,7 @@ char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, chreturn NULL ; *modp =__map__is_kmodule (map) ? (char *)map__dso (map)->short_name :NULL ; - *addrp = map->unmap_ip (map, sym->start); + *addrp =map__unmap_ip (map, sym->start);return sym->name; }@@ -3388,17 +3388,17 @@ bool machine__is_lock_function(struct machine *machine, u64 addr)return false; } - machine->sched.text_start = kmap->unmap_ip (kmap, sym->start); + machine->sched.text_start =map__unmap_ip (kmap, sym->start);/* should not fail from here */ sym =machine__find_kernel_symbol_by_name (machine,"__sched_text_end" , &kmap); - machine->sched.text_end = kmap->unmap_ip (kmap, sym->start); + machine->sched.text_end =map__unmap_ip (kmap, sym->start); sym =machine__find_kernel_symbol_by_name (machine,"__lock_text_start" , &kmap); - machine->lock.text_start = kmap->unmap_ip (kmap, sym->start); + machine->lock.text_start =map__unmap_ip (kmap, sym->start); sym =machine__find_kernel_symbol_by_name (machine,"__lock_text_end" , &kmap); - machine->lock.text_end = kmap->unmap_ip (kmap, sym->start); + machine->lock.text_end =map__unmap_ip (kmap, sym->start); }/* failed to get kernel symbols */ tools/perf/util/map.c +5
-5@@ -519,7 +519,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip)if (dso->kernel ==DSO_SPACE__USER )return rip + dso->text_offset; -return map->unmap_ip (map, rip) - map->reloc; +return map__unmap_ip (map, rip) - map->reloc; }/** @@ -539,24 +539,24 @@ u64 map__objdump_2mem(struct map *map, u64 ip)const struct dso *dso =map__dso (map);if (!dso->adjust_symbols) -return map->unmap_ip (map, ip); +return map__unmap_ip (map, ip);if (dso->rel) -return map->unmap_ip (map, ip + map->pgoff); +return map__unmap_ip (map, ip + map->pgoff);/* * kernel modules also have DSO_TYPE_USER in dso->kernel, * but all kernel modules are ET_REL, so won't get here. */ if (dso->kernel ==DSO_SPACE__USER ) -return map->unmap_ip (map, ip - dso->text_offset); +return map__unmap_ip (map, ip - dso->text_offset);return ip + map->reloc; }bool map__contains_symbol (const struct map *map,const struct symbol *sym) { -u64 ip = map->unmap_ip (map, sym->start); +u64 ip =map__unmap_ip (map, sym->start);return ip >=map__start (map) && ip <map__end (map); }tools/perf/util/map.h +10
-0@@ -52,6 +52,16 @@ static inline struct dso *map__dso(const struct map *map)return map->dso; } +static inline u64 map__map_ip (const struct map *map,u64 ip) +{ +return map->map_ip (map, ip); +} + +static inline u64 map__unmap_ip (const struct map *map,u64 ip) +{ +return map->unmap_ip (map, ip); +} +static inline u64 map__start (const struct map *map) {return map->start;tools/perf/util/maps.c +4
-4@@ -194,7 +194,7 @@ struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp)if (map !=NULL &&map__load (map) >=0 ) {if (mapp !=NULL ) *mapp = map; -return map__find_symbol (map, map->map_ip (map, addr)); +return map__find_symbol (map,map__map_ip (map, addr)); }return NULL ;@@ -237,7 +237,7 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams)return -1 ; } - ams->al_addr = ams->ms.map->map_ip (ams->ms.map, ams->addr); + ams->al_addr =map__map_ip (ams->ms.map, ams->addr); ams->ms.sym =map__find_symbol (ams->ms.map, ams->al_addr);return ams->ms.sym ?0 :-1 ;@@ -349,8 +349,8 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) after->start =map__end (map); after->pgoff +=map__end (map) -map__start (pos->map); -assert (pos->map->map_ip (pos->map,map__end (map)) == - after->map_ip (after,map__end (map))); +assert (map__map_ip (pos->map,map__end (map)) == +map__map_ip (after,map__end (map))); err =__maps__insert (maps, after);if (err) goto put_map;tools/perf/util/probe-event.c +4
-4@@ -141,7 +141,7 @@ static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, sym =machine__find_kernel_symbol_by_name (host_machine, name, &map);if (!sym)return -ENOENT ; - *addr = map->unmap_ip (map, sym->start) - + *addr =map__unmap_ip (map, sym->start) - ((reloc) ?0 : map->reloc) - ((reladdr) ?map__start (map) :0 ); }@@ -400,7 +400,7 @@ static int find_alternative_probe_point(struct debuginfo *dinfo,"Consider identifying the final function used at run time and set the probe directly on that.\n" , pp->function); }else - address = map->unmap_ip (map, sym->start) - map->reloc; + address =map__unmap_ip (map, sym->start) - map->reloc;break ; }if (!address) {@@ -2249,7 +2249,7 @@ static int find_perf_probe_point_from_map(struct probe_trace_point *tp, goto out; pp->retprobe = tp->retprobe; - pp->offset = addr - map->unmap_ip (map, sym->start); + pp->offset = addr -map__unmap_ip (map, sym->start); pp->function =strdup (sym->name); ret = pp->function ?0 : -ENOMEM ;@@ -3123,7 +3123,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev, goto err_out; }/* Add one probe point */ - tp->address = map->unmap_ip (map, sym->start) + pp->offset; + tp->address =map__unmap_ip (map, sym->start) + pp->offset;/* Check the kprobe (not in module) is within .text */ if (!pev->uprobes && !pev->target &&tools/perf/util/scripting-engines/trace-event-python.c +1
-1@@ -471,7 +471,7 @@ static PyObject *python_process_callchain(struct perf_sample *sample,struct addr_location node_al;unsigned long offset; - node_al.addr = map->map_ip (map, node->ip); + node_al.addr =map__map_ip (map, node->ip); node_al.map = map; offset =get_offset (node->ms.sym, &node_al);tools/perf/util/sort.c +6
-6@@ -364,7 +364,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms,u64 rip = ip;if (dso && dso->kernel && dso->adjust_symbols) - rip = map->unmap_ip (map, ip); + rip =map__unmap_ip (map, ip); ret +=repsep_snprintf (bf, size,"%-#*llx %c " ,BITS_PER_LONG /4 +2 , rip, o);@@ -375,7 +375,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms,if (sym->type ==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)); + ip -map__unmap_ip (map, sym->start)); }else { ret += repsep_snprintf(bf + ret, size - ret, "%.*s", width - ret,@@ -1147,7 +1147,7 @@ static int _hist_entry__addr_snprintf(struct map_symbol *ms,if (sym->type ==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)); + ip -map__unmap_ip (map, sym->start)); }else { ret += repsep_snprintf(bf + ret, size - ret, "%.*s", width - ret,@@ -2104,9 +2104,9 @@ sort__addr_cmp(struct hist_entry *left, struct hist_entry *right)struct map *right_map = right->ms.map;if (left_map) - left_ip = left_map->unmap_ip (left_map, left_ip); + left_ip =map__unmap_ip (left_map, left_ip);if (right_map) - right_ip = right_map->unmap_ip (right_map, right_ip); + right_ip =map__unmap_ip (right_map, right_ip);return _sort__addr_cmp (left_ip, right_ip); }@@ -2118,7 +2118,7 @@ static int hist_entry__addr_snprintf(struct hist_entry *he, char *bf,struct map *map = he->ms.map;if (map) - ip = map->unmap_ip (map, ip); + ip =map__unmap_ip (map, ip);return repsep_snprintf (bf, size,"%-#*llx" , width, ip); }tools/perf/util/symbol.c +2
-2@@ -896,8 +896,8 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,* So that we look just like we get from .ko files, * i.e. not prelinked, relative to initial_map->start. */ - pos->start = curr_map->map_ip (curr_map, pos->start); - pos->end = curr_map->map_ip (curr_map, pos->end); + 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)) {/* * These symbols are not needed anymore since the tools/perf/util/thread.c +1
-1@@ -464,7 +464,7 @@ int thread__memcpy(struct thread *thread, struct machine *machine,if ( !dso || dso->data.status ==DSO_DATA_STATUS_ERROR ||map__load (al.map) <0 )return -1 ; - offset = al.map->map_ip (al.map, ip); + offset =map__map_ip (al.map, ip);if (is64bit) *is64bit = dso->is_64_bit;tools/perf/util/unwind-libdw.c +1
-1@@ -115,7 +115,7 @@ static int entry(u64 ip, struct unwind_info *ui)pr_debug ("unwind: %s:ip = 0x%" PRIx64" (0x%" PRIx64")\n" , al.sym ? al.sym->name :"''" , ip, - al.map ? al.map->map_ip (al.map, ip) : (u64 )0 ); + al.map ?map__map_ip (al.map, ip) : (u64 )0 );return 0 ; }tools/perf/util/unwind-libunwind-local.c +1
-2@@ -640,7 +640,7 @@ static int entry(u64 ip, struct thread *thread,pr_debug ("unwind: %s:ip = 0x%" PRIx64" (0x%" PRIx64")\n" , al.sym ? al.sym->name :"''" , ip, - al.map ? al.map->map_ip (al.map, ip) : (u64 )0 ); + al.map ?map__map_ip (al.map, ip) : (u64 )0 );return cb (&e, arg); } --Quoted 2 lines
2.40.0.348.gf938b09366-goog
--
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v6 02/12] perf map: Add helper for map_ip and unmap_ip
Em Thu, Apr 06, 2023 at 06:12:13PM -0700, Ian Rogers escreveu:
Quoted 14 lines · Arnaldo Carvalho de Melo
Quoted 7 lines
Em Tue, Apr 04, 2023 at 01:59:44PM -0700, Ian Rogers escreveu:
Quoted 4 lines
Later changes will add reference count checking for struct map, add a helper function to invoke the
map_ipandunmap_ipfunction pointers. The helper allows the reference count check to be in fewer places.Quoted 5 lines
Fixing these:
⬢[acme@toolbox perf-tools-next]$ find
tools/perf/-name "*.[ch]" | xargs grep -- '->map_ip('tools/perf/arch/s390/annotate/instructions.c:map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr))==tools/perf/util/cs-etm.c: offset =al.map->map_ip(al.map, address);
Quoted 7 lines
Agreed on fixing these.
Quoted 2 lines
tools/perf/util/annotate.c: map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr)) == tools/perf/util/annotate.c: map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr)) ==Here the dispatched function pointer is from map and the first argument is target.ms.map, so the helper function doesn't apply.
But they are the same (ditto for jump__parse):
static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
{
char *endptr, *tok, *name;
struct map *map = ms->map;
struct addr_map_symbol target = {
.ms = { .map = map, },
};
ops->target.addr = strtoull(ops->raw, &endptr, 16);
name = strchr(endptr, '<');
if (name == NULL)
goto indirect_call;
name++;
if (arch->objdump.skip_functions_char &&
strchr(name, arch->objdump.skip_functions_char))
return -1;
tok = strchr(name, '>');
if (tok == NULL)
return -1;
*tok = '\0';
ops->target.name = strdup(name);
*tok = '>';
if (ops->target.name == NULL)
return -1;
find_target:
target.addr = map__objdump_2mem(map, ops->target.addr);
if (maps__find_ams(ms->maps, &target) == 0 &&
map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
ops->target.sym = target.ms.sym;
return 0;indirect_call:
tok = strchr(endptr, '*');
if (tok != NULL) {
endptr++;
/* Indirect call can use a non-rip register and offset: callq *0x8(%rbx).
* Do not parse such instruction. */
if (strstr(endptr, "(%r") == NULL)
ops->target.addr = strtoull(endptr, NULL, 16);
}
goto find_target;
}Quoted 693 lines
Quoted 2 lines
tools/perf/util/map.c: * e.g.map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip)))== iptools/perf/util/map.h: returnmap->map_ip(map, ip);This is a comment and the helper definition.
Quoted 4 lines
⬢[acme@toolbox perf-tools-next]$
⬢[acme@toolbox perf-tools-next]$ find
tools/perf/-name "*.[ch]" | xargs grep -- '->unmap_ip('tools/perf/arch/powerpc/util/sym-handling.c: if (map->unmap_ip(map, sym->start)==tev->point.address) {Agreed on fixing this.
Thanks,
IanQuoted 678 lines
tools/perf/util/map.h: returnmap->unmap_ip(map, ip); ⬢[acme@toolbox perf-tools-next]$Quoted 669 lines
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/arch/s390/annotate/instructions.c | 3 ++- tools/perf/builtin-kallsyms.c | 2 +- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-lock.c | 4 ++-- tools/perf/builtin-script.c | 2 +- tools/perf/tests/vmlinux-kallsyms.c | 10 +++++----- tools/perf/util/annotate.c | 16 +++++++++------- tools/perf/util/bpf_lock_contention.c | 4 ++-- tools/perf/util/dlfilter.c | 2 +- tools/perf/util/dso.c | 6 ++++-- tools/perf/util/event.c | 8 ++++---- tools/perf/util/evsel_fprintf.c | 2 +- tools/perf/util/intel-pt.c | 10 +++++----- tools/perf/util/machine.c | 16 ++++++++-------- tools/perf/util/map.c | 10 +++++----- tools/perf/util/map.h | 10 ++++++++++ tools/perf/util/maps.c | 8 ++++---- tools/perf/util/probe-event.c | 8 ++++---- .../util/scripting-engines/trace-event-python.c | 2 +- tools/perf/util/sort.c | 12 ++++++------ tools/perf/util/symbol.c | 4 ++-- tools/perf/util/thread.c | 2 +- tools/perf/util/unwind-libdw.c | 2 +- tools/perf/util/unwind-libunwind-local.c | 2 +- 24 files changed, 81 insertions(+), 66 deletions(-)tools/perf/arch/s390/annotate/instructions.c +2
-1@@ -39,7 +39,8 @@ static int s390_call__parse(struct arch *arch, struct ins_operands *ops, target.addr =map__objdump_2mem (map, ops->target.addr);if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;return 0 ;tools/perf/builtin-kallsyms.c +1
-1@@ -39,7 +39,7 @@ static int __cmd_kallsyms(int argc, const char **argv) dso =map__dso (map);printf ("%s: %s %s %#" PRIx64"-%#" PRIx64" (%#" PRIx64"-%#" PRIx64")\n" , symbol->name, dso->short_name, dso->long_name, - map->unmap_ip (map, symbol->start), map->unmap_ip (map, symbol->end), +map__unmap_ip (map, symbol->start),map__unmap_ip (map, symbol->end), symbol->start, symbol->end); }tools/perf/builtin-kmem.c +1
-1@@ -1024,7 +1024,7 @@ static void __print_slab_result(struct rb_root *root,if (sym !=NULL )snprintf (buf,sizeof (buf),"%s+%" PRIx64"" , sym->name, - addr - map->unmap_ip (map, sym->start)); + addr -map__unmap_ip (map, sym->start));else snprintf (buf,sizeof (buf),"%#" PRIx64"" , addr);printf (" %-34s |" , buf);tools/perf/builtin-lock.c +2
-2@@ -900,7 +900,7 @@ static int get_symbol_name_offset(struct map *map, struct symbol *sym, u64 ip,return 0 ; } - offset = map->map_ip (map, ip) - sym->start; + offset =map__map_ip (map, ip) - sym->start;if (offset)return scnprintf (buf, size,"%s+%#lx" , sym->name, offset);@@ -1070,7 +1070,7 @@ static int report_lock_contention_begin_event(struct evsel *evsel,return -ENOMEM ; } - addrs[filters.nr_addrs++] = kmap->unmap_ip (kmap, sym->start); + addrs[filters.nr_addrs++] =map__unmap_ip (kmap, sym->start); filters.addrs = addrs; } }tools/perf/builtin-script.c +1
-1@@ -1088,7 +1088,7 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,/* Load maps to ensure dso->is_64_bit has been updated */ map__load (al.map); - offset = al.map->map_ip (al.map, start); + offset =map__map_ip (al.map, start); len =dso__data_read_offset (dso, machine, offset, (u8 *)buffer, end - start +MAXINSN );tools/perf/tests/vmlinux-kallsyms.c +5
-5@@ -13,7 +13,7 @@#include "debug.h" #include "machine.h" -#define UM (x) kallsyms_map->unmap_ip(kallsyms_map, (x)) +#define UM (x) map__unmap_ip(kallsyms_map, (x))static bool is_ignored_symbol (const char *name,char type) {@@ -221,8 +221,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unusedif (sym->start == sym->end)continue ; - mem_start = vmlinux_map->unmap_ip (vmlinux_map, sym->start); - mem_end = vmlinux_map->unmap_ip (vmlinux_map, sym->end); + mem_start =map__unmap_ip (vmlinux_map, sym->start); + mem_end =map__unmap_ip (vmlinux_map, sym->end); first_pair =machine__find_kernel_symbol (&kallsyms, mem_start,NULL ); pair = first_pair;@@ -319,8 +319,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unusedmaps__for_each_entry (maps, rb_node) {struct map *pair, *map = rb_node->map; - mem_start = vmlinux_map->unmap_ip (vmlinux_map,map__start (map)); - mem_end = vmlinux_map->unmap_ip (vmlinux_map,map__end (map)); + mem_start =map__unmap_ip (vmlinux_map,map__start (map)); + mem_end =map__unmap_ip (vmlinux_map,map__end (map)); pair =maps__find (kallsyms.kmaps, mem_start);if (pair ==NULL || pair->priv)tools/perf/util/annotate.c +9
-7@@ -272,7 +272,8 @@ static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_s target.addr =map__objdump_2mem (map, ops->target.addr);if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;return 0 ;@@ -376,8 +377,8 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s } target.addr =map__objdump_2mem (map, ops->target.addr); - start = map->unmap_ip (map, sym->start), - end = map->unmap_ip (map, sym->end); + start =map__unmap_ip (map, sym->start); + end =map__unmap_ip (map, sym->end); ops->target.outside = target.addr < start || target.addr > end;@@ -400,7 +401,8 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s* the symbol searching and disassembly should be done. */ if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == ops->target.addr) +map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == + ops->target.addr) ops->target.sym = target.ms.sym;if (!ops->target.outside) {@@ -881,7 +883,7 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms,unsigned offset;struct sym_hist *h; -pr_debug3 ("%s: addr=%#" PRIx64"\n" , __func__, ms->map->unmap_ip (ms->map, addr)); +pr_debug3 ("%s: addr=%#" PRIx64"\n" , __func__,map__unmap_ip (ms->map, addr));if ((addr < sym->start || addr >= sym->end) && (addr != sym->end || sym->start != sym->end)) {@@ -1977,8 +1979,8 @@ static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)return err;pr_debug ("%s: filename=%s, sym=%s, start=%#" PRIx64", end=%#" PRIx64"\n" , __func__, - symfs_filename, sym->name, map->unmap_ip (map, sym->start), - map->unmap_ip (map, sym->end)); + symfs_filename, sym->name,map__unmap_ip (map, sym->start), +map__unmap_ip (map, sym->end));pr_debug ("annotating [%p] %30s : [%p] %30s\n" , dso, dso->long_name, sym, sym->name);tools/perf/util/bpf_lock_contention.c +2
-2@@ -74,7 +74,7 @@ int lock_contention_prepare(struct lock_contention *con)continue ; } - addrs[con->filters->nr_addrs++] = kmap->unmap_ip (kmap, sym->start); + addrs[con->filters->nr_addrs++] =map__unmap_ip (kmap, sym->start); con->filters->addrs = addrs; } naddrs = con->filters->nr_addrs;@@ -233,7 +233,7 @@ static const char *lock_contention_get_name(struct lock_contention *con,if (sym) {unsigned long offset; - offset = kmap->map_ip (kmap, addr) - sym->start; + offset =map__map_ip (kmap, addr) - sym->start;if (offset ==0 )return sym->name;tools/perf/util/dlfilter.c +1
-1@@ -278,7 +278,7 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len) map = a.map; have_map: - offset = map->map_ip (map, ip); + offset =map__map_ip (map, ip);if (ip + len >=map__end (map)) len =map__end (map) - ip;return dso__data_read_offset (map__dso (map), d->machine, offset, buf, len);tools/perf/util/dso.c +4
-2@@ -1122,7 +1122,8 @@ ssize_t dso__data_read_addr(struct dso *dso, struct map *map, struct machine *machine, u64 addr, u8 *data, ssize_t size) { - u64 offset = map->map_ip(map, addr); + u64 offset = map__map_ip(map, addr); +return dso__data_read_offset (dso, machine, offset, data, size); }@@ -1162,7 +1163,8 @@ ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map, struct machine *machine, u64 addr, const u8 *data, ssize_t size) { - u64 offset = map->map_ip(map, addr); + u64 offset = map__map_ip(map, addr); +return dso__data_write_cache_offs (dso, machine, offset, data, size); }tools/perf/util/event.c +4
-4@@ -487,7 +487,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *ma al.map =maps__find (machine__kernel_maps (machine), tp->addr);if (al.map &&map__load (al.map) >=0 ) { - al.addr = al.map->map_ip (al.map, tp->addr); + al.addr =map__map_ip (al.map, tp->addr); al.sym =map__find_symbol (al.map, al.addr);if (al.sym) ret +=symbol__fprintf_symname_offs (al.sym, &al, fp);@@ -622,7 +622,7 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,*/ if (load_map)map__load (al->map); - al->addr = al->map->map_ip (al->map, al->addr); + al->addr =map__map_ip (al->map, al->addr); }return al->map;@@ -743,12 +743,12 @@ int machine__resolve(struct machine *machine, struct addr_location *al, }if (!ret && al->sym) {snprintf (al_addr_str, sz,"0x%" PRIx64, - al->map->unmap_ip (al->map, al->sym->start)); +map__unmap_ip (al->map, al->sym->start)); ret =strlist__has_entry (symbol_conf.sym_list, al_addr_str); }if (!ret && symbol_conf.addr_list && al->map) { -unsigned long addr = al->map->unmap_ip (al->map, al->addr); +unsigned long addr =map__unmap_ip (al->map, al->addr); ret =intlist__has_entry (symbol_conf.addr_list, addr);if (!ret && symbol_conf.addr_range) {tools/perf/util/evsel_fprintf.c +1
-1@@ -151,7 +151,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment, printed +=fprintf (fp," <-" );if (map) - addr = map->map_ip (map, node->ip); + addr =map__map_ip (map, node->ip);if (print_ip) {/* Show binary offset for userspace addr */ tools/perf/util/intel-pt.c +5
-5@@ -816,7 +816,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn, dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE))return -ENOENT ; - offset = al.map->map_ip (al.map, *ip); + offset =map__map_ip (al.map, *ip);if (!to_ip && one_map) {struct intel_pt_cache_entry *e;@@ -987,7 +987,7 @@ static int __intel_pt_pgd_ip(uint64_t ip, void *data)if (!thread__find_map (thread, cpumode, ip, &al) || !map__dso (al.map))return -EINVAL ; - offset = al.map->map_ip (al.map, ip); + offset =map__map_ip (al.map, ip);return intel_pt_match_pgd_ip (ptq->pt, ip, offset,map__dso (al.map)->long_name); }@@ -2749,7 +2749,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)for (sym = start; sym; sym =dso__next_symbol (sym)) {if (sym->binding ==STB_GLOBAL && !strcmp (sym->name,"__switch_to" )) { - ip = map->unmap_ip (map, sym->start); + ip =map__unmap_ip (map, sym->start);if (ip >=map__start (map) && ip <map__end (map)) { switch_ip = ip;break ;@@ -2767,7 +2767,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)for (sym = start; sym; sym =dso__next_symbol (sym)) {if (!strcmp (sym->name, ptss)) { - ip = map->unmap_ip (map, sym->start); + ip =map__unmap_ip (map, sym->start);if (ip >=map__start (map) && ip <map__end (map)) { *ptss_ip = ip;break ;@@ -3393,7 +3393,7 @@ static int intel_pt_text_poke(struct intel_pt *pt, union perf_event *event)if (!dso || !dso->auxtrace_cache)continue ; - offset = al.map->map_ip (al.map, addr); + offset =map__map_ip (al.map, addr); e =intel_pt_cache_lookup (dso, machine, offset);if (!e)tools/perf/util/machine.c +8
-8@@ -919,7 +919,7 @@ static int machine__process_ksymbol_register(struct machine *machine, dso =map__dso (map); } - sym =symbol__new (map->map_ip (map,map__start (map)), + sym =symbol__new (map__map_ip (map,map__start (map)), event->ksymbol.len,0 ,0 , event->ksymbol.name);if (!sym)@@ -944,7 +944,7 @@ static int machine__process_ksymbol_unregister(struct machine *machine,else {struct dso *dso =map__dso (map); - sym =dso__find_symbol (dso, map->map_ip (map,map__start (map))); + sym =dso__find_symbol (dso,map__map_ip (map,map__start (map)));if (sym)dso__delete_symbol (dso, sym); }@@ -1279,7 +1279,7 @@ int machine__map_x86_64_entry_trampolines(struct machine *machine, dest_map =maps__find (kmaps, map->pgoff);if (dest_map != map) - map->pgoff = dest_map->map_ip (dest_map, map->pgoff); + map->pgoff =map__map_ip (dest_map, map->pgoff); found = true; }if (found || machine->trampolines_mapped)@@ -3345,7 +3345,7 @@ char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, chreturn NULL ; *modp =__map__is_kmodule (map) ? (char *)map__dso (map)->short_name :NULL ; - *addrp = map->unmap_ip (map, sym->start); + *addrp =map__unmap_ip (map, sym->start);return sym->name; }@@ -3388,17 +3388,17 @@ bool machine__is_lock_function(struct machine *machine, u64 addr)return false; } - machine->sched.text_start = kmap->unmap_ip (kmap, sym->start); + machine->sched.text_start =map__unmap_ip (kmap, sym->start);/* should not fail from here */ sym =machine__find_kernel_symbol_by_name (machine,"__sched_text_end" , &kmap); - machine->sched.text_end = kmap->unmap_ip (kmap, sym->start); + machine->sched.text_end =map__unmap_ip (kmap, sym->start); sym =machine__find_kernel_symbol_by_name (machine,"__lock_text_start" , &kmap); - machine->lock.text_start = kmap->unmap_ip (kmap, sym->start); + machine->lock.text_start =map__unmap_ip (kmap, sym->start); sym =machine__find_kernel_symbol_by_name (machine,"__lock_text_end" , &kmap); - machine->lock.text_end = kmap->unmap_ip (kmap, sym->start); + machine->lock.text_end =map__unmap_ip (kmap, sym->start); }/* failed to get kernel symbols */ tools/perf/util/map.c +5
-5@@ -519,7 +519,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip)if (dso->kernel ==DSO_SPACE__USER )return rip + dso->text_offset; -return map->unmap_ip (map, rip) - map->reloc; +return map__unmap_ip (map, rip) - map->reloc; }/** @@ -539,24 +539,24 @@ u64 map__objdump_2mem(struct map *map, u64 ip)const struct dso *dso =map__dso (map);if (!dso->adjust_symbols) -return map->unmap_ip (map, ip); +return map__unmap_ip (map, ip);if (dso->rel) -return map->unmap_ip (map, ip + map->pgoff); +return map__unmap_ip (map, ip + map->pgoff);/* * kernel modules also have DSO_TYPE_USER in dso->kernel, * but all kernel modules are ET_REL, so won't get here. */ if (dso->kernel ==DSO_SPACE__USER ) -return map->unmap_ip (map, ip - dso->text_offset); +return map__unmap_ip (map, ip - dso->text_offset);return ip + map->reloc; }bool map__contains_symbol (const struct map *map,const struct symbol *sym) { -u64 ip = map->unmap_ip (map, sym->start); +u64 ip =map__unmap_ip (map, sym->start);return ip >=map__start (map) && ip <map__end (map); }tools/perf/util/map.h +10
-0@@ -52,6 +52,16 @@ static inline struct dso *map__dso(const struct map *map)return map->dso; } +static inline u64 map__map_ip (const struct map *map,u64 ip) +{ +return map->map_ip (map, ip); +} + +static inline u64 map__unmap_ip (const struct map *map,u64 ip) +{ +return map->unmap_ip (map, ip); +} +static inline u64 map__start (const struct map *map) {return map->start;tools/perf/util/maps.c +4
-4@@ -194,7 +194,7 @@ struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp)if (map !=NULL &&map__load (map) >=0 ) {if (mapp !=NULL ) *mapp = map; -return map__find_symbol (map, map->map_ip (map, addr)); +return map__find_symbol (map,map__map_ip (map, addr)); }return NULL ;@@ -237,7 +237,7 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams)return -1 ; } - ams->al_addr = ams->ms.map->map_ip (ams->ms.map, ams->addr); + ams->al_addr =map__map_ip (ams->ms.map, ams->addr); ams->ms.sym =map__find_symbol (ams->ms.map, ams->al_addr);return ams->ms.sym ?0 :-1 ;@@ -349,8 +349,8 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) after->start =map__end (map); after->pgoff +=map__end (map) -map__start (pos->map); -assert (pos->map->map_ip (pos->map,map__end (map)) == - after->map_ip (after,map__end (map))); +assert (map__map_ip (pos->map,map__end (map)) == +map__map_ip (after,map__end (map))); err =__maps__insert (maps, after);if (err) goto put_map;tools/perf/util/probe-event.c +4
-4@@ -141,7 +141,7 @@ static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, sym =machine__find_kernel_symbol_by_name (host_machine, name, &map);if (!sym)return -ENOENT ; - *addr = map->unmap_ip (map, sym->start) - + *addr =map__unmap_ip (map, sym->start) - ((reloc) ?0 : map->reloc) - ((reladdr) ?map__start (map) :0 ); }@@ -400,7 +400,7 @@ static int find_alternative_probe_point(struct debuginfo *dinfo,"Consider identifying the final function used at run time and set the probe directly on that.\n" , pp->function); }else - address = map->unmap_ip (map, sym->start) - map->reloc; + address =map__unmap_ip (map, sym->start) - map->reloc;break ; }if (!address) {@@ -2249,7 +2249,7 @@ static int find_perf_probe_point_from_map(struct probe_trace_point *tp, goto out; pp->retprobe = tp->retprobe; - pp->offset = addr - map->unmap_ip (map, sym->start); + pp->offset = addr -map__unmap_ip (map, sym->start); pp->function =strdup (sym->name); ret = pp->function ?0 : -ENOMEM ;@@ -3123,7 +3123,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev, goto err_out; }/* Add one probe point */ - tp->address = map->unmap_ip (map, sym->start) + pp->offset; + tp->address =map__unmap_ip (map, sym->start) + pp->offset;/* Check the kprobe (not in module) is within .text */ if (!pev->uprobes && !pev->target &&tools/perf/util/scripting-engines/trace-event-python.c +1
-1@@ -471,7 +471,7 @@ static PyObject *python_process_callchain(struct perf_sample *sample,struct addr_location node_al;unsigned long offset; - node_al.addr = map->map_ip (map, node->ip); + node_al.addr =map__map_ip (map, node->ip); node_al.map = map; offset =get_offset (node->ms.sym, &node_al);tools/perf/util/sort.c +6
-6@@ -364,7 +364,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms,u64 rip = ip;if (dso && dso->kernel && dso->adjust_symbols) - rip = map->unmap_ip (map, ip); + rip =map__unmap_ip (map, ip); ret +=repsep_snprintf (bf, size,"%-#*llx %c " ,BITS_PER_LONG /4 +2 , rip, o);@@ -375,7 +375,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms,if (sym->type ==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)); + ip -map__unmap_ip (map, sym->start)); }else { ret += repsep_snprintf(bf + ret, size - ret, "%.*s", width - ret,@@ -1147,7 +1147,7 @@ static int _hist_entry__addr_snprintf(struct map_symbol *ms,if (sym->type ==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)); + ip -map__unmap_ip (map, sym->start)); }else { ret += repsep_snprintf(bf + ret, size - ret, "%.*s", width - ret,@@ -2104,9 +2104,9 @@ sort__addr_cmp(struct hist_entry *left, struct hist_entry *right)struct map *right_map = right->ms.map;if (left_map) - left_ip = left_map->unmap_ip (left_map, left_ip); + left_ip =map__unmap_ip (left_map, left_ip);if (right_map) - right_ip = right_map->unmap_ip (right_map, right_ip); + right_ip =map__unmap_ip (right_map, right_ip);return _sort__addr_cmp (left_ip, right_ip); }@@ -2118,7 +2118,7 @@ static int hist_entry__addr_snprintf(struct hist_entry *he, char *bf,struct map *map = he->ms.map;if (map) - ip = map->unmap_ip (map, ip); + ip =map__unmap_ip (map, ip);return repsep_snprintf (bf, size,"%-#*llx" , width, ip); }tools/perf/util/symbol.c +2
-2@@ -896,8 +896,8 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,* So that we look just like we get from .ko files, * i.e. not prelinked, relative to initial_map->start. */ - pos->start = curr_map->map_ip (curr_map, pos->start); - pos->end = curr_map->map_ip (curr_map, pos->end); + 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)) {/* * These symbols are not needed anymore since the tools/perf/util/thread.c +1
-1@@ -464,7 +464,7 @@ int thread__memcpy(struct thread *thread, struct machine *machine,if ( !dso || dso->data.status ==DSO_DATA_STATUS_ERROR ||map__load (al.map) <0 )return -1 ; - offset = al.map->map_ip (al.map, ip); + offset =map__map_ip (al.map, ip);if (is64bit) *is64bit = dso->is_64_bit;tools/perf/util/unwind-libdw.c +1
-1@@ -115,7 +115,7 @@ static int entry(u64 ip, struct unwind_info *ui)pr_debug ("unwind: %s:ip = 0x%" PRIx64" (0x%" PRIx64")\n" , al.sym ? al.sym->name :"''" , ip, - al.map ? al.map->map_ip (al.map, ip) : (u64 )0 ); + al.map ?map__map_ip (al.map, ip) : (u64 )0 );return 0 ; }tools/perf/util/unwind-libunwind-local.c +1
-2@@ -640,7 +640,7 @@ static int entry(u64 ip, struct thread *thread,pr_debug ("unwind: %s:ip = 0x%" PRIx64" (0x%" PRIx64")\n" , al.sym ? al.sym->name :"''" , ip, - al.map ? al.map->map_ip (al.map, ip) : (u64 )0 ); + al.map ?map__map_ip (al.map, ip) : (u64 )0 );return cb (&e, arg); } --Quoted 2 lines
2.40.0.348.gf938b09366-goog
--
- Arnaldo
Signature · 1 line
- Arnaldo
Ian Rogers
reviewerPATCH v6 3/12 perf map: Add accessors for prot, priv and flags
In reply to Ian RogersLater changes will add reference count checking for struct map. Add an accessor so that the reference count check is only necessary in one place.
--- tools/perf/builtin-inject.c | 2 +- tools/perf/builtin-report.c | 9 +++++---- tools/perf/tests/vmlinux-kallsyms.c | 4 ++-- tools/perf/util/map.h | 15 +++++++++++++++ tools/perf/util/sort.c | 6 +++--- tools/perf/util/symbol.c | 4 ++-- 6 files changed, 28 insertions(+), 12 deletions(-)
tools/perf/builtin-inject.c +1 -1
@@ -758,7 +758,7 @@ int perf_event__inject_buildid(struct perf_tool *tool, union perf_event *event,if (!dso->hit) { dso->hit =1 ;dso__inject_build_id (dso, tool, machine, - sample->cpumode, al.map->flags); + sample->cpumode,map__flags (al.map)); } }
tools/perf/builtin-report.c +5 -4
@@ -849,13 +849,14 @@ static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)maps__for_each_entry (maps, rb_node) {struct map *map = rb_node->map;const struct dso *dso =map__dso (map); +u32 prot =map__prot (map); printed +=fprintf (fp,"%*s %" PRIx64"-%" PRIx64" %c%c%c%c %08" PRIx64" %" PRIu64" %s\n" , indent,"" ,map__start (map),map__end (map), - map->prot &PROT_READ ?'r' :'-' , - map->prot &PROT_WRITE ?'w' :'-' , - map->prot &PROT_EXEC ?'x' :'-' , - map->flags &MAP_SHARED ?'s' :'p' , + prot &PROT_READ ?'r' :'-' , + prot &PROT_WRITE ?'w' :'-' , + prot &PROT_EXEC ?'x' :'-' , +map__flags (map) ?'s' :'p' , map->pgoff, dso->id.ino, dso->name); }
tools/perf/tests/vmlinux-kallsyms.c +2 -2
@@ -323,7 +323,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused mem_end =map__unmap_ip (vmlinux_map,map__end (map)); pair =maps__find (kallsyms.kmaps, mem_start); -if (pair ==NULL || pair->priv) +if (pair ==NULL ||map__priv (pair))continue ;if (map__start (pair) == mem_start) {
@@ -351,7 +351,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unusedmaps__for_each_entry (maps, rb_node) {struct map *map = rb_node->map; -if (!map->priv) { +if (!map__priv (map)) {if (!header_printed) {pr_info ("WARN: Maps only in kallsyms:\n" ); header_printed = true;
tools/perf/util/map.h +15 -0
@@ -72,6 +72,21 @@ static inline u64 map__end(const struct map *map)return map->end; } +static inline u32 map__flags (const struct map *map) +{ +return map->flags; +} + +static inline u32 map__prot (const struct map *map) +{ +return map->prot; +} + +static inline bool map__priv (const struct map *map) +{ +return map->priv; +} +static inline size_t map__size (const struct map *map) {return map__end (map) -map__start (map);
tools/perf/util/sort.c +3 -3
@@ -1540,7 +1540,7 @@ sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)*/ if ((left->cpumode !=PERF_RECORD_MISC_KERNEL ) && - (!(l_map->flags &MAP_SHARED )) && !l_dso->id.maj && !l_dso->id.min && + (!(map__flags (l_map) &MAP_SHARED )) && !l_dso->id.maj && !l_dso->id.min && !l_dso->id.ino && !l_dso->id.ino_generation) {/* userspace anonymous */
@@ -1576,8 +1576,8 @@ static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,/* print [s] for shared data mmaps */ if ((he->cpumode !=PERF_RECORD_MISC_KERNEL ) && - map && !(map->prot &PROT_EXEC ) && - (map->flags &MAP_SHARED ) && + map && !(map__prot (map) &PROT_EXEC ) && + (map__flags (map) &MAP_SHARED ) && (dso->id.maj || dso->id.min || dso->id.ino || dso->id.ino_generation)) level ='s' ;else if (!map)
tools/perf/util/symbol.c +2 -2
@@ -1396,7 +1396,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map, }/* Read new maps into temporary lists */ - err =file__read_maps (fd, map->prot &PROT_EXEC , kcore_mapfn, &md, + err =file__read_maps (fd,map__prot (map) &PROT_EXEC , kcore_mapfn, &md, &is_64_bit);if (err) goto out_err;
@@ -1509,7 +1509,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map,close (fd); -if (map->prot &PROT_EXEC ) +if (map__prot (map) &PROT_EXEC )pr_debug ("Using %s for kernel object code\n" , kcore_filename);else pr_debug ("Using %s for kernel data\n" , kcore_filename);
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH v6 4/12 perf map: Add accessors for pgoff and reloc
In reply to Ian RogersLater changes will add reference count checking for struct map. Add accessors so that the reference count check is only necessary in one place.
--- tools/perf/arch/x86/util/event.c | 2 +- tools/perf/builtin-report.c | 2 +- tools/perf/tests/vmlinux-kallsyms.c | 4 ++-- tools/perf/util/machine.c | 4 ++-- tools/perf/util/map.c | 14 +++++++------- tools/perf/util/map.h | 10 ++++++++++ tools/perf/util/probe-event.c | 8 ++++---- tools/perf/util/symbol.c | 6 +++--- tools/perf/util/unwind-libdw.c | 6 +++--- 9 files changed, 33 insertions(+), 23 deletions(-)
tools/perf/arch/x86/util/event.c +1 -1
@@ -61,7 +61,7 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *tool, event->mmap.start =map__start (map); event->mmap.len =map__size (map); - event->mmap.pgoff = map->pgoff; + event->mmap.pgoff =map__pgoff (map); event->mmap.pid = machine->pid;strlcpy (event->mmap.filename, kmap->name,PATH_MAX );
tools/perf/builtin-report.c +1 -1
@@ -857,7 +857,7 @@ static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp) prot &PROT_WRITE ?'w' :'-' , prot &PROT_EXEC ?'x' :'-' ,map__flags (map) ?'s' :'p' , - map->pgoff, +map__pgoff (map), dso->id.ino, dso->name); }
tools/perf/tests/vmlinux-kallsyms.c +2 -2
@@ -335,10 +335,10 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused }pr_info ("WARN: %" PRIx64"-%" PRIx64" %" PRIx64" %s in kallsyms as" , -map__start (map),map__end (map), map->pgoff, dso->name); +map__start (map),map__end (map),map__pgoff (map), dso->name);if (mem_end !=map__end (pair))pr_info (":\nWARN: *%" PRIx64"-%" PRIx64" %" PRIx64, -map__start (pair),map__end (pair), pair->pgoff); +map__start (pair),map__end (pair),map__pgoff (pair));pr_info (" %s\n" , dso->name); pair->priv =1 ; }
tools/perf/util/machine.c +2 -2
@@ -1277,9 +1277,9 @@ int machine__map_x86_64_entry_trampolines(struct machine *machine,if (!kmap || !is_entry_trampoline (kmap->name))continue ; - dest_map =maps__find (kmaps, map->pgoff); + dest_map =maps__find (kmaps,map__pgoff (map));if (dest_map != map) - map->pgoff =map__map_ip (dest_map, map->pgoff); + map->pgoff =map__map_ip (dest_map,map__pgoff (map)); found = true; }if (found || machine->trampolines_mapped)
tools/perf/util/map.c +7 -7
@@ -421,7 +421,7 @@ size_t map__fprintf(struct map *map, FILE *fp)const struct dso *dso =map__dso (map);return fprintf (fp," %" PRIx64"-%" PRIx64" %" PRIx64" %s\n" , -map__start (map),map__end (map), map->pgoff, dso->name); +map__start (map),map__end (map),map__pgoff (map), dso->name); }size_t map__fprintf_dsoname (struct map *map,FILE *fp)
@@ -510,7 +510,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip)return rip;if (dso->rel) -return rip - map->pgoff; +return rip -map__pgoff (map);/* * kernel modules also have DSO_TYPE_USER in dso->kernel,
@@ -519,7 +519,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip)if (dso->kernel ==DSO_SPACE__USER )return rip + dso->text_offset; -return map__unmap_ip (map, rip) - map->reloc; +return map__unmap_ip (map, rip) -map__reloc (map); }/**
@@ -542,7 +542,7 @@ u64 map__objdump_2mem(struct map *map, u64 ip)return map__unmap_ip (map, ip);if (dso->rel) -return map__unmap_ip (map, ip + map->pgoff); +return map__unmap_ip (map, ip +map__pgoff (map));/* * kernel modules also have DSO_TYPE_USER in dso->kernel,
@@ -551,7 +551,7 @@ u64 map__objdump_2mem(struct map *map, u64 ip)if (dso->kernel ==DSO_SPACE__USER )return map__unmap_ip (map, ip - dso->text_offset); -return ip + map->reloc; +return ip +map__reloc (map); }bool map__contains_symbol (const struct map *map,const struct symbol *sym)
@@ -592,12 +592,12 @@ struct maps *map__kmaps(struct map *map)u64 map__dso_map_ip (const struct map *map,u64 ip) { -return ip -map__start (map) + map->pgoff; +return ip -map__start (map) +map__pgoff (map); }u64 map__dso_unmap_ip (const struct map *map,u64 ip) { -return ip +map__start (map) - map->pgoff; +return ip +map__start (map) -map__pgoff (map); }u64 identity__map_ip (const struct map *map__maybe_unused ,u64 ip)
tools/perf/util/map.h +10 -0
@@ -72,6 +72,16 @@ static inline u64 map__end(const struct map *map)return map->end; } +static inline u64 map__pgoff (const struct map *map) +{ +return map->pgoff; +} + +static inline u64 map__reloc (const struct map *map) +{ +return map->reloc; +} +static inline u32 map__flags (const struct map *map) {return map->flags;
tools/perf/util/probe-event.c +4 -4
@@ -135,14 +135,14 @@ static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,/* ref_reloc_sym is just a label. Need a special fix*/ reloc_sym =kernel_get_ref_reloc_sym (&map);if (reloc_sym &&strcmp (name, reloc_sym->name) ==0 ) - *addr = (!map->reloc || reloc) ? reloc_sym->addr : + *addr = (!map__reloc (map) || reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;else { sym =machine__find_kernel_symbol_by_name (host_machine, name, &map);if (!sym)return -ENOENT ; *addr =map__unmap_ip (map, sym->start) - - ((reloc) ?0 : map->reloc) - + ((reloc) ?0 :map__reloc (map)) - ((reladdr) ?map__start (map) :0 ); }return 0 ;
@@ -400,7 +400,7 @@ static int find_alternative_probe_point(struct debuginfo *dinfo,"Consider identifying the final function used at run time and set the probe directly on that.\n" , pp->function); }else - address =map__unmap_ip (map, sym->start) - map->reloc; + address =map__unmap_ip (map, sym->start) -map__reloc (map);break ; }if (!address) {
@@ -866,7 +866,7 @@ post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,free (tevs[i].point.symbol); tevs[i].point.symbol = tmp; tevs[i].point.offset = tevs[i].point.address - - (map->reloc ? reloc_sym->unrelocated_addr : + (map__reloc (map) ? reloc_sym->unrelocated_addr : reloc_sym->addr); }return skipped;
tools/perf/util/symbol.c +3 -3
@@ -810,11 +810,11 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)continue ; } curr_map_dso =map__dso (curr_map); - pos->start -=map__start (curr_map) - curr_map->pgoff; + pos->start -=map__start (curr_map) -map__pgoff (curr_map);if (pos->end >map__end (curr_map)) pos->end =map__end (curr_map);if (pos->end) - pos->end -=map__start (curr_map) - curr_map->pgoff; + pos->end -=map__start (curr_map) -map__pgoff (curr_map);symbols__insert (&curr_map_dso->symbols, pos); ++count; }
@@ -1459,7 +1459,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map,if (new_map == replacement_map) { map->start =map__start (new_map); map->end =map__end (new_map); - map->pgoff = new_map->pgoff; + map->pgoff =map__pgoff (new_map); map->map_ip = new_map->map_ip; map->unmap_ip = new_map->unmap_ip;/* Ensure maps are correctly ordered */
tools/perf/util/unwind-libdw.c +3 -3
@@ -62,19 +62,19 @@ static int __report_module(struct addr_location *al, u64 ip,Dwarf_Addr s;dwfl_module_info (mod,NULL , &s,NULL ,NULL ,NULL ,NULL ,NULL ); -if (s !=map__start (al->map) - al->map->pgoff) +if (s !=map__start (al->map) -map__pgoff (al->map)) mod =0 ; }if (!mod) mod =dwfl_report_elf (ui->dwfl, dso->short_name, dso->long_name,-1 , -map__start (al->map) - al->map->pgoff, false); +map__start (al->map) -map__pgoff (al->map), false);if (!mod) {char filename[PATH_MAX ];if (dso__build_id_filename (dso, filename,sizeof (filename), false)) mod =dwfl_report_elf (ui->dwfl, dso->short_name, filename,-1 , -map__start (al->map) - al->map->pgoff, false); +map__start (al->map) -map__pgoff (al->map), false); }if (mod) {
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH v6 5/12 perf test: Add extra diagnostics to maps test
In reply to Ian RogersDump the resultant and comparison maps on failure.
--- tools/perf/tests/maps.c | 51 +++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 15 deletions(-)
tools/perf/tests/maps.c +36 -15
@@ -1,4 +1,5 @@// SPDX-License-Identifier: GPL-2.0 +#include <inttypes.h> #include <linux/compiler.h> #include <linux/kernel.h> #include "tests.h"
@@ -17,22 +18,42 @@ static int check_maps(struct map_def *merged, unsigned int size, struct maps *ma {struct map_rb_node *rb_node;unsigned int i =0 ; - -maps__for_each_entry (maps, rb_node) { -struct map *map = rb_node->map; - -if (i >0 ) -TEST_ASSERT_VAL ("less maps expected" , (map && i < size) || (!map && i == size)); - -TEST_ASSERT_VAL ("wrong map start" ,map__start (map) == merged[i].start); -TEST_ASSERT_VAL ("wrong map end" ,map__end (map) == merged[i].end); -TEST_ASSERT_VAL ("wrong map name" , !strcmp (map__dso (map)->name, merged[i].name)); -TEST_ASSERT_VAL ("wrong map refcnt" ,refcount_read (&map->refcnt) ==1 ); - - i++; +bool failed = false; + +if (maps__nr_maps (maps) != size) { +pr_debug ("Expected %d maps, got %d" , size,maps__nr_maps (maps)); + failed = true; + }else { +maps__for_each_entry (maps, rb_node) { +struct map *map = rb_node->map; + +if (map__start (map) != merged[i].start || +map__end (map) != merged[i].end || +strcmp (map__dso (map)->name, merged[i].name) || +refcount_read (&map->refcnt) !=1 ) { + failed = true; + } + i++; + } } - -return TEST_OK ; +if (failed) { +pr_debug ("Expected:\n" ); +for (i =0 ; i < size; i++) { +pr_debug ("\tstart: %" PRIu64" end: %" PRIu64" name: '%s' refcnt: 1\n" , + merged[i].start, merged[i].end, merged[i].name); + } +pr_debug ("Got:\n" ); +maps__for_each_entry (maps, rb_node) { +struct map *map = rb_node->map; + +pr_debug ("\tstart: %" PRIu64" end: %" PRIu64" name: '%s' refcnt: %d\n" , +map__start (map), +map__end (map), +map__dso (map)->name, +refcount_read (&map->refcnt)); + } + } +return failed ?TEST_FAIL :TEST_OK ; }static int test__maps__merge_in (struct test_suite *t__maybe_unused ,int subtest__maybe_unused )
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH v6 6/12 perf maps: Modify maps_by_name to hold a reference to a map
In reply to Ian RogersTo make it clearer about the ownership of a reference count split the by-name case from the regular start-address sorted tree. Put the reference count when maps_by_name is freed, which requires moving a decrement to nr_maps in maps__remove. Add two missing map puts in maps__fixup_overlappings in the event maps__insert fails.
--- tools/perf/util/maps.c | 30 ++++++++++++++++-------------- tools/perf/util/symbol.c | 21 +++++++++++++++++---- 2 files changed, 33 insertions(+), 18 deletions(-)
tools/perf/util/maps.c +16 -14
@@ -26,6 +26,9 @@ static void __maps__free_maps_by_name(struct maps *maps)/* * Free everything to try to do it from the rbtree in the next search */ +for (unsigned int i =0 ; i <maps__nr_maps (maps); i++) +map__put (maps__maps_by_name (maps)[i]); +zfree (&maps->maps_by_name); maps->nr_maps_allocated =0 ; }
@@ -42,7 +45,7 @@ static int __maps__insert(struct maps *maps, struct map *map)return -ENOMEM ;RB_CLEAR_NODE (&new_rb_node->rb_node); - new_rb_node->map = map; + new_rb_node->map =map__get (map);while (*p !=NULL ) { parent = *p;
@@ -55,7 +58,6 @@ static int __maps__insert(struct maps *maps, struct map *map)rb_link_node (&new_rb_node->rb_node, parent, p);rb_insert_color (&new_rb_node->rb_node,maps__entries (maps)); -map__get (map);return 0 ; }
@@ -100,7 +102,7 @@ int maps__insert(struct maps *maps, struct map *map) maps->maps_by_name = maps_by_name; maps->nr_maps_allocated = nr_allocate; } -maps__maps_by_name (maps)[maps__nr_maps (maps) -1 ] = map; +maps__maps_by_name (maps)[maps__nr_maps (maps) -1 ] =map__get (map);__maps__sort_by_name (maps); } out:
@@ -126,9 +128,9 @@ void maps__remove(struct maps *maps, struct map *map) rb_node =maps__find_node (maps, map);assert (rb_node->map == map);__maps__remove (maps, rb_node); - --maps->nr_maps;if (maps__maps_by_name (maps))__maps__free_maps_by_name (maps); + --maps->nr_maps;up_write (maps__lock (maps)); }
@@ -136,6 +138,9 @@ static void __maps__purge(struct maps *maps) {struct map_rb_node *pos, *next; +if (maps__maps_by_name (maps)) +__maps__free_maps_by_name (maps); +maps__for_each_entry_safe (maps, pos, next) {rb_erase_init (&pos->rb_node,maps__entries (maps));map__put (pos->map);
@@ -293,7 +298,7 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) } next = first; -while (next) { +while (next && !err) { struct map_rb_node *pos = rb_entry(next, struct map_rb_node, rb_node); next =rb_next (&pos->rb_node);
@@ -331,8 +336,10 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) before->end =map__start (map); err =__maps__insert (maps, before); -if (err) +if (err) { +map__put (before); goto put_map; + }if (verbose >=2 && !use_browser)map__fprintf (before, fp);
@@ -352,22 +359,17 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)assert (map__map_ip (pos->map,map__end (map)) ==map__map_ip (after,map__end (map))); err =__maps__insert (maps, after); -if (err) +if (err) { +map__put (after); goto put_map; - + }if (verbose >=2 && !use_browser)map__fprintf (after, fp);map__put (after); } put_map:map__put (pos->map); - -if (err) - goto out; } - - err =0 ; -out:up_write (maps__lock (maps));return err; }
tools/perf/util/symbol.c +17 -4
@@ -2053,10 +2053,23 @@ int dso__load(struct dso *dso, struct map *map)static int map__strcmp (const void *a,const void *b) { -const struct dso *dso_a =map__dso (*(const struct map **)a); -const struct dso *dso_b =map__dso (*(const struct map **)b); +const struct map *map_a = *(const struct map **)a; +const struct map *map_b = *(const struct map **)b; +const struct dso *dso_a =map__dso (map_a); +const struct dso *dso_b =map__dso (map_b); +int ret =strcmp (dso_a->short_name, dso_b->short_name); -return strcmp (dso_a->short_name, dso_b->short_name); +if (ret ==0 && map_a != map_b) { +/* +* Ensure distinct but name equal maps have an order in part to +* aid reference counting. +*/ + ret = (int )map__start (map_a) - (int )map__start (map_b); +if (ret ==0 ) + ret = (int )((intptr_t )map_a - (intptr_t )map_b); + } + +return ret; }static int map__strcmp_name (const void *name,const void *b)
@@ -2088,7 +2101,7 @@ static int map__groups__sort_by_name_from_rbtree(struct maps *maps) maps->nr_maps_allocated =maps__nr_maps (maps);maps__for_each_entry (maps, rb_node) - maps_by_name[i++] = rb_node->map; + maps_by_name[i++] =map__get (rb_node->map);__maps__sort_by_name (maps);
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH v6 7/12 perf map: Changes to reference counting
In reply to Ian RogersWhen a pointer to a map exists do a get, when that pointer is overwritten or freed, put the map. This avoids issues with gets and puts being inconsistently used causing, use after puts, etc. For example, the map in struct addr_location is changed to hold a reference count. Reference count checking and address sanitizer were used to identify issues.
--- tools/perf/tests/code-reading.c | 1 + tools/perf/tests/hists_cumulate.c | 10 ++++ tools/perf/tests/hists_filter.c | 10 ++++ tools/perf/tests/hists_link.c | 18 +++++- tools/perf/tests/hists_output.c | 10 ++++ tools/perf/tests/mmap-thread-lookup.c | 1 + tools/perf/util/callchain.c | 9 +-- tools/perf/util/event.c | 6 +- tools/perf/util/hist.c | 10 ++-- tools/perf/util/machine.c | 79 ++++++++++++++++----------- tools/perf/util/map.c | 2 +- 11 files changed, 112 insertions(+), 44 deletions(-)
tools/perf/tests/code-reading.c +1 -0
@@ -366,6 +366,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode, }pr_debug ("Bytes read match those read by objdump\n" ); out: +map__put (al.map);return err; }
tools/perf/tests/hists_cumulate.c +10 -0
@@ -112,6 +112,7 @@ static int add_hist_entries(struct hists *hists, struct machine *machine) } fake_samples[i].thread = al.thread; +map__put (fake_samples[i].map); fake_samples[i].map = al.map; fake_samples[i].sym = al.sym; }
@@ -147,6 +148,14 @@ static void del_hist_entries(struct hists *hists) } } +static void put_fake_samples (void ) +{ +size_t i; + +for (i =0 ; i <ARRAY_SIZE (fake_samples); i++) +map__put (fake_samples[i].map); +} +typedef int (*test_fn_t )(struct evsel *,struct machine *);#define COMM (he) (thread__comm_str(he->thread))
@@ -733,6 +742,7 @@ static int test__hists_cumulate(struct test_suite *test __maybe_unused, int subt/* tear down everything */ evlist__delete (evlist);machines__exit (&machines); +put_fake_samples ();return err; }
tools/perf/tests/hists_filter.c +10 -0
@@ -89,6 +89,7 @@ static int add_hist_entries(struct evlist *evlist, } fake_samples[i].thread = al.thread; +map__put (fake_samples[i].map); fake_samples[i].map = al.map; fake_samples[i].sym = al.sym; }
@@ -101,6 +102,14 @@ static int add_hist_entries(struct evlist *evlist,return TEST_FAIL ; } +static void put_fake_samples (void ) +{ +size_t i; + +for (i =0 ; i <ARRAY_SIZE (fake_samples); i++) +map__put (fake_samples[i].map); +} +static int test__hists_filter (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) {int err =TEST_FAIL ;
@@ -322,6 +331,7 @@ static int test__hists_filter(struct test_suite *test __maybe_unused, int subtesevlist__delete (evlist);reset_output_field ();machines__exit (&machines); +put_fake_samples ();return err; }
tools/perf/tests/hists_link.c +17 -1
@@ -6,6 +6,7 @@#include "evsel.h" #include "evlist.h" #include "machine.h" +#include "map.h" #include "parse-events.h" #include "hists_common.h" #include "util/mmap.h"
@@ -94,6 +95,7 @@ static int add_hist_entries(struct evlist *evlist, struct machine *machine) } fake_common_samples[k].thread = al.thread; +map__put (fake_common_samples[k].map); fake_common_samples[k].map = al.map; fake_common_samples[k].sym = al.sym; }
@@ -126,11 +128,24 @@ static int add_hist_entries(struct evlist *evlist, struct machine *machine)return -1 ; } +static void put_fake_samples (void ) +{ +size_t i, j; + +for (i =0 ; i <ARRAY_SIZE (fake_common_samples); i++) +map__put (fake_common_samples[i].map); +for (i =0 ; i <ARRAY_SIZE (fake_samples); i++) { +for (j =0 ; j <ARRAY_SIZE (fake_samples[0 ]); j++) +map__put (fake_samples[i][j].map); + } +} +static int find_sample (struct sample *samples,size_t nr_samples,struct thread *t,struct map *m,struct symbol *s) {while (nr_samples--) { -if (samples->thread == t && samples->map == m && +if (samples->thread == t && + samples->map == m && samples->sym == s)return 1 ; samples++;
@@ -336,6 +351,7 @@ static int test__hists_link(struct test_suite *test __maybe_unused, int subtestevlist__delete (evlist);reset_output_field ();machines__exit (&machines); +put_fake_samples ();return err; }
tools/perf/tests/hists_output.c +10 -0
@@ -78,6 +78,7 @@ static int add_hist_entries(struct hists *hists, struct machine *machine) } fake_samples[i].thread = al.thread; +map__put (fake_samples[i].map); fake_samples[i].map = al.map; fake_samples[i].sym = al.sym; }
@@ -113,6 +114,14 @@ static void del_hist_entries(struct hists *hists) } } +static void put_fake_samples (void ) +{ +size_t i; + +for (i =0 ; i <ARRAY_SIZE (fake_samples); i++) +map__put (fake_samples[i].map); +} +typedef int (*test_fn_t )(struct evsel *,struct machine *);#define COMM (he) (thread__comm_str(he->thread))
@@ -620,6 +629,7 @@ static int test__hists_output(struct test_suite *test __maybe_unused, int subtes/* tear down everything */ evlist__delete (evlist);machines__exit (&machines); +put_fake_samples ();return err; }
tools/perf/tests/mmap-thread-lookup.c +1 -0
@@ -203,6 +203,7 @@ static int mmap_events(synth_cb synth) }pr_debug ("map %p, addr %" PRIx64"\n" , al.map,map__start (al.map)); +map__put (al.map); }machine__delete_threads (machine);
tools/perf/util/callchain.c +5 -4
@@ -589,7 +589,7 @@ fill_node(struct callchain_node *node, struct callchain_cursor *cursor) } call->ip = cursor_node->ip; call->ms = cursor_node->ms; -map__get (call->ms.map); + call->ms.map =map__get (call->ms.map); call->srcline = cursor_node->srcline;if (cursor_node->branch) {
@@ -1067,7 +1067,7 @@ int callchain_cursor_append(struct callchain_cursor *cursor, node->ip = ip;map__zput (node->ms.map); node->ms = *ms; -map__get (node->ms.map); + node->ms.map =map__get (node->ms.map); node->branch = branch; node->nr_loop_iter = nr_loop_iter; node->iter_cycles = iter_cycles;
@@ -1115,7 +1115,8 @@ int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *struct machine *machine =maps__machine (node->ms.maps); al->maps = node->ms.maps; - al->map = node->ms.map; +map__put (al->map); + al->map =map__get (node->ms.map); al->sym = node->ms.sym; al->srcline = node->srcline; al->addr = node->ip;
@@ -1528,7 +1529,7 @@ int callchain_node__make_parent_list(struct callchain_node *node) goto out; *new = *chain; new->has_children = false; -map__get (new->ms.map); + new->ms.map =map__get (new->ms.map);list_add_tail (&new->list, &head); } parent = parent->parent;
tools/perf/util/event.c +4 -2
@@ -485,13 +485,14 @@ size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *maif (machine) {struct addr_location al; - al.map =maps__find (machine__kernel_maps (machine), tp->addr); + al.map =map__get (maps__find (machine__kernel_maps (machine), tp->addr));if (al.map &&map__load (al.map) >=0 ) { al.addr =map__map_ip (al.map, tp->addr); al.sym =map__find_symbol (al.map, al.addr);if (al.sym) ret +=symbol__fprintf_symname_offs (al.sym, &al, fp); } +map__put (al.map); } ret +=fprintf (fp," old len %u new len %u\n" , tp->old_len, tp->new_len); old = true;
@@ -614,7 +615,7 @@ struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,return NULL ; } - al->map =maps__find (maps, al->addr); + al->map =map__get (maps__find (maps, al->addr));if (al->map !=NULL ) {/* * Kernel maps might be changed when loading symbols so loading
@@ -773,6 +774,7 @@ int machine__resolve(struct machine *machine, struct addr_location *al,*/ void addr_location__put (struct addr_location *al) { +map__zput (al->map);thread__zput (al->thread); }
tools/perf/util/hist.c +5 -5
@@ -450,7 +450,7 @@ static int hist_entry__init(struct hist_entry *he,memset (&he->stat,0 ,sizeof (he->stat)); } -map__get (he->ms.map); + he->ms.map =map__get (he->ms.map);if (he->branch_info) {/*
@@ -465,13 +465,13 @@ static int hist_entry__init(struct hist_entry *he,memcpy (he->branch_info, template->branch_info,sizeof (*he->branch_info)); -map__get (he->branch_info->from.ms.map); -map__get (he->branch_info->to.ms.map); + he->branch_info->from.ms.map =map__get (he->branch_info->from.ms.map); + he->branch_info->to.ms.map =map__get (he->branch_info->to.ms.map); }if (he->mem_info) { -map__get (he->mem_info->iaddr.ms.map); -map__get (he->mem_info->daddr.ms.map); + he->mem_info->iaddr.ms.map =map__get (he->mem_info->iaddr.ms.map); + he->mem_info->daddr.ms.map =map__get (he->mem_info->daddr.ms.map); }if (hist_entry__has_callchains (he) && symbol_conf.use_callchain)
tools/perf/util/machine.c +48 -31
146 lines not on this page · raw message
tools/perf/util/map.c +1 -1
@@ -410,7 +410,7 @@ struct map *map__clone(struct map *from) map =memdup (from, size);if (map !=NULL ) {refcount_set (&map->refcnt,1 ); -dso__get (dso); + map->dso =dso__get (dso); }return map;
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH v6 8/12 libperf: Add reference count checking macros.
In reply to Ian RogersThe macros serve as a way to debug use of a reference counted struct. The macros add a memory allocated pointer that is interposed between the reference counted original struct at a get and freed by a put. The pointer replaces the original struct, so use of the struct name via APIs remains unchanged.
--- tools/lib/perf/include/internal/rc_check.h | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 tools/lib/perf/include/internal/rc_check.h
tools/lib/perf/include/internal/rc_check.h new file +94 -0
@@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __LIBPERF_INTERNAL_RC_CHECK_H +#define __LIBPERF_INTERNAL_RC_CHECK_H + +#include <stdlib.h> +#include <linux/zalloc.h> + +/* +* Shared reference count checking macros. +* +* Reference count checking is an approach to sanitizing the use of reference +* counted structs. It leverages address and leak sanitizers to make sure gets +* are paired with a put. Reference count checking adds a malloc-ed layer of +* indirection on a get, and frees it on a put. A missed put will be reported as +* a memory leak. A double put will be reported as a double free. Accessing +* after a put will cause a use-after-free and/or a segfault. +*/ + +#ifndef REFCNT_CHECKING +/* Replaces "struct foo" so that the pointer may be interposed. */ +#define DECLARE_RC_STRUCT (struct_name) \ + struct struct_name + +/* Declare a reference counted struct variable. */ +#define RC_STRUCT (struct_name) struct struct_name + +/* +* Interpose the indirection. Result will hold the indirection and object is the +* reference counted struct. +*/ +#define ADD_RC_CHK (result, object) (result = object, object) + +/* Strip the indirection layer. */ +#define RC_CHK_ACCESS (object) object + +/* Frees the object and the indirection layer. */ +#define RC_CHK_FREE (object) free(object) + +/* A get operation adding the indirection layer. */ +#define RC_CHK_GET (result, object) ADD_RC_CHK(result, object) + +/* A put operation removing the indirection layer. */ +#define RC_CHK_PUT (object) {} + +#else + +/* Replaces "struct foo" so that the pointer may be interposed. */ +#define DECLARE_RC_STRUCT (struct_name) \ + struct original_##struct_name; \ + struct struct_name { \ + struct original_##struct_name *orig; \ + }; \ + struct original_##struct_name + +/* Declare a reference counted struct variable. */ +#define RC_STRUCT (struct_name) struct original_##struct_name + +/* +* Interpose the indirection. Result will hold the indirection and object is the +* reference counted struct. +*/ +#define ADD_RC_CHK (result, object) \ + ( \ + object ? (result = malloc(sizeof(*result)), \ + result ? (result->orig = object, result) \ + : (result = NULL, NULL)) \ + : (result = NULL, NULL) \ + ) + +/* Strip the indirection layer. */ +#define RC_CHK_ACCESS (object) object->orig + +/* Frees the object and the indirection layer. */ +#define RC_CHK_FREE (object) \ + do { \ + zfree(&object->orig); \ + free(object); \ + } while(0) + +/* A get operation adding the indirection layer. */ +#define RC_CHK_GET (result, object) ADD_RC_CHK(result, (object ? object->orig : NULL)) + +/* A put operation removing the indirection layer. */ +#define RC_CHK_PUT (object) \ + do { \ + if (object) { \ + object->orig = NULL; \ + free(object); \ + } \ + } while(0) + +#endif + +#endif /* __LIBPERF_INTERNAL_RC_CHECK_H */
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH v6 9/12 perf cpumap: Add reference count checking
In reply to Ian RogersEnabled when REFCNT_CHECKING is defined. The change adds a memory allocated pointer that is interposed between the reference counted cpu map at a get and freed by a put. The pointer replaces the original perf_cpu_map struct, so use of the perf_cpu_map via APIs remains unchanged. Any use of the cpu map without the API requires two versions, handled via the RC_CHK_ACCESS macro.
This change is intended to catch:
- use after put: using a cpumap after you have put it will cause a segv.
- unbalanced puts: two puts for a get will result in a double free that can be captured and reported by tools like address sanitizer, including with the associated stack traces of allocation and frees.
- missing puts: if a put is missing then the get turns into a memory leak that can be reported by leak sanitizer, including the stack trace at the point the get occurs.
--- tools/lib/perf/Makefile | 2 +- tools/lib/perf/cpumap.c | 94 +++++++++++++----------- tools/lib/perf/include/internal/cpumap.h | 4 +- tools/perf/tests/cpumap.c | 4 +- tools/perf/util/cpumap.c | 40 +++++----- tools/perf/util/pmu.c | 8 +- 6 files changed, 81 insertions(+), 71 deletions(-)
tools/lib/perf/Makefile +1 -1
@@ -188,7 +188,7 @@ install_lib: libs cp -fpR $(LIBPERF_ALL) $(DESTDIR)$(libdir_SQ) HDRS := bpf_perf.h core.h cpumap.h threadmap.h evlist.h evsel.h event.h mmap.h -INTERNAL_HDRS := cpumap.h evlist.h evsel.h lib.h mmap.h threadmap.h xyarray.h +INTERNAL_HDRS := cpumap.h evlist.h evsel.h lib.h mmap.h rc_check.h threadmap.h xyarray.h INSTALL_HDRS_PFX := $(DESTDIR)$(prefix)/include/perf INSTALL_HDRS := $(addprefix $(INSTALL_HDRS_PFX)/, $(HDRS))
tools/lib/perf/cpumap.c +52 -42
199 lines not on this page · raw message
tools/lib/perf/include/internal/cpumap.h +3 -1
@@ -4,6 +4,7 @@#include <linux/refcount.h> #include <perf/cpumap.h> +#include <internal/rc_check.h> /** * A sized, reference counted, sorted array of integers representing CPU
@@ -12,7 +13,7 @@* gaps if CPU numbers were used. For events associated with a pid, rather than * a CPU, a single dummy map with an entry of -1 is used. */ -struct perf_cpu_map { +DECLARE_RC_STRUCT (perf_cpu_map) {refcount_t refcnt;/** Length of the map array. */ int nr;
@@ -24,6 +25,7 @@ struct perf_cpu_map {#define MAX_NR_CPUS 2048#endif +struct perf_cpu_map *perf_cpu_map__alloc (int nr_cpus);int perf_cpu_map__idx (const struct perf_cpu_map *cpus,struct perf_cpu cpu);bool perf_cpu_map__is_subset (const struct perf_cpu_map *a,const struct perf_cpu_map *b);
tools/perf/tests/cpumap.c +2 -2
@@ -68,7 +68,7 @@ static int process_event_cpus(struct perf_tool *tool __maybe_unused,TEST_ASSERT_VAL ("wrong nr" ,perf_cpu_map__nr (map) ==2 );TEST_ASSERT_VAL ("wrong cpu" ,perf_cpu_map__cpu (map,0 ).cpu ==1 );TEST_ASSERT_VAL ("wrong cpu" ,perf_cpu_map__cpu (map,1 ).cpu ==256 ); -TEST_ASSERT_VAL ("wrong refcnt" ,refcount_read (&map->refcnt) ==1 ); +TEST_ASSERT_VAL ("wrong refcnt" ,refcount_read (&RC_CHK_ACCESS (map)->refcnt) ==1 );perf_cpu_map__put (map);return 0 ; }
@@ -94,7 +94,7 @@ static int process_event_range_cpus(struct perf_tool *tool __maybe_unused,TEST_ASSERT_VAL ("wrong nr" ,perf_cpu_map__nr (map) ==256 );TEST_ASSERT_VAL ("wrong cpu" ,perf_cpu_map__cpu (map,0 ).cpu ==1 );TEST_ASSERT_VAL ("wrong cpu" ,perf_cpu_map__max (map).cpu ==256 ); -TEST_ASSERT_VAL ("wrong refcnt" ,refcount_read (&map->refcnt) ==1 ); +TEST_ASSERT_VAL ("wrong refcnt" ,refcount_read (&RC_CHK_ACCESS (map)->refcnt) ==1 );perf_cpu_map__put (map);return 0 ; }
tools/perf/util/cpumap.c +19 -21
@@ -77,9 +77,9 @@ static struct perf_cpu_map *cpu_map__from_entries(const struct perf_record_cpu_m* otherwise it would become 65535. */ if (data->cpus_data.cpu[i] == (u16) -1 ) - map->map[i].cpu =-1 ; +RC_CHK_ACCESS (map)->map[i].cpu =-1 ;else - map->map[i].cpu = (int ) data->cpus_data.cpu[i]; +RC_CHK_ACCESS (map)->map[i].cpu = (int ) data->cpus_data.cpu[i]; } }
@@ -107,7 +107,7 @@ static struct perf_cpu_map *cpu_map__from_mask(const struct perf_record_cpu_map_perf_record_cpu_map_data__read_one_mask (data, i, local_copy);for_each_set_bit (cpu, local_copy,64 ) - map->map[j++].cpu = cpu + cpus_per_i; +RC_CHK_ACCESS (map)->map[j++].cpu = cpu + cpus_per_i; }return map;
@@ -124,11 +124,11 @@ static struct perf_cpu_map *cpu_map__from_range(const struct perf_record_cpu_mapreturn NULL ;if (data->range_cpu_data.any_cpu) - map->map[i++].cpu =-1 ; +RC_CHK_ACCESS (map)->map[i++].cpu =-1 ;for (int cpu = data->range_cpu_data.start_cpu; cpu <= data->range_cpu_data.end_cpu; i++, cpu++) - map->map[i].cpu = cpu; +RC_CHK_ACCESS (map)->map[i].cpu = cpu;return map; }
@@ -160,16 +160,13 @@ size_t cpu_map__fprintf(struct perf_cpu_map *map, FILE *fp)struct perf_cpu_map *perf_cpu_map__empty_new (int nr) { -struct perf_cpu_map *cpus =malloc (sizeof (*cpus) +sizeof (int ) * nr); +struct perf_cpu_map *cpus =perf_cpu_map__alloc (nr);if (cpus !=NULL ) {int i; - cpus->nr = nr;for (i =0 ; i < nr; i++) - cpus->map[i].cpu =-1 ; - -refcount_set (&cpus->refcnt,1 ); +RC_CHK_ACCESS (cpus)->map[i].cpu =-1 ; }return cpus;
@@ -239,7 +236,7 @@ struct cpu_aggr_map *cpu_aggr_map__new(const struct perf_cpu_map *cpus, {int idx;struct perf_cpu cpu; -struct cpu_aggr_map *c =cpu_aggr_map__empty_new (cpus->nr); +struct cpu_aggr_map *c =cpu_aggr_map__empty_new (perf_cpu_map__nr (cpus));if (!c)return NULL ;
@@ -263,7 +260,7 @@ struct cpu_aggr_map *cpu_aggr_map__new(const struct perf_cpu_map *cpus, } }/* Trim. */ -if (c->nr != cpus->nr) { +if (c->nr !=perf_cpu_map__nr (cpus)) {struct cpu_aggr_map *trimmed_c =realloc (c,sizeof (struct cpu_aggr_map ) +sizeof (struct aggr_cpu_id ) * c->nr);
@@ -582,31 +579,32 @@ size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)#define COMMA first ? "" : "," -for (i =0 ; i < map->nr +1 ; i++) { +for (i =0 ; i <perf_cpu_map__nr (map) +1 ; i++) {struct perf_cpu cpu = { .cpu =INT_MAX }; -bool last = i == map->nr; +bool last = i ==perf_cpu_map__nr (map);if (!last) - cpu = map->map[i]; + cpu =perf_cpu_map__cpu (map, i);if (start ==-1 ) { start = i;if (last) { ret +=snprintf (buf + ret, size - ret,"%s%d" ,COMMA , - map->map[i].cpu); +perf_cpu_map__cpu (map, i).cpu); } - }else if (((i - start) != (cpu.cpu - map->map[start].cpu)) || last) { + }else if (((i - start) != (cpu.cpu -perf_cpu_map__cpu (map, start).cpu)) || last) {int end = i -1 ;if (start == end) { ret +=snprintf (buf + ret, size - ret,"%s%d" ,COMMA , - map->map[start].cpu); +perf_cpu_map__cpu (map, start).cpu); }else { ret +=snprintf (buf + ret, size - ret,"%s%d-%d" ,COMMA , - map->map[start].cpu, map->map[end].cpu); +perf_cpu_map__cpu (map, start).cpu, +perf_cpu_map__cpu (map, end).cpu); } first = false; start = i;
@@ -633,7 +631,7 @@ size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size)int i, cpu;char *ptr = buf;unsigned char *bitmap; -struct perf_cpu last_cpu =perf_cpu_map__cpu (map, map->nr -1 ); +struct perf_cpu last_cpu =perf_cpu_map__cpu (map,perf_cpu_map__nr (map) -1 );if (buf ==NULL )return 0 ;
@@ -644,7 +642,7 @@ size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size)return 0 ; } -for (i =0 ; i < map->nr; i++) { +for (i =0 ; i <perf_cpu_map__nr (map); i++) { cpu =perf_cpu_map__cpu (map, i).cpu; bitmap[cpu /8 ] |=1 << (cpu %8 ); }
tools/perf/util/pmu.c +4 -4
@@ -1966,13 +1966,13 @@ int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,perf_cpu_map__for_each_cpu (cpu, i, cpus) {if (!perf_cpu_map__has (pmu_cpus, cpu)) - unmatched_cpus->map[unmatched_nr++] = cpu; +RC_CHK_ACCESS (unmatched_cpus)->map[unmatched_nr++] = cpu;else - matched_cpus->map[matched_nr++] = cpu; +RC_CHK_ACCESS (matched_cpus)->map[matched_nr++] = cpu; } - unmatched_cpus->nr = unmatched_nr; - matched_cpus->nr = matched_nr; +RC_CHK_ACCESS (unmatched_cpus)->nr = unmatched_nr; +RC_CHK_ACCESS (matched_cpus)->nr = matched_nr; *mcpus_ptr = matched_cpus; *ucpus_ptr = unmatched_cpus;return 0 ;
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH v6 10/12 perf namespaces: Add reference count checking
In reply to Ian RogersAdd reference count checking controlled by REFCNT_CHECKING ifdef. The reference count checking interposes an allocated pointer between the reference counted struct on a get and frees the pointer on a put. Accesses after a put cause faults and use after free, missed puts are caughts as leaks and double puts are double frees.
This checking helped resolve a memory leak and use after free:
https://lore.kernel.org/linux-perf-users/CAP-5=fWZH20L4kv-BwVtGLwR=Em3AOOT+Q4QGivvQuYn5AsPRg@mail.gmail.com/
--- tools/perf/builtin-inject.c | 2 +- tools/perf/util/annotate.c | 2 +- tools/perf/util/dso.c | 2 +- tools/perf/util/dsos.c | 2 +- tools/perf/util/namespaces.c | 132 ++++++++++++++++++++--------------- tools/perf/util/namespaces.h | 3 +- tools/perf/util/symbol.c | 2 +- 7 files changed, 83 insertions(+), 62 deletions(-)
tools/perf/builtin-inject.c +1 -1
@@ -632,7 +632,7 @@ static int dso__read_build_id(struct dso *dso)else if (dso->nsinfo) {char *new_name; - new_name =filename_with_chroot (dso->nsinfo->pid, + new_name =filename_with_chroot (RC_CHK_ACCESS (dso->nsinfo)->pid, dso->long_name);if (new_name &&filename__read_build_id (new_name, &dso->bid) >0 ) dso->has_build_id = true;
tools/perf/util/annotate.c +1 -1
@@ -1693,7 +1693,7 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filmutex_lock (&dso->lock);if (access (filename,R_OK ) && errno ==ENOENT && dso->nsinfo) { -char *new_name =filename_with_chroot (dso->nsinfo->pid, +char *new_name =filename_with_chroot (RC_CHK_ACCESS (dso->nsinfo)->pid, filename);if (new_name) {strlcpy (filename, new_name, filename_size);
tools/perf/util/dso.c +1 -1
@@ -515,7 +515,7 @@ static int __open_dso(struct dso *dso, struct machine *machine)if (errno !=ENOENT || dso->nsinfo ==NULL ) goto out; - new_name =filename_with_chroot (dso->nsinfo->pid, name); + new_name =filename_with_chroot (RC_CHK_ACCESS (dso->nsinfo)->pid, name);if (!new_name) goto out;
tools/perf/util/dsos.c +1 -1
@@ -91,7 +91,7 @@ bool __dsos__read_build_ids(struct list_head *head, bool with_hits) have_build_id = true; pos->has_build_id = true; } else if (errno == ENOENT && pos->nsinfo) { - char *new_name = filename_with_chroot(pos->nsinfo->pid, + char *new_name = filename_with_chroot(RC_CHK_ACCESS(pos->nsinfo)->pid, pos->long_name); if (new_name && filename__read_build_id(new_name,
tools/perf/util/namespaces.c +76 -56
240 lines not on this page · raw message
tools/perf/util/namespaces.h +2 -1
@@ -13,6 +13,7 @@#include <linux/perf_event.h> #include <linux/refcount.h> #include <linux/types.h> +#include <internal/rc_check.h> #ifndef HAVE_SETNS_SUPPORT int setns (int fd,int nstype);
@@ -29,7 +30,7 @@ struct namespaces {struct namespaces *namespaces__new (struct perf_record_namespaces *event);void namespaces__free (struct namespaces *namespaces); -struct nsinfo { +DECLARE_RC_STRUCT (nsinfo) {pid_t pid;pid_t tgid;pid_t nstgid;
tools/perf/util/symbol.c +1 -1
@@ -1963,7 +1963,7 @@ int dso__load(struct dso *dso, struct map *map) is_reg =is_regular_file (name);if (!is_reg && errno ==ENOENT && dso->nsinfo) { -char *new_name =filename_with_chroot (dso->nsinfo->pid, +char *new_name =filename_with_chroot (RC_CHK_ACCESS (dso->nsinfo)->pid, name);if (new_name) { is_reg =is_regular_file (new_name);
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH v6 11/12 perf maps: Add reference count checking.
In reply to Ian RogersAdd reference count checking to make sure of good use of get and put. Add and use accessors to reduce RC_CHK clutter.
The only significant issue was in tests/thread-maps-share.c where reference counts were released in the reverse order to acquisition, leading to a use after put. This was fixed by reversing the put order.
--- tools/perf/tests/thread-maps-share.c | 29 ++++++------- tools/perf/util/machine.c | 2 +- tools/perf/util/maps.c | 53 +++++++++++++----------- tools/perf/util/maps.h | 17 ++++---- tools/perf/util/symbol.c | 13 +++--- tools/perf/util/unwind-libdw.c | 2 +- tools/perf/util/unwind-libunwind-local.c | 2 +- tools/perf/util/unwind-libunwind.c | 2 +- 8 files changed, 64 insertions(+), 56 deletions(-)
tools/perf/tests/thread-maps-share.c +15 -14
@@ -43,12 +43,12 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int s leader && t1 && t2 && t3 && other); maps = leader->maps; -TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&maps->refcnt),4 ); +TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&RC_CHK_ACCESS (maps)->refcnt),4 );/* test the maps pointer is shared */ -TEST_ASSERT_VAL ("maps don't match" , maps == t1->maps); -TEST_ASSERT_VAL ("maps don't match" , maps == t2->maps); -TEST_ASSERT_VAL ("maps don't match" , maps == t3->maps); +TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (maps) ==RC_CHK_ACCESS (t1->maps)); +TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (maps) ==RC_CHK_ACCESS (t2->maps)); +TEST_ASSERT_VAL ("maps don't match" ,RC_CHK_ACCESS (maps) ==RC_CHK_ACCESS (t3->maps));/* * Verify the other leader was created by previous call.
@@ -71,25 +71,26 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int smachine__remove_thread (machine, other_leader); other_maps = other->maps; -TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&other_maps->refcnt),2 ); +TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&RC_CHK_ACCESS (other_maps)->refcnt),2 ); -TEST_ASSERT_VAL ("maps don't match" , other_maps == other_leader->maps); +TEST_ASSERT_VAL ("maps don't match" , +RC_CHK_ACCESS (other_maps) ==RC_CHK_ACCESS (other_leader->maps));/* release thread group */ -thread__put (leader); -TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&maps->refcnt),3 ); - -thread__put (t1); -TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&maps->refcnt),2 ); +thread__put (t3); +TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&RC_CHK_ACCESS (maps)->refcnt),3 );thread__put (t2); -TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&maps->refcnt),1 ); +TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&RC_CHK_ACCESS (maps)->refcnt),2 ); -thread__put (t3); +thread__put (t1); +TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&RC_CHK_ACCESS (maps)->refcnt),1 ); + +thread__put (leader);/* release other group */ thread__put (other_leader); -TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&other_maps->refcnt),1 ); +TEST_ASSERT_EQUAL ("wrong refcnt" ,refcount_read (&RC_CHK_ACCESS (other_maps)->refcnt),1 );thread__put (other);
tools/perf/util/machine.c +1 -1
@@ -435,7 +435,7 @@ static struct thread *findnew_guest_code(struct machine *machine,return NULL ;/* Assume maps are set up if there are any */ -if (thread->maps->nr_maps) +if (RC_CHK_ACCESS (thread->maps)->nr_maps)return thread; host_thread =machine__find_thread (host_machine,-1 , pid);
tools/perf/util/maps.c +29 -24
120 lines not on this page · raw message
tools/perf/util/maps.h +9 -8
@@ -8,6 +8,7 @@#include <stdbool.h> #include <linux/types.h> #include "rwsem.h" +#include <internal/rc_check.h> struct ref_reloc_sym ;struct machine ;
@@ -32,7 +33,7 @@ struct map *maps__find(struct maps *maps, u64 addr);for (map =maps__first (maps), next =map_rb_node__next (map); map; \ map = next, next =map_rb_node__next (map)) -struct maps { +DECLARE_RC_STRUCT(maps) {struct rb_root entries;struct rw_semaphore lock;struct machine *machine;
@@ -65,38 +66,38 @@ void maps__put(struct maps *maps);static inline struct rb_root *maps__entries (struct maps *maps) { -return &maps->entries; +return &RC_CHK_ACCESS (maps)->entries; }static inline struct machine *maps__machine (struct maps *maps) { -return maps->machine; +return RC_CHK_ACCESS (maps)->machine; }static inline struct rw_semaphore *maps__lock (struct maps *maps) { -return &maps->lock; +return &RC_CHK_ACCESS (maps)->lock; }static inline struct map **maps__maps_by_name (struct maps *maps) { -return maps->maps_by_name; +return RC_CHK_ACCESS (maps)->maps_by_name; }static inline unsigned int maps__nr_maps (const struct maps *maps) { -return maps->nr_maps; +return RC_CHK_ACCESS (maps)->nr_maps; }#ifdef HAVE_LIBUNWIND_SUPPORT static inline void *maps__addr_space (struct maps *maps) { -return maps->addr_space; +return RC_CHK_ACCESS (maps)->addr_space; }static inline const struct unwind_libunwind_ops *maps__unwind_libunwind_ops (const struct maps *maps) { -return maps->unwind_libunwind_ops; +return RC_CHK_ACCESS (maps)->unwind_libunwind_ops; }#endif
tools/perf/util/symbol.c +7 -6
@@ -2097,8 +2097,8 @@ static int map__groups__sort_by_name_from_rbtree(struct maps *maps)up_read (maps__lock (maps));down_write (maps__lock (maps)); - maps->maps_by_name = maps_by_name; - maps->nr_maps_allocated =maps__nr_maps (maps); +RC_CHK_ACCESS (maps)->maps_by_name = maps_by_name; +RC_CHK_ACCESS (maps)->nr_maps_allocated =maps__nr_maps (maps);maps__for_each_entry (maps, rb_node) maps_by_name[i++] =map__get (rb_node->map);
@@ -2133,11 +2133,12 @@ struct map *maps__find_by_name(struct maps *maps, const char *name)down_read (maps__lock (maps)); -if (maps->last_search_by_name) { -const struct dso *dso =map__dso (maps->last_search_by_name); + +if (RC_CHK_ACCESS (maps)->last_search_by_name) { +const struct dso *dso =map__dso (RC_CHK_ACCESS (maps)->last_search_by_name);if (strcmp (dso->short_name, name) ==0 ) { - map = maps->last_search_by_name; + map =RC_CHK_ACCESS (maps)->last_search_by_name; goto out_unlock; } }
@@ -2157,7 +2158,7 @@ struct map *maps__find_by_name(struct maps *maps, const char *name) map = rb_node->map; dso =map__dso (map);if (strcmp (dso->short_name, name) ==0 ) { - maps->last_search_by_name = map; +RC_CHK_ACCESS (maps)->last_search_by_name = map; goto out_unlock; } }
tools/perf/util/unwind-libdw.c +1 -1
@@ -230,7 +230,7 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg,struct unwind_info *ui, ui_buf = { .sample = data, .thread = thread, - .machine = thread->maps->machine, + .machine =RC_CHK_ACCESS (thread->maps)->machine, .cb = cb, .arg = arg, .max_stack = max_stack,
tools/perf/util/unwind-libunwind-local.c +1 -1
@@ -677,7 +677,7 @@ static int _unwind__prepare_access(struct maps *maps) {void *addr_space =unw_create_addr_space (&accessors,0 ); - maps->addr_space = addr_space; +RC_CHK_ACCESS (maps)->addr_space = addr_space;if (!addr_space) {pr_err ("unwind: Can't create unwind address space.\n" );return -ENOMEM ;
tools/perf/util/unwind-libunwind.c +1 -1
@@ -14,7 +14,7 @@ struct unwind_libunwind_ops __weak *arm64_unwind_libunwind_ops;static void unwind__register_ops (struct maps *maps,struct unwind_libunwind_ops *ops) { - maps->unwind_libunwind_ops = ops; +RC_CHK_ACCESS (maps)->unwind_libunwind_ops = ops; }int unwind__prepare_access (struct maps *maps,struct map *map,bool *initialized)
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH v6 12/12 perf map: Add reference count checking
In reply to Ian RogersThere's no strict get/put policy with map that leads to leaks or use after free. Reference count checking identifies correct pairing of gets and puts.
--- tools/perf/arch/s390/annotate/instructions.c | 5 +- tools/perf/builtin-top.c | 4 +- tools/perf/tests/hists_link.c | 2 +- tools/perf/tests/maps.c | 20 +++--- tools/perf/tests/vmlinux-kallsyms.c | 4 +- tools/perf/util/annotate.c | 10 +-- tools/perf/util/machine.c | 25 +++---- tools/perf/util/map.c | 69 +++++++++++--------- tools/perf/util/map.h | 32 +++++---- tools/perf/util/maps.c | 11 ++-- tools/perf/util/symbol-elf.c | 26 ++++---- tools/perf/util/symbol.c | 40 +++++++----- 12 files changed, 135 insertions(+), 113 deletions(-)
tools/perf/arch/s390/annotate/instructions.c +3 -2
@@ -39,8 +39,9 @@ static int s390_call__parse(struct arch *arch, struct ins_operands *ops, target.addr =map__objdump_2mem (map, ops->target.addr);if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == - ops->target.addr) +map__rip_2objdump (target.ms.map, +RC_CHK_ACCESS (map)->map_ip (target.ms.map, target.addr) + ) == ops->target.addr) ops->target.sym = target.ms.sym;return 0 ;
tools/perf/builtin-top.c +2 -2
@@ -191,7 +191,7 @@ static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)if (use_browser <=0 )sleep (5 ); - map->erange_warned = true; +RC_CHK_ACCESS (map)->erange_warned = true; } static void perf_top__record_precise_ip(struct perf_top *top,
@@ -225,7 +225,7 @@ static void perf_top__record_precise_ip(struct perf_top *top,*/ mutex_unlock (&he->hists->lock); -if (err == -ERANGE && !he->ms.map->erange_warned) +if (err == -ERANGE && !RC_CHK_ACCESS (he->ms.map)->erange_warned)ui__warn_map_erange (he->ms.map, sym, ip);else if (err == -ENOMEM ) { pr_err("Not enough memory for annotating '%s' symbol!\n",
tools/perf/tests/hists_link.c +1 -1
@@ -145,7 +145,7 @@ static int find_sample(struct sample *samples, size_t nr_samples, {while (nr_samples--) {if (samples->thread == t && - samples->map == m && +RC_CHK_ACCESS (samples->map) ==RC_CHK_ACCESS (m) && samples->sym == s)return 1 ; samples++;
tools/perf/tests/maps.c +10 -10
@@ -30,7 +30,7 @@ static int check_maps(struct map_def *merged, unsigned int size, struct maps *maif (map__start (map) != merged[i].start ||map__end (map) != merged[i].end ||strcmp (map__dso (map)->name, merged[i].name) || -refcount_read (&map->refcnt) !=1 ) { +refcount_read (&RC_CHK_ACCESS (map)->refcnt) !=1 ) { failed = true; } i++;
@@ -50,7 +50,7 @@ static int check_maps(struct map_def *merged, unsigned int size, struct maps *mamap__start (map),map__end (map),map__dso (map)->name, - refcount_read(&map->refcnt)); + refcount_read(&RC_CHK_ACCESS(map)->refcnt)); } }return failed ?TEST_FAIL :TEST_OK ;
@@ -95,8 +95,8 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest map =dso__new_map (bpf_progs[i].name);TEST_ASSERT_VAL ("failed to create map" , map); - map->start = bpf_progs[i].start; - map->end = bpf_progs[i].end; +RC_CHK_ACCESS (map)->start = bpf_progs[i].start; +RC_CHK_ACCESS (map)->end = bpf_progs[i].end;TEST_ASSERT_VAL ("failed to insert map" ,maps__insert (maps, map) ==0 );map__put (map); }
@@ -111,16 +111,16 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtestTEST_ASSERT_VAL ("failed to create map" , map_kcore3);/* kcore1 map overlaps over all bpf maps */ - map_kcore1->start =100 ; - map_kcore1->end =1000 ; +RC_CHK_ACCESS (map_kcore1)->start =100 ; +RC_CHK_ACCESS (map_kcore1)->end =1000 ;/* kcore2 map hides behind bpf_prog_2 */ - map_kcore2->start =550 ; - map_kcore2->end =570 ; +RC_CHK_ACCESS (map_kcore2)->start =550 ; +RC_CHK_ACCESS (map_kcore2)->end =570 ;/* kcore3 map hides behind bpf_prog_3, kcore1 and adds new map */ - map_kcore3->start =880 ; - map_kcore3->end =1100 ; +RC_CHK_ACCESS (map_kcore3)->start =880 ; +RC_CHK_ACCESS (map_kcore3)->end =1100 ; ret =maps__merge_in (maps, map_kcore1);TEST_ASSERT_VAL ("failed to merge map" , !ret);
tools/perf/tests/vmlinux-kallsyms.c +2 -2
@@ -304,7 +304,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused dso->short_name : dso->name));if (pair) { - pair->priv =1 ; +RC_CHK_ACCESS (pair)->priv =1 ; }else {if (!header_printed) {pr_info ("WARN: Maps only in vmlinux:\n" );
@@ -340,7 +340,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unusedpr_info (":\nWARN: *%" PRIx64"-%" PRIx64" %" PRIx64,map__start (pair),map__end (pair),map__pgoff (pair));pr_info (" %s\n" , dso->name); - pair->priv =1 ; +RC_CHK_ACCESS (pair)->priv =1 ; } }
tools/perf/util/annotate.c +6 -4
@@ -272,8 +272,9 @@ static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_s target.addr =map__objdump_2mem (map, ops->target.addr);if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == - ops->target.addr) +map__rip_2objdump (target.ms.map, +RC_CHK_ACCESS (map)->map_ip (target.ms.map, target.addr) + ) == ops->target.addr) ops->target.sym = target.ms.sym;return 0 ;
@@ -401,8 +402,9 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s* the symbol searching and disassembly should be done. */ if (maps__find_ams (ms->maps, &target) ==0 && -map__rip_2objdump (target.ms.map, map->map_ip (target.ms.map, target.addr)) == - ops->target.addr) +map__rip_2objdump (target.ms.map, +RC_CHK_ACCESS (map)->map_ip (target.ms.map, target.addr) + ) == ops->target.addr) ops->target.sym = target.ms.sym;if (!ops->target.outside) {
tools/perf/util/machine.c +13 -12
@@ -910,8 +910,8 @@ static int machine__process_ksymbol_register(struct machine *machine,dso__set_loaded (dso); } - map->start = event->ksymbol.addr; - map->end =map__start (map) + event->ksymbol.len; +RC_CHK_ACCESS (map)->start = event->ksymbol.addr; +RC_CHK_ACCESS (map)->end =map__start (map) + event->ksymbol.len; err =maps__insert (machine__kernel_maps (machine), map);if (err) { err = -ENOMEM ;
@@ -953,7 +953,7 @@ static int machine__process_ksymbol_unregister(struct machine *machine,if (!map)return 0 ; -if (map != machine->vmlinux_map) +if (RC_CHK_ACCESS (map) !=RC_CHK_ACCESS (machine->vmlinux_map))maps__remove (machine__kernel_maps (machine), map);else {struct dso *dso =map__dso (map);
@@ -1218,8 +1218,8 @@ int machine__create_extra_kernel_map(struct machine *machine,if (!map)return -ENOMEM ; - map->end = xm->end; - map->pgoff = xm->pgoff; +RC_CHK_ACCESS (map)->end = xm->end; +RC_CHK_ACCESS (map)->pgoff = xm->pgoff; kmap =map__kmap (map);
@@ -1291,7 +1291,7 @@ int machine__map_x86_64_entry_trampolines(struct machine *machine, dest_map =maps__find (kmaps,map__pgoff (map));if (dest_map != map) - map->pgoff =map__map_ip (dest_map,map__pgoff (map)); +RC_CHK_ACCESS (map)->pgoff =map__map_ip (dest_map,map__pgoff (map)); found = true; }if (found || machine->trampolines_mapped)
@@ -1342,7 +1342,8 @@ __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)if (machine->vmlinux_map ==NULL )return -ENOMEM ; - machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip; +RC_CHK_ACCESS (machine->vmlinux_map)->map_ip = identity__map_ip; +RC_CHK_ACCESS (machine->vmlinux_map)->unmap_ip = identity__map_ip;return maps__insert (machine__kernel_maps (machine), machine->vmlinux_map); }
@@ -1623,7 +1624,7 @@ static int machine__create_module(void *arg, const char *name, u64 start, map =machine__addnew_module_map (machine, start, name);if (map ==NULL )return -1 ; - map->end = start + size; +RC_CHK_ACCESS (map)->end = start + size;dso__kernel_module_get_build_id (map__dso (map), machine->root_dir);map__put (map);
@@ -1659,14 +1660,14 @@ static int machine__create_modules(struct machine *machine)static void machine__set_kernel_mmap (struct machine *machine,u64 start,u64 end) { - machine->vmlinux_map->start = start; - machine->vmlinux_map->end = end; +RC_CHK_ACCESS (machine->vmlinux_map)->start = start; +RC_CHK_ACCESS (machine->vmlinux_map)->end = end;/* * Be a bit paranoid here, some perf.data file came with * a zero sized synthesized MMAP event for the kernel. */ if (start ==0 && end ==0 ) - machine->vmlinux_map->end = ~0ULL ; +RC_CHK_ACCESS (machine->vmlinux_map)->end = ~0ULL ; } static int machine__update_kernel_mmap(struct machine *machine,
@@ -1810,7 +1811,7 @@ static int machine__process_kernel_mmap_event(struct machine *machine,if (map ==NULL ) goto out_problem; - map->end =map__start (map) + xm->end - xm->start; +RC_CHK_ACCESS (map)->end =map__start (map) + xm->end - xm->start;if (build_id__is_defined (bid))dso__set_build_id (map__dso (map), bid);
tools/perf/util/map.c +38 -31
158 lines not on this page · raw message
tools/perf/util/map.h +18 -14
@@ -10,12 +10,13 @@#include <string.h> #include <stdbool.h> #include <linux/types.h> +#include <internal/rc_check.h> struct dso ;struct maps ;struct machine ; -struct map { +DECLARE_RC_STRUCT (map) {u64 start;u64 end; bool erange_warned:1;
@@ -49,52 +50,52 @@ u64 identity__map_ip(const struct map *map __maybe_unused, u64 ip);static inline struct dso *map__dso (const struct map *map) { -return map->dso; +return RC_CHK_ACCESS (map)->dso; }static inline u64 map__map_ip (const struct map *map,u64 ip) { -return map->map_ip (map, ip); +return RC_CHK_ACCESS (map)->map_ip (map, ip); }static inline u64 map__unmap_ip (const struct map *map,u64 ip) { -return map->unmap_ip (map, ip); +return RC_CHK_ACCESS (map)->unmap_ip (map, ip); }static inline u64 map__start (const struct map *map) { -return map->start; +return RC_CHK_ACCESS (map)->start; }static inline u64 map__end (const struct map *map) { -return map->end; +return RC_CHK_ACCESS (map)->end; }static inline u64 map__pgoff (const struct map *map) { -return map->pgoff; +return RC_CHK_ACCESS (map)->pgoff; }static inline u64 map__reloc (const struct map *map) { -return map->reloc; +return RC_CHK_ACCESS (map)->reloc; }static inline u32 map__flags (const struct map *map) { -return map->flags; +return RC_CHK_ACCESS (map)->flags; }static inline u32 map__prot (const struct map *map) { -return map->prot; +return RC_CHK_ACCESS (map)->prot; }static inline bool map__priv (const struct map *map) { -return map->priv; +return RC_CHK_ACCESS (map)->priv; }static inline size_t map__size (const struct map *map)
@@ -153,9 +154,12 @@ struct map *map__clone(struct map *map);static inline struct map *map__get (struct map *map) { -if (map) -refcount_inc (&map->refcnt); -return map; +struct map *result; + +if (RC_CHK_GET (result, map)) +refcount_inc (&RC_CHK_ACCESS (map)->refcnt); + +return result; }void map__put (struct map *map);
tools/perf/util/maps.c +6 -5
@@ -126,7 +126,7 @@ void maps__remove(struct maps *maps, struct map *map)RC_CHK_ACCESS (maps)->last_search_by_name =NULL ; rb_node =maps__find_node (maps, map); -assert (rb_node->map == map); +assert (rb_node->RC_CHK_ACCESS (map) ==RC_CHK_ACCESS (map));__maps__remove (maps, rb_node);if (maps__maps_by_name (maps))__maps__free_maps_by_name (maps);
@@ -339,7 +339,7 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) goto put_map; } - before->end =map__start (map); +RC_CHK_ACCESS (before)->end =map__start (map); err =__maps__insert (maps, before);if (err) {map__put (before);
@@ -359,8 +359,9 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) goto put_map; } - after->start =map__end (map); - after->pgoff +=map__end (map) -map__start (pos->map); +RC_CHK_ACCESS (after)->start =map__end (map); +RC_CHK_ACCESS (after)->pgoff += +map__end (map) -map__start (pos->map);assert (map__map_ip (pos->map,map__end (map)) ==map__map_ip (after,map__end (map))); err =__maps__insert (maps, after);
@@ -420,7 +421,7 @@ struct map_rb_node *maps__find_node(struct maps *maps, struct map *map)struct map_rb_node *rb_node;maps__for_each_entry (maps, rb_node) { -if (rb_node->map == map) +if (rb_node->RC_CHK_ACCESS (map) ==RC_CHK_ACCESS (map))return rb_node; }return NULL ;
tools/perf/util/symbol-elf.c +14 -12
@@ -1354,11 +1354,11 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map,*/ if (*remap_kernel && dso->kernel && !kmodule) { *remap_kernel = false; - map->start = shdr->sh_addr +ref_reloc (kmap); - map->end =map__start (map) + shdr->sh_size; - map->pgoff = shdr->sh_offset; - map->map_ip = map__dso_map_ip; - map->unmap_ip = map__dso_unmap_ip; +RC_CHK_ACCESS (map)->start = shdr->sh_addr +ref_reloc (kmap); +RC_CHK_ACCESS (map)->end =map__start (map) + shdr->sh_size; +RC_CHK_ACCESS (map)->pgoff = shdr->sh_offset; +RC_CHK_ACCESS (map)->map_ip = map__dso_map_ip; +RC_CHK_ACCESS (map)->unmap_ip = map__dso_unmap_ip;/* Ensure maps are correctly ordered */ if (kmaps) {int err;
@@ -1379,7 +1379,7 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map,*/ if (*remap_kernel && kmodule) { *remap_kernel = false; - map->pgoff = shdr->sh_offset; +RC_CHK_ACCESS (map)->pgoff = shdr->sh_offset; } *curr_mapp = map;
@@ -1414,11 +1414,13 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map,map__kmap (curr_map)->kmaps = kmaps;if (adjust_kernel_syms) { - curr_map->start = shdr->sh_addr +ref_reloc (kmap); - curr_map->end =map__start (curr_map) + shdr->sh_size; - curr_map->pgoff = shdr->sh_offset; +RC_CHK_ACCESS (curr_map)->start = shdr->sh_addr +ref_reloc (kmap); +RC_CHK_ACCESS (curr_map)->end =map__start (curr_map) + + shdr->sh_size; +RC_CHK_ACCESS (curr_map)->pgoff = shdr->sh_offset; }else { - curr_map->map_ip = curr_map->unmap_ip = identity__map_ip; +RC_CHK_ACCESS (curr_map)->map_ip = identity__map_ip; +RC_CHK_ACCESS (curr_map)->unmap_ip = identity__map_ip; } curr_dso->symtab_type = dso->symtab_type;if (maps__insert (kmaps, curr_map))
@@ -1525,7 +1527,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,if (strcmp (elf_name, kmap->ref_reloc_sym->name))continue ; kmap->ref_reloc_sym->unrelocated_addr = sym.st_value; - map->reloc = kmap->ref_reloc_sym->addr - +RC_CHK_ACCESS (map)->reloc = kmap->ref_reloc_sym->addr - kmap->ref_reloc_sym->unrelocated_addr;break ; }
@@ -1536,7 +1538,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,* attempted to prelink vdso to its virtual address. */ if (dso__is_vdso (dso)) - map->reloc =map__start (map) - dso->text_offset; +RC_CHK_ACCESS (map)->reloc =map__start (map) - dso->text_offset; dso->adjust_symbols = runtime_ss->adjust_symbols ||ref_reloc (kmap);/*
tools/perf/util/symbol.c +22 -18
@@ -279,7 +279,7 @@ void maps__fixup_end(struct maps *maps)maps__for_each_entry (maps, curr) {if (prev !=NULL && !map__end (prev->map)) - prev->map->end =map__start (curr->map); +RC_CHK_ACCESS (prev->map)->end =map__start (curr->map); prev = curr; }
@@ -289,7 +289,7 @@ void maps__fixup_end(struct maps *maps)* last map final address. */ if (curr && !map__end (curr->map)) - curr->map->end = ~0ULL ; +RC_CHK_ACCESS (curr->map)->end = ~0ULL ;up_write (maps__lock (maps)); }
@@ -865,7 +865,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, *module++ ='\0' ; curr_map_dso =map__dso (curr_map);if (strcmp (curr_map_dso->short_name, module)) { -if (curr_map != initial_map && +if (RC_CHK_ACCESS (curr_map) !=RC_CHK_ACCESS (initial_map) && dso->kernel ==DSO_SPACE__KERNEL_GUEST &&machine__is_default_guest (machine)) {/*
@@ -944,7 +944,8 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,return -1 ; } - curr_map->map_ip = curr_map->unmap_ip = identity__map_ip; +RC_CHK_ACCESS (curr_map)->map_ip = identity__map_ip; +RC_CHK_ACCESS (curr_map)->unmap_ip = identity__map_ip;if (maps__insert (kmaps, curr_map)) {dso__put (ndso);return -1 ;
@@ -1250,8 +1251,8 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)return -ENOMEM ; } - list_node->map->end =map__start (list_node->map) + len; - list_node->map->pgoff = pgoff; + list_node->RC_CHK_ACCESS (map)->end =map__start (list_node->map) + len; + list_node->RC_CHK_ACCESS (map)->pgoff = pgoff;list_add (&list_node->node, &md->maps);
@@ -1286,7 +1287,7 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map)* |new......| -> |new..| * |old....| -> |old....| */ - new_map->end =map__start (old_map); +RC_CHK_ACCESS (new_map)->end =map__start (old_map); }else {/* * |new.............| -> |new..| |new..|
@@ -1306,10 +1307,12 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map) goto out; } - m->map->end =map__start (old_map); + +RC_CHK_ACCESS (m->map)->end =map__start (old_map);list_add_tail (&m->node, &merged); - new_map->pgoff +=map__end (old_map) -map__start (new_map); - new_map->start =map__end (old_map); +RC_CHK_ACCESS (new_map)->pgoff += +map__end (old_map) -map__start (new_map); +RC_CHK_ACCESS (new_map)->start =map__end (old_map); } }else {/*
@@ -1329,8 +1332,9 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map)* |new......| -> |new...| * |old....| -> |old....| */ - new_map->pgoff +=map__end (old_map) -map__start (new_map); - new_map->start =map__end (old_map); +RC_CHK_ACCESS (new_map)->pgoff += +map__end (old_map) -map__start (new_map); +RC_CHK_ACCESS (new_map)->start =map__end (old_map); } } }
@@ -1456,12 +1460,12 @@ static int dso__load_kcore(struct dso *dso, struct map *map,list_del_init (&new_node->node); -if (new_map == replacement_map) { - map->start =map__start (new_map); - map->end =map__end (new_map); - map->pgoff =map__pgoff (new_map); - map->map_ip = new_map->map_ip; - map->unmap_ip = new_map->unmap_ip; +if (RC_CHK_ACCESS (new_map) ==RC_CHK_ACCESS (replacement_map)) { +RC_CHK_ACCESS (map)->start =map__start (new_map); +RC_CHK_ACCESS (map)->end =map__end (new_map); +RC_CHK_ACCESS (map)->pgoff =map__pgoff (new_map); +RC_CHK_ACCESS (map)->map_ip =RC_CHK_ACCESS (new_map)->map_ip; +RC_CHK_ACCESS (map)->unmap_ip =RC_CHK_ACCESS (new_map)->unmap_ip;/* Ensure maps are correctly ordered */ map__get (map);maps__remove (kmaps, map);
Signature · 1 line
2.40.0.348.gf938b09366-goog