linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf drm_pmu: Prevent resource leak in for_each_drm_fdinfo_in_dir()
@ 2025-08-14  6:06 Sergio Perez Gonzalez
  2025-08-14 20:31 ` Namhyung Kim
  0 siblings, 1 reply; 3+ messages in thread
From: Sergio Perez Gonzalez @ 2025-08-14  6:06 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, kan.liang, colin.i.king
  Cc: Sergio Perez Gonzalez, linux-perf-users, linux-kernel

Close fdinfo_dir_fd and fd_dir prior to exit, in the event of
cb() error.

Signed-off-by: Sergio Perez Gonzalez <sperezglz@gmail.com>
---
 tools/perf/util/drm_pmu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/drm_pmu.c b/tools/perf/util/drm_pmu.c
index 988890f37ba7..416aeac7956e 100644
--- a/tools/perf/util/drm_pmu.c
+++ b/tools/perf/util/drm_pmu.c
@@ -403,7 +403,7 @@ static int for_each_drm_fdinfo_in_dir(int (*cb)(void *args, int fdinfo_dir_fd, c
 	DIR *fd_dir;
 	struct dirent *fd_entry;
 	int fd_dir_fd, fdinfo_dir_fd = -1;
-
+	int ret = 0;
 
 	scnprintf(buf, sizeof(buf), "%s/fd", pid_name);
 	fd_dir_fd = openat(proc_dir, buf, O_DIRECTORY);
@@ -418,7 +418,6 @@ static int for_each_drm_fdinfo_in_dir(int (*cb)(void *args, int fdinfo_dir_fd, c
 		struct stat stat;
 		unsigned int minor;
 		bool is_dup = false;
-		int ret;
 
 		if (fd_entry->d_type != DT_LNK)
 			continue;
@@ -458,12 +457,13 @@ 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 out;
 	}
+out:
 	if (fdinfo_dir_fd != -1)
 		close(fdinfo_dir_fd);
 	closedir(fd_dir);
-	return 0;
+	return ret;
 }
 
 static int for_each_drm_fdinfo(bool skip_all_duplicates,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf drm_pmu: Prevent resource leak in for_each_drm_fdinfo_in_dir()
  2025-08-14  6:06 [PATCH] perf drm_pmu: Prevent resource leak in for_each_drm_fdinfo_in_dir() Sergio Perez Gonzalez
@ 2025-08-14 20:31 ` Namhyung Kim
  2025-08-14 20:42   ` Namhyung Kim
  0 siblings, 1 reply; 3+ messages in thread
From: Namhyung Kim @ 2025-08-14 20:31 UTC (permalink / raw)
  To: Sergio Perez Gonzalez
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	irogers, adrian.hunter, kan.liang, colin.i.king, linux-perf-users,
	linux-kernel

Hello,

On Thu, Aug 14, 2025 at 12:06:11AM -0600, Sergio Perez Gonzalez wrote:
> Close fdinfo_dir_fd and fd_dir prior to exit, in the event of
> cb() error.
> 
> Signed-off-by: Sergio Perez Gonzalez <sperezglz@gmail.com>

Acked-by: Namhyung Kim <namhyung@kernel.org>

Only a nitpick below.

> ---
>  tools/perf/util/drm_pmu.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/perf/util/drm_pmu.c b/tools/perf/util/drm_pmu.c
> index 988890f37ba7..416aeac7956e 100644
> --- a/tools/perf/util/drm_pmu.c
> +++ b/tools/perf/util/drm_pmu.c
> @@ -403,7 +403,7 @@ static int for_each_drm_fdinfo_in_dir(int (*cb)(void *args, int fdinfo_dir_fd, c
>  	DIR *fd_dir;
>  	struct dirent *fd_entry;
>  	int fd_dir_fd, fdinfo_dir_fd = -1;
> -
> +	int ret = 0;
>  
>  	scnprintf(buf, sizeof(buf), "%s/fd", pid_name);
>  	fd_dir_fd = openat(proc_dir, buf, O_DIRECTORY);
> @@ -418,7 +418,6 @@ static int for_each_drm_fdinfo_in_dir(int (*cb)(void *args, int fdinfo_dir_fd, c
>  		struct stat stat;
>  		unsigned int minor;
>  		bool is_dup = false;
> -		int ret;
>  
>  		if (fd_entry->d_type != DT_LNK)
>  			continue;
> @@ -458,12 +457,13 @@ 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 out;

It could be just 'break'.

Thanks,
Namhyung


>  	}
> +out:
>  	if (fdinfo_dir_fd != -1)
>  		close(fdinfo_dir_fd);
>  	closedir(fd_dir);
> -	return 0;
> +	return ret;
>  }
>  
>  static int for_each_drm_fdinfo(bool skip_all_duplicates,
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf drm_pmu: Prevent resource leak in for_each_drm_fdinfo_in_dir()
  2025-08-14 20:31 ` Namhyung Kim
@ 2025-08-14 20:42   ` Namhyung Kim
  0 siblings, 0 replies; 3+ messages in thread
From: Namhyung Kim @ 2025-08-14 20:42 UTC (permalink / raw)
  To: Sergio Perez Gonzalez, zhaoguohan
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	irogers, adrian.hunter, kan.liang, colin.i.king, linux-perf-users,
	linux-kernel

On Thu, Aug 14, 2025 at 01:31:01PM -0700, Namhyung Kim wrote:
> Hello,
> 
> On Thu, Aug 14, 2025 at 12:06:11AM -0600, Sergio Perez Gonzalez wrote:
> > Close fdinfo_dir_fd and fd_dir prior to exit, in the event of
> > cb() error.
> > 
> > Signed-off-by: Sergio Perez Gonzalez <sperezglz@gmail.com>
> 
> Acked-by: Namhyung Kim <namhyung@kernel.org>

Just found another fix was posted already:

https://lore.kernel.org/r/20250813033432.8943-1-zhaoguohan@kylinos.cn

Thanks,
Namhyung

> 
> Only a nitpick below.
> 
> > ---
> >  tools/perf/util/drm_pmu.c | 8 ++++----
> >  1 file changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/tools/perf/util/drm_pmu.c b/tools/perf/util/drm_pmu.c
> > index 988890f37ba7..416aeac7956e 100644
> > --- a/tools/perf/util/drm_pmu.c
> > +++ b/tools/perf/util/drm_pmu.c
> > @@ -403,7 +403,7 @@ static int for_each_drm_fdinfo_in_dir(int (*cb)(void *args, int fdinfo_dir_fd, c
> >  	DIR *fd_dir;
> >  	struct dirent *fd_entry;
> >  	int fd_dir_fd, fdinfo_dir_fd = -1;
> > -
> > +	int ret = 0;
> >  
> >  	scnprintf(buf, sizeof(buf), "%s/fd", pid_name);
> >  	fd_dir_fd = openat(proc_dir, buf, O_DIRECTORY);
> > @@ -418,7 +418,6 @@ static int for_each_drm_fdinfo_in_dir(int (*cb)(void *args, int fdinfo_dir_fd, c
> >  		struct stat stat;
> >  		unsigned int minor;
> >  		bool is_dup = false;
> > -		int ret;
> >  
> >  		if (fd_entry->d_type != DT_LNK)
> >  			continue;
> > @@ -458,12 +457,13 @@ 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 out;
> 
> It could be just 'break'.
> 
> Thanks,
> Namhyung
> 
> 
> >  	}
> > +out:
> >  	if (fdinfo_dir_fd != -1)
> >  		close(fdinfo_dir_fd);
> >  	closedir(fd_dir);
> > -	return 0;
> > +	return ret;
> >  }
> >  
> >  static int for_each_drm_fdinfo(bool skip_all_duplicates,
> > -- 
> > 2.43.0
> > 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-08-14 20:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-14  6:06 [PATCH] perf drm_pmu: Prevent resource leak in for_each_drm_fdinfo_in_dir() Sergio Perez Gonzalez
2025-08-14 20:31 ` Namhyung Kim
2025-08-14 20:42   ` Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).