Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4296008rwb; Tue, 20 Sep 2022 11:36:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4khTezbvd5ZS6XCg7Wwm66ks69jXy2N7bT4kQpFYTLXi4ATj2nnUmYcCrMmoRQW0vxVm74 X-Received: by 2002:a63:4e47:0:b0:43a:9d5:d1ef with SMTP id o7-20020a634e47000000b0043a09d5d1efmr10963046pgl.400.1663698986202; Tue, 20 Sep 2022 11:36:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663698986; cv=none; d=google.com; s=arc-20160816; b=h0vxhE49dmm4FReRsG+vjl/KYT/QC3+bHOHo1G5PBfzZxILKaXJcTw+rmz90HOcNGy 8hrqfB1QTgSp0n5I48xdtiPbuAEgJqvgXAcOjEFpJj5J7L37h4HqwBUH8nmotoysYwJ4 zZI5Dg0uETY4jYzTU7Ys+FjhqoBnAJ5GbfqHuAGysSs9VQRIpw3IYeiUOyM2Geh/UgyY yo0K53U0OEvV+SUs/UJDfNZ1OaWXAnPXqVjlEbE11Z2g5x9cgN3L69F6RUd05AJuvSDP +vq9yKjfvBq5gt+lTL7ZYIoCzboI8EqilT6vfhi3gLSKoB7zxBrGWvMlS5beN8FkjVKZ GKLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=PeN1RcuQCUj1AaYPYRKMExfsBbior2SClHPctysKngU=; b=n4wQBJhrdoQRQJbirjgwqvNCEKRJfkiPH6W/u+nFLSi2SJCiDI7NQiaGSR+W3D2t6L D0ON7AAVXrqPuV0Vc5L1zpxsDcTXXwhr970+fpqU4HjeRWxjZwjDLsT4bXAbHpuHqcJf 3vL+82TeQPWKikQvxvInVd7o+zELdNGgVXnu91S3oLHYL9QIw20Hl+8eNLCyaFrPYWwH 2AqvucYGKClWCIkxk8qzn+2pAWyL9qFXLmAdm9jPs73vJ1gMA88bT2Mi6UCyL4EeMV7W RbO4F3z3WB2mA36X88dEP3hFXKNyevebLnLsuGp4tz9XecyiBQPONO4ZufBRFkxKwh4k m+8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ggFCO0+h; 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 r8-20020a632b08000000b00434e31807f6si400489pgr.676.2022.09.20.11.36.14; Tue, 20 Sep 2022 11:36:26 -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=ggFCO0+h; 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 S230323AbiITSJP (ORCPT + 99 others); Tue, 20 Sep 2022 14:09:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230148AbiITSJI (ORCPT ); Tue, 20 Sep 2022 14:09:08 -0400 Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C69C965279 for ; Tue, 20 Sep 2022 11:09:06 -0700 (PDT) Received: by mail-qk1-x731.google.com with SMTP id d17so2197887qko.13 for ; Tue, 20 Sep 2022 11:09:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date; bh=PeN1RcuQCUj1AaYPYRKMExfsBbior2SClHPctysKngU=; b=ggFCO0+hykBT6WHMuAite/PFcgFf/Y7+JkO6OFit1JV+mBnMrmPqlh51H8nJcdEva4 hhEoN82FclFB2lqCsyBzur8cuC4qQIAMpVNeZRbELgzh2aiqqLUyGXdNAPE4lZmF/sMr WaDppnlEWZTkYqbb6UaE9BU+1F7dz/0jeT4YR2X7M7wDTMwxP6mTjWMzYOimq12e57Wr 5kBFfsZSBH9pn3tdXOaOIiWKBm/PTeQrGz8s/qjLFqtRtT7Ie3O90r/oDmoalw+3th0s qktVwh65HMegNkhCrAT3PVrvBSfEt1G3zeWftltwPZXhfxJ/NkVdrEA3gsp1hRUVrFa2 iRug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=PeN1RcuQCUj1AaYPYRKMExfsBbior2SClHPctysKngU=; b=BNg1b/W/fVMQIjGlxih4LUnPCcBWTYqBiLf6K7qKVF/BeIWelkK9Tns6dnlMiMuW/B suOS3g6f0PHz56pHyd1gjWpTuZzjlk6U8hJU1rz64AHM6GLFqX2TiZZ3DSANmGy8Nluk Nv2Lat+OlOThKsh9dr3M4M8APim1F5TFZEgg9YZA4FLqvOmCnVBkg5DJjyMPnxqSZZXn 3Z7tgfgZ1p+HIqEDXKMHkPtGrz4zE8u+m+3XbEX/80djLzIXY/P9jrhYTKwYkdpXh91C tM7pVTy2/9eGxwHk4uQJqMl2cDlLq8KwjTsMFhr8wKryedJzfrTk0z38z6EPHYtQ4ImG t/xw== X-Gm-Message-State: ACrzQf1GKUCiR1Dqy076yXmGK5IfQouDRnFYsrEEFUZ88p/tFXFKEEpS nBU54+3TjvB0pqf8eqsZM+lEuDAo5Ew+NehC/H0zHg== X-Received: by 2002:a05:620a:4447:b0:6c6:c438:1ced with SMTP id w7-20020a05620a444700b006c6c4381cedmr18055169qkp.658.1663697345804; Tue, 20 Sep 2022 11:09:05 -0700 (PDT) MIME-Version: 1.0 References: <20220913120721.GA113257@k08j02272.eu95sqa> In-Reply-To: From: David Matlack Date: Tue, 20 Sep 2022 11:08:39 -0700 Message-ID: Subject: Re: [PATCH v2 1/6] KVM: x86/mmu: Fix wrong gfn range of tlb flushing in validate_direct_spte() To: Liam Ni 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" Content-Transfer-Encoding: quoted-printable 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=unavailable 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 Thu, Sep 15, 2022 at 4:47 AM Liam Ni wrote: > > On Tue, 13 Sept 2022 at 20:13, Hou Wenlong = wrote: > > > > On Thu, Sep 08, 2022 at 01:43:54AM +0800, David Matlack wrote: > > > On Wed, Aug 24, 2022 at 05:29:18PM +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, struc= t kvm_mmu_page *sp) > > > > +{ > > > > + WARN_ON_ONCE(!sp->role.direct); > > > > + kvm_flush_remote_tlbs_with_address(kvm, sp->gfn, > > > > + KVM_PAGES_PER_HPAGE(sp->role= .level + 1)); > > Do we need "+1" here? sp->role.level=3D1 means 4k page. > I think here should be =E2=80=9CKVM_PAGES_PER_HPAGE(sp->role.level)=E2= =80=9D Yes we need the "+ 1" here. kvm_flush_remote_tlbs_direct_sp() must flush all memory mapped by the shadow page, which is equivalent to the amount of memory mapped by a huge page at the next higher level. For example, a shadow page with role.level =3D=3D PG_LEVEL_4K maps 2 MiB of the guest physical address space since 512 PTEs x 4KiB per PTE =3D 2MiB. > > > > > > > nit: I think it would make sense to introduce > > > kvm_flush_remote_tlbs_gfn() in this patch since you are going to > > > eventually use it here anyway. > > > > > OK, I'll do it in the next version. Thanks! > > > > > > +} > > > > + > > > > static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 = gfn, > > > > unsigned int access) > > > > { > > > > @@ -2341,7 +2349,7 @@ static void validate_direct_spte(struct kvm_v= cpu *vcpu, u64 *sptep, > > > > return; > > > > > > > > drop_parent_pte(child, sptep); > > > > - kvm_flush_remote_tlbs_with_address(vcpu->kvm, child->gf= n, 1); > > > > + kvm_flush_remote_tlbs_direct_sp(vcpu->kvm, child); > > > > } > > > > } > > > > > > > > -- > > > > 2.31.1 > > > >