LoongArch: KVM: simplify KVM routines
Subsystems (3)
cover LoongArch: KVM: simplify KVM routines
From: Yury Norov (NVIDIA) <[email protected]>
Switch KVM functions to use a proper bitmaps API.
Yury Norov (NVIDIA) (2):
LoongArch: KVM: rework kvm_send_pv_ipi()
LoongArch: KVM:: simplify kvm_deliver_intr()
arch/loongarch/kvm/exit.c | 31 ++++++++++++------------------- arch/loongarch/kvm/interrupt.c | 23 +++-------------------- 2 files changed, 15 insertions(+), 39 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 LoongArch: KVM: rework kvm_send_pv_ipi()
From: "Yury Norov (NVIDIA)" <[email protected]>
The function in fact traverses a "bitmap" stored in GPR regs A1 and A2, but does it in a non-obvious way by creating a single-word bitmap twice.
This patch switches the function to create a single 2-word bitmap, and also employs for_each_set_bit() macro, as it helps to drop most of housekeeping code.
While there, convert the function to return void to not confuse readers with unchecked result.
--- arch/loongarch/kvm/exit.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-)
arch/loongarch/kvm/exit.c +12 -19
@@ -821,32 +821,25 @@ static int kvm_handle_lbt_disabled(struct kvm_vcpu *vcpu, int ecode)return RESUME_GUEST ; } -static int kvm_send_pv_ipi (struct kvm_vcpu *vcpu) +static void kvm_send_pv_ipi (struct kvm_vcpu *vcpu) { -unsigned int min, cpu, i; -unsigned long ipi_bitmap; + DECLARE_BITMAP(ipi_bitmap, BITS_PER_LONG * 2) = { +kvm_read_reg (vcpu,LOONGARCH_GPR_A1 ), +kvm_read_reg (vcpu,LOONGARCH_GPR_A2 ) + }; +unsigned int min, cpu;struct kvm_vcpu *dest; min =kvm_read_reg (vcpu,LOONGARCH_GPR_A3 ); -for (i =0 ; i <2 ; i++, min +=BITS_PER_LONG ) { - ipi_bitmap =kvm_read_reg (vcpu,LOONGARCH_GPR_A1 + i); -if (!ipi_bitmap) +for_each_set_bit (cpu, ipi_bitmap,BITS_PER_LONG *2 ) { + dest =kvm_get_vcpu_by_cpuid (vcpu->kvm, cpu + min); +if (!dest)continue ; - cpu =find_first_bit ((void *)&ipi_bitmap,BITS_PER_LONG ); -while (cpu <BITS_PER_LONG ) { - dest =kvm_get_vcpu_by_cpuid (vcpu->kvm, cpu + min); - cpu =find_next_bit ((void *)&ipi_bitmap,BITS_PER_LONG , cpu +1 ); -if (!dest) -continue ; - -/* Send SWI0 to dest vcpu to emulate IPI interrupt */ -kvm_queue_irq (dest,INT_SWI0 ); -kvm_vcpu_kick (dest); - } +/* Send SWI0 to dest vcpu to emulate IPI interrupt */ +kvm_queue_irq (dest,INT_SWI0 ); +kvm_vcpu_kick (dest); } - -return 0 ; }/*
Signature · 1 line
2.43.0
Re: [PATCH 1/2] LoongArch: KVM: rework kvm_send_pv_ipi()
Quoted 57 lines · Yury Norov
From: "Yury Norov (NVIDIA)" <[email protected]>
The function in fact traverses a "bitmap" stored in GPR regs A1 and A2, but does it in a non-obvious way by creating a single-word bitmap twice.
This patch switches the function to create a single 2-word bitmap, and also employs
for_each_set_bit()macro, as it helps to drop most of housekeeping code.While there, convert the function to return void to not confuse readers with unchecked result.
Signed-off-by Yury Norov (NVIDIA) <[email protected]>--- arch/loongarch/kvm/exit.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-)arch/loongarch/kvm/exit.c +9
-16@@ -821,32 +821,25 @@ static int kvm_handle_lbt_disabled(struct kvm_vcpu *vcpu, int ecode)return RESUME_GUEST ; } -static int kvm_send_pv_ipi (struct kvm_vcpu *vcpu) +static void kvm_send_pv_ipi (struct kvm_vcpu *vcpu) { -unsigned int min, cpu, i; -unsigned long ipi_bitmap; + DECLARE_BITMAP(ipi_bitmap, BITS_PER_LONG * 2) = { +kvm_read_reg (vcpu,LOONGARCH_GPR_A1 ), +kvm_read_reg (vcpu,LOONGARCH_GPR_A2 ) + }; +unsigned int min, cpu;struct kvm_vcpu *dest; min =kvm_read_reg (vcpu,LOONGARCH_GPR_A3 ); -for (i =0 ; i <2 ; i++, min +=BITS_PER_LONG ) { - ipi_bitmap =kvm_read_reg (vcpu,LOONGARCH_GPR_A1 + i); -if (!ipi_bitmap) +for_each_set_bit (cpu, ipi_bitmap,BITS_PER_LONG *2 ) { + dest =kvm_get_vcpu_by_cpuid (vcpu->kvm, cpu + min); +if (!dest)continue ; - cpu =find_first_bit ((void *)&ipi_bitmap,BITS_PER_LONG ); -while (cpu <BITS_PER_LONG ) { - dest =kvm_get_vcpu_by_cpuid (vcpu->kvm, cpu + min); - cpu =find_next_bit ((void *)&ipi_bitmap,BITS_PER_LONG , cpu +1 ); -if (!dest) -continue ; - -/* Send SWI0 to dest vcpu to emulate IPI interrupt */ -kvm_queue_irq (dest,INT_SWI0 ); -kvm_vcpu_kick (dest);
arch/loongarch/kvm/exit.c static int kvm_handle_lbt_disabled(struct kvm_vcpu *vcpu, int ecode) · line 845
Quoted 10 lines
arch/loongarch/kvm/exit.c +3
-3- } +/* Send SWI0 to dest vcpu to emulate IPI interrupt */ +kvm_queue_irq (dest,INT_SWI0 ); +kvm_vcpu_kick (dest); } - -return 0 ; }/*
PATCH 2/2 LoongArch: KVM:: simplify kvm_deliver_intr()
In reply to Yury NorovFrom: "Yury Norov (NVIDIA)" <[email protected]>
The function opencodes for_each_set_bit() macro, which makes it bulky. Using the proper API makes all the housekeeping code going away.
--- arch/loongarch/kvm/interrupt.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-)
arch/loongarch/kvm/interrupt.c +4 -21
@@ -83,28 +83,11 @@ void kvm_deliver_intr(struct kvm_vcpu *vcpu)unsigned long *pending = &vcpu->arch.irq_pending;unsigned long *pending_clr = &vcpu->arch.irq_clear; -if (!(*pending) && !(*pending_clr)) -return ; - -if (*pending_clr) { - priority =__ffs (*pending_clr); -while (priority <=INT_IPI ) { -kvm_irq_clear (vcpu, priority); - priority =find_next_bit (pending_clr, -BITS_PER_BYTE *sizeof (*pending_clr), - priority +1 ); - } - } +for_each_set_bit (priority, pending_clr,INT_IPI +1 ) +kvm_irq_clear (vcpu, priority); -if (*pending) { - priority =__ffs (*pending); -while (priority <=INT_IPI ) { -kvm_irq_deliver (vcpu, priority); - priority =find_next_bit (pending, -BITS_PER_BYTE *sizeof (*pending), - priority +1 ); - } - } +for_each_set_bit (priority, pending,INT_IPI +1 ) +kvm_irq_deliver (vcpu, priority); }int kvm_pending_timer (struct kvm_vcpu *vcpu)
Signature · 1 line
2.43.0
Re: [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr()
Quoted 38 lines · Yury Norov
From: "Yury Norov (NVIDIA)" <[email protected]>
The function opencodes
for_each_set_bit()macro, which makes it bulky. Using the proper API makes all the housekeeping code going away.Signed-off-by Yury Norov (NVIDIA) <[email protected]>--- arch/loongarch/kvm/interrupt.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-)arch/loongarch/kvm/interrupt.c +2
-16@@ -83,28 +83,11 @@ void kvm_deliver_intr(struct kvm_vcpu *vcpu)unsigned long *pending = &vcpu->arch.irq_pending;unsigned long *pending_clr = &vcpu->arch.irq_clear; -if (!(*pending) && !(*pending_clr)) -return ; - -if (*pending_clr) { - priority =__ffs (*pending_clr); -while (priority <=INT_IPI ) { -kvm_irq_clear (vcpu, priority); - priority =find_next_bit (pending_clr, -BITS_PER_BYTE *sizeof (*pending_clr), - priority +1 ); - } - } +for_each_set_bit (priority, pending_clr,INT_IPI +1 ) +kvm_irq_clear (vcpu, priority); -if (*pending) { - priority =__ffs (*pending); -while (priority <=INT_IPI ) { -kvm_irq_deliver (vcpu, priority);
arch/loongarch/kvm/interrupt.c void kvm_deliver_intr(struct kvm_vcpu *vcpu) · line 93
Quoted 10 lines
arch/loongarch/kvm/interrupt.c +2
-5- priority =find_next_bit (pending, -BITS_PER_BYTE *sizeof (*pending), - priority +1 ); - } - } +for_each_set_bit (priority, pending,INT_IPI +1 ) +kvm_irq_deliver (vcpu, priority); }int kvm_pending_timer (struct kvm_vcpu *vcpu)
Hi Yury,
Thanks for your patch. And it looks good to me.
Regards
Bibo Mao
Huacai Chen
maintainerRe: [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr()
In reply to Yury NorovHi, Yury,
Quoted 16 lines · Yury Norov
From: "Yury Norov (NVIDIA)" <[email protected]>
The function opencodes
for_each_set_bit()macro, which makes it bulky. Using the proper API makes all the housekeeping code going away.Signed-off-by Yury Norov (NVIDIA) <[email protected]>--- arch/loongarch/kvm/interrupt.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-)arch/loongarch/kvm/interrupt.c +0
-0
arch/loongarch/kvm/interrupt.c void kvm_deliver_intr(struct kvm_vcpu *vcpu) · line 87
Quoted 5 lines
arch/loongarch/kvm/interrupt.c +0
-2@@ -83,28 +83,11 @@ void kvm_deliver_intr(struct kvm_vcpu *vcpu)unsigned long *pending = &vcpu->arch.irq_pending;unsigned long *pending_clr = &vcpu->arch.irq_clear; -if (!(*pending) && !(*pending_clr)) -return ;
Is it necessary to keep these two lines?
Huacai
Quoted 31 lines
arch/loongarch/kvm/interrupt.c +4
-20- -if (*pending_clr) { - priority =__ffs (*pending_clr); -while (priority <=INT_IPI ) { -kvm_irq_clear (vcpu, priority); - priority =find_next_bit (pending_clr, -BITS_PER_BYTE *sizeof (*pending_clr), - priority +1 ); - } - } +for_each_set_bit (priority, pending_clr,INT_IPI +1 ) +kvm_irq_clear (vcpu, priority); -if (*pending) { - priority =__ffs (*pending); -while (priority <=INT_IPI ) { -kvm_irq_deliver (vcpu, priority); - priority =find_next_bit (pending, -BITS_PER_BYTE *sizeof (*pending), - priority +1 ); - } - } +for_each_set_bit (priority, pending,INT_IPI +1 ) +kvm_irq_deliver (vcpu, priority); }int kvm_pending_timer (struct kvm_vcpu *vcpu) --2.43.0
Re: [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr()
Quoted 22 lines · Huacai Chen
Hi, Yury,
Quoted 19 lines · Yury Norov
From: "Yury Norov (NVIDIA)" <[email protected]>
The function opencodes
for_each_set_bit()macro, which makes it bulky. Using the proper API makes all the housekeeping code going away.Signed-off-by Yury Norov (NVIDIA) <[email protected]>--- arch/loongarch/kvm/interrupt.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-)arch/loongarch/kvm/interrupt.c +0
-0@@ -83,28 +83,11 @@ void kvm_deliver_intr(struct kvm_vcpu *vcpu)unsigned long *pending = &vcpu->arch.irq_pending;unsigned long *pending_clr = &vcpu->arch.irq_clear;
Quoted 3 lines
arch/loongarch/kvm/interrupt.c void kvm_deliver_intr(struct kvm_vcpu *vcpu) · line 87
Quoted 2 lines
arch/loongarch/kvm/interrupt.c +0
-2-if (!(*pending) && !(*pending_clr)) -return ;Is it necessary to keep these two lines?
No. They duplicate the existing logic, and the new one based on for_each_set_bit(). That's why I remove them.
Thanks,
Yury
Quoted 31 lines
Quoted 31 lines
arch/loongarch/kvm/interrupt.c +4
-20- -if (*pending_clr) { - priority =__ffs (*pending_clr); -while (priority <=INT_IPI ) { -kvm_irq_clear (vcpu, priority); - priority =find_next_bit (pending_clr, -BITS_PER_BYTE *sizeof (*pending_clr), - priority +1 ); - } - } +for_each_set_bit (priority, pending_clr,INT_IPI +1 ) +kvm_irq_clear (vcpu, priority); -if (*pending) { - priority =__ffs (*pending); -while (priority <=INT_IPI ) { -kvm_irq_deliver (vcpu, priority); - priority =find_next_bit (pending, -BITS_PER_BYTE *sizeof (*pending), - priority +1 ); - } - } +for_each_set_bit (priority, pending,INT_IPI +1 ) +kvm_irq_deliver (vcpu, priority); }int kvm_pending_timer (struct kvm_vcpu *vcpu) --2.43.0
Applied, thanks.
Huacai
Quoted 16 lines · Yury Norov
From: Yury Norov (NVIDIA) <[email protected]>
Switch KVM functions to use a proper bitmaps API.
Yury Norov (NVIDIA) (2):
LoongArch: KVM: reworkkvm_send_pv_ipi()
LoongArch: KVM:: simplifykvm_deliver_intr()arch/loongarch/kvm/exit.c | 31 ++++++++++++------------------- arch/loongarch/kvm/interrupt.c | 23 +++-------------------- 2 files changed, 15 insertions(+), 39 deletions(-)--
2.43.0