Skip to contentSkip to conversation

mm: report per-page metadata information

PATCH Superseded Sourav Panda 3 participants 7 messages Oct 18, 2023 – Oct 19, 2023

Subsystems (10)

v2 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm: report per-page metadata information

Changelog:
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
-----------------------------------
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                       |  8 +++++++-
 mm/hugetlb_vmemmap.c               |  8 +++++++-
 mm/mm_init.c                       |  3 +++
 mm/page_alloc.c                    |  1 +
 mm/page_ext.c                      | 18 ++++++++++++++----
 mm/sparse-vmemmap.c                |  3 +++
 mm/sparse.c                        |  7 ++++++-
 mm/vmstat.c                        | 24 ++++++++++++++++++++++++
 13 files changed, 84 insertions(+), 7 deletions(-)
Signature · 1 line
2.42.0.655.g421f12c284-goog
#1 lore raw

PATCH v2 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.

Change-Id I4351791c9f4c1e9759cbd8e646e808565dbb595f
---
 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                       |  8 +++++++-
 mm/hugetlb_vmemmap.c               |  8 +++++++-
 mm/mm_init.c                       |  3 +++
 mm/page_alloc.c                    |  1 +
 mm/page_ext.c                      | 28 ++++++++++++++++++++--------
 mm/sparse-vmemmap.c                |  3 +++
 mm/sparse.c                        |  7 ++++++-
 mm/vmstat.c                        | 24 ++++++++++++++++++++++++
 13 files changed, 90 insertions(+), 11 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 +7 -1
index 52d26072dfda..546f8338aa79 100644
@@ -1758,6 +1758,10 @@ 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
+		__mod_node_page_state(NODE_DATA(page_to_nid(&folio->page)),
+				      NR_PAGE_METADATA, -huge_page_order(h));
+#endif
 		__free_pages(&folio->page, huge_page_order(h));
 	}
 }
@@ -2143,7 +2147,9 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
 		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
 		return NULL;
 	}
-
+#ifndef CONFIG_SPARSEMEM_VMEMMAP
+	__mod_node_page_state(NODE_DATA(nid), NR_PAGE_METADATA, huge_page_order(h));
+#endif
 	__count_vm_event(HTLB_BUDDY_PGALLOC);
 	return page_folio(page);
 }
mm/hugetlb_vmemmap.c +7 -1
index 4b9734777f69..71c44d2471d0 100644
@@ -214,6 +214,8 @@ static inline void free_vmemmap_page(struct page *page)
 		free_bootmem_page(page);
 	else
 		__free_page(page);
+	__mod_node_page_state(NODE_DATA(page_to_nid(page)),
+			      NR_PAGE_METADATA, -1);
 }
 
 /* Free a list of the vmemmap pages */
@@ -336,6 +338,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,
@@ -384,11 +387,14 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
 	unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
 	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(NODE_DATA(page_to_nid(page)),
+				      NR_PAGE_METADATA, 1);
 		list_add_tail(&page->lru, list);
 	}
 
mm/mm_init.c +3 -0
index 50f2f34745af..e02dce7e2e9a 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,
+						PAGE_ALIGN(size) >> PAGE_SHIFT);
 	}
 	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 95546f376302..aa3cb96922e9 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 +20 -8
index 4548fcc66d74..825413888112 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,
+			      PAGE_ALIGN(table_size) >> PAGE_SHIFT);
 	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,
+				    PAGE_ALIGN(size) >> PAGE_SHIFT);
+	}
 
 	return addr;
 }
@@ -303,18 +308,25 @@ 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;
+
+	table_size = page_ext_size * PAGES_PER_SECTION;
+
 	if (is_vmalloc_addr(addr)) {
+		page = vmalloc_to_page(addr);
 		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);
 
 		BUG_ON(PageReserved(page));
 		kmemleak_free(addr);
 		free_pages_exact(addr, table_size);
 	}
+
+	__mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA,
+			      -1L * (PAGE_ALIGN(table_size) >> PAGE_SHIFT));
+
 }
 
 static void __free_page_ext(unsigned long pfn)
mm/sparse-vmemmap.c +3 -0
index a2cbe44c48e1..e33f302db7c6 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,
+			      PAGE_ALIGN(end - start) >> PAGE_SHIFT);
+
 	return pfn_to_page(pfn);
 }
mm/sparse.c +6 -1
index 77d91e565045..db78233a85ef 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, PAGE_ALIGN(size) >> PAGE_SHIFT);
+#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(NODE_DATA(page_to_nid(pfn_to_page(pfn))), NR_PAGE_METADATA,
+			      (long)-1 * (PAGE_ALIGN(end - start) >> PAGE_SHIFT));
 	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.655.g421f12c284-goog
maintainer
#2 lore raw
Quoted 30 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.

Quoted 3 lines
Change-Id I4351791c9f4c1e9759cbd8e646e808565dbb595f

checkpatch.pl should have said that "Change-Id:" should be removed, why didn't you run this on your patch before submitting it?

thanks,

greg k-h

#3 lore raw

Hi Greg,

Thank you very much for reviewing my patch. I removed the other style errors pointed by checkpatch.pl but missed this one. My bad! I will fix it in version 3.

With regards,
Sourav Panda

Quoted 42 lines · Greg KH
Quoted 30 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.

Quoted 3 lines
Change-Id I4351791c9f4c1e9759cbd8e646e808565dbb595f

checkpatch.pl should have said that "Change-Id:" should be removed, why didn't you run this on your patch before submitting it?

thanks,

greg k-h

#4 lore raw
In reply to Greg KH
Quoted 35 lines · Greg KH
Quoted 30 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.

Quoted 3 lines
Change-Id I4351791c9f4c1e9759cbd8e646e808565dbb595f
Quoted 7 lines

checkpatch.pl should have said that "Change-Id:" should be removed, why didn't you run this on your patch before submitting it?

thanks,

greg k-h

Hi Greg,

Thank you very much for reviewing my patch. I removed the other style errors pointed by checkpatch.pl but missed this one. My bad! I will fix it in version 3.

With regards,
Sourav Panda

#5 lore raw
In reply to Sourav Panda

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

Quoted 2 lines · Sourav Panda
mm/mm_init.c +2 -0
+		mod_node_early_perpage_metadata(pgdat->node_id,
+						PAGE_ALIGN(size) >> PAGE_SHIFT);

What a curious way of writing DIV_ROUND_UP(size, PAGE_SIZE) (throughout). A quick grep shows about 230 DIV_ROUND_UP and 110 of what you wrote, so it's not something you invented, but DIV_ROUND_UP is clearer.

Quoted 16 lines
mm/page_ext.c +6 -3
@@ -303,18 +308,25 @@ 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;
+
+	table_size = page_ext_size * PAGES_PER_SECTION;
+
 	if (is_vmalloc_addr(addr)) {
+		page = vmalloc_to_page(addr);
 		vfree(addr);
 	} else {
-		struct page *page = virt_to_page(addr);
-		size_t table_size;
-

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

Quoted 10 lines
mm/page_ext.c +4 -1
-		table_size = page_ext_size * PAGES_PER_SECTION;
+		page = virt_to_page(addr);
 
 		BUG_ON(PageReserved(page));
 		kmemleak_free(addr);
 		free_pages_exact(addr, table_size);
 	}
+
+	__mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA,
+			      -1L * (PAGE_ALIGN(table_size) >> PAGE_SHIFT));

This troubles me. We're freeing the memory and then dereferencing the page that was freed. I know that struct pages don't go away when they're freed, and they don't change which node they're allocated to, but it feels wrong. I'd be happier if the page_pgdat() were extracted prior to freeing the memory.

#6 lore raw
Quoted 39 lines · Matthew Wilcox

Review

Quoted 2 lines · Sourav Panda
(quoted diff) +2 -0
+             mod_node_early_perpage_metadata(pgdat->node_id,
+                                             PAGE_ALIGN(size) >>

PAGE_SHIFT);

What a curious way of writing DIV_ROUND_UP(size, PAGE_SIZE) (throughout). A quick grep shows about 230 DIV_ROUND_UP and 110 of what you wrote, so it's not something you invented, but DIV_ROUND_UP is clearer.

Review static int __meminit

Quoted 1 line
(quoted diff) +0 -0
@@ -303,18 +308,25 @@ static int __meminit

init_section_page_ext(unsigned long pfn, int nid)

Quoted 15 lines
mm/page_ext.c +6 -3
 static void free_page_ext(void *addr)
 {
+     size_t table_size;
+     struct page *page;
+
+     table_size = page_ext_size * PAGES_PER_SECTION;
+
      if (is_vmalloc_addr(addr)) {
+             page = vmalloc_to_page(addr);
              vfree(addr);
      } else {
-             struct page *page = virt_to_page(addr);
-             size_t table_size;
-

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

Quoted 10 lines
mm/page_ext.c +4 -1
-             table_size = page_ext_size * PAGES_PER_SECTION;
+             page = virt_to_page(addr);
 
              BUG_ON(PageReserved(page));
              kmemleak_free(addr);
              free_pages_exact(addr, table_size);
      }
+
+     __mod_node_page_state(page_pgdat(page), NR_PAGE_METADATA,
+                           -1L * (PAGE_ALIGN(table_size) >>

PAGE_SHIFT));

Quoted 6 lines

This troubles me. We're freeing the memory and then dereferencing the page that was freed. I know that struct pages don't go away when they're freed, and they don't change which node they're allocated to, but it feels wrong. I'd be happier if the page_pgdat() were extracted prior to freeing the memory.

Thank you Matthew Wilcox for pointing this out. I will extract page_pgdat() prior to freeing the memory in v3.

With regards,
Sourav Panda