Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3748228pxv; Mon, 26 Jul 2021 10:57:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXNocmf2mNg/AeNsm8a3GYFaoUx+Lhqbp4TfHHC5LH0omldkfHZkW2pLEbeEU19RlfHWPy X-Received: by 2002:a17:906:4e12:: with SMTP id z18mr18170676eju.543.1627322255461; Mon, 26 Jul 2021 10:57:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627322255; cv=none; d=google.com; s=arc-20160816; b=tN8QwCNmRuqv1dD0sEOsBkjumH5xAuNO5UKEgiHGXc/74jgR7sqJ2nn6g1YoICzFPY XJYrtqO7D6iNuc23MWrwZCg3H2hHr52n7MpbZ61pjhhYgDAh/M2saPE3hmMk/pmi+/DM x7a8m/uVQ/kQ8xP3UC/G0QPMuc3V6IxgM1kPw5oBySJlzCruKxOdMx116xFw8Tsr0X75 b70jaQdwIsmJJKwHLVNFpoL/ye7YxR4mAeuh1Zt3rV34PVvsZlrYEE9Swh4z1ZE8lo11 CMJK3rLAShBA2MYZNa1+wtm0SUr/exuH2K8Y48UKd78vesMP9ThqGtqfsAy2DmBMaidP E05g== 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=Pod+cFJd0HRiOKBwqMg7GhCqe0Dwn2Ux12SPPveDuz0=; b=eaeRO2rzCoCfFb7ibLgGKkwVXMa/dKB9Ezet5BdO0pKmq2oHDR1TwrLN44Kp54qxmx c9YZ6r4HXWSkcOOemSlhD7dlEFOau89J/H12XLro4mTzw2C2qAVpGXVCOM16eiOnXzi/ oxQVvi2Ge2LsAsZL+SHAiWGhHZBIO4xIU4sWoBVCQyK+ofaSm3P5AnjA2bxwRPiQa5CC RQHyekCwo2fx0b+Dr8ehOPhbGRfJJcuXSK1FV5KUs105+s+5Ko7yluJoMWiHnejfGa7g 5vVFSmpYRJeG67KWmMJsDZIcYlowvMtGuimwxSvjdb6No1bc7yaVDVB0WASiiVrvfDrh eNgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=r+Vp2vQ9; 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 dn25si540142edb.332.2021.07.26.10.57.11; Mon, 26 Jul 2021 10:57:35 -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=r+Vp2vQ9; 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 S232280AbhGZRNo (ORCPT + 99 others); Mon, 26 Jul 2021 13:13:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231962AbhGZRNk (ORCPT ); Mon, 26 Jul 2021 13:13:40 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F5B9C061760 for ; Mon, 26 Jul 2021 10:54:09 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id o7-20020ac87c470000b029025f8084df09so4880777qtv.6 for ; Mon, 26 Jul 2021 10:54:09 -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=Pod+cFJd0HRiOKBwqMg7GhCqe0Dwn2Ux12SPPveDuz0=; b=r+Vp2vQ9xFmyHkJGZ11F9CGL8HamVo2WBZjEfRxh2RXs7ddPgtjbVo0ojL884xRRaz qawzYDy485UOCeY4sqSPuiQB81fgVquVEaJ0s3p2kShy3NTMobaR6XuXwcA0Wt4yhEkP vafRx3UOsz+8eJX9bFRQfZqX2R41KmFnjllsB64KJtTPgYoqERcUpoY5hdi6r7N1GbUG W0ag/TaCDPHDxvE3HOuFGcn2LU1meGZbzKU6M+15DGls7mkfSAeD8iEbSCKLi5AxfZRh HANlVlXM9z+zpUF55l/7Nr07By4g58AU1k22xiJcXkZfxGOsaMSmBHvZoDf+38Bc9Smn TIKA== 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=Pod+cFJd0HRiOKBwqMg7GhCqe0Dwn2Ux12SPPveDuz0=; b=bTaqDnf2PijHA3DK57pqtSNZBY2bmo+HcMOFwtaEaxVMXpEuo4nT/VT6IayIwO2kLQ XL33b7Ii52PycUQgH1Wd8IcbKBXSKOVXpXtoKRDEGd6pgcDxiDkmg2pm72wmGect8TP8 3geleeYC6sO6/TlIU5S2EsPxvT3caDk8rganXtX8SoFzPBk/OAyLoJq0VjjAZ3KY4BM2 9pnQ7y5sYIRZuutqRNC+u2OjYIwBwSMYQ5BS5C9uL9N5etMpgAtpKZB0WP5PoOVPY5h+ DSGWu+47/b/NzyACJCgJ06hBuuuuJGkmaSMYaVX/n66A6kMjvXTKPRIOhzmvrx//Otf5 jCqw== X-Gm-Message-State: AOAM533bctZgrd66S0kZYj2kZmqQIMQ67GIyvpR1RKbAH5dMpk8YHHDS 0npoYsS9+gIVC17US/GN+Vb0qJjeLzYV X-Received: from mihenry-linux-desktop.kir.corp.google.com ([2620:15c:29:204:93c5:105:4dbc:13cf]) (user=mizhang job=sendgmr) by 2002:a0c:e70f:: with SMTP id d15mr18640267qvn.47.1627322048130; Mon, 26 Jul 2021 10:54:08 -0700 (PDT) Reply-To: Mingwei Zhang Date: Mon, 26 Jul 2021 10:53:56 -0700 In-Reply-To: <20210726175357.1572951-1-mizhang@google.com> Message-Id: <20210726175357.1572951-3-mizhang@google.com> Mime-Version: 1.0 References: <20210726175357.1572951-1-mizhang@google.com> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog Subject: [PATCH v2 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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