Re: [PATCH v2 0/2] LoongArch: KVM: Fix kvm_loongarch_env_init() error path

From: Bibo Mao

Date: Wed Sep 02 2026 - 04:17:23 EST




On 2026/9/2 下午3:07, Chaithanya Lagisetty wrote:
kvm_loongarch_env_init() leaves everything it has already acquired behind
when one of the IPI/EIOINTC/PCH-PIC/DMSINTC device registrations fails, and
kvm_loongarch_init() does not call kvm_loongarch_env_exit() on that error,
so nothing else cleans up either.

v1 unwound only the memory and the perf callbacks. Bibo Mao pointed out
that the device registrations should be undone through wrapped unregister
helpers, so patch 1 adds those and patch 2 uses them to unwind the error
path in reverse order of registration. Patch 2 also uses them in
kvm_loongarch_env_exit(), so that the device ops are unregistered on normal
teardown as well.

Link: https://lore.kernel.org/all/20260817115847.2705791-1-nagachaithanya9911@xxxxxxxxx/ [v1]

v2:
- Add unregister helpers as suggested by Bibo Mao
- Unwind device registrations in reverse order
- Use unregister helpers in kvm_loongarch_env_exit()
- Split into two patches, so the fix only uses the helpers
- Reword commit message to avoid implementation-specific details, and
retitle from "memory leak" to "resource leak"

Compile-tested only, no LoongArch hardware: loongson64_defconfig with
CONFIG_KVM=m and CONFIG_KVM=y, W=1, each patch built individually, no new
warnings.

Chaithanya Lagisetty (2):
LoongArch: KVM: Add unregister helpers for the KVM interrupt devices
LoongArch: KVM: Fix resource leak in kvm_loongarch_env_init() error
path

arch/loongarch/include/asm/kvm_dmsintc.h | 1 +
arch/loongarch/include/asm/kvm_eiointc.h | 1 +
arch/loongarch/include/asm/kvm_ipi.h | 1 +
arch/loongarch/include/asm/kvm_pch_pic.h | 1 +
arch/loongarch/kvm/intc/dmsintc.c | 5 ++++
arch/loongarch/kvm/intc/eiointc.c | 5 ++++
arch/loongarch/kvm/intc/ipi.c | 5 ++++
arch/loongarch/kvm/intc/pch_pic.c | 5 ++++
arch/loongarch/kvm/main.c | 32 +++++++++++++++++++++---
9 files changed, 52 insertions(+), 4 deletions(-)

For this series:
Reviewed-by: Bibo Mao <maobibo@xxxxxxxxxxx>