Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4318422rwb; Tue, 20 Sep 2022 12:01:35 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6YXzt3GuVWuHeEybA8IPLBWHLs7Fs3/Fve2HxaD+ecQKt9cQplYRXONNJ7/RAS8B4KbD7S X-Received: by 2002:a17:906:5a71:b0:770:8625:489e with SMTP id my49-20020a1709065a7100b007708625489emr18078629ejc.405.1663700495238; Tue, 20 Sep 2022 12:01:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663700495; cv=none; d=google.com; s=arc-20160816; b=Z+XoYY329Fn7LbekWJiBb+wlyFnaVtd79kolCI303k3BVtOLHBertLEs7+LQfro99v u66+OWNtZPzHml84AtCA3evZUUdVGlDLLQhka9a0AHdvM+/NqQHdjyShqOgfSRXpR5VV +Dc+LPgnBouc7wpvncpE2gV6AQoevMOMlSCA2Yu0P/vf+he1VWJf0FrLSKzbhSiIkKql rBPr36qJXvyt88BLB2jL72Wgsj999B1/zTf93g81f1GfWkNgS8u9L7AGSlakY7d/WKsN 2jdraLCjSa6Gor/OIyjVBNqI7LrkvOZzrGP8wd/Ugb30HnzdX8eiofbmfPqJY2k1Yzky y3uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=IQ9PPDyjj+lezNSSGrpRlCchgA2fjl3IyszjxDAAz7w=; b=t3jaHJVmNNXpPREuSbq1PeCOBCEur7QI+ddvzOuhP9n1A0CPblQJrCJ7h2XIedwZgY UrpPXMG84MtPp86Q8UOfY+URD1c5yKyVz7ph4IcdnlEZde5Ed5uitYMpLyZsKMrWe9Xj s0apaKXW9BphaNCf9LEolbShP8O6ATISXIO5Xqh/+JuWDLdeCUdXa3tzYtiHzCOi1d+J 9ejER9XKHVKu1kC/vh4lyfgweDd0nOjIOi25ZAMZ5Wv1ZyFKADCKSY3RweEuJeY7ldi4 UM1swGPEEirAcAIypMWAXIICpBPixg57OFWGJ+FY4CAj4qB6/hp1n3a3XOtwva9Gci6n 1E7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=VgNVATk8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i6-20020a50d746000000b004487436b380si540503edj.363.2022.09.20.12.01.09; Tue, 20 Sep 2022 12:01:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=VgNVATk8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229605AbiITSot (ORCPT + 99 others); Tue, 20 Sep 2022 14:44:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229499AbiITSor (ORCPT ); Tue, 20 Sep 2022 14:44:47 -0400 Received: from mail-qv1-xf2a.google.com (mail-qv1-xf2a.google.com [IPv6:2607:f8b0:4864:20::f2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB3DE13D77 for ; Tue, 20 Sep 2022 11:44:44 -0700 (PDT) Received: by mail-qv1-xf2a.google.com with SMTP id g4so2700235qvo.3 for ; Tue, 20 Sep 2022 11:44:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=IQ9PPDyjj+lezNSSGrpRlCchgA2fjl3IyszjxDAAz7w=; b=VgNVATk8TWzGJTvL5fgSB/Swnr0d1NFPmPI8WXt2gnAJAOWBy+K3vG1rVCoTckL+om LXmIcep6A2xb3HMjMDn98kelZqn7VTBJRe6RkICex6c7a1I3aTqu8IQ52I+aciQ1/gZG 1ctbR39n94hyRXIrL5JF5E/nBYmtLTf7eoYjTL30W0xw44GsiUTmLiy6v3kWMIfklcMf nXGFcpryar+moC3LRbu2MCPnfzvW/GIrr0vxfW8T9ZOB8xZ703riH/Yrx0x7Uej0d1gO wdAsCxhKdJlxI9w89qGnnVbGWL1Se/xFTLsOPpvAoWs6pC8noYvDNM3oRoVZiDdDPh9W ZDPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=IQ9PPDyjj+lezNSSGrpRlCchgA2fjl3IyszjxDAAz7w=; b=iOPKO3KYbnMZwefV+nv+OtfzXt47RNqqX9v/z6oL/MH7IkK2jdqPD/85dp+ik2EAK8 RoySrl6PAqp3/quDfKbKuILmGSrQGI4pMRSi6K2zlorpnNjcZfW9fwzWc+9UoQOD8rT7 e/yQgxCWUHC/uQxrYUntKO/qvMKm0E5Cd7QABlFiD7BcyiPnzM8a0z4Y6RN3osf/Bolt vL/ZrgEU/ERPRCUgOShT07XhcYtvnr4tL0S8JTpzT5H4oerVNDTQcGSaIRSTk+TcMDl4 ALNMMJEgSnBdbqZBK+/Ch7bNHHFp0P9q07Ml82qdHRc2p8jLBxMIY+8yBVaDFmcRIH6K do3g== X-Gm-Message-State: ACrzQf26G8gy5EX0+aeqjoMZ1sVNVAcc2EGdhtfZuaaVh9dIjbPh6IDf x3i57hv5uW5ov5HprV4P89jOufoW/M+Y56eWxbKe7w== X-Received: by 2002:a05:6214:c2a:b0:4ad:67d:c25a with SMTP id a10-20020a0562140c2a00b004ad067dc25amr20574500qvd.125.1663699483375; Tue, 20 Sep 2022 11:44:43 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Matlack Date: Tue, 20 Sep 2022 11:44:17 -0700 Message-ID: Subject: Re: [PATCH v2 1/6] KVM: x86/mmu: Fix wrong gfn range of tlb flushing in validate_direct_spte() To: Robert Hoo Cc: Hou Wenlong , kvm list , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , X86 ML , "H. Peter Anvin" , Lan Tianyu , LKML Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Sep 20, 2022 at 11:32 AM David Matlack wrote: > > On Sun, Sep 18, 2022 at 09:11:00PM +0800, Robert Hoo wrote: > > On Wed, 2022-08-24 at 17:29 +0800, Hou Wenlong wrote: > > > The spte pointing to the children SP is dropped, so the > > > whole gfn range covered by the children SP should be flushed. > > > Although, Hyper-V may treat a 1-page flush the same if the > > > address points to a huge page, it still would be better > > > to use the correct size of huge page. Also introduce > > > a helper function to do range-based flushing when a direct > > > SP is dropped, which would help prevent future buggy use > > > of kvm_flush_remote_tlbs_with_address() in such case. > > > > > > Fixes: c3134ce240eed ("KVM: Replace old tlb flush function with new > > > one to flush a specified range.") > > > Suggested-by: David Matlack > > > Signed-off-by: Hou Wenlong > > > --- > > > arch/x86/kvm/mmu/mmu.c | 10 +++++++++- > > > 1 file changed, 9 insertions(+), 1 deletion(-) > > > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > > index e418ef3ecfcb..a3578abd8bbc 100644 > > > --- a/arch/x86/kvm/mmu/mmu.c > > > +++ b/arch/x86/kvm/mmu/mmu.c > > > @@ -260,6 +260,14 @@ void kvm_flush_remote_tlbs_with_address(struct > > > kvm *kvm, > > > kvm_flush_remote_tlbs_with_range(kvm, &range); > > > } > > > > > > +/* Flush all memory mapped by the given direct SP. */ > > > +static void kvm_flush_remote_tlbs_direct_sp(struct kvm *kvm, struct > > > kvm_mmu_page *sp) > > > +{ > > > + WARN_ON_ONCE(!sp->role.direct); > > > > What if !sp->role.direct? Below flushing sp->gfn isn't expected? but > > still to do it. Is this operation harmless? > > Flushing TLBs is always harmless because KVM cannot ever assume an entry is > in the TLB. However, *not* (properly) flushing TLBs can be harmful. If KVM ever > calls kvm_flush_remote_tlbs_direct_sp() with an indirect SP, that is a bug in > KVM. The TLB flush here won't be harmful, as I explained, but KVM will miss a > TLB flush. > > That being said, I don't think any changes here are necessary. > kvm_flush_remote_tlbs_direct_sp() only has one caller, validate_direct_spte(), > which only operates on direct SPs. The name of the function also makes it > obvious this should only be called with a direct SP. And if we ever mess this > up in the future, we'll see the WARN_ON(). That being said, we might as well replace the WARN_ON_ONCE() with KVM_BUG_ON(). That will still do a WARN_ON_ONCE() but has the added benefit of terminating the VM.