Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM/arm64: vgic-its: Fix memory leak when vgic_its_set_abi() fails
@ 2026-06-04  3:14 Jackie Liu
  2026-06-04  6:41 ` Oliver Upton
  0 siblings, 1 reply; 3+ messages in thread
From: Jackie Liu @ 2026-06-04  3:14 UTC (permalink / raw)
  To: maz, linux-arm-kernel; +Cc: oupton, yuzenghui, will, kvmarm

From: Jackie Liu <liuyun01@kylinos.cn>

In vgic_its_create(), if vgic_its_set_abi() fails after allocating the
its structure and setting kvm state, the allocated 'its' is leaked
because the function returns without freeing it.

Fix by rolling back the kvm state flags and freeing the its structure
when vgic_its_set_abi() returns an error.

Fixes: 71afe470e20d ("KVM: arm64: vgic-its: Introduce migration ABI infrastructure")
Signed-off-by: Jackie Liu <liuyun01@kylinos.cn>
---
 arch/arm64/kvm/vgic/vgic-its.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/arch/arm64/kvm/vgic/vgic-its.c b/arch/arm64/kvm/vgic/vgic-its.c
index 1d7e5d560af4..83718eab4e06 100644
--- a/arch/arm64/kvm/vgic/vgic-its.c
+++ b/arch/arm64/kvm/vgic/vgic-its.c
@@ -1878,8 +1878,6 @@ static int vgic_its_create(struct kvm_device *dev, u32 type)
 	INIT_LIST_HEAD(&its->collection_list);
 	xa_init(&its->translation_cache);
 
-	dev->kvm->arch.vgic.msis_require_devid = true;
-	dev->kvm->arch.vgic.has_its = true;
 	its->enabled = false;
 	its->dev = dev;
 
@@ -1887,15 +1885,21 @@ static int vgic_its_create(struct kvm_device *dev, u32 type)
 		((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
 	its->baser_coll_table = INITIAL_BASER_VALUE |
 		((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
-	dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
-
-	dev->private = its;
 
 	ret = vgic_its_set_abi(its, NR_ITS_ABIS - 1);
+	if (ret) {
+		mutex_unlock(&dev->kvm->arch.config_lock);
+		kfree(its);
+		return ret;
+	}
 
-	mutex_unlock(&dev->kvm->arch.config_lock);
+	dev->kvm->arch.vgic.msis_require_devid = true;
+	dev->kvm->arch.vgic.has_its = true;
+	dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
+	dev->private = its;
 
-	return ret;
+	mutex_unlock(&dev->kvm->arch.config_lock);
+	return 0;
 }
 
 static void vgic_its_destroy(struct kvm_device *kvm_dev)
-- 
2.54.0



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

* Re: [PATCH] KVM/arm64: vgic-its: Fix memory leak when vgic_its_set_abi() fails
  2026-06-04  3:14 [PATCH] KVM/arm64: vgic-its: Fix memory leak when vgic_its_set_abi() fails Jackie Liu
@ 2026-06-04  6:41 ` Oliver Upton
  2026-06-04  6:59   ` Jackie Liu
  0 siblings, 1 reply; 3+ messages in thread
From: Oliver Upton @ 2026-06-04  6:41 UTC (permalink / raw)
  To: Jackie Liu; +Cc: maz, linux-arm-kernel, yuzenghui, will, kvmarm

On Thu, Jun 04, 2026 at 11:14:26AM +0800, Jackie Liu wrote:
> From: Jackie Liu <liuyun01@kylinos.cn>
> 
> In vgic_its_create(), if vgic_its_set_abi() fails after allocating the
> its structure and setting kvm state, the allocated 'its' is leaked
> because the function returns without freeing it.
> 
> Fix by rolling back the kvm state flags and freeing the its structure
> when vgic_its_set_abi() returns an error.
> 
> Fixes: 71afe470e20d ("KVM: arm64: vgic-its: Introduce migration ABI infrastructure")
> Signed-off-by: Jackie Liu <liuyun01@kylinos.cn>

vgic_its_set_abi() always succeeds so there's no actual memory leak
here. I'd prefer getting rid of the return value instead.

Thanks,
Oliver


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

* Re: [PATCH] KVM/arm64: vgic-its: Fix memory leak when vgic_its_set_abi() fails
  2026-06-04  6:41 ` Oliver Upton
@ 2026-06-04  6:59   ` Jackie Liu
  0 siblings, 0 replies; 3+ messages in thread
From: Jackie Liu @ 2026-06-04  6:59 UTC (permalink / raw)
  To: Oliver Upton; +Cc: maz, linux-arm-kernel, yuzenghui, will, kvmarm

2026年6月4日 14:41, "Oliver Upton" <oupton@kernel.org mailto:oupton@kernel.org?to=%22Oliver%20Upton%22%20%3Coupton%40kernel.org%3E > 写到:


> 
> On Thu, Jun 04, 2026 at 11:14:26AM +0800, Jackie Liu wrote:
> 
> > 
> > From: Jackie Liu <liuyun01@kylinos.cn>
> >  
> >  In vgic_its_create(), if vgic_its_set_abi() fails after allocating the
> >  its structure and setting kvm state, the allocated 'its' is leaked
> >  because the function returns without freeing it.
> >  
> >  Fix by rolling back the kvm state flags and freeing the its structure
> >  when vgic_its_set_abi() returns an error.
> >  
> >  Fixes: 71afe470e20d ("KVM: arm64: vgic-its: Introduce migration ABI infrastructure")
> >  Signed-off-by: Jackie Liu <liuyun01@kylinos.cn>
> > 
> vgic_its_set_abi() always succeeds so there's no actual memory leak
> here. I'd prefer getting rid of the return value instead.

That was my original thought as well. However, I kept the defensive cleanup logic in place
to make future extensions easier. This way, if additional resource allocation paths are 
introduced later, we're less likely to miss the corresponding cleanup.

That said, looking at the current code, the return value no longer serves any real 
purpose, so removing it would also be a reasonable and cleaner approach.

I'll send a V2 with this change.

Thanks.
Jackie


> 
> Thanks,
> Oliver
>


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

end of thread, other threads:[~2026-06-04  7:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-04  3:14 [PATCH] KVM/arm64: vgic-its: Fix memory leak when vgic_its_set_abi() fails Jackie Liu
2026-06-04  6:41 ` Oliver Upton
2026-06-04  6:59   ` Jackie Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox