Skip to content

Commit

Permalink
perf map: Add accessor for start and end
Browse files Browse the repository at this point in the history
Later changes will add reference count checking for struct map, start
and end are frequently accessed variables. Add an accessor so that the
reference count check is only necessary in one place.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Eric Dumazet <edumazet@google.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Hao Luo <haoluo@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Miaoqian Lin <linmq006@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Riccardo Mancini <rickyman7@gmail.com>
Cc: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
Cc: Song Liu <song@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Yury Norov <yury.norov@gmail.com>
Link: https://lore.kernel.org/r/20230320212248.1175731-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Ian Rogers authored and Arnaldo Carvalho de Melo committed Apr 4, 2023
1 parent 63df0e4 commit e5116f4
Show file tree
Hide file tree
Showing 29 changed files with 125 additions and 114 deletions.
2 changes: 1 addition & 1 deletion tools/perf/arch/arm/tests/dwarf-unwind.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static int sample_ustack(struct perf_sample *sample,
return -1;
}

stack_size = map->end - sp;
stack_size = map__end(map) - sp;
stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;

memcpy(buf, (void *) sp, stack_size);
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/arch/arm64/tests/dwarf-unwind.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static int sample_ustack(struct perf_sample *sample,
return -1;
}

stack_size = map->end - sp;
stack_size = map__end(map) - sp;
stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;

memcpy(buf, (void *) sp, stack_size);
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/arch/powerpc/tests/dwarf-unwind.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static int sample_ustack(struct perf_sample *sample,
return -1;
}

stack_size = map->end - sp;
stack_size = map__end(map) - sp;
stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;

memcpy(buf, (void *) sp, stack_size);
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/arch/powerpc/util/skip-callchain-idx.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
return skip_slot;
}

rc = check_return_addr(dso, al.map->start, ip);
rc = check_return_addr(dso, map__start(al.map), ip);

pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n",
dso->long_name, al.sym->name, ip, rc);
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/arch/x86/tests/dwarf-unwind.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static int sample_ustack(struct perf_sample *sample,
return -1;
}

stack_size = map->end - sp;
stack_size = map__end(map) - sp;
stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;

memcpy(buf, (void *) sp, stack_size);
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/arch/x86/util/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,

event->mmap.header.size = size;

event->mmap.start = map->start;
event->mmap.len = map->end - map->start;
event->mmap.start = map__start(map);
event->mmap.len = map__size(map);
event->mmap.pgoff = map->pgoff;
event->mmap.pid = machine->pid;

Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-buildid-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static int buildid__map_cb(struct map *map, void *arg __maybe_unused)
memset(bid_buf, 0, sizeof(bid_buf));
if (dso->has_build_id)
build_id__sprintf(&dso->bid, bid_buf);
printf("%s %16" PRIx64 " %16" PRIx64, bid_buf, map->start, map->end);
printf("%s %16" PRIx64 " %16" PRIx64, bid_buf, map__start(map), map__end(map));
if (dso->long_name != NULL) {
printf(" %s", dso->long_name);
} else if (dso->short_name != NULL) {
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-report.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
const struct dso *dso = map__dso(map);

printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n",
indent, "", map->start, map->end,
indent, "", map__start(map), map__end(map),
map->prot & PROT_READ ? 'r' : '-',
map->prot & PROT_WRITE ? 'w' : '-',
map->prot & PROT_EXEC ? 'x' : '-',
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-script.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
if (al.addr < al.sym->end)
off = al.addr - al.sym->start;
else
off = al.addr - al.map->start - al.sym->start;
off = al.addr - map__start(al.map) - al.sym->start;
printed += fprintf(fp, "\t%s", al.sym->name);
if (off)
printed += fprintf(fp, "%+d", off);
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-top.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
"Not all samples will be on the annotation output.\n\n"
"Please report to linux-kernel@vger.kernel.org\n",
ip, dso->long_name, dso__symtab_origin(dso),
map->start, map->end, sym->start, sym->end,
map__start(map), map__end(map), sym->start, sym->end,
sym->binding == STB_GLOBAL ? 'g' :
sym->binding == STB_LOCAL ? 'l' : 'w', sym->name,
err ? "[unknown]" : uts.machine,
Expand Down
8 changes: 4 additions & 4 deletions tools/perf/tests/code-reading.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
len = BUFSZ;

/* Do not go off the map */
if (addr + len > al.map->end)
len = al.map->end - addr;
if (addr + len > map__end(al.map))
len = map__end(al.map) - addr;

/* Read the object code using perf */
ret_len = dso__data_read_offset(dso, maps__machine(thread->maps),
Expand All @@ -291,7 +291,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
size_t d;

for (d = 0; d < state->done_cnt; d++) {
if (state->done[d] == al.map->start) {
if (state->done[d] == map__start(al.map)) {
pr_debug("kcore map tested already");
pr_debug(" - skipping\n");
goto out;
Expand All @@ -301,7 +301,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
pr_debug("Too many kcore maps - skipping\n");
goto out;
}
state->done[state->done_cnt++] = al.map->start;
state->done[state->done_cnt++] = map__start(al.map);
}

objdump_name = dso->long_name;
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/tests/maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ static int check_maps(struct map_def *merged, unsigned int size, struct maps *ma
if (i > 0)
TEST_ASSERT_VAL("less maps expected", (map && i < size) || (!map && i == size));

TEST_ASSERT_VAL("wrong map start", map->start == merged[i].start);
TEST_ASSERT_VAL("wrong map end", map->end == merged[i].end);
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);

Expand Down
2 changes: 1 addition & 1 deletion tools/perf/tests/mmap-thread-lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ static int mmap_events(synth_cb synth)
break;
}

pr_debug("map %p, addr %" PRIx64 "\n", al.map, al.map->start);
pr_debug("map %p, addr %" PRIx64 "\n", al.map, map__start(al.map));
}

machine__delete_threads(machine);
Expand Down
14 changes: 7 additions & 7 deletions tools/perf/tests/vmlinux-kallsyms.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused

continue;
}
} else if (mem_start == kallsyms.vmlinux_map->end) {
} else if (mem_start == map__end(kallsyms.vmlinux_map)) {
/*
* Ignore aliases to _etext, i.e. to the end of the kernel text area,
* such as __indirect_thunk_end.
Expand Down Expand Up @@ -319,14 +319,14 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused
maps__for_each_entry(maps, rb_node) {
struct map *pair, *map = rb_node->map;

mem_start = vmlinux_map->unmap_ip(vmlinux_map, map->start);
mem_end = vmlinux_map->unmap_ip(vmlinux_map, map->end);
mem_start = vmlinux_map->unmap_ip(vmlinux_map, map__start(map));
mem_end = vmlinux_map->unmap_ip(vmlinux_map, map__end(map));

pair = maps__find(kallsyms.kmaps, mem_start);
if (pair == NULL || pair->priv)
continue;

if (pair->start == mem_start) {
if (map__start(pair) == mem_start) {
struct dso *dso = map__dso(map);

if (!header_printed) {
Expand All @@ -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->end, map->pgoff, dso->name);
if (mem_end != pair->end)
map__start(map), map__end(map), map->pgoff, dso->name);
if (mem_end != map__end(pair))
pr_info(":\nWARN: *%" PRIx64 "-%" PRIx64 " %" PRIx64,
pair->start, pair->end, pair->pgoff);
map__start(pair), map__end(pair), pair->pgoff);
pr_info(" %s\n", dso->name);
pair->priv = 1;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/util/annotate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1008,13 +1008,13 @@ int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
if (start &&
(start->ms.sym == ams->ms.sym ||
(ams->ms.sym &&
start->addr == ams->ms.sym->start + ams->ms.map->start)))
start->addr == ams->ms.sym->start + map__start(ams->ms.map))))
saddr = start->al_addr;
if (saddr == 0)
pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
ams->addr,
start ? start->addr : 0,
ams->ms.sym ? ams->ms.sym->start + ams->ms.map->start : 0,
ams->ms.sym ? ams->ms.sym->start + map__start(ams->ms.map) : 0,
saddr);
err = symbol__account_cycles(ams->al_addr, saddr, ams->ms.sym, cycles);
if (err)
Expand Down
8 changes: 4 additions & 4 deletions tools/perf/util/dlfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al)
if (al->addr < sym->end)
d_al->symoff = al->addr - sym->start;
else
d_al->symoff = al->addr - al->map->start - sym->start;
d_al->symoff = al->addr - map__start(al->map) - sym->start;
d_al->sym_binding = sym->binding;
} else {
d_al->sym = NULL;
Expand Down Expand Up @@ -268,7 +268,7 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)

map = al->map;

if (map && ip >= map->start && ip < map->end &&
if (map && ip >= map__start(map) && ip < map__end(map) &&
machine__kernel_ip(d->machine, ip) == machine__kernel_ip(d->machine, d->sample->ip))
goto have_map;

Expand All @@ -279,8 +279,8 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)
map = a.map;
have_map:
offset = map->map_ip(map, ip);
if (ip + len >= map->end)
len = map->end - 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);
}

Expand Down
8 changes: 4 additions & 4 deletions tools/perf/util/intel-pt.c
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
goto out_no_cache;
}

if (*ip >= al.map->end)
if (*ip >= map__end(al.map))
break;

offset += intel_pt_insn->length;
Expand Down Expand Up @@ -2750,7 +2750,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
if (sym->binding == STB_GLOBAL &&
!strcmp(sym->name, "__switch_to")) {
ip = map->unmap_ip(map, sym->start);
if (ip >= map->start && ip < map->end) {
if (ip >= map__start(map) && ip < map__end(map)) {
switch_ip = ip;
break;
}
Expand All @@ -2768,7 +2768,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);
if (ip >= map->start && ip < map->end) {
if (ip >= map__start(map) && ip < map__end(map)) {
*ptss_ip = ip;
break;
}
Expand Down Expand Up @@ -3356,7 +3356,7 @@ static int intel_pt_process_aux_output_hw_id(struct intel_pt *pt,
static int intel_pt_find_map(struct thread *thread, u8 cpumode, u64 addr,
struct addr_location *al)
{
if (!al->map || addr < al->map->start || addr >= al->map->end) {
if (!al->map || addr < map__start(al->map) || addr >= map__end(al->map)) {
if (!thread__find_map(thread, cpumode, addr, al))
return -1;
}
Expand Down
14 changes: 7 additions & 7 deletions tools/perf/util/machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
}

map->start = event->ksymbol.addr;
map->end = map->start + event->ksymbol.len;
map->end = map__start(map) + event->ksymbol.len;
err = maps__insert(machine__kernel_maps(machine), map);
map__put(map);
if (err)
Expand All @@ -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),
sym = symbol__new(map->map_ip(map, map__start(map)),
event->ksymbol.len,
0, 0, event->ksymbol.name);
if (!sym)
Expand All @@ -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));
sym = dso__find_symbol(dso, map->map_ip(map, map__start(map)));
if (sym)
dso__delete_symbol(dso, sym);
}
Expand Down Expand Up @@ -1217,7 +1217,7 @@ int machine__create_extra_kernel_map(struct machine *machine,

if (!err) {
pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
kmap->name, map->start, map->end);
kmap->name, map__start(map), map__end(map));
}

map__put(map);
Expand Down Expand Up @@ -1722,7 +1722,7 @@ int machine__create_kernel_maps(struct machine *machine)
struct map_rb_node *next = map_rb_node__next(rb_node);

if (next)
machine__set_kernel_mmap(machine, start, next->map->start);
machine__set_kernel_mmap(machine, start, map__start(next->map));
}

out_put:
Expand Down Expand Up @@ -1795,7 +1795,7 @@ static int machine__process_kernel_mmap_event(struct machine *machine,
if (map == NULL)
goto out_problem;

map->end = map->start + xm->end - xm->start;
map->end = map__start(map) + xm->end - xm->start;

if (build_id__is_defined(bid))
dso__set_build_id(map__dso(map), bid);
Expand Down Expand Up @@ -3293,7 +3293,7 @@ int machine__get_kernel_start(struct machine *machine)
* kernel_start = 1ULL << 63 for x86_64.
*/
if (!err && !machine__is(machine, "x86_64"))
machine->kernel_start = map->start;
machine->kernel_start = map__start(map);
}
return err;
}
Expand Down
8 changes: 4 additions & 4 deletions tools/perf/util/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -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->end, map->pgoff, dso->name);
map__start(map), map__end(map), map->pgoff, dso->name);
}

size_t map__fprintf_dsoname(struct map *map, FILE *fp)
Expand Down Expand Up @@ -558,7 +558,7 @@ bool map__contains_symbol(const struct map *map, const struct symbol *sym)
{
u64 ip = map->unmap_ip(map, sym->start);

return ip >= map->start && ip < map->end;
return ip >= map__start(map) && ip < map__end(map);
}

struct kmap *__map__kmap(struct map *map)
Expand Down Expand Up @@ -592,12 +592,12 @@ struct maps *map__kmaps(struct map *map)

u64 map__map_ip(const struct map *map, u64 ip)
{
return ip - map->start + map->pgoff;
return ip - map__start(map) + map->pgoff;
}

u64 map__unmap_ip(const struct map *map, u64 ip)
{
return ip + map->start - map->pgoff;
return ip + map__start(map) - map->pgoff;
}

u64 identity__map_ip(const struct map *map __maybe_unused, u64 ip)
Expand Down
12 changes: 11 additions & 1 deletion tools/perf/util/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,19 @@ static inline struct dso *map__dso(const struct map *map)
return map->dso;
}

static inline u64 map__start(const struct map *map)
{
return map->start;
}

static inline u64 map__end(const struct map *map)
{
return map->end;
}

static inline size_t map__size(const struct map *map)
{
return map->end - map->start;
return map__end(map) - map__start(map);
}

/* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
Expand Down
Loading

0 comments on commit e5116f4

Please sign in to comment.