RISC-V: KVM: AIA: Initialize per-CPU HGEI free_bitmap only once
Subsystems (3)
PATCH 1/1 RISC-V: KVM: AIA: Initialize per-CPU HGEI free_bitmap only once
kvm_riscv_aia_enable() may run more than once on the same CPU. The per-CPU free_bitmap only needs to be initialized once, from the HGEIE width discovered on that CPU. Perform the HGEIE probe and bitmap setup only on the first call, and set free_bitmap_initialized after the bitmap has been populated.
--- arch/riscv/kvm/aia.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
arch/riscv/kvm/aia.c +6 -4
@@ -610,6 +610,9 @@ void kvm_riscv_aia_enable(void) lc = (gc) ?this_cpu_ptr (gc->local) :NULL ; hgctrl =this_cpu_ptr (&aia_hgei); +if (hgctrl->free_bitmap_initialized) + goto skip_hgctrl_init; +/* Figure-out number of bits in HGEIE */ csr_write (CSR_HGEIE ,-1UL ); hgctrl->nr_hgei =fls_long (csr_read (CSR_HGEIE ));
@@ -634,12 +637,11 @@ void kvm_riscv_aia_enable(void) }while (!atomic_try_cmpxchg (&kvm_riscv_aia_nr_hgei, &aia_nr_hgei, hgctrl->nr_hgei));raw_spin_lock_irqsave (&hgctrl->lock, flags); -if (!hgctrl->free_bitmap_initialized) { - hgctrl->free_bitmap = (hgctrl->nr_hgei) ?GENMASK_ULL (hgctrl->nr_hgei,1 ) :0 ; - hgctrl->free_bitmap_initialized = true; - } + hgctrl->free_bitmap = (hgctrl->nr_hgei) ?GENMASK_ULL (hgctrl->nr_hgei,1 ) :0 ; + hgctrl->free_bitmap_initialized = true;raw_spin_unlock_irqrestore (&hgctrl->lock, flags); +skip_hgctrl_init:csr_write (CSR_HVICTL ,aia_hvictl_value (false));csr_write (CSR_HVIPRIO1 ,0x0 );csr_write (CSR_HVIPRIO2 ,0x0 );
---
base-commit: 93f51579e7df248780214094418f205253383cc5
change-id: 20260924-free_bitmap_initialized-abdc94a528fb
Best regards,
Signature · 1 line
GUO Ren (XuanTie) <[email protected]>
Anup Patel
maintainerQuoted 6 lines · GUO Ren (XuanTie)
kvm_riscv_aia_enable()may run more than once on the same CPU. The per-CPUfree_bitmaponly needs to be initialized once, from the HGEIE width discovered on that CPU. Perform the HGEIE probe and bitmap setup only on the first call, and setfree_bitmap_initializedafter the bitmap has been populated.
The per-CPU free_bitmap for HGEI lines only one unsigned long so I am not sure what you are trying to save by skipping one unsigned long.
Regards,
Anup
Quoted 45 lines
Signed-off-by GUO Ren (XuanTie) <[email protected]>--- arch/riscv/kvm/aia.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)arch/riscv/kvm/aia.c +6
-5@@ -610,6 +610,9 @@ void kvm_riscv_aia_enable(void) lc = (gc) ?this_cpu_ptr (gc->local) :NULL ; hgctrl =this_cpu_ptr (&aia_hgei); +if (hgctrl->free_bitmap_initialized) + goto skip_hgctrl_init; +/* Figure-out number of bits in HGEIE */ csr_write (CSR_HGEIE ,-1UL ); hgctrl->nr_hgei =fls_long (csr_read (CSR_HGEIE ));@@ -634,12 +637,11 @@ void kvm_riscv_aia_enable(void) }while (!atomic_try_cmpxchg (&kvm_riscv_aia_nr_hgei, &aia_nr_hgei, hgctrl->nr_hgei));raw_spin_lock_irqsave (&hgctrl->lock, flags); -if (!hgctrl->free_bitmap_initialized) { - hgctrl->free_bitmap = (hgctrl->nr_hgei) ?GENMASK_ULL (hgctrl->nr_hgei,1 ) :0 ; - hgctrl->free_bitmap_initialized = true; - } + hgctrl->free_bitmap = (hgctrl->nr_hgei) ?GENMASK_ULL (hgctrl->nr_hgei,1 ) :0 ; + hgctrl->free_bitmap_initialized = true;raw_spin_unlock_irqrestore (&hgctrl->lock, flags); +skip_hgctrl_init:csr_write (CSR_HVICTL ,aia_hvictl_value (false));csr_write (CSR_HVIPRIO1 ,0x0 );csr_write (CSR_HVIPRIO2 ,0x0 ); ---base-commit:
93f51579e7df248780214094418f205253383cc5
change-id: 20260924-free_bitmap_initialized-abdc94a528fbBest regards,
--
GUO Ren (XuanTie) <[email protected]>
Hi Anup,
Quoted 12 lines · Anup Patel
Quoted 6 lines · GUO Ren (XuanTie)
kvm_riscv_aia_enable()may run more than once on the same CPU. The per-CPUfree_bitmaponly needs to be initialized once, from the HGEIE width discovered on that CPU. Perform the HGEIE probe and bitmap setup only on the first call, and setfree_bitmap_initializedafter the bitmap has been populated.The per-CPU
free_bitmapfor HGEI lines only one unsigned long so I am not sure what you are trying to save by skipping one unsigned long.
Thanks for the review. To clarify, the early-exit skips the entire block below — not just the bitmap assignment:
---
/* Figure-out number of bits in HGEIE */ csr_write(CSR_HGEIE, -1UL);hgctrl->nr_hgei = fls_long(csr_read(CSR_HGEIE)); csr_write(CSR_HGEIE, 0);
if (hgctrl->nr_hgei)
hgctrl->nr_hgei--;
if (lc)
hgctrl->nr_hgei = min((ulong)hgctrl->nr_hgei, lc->nr_guest_files);
else
hgctrl->nr_hgei = 0;aia_nr_hgei = atomic_read(&kvm_riscv_aia_nr_hgei);
do {
if (aia_nr_hgei <= hgctrl->nr_hgei)
break;
} while (!atomic_try_cmpxchg(&kvm_riscv_aia_nr_hgei,
&aia_nr_hgei, hgctrl->nr_hgei));raw_spin_lock_irqsave(&hgctrl->lock, flags);
if (!hgctrl->free_bitmap_initialized) {
hgctrl->free_bitmap = (hgctrl->nr_hgei) ?
GENMASK_ULL(hgctrl->nr_hgei, 1) : 0;
hgctrl->free_bitmap_initialized = true;
}raw_spin_unlock_irqrestore(&hgctrl->lock, flags);
---
Once a hart has been initialized, nr_hgei is a hardware property that won't change. Re-probing CSR_HGEIE, re-running the atomic cmpxchg loop, and re-acquiring the irqsave spinlock on every subsequent kvm_riscv_aia_enable() is pure overhead.
The free_bitmap_initialized flag in the original code already anticipated repeated calls. This patch simply hoists that check to skip the redundant probe entirely.
Signature · 2 lines
Best Regards Guo Ren