Commit b194c9cd authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf evsel: Fix memory leaks relating to unit

unit may have a strdup pointer or be to a literal, consequently memory
assocciated with it isn't freed. Change it so the unit is always strdup
and so the memory can be safely freed.

Fix related issue in perf_event__process_event_update() for name and
own_cpus. Leaks were spotted by leak sanitizer.
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/20211118084749.2191447-1-irogers@google.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent d9fc7061
...@@ -88,7 +88,6 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes ...@@ -88,7 +88,6 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
struct evsel *evsel; struct evsel *evsel;
struct event_name tmp; struct event_name tmp;
struct evlist *evlist = evlist__new_default(); struct evlist *evlist = evlist__new_default();
char *unit = strdup("KRAVA");
TEST_ASSERT_VAL("failed to get evlist", evlist); TEST_ASSERT_VAL("failed to get evlist", evlist);
...@@ -99,7 +98,8 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes ...@@ -99,7 +98,8 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
perf_evlist__id_add(&evlist->core, &evsel->core, 0, 0, 123); perf_evlist__id_add(&evlist->core, &evsel->core, 0, 0, 123);
evsel->unit = unit; free((char *)evsel->unit);
evsel->unit = strdup("KRAVA");
TEST_ASSERT_VAL("failed to synthesize attr update unit", TEST_ASSERT_VAL("failed to synthesize attr update unit",
!perf_event__synthesize_event_update_unit(NULL, evsel, process_event_unit)); !perf_event__synthesize_event_update_unit(NULL, evsel, process_event_unit));
...@@ -119,7 +119,6 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes ...@@ -119,7 +119,6 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
TEST_ASSERT_VAL("failed to synthesize attr update cpus", TEST_ASSERT_VAL("failed to synthesize attr update cpus",
!perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus)); !perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus));
free(unit);
evlist__delete(evlist); evlist__delete(evlist);
return 0; return 0;
} }
......
...@@ -241,7 +241,7 @@ void evsel__init(struct evsel *evsel, ...@@ -241,7 +241,7 @@ void evsel__init(struct evsel *evsel,
{ {
perf_evsel__init(&evsel->core, attr, idx); perf_evsel__init(&evsel->core, attr, idx);
evsel->tracking = !idx; evsel->tracking = !idx;
evsel->unit = ""; evsel->unit = strdup("");
evsel->scale = 1.0; evsel->scale = 1.0;
evsel->max_events = ULONG_MAX; evsel->max_events = ULONG_MAX;
evsel->evlist = NULL; evsel->evlist = NULL;
...@@ -276,13 +276,8 @@ struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx) ...@@ -276,13 +276,8 @@ struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx)
} }
if (evsel__is_clock(evsel)) { if (evsel__is_clock(evsel)) {
/* free((char *)evsel->unit);
* The evsel->unit points to static alias->unit evsel->unit = strdup("msec");
* so it's ok to use static string in here.
*/
static const char *unit = "msec";
evsel->unit = unit;
evsel->scale = 1e-6; evsel->scale = 1e-6;
} }
...@@ -420,7 +415,11 @@ struct evsel *evsel__clone(struct evsel *orig) ...@@ -420,7 +415,11 @@ struct evsel *evsel__clone(struct evsel *orig)
evsel->max_events = orig->max_events; evsel->max_events = orig->max_events;
evsel->tool_event = orig->tool_event; evsel->tool_event = orig->tool_event;
evsel->unit = orig->unit; free((char *)evsel->unit);
evsel->unit = strdup(orig->unit);
if (evsel->unit == NULL)
goto out_err;
evsel->scale = orig->scale; evsel->scale = orig->scale;
evsel->snapshot = orig->snapshot; evsel->snapshot = orig->snapshot;
evsel->per_pkg = orig->per_pkg; evsel->per_pkg = orig->per_pkg;
...@@ -1441,6 +1440,7 @@ void evsel__exit(struct evsel *evsel) ...@@ -1441,6 +1440,7 @@ void evsel__exit(struct evsel *evsel)
zfree(&evsel->group_name); zfree(&evsel->group_name);
zfree(&evsel->name); zfree(&evsel->name);
zfree(&evsel->pmu_name); zfree(&evsel->pmu_name);
zfree(&evsel->unit);
zfree(&evsel->metric_id); zfree(&evsel->metric_id);
evsel__zero_per_pkg(evsel); evsel__zero_per_pkg(evsel);
hashmap__free(evsel->per_pkg_mask); hashmap__free(evsel->per_pkg_mask);
......
...@@ -4257,9 +4257,11 @@ int perf_event__process_event_update(struct perf_tool *tool __maybe_unused, ...@@ -4257,9 +4257,11 @@ int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
switch (ev->type) { switch (ev->type) {
case PERF_EVENT_UPDATE__UNIT: case PERF_EVENT_UPDATE__UNIT:
free((char *)evsel->unit);
evsel->unit = strdup(ev->data); evsel->unit = strdup(ev->data);
break; break;
case PERF_EVENT_UPDATE__NAME: case PERF_EVENT_UPDATE__NAME:
free(evsel->name);
evsel->name = strdup(ev->data); evsel->name = strdup(ev->data);
break; break;
case PERF_EVENT_UPDATE__SCALE: case PERF_EVENT_UPDATE__SCALE:
...@@ -4268,11 +4270,11 @@ int perf_event__process_event_update(struct perf_tool *tool __maybe_unused, ...@@ -4268,11 +4270,11 @@ int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
break; break;
case PERF_EVENT_UPDATE__CPUS: case PERF_EVENT_UPDATE__CPUS:
ev_cpus = (struct perf_record_event_update_cpus *)ev->data; ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
map = cpu_map__new_data(&ev_cpus->cpus); map = cpu_map__new_data(&ev_cpus->cpus);
if (map) if (map) {
perf_cpu_map__put(evsel->core.own_cpus);
evsel->core.own_cpus = map; evsel->core.own_cpus = map;
else } else
pr_err("failed to get event_update cpus\n"); pr_err("failed to get event_update cpus\n");
default: default:
break; break;
......
...@@ -402,8 +402,10 @@ static int add_event_tool(struct list_head *list, int *idx, ...@@ -402,8 +402,10 @@ static int add_event_tool(struct list_head *list, int *idx,
if (!evsel) if (!evsel)
return -ENOMEM; return -ENOMEM;
evsel->tool_event = tool_event; evsel->tool_event = tool_event;
if (tool_event == PERF_TOOL_DURATION_TIME) if (tool_event == PERF_TOOL_DURATION_TIME) {
evsel->unit = "ns"; free((char *)evsel->unit);
evsel->unit = strdup("ns");
}
return 0; return 0;
} }
...@@ -1630,7 +1632,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state, ...@@ -1630,7 +1632,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
if (parse_state->fake_pmu) if (parse_state->fake_pmu)
return 0; return 0;
evsel->unit = info.unit; free((char *)evsel->unit);
evsel->unit = strdup(info.unit);
evsel->scale = info.scale; evsel->scale = info.scale;
evsel->per_pkg = info.per_pkg; evsel->per_pkg = info.per_pkg;
evsel->snapshot = info.snapshot; evsel->snapshot = info.snapshot;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment