Skip to contentSkip to conversation

mm/show_mem: Bug fix for print mem alloc info

PATCH Superseded Yueyang Pan 4 participants 12 messages Sep 2, 2025 – Sep 3, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm/show_mem: Bug fix for print mem alloc info

This patch set fixes two issues we saw in production rollout.

The first issue is that we saw all zero output of memory allocation profiling information from show_mem() if CONFIG_MEM_ALLOC_PROFILING is set and sysctl.vm.mem_profiling=0. This cause ambiguity as we don't know what 0B actually means in the output. It can mean either memory allocation profiling is temporary disabled or the allocation at that position is actually 0. Such ambiguity will make further parsing harder as we cannot differentiate between two case.

The second issue is that multiple entities can call show_mem() which messed up the allocation info in dmesg. We saw outputs like this:

```
    327 MiB    83635 mm/compaction.c:1880 func:compaction_alloc
   48.4 GiB 12684937 mm/memory.c:1061 func:folio_prealloc
   7.48 GiB    10899 mm/huge_memory.c:1159 func:vma_alloc_anon_folio_pmd
    298 MiB    95216 kernel/fork.c:318 func:alloc_thread_stack_node
    250 MiB    63901 mm/zsmalloc.c:987 func:alloc_zspage
    1.42 GiB   372527 mm/memory.c:1063 func:folio_prealloc
    1.17 GiB    95693 mm/slub.c:2424 func:alloc_slab_page
     651 MiB   166732 mm/readahead.c:270 func:page_cache_ra_unbounded
     419 MiB   107261 net/core/page_pool.c:572 func:__page_pool_alloc_pages_slow
     404 MiB   103425 arch/x86/mm/pgtable.c:25 func:pte_alloc_one
```

The above example is because one kthread invokes show_mem() from __alloc_pages_slowpath while kernel itself calls oom_kill_process()

Revision History
=================
Changes from v1 [1]
- Dump status of memory allocation profiling instead of disabling the output following Vishal's advise.
- Move lock from file scope to within __show_mem() and replace mutex with spinlock following Andrew, Vlastimil and Shakeel's advice.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Yueyang Pan (2):
mm/show_mem: Dump the status of the mem alloc profiling before
printing
mm/show_mem: Add trylock while printing alloc info

 mm/show_mem.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/2 mm/show_mem: Dump the status of the mem alloc profiling before printing

This patch prints the status of the memory allocation profiling before __show_mem actually prints the detailed allocation info. This way will let us know the 0B we saw in allocation info is because the profiling is disabled or the allocation is actually 0B.

---
 mm/show_mem.c | 2 ++
 1 file changed, 2 insertions(+)
mm/show_mem.c +2 -0
index ecf20a93ea54..51892ce2efc4 100644
@@ -427,6 +427,8 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 
 		nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false);
 		if (nr) {
+			pr_notice("Memory alloction profiling is turned %s\n",
+				mem_alloc_profiling_enabled() ? "on" : "off");
 			pr_notice("Memory allocations:\n");
 			for (i = 0; i < nr; i++) {
 				struct codetag *ct = tags[i].ct;
Signature · 1 line
2.47.3
#6 lore raw

PATCH v2 2/2 mm/show_mem: Add trylock while printing alloc info

In reply to Yueyang Pan

In production, show_mem() can be called concurrently from two different entities, for example one from oom_kill_process() another from __alloc_pages_slowpath from another kthread. This patch adds a spinlock and invokes trylock before printing out the kernel alloc info in show_mem(). This way two alloc info won't interleave with each other, which then makes parsing easier.

---
 mm/show_mem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/show_mem.c +3 -1
index 51892ce2efc4..4c876ea2b66f 100644
@@ -396,6 +396,7 @@ static void show_free_areas(unsigned int filter, nodemask_t *nodemask, int max_z
 
 void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 {
+	static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);
 	unsigned long total = 0, reserved = 0, highmem = 0;
 	struct zone *zone;
 
@@ -421,7 +422,7 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 	printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages));
 #endif
 #ifdef CONFIG_MEM_ALLOC_PROFILING
-	{
+	if (spin_trylock(&mem_alloc_profiling_spinlock)) {
 		struct codetag_bytes tags[10];
 		size_t i, nr;
 
@@ -449,6 +450,7 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 						  ct->lineno, ct->function);
 			}
 		}
+		spin_unlock(&mem_alloc_profiling_spinlock);
 	}
 #endif
 }
Signature · 1 line
2.47.3
Acked-by on v2 2/2
#7 lore raw

Re: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info

Quoted 8 lines · Yueyang Pan

In production, show_mem() can be called concurrently from two different entities, for example one from oom_kill_process() another from __alloc_pages_slowpath from another kthread. This patch adds a spinlock and invokes trylock before printing out the kernel alloc info in show_mem(). This way two alloc info won't interleave with each other, which then makes parsing easier.

Quoted 33 lines
---
 mm/show_mem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/show_mem.c +3 -1
index 51892ce2efc4..4c876ea2b66f 100644
@@ -396,6 +396,7 @@ static void show_free_areas(unsigned int filter, nodemask_t *nodemask, int max_z
 
 void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 {
+	static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);
 	unsigned long total = 0, reserved = 0, highmem = 0;
 	struct zone *zone;
 
@@ -421,7 +422,7 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 	printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages));
 #endif
 #ifdef CONFIG_MEM_ALLOC_PROFILING
-	{
+	if (spin_trylock(&mem_alloc_profiling_spinlock)) {
 		struct codetag_bytes tags[10];
 		size_t i, nr;
 
@@ -449,6 +450,7 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 						  ct->lineno, ct->function);
 			}
 		}
+		spin_unlock(&mem_alloc_profiling_spinlock);
 	}
 #endif
 }
#2 lore raw

Re: [PATCH v2 1/2] mm/show_mem: Dump the status of the mem alloc profiling before printing

In reply to Yueyang Pan
Quoted 16 lines · Yueyang Pan

This patch prints the status of the memory allocation profiling before __show_mem actually prints the detailed allocation info. This way will let us know the 0B we saw in allocation info is because the profiling is disabled or the allocation is actually 0B.

---
 mm/show_mem.c | 2 ++
 1 file changed, 2 insertions(+)
mm/show_mem.c +0 -0
index ecf20a93ea54..51892ce2efc4 100644

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 430

Quoted 4 lines
mm/show_mem.c +1 -0
@@ -427,6 +427,8 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 
 		nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false);
 		if (nr) {
+			pr_notice("Memory alloction profiling is turned %s\n",

There's a typo "alloction".

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 432

Quoted 2 lines
mm/show_mem.c +1 -0
+				mem_alloc_profiling_enabled() ? "on" : "off");
 			pr_notice("Memory allocations:\n");

But I think the message would be more obvious if it said e.g.:

"Memory allocations (profiling is currently [on/off]):"

Quoted 2 lines
mm/show_mem.c +0 -0
 			for (i = 0; i < nr; i++) {
 				struct codetag *ct = tags[i].ct;
Acked-by on v2 2/2
#8 lore raw

Re: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info

In reply to Yueyang Pan
Quoted 8 lines · Yueyang Pan

In production, show_mem() can be called concurrently from two different entities, for example one from oom_kill_process() another from __alloc_pages_slowpath from another kthread. This patch adds a spinlock and invokes trylock before printing out the kernel alloc info in show_mem(). This way two alloc info won't interleave with each other, which then makes parsing easier.

Quoted 33 lines
---
 mm/show_mem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/show_mem.c +3 -1
index 51892ce2efc4..4c876ea2b66f 100644
@@ -396,6 +396,7 @@ static void show_free_areas(unsigned int filter, nodemask_t *nodemask, int max_z
 
 void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 {
+	static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);
 	unsigned long total = 0, reserved = 0, highmem = 0;
 	struct zone *zone;
 
@@ -421,7 +422,7 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 	printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages));
 #endif
 #ifdef CONFIG_MEM_ALLOC_PROFILING
-	{
+	if (spin_trylock(&mem_alloc_profiling_spinlock)) {
 		struct codetag_bytes tags[10];
 		size_t i, nr;
 
@@ -449,6 +450,7 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 						  ct->lineno, ct->function);
 			}
 		}
+		spin_unlock(&mem_alloc_profiling_spinlock);
 	}
 #endif
 }
#3 lore raw

Re: [PATCH v2 1/2] mm/show_mem: Dump the status of the mem alloc profiling before printing

In reply to Vlastimil Babka
Quoted 18 lines · Vlastimil Babka
Quoted 17 lines · Yueyang Pan

This patch prints the status of the memory allocation profiling before __show_mem actually prints the detailed allocation info. This way will let us know the 0B we saw in allocation info is because the profiling is disabled or the allocation is actually 0B.

---
 mm/show_mem.c | 2 ++
 1 file changed, 2 insertions(+)
mm/show_mem.c +0 -0
index ecf20a93ea54..51892ce2efc4 100644
@@ -427,6 +427,8 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 
Quoted 5 lines

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 430

Quoted 3 lines
mm/show_mem.c +1 -0
 		nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false);
 		if (nr) {
+			pr_notice("Memory alloction profiling is turned %s\n",

There's a typo "alloction".

Nice catch. Thanks!

Quoted 7 lines

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 432

Quoted 3 lines
mm/show_mem.c +1 -0
+				mem_alloc_profiling_enabled() ? "on" : "off");
 			pr_notice("Memory allocations:\n");

But I think the message would be more obvious if it said e.g.:

"Memory allocations (profiling is currently [on/off]):"

I can change it in the next version. I saw Andrew gave approval. In this case, shall I send v3 or create a new patch?

Quoted 4 lines

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 434

Quoted 3 lines
mm/show_mem.c +0 -0
 			for (i = 0; i < nr; i++) {
 				struct codetag *ct = tags[i].ct;

Thanks
Pan

#9 lore raw

Re: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info

In reply to Yueyang Pan
kernel test robot noticed the following build warnings:

Hi Yueyang,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yueyang-Pan/mm-show_mem-Dump-the-status-of-the-mem-alloc-profiling-before-printing/20250903-000616
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/1491df0ac12a7626b7c9b00e26a6e10adb8c9045.1756827906.git.pyyjason%40gmail.com
patch subject: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info
config: i386-buildonly-randconfig-001-20250903 (https://download.01.org/0day-ci/archive/20250903/[email protected]/config)
compiler: gcc-13 (Debian 13.3.0-16) 13.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250903/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from mm/show_mem.c:18:
mm/show_mem.c: In function 'show_free_areas':
mm/show_mem.c:336:49: error: 'NR_ZSPAGES' undeclared (first use in this function); did you mean 'NR_STATS'?
336 | K(zone_page_state(zone, NR_ZSPAGES)),
| ^~~~~~~~~~
mm/internal.h:560:16: note: in definition of macro 'K'

     560 | #define K(x) ((x) << (PAGE_SHIFT-10))
         |                ^
   include/linux/printk.h:512:26: note: in expansion of macro 'printk_index_wrap'
     512 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
         |                          ^~~~~~~~~~~~~~~~~
   mm/show_mem.c:298:17: note: in expansion of macro 'printk'
     298 |                 printk(KERN_CONT
         |                 ^~~~~~
   mm/show_mem.c:336:49: note: each undeclared identifier is reported only once for each function it appears in
     336 |                         K(zone_page_state(zone, NR_ZSPAGES)),
         |                                                 ^~~~~~~~~~
   mm/internal.h:560:16: note: in definition of macro 'K'
     560 | #define K(x) ((x) << (PAGE_SHIFT-10))
         |                ^
   include/linux/printk.h:512:26: note: in expansion of macro 'printk_index_wrap'
     512 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
         |                          ^~~~~~~~~~~~~~~~~
   mm/show_mem.c:298:17: note: in expansion of macro 'printk'
     298 |                 printk(KERN_CONT
         |                 ^~~~~~
   In file included from include/linux/spinlock.h:89,
                    from include/linux/wait.h:9,
                    from include/linux/wait_bit.h:8,
                    from include/linux/fs.h:7,
                    from include/linux/highmem.h:5,
                    from include/linux/bvec.h:10,
                    from include/linux/blk_types.h:10,
                    from include/linux/blkdev.h:9,
                    from mm/show_mem.c:8:
   mm/show_mem.c: In function '__show_mem':
>> mm/show_mem.c:399:32: warning: unused variable 'mem_alloc_profiling_spinlock' [-Wunused-variable]
     399 |         static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);
         |                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

include/linux/spinlock_types.h:43:44: note: in definition of macro 'DEFINE_SPINLOCK'
43 | #define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
| ^

vim +/mem_alloc_profiling_spinlock +399 mm/show_mem.c

396
397 void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
398 {

 > 399		static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

Re: [PATCH v2 1/2] mm/show_mem: Dump the status of the mem alloc profiling before printing

In reply to Yueyang Pan
Quoted 33 lines · Yueyang Pan
Quoted 18 lines · Vlastimil Babka
Quoted 17 lines · Yueyang Pan

This patch prints the status of the memory allocation profiling before __show_mem actually prints the detailed allocation info. This way will let us know the 0B we saw in allocation info is because the profiling is disabled or the allocation is actually 0B.

---
 mm/show_mem.c | 2 ++
 1 file changed, 2 insertions(+)
mm/show_mem.c +0 -0
index ecf20a93ea54..51892ce2efc4 100644
@@ -427,6 +427,8 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 
Quoted 5 lines

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 430

Quoted 3 lines
mm/show_mem.c +1 -0
 		nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false);
 		if (nr) {
+			pr_notice("Memory alloction profiling is turned %s\n",

There's a typo "alloction".

Nice catch. Thanks!

Quoted 6 lines

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 432

Quoted 3 lines
mm/show_mem.c +1 -0
+				mem_alloc_profiling_enabled() ? "on" : "off");
 			pr_notice("Memory allocations:\n");

But I think the message would be more obvious if it said e.g.:

Quoted 4 lines
Quoted 1 line

"Memory allocations (profiling is currently [on/off]):"

I can change it in the next version. I saw Andrew gave approval. In this case, shall I send v3 or create a new patch?

You can send v3 (given patch 2/2 needs warning fix too) and Andrew can replace or convert to fixups.

Quoted 7 lines
Quoted 4 lines

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 434

Quoted 3 lines
mm/show_mem.c +0 -0
 			for (i = 0; i < nr; i++) {
 				struct codetag *ct = tags[i].ct;

Thanks
Pan

#5 lore raw

Re: [PATCH v2 1/2] mm/show_mem: Dump the status of the mem alloc profiling before printing

In reply to Yueyang Pan
Quoted 33 lines · Yueyang Pan
Quoted 18 lines · Vlastimil Babka
Quoted 17 lines · Yueyang Pan

This patch prints the status of the memory allocation profiling before __show_mem actually prints the detailed allocation info. This way will let us know the 0B we saw in allocation info is because the profiling is disabled or the allocation is actually 0B.

---
 mm/show_mem.c | 2 ++
 1 file changed, 2 insertions(+)
mm/show_mem.c +0 -0
index ecf20a93ea54..51892ce2efc4 100644
@@ -427,6 +427,8 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 
Quoted 5 lines

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 430

Quoted 3 lines
mm/show_mem.c +1 -0
 		nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false);
 		if (nr) {
+			pr_notice("Memory alloction profiling is turned %s\n",

There's a typo "alloction".

Nice catch. Thanks!

Quoted 6 lines

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 432

Quoted 3 lines
mm/show_mem.c +1 -0
+				mem_alloc_profiling_enabled() ? "on" : "off");
 			pr_notice("Memory allocations:\n");

But I think the message would be more obvious if it said e.g.:

Quoted 5 lines
Quoted 1 line

"Memory allocations (profiling is currently [on/off]):"

I can change it in the next version. I saw Andrew gave approval. In this case, shall I send v3 or create a new patch?

Send a v3 as a change is needed in both patches.

Quoted 7 lines
Quoted 4 lines

mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 434

Quoted 3 lines
mm/show_mem.c +0 -0
 			for (i = 0; i < nr; i++) {
 				struct codetag *ct = tags[i].ct;

Thanks
Pan

#10 lore raw

Re: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info

In reply to kernel test robot
Quoted 19 lines · kernel test robot

Hi Yueyang,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yueyang-Pan/mm-show_mem-Dump-the-status-of-the-mem-alloc-profiling-before-printing/20250903-000616
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/1491df0ac12a7626b7c9b00e26a6e10adb8c9045.1756827906.git.pyyjason%40gmail.com
patch subject: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info
config: i386-buildonly-randconfig-001-20250903 (https://download.01.org/0day-ci/archive/20250903/[email protected]/config)
compiler: gcc-13 (Debian 13.3.0-16) 13.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250903/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Quoted 7 lines

All warnings (new ones prefixed by >>):

In file included from mm/show_mem.c:18:
mm/show_mem.c: In function 'show_free_areas':
mm/show_mem.c:336:49: error: 'NR_ZSPAGES' undeclared (first use in this function); did you mean 'NR_STATS'?
336 | K(zone_page_state(zone, NR_ZSPAGES)),
| ^~~~~~~~~~

This is from a different patch and being fixed. Interesting that lkp will report additional warnings even in presence of prior errors.

Quoted 30 lines

mm/internal.h:560:16: note: in definition of macro 'K'

     560 | #define K(x) ((x) << (PAGE_SHIFT-10))
         |                ^
   include/linux/printk.h:512:26: note: in expansion of macro 'printk_index_wrap'
     512 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
         |                          ^~~~~~~~~~~~~~~~~
   mm/show_mem.c:298:17: note: in expansion of macro 'printk'
     298 |                 printk(KERN_CONT
         |                 ^~~~~~
   mm/show_mem.c:336:49: note: each undeclared identifier is reported only once for each function it appears in
     336 |                         K(zone_page_state(zone, NR_ZSPAGES)),
         |                                                 ^~~~~~~~~~
   mm/internal.h:560:16: note: in definition of macro 'K'
     560 | #define K(x) ((x) << (PAGE_SHIFT-10))
         |                ^
   include/linux/printk.h:512:26: note: in expansion of macro 'printk_index_wrap'
     512 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
         |                          ^~~~~~~~~~~~~~~~~
   mm/show_mem.c:298:17: note: in expansion of macro 'printk'
     298 |                 printk(KERN_CONT
         |                 ^~~~~~
   In file included from include/linux/spinlock.h:89,
                    from include/linux/wait.h:9,
                    from include/linux/wait_bit.h:8,
                    from include/linux/fs.h:7,
                    from include/linux/highmem.h:5,
                    from include/linux/bvec.h:10,
                    from include/linux/blk_types.h:10,
                    from include/linux/blkdev.h:9,
                    from mm/show_mem.c:8:
Quoted 3 lines

mm/show_mem.c: In function '__show_mem':

Quoted 1 line
Quoted 1 line

mm/show_mem.c:399:32: warning: unused variable 'mem_alloc_profiling_spinlock' [-Wunused-variable]

     399 |         static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);

This is the warning

I think you can simply move the definition to the existing #ifdef CONFIG_MEM_ALLOC_PROFILING block above the spin_trylock(). The kernel now uses a new enough C standard to allow this and not only at the beginning of a cuntion. While not encouraged to do that in general, this seems to be a valid use case.

Quoted 13 lines
         |                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

include/linux/spinlock_types.h:43:44: note: in definition of macro 'DEFINE_SPINLOCK'
43 | #define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
| ^

vim +/mem_alloc_profiling_spinlock +399 mm/show_mem.c

   396	
   397	void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
   398	{
Quoted 1 line

399 static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);

#11 lore raw

Re: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info

Quoted 22 lines · Vlastimil Babka
Quoted 21 lines · kernel test robot

Hi Yueyang,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yueyang-Pan/mm-show_mem-Dump-the-status-of-the-mem-alloc-profiling-before-printing/20250903-000616
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/1491df0ac12a7626b7c9b00e26a6e10adb8c9045.1756827906.git.pyyjason%40gmail.com
patch subject: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info
config: i386-buildonly-randconfig-001-20250903 (https://download.01.org/0day-ci/archive/20250903/[email protected]/config)
compiler: gcc-13 (Debian 13.3.0-16) 13.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250903/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

Quoted 8 lines
Quoted 5 lines

In file included from mm/show_mem.c:18:
mm/show_mem.c: In function 'show_free_areas':
mm/show_mem.c:336:49: error: 'NR_ZSPAGES' undeclared (first use in this function); did you mean 'NR_STATS'?
336 | K(zone_page_state(zone, NR_ZSPAGES)),
| ^~~~~~~~~~

This is from a different patch and being fixed. Interesting that lkp will report additional warnings even in presence of prior errors.

Thanks. Understood.

Quoted 35 lines
Quoted 31 lines

mm/internal.h:560:16: note: in definition of macro 'K'

     560 | #define K(x) ((x) << (PAGE_SHIFT-10))
         |                ^
   include/linux/printk.h:512:26: note: in expansion of macro 'printk_index_wrap'
     512 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
         |                          ^~~~~~~~~~~~~~~~~
   mm/show_mem.c:298:17: note: in expansion of macro 'printk'
     298 |                 printk(KERN_CONT
         |                 ^~~~~~
   mm/show_mem.c:336:49: note: each undeclared identifier is reported only once for each function it appears in
     336 |                         K(zone_page_state(zone, NR_ZSPAGES)),
         |                                                 ^~~~~~~~~~
   mm/internal.h:560:16: note: in definition of macro 'K'
     560 | #define K(x) ((x) << (PAGE_SHIFT-10))
         |                ^
   include/linux/printk.h:512:26: note: in expansion of macro 'printk_index_wrap'
     512 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
         |                          ^~~~~~~~~~~~~~~~~
   mm/show_mem.c:298:17: note: in expansion of macro 'printk'
     298 |                 printk(KERN_CONT
         |                 ^~~~~~
   In file included from include/linux/spinlock.h:89,
                    from include/linux/wait.h:9,
                    from include/linux/wait_bit.h:8,
                    from include/linux/fs.h:7,
                    from include/linux/highmem.h:5,
                    from include/linux/bvec.h:10,
                    from include/linux/blk_types.h:10,
                    from include/linux/blkdev.h:9,
                    from mm/show_mem.c:8:
Quoted 3 lines

mm/show_mem.c: In function '__show_mem':

Quoted 1 line
Quoted 1 line

mm/show_mem.c:399:32: warning: unused variable 'mem_alloc_profiling_spinlock' [-Wunused-variable]

     399 |         static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);
Quoted 8 lines

This is the warning

I think you can simply move the definition to the existing #ifdef CONFIG_MEM_ALLOC_PROFILING block above the spin_trylock(). The kernel now uses a new enough C standard to allow this and not only at the beginning of a cuntion. While not encouraged to do that in general, this seems to be a valid use case.

Let me quickly fix both and push v3

Quoted 15 lines
Quoted 14 lines
         |                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

include/linux/spinlock_types.h:43:44: note: in definition of macro 'DEFINE_SPINLOCK'
43 | #define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
| ^

vim +/mem_alloc_profiling_spinlock +399 mm/show_mem.c

   396	
   397	void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
   398	{
Quoted 1 line

399 static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);