From patchwork Tue Apr 26 05:39:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 12826477 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 30F57C433FE for ; Tue, 26 Apr 2022 05:39:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7DC666B0082; Tue, 26 Apr 2022 01:39:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 763CF6B0081; Tue, 26 Apr 2022 01:39:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 546BE6B0082; Tue, 26 Apr 2022 01:39:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id 3EA1C6B0080 for ; Tue, 26 Apr 2022 01:39:27 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 170F612077C for ; Tue, 26 Apr 2022 05:39:27 +0000 (UTC) X-FDA: 79397927574.19.021FC4E Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf14.hostedemail.com (Postfix) with ESMTP id B7257100041 for ; Tue, 26 Apr 2022 05:39:25 +0000 (UTC) Received: by mail-pf1-f202.google.com with SMTP id 67-20020a621846000000b0050d22f49732so5116064pfy.14 for ; Mon, 25 Apr 2022 22:39:26 -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=a9VzBJlMLigws4pRoWaDUwEnDDiqJQ0nJjM8cxStkWo=; b=p8mJqCwHacTbFhvYHu8ybGXK2P+PghFMqaxHHJuqdpta07t/m8JQTkdFg82eEvuWYd OdSYMSKPBvO7ptjR046sAX/bHvFVjhg/pzuwVzXmGfoIsID/2GXXQHWYzh25sp8Zhqzf htWeN1iMzxl7/9Pdv1zinxHd6q+dx76sncvVGGjZOTWwwfxxB2qSbB8gn6IY5+O3yc7/ BeF9G0WjghrY1fZ5jCmiJkVNg5fp3DJ1BPeCzpmLCtKqzsEWqGLssoJmyaPfwKCC+CiD 5TQ/VweVfCM1sVLfYaViWaZEyQtlCxE0YpkORn8opIIBD11EyDSBFcvRbtm+lxTTiRjt 7Vwg== 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=a9VzBJlMLigws4pRoWaDUwEnDDiqJQ0nJjM8cxStkWo=; b=uuYLTZAZvbXrlcUJbdZqLPpEsAxQAmKLjtcjK/ZBFBaFrGXnjbwBH6x2uZeOuHeTxR ovGdEFRDiXXXDQopovLfVSzgbdt37X0dU2QeB6Fp2obxahzJ3k49pdaFb2rvB2BHZNN1 2XLg86TC8jigmiY2P9QkbPNaKxxQJt11ku/ezMevpKYWJjwd1P9j4yGjyA+J4E+OP5wD C5aqLu/UGoo7yjvx02Nd7TvMS/UALJ4fgRtg4MR56BMyEvAsQbGov3kLYpM0Wm+f0+4Y pP0Xe6L1JKeZHbMKih2npJWbCzc5HYQUtby/HagiACub4/SLUxuT0nXLcQVcUF/XUVPh CiuQ== X-Gm-Message-State: AOAM533ln0sp+eZIY4C3qAhj21DcFXGS/6gktmvf9750S4bwxg/bTb4a btMFjvtTRXHbjT5XZ62lhgtGV5GhA24XsqMW X-Google-Smtp-Source: ABdhPJxYCaRW6ujjy+jD12c3CK0Xph6XJaTEbXPAfsnpbu06l91YAgqtjLNqwlREDx07xf+SlrnmNlHPGW+NrZOR X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a63:de12:0:b0:3ab:7c33:2894 with SMTP id f18-20020a63de12000000b003ab7c332894mr3565024pgg.187.1650951565540; Mon, 25 Apr 2022 22:39:25 -0700 (PDT) Date: Tue, 26 Apr 2022 05:39:03 +0000 In-Reply-To: <20220426053904.3684293-1-yosryahmed@google.com> Message-Id: <20220426053904.3684293-6-yosryahmed@google.com> Mime-Version: 1.0 References: <20220426053904.3684293-1-yosryahmed@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v3 5/6] KVM: riscv/mmu: count KVM page table pages in pagetable stats From: Yosry Ahmed To: Sean Christopherson , Huacai Chen , Aleksandar Markovic , Anup Patel , Atish Patra , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , James Morse , Catalin Marinas , Shameer Kolothum , Marc Zyngier , Alexandru Elisei , Suzuki K Poulose Cc: linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, Yosry Ahmed X-Rspamd-Queue-Id: B7257100041 X-Stat-Signature: ah69zx8b5itae5ax9z5zyizputsxa8qz X-Rspam-User: Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=p8mJqCwH; spf=pass (imf14.hostedemail.com: domain of 3jYVnYgoKCFAG6A9Gsz4wvy66y3w.u64305CF-442Dsu2.69y@flex--yosryahmed.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3jYVnYgoKCFAG6A9Gsz4wvy66y3w.u64305CF-442Dsu2.69y@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam09 X-HE-Tag: 1650951565-387353 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Count the pages used by KVM in riscv for page tables in pagetable stats. Signed-off-by: Yosry Ahmed --- arch/riscv/kvm/mmu.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index f80a34fbf102..fcfb75713750 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -152,6 +152,7 @@ static int stage2_set_pte(struct kvm *kvm, u32 level, next_ptep = kvm_mmu_memory_cache_alloc(pcache); if (!next_ptep) return -ENOMEM; + kvm_account_pgtable_pages((void *)next_ptep, +1); *ptep = pfn_pte(PFN_DOWN(__pa(next_ptep)), __pgprot(_PAGE_TABLE)); } else { @@ -229,6 +230,7 @@ static void stage2_op_pte(struct kvm *kvm, gpa_t addr, pte_t *next_ptep; u32 next_ptep_level; unsigned long next_page_size, page_size; + struct page *p; ret = stage2_level_to_page_size(ptep_level, &page_size); if (ret) @@ -252,8 +254,13 @@ static void stage2_op_pte(struct kvm *kvm, gpa_t addr, for (i = 0; i < PTRS_PER_PTE; i++) stage2_op_pte(kvm, addr + i * next_page_size, &next_ptep[i], next_ptep_level, op); - if (op == STAGE2_OP_CLEAR) - put_page(virt_to_page(next_ptep)); + if (op == STAGE2_OP_CLEAR) { + p = virt_to_page(next_ptep); + if (page_count(p) == 1) + kvm_account_pgtable_pages((void *)next_ptep, + -1); + put_page(p); + } } else { if (op == STAGE2_OP_CLEAR) set_pte(ptep, __pte(0)); @@ -700,25 +707,27 @@ int kvm_riscv_stage2_map(struct kvm_vcpu *vcpu, int kvm_riscv_stage2_alloc_pgd(struct kvm *kvm) { struct page *pgd_page; + int order; if (kvm->arch.pgd != NULL) { kvm_err("kvm_arch already initialized?\n"); return -EINVAL; } - pgd_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, - get_order(stage2_pgd_size)); + order = get_order(stage2_pgd_size); + pgd_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); if (!pgd_page) return -ENOMEM; kvm->arch.pgd = page_to_virt(pgd_page); kvm->arch.pgd_phys = page_to_phys(pgd_page); - + kvm_account_pgtable_pages((void *)kvm->arch.pgd, +(1UL << order)); return 0; } void kvm_riscv_stage2_free_pgd(struct kvm *kvm) { void *pgd = NULL; + int order; spin_lock(&kvm->mmu_lock); if (kvm->arch.pgd) { @@ -729,8 +738,11 @@ void kvm_riscv_stage2_free_pgd(struct kvm *kvm) } spin_unlock(&kvm->mmu_lock); - if (pgd) - free_pages((unsigned long)pgd, get_order(stage2_pgd_size)); + if (pgd) { + order = get_order(stage2_pgd_size); + kvm_account_pgtable_pages((void *)pgd, -(1UL << order)); + free_pages((unsigned long)pgd, order); + } } void kvm_riscv_stage2_update_hgatp(struct kvm_vcpu *vcpu)