riscv: KVM: Add hart-to-vCPU mapping for faster MSI injection
Subsystems (3)
PATCH v2 1/1 riscv: KVM: Add hart-to-vCPU mapping for faster MSI injection
Replace linear searches over all vCPUs in MSI injection paths with a hart_index -> vCPU xarray lookup table. This avoids O(n) scans on every injected interrupt and improves performance when the number of vCPUs is large.
Both kvm_riscv_aia_inject_msi_by_id() and kvm_riscv_aia_inject_msi() now use the table instead of iterating over the vCPU list.
Changes in v2:
- Use xarray instead of normal array to handle incontiguous hart_index.
- Link to v1:
https://lore.kernel.org/kvm-riscv/[email protected]/
--- arch/riscv/include/asm/kvm_aia.h | 3 ++ arch/riscv/kvm/aia_device.c | 50 +++++++++++++++++--------------- 2 files changed, 30 insertions(+), 23 deletions(-)
arch/riscv/include/asm/kvm_aia.h +3 -0
@@ -12,6 +12,7 @@#include <linux/jump_label.h> #include <linux/kvm_types.h> +#include <linux/xarray.h> #include <asm/csr.h> struct kvm_aia {
@@ -47,6 +48,8 @@ struct kvm_aia {/* Internal state of APLIC */ void *aplic_state; + +struct xarray hart_to_vcpu; };struct kvm_vcpu_aia_csr {
arch/riscv/kvm/aia_device.c +27 -23
102 lines not on this page · raw message
Signature · 1 line
2.53.0
Quoted 10 lines · BillXiang
Replace linear searches over all vCPUs in MSI injection paths with a
hart_index-> vCPU xarray lookup table. This avoids O(n) scans on every injected interrupt and improves performance when the number of vCPUs is large.Both
kvm_riscv_aia_inject_msi_by_id()andkvm_riscv_aia_inject_msi()now use the table instead of iterating over the vCPU list.Signed-off-by BillXiang <[email protected]>Changes in v2:
Hi Bill,
The change log should be under the tear line.
Quoted 141 lines
- Use xarray instead of normal array to handle incontiguous
hart_index.
- Link to v1:https://lore.kernel.org/kvm-riscv/[email protected]/ --- arch/riscv/include/asm/kvm_aia.h | 3 ++ arch/riscv/kvm/aia_device.c | 50 +++++++++++++++++--------------- 2 files changed, 30 insertions(+), 23 deletions(-)arch/riscv/include/asm/kvm_aia.h +3
-0@@ -12,6 +12,7 @@#include <linux/jump_label.h> #include <linux/kvm_types.h> +#include <linux/xarray.h> #include <asm/csr.h> struct kvm_aia {@@ -47,6 +48,8 @@ struct kvm_aia {/* Internal state of APLIC */ void *aplic_state; + +struct xarray hart_to_vcpu; };struct kvm_vcpu_aia_csr {arch/riscv/kvm/aia_device.c +27
-23@@ -259,6 +259,8 @@ static int aia_init(struct kvm *kvm)if (ret)return ret; +xa_init (&aia->hart_to_vcpu); +/* Iterate over each VCPU */ kvm_for_each_vcpu (idx, vcpu, kvm) { vaia = &vcpu->arch.aia_context;@@ -280,6 +282,11 @@ static int aia_init(struct kvm *kvm)/* Update HART index of the IMSIC based on IMSIC base */ vaia->hart_index =aia_imsic_hart_index (aia, vaia->imsic_addr); + + ret =xa_insert (&aia->hart_to_vcpu, vaia->hart_index, vcpu,GFP_KERNEL ); +if (ret) { + goto fail_cleanup_imsics; + }/* Initialize IMSIC for this VCPU */ ret =kvm_riscv_vcpu_aia_imsic_init (vcpu);@@ -299,6 +306,7 @@ static int aia_init(struct kvm *kvm)continue ;kvm_riscv_vcpu_aia_imsic_cleanup (vcpu); } +xa_destroy (&aia->hart_to_vcpu);kvm_riscv_aia_aplic_cleanup (kvm);return ret; }@@ -557,30 +565,25 @@ void kvm_riscv_vcpu_aia_deinit(struct kvm_vcpu *vcpu)int kvm_riscv_aia_inject_msi_by_id (struct kvm *kvm,u32 hart_index,u32 guest_index,u32 iid) { -unsigned long idx;struct kvm_vcpu *vcpu; +struct kvm_aia *aia = &kvm->arch.aia;/* Proceed only if AIA was initialized successfully */ if (!kvm_riscv_aia_initialized (kvm))return -EBUSY ; -/* Inject MSI to matching VCPU */ -kvm_for_each_vcpu (idx, vcpu, kvm) { -if (vcpu->arch.aia_context.hart_index == hart_index) -return kvm_riscv_vcpu_aia_imsic_inject (vcpu, - guest_index, -0 , iid); - } - -return 0 ; + vcpu =xa_load (&aia->hart_to_vcpu, hart_index); +if (!vcpu) +return 0 ; + +return kvm_riscv_vcpu_aia_imsic_inject (vcpu, guest_index,0 , iid); }int kvm_riscv_aia_inject_msi (struct kvm *kvm,struct kvm_msi *msi) {gpa_t tppn, ippn; -unsigned long idx;struct kvm_vcpu *vcpu; -u32 g, toff, iid = msi->data; +u32 g, toff, iid = msi->data, hart_index;struct kvm_aia *aia = &kvm->arch.aia;gpa_t target = (((gpa_t )msi->address_hi) <<32 ) | msi->address_lo;@@ -595,18 +598,16 @@ int kvm_riscv_aia_inject_msi(struct kvm *kvm, struct kvm_msi *msi) g = tppn & (BIT (aia->nr_guest_bits) -1 ); tppn &= ~((gpa_t)(BIT (aia->nr_guest_bits) -1 )); -/* Inject MSI to matching VCPU */ -kvm_for_each_vcpu (idx, vcpu, kvm) { - ippn = vcpu->arch.aia_context.imsic_addr >> -IMSIC_MMIO_PAGE_SHIFT ; -if (ippn == tppn) { - toff = target & (IMSIC_MMIO_PAGE_SZ -1 ); -return kvm_riscv_vcpu_aia_imsic_inject (vcpu, g, - toff, iid); - } - } + hart_index =aia_imsic_hart_index (aia, target); + vcpu =xa_load (&aia->hart_to_vcpu, hart_index); +if (!vcpu) +return 0 ; -return 0 ; + ippn = vcpu->arch.aia_context.imsic_addr >>IMSIC_MMIO_PAGE_SHIFT ; +if (ippn != tppn) +return 0 ; + toff = target & (IMSIC_MMIO_PAGE_SZ -1 ); +return kvm_riscv_vcpu_aia_imsic_inject (vcpu, g, toff, iid); }int kvm_riscv_aia_inject_irq (struct kvm *kvm,unsigned int irq,bool level)@@ -647,9 +648,12 @@ void kvm_riscv_aia_init_vm(struct kvm *kvm)void kvm_riscv_aia_destroy_vm (struct kvm *kvm) { +struct kvm_aia *aia = &kvm->arch.aia;/* Proceed only if AIA was initialized successfully */ if (!kvm_riscv_aia_initialized (kvm))return ; + +xa_destroy (&aia->hart_to_vcpu);/* Cleanup APLIC context */ kvm_riscv_aia_aplic_cleanup (kvm);
Quoted 7 lines · Nutty.Liu
Quoted 5 lines · BillXiang
Replace linear searches over all vCPUs in MSI injection paths with a
hart_index-> vCPU xarray lookup table. This avoids O(n) scans on every injected interrupt and improves performance when the number of vCPUs is large.
Quoted 8 lines
Quoted 5 lines
Both
kvm_riscv_aia_inject_msi_by_id()andkvm_riscv_aia_inject_msi()now use the table instead of iterating over the vCPU list.Signed-off-by BillXiang <[email protected]>Changes in v2:
Hi Bill,
The change log should be under the tear line.
Thanks for the reminder.
Quoted 132 lines
Quoted 132 lines
- Use xarray instead of normal array to handle incontiguous
hart_index.
- Link to v1:
https://lore.kernel.org/kvm-riscv/20260730093614.983508-1-
[email protected]/
---
arch/riscv/include/asm/kvm_aia.h| 3 ++ arch/riscv/kvm/aia_device.c | 50 +++++++++++++++++---------------2 files changed, 30 insertions(+), 23 deletions(-)arch/riscv/include/ renamed from arch/riscv/include/asm/kvm_aia.h +0
-0
asm/kvm_aia.h
indexab03f853e507..3b075f18e1d6100644arch/riscv/include/asm/kvm_aia.h +3
-0@@ -12,6 +12,7 @@ #include <linux/jump_label.h> #include <linux/kvm_types.h> +#include <linux/xarray.h> #include <asm/csr.h> struct kvm_aia {@@ -47,6 +48,8 @@ struct kvm_aia { /* Internal state of APLIC */ void *aplic_state; + + struct xarray hart_to_vcpu; }; struct kvm_vcpu_aia_csr {arch/riscv/kvm/aia_device.c +24
-23@@ -259,6 +259,8 @@ static int aia_init(struct kvm *kvm) if (ret) return ret; + xa_init(&aia->hart_to_vcpu); + /* Iterate over each VCPU */ kvm_for_each_vcpu(idx, vcpu, kvm) { vaia = &vcpu->arch.aia_context;@@ -280,6 +282,11 @@ static int aia_init(struct kvm *kvm) /* Update HART index of the IMSIC based on IMSIC base */ vaia->hart_index = aia_imsic_hart_index(aia, vaia->imsic_addr); + + ret = xa_insert(&aia->hart_to_vcpu, vaia->hart_index, vcpu,GFP_KERNEL ); + if (ret) { + goto fail_cleanup_imsics; + } /* Initialize IMSIC for this VCPU */ ret = kvm_riscv_vcpu_aia_imsic_init(vcpu);@@ -299,6 +306,7 @@ static int aia_init(struct kvm *kvm) continue; kvm_riscv_vcpu_aia_imsic_cleanup(vcpu); } + xa_destroy(&aia->hart_to_vcpu); kvm_riscv_aia_aplic_cleanup(kvm); return ret; }@@ -557,30 +565,25 @@ void kvm_riscv_vcpu_aia_deinit(struct kvm_vcpu *vcpu) int kvm_riscv_aia_inject_msi_by_id(struct kvm *kvm, u32 hart_index, u32 guest_index, u32 iid) { - unsigned long idx; struct kvm_vcpu *vcpu; + struct kvm_aia *aia = &kvm->arch.aia; /* Proceed only if AIA was initialized successfully */ if (!kvm_riscv_aia_initialized(kvm)) return -EBUSY; - /* Inject MSI to matching VCPU */ - kvm_for_each_vcpu(idx, vcpu, kvm) { - if (vcpu->arch.aia_context.hart_index == hart_index) - return kvm_riscv_vcpu_aia_imsic_inject(vcpu, - guest_index, - 0, iid); - } - - return 0; + vcpu = xa_load(&aia->hart_to_vcpu, hart_index); + if (!vcpu) + return 0; + + return kvm_riscv_vcpu_aia_imsic_inject(vcpu, guest_index, 0, iid); } int kvm_riscv_aia_inject_msi(struct kvm *kvm, struct kvm_msi *msi) { gpa_t tppn, ippn; - unsigned long idx; struct kvm_vcpu *vcpu; - u32 g, toff, iid = msi->data; + u32 g, toff, iid = msi->data, hart_index; struct kvm_aia *aia = &kvm->arch.aia; gpa_t target = (((gpa_t)msi->address_hi) << 32) | msi->address_lo;@@ -595,18 +598,16 @@ int kvm_riscv_aia_inject_msi(struct kvm *kvm, struct kvm_msi *msi) g = tppn & (BIT(aia->nr_guest_bits) - 1); tppn &= ~((gpa_t)(BIT(aia->nr_guest_bits) - 1)); - /* Inject MSI to matching VCPU */ - kvm_for_each_vcpu(idx, vcpu, kvm) { - ippn = vcpu->arch.aia_context.imsic_addr >> - IMSIC_MMIO_PAGE_SHIFT; - if (ippn == tppn) { - toff = target & (IMSIC_MMIO_PAGE_SZ - 1); - return kvm_riscv_vcpu_aia_imsic_inject(vcpu, g, - toff, iid); - } - } + hart_index = aia_imsic_hart_index(aia, target); + vcpu = xa_load(&aia->hart_to_vcpu, hart_index); + if (!vcpu) + return 0; - return 0; + ippn = vcpu->arch.aia_context.imsic_addr >> IMSIC_MMIO_PAGE_SHIFT; + if (ippn != tppn) + return 0; + toff = target & (IMSIC_MMIO_PAGE_SZ - 1); + return kvm_riscv_vcpu_aia_imsic_inject(vcpu, g, toff, iid); }int
kvm_riscv_aia_inject_irq(struct kvm *kvm, unsigned int irq, bool level)arch/riscv/kvm/aia_device.c +3
-0@@ -647,9 +648,12 @@ void kvm_riscv_aia_init_vm(struct kvm *kvm) void kvm_riscv_aia_destroy_vm(struct kvm *kvm) { + struct kvm_aia *aia = &kvm->arch.aia; /* Proceed only if AIA was initialized successfully */ if (!kvm_riscv_aia_initialized(kvm)) return; + + xa_destroy(&aia->hart_to_vcpu); /* Cleanup APLIC context */ kvm_riscv_aia_aplic_cleanup(kvm);