Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp53167iog; Tue, 28 Jun 2022 16:12:29 -0700 (PDT) X-Google-Smtp-Source: AGRyM1saqCYNy6b/P4qj+jVaLOGU3zIQwIY7DGzOyyhX2tqfjYwS3xwsJIKOq4QVIcw9dIiIJS3k X-Received: by 2002:a17:903:2ca:b0:156:f1cc:7cb6 with SMTP id s10-20020a17090302ca00b00156f1cc7cb6mr5992084plk.174.1656457949390; Tue, 28 Jun 2022 16:12:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656457949; cv=none; d=google.com; s=arc-20160816; b=rNtoy6m2uvIQGHoUx+eguOJO5F3vyavjCZ2C0l2qbbGGtI+GNuMEShBhs+6STgSR/Y +v/FONwPEqAguXhusSwwtCSWi1HmcbRWsgNvc78EsQrUqRac7i03eQPBy61YCsLfBQcC 3SxtNSrZlHSdqoKbyBkrIiaoX+L8rn21nhpJVzjh1+WVkRQocerYnTJaPAno/KUfmcRU 8qbgOohDcuhWSBXi8kAB7B8JkOYltfS4ze0YtK8N9hgobQCn91V6KIouR9Y2EaFxrh/s OdFn4ooOAoBlzt3aYPDwVA9UNHhBNl92YPB7j2T2OlJ1gwPNgYQE/B7hWLIk82f+27eC 8J6g== 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:dkim-signature; bh=zRmORXBXFMKlgEhh847L9wk5+Tkdrd0vAZmkSGVZYF4=; b=gc7VCiFeUMqFyy29JvKsmcDy2z5lKkgs26lSo76XOBzjMRtcfGpPenzvqPQxw0hxCd uZZUclb71+M4CMKfhNSlSDlWFn5C8ca7fKmlbOdFsAPwzj+GKY0zUDiGFzEBE1S3TZq8 ecsZNKOX/tPajuM0lPqn0SCq9ezTbPi/+c4Lod73ahPzRunEoMd3QuL41nvsNFXsJIJP V9KZDyE9LTFV+TKzw8dgFE2N1TKWqCrfgJ6x0VxinGZSJoosDYYfoPekytYI29EDTHKe NCrmgwj7axQ36AW8JGI4GENDSofumqH4NUBgwNb0wqpcJBhAh+M73taOfIkPGpiB7yZy PbpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=fbkb1mcL; 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 x5-20020aa78f05000000b0051c31974ad5si12215444pfr.65.2022.06.28.16.11.53; Tue, 28 Jun 2022 16:12:29 -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=fbkb1mcL; 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 S230060AbiF1WJ4 (ORCPT + 99 others); Tue, 28 Jun 2022 18:09:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229917AbiF1WJu (ORCPT ); Tue, 28 Jun 2022 18:09:50 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03C16340E3 for ; Tue, 28 Jun 2022 15:09:49 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id jg5-20020a17090326c500b0016a020648bcso7591646plb.19 for ; Tue, 28 Jun 2022 15:09:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=zRmORXBXFMKlgEhh847L9wk5+Tkdrd0vAZmkSGVZYF4=; b=fbkb1mcL4JwMDC7INxQP/HLMkigu2+XSoRdqZvn/egsmloIZPHZzc0WSEqasztzAbL JwN3YXP4JXLtPXTjMdf06sqC/OcuK6YB7u7C1Abpg/vXJzO9+F6hCF6veT4GAIDg3QqM JegO43jEF/1XnMY80wr4LJhXCz7ZBPTSrQUveykF501sFMywVnbfqOBb7BI0No43zQ8K KQ3rZNWLlzxCPLtjWJnHHfIMuvBDD6O9R3L8/U0XL5Ab2yjBEZl1K/edqE7Ja4rCM9eN 962IWdUD2dBtGNrdnbdxRl0Om9n610p8gV0CeWzQv3iJf4MHjG5sGmfEf7fqf3qwkQZH e8pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=zRmORXBXFMKlgEhh847L9wk5+Tkdrd0vAZmkSGVZYF4=; b=JvGJd+A1AT8r3BXGE4jeFr2O+KLesgAhYe5U/sNhgQ2UKCfqa6IIRxy1Qu5JlStANa XHU9YVB23eUSfKagLt0m5VEyKGHmMyCrT909YSQ1jPn+zW1/hJA8+r94zbXf/vj98ZEu wgnd0ykw/e/GxrO6dQns6M4WFPDRsHdXSMKciH3Jd4LmuGB2bxgHzcnmcJMWePhN101p KkNUR99qA5Bl6kuZToyd7gM8kNfwvKgCsJbfefTfwZMFldC3qvk6nyHbxhB/WtoeoG4z FkZMGJi2DyhPJQHpqW1V9ATwsxQiXg9AGE9jqJo+KEbe5byMXxXjEL9zgZr9Z+bBPpJI mBIg== X-Gm-Message-State: AJIora8HAq310rvLA+rLb8DgeirF72f8RwKTqh/bqXpmPtM1hAjJDFgA kNJrBvn4S9701OwQUOuNyAqIZRd2m5aWxL7w X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a17:90b:180e:b0:1ec:d129:708 with SMTP id lw14-20020a17090b180e00b001ecd1290708mr53378pjb.235.1656454188475; Tue, 28 Jun 2022 15:09:48 -0700 (PDT) Date: Tue, 28 Jun 2022 22:09:37 +0000 In-Reply-To: <20220628220938.3657876-1-yosryahmed@google.com> Message-Id: <20220628220938.3657876-4-yosryahmed@google.com> Mime-Version: 1.0 References: <20220628220938.3657876-1-yosryahmed@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v6 3/4] KVM: x86/mmu: count KVM mmu usage in secondary pagetable stats. From: Yosry Ahmed To: Tejun Heo , Johannes Weiner , Zefan Li , Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Andrew Morton , Michal Hocko , Roman Gushchin , Shakeel Butt , Oliver Upton Cc: Huang@google.com, Shaoqin , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-mm@kvack.org, Yosry Ahmed Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_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 Count the pages used by KVM mmu on x86 in memory stats under secondary pagetable stats (e.g. "SecPageTables" in /proc/meminfo) to give better visibility into the memory consumption of KVM mmu in a similar way to how normal user page tables are accounted. Signed-off-by: Yosry Ahmed --- arch/x86/kvm/mmu/mmu.c | 16 ++++++++++++++-- arch/x86/kvm/mmu/tdp_mmu.c | 12 ++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index f7fa4c31b7c52..b1645202658ab 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1664,6 +1664,18 @@ static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, long nr) percpu_counter_add(&kvm_total_used_mmu_pages, nr); } +static void kvm_account_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp) +{ + kvm_mod_used_mmu_pages(kvm, +1); + kvm_account_pgtable_pages((void *)sp->spt, +1); +} + +static void kvm_unaccount_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp) +{ + kvm_mod_used_mmu_pages(kvm, -1); + kvm_account_pgtable_pages((void *)sp->spt, -1); +} + static void kvm_mmu_free_shadow_page(struct kvm_mmu_page *sp) { MMU_WARN_ON(!is_empty_shadow_page(sp->spt)); @@ -2123,7 +2135,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_shadow_page(struct kvm *kvm, */ sp->mmu_valid_gen = kvm->arch.mmu_valid_gen; list_add(&sp->link, &kvm->arch.active_mmu_pages); - kvm_mod_used_mmu_pages(kvm, +1); + kvm_account_mmu_page(kvm, sp); sp->gfn = gfn; sp->role = role; @@ -2450,7 +2462,7 @@ static bool __kvm_mmu_prepare_zap_page(struct kvm *kvm, list_add(&sp->link, invalid_list); else list_move(&sp->link, invalid_list); - kvm_mod_used_mmu_pages(kvm, -1); + kvm_unaccount_mmu_page(kvm, sp); } else { /* * Remove the active root from the active page list, the root diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index f3a430d64975c..3c5cb6054819a 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -372,6 +372,16 @@ static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn, } } +static void tdp_account_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp) +{ + kvm_account_pgtable_pages((void *)sp->spt, +1); +} + +static void tdp_unaccount_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp) +{ + kvm_account_pgtable_pages((void *)sp->spt, -1); +} + /** * tdp_mmu_unlink_sp() - Remove a shadow page from the list of used pages * @@ -384,6 +394,7 @@ static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn, static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp, bool shared) { + tdp_unaccount_mmu_page(kvm, sp); if (shared) spin_lock(&kvm->arch.tdp_mmu_pages_lock); else @@ -1136,6 +1147,7 @@ static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter, if (account_nx) account_huge_nx_page(kvm, sp); spin_unlock(&kvm->arch.tdp_mmu_pages_lock); + tdp_account_mmu_page(kvm, sp); return 0; } -- 2.37.0.rc0.161.g10f37bed90-goog