Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753587AbdLLODV (ORCPT ); Tue, 12 Dec 2017 09:03:21 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60310 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753505AbdLLODQ (ORCPT ); Tue, 12 Dec 2017 09:03:16 -0500 Subject: Re: [PATCH 1/2] KVM: MMU: Fix infinite loop when there is no available mmu page To: Wanpeng Li , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , Wanpeng Li References: <1512454891-7062-1-git-send-email-wanpeng.li@hotmail.com> From: Paolo Bonzini Message-ID: Date: Tue, 12 Dec 2017 15:03:12 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: <1512454891-7062-1-git-send-email-wanpeng.li@hotmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 12 Dec 2017 14:03:15 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2958 Lines: 90 On 05/12/2017 07:21, Wanpeng Li wrote: > From: Wanpeng Li > > The below test case can cause infinite loop in kvm when ept=0. > > #include > #include > #include > #include > #include > #include > #include > > long r[5]; > int main() > { > r[2] = open("/dev/kvm", O_RDONLY); > r[3] = ioctl(r[2], KVM_CREATE_VM, 0); > r[4] = ioctl(r[3], KVM_CREATE_VCPU, 7); > ioctl(r[4], KVM_RUN, 0); > } > > It doesn't setup the memory regions, mmu_alloc_shadow/direct_roots() in > kvm return 1 when kvm fails to allocate root page table which can result > in beblow infinite loop: > > vcpu_run() { > for (;;) { > r = vcpu_enter_guest()::kvm_mmu_reload() returns 1 > if (r <= 0) > break; > if (need_resched()) > cond_resched(); > } > } > > This patch fixes it by returning -ENOSPC when there is no available kvm mmu > page for root page table. > > Cc: Paolo Bonzini > Cc: Radim Krčmář > Fixes: 26eeb53cf0f (KVM: MMU: Bail out immediately if there is no available mmu page) > Signed-off-by: Wanpeng Li > --- > arch/x86/kvm/mmu.c | 8 ++++---- > 1 file changed, 4 insertions(+), 4 deletions(-) > > diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c > index c9aaa18..89da688 100644 > --- a/arch/x86/kvm/mmu.c > +++ b/arch/x86/kvm/mmu.c > @@ -3395,7 +3395,7 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) > spin_lock(&vcpu->kvm->mmu_lock); > if(make_mmu_pages_available(vcpu) < 0) { > spin_unlock(&vcpu->kvm->mmu_lock); > - return 1; > + return -ENOSPC; > } > sp = kvm_mmu_get_page(vcpu, 0, 0, > vcpu->arch.mmu.shadow_root_level, 1, ACC_ALL); > @@ -3410,7 +3410,7 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) > spin_lock(&vcpu->kvm->mmu_lock); > if (make_mmu_pages_available(vcpu) < 0) { > spin_unlock(&vcpu->kvm->mmu_lock); > - return 1; > + return -ENOSPC; > } > sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT), > i << 30, PT32_ROOT_LEVEL, 1, ACC_ALL); > @@ -3450,7 +3450,7 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu) > spin_lock(&vcpu->kvm->mmu_lock); > if (make_mmu_pages_available(vcpu) < 0) { > spin_unlock(&vcpu->kvm->mmu_lock); > - return 1; > + return -ENOSPC; > } > sp = kvm_mmu_get_page(vcpu, root_gfn, 0, > vcpu->arch.mmu.shadow_root_level, 0, ACC_ALL); > @@ -3487,7 +3487,7 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu) > spin_lock(&vcpu->kvm->mmu_lock); > if (make_mmu_pages_available(vcpu) < 0) { > spin_unlock(&vcpu->kvm->mmu_lock); > - return 1; > + return -ENOSPC; > } > sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, PT32_ROOT_LEVEL, > 0, ACC_ALL); > Queued, thanks.