* [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; 2+ 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] 2+ 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
0 siblings, 0 replies; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2026-07-31 17:04 UTC | newest]
Thread overview: 2+ 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox