* [PATCH] RISC-V: KVM: Use flexible array for APLIC IRQ state
@ 2026-05-11 3:21 Rosen Penev
2026-06-01 8:28 ` Anup Patel
0 siblings, 1 reply; 2+ messages in thread
From: Rosen Penev @ 2026-05-11 3:21 UTC (permalink / raw)
To: linux-riscv
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Kees Cook, Gustavo A. R. Silva,
open list:KERNEL VIRTUAL MACHINE FOR RISC-V (KVM/riscv),
open list:KERNEL VIRTUAL MACHINE FOR RISC-V (KVM/riscv),
open list,
open list:KERNEL HARDENING (not covered by other
areas):Keyword:b__counted_by(_le|_be)?b
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 <rosenp@gmail.com>
---
arch/riscv/kvm/aia_aplic.c | 15 +++------------
1 file changed, 3 insertions(+), 12 deletions(-)
diff --git a/arch/riscv/kvm/aia_aplic.c b/arch/riscv/kvm/aia_aplic.c
index 3464f3351df7..748107c347d9 100644
--- a/arch/riscv/kvm/aia_aplic.c
+++ b/arch/riscv/kvm/aia_aplic.c
@@ -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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] RISC-V: KVM: Use flexible array for APLIC IRQ state
2026-05-11 3:21 [PATCH] RISC-V: KVM: Use flexible array for APLIC IRQ state Rosen Penev
@ 2026-06-01 8:28 ` Anup Patel
0 siblings, 0 replies; 2+ messages in thread
From: Anup Patel @ 2026-06-01 8:28 UTC (permalink / raw)
To: Rosen Penev
Cc: linux-riscv, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Kees Cook, Gustavo A. R. Silva,
open list:KERNEL VIRTUAL MACHINE FOR RISC-V (KVM/riscv),
open list:KERNEL VIRTUAL MACHINE FOR RISC-V (KVM/riscv),
open list,
open list:KERNEL HARDENING (not covered by other
areas):Keyword:b__counted_by(_le|_be)?b
On Mon, May 11, 2026 at 8:52 AM Rosen Penev <rosenp@gmail.com> wrote:
>
> 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 <rosenp@gmail.com>
LGTM.
Reviewed-by: Anup Patel <anup@brainfault.org>
Queued this patch for Linux-7.2
Thanks,
Anup
> ---
> arch/riscv/kvm/aia_aplic.c | 15 +++------------
> 1 file changed, 3 insertions(+), 12 deletions(-)
>
> diff --git a/arch/riscv/kvm/aia_aplic.c b/arch/riscv/kvm/aia_aplic.c
> index 3464f3351df7..748107c347d9 100644
> --- a/arch/riscv/kvm/aia_aplic.c
> +++ b/arch/riscv/kvm/aia_aplic.c
> @@ -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
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-06-01 8:28 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-11 3:21 [PATCH] RISC-V: KVM: Use flexible array for APLIC IRQ state Rosen Penev
2026-06-01 8:28 ` Anup Patel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®