irqchip/gic-v4, KVM: arm64: Fix the vgic init error paths
Fuad Tabba
reviewerv2 cover irqchip/gic-v4, KVM: arm64: Fix the vgic init error paths
Hi folks,
Changes since v1 [1]:
- New patch 4: restore its_vm.nr_vpes in vgic_v4_teardown() so that its_free_vcpu_irqs() frees every vPE. (sashiko, Marc)
- Patch 2: corrected the commit message. Skipping a vPE with no SGI domain avoids a wrong free, not a warning.
- Patch 3: added a second Fixes: tag. The SPI-array leak arrived with the routing call, well before the vPE one.
- No code changes to patches 1 to 3.
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 (aa8e5dc6a7a2a).
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 | 11 +++++++++-- arch/arm64/kvm/vgic/vgic-v4.c | 3 +++ drivers/irqchip/irq-gic-v4.c | 34 +++++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 8 deletions(-)
base-commit: aa8e5dc6a7a2a1141ab40706a51010adcd0e57d2
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH v2 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.
--- drivers/irqchip/irq-gic-v4.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/irqchip/irq-gic-v4.c +16 -4
@@ -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
Re: [PATCH v2 1/4] irqchip/gic-v4: Clear the domain and fwnode pointers after freeing them
Quoted 6 lines · Fuad Tabba
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_vmand structits_vpeare 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 callsirq_domain_remove()on freed memory.
Hi Tabba,
Real nice finding!
Quoted 70 lines
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")Signed-off-by Fuad Tabba <[email protected]>--- drivers/irqchip/irq-gic-v4.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)drivers/irqchip/irq-gic-v4.c +16
-5@@ -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) --2.39.5
Fuad Tabba
reviewerPATCH v2 2/4 irqchip/gic-v4: Unwind what its_alloc_vcpu_irqs() allocated on failure
In reply to Fuad TabbaA 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.
--- drivers/irqchip/irq-gic-v4.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/irqchip/irq-gic-v4.c +12 -2
@@ -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
Re: [PATCH v2 2/4] irqchip/gic-v4: Unwind what its_alloc_vcpu_irqs() allocated on failure
Quoted 11 lines · 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, sinceirq_domain_remove()frees neither. Each leaked vPE takes its ITS state with it, avpe_idand 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 toirq_default_domain, and the live irq it returns would then be freed.
Quoted 54 lines
Fixes 6d31b6ff985d ("irqchip/gic-v4.1: Add VSGI allocation/teardown")Signed-off-by Fuad Tabba <[email protected]>--- drivers/irqchip/irq-gic-v4.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)drivers/irqchip/irq-gic-v4.c +12
-3@@ -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 ; --2.39.5
Fuad Tabba
reviewerPATCH v2 3/4 KVM: arm64: vgic: Tear down what vgic_init() created when it fails
In reply to Fuad TabbaOnce 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 today 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 both on the two paths that can reach them, so the ioctl is all or nothing and a retry starts from scratch. dist->nr_spis stays frozen, since the SPI count cannot change once vgic_init() has supplied it.
--- arch/arm64/kvm/vgic/vgic-init.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +9 -2
@@ -462,7 +462,7 @@ int vgic_init(struct kvm *kvm)if (vgic_supports_direct_irqs (kvm)) { ret =vgic_v4_init (kvm);if (ret) -return ret; + goto out_teardown; } }else { ret =vgic_v5_init (kvm);
@@ -475,12 +475,19 @@ int vgic_init(struct kvm *kvm) ret =kvm_vgic_setup_default_irq_routing (kvm);if (ret) -return ret; + goto out_teardown;vgic_debug_init (kvm); dist->initialized = true;return 0 ; + +out_teardown: +vgic_v4_teardown (kvm); +kfree (dist->spis); + dist->spis =NULL ; + +return ret; }static void kvm_vgic_dist_destroy (struct kvm *kvm)
Signature · 1 line
2.39.5
Re: [PATCH v2 3/4] KVM: arm64: vgic: Tear down what vgic_init() created when it fails
Quoted 30 lines · Fuad Tabba
Once
kvm_vgic_dist_init()has succeeded, every later failure invgic_init()returns with the SPI array still allocated. A failure aftervgic_v4_init()has also succeeded, which today means onlykvm_vgic_setup_default_irq_routing(), leaves the vPE array behind as well.A failed
vgic_init()leaveskvm_arch_vcpu_precreate()admitting new vCPUs, so a retry ofKVM_DEV_ARM_VGIC_CTRL_INITreaches 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 both on the two paths that can reach them, so the ioctl is all or nothing and a retry starts from scratch.
dist->nr_spisstays frozen, since the SPI count cannot change oncevgic_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")Reported-by Sashiko <[email protected]>Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/vgic/vgic-init.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)arch/arm64/kvm/vgic/vgic-init.c +0
-0
arch/arm64/kvm/vgic/vgic-init.c int vgic_init(struct kvm *kvm) · line 464
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-init.c +0
-0@@ -462,7 +462,7 @@ int vgic_init(struct kvm *kvm)if (vgic_supports_direct_irqs (kvm)) { ret =vgic_v4_init (kvm);if (ret)
Hi Tabba,
arch/arm64/kvm/vgic/vgic-init.c int vgic_init(struct kvm *kvm) · line 465
Quoted 2 lines
arch/arm64/kvm/vgic/vgic-init.c +1
-1-return ret; + goto out_teardown;
The vigc_v4_init already called vgic_v4_teardown() in error path, thus I think just free dist->spis is enough yet ?
Quoted 27 lines
arch/arm64/kvm/vgic/vgic-init.c +8
-2} }else { ret =vgic_v5_init (kvm);@@ -475,12 +475,19 @@ int vgic_init(struct kvm *kvm) ret =kvm_vgic_setup_default_irq_routing (kvm);if (ret) -return ret; + goto out_teardown;vgic_debug_init (kvm); dist->initialized = true;return 0 ; + +out_teardown: +vgic_v4_teardown (kvm); +kfree (dist->spis); + dist->spis =NULL ; + +return ret; }static void kvm_vgic_dist_destroy (struct kvm *kvm) --2.39.5
Fuad Tabba
reviewerRe: [PATCH v2 3/4] KVM: arm64: vgic: Tear down what vgic_init() created when it fails
Hi Yuan,
On Tue, 25 Aug 2026 at 10:18, Yao Yuan <[email protected]> wrote:
...
Quoted 11 lines
arch/arm64/kvm/vgic/vgic-init.c int vgic_init(struct kvm *kvm) · line 464
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-init.c +0
-0@@ -462,7 +462,7 @@ int vgic_init(struct kvm *kvm)if (vgic_supports_direct_irqs (kvm)) { ret =vgic_v4_init (kvm);if (ret)Hi Tabba,
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-init.c int vgic_init(struct kvm *kvm) · line 465
Quoted 2 lines
arch/arm64/kvm/vgic/vgic-init.c +1
-1-return ret; + goto out_teardown;The
vigc_v4_initalready calledvgic_v4_teardown()in error path, thus I think just freedist->spisis enough yet ?
vgic_v4_init() unwinds its own state on every error return, so vgic_v4_teardown() is a no-op there and freeing the SPI array would be enough. I'll fold the label split in if I respin.
Thanks for the reviews!
/fuad
Quoted 28 lines
Quoted 28 lines
arch/arm64/kvm/vgic/vgic-init.c +8
-2} }else { ret =vgic_v5_init (kvm);@@ -475,12 +475,19 @@ int vgic_init(struct kvm *kvm) ret =kvm_vgic_setup_default_irq_routing (kvm);if (ret) -return ret; + goto out_teardown;vgic_debug_init (kvm); dist->initialized = true;return 0 ; + +out_teardown: +vgic_v4_teardown (kvm); +kfree (dist->spis); + dist->spis =NULL ; + +return ret; }static void kvm_vgic_dist_destroy (struct kvm *kvm) --2.39.5
Fuad Tabba
reviewerPATCH v2 4/4 KVM: arm64: vgic-v4: Restore nr_vpes before freeing the vPE resources
In reply to Fuad Tabbavgic_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.
--- arch/arm64/kvm/vgic/vgic-v4.c | 3 +++ 1 file changed, 3 insertions(+)
arch/arm64/kvm/vgic/vgic-v4.c +3 -0
@@ -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