Skip to content

Commit 83fa590

Browse files
Yang JihongSasha Levin
Yang Jihong
authored and
Sasha Levin
committed
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 2b03bc5 commit 83fa590

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

tools/perf/builtin-sched.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3252,8 +3252,6 @@ static int perf_sched__lat(struct perf_sched *sched)
32523252

32533253
static int setup_map_cpus(struct perf_sched *sched)
32543254
{
3255-
struct perf_cpu_map *map;
3256-
32573255
sched->max_cpu.cpu = sysconf(_SC_NPROCESSORS_CONF);
32583256

32593257
if (sched->map.comp) {
@@ -3262,16 +3260,15 @@ static int setup_map_cpus(struct perf_sched *sched)
32623260
return -1;
32633261
}
32643262

3265-
if (!sched->map.cpus_str)
3266-
return 0;
3267-
3268-
map = perf_cpu_map__new(sched->map.cpus_str);
3269-
if (!map) {
3270-
pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3271-
return -1;
3263+
if (sched->map.cpus_str) {
3264+
sched->map.cpus = perf_cpu_map__new(sched->map.cpus_str);
3265+
if (!sched->map.cpus) {
3266+
pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3267+
zfree(&sched->map.comp_cpus);
3268+
return -1;
3269+
}
32723270
}
32733271

3274-
sched->map.cpus = map;
32753272
return 0;
32763273
}
32773274

@@ -3311,20 +3308,34 @@ static int setup_color_cpus(struct perf_sched *sched)
33113308

33123309
static int perf_sched__map(struct perf_sched *sched)
33133310
{
3311+
int rc = -1;
3312+
33143313
if (setup_map_cpus(sched))
3315-
return -1;
3314+
return rc;
33163315

33173316
if (setup_color_pids(sched))
3318-
return -1;
3317+
goto out_put_map_cpus;
33193318

33203319
if (setup_color_cpus(sched))
3321-
return -1;
3320+
goto out_put_color_pids;
33223321

33233322
setup_pager();
33243323
if (perf_sched__read_events(sched))
3325-
return -1;
3324+
goto out_put_color_cpus;
3325+
3326+
rc = 0;
33263327
print_bad_events(sched);
3327-
return 0;
3328+
3329+
out_put_color_cpus:
3330+
perf_cpu_map__put(sched->map.color_cpus);
3331+
3332+
out_put_color_pids:
3333+
perf_thread_map__put(sched->map.color_pids);
3334+
3335+
out_put_map_cpus:
3336+
zfree(&sched->map.comp_cpus);
3337+
perf_cpu_map__put(sched->map.cpus);
3338+
return rc;
33283339
}
33293340

33303341
static int perf_sched__replay(struct perf_sched *sched)

0 commit comments

Comments
 (0)