Skip to contentSkip to conversation

RISC-V: KVM: remove a redundant condition in kvm_arch_vcpu_ioctl_run()

PATCH In review Chao Du 3 participants 4 messages Dec 11, 2023 – Dec 13, 2023

Subsystems (3)
lore
Share & tools
lore

Chao Du

author
#0 lore raw

PATCH 1/1 RISC-V: KVM: remove a redundant condition in kvm_arch_vcpu_ioctl_run()

The latest ret value is updated by kvm_riscv_vcpu_aia_update(), the loop will continue if the ret is less than or equal to zero. So the later condition will never hit. Thus remove it.

---
 arch/riscv/kvm/vcpu.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/riscv/kvm/vcpu.c +1 -2
index e087c809073c..bf3952d1a621 100644
@@ -757,8 +757,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 		/* Update HVIP CSR for current CPU */
 		kvm_riscv_update_hvip(vcpu);
 
-		if (ret <= 0 ||
-		    kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) ||
+		if (kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) ||
 		    kvm_request_pending(vcpu) ||
 		    xfer_to_guest_mode_work_pending()) {
 			vcpu->mode = OUTSIDE_GUEST_MODE;
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Chao Du

The latest ret value is updated by kvm_riscv_vcpu_aia_update(), the loop will continue if the ret is less than or equal to zero. So the later condition will never hit. Thus remove it.

Good catch. There's another potential optimization to be done a little above this change:

	while (ret > 0) {
		(...)

		/* Update AIA HW state before entering guest */
		ret = kvm_riscv_vcpu_aia_update(vcpu);
		if (ret <= 0) {
			preempt_enable();
			continue; <------------------
		}
		(...)

Note that this 'continue' isn't doing much - we'll restart the loop with ret <= 0 while requiring ret > 0 to do another iteration. I.e. this 'continue' can be replaced for 'break' without compromising the logic.

(of course, testing it to be sure is always advised :D )

Quoted 3 lines

---

Quoted 17 lines
  arch/riscv/kvm/vcpu.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
arch/riscv/kvm/vcpu.c +1 -2
index e087c809073c..bf3952d1a621 100644
@@ -757,8 +757,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
  		/* Update HVIP CSR for current CPU */
  		kvm_riscv_update_hvip(vcpu);
 
-		if (ret <= 0 ||
-		    kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) ||
+		if (kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) ||
  		    kvm_request_pending(vcpu) ||
  		    xfer_to_guest_mode_work_pending()) {
  			vcpu->mode = OUTSIDE_GUEST_MODE;
author
#2 lore raw
Quoted 25 lines

-----Original Messages-----From:"Daniel Henrique Barboza" <[email protected]>Sent Time:2023-12-11 20:06:33 (Monday)To:"Chao Du" <[email protected]>, kvm at vger.kernel.org, kvm-riscv at lists.infradead.org, anup at brainfault.org, atishp at atishpatra.orgCc:Subject:Re: [PATCH] RISC-V: KVM: remove a redundant condition in kvm_arch_vcpu_ioctl_run()

Quoted 3 lines · Chao Du

The latest ret value is updated by kvm_riscv_vcpu_aia_update(), the loop will continue if the ret is less than or equal to zero. So the later condition will never hit. Thus remove it.

Good catch. There's another potential optimization to be done a little above this change:

	while (ret > 0) {
		(...)

		/* Update AIA HW state before entering guest */
		ret = kvm_riscv_vcpu_aia_update(vcpu);
		if (ret <= 0) {
			preempt_enable();
			continue; <------------------
		}
		(...)
Quoted 3 lines

Note that this 'continue' isn't doing much - we'll restart the loop with ret <= 0 while requiring ret > 0 to do another iteration. I.e. this 'continue' can be replaced for 'break' without compromising the logic.

Yes, you are right. The 'continue' could be replaced by a 'break'. I also find another small point:

		ret = 1;

		(...)

		/* Update AIA HW state before entering guest */
		ret = kvm_riscv_vcpu_aia_update(vcpu);

the assignment 'ret = 1' is somehow redundant, since it will be overwritten before any reference.

Quoted 28 lines

(of course, testing it to be sure is always advised :D )

Quoted 3 lines

---

Quoted 17 lines
  arch/riscv/kvm/vcpu.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
arch/riscv/kvm/vcpu.c +1 -2
index e087c809073c..bf3952d1a621 100644
@@ -757,8 +757,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
  		/* Update HVIP CSR for current CPU */
  		kvm_riscv_update_hvip(vcpu);
 
-		if (ret <= 0 ||
-		    kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) ||
+		if (kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) ||
  		    kvm_request_pending(vcpu) ||
  		    xfer_to_guest_mode_work_pending()) {
  			vcpu->mode = OUTSIDE_GUEST_MODE;
maintainer
#3 lore raw
In reply to Chao Du
Quoted 6 lines · On Mon, Dec 11, 2023 at 3:11?PM Chao Du <[email protected]> wrote:

The latest ret value is updated by kvm_riscv_vcpu_aia_update(), the loop will continue if the ret is less than or equal to zero. So the later condition will never hit. Thus remove it.

Queued this patch for Linux-6.8

Thanks,
Anup

Quoted 21 lines
---
 arch/riscv/kvm/vcpu.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/riscv/kvm/vcpu.c +1 -3
index e087c809073c..bf3952d1a621 100644
@@ -757,8 +757,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
                /* Update HVIP CSR for current CPU */
                kvm_riscv_update_hvip(vcpu);
 
-               if (ret <= 0 ||
-                   kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) ||
+               if (kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) ||
                    kvm_request_pending(vcpu) ||
                    xfer_to_guest_mode_work_pending()) {
                        vcpu->mode = OUTSIDE_GUEST_MODE;
--

2.17.1