Skip to contentSkip to conversation

LoongArch: KVM: simplify KVM routines

PATCH Applied Yury Norov 3 participants 8 messages Jul 16, 2025 – Jul 20, 2025

Subsystems (3)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

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
author
#1 lore raw

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
index fa52251b3bf1..359afa909cee 100644
@@ -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
maintainer Reviewed-by on v1 1/2
#2 lore raw

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.

---
  arch/loongarch/kvm/exit.c | 31 ++++++++++++-------------------
  1 file changed, 12 insertions(+), 19 deletions(-)
arch/loongarch/kvm/exit.c +9 -16
index fa52251b3bf1..359afa909cee 100644
@@ -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;
  }
 
  /*
author
#3 lore raw

PATCH 2/2 LoongArch: KVM:: simplify kvm_deliver_intr()

In reply to 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.

---
 arch/loongarch/kvm/interrupt.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
arch/loongarch/kvm/interrupt.c +4 -21
index 4c3f22de4b40..8462083f0301 100644
@@ -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
maintainer Reviewed-by on v1 2/2
#4 lore raw

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.

---
  arch/loongarch/kvm/interrupt.c | 25 ++++---------------------
  1 file changed, 4 insertions(+), 21 deletions(-)
arch/loongarch/kvm/interrupt.c +2 -16
index 4c3f22de4b40..8462083f0301 100644
@@ -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

maintainer
#5 lore raw

Re: [PATCH 2/2] LoongArch: KVM:: simplify kvm_deliver_intr()

In reply to Yury Norov

Hi, 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.

---
 arch/loongarch/kvm/interrupt.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
arch/loongarch/kvm/interrupt.c +0 -0
index 4c3f22de4b40..8462083f0301 100644

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

author
#6 lore raw

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.

---
 arch/loongarch/kvm/interrupt.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
arch/loongarch/kvm/interrupt.c +0 -0
index 4c3f22de4b40..8462083f0301 100644
@@ -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

maintainer Applied
#7 lore raw
In reply to Yury Norov

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