Skip to content

Commit 376e2f8

Browse files
luxukernelavpatel
authored andcommitted
irqchip/riscv-imsic: Adjust the number of available guest irq files
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, 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> Reviewed-by: Nutty Liu <nutty.liu@hotmail.com> Reviewed-by: Anup Patel <anup@brainfault.org> Acked-by: Thomas Gleixner <tglx@kernel.org> Link: https://lore.kernel.org/r/20260104133457.57742-1-luxu.kernel@bytedance.com Signed-off-by: Anup Patel <anup@brainfault.org>
1 parent ed7ae7a commit 376e2f8

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

arch/riscv/kvm/aia.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ int kvm_riscv_aia_init(void)
630630
*/
631631
if (gc)
632632
kvm_riscv_aia_nr_hgei = min((ulong)kvm_riscv_aia_nr_hgei,
633-
BIT(gc->guest_index_bits) - 1);
633+
gc->nr_guest_files);
634634
else
635635
kvm_riscv_aia_nr_hgei = 0;
636636

drivers/irqchip/irq-riscv-imsic-state.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ static int __init imsic_parse_fwnode(struct fwnode_handle *fwnode,
784784

785785
int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
786786
{
787-
u32 i, j, index, nr_parent_irqs, nr_mmios, nr_handlers = 0;
787+
u32 i, j, index, nr_parent_irqs, nr_mmios, nr_guest_files, nr_handlers = 0;
788788
struct imsic_global_config *global;
789789
struct imsic_local_config *local;
790790
void __iomem **mmios_va = NULL;
@@ -878,6 +878,7 @@ int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
878878
}
879879

880880
/* Configure handlers for target CPUs */
881+
global->nr_guest_files = BIT(global->guest_index_bits) - 1;
881882
for (i = 0; i < nr_parent_irqs; i++) {
882883
rc = imsic_get_parent_hartid(fwnode, i, &hartid);
883884
if (rc) {
@@ -918,6 +919,15 @@ int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
918919
local->msi_pa = mmios[index].start + reloff;
919920
local->msi_va = mmios_va[index] + reloff;
920921

922+
/*
923+
* KVM uses global->nr_guest_files to determine the available guest
924+
* interrupt files on each CPU. Take the minimum number of guest
925+
* interrupt files across all CPUs to avoid KVM incorrectly allocating
926+
* an unexisted or unmapped guest interrupt file on some CPUs.
927+
*/
928+
nr_guest_files = (resource_size(&mmios[index]) - reloff) / IMSIC_MMIO_PAGE_SZ - 1;
929+
global->nr_guest_files = min(global->nr_guest_files, nr_guest_files);
930+
921931
nr_handlers++;
922932
}
923933

include/linux/irqchip/riscv-imsic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ struct imsic_global_config {
6868
/* Number of guest interrupt identities */
6969
u32 nr_guest_ids;
7070

71+
/* Number of guest interrupt files per core */
72+
u32 nr_guest_files;
73+
7174
/* Per-CPU IMSIC addresses */
7275
struct imsic_local_config __percpu *local;
7376
};

0 commit comments

Comments
 (0)