All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH kvmtool] arm64: Fix resource leaks in find_pmu_cpumask()
@ 2026-07-23  9:04 Zongmin Zhou
  2026-07-31 17:04 ` Will Deacon
  0 siblings, 1 reply; 4+ messages in thread
From: Zongmin Zhou @ 2026-07-23  9:04 UTC (permalink / raw)
  To: kvm, will, julien.thierry.kdev; +Cc: Zongmin Zhou

From: Zongmin Zhou <zhouzongmin@kylinos.cn>

Close file descriptors on read_file() failure and close the directory
stream before returning from the function.

Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>
---
 arm64/pmu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arm64/pmu.c b/arm64/pmu.c
index 78c15f1..b4d7605 100644
--- a/arm64/pmu.c
+++ b/arm64/pmu.c
@@ -75,7 +75,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
 	unsigned long val;
 	ssize_t fd_sz;
 	int fd, ret;
-	DIR *dir;
+	DIR *dir = NULL;
 
 	memset(buf, 0, sizeof(buf));
 
@@ -109,6 +109,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
 		fd_sz = read_file(fd, cpulist, PAGE_SIZE);
 		if (fd_sz < 0) {
 			pmu_id = -errno;
+			close(fd);
 			goto out_free;
 		}
 		close(fd);
@@ -142,6 +143,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
 		fd_sz = read_file(fd, buf, PMU_ID_MAXLEN - 1);
 		if (fd_sz < 0) {
 			pmu_id = -errno;
+			close(fd);
 			goto out_free;
 		}
 		close(fd);
@@ -162,6 +164,8 @@ next_dir:
 	}
 
 out_free:
+	if (dir)
+		closedir(dir);
 	free(path);
 	free(cpulist);
 	return pmu_id;
-- 
2.34.1


No virus found
		Checked by Hillstone Network AntiVirus


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

* Re: [PATCH kvmtool] arm64: Fix resource leaks in find_pmu_cpumask()
  2026-07-23  9:04 [PATCH kvmtool] arm64: Fix resource leaks in find_pmu_cpumask() Zongmin Zhou
@ 2026-07-31 17:04 ` Will Deacon
  2026-08-06  2:32   ` [PATCH kvmtool v2] " Zongmin Zhou
  0 siblings, 1 reply; 4+ messages in thread
From: Will Deacon @ 2026-07-31 17:04 UTC (permalink / raw)
  To: Zongmin Zhou; +Cc: kvm, julien.thierry.kdev, Zongmin Zhou

On Thu, Jul 23, 2026 at 05:04:13PM +0800, Zongmin Zhou wrote:
> From: Zongmin Zhou <zhouzongmin@kylinos.cn>
> 
> Close file descriptors on read_file() failure and close the directory
> stream before returning from the function.
> 
> Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>
> ---
>  arm64/pmu.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/arm64/pmu.c b/arm64/pmu.c
> index 78c15f1..b4d7605 100644
> --- a/arm64/pmu.c
> +++ b/arm64/pmu.c
> @@ -75,7 +75,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
>  	unsigned long val;
>  	ssize_t fd_sz;
>  	int fd, ret;
> -	DIR *dir;
> +	DIR *dir = NULL;
>  
>  	memset(buf, 0, sizeof(buf));
>  
> @@ -109,6 +109,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
>  		fd_sz = read_file(fd, cpulist, PAGE_SIZE);
>  		if (fd_sz < 0) {
>  			pmu_id = -errno;
> +			close(fd);
>  			goto out_free;
>  		}
>  		close(fd);

Seems a bit grotty to have identical calls to close() on the success and
failure paths.

> @@ -142,6 +143,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
>  		fd_sz = read_file(fd, buf, PMU_ID_MAXLEN - 1);
>  		if (fd_sz < 0) {
>  			pmu_id = -errno;
> +			close(fd);
>  			goto out_free;
>  		}
>  		close(fd);

Same here...

Will

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

* [PATCH kvmtool v2] arm64: Fix resource leaks in find_pmu_cpumask()
  2026-07-31 17:04 ` Will Deacon
@ 2026-08-06  2:32   ` Zongmin Zhou
  2026-08-11 15:29     ` Alexandru Elisei
  0 siblings, 1 reply; 4+ messages in thread
From: Zongmin Zhou @ 2026-08-06  2:32 UTC (permalink / raw)
  To: will; +Cc: julien.thierry.kdev, kvm, Zongmin Zhou

From: Zongmin Zhou <zhouzongmin@kylinos.cn>

Close file descriptors on read_file() failure and close the directory
stream before returning from the function.

Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>
---
Changes since v1:

Save errno before close() so a close() failure doesn't clobber it,
allowing a single close() for both paths.
---
 arm64/pmu.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/arm64/pmu.c b/arm64/pmu.c
index 78c15f1..ef7faca 100644
--- a/arm64/pmu.c
+++ b/arm64/pmu.c
@@ -75,7 +75,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
 	unsigned long val;
 	ssize_t fd_sz;
 	int fd, ret;
-	DIR *dir;
+	DIR *dir = NULL;
 
 	memset(buf, 0, sizeof(buf));
 
@@ -107,11 +107,12 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
 			goto next_dir;
 
 		fd_sz = read_file(fd, cpulist, PAGE_SIZE);
+		ret = errno;
+		close(fd);
 		if (fd_sz < 0) {
-			pmu_id = -errno;
+			pmu_id = -ret;
 			goto out_free;
 		}
-		close(fd);
 
 		ret = cpulist_parse(cpulist, &pmu_cpumask);
 		if (ret) {
@@ -140,11 +141,12 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
 			goto next_dir;
 
 		fd_sz = read_file(fd, buf, PMU_ID_MAXLEN - 1);
+		ret = errno;
+		close(fd);
 		if (fd_sz < 0) {
-			pmu_id = -errno;
+			pmu_id = -ret;
 			goto out_free;
 		}
-		close(fd);
 
 		val = strtoul(buf, NULL, 10);
 		if (val > INT_MAX) {
@@ -162,6 +164,8 @@ next_dir:
 	}
 
 out_free:
+	if (dir)
+		closedir(dir);
 	free(path);
 	free(cpulist);
 	return pmu_id;
-- 
2.34.1


No virus found
		Checked by Hillstone Network AntiVirus


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

* Re: [PATCH kvmtool v2] arm64: Fix resource leaks in find_pmu_cpumask()
  2026-08-06  2:32   ` [PATCH kvmtool v2] " Zongmin Zhou
@ 2026-08-11 15:29     ` Alexandru Elisei
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandru Elisei @ 2026-08-11 15:29 UTC (permalink / raw)
  To: Zongmin Zhou; +Cc: will, julien.thierry.kdev, kvm, Zongmin Zhou

Hi,

On Thu, Aug 06, 2026 at 10:32:18AM +0800, Zongmin Zhou wrote:
> From: Zongmin Zhou <zhouzongmin@kylinos.cn>
> 
> Close file descriptors on read_file() failure and close the directory
> stream before returning from the function.
> 
> Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>
> ---
> Changes since v1:
> 
> Save errno before close() so a close() failure doesn't clobber it,
> allowing a single close() for both paths.
> ---
>  arm64/pmu.c | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/arm64/pmu.c b/arm64/pmu.c
> index 78c15f1..ef7faca 100644
> --- a/arm64/pmu.c
> +++ b/arm64/pmu.c
> @@ -75,7 +75,7 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
>  	unsigned long val;
>  	ssize_t fd_sz;
>  	int fd, ret;
> -	DIR *dir;
> +	DIR *dir = NULL;
>  
>  	memset(buf, 0, sizeof(buf));
>  
> @@ -107,11 +107,12 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
>  			goto next_dir;
>  
>  		fd_sz = read_file(fd, cpulist, PAGE_SIZE);
> +		ret = errno;
> +		close(fd);
>  		if (fd_sz < 0) {
> -			pmu_id = -errno;
> +			pmu_id = -ret;
>  			goto out_free;
>  		}
> -		close(fd);
>  
>  		ret = cpulist_parse(cpulist, &pmu_cpumask);
>  		if (ret) {
> @@ -140,11 +141,12 @@ static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask)
>  			goto next_dir;
>  
>  		fd_sz = read_file(fd, buf, PMU_ID_MAXLEN - 1);
> +		ret = errno;
> +		close(fd);
>  		if (fd_sz < 0) {
> -			pmu_id = -errno;
> +			pmu_id = -ret;
>  			goto out_free;
>  		}
> -		close(fd);
>  
>  		val = strtoul(buf, NULL, 10);
>  		if (val > INT_MAX) {
> @@ -162,6 +164,8 @@ next_dir:
>  	}
>  
>  out_free:
> +	if (dir)
> +		closedir(dir);
>  	free(path);
>  	free(cpulist);
>  	return pmu_id;

Looks good to me:

Reviewed-by: Alexandru Elisei <alexandru.elisei@arm.com>

Just one tiny, tiny nitpick: I would have set ret = -errno so we can do
pmu_id = ret in case of error, like we do for cpulist_parse() errors, but that's
totally unimportant.

Thanks,
Alex

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

end of thread, other threads:[~2026-08-11 15:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-23  9:04 [PATCH kvmtool] arm64: Fix resource leaks in find_pmu_cpumask() Zongmin Zhou
2026-07-31 17:04 ` Will Deacon
2026-08-06  2:32   ` [PATCH kvmtool v2] " Zongmin Zhou
2026-08-11 15:29     ` Alexandru Elisei

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.