Skip to contentSkip to conversation

RISC-V: KVM: Exit run-loop immediately if xfer_to_guest fails

PATCH In review Anup Patel 3 participants 3 messages Nov 8, 2022 – Nov 27, 2022

Subsystems (3)
lore
Share & tools
lore

Anup Patel

author
#0 lore raw

PATCH 1/1 RISC-V: KVM: Exit run-loop immediately if xfer_to_guest fails

If xfer_to_guest_mode_handle_work() fails in the run-loop then exit the run-loop immediately instead of doing it after some more work.

---
 arch/riscv/kvm/vcpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/riscv/kvm/vcpu.c +3 -2
index 71ebbc4821f0..17d5b3f8c2ee 100644
@@ -984,8 +984,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 	while (ret > 0) {
 		/* Check conditions before entering the guest */
 		ret = xfer_to_guest_mode_handle_work(vcpu);
-		if (!ret)
-			ret = 1;
+		if (ret)
+			continue;
+		ret = 1;
 
 		kvm_riscv_gstage_vmid_update(vcpu);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 14 lines · Anup Patel

If xfer_to_guest_mode_handle_work() fails in the run-loop then exit the run-loop immediately instead of doing it after some more work.

---
 arch/riscv/kvm/vcpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/riscv/kvm/vcpu.c +0 -0
index 71ebbc4821f0..17d5b3f8c2ee 100644

arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · line 988

Quoted 7 lines
arch/riscv/kvm/vcpu.c +2 -2
@@ -984,8 +984,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 	while (ret > 0) {
 		/* Check conditions before entering the guest */
 		ret = xfer_to_guest_mode_handle_work(vcpu);
-		if (!ret)
-			ret = 1;
+		if (ret)
+			continue;

If that is supposed to exit the loop, it would be clearer to just use break.

arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · line 989

Quoted 1 line
arch/riscv/kvm/vcpu.c +1 -0
+		ret = 1;

There is a condition on ret <= 0 later in the loop that no longer can be true.

Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
maintainer
#2 lore raw
Quoted 12 lines · Andreas Schwab
Quoted 9 lines · Anup Patel

If xfer_to_guest_mode_handle_work() fails in the run-loop then exit the run-loop immediately instead of doing it after some more work.

---
 arch/riscv/kvm/vcpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 15 lines

arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · line 988

Quoted 12 lines
arch/riscv/kvm/vcpu.c +2 -2
index 71ebbc4821f0..17d5b3f8c2ee 100644
@@ -984,8 +984,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
      while (ret > 0) {
              /* Check conditions before entering the guest */
              ret = xfer_to_guest_mode_handle_work(vcpu);
-             if (!ret)
-                     ret = 1;
+             if (ret)
+                     continue;

If that is supposed to exit the loop, it would be clearer to just use break.

This is a convention within the run-loop that we continue whenever whenever "ret" is no longer suitable to continue. I don't see any particular advantage in breaking this convention over here.

Quoted 5 lines

arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · line 989

Quoted 2 lines
arch/riscv/kvm/vcpu.c +1 -0
+             ret = 1;

There is a condition on ret <= 0 later in the loop that no longer can be true.

Yes, for now the "ret <= 0" check is useless and the compiler will optimize this comparison. We will be soon having more stuff added to run-loop (such as AIA update) which will make "ret <= 0" check useful again.

Quoted 5 lines

--
Andreas Schwab, SUSE Labs, schwab at suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something completely different."

Queued this patch for Linux-6.2

Thanks,
Anup