From patchwork Tue Aug 23 00:46:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 12951575 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 E2343C32774 for ; Tue, 23 Aug 2022 00:46:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7A2D78D0007; Mon, 22 Aug 2022 20:46:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 753408D0001; Mon, 22 Aug 2022 20:46:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57EFA8D0007; Mon, 22 Aug 2022 20:46:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 41F958D0001 for ; Mon, 22 Aug 2022 20:46:52 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 18760C135C for ; Tue, 23 Aug 2022 00:46:52 +0000 (UTC) X-FDA: 79829017464.27.9921D25 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf11.hostedemail.com (Postfix) with ESMTP id C8A9C40003 for ; Tue, 23 Aug 2022 00:46:51 +0000 (UTC) Received: by mail-pl1-f201.google.com with SMTP id p18-20020a170902a41200b00172b0dc71e0so8135400plq.0 for ; Mon, 22 Aug 2022 17:46:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=5niTR1/vKfTFRgtx85YXkvJ6CCfCYM2aDePdp0OgEpE=; b=G8TGuUcHSEHIGStRwECqzrlZj0Y0NjCpx35ggoYoz2hySK3lBHVk/kHKixSi+zm9uf MMELcNyl/ILyCYybMJq3NgCRXdRooTSjLTqNE95ShBUqpEYWDlVGs9X6/eOvD2uHKBnc 1eOI6YdZW4CxnGDRVcfeukqj4Xq9UqrleMaIH6cnwLzBFm7Qf6vybqxxIvdJhNGOz+8C ihIqjvUkmeFHqLEqwJwD6UUnIrpd44ZFrMCykfwoRHmerknP9Og/MaOQQe6IQxIqTCT6 M1HQPImVz1VcOhPzbblEqC4V49TpD0sAlHRGMGN/Ld5paJbwvTceb1Scd7ZEwI7OxFFk 7weg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=5niTR1/vKfTFRgtx85YXkvJ6CCfCYM2aDePdp0OgEpE=; b=zV1OaFVG0VUsTXcGm+Qa36CmUwLExmKH7DeXfF/NdzL/YKNHChZ92scZZEat2Ww0C+ EbTP3MrMUPa8rVpq4u1k/wuwzYkTXLgGbVIcxgdkx1Hv0Rg80jbqXCr0gM8S51gU9t6v TM7ZNADht8rwtZ0Vc8T6U0Vdu4eYyy4LSZ5aH+f/RL5b8BbbEz32t+NabUF9dDpP1M+N HO2MChno0f5MoTl8j7H0mZ+EiEvVBcFdTVjV97BC9UjXK0IT67/5lHo7DIIo2MN2k3PM pEOtPmwTBfDsTHCJGxJPxtyyL6XM87AHS3/0TWkI4wW01ULGywepc3GxBU8BMAef0FLc LWBw== X-Gm-Message-State: ACgBeo2g1WR5K3Mjfz7Q8UTCARQHPsiIOgeLhJ5qDkkIYxvcZMbkRnYc r4r8O4xlTnmwXXIx9U8t1ga6sOBVKcMS3wYG X-Google-Smtp-Source: AA6agR6PQbVcJtZOJoJgA+LAQcdkbmjco2mwIDMXQcfDdzMC5Biz4IRKLTO6FeVPDLne3VNXdMZ+DrmLbqf2VuOI X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a17:90a:249:b0:1e0:a8a3:3c6c with SMTP id t9-20020a17090a024900b001e0a8a33c6cmr78092pje.0.1661215610056; Mon, 22 Aug 2022 17:46:50 -0700 (PDT) Date: Tue, 23 Aug 2022 00:46:39 +0000 In-Reply-To: <20220823004639.2387269-1-yosryahmed@google.com> Message-Id: <20220823004639.2387269-5-yosryahmed@google.com> Mime-Version: 1.0 References: <20220823004639.2387269-1-yosryahmed@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH v7 4/4] KVM: arm64/mmu: count KVM s2 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 , Oliver Upton ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661215611; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5niTR1/vKfTFRgtx85YXkvJ6CCfCYM2aDePdp0OgEpE=; b=1ayGn9qq0RGw07iGAMOBP3ZkVPckEbr6ryaMlZDLRQSiJsVr0F2lkLNfSsrJS1UXhgx4hV hCD0zooDqXnLrVOXQ64ON9ysj10Bk5+mj2xoW841RZBiK6TiPi2GuFjY4/EkOT2FNOfTAd AK3Clft3dRD2Mp3SlMoCOl+mcTAJUDw= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=G8TGuUcH; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3eiMEYwoKCO8pfjipRYdVUXffXcV.TfdcZelo-ddbmRTb.fiX@flex--yosryahmed.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3eiMEYwoKCO8pfjipRYdVUXffXcV.TfdcZelo-ddbmRTb.fiX@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661215611; a=rsa-sha256; cv=none; b=uYl81x5b1HkHCpS5W0v2hozvr4QQhcJr2m1xDQ3g6m1H17HOWOrlQbu2D9P+WbacegREJE yBu32PNxSB5RkgGOVrrmXqqjYuAIOUt7D6Q25nkfWdXeJg/TFi1H1UCye/Rgwqklayv8Ee mlzuigk3U8u6oZyk+FA3zecIuGb8tu8= X-Rspamd-Queue-Id: C8A9C40003 X-Rspam-User: Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=G8TGuUcH; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3eiMEYwoKCO8pfjipRYdVUXffXcV.TfdcZelo-ddbmRTb.fiX@flex--yosryahmed.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3eiMEYwoKCO8pfjipRYdVUXffXcV.TfdcZelo-ddbmRTb.fiX@flex--yosryahmed.bounces.google.com X-Rspamd-Server: rspam03 X-Stat-Signature: cgkn9putp5g9xxhdi3hxyqtcf5rky5qx X-HE-Tag: 1661215611-867896 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 arm64 for stage2 mmu 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 Reviewed-by: Oliver Upton Reviewed-by: Marc Zyngier --- arch/arm64/kvm/mmu.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index c9a13e487187..34c5feed9dc1 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -92,9 +92,13 @@ static bool kvm_is_device_pfn(unsigned long pfn) static void *stage2_memcache_zalloc_page(void *arg) { struct kvm_mmu_memory_cache *mc = arg; + void *virt; /* Allocated with __GFP_ZERO, so no need to zero */ - return kvm_mmu_memory_cache_alloc(mc); + virt = kvm_mmu_memory_cache_alloc(mc); + if (virt) + kvm_account_pgtable_pages(virt, 1); + return virt; } static void *kvm_host_zalloc_pages_exact(size_t size) @@ -102,6 +106,21 @@ static void *kvm_host_zalloc_pages_exact(size_t size) return alloc_pages_exact(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO); } +static void *kvm_s2_zalloc_pages_exact(size_t size) +{ + void *virt = kvm_host_zalloc_pages_exact(size); + + if (virt) + kvm_account_pgtable_pages(virt, (size >> PAGE_SHIFT)); + return virt; +} + +static void kvm_s2_free_pages_exact(void *virt, size_t size) +{ + kvm_account_pgtable_pages(virt, -(size >> PAGE_SHIFT)); + free_pages_exact(virt, size); +} + static void kvm_host_get_page(void *addr) { get_page(virt_to_page(addr)); @@ -112,6 +131,15 @@ static void kvm_host_put_page(void *addr) put_page(virt_to_page(addr)); } +static void kvm_s2_put_page(void *addr) +{ + struct page *p = virt_to_page(addr); + /* Dropping last refcount, the page will be freed */ + if (page_count(p) == 1) + kvm_account_pgtable_pages(addr, -1); + put_page(p); +} + static int kvm_host_page_count(void *addr) { return page_count(virt_to_page(addr)); @@ -625,10 +653,10 @@ static int get_user_mapping_size(struct kvm *kvm, u64 addr) static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { .zalloc_page = stage2_memcache_zalloc_page, - .zalloc_pages_exact = kvm_host_zalloc_pages_exact, - .free_pages_exact = free_pages_exact, + .zalloc_pages_exact = kvm_s2_zalloc_pages_exact, + .free_pages_exact = kvm_s2_free_pages_exact, .get_page = kvm_host_get_page, - .put_page = kvm_host_put_page, + .put_page = kvm_s2_put_page, .page_count = kvm_host_page_count, .phys_to_virt = kvm_host_va, .virt_to_phys = kvm_host_pa,