Skip to contentSkip to conversation

mm/vmstat: Fix build with MEMCG=y and VM_EVENT_COUNTERS=n

PATCH Applied Kirill A. Shutemov 5 participants 9 messages Jun 4, 2025 – Jun 5, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/vmstat: Fix build with MEMCG=y and VM_EVENT_COUNTERS=n

When compiling with MEMCG enabled but VM_EVENT_COUNTERS disabled, BUILD_BUG_ON() is triggered in vmstat_start because the vmstat_text array is larger than NR_VMSTAT_ITEMS.

This issue arises because some elements of the vmstat_text array are present when either MEMCG or VM_EVENT_COUNTERS is enabled, but NR_VMSTAT_ITEMS only accounts for these elements if VM_EVENT_COUNTERS is enabled.

The recent change in the BUILD_BUG_ON() check made it more strict, disallowing extra elements in the array, which revealed the issue.

Instead of adjusting the NR_VMSTAT_ITEMS definition to account for MEMCG, make MEMCG select VM_EVENT_COUNTERS. VM_EVENT_COUNTERS is enabled in most configurations anyway.

There is no need to backport this fix to stable trees. Without the strict BUILD_BUG_ON(), the issue is not harmful. The elements in question would only be read by the memcg code, not by /proc/vmstat.

Fixes ebc5d83d0443 ("mm/memcontrol: use vmstat names for printing statistics")
Cc Konstantin Khlebnikov <[email protected]>
---
 include/linux/vmstat.h | 4 ++--
 init/Kconfig           | 1 +
 mm/vmstat.c            | 4 ++--
 3 files changed, 5 insertions(+), 4 deletions(-)
include/linux/vmstat.h +2 -2
index b2ccb6845595..c287998908bf 100644
@@ -507,7 +507,7 @@ static inline const char *lru_list_name(enum lru_list lru)
 	return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
 }
 
-#if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
+#if defined(CONFIG_VM_EVENT_COUNTERS)
 static inline const char *vm_event_name(enum vm_event_item item)
 {
 	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
@@ -516,7 +516,7 @@ static inline const char *vm_event_name(enum vm_event_item item)
 			   NR_VM_STAT_ITEMS +
 			   item];
 }
-#endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
+#endif /* CONFIG_VM_EVENT_COUNTERS */
 
 #ifdef CONFIG_MEMCG
 
init/Kconfig +1 -0
index ab83abe0fd9d..dd332cac6036 100644
@@ -989,6 +989,7 @@ config MEMCG
 	select PAGE_COUNTER
 	select EVENTFD
 	select SLAB_OBJ_EXT
+	select VM_EVENT_COUNTERS
 	help
 	  Provides control over the memory footprint of tasks in a cgroup.
 
mm/vmstat.c +2 -2
index 27dc37168cfd..c3114b8826e4 100644
@@ -1301,7 +1301,7 @@ const char * const vmstat_text[] = {
 	[I(NR_MEMMAP_BOOT_PAGES)]		= "nr_memmap_boot_pages",
 #undef I
 
-#if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
+#if defined(CONFIG_VM_EVENT_COUNTERS)
 	/* enum vm_event_item counters */
 #define I(x) (NR_VM_ZONE_STAT_ITEMS + NR_VM_NUMA_EVENT_ITEMS + \
 	     NR_VM_NODE_STAT_ITEMS + NR_VM_STAT_ITEMS + x)
@@ -1498,7 +1498,7 @@ const char * const vmstat_text[] = {
 #endif
 #endif
 #undef I
-#endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
+#endif /* CONFIG_VM_EVENT_COUNTERS */
 };
 #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA || CONFIG_MEMCG */
 
Signature · 1 line
2.47.2
Acked-by on v1 1/1
#1 lore raw
Quoted 16 lines · Kirill A. Shutemov

When compiling with MEMCG enabled but VM_EVENT_COUNTERS disabled, BUILD_BUG_ON() is triggered in vmstat_start because the vmstat_text array is larger than NR_VMSTAT_ITEMS.

This issue arises because some elements of the vmstat_text array are present when either MEMCG or VM_EVENT_COUNTERS is enabled, but NR_VMSTAT_ITEMS only accounts for these elements if VM_EVENT_COUNTERS is enabled.

The recent change in the BUILD_BUG_ON() check made it more strict, disallowing extra elements in the array, which revealed the issue.

Instead of adjusting the NR_VMSTAT_ITEMS definition to account for MEMCG, make MEMCG select VM_EVENT_COUNTERS. VM_EVENT_COUNTERS is enabled in most configurations anyway.

Quoted 7 lines

There is no need to backport this fix to stable trees. Without the strict BUILD_BUG_ON(), the issue is not harmful. The elements in question would only be read by the memcg code, not by /proc/vmstat.

Fixes ebc5d83d0443 ("mm/memcontrol: use vmstat names for printing statistics")

Well in that case I think we should put Fixes: to the BUILD_BUG_ON() change. And if it's not yet a stable sha1, squash that together with this? It doesn't seem ebc5d83d0443 alone needs this fix.

Quoted 1 line
Cc Konstantin Khlebnikov <[email protected]>

Otherwise,

Quoted 62 lines
---
 include/linux/vmstat.h | 4 ++--
 init/Kconfig           | 1 +
 mm/vmstat.c            | 4 ++--
 3 files changed, 5 insertions(+), 4 deletions(-)
include/linux/vmstat.h +2 -2
index b2ccb6845595..c287998908bf 100644
@@ -507,7 +507,7 @@ static inline const char *lru_list_name(enum lru_list lru)
 	return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
 }
 
-#if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
+#if defined(CONFIG_VM_EVENT_COUNTERS)
 static inline const char *vm_event_name(enum vm_event_item item)
 {
 	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
@@ -516,7 +516,7 @@ static inline const char *vm_event_name(enum vm_event_item item)
 			   NR_VM_STAT_ITEMS +
 			   item];
 }
-#endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
+#endif /* CONFIG_VM_EVENT_COUNTERS */
 
 #ifdef CONFIG_MEMCG
 
init/Kconfig +1 -0
index ab83abe0fd9d..dd332cac6036 100644
@@ -989,6 +989,7 @@ config MEMCG
 	select PAGE_COUNTER
 	select EVENTFD
 	select SLAB_OBJ_EXT
+	select VM_EVENT_COUNTERS
 	help
 	  Provides control over the memory footprint of tasks in a cgroup.
 
mm/vmstat.c +2 -2
index 27dc37168cfd..c3114b8826e4 100644
@@ -1301,7 +1301,7 @@ const char * const vmstat_text[] = {
 	[I(NR_MEMMAP_BOOT_PAGES)]		= "nr_memmap_boot_pages",
 #undef I
 
-#if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
+#if defined(CONFIG_VM_EVENT_COUNTERS)
 	/* enum vm_event_item counters */
 #define I(x) (NR_VM_ZONE_STAT_ITEMS + NR_VM_NUMA_EVENT_ITEMS + \
 	     NR_VM_NODE_STAT_ITEMS + NR_VM_STAT_ITEMS + x)
@@ -1498,7 +1498,7 @@ const char * const vmstat_text[] = {
 #endif
 #endif
 #undef I
-#endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
+#endif /* CONFIG_VM_EVENT_COUNTERS */
 };
 #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA || CONFIG_MEMCG */
 
maintainer
#2 lore raw
Quoted 11 lines · On Wed, 4 Jun 2025 11:56:42 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 7 lines

There is no need to backport this fix to stable trees. Without the strict BUILD_BUG_ON(), the issue is not harmful. The elements in question would only be read by the memcg code, not by /proc/vmstat.

Fixes ebc5d83d0443 ("mm/memcontrol: use vmstat names for printing statistics")

Well in that case I think we should put Fixes: to the BUILD_BUG_ON() change. And if it's not yet a stable sha1, squash that together with this? It doesn't seem ebc5d83d0443 alone needs this fix.

I shuffled things around.

I moved "mm: strictly check vmstat_text array size" from mm-hotfixes and back into mm-new for the next cycle.

I reworked "mm/vmstat: fix build with MEMCG=y and VM_EVENT_COUNTERS=n" so it precedes "mm: strictly check vmstat_text array size".

I reworked "mm/vmstat: utilize designated initializers for the vmstat_text array" so it comes last.

So the applying order is now

mm-hotfixes:
mm-fix-vmstat-after-removing-nr_bounce.patch

mm-new:
mm-vmstat-fix-build-with-memcg=y-and-vm_event_counters=n.patch mm-strictly-check-vmstat_text-array-size.patch mm-vmstat-utilize-designated-initializers-for-the-vmstat_text-array.patch

and everything should land nicely.

#3 lore raw
Quoted 19 lines · Andrew Morton
Quoted 11 lines · On Wed, 4 Jun 2025 11:56:42 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 7 lines

There is no need to backport this fix to stable trees. Without the strict BUILD_BUG_ON(), the issue is not harmful. The elements in question would only be read by the memcg code, not by /proc/vmstat.

Fixes ebc5d83d0443 ("mm/memcontrol: use vmstat names for printing statistics")

Well in that case I think we should put Fixes: to the BUILD_BUG_ON() change. And if it's not yet a stable sha1, squash that together with this? It doesn't seem ebc5d83d0443 alone needs this fix.

I shuffled things around.

I moved "mm: strictly check vmstat_text array size" from mm-hotfixes and back into mm-new for the next cycle.

Quoted 8 lines

I reworked "mm/vmstat: fix build with MEMCG=y and VM_EVENT_COUNTERS=n" so it precedes "mm: strictly check vmstat_text array size".

I reworked "mm/vmstat: utilize designated initializers for the vmstat_text array" so it comes last.

So the applying order is now

I checked and in general it looks good, except a nit below.

Quoted 6 lines

mm-hotfixes:
mm-fix-vmstat-after-removing-nr_bounce.patch

mm-new:
mm-vmstat-fix-build-with-memcg=y-and-vm_event_counters=n.patch mm-strictly-check-vmstat_text-array-size.patch

The changelogs of these two don't reflect the new ordering though, maybe Kirill can provide updated ones?

Quoted 3 lines

mm-vmstat-utilize-designated-initializers-for-the-vmstat_text-array.patch

and everything should land nicely.

#4 lore raw
Quoted 34 lines · Vlastimil Babka
Quoted 19 lines · Andrew Morton
Quoted 11 lines · On Wed, 4 Jun 2025 11:56:42 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 7 lines

There is no need to backport this fix to stable trees. Without the strict BUILD_BUG_ON(), the issue is not harmful. The elements in question would only be read by the memcg code, not by /proc/vmstat.

Fixes ebc5d83d0443 ("mm/memcontrol: use vmstat names for printing statistics")

Well in that case I think we should put Fixes: to the BUILD_BUG_ON() change. And if it's not yet a stable sha1, squash that together with this? It doesn't seem ebc5d83d0443 alone needs this fix.

I shuffled things around.

I moved "mm: strictly check vmstat_text array size" from mm-hotfixes and back into mm-new for the next cycle.

Quoted 8 lines

I reworked "mm/vmstat: fix build with MEMCG=y and VM_EVENT_COUNTERS=n" so it precedes "mm: strictly check vmstat_text array size".

I reworked "mm/vmstat: utilize designated initializers for the vmstat_text array" so it comes last.

So the applying order is now

I checked and in general it looks good, except a nit below.

Quoted 3 lines

mm-hotfixes:
mm-fix-vmstat-after-removing-nr_bounce.patch

Quoted 6 lines
Quoted 3 lines

mm-new:
mm-vmstat-fix-build-with-memcg=y-and-vm_event_counters=n.patch mm-strictly-check-vmstat_text-array-size.patch

The changelogs of these two don't reflect the new ordering though, maybe Kirill can provide updated ones?

Maybe something like this, for the first patch?

mm/vmstat: Make MEMCG select VM_EVENT_COUNTERS

The vmstat_text array contains labels for counters displayed in /proc/vmstat. It is important to keep the labels in sync with the counters.

There is a BUILD_BUG_ON() check in vmstat_start() that ensures the size of the vmstat_text is not smaller than VM_EVENT_COUNTERS. This helps to catch cases where a new counter is added but the label is not. However, it does not help if a counter is removed but the label remains.

It would be nice to make the BUILD_BUG_ON() check more strict to catch such cases. However, when compiling with MEMCG enabled but VM_EVENT_COUNTERS disabled, the vmstat_text array is larger than NR_VMSTAT_ITEMS.

This issue arises because some elements of the vmstat_text array are present when either MEMCG or VM_EVENT_COUNTERS is enabled, but NR_VMSTAT_ITEMS only accounts for these elements if VM_EVENT_COUNTERS is enabled.

Instead of adjusting the NR_VMSTAT_ITEMS definition to account for MEMCG, make MEMCG select VM_EVENT_COUNTERS. VM_EVENT_COUNTERS is enabled in most configurations anyway.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#5 lore raw
Quoted 35 lines · Kirill A. Shutemov
Quoted 34 lines · Vlastimil Babka
Quoted 19 lines · Andrew Morton
Quoted 11 lines · On Wed, 4 Jun 2025 11:56:42 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 7 lines

There is no need to backport this fix to stable trees. Without the strict BUILD_BUG_ON(), the issue is not harmful. The elements in question would only be read by the memcg code, not by /proc/vmstat.

Fixes ebc5d83d0443 ("mm/memcontrol: use vmstat names for printing statistics")

Well in that case I think we should put Fixes: to the BUILD_BUG_ON() change. And if it's not yet a stable sha1, squash that together with this? It doesn't seem ebc5d83d0443 alone needs this fix.

I shuffled things around.

I moved "mm: strictly check vmstat_text array size" from mm-hotfixes and back into mm-new for the next cycle.

Quoted 8 lines

I reworked "mm/vmstat: fix build with MEMCG=y and VM_EVENT_COUNTERS=n" so it precedes "mm: strictly check vmstat_text array size".

I reworked "mm/vmstat: utilize designated initializers for the vmstat_text array" so it comes last.

So the applying order is now

I checked and in general it looks good, except a nit below.

Quoted 3 lines

mm-hotfixes:
mm-fix-vmstat-after-removing-nr_bounce.patch

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

mm-new:
mm-vmstat-fix-build-with-memcg=y-and-vm_event_counters=n.patch mm-strictly-check-vmstat_text-array-size.patch

The changelogs of these two don't reflect the new ordering though, maybe Kirill can provide updated ones?

Maybe something like this, for the first patch?

LGTM, thanks!

Quoted 22 lines
Quoted 1 line

mm/vmstat: Make MEMCG select VM_EVENT_COUNTERS

The vmstat_text array contains labels for counters displayed in /proc/vmstat. It is important to keep the labels in sync with the counters.

There is a BUILD_BUG_ON() check in vmstat_start() that ensures the size of the vmstat_text is not smaller than VM_EVENT_COUNTERS. This helps to catch cases where a new counter is added but the label is not. However, it does not help if a counter is removed but the label remains.

It would be nice to make the BUILD_BUG_ON() check more strict to catch such cases. However, when compiling with MEMCG enabled but VM_EVENT_COUNTERS disabled, the vmstat_text array is larger than NR_VMSTAT_ITEMS.

This issue arises because some elements of the vmstat_text array are present when either MEMCG or VM_EVENT_COUNTERS is enabled, but NR_VMSTAT_ITEMS only accounts for these elements if VM_EVENT_COUNTERS is enabled.

Instead of adjusting the NR_VMSTAT_ITEMS definition to account for MEMCG, make MEMCG select VM_EVENT_COUNTERS. VM_EVENT_COUNTERS is enabled in most configurations anyway.

maintainer
#6 lore raw
In reply to Kirill A. Shutemov
Quoted 6 lines · On Thu, 5 Jun 2025 14:53:59 +0300 "Kirill A. Shutemov"…
Quoted 2 lines

The changelogs of these two don't reflect the new ordering though, maybe Kirill can provide updated ones?

Maybe something like this, for the first patch?

Quoted 7 lines

mm/vmstat: Make MEMCG select VM_EVENT_COUNTERS

The vmstat_text array contains labels for counters displayed in /proc/vmstat. It is important to keep the labels in sync with the counters.

...

Updated, thanks.

Tested-by Acked-by on v1 1/1
#7 lore raw
In reply to Kirill A. Shutemov
Quoted 16 lines · Kirill A. Shutemov

When compiling with MEMCG enabled but VM_EVENT_COUNTERS disabled, BUILD_BUG_ON() is triggered in vmstat_start because the vmstat_text array is larger than NR_VMSTAT_ITEMS.

This issue arises because some elements of the vmstat_text array are present when either MEMCG or VM_EVENT_COUNTERS is enabled, but NR_VMSTAT_ITEMS only accounts for these elements if VM_EVENT_COUNTERS is enabled.

The recent change in the BUILD_BUG_ON() check made it more strict, disallowing extra elements in the array, which revealed the issue.

Instead of adjusting the NR_VMSTAT_ITEMS definition to account for MEMCG, make MEMCG select VM_EVENT_COUNTERS. VM_EVENT_COUNTERS is enabled in most configurations anyway.

Quoted 8 lines

There is no need to backport this fix to stable trees. Without the strict BUILD_BUG_ON(), the issue is not harmful. The elements in question would only be read by the memcg code, not by /proc/vmstat.

Fixes ebc5d83d0443 ("mm/memcontrol: use vmstat names for printing statistics")
Cc Konstantin Khlebnikov <[email protected]>

Thanks.

Quoted 62 lines
---
 include/linux/vmstat.h | 4 ++--
 init/Kconfig           | 1 +
 mm/vmstat.c            | 4 ++--
 3 files changed, 5 insertions(+), 4 deletions(-)
include/linux/vmstat.h +2 -2
index b2ccb6845595..c287998908bf 100644
@@ -507,7 +507,7 @@ static inline const char *lru_list_name(enum lru_list lru)
 	return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
 }
 
-#if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
+#if defined(CONFIG_VM_EVENT_COUNTERS)
 static inline const char *vm_event_name(enum vm_event_item item)
 {
 	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
@@ -516,7 +516,7 @@ static inline const char *vm_event_name(enum vm_event_item item)
 			   NR_VM_STAT_ITEMS +
 			   item];
 }
-#endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
+#endif /* CONFIG_VM_EVENT_COUNTERS */
 
 #ifdef CONFIG_MEMCG
 
init/Kconfig +1 -0
index ab83abe0fd9d..dd332cac6036 100644
@@ -989,6 +989,7 @@ config MEMCG
 	select PAGE_COUNTER
 	select EVENTFD
 	select SLAB_OBJ_EXT
+	select VM_EVENT_COUNTERS
 	help
 	  Provides control over the memory footprint of tasks in a cgroup.
 
mm/vmstat.c +2 -2
index 27dc37168cfd..c3114b8826e4 100644
@@ -1301,7 +1301,7 @@ const char * const vmstat_text[] = {
 	[I(NR_MEMMAP_BOOT_PAGES)]		= "nr_memmap_boot_pages",
 #undef I
 
-#if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
+#if defined(CONFIG_VM_EVENT_COUNTERS)
 	/* enum vm_event_item counters */
 #define I(x) (NR_VM_ZONE_STAT_ITEMS + NR_VM_NUMA_EVENT_ITEMS + \
 	     NR_VM_NODE_STAT_ITEMS + NR_VM_STAT_ITEMS + x)
@@ -1498,7 +1498,7 @@ const char * const vmstat_text[] = {
 #endif
 #endif
 #undef I
-#endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
+#endif /* CONFIG_VM_EVENT_COUNTERS */
 };
 #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA || CONFIG_MEMCG */
 
Signature · 1 line
~Randy
Acked-by on v1 1/1
#8 lore raw
In reply to Kirill A. Shutemov
Quoted 16 lines · Kirill A. Shutemov

When compiling with MEMCG enabled but VM_EVENT_COUNTERS disabled, BUILD_BUG_ON() is triggered in vmstat_start because the vmstat_text array is larger than NR_VMSTAT_ITEMS.

This issue arises because some elements of the vmstat_text array are present when either MEMCG or VM_EVENT_COUNTERS is enabled, but NR_VMSTAT_ITEMS only accounts for these elements if VM_EVENT_COUNTERS is enabled.

The recent change in the BUILD_BUG_ON() check made it more strict, disallowing extra elements in the array, which revealed the issue.

Instead of adjusting the NR_VMSTAT_ITEMS definition to account for MEMCG, make MEMCG select VM_EVENT_COUNTERS. VM_EVENT_COUNTERS is enabled in most configurations anyway.

Quoted 8 lines

There is no need to backport this fix to stable trees. Without the strict BUILD_BUG_ON(), the issue is not harmful. The elements in question would only be read by the memcg code, not by /proc/vmstat.

Fixes ebc5d83d0443 ("mm/memcontrol: use vmstat names for printing statistics")
Cc Konstantin Khlebnikov <[email protected]>