Re: [PATCH 1/2] KVM: SVM: avoid infinite loop on NPF from bad address

From: Wanpeng Li
Date: Wed Jul 08 2020 - 04:18:04 EST


On Sat, 18 Apr 2020 at 00:39, Paolo Bonzini <pbonzini@xxxxxxxxxx> wrote:
>
> When a nested page fault is taken from an address that does not have
> a memslot associated to it, kvm_mmu_do_page_fault returns RET_PF_EMULATE
> (via mmu_set_spte) and kvm_mmu_page_fault then invokes svm_need_emulation_on_page_fault.
>
> The default answer there is to return false, but in this case this just
> causes the page fault to be retried ad libitum. Since this is not a
> fast path, and the only other case where it is taken is an erratum,
> just stick a kvm_vcpu_gfn_to_memslot check in there to detect the
> common case where the erratum is not happening.
>
> This fixes an infinite loop in the new set_memory_region_test.
>
> Fixes: 05d5a4863525 ("KVM: SVM: Workaround errata#1096 (insn_len maybe zero on SMAP violation)")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
> ---
> arch/x86/kvm/svm/svm.c | 7 +++++++
> virt/kvm/kvm_main.c | 1 +
> 2 files changed, 8 insertions(+)
>
> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> index a91e397d6750..c86f7278509b 100644
> --- a/arch/x86/kvm/svm/svm.c
> +++ b/arch/x86/kvm/svm/svm.c
> @@ -3837,6 +3837,13 @@ static bool svm_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
> bool smap = cr4 & X86_CR4_SMAP;
> bool is_user = svm_get_cpl(vcpu) == 3;
>
> + /*
> + * If RIP is invalid, go ahead with emulation which will cause an
> + * internal error exit.
> + */
> + if (!kvm_vcpu_gfn_to_memslot(vcpu, kvm_rip_read(vcpu) >> PAGE_SHIFT))
> + return true;
> +
> /*
> * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
> *
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index e2f60e313c87..e7436d054305 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -1602,6 +1602,7 @@ struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn
> {
> return __gfn_to_memslot(kvm_vcpu_memslots(vcpu), gfn);
> }
> +EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_memslot);

This commit incurs the linux guest fails to boot once add --overcommit
cpu-pm=on or not intercept hlt instruction, any thoughts?

<...>-35787 [038] .... 2825.959082: kvm_exit: vcpu 1 reason npf rip
0xfd11d info 100000014 fd000
<...>-35788 [037] .... 2825.959082: kvm_exit: vcpu 2 reason npf rip
0xfd11d info 100000014 fd000
<...>-35789 [036] .... 2825.959082: kvm_exit: vcpu 3 reason npf rip
0xfd11d info 100000014 fd000
<...>-35788 [037] .... 2825.959082: kvm_page_fault: address fd000 error_code 14
<...>-35789 [036] .... 2825.959082: kvm_page_fault: address fd000 error_code 14
<...>-35787 [038] .... 2825.959083: kvm_page_fault: address fd000 error_code 14
<...>-35788 [037] .... 2825.959086: kvm_emulate_insn: 0:fd11d: (prot32)
<...>-35788 [037] .... 2825.959086: kvm_emulate_insn: 0:fd11d: (prot32) failed
<...>-35789 [036] .... 2825.959087: kvm_emulate_insn: 0:fd11d: (prot32)
<...>-35789 [036] .... 2825.959087: kvm_emulate_insn: 0:fd11d: (prot32) failed
<...>-35788 [037] .... 2825.959087: kvm_fpu: unload
<...>-35787 [038] .... 2825.959087: kvm_emulate_insn: 0:fd11d: (prot32)
<...>-35787 [038] .... 2825.959087: kvm_emulate_insn: 0:fd11d: (prot32) failed
<...>-35789 [036] .... 2825.959087: kvm_fpu: unload
<...>-35787 [038] .... 2825.959088: kvm_fpu: unload
<...>-35788 [037] .... 2825.959088: kvm_userspace_exit: reason
KVM_EXIT_INTERNAL_ERROR (17)
<...>-35789 [036] .... 2825.959089: kvm_userspace_exit: reason
KVM_EXIT_INTERNAL_ERROR (17)
<...>-35787 [038] .... 2825.959089: kvm_userspace_exit: reason
KVM_EXIT_INTERNAL_ERROR (17)