Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4382756ioa; Wed, 27 Apr 2022 02:33:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyL6o+MTklFEKPKUu/VLQWQKTYSdtD7tHg70IFk+LIRrKsYxpnMPhybDwnvX12DYwd9z1sW X-Received: by 2002:aa7:82d9:0:b0:4fa:2c7f:41e with SMTP id f25-20020aa782d9000000b004fa2c7f041emr29169308pfn.1.1651052011339; Wed, 27 Apr 2022 02:33:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651052011; cv=none; d=google.com; s=arc-20160816; b=TbDsBw6JTLof8wSQ4PVxuZY3kwgQ/csFSd5okkzOBUmQ4GBEcfx4u/Y3ohrCOT5/vV E6jL9aKPW/uMb/cqVuxCDM3zjnNZUXnpBGTNfZc3qCySBttP/i9hkD+NnNpaRfARCuLj b7uTlyzr1PECZyxxWFEFHbJhGOQKZD8P3NefOPtQhhu1OE6uC9mJz2/B4sruByyZfdnC ZQvy/vw9b6ekx26yffTjwoB7OGNs1UDLYklFrtRxEitfAcY5tqCNkr/pvAejxkQPaQSx k+RLPFRY+9SB5VLj95LIqcUPwKlm5QYjIb9WLr94T/Q/CYKcRJtOjwUVbTD+z2BoV5te Ku/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=iQFCk4jvRcDNbkwKt80r8M9Ev1zqOHCdGqGh5NtELGk=; b=TjbMLD714m69HDcDqJoMcY5CmXv67Ewcm3dhnxY7oroaTRjYyXtEheTKeUvsty5UJe uDy2yjZQUTPSX9OR2NPW4qUjOMnSBBS2KoG2ilNG2i3ENl61yjvAEVwzO4jYJLDJ0wTv XhVA6TtIgFHvfI2SiO9oArhZYFNGnG3rstouMiKIsDBkw5F0uj8JTWJttniiSg5ms9D2 Gi7bh4I7ZqrOSFTDXd+1MBSH5aPVf+zG5z88gpVQkcM8XfiBHAJLrXidEMPsqJflzB7X e+sEloS6gjxgziHPtj2zI92FtQCuEf8LCn8E8EMMuh3bQsgn8x86PR+6LmjKfBPoGq6t V13A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kXX2A5pP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id z8-20020aa78888000000b004faf1d84ebcsi918527pfe.279.2022.04.27.02.33.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 02:33:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kXX2A5pP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0DB6525A9CC; Wed, 27 Apr 2022 02:13:30 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354202AbiDZTcQ (ORCPT + 99 others); Tue, 26 Apr 2022 15:32:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346703AbiDZTcG (ORCPT ); Tue, 26 Apr 2022 15:32:06 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E65FAC04F for ; Tue, 26 Apr 2022 12:28:42 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id q20so11864733wmq.1 for ; Tue, 26 Apr 2022 12:28:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=iQFCk4jvRcDNbkwKt80r8M9Ev1zqOHCdGqGh5NtELGk=; b=kXX2A5pPeYzQ7cMHjUay8+MRco1JY+qcFRaRS08NAVmtfBRdvFVDmNjStpeUWkd6Zl vQU5f0FWDf6xsz3RbldyIy9/g5QXdZSQZuDeQifodlFGCf1YGsKpdQYyR2g9y7Y6oB7V /fsu0vO21DVFRug79wN6ff9wbCwuMNbpj0fPxn9wyJ+3cQTLW9PimSQ9f6xgnaxtH2fO AlrXyAXCzlK49e6n1Ju/HDLsl9jTbgxFH3GdBJYGeSidfswDSkOpdP8/KDLnQJLtqZUp IpGuwUWRrQLmAANIliRHGcWrJVnvTEjLAdmNwn2651HlpB30PBm/qWLfwGYSI3Qrg/mB i7nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=iQFCk4jvRcDNbkwKt80r8M9Ev1zqOHCdGqGh5NtELGk=; b=WvljTzucjF0uhGH2Np1aZkTR4AGgHmRV6ti0rrXkyHDI1xfIoaGsquqlMYdPXEyyy+ m5+BpfTBqFvfi5pMJ2Q8lAolqVr8ahsVbrZhZG+/YgotDJw71S+WzZd74H5OQvFowTwS xuX4w5CoUvXvtWZ8e/8+ZxCr84G4uGEAsrbzK+o3p/zeo3Db4qvbsMYpKuE3Qa2aOfWQ VBWR5ZDd9nMsQupPYhp5DPy+L9SJn2NPd2xUHZoFTOyhoTh2QwITw5hzCttNyKdb6NTL PjO4MkrPtqLmt1zLXvIKPQtd63NbUuJuH12UThVbno+NBmZ+NhbDjM/V/FGxVQsWUvUh +kpw== X-Gm-Message-State: AOAM531t0GhjCWcsAqKPokIU/3iua1sYjj3P1qDudVAlLpg5IwyfLNyi Ua9bAQrafXlYDEr6D0Rq+ZD0DXS986sI53NeWebW+Q== X-Received: by 2002:a05:600c:1c90:b0:393:e5b9:b567 with SMTP id k16-20020a05600c1c9000b00393e5b9b567mr16137822wms.27.1651001314470; Tue, 26 Apr 2022 12:28:34 -0700 (PDT) MIME-Version: 1.0 References: <20220426053904.3684293-1-yosryahmed@google.com> <20220426053904.3684293-5-yosryahmed@google.com> In-Reply-To: From: Yosry Ahmed Date: Tue, 26 Apr 2022 12:27:57 -0700 Message-ID: Subject: Re: [PATCH v3 4/6] KVM: arm64/mmu: count KVM page table pages in pagetable stats To: Oliver Upton Cc: 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 , linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Linux Kernel Mailing List , linux-fsdevel@vger.kernel.org, Linux-MM , cgroups@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,USER_IN_DEF_DKIM_WL autolearn=no 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 Hi Oliver, Thanks so much for taking the time to take a look at this! On Tue, Apr 26, 2022 at 12:35 AM Oliver Upton wrote: > > Hi Yosry, > > On Tue, Apr 26, 2022 at 05:39:02AM +0000, Yosry Ahmed wrote: > > Count the pages used by KVM in arm64 for page tables in pagetable stats. > > > > Account pages allocated for PTEs in pgtable init functions and > > kvm_set_table_pte(). > > > > Since most page table pages are freed using put_page(), add a helper > > function put_pte_page() that checks if this is the last ref for a pte > > page before putting it, and unaccounts stats accordingly. > > > > Signed-off-by: Yosry Ahmed > > --- > > arch/arm64/kernel/image-vars.h | 3 ++ > > arch/arm64/kvm/hyp/pgtable.c | 50 +++++++++++++++++++++------------- > > 2 files changed, 34 insertions(+), 19 deletions(-) > > > > diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h > > index 241c86b67d01..25bf058714f6 100644 > > --- a/arch/arm64/kernel/image-vars.h > > +++ b/arch/arm64/kernel/image-vars.h > > @@ -143,6 +143,9 @@ KVM_NVHE_ALIAS(__hyp_rodata_end); > > /* pKVM static key */ > > KVM_NVHE_ALIAS(kvm_protected_mode_initialized); > > > > +/* Called by kvm_account_pgtable_pages() to update pagetable stats */ > > +KVM_NVHE_ALIAS(__mod_lruvec_page_state); > > + > > #endif /* CONFIG_KVM */ > > > > #endif /* __ARM64_KERNEL_IMAGE_VARS_H */ > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > > index 2cb3867eb7c2..53e13c3313e9 100644 > > --- a/arch/arm64/kvm/hyp/pgtable.c > > +++ b/arch/arm64/kvm/hyp/pgtable.c > > @@ -152,6 +152,7 @@ static void kvm_set_table_pte(kvm_pte_t *ptep, kvm_pte_t *childp, > > > > WARN_ON(kvm_pte_valid(old)); > > smp_store_release(ptep, pte); > > + kvm_account_pgtable_pages((void *)childp, +1); > > What page tables do we want to account? KVM on ARM manages several page > tables. > > For regular KVM, the host kernel manages allocations for the hyp stage 1 > tables in addition to the stage 2 tables used for a particular VM. The > former is system overhead whereas the latter could be attributed to a > guest VM. Honestly I would love to get your input on this. The main motivation here is to give users insights on the kernel memory usage on their system (or in a cgroup). We currently have NR_PAGETABLE stats for normal kernel page tables (allocated using __pte_alloc_one()/pte_free()), this shows up in /proc/meminfo, /path/to/cgroup/memory.stat, and node stats. The idea is to add NR_SECONDARY_PAGETABLE that should include the memory used for kvm pagetables, which should be a separate category (no overlap). What gets included or not depends on the semantics of KVM and what exactly falls under the category of secondary pagetables from the user's pov. Currently it looks like s2 page table allocations get accounted to kmem of memory control groups (GFP_KERNEL_ACCOUNT), while hyp page table allocations do not (GFP_KERNEL). So we could either follow this and only account s2 page table allocations in the stats, or make hyp allocations use GFP_KERNEL_ACCOUNT as well and add them to the stats. Let me know what you think. > > I imagine protected KVM is out of scope, since it actually manages its > own allocations outside of the host kernel. > > Given this, I would recommend adding the accounting hooks to mmu.c as > that is where we alloc/free table pages and it is in the host address > space. kvm_s2_mm_ops and kvm_hyp_mm_ops point to all the relevant > functions, though the latter is only relevant if we want to count system > page tables too. Yeah moving the accounting hooks to mmu.c is much cleaner, I will do this in the next version. The only reason I did not do this is that I found other kvm_pgtable_mm_ops structs (such as pkvm_pgtable_mm_ops), but it looks like these may be irrelevant here. > > -- > Thanks, > Oliver