linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/4] Fixes for memmap accounting
@ 2024-08-08 15:42 Pasha Tatashin
  2024-08-08 15:42 ` [PATCH v3 1/4] mm: update the memmap stat before page is freed Pasha Tatashin
                   ` (3 more replies)
  0 siblings, 4 replies; 20+ messages in thread
From: Pasha Tatashin @ 2024-08-08 15:42 UTC (permalink / raw)
  To: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs,
	pasha.tatashin, rientjes, rppt, souravpanda, vbabka, willy,
	dan.j.williams, yi.zhang, alison.schofield, david, yosryahmed

Memmap accounting provides us with observability of how much memory is
used for per-page metadata: i.e. "struct page"'s and "struct page_ext".
It also provides with information of how much was allocated using
boot allocator (i.e. not part of MemTotal), and how much was allocated
using buddy allocated (i.e. part of MemTotal).

This small series fixes a few problems that were discovered with the
original patch.

Changes:
	- Added patch "mm: add system wide stats items category"
	- Changed from using system-wide events to system-wide stats
v2:
https://lore.kernel.org/lkml/20240807211929.3433304-1-pasha.tatashin@soleen.com

Pasha Tatashin (4):
  mm: update the memmap stat before page is freed
  mm: don't account memmap on failure
  mm: add system wide stats items category
  mm: don't account memmap per-node

 include/linux/mmzone.h |  2 --
 include/linux/vmstat.h | 22 +++++++-----------
 mm/hugetlb_vmemmap.c   | 13 +++++------
 mm/mm_init.c           |  3 +--
 mm/page_alloc.c        |  1 -
 mm/page_ext.c          | 15 +++++--------
 mm/sparse-vmemmap.c    | 11 ++++-----
 mm/sparse.c            |  5 ++---
 mm/vmstat.c            | 51 ++++++++++++++++++++----------------------
 9 files changed, 49 insertions(+), 74 deletions(-)

-- 
2.46.0.76.ge559c4bf1a-goog



^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v3 1/4] mm: update the memmap stat before page is freed
  2024-08-08 15:42 [PATCH v3 0/4] Fixes for memmap accounting Pasha Tatashin
@ 2024-08-08 15:42 ` Pasha Tatashin
  2024-08-08 17:17   ` Yosry Ahmed
  2024-08-08 17:17   ` Fan Ni
  2024-08-08 15:42 ` [PATCH v3 2/4] mm: don't account memmap on failure Pasha Tatashin
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 20+ messages in thread
From: Pasha Tatashin @ 2024-08-08 15:42 UTC (permalink / raw)
  To: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs,
	pasha.tatashin, rientjes, rppt, souravpanda, vbabka, willy,
	dan.j.williams, yi.zhang, alison.schofield, david, yosryahmed

It is more logical to update the stat before the page is freed, to avoid
use after free scenarios.

Fixes: 15995a352474 ("mm: report per-page metadata information")
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
---
 mm/hugetlb_vmemmap.c | 4 ++--
 mm/page_ext.c        | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index 829112b0a914..fa83a7b38199 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -185,11 +185,11 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end,
 static inline void free_vmemmap_page(struct page *page)
 {
 	if (PageReserved(page)) {
-		free_bootmem_page(page);
 		mod_node_page_state(page_pgdat(page), NR_MEMMAP_BOOT, -1);
+		free_bootmem_page(page);
 	} else {
-		__free_page(page);
 		mod_node_page_state(page_pgdat(page), NR_MEMMAP, -1);
+		__free_page(page);
 	}
 }
 
diff --git a/mm/page_ext.c b/mm/page_ext.c
index c191e490c401..962d45eee1f8 100644
--- a/mm/page_ext.c
+++ b/mm/page_ext.c
@@ -330,18 +330,18 @@ static void free_page_ext(void *addr)
 	if (is_vmalloc_addr(addr)) {
 		page = vmalloc_to_page(addr);
 		pgdat = page_pgdat(page);
+		mod_node_page_state(pgdat, NR_MEMMAP,
+				    -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
 		vfree(addr);
 	} else {
 		page = virt_to_page(addr);
 		pgdat = page_pgdat(page);
+		mod_node_page_state(pgdat, NR_MEMMAP,
+				    -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
 		BUG_ON(PageReserved(page));
 		kmemleak_free(addr);
 		free_pages_exact(addr, table_size);
 	}
-
-	mod_node_page_state(pgdat, NR_MEMMAP,
-			    -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
-
 }
 
 static void __free_page_ext(unsigned long pfn)
-- 
2.46.0.76.ge559c4bf1a-goog



^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v3 2/4] mm: don't account memmap on failure
  2024-08-08 15:42 [PATCH v3 0/4] Fixes for memmap accounting Pasha Tatashin
  2024-08-08 15:42 ` [PATCH v3 1/4] mm: update the memmap stat before page is freed Pasha Tatashin
@ 2024-08-08 15:42 ` Pasha Tatashin
  2024-08-08 17:18   ` Yosry Ahmed
                     ` (2 more replies)
  2024-08-08 15:42 ` [PATCH v3 3/4] mm: add system wide stats items category Pasha Tatashin
  2024-08-08 15:42 ` [PATCH v3 4/4] mm: don't account memmap per-node Pasha Tatashin
  3 siblings, 3 replies; 20+ messages in thread
From: Pasha Tatashin @ 2024-08-08 15:42 UTC (permalink / raw)
  To: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs,
	pasha.tatashin, rientjes, rppt, souravpanda, vbabka, willy,
	dan.j.williams, yi.zhang, alison.schofield, david, yosryahmed

When in alloc_vmemmap_page_list() memmap is failed to allocate, do
not account, the memory is going to be release at the function exit.

Fixes: 15995a352474 ("mm: report per-page metadata information")
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
---
 mm/hugetlb_vmemmap.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index fa83a7b38199..70027869d844 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -392,13 +392,10 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
 
 	for (i = 0; i < nr_pages; i++) {
 		page = alloc_pages_node(nid, gfp_mask, 0);
-		if (!page) {
-			mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, i);
+		if (!page)
 			goto out;
-		}
 		list_add(&page->lru, list);
 	}
-
 	mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, nr_pages);
 
 	return 0;
-- 
2.46.0.76.ge559c4bf1a-goog



^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v3 3/4] mm: add system wide stats items category
  2024-08-08 15:42 [PATCH v3 0/4] Fixes for memmap accounting Pasha Tatashin
  2024-08-08 15:42 ` [PATCH v3 1/4] mm: update the memmap stat before page is freed Pasha Tatashin
  2024-08-08 15:42 ` [PATCH v3 2/4] mm: don't account memmap on failure Pasha Tatashin
@ 2024-08-08 15:42 ` Pasha Tatashin
  2024-08-08 18:20   ` David Hildenbrand
  2024-08-08 15:42 ` [PATCH v3 4/4] mm: don't account memmap per-node Pasha Tatashin
  3 siblings, 1 reply; 20+ messages in thread
From: Pasha Tatashin @ 2024-08-08 15:42 UTC (permalink / raw)
  To: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs,
	pasha.tatashin, rientjes, rppt, souravpanda, vbabka, willy,
	dan.j.williams, yi.zhang, alison.schofield, david, yosryahmed

/proc/vmstat contains events and stats, events can only grow, but stats
can grow and srhink.

vmstat has the following:
-------------------------
NR_VM_ZONE_STAT_ITEMS:	per-zone stats
NR_VM_NUMA_EVENT_ITEMS:	per-numa events
NR_VM_NODE_STAT_ITEMS:	per-numa stats
NR_VM_WRITEBACK_STAT_ITEMS:	system-wide background-writeback and
				dirty-throttling tresholds.
NR_VM_EVENT_ITEMS:	system-wide events
-------------------------

Rename NR_VM_WRITEBACK_STAT_ITEMS to NR_VM_STAT_ITEMS, to track the
system-wide stats, we are going to add per-page metadata stats to this
category in the next patch.

Also delete unused writeback_stat_name() function.

Suggested-by: Yosry Ahmed <yosryahmed@google.com>
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
---
 include/linux/vmstat.h | 15 ++++-----------
 mm/vmstat.c            |  6 +++---
 2 files changed, 7 insertions(+), 14 deletions(-)

diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h
index 23cd17942036..9ab4fa5e09b5 100644
--- a/include/linux/vmstat.h
+++ b/include/linux/vmstat.h
@@ -34,10 +34,11 @@ struct reclaim_stat {
 	unsigned nr_lazyfree_fail;
 };
 
-enum writeback_stat_item {
+/* Stat data for system wide items */
+enum vm_stat_item {
 	NR_DIRTY_THRESHOLD,
 	NR_DIRTY_BG_THRESHOLD,
-	NR_VM_WRITEBACK_STAT_ITEMS,
+	NR_VM_STAT_ITEMS,
 };
 
 #ifdef CONFIG_VM_EVENT_COUNTERS
@@ -514,21 +515,13 @@ static inline const char *lru_list_name(enum lru_list lru)
 	return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
 }
 
-static inline const char *writeback_stat_name(enum writeback_stat_item item)
-{
-	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
-			   NR_VM_NUMA_EVENT_ITEMS +
-			   NR_VM_NODE_STAT_ITEMS +
-			   item];
-}
-
 #if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
 static inline const char *vm_event_name(enum vm_event_item item)
 {
 	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
 			   NR_VM_NUMA_EVENT_ITEMS +
 			   NR_VM_NODE_STAT_ITEMS +
-			   NR_VM_WRITEBACK_STAT_ITEMS +
+			   NR_VM_STAT_ITEMS +
 			   item];
 }
 #endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
diff --git a/mm/vmstat.c b/mm/vmstat.c
index 04a1cb6cc636..6f8aa4766f16 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1257,7 +1257,7 @@ const char * const vmstat_text[] = {
 	"pgdemote_khugepaged",
 	"nr_memmap",
 	"nr_memmap_boot",
-	/* enum writeback_stat_item counters */
+	/* system-wide enum vm_stat_item counters */
 	"nr_dirty_threshold",
 	"nr_dirty_background_threshold",
 
@@ -1790,7 +1790,7 @@ static const struct seq_operations zoneinfo_op = {
 #define NR_VMSTAT_ITEMS (NR_VM_ZONE_STAT_ITEMS + \
 			 NR_VM_NUMA_EVENT_ITEMS + \
 			 NR_VM_NODE_STAT_ITEMS + \
-			 NR_VM_WRITEBACK_STAT_ITEMS + \
+			 NR_VM_STAT_ITEMS + \
 			 (IS_ENABLED(CONFIG_VM_EVENT_COUNTERS) ? \
 			  NR_VM_EVENT_ITEMS : 0))
 
@@ -1827,7 +1827,7 @@ static void *vmstat_start(struct seq_file *m, loff_t *pos)
 
 	global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
 			    v + NR_DIRTY_THRESHOLD);
-	v += NR_VM_WRITEBACK_STAT_ITEMS;
+	v += NR_VM_STAT_ITEMS;
 
 #ifdef CONFIG_VM_EVENT_COUNTERS
 	all_vm_events(v);
-- 
2.46.0.76.ge559c4bf1a-goog



^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v3 4/4] mm: don't account memmap per-node
  2024-08-08 15:42 [PATCH v3 0/4] Fixes for memmap accounting Pasha Tatashin
                   ` (2 preceding siblings ...)
  2024-08-08 15:42 ` [PATCH v3 3/4] mm: add system wide stats items category Pasha Tatashin
@ 2024-08-08 15:42 ` Pasha Tatashin
  2024-08-08 18:26   ` David Hildenbrand
  2024-08-08 18:55   ` Alison Schofield
  3 siblings, 2 replies; 20+ messages in thread
From: Pasha Tatashin @ 2024-08-08 15:42 UTC (permalink / raw)
  To: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs,
	pasha.tatashin, rientjes, rppt, souravpanda, vbabka, willy,
	dan.j.williams, yi.zhang, alison.schofield, david, yosryahmed

Currently, when memory is hot-plugged or hot-removed the accounting is
done based on the assumption that memmap is allocated from the same node
as the hot-plugged/hot-removed memory, which is not always the case.

In addition, there are challenges with keeping the node id of the memory
that is being remove to the time when memmap accounting is actually
performed: since this is done after remove_pfn_range_from_zone(), and
also after remove_memory_block_devices(). Meaning that we cannot use
pgdat nor walking though memblocks to get the nid.

Given all of that, account the memmap overhead system wide instead.

For this we are going to be using global atomic counters, but given that
memmap size is rarely modified, and normally is only modified either
during early boot when there is only one CPU, or under a hotplug global
mutex lock, therefore there is no need for per-cpu optimizations.

Reported-by: Yi Zhang <yi.zhang@redhat.com>
Closes: https://lore.kernel.org/linux-cxl/CAHj4cs9Ax1=CoJkgBGP_+sNu6-6=6v=_L-ZBZY0bVLD3wUWZQg@mail.gmail.com
Reported-by: Alison Schofield <alison.schofield@intel.com>
Closes: https://lore.kernel.org/linux-mm/Zq0tPd2h6alFz8XF@aschofie-mobl2/#t

Fixes: 15995a352474 ("mm: report per-page metadata information")
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
---
 include/linux/mmzone.h |  2 --
 include/linux/vmstat.h |  7 ++++---
 mm/hugetlb_vmemmap.c   |  8 ++++----
 mm/mm_init.c           |  3 +--
 mm/page_alloc.c        |  1 -
 mm/page_ext.c          | 15 +++++---------
 mm/sparse-vmemmap.c    | 11 ++++-------
 mm/sparse.c            |  5 ++---
 mm/vmstat.c            | 45 ++++++++++++++++++++----------------------
 9 files changed, 41 insertions(+), 56 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 41458892bc8a..1dc6248feb83 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -220,8 +220,6 @@ enum node_stat_item {
 	PGDEMOTE_KSWAPD,
 	PGDEMOTE_DIRECT,
 	PGDEMOTE_KHUGEPAGED,
-	NR_MEMMAP, /* page metadata allocated through buddy allocator */
-	NR_MEMMAP_BOOT, /* page metadata allocated through boot allocator */
 	NR_VM_NODE_STAT_ITEMS
 };
 
diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h
index 9ab4fa5e09b5..596c050ed492 100644
--- a/include/linux/vmstat.h
+++ b/include/linux/vmstat.h
@@ -38,6 +38,8 @@ struct reclaim_stat {
 enum vm_stat_item {
 	NR_DIRTY_THRESHOLD,
 	NR_DIRTY_BG_THRESHOLD,
+	NR_MEMMAP,	/* page metadata allocated through buddy allocator */
+	NR_MEMMAP_BOOT,	/* page metadata allocated through boot allocator */
 	NR_VM_STAT_ITEMS,
 };
 
@@ -618,7 +620,6 @@ static inline void lruvec_stat_sub_folio(struct folio *folio,
 	lruvec_stat_mod_folio(folio, idx, -folio_nr_pages(folio));
 }
 
-void __meminit mod_node_early_perpage_metadata(int nid, long delta);
-void __meminit store_early_perpage_metadata(void);
-
+void mod_memmap_boot(long delta);
+void mod_memmap(long delta);
 #endif /* _LINUX_VMSTAT_H */
diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index 70027869d844..60a5faa1f341 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -185,10 +185,10 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end,
 static inline void free_vmemmap_page(struct page *page)
 {
 	if (PageReserved(page)) {
-		mod_node_page_state(page_pgdat(page), NR_MEMMAP_BOOT, -1);
+		mod_memmap_boot(-1);
 		free_bootmem_page(page);
 	} else {
-		mod_node_page_state(page_pgdat(page), NR_MEMMAP, -1);
+		mod_memmap(-1);
 		__free_page(page);
 	}
 }
@@ -341,7 +341,7 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end,
 		copy_page(page_to_virt(walk.reuse_page),
 			  (void *)walk.reuse_addr);
 		list_add(&walk.reuse_page->lru, vmemmap_pages);
-		mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, 1);
+		mod_memmap(1);
 	}
 
 	/*
@@ -396,7 +396,7 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
 			goto out;
 		list_add(&page->lru, list);
 	}
-	mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, nr_pages);
+	mod_memmap(nr_pages);
 
 	return 0;
 out:
diff --git a/mm/mm_init.c b/mm/mm_init.c
index 75c3bd42799b..29677274e74e 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -1623,8 +1623,7 @@ 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));
+	mod_memmap_boot(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,
 		 (unsigned long)pgdat->node_mem_map);
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 28f80daf5c04..875d76e8684a 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -5755,7 +5755,6 @@ 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)
diff --git a/mm/page_ext.c b/mm/page_ext.c
index 962d45eee1f8..a4b485b39330 100644
--- a/mm/page_ext.c
+++ b/mm/page_ext.c
@@ -214,8 +214,7 @@ 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_MEMMAP_BOOT,
-			    DIV_ROUND_UP(table_size, PAGE_SIZE));
+	mod_memmap_boot(DIV_ROUND_UP(table_size, PAGE_SIZE));
 	return 0;
 }
 
@@ -275,10 +274,8 @@ static void *__meminit alloc_page_ext(size_t size, int nid)
 	else
 		addr = vzalloc_node(size, nid);
 
-	if (addr) {
-		mod_node_page_state(NODE_DATA(nid), NR_MEMMAP,
-				    DIV_ROUND_UP(size, PAGE_SIZE));
-	}
+	if (addr)
+		mod_memmap(DIV_ROUND_UP(size, PAGE_SIZE));
 
 	return addr;
 }
@@ -330,14 +327,12 @@ static void free_page_ext(void *addr)
 	if (is_vmalloc_addr(addr)) {
 		page = vmalloc_to_page(addr);
 		pgdat = page_pgdat(page);
-		mod_node_page_state(pgdat, NR_MEMMAP,
-				    -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
+		mod_memmap(-1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
 		vfree(addr);
 	} else {
 		page = virt_to_page(addr);
 		pgdat = page_pgdat(page);
-		mod_node_page_state(pgdat, NR_MEMMAP,
-				    -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
+		mod_memmap(-1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
 		BUG_ON(PageReserved(page));
 		kmemleak_free(addr);
 		free_pages_exact(addr, table_size);
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index 1dda6c53370b..307f6fe83598 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -469,13 +469,10 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,
 	if (r < 0)
 		return NULL;
 
-	if (system_state == SYSTEM_BOOTING) {
-		mod_node_early_perpage_metadata(nid, DIV_ROUND_UP(end - start,
-								  PAGE_SIZE));
-	} else {
-		mod_node_page_state(NODE_DATA(nid), NR_MEMMAP,
-				    DIV_ROUND_UP(end - start, PAGE_SIZE));
-	}
+	if (system_state == SYSTEM_BOOTING)
+		mod_memmap_boot(DIV_ROUND_UP(end - start, PAGE_SIZE));
+	else
+		mod_memmap(DIV_ROUND_UP(end - start, PAGE_SIZE));
 
 	return pfn_to_page(pfn);
 }
diff --git a/mm/sparse.c b/mm/sparse.c
index e4b830091d13..9cc80ba1f7c1 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -463,7 +463,7 @@ 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));
+	mod_memmap_boot(DIV_ROUND_UP(size, PAGE_SIZE));
 #endif
 }
 
@@ -643,8 +643,7 @@ 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_MEMMAP,
-			    -1L * (DIV_ROUND_UP(end - start, PAGE_SIZE)));
+	mod_memmap(-1L * (DIV_ROUND_UP(end - start, PAGE_SIZE)));
 	vmemmap_free(start, end, altmap);
 }
 static void free_map_bootmem(struct page *memmap)
diff --git a/mm/vmstat.c b/mm/vmstat.c
index 6f8aa4766f16..896f9a2ace8a 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1033,6 +1033,23 @@ unsigned long node_page_state(struct pglist_data *pgdat,
 }
 #endif
 
+/*
+ * Count number of pages "struct page" and "struct page_ext" consume.
+ * nr_memmap_boot: # of pages allocated by boot allocator & not part of MemTotal
+ * nr_memmap: # of pages that were allocated by buddy allocator
+ */
+static atomic_long_t nr_memmap_boot, nr_memmap;
+
+void mod_memmap_boot(long delta)
+{
+	atomic_long_add(delta, &nr_memmap_boot);
+}
+
+void mod_memmap(long delta)
+{
+	atomic_long_add(delta, &nr_memmap);
+}
+
 #ifdef CONFIG_COMPACTION
 
 struct contig_page_info {
@@ -1255,11 +1272,11 @@ const char * const vmstat_text[] = {
 	"pgdemote_kswapd",
 	"pgdemote_direct",
 	"pgdemote_khugepaged",
-	"nr_memmap",
-	"nr_memmap_boot",
 	/* system-wide enum vm_stat_item counters */
 	"nr_dirty_threshold",
 	"nr_dirty_background_threshold",
+	"nr_memmap",
+	"nr_memmap_boot",
 
 #if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
 	/* enum vm_event_item counters */
@@ -1827,6 +1844,8 @@ static void *vmstat_start(struct seq_file *m, loff_t *pos)
 
 	global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
 			    v + NR_DIRTY_THRESHOLD);
+	v[NR_MEMMAP_BOOT] = atomic_long_read(&nr_memmap_boot);
+	v[NR_MEMMAP] = atomic_long_read(&nr_memmap);
 	v += NR_VM_STAT_ITEMS;
 
 #ifdef CONFIG_VM_EVENT_COUNTERS
@@ -2285,25 +2304,3 @@ 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] __meminitdata;
-
-void __meminit mod_node_early_perpage_metadata(int nid, long delta)
-{
-	early_perpage_metadata[nid] += delta;
-}
-
-void __meminit 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_MEMMAP_BOOT,
-				    early_perpage_metadata[nid]);
-	}
-}
-- 
2.46.0.76.ge559c4bf1a-goog



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 1/4] mm: update the memmap stat before page is freed
  2024-08-08 15:42 ` [PATCH v3 1/4] mm: update the memmap stat before page is freed Pasha Tatashin
@ 2024-08-08 17:17   ` Yosry Ahmed
  2024-08-08 17:20     ` Yosry Ahmed
  2024-08-08 17:17   ` Fan Ni
  1 sibling, 1 reply; 20+ messages in thread
From: Yosry Ahmed @ 2024-08-08 17:17 UTC (permalink / raw)
  To: Pasha Tatashin
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	alison.schofield, david

On Thu, Aug 8, 2024 at 8:42 AM Pasha Tatashin <pasha.tatashin@soleen.com> wrote:
>
> It is more logical to update the stat before the page is freed, to avoid
> use after free scenarios.
>
> Fixes: 15995a352474 ("mm: report per-page metadata information")
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> Reviewed-by: David Hildenbrand <david@redhat.com>

Reviewed-by: Yosry Ahmed <yosryahmed@google.com>

> ---
>  mm/hugetlb_vmemmap.c | 4 ++--
>  mm/page_ext.c        | 8 ++++----
>  2 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> index 829112b0a914..fa83a7b38199 100644
> --- a/mm/hugetlb_vmemmap.c
> +++ b/mm/hugetlb_vmemmap.c
> @@ -185,11 +185,11 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end,
>  static inline void free_vmemmap_page(struct page *page)
>  {
>         if (PageReserved(page)) {
> -               free_bootmem_page(page);
>                 mod_node_page_state(page_pgdat(page), NR_MEMMAP_BOOT, -1);
> +               free_bootmem_page(page);
>         } else {
> -               __free_page(page);
>                 mod_node_page_state(page_pgdat(page), NR_MEMMAP, -1);
> +               __free_page(page);
>         }
>  }
>
> diff --git a/mm/page_ext.c b/mm/page_ext.c
> index c191e490c401..962d45eee1f8 100644
> --- a/mm/page_ext.c
> +++ b/mm/page_ext.c
> @@ -330,18 +330,18 @@ static void free_page_ext(void *addr)
>         if (is_vmalloc_addr(addr)) {
>                 page = vmalloc_to_page(addr);
>                 pgdat = page_pgdat(page);
> +               mod_node_page_state(pgdat, NR_MEMMAP,
> +                                   -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
>                 vfree(addr);
>         } else {
>                 page = virt_to_page(addr);
>                 pgdat = page_pgdat(page);
> +               mod_node_page_state(pgdat, NR_MEMMAP,
> +                                   -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
>                 BUG_ON(PageReserved(page));
>                 kmemleak_free(addr);
>                 free_pages_exact(addr, table_size);
>         }
> -
> -       mod_node_page_state(pgdat, NR_MEMMAP,
> -                           -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
> -
>  }
>
>  static void __free_page_ext(unsigned long pfn)
> --
> 2.46.0.76.ge559c4bf1a-goog
>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 1/4] mm: update the memmap stat before page is freed
  2024-08-08 15:42 ` [PATCH v3 1/4] mm: update the memmap stat before page is freed Pasha Tatashin
  2024-08-08 17:17   ` Yosry Ahmed
@ 2024-08-08 17:17   ` Fan Ni
  1 sibling, 0 replies; 20+ messages in thread
From: Fan Ni @ 2024-08-08 17:17 UTC (permalink / raw)
  To: Pasha Tatashin
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	alison.schofield, david, yosryahmed

On Thu, Aug 08, 2024 at 03:42:34PM +0000, Pasha Tatashin wrote:
> It is more logical to update the stat before the page is freed, to avoid
> use after free scenarios.
> 
> Fixes: 15995a352474 ("mm: report per-page metadata information")
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> Reviewed-by: David Hildenbrand <david@redhat.com>

Reviewed-by: Fan Ni <fan.ni@samsung.com>

> ---
>  mm/hugetlb_vmemmap.c | 4 ++--
>  mm/page_ext.c        | 8 ++++----
>  2 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> index 829112b0a914..fa83a7b38199 100644
> --- a/mm/hugetlb_vmemmap.c
> +++ b/mm/hugetlb_vmemmap.c
> @@ -185,11 +185,11 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end,
>  static inline void free_vmemmap_page(struct page *page)
>  {
>  	if (PageReserved(page)) {
> -		free_bootmem_page(page);
>  		mod_node_page_state(page_pgdat(page), NR_MEMMAP_BOOT, -1);
> +		free_bootmem_page(page);
>  	} else {
> -		__free_page(page);
>  		mod_node_page_state(page_pgdat(page), NR_MEMMAP, -1);
> +		__free_page(page);
>  	}
>  }
>  
> diff --git a/mm/page_ext.c b/mm/page_ext.c
> index c191e490c401..962d45eee1f8 100644
> --- a/mm/page_ext.c
> +++ b/mm/page_ext.c
> @@ -330,18 +330,18 @@ static void free_page_ext(void *addr)
>  	if (is_vmalloc_addr(addr)) {
>  		page = vmalloc_to_page(addr);
>  		pgdat = page_pgdat(page);
> +		mod_node_page_state(pgdat, NR_MEMMAP,
> +				    -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
>  		vfree(addr);
>  	} else {
>  		page = virt_to_page(addr);
>  		pgdat = page_pgdat(page);
> +		mod_node_page_state(pgdat, NR_MEMMAP,
> +				    -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
>  		BUG_ON(PageReserved(page));
>  		kmemleak_free(addr);
>  		free_pages_exact(addr, table_size);
>  	}
> -
> -	mod_node_page_state(pgdat, NR_MEMMAP,
> -			    -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
> -
>  }
>  
>  static void __free_page_ext(unsigned long pfn)
> -- 
> 2.46.0.76.ge559c4bf1a-goog
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 2/4] mm: don't account memmap on failure
  2024-08-08 15:42 ` [PATCH v3 2/4] mm: don't account memmap on failure Pasha Tatashin
@ 2024-08-08 17:18   ` Yosry Ahmed
  2024-08-08 17:19   ` Fan Ni
  2024-08-08 18:12   ` David Hildenbrand
  2 siblings, 0 replies; 20+ messages in thread
From: Yosry Ahmed @ 2024-08-08 17:18 UTC (permalink / raw)
  To: Pasha Tatashin
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	alison.schofield, david

On Thu, Aug 8, 2024 at 8:42 AM Pasha Tatashin <pasha.tatashin@soleen.com> wrote:
>
> When in alloc_vmemmap_page_list() memmap is failed to allocate, do
> not account, the memory is going to be release at the function exit.
>
> Fixes: 15995a352474 ("mm: report per-page metadata information")
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>

Reviewed-by: Yosry Ahmed <yosryahmed@google.com>

> ---
>  mm/hugetlb_vmemmap.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)
>
> diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> index fa83a7b38199..70027869d844 100644
> --- a/mm/hugetlb_vmemmap.c
> +++ b/mm/hugetlb_vmemmap.c
> @@ -392,13 +392,10 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
>
>         for (i = 0; i < nr_pages; i++) {
>                 page = alloc_pages_node(nid, gfp_mask, 0);
> -               if (!page) {
> -                       mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, i);
> +               if (!page)
>                         goto out;
> -               }
>                 list_add(&page->lru, list);
>         }
> -
>         mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, nr_pages);
>
>         return 0;
> --
> 2.46.0.76.ge559c4bf1a-goog
>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 2/4] mm: don't account memmap on failure
  2024-08-08 15:42 ` [PATCH v3 2/4] mm: don't account memmap on failure Pasha Tatashin
  2024-08-08 17:18   ` Yosry Ahmed
@ 2024-08-08 17:19   ` Fan Ni
  2024-08-08 18:12   ` David Hildenbrand
  2 siblings, 0 replies; 20+ messages in thread
From: Fan Ni @ 2024-08-08 17:19 UTC (permalink / raw)
  To: Pasha Tatashin
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	alison.schofield, david, yosryahmed

On Thu, Aug 08, 2024 at 03:42:35PM +0000, Pasha Tatashin wrote:
> When in alloc_vmemmap_page_list() memmap is failed to allocate, do
> not account, the memory is going to be release at the function exit.
> 
> Fixes: 15995a352474 ("mm: report per-page metadata information")
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> ---

Reviewed-by: Fan Ni <fan.ni@samsung.com>

>  mm/hugetlb_vmemmap.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)
> 
> diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> index fa83a7b38199..70027869d844 100644
> --- a/mm/hugetlb_vmemmap.c
> +++ b/mm/hugetlb_vmemmap.c
> @@ -392,13 +392,10 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
>  
>  	for (i = 0; i < nr_pages; i++) {
>  		page = alloc_pages_node(nid, gfp_mask, 0);
> -		if (!page) {
> -			mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, i);
> +		if (!page)
>  			goto out;
> -		}
>  		list_add(&page->lru, list);
>  	}
> -
>  	mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, nr_pages);
>  
>  	return 0;
> -- 
> 2.46.0.76.ge559c4bf1a-goog
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 1/4] mm: update the memmap stat before page is freed
  2024-08-08 17:17   ` Yosry Ahmed
@ 2024-08-08 17:20     ` Yosry Ahmed
  2024-08-08 17:37       ` Pasha Tatashin
  0 siblings, 1 reply; 20+ messages in thread
From: Yosry Ahmed @ 2024-08-08 17:20 UTC (permalink / raw)
  To: Pasha Tatashin
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	alison.schofield, david

On Thu, Aug 8, 2024 at 10:17 AM Yosry Ahmed <yosryahmed@google.com> wrote:
>
> On Thu, Aug 8, 2024 at 8:42 AM Pasha Tatashin <pasha.tatashin@soleen.com> wrote:
> >
> > It is more logical to update the stat before the page is freed, to avoid
> > use after free scenarios.
> >
> > Fixes: 15995a352474 ("mm: report per-page metadata information")
> > Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> > Reviewed-by: David Hildenbrand <david@redhat.com>
>
> Reviewed-by: Yosry Ahmed <yosryahmed@google.com>

Actually although I think this patch is correct, it shouldn't be
needed after patch 4 because we no longer use the page or pgdat to
update the stats.

>
> > ---
> >  mm/hugetlb_vmemmap.c | 4 ++--
> >  mm/page_ext.c        | 8 ++++----
> >  2 files changed, 6 insertions(+), 6 deletions(-)
> >
> > diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> > index 829112b0a914..fa83a7b38199 100644
> > --- a/mm/hugetlb_vmemmap.c
> > +++ b/mm/hugetlb_vmemmap.c
> > @@ -185,11 +185,11 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end,
> >  static inline void free_vmemmap_page(struct page *page)
> >  {
> >         if (PageReserved(page)) {
> > -               free_bootmem_page(page);
> >                 mod_node_page_state(page_pgdat(page), NR_MEMMAP_BOOT, -1);
> > +               free_bootmem_page(page);
> >         } else {
> > -               __free_page(page);
> >                 mod_node_page_state(page_pgdat(page), NR_MEMMAP, -1);
> > +               __free_page(page);
> >         }
> >  }
> >
> > diff --git a/mm/page_ext.c b/mm/page_ext.c
> > index c191e490c401..962d45eee1f8 100644
> > --- a/mm/page_ext.c
> > +++ b/mm/page_ext.c
> > @@ -330,18 +330,18 @@ static void free_page_ext(void *addr)
> >         if (is_vmalloc_addr(addr)) {
> >                 page = vmalloc_to_page(addr);
> >                 pgdat = page_pgdat(page);
> > +               mod_node_page_state(pgdat, NR_MEMMAP,
> > +                                   -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
> >                 vfree(addr);
> >         } else {
> >                 page = virt_to_page(addr);
> >                 pgdat = page_pgdat(page);
> > +               mod_node_page_state(pgdat, NR_MEMMAP,
> > +                                   -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
> >                 BUG_ON(PageReserved(page));
> >                 kmemleak_free(addr);
> >                 free_pages_exact(addr, table_size);
> >         }
> > -
> > -       mod_node_page_state(pgdat, NR_MEMMAP,
> > -                           -1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
> > -
> >  }
> >
> >  static void __free_page_ext(unsigned long pfn)
> > --
> > 2.46.0.76.ge559c4bf1a-goog
> >


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 1/4] mm: update the memmap stat before page is freed
  2024-08-08 17:20     ` Yosry Ahmed
@ 2024-08-08 17:37       ` Pasha Tatashin
  0 siblings, 0 replies; 20+ messages in thread
From: Pasha Tatashin @ 2024-08-08 17:37 UTC (permalink / raw)
  To: Yosry Ahmed
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	alison.schofield, david

On Thu, Aug 8, 2024 at 1:21 PM Yosry Ahmed <yosryahmed@google.com> wrote:
>
> On Thu, Aug 8, 2024 at 10:17 AM Yosry Ahmed <yosryahmed@google.com> wrote:
> >
> > On Thu, Aug 8, 2024 at 8:42 AM Pasha Tatashin <pasha.tatashin@soleen.com> wrote:
> > >
> > > It is more logical to update the stat before the page is freed, to avoid
> > > use after free scenarios.
> > >
> > > Fixes: 15995a352474 ("mm: report per-page metadata information")
> > > Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> > > Reviewed-by: David Hildenbrand <david@redhat.com>
> >
> > Reviewed-by: Yosry Ahmed <yosryahmed@google.com>
>
> Actually although I think this patch is correct, it shouldn't be
> needed after patch 4 because we no longer use the page or pgdat to
> update the stats.

Good point, also there is a warning that pgdat is not used anymore.

I will remove this patch from the series, and clean-up free_page_ext()
by removing pgdat, once patches 3/4 and 4/4 are reviewed.

Pasha

Pasha


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 2/4] mm: don't account memmap on failure
  2024-08-08 15:42 ` [PATCH v3 2/4] mm: don't account memmap on failure Pasha Tatashin
  2024-08-08 17:18   ` Yosry Ahmed
  2024-08-08 17:19   ` Fan Ni
@ 2024-08-08 18:12   ` David Hildenbrand
  2024-08-08 18:54     ` Pasha Tatashin
  2 siblings, 1 reply; 20+ messages in thread
From: David Hildenbrand @ 2024-08-08 18:12 UTC (permalink / raw)
  To: Pasha Tatashin, akpm, linux-kernel, linux-mm, linux-cxl,
	cerasuolodomenico, hannes, j.granados, lizhijian, muchun.song,
	nphamcs, rientjes, rppt, souravpanda, vbabka, willy,
	dan.j.williams, yi.zhang, alison.schofield, yosryahmed

On 08.08.24 17:42, Pasha Tatashin wrote:
> When in alloc_vmemmap_page_list() memmap is failed to allocate, do
> not account, the memory is going to be release at the function exit.

I would write it as

"When we fail to allocate the mmemmap in alloc_vmemmap_page_list(), do 
not account any already-allocated pages: we're going to free all them 
before we return from the function."

Acked-by: David Hildenbrand <david@redhat.com>

> 
> Fixes: 15995a352474 ("mm: report per-page metadata information")
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> ---
>   mm/hugetlb_vmemmap.c | 5 +----
>   1 file changed, 1 insertion(+), 4 deletions(-)
> 
> diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> index fa83a7b38199..70027869d844 100644
> --- a/mm/hugetlb_vmemmap.c
> +++ b/mm/hugetlb_vmemmap.c
> @@ -392,13 +392,10 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
>   
>   	for (i = 0; i < nr_pages; i++) {
>   		page = alloc_pages_node(nid, gfp_mask, 0);
> -		if (!page) {
> -			mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, i);
> +		if (!page)
>   			goto out;
> -		}
>   		list_add(&page->lru, list);
>   	}
> -
>   	mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, nr_pages);
>   
>   	return 0;

-- 
Cheers,

David / dhildenb



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 3/4] mm: add system wide stats items category
  2024-08-08 15:42 ` [PATCH v3 3/4] mm: add system wide stats items category Pasha Tatashin
@ 2024-08-08 18:20   ` David Hildenbrand
  2024-08-08 19:01     ` Pasha Tatashin
  0 siblings, 1 reply; 20+ messages in thread
From: David Hildenbrand @ 2024-08-08 18:20 UTC (permalink / raw)
  To: Pasha Tatashin, akpm, linux-kernel, linux-mm, linux-cxl,
	cerasuolodomenico, hannes, j.granados, lizhijian, muchun.song,
	nphamcs, rientjes, rppt, souravpanda, vbabka, willy,
	dan.j.williams, yi.zhang, alison.schofield, yosryahmed

On 08.08.24 17:42, Pasha Tatashin wrote:
> /proc/vmstat contains events and stats, events can only grow, but stats
> can grow and srhink.

s/shrink/

I think we discussed exposing this in /proc/meminfo. There, it would be 
much easier to simply have a global variable, print it, and be done with 
it. Like we do with TotalCma.

For /proc/vmstat that's likely the right approach what you have here.

> 
> vmstat has the following:
> -------------------------
> NR_VM_ZONE_STAT_ITEMS:	per-zone stats
> NR_VM_NUMA_EVENT_ITEMS:	per-numa events
> NR_VM_NODE_STAT_ITEMS:	per-numa stats
> NR_VM_WRITEBACK_STAT_ITEMS:	system-wide background-writeback and
> 				dirty-throttling tresholds.
> NR_VM_EVENT_ITEMS:	system-wide events
> -------------------------
> 
> Rename NR_VM_WRITEBACK_STAT_ITEMS to NR_VM_STAT_ITEMS, to track the
> system-wide stats, we are going to add per-page metadata stats to this
> category in the next patch.
> 
> Also delete unused writeback_stat_name() function.
> 
> Suggested-by: Yosry Ahmed <yosryahmed@google.com>
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> ---
>   include/linux/vmstat.h | 15 ++++-----------
>   mm/vmstat.c            |  6 +++---
>   2 files changed, 7 insertions(+), 14 deletions(-)
> 
> diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h
> index 23cd17942036..9ab4fa5e09b5 100644
> --- a/include/linux/vmstat.h
> +++ b/include/linux/vmstat.h
> @@ -34,10 +34,11 @@ struct reclaim_stat {
>   	unsigned nr_lazyfree_fail;
>   };
>   
> -enum writeback_stat_item {
> +/* Stat data for system wide items */
> +enum vm_stat_item {
>   	NR_DIRTY_THRESHOLD,
>   	NR_DIRTY_BG_THRESHOLD,
> -	NR_VM_WRITEBACK_STAT_ITEMS,
> +	NR_VM_STAT_ITEMS,
>   };
>   
>   #ifdef CONFIG_VM_EVENT_COUNTERS
> @@ -514,21 +515,13 @@ static inline const char *lru_list_name(enum lru_list lru)
>   	return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
>   }
>   
> -static inline const char *writeback_stat_name(enum writeback_stat_item item)
> -{
> -	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
> -			   NR_VM_NUMA_EVENT_ITEMS +
> -			   NR_VM_NODE_STAT_ITEMS +
> -			   item];
> -}
> -
>   #if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
>   static inline const char *vm_event_name(enum vm_event_item item)
>   {
>   	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
>   			   NR_VM_NUMA_EVENT_ITEMS +
>   			   NR_VM_NODE_STAT_ITEMS +
> -			   NR_VM_WRITEBACK_STAT_ITEMS +
> +			   NR_VM_STAT_ITEMS +
>   			   item];
>   }
>   #endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
> diff --git a/mm/vmstat.c b/mm/vmstat.c
> index 04a1cb6cc636..6f8aa4766f16 100644
> --- a/mm/vmstat.c
> +++ b/mm/vmstat.c
> @@ -1257,7 +1257,7 @@ const char * const vmstat_text[] = {
>   	"pgdemote_khugepaged",
>   	"nr_memmap",
>   	"nr_memmap_boot",
> -	/* enum writeback_stat_item counters */
> +	/* system-wide enum vm_stat_item counters */
>   	"nr_dirty_threshold",
>   	"nr_dirty_background_threshold",
>   
> @@ -1790,7 +1790,7 @@ static const struct seq_operations zoneinfo_op = {
>   #define NR_VMSTAT_ITEMS (NR_VM_ZONE_STAT_ITEMS + \
>   			 NR_VM_NUMA_EVENT_ITEMS + \
>   			 NR_VM_NODE_STAT_ITEMS + \
> -			 NR_VM_WRITEBACK_STAT_ITEMS + \
> +			 NR_VM_STAT_ITEMS + \
>   			 (IS_ENABLED(CONFIG_VM_EVENT_COUNTERS) ? \
>   			  NR_VM_EVENT_ITEMS : 0))
>   
> @@ -1827,7 +1827,7 @@ static void *vmstat_start(struct seq_file *m, loff_t *pos)
>   
>   	global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
>   			    v + NR_DIRTY_THRESHOLD);
> -	v += NR_VM_WRITEBACK_STAT_ITEMS;
> +	v += NR_VM_STAT_ITEMS;
>   
>   #ifdef CONFIG_VM_EVENT_COUNTERS
>   	all_vm_events(v);

-- 
Cheers,

David / dhildenb



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 4/4] mm: don't account memmap per-node
  2024-08-08 15:42 ` [PATCH v3 4/4] mm: don't account memmap per-node Pasha Tatashin
@ 2024-08-08 18:26   ` David Hildenbrand
  2024-08-08 19:03     ` Pasha Tatashin
  2024-08-08 18:55   ` Alison Schofield
  1 sibling, 1 reply; 20+ messages in thread
From: David Hildenbrand @ 2024-08-08 18:26 UTC (permalink / raw)
  To: Pasha Tatashin, akpm, linux-kernel, linux-mm, linux-cxl,
	cerasuolodomenico, hannes, j.granados, lizhijian, muchun.song,
	nphamcs, rientjes, rppt, souravpanda, vbabka, willy,
	dan.j.williams, yi.zhang, alison.schofield, yosryahmed

>   #ifdef CONFIG_COMPACTION
>   
>   struct contig_page_info {
> @@ -1255,11 +1272,11 @@ const char * const vmstat_text[] = {
>   	"pgdemote_kswapd",
>   	"pgdemote_direct",
>   	"pgdemote_khugepaged",
> -	"nr_memmap",
> -	"nr_memmap_boot",
>   	/* system-wide enum vm_stat_item counters */
>   	"nr_dirty_threshold",
>   	"nr_dirty_background_threshold",
> +	"nr_memmap",
> +	"nr_memmap_boot",

While we're at it (sorry, I should have found more time revieweing this 
earlier!) ...

What is the unit here? Should this be "nr_memmap_pages" / 
"nr_memmap_boot_pages"? Like

$ cat /proc/vmstat  | grep pages
nr_free_pages 3618323
nr_zspages 3
nr_anon_pages 1053316
nr_file_pages 3199210
nr_shmem_hugepages 546
nr_file_hugepages 0
nr_anon_transparent_hugepages 0
nr_page_table_pages 15215
nr_sec_page_table_pages 0
numa_pages_migrated 0


Nothing else jumped at me.

-- 
Cheers,

David / dhildenb



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 2/4] mm: don't account memmap on failure
  2024-08-08 18:12   ` David Hildenbrand
@ 2024-08-08 18:54     ` Pasha Tatashin
  0 siblings, 0 replies; 20+ messages in thread
From: Pasha Tatashin @ 2024-08-08 18:54 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	alison.schofield, yosryahmed

On Thu, Aug 8, 2024 at 2:12 PM David Hildenbrand <david@redhat.com> wrote:
>
> On 08.08.24 17:42, Pasha Tatashin wrote:
> > When in alloc_vmemmap_page_list() memmap is failed to allocate, do
> > not account, the memory is going to be release at the function exit.
>
> I would write it as
>
> "When we fail to allocate the mmemmap in alloc_vmemmap_page_list(), do
> not account any already-allocated pages: we're going to free all them
> before we return from the function."
>

Will change.

> Acked-by: David Hildenbrand <david@redhat.com>

Thank you,
Pasha

>
> >
> > Fixes: 15995a352474 ("mm: report per-page metadata information")
> > Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> > ---
> >   mm/hugetlb_vmemmap.c | 5 +----
> >   1 file changed, 1 insertion(+), 4 deletions(-)
> >
> > diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> > index fa83a7b38199..70027869d844 100644
> > --- a/mm/hugetlb_vmemmap.c
> > +++ b/mm/hugetlb_vmemmap.c
> > @@ -392,13 +392,10 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
> >
> >       for (i = 0; i < nr_pages; i++) {
> >               page = alloc_pages_node(nid, gfp_mask, 0);
> > -             if (!page) {
> > -                     mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, i);
> > +             if (!page)
> >                       goto out;
> > -             }
> >               list_add(&page->lru, list);
> >       }
> > -
> >       mod_node_page_state(NODE_DATA(nid), NR_MEMMAP, nr_pages);
> >
> >       return 0;
>
> --
> Cheers,
>
> David / dhildenb
>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 4/4] mm: don't account memmap per-node
  2024-08-08 15:42 ` [PATCH v3 4/4] mm: don't account memmap per-node Pasha Tatashin
  2024-08-08 18:26   ` David Hildenbrand
@ 2024-08-08 18:55   ` Alison Schofield
  2024-08-08 19:05     ` Pasha Tatashin
  1 sibling, 1 reply; 20+ messages in thread
From: Alison Schofield @ 2024-08-08 18:55 UTC (permalink / raw)
  To: Pasha Tatashin
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	david, yosryahmed

On Thu, Aug 08, 2024 at 03:42:37PM +0000, Pasha Tatashin wrote:
> Currently, when memory is hot-plugged or hot-removed the accounting is
> done based on the assumption that memmap is allocated from the same node
> as the hot-plugged/hot-removed memory, which is not always the case.
> 
> In addition, there are challenges with keeping the node id of the memory
> that is being remove to the time when memmap accounting is actually
> performed: since this is done after remove_pfn_range_from_zone(), and
> also after remove_memory_block_devices(). Meaning that we cannot use
> pgdat nor walking though memblocks to get the nid.
> 

How about directly include the failing cases and user visible impacts as
reported in the Tags appended below.

--Alison

> Given all of that, account the memmap overhead system wide instead.
> 
> For this we are going to be using global atomic counters, but given that
> memmap size is rarely modified, and normally is only modified either
> during early boot when there is only one CPU, or under a hotplug global
> mutex lock, therefore there is no need for per-cpu optimizations.
> 
> Reported-by: Yi Zhang <yi.zhang@redhat.com>
> Closes: https://lore.kernel.org/linux-cxl/CAHj4cs9Ax1=CoJkgBGP_+sNu6-6=6v=_L-ZBZY0bVLD3wUWZQg@mail.gmail.com
> Reported-by: Alison Schofield <alison.schofield@intel.com>
> Closes: https://lore.kernel.org/linux-mm/Zq0tPd2h6alFz8XF@aschofie-mobl2/#t
> 
> Fixes: 15995a352474 ("mm: report per-page metadata information")
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> ---

snip

>



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 3/4] mm: add system wide stats items category
  2024-08-08 18:20   ` David Hildenbrand
@ 2024-08-08 19:01     ` Pasha Tatashin
  0 siblings, 0 replies; 20+ messages in thread
From: Pasha Tatashin @ 2024-08-08 19:01 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	alison.schofield, yosryahmed

On Thu, Aug 8, 2024 at 2:20 PM David Hildenbrand <david@redhat.com> wrote:
>
> On 08.08.24 17:42, Pasha Tatashin wrote:
> > /proc/vmstat contains events and stats, events can only grow, but stats
> > can grow and srhink.
>
> s/shrink/

Thanks, fixed.

>
> I think we discussed exposing this in /proc/meminfo. There, it would be
> much easier to simply have a global variable, print it, and be done with
> it. Like we do with TotalCma.

Originally, Sourav had proposed adding per-page metadata stat to
/proc/meminfo, but it was decided against, because all the other stats
in /proc/meminfo are part of MemTotal, but memmap allocations are not
always part of MemTotal which makes things confusing.

This is why it makes sense to keep nr_memmap only in /proc/vmstat.


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 4/4] mm: don't account memmap per-node
  2024-08-08 18:26   ` David Hildenbrand
@ 2024-08-08 19:03     ` Pasha Tatashin
  0 siblings, 0 replies; 20+ messages in thread
From: Pasha Tatashin @ 2024-08-08 19:03 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	alison.schofield, yosryahmed

On Thu, Aug 8, 2024 at 2:26 PM David Hildenbrand <david@redhat.com> wrote:
>
> >   #ifdef CONFIG_COMPACTION
> >
> >   struct contig_page_info {
> > @@ -1255,11 +1272,11 @@ const char * const vmstat_text[] = {
> >       "pgdemote_kswapd",
> >       "pgdemote_direct",
> >       "pgdemote_khugepaged",
> > -     "nr_memmap",
> > -     "nr_memmap_boot",
> >       /* system-wide enum vm_stat_item counters */
> >       "nr_dirty_threshold",
> >       "nr_dirty_background_threshold",
> > +     "nr_memmap",
> > +     "nr_memmap_boot",
>
> While we're at it (sorry, I should have found more time revieweing this
> earlier!) ...
>
> What is the unit here? Should this be "nr_memmap_pages" /
> "nr_memmap_boot_pages"? Like
>
> $ cat /proc/vmstat  | grep pages
> nr_free_pages 3618323
> nr_zspages 3
> nr_anon_pages 1053316
> nr_file_pages 3199210
> nr_shmem_hugepages 546
> nr_file_hugepages 0
> nr_anon_transparent_hugepages 0
> nr_page_table_pages 15215
> nr_sec_page_table_pages 0
> numa_pages_migrated 0
>
>
> Nothing else jumped at me.

Good idea, I will add _pages


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 4/4] mm: don't account memmap per-node
  2024-08-08 18:55   ` Alison Schofield
@ 2024-08-08 19:05     ` Pasha Tatashin
  2024-08-08 21:12       ` Alison Schofield
  0 siblings, 1 reply; 20+ messages in thread
From: Pasha Tatashin @ 2024-08-08 19:05 UTC (permalink / raw)
  To: Alison Schofield
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	david, yosryahmed

On Thu, Aug 8, 2024 at 2:55 PM Alison Schofield
<alison.schofield@intel.com> wrote:
>
> On Thu, Aug 08, 2024 at 03:42:37PM +0000, Pasha Tatashin wrote:
> > Currently, when memory is hot-plugged or hot-removed the accounting is
> > done based on the assumption that memmap is allocated from the same node
> > as the hot-plugged/hot-removed memory, which is not always the case.
> >
> > In addition, there are challenges with keeping the node id of the memory
> > that is being remove to the time when memmap accounting is actually
> > performed: since this is done after remove_pfn_range_from_zone(), and
> > also after remove_memory_block_devices(). Meaning that we cannot use
> > pgdat nor walking though memblocks to get the nid.
> >
>
> How about directly include the failing cases and user visible impacts as
> reported in the Tags appended below.

Do you mean adding panic backtraces or repro steps to the commit log?


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 4/4] mm: don't account memmap per-node
  2024-08-08 19:05     ` Pasha Tatashin
@ 2024-08-08 21:12       ` Alison Schofield
  0 siblings, 0 replies; 20+ messages in thread
From: Alison Schofield @ 2024-08-08 21:12 UTC (permalink / raw)
  To: Pasha Tatashin
  Cc: akpm, linux-kernel, linux-mm, linux-cxl, cerasuolodomenico,
	hannes, j.granados, lizhijian, muchun.song, nphamcs, rientjes,
	rppt, souravpanda, vbabka, willy, dan.j.williams, yi.zhang,
	david, yosryahmed

On Thu, Aug 08, 2024 at 03:05:32PM -0400, Pasha Tatashin wrote:
> On Thu, Aug 8, 2024 at 2:55 PM Alison Schofield
> <alison.schofield@intel.com> wrote:
> >
> > On Thu, Aug 08, 2024 at 03:42:37PM +0000, Pasha Tatashin wrote:
> > > Currently, when memory is hot-plugged or hot-removed the accounting is
> > > done based on the assumption that memmap is allocated from the same node
> > > as the hot-plugged/hot-removed memory, which is not always the case.
> > >
> > > In addition, there are challenges with keeping the node id of the memory
> > > that is being remove to the time when memmap accounting is actually
> > > performed: since this is done after remove_pfn_range_from_zone(), and
> > > also after remove_memory_block_devices(). Meaning that we cannot use
> > > pgdat nor walking though memblocks to get the nid.
> > >
> >
> > How about directly include the failing cases and user visible impacts as
> > reported in the Tags appended below.
> 
> Do you mean adding panic backtraces or repro steps to the commit log?

Yes. Perhaps abbreviated but something that makes the connection,
like this:

ndctl users reported a GPF when trying to destroy a namespace:
# ndctl destroy-namespace all -r all -f
Segmentation fault
dmesg:
[] Oops: general protection fault, probably for
non-canonical address 0xdffffc0000005650: 0000 [#1] PREEMPT SMP KASAN
PTI
[] KASAN: probably user-memory-access in range
[0x000000000002b280-0x000000000002b287]
[] CPU: 26 UID: 0 PID: 1868 Comm: ndctl Not tainted 6.11.0-rc1 #1
[] Hardware name: Dell Inc. PowerEdge R640/08HT8T, BIOS
2.20.1 09/13/2023
[] RIP: 0010:mod_node_page_state+0x2a/0x110


cxl-test users report a GPF when trying to unload the test module:
# modrpobe -r cxl-test
dmesg
[] BUG: unable to handle page fault for address: 0000000000004200
[] #PF: supervisor read access in kernel mode
[] #PF: error_code(0x0000) - not-present page
[] PGD 0 P4D 0 
[] Oops: Oops: 0000 [#1] PREEMPT SMP PTI
[] CPU: 0 UID: 0 PID: 1076 Comm: modprobe Tainted: G           O     N 6.11.0-rc1 #197
[] Tainted: [O]=OOT_MODULE, [N]=TEST
[] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
[] RIP: 0010:mod_node_page_state+0x6/0x90





^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2024-08-08 21:12 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-08 15:42 [PATCH v3 0/4] Fixes for memmap accounting Pasha Tatashin
2024-08-08 15:42 ` [PATCH v3 1/4] mm: update the memmap stat before page is freed Pasha Tatashin
2024-08-08 17:17   ` Yosry Ahmed
2024-08-08 17:20     ` Yosry Ahmed
2024-08-08 17:37       ` Pasha Tatashin
2024-08-08 17:17   ` Fan Ni
2024-08-08 15:42 ` [PATCH v3 2/4] mm: don't account memmap on failure Pasha Tatashin
2024-08-08 17:18   ` Yosry Ahmed
2024-08-08 17:19   ` Fan Ni
2024-08-08 18:12   ` David Hildenbrand
2024-08-08 18:54     ` Pasha Tatashin
2024-08-08 15:42 ` [PATCH v3 3/4] mm: add system wide stats items category Pasha Tatashin
2024-08-08 18:20   ` David Hildenbrand
2024-08-08 19:01     ` Pasha Tatashin
2024-08-08 15:42 ` [PATCH v3 4/4] mm: don't account memmap per-node Pasha Tatashin
2024-08-08 18:26   ` David Hildenbrand
2024-08-08 19:03     ` Pasha Tatashin
2024-08-08 18:55   ` Alison Schofield
2024-08-08 19:05     ` Pasha Tatashin
2024-08-08 21:12       ` Alison Schofield

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox