KVM: arm/arm64: properly use vcpu requests
authorAndrew Jones <drjones@redhat.com>
Sun, 4 Jun 2017 12:43:54 +0000 (14:43 +0200)
committerChristoffer Dall <cdall@linaro.org>
Sun, 4 Jun 2017 14:53:47 +0000 (16:53 +0200)
arm/arm64 already has one VCPU request used when setting pause,
but it doesn't properly check requests in VCPU RUN. Check it
and also make sure we set vcpu->mode at the appropriate time
(before the check) and with the appropriate barriers. See
Documentation/virtual/kvm/vcpu-requests.rst. Also make sure we
don't leave any vcpu requests we don't intend to handle later
set in the request bitmap. If we don't clear them, then
kvm_request_pending() may return true when it shouldn't.

Using VCPU requests properly fixes a small race where pause
could get set just as a VCPU was entering guest mode.

Signed-off-by: Andrew Jones <drjones@redhat.com>
Reviewed-by: Christoffer Dall <cdall@linaro.org>
Signed-off-by: Christoffer Dall <cdall@linaro.org>
arch/arm/kvm/handle_exit.c
arch/arm64/kvm/handle_exit.c
virt/kvm/arm/arm.c
virt/kvm/arm/psci.c

index 5fd7968..a2b4f7b 100644 (file)
@@ -72,6 +72,7 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu, struct kvm_run *run)
                trace_kvm_wfx(*vcpu_pc(vcpu), false);
                vcpu->stat.wfi_exit_stat++;
                kvm_vcpu_block(vcpu);
+               kvm_clear_request(KVM_REQ_UNHALT, vcpu);
        }
 
        kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
index fa1b18e..17d8a16 100644 (file)
@@ -89,6 +89,7 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu, struct kvm_run *run)
                trace_kvm_wfx_arm64(*vcpu_pc(vcpu), false);
                vcpu->stat.wfi_exit_stat++;
                kvm_vcpu_block(vcpu);
+               kvm_clear_request(KVM_REQ_UNHALT, vcpu);
        }
 
        kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
index 3c387fd..1382126 100644 (file)
@@ -546,6 +546,7 @@ void kvm_arm_resume_guest(struct kvm *kvm)
 
        kvm_for_each_vcpu(i, vcpu, kvm) {
                vcpu->arch.pause = false;
+               kvm_clear_request(KVM_REQ_VCPU_EXIT, vcpu);
                swake_up(kvm_arch_vcpu_wq(vcpu));
        }
 }
@@ -638,8 +639,18 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
                        run->exit_reason = KVM_EXIT_INTR;
                }
 
+               /*
+                * Ensure we set mode to IN_GUEST_MODE after we disable
+                * interrupts and before the final VCPU requests check.
+                * See the comment in kvm_vcpu_exiting_guest_mode() and
+                * Documentation/virtual/kvm/vcpu-requests.rst
+                */
+               smp_store_mb(vcpu->mode, IN_GUEST_MODE);
+
                if (ret <= 0 || need_new_vmid_gen(vcpu->kvm) ||
-                       vcpu->arch.power_off || vcpu->arch.pause) {
+                   kvm_request_pending(vcpu) ||
+                   vcpu->arch.power_off || vcpu->arch.pause) {
+                       vcpu->mode = OUTSIDE_GUEST_MODE;
                        local_irq_enable();
                        kvm_pmu_sync_hwstate(vcpu);
                        kvm_timer_sync_hwstate(vcpu);
@@ -655,7 +666,6 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
                 */
                trace_kvm_entry(*vcpu_pc(vcpu));
                guest_enter_irqoff();
-               vcpu->mode = IN_GUEST_MODE;
 
                ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
 
index a08d7a9..f68be2c 100644 (file)
@@ -57,6 +57,7 @@ static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
         * for KVM will preserve the register state.
         */
        kvm_vcpu_block(vcpu);
+       kvm_clear_request(KVM_REQ_UNHALT, vcpu);
 
        return PSCI_RET_SUCCESS;
 }