KVM flushes the APF queue completely when the asynchronous pagefault is disabled, therefore this case should not occur. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a1c49bc681c4..9018d56b4b0a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13466,7 +13466,7 @@ void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) { - if (!kvm_pv_async_pf_enabled(vcpu)) + if (WARN_ON_ONCE(!kvm_pv_async_pf_enabled(vcpu))) return true; else return kvm_lapic_enabled(vcpu) && apf_pageready_slot_free(vcpu); -- 2.49.0 Fix a semi theoretical race condition in reading of page_ready_pending in kvm_arch_async_page_present_queued. Only trust the value of page_ready_pending if the guest is about to enter guest mode (vcpu->mode). To achieve this, read the vcpu->mode using smp_load_acquire which is paired with smp_store_release in vcpu_enter_guest. Then only if vcpu_mode is IN_GUEST_MODE, trust the value of the page_ready_pending because it was written before and therefore its correct value is visible. Also if the above mentioned check is true, avoid raising the request on the target vCPU. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/x86.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9018d56b4b0a..3d45a4cd08a4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13459,9 +13459,14 @@ void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) { - kvm_make_request(KVM_REQ_APF_READY, vcpu); - if (!vcpu->arch.apf.pageready_pending) + /* Pairs with smp_store_release in vcpu_enter_guest. */ + bool in_guest_mode = (smp_load_acquire(&vcpu->mode) == IN_GUEST_MODE); + bool page_ready_pending = READ_ONCE(vcpu->arch.apf.pageready_pending); + + if (!in_guest_mode || !page_ready_pending) { + kvm_make_request(KVM_REQ_APF_READY, vcpu); kvm_vcpu_kick(vcpu); + } } bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) -- 2.49.0 Currently a #SMI can cause KVM to drop an #APF ready event and subsequently causes the guest to never resume the task that is waiting for it. This can result in tasks becoming permanently stuck within the guest. This happens because KVM flushes the APF queue without notifying the guest of completed APF requests when the guest exits to real mode. And the SMM exit code calls kvm_set_cr0 with CR.PE == 0, which triggers this code. It must be noted that while this flush is reasonable to do for the actual real mode entry, it is actually achieves nothing because it is too late to flush this queue on SMM exit. To fix this, avoid doing this flush altogether, and handle the real mode entry/exits in the same way KVM already handles the APIC enable/disable events: APF completion events are not injected while APIC is disabled, and once APIC is re-enabled, KVM raises the KVM_REQ_APF_READY request which causes the first pending #APF ready event to be injected prior to entry to the guest mode. This change also has the side benefit of preserving #APF events if the guest temporarily enters real mode - for example, to call firmware - although such usage should be extermery rare in modern operating systems. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/x86.c | 11 +++++++---- arch/x86/kvm/x86.h | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3d45a4cd08a4..5dfe166025bf 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1118,15 +1118,18 @@ void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned lon } if ((cr0 ^ old_cr0) & X86_CR0_PG) { - kvm_clear_async_pf_completion_queue(vcpu); - kvm_async_pf_hash_reset(vcpu); - /* * Clearing CR0.PG is defined to flush the TLB from the guest's * perspective. */ if (!(cr0 & X86_CR0_PG)) kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); + + /* + * Re-check APF completion events, when the guest re-enables paging. + */ + if ((cr0 & X86_CR0_PG) && kvm_pv_async_pf_enabled(vcpu)) + kvm_make_request(KVM_REQ_APF_READY, vcpu); } if ((cr0 ^ old_cr0) & KVM_MMU_CR0_ROLE_BITS) @@ -3547,7 +3550,7 @@ static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info) return 0; } -static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu) +bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu) { u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT; diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index bcfd9b719ada..3949c938a88d 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -698,5 +698,6 @@ int ____kvm_emulate_hypercall(struct kvm_vcpu *vcpu, int cpl, }) int kvm_emulate_hypercall(struct kvm_vcpu *vcpu); +bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu); #endif -- 2.49.0