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=-10.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,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 8FEF6C47089 for ; Fri, 28 May 2021 01:13:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6EA61613C9 for ; Fri, 28 May 2021 01:13:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234974AbhE1BOr (ORCPT ); Thu, 27 May 2021 21:14:47 -0400 Received: from out30-43.freemail.mail.aliyun.com ([115.124.30.43]:43438 "EHLO out30-43.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233887AbhE1BOp (ORCPT ); Thu, 27 May 2021 21:14:45 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R851e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04426;MF=laijs@linux.alibaba.com;NM=1;PH=DS;RN=15;SR=0;TI=SMTPD_---0UaJKUwf_1622164388; Received: from C02XQCBJJG5H.local(mailfrom:laijs@linux.alibaba.com fp:SMTPD_---0UaJKUwf_1622164388) by smtp.aliyun-inc.com(127.0.0.1); Fri, 28 May 2021 09:13:09 +0800 Subject: Re: [PATCH] KVM: X86: fix tlb_flush_guest() To: Sean Christopherson , Paolo Bonzini Cc: Lai Jiangshan , linux-kernel@vger.kernel.org, Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Maxim Levitsky , kvm@vger.kernel.org References: <20210527023922.2017-1-jiangshanlai@gmail.com> <78ad9dff-9a20-c17f-cd8f-931090834133@redhat.com> From: Lai Jiangshan Message-ID: Date: Fri, 28 May 2021 09:13:08 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/5/28 03:28, Sean Christopherson wrote: > On Thu, May 27, 2021, Sean Christopherson wrote: >>> KVM_REQ_MMU_RELOAD is overkill, nuking the shadow page tables will completely >>> offset the performance gains of the paravirtualized flush. > > Argh, I take that back. The PV KVM_VCPU_FLUSH_TLB flag doesn't distinguish > between flushing a specific mm and flushing the entire TLB. The HyperV usage > (via KVM_REQ) also throws everything into a single bucket. A full RELOAD still > isn't necessary as KVM just needs to sync all roots, not blast them away. For > previous roots, KVM doesn't have a mechanism to defer the sync, so the immediate > fix will need to unload those roots. > > And looking at KVM's other flows, __kvm_mmu_new_pgd() and kvm_set_cr3() are also > broken with respect to previous roots. E.g. if the guest does a MOV CR3 that > flushes the entire TLB, followed by a MOV CR3 with PCID_NOFLUSH=1, KVM will fail > to sync the MMU on the second flush even though the guest can technically rely > on the first MOV CR3 to have synchronized any previous changes relative to the > fisrt MOV CR3. Could you elaborate the problem please? When can a MOV CR3 that needs to flush the entire TLB if PCID is enabled? If CR4.PCIDE = 1 and bit 63 of the instruction’s source operand is 0, the instruction invalidates all TLB entries associated with the PCID specified in bits 11:0 of the instruction’s source operand except those for global pages. It also invalidates all entries in all paging-structure caches associated with that PCID. It is not required to invalidate entries in the TLBs and paging-structure caches that are associated with other PCIDs. > > Lai, if it's ok with you, I'll massage this patch as discussed and fold it into > a larger series to fix the other bugs and do additional cleanup/improvements. > >>> I believe the minimal fix is: >>> >>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c >>> index 81ab3b8f22e5..b0072063f9bf 100644 >>> --- a/arch/x86/kvm/x86.c >>> +++ b/arch/x86/kvm/x86.c >>> @@ -3072,6 +3072,9 @@ static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu) >>> static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu) >>> { >>> ++vcpu->stat.tlb_flush; >>> + >>> + if (!tdp_enabled) >>> + kvm_mmu_sync_roots(vcpu); >>> static_call(kvm_x86_tlb_flush_guest)(vcpu); >>> }