From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY, USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 064E7C4320A for ; Sat, 14 Aug 2021 09:47:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DBD7E60F00 for ; Sat, 14 Aug 2021 09:47:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237681AbhHNJsQ (ORCPT ); Sat, 14 Aug 2021 05:48:16 -0400 Received: from out30-130.freemail.mail.aliyun.com ([115.124.30.130]:50226 "EHLO out30-130.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232648AbhHNJsP (ORCPT ); Sat, 14 Aug 2021 05:48:15 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04420;MF=laijs@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0UiyB2fi_1628934463; Received: from C02XQCBJJG5H.local(mailfrom:laijs@linux.alibaba.com fp:SMTPD_---0UiyB2fi_1628934463) by smtp.aliyun-inc.com(127.0.0.1); Sat, 14 Aug 2021 17:47:43 +0800 Subject: Re: [PATCH 1/2] KVM: X86: Check pte present first in __shadow_walk_next() To: Sean Christopherson , Lai Jiangshan Cc: linux-kernel@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , kvm@vger.kernel.org References: <20210812043630.2686-1-jiangshanlai@gmail.com> From: Lai Jiangshan Message-ID: <4df6cdb1-c559-fee0-7efa-a2afa059c945@linux.alibaba.com> Date: Sat, 14 Aug 2021 17:47:43 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/8/13 00:03, Sean Christopherson wrote: > > And for clarity and safety, I think it would be worth adding the patch below as > a prep patch to document and enforce that walking the non-leaf SPTEs when faulting > in a page should never terminate early. I'v sent V2 patch which was updated as you suggested. The patch you provided below doesn't need to be updated. It and my V2 patch do not depend on each other, so I didn't resent your patch with mine together. For your patch Acked-by: Lai Jiangshan And it can be queued earlier. > > > From 1c202a7e82b1931e4eb37b23aa9d7108340a6cd2 Mon Sep 17 00:00:00 2001 > From: Sean Christopherson > Date: Thu, 12 Aug 2021 08:38:35 -0700 > Subject: [PATCH] KVM: x86/mmu: Verify shadow walk doesn't terminate early in > page faults > > WARN and bail if the shadow walk for faulting in a SPTE terminates early, > i.e. doesn't reach the expected level because the walk encountered a > terminal SPTE. The shadow walks for page faults are subtle in that they > install non-leaf SPTEs (zapping leaf SPTEs if necessary!) in the loop > body, and consume the newly created non-leaf SPTE in the loop control, > e.g. __shadow_walk_next(). In other words, the walks guarantee that the > walk will stop if and only if the target level is reached by installing > non-leaf SPTEs to guarantee the walk remains valid. > > Opportunistically use fault->goal-level instead of it.level in > FNAME(fetch) to further clarify that KVM always installs the leaf SPTE at > the target level. > > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/mmu/mmu.c | 3 +++ > arch/x86/kvm/mmu/paging_tmpl.h | 7 +++++-- > 2 files changed, 8 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index a272ccbddfa1..2a243ae1d64c 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -2992,6 +2992,9 @@ static int __direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) > account_huge_nx_page(vcpu->kvm, sp); > } > > + if (WARN_ON_ONCE(it.level != fault->goal_level)) > + return -EFAULT; > + > ret = mmu_set_spte(vcpu, it.sptep, ACC_ALL, > fault->write, fault->goal_level, base_gfn, fault->pfn, > fault->prefault, fault->map_writable); > diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h > index f70afecbf3a2..3a8a7b2f9979 100644 > --- a/arch/x86/kvm/mmu/paging_tmpl.h > +++ b/arch/x86/kvm/mmu/paging_tmpl.h > @@ -749,9 +749,12 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, > } > } > > + if (WARN_ON_ONCE(it.level != fault->goal_level)) > + return -EFAULT; > + > ret = mmu_set_spte(vcpu, it.sptep, gw->pte_access, fault->write, > - it.level, base_gfn, fault->pfn, fault->prefault, > - fault->map_writable); > + fault->goal_level, base_gfn, fault->pfn, > + fault->prefault, fault->map_writable); > if (ret == RET_PF_SPURIOUS) > return ret; > > -- > 2.33.0.rc1.237.g0d66db33f3-goog >