From patchwork Wed Nov 1 23:08:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sourav Panda X-Patchwork-Id: 16161 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp10163vqu; Wed, 1 Nov 2023 16:08:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWLuB4GO4naU/2iQgIPCogeCk4HONMKvgPW4xLJbdm+7j0wdC+HlqLamjKQgwEdE3uUfNu X-Received: by 2002:a17:903:1cd:b0:1cc:4073:88ad with SMTP id e13-20020a17090301cd00b001cc407388admr10713072plh.29.1698880128490; Wed, 01 Nov 2023 16:08:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698880128; cv=none; d=google.com; s=arc-20160816; b=U7L+vTMhe1+ZDEhLI0yqnEiwExgLnX2WEynpHz/PqSDSmzfn3onY9KM7nt6eHLh9gW jHPon0s6m0UfCaGZD0eq9/Xf1bQixG8TJmEfq/UWlDMe7hQcfT2mZgstYqCRrgY+fNbb kd29P4KN2L6rpld9NyCC/dZ+JzUDx9M24LLEYQltsAXdRyWHSWeteOeosaPBBU+hpsd4 NEYhwgUhsM20dHQWDcGJl/uzArOaK+xkyqyFTO4qg5OHC+nUnDDXUfb3fnqn4rtHOBDz f6r8cK1cYDn38y7js9N2hFXI22gsNRGevroUE01ivcg/PumEOuwL77AYy37EmXbXp4n0 5MXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:message-id:mime-version:date :dkim-signature; bh=5qWWijUVxf0dBo3Nq+4AwtcGaRZKaTUhIgmJU1h7y10=; fh=3KAeh75kSaTXAYbfgkJgJ72uwnVS82YU+YKFfx7rW3c=; b=v1DqYzTEGdQ43GfguBMWr48eP08ZfrHT8SPXci4ZloKKXQu7/r6NgWZ3yKSy8K8RFl Rsh7dwbuA/aFki+MwAVG7oAIT30ITZ+fIyvcHwPQxJIZ89BE25bN1AF+kd+MorsjsJX1 VVTfK9Np8pDxNoYfIg6hlp3lvTYT9vGPEQgzZkLRD5MMeuqsE7S17fRNG2r+19ZrkIPk YwW8pBVo1iyiXjhae0mc+LLzzboQsEHVyk6PIKWimWYLCakPotu3lxvMvD4cm3fiaYfp dNDOhCCKGoaTEhBPMm54eNS9uiW3zFDs5iT8vL8h/OBwRfVGR4C86gDnX1ueygde4Tvc b3IA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="vS7A/Awz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id jx15-20020a170903138f00b001c732b6869csi3818964plb.63.2023.11.01.16.08.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 16:08:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="vS7A/Awz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 7188981A3355; Wed, 1 Nov 2023 16:08:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234965AbjKAXIX (ORCPT + 35 others); Wed, 1 Nov 2023 19:08:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231526AbjKAXIW (ORCPT ); Wed, 1 Nov 2023 19:08:22 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 598E6101 for ; Wed, 1 Nov 2023 16:08:19 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9cb10a5d44so188916276.0 for ; Wed, 01 Nov 2023 16:08:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698880098; x=1699484898; darn=vger.kernel.org; h=to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=5qWWijUVxf0dBo3Nq+4AwtcGaRZKaTUhIgmJU1h7y10=; b=vS7A/AwzFuO6c9Wg8F034uLUYOAV13dOAuEist4CoQYvCVmgKZz52Gl5NLwoL7Rn80 p9JPyphNGf3OidqQ3aCvnnwaptX2e/dmUm6stsGMUl0fw8dqS5G1Ff5Sy/LWIfkmJcG/ h5Ilp6CGIAe0aOLb0j7Dg6iZ5ZO1/uMIKkz/DDuwKT11v/yvkkrMDxrxDmlRYufAUSCQ quCw7TzMbcGCk/ek+TI3RPgaNN2wsWTEzv6PTfRXt6A1xclPi2TzzPLHlO74rHO8eJlo 6hPSmfPPJxY5hG1GofEJonwumRtbRhMAzT72dzx2WicJaD7A33j468boPImPpeavjwBk oZUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698880098; x=1699484898; h=to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=5qWWijUVxf0dBo3Nq+4AwtcGaRZKaTUhIgmJU1h7y10=; b=KJrFxfQfeH4YzpcoiFA5Q5ujgSpbjPISUIucBbGA1zt+qr+su3YfEFylQB7H3SNIa3 R73hpLkjAEPVRLHBnYNwwecLvt4/rR24psTwcb36g9INUEbUbRyNk4sZSIEjqfW8DcK5 O/YwrgCrT7jgM4leroDL82lcc+kavlC2rCe2wCql9578PXdvFORKx2jGTWU4rUgthVVh p2+REYa+z8SgMvZA1UtgyJaZnfKFeBnVWupkebtHTOtwxDlLUFMPqU1CPXU9KhGrKWKH qAg00bhr/agnQwgPwGFtGNUY1m6CuaRlleIKyM9S/tEFtKuHC/BG1v1P8xUustWWFIg+ 59yw== X-Gm-Message-State: AOJu0YwP5eKyvByYdiqM4Sjc+dFZ0H6nVZ4GQAWF8OqMvkB0GwoIWB1o rxQWTBlO5cUpk7VYjISHk1i6cM1/8DGkydZeng== X-Received: from souravpanda.svl.corp.google.com ([2620:15c:2a3:200:9bf9:be0e:41ed:e474]) (user=souravpanda job=sendgmr) by 2002:a25:abc7:0:b0:da0:6216:798f with SMTP id v65-20020a25abc7000000b00da06216798fmr238035ybi.13.1698880098507; Wed, 01 Nov 2023 16:08:18 -0700 (PDT) Date: Wed, 1 Nov 2023 16:08:15 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231101230816.1459373-1-souravpanda@google.com> Subject: [PATCH v5 0/1] mm: report per-page metadata information From: Sourav Panda To: corbet@lwn.net, gregkh@linuxfoundation.org, rafael@kernel.org, akpm@linux-foundation.org, mike.kravetz@oracle.com, muchun.song@linux.dev, rppt@kernel.org, david@redhat.com, rdunlap@infradead.org, chenlinxuan@uniontech.com, yang.yang29@zte.com.cn, souravpanda@google.com, tomas.mudrunka@gmail.com, bhelgaas@google.com, ivan@cloudflare.com, pasha.tatashin@soleen.com, yosryahmed@google.com, hannes@cmpxchg.org, shakeelb@google.com, kirill.shutemov@linux.intel.com, wangkefeng.wang@huawei.com, adobriyan@gmail.com, vbabka@suse.cz, Liam.Howlett@Oracle.com, surenb@google.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, willy@infradead.org, weixugc@google.com 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=ham 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 01 Nov 2023 16:08:35 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781404929396595669 X-GMAIL-MSGID: 1781404929253165193 Changelog: v5: - Addressed comments from Muchun Song. - Fixed errors introduced in v4 when CONFIG_SPARSEMEM_VMEMMAP is disabled by testing against FLATMEM and SPARSEMEM memory models. - Handled the condition wherein the allocation of walk.reuse_page fails, by moving NR_PAGE_METADATA update into the clause if( walk.reuse_page ). - Removed the usage of DIV_ROUND_UP from alloc_vmemmap_page_list since "end - start" is always a multiple of PAGE_SIZE. - Modified alloc_vmemmap_page_list to update NR_PAGE_METADATA once instead of every loop. v4: - Addressed comment from Matthew Wilcox. - Used __node_stat_sub_folio and __node_stat_add_folio instead of __mod_node_page_state in mm/hugetlb.c. - Used page_pgdat wherever possible in the entire patch. - Used DIV_ROUND_UP() wherever possible in the entire patch. v3: - Addressed one comment from Matthew Wilcox. - In free_page_ext, page_pgdat() is now extracted prior to freeing the memory. v2: - Fixed the three bugs reported by kernel test robot. - Enhanced the commit message as recommended by David Hildenbrand. - Addressed comments from Matthew Wilcox: - Simplified alloc_vmemmap_page_list() and free_page_ext() as recommended. - Used the appropriate comment style in mm/vmstat.c. - Replaced writeout_early_perpage_metadata() with store_early_perpage_metadata() to reduce ambiguity with what swap does. - Addressed comments from Mike Rapoport: - Simplified the loop in alloc_vmemmap_page_list(). - Could NOT address a comment to move store_early_perpage_metadata() near where nodes and page allocator are initialized. - Included the vmalloc()ed page_ext in accounting within free_page_ext(). - Made early_perpage_metadata[MAX_NUMNODES] static. Previous approaches and discussions ----------------------------------- v4: https://lore.kernel.org/all/20231031223846.827173-1-souravpanda@google.com v3: https://lore.kernel.org/all/20231031174459.459480-1-souravpanda@google.com v2: https://lore.kernel.org/all/20231018005548.3505662-1-souravpanda@google.com v1: https://lore.kernel.org/r/20230913173000.4016218-2-souravpanda@google.com Hi! This patch adds a new per-node PageMetadata field to /sys/devices/system/node/nodeN/meminfo and a global PageMetadata field to /proc/meminfo. This information can be used by users to see how much memory is being used by per-page metadata, which can vary depending on build configuration, machine architecture, and system use. Per-page metadata is the amount of memory that Linux needs in order to manage memory at the page granularity. The majority of such memory is used by "struct page" and "page_ext" data structures. Background ---------- Kernel overhead observability is missing some of the largest allocations during runtime, including vmemmap (struct pages) and page_ext. This patch aims to address this problem by exporting a new metric PageMetadata. On the contrary, the kernel does provide observibility for boot memory allocations. For example, the metric reserved_pages depicts the pages allocated by the bootmem allocator. This can be simply calculated as present_pages - managed_pages, which are both exported in /proc/zoneinfo. The metric reserved_pages is primarily composed of struct pages and page_ext. What about the struct pages (allocated by bootmem allocator) that are free'd during hugetlbfs allocations and then allocated by buddy-allocator once hugtlbfs pages are free'd? /proc/meminfo MemTotal changes: MemTotal does not include memblock allocations but includes buddy allocations. However, during runtime memblock allocations can be shifted into buddy allocations, and therefore become part of MemTotal. Once the struct pages get allocated by buddy allocator, we lose track of these struct page allocations overhead accounting. Therefore, we must export a new metric that we shall refer to as PageMetadata (exported by node). This shall also comprise the struct page and page_ext allocations made during runtime. Results and analysis -------------------- Memory model: Sparsemem-vmemmap $ echo 1 > /proc/sys/vm/hugetlb_optimize_vmemmap $ cat /proc/meminfo | grep MemTotal MemTotal: 32918196 kB $ cat /proc/meminfo | grep Meta PageMetadata: 589824 kB $ cat /sys/devices/system/node/node0/meminfo | grep Meta Node 0 PageMetadata: 294912 kB $ cat /sys/devices/system/node/node1/meminfo | grep Meta Node 1 PageMetadata: 294912 kB AFTER HUGTLBFS RESERVATION $ echo 512 > /proc/sys/vm/nr_hugepages $ cat /proc/meminfo | grep MemTotal MemTotal: 32934580 kB $ cat /proc/meminfo | grep Meta PageMetadata: 575488 kB $ cat /sys/devices/system/node/node0/meminfo | grep Meta Node 0 PageMetadata: 287744 kB $ cat /sys/devices/system/node/node1/meminfo | grep Meta Node 1 PageMetadata: 287744 kB AFTER FREEING HUGTLBFS RESERVATION $ echo 0 > /proc/sys/vm/nr_hugepages $ cat /proc/meminfo | grep MemTotal MemTotal: 32934580 kB $ cat /proc/meminfo | grep Meta PageMetadata: 589824 kB $ cat /sys/devices/system/node/node0/meminfo | grep Meta Node 0 PageMetadata: 294912 kB $ cat /sys/devices/system/node/node1/meminfo | grep Meta Node 1 PageMetadata: 294912 kB Sourav Panda (1): mm: report per-page metadata information Documentation/filesystems/proc.rst | 3 +++ drivers/base/node.c | 2 ++ fs/proc/meminfo.c | 7 +++++++ include/linux/mmzone.h | 3 +++ include/linux/vmstat.h | 4 ++++ mm/hugetlb.c | 11 ++++++++-- mm/hugetlb_vmemmap.c | 12 +++++++++-- mm/mm_init.c | 3 +++ mm/page_alloc.c | 1 + mm/page_ext.c | 32 +++++++++++++++++++++--------- mm/sparse-vmemmap.c | 3 +++ mm/sparse.c | 7 ++++++- mm/vmstat.c | 24 ++++++++++++++++++++++ 13 files changed, 98 insertions(+), 14 deletions(-)