Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp7322706pxv; Fri, 30 Jul 2021 16:02:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy8FsQUyBgMAMf24glByGq/LuklhPA1t5LSeqHrOEDNlvZb/ghwGkZs5acSicWolYSNr5l8 X-Received: by 2002:a5d:9808:: with SMTP id a8mr4090685iol.1.1627686151830; Fri, 30 Jul 2021 16:02:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627686151; cv=none; d=google.com; s=arc-20160816; b=FCirC85UXvOrn+GJJSGiVybugDmMET/kEujCallMhHwSZU4boHOqe83a1KnE93YGZX ibcC9XKsBQiCHnGanVXXx1NY5MWokSxwXUG87oDMnXGFToLT1Va2gaPUZ/XGpKISWWJM LED2RsF8/dy8QAE5M7OSTj3fQNs8MskhK4ZBn92K6xGCgI9P2odyosMu9hqR9lWmloPt EdiYVwOXI1f4aPIG6StsmefD7bW7HBE68d5BRBikLubimYsq2AbZRUzVYlkXg7g+pk8O JlH/X0mZneSC0k+uqKit9s94T+nj60VCF8nDqEsBoYpqQhffsEA2WEWhElqcVIeTRNYD fXAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=WOuUX+XoU7663u8UFn5j2EHWwVW3ujHXkDlL4NsSjs0=; b=KXY4GtIr8ltB8Ek0QGvggX6QMPv8/SNaYi9Qfv2csszg0K6bz/abIMcNfqHZVl6D68 yTZqbXbGW3RMAkBbECW1v1g9SEzBZhKuSk1njaaswZ/iwWXR8Z2sQO4pRB2NXveSNipW dyx5tCKx+ZhCt4s5BT5Sd6bQ+nvBdv1RzK80axn/YqJp4U9Xmd7XM3ePfKvbjDaTvvHS KQgv7f667oNqHU6BoYjiXaqMhJjHruvy+20tUkO8qsdyTZkeJ9CdwuvmSvZnoehe67Cq dPii7dvki1Gw4KmQp8tLwOds28+rBBv3lKv+Et678YKSzF6h//uvmAqMvq4BtbUdrA5r JZIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=iY9zlta5; 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 q21si3351713jat.69.2021.07.30.16.02.20; Fri, 30 Jul 2021 16:02:31 -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=iY9zlta5; 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 S234007AbhG3W7z (ORCPT + 99 others); Fri, 30 Jul 2021 18:59:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233957AbhG3W7x (ORCPT ); Fri, 30 Jul 2021 18:59:53 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66DB4C061765 for ; Fri, 30 Jul 2021 15:59:48 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id t18-20020a0cd4120000b02902fbda5d4988so6929727qvh.11 for ; Fri, 30 Jul 2021 15:59:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=WOuUX+XoU7663u8UFn5j2EHWwVW3ujHXkDlL4NsSjs0=; b=iY9zlta5rGtyt9y69A6lMEqBEpSRaocSketV5A0B7mLv0cL6ooPCXHEUervS206jU3 ZddMpCudqXX9qNhKf9kkVMtLdka8/kU1bF04rjI74CwqWTpxiThLtN2q3cmKhyL+BFGs SID335T15BHd36nQ3nx60xa1SjNxyjsvQv9jDXWClYVN0UKCmVlT3JuuKaKBX7JoguMS OMY9PVZcgBWdHQV/i6jegzgwU59+C58pCwB9Z8lOVCf69pfY6gn0oCwUh0Sl2m47eGp6 wxB+rOn+H7kq2M/6DYq2Deq3vEjDXmnHmRYSDkDY9ogu13TRH92Yxb0VSCvCDg2+F7yP wkcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=WOuUX+XoU7663u8UFn5j2EHWwVW3ujHXkDlL4NsSjs0=; b=auOXaL4ginDB8WtGpDcqBM+rMTw9UMFlAI9rNwBij0kANqgNNtamdVR5eSmzA56alU DOGD/3ghXb80pY0o+QkdJEJYzi2F2Qck++UrGDl7zyR+H+G7Ft/kyZFpXD8JBuIZ/pQP lAhbED9Xl/Di8QDGHV5m7Ve/Ag+PU6+E87qPZNu4AyiPbQ6SsCD3iJ4X9G/+kIkHWGRy 6yKozlnrQOD8KCpAuWOYVbhg8hOo1eXzSoDvXyN5sihiEpdi+vLLcTEKDKh//qH2LmKV x2EBqZnuL6EpqgH3bPQEYt0B6pUE5ItDdgzsqcX5Ge/cLMpYvcwmTRz2KD8qcyvjnZr4 7exQ== X-Gm-Message-State: AOAM531Sg/M44avuHmkYpjO2ZV0Mtjtin+tReOeDqU8mEA/udgcWQNP6 M1+3tT3klpy1gVfqSfbygchBKh8Vyg/P X-Received: from mihenry-linux-desktop.kir.corp.google.com ([2620:15c:29:204:a198:4c3e:b951:58e3]) (user=mizhang job=sendgmr) by 2002:a05:6214:d4d:: with SMTP id 13mr5266594qvr.42.1627685987547; Fri, 30 Jul 2021 15:59:47 -0700 (PDT) Reply-To: Mingwei Zhang Date: Fri, 30 Jul 2021 15:59:38 -0700 In-Reply-To: <20210730225939.3852712-1-mizhang@google.com> Message-Id: <20210730225939.3852712-3-mizhang@google.com> Mime-Version: 1.0 References: <20210730225939.3852712-1-mizhang@google.com> X-Mailer: git-send-email 2.32.0.554.ge1b32706d8-goog Subject: [PATCH v3 2/3] KVM: x86/mmu: Avoid collision with !PRESENT SPTEs in TDP MMU lpage stats From: Mingwei Zhang To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gardon , Mingwei Zhang , Jing Zhang , David Matlack Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson 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". Reviewed-by: Ben Gardon Fixes: 1699f65c8b65 ("kvm/x86: Fix 'lpages' kvm stat for TDM MMU") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Signed-off-by: Mingwei Zhang --- 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.554.ge1b32706d8-goog