Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Alexandru Elisei <alexandru.elisei@arm.com>
To: Zongmin Zhou <min_halo@163.com>
Cc: will@kernel.org, julien.thierry.kdev@gmail.com,
	kvm@vger.kernel.org, Zongmin Zhou <zhouzongmin@kylinos.cn>
Subject: Re: [PATCH kvmtool v2] arm64: Fix resource leaks in find_pmu_cpumask()
Date: Tue, 11 Aug 2026 16:29:37 +0100	[thread overview]
Message-ID: <ans_4VcCJblNYplt@raptor> (raw)
In-Reply-To: <20260806023218.21068-1-min_halo@163.com>

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

  reply	other threads:[~2026-08-11 15:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]
2026-08-12 12:42     ` Will Deacon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ans_4VcCJblNYplt@raptor \
    --to=alexandru.elisei@arm.com \
    --cc=julien.thierry.kdev@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=min_halo@163.com \
    --cc=will@kernel.org \
    --cc=zhouzongmin@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox