Skip to contentSkip to conversation

KVM/arm64: vgic-its: Fix memory leak when vgic_its_set_abi() fails

PATCH In review Jackie Liu 2 participants 3 messages Jun 4

Subsystems (2)
lore
Share & tools
lore

Jackie Liu

author
#0 lore raw

PATCH 1/1 KVM/arm64: vgic-its: Fix memory leak when vgic_its_set_abi() fails

From: Jackie Liu <[email protected]>

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")
---
 arch/arm64/kvm/vgic/vgic-its.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +11 -7
index 1d7e5d560af4..83718eab4e06 100644
@@ -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)
Signature · 1 line
2.54.0
maintainer
#1 lore raw
Quoted 11 lines · Jackie Liu

From: Jackie Liu <[email protected]>

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")

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

author
#2 lore raw

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

Quoted 10 lines · Jackie Liu
Quoted 8 lines

From: Jackie Liu <[email protected]>

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.

Quoted 8 lines
Quoted 6 lines

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")

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

Quoted 4 lines

Thanks,
Oliver