Skip to contentSkip to conversation

mm: report per-page metadata information

PATCH Superseded Sourav Panda 2 participants 7 messages Oct 31, 2023 – Nov 3, 2023

Subsystems (10)

v4 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v4 cover mm: report per-page metadata information

Changelog:
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 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
-----------------------------------
v3:
https://lore.kernel.org/all/[email protected]
v2:
https://lore.kernel.org/all/[email protected]
v1:
https://lore.kernel.org/r/[email protected]

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               |  8 ++++++--
 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, 94 insertions(+), 14 deletions(-)
Signature · 1 line
2.42.0.820.g83a721a137-goog
#1 lore raw

PATCH v4 1/1 mm: report per-page metadata information

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. In contrast to most other memory consumption statistics, per-page metadata might not be included in MemTotal. For example, MemTotal does not include memblock allocations but includes buddy allocations. While on the other hand, per-page metadata would include both memblock and buddy allocations.

This memory depends on build configurations, machine architectures, and the way system is used:

Build configuration may include extra fields into "struct page", and enable / disable "page_ext"
Machine architecture defines base page sizes. For example 4K x86, 8K SPARC, 64K ARM64 (optionally), etc. The per-page metadata overhead is smaller on machines with larger page sizes. System use can change per-page overhead by using vmemmap optimizations with hugetlb pages, and emulated pmem devdax pages. Also, boot parameters can determine whether page_ext is needed to be allocated. This memory can be part of MemTotal or be outside MemTotal depending on whether the memory was hot-plugged, booted with, or hugetlb memory was returned back to the system.

---
 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               |  8 ++++++--
 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, 94 insertions(+), 14 deletions(-)
Documentation/filesystems/proc.rst +3 -0
index 2b59cff8be17..c121f2ef9432 100644
@@ -987,6 +987,7 @@ Example output. You may not have all of these fields.
     AnonPages:       4654780 kB
     Mapped:           266244 kB
     Shmem:              9976 kB
+    PageMetadata:     513419 kB
     KReclaimable:     517708 kB
     Slab:             660044 kB
     SReclaimable:     517708 kB
@@ -1089,6 +1090,8 @@ Mapped
               files which have been mmapped, such as libraries
 Shmem
               Total memory used by shared memory (shmem) and tmpfs
+PageMetadata
+              Memory used for per-page metadata
 KReclaimable
               Kernel allocations that the kernel will attempt to reclaim
               under memory pressure. Includes SReclaimable (below), and other
drivers/base/node.c +2 -0
index 493d533f8375..da728542265f 100644
@@ -428,6 +428,7 @@ static ssize_t node_read_meminfo(struct device *dev,
 			     "Node %d Mapped:         %8lu kB\n"
 			     "Node %d AnonPages:      %8lu kB\n"
 			     "Node %d Shmem:          %8lu kB\n"
+			     "Node %d PageMetadata:   %8lu kB\n"
 			     "Node %d KernelStack:    %8lu kB\n"
 #ifdef CONFIG_SHADOW_CALL_STACK
 			     "Node %d ShadowCallStack:%8lu kB\n"
@@ -458,6 +459,7 @@ static ssize_t node_read_meminfo(struct device *dev,
 			     nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
 			     nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
 			     nid, K(i.sharedram),
+			     nid, K(node_page_state(pgdat, NR_PAGE_METADATA)),
 			     nid, node_page_state(pgdat, NR_KERNEL_STACK_KB),
 #ifdef CONFIG_SHADOW_CALL_STACK
 			     nid, node_page_state(pgdat, NR_KERNEL_SCS_KB),
fs/proc/meminfo.c +7 -0
index 45af9a989d40..f141bb2a550d 100644
@@ -39,7 +39,9 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
 	long available;
 	unsigned long pages[NR_LRU_LISTS];
 	unsigned long sreclaimable, sunreclaim;
+	unsigned long nr_page_metadata;
 	int lru;
+	int nid;
 
 	si_meminfo(&i);
 	si_swapinfo(&i);
@@ -57,6 +59,10 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
 	sreclaimable = global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B);
 	sunreclaim = global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B);
 
+	nr_page_metadata = 0;
+	for_each_online_node(nid)
+		nr_page_metadata += node_page_state(NODE_DATA(nid), NR_PAGE_METADATA);
+
 	show_val_kb(m, "MemTotal:       ", i.totalram);
 	show_val_kb(m, "MemFree:        ", i.freeram);
 	show_val_kb(m, "MemAvailable:   ", available);
@@ -104,6 +110,7 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
 	show_val_kb(m, "Mapped:         ",
 		    global_node_page_state(NR_FILE_MAPPED));
 	show_val_kb(m, "Shmem:          ", i.sharedram);
+	show_val_kb(m, "PageMetadata:   ", nr_page_metadata);
 	show_val_kb(m, "KReclaimable:   ", sreclaimable +
 		    global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE));
 	show_val_kb(m, "Slab:           ", sreclaimable + sunreclaim);
include/linux/mmzone.h +3 -0
index 4106fbc5b4b3..dda1ad522324 100644
@@ -207,6 +207,9 @@ enum node_stat_item {
 	PGPROMOTE_SUCCESS,	/* promote successfully */
 	PGPROMOTE_CANDIDATE,	/* candidate pages to promote */
 #endif
+	NR_PAGE_METADATA,	/* Page metadata size (struct page and page_ext)
+				 * in pages
+				 */
 	NR_VM_NODE_STAT_ITEMS
 };
 
include/linux/vmstat.h +4 -0
index fed855bae6d8..af096a881f03 100644
@@ -656,4 +656,8 @@ static inline void lruvec_stat_sub_folio(struct folio *folio,
 {
 	lruvec_stat_mod_folio(folio, idx, -folio_nr_pages(folio));
 }
+
+void __init mod_node_early_perpage_metadata(int nid, long delta);
+void __init store_early_perpage_metadata(void);
+
 #endif /* _LINUX_VMSTAT_H */
mm/hugetlb.c +9 -2
index 1301ba7b2c9a..cd3158a9c7f3 100644
@@ -1790,6 +1790,9 @@ static void __update_and_free_hugetlb_folio(struct hstate *h,
 		destroy_compound_gigantic_folio(folio, huge_page_order(h));
 		free_gigantic_folio(folio, huge_page_order(h));
 	} else {
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+		__node_stat_sub_folio(folio, NR_PAGE_METADATA);
+#endif
 		__free_pages(&folio->page, huge_page_order(h));
 	}
 }
@@ -2125,6 +2128,7 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
 	struct page *page;
 	bool alloc_try_hard = true;
 	bool retry = true;
+	struct folio *folio;
 
 	/*
 	 * By default we always try hard to allocate the page with
@@ -2175,9 +2179,12 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
 		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
 		return NULL;
 	}
-
+	folio = page_folio(page);
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+	__node_stat_add_folio(folio, NR_PAGE_METADATA)
+#endif
 	__count_vm_event(HTLB_BUDDY_PGALLOC);
-	return page_folio(page);
+	return folio;
 }
 
 /*
mm/hugetlb_vmemmap.c +6 -2
index 4b9734777f69..804a93d18cab 100644
@@ -214,6 +214,7 @@ static inline void free_vmemmap_page(struct page *page)
 		free_bootmem_page(page);
 	else
 		__free_page(page);
+	__mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA, -1);
 }
 
 /* Free a list of the vmemmap pages */
@@ -336,6 +337,7 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
 			  (void *)walk.reuse_addr);
 		list_add(&walk.reuse_page->lru, &vmemmap_pages);
 	}
+	__mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA, 1);
 
 	/*
 	 * In order to make remapping routine most efficient for the huge pages,
@@ -381,14 +383,16 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
 				   struct list_head *list)
 {
 	gfp_t gfp_mask = GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_THISNODE;
-	unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
+	unsigned long nr_pages = DIV_ROUND_UP(end - start, PAGE_SIZE);
 	int nid = page_to_nid((struct page *)start);
 	struct page *page, *next;
+	int i;
 
-	while (nr_pages--) {
+	for (i = 0; i < nr_pages; i++) {
 		page = alloc_pages_node(nid, gfp_mask, 0);
 		if (!page)
 			goto out;
+		__mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA, 1);
 		list_add_tail(&page->lru, list);
 	}
 
mm/mm_init.c +3 -0
index 50f2f34745af..6997bf00945b 100644
@@ -26,6 +26,7 @@
 #include <linux/pgtable.h>
 #include <linux/swap.h>
 #include <linux/cma.h>
+#include <linux/vmstat.h>
 #include "internal.h"
 #include "slab.h"
 #include "shuffle.h"
@@ -1656,6 +1657,8 @@ static void __init alloc_node_mem_map(struct pglist_data *pgdat)
 			panic("Failed to allocate %ld bytes for node %d memory map\n",
 			      size, pgdat->node_id);
 		pgdat->node_mem_map = map + offset;
+		mod_node_early_perpage_metadata(pgdat->node_id,
+						DIV_ROUND_UP(size, PAGE_SIZE));
 	}
 	pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n",
 				__func__, pgdat->node_id, (unsigned long)pgdat,
mm/page_alloc.c +1 -0
index 85741403948f..522dc0c52610 100644
@@ -5443,6 +5443,7 @@ void __init setup_per_cpu_pageset(void)
 	for_each_online_pgdat(pgdat)
 		pgdat->per_cpu_nodestats =
 			alloc_percpu(struct per_cpu_nodestat);
+	store_early_perpage_metadata();
 }
 
 __meminit void zone_pcp_init(struct zone *zone)
mm/page_ext.c +23 -9
index 4548fcc66d74..d8d6db9c3d75 100644
@@ -201,6 +201,8 @@ static int __init alloc_node_page_ext(int nid)
 		return -ENOMEM;
 	NODE_DATA(nid)->node_page_ext = base;
 	total_usage += table_size;
+	__mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+			      DIV_ROUND_UP(table_size, PAGE_SIZE));
 	return 0;
 }
 
@@ -255,12 +257,15 @@ static void *__meminit alloc_page_ext(size_t size, int nid)
 	void *addr = NULL;
 
 	addr = alloc_pages_exact_nid(nid, size, flags);
-	if (addr) {
+	if (addr)
 		kmemleak_alloc(addr, size, 1, flags);
-		return addr;
-	}
+	else
+		addr = vzalloc_node(size, nid);
 
-	addr = vzalloc_node(size, nid);
+	if (addr) {
+		mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+				    DIV_ROUND_UP(size, PAGE_SIZE));
+	}
 
 	return addr;
 }
@@ -303,18 +308,27 @@ static int __meminit init_section_page_ext(unsigned long pfn, int nid)
 
 static void free_page_ext(void *addr)
 {
+	size_t table_size;
+	struct page *page;
+	struct pglist_data *pgdat;
+
+	table_size = page_ext_size * PAGES_PER_SECTION;
+
 	if (is_vmalloc_addr(addr)) {
+		page = vmalloc_to_page(addr);
+		pgdat = page_pgdat(page);
 		vfree(addr);
 	} else {
-		struct page *page = virt_to_page(addr);
-		size_t table_size;
-
-		table_size = page_ext_size * PAGES_PER_SECTION;
-
+		page = virt_to_page(addr);
+		pgdat = page_pgdat(page);
 		BUG_ON(PageReserved(page));
 		kmemleak_free(addr);
 		free_pages_exact(addr, table_size);
 	}
+
+	__mod_node_page_state(pgdat, NR_PAGE_METADATA,
+			      -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
+
 }
 
 static void __free_page_ext(unsigned long pfn)
mm/sparse-vmemmap.c +3 -0
index a2cbe44c48e1..2bc67b2c2aa2 100644
@@ -469,5 +469,8 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,
 	if (r < 0)
 		return NULL;
 
+	__mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+			      DIV_ROUND_UP(end - start, PAGE_SIZE));
+
 	return pfn_to_page(pfn);
 }
mm/sparse.c +6 -1
index 77d91e565045..7f67b5486cd1 100644
@@ -14,7 +14,7 @@
 #include <linux/swap.h>
 #include <linux/swapops.h>
 #include <linux/bootmem_info.h>
-
+#include <linux/vmstat.h>
 #include "internal.h"
 #include <asm/dma.h>
 
@@ -465,6 +465,9 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
 	 */
 	sparsemap_buf = memmap_alloc(size, section_map_size(), addr, nid, true);
 	sparsemap_buf_end = sparsemap_buf + size;
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+	mod_node_early_perpage_metadata(nid, DIV_ROUND_UP(size, PAGE_SIZE));
+#endif
 }
 
 static void __init sparse_buffer_fini(void)
@@ -641,6 +644,8 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
 	unsigned long start = (unsigned long) pfn_to_page(pfn);
 	unsigned long end = start + nr_pages * sizeof(struct page);
 
+	__mod_node_page_state(page_pgdat(pfn_to_page(pfn)), NR_PAGE_METADATA,
+			      -1L * (DIV_ROUND_UP(end - start, PAGE_SIZE)));
 	vmemmap_free(start, end, altmap);
 }
 static void free_map_bootmem(struct page *memmap)
mm/vmstat.c +24 -0
index 00e81e99c6ee..070d2b3d2bcc 100644
@@ -1245,6 +1245,7 @@ const char * const vmstat_text[] = {
 	"pgpromote_success",
 	"pgpromote_candidate",
 #endif
+	"nr_page_metadata",
 
 	/* enum writeback_stat_item counters */
 	"nr_dirty_threshold",
@@ -2274,4 +2275,27 @@ static int __init extfrag_debug_init(void)
 }
 
 module_init(extfrag_debug_init);
+
 #endif
+
+/*
+ * Page metadata size (struct page and page_ext) in pages
+ */
+static unsigned long early_perpage_metadata[MAX_NUMNODES] __initdata;
+
+void __init mod_node_early_perpage_metadata(int nid, long delta)
+{
+	early_perpage_metadata[nid] += delta;
+}
+
+void __init store_early_perpage_metadata(void)
+{
+	int nid;
+	struct pglist_data *pgdat;
+
+	for_each_online_pgdat(pgdat) {
+		nid = pgdat->node_id;
+		__mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+				      early_perpage_metadata[nid]);
+	}
+}
Signature · 1 line
2.42.0.820.g83a721a137-goog
maintainer
#2 lore raw
Quoted 172 lines · Sourav Panda

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. In contrast to most other memory consumption statistics, per-page metadata might not be included in MemTotal. For example, MemTotal does not include memblock allocations but includes buddy allocations. While on the other hand, per-page metadata would include both memblock and buddy allocations.

This memory depends on build configurations, machine architectures, and the way system is used:

Build configuration may include extra fields into "struct page", and enable / disable "page_ext"
Machine architecture defines base page sizes. For example 4K x86, 8K SPARC, 64K ARM64 (optionally), etc. The per-page metadata overhead is smaller on machines with larger page sizes. System use can change per-page overhead by using vmemmap optimizations with hugetlb pages, and emulated pmem devdax pages. Also, boot parameters can determine whether page_ext is needed to be allocated. This memory can be part of MemTotal or be outside MemTotal depending on whether the memory was hot-plugged, booted with, or hugetlb memory was returned back to the system.

---
  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               |  8 ++++++--
  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, 94 insertions(+), 14 deletions(-)
Documentation/filesystems/proc.rst +3 -0
index 2b59cff8be17..c121f2ef9432 100644
@@ -987,6 +987,7 @@ Example output. You may not have all of these fields.
      AnonPages:       4654780 kB
      Mapped:           266244 kB
      Shmem:              9976 kB
+    PageMetadata:     513419 kB
      KReclaimable:     517708 kB
      Slab:             660044 kB
      SReclaimable:     517708 kB
@@ -1089,6 +1090,8 @@ Mapped
                files which have been mmapped, such as libraries
  Shmem
                Total memory used by shared memory (shmem) and tmpfs
+PageMetadata
+              Memory used for per-page metadata
  KReclaimable
                Kernel allocations that the kernel will attempt to reclaim
                under memory pressure. Includes SReclaimable (below), and other
drivers/base/node.c +2 -0
index 493d533f8375..da728542265f 100644
@@ -428,6 +428,7 @@ static ssize_t node_read_meminfo(struct device *dev,
  			     "Node %d Mapped:         %8lu kB\n"
  			     "Node %d AnonPages:      %8lu kB\n"
  			     "Node %d Shmem:          %8lu kB\n"
+			     "Node %d PageMetadata:   %8lu kB\n"
  			     "Node %d KernelStack:    %8lu kB\n"
  #ifdef CONFIG_SHADOW_CALL_STACK
  			     "Node %d ShadowCallStack:%8lu kB\n"
@@ -458,6 +459,7 @@ static ssize_t node_read_meminfo(struct device *dev,
  			     nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
  			     nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
  			     nid, K(i.sharedram),
+			     nid, K(node_page_state(pgdat, NR_PAGE_METADATA)),
  			     nid, node_page_state(pgdat, NR_KERNEL_STACK_KB),
  #ifdef CONFIG_SHADOW_CALL_STACK
  			     nid, node_page_state(pgdat, NR_KERNEL_SCS_KB),
fs/proc/meminfo.c +7 -0
index 45af9a989d40..f141bb2a550d 100644
@@ -39,7 +39,9 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
  	long available;
  	unsigned long pages[NR_LRU_LISTS];
  	unsigned long sreclaimable, sunreclaim;
+	unsigned long nr_page_metadata;
  	int lru;
+	int nid;
 
  	si_meminfo(&i);
  	si_swapinfo(&i);
@@ -57,6 +59,10 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
  	sreclaimable = global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B);
  	sunreclaim = global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B);
 
+	nr_page_metadata = 0;
+	for_each_online_node(nid)
+		nr_page_metadata += node_page_state(NODE_DATA(nid), NR_PAGE_METADATA);
+
  	show_val_kb(m, "MemTotal:       ", i.totalram);
  	show_val_kb(m, "MemFree:        ", i.freeram);
  	show_val_kb(m, "MemAvailable:   ", available);
@@ -104,6 +110,7 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
  	show_val_kb(m, "Mapped:         ",
  		    global_node_page_state(NR_FILE_MAPPED));
  	show_val_kb(m, "Shmem:          ", i.sharedram);
+	show_val_kb(m, "PageMetadata:   ", nr_page_metadata);
  	show_val_kb(m, "KReclaimable:   ", sreclaimable +
  		    global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE));
  	show_val_kb(m, "Slab:           ", sreclaimable + sunreclaim);
include/linux/mmzone.h +3 -0
index 4106fbc5b4b3..dda1ad522324 100644
@@ -207,6 +207,9 @@ enum node_stat_item {
  	PGPROMOTE_SUCCESS,	/* promote successfully */
  	PGPROMOTE_CANDIDATE,	/* candidate pages to promote */
  #endif
+	NR_PAGE_METADATA,	/* Page metadata size (struct page and page_ext)
+				 * in pages
+				 */
  	NR_VM_NODE_STAT_ITEMS
  };
 
include/linux/vmstat.h +4 -0
index fed855bae6d8..af096a881f03 100644
@@ -656,4 +656,8 @@ static inline void lruvec_stat_sub_folio(struct folio *folio,
  {
  	lruvec_stat_mod_folio(folio, idx, -folio_nr_pages(folio));
  }
+
+void __init mod_node_early_perpage_metadata(int nid, long delta);
+void __init store_early_perpage_metadata(void);
+
  #endif /* _LINUX_VMSTAT_H */
mm/hugetlb.c +4 -0
index 1301ba7b2c9a..cd3158a9c7f3 100644
@@ -1790,6 +1790,9 @@ static void __update_and_free_hugetlb_folio(struct hstate *h,
  		destroy_compound_gigantic_folio(folio, huge_page_order(h));
  		free_gigantic_folio(folio, huge_page_order(h));
  	} else {
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+		__node_stat_sub_folio(folio, NR_PAGE_METADATA);
+#endif
  		__free_pages(&folio->page, huge_page_order(h));
  	}
  }
@@ -2125,6 +2128,7 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
  	struct page *page;
  	bool alloc_try_hard = true;
  	bool retry = true;
+	struct folio *folio;
 
  	/*
  	 * By default we always try hard to allocate the page with

mm/hugetlb.c static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, · line 2184

Quoted 7 lines
mm/hugetlb.c +3 -1
@@ -2175,9 +2179,12 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
  		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  		return NULL;
  	}
-
+	folio = page_folio(page);
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+	__node_stat_add_folio(folio, NR_PAGE_METADATA)

Seems you have not tested this patch with CONFIG_SPARSEMEM_VMEMMAP disabled. You missed ";" in the end.

mm/hugetlb.c static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, · line 2185

Quoted 1 line
mm/hugetlb.c +1 -0
+#endif

I am curious why we should account HugeTLB pages as metadata.

Quoted 19 lines
mm/hugetlb.c +1 -1
  	__count_vm_event(HTLB_BUDDY_PGALLOC);
-	return page_folio(page);
+	return folio;
  }
 
  /*
mm/hugetlb_vmemmap.c +1 -0
index 4b9734777f69..804a93d18cab 100644
@@ -214,6 +214,7 @@ static inline void free_vmemmap_page(struct page *page)
  		free_bootmem_page(page);
  	else
  		__free_page(page);
+	__mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA, -1);
  }
 
  /* Free a list of the vmemmap pages */

mm/hugetlb_vmemmap.c static int vmemmap_remap_free(unsigned long start, unsigned long end, · line 340

Quoted 4 lines
mm/hugetlb_vmemmap.c +1 -0
@@ -336,6 +337,7 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
  			  (void *)walk.reuse_addr);
  		list_add(&walk.reuse_page->lru, &vmemmap_pages);
  	}
+	__mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA, 1);

What if allocation of walk.reuse_page fails?

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 386

Quoted 9 lines
mm/hugetlb_vmemmap.c +1 -1
  	/*
  	 * In order to make remapping routine most efficient for the huge pages,
@@ -381,14 +383,16 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
  				   struct list_head *list)
  {
  	gfp_t gfp_mask = GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_THISNODE;
-	unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
+	unsigned long nr_pages = DIV_ROUND_UP(end - start, PAGE_SIZE);

"end - start" is always multiple of PAGE_SIZE, why we need DIV_ROUND_UP here?

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 397

Quoted 12 lines
mm/hugetlb_vmemmap.c +3 -1
  	int nid = page_to_nid((struct page *)start);
  	struct page *page, *next;
+	int i;
 
-	while (nr_pages--) {
+	for (i = 0; i < nr_pages; i++) {
  		page = alloc_pages_node(nid, gfp_mask, 0);
  		if (!page)
  			goto out;
+		__mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA, 1);
  		list_add_tail(&page->lru, list);
  	}

Count one by ine is really inefficient. Can't we count *nr_pages* at one time?

Quoted 185 lines
mm/mm_init.c +3 -0
index 50f2f34745af..6997bf00945b 100644
@@ -26,6 +26,7 @@
  #include <linux/pgtable.h>
  #include <linux/swap.h>
  #include <linux/cma.h>
+#include <linux/vmstat.h>
  #include "internal.h"
  #include "slab.h"
  #include "shuffle.h"
@@ -1656,6 +1657,8 @@ static void __init alloc_node_mem_map(struct pglist_data *pgdat)
  			panic("Failed to allocate %ld bytes for node %d memory map\n",
  			      size, pgdat->node_id);
  		pgdat->node_mem_map = map + offset;
+		mod_node_early_perpage_metadata(pgdat->node_id,
+						DIV_ROUND_UP(size, PAGE_SIZE));
  	}
  	pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n",
  				__func__, pgdat->node_id, (unsigned long)pgdat,
mm/page_alloc.c +1 -0
index 85741403948f..522dc0c52610 100644
@@ -5443,6 +5443,7 @@ void __init setup_per_cpu_pageset(void)
  	for_each_online_pgdat(pgdat)
  		pgdat->per_cpu_nodestats =
  			alloc_percpu(struct per_cpu_nodestat);
+	store_early_perpage_metadata();
  }
 
  __meminit void zone_pcp_init(struct zone *zone)
mm/page_ext.c +23 -9
index 4548fcc66d74..d8d6db9c3d75 100644
@@ -201,6 +201,8 @@ static int __init alloc_node_page_ext(int nid)
  		return -ENOMEM;
  	NODE_DATA(nid)->node_page_ext = base;
  	total_usage += table_size;
+	__mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+			      DIV_ROUND_UP(table_size, PAGE_SIZE));
  	return 0;
  }
 
@@ -255,12 +257,15 @@ static void *__meminit alloc_page_ext(size_t size, int nid)
  	void *addr = NULL;
 
  	addr = alloc_pages_exact_nid(nid, size, flags);
-	if (addr) {
+	if (addr)
  		kmemleak_alloc(addr, size, 1, flags);
-		return addr;
-	}
+	else
+		addr = vzalloc_node(size, nid);
 
-	addr = vzalloc_node(size, nid);
+	if (addr) {
+		mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+				    DIV_ROUND_UP(size, PAGE_SIZE));
+	}
 
  	return addr;
  }
@@ -303,18 +308,27 @@ static int __meminit init_section_page_ext(unsigned long pfn, int nid)
 
  static void free_page_ext(void *addr)
  {
+	size_t table_size;
+	struct page *page;
+	struct pglist_data *pgdat;
+
+	table_size = page_ext_size * PAGES_PER_SECTION;
+
  	if (is_vmalloc_addr(addr)) {
+		page = vmalloc_to_page(addr);
+		pgdat = page_pgdat(page);
  		vfree(addr);
  	} else {
-		struct page *page = virt_to_page(addr);
-		size_t table_size;
-
-		table_size = page_ext_size * PAGES_PER_SECTION;
-
+		page = virt_to_page(addr);
+		pgdat = page_pgdat(page);
  		BUG_ON(PageReserved(page));
  		kmemleak_free(addr);
  		free_pages_exact(addr, table_size);
  	}
+
+	__mod_node_page_state(pgdat, NR_PAGE_METADATA,
+			      -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
+
  }
 
  static void __free_page_ext(unsigned long pfn)
mm/sparse-vmemmap.c +3 -0
index a2cbe44c48e1..2bc67b2c2aa2 100644
@@ -469,5 +469,8 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,
  	if (r < 0)
  		return NULL;
 
+	__mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+			      DIV_ROUND_UP(end - start, PAGE_SIZE));
+
  	return pfn_to_page(pfn);
  }
mm/sparse.c +6 -1
index 77d91e565045..7f67b5486cd1 100644
@@ -14,7 +14,7 @@
  #include <linux/swap.h>
  #include <linux/swapops.h>
  #include <linux/bootmem_info.h>
-
+#include <linux/vmstat.h>
  #include "internal.h"
  #include <asm/dma.h>
 
@@ -465,6 +465,9 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
  	 */
  	sparsemap_buf = memmap_alloc(size, section_map_size(), addr, nid, true);
  	sparsemap_buf_end = sparsemap_buf + size;
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+	mod_node_early_perpage_metadata(nid, DIV_ROUND_UP(size, PAGE_SIZE));
+#endif
  }
 
  static void __init sparse_buffer_fini(void)
@@ -641,6 +644,8 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
  	unsigned long start = (unsigned long) pfn_to_page(pfn);
  	unsigned long end = start + nr_pages * sizeof(struct page);
 
+	__mod_node_page_state(page_pgdat(pfn_to_page(pfn)), NR_PAGE_METADATA,
+			      -1L * (DIV_ROUND_UP(end - start, PAGE_SIZE)));
  	vmemmap_free(start, end, altmap);
  }
  static void free_map_bootmem(struct page *memmap)
mm/vmstat.c +24 -0
index 00e81e99c6ee..070d2b3d2bcc 100644
@@ -1245,6 +1245,7 @@ const char * const vmstat_text[] = {
  	"pgpromote_success",
  	"pgpromote_candidate",
  #endif
+	"nr_page_metadata",
 
  	/* enum writeback_stat_item counters */
  	"nr_dirty_threshold",
@@ -2274,4 +2275,27 @@ static int __init extfrag_debug_init(void)
  }
 
  module_init(extfrag_debug_init);
+
  #endif
+
+/*
+ * Page metadata size (struct page and page_ext) in pages
+ */
+static unsigned long early_perpage_metadata[MAX_NUMNODES] __initdata;
+
+void __init mod_node_early_perpage_metadata(int nid, long delta)
+{
+	early_perpage_metadata[nid] += delta;
+}
+
+void __init store_early_perpage_metadata(void)
+{
+	int nid;
+	struct pglist_data *pgdat;
+
+	for_each_online_pgdat(pgdat) {
+		nid = pgdat->node_id;
+		__mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+				      early_perpage_metadata[nid]);
+	}
+}
#3 lore raw
Quoted 191 lines · Muchun Song
Quoted 48 lines · Sourav Panda

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. In contrast to most other memory consumption statistics, per-page metadata might not be included in MemTotal. For example, MemTotal does not include memblock allocations but includes buddy allocations. While on the other hand, per-page metadata would include both memblock and buddy allocations.

This memory depends on build configurations, machine architectures, and the way system is used:

Build configuration may include extra fields into "struct page", and enable / disable "page_ext"
Machine architecture defines base page sizes. For example 4K x86, 8K SPARC, 64K ARM64 (optionally), etc. The per-page metadata overhead is smaller on machines with larger page sizes. System use can change per-page overhead by using vmemmap optimizations with hugetlb pages, and emulated pmem devdax pages. Also, boot parameters can determine whether page_ext is needed to be allocated. This memory can be part of MemTotal or be outside MemTotal depending on whether the memory was hot-plugged, booted with, or hugetlb memory was returned back to the system.

---
  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               |  8 ++++++--
  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, 94 insertions(+), 14 deletions(-)

Review

Quoted 1 line
(quoted diff) +0 -0

b/Documentation/filesystems/proc.rst

Quoted 12 lines

index 2b59cff8be17..c121f2ef9432 100644

Documentation/filesystems/proc.rst +1 -0
@@ -987,6 +987,7 @@ Example output. You may not have all of these fields.
      AnonPages:       4654780 kB
      Mapped:           266244 kB
      Shmem:              9976 kB
+    PageMetadata:     513419 kB
      KReclaimable:     517708 kB
      Slab:             660044 kB
      SReclaimable:     517708 kB

Documentation/filesystems/proc.rst Mapped · line 1096

Quoted 7 lines
Documentation/filesystems/proc.rst +2 -0
@@ -1089,6 +1090,8 @@ Mapped
                files which have been mmapped, such as libraries
  Shmem
                Total memory used by shared memory (shmem) and tmpfs
+PageMetadata
+              Memory used for per-page metadata
  KReclaimable
                Kernel allocations that the kernel will attempt to

reclaim

Documentation/filesystems/proc.rst Mapped · line 1097

Quoted 1 line
Documentation/filesystems/proc.rst +0 -0
                under memory pressure. Includes SReclaimable (below),

and other

Quoted 13 lines
drivers/base/node.c +1 -0
index 493d533f8375..da728542265f 100644
@@ -428,6 +428,7 @@ static ssize_t node_read_meminfo(struct device *dev,
                           "Node %d Mapped:         %8lu kB\n"
                           "Node %d AnonPages:      %8lu kB\n"
                           "Node %d Shmem:          %8lu kB\n"
+                          "Node %d PageMetadata:   %8lu kB\n"
                           "Node %d KernelStack:    %8lu kB\n"
  #ifdef CONFIG_SHADOW_CALL_STACK
                           "Node %d ShadowCallStack:%8lu kB\n"

drivers/base/node.c static ssize_t node_read_meminfo(struct device *dev, · line 459

Quoted 1 line
drivers/base/node.c +0 -0
@@ -458,6 +459,7 @@ static ssize_t node_read_meminfo(struct device *dev,
                           nid, K(node_page_state(pgdat,

NR_FILE_MAPPED)),

drivers/base/node.c static ssize_t node_read_meminfo(struct device *dev, · line 460

Quoted 1 line
drivers/base/node.c +0 -0
                           nid, K(node_page_state(pgdat,

NR_ANON_MAPPED)),

drivers/base/node.c static ssize_t node_read_meminfo(struct device *dev, · line 462

Quoted 2 lines
drivers/base/node.c +1 -0
                           nid, K(i.sharedram),
+                          nid, K(node_page_state(pgdat,

NR_PAGE_METADATA)),

drivers/base/node.c static ssize_t node_read_meminfo(struct device *dev, · line 463

Quoted 1 line
drivers/base/node.c +0 -0
                           nid, node_page_state(pgdat,

NR_KERNEL_STACK_KB),

fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m, void

Quoted 7 lines
drivers/base/node.c +0 -0
  #ifdef CONFIG_SHADOW_CALL_STACK
                           nid, node_page_state(pgdat, NR_KERNEL_SCS_KB),
fs/proc/meminfo.c +0 -0
index 45af9a989d40..f141bb2a550d 100644
@@ -39,7 +39,9 @@ static int meminfo_proc_show(struct seq_file *m, void

*v)

fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m, void

Quoted 10 lines
fs/proc/meminfo.c +2 -0
      long available;
      unsigned long pages[NR_LRU_LISTS];
      unsigned long sreclaimable, sunreclaim;
+     unsigned long nr_page_metadata;
      int lru;
+     int nid;
 
      si_meminfo(&i);
      si_swapinfo(&i);
@@ -57,6 +59,10 @@ static int meminfo_proc_show(struct seq_file *m, void

*v)

fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m, void *v)

Quoted 6 lines
fs/proc/meminfo.c +3 -0
      sreclaimable = global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B);
      sunreclaim = global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B);
 
+     nr_page_metadata = 0;
+     for_each_online_node(nid)
+             nr_page_metadata += node_page_state(NODE_DATA(nid),

NR_PAGE_METADATA);

fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m,

Quoted 5 lines
fs/proc/meminfo.c +1 -0
+
      show_val_kb(m, "MemTotal:       ", i.totalram);
      show_val_kb(m, "MemFree:        ", i.freeram);
      show_val_kb(m, "MemAvailable:   ", available);
@@ -104,6 +110,7 @@ static int meminfo_proc_show(struct seq_file *m,

void *v)

Quoted 12 lines
fs/proc/meminfo.c +1 -0
      show_val_kb(m, "Mapped:         ",
                  global_node_page_state(NR_FILE_MAPPED));
      show_val_kb(m, "Shmem:          ", i.sharedram);
+     show_val_kb(m, "PageMetadata:   ", nr_page_metadata);
      show_val_kb(m, "KReclaimable:   ", sreclaimable +
                  global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE));
      show_val_kb(m, "Slab:           ", sreclaimable + sunreclaim);
include/linux/mmzone.h +0 -0
index 4106fbc5b4b3..dda1ad522324 100644

include/linux/mmzone.h enum node_stat_item { · line 210

Quoted 4 lines
include/linux/mmzone.h +1 -0
@@ -207,6 +207,9 @@ enum node_stat_item {
      PGPROMOTE_SUCCESS,      /* promote successfully */
      PGPROMOTE_CANDIDATE,    /* candidate pages to promote */
  #endif
+     NR_PAGE_METADATA,       /* Page metadata size (struct page and

page_ext)

include/linux/vmstat.h static inline void lruvec_stat_sub_folio(struct

Quoted 10 lines
include/linux/mmzone.h +2 -0
+                              * in pages
+                              */
      NR_VM_NODE_STAT_ITEMS
  };
 
include/linux/vmstat.h +0 -0
index fed855bae6d8..af096a881f03 100644
@@ -656,4 +656,8 @@ static inline void lruvec_stat_sub_folio(struct

folio *folio,

mm/hugetlb.c static void __update_and_free_hugetlb_folio(struct

Quoted 13 lines
include/linux/vmstat.h +4 -0
  {
      lruvec_stat_mod_folio(folio, idx, -folio_nr_pages(folio));
  }
+
+void __init mod_node_early_perpage_metadata(int nid, long delta);
+void __init store_early_perpage_metadata(void);
+
  #endif /* _LINUX_VMSTAT_H */
mm/hugetlb.c +0 -0
index 1301ba7b2c9a..cd3158a9c7f3 100644
@@ -1790,6 +1790,9 @@ static void __update_and_free_hugetlb_folio(struct

hstate *h,

mm/hugetlb.c static struct folio

Quoted 10 lines
mm/hugetlb.c +3 -0
              destroy_compound_gigantic_folio(folio, huge_page_order(h));
              free_gigantic_folio(folio, huge_page_order(h));
      } else {
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+             __node_stat_sub_folio(folio, NR_PAGE_METADATA);
+#endif
              __free_pages(&folio->page, huge_page_order(h));
      }
  }
@@ -2125,6 +2128,7 @@ static struct folio

*alloc_buddy_hugetlb_folio(struct hstate *h,

mm/hugetlb.c static struct folio

Quoted 8 lines
mm/hugetlb.c +1 -0
      struct page *page;
      bool alloc_try_hard = true;
      bool retry = true;
+     struct folio *folio;
 
      /*
       * By default we always try hard to allocate the page with
@@ -2175,9 +2179,12 @@ static struct folio

*alloc_buddy_hugetlb_folio(struct hstate *h,

Quoted 12 lines

mm/hugetlb.c static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, · line 2184

Quoted 7 lines
mm/hugetlb.c +3 -1
              __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
              return NULL;
      }
-
+     folio = page_folio(page);
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+     __node_stat_add_folio(folio, NR_PAGE_METADATA)

Seems you have not tested this patch with CONFIG_SPARSEMEM_VMEMMAP disabled.
You missed ";" in the end.

Thanks for reviewing this patch. I will submit v5 by testing against FLATMEM and
SPARSEMEM (VMEMMAP disabled) memory model on ARM32. This error was introduced
in v4.

Quoted 5 lines

mm/hugetlb.c static struct folio · line 2185

Quoted 2 lines
mm/hugetlb.c +1 -0
+#endif

I am curious why we should account HugeTLB pages as metadata.

When HugeTLB pages are reserved, memory pertaining to redundant struct page are
returned to the buddy allocator for other uses. This essentially reflects the change in the
amount of struct pages when HugeTLB pages are reserved and free'd.

Quoted 22 lines

mm/hugetlb_vmemmap.c static inline void free_vmemmap_page(struct page

Quoted 12 lines
mm/hugetlb.c +1 -1
      __count_vm_event(HTLB_BUDDY_PGALLOC);
-     return page_folio(page);
+     return folio;
  }
 
  /*
mm/hugetlb_vmemmap.c +0 -0
index 4b9734777f69..804a93d18cab 100644
@@ -214,6 +214,7 @@ static inline void free_vmemmap_page(struct page

*page)

mm/hugetlb_vmemmap.c static int vmemmap_remap_free(unsigned long start,

Quoted 8 lines
mm/hugetlb_vmemmap.c +1 -0
              free_bootmem_page(page);
      else
              __free_page(page);
+     __mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA, -1);
  }
 
  /* Free a list of the vmemmap pages */
@@ -336,6 +337,7 @@ static int vmemmap_remap_free(unsigned long start,

unsigned long end,

Quoted 7 lines

mm/hugetlb_vmemmap.c static int vmemmap_remap_free(unsigned long start, unsigned long end, · line 340

Quoted 4 lines
mm/hugetlb_vmemmap.c +1 -0
                        (void *)walk.reuse_addr);
              list_add(&walk.reuse_page->lru, &vmemmap_pages);
      }
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA, 1);

What if allocation of walk.reuse_page fails?

Thank you for pointing this out. I will move the NR_PAGE_METADATA update within the
if ( walk.reuse_page ) clause to cover the case where walk.reuse_page fails.

Quoted 7 lines

mm/hugetlb_vmemmap.c static int vmemmap_remap_free(unsigned long start, · line 342

Quoted 4 lines
mm/hugetlb_vmemmap.c +0 -0
      /*
       * In order to make remapping routine most efficient for the huge

pages,

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long

Quoted 1 line
mm/hugetlb_vmemmap.c +0 -0
@@ -381,14 +383,16 @@ static int alloc_vmemmap_page_list(unsigned long

start, unsigned long end,

Quoted 9 lines

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 386

Quoted 5 lines
mm/hugetlb_vmemmap.c +1 -1
                                 struct list_head *list)
  {
      gfp_t gfp_mask = GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_THISNODE;
-     unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
+     unsigned long nr_pages = DIV_ROUND_UP(end - start, PAGE_SIZE);

"end - start" is always multiple of PAGE_SIZE, why we need DIV_ROUND_UP here?

Thank you. I agree with this and will revert this change in v5.

Quoted 11 lines

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,

Quoted 11 lines
mm/hugetlb_vmemmap.c +3 -1
      int nid = page_to_nid((struct page *)start);
      struct page *page, *next;
+     int i;
 
-     while (nr_pages--) {
+     for (i = 0; i < nr_pages; i++) {
              page = alloc_pages_node(nid, gfp_mask, 0);
              if (!page)
                      goto out;
+             __mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA,
Quoted 7 lines

1);

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 397

Quoted 2 lines
mm/hugetlb_vmemmap.c +0 -0
              list_add_tail(&page->lru, list);
      }

Count one by ine is really inefficient. Can't we count *nr_pages* at one time?

Thanks for suggesting this optimization. I will modify the implementation to update the
metadata once as opposed to every iteration.

Quoted 201 lines

mm/mm_init.c static void __init alloc_node_mem_map(struct

Quoted 15 lines
mm/mm_init.c +1 -0
index 50f2f34745af..6997bf00945b 100644
@@ -26,6 +26,7 @@
  #include <linux/pgtable.h>
  #include <linux/swap.h>
  #include <linux/cma.h>
+#include <linux/vmstat.h>
  #include "internal.h"
  #include "slab.h"
  #include "shuffle.h"
@@ -1656,6 +1657,8 @@ static void __init alloc_node_mem_map(struct

pglist_data *pgdat)

mm/mm_init.c static void __init alloc_node_mem_map(struct · line 1657

Quoted 1 line
mm/mm_init.c +0 -0
                      panic("Failed to allocate %ld bytes for node %d

memory map\n",

mm/mm_init.c static void __init alloc_node_mem_map(struct pglist_data *pgdat)

Quoted 4 lines
mm/mm_init.c +2 -0
                            size, pgdat->node_id);
              pgdat->node_mem_map = map + offset;
+             mod_node_early_perpage_metadata(pgdat->node_id,
+                                             DIV_ROUND_UP(size,

PAGE_SIZE));

mm/mm_init.c static void __init alloc_node_mem_map(struct pglist_data *pgdat)

Quoted 3 lines
mm/mm_init.c +0 -0
      }
      pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n",
                              __func__, pgdat->node_id, (unsigned

long)pgdat,

mm/page_ext.c static void *__meminit alloc_page_ext(size_t size,

Quoted 26 lines
mm/page_alloc.c +1 -0
index 85741403948f..522dc0c52610 100644
@@ -5443,6 +5443,7 @@ void __init setup_per_cpu_pageset(void)
      for_each_online_pgdat(pgdat)
              pgdat->per_cpu_nodestats =
                      alloc_percpu(struct per_cpu_nodestat);
+     store_early_perpage_metadata();
  }
 
  __meminit void zone_pcp_init(struct zone *zone)
mm/page_ext.c +2 -0
index 4548fcc66d74..d8d6db9c3d75 100644
@@ -201,6 +201,8 @@ static int __init alloc_node_page_ext(int nid)
              return -ENOMEM;
      NODE_DATA(nid)->node_page_ext = base;
      total_usage += table_size;
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                           DIV_ROUND_UP(table_size, PAGE_SIZE));
      return 0;
  }
 
@@ -255,12 +257,15 @@ static void *__meminit alloc_page_ext(size_t size,

int nid)

mm/page_ext.c static int __meminit

Quoted 20 lines
mm/page_ext.c +7 -4
      void *addr = NULL;
 
      addr = alloc_pages_exact_nid(nid, size, flags);
-     if (addr) {
+     if (addr)
              kmemleak_alloc(addr, size, 1, flags);
-             return addr;
-     }
+     else
+             addr = vzalloc_node(size, nid);
 
-     addr = vzalloc_node(size, nid);
+     if (addr) {
+             mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                                 DIV_ROUND_UP(size, PAGE_SIZE));
+     }
 
      return addr;
  }
@@ -303,18 +308,27 @@ static int __meminit

init_section_page_ext(unsigned long pfn, int nid)

mm/sparse-vmemmap.c struct page * __meminit

Quoted 37 lines
mm/page_ext.c +14 -5
  static void free_page_ext(void *addr)
  {
+     size_t table_size;
+     struct page *page;
+     struct pglist_data *pgdat;
+
+     table_size = page_ext_size * PAGES_PER_SECTION;
+
      if (is_vmalloc_addr(addr)) {
+             page = vmalloc_to_page(addr);
+             pgdat = page_pgdat(page);
              vfree(addr);
      } else {
-             struct page *page = virt_to_page(addr);
-             size_t table_size;
-
-             table_size = page_ext_size * PAGES_PER_SECTION;
-
+             page = virt_to_page(addr);
+             pgdat = page_pgdat(page);
              BUG_ON(PageReserved(page));
              kmemleak_free(addr);
              free_pages_exact(addr, table_size);
      }
+
+     __mod_node_page_state(pgdat, NR_PAGE_METADATA,
+                           -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
+
  }
 
  static void __free_page_ext(unsigned long pfn)
mm/sparse-vmemmap.c +0 -0
index a2cbe44c48e1..2bc67b2c2aa2 100644
@@ -469,5 +469,8 @@ struct page * __meminit

__populate_section_memmap(unsigned long pfn,

mm/sparse.c static void __init sparse_buffer_init(unsigned long

Quoted 22 lines
mm/sparse-vmemmap.c +3 -0
      if (r < 0)
              return NULL;
 
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                           DIV_ROUND_UP(end - start, PAGE_SIZE));
+
      return pfn_to_page(pfn);
  }
mm/sparse.c +1 -1
index 77d91e565045..7f67b5486cd1 100644
@@ -14,7 +14,7 @@
  #include <linux/swap.h>
  #include <linux/swapops.h>
  #include <linux/bootmem_info.h>
-
+#include <linux/vmstat.h>
  #include "internal.h"
  #include <asm/dma.h>
 
@@ -465,6 +465,9 @@ static void __init sparse_buffer_init(unsigned long

size, int nid)

mm/sparse.c static void __init sparse_buffer_init(unsigned long · line 466

Quoted 2 lines
mm/sparse.c +0 -0
       */
      sparsemap_buf = memmap_alloc(size, section_map_size(), addr, nid,

true);

mm/sparse.c static void __init sparse_buffer_init(unsigned long size, int nid)

Quoted 3 lines
mm/sparse.c +2 -0
      sparsemap_buf_end = sparsemap_buf + size;
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+     mod_node_early_perpage_metadata(nid, DIV_ROUND_UP(size,

PAGE_SIZE));

mm/sparse.c static void depopulate_section_memmap(unsigned long

Quoted 5 lines
mm/sparse.c +1 -0
+#endif
  }
 
  static void __init sparse_buffer_fini(void)
@@ -641,6 +644,8 @@ static void depopulate_section_memmap(unsigned long

pfn, unsigned long nr_pages,

mm/sparse.c static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,

Quoted 4 lines
mm/sparse.c +1 -0
      unsigned long start = (unsigned long) pfn_to_page(pfn);
      unsigned long end = start + nr_pages * sizeof(struct page);
 
+     __mod_node_page_state(page_pgdat(pfn_to_page(pfn)),

NR_PAGE_METADATA,

mm/sparse.c static void depopulate_section_memmap(unsigned long · line 648

Quoted 1 line
mm/sparse.c +1 -0
+                           -1L * (DIV_ROUND_UP(end - start,

PAGE_SIZE)));

Quoted 43 lines
mm/sparse.c +0 -0
      vmemmap_free(start, end, altmap);
  }
  static void free_map_bootmem(struct page *memmap)
mm/vmstat.c +24 -0
index 00e81e99c6ee..070d2b3d2bcc 100644
@@ -1245,6 +1245,7 @@ const char * const vmstat_text[] = {
      "pgpromote_success",
      "pgpromote_candidate",
  #endif
+     "nr_page_metadata",
 
      /* enum writeback_stat_item counters */
      "nr_dirty_threshold",
@@ -2274,4 +2275,27 @@ static int __init extfrag_debug_init(void)
  }
 
  module_init(extfrag_debug_init);
+
  #endif
+
+/*
+ * Page metadata size (struct page and page_ext) in pages
+ */
+static unsigned long early_perpage_metadata[MAX_NUMNODES] __initdata;
+
+void __init mod_node_early_perpage_metadata(int nid, long delta)
+{
+     early_perpage_metadata[nid] += delta;
+}
+
+void __init store_early_perpage_metadata(void)
+{
+     int nid;
+     struct pglist_data *pgdat;
+
+     for_each_online_pgdat(pgdat) {
+             nid = pgdat->node_id;
+             __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                                   early_perpage_metadata[nid]);
+     }
+}
maintainer
#4 lore raw
Quoted 198 lines · Sourav Panda

On Tue, Oct 31, 2023 at 8:38 PM Muchun Song <[email protected]> wrote:

Quoted 172 lines · Sourav Panda

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. In contrast to most other memory consumption statistics, per-page metadata might not be included in MemTotal. For example, MemTotal does not include memblock allocations but includes buddy allocations. While on the other hand, per-page metadata would include both memblock and buddy allocations.

This memory depends on build configurations, machine architectures, and the way system is used:

Build configuration may include extra fields into "struct page", and enable / disable "page_ext"
Machine architecture defines base page sizes. For example 4K x86, 8K SPARC, 64K ARM64 (optionally), etc. The per-page metadata overhead is smaller on machines with larger page sizes. System use can change per-page overhead by using vmemmap optimizations with hugetlb pages, and emulated pmem devdax pages. Also, boot parameters can determine whether page_ext is needed to be allocated. This memory can be part of MemTotal or be outside MemTotal depending on whether the memory was hot-plugged, booted with, or hugetlb memory was returned back to the system.

---
  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               |  8 ++++++--
  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, 94 insertions(+), 14 deletions(-)
Documentation/filesystems/proc.rst +3 -0
index 2b59cff8be17..c121f2ef9432 100644
@@ -987,6 +987,7 @@ Example output. You may not have all of these fields.
      AnonPages:       4654780 kB
      Mapped:           266244 kB
      Shmem:              9976 kB
+    PageMetadata:     513419 kB
      KReclaimable:     517708 kB
      Slab:             660044 kB
      SReclaimable:     517708 kB
@@ -1089,6 +1090,8 @@ Mapped
                files which have been mmapped, such as libraries
  Shmem
                Total memory used by shared memory (shmem) and tmpfs
+PageMetadata
+              Memory used for per-page metadata
  KReclaimable
                Kernel allocations that the kernel will attempt to reclaim
                under memory pressure. Includes SReclaimable (below), and other
drivers/base/node.c +2 -0
index 493d533f8375..da728542265f 100644
@@ -428,6 +428,7 @@ static ssize_t node_read_meminfo(struct device *dev,
                           "Node %d Mapped:         %8lu kB\n"
                           "Node %d AnonPages:      %8lu kB\n"
                           "Node %d Shmem:          %8lu kB\n"
+                          "Node %d PageMetadata:   %8lu kB\n"
                           "Node %d KernelStack:    %8lu kB\n"
  #ifdef CONFIG_SHADOW_CALL_STACK
                           "Node %d ShadowCallStack:%8lu kB\n"
@@ -458,6 +459,7 @@ static ssize_t node_read_meminfo(struct device *dev,
                           nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
                           nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
                           nid, K(i.sharedram),
+                          nid, K(node_page_state(pgdat, NR_PAGE_METADATA)),
                           nid, node_page_state(pgdat, NR_KERNEL_STACK_KB),
  #ifdef CONFIG_SHADOW_CALL_STACK
                           nid, node_page_state(pgdat, NR_KERNEL_SCS_KB),
fs/proc/meminfo.c +7 -0
index 45af9a989d40..f141bb2a550d 100644
@@ -39,7 +39,9 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
      long available;
      unsigned long pages[NR_LRU_LISTS];
      unsigned long sreclaimable, sunreclaim;
+     unsigned long nr_page_metadata;
      int lru;
+     int nid;
 
      si_meminfo(&i);
      si_swapinfo(&i);
@@ -57,6 +59,10 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
      sreclaimable = global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B);
      sunreclaim = global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B);
 
+     nr_page_metadata = 0;
+     for_each_online_node(nid)
+             nr_page_metadata += node_page_state(NODE_DATA(nid), NR_PAGE_METADATA);
+
      show_val_kb(m, "MemTotal:       ", i.totalram);
      show_val_kb(m, "MemFree:        ", i.freeram);
      show_val_kb(m, "MemAvailable:   ", available);
@@ -104,6 +110,7 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
      show_val_kb(m, "Mapped:         ",
                  global_node_page_state(NR_FILE_MAPPED));
      show_val_kb(m, "Shmem:          ", i.sharedram);
+     show_val_kb(m, "PageMetadata:   ", nr_page_metadata);
      show_val_kb(m, "KReclaimable:   ", sreclaimable +
                  global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE));
      show_val_kb(m, "Slab:           ", sreclaimable + sunreclaim);
include/linux/mmzone.h +3 -0
index 4106fbc5b4b3..dda1ad522324 100644
@@ -207,6 +207,9 @@ enum node_stat_item {
      PGPROMOTE_SUCCESS,      /* promote successfully */
      PGPROMOTE_CANDIDATE,    /* candidate pages to promote */
  #endif
+     NR_PAGE_METADATA,       /* Page metadata size (struct page and page_ext)
+                              * in pages
+                              */
      NR_VM_NODE_STAT_ITEMS
  };
 
include/linux/vmstat.h +4 -0
index fed855bae6d8..af096a881f03 100644
@@ -656,4 +656,8 @@ static inline void lruvec_stat_sub_folio(struct folio *folio,
  {
      lruvec_stat_mod_folio(folio, idx, -folio_nr_pages(folio));
  }
+
+void __init mod_node_early_perpage_metadata(int nid, long delta);
+void __init store_early_perpage_metadata(void);
+
  #endif /* _LINUX_VMSTAT_H */
mm/hugetlb.c +4 -0
index 1301ba7b2c9a..cd3158a9c7f3 100644
@@ -1790,6 +1790,9 @@ static void __update_and_free_hugetlb_folio(struct hstate *h,
              destroy_compound_gigantic_folio(folio, huge_page_order(h));
              free_gigantic_folio(folio, huge_page_order(h));
      } else {
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+             __node_stat_sub_folio(folio, NR_PAGE_METADATA);
+#endif
              __free_pages(&folio->page, huge_page_order(h));
      }
  }
@@ -2125,6 +2128,7 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
      struct page *page;
      bool alloc_try_hard = true;
      bool retry = true;
+     struct folio *folio;
 
      /*
       * By default we always try hard to allocate the page with

mm/hugetlb.c static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, · line 2184

Quoted 7 lines
mm/hugetlb.c +3 -1
@@ -2175,9 +2179,12 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
              __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
              return NULL;
      }
-
+     folio = page_folio(page);
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+     __node_stat_add_folio(folio, NR_PAGE_METADATA)

Seems you have not tested this patch with CONFIG_SPARSEMEM_VMEMMAP disabled. You missed ";" in the end.

Thanks for reviewing this patch. I will submit v5 by testing against FLATMEM and SPARSEMEM (VMEMMAP disabled) memory model on ARM32. This error was introduced in v4.

Quoted 6 lines

mm/hugetlb.c static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, · line 2185

Quoted 1 line
mm/hugetlb.c +1 -0
+#endif

I am curious why we should account HugeTLB pages as metadata.

When HugeTLB pages are reserved, memory pertaining to redundant struct page are returned to the buddy allocator for other uses. This essentially reflects the change in the

Why the struct page are returned to the buddy when CONFIG_SPARSEMEM_VMEMMAP is disabled? Why it is added with a PAGE_SIZE?

Quoted 250 lines

amount of struct pages when HugeTLB pages are reserved and free'd.

Quoted 19 lines
mm/hugetlb.c +1 -1
      __count_vm_event(HTLB_BUDDY_PGALLOC);
-     return page_folio(page);
+     return folio;
  }
 
  /*
mm/hugetlb_vmemmap.c +1 -0
index 4b9734777f69..804a93d18cab 100644
@@ -214,6 +214,7 @@ static inline void free_vmemmap_page(struct page *page)
              free_bootmem_page(page);
      else
              __free_page(page);
+     __mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA, -1);
  }
 
  /* Free a list of the vmemmap pages */

mm/hugetlb_vmemmap.c static int vmemmap_remap_free(unsigned long start, unsigned long end, · line 340

Quoted 4 lines
mm/hugetlb_vmemmap.c +1 -0
@@ -336,6 +337,7 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
                        (void *)walk.reuse_addr);
              list_add(&walk.reuse_page->lru, &vmemmap_pages);
      }
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA, 1);

What if allocation of walk.reuse_page fails?

Thank you for pointing this out. I will move the NR_PAGE_METADATA update within the if ( walk.reuse_page ) clause to cover the case where walk.reuse_page fails.

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 386

Quoted 9 lines
mm/hugetlb_vmemmap.c +1 -1
      /*
       * In order to make remapping routine most efficient for the huge pages,
@@ -381,14 +383,16 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
                                 struct list_head *list)
  {
      gfp_t gfp_mask = GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_THISNODE;
-     unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
+     unsigned long nr_pages = DIV_ROUND_UP(end - start, PAGE_SIZE);

"end - start" is always multiple of PAGE_SIZE, why we need DIV_ROUND_UP here?

Thank you. I agree with this and will revert this change in v5.

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 397

Quoted 12 lines
mm/hugetlb_vmemmap.c +3 -1
      int nid = page_to_nid((struct page *)start);
      struct page *page, *next;
+     int i;
 
-     while (nr_pages--) {
+     for (i = 0; i < nr_pages; i++) {
              page = alloc_pages_node(nid, gfp_mask, 0);
              if (!page)
                      goto out;
+             __mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA, 1);
              list_add_tail(&page->lru, list);
      }

Count one by ine is really inefficient. Can't we count *nr_pages* at one time?

Thanks for suggesting this optimization. I will modify the implementation to update the metadata once as opposed to every iteration.

Quoted 185 lines
mm/mm_init.c +3 -0
index 50f2f34745af..6997bf00945b 100644
@@ -26,6 +26,7 @@
  #include <linux/pgtable.h>
  #include <linux/swap.h>
  #include <linux/cma.h>
+#include <linux/vmstat.h>
  #include "internal.h"
  #include "slab.h"
  #include "shuffle.h"
@@ -1656,6 +1657,8 @@ static void __init alloc_node_mem_map(struct pglist_data *pgdat)
                      panic("Failed to allocate %ld bytes for node %d memory map\n",
                            size, pgdat->node_id);
              pgdat->node_mem_map = map + offset;
+             mod_node_early_perpage_metadata(pgdat->node_id,
+                                             DIV_ROUND_UP(size, PAGE_SIZE));
      }
      pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n",
                              __func__, pgdat->node_id, (unsigned long)pgdat,
mm/page_alloc.c +1 -0
index 85741403948f..522dc0c52610 100644
@@ -5443,6 +5443,7 @@ void __init setup_per_cpu_pageset(void)
      for_each_online_pgdat(pgdat)
              pgdat->per_cpu_nodestats =
                      alloc_percpu(struct per_cpu_nodestat);
+     store_early_perpage_metadata();
  }
 
  __meminit void zone_pcp_init(struct zone *zone)
mm/page_ext.c +23 -9
index 4548fcc66d74..d8d6db9c3d75 100644
@@ -201,6 +201,8 @@ static int __init alloc_node_page_ext(int nid)
              return -ENOMEM;
      NODE_DATA(nid)->node_page_ext = base;
      total_usage += table_size;
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                           DIV_ROUND_UP(table_size, PAGE_SIZE));
      return 0;
  }
 
@@ -255,12 +257,15 @@ static void *__meminit alloc_page_ext(size_t size, int nid)
      void *addr = NULL;
 
      addr = alloc_pages_exact_nid(nid, size, flags);
-     if (addr) {
+     if (addr)
              kmemleak_alloc(addr, size, 1, flags);
-             return addr;
-     }
+     else
+             addr = vzalloc_node(size, nid);
 
-     addr = vzalloc_node(size, nid);
+     if (addr) {
+             mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                                 DIV_ROUND_UP(size, PAGE_SIZE));
+     }
 
      return addr;
  }
@@ -303,18 +308,27 @@ static int __meminit init_section_page_ext(unsigned long pfn, int nid)
 
  static void free_page_ext(void *addr)
  {
+     size_t table_size;
+     struct page *page;
+     struct pglist_data *pgdat;
+
+     table_size = page_ext_size * PAGES_PER_SECTION;
+
      if (is_vmalloc_addr(addr)) {
+             page = vmalloc_to_page(addr);
+             pgdat = page_pgdat(page);
              vfree(addr);
      } else {
-             struct page *page = virt_to_page(addr);
-             size_t table_size;
-
-             table_size = page_ext_size * PAGES_PER_SECTION;
-
+             page = virt_to_page(addr);
+             pgdat = page_pgdat(page);
              BUG_ON(PageReserved(page));
              kmemleak_free(addr);
              free_pages_exact(addr, table_size);
      }
+
+     __mod_node_page_state(pgdat, NR_PAGE_METADATA,
+                           -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
+
  }
 
  static void __free_page_ext(unsigned long pfn)
mm/sparse-vmemmap.c +3 -0
index a2cbe44c48e1..2bc67b2c2aa2 100644
@@ -469,5 +469,8 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,
      if (r < 0)
              return NULL;
 
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                           DIV_ROUND_UP(end - start, PAGE_SIZE));
+
      return pfn_to_page(pfn);
  }
mm/sparse.c +6 -1
index 77d91e565045..7f67b5486cd1 100644
@@ -14,7 +14,7 @@
  #include <linux/swap.h>
  #include <linux/swapops.h>
  #include <linux/bootmem_info.h>
-
+#include <linux/vmstat.h>
  #include "internal.h"
  #include <asm/dma.h>
 
@@ -465,6 +465,9 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
       */
      sparsemap_buf = memmap_alloc(size, section_map_size(), addr, nid, true);
      sparsemap_buf_end = sparsemap_buf + size;
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+     mod_node_early_perpage_metadata(nid, DIV_ROUND_UP(size, PAGE_SIZE));
+#endif
  }
 
  static void __init sparse_buffer_fini(void)
@@ -641,6 +644,8 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
      unsigned long start = (unsigned long) pfn_to_page(pfn);
      unsigned long end = start + nr_pages * sizeof(struct page);
 
+     __mod_node_page_state(page_pgdat(pfn_to_page(pfn)), NR_PAGE_METADATA,
+                           -1L * (DIV_ROUND_UP(end - start, PAGE_SIZE)));
      vmemmap_free(start, end, altmap);
  }
  static void free_map_bootmem(struct page *memmap)
mm/vmstat.c +24 -0
index 00e81e99c6ee..070d2b3d2bcc 100644
@@ -1245,6 +1245,7 @@ const char * const vmstat_text[] = {
      "pgpromote_success",
      "pgpromote_candidate",
  #endif
+     "nr_page_metadata",
 
      /* enum writeback_stat_item counters */
      "nr_dirty_threshold",
@@ -2274,4 +2275,27 @@ static int __init extfrag_debug_init(void)
  }
 
  module_init(extfrag_debug_init);
+
  #endif
+
+/*
+ * Page metadata size (struct page and page_ext) in pages
+ */
+static unsigned long early_perpage_metadata[MAX_NUMNODES] __initdata;
+
+void __init mod_node_early_perpage_metadata(int nid, long delta)
+{
+     early_perpage_metadata[nid] += delta;
+}
+
+void __init store_early_perpage_metadata(void)
+{
+     int nid;
+     struct pglist_data *pgdat;
+
+     for_each_online_pgdat(pgdat) {
+             nid = pgdat->node_id;
+             __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                                   early_perpage_metadata[nid]);
+     }
+}
#5 lore raw
Quoted 231 lines · Muchun Song
Quoted 7 lines · Sourav Panda

On Tue, Oct 31, 2023 at 8:38 PM Muchun Song <[email protected]>

Quoted 10 lines · wrote:
Quoted 7 lines · Sourav Panda

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.

Quoted 5 lines
Quoted 5 lines

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. In contrast to most other memory consumption statistics, per-page metadata might not be included in MemTotal. For example, MemTotal does not include

memblock

Quoted 18 lines
Quoted 18 lines

allocations but includes buddy allocations. While on the other hand, per-page metadata would include both memblock and buddy allocations.

This memory depends on build configurations, machine architectures, and the way system is used:

Build configuration may include extra fields into "struct page", and enable / disable "page_ext"
Machine architecture defines base page sizes. For example 4K x86, 8K SPARC, 64K ARM64 (optionally), etc. The per-page metadata overhead is smaller on machines with larger page sizes. System use can change per-page overhead by using vmemmap optimizations with hugetlb pages, and emulated pmem devdax pages. Also, boot parameters can determine whether page_ext is needed to be allocated. This memory can be part of MemTotal or be outside MemTotal depending on whether the memory was hot-plugged, booted with, or hugetlb memory was returned back to the system.

Quoted 13 lines
Quoted 13 lines
---
  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               |  8 ++++++--
  mm/mm_init.c                       |  3 +++
  mm/page_alloc.c                    |  1 +
  mm/page_ext.c                      | 32

+++++++++++++++++++++---------

Quoted 6 lines

Review

Quoted 6 lines
  mm/sparse-vmemmap.c                |  3 +++
  mm/sparse.c                        |  7 ++++++-
  mm/vmstat.c                        | 24 ++++++++++++++++++++++
  13 files changed, 94 insertions(+), 14 deletions(-)
(quoted diff) +0 -0

b/Documentation/filesystems/proc.rst

Quoted 4 lines

Documentation/filesystems/proc.rst Example output. You may not have all of these

Quoted 4 lines

index 2b59cff8be17..c121f2ef9432 100644

Documentation/filesystems/proc.rst +0 -0
@@ -987,6 +987,7 @@ Example output. You may not have all of these

fields.

Quoted 7 lines
Quoted 7 lines
Documentation/filesystems/proc.rst +1 -0
      AnonPages:       4654780 kB
      Mapped:           266244 kB
      Shmem:              9976 kB
+    PageMetadata:     513419 kB
      KReclaimable:     517708 kB
      Slab:             660044 kB
      SReclaimable:     517708 kB
Quoted 8 lines

Documentation/filesystems/proc.rst Mapped · line 1096

Quoted 8 lines
Documentation/filesystems/proc.rst +2 -0
@@ -1089,6 +1090,8 @@ Mapped
                files which have been mmapped, such as libraries
  Shmem
                Total memory used by shared memory (shmem) and tmpfs
+PageMetadata
+              Memory used for per-page metadata
  KReclaimable
                Kernel allocations that the kernel will attempt to

reclaim

Quoted 1 line

Documentation/filesystems/proc.rst Mapped · line 1097

Quoted 1 line
Documentation/filesystems/proc.rst +0 -0
                under memory pressure. Includes SReclaimable (below),

and other

Quoted 5 lines

drivers/base/node.c static ssize_t node_read_meminfo(struct device

Quoted 5 lines
drivers/base/node.c +0 -0
index 493d533f8375..da728542265f 100644
@@ -428,6 +428,7 @@ static ssize_t node_read_meminfo(struct device

*dev,

Quoted 8 lines

drivers/base/node.c static ssize_t node_read_meminfo(struct device

Quoted 8 lines
drivers/base/node.c +1 -0
                           "Node %d Mapped:         %8lu kB\n"
                           "Node %d AnonPages:      %8lu kB\n"
                           "Node %d Shmem:          %8lu kB\n"
+                          "Node %d PageMetadata:   %8lu kB\n"
                           "Node %d KernelStack:    %8lu kB\n"
  #ifdef CONFIG_SHADOW_CALL_STACK
                           "Node %d ShadowCallStack:%8lu kB\n"
@@ -458,6 +459,7 @@ static ssize_t node_read_meminfo(struct device

*dev,

Quoted 1 line

drivers/base/node.c static ssize_t node_read_meminfo(struct device · line 459

Quoted 1 line
drivers/base/node.c +0 -0
                           nid, K(node_page_state(pgdat,

NR_FILE_MAPPED)),

Quoted 1 line

drivers/base/node.c static ssize_t node_read_meminfo(struct device · line 460

Quoted 1 line
drivers/base/node.c +0 -0
                           nid, K(node_page_state(pgdat,

NR_ANON_MAPPED)),

Quoted 2 lines

drivers/base/node.c static ssize_t node_read_meminfo(struct device · line 462

Quoted 2 lines
drivers/base/node.c +1 -0
                           nid, K(i.sharedram),
+                          nid, K(node_page_state(pgdat,

NR_PAGE_METADATA)),

Quoted 1 line

drivers/base/node.c static ssize_t node_read_meminfo(struct device · line 463

Quoted 1 line
drivers/base/node.c +0 -0
                           nid, node_page_state(pgdat,

NR_KERNEL_STACK_KB),

Quoted 2 lines

drivers/base/node.c static ssize_t node_read_meminfo(struct device · line 465

Quoted 2 lines
drivers/base/node.c +0 -0
  #ifdef CONFIG_SHADOW_CALL_STACK
                           nid, node_page_state(pgdat,

NR_KERNEL_SCS_KB),

Quoted 5 lines

fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m,

Quoted 5 lines
fs/proc/meminfo.c +0 -0
index 45af9a989d40..f141bb2a550d 100644
@@ -39,7 +39,9 @@ static int meminfo_proc_show(struct seq_file *m,

void *v)

Quoted 10 lines

fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m,

Quoted 10 lines
fs/proc/meminfo.c +2 -0
      long available;
      unsigned long pages[NR_LRU_LISTS];
      unsigned long sreclaimable, sunreclaim;
+     unsigned long nr_page_metadata;
      int lru;
+     int nid;
 
      si_meminfo(&i);
      si_swapinfo(&i);
@@ -57,6 +59,10 @@ static int meminfo_proc_show(struct seq_file *m,

void *v)

Quoted 1 line

fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m, · line 59

Quoted 1 line
fs/proc/meminfo.c +0 -0
      sreclaimable =

global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B);

Quoted 1 line

fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m, · line 60

Quoted 1 line
fs/proc/meminfo.c +0 -0
      sunreclaim =

global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B);

Quoted 4 lines

fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m, void *v)

Quoted 4 lines
fs/proc/meminfo.c +3 -0
+     nr_page_metadata = 0;
+     for_each_online_node(nid)
+             nr_page_metadata += node_page_state(NODE_DATA(nid),

NR_PAGE_METADATA);

Quoted 5 lines

fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m,

Quoted 5 lines
fs/proc/meminfo.c +1 -0
+
      show_val_kb(m, "MemTotal:       ", i.totalram);
      show_val_kb(m, "MemFree:        ", i.freeram);
      show_val_kb(m, "MemAvailable:   ", available);
@@ -104,6 +110,7 @@ static int meminfo_proc_show(struct seq_file *m,

void *v)

Quoted 8 lines
Quoted 8 lines
fs/proc/meminfo.c +1 -0
      show_val_kb(m, "Mapped:         ",
                  global_node_page_state(NR_FILE_MAPPED));
      show_val_kb(m, "Shmem:          ", i.sharedram);
+     show_val_kb(m, "PageMetadata:   ", nr_page_metadata);
      show_val_kb(m, "KReclaimable:   ", sreclaimable +
                  global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE));
      show_val_kb(m, "Slab:           ", sreclaimable + sunreclaim);
include/linux/mmzone.h +0 -0
Quoted 8 lines

include/linux/mmzone.h enum node_stat_item { · line 210

Quoted 8 lines

index 4106fbc5b4b3..dda1ad522324 100644

include/linux/mmzone.h +1 -0
@@ -207,6 +207,9 @@ enum node_stat_item {
      PGPROMOTE_SUCCESS,      /* promote successfully */
      PGPROMOTE_CANDIDATE,    /* candidate pages to promote */
  #endif
+     NR_PAGE_METADATA,       /* Page metadata size (struct page and

page_ext)

Quoted 10 lines

include/linux/vmstat.h static inline void lruvec_stat_sub_folio(struct

Quoted 10 lines
include/linux/mmzone.h +2 -0
+                              * in pages
+                              */
      NR_VM_NODE_STAT_ITEMS
  };
 
include/linux/vmstat.h +0 -0
index fed855bae6d8..af096a881f03 100644
@@ -656,4 +656,8 @@ static inline void lruvec_stat_sub_folio(struct

folio *folio,

Quoted 5 lines
Quoted 5 lines
include/linux/vmstat.h +2 -0
  {
      lruvec_stat_mod_folio(folio, idx, -folio_nr_pages(folio));
  }
+
+void __init mod_node_early_perpage_metadata(int nid, long delta);
Quoted 8 lines

mm/hugetlb.c static void

Quoted 8 lines
include/linux/vmstat.h +2 -0
+void __init store_early_perpage_metadata(void);
+
  #endif /* _LINUX_VMSTAT_H */
mm/hugetlb.c +0 -0
index 1301ba7b2c9a..cd3158a9c7f3 100644
@@ -1790,6 +1790,9 @@ static void

__update_and_free_hugetlb_folio(struct hstate *h,

Quoted 1 line

mm/hugetlb.c static void · line 1790

Quoted 1 line
mm/hugetlb.c +0 -0
              destroy_compound_gigantic_folio(folio,

huge_page_order(h));

Quoted 9 lines

mm/hugetlb.c static struct folio

Quoted 9 lines
mm/hugetlb.c +3 -0
              free_gigantic_folio(folio, huge_page_order(h));
      } else {
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+             __node_stat_sub_folio(folio, NR_PAGE_METADATA);
+#endif
              __free_pages(&folio->page, huge_page_order(h));
      }
  }
@@ -2125,6 +2128,7 @@ static struct folio

*alloc_buddy_hugetlb_folio(struct hstate *h,

Quoted 8 lines

mm/hugetlb.c static struct folio

Quoted 8 lines
mm/hugetlb.c +1 -0
      struct page *page;
      bool alloc_try_hard = true;
      bool retry = true;
+     struct folio *folio;
 
      /*
       * By default we always try hard to allocate the page with
@@ -2175,9 +2179,12 @@ static struct folio

*alloc_buddy_hugetlb_folio(struct hstate *h,

Quoted 9 lines

mm/hugetlb.c static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, · line 2184

Quoted 7 lines
mm/hugetlb.c +3 -1
              __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
              return NULL;
      }
-
+     folio = page_folio(page);
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+     __node_stat_add_folio(folio, NR_PAGE_METADATA)

Seems you have not tested this patch with CONFIG_SPARSEMEM_VMEMMAP

disabled.

Quoted 3 lines

You missed ";" in the end.

Thanks for reviewing this patch. I will submit v5 by testing against

FLATMEM and

Quoted 1 line

SPARSEMEM (VMEMMAP disabled) memory model on ARM32. This error was

introduced

Quoted 7 lines

in v4.

mm/hugetlb.c static struct folio · line 2185

Quoted 1 line
mm/hugetlb.c +1 -0
+#endif

I am curious why we should account HugeTLB pages as metadata.

Quoted 9 lines
Quoted 1 line

When HugeTLB pages are reserved, memory pertaining to redundant `struct

page` are

Quoted 1 line

returned to the buddy allocator for other uses. This essentially

reflects the change in the

Why the struct page are returned to the buddy when CONFIG_SPARSEMEM_VMEMMAP
is disabled? Why it is added with a PAGE_SIZE?

Thank you again!

I see both your points. I believe the solution is to remove updates to NR_PAGE_METADATA
when CONFIG_SPARSEMEM_VMEMMAP is disabled. I shall do this in v6 if you agree.

Why is it added with a PAGE_SIZE?
- You are correct. First of all we should not even update it when CONFIG_SPARSEMEM_VMEMMAP
is disabled. Furthermore, even if we did, we should have done it by a
factor of
huge_page_order(h) instead of folio_nr_pages(folio).
- This error was introduced in v4 as we switched from __mod_node_page_state(pg_dat, NR_PAGE_METADATA, huge_page_order(h)) to __node_stat_sub_folio(folio, NR_PAGE_METADATA).
- huge_page_order(h) returns 9 for each 2 MB HugeTLB page allocation while folio_nr_pages(folio), which is called in __node_stat_sub_folio returns
512.
- Removing updates to NR_PAGE_METADATA when CONFIG_SPARSEMEM_VMEMMAP is disabled shall fix this problem.

Quoted 277 lines
Quoted 8 lines

amount of struct pages when HugeTLB pages are reserved and free'd.

Quoted 5 lines
mm/hugetlb.c +1 -1
      __count_vm_event(HTLB_BUDDY_PGALLOC);
-     return page_folio(page);
+     return folio;
  }
 
Quoted 6 lines

mm/hugetlb_vmemmap.c static inline void free_vmemmap_page(struct page

Quoted 6 lines
mm/hugetlb.c +0 -0
  /*
mm/hugetlb_vmemmap.c +0 -0
index 4b9734777f69..804a93d18cab 100644
@@ -214,6 +214,7 @@ static inline void free_vmemmap_page(struct page

*page)

Quoted 8 lines

mm/hugetlb_vmemmap.c static int vmemmap_remap_free(unsigned long start,

Quoted 8 lines
mm/hugetlb_vmemmap.c +1 -0
              free_bootmem_page(page);
      else
              __free_page(page);
+     __mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA, -1);
  }
 
  /* Free a list of the vmemmap pages */
@@ -336,6 +337,7 @@ static int vmemmap_remap_free(unsigned long start,

unsigned long end,

Quoted 8 lines

mm/hugetlb_vmemmap.c static int vmemmap_remap_free(unsigned long start, unsigned long end, · line 340

Quoted 4 lines
mm/hugetlb_vmemmap.c +1 -0
                        (void *)walk.reuse_addr);
              list_add(&walk.reuse_page->lru, &vmemmap_pages);
      }
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA, 1);

What if allocation of walk.reuse_page fails?

Thank you for pointing this out. I will move the NR_PAGE_METADATA update

within the

Quoted 1 line

if ( walk.reuse_page ) clause to cover the case where walk.reuse_page

fails.

Quoted 4 lines

mm/hugetlb_vmemmap.c static int vmemmap_remap_free(unsigned long start, · line 342

Quoted 4 lines
mm/hugetlb_vmemmap.c +0 -0
      /*
       * In order to make remapping routine most efficient for the

huge pages,

Quoted 1 line

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long

Quoted 1 line
mm/hugetlb_vmemmap.c +0 -0
@@ -381,14 +383,16 @@ static int alloc_vmemmap_page_list(unsigned long

start, unsigned long end,

Quoted 3 lines

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,

Quoted 3 lines
mm/hugetlb_vmemmap.c +0 -0
                                 struct list_head *list)
  {
      gfp_t gfp_mask = GFP_KERNEL | __GFP_RETRY_MAYFAIL |

__GFP_THISNODE;

Quoted 18 lines

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 386

Quoted 2 lines
mm/hugetlb_vmemmap.c +1 -1
-     unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
+     unsigned long nr_pages = DIV_ROUND_UP(end - start, PAGE_SIZE);

"end - start" is always multiple of PAGE_SIZE, why we need DIV_ROUND_UP here?

Thank you. I agree with this and will revert this change in v5.

Quoted 10 lines
mm/hugetlb_vmemmap.c +3 -1
      int nid = page_to_nid((struct page *)start);
      struct page *page, *next;
+     int i;
 
-     while (nr_pages--) {
+     for (i = 0; i < nr_pages; i++) {
              page = alloc_pages_node(nid, gfp_mask, 0);
              if (!page)
                      goto out;
+             __mod_node_page_state(page_pgdat(page),

NR_PAGE_METADATA, 1);

Quoted 7 lines

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 397

Quoted 2 lines
mm/hugetlb_vmemmap.c +0 -0
              list_add_tail(&page->lru, list);
      }

Count one by ine is really inefficient. Can't we count *nr_pages* at one time?

Thanks for suggesting this optimization. I will modify the

implementation to update the

Quoted 16 lines

metadata once as opposed to every iteration.

mm/mm_init.c static void __init alloc_node_mem_map(struct

Quoted 14 lines
mm/mm_init.c +1 -0
index 50f2f34745af..6997bf00945b 100644
@@ -26,6 +26,7 @@
  #include <linux/pgtable.h>
  #include <linux/swap.h>
  #include <linux/cma.h>
+#include <linux/vmstat.h>
  #include "internal.h"
  #include "slab.h"
  #include "shuffle.h"
@@ -1656,6 +1657,8 @@ static void __init alloc_node_mem_map(struct

pglist_data *pgdat)

Quoted 1 line

mm/mm_init.c static void __init alloc_node_mem_map(struct · line 1657

Quoted 1 line
mm/mm_init.c +0 -0
                      panic("Failed to allocate %ld bytes for node %d

memory map\n",

Quoted 4 lines

mm/mm_init.c static void __init alloc_node_mem_map(struct pglist_data *pgdat)

Quoted 4 lines
mm/mm_init.c +2 -0
                            size, pgdat->node_id);
              pgdat->node_mem_map = map + offset;
+             mod_node_early_perpage_metadata(pgdat->node_id,
+                                             DIV_ROUND_UP(size,

PAGE_SIZE));

Quoted 3 lines

mm/mm_init.c static void __init alloc_node_mem_map(struct pglist_data *pgdat)

Quoted 3 lines
mm/mm_init.c +0 -0
      }
      pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n",
                              __func__, pgdat->node_id, (unsigned

long)pgdat,

Quoted 25 lines
Quoted 25 lines
mm/page_alloc.c +1 -0
index 85741403948f..522dc0c52610 100644
@@ -5443,6 +5443,7 @@ void __init setup_per_cpu_pageset(void)
      for_each_online_pgdat(pgdat)
              pgdat->per_cpu_nodestats =
                      alloc_percpu(struct per_cpu_nodestat);
+     store_early_perpage_metadata();
  }
 
  __meminit void zone_pcp_init(struct zone *zone)
mm/page_ext.c +2 -0
index 4548fcc66d74..d8d6db9c3d75 100644
@@ -201,6 +201,8 @@ static int __init alloc_node_page_ext(int nid)
              return -ENOMEM;
      NODE_DATA(nid)->node_page_ext = base;
      total_usage += table_size;
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                           DIV_ROUND_UP(table_size, PAGE_SIZE));
      return 0;
  }
 
Quoted 1 line

mm/page_ext.c static void *__meminit alloc_page_ext(size_t

Quoted 1 line
mm/page_ext.c +0 -0
@@ -255,12 +257,15 @@ static void *__meminit alloc_page_ext(size_t

size, int nid)

Quoted 17 lines
Quoted 17 lines
mm/page_ext.c +7 -4
      void *addr = NULL;
 
      addr = alloc_pages_exact_nid(nid, size, flags);
-     if (addr) {
+     if (addr)
              kmemleak_alloc(addr, size, 1, flags);
-             return addr;
-     }
+     else
+             addr = vzalloc_node(size, nid);
 
-     addr = vzalloc_node(size, nid);
+     if (addr) {
+             mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                                 DIV_ROUND_UP(size, PAGE_SIZE));
+     }
 
Quoted 3 lines

mm/page_ext.c static int __meminit

Quoted 3 lines
mm/page_ext.c +0 -0
      return addr;
  }
@@ -303,18 +308,27 @@ static int __meminit

init_section_page_ext(unsigned long pfn, int nid)

Quoted 20 lines
Quoted 20 lines
mm/page_ext.c +9 -5
  static void free_page_ext(void *addr)
  {
+     size_t table_size;
+     struct page *page;
+     struct pglist_data *pgdat;
+
+     table_size = page_ext_size * PAGES_PER_SECTION;
+
      if (is_vmalloc_addr(addr)) {
+             page = vmalloc_to_page(addr);
+             pgdat = page_pgdat(page);
              vfree(addr);
      } else {
-             struct page *page = virt_to_page(addr);
-             size_t table_size;
-
-             table_size = page_ext_size * PAGES_PER_SECTION;
-
+             page = virt_to_page(addr);
Quoted 8 lines

mm/page_ext.c static int __meminit init_section_page_ext(unsigned long pfn, int nid)

Quoted 8 lines
mm/page_ext.c +4 -0
+             pgdat = page_pgdat(page);
              BUG_ON(PageReserved(page));
              kmemleak_free(addr);
              free_pages_exact(addr, table_size);
      }
+
+     __mod_node_page_state(pgdat, NR_PAGE_METADATA,
+                           -1L * (DIV_ROUND_UP(table_size,

PAGE_SIZE)));

Quoted 9 lines

mm/sparse-vmemmap.c struct page * __meminit

Quoted 9 lines
mm/page_ext.c +1 -0
+
  }
 
  static void __free_page_ext(unsigned long pfn)
mm/sparse-vmemmap.c +0 -0
index a2cbe44c48e1..2bc67b2c2aa2 100644
@@ -469,5 +469,8 @@ struct page * __meminit

__populate_section_memmap(unsigned long pfn,

Quoted 21 lines
Quoted 21 lines
mm/sparse-vmemmap.c +3 -0
      if (r < 0)
              return NULL;
 
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                           DIV_ROUND_UP(end - start, PAGE_SIZE));
+
      return pfn_to_page(pfn);
  }
mm/sparse.c +1 -1
index 77d91e565045..7f67b5486cd1 100644
@@ -14,7 +14,7 @@
  #include <linux/swap.h>
  #include <linux/swapops.h>
  #include <linux/bootmem_info.h>
-
+#include <linux/vmstat.h>
  #include "internal.h"
  #include <asm/dma.h>
 
Quoted 1 line

mm/sparse.c static void __init sparse_buffer_init(unsigned

Quoted 1 line
mm/sparse.c +0 -0
@@ -465,6 +465,9 @@ static void __init sparse_buffer_init(unsigned

long size, int nid)

Quoted 2 lines

mm/sparse.c static void __init sparse_buffer_init(unsigned · line 466

Quoted 2 lines
mm/sparse.c +0 -0
       */
      sparsemap_buf = memmap_alloc(size, section_map_size(), addr,

nid, true);

Quoted 3 lines

mm/sparse.c static void __init sparse_buffer_init(unsigned long size, int nid)

Quoted 3 lines
mm/sparse.c +2 -0
      sparsemap_buf_end = sparsemap_buf + size;
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+     mod_node_early_perpage_metadata(nid, DIV_ROUND_UP(size,

PAGE_SIZE));

Quoted 5 lines

mm/sparse.c static void depopulate_section_memmap(unsigned

Quoted 5 lines
mm/sparse.c +1 -0
+#endif
  }
 
  static void __init sparse_buffer_fini(void)
@@ -641,6 +644,8 @@ static void depopulate_section_memmap(unsigned

long pfn, unsigned long nr_pages,

Quoted 4 lines

mm/sparse.c static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,

Quoted 4 lines
mm/sparse.c +1 -0
      unsigned long start = (unsigned long) pfn_to_page(pfn);
      unsigned long end = start + nr_pages * sizeof(struct page);
 
+     __mod_node_page_state(page_pgdat(pfn_to_page(pfn)),

NR_PAGE_METADATA,

Quoted 1 line

mm/sparse.c static void depopulate_section_memmap(unsigned · line 648

Quoted 1 line
mm/sparse.c +1 -0
+                           -1L * (DIV_ROUND_UP(end - start,

PAGE_SIZE)));

Quoted 43 lines
Quoted 43 lines
mm/sparse.c +0 -0
      vmemmap_free(start, end, altmap);
  }
  static void free_map_bootmem(struct page *memmap)
mm/vmstat.c +24 -0
index 00e81e99c6ee..070d2b3d2bcc 100644
@@ -1245,6 +1245,7 @@ const char * const vmstat_text[] = {
      "pgpromote_success",
      "pgpromote_candidate",
  #endif
+     "nr_page_metadata",
 
      /* enum writeback_stat_item counters */
      "nr_dirty_threshold",
@@ -2274,4 +2275,27 @@ static int __init extfrag_debug_init(void)
  }
 
  module_init(extfrag_debug_init);
+
  #endif
+
+/*
+ * Page metadata size (struct page and page_ext) in pages
+ */
+static unsigned long early_perpage_metadata[MAX_NUMNODES] __initdata;
+
+void __init mod_node_early_perpage_metadata(int nid, long delta)
+{
+     early_perpage_metadata[nid] += delta;
+}
+
+void __init store_early_perpage_metadata(void)
+{
+     int nid;
+     struct pglist_data *pgdat;
+
+     for_each_online_pgdat(pgdat) {
+             nid = pgdat->node_id;
+             __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                                   early_perpage_metadata[nid]);
+     }
+}
maintainer
#6 lore raw
Quoted 217 lines · Sourav Panda
Quoted 196 lines · Muchun Song

On Nov 2, 2023, at 06:58, Sourav Panda <[email protected]> wrote:

On Tue, Oct 31, 2023 at 8:38 PM Muchun Song <[email protected]> wrote:

Quoted 172 lines · Sourav Panda

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. In contrast to most other memory consumption statistics, per-page metadata might not be included in MemTotal. For example, MemTotal does not include memblock allocations but includes buddy allocations. While on the other hand, per-page metadata would include both memblock and buddy allocations.

This memory depends on build configurations, machine architectures, and the way system is used:

Build configuration may include extra fields into "struct page", and enable / disable "page_ext"
Machine architecture defines base page sizes. For example 4K x86, 8K SPARC, 64K ARM64 (optionally), etc. The per-page metadata overhead is smaller on machines with larger page sizes. System use can change per-page overhead by using vmemmap optimizations with hugetlb pages, and emulated pmem devdax pages. Also, boot parameters can determine whether page_ext is needed to be allocated. This memory can be part of MemTotal or be outside MemTotal depending on whether the memory was hot-plugged, booted with, or hugetlb memory was returned back to the system.

---
  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               |  8 ++++++--
  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, 94 insertions(+), 14 deletions(-)
Documentation/filesystems/proc.rst +3 -0
index 2b59cff8be17..c121f2ef9432 100644
@@ -987,6 +987,7 @@ Example output. You may not have all of these fields.
      AnonPages:       4654780 kB
      Mapped:           266244 kB
      Shmem:              9976 kB
+    PageMetadata:     513419 kB
      KReclaimable:     517708 kB
      Slab:             660044 kB
      SReclaimable:     517708 kB
@@ -1089,6 +1090,8 @@ Mapped
                files which have been mmapped, such as libraries
  Shmem
                Total memory used by shared memory (shmem) and tmpfs
+PageMetadata
+              Memory used for per-page metadata
  KReclaimable
                Kernel allocations that the kernel will attempt to reclaim
                under memory pressure. Includes SReclaimable (below), and other
drivers/base/node.c +2 -0
index 493d533f8375..da728542265f 100644
@@ -428,6 +428,7 @@ static ssize_t node_read_meminfo(struct device *dev,
                           "Node %d Mapped:         %8lu kB\n"
                           "Node %d AnonPages:      %8lu kB\n"
                           "Node %d Shmem:          %8lu kB\n"
+                          "Node %d PageMetadata:   %8lu kB\n"
                           "Node %d KernelStack:    %8lu kB\n"
  #ifdef CONFIG_SHADOW_CALL_STACK
                           "Node %d ShadowCallStack:%8lu kB\n"
@@ -458,6 +459,7 @@ static ssize_t node_read_meminfo(struct device *dev,
                           nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
                           nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
                           nid, K(i.sharedram),
+                          nid, K(node_page_state(pgdat, NR_PAGE_METADATA)),
                           nid, node_page_state(pgdat, NR_KERNEL_STACK_KB),
  #ifdef CONFIG_SHADOW_CALL_STACK
                           nid, node_page_state(pgdat, NR_KERNEL_SCS_KB),
fs/proc/meminfo.c +7 -0
index 45af9a989d40..f141bb2a550d 100644
@@ -39,7 +39,9 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
      long available;
      unsigned long pages[NR_LRU_LISTS];
      unsigned long sreclaimable, sunreclaim;
+     unsigned long nr_page_metadata;
      int lru;
+     int nid;
 
      si_meminfo(&i);
      si_swapinfo(&i);
@@ -57,6 +59,10 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
      sreclaimable = global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B);
      sunreclaim = global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B);
 
+     nr_page_metadata = 0;
+     for_each_online_node(nid)
+             nr_page_metadata += node_page_state(NODE_DATA(nid), NR_PAGE_METADATA);
+
      show_val_kb(m, "MemTotal:       ", i.totalram);
      show_val_kb(m, "MemFree:        ", i.freeram);
      show_val_kb(m, "MemAvailable:   ", available);
@@ -104,6 +110,7 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
      show_val_kb(m, "Mapped:         ",
                  global_node_page_state(NR_FILE_MAPPED));
      show_val_kb(m, "Shmem:          ", i.sharedram);
+     show_val_kb(m, "PageMetadata:   ", nr_page_metadata);
      show_val_kb(m, "KReclaimable:   ", sreclaimable +
                  global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE));
      show_val_kb(m, "Slab:           ", sreclaimable + sunreclaim);
include/linux/mmzone.h +3 -0
index 4106fbc5b4b3..dda1ad522324 100644
@@ -207,6 +207,9 @@ enum node_stat_item {
      PGPROMOTE_SUCCESS,      /* promote successfully */
      PGPROMOTE_CANDIDATE,    /* candidate pages to promote */
  #endif
+     NR_PAGE_METADATA,       /* Page metadata size (struct page and page_ext)
+                              * in pages
+                              */
      NR_VM_NODE_STAT_ITEMS
  };
 
include/linux/vmstat.h +4 -0
index fed855bae6d8..af096a881f03 100644
@@ -656,4 +656,8 @@ static inline void lruvec_stat_sub_folio(struct folio *folio,
  {
      lruvec_stat_mod_folio(folio, idx, -folio_nr_pages(folio));
  }
+
+void __init mod_node_early_perpage_metadata(int nid, long delta);
+void __init store_early_perpage_metadata(void);
+
  #endif /* _LINUX_VMSTAT_H */
mm/hugetlb.c +4 -0
index 1301ba7b2c9a..cd3158a9c7f3 100644
@@ -1790,6 +1790,9 @@ static void __update_and_free_hugetlb_folio(struct hstate *h,
              destroy_compound_gigantic_folio(folio, huge_page_order(h));
              free_gigantic_folio(folio, huge_page_order(h));
      } else {
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+             __node_stat_sub_folio(folio, NR_PAGE_METADATA);
+#endif
              __free_pages(&folio->page, huge_page_order(h));
      }
  }
@@ -2125,6 +2128,7 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
      struct page *page;
      bool alloc_try_hard = true;
      bool retry = true;
+     struct folio *folio;
 
      /*
       * By default we always try hard to allocate the page with

mm/hugetlb.c static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, · line 2184

Quoted 7 lines
mm/hugetlb.c +3 -1
@@ -2175,9 +2179,12 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
              __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
              return NULL;
      }
-
+     folio = page_folio(page);
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+     __node_stat_add_folio(folio, NR_PAGE_METADATA)

Seems you have not tested this patch with CONFIG_SPARSEMEM_VMEMMAP disabled. You missed ";" in the end.

Thanks for reviewing this patch. I will submit v5 by testing against FLATMEM and SPARSEMEM (VMEMMAP disabled) memory model on ARM32. This error was introduced in v4.

Quoted 6 lines

mm/hugetlb.c static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, · line 2185

Quoted 1 line
mm/hugetlb.c +1 -0
+#endif

I am curious why we should account HugeTLB pages as metadata.

When HugeTLB pages are reserved, memory pertaining to redundant struct page are returned to the buddy allocator for other uses. This essentially reflects the change in the

Why the struct page are returned to the buddy when CONFIG_SPARSEMEM_VMEMMAP is disabled? Why it is added with a PAGE_SIZE?

Thank you again!

Quoted 7 lines

I see both your points. I believe the solution is to remove updates to NR_PAGE_METADATA when CONFIG_SPARSEMEM_VMEMMAP is disabled. I shall do this in v6 if you agree.

Why is it added with a PAGE_SIZE?
- You are correct. First of all we should not even update it when CONFIG_SPARSEMEM_VMEMMAP is disabled. Furthermore, even if we did, we should have done it by a factor of huge_page_order(h) instead of folio_nr_pages(folio).

Actually, I think we should remove this counting from this path regardless of whether CONFIG_SPARSEMEM_VMEMMAP is enabled or not. The metadata (aka page structure) is not allocated from here, it is allocated from where the vmemmap areas are initialized during boot time. You should count there.

Quoted 259 lines

- This error was introduced in v4 as we switched from __mod_node_page_state(pg_dat, NR_PAGE_METADATA, huge_page_order(h)) to __node_stat_sub_folio(folio, NR_PAGE_METADATA).
- huge_page_order(h) returns 9 for each 2 MB HugeTLB page allocation while folio_nr_pages(folio), which is called in __node_stat_sub_folio returns 512.
- Removing updates to NR_PAGE_METADATA when CONFIG_SPARSEMEM_VMEMMAP is disabled shall fix this problem.

Quoted 250 lines

amount of struct pages when HugeTLB pages are reserved and free'd.

Quoted 19 lines
mm/hugetlb.c +1 -1
      __count_vm_event(HTLB_BUDDY_PGALLOC);
-     return page_folio(page);
+     return folio;
  }
 
  /*
mm/hugetlb_vmemmap.c +1 -0
index 4b9734777f69..804a93d18cab 100644
@@ -214,6 +214,7 @@ static inline void free_vmemmap_page(struct page *page)
              free_bootmem_page(page);
      else
              __free_page(page);
+     __mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA, -1);
  }
 
  /* Free a list of the vmemmap pages */

mm/hugetlb_vmemmap.c static int vmemmap_remap_free(unsigned long start, unsigned long end, · line 340

Quoted 4 lines
mm/hugetlb_vmemmap.c +1 -0
@@ -336,6 +337,7 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
                        (void *)walk.reuse_addr);
              list_add(&walk.reuse_page->lru, &vmemmap_pages);
      }
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA, 1);

What if allocation of walk.reuse_page fails?

Thank you for pointing this out. I will move the NR_PAGE_METADATA update within the if ( walk.reuse_page ) clause to cover the case where walk.reuse_page fails.

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 386

Quoted 9 lines
mm/hugetlb_vmemmap.c +1 -1
      /*
       * In order to make remapping routine most efficient for the huge pages,
@@ -381,14 +383,16 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
                                 struct list_head *list)
  {
      gfp_t gfp_mask = GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_THISNODE;
-     unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
+     unsigned long nr_pages = DIV_ROUND_UP(end - start, PAGE_SIZE);

"end - start" is always multiple of PAGE_SIZE, why we need DIV_ROUND_UP here?

Thank you. I agree with this and will revert this change in v5.

mm/hugetlb_vmemmap.c static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, · line 397

Quoted 12 lines
mm/hugetlb_vmemmap.c +3 -1
      int nid = page_to_nid((struct page *)start);
      struct page *page, *next;
+     int i;
 
-     while (nr_pages--) {
+     for (i = 0; i < nr_pages; i++) {
              page = alloc_pages_node(nid, gfp_mask, 0);
              if (!page)
                      goto out;
+             __mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA, 1);
              list_add_tail(&page->lru, list);
      }

Count one by ine is really inefficient. Can't we count *nr_pages* at one time?

Thanks for suggesting this optimization. I will modify the implementation to update the metadata once as opposed to every iteration.

Quoted 185 lines
mm/mm_init.c +3 -0
index 50f2f34745af..6997bf00945b 100644
@@ -26,6 +26,7 @@
  #include <linux/pgtable.h>
  #include <linux/swap.h>
  #include <linux/cma.h>
+#include <linux/vmstat.h>
  #include "internal.h"
  #include "slab.h"
  #include "shuffle.h"
@@ -1656,6 +1657,8 @@ static void __init alloc_node_mem_map(struct pglist_data *pgdat)
                      panic("Failed to allocate %ld bytes for node %d memory map\n",
                            size, pgdat->node_id);
              pgdat->node_mem_map = map + offset;
+             mod_node_early_perpage_metadata(pgdat->node_id,
+                                             DIV_ROUND_UP(size, PAGE_SIZE));
      }
      pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n",
                              __func__, pgdat->node_id, (unsigned long)pgdat,
mm/page_alloc.c +1 -0
index 85741403948f..522dc0c52610 100644
@@ -5443,6 +5443,7 @@ void __init setup_per_cpu_pageset(void)
      for_each_online_pgdat(pgdat)
              pgdat->per_cpu_nodestats =
                      alloc_percpu(struct per_cpu_nodestat);
+     store_early_perpage_metadata();
  }
 
  __meminit void zone_pcp_init(struct zone *zone)
mm/page_ext.c +23 -9
index 4548fcc66d74..d8d6db9c3d75 100644
@@ -201,6 +201,8 @@ static int __init alloc_node_page_ext(int nid)
              return -ENOMEM;
      NODE_DATA(nid)->node_page_ext = base;
      total_usage += table_size;
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                           DIV_ROUND_UP(table_size, PAGE_SIZE));
      return 0;
  }
 
@@ -255,12 +257,15 @@ static void *__meminit alloc_page_ext(size_t size, int nid)
      void *addr = NULL;
 
      addr = alloc_pages_exact_nid(nid, size, flags);
-     if (addr) {
+     if (addr)
              kmemleak_alloc(addr, size, 1, flags);
-             return addr;
-     }
+     else
+             addr = vzalloc_node(size, nid);
 
-     addr = vzalloc_node(size, nid);
+     if (addr) {
+             mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                                 DIV_ROUND_UP(size, PAGE_SIZE));
+     }
 
      return addr;
  }
@@ -303,18 +308,27 @@ static int __meminit init_section_page_ext(unsigned long pfn, int nid)
 
  static void free_page_ext(void *addr)
  {
+     size_t table_size;
+     struct page *page;
+     struct pglist_data *pgdat;
+
+     table_size = page_ext_size * PAGES_PER_SECTION;
+
      if (is_vmalloc_addr(addr)) {
+             page = vmalloc_to_page(addr);
+             pgdat = page_pgdat(page);
              vfree(addr);
      } else {
-             struct page *page = virt_to_page(addr);
-             size_t table_size;
-
-             table_size = page_ext_size * PAGES_PER_SECTION;
-
+             page = virt_to_page(addr);
+             pgdat = page_pgdat(page);
              BUG_ON(PageReserved(page));
              kmemleak_free(addr);
              free_pages_exact(addr, table_size);
      }
+
+     __mod_node_page_state(pgdat, NR_PAGE_METADATA,
+                           -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
+
  }
 
  static void __free_page_ext(unsigned long pfn)
mm/sparse-vmemmap.c +3 -0
index a2cbe44c48e1..2bc67b2c2aa2 100644
@@ -469,5 +469,8 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,
      if (r < 0)
              return NULL;
 
+     __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                           DIV_ROUND_UP(end - start, PAGE_SIZE));
+
      return pfn_to_page(pfn);
  }
mm/sparse.c +6 -1
index 77d91e565045..7f67b5486cd1 100644
@@ -14,7 +14,7 @@
  #include <linux/swap.h>
  #include <linux/swapops.h>
  #include <linux/bootmem_info.h>
-
+#include <linux/vmstat.h>
  #include "internal.h"
  #include <asm/dma.h>
 
@@ -465,6 +465,9 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
       */
      sparsemap_buf = memmap_alloc(size, section_map_size(), addr, nid, true);
      sparsemap_buf_end = sparsemap_buf + size;
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+     mod_node_early_perpage_metadata(nid, DIV_ROUND_UP(size, PAGE_SIZE));
+#endif
  }
 
  static void __init sparse_buffer_fini(void)
@@ -641,6 +644,8 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
      unsigned long start = (unsigned long) pfn_to_page(pfn);
      unsigned long end = start + nr_pages * sizeof(struct page);
 
+     __mod_node_page_state(page_pgdat(pfn_to_page(pfn)), NR_PAGE_METADATA,
+                           -1L * (DIV_ROUND_UP(end - start, PAGE_SIZE)));
      vmemmap_free(start, end, altmap);
  }
  static void free_map_bootmem(struct page *memmap)
mm/vmstat.c +24 -0
index 00e81e99c6ee..070d2b3d2bcc 100644
@@ -1245,6 +1245,7 @@ const char * const vmstat_text[] = {
      "pgpromote_success",
      "pgpromote_candidate",
  #endif
+     "nr_page_metadata",
 
      /* enum writeback_stat_item counters */
      "nr_dirty_threshold",
@@ -2274,4 +2275,27 @@ static int __init extfrag_debug_init(void)
  }
 
  module_init(extfrag_debug_init);
+
  #endif
+
+/*
+ * Page metadata size (struct page and page_ext) in pages
+ */
+static unsigned long early_perpage_metadata[MAX_NUMNODES] __initdata;
+
+void __init mod_node_early_perpage_metadata(int nid, long delta)
+{
+     early_perpage_metadata[nid] += delta;
+}
+
+void __init store_early_perpage_metadata(void)
+{
+     int nid;
+     struct pglist_data *pgdat;
+
+     for_each_online_pgdat(pgdat) {
+             nid = pgdat->node_id;
+             __mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA,
+                                   early_perpage_metadata[nid]);
+     }
+}