mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "BillXiang" <xiangwencheng@lanxincomputing.com>
To: "Anup Patel" <anup@brainfault.org>
Cc: <atish.patra@linux.dev>, <pjw@kernel.org>, <palmer@dabbelt.com>,
	 <aou@eecs.berkeley.edu>, <alex@ghiti.fr>, <kvm@vger.kernel.org>,
	 <kvm-riscv@lists.infradead.org>,
	<linux-riscv@lists.infradead.org>,
	 <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] riscv: KVM: Add hart-to-vCPU mapping for faster MSI injection
Date: Thu, 8 Oct 2026 14:41:39 +0800	[thread overview]
Message-ID: <3c62dcd4-013f-4de1-93ee-dc74909060df@lanxincomputing.com> (raw)
In-Reply-To: <CAAhSdy0UgBGd2M=Zq3XiaTY-Y5uTrAcOtFF=haKgmFBRB1=ggw@mail.gmail.com>

On 10/2/2026 2:53 PM, Anup Patel wrote:
> On Thu, Jul 30, 2026 at 3:06 PM BillXiang
> <xiangwencheng@lanxincomputing.com> wrote:
>>
>> Replace linear searches over all vCPUs in MSI injection paths with a
>> direct hart_index -> vCPU 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.
> 
> The AIA hart_index bits for VCPUs are based on the IMSIC address
> set by the KVM user-space and are not required to be contiguous hence
> this patch is already broken.

Hi Anup,

Thanks for the review. You’re right — hart_index is not guaranteed 
contiguous because it comes from the userspace-provided IMSIC address. 
Sizing the table by created_vcpus and bounds-checking against it is 
therefore wrong.

I'll use an xarray keyed by hart_index instead, or drop the optimization 
if that’s preferred.

Regards,
Bill

> 
> NACK from my side.
> 
> Regards,
> Anup
> 
>>
>> Signed-off-by: BillXiang <xiangwencheng@lanxincomputing.com>
>> ---
>>   arch/riscv/include/asm/kvm_aia.h |  2 +
>>   arch/riscv/kvm/aia_device.c      | 67 +++++++++++++++++++++-----------
>>   2 files changed, 47 insertions(+), 22 deletions(-)
>>
>> diff --git a/arch/riscv/include/asm/kvm_aia.h b/arch/riscv/include/asm/kvm_aia.h
>> index c67ec5ac0..71240b0a2 100644
>> --- a/arch/riscv/include/asm/kvm_aia.h
>> +++ b/arch/riscv/include/asm/kvm_aia.h
>> @@ -47,6 +47,8 @@ struct kvm_aia {
>>
>>          /* Internal state of APLIC */
>>          void            *aplic_state;
>> +
>> +       struct kvm_vcpu **hart_to_vcpu;
>>   };
>>
>>   struct kvm_vcpu_aia_csr {
>> diff --git a/arch/riscv/kvm/aia_device.c b/arch/riscv/kvm/aia_device.c
>> index be83c2d5f..595553817 100644
>> --- a/arch/riscv/kvm/aia_device.c
>> +++ b/arch/riscv/kvm/aia_device.c
>> @@ -253,6 +253,14 @@ static int aia_init(struct kvm *kvm)
>>          if (ret)
>>                  return ret;
>>
>> +       aia->hart_to_vcpu = kcalloc(kvm->created_vcpus,
>> +                                       sizeof(struct kvm_vcpu*),
>> +                                       GFP_KERNEL);
>> +       if (!aia->hart_to_vcpu) {
>> +               ret = -ENOMEM;
>> +               goto fail_cleanup_aplic;
>> +       }
>> +
>>          /* Iterate over each VCPU */
>>          kvm_for_each_vcpu(idx, vcpu, kvm) {
>>                  vaia = &vcpu->arch.aia_context;
>> @@ -274,6 +282,12 @@ 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);
>> +
>> +               if (aia->hart_to_vcpu[vaia->hart_index]) {
>> +                       ret = -EINVAL;
>> +                       goto fail_cleanup_imsics;
>> +               }
>> +               aia->hart_to_vcpu[vaia->hart_index] = vcpu;
>>
>>                  /* Initialize IMSIC for this VCPU */
>>                  ret = kvm_riscv_vcpu_aia_imsic_init(vcpu);
>> @@ -293,6 +307,9 @@ static int aia_init(struct kvm *kvm)
>>                          continue;
>>                  kvm_riscv_vcpu_aia_imsic_cleanup(vcpu);
>>          }
>> +       kfree(aia->hart_to_vcpu);
>> +       aia->hart_to_vcpu = NULL;
>> +fail_cleanup_aplic:
>>          kvm_riscv_aia_aplic_cleanup(kvm);
>>          return ret;
>>   }
>> @@ -551,30 +568,28 @@ 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);
>> -       }
>> +       if (!aia->hart_to_vcpu || hart_index >= kvm->created_vcpus)
>> +               return 0;
>>
>> -       return 0;
>> +       vcpu = 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;
>>
>> @@ -589,18 +604,22 @@ 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);
>> -               }
>> -       }
>> +       if (!aia->hart_to_vcpu)
>> +               return 0;
>>
>> -       return 0;
>> +       hart_index = aia_imsic_hart_index(aia, target);
>> +       if(hart_index >= kvm->created_vcpus)
>> +               return 0;
>> +
>> +       vcpu = aia->hart_to_vcpu[hart_index];
>> +       if (!vcpu)
>> +               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)
>> @@ -641,10 +660,14 @@ 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;
>>
>> +       kfree(aia->hart_to_vcpu);
>> +       aia->hart_to_vcpu = NULL;
>> +
>>          /* Cleanup APLIC context */
>>          kvm_riscv_aia_aplic_cleanup(kvm);
>>   }
>> --
>> 2.53.0

      reply	other threads:[~2026-10-08  6:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-30  9:36 BillXiang
2026-10-02  6:53 ` Anup Patel
2026-10-08  6:41   ` BillXiang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3c62dcd4-013f-4de1-93ee-dc74909060df@lanxincomputing.com \
    --to=xiangwencheng@lanxincomputing.com \
    --cc=alex@ghiti.fr \
    --cc=anup@brainfault.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=atish.patra@linux.dev \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®