Skip to content

Commit

Permalink
perf maps: Remove rb_node from struct map
Browse files Browse the repository at this point in the history
struct map is reference counted, having it also be a node in an
red-black tree complicates the reference counting. Switch to having a
map_rb_node which is a red-block tree node but points at the reference
counted struct map. This reference is responsible for a single reference
count.

Committer notes:

Fixed up tools/perf/util/unwind-libunwind-local.c to use map_rb_node as
well.

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 8372020 commit ff583dc
Show file tree
Hide file tree
Showing 17 changed files with 295 additions and 186 deletions.
13 changes: 7 additions & 6 deletions tools/perf/arch/x86/util/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
struct machine *machine)
{
int rc = 0;
struct map *pos;
struct map_rb_node *pos;
struct maps *kmaps = machine__kernel_maps(machine);
union perf_event *event = zalloc(sizeof(event->mmap) +
machine->id_hdr_size);
Expand All @@ -33,11 +33,12 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
maps__for_each_entry(kmaps, pos) {
struct kmap *kmap;
size_t size;
struct map *map = pos->map;

if (!__map__is_extra_kernel_map(pos))
if (!__map__is_extra_kernel_map(map))
continue;

kmap = map__kmap(pos);
kmap = map__kmap(map);

size = sizeof(event->mmap) - sizeof(event->mmap.filename) +
PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) +
Expand All @@ -58,9 +59,9 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,

event->mmap.header.size = size;

event->mmap.start = pos->start;
event->mmap.len = pos->end - pos->start;
event->mmap.pgoff = pos->pgoff;
event->mmap.start = map->start;
event->mmap.len = map->end - map->start;
event->mmap.pgoff = map->pgoff;
event->mmap.pid = machine->pid;

strlcpy(event->mmap.filename, kmap->name, PATH_MAX);
Expand Down
6 changes: 4 additions & 2 deletions tools/perf/builtin-report.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,9 +844,11 @@ static struct task *tasks_list(struct task *task, struct machine *machine)
static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
{
size_t printed = 0;
struct map *map;
struct map_rb_node *rb_node;

maps__for_each_entry(maps, rb_node) {
struct map *map = rb_node->map;

maps__for_each_entry(maps, map) {
printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n",
indent, "", map->start, map->end,
map->prot & PROT_READ ? 'r' : '-',
Expand Down
8 changes: 5 additions & 3 deletions tools/perf/tests/maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ struct map_def {

static int check_maps(struct map_def *merged, unsigned int size, struct maps *maps)
{
struct map *map;
struct map_rb_node *rb_node;
unsigned int i = 0;

maps__for_each_entry(maps, map) {
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));

Expand Down Expand Up @@ -74,7 +76,7 @@ static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest

map->start = bpf_progs[i].start;
map->end = bpf_progs[i].end;
maps__insert(maps, map);
TEST_ASSERT_VAL("failed to insert map", maps__insert(maps, map) == 0);
map__put(map);
}

Expand Down
17 changes: 10 additions & 7 deletions tools/perf/tests/vmlinux-kallsyms.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused
int err = TEST_FAIL;
struct rb_node *nd;
struct symbol *sym;
struct map *kallsyms_map, *vmlinux_map, *map;
struct map *kallsyms_map, *vmlinux_map;
struct map_rb_node *rb_node;
struct machine kallsyms, vmlinux;
struct maps *maps;
u64 mem_start, mem_end;
Expand Down Expand Up @@ -290,15 +291,15 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused

header_printed = false;

maps__for_each_entry(maps, map) {
struct map *
maps__for_each_entry(maps, rb_node) {
struct map *map = rb_node->map;
/*
* If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
* the kernel will have the path for the vmlinux file being used,
* so use the short name, less descriptive but the same ("[kernel]" in
* both cases.
*/
pair = maps__find_by_name(kallsyms.kmaps, (map->dso->kernel ?
struct map *pair = maps__find_by_name(kallsyms.kmaps, (map->dso->kernel ?
map->dso->short_name :
map->dso->name));
if (pair) {
Expand All @@ -314,8 +315,8 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused

header_printed = false;

maps__for_each_entry(maps, map) {
struct map *pair;
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);
Expand Down Expand Up @@ -344,7 +345,9 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused

maps = machine__kernel_maps(&kallsyms);

maps__for_each_entry(maps, map) {
maps__for_each_entry(maps, rb_node) {
struct map *map = rb_node->map;

if (!map->priv) {
if (!header_printed) {
pr_info("WARN: Maps only in kallsyms:\n");
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/bpf_lock_contention.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ int lock_contention_read(struct lock_contention *con)
}

/* make sure it loads the kernel map */
map__load(maps__first(machine->kmaps));
map__load(maps__first(machine->kmaps)->map);

prev_key = NULL;
while (!bpf_map_get_next_key(fd, prev_key, &key)) {
Expand Down
68 changes: 44 additions & 24 deletions tools/perf/util/machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ static int machine__process_ksymbol_register(struct machine *machine,

if (!map) {
struct dso *dso = dso__new(event->ksymbol.name);
int err;

if (dso) {
dso->kernel = DSO_SPACE__KERNEL;
Expand All @@ -902,8 +903,11 @@ static int machine__process_ksymbol_register(struct machine *machine,

map->start = event->ksymbol.addr;
map->end = map->start + event->ksymbol.len;
maps__insert(machine__kernel_maps(machine), map);
err = maps__insert(machine__kernel_maps(machine), map);
map__put(map);
if (err)
return err;

dso__set_loaded(dso);

if (is_bpf_image(event->ksymbol.name)) {
Expand Down Expand Up @@ -1003,6 +1007,7 @@ static struct map *machine__addnew_module_map(struct machine *machine, u64 start
struct map *map = NULL;
struct kmod_path m;
struct dso *dso;
int err;

if (kmod_path__parse_name(&m, filename))
return NULL;
Expand All @@ -1015,10 +1020,14 @@ static struct map *machine__addnew_module_map(struct machine *machine, u64 start
if (map == NULL)
goto out;

maps__insert(machine__kernel_maps(machine), map);
err = maps__insert(machine__kernel_maps(machine), map);

/* Put the map here because maps__insert already got it */
map__put(map);

/* If maps__insert failed, return NULL. */
if (err)
map = NULL;
out:
/* put the dso here, corresponding to machine__findnew_module_dso */
dso__put(dso);
Expand Down Expand Up @@ -1185,10 +1194,11 @@ int machine__create_extra_kernel_map(struct machine *machine,
{
struct kmap *kmap;
struct map *map;
int err;

map = map__new2(xm->start, kernel);
if (!map)
return -1;
return -ENOMEM;

map->end = xm->end;
map->pgoff = xm->pgoff;
Expand All @@ -1197,14 +1207,16 @@ int machine__create_extra_kernel_map(struct machine *machine,

strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);

maps__insert(machine__kernel_maps(machine), map);
err = maps__insert(machine__kernel_maps(machine), map);

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

map__put(map);

return 0;
return err;
}

static u64 find_entry_trampoline(struct dso *dso)
Expand Down Expand Up @@ -1245,16 +1257,16 @@ int machine__map_x86_64_entry_trampolines(struct machine *machine,
struct maps *kmaps = machine__kernel_maps(machine);
int nr_cpus_avail, cpu;
bool found = false;
struct map *map;
struct map_rb_node *rb_node;
u64 pgoff;

/*
* In the vmlinux case, pgoff is a virtual address which must now be
* mapped to a vmlinux offset.
*/
maps__for_each_entry(kmaps, map) {
maps__for_each_entry(kmaps, rb_node) {
struct map *dest_map, *map = rb_node->map;
struct kmap *kmap = __map__kmap(map);
struct map *dest_map;

if (!kmap || !is_entry_trampoline(kmap->name))
continue;
Expand Down Expand Up @@ -1309,11 +1321,10 @@ __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)

machine->vmlinux_map = map__new2(0, kernel);
if (machine->vmlinux_map == NULL)
return -1;
return -ENOMEM;

machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
maps__insert(machine__kernel_maps(machine), machine->vmlinux_map);
return 0;
return maps__insert(machine__kernel_maps(machine), machine->vmlinux_map);
}

void machine__destroy_kernel_maps(struct machine *machine)
Expand Down Expand Up @@ -1635,25 +1646,26 @@ static void machine__set_kernel_mmap(struct machine *machine,
machine->vmlinux_map->end = ~0ULL;
}

static void machine__update_kernel_mmap(struct machine *machine,
static int machine__update_kernel_mmap(struct machine *machine,
u64 start, u64 end)
{
struct map *map = machine__kernel_map(machine);
int err;

map__get(map);
maps__remove(machine__kernel_maps(machine), map);

machine__set_kernel_mmap(machine, start, end);

maps__insert(machine__kernel_maps(machine), map);
err = maps__insert(machine__kernel_maps(machine), map);
map__put(map);
return err;
}

int machine__create_kernel_maps(struct machine *machine)
{
struct dso *kernel = machine__get_kernel(machine);
const char *name = NULL;
struct map *map;
u64 start = 0, end = ~0ULL;
int ret;

Expand Down Expand Up @@ -1685,17 +1697,22 @@ int machine__create_kernel_maps(struct machine *machine)
* we have a real start address now, so re-order the kmaps
* assume it's the last in the kmaps
*/
machine__update_kernel_mmap(machine, start, end);
ret = machine__update_kernel_mmap(machine, start, end);
if (ret < 0)
goto out_put;
}

if (machine__create_extra_kernel_maps(machine, kernel))
pr_debug("Problems creating extra kernel maps, continuing anyway...\n");

if (end == ~0ULL) {
/* update end address of the kernel map using adjacent module address */
map = map__next(machine__kernel_map(machine));
if (map)
machine__set_kernel_mmap(machine, start, map->start);
struct map_rb_node *rb_node = maps__find_node(machine__kernel_maps(machine),
machine__kernel_map(machine));
struct map_rb_node *next = map_rb_node__next(rb_node);

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

out_put:
Expand Down Expand Up @@ -1828,7 +1845,10 @@ static int machine__process_kernel_mmap_event(struct machine *machine,
if (strstr(kernel->long_name, "vmlinux"))
dso__set_short_name(kernel, "[kernel.vmlinux]", false);

machine__update_kernel_mmap(machine, xm->start, xm->end);
if (machine__update_kernel_mmap(machine, xm->start, xm->end) < 0) {
dso__put(kernel);
goto out_problem;
}

if (build_id__is_defined(bid))
dso__set_build_id(kernel, bid);
Expand Down Expand Up @@ -3330,11 +3350,11 @@ int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv
int machine__for_each_kernel_map(struct machine *machine, machine__map_t fn, void *priv)
{
struct maps *maps = machine__kernel_maps(machine);
struct map *map;
struct map_rb_node *pos;
int err = 0;

for (map = maps__first(maps); map != NULL; map = map__next(map)) {
err = fn(map, priv);
maps__for_each_entry(maps, pos) {
err = fn(pos->map, priv);
if (err != 0) {
break;
}
Expand Down
16 changes: 0 additions & 16 deletions tools/perf/util/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
map->dso = dso__get(dso);
map->map_ip = map__map_ip;
map->unmap_ip = map__unmap_ip;
RB_CLEAR_NODE(&map->rb_node);
map->erange_warned = false;
refcount_set(&map->refcnt, 1);
}
Expand Down Expand Up @@ -397,7 +396,6 @@ struct map *map__clone(struct map *from)
map = memdup(from, size);
if (map != NULL) {
refcount_set(&map->refcnt, 1);
RB_CLEAR_NODE(&map->rb_node);
dso__get(map->dso);
}

Expand Down Expand Up @@ -537,20 +535,6 @@ bool map__contains_symbol(const struct map *map, const struct symbol *sym)
return ip >= map->start && ip < map->end;
}

static struct map *__map__next(struct map *map)
{
struct rb_node *next = rb_next(&map->rb_node);

if (next)
return rb_entry(next, struct map, rb_node);
return NULL;
}

struct map *map__next(struct map *map)
{
return map ? __map__next(map) : NULL;
}

struct kmap *__map__kmap(struct map *map)
{
if (!map->dso || !map->dso->kernel)
Expand Down
1 change: 0 additions & 1 deletion tools/perf/util/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ struct maps;
struct machine;

struct map {
struct rb_node rb_node;
u64 start;
u64 end;
bool erange_warned:1;
Expand Down
Loading

0 comments on commit ff583dc

Please sign in to comment.