diff mbox series

[v4] irqchip/riscv-imsic: Adjust the number of available guest irq files

Message ID 20251222093718.26223-1-luxu.kernel@bytedance.com
State New
Headers show
Series [v4] irqchip/riscv-imsic: Adjust the number of available guest irq files | expand

Commit Message

Xu Lu Dec. 22, 2025, 9:37 a.m. UTC
Currently, KVM assumes the minimum of implemented HGEIE bits and
"BIT(gc->guest_index_bits) - 1" as the number of guest files available
across all CPUs. This will not work when CPUs have different number
of guest files because KVM may incorrectly allocate a guest file on a CPU
with fewer guest files.

To address above, during initialization, we calculate the number of
available guest interrupt files according to MMIO resources and constrain
the number of guest interrupt files that can be allocated by KVM.

Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
---
 arch/riscv/kvm/aia.c                    |  2 +-
 drivers/irqchip/irq-riscv-imsic-state.c | 12 +++++++++++-
 include/linux/irqchip/riscv-imsic.h     |  3 +++
 3 files changed, 15 insertions(+), 2 deletions(-)

Comments

Nutty.Liu Dec. 23, 2025, 6:15 a.m. UTC | #1
On 12/22/2025 5:37 PM, Xu Lu wrote:
> Currently, KVM assumes the minimum of implemented HGEIE bits and
> "BIT(gc->guest_index_bits) - 1" as the number of guest files available
> across all CPUs. This will not work when CPUs have different number
> of guest files because KVM may incorrectly allocate a guest file on a CPU
> with fewer guest files.
>
> To address above, during initialization, we calculate the number of
> available guest interrupt files according to MMIO resources and constrain
> the number of guest interrupt files that can be allocated by KVM.
>
> Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
> ---
>   arch/riscv/kvm/aia.c                    |  2 +-
>   drivers/irqchip/irq-riscv-imsic-state.c | 12 +++++++++++-
>   include/linux/irqchip/riscv-imsic.h     |  3 +++
>   3 files changed, 15 insertions(+), 2 deletions(-)
Reviewed-by: Nutty Liu <nutty.liu@hotmail.com>

Thanks,
Nutty
> diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c
> index dad3181856600..cac3c2b51d724 100644
> --- a/arch/riscv/kvm/aia.c
> +++ b/arch/riscv/kvm/aia.c
> @@ -630,7 +630,7 @@ int kvm_riscv_aia_init(void)
>   	 */
>   	if (gc)
>   		kvm_riscv_aia_nr_hgei = min((ulong)kvm_riscv_aia_nr_hgei,
> -					    BIT(gc->guest_index_bits) - 1);
> +					    gc->nr_guest_files);
>   	else
>   		kvm_riscv_aia_nr_hgei = 0;
>   
> diff --git a/drivers/irqchip/irq-riscv-imsic-state.c b/drivers/irqchip/irq-riscv-imsic-state.c
> index dc95ad856d80a..cccca38983577 100644
> --- a/drivers/irqchip/irq-riscv-imsic-state.c
> +++ b/drivers/irqchip/irq-riscv-imsic-state.c
> @@ -794,7 +794,7 @@ static int __init imsic_parse_fwnode(struct fwnode_handle *fwnode,
>   
>   int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
>   {
> -	u32 i, j, index, nr_parent_irqs, nr_mmios, nr_handlers = 0;
> +	u32 i, j, index, nr_parent_irqs, nr_mmios, nr_guest_files, nr_handlers = 0;
>   	struct imsic_global_config *global;
>   	struct imsic_local_config *local;
>   	void __iomem **mmios_va = NULL;
> @@ -888,6 +888,7 @@ int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
>   	}
>   
>   	/* Configure handlers for target CPUs */
> +	global->nr_guest_files = BIT(global->guest_index_bits) - 1;
>   	for (i = 0; i < nr_parent_irqs; i++) {
>   		rc = imsic_get_parent_hartid(fwnode, i, &hartid);
>   		if (rc) {
> @@ -928,6 +929,15 @@ int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
>   		local->msi_pa = mmios[index].start + reloff;
>   		local->msi_va = mmios_va[index] + reloff;
>   
> +		/*
> +		 * KVM uses global->nr_guest_files to determine the available guest
> +		 * interrupt files on each CPU. Take the minimum number of guest
> +		 * interrupt files across all CPUs to avoid KVM incorrectly allocatling
> +		 * an unexisted or unmapped guest interrupt file on some CPUs.
> +		 */
> +		nr_guest_files = (resource_size(&mmios[index]) - reloff) / IMSIC_MMIO_PAGE_SZ - 1;
> +		global->nr_guest_files = min(global->nr_guest_files, nr_guest_files);
> +
>   		nr_handlers++;
>   	}
>   
> diff --git a/include/linux/irqchip/riscv-imsic.h b/include/linux/irqchip/riscv-imsic.h
> index 7494952c55187..43aed52385008 100644
> --- a/include/linux/irqchip/riscv-imsic.h
> +++ b/include/linux/irqchip/riscv-imsic.h
> @@ -69,6 +69,9 @@ struct imsic_global_config {
>   	/* Number of guest interrupt identities */
>   	u32					nr_guest_ids;
>   
> +	/* Number of guest interrupt files per core */
> +	u32					nr_guest_files;
> +
>   	/* Per-CPU IMSIC addresses */
>   	struct imsic_local_config __percpu	*local;
>   };
Anup Patel Jan. 4, 2026, 1:16 p.m. UTC | #2
On Mon, Dec 22, 2025 at 3:07 PM Xu Lu <luxu.kernel@bytedance.com> wrote:
>
> Currently, KVM assumes the minimum of implemented HGEIE bits and
> "BIT(gc->guest_index_bits) - 1" as the number of guest files available
> across all CPUs. This will not work when CPUs have different number
> of guest files because KVM may incorrectly allocate a guest file on a CPU
> with fewer guest files.
>
> To address above, during initialization, we calculate the number of

/, we calculate .../, calculate .../

> available guest interrupt files according to MMIO resources and constrain
> the number of guest interrupt files that can be allocated by KVM.
>
> Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
> ---
>  arch/riscv/kvm/aia.c                    |  2 +-
>  drivers/irqchip/irq-riscv-imsic-state.c | 12 +++++++++++-
>  include/linux/irqchip/riscv-imsic.h     |  3 +++
>  3 files changed, 15 insertions(+), 2 deletions(-)
>
> diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c
> index dad3181856600..cac3c2b51d724 100644
> --- a/arch/riscv/kvm/aia.c
> +++ b/arch/riscv/kvm/aia.c
> @@ -630,7 +630,7 @@ int kvm_riscv_aia_init(void)
>          */
>         if (gc)
>                 kvm_riscv_aia_nr_hgei = min((ulong)kvm_riscv_aia_nr_hgei,
> -                                           BIT(gc->guest_index_bits) - 1);
> +                                           gc->nr_guest_files);
>         else
>                 kvm_riscv_aia_nr_hgei = 0;
>
> diff --git a/drivers/irqchip/irq-riscv-imsic-state.c b/drivers/irqchip/irq-riscv-imsic-state.c
> index dc95ad856d80a..cccca38983577 100644
> --- a/drivers/irqchip/irq-riscv-imsic-state.c
> +++ b/drivers/irqchip/irq-riscv-imsic-state.c
> @@ -794,7 +794,7 @@ static int __init imsic_parse_fwnode(struct fwnode_handle *fwnode,
>
>  int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
>  {
> -       u32 i, j, index, nr_parent_irqs, nr_mmios, nr_handlers = 0;
> +       u32 i, j, index, nr_parent_irqs, nr_mmios, nr_guest_files, nr_handlers = 0;
>         struct imsic_global_config *global;
>         struct imsic_local_config *local;
>         void __iomem **mmios_va = NULL;
> @@ -888,6 +888,7 @@ int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
>         }
>
>         /* Configure handlers for target CPUs */
> +       global->nr_guest_files = BIT(global->guest_index_bits) - 1;
>         for (i = 0; i < nr_parent_irqs; i++) {
>                 rc = imsic_get_parent_hartid(fwnode, i, &hartid);
>                 if (rc) {
> @@ -928,6 +929,15 @@ int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
>                 local->msi_pa = mmios[index].start + reloff;
>                 local->msi_va = mmios_va[index] + reloff;
>
> +               /*
> +                * KVM uses global->nr_guest_files to determine the available guest
> +                * interrupt files on each CPU. Take the minimum number of guest
> +                * interrupt files across all CPUs to avoid KVM incorrectly allocatling

s/allocatling/allocating/

> +                * an unexisted or unmapped guest interrupt file on some CPUs.
> +                */
> +               nr_guest_files = (resource_size(&mmios[index]) - reloff) / IMSIC_MMIO_PAGE_SZ - 1;
> +               global->nr_guest_files = min(global->nr_guest_files, nr_guest_files);
> +
>                 nr_handlers++;
>         }
>
> diff --git a/include/linux/irqchip/riscv-imsic.h b/include/linux/irqchip/riscv-imsic.h
> index 7494952c55187..43aed52385008 100644
> --- a/include/linux/irqchip/riscv-imsic.h
> +++ b/include/linux/irqchip/riscv-imsic.h
> @@ -69,6 +69,9 @@ struct imsic_global_config {
>         /* Number of guest interrupt identities */
>         u32                                     nr_guest_ids;
>
> +       /* Number of guest interrupt files per core */
> +       u32                                     nr_guest_files;
> +
>         /* Per-CPU IMSIC addresses */
>         struct imsic_local_config __percpu      *local;
>  };
> --
> 2.20.1
>

Otherwise, it looks good to me.

Reviewed-by: Anup Patel <anup@brainfault.org>

@tglx, Is it okay to take this through the KVM RISC-V tree
since this change focuses on guest files used by KVM ?

Regards,
Anup
Thomas Gleixner Jan. 9, 2026, 12:32 p.m. UTC | #3
On Sun, Jan 04 2026 at 18:46, Anup Patel wrote:
> On Mon, Dec 22, 2025 at 3:07 PM Xu Lu <luxu.kernel@bytedance.com> wrote:
>
> Reviewed-by: Anup Patel <anup@brainfault.org>
>
> @tglx, Is it okay to take this through the KVM RISC-V tree
> since this change focuses on guest files used by KVM ?

Sure, if there are no conflicts against tip irq/drivers.

Acked-by: Thomas Gleixner <tglx@kernel.org>
diff mbox series

Patch

diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c
index dad3181856600..cac3c2b51d724 100644
--- a/arch/riscv/kvm/aia.c
+++ b/arch/riscv/kvm/aia.c
@@ -630,7 +630,7 @@  int kvm_riscv_aia_init(void)
 	 */
 	if (gc)
 		kvm_riscv_aia_nr_hgei = min((ulong)kvm_riscv_aia_nr_hgei,
-					    BIT(gc->guest_index_bits) - 1);
+					    gc->nr_guest_files);
 	else
 		kvm_riscv_aia_nr_hgei = 0;
 
diff --git a/drivers/irqchip/irq-riscv-imsic-state.c b/drivers/irqchip/irq-riscv-imsic-state.c
index dc95ad856d80a..cccca38983577 100644
--- a/drivers/irqchip/irq-riscv-imsic-state.c
+++ b/drivers/irqchip/irq-riscv-imsic-state.c
@@ -794,7 +794,7 @@  static int __init imsic_parse_fwnode(struct fwnode_handle *fwnode,
 
 int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
 {
-	u32 i, j, index, nr_parent_irqs, nr_mmios, nr_handlers = 0;
+	u32 i, j, index, nr_parent_irqs, nr_mmios, nr_guest_files, nr_handlers = 0;
 	struct imsic_global_config *global;
 	struct imsic_local_config *local;
 	void __iomem **mmios_va = NULL;
@@ -888,6 +888,7 @@  int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
 	}
 
 	/* Configure handlers for target CPUs */
+	global->nr_guest_files = BIT(global->guest_index_bits) - 1;
 	for (i = 0; i < nr_parent_irqs; i++) {
 		rc = imsic_get_parent_hartid(fwnode, i, &hartid);
 		if (rc) {
@@ -928,6 +929,15 @@  int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
 		local->msi_pa = mmios[index].start + reloff;
 		local->msi_va = mmios_va[index] + reloff;
 
+		/*
+		 * KVM uses global->nr_guest_files to determine the available guest
+		 * interrupt files on each CPU. Take the minimum number of guest
+		 * interrupt files across all CPUs to avoid KVM incorrectly allocatling
+		 * an unexisted or unmapped guest interrupt file on some CPUs.
+		 */
+		nr_guest_files = (resource_size(&mmios[index]) - reloff) / IMSIC_MMIO_PAGE_SZ - 1;
+		global->nr_guest_files = min(global->nr_guest_files, nr_guest_files);
+
 		nr_handlers++;
 	}
 
diff --git a/include/linux/irqchip/riscv-imsic.h b/include/linux/irqchip/riscv-imsic.h
index 7494952c55187..43aed52385008 100644
--- a/include/linux/irqchip/riscv-imsic.h
+++ b/include/linux/irqchip/riscv-imsic.h
@@ -69,6 +69,9 @@  struct imsic_global_config {
 	/* Number of guest interrupt identities */
 	u32					nr_guest_ids;
 
+	/* Number of guest interrupt files per core */
+	u32					nr_guest_files;
+
 	/* Per-CPU IMSIC addresses */
 	struct imsic_local_config __percpu	*local;
 };