Skip to contentSkip to conversation

irqchip/gic-v4, KVM: arm64: Fix the vgic init error paths

PATCH Applied Fuad Tabba 3 participants 10 messages Sep 21 – Nov 3

Subsystems (4)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

v3 cover irqchip/gic-v4, KVM: arm64: Fix the vgic init error paths

Hi folks,

Changes since v2 [1]:
- Patch 3: a vgic_v4_init() failure now frees only the SPI array, since that function unwinds its own state; the teardown label is for the routing failure. (Yuan Yao)
- Rebased on kvmarm/next, where the routing failure already tears down the GICv5 state. Patch 3 moves that into the shared label, so the SPI array is freed on that path too.
- Collected Reviewed-by from Yuan Yao on patches 1 and 2. (thanks!)

Four fixes on the paths that run when vgic init fails partway. Each one needs an allocation failure to reach.

Sashiko reported the vgic_init() case [2] while reviewing the SPI-array leak fix, and the two irq-gic-v4.c fixes came out of chasing the first [3]. Then, just when I thought I'd squashed every bug Sashiko could find, it turned up one more in its v1 review: the pre-existing nr_vpes one, fixed in patch 4 as Marc suggested [4].

Patch 1 clears the irq domain and fwnode pointers at the four GICv4 sites that free them and leave them set. Patch 2 makes its_alloc_vcpu_irqs() release what it allocated when the SGI loop fails, which is both the SGI domains and the vPE irqs, wider than the SGI-domain leak I described before posting v1 [3]. Patch 3 releases the SPI array and the vPEs on vgic_init()'s later failure paths, so KVM_DEV_ARM_VGIC_CTRL_INIT is all or nothing and a retry starts from scratch. Patch 4 is Marc's suggested fix [4] for the nr_vpes overload.

These patches run across two subsystems, but ordering matters, which is why this is one series. Patch 2 reuses its_free_sgi_irqs() from an error path, which only works once patch 1 has cleared the pointers. Patch 3 runs vgic_v4_teardown() on a path it never ran on before, which adds a route into what patch 1 fixes. Patch 4 is only correct on top of patch 3, which is what stops online_vcpus outgrowing the vPE array.

Based on kvmarm/next (23ddf997ab16b).

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/CA+EHjTyDix+y6NTLTsXP5j9Sn2VLOZcsw94LntQxg_etRBJZuA@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/

Fuad Tabba (4):
irqchip/gic-v4: Clear the domain and fwnode pointers after freeing
them
irqchip/gic-v4: Unwind what its_alloc_vcpu_irqs() allocated on failure
KVM: arm64: vgic: Tear down what vgic_init() created when it fails
KVM: arm64: vgic-v4: Restore nr_vpes before freeing the vPE resources

 arch/arm64/kvm/vgic/vgic-init.c | 21 +++++++++++++-------
 arch/arm64/kvm/vgic/vgic-v4.c   |  3 +++
 drivers/irqchip/irq-gic-v4.c    | 34 +++++++++++++++++++++++++++------
 3 files changed, 45 insertions(+), 13 deletions(-)

base-commit: 23ddf997ab16b5f4aa9f948f5a68f5b35e4e398f

Signature · 1 line
2.39.5
author reviewer
#1 lore raw

PATCH v3 1/4 irqchip/gic-v4: Clear the domain and fwnode pointers after freeing them

The GICv4 allocation and teardown paths free their irq domains and fwnodes but leave the pointers set, and the allocation error paths test those pointers before removing them. struct its_vm and struct its_vpe are embedded in KVM's per-VM and per-vCPU state, so nothing re-zeroes them between two attempts, and an error path taken after an earlier one already freed the domain calls irq_domain_remove() on freed memory.

Reaching this takes two allocation failures, one to leave the stale pointer behind and one to send the next attempt down the error path.

Fixes 7de5c0af9c7c ("irqchip/gic-v4: Add per-VM VPE domain creation")
Fixes 6d31b6ff985d ("irqchip/gic-v4.1: Add VSGI allocation/teardown")
---
 drivers/irqchip/irq-gic-v4.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/irqchip/irq-gic-v4.c +16 -4
index 8455b4a5fbb0d..754839e409f88 100644
@@ -147,10 +147,14 @@ static int its_alloc_vcpu_sgis(struct its_vpe *vpe, int idx)
 	return 0;
 
 err:
-	if (vpe->sgi_domain)
+	if (vpe->sgi_domain) {
 		irq_domain_remove(vpe->sgi_domain);
-	if (vpe->fwnode)
+		vpe->sgi_domain = NULL;
+	}
+	if (vpe->fwnode) {
 		irq_domain_free_fwnode(vpe->fwnode);
+		vpe->fwnode = NULL;
+	}
 	kfree(name);
 	return -ENOMEM;
 }
@@ -191,10 +195,14 @@ int its_alloc_vcpu_irqs(struct its_vm *vm)
 	return 0;
 
 err:
-	if (vm->domain)
+	if (vm->domain) {
 		irq_domain_remove(vm->domain);
-	if (vm->fwnode)
+		vm->domain = NULL;
+	}
+	if (vm->fwnode) {
 		irq_domain_free_fwnode(vm->fwnode);
+		vm->fwnode = NULL;
+	}
 
 	return -ENOMEM;
 }
@@ -215,6 +223,8 @@ static void its_free_sgi_irqs(struct its_vm *vm)
 		irq_domain_free_irqs(irq, 16);
 		irq_domain_remove(vm->vpes[i]->sgi_domain);
 		irq_domain_free_fwnode(vm->vpes[i]->fwnode);
+		vm->vpes[i]->sgi_domain = NULL;
+		vm->vpes[i]->fwnode = NULL;
 	}
 }
 
@@ -224,6 +234,8 @@ void its_free_vcpu_irqs(struct its_vm *vm)
 	irq_domain_free_irqs(vm->vpes[0]->irq, vm->nr_vpes);
 	irq_domain_remove(vm->domain);
 	irq_domain_free_fwnode(vm->fwnode);
+	vm->domain = NULL;
+	vm->fwnode = NULL;
 }
 
 static int its_send_vpe_cmd(struct its_vpe *vpe, struct its_cmd_info *info)
Signature · 1 line
2.39.5
author reviewer
#2 lore raw

PATCH v3 2/4 irqchip/gic-v4: Unwind what its_alloc_vcpu_irqs() allocated on failure

In reply to Fuad Tabba

A failure in the its_alloc_vcpu_sgis() loop leaves behind both the SGI domains created for the vPEs below the failing index and the vPE irqs allocated before the loop, since irq_domain_remove() frees neither. Each leaked vPE takes its ITS state with it, a vpe_id and an LPI pending table.

Free both from a second label before the existing unwind. With the freed pointers now cleared, its_free_sgi_irqs() can skip a vPE with no SGI domain and be reused there. The check has to precede the lookup:
irq_find_mapping(NULL, 0) falls back to irq_default_domain, and the live irq it returns would then be freed.

Fixes 6d31b6ff985d ("irqchip/gic-v4.1: Add VSGI allocation/teardown")
---
 drivers/irqchip/irq-gic-v4.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/irqchip/irq-gic-v4.c +12 -2
index 754839e409f88..f707a3cb281aa 100644
@@ -159,6 +159,8 @@ static int its_alloc_vcpu_sgis(struct its_vpe *vpe, int idx)
 	return -ENOMEM;
 }
 
+static void its_free_sgi_irqs(struct its_vm *vm);
+
 int its_alloc_vcpu_irqs(struct its_vm *vm)
 {
 	int vpe_base_irq, i;
@@ -189,11 +191,14 @@ int its_alloc_vcpu_irqs(struct its_vm *vm)
 		vm->vpes[i]->irq = vpe_base_irq + i;
 		ret = its_alloc_vcpu_sgis(vm->vpes[i], i);
 		if (ret)
-			goto err;
+			goto err_free_irqs;
 	}
 
 	return 0;
 
+err_free_irqs:
+	its_free_sgi_irqs(vm);
+	irq_domain_free_irqs(vpe_base_irq, vm->nr_vpes);
 err:
 	if (vm->domain) {
 		irq_domain_remove(vm->domain);
@@ -215,8 +220,13 @@ static void its_free_sgi_irqs(struct its_vm *vm)
 		return;
 
 	for (i = 0; i < vm->nr_vpes; i++) {
-		unsigned int irq = irq_find_mapping(vm->vpes[i]->sgi_domain, 0);
+		unsigned int irq;
 
+		/* irq_find_mapping() falls back to the default domain on NULL. */
+		if (!vm->vpes[i]->sgi_domain)
+			continue;
+
+		irq = irq_find_mapping(vm->vpes[i]->sgi_domain, 0);
 		if (WARN_ON(!irq))
 			continue;
 
Signature · 1 line
2.39.5
author reviewer
#3 lore raw

PATCH v3 3/4 KVM: arm64: vgic: Tear down what vgic_init() created when it fails

In reply to Fuad Tabba

Once kvm_vgic_dist_init() has succeeded, every later failure in vgic_init() returns with the SPI array still allocated. A failure after vgic_v4_init() has also succeeded, which means only kvm_vgic_setup_default_irq_routing(), leaves the vPE array behind as well.

A failed vgic_init() leaves kvm_arch_vcpu_precreate() admitting new vCPUs, so a retry of KVM_DEV_ARM_VGIC_CTRL_INIT reaches vgic_v4_init()'s early return with an array that no longer covers every vCPU, and vgic_v3_load()'s WARN_ON(vgic_v4_load()) fires on the first one it misses.

Release each on the paths that can reach it, so the ioctl is all or nothing and a retry starts from scratch. vgic_v4_init() unwinds its own state on every error return, so its failure leaves only the SPI array to free. dist->nr_spis stays frozen, since the SPI count cannot change once vgic_init() has supplied it.

Fixes 180ae7b11823 ("KVM: arm/arm64: Enable irqchip routing")
Fixes 74fe55dc9ab7 ("KVM: arm/arm64: GICv4: Add init/teardown of the per-VM vPE irq domain")
---
 arch/arm64/kvm/vgic/vgic-init.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +14 -7
index 1537f4d20318e..67d1e8ce2aa2f 100644
@@ -513,7 +513,7 @@ int vgic_init(struct kvm *kvm)
 		if (vgic_supports_direct_irqs(kvm)) {
 			ret = vgic_v4_init(kvm);
 			if (ret)
-				return ret;
+				goto out_free_spis;
 		}
 	} else {
 		if (!dist->nr_spis)
@@ -528,17 +528,24 @@ int vgic_init(struct kvm *kvm)
 		kvm_vgic_vcpu_reset(vcpu);
 
 	ret = kvm_vgic_setup_default_irq_routing(kvm);
-	if (ret) {
-		if (vgic_is_v5(kvm))
-			vgic_v5_teardown(kvm);
-
-		return ret;
-	}
+	if (ret)
+		goto out_teardown;
 
 	vgic_debug_init(kvm);
 	dist->initialized = true;
 
 	return 0;
+
+out_teardown:
+	if (vgic_is_v5(kvm))
+		vgic_v5_teardown(kvm);
+	else
+		vgic_v4_teardown(kvm);
+out_free_spis:
+	kfree(dist->spis);
+	dist->spis = NULL;
+
+	return ret;
 }
 
 static void kvm_vgic_dist_destroy(struct kvm *kvm)
Signature · 1 line
2.39.5
author reviewer
#4 lore raw

PATCH v3 4/4 KVM: arm64: vgic-v4: Restore nr_vpes before freeing the vPE resources

In reply to Fuad Tabba

vgic_v4_init() truncates its_vm.nr_vpes to the failing index to bound vgic_v4_teardown()'s free_irq() loop, but its_free_vcpu_irqs() reads the same field: the vPE irqs and the v4.1 SGI domains at or above that index are never freed, each one leaking a vpe_id and an LPI pending table.

Restore the count from online_vcpus before that call. This depends on the previous patch: a failed vgic_init() keeps admitting vCPUs, so without that patch's teardown the vPE array outlives the failure while online_vcpus grows past the size it was allocated with.

Fixes bdb2d2ccac65 ("KVM: arm/arm64: GICv4: Add doorbell interrupt handling")
---
 arch/arm64/kvm/vgic/vgic-v4.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/vgic/vgic-v4.c +3 -0
index ed236f083f0d7..30e1de3fc7d9d 100644
@@ -333,6 +333,9 @@ void vgic_v4_teardown(struct kvm *kvm)
 		free_irq(irq, vcpu);
 	}
 
+	/* Make sure we free all VM-wide, per-CPU resources */
+	its_vm->nr_vpes = atomic_read(&kvm->online_vcpus);
+
 	its_free_vcpu_irqs(its_vm);
 	kfree(its_vm->vpes);
 	its_vm->nr_vpes = 0;
Signature · 1 line
2.39.5
maintainer
#5 lore raw
In reply to Fuad Tabba
Quoted 11 lines · Fuad Tabba

Hi folks,

Changes since v2 [1]:
- Patch 3: a vgic_v4_init() failure now frees only the SPI array, since that function unwinds its own state; the teardown label is for the routing failure. (Yuan Yao)
- Rebased on kvmarm/next, where the routing failure already tears down the GICv5 state. Patch 3 moves that into the shared label, so the SPI array is freed on that path too.
- Collected Reviewed-by from Yuan Yao on patches 1 and 2. (thanks!)

Quoted 2 lines

Four fixes on the paths that run when vgic init fails partway. Each one needs an allocation failure to reach.

Marc?

maintainer
#6 lore raw
Quoted 13 lines · Thomas Gleixner
Quoted 11 lines · Fuad Tabba

Hi folks,

Changes since v2 [1]:
- Patch 3: a vgic_v4_init() failure now frees only the SPI array, since that function unwinds its own state; the teardown label is for the routing failure. (Yuan Yao)
- Rebased on kvmarm/next, where the routing failure already tears down the GICv5 state. Patch 3 moves that into the shared label, so the SPI array is freed on that path too.
- Collected Reviewed-by from Yuan Yao on patches 1 and 2. (thanks!)

Quoted 4 lines
Quoted 2 lines

Four fixes on the paths that run when vgic init fails partway. Each one needs an allocation failure to reach.

Marc?

I was about to say that I had already queued that, except that it appears it only happened in my head, and nowhere else... :-(

If that's OK with you, I'll queue (for real!) the whole thing via the kvmarm tree, targeting 7.4-rc1.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#7 lore raw
Quoted 18 lines · Marc Zyngier
Quoted 16 lines · Thomas Gleixner
Quoted 13 lines · Fuad Tabba

Hi folks,

Changes since v2 [1]:
- Patch 3: a vgic_v4_init() failure now frees only the SPI array, since that function unwinds its own state; the teardown label is for the routing failure. (Yuan Yao)
- Rebased on kvmarm/next, where the routing failure already tears down the GICv5 state. Patch 3 moves that into the shared label, so the SPI array is freed on that path too.
- Collected Reviewed-by from Yuan Yao on patches 1 and 2. (thanks!)

Four fixes on the paths that run when vgic init fails partway. Each one needs an allocation failure to reach.

Quoted 7 lines
Quoted 1 line

Marc?

I was about to say that I had already queued that, except that it appears it only happened in my head, and nowhere else... :-(

If that's OK with you, I'll queue (for real!) the whole thing via the kvmarm tree, targeting 7.4-rc1.

Ack

maintainer Applied
#8 lore raw
In reply to Fuad Tabba
Quoted 10 lines · Fuad Tabba

Changes since v2 [1]:
- Patch 3: a vgic_v4_init() failure now frees only the SPI array, since that function unwinds its own state; the teardown label is for the routing failure. (Yuan Yao)
- Rebased on kvmarm/next, where the routing failure already tears down the GICv5 state. Patch 3 moves that into the shared label, so the SPI array is freed on that path too.
- Collected Reviewed-by from Yuan Yao on patches 1 and 2. (thanks!)

[...]

Applied to fixes, thanks!

[1/4] irqchip/gic-v4: Clear the domain and fwnode pointers after freeing them

      commit: 1fbdcc13ab303987048669c73134352ef5706666
[2/4] irqchip/gic-v4: Unwind what its_alloc_vcpu_irqs() allocated on failure
      commit: 6b44f7777ee826360714044b1a82fbdb1388304c
[3/4] KVM: arm64: vgic: Tear down what vgic_init() created when it fails
      commit: 25993ec8c5bb30baa746936d7a76f03c58150927
[4/4] KVM: arm64: vgic-v4: Restore nr_vpes before freeing the vPE resources
      commit: 99ec74a33d52219f57cc61a2bc7bd5114cc997b0

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer Applied
#9 lore raw
In reply to Fuad Tabba
Quoted 10 lines · Fuad Tabba

Changes since v2 [1]:
- Patch 3: a vgic_v4_init() failure now frees only the SPI array, since that function unwinds its own state; the teardown label is for the routing failure. (Yuan Yao)
- Rebased on kvmarm/next, where the routing failure already tears down the GICv5 state. Patch 3 moves that into the shared label, so the SPI array is freed on that path too.
- Collected Reviewed-by from Yuan Yao on patches 1 and 2. (thanks!)

[...]

Applied to fixes, thanks!

[1/4] irqchip/gic-v4: Clear the domain and fwnode pointers after freeing them

      commit: 861bebd1fac5f5a25c11e40aec3c3291542045d1
[2/4] irqchip/gic-v4: Unwind what its_alloc_vcpu_irqs() allocated on failure
      commit: fd86d69c951765e24e89a162c53dfe33a8aba976
[3/4] KVM: arm64: vgic: Tear down what vgic_init() created when it fails
      commit: 9f63ac1e31c370d5ed9d49caec4faa469d0007fc
[4/4] KVM: arm64: vgic-v4: Restore nr_vpes before freeing the vPE resources
      commit: 1ad8b8b8827e72b7d5907ee47c43ca0e2506b4f5

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.