Skip to content

Commit d3abd7b

Browse files
captain5050acmel
authored andcommitted
perf metrics: Copy entire pmu_event in find metric
The pmu_event passed to the pmu_events_table_for_each_event is invalid after the loop. Copy the entire struct in metricgroup__find_metric. Reduce the scope of this function to static. Signed-off-by: Ian Rogers <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: John Garry <[email protected]> Cc: Kan Liang <[email protected]> Cc: Leo Yan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Mike Leach <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Ravi Bangoria <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Will Deacon <[email protected]> Cc: Xing Zhengjun <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 1ba3752 commit d3abd7b

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

tools/perf/util/metricgroup.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,10 @@ struct metricgroup_add_iter_data {
879879
const struct pmu_events_table *table;
880880
};
881881

882+
static bool metricgroup__find_metric(const char *metric,
883+
const struct pmu_events_table *table,
884+
struct pmu_event *pe);
885+
882886
static int add_metric(struct list_head *metric_list,
883887
const struct pmu_event *pe,
884888
const char *modifier,
@@ -914,7 +918,7 @@ static int resolve_metric(struct list_head *metric_list,
914918
size_t bkt;
915919
struct to_resolve {
916920
/* The metric to resolve. */
917-
const struct pmu_event *pe;
921+
struct pmu_event pe;
918922
/*
919923
* The key in the IDs map, this may differ from in case,
920924
* etc. from pe->metric_name.
@@ -928,16 +932,15 @@ static int resolve_metric(struct list_head *metric_list,
928932
* the pending array.
929933
*/
930934
hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) {
931-
const struct pmu_event *pe;
935+
struct pmu_event pe;
932936

933-
pe = metricgroup__find_metric(cur->key, table);
934-
if (pe) {
937+
if (metricgroup__find_metric(cur->key, table, &pe)) {
935938
pending = realloc(pending,
936939
(pending_cnt + 1) * sizeof(struct to_resolve));
937940
if (!pending)
938941
return -ENOMEM;
939942

940-
pending[pending_cnt].pe = pe;
943+
memcpy(&pending[pending_cnt].pe, &pe, sizeof(pe));
941944
pending[pending_cnt].key = cur->key;
942945
pending_cnt++;
943946
}
@@ -952,7 +955,7 @@ static int resolve_metric(struct list_head *metric_list,
952955
* context.
953956
*/
954957
for (i = 0; i < pending_cnt; i++) {
955-
ret = add_metric(metric_list, pending[i].pe, modifier, metric_no_group,
958+
ret = add_metric(metric_list, &pending[i].pe, modifier, metric_no_group,
956959
root_metric, visited, table);
957960
if (ret)
958961
break;
@@ -1073,7 +1076,7 @@ static int __add_metric(struct list_head *metric_list,
10731076

10741077
struct metricgroup__find_metric_data {
10751078
const char *metric;
1076-
const struct pmu_event *pe;
1079+
struct pmu_event *pe;
10771080
};
10781081

10791082
static int metricgroup__find_metric_callback(const struct pmu_event *pe,
@@ -1085,21 +1088,21 @@ static int metricgroup__find_metric_callback(const struct pmu_event *pe,
10851088
if (!match_metric(pe->metric_name, data->metric))
10861089
return 0;
10871090

1088-
data->pe = pe;
1089-
return -1;
1091+
memcpy(data->pe, pe, sizeof(*pe));
1092+
return 1;
10901093
}
10911094

1092-
const struct pmu_event *metricgroup__find_metric(const char *metric,
1093-
const struct pmu_events_table *table)
1095+
static bool metricgroup__find_metric(const char *metric,
1096+
const struct pmu_events_table *table,
1097+
struct pmu_event *pe)
10941098
{
10951099
struct metricgroup__find_metric_data data = {
10961100
.metric = metric,
1097-
.pe = NULL,
1101+
.pe = pe,
10981102
};
10991103

1100-
pmu_events_table_for_each_event(table, metricgroup__find_metric_callback, &data);
1101-
1102-
return data.pe;
1104+
return pmu_events_table_for_each_event(table, metricgroup__find_metric_callback, &data)
1105+
? true : false;
11031106
}
11041107

11051108
static int add_metric(struct list_head *metric_list,

tools/perf/util/metricgroup.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ int metricgroup__parse_groups(const struct option *opt,
6969
bool metric_no_group,
7070
bool metric_no_merge,
7171
struct rblist *metric_events);
72-
const struct pmu_event *metricgroup__find_metric(const char *metric,
73-
const struct pmu_events_table *table);
7472
int metricgroup__parse_groups_test(struct evlist *evlist,
7573
const struct pmu_events_table *table,
7674
const char *str,

0 commit comments

Comments
 (0)