tools/perf/util/drm_pmu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
From: GuoHan Zhao <zhaoguohan@kylinos.cn>
Fix file descriptor leak when callback function returns error. The
function was directly returning without closing fdinfo_dir_fd and
fd_dir when cb() returned non-zero value.
Fixes: 28917cb17f9d ("perf drm_pmu: Add a tool like PMU to expose DRM information")
Signed-off-by: GuoHan Zhao <zhaoguohan@kylinos.cn>
---
V1 -> V2: Added the Fixes tag in commit message
---
tools/perf/util/drm_pmu.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/drm_pmu.c b/tools/perf/util/drm_pmu.c
index 988890f37ba7..424a76d1a953 100644
--- a/tools/perf/util/drm_pmu.c
+++ b/tools/perf/util/drm_pmu.c
@@ -458,8 +458,10 @@ static int for_each_drm_fdinfo_in_dir(int (*cb)(void *args, int fdinfo_dir_fd, c
}
ret = cb(args, fdinfo_dir_fd, fd_entry->d_name);
if (ret)
- return ret;
+ goto cleanup;
}
+
+cleanup:
if (fdinfo_dir_fd != -1)
close(fdinfo_dir_fd);
closedir(fd_dir);
--
2.43.0
… > +++ b/tools/perf/util/drm_pmu.c > @@ -458,8 +458,10 @@ static int for_each_drm_fdinfo_in_dir(int (*cb)(void *args, int fdinfo_dir_fd, c > } > ret = cb(args, fdinfo_dir_fd, fd_entry->d_name); > if (ret) > - return ret; > + goto cleanup; > } > + > +cleanup: > if (fdinfo_dir_fd != -1) Would it be nicer to use a label like “close_fdinfo” instead at this source code place (so that a repeated check could be avoided)? > close(fdinfo_dir_fd); > closedir(fd_dir); Regards, Markus
On Tue, Aug 12, 2025 at 8:35 PM <zhaoguohan@kylinos.cn> wrote: > > From: GuoHan Zhao <zhaoguohan@kylinos.cn> > > Fix file descriptor leak when callback function returns error. The > function was directly returning without closing fdinfo_dir_fd and > fd_dir when cb() returned non-zero value. > > Fixes: 28917cb17f9d ("perf drm_pmu: Add a tool like PMU to expose DRM information") > Signed-off-by: GuoHan Zhao <zhaoguohan@kylinos.cn> Reviewed-by: Ian Rogers <irogers@google.com> Thanks, Ian > --- > V1 -> V2: Added the Fixes tag in commit message > --- > tools/perf/util/drm_pmu.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/tools/perf/util/drm_pmu.c b/tools/perf/util/drm_pmu.c > index 988890f37ba7..424a76d1a953 100644 > --- a/tools/perf/util/drm_pmu.c > +++ b/tools/perf/util/drm_pmu.c > @@ -458,8 +458,10 @@ static int for_each_drm_fdinfo_in_dir(int (*cb)(void *args, int fdinfo_dir_fd, c > } > ret = cb(args, fdinfo_dir_fd, fd_entry->d_name); > if (ret) > - return ret; > + goto cleanup; > } > + > +cleanup: > if (fdinfo_dir_fd != -1) > close(fdinfo_dir_fd); > closedir(fd_dir); > -- > 2.43.0 >
© 2016 - 2025 Red Hat, Inc.