Skip to content

Commit 9472599

Browse files
captain5050acmel
authored andcommitted
libperf evsel: Open shouldn't leak fd on failure
If perf_event_open() fails the fd is opened but it is only freed by closing (not by delete). Typically when an open fails you don't call close and so this results in a memory leak. To avoid this, add a close when open fails. Signed-off-by: Ian Rogers <irogers@google.com> Reviewed-By: Kajol Jain <kjain@linux.ibm.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rob Herring <robh@kernel.org> Cc: Stephane Eranian <eranian@google.com> Link: https://lore.kernel.org/r/20220609052355.1300162-2-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent ec90610 commit 9472599

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

tools/lib/perf/evsel.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,30 @@ int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
149149
int fd, group_fd, *evsel_fd;
150150

151151
evsel_fd = FD(evsel, idx, thread);
152-
if (evsel_fd == NULL)
153-
return -EINVAL;
152+
if (evsel_fd == NULL) {
153+
err = -EINVAL;
154+
goto out;
155+
}
154156

155157
err = get_group_fd(evsel, idx, thread, &group_fd);
156158
if (err < 0)
157-
return err;
159+
goto out;
158160

159161
fd = sys_perf_event_open(&evsel->attr,
160162
threads->map[thread].pid,
161163
cpu, group_fd, 0);
162164

163-
if (fd < 0)
164-
return -errno;
165+
if (fd < 0) {
166+
err = -errno;
167+
goto out;
168+
}
165169

166170
*evsel_fd = fd;
167171
}
168172
}
173+
out:
174+
if (err)
175+
perf_evsel__close(evsel);
169176

170177
return err;
171178
}

0 commit comments

Comments
 (0)