Skip to content

Commit 9c68ae9

Browse files
kmjohansenacmel
authored andcommitted
perf callchain: Reference count maps
If dso__load_kcore frees all of the existing maps, but one has already been attached to a callchain cursor node, then we can get a SIGSEGV in any function that happens to try to use this invalid cursor. Use the existing map refcount mechanism to forestall cleanup of a map until the cursor iterates past the node. Signed-off-by: Krister Johansen <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: [email protected] Fixes: 84c2caf ("perf tools: Reference count struct map") Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent c0621ac commit 9c68ae9

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

tools/perf/util/callchain.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
449449
}
450450
call->ip = cursor_node->ip;
451451
call->ms.sym = cursor_node->sym;
452-
call->ms.map = cursor_node->map;
452+
call->ms.map = map__get(cursor_node->map);
453453

454454
if (cursor_node->branch) {
455455
call->branch_count = 1;
@@ -489,6 +489,7 @@ add_child(struct callchain_node *parent,
489489

490490
list_for_each_entry_safe(call, tmp, &new->val, list) {
491491
list_del(&call->list);
492+
map__zput(call->ms.map);
492493
free(call);
493494
}
494495
free(new);
@@ -773,6 +774,7 @@ merge_chain_branch(struct callchain_cursor *cursor,
773774
list->ms.map, list->ms.sym,
774775
false, NULL, 0, 0);
775776
list_del(&list->list);
777+
map__zput(list->ms.map);
776778
free(list);
777779
}
778780

@@ -823,7 +825,8 @@ int callchain_cursor_append(struct callchain_cursor *cursor,
823825
}
824826

825827
node->ip = ip;
826-
node->map = map;
828+
map__zput(node->map);
829+
node->map = map__get(map);
827830
node->sym = sym;
828831
node->branch = branch;
829832
node->nr_loop_iter = nr_loop_iter;
@@ -1154,11 +1157,13 @@ static void free_callchain_node(struct callchain_node *node)
11541157

11551158
list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
11561159
list_del(&list->list);
1160+
map__zput(list->ms.map);
11571161
free(list);
11581162
}
11591163

11601164
list_for_each_entry_safe(list, tmp, &node->val, list) {
11611165
list_del(&list->list);
1166+
map__zput(list->ms.map);
11621167
free(list);
11631168
}
11641169

@@ -1222,6 +1227,7 @@ int callchain_node__make_parent_list(struct callchain_node *node)
12221227
goto out;
12231228
*new = *chain;
12241229
new->has_children = false;
1230+
map__get(new->ms.map);
12251231
list_add_tail(&new->list, &head);
12261232
}
12271233
parent = parent->parent;
@@ -1242,6 +1248,7 @@ int callchain_node__make_parent_list(struct callchain_node *node)
12421248
out:
12431249
list_for_each_entry_safe(chain, new, &head, list) {
12441250
list_del(&chain->list);
1251+
map__zput(chain->ms.map);
12451252
free(chain);
12461253
}
12471254
return -ENOMEM;

tools/perf/util/callchain.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/list.h>
66
#include <linux/rbtree.h>
77
#include "event.h"
8+
#include "map.h"
89
#include "symbol.h"
910

1011
#define HELP_PAD "\t\t\t\t"
@@ -184,8 +185,13 @@ int callchain_merge(struct callchain_cursor *cursor,
184185
*/
185186
static inline void callchain_cursor_reset(struct callchain_cursor *cursor)
186187
{
188+
struct callchain_cursor_node *node;
189+
187190
cursor->nr = 0;
188191
cursor->last = &cursor->first;
192+
193+
for (node = cursor->first; node != NULL; node = node->next)
194+
map__zput(node->map);
189195
}
190196

191197
int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,

tools/perf/util/hist.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "util.h"
22
#include "build-id.h"
33
#include "hist.h"
4+
#include "map.h"
45
#include "session.h"
56
#include "sort.h"
67
#include "evlist.h"
@@ -1019,6 +1020,10 @@ int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
10191020
int max_stack_depth, void *arg)
10201021
{
10211022
int err, err2;
1023+
struct map *alm = NULL;
1024+
1025+
if (al && al->map)
1026+
alm = map__get(al->map);
10221027

10231028
err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
10241029
iter->evsel, al, max_stack_depth);
@@ -1058,6 +1063,8 @@ int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
10581063
if (!err)
10591064
err = err2;
10601065

1066+
map__put(alm);
1067+
10611068
return err;
10621069
}
10631070

0 commit comments

Comments
 (0)