Skip to content

Commit f231623

Browse files
Yang Jihonggregkh
Yang Jihong
authored andcommitted
perf sched: Fix memory leak in perf_sched__map()
[ Upstream commit ef76a5a ] 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: Yang Jihong <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Link: https://lore.kernel.org/r/[email protected] Stable-dep-of: 1a5efc9 ("libsubcmd: Don't free the usage string") Signed-off-by: Sasha Levin <[email protected]>
1 parent ee9a32c commit f231623

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

tools/perf/builtin-sched.c

+26-15
Original file line numberDiff line numberDiff line change
@@ -3240,8 +3240,6 @@ static int perf_sched__lat(struct perf_sched *sched)
32403240

32413241
static int setup_map_cpus(struct perf_sched *sched)
32423242
{
3243-
struct perf_cpu_map *map;
3244-
32453243
sched->max_cpu.cpu = sysconf(_SC_NPROCESSORS_CONF);
32463244

32473245
if (sched->map.comp) {
@@ -3250,16 +3248,15 @@ static int setup_map_cpus(struct perf_sched *sched)
32503248
return -1;
32513249
}
32523250

3253-
if (!sched->map.cpus_str)
3254-
return 0;
3255-
3256-
map = perf_cpu_map__new(sched->map.cpus_str);
3257-
if (!map) {
3258-
pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3259-
return -1;
3251+
if (sched->map.cpus_str) {
3252+
sched->map.cpus = perf_cpu_map__new(sched->map.cpus_str);
3253+
if (!sched->map.cpus) {
3254+
pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3255+
zfree(&sched->map.comp_cpus);
3256+
return -1;
3257+
}
32603258
}
32613259

3262-
sched->map.cpus = map;
32633260
return 0;
32643261
}
32653262

@@ -3299,20 +3296,34 @@ static int setup_color_cpus(struct perf_sched *sched)
32993296

33003297
static int perf_sched__map(struct perf_sched *sched)
33013298
{
3299+
int rc = -1;
3300+
33023301
if (setup_map_cpus(sched))
3303-
return -1;
3302+
return rc;
33043303

33053304
if (setup_color_pids(sched))
3306-
return -1;
3305+
goto out_put_map_cpus;
33073306

33083307
if (setup_color_cpus(sched))
3309-
return -1;
3308+
goto out_put_color_pids;
33103309

33113310
setup_pager();
33123311
if (perf_sched__read_events(sched))
3313-
return -1;
3312+
goto out_put_color_cpus;
3313+
3314+
rc = 0;
33143315
print_bad_events(sched);
3315-
return 0;
3316+
3317+
out_put_color_cpus:
3318+
perf_cpu_map__put(sched->map.color_cpus);
3319+
3320+
out_put_color_pids:
3321+
perf_thread_map__put(sched->map.color_pids);
3322+
3323+
out_put_map_cpus:
3324+
zfree(&sched->map.comp_cpus);
3325+
perf_cpu_map__put(sched->map.cpus);
3326+
return rc;
33163327
}
33173328

33183329
static int perf_sched__replay(struct perf_sched *sched)

0 commit comments

Comments
 (0)