RISC-V: KVM: Use flexible array for APLIC IRQ state
Subsystems (3)
PATCH 1/1 RISC-V: KVM: Use flexible array for APLIC IRQ state
Store the per-source APLIC IRQ state in the APLIC allocation instead of allocating it separately.
This ties the IRQ state lifetime directly to the APLIC state, removes a separate allocation failure path, and lets __counted_by() describe the array bounds.
Assisted-by Codex:GPT-5.5
Signed-off-by Rosen Penev <[email protected]>
--- arch/riscv/kvm/aia_aplic.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)
arch/riscv/kvm/aia_aplic.c +3 -12
@@ -35,7 +35,7 @@ struct aplic {u32 nr_irqs;u32 nr_words; -struct aplic_irq *irqs; + struct aplic_irq irqs[] __counted_by(nr_irqs); };static u32 aplic_read_sourcecfg (struct aplic *aplic,u32 irq)
@@ -581,7 +581,7 @@ int kvm_riscv_aia_aplic_init(struct kvm *kvm)return 0 ;/* Allocate APLIC global state */ - aplic =kzalloc_obj (*aplic); + aplic =kzalloc_flex (*aplic, irqs, kvm->arch.aia.nr_sources +1 );if (!aplic)return -ENOMEM ; kvm->arch.aia.aplic_state = aplic;
@@ -589,11 +589,6 @@ int kvm_riscv_aia_aplic_init(struct kvm *kvm)/* Setup APLIC IRQs */ aplic->nr_irqs = kvm->arch.aia.nr_sources +1 ; aplic->nr_words =DIV_ROUND_UP (aplic->nr_irqs,32 ); - aplic->irqs =kzalloc_objs (*aplic->irqs, aplic->nr_irqs); -if (!aplic->irqs) { - ret = -ENOMEM ; - goto fail_free_aplic; - }for (i =0 ; i < aplic->nr_irqs; i++)raw_spin_lock_init (&aplic->irqs[i].lock);
@@ -606,7 +601,7 @@ int kvm_riscv_aia_aplic_init(struct kvm *kvm) &aplic->iodev);mutex_unlock (&kvm->slots_lock);if (ret) - goto fail_free_aplic_irqs; + goto fail_free_aplic;/* Setup default IRQ routing */ ret =kvm_riscv_setup_default_irq_routing (kvm, aplic->nr_irqs);
@@ -619,8 +614,6 @@ int kvm_riscv_aia_aplic_init(struct kvm *kvm)mutex_lock (&kvm->slots_lock);kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , &aplic->iodev);mutex_unlock (&kvm->slots_lock); -fail_free_aplic_irqs: -kfree (aplic->irqs); fail_free_aplic: kvm->arch.aia.aplic_state =NULL ;kfree (aplic);
@@ -638,8 +631,6 @@ void kvm_riscv_aia_aplic_cleanup(struct kvm *kvm)kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , &aplic->iodev);mutex_unlock (&kvm->slots_lock); -kfree (aplic->irqs); - kvm->arch.aia.aplic_state =NULL ;kfree (aplic); }
Signature · 1 line
2.54.0
Quoted 10 lines · Rosen Penev
Store the per-source APLIC IRQ state in the APLIC allocation instead of allocating it separately.
This ties the IRQ state lifetime directly to the APLIC state, removes a separate allocation failure path, and lets
__counted_by()describe the array bounds.Assisted-by Codex:GPT-5.5Signed-off-by Rosen Penev <[email protected]>
LGTM.
Reviewed-by Anup Patel <[email protected]>
Queued this patch for Linux-7.2
Thanks,
Anup
Quoted 68 lines
--- arch/riscv/kvm/aia_aplic.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)arch/riscv/kvm/aia_aplic.c +3
-13@@ -35,7 +35,7 @@ struct aplic {u32 nr_irqs;u32 nr_words; -struct aplic_irq *irqs; + struct aplic_irq irqs[] __counted_by(nr_irqs); };static u32 aplic_read_sourcecfg (struct aplic *aplic,u32 irq)@@ -581,7 +581,7 @@ int kvm_riscv_aia_aplic_init(struct kvm *kvm)return 0 ;/* Allocate APLIC global state */ - aplic =kzalloc_obj (*aplic); + aplic =kzalloc_flex (*aplic, irqs, kvm->arch.aia.nr_sources +1 );if (!aplic)return -ENOMEM ; kvm->arch.aia.aplic_state = aplic;@@ -589,11 +589,6 @@ int kvm_riscv_aia_aplic_init(struct kvm *kvm)/* Setup APLIC IRQs */ aplic->nr_irqs = kvm->arch.aia.nr_sources +1 ; aplic->nr_words =DIV_ROUND_UP (aplic->nr_irqs,32 ); - aplic->irqs =kzalloc_objs (*aplic->irqs, aplic->nr_irqs); -if (!aplic->irqs) { - ret = -ENOMEM ; - goto fail_free_aplic; - }for (i =0 ; i < aplic->nr_irqs; i++)raw_spin_lock_init (&aplic->irqs[i].lock);@@ -606,7 +601,7 @@ int kvm_riscv_aia_aplic_init(struct kvm *kvm) &aplic->iodev);mutex_unlock (&kvm->slots_lock);if (ret) - goto fail_free_aplic_irqs; + goto fail_free_aplic;/* Setup default IRQ routing */ ret =kvm_riscv_setup_default_irq_routing (kvm, aplic->nr_irqs);@@ -619,8 +614,6 @@ int kvm_riscv_aia_aplic_init(struct kvm *kvm)mutex_lock (&kvm->slots_lock);kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , &aplic->iodev);mutex_unlock (&kvm->slots_lock); -fail_free_aplic_irqs: -kfree (aplic->irqs); fail_free_aplic: kvm->arch.aia.aplic_state =NULL ;kfree (aplic);@@ -638,8 +631,6 @@ void kvm_riscv_aia_aplic_cleanup(struct kvm *kvm)kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , &aplic->iodev);mutex_unlock (&kvm->slots_lock); -kfree (aplic->irqs); - kvm->arch.aia.aplic_state =NULL ;kfree (aplic); } --2.54.0