Commit ef76a5af authored by Yang Jihong's avatar Yang Jihong Committed by Namhyung Kim

perf sched: Fix memory leak in perf_sched__map()

perf_sched__map() needs to free memory of map_cpus, color_pids and
color_cpus in normal path and rollback allocated memory in error path.
Signed-off-by: default avatarYang Jihong <yangjihong1@huawei.com>
Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240206083228.172607-3-yangjihong1@huawei.com
parent c6907863
...@@ -3208,8 +3208,6 @@ static int perf_sched__lat(struct perf_sched *sched) ...@@ -3208,8 +3208,6 @@ static int perf_sched__lat(struct perf_sched *sched)
static int setup_map_cpus(struct perf_sched *sched) static int setup_map_cpus(struct perf_sched *sched)
{ {
struct perf_cpu_map *map;
sched->max_cpu.cpu = sysconf(_SC_NPROCESSORS_CONF); sched->max_cpu.cpu = sysconf(_SC_NPROCESSORS_CONF);
if (sched->map.comp) { if (sched->map.comp) {
...@@ -3218,16 +3216,15 @@ static int setup_map_cpus(struct perf_sched *sched) ...@@ -3218,16 +3216,15 @@ static int setup_map_cpus(struct perf_sched *sched)
return -1; return -1;
} }
if (!sched->map.cpus_str) if (sched->map.cpus_str) {
return 0; sched->map.cpus = perf_cpu_map__new(sched->map.cpus_str);
if (!sched->map.cpus) {
map = perf_cpu_map__new(sched->map.cpus_str); pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
if (!map) { zfree(&sched->map.comp_cpus);
pr_err("failed to get cpus map from %s\n", sched->map.cpus_str); return -1;
return -1; }
} }
sched->map.cpus = map;
return 0; return 0;
} }
...@@ -3267,20 +3264,34 @@ static int setup_color_cpus(struct perf_sched *sched) ...@@ -3267,20 +3264,34 @@ static int setup_color_cpus(struct perf_sched *sched)
static int perf_sched__map(struct perf_sched *sched) static int perf_sched__map(struct perf_sched *sched)
{ {
int rc = -1;
if (setup_map_cpus(sched)) if (setup_map_cpus(sched))
return -1; return rc;
if (setup_color_pids(sched)) if (setup_color_pids(sched))
return -1; goto out_put_map_cpus;
if (setup_color_cpus(sched)) if (setup_color_cpus(sched))
return -1; goto out_put_color_pids;
setup_pager(); setup_pager();
if (perf_sched__read_events(sched)) if (perf_sched__read_events(sched))
return -1; goto out_put_color_cpus;
rc = 0;
print_bad_events(sched); print_bad_events(sched);
return 0;
out_put_color_cpus:
perf_cpu_map__put(sched->map.color_cpus);
out_put_color_pids:
perf_thread_map__put(sched->map.color_pids);
out_put_map_cpus:
zfree(&sched->map.comp_cpus);
perf_cpu_map__put(sched->map.cpus);
return rc;
} }
static int perf_sched__replay(struct perf_sched *sched) static int perf_sched__replay(struct perf_sched *sched)
......
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