[PATCH] LoongArch: KVM: Free init resources if kvm_init() fails

Chaithanya Lagisetty posted 1 patch 1 month, 1 week ago
arch/loongarch/kvm/main.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
[PATCH] LoongArch: KVM: Free init resources if kvm_init() fails
Posted by Chaithanya Lagisetty 1 month, 1 week ago
kvm_loongarch_init() calls kvm_loongarch_env_init() to allocate the
per-CPU kvm_context (vmcs) and kvm_loongarch_ops and to register the
perf callbacks, and then calls kvm_init(). If kvm_init() fails its
result is returned directly, but since module_init() does not run
module_exit() on failure, kvm_loongarch_env_exit() is never called and
those resources are leaked.

Call kvm_loongarch_env_exit() when kvm_init() fails, matching the
teardown-on-failure pattern used by riscv_kvm_init().

Fixes: 2bd6ac687261 ("LoongArch: KVM: Implement kvm module related interface")
Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
---
 arch/loongarch/kvm/main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c
index 3e1005526f4b..b6ddf5827c03 100644
--- a/arch/loongarch/kvm/main.c
+++ b/arch/loongarch/kvm/main.c
@@ -428,7 +428,11 @@ static int kvm_loongarch_init(void)
 	if (r)
 		return r;
 
-	return kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
+	r = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
+	if (r)
+		kvm_loongarch_env_exit();
+
+	return r;
 }
 
 static void kvm_loongarch_exit(void)
-- 
2.43.0
Re: [PATCH] LoongArch: KVM: Free init resources if kvm_init() fails
Posted by Huacai Chen 3 weeks, 2 days ago
Applied, thanks.

Huacai

On Mon, Aug 17, 2026 at 8:39 PM Chaithanya Lagisetty
<nagachaithanya9911@gmail.com> wrote:
>
> kvm_loongarch_init() calls kvm_loongarch_env_init() to allocate the
> per-CPU kvm_context (vmcs) and kvm_loongarch_ops and to register the
> perf callbacks, and then calls kvm_init(). If kvm_init() fails its
> result is returned directly, but since module_init() does not run
> module_exit() on failure, kvm_loongarch_env_exit() is never called and
> those resources are leaked.
>
> Call kvm_loongarch_env_exit() when kvm_init() fails, matching the
> teardown-on-failure pattern used by riscv_kvm_init().
>
> Fixes: 2bd6ac687261 ("LoongArch: KVM: Implement kvm module related interface")
> Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
> ---
>  arch/loongarch/kvm/main.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c
> index 3e1005526f4b..b6ddf5827c03 100644
> --- a/arch/loongarch/kvm/main.c
> +++ b/arch/loongarch/kvm/main.c
> @@ -428,7 +428,11 @@ static int kvm_loongarch_init(void)
>         if (r)
>                 return r;
>
> -       return kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
> +       r = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
> +       if (r)
> +               kvm_loongarch_env_exit();
> +
> +       return r;
>  }
>
>  static void kvm_loongarch_exit(void)
> --
> 2.43.0
>
Re: [PATCH] LoongArch: KVM: Free init resources if kvm_init() fails
Posted by Bibo Mao 3 weeks, 6 days ago

On 2026/8/17 下午8:39, Chaithanya Lagisetty wrote:
> kvm_loongarch_init() calls kvm_loongarch_env_init() to allocate the
> per-CPU kvm_context (vmcs) and kvm_loongarch_ops and to register the
> perf callbacks, and then calls kvm_init(). If kvm_init() fails its
> result is returned directly, but since module_init() does not run
> module_exit() on failure, kvm_loongarch_env_exit() is never called and
> those resources are leaked.
> 
> Call kvm_loongarch_env_exit() when kvm_init() fails, matching the
> teardown-on-failure pattern used by riscv_kvm_init().
> 
> Fixes: 2bd6ac687261 ("LoongArch: KVM: Implement kvm module related interface")
> Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
> ---
>   arch/loongarch/kvm/main.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c
> index 3e1005526f4b..b6ddf5827c03 100644
> --- a/arch/loongarch/kvm/main.c
> +++ b/arch/loongarch/kvm/main.c
> @@ -428,7 +428,11 @@ static int kvm_loongarch_init(void)
>   	if (r)
>   		return r;
>   
> -	return kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
> +	r = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
> +	if (r)
> +		kvm_loongarch_env_exit();
> +
> +	return r;
>   }
>   
>   static void kvm_loongarch_exit(void)
> 
Sorry the the late response.

Reviewed-by: Bibo Mao <maobibo@loongson.cn>

Re: [PATCH] LoongArch: KVM: Free init resources if kvm_init() fails
Posted by Huacai Chen 4 weeks ago
Hi, Bibo,

What do you think about this?


Huacai

On Mon, Aug 17, 2026 at 8:39 PM Chaithanya Lagisetty
<nagachaithanya9911@gmail.com> wrote:
>
> kvm_loongarch_init() calls kvm_loongarch_env_init() to allocate the
> per-CPU kvm_context (vmcs) and kvm_loongarch_ops and to register the
> perf callbacks, and then calls kvm_init(). If kvm_init() fails its
> result is returned directly, but since module_init() does not run
> module_exit() on failure, kvm_loongarch_env_exit() is never called and
> those resources are leaked.
>
> Call kvm_loongarch_env_exit() when kvm_init() fails, matching the
> teardown-on-failure pattern used by riscv_kvm_init().
>
> Fixes: 2bd6ac687261 ("LoongArch: KVM: Implement kvm module related interface")
> Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
> ---
>  arch/loongarch/kvm/main.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c
> index 3e1005526f4b..b6ddf5827c03 100644
> --- a/arch/loongarch/kvm/main.c
> +++ b/arch/loongarch/kvm/main.c
> @@ -428,7 +428,11 @@ static int kvm_loongarch_init(void)
>         if (r)
>                 return r;
>
> -       return kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
> +       r = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
> +       if (r)
> +               kvm_loongarch_env_exit();
> +
> +       return r;
>  }
>
>  static void kvm_loongarch_exit(void)
> --
> 2.43.0
>