Skip to content

Commit ef76a5a

Browse files
Yang Jihongnamhyung
Yang Jihong
authored andcommitted
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: Yang Jihong <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent c690786 commit ef76a5a

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
@@ -3208,8 +3208,6 @@ static int perf_sched__lat(struct perf_sched *sched)
32083208

32093209
static int setup_map_cpus(struct perf_sched *sched)
32103210
{
3211-
struct perf_cpu_map *map;
3212-
32133211
sched->max_cpu.cpu = sysconf(_SC_NPROCESSORS_CONF);
32143212

32153213
if (sched->map.comp) {
@@ -3218,16 +3216,15 @@ static int setup_map_cpus(struct perf_sched *sched)
32183216
return -1;
32193217
}
32203218

3221-
if (!sched->map.cpus_str)
3222-
return 0;
3223-
3224-
map = perf_cpu_map__new(sched->map.cpus_str);
3225-
if (!map) {
3226-
pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3227-
return -1;
3219+
if (sched->map.cpus_str) {
3220+
sched->map.cpus = perf_cpu_map__new(sched->map.cpus_str);
3221+
if (!sched->map.cpus) {
3222+
pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3223+
zfree(&sched->map.comp_cpus);
3224+
return -1;
3225+
}
32283226
}
32293227

3230-
sched->map.cpus = map;
32313228
return 0;
32323229
}
32333230

@@ -3267,20 +3264,34 @@ static int setup_color_cpus(struct perf_sched *sched)
32673264

32683265
static int perf_sched__map(struct perf_sched *sched)
32693266
{
3267+
int rc = -1;
3268+
32703269
if (setup_map_cpus(sched))
3271-
return -1;
3270+
return rc;
32723271

32733272
if (setup_color_pids(sched))
3274-
return -1;
3273+
goto out_put_map_cpus;
32753274

32763275
if (setup_color_cpus(sched))
3277-
return -1;
3276+
goto out_put_color_pids;
32783277

32793278
setup_pager();
32803279
if (perf_sched__read_events(sched))
3281-
return -1;
3280+
goto out_put_color_cpus;
3281+
3282+
rc = 0;
32823283
print_bad_events(sched);
3283-
return 0;
3284+
3285+
out_put_color_cpus:
3286+
perf_cpu_map__put(sched->map.color_cpus);
3287+
3288+
out_put_color_pids:
3289+
perf_thread_map__put(sched->map.color_pids);
3290+
3291+
out_put_map_cpus:
3292+
zfree(&sched->map.comp_cpus);
3293+
perf_cpu_map__put(sched->map.cpus);
3294+
return rc;
32843295
}
32853296

32863297
static int perf_sched__replay(struct perf_sched *sched)

0 commit comments

Comments
 (0)