kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] LoongArch: KVM: simplify KVM routines
@ 2025-07-16 16:59 Yury Norov
  2025-07-16 16:59 ` [PATCH 1/2] LoongArch: KVM: rework kvm_send_pv_ipi() Yury Norov
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Yury Norov @ 2025-07-16 16:59 UTC (permalink / raw)
  To: Tianrui Zhao, Bibo Mao, Huacai Chen, WANG Xuerui, kvm, loongarch,
	linux-kernel
  Cc: Yury Norov

From: Yury Norov (NVIDIA) <yury.norov@gmail.com>

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(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/2] LoongArch: KVM: rework kvm_send_pv_ipi()
  2025-07-16 16:59 [PATCH 0/2] LoongArch: KVM: simplify KVM routines Yury Norov
@ 2025-07-16 16:59 ` Yury Norov
  2025-07-18  2:25   ` Bibo Mao
  2025-07-16 16:59 ` [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr() Yury Norov
  2025-07-20  8:50 ` [PATCH 0/2] LoongArch: KVM: simplify KVM routines Huacai Chen
  2 siblings, 1 reply; 8+ messages in thread
From: Yury Norov @ 2025-07-16 16:59 UTC (permalink / raw)
  To: Tianrui Zhao, Bibo Mao, Huacai Chen, WANG Xuerui, kvm, loongarch,
	linux-kernel
  Cc: Yury Norov

From: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>

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) <yury.norov@gmail.com>
---
 arch/loongarch/kvm/exit.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)

diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c
index fa52251b3bf1..359afa909cee 100644
--- a/arch/loongarch/kvm/exit.c
+++ b/arch/loongarch/kvm/exit.c
@@ -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;
 }
 
 /*
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr()
  2025-07-16 16:59 [PATCH 0/2] LoongArch: KVM: simplify KVM routines Yury Norov
  2025-07-16 16:59 ` [PATCH 1/2] LoongArch: KVM: rework kvm_send_pv_ipi() Yury Norov
@ 2025-07-16 16:59 ` Yury Norov
  2025-07-18  2:37   ` Bibo Mao
  2025-07-18  4:13   ` Huacai Chen
  2025-07-20  8:50 ` [PATCH 0/2] LoongArch: KVM: simplify KVM routines Huacai Chen
  2 siblings, 2 replies; 8+ messages in thread
From: Yury Norov @ 2025-07-16 16:59 UTC (permalink / raw)
  To: Tianrui Zhao, Bibo Mao, Huacai Chen, WANG Xuerui, kvm, loongarch,
	linux-kernel
  Cc: Yury Norov

From: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>

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) <yury.norov@gmail.com>
---
 arch/loongarch/kvm/interrupt.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)

diff --git a/arch/loongarch/kvm/interrupt.c b/arch/loongarch/kvm/interrupt.c
index 4c3f22de4b40..8462083f0301 100644
--- a/arch/loongarch/kvm/interrupt.c
+++ b/arch/loongarch/kvm/interrupt.c
@@ -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)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] LoongArch: KVM: rework kvm_send_pv_ipi()
  2025-07-16 16:59 ` [PATCH 1/2] LoongArch: KVM: rework kvm_send_pv_ipi() Yury Norov
@ 2025-07-18  2:25   ` Bibo Mao
  0 siblings, 0 replies; 8+ messages in thread
From: Bibo Mao @ 2025-07-18  2:25 UTC (permalink / raw)
  To: Yury Norov, Tianrui Zhao, Huacai Chen, WANG Xuerui, kvm,
	loongarch, linux-kernel



On 2025/7/17 上午12:59, Yury Norov wrote:
> From: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>
> 
> 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) <yury.norov@gmail.com>
> ---
>   arch/loongarch/kvm/exit.c | 31 ++++++++++++-------------------
>   1 file changed, 12 insertions(+), 19 deletions(-)
> 
> diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c
> index fa52251b3bf1..359afa909cee 100644
> --- a/arch/loongarch/kvm/exit.c
> +++ b/arch/loongarch/kvm/exit.c
> @@ -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;
>   }
>   
>   /*
> 
Reviewed-by: Bibo Mao <maobibo@loongson.cn>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr()
  2025-07-16 16:59 ` [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr() Yury Norov
@ 2025-07-18  2:37   ` Bibo Mao
  2025-07-18  4:13   ` Huacai Chen
  1 sibling, 0 replies; 8+ messages in thread
From: Bibo Mao @ 2025-07-18  2:37 UTC (permalink / raw)
  To: Yury Norov, Tianrui Zhao, Huacai Chen, WANG Xuerui, kvm,
	loongarch, linux-kernel



On 2025/7/17 上午12:59, Yury Norov wrote:
> From: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>
> 
> 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) <yury.norov@gmail.com>
> ---
>   arch/loongarch/kvm/interrupt.c | 25 ++++---------------------
>   1 file changed, 4 insertions(+), 21 deletions(-)
> 
> diff --git a/arch/loongarch/kvm/interrupt.c b/arch/loongarch/kvm/interrupt.c
> index 4c3f22de4b40..8462083f0301 100644
> --- a/arch/loongarch/kvm/interrupt.c
> +++ b/arch/loongarch/kvm/interrupt.c
> @@ -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)
> 
Hi Yury,

Thanks for your patch. And it looks good to me.

Reviewed-by: Bibo Mao <maobibo@loongson.cn>

Regards
Bibo Mao


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr()
  2025-07-16 16:59 ` [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr() Yury Norov
  2025-07-18  2:37   ` Bibo Mao
@ 2025-07-18  4:13   ` Huacai Chen
  2025-07-18 13:24     ` Yury Norov
  1 sibling, 1 reply; 8+ messages in thread
From: Huacai Chen @ 2025-07-18  4:13 UTC (permalink / raw)
  To: Yury Norov
  Cc: Tianrui Zhao, Bibo Mao, WANG Xuerui, kvm, loongarch, linux-kernel

Hi, Yury,

On Thu, Jul 17, 2025 at 12:59 AM Yury Norov <yury.norov@gmail.com> wrote:
>
> From: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>
>
> 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) <yury.norov@gmail.com>
> ---
>  arch/loongarch/kvm/interrupt.c | 25 ++++---------------------
>  1 file changed, 4 insertions(+), 21 deletions(-)
>
> diff --git a/arch/loongarch/kvm/interrupt.c b/arch/loongarch/kvm/interrupt.c
> index 4c3f22de4b40..8462083f0301 100644
> --- a/arch/loongarch/kvm/interrupt.c
> +++ b/arch/loongarch/kvm/interrupt.c
> @@ -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

> -
> -       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
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr()
  2025-07-18  4:13   ` Huacai Chen
@ 2025-07-18 13:24     ` Yury Norov
  0 siblings, 0 replies; 8+ messages in thread
From: Yury Norov @ 2025-07-18 13:24 UTC (permalink / raw)
  To: Huacai Chen
  Cc: Tianrui Zhao, Bibo Mao, WANG Xuerui, kvm, loongarch, linux-kernel

On Fri, Jul 18, 2025 at 12:13:46PM +0800, Huacai Chen wrote:
> Hi, Yury,
> 
> On Thu, Jul 17, 2025 at 12:59 AM Yury Norov <yury.norov@gmail.com> wrote:
> >
> > From: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>
> >
> > 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) <yury.norov@gmail.com>
> > ---
> >  arch/loongarch/kvm/interrupt.c | 25 ++++---------------------
> >  1 file changed, 4 insertions(+), 21 deletions(-)
> >
> > diff --git a/arch/loongarch/kvm/interrupt.c b/arch/loongarch/kvm/interrupt.c
> > index 4c3f22de4b40..8462083f0301 100644
> > --- a/arch/loongarch/kvm/interrupt.c
> > +++ b/arch/loongarch/kvm/interrupt.c
> > @@ -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?

No. They duplicate the existing logic, and the new one based on
for_each_set_bit(). That's why I remove them.

Thanks,
Yury

> > -
> > -       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
> >
> >

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 0/2] LoongArch: KVM: simplify KVM routines
  2025-07-16 16:59 [PATCH 0/2] LoongArch: KVM: simplify KVM routines Yury Norov
  2025-07-16 16:59 ` [PATCH 1/2] LoongArch: KVM: rework kvm_send_pv_ipi() Yury Norov
  2025-07-16 16:59 ` [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr() Yury Norov
@ 2025-07-20  8:50 ` Huacai Chen
  2 siblings, 0 replies; 8+ messages in thread
From: Huacai Chen @ 2025-07-20  8:50 UTC (permalink / raw)
  To: Yury Norov
  Cc: Tianrui Zhao, Bibo Mao, WANG Xuerui, kvm, loongarch, linux-kernel

Applied, thanks.

Huacai

On Thu, Jul 17, 2025 at 12:59 AM Yury Norov <yury.norov@gmail.com> wrote:
>
> From: Yury Norov (NVIDIA) <yury.norov@gmail.com>
>
> 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(-)
>
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-07-20  8:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-16 16:59 [PATCH 0/2] LoongArch: KVM: simplify KVM routines Yury Norov
2025-07-16 16:59 ` [PATCH 1/2] LoongArch: KVM: rework kvm_send_pv_ipi() Yury Norov
2025-07-18  2:25   ` Bibo Mao
2025-07-16 16:59 ` [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr() Yury Norov
2025-07-18  2:37   ` Bibo Mao
2025-07-18  4:13   ` Huacai Chen
2025-07-18 13:24     ` Yury Norov
2025-07-20  8:50 ` [PATCH 0/2] LoongArch: KVM: simplify KVM routines Huacai Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).