Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3856699pxv; Mon, 26 Jul 2021 14:05:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxM+KZXYjL4XK0FafZim4rnukTbPmoG7mhPChdR2irm+8ZXdG1VO2opERdxL3qMQ6UsK1ms X-Received: by 2002:a17:906:c251:: with SMTP id bl17mr11792729ejb.219.1627333539873; Mon, 26 Jul 2021 14:05:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627333539; cv=none; d=google.com; s=arc-20160816; b=0vWFJzY6RFQUUJ4Ct7qBJ1ZQoVyXHre5h8Z5+sxTBjIGocmbyfYrqORnFTurXy4U6+ ytqZvaz0Y5OOgQWHSNNwTDAYCie3ANhZ+1iCkq1Bu+dYcsTkTwpPfWQPQZ3DsKblVDxY L0EcNBgrzsQIKonl1Wf7M1WOU57UEhwe16XySUKoTHZB7e+OurKAF1kTZf4v9mtT3PmO qWVcdaBdGkDD5fzSrGiUe4RrU3KsYIJwuT3PlCP7aq509PHBcGKjG9xn/4NnwQUousu1 NMgtfGTMwYMmylnoCC4B1Z7qZFhwqM3eUw4p96cBm4b8Bl8EbWQDwjkBTCsRuRyvlcze qTEA== 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=6IT8x9lzIx6n/BJ/OVx0lxvbExZ5qOlfUXixHe0bbys=; b=e2Dlz6hdGLYL45uqf7bkTiW5tfBgVEPdD3mZllXTuuoKfE38VZFwa4XOeWtHUh0SRX 2nUcIcWvyydclFcgCMvXm4MIY+BPGGDv80qkSrbhryMaX1+LgS4yfsNokkNHP3GUWX2P 71XMLe9PeDSRSDK+Sb+iOcxSwvGT2MwioY3h4UXvZIBvWS6NVeCOhtEeWmH8CsrLDi4h iKFdyuaP3bK3k8Z8PLDSUq1yvr+vdrUluiQy6pw1VWx75f9OkZ4Y+SkRet+nemNmlwcO 1dgNY7NzxHJwA7IjX0qgRu6fTxbSI9TyHCIpX4xJRlb+G2LDucML+P5ncY/Ojrd91XAT curw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=pDMgqn4v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u20si981301ejz.711.2021.07.26.14.05.16; Mon, 26 Jul 2021 14:05:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=pDMgqn4v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233002AbhGZUWO (ORCPT + 99 others); Mon, 26 Jul 2021 16:22:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232772AbhGZUWM (ORCPT ); Mon, 26 Jul 2021 16:22:12 -0400 Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF6C2C061757 for ; Mon, 26 Jul 2021 14:02:40 -0700 (PDT) Received: by mail-io1-xd2f.google.com with SMTP id j21so13609826ioo.6 for ; Mon, 26 Jul 2021 14:02:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=6IT8x9lzIx6n/BJ/OVx0lxvbExZ5qOlfUXixHe0bbys=; b=pDMgqn4vHsn0oX7XfuvhKNIaiMD0p27Xd/46lgidJzEQgnLDi7rh4obI6GXlO5KNhz uW3JEdP8KUx77Bp+uaLK6lESAfW/bwcJVgwXCB4UjsN2H6PJwSA1anMG35GDUNATQzLZ LSqba8zm3DJLWHBQJH7HYrodbAkYRb3of4F6lEM1vXdb/HuhkUciAscbG3BRrgwCbVkF nqQFVxtqErHql/RTTVnYqZqBslRYsMuaY6pLWoPMpSnsW3X6kl3kVmzB8bq77HzJr3tJ xBvSG3fYigdlMKXD4uaGugG3yTbJAgn3ilGhJG4hyNCksDNZLDJ7XvqWVqNKDGQ73KIn U7Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=6IT8x9lzIx6n/BJ/OVx0lxvbExZ5qOlfUXixHe0bbys=; b=reY7LSbKZKU7wxPnu1umeahwSE0IUNBKycMdlRTKLyGpx67uRCtPaNo+5ibz5Ij27e Ba4hgz/EONzch2NaXcDS4k4zwFj5EJs1Cyn3acm6ltkHSCHdRBraALxosyaRmYGtILAW vIzl8n1K+5l5zo2wWo2GhMoPQ2lWKbeiBJ62Ld9h8tSOPm6zlFq9JrQ8GeErNx9k9f+X shmcOdJoIK9qqu/FZu/MO+6ma+xevrDCimHYg9sORottJKfMTsgkK/nWE8QMnFxNB1RD JWR0yYtbcKLQUPHq47T+hZb4nniDm9zCGkVIBAL2OfVhTGC+DoHeAe9bTpG4FuAfR2/6 LCgg== X-Gm-Message-State: AOAM530auUleLqpBGJ4BQM9axtUFUuPswK5xorDSni7jUXKpNycUEHrj xf3kz7F40k3I8FcoDC0xUTLg0aYonhdvAlUvzDznKg== X-Received: by 2002:a6b:2bd4:: with SMTP id r203mr11606325ior.157.1627333360050; Mon, 26 Jul 2021 14:02:40 -0700 (PDT) MIME-Version: 1.0 References: <20210726175357.1572951-1-mizhang@google.com> <20210726175357.1572951-3-mizhang@google.com> In-Reply-To: <20210726175357.1572951-3-mizhang@google.com> From: Ben Gardon Date: Mon, 26 Jul 2021 14:02:27 -0700 Message-ID: Subject: Re: [PATCH v2 2/3] KVM: x86/mmu: Avoid collision with !PRESENT SPTEs in TDP MMU lpage stats To: Mingwei Zhang Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm , LKML , Jing Zhang Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 26, 2021 at 10:54 AM Mingwei Zhang wrote: > > Factor in whether or not the old/new SPTEs are shadow-present when > adjusting the large page stats in the TDP MMU. A modified MMIO SPTE can > toggle the page size bit, as bit 7 is used to store the MMIO generation, > i.e. is_large_pte() can get a false positive when called on a MMIO SPTE. > Ditto for nuking SPTEs with REMOVED_SPTE, which sets bit 7 in its magic > value. > > Opportunistically move the logic below the check to verify at least one > of the old/new SPTEs is shadow present. > > Use is/was_leaf even though is/was_present would suffice. The code > generation is roughly equivalent since all flags need to be computed > prior to the code in question, and using the *_leaf flags will minimize > the diff in a future enhancement to account all pages, i.e. will change > the check to "is_leaf != was_leaf". > > Suggested-by: Sean Christopherson > Signed-off-by: Mingwei Zhang Reviewed-by: Ben Gardon > --- > arch/x86/kvm/mmu/tdp_mmu.c | 20 +++++++++++++------- > 1 file changed, 13 insertions(+), 7 deletions(-) > > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > index caac4ddb46df..cba2ab5db2a0 100644 > --- a/arch/x86/kvm/mmu/tdp_mmu.c > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > @@ -413,6 +413,7 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, > bool was_leaf = was_present && is_last_spte(old_spte, level); > bool is_leaf = is_present && is_last_spte(new_spte, level); > bool pfn_changed = spte_to_pfn(old_spte) != spte_to_pfn(new_spte); > + bool was_large, is_large; > > WARN_ON(level > PT64_ROOT_MAX_LEVEL); > WARN_ON(level < PG_LEVEL_4K); > @@ -446,13 +447,6 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, > > trace_kvm_tdp_mmu_spte_changed(as_id, gfn, level, old_spte, new_spte); > > - if (is_large_pte(old_spte) != is_large_pte(new_spte)) { > - if (is_large_pte(old_spte)) > - atomic64_sub(1, (atomic64_t*)&kvm->stat.lpages); > - else > - atomic64_add(1, (atomic64_t*)&kvm->stat.lpages); > - } > - > /* > * The only times a SPTE should be changed from a non-present to > * non-present state is when an MMIO entry is installed/modified/ > @@ -478,6 +472,18 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, > return; > } > > + /* > + * Update large page stats if a large page is being zapped, created, or > + * is replacing an existing shadow page. > + */ > + was_large = was_leaf && is_large_pte(old_spte); > + is_large = is_leaf && is_large_pte(new_spte); > + if (was_large != is_large) { > + if (was_large) > + atomic64_sub(1, (atomic64_t *)&kvm->stat.lpages); > + else > + atomic64_add(1, (atomic64_t *)&kvm->stat.lpages); > + } > > if (was_leaf && is_dirty_spte(old_spte) && > (!is_present || !is_dirty_spte(new_spte) || pfn_changed)) > -- > 2.32.0.432.gabb21c7263-goog >