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

Yueyang Pan posted 2 patches 1 month ago
There is a newer version of this series
[PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info
Posted by Yueyang Pan 1 month ago
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.

Signed-off-by: Yueyang Pan <pyyjason@gmail.com>
---
 mm/show_mem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mm/show_mem.c b/mm/show_mem.c
index 51892ce2efc4..4c876ea2b66f 100644
--- a/mm/show_mem.c
+++ b/mm/show_mem.c
@@ -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.47.3
Re: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info
Posted by kernel test robot 1 month ago
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/202509031744.HcibSETe-lkp@intel.com/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/202509031744.HcibSETe-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202509031744.HcibSETe-lkp@intel.com/

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);

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info
Posted by Vlastimil Babka 4 weeks, 1 day ago
On 9/3/25 11:47, kernel test robot wrote:
> 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/202509031744.HcibSETe-lkp@intel.com/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/202509031744.HcibSETe-lkp@intel.com/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 <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202509031744.HcibSETe-lkp@intel.com/
> 
> 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.

>    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);

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.


>          |                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
>    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);
>
Re: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info
Posted by Yueyang Pan 4 weeks, 1 day ago
On Wed, Sep 03, 2025 at 12:16:45PM +0200, Vlastimil Babka wrote:
> On 9/3/25 11:47, kernel test robot wrote:
> > 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/202509031744.HcibSETe-lkp@intel.com/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/202509031744.HcibSETe-lkp@intel.com/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 <lkp@intel.com>
> > | Closes: https://lore.kernel.org/oe-kbuild-all/202509031744.HcibSETe-lkp@intel.com/
> > 
> > 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.

Thanks. Understood.

> 
> >    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);
> 
> 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

> 
> >          |                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >    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);
> > 
>
Re: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info
Posted by Usama Arif 1 month ago

On 02/09/2025 16:57, Yueyang Pan wrote:
> 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.
> 
> Signed-off-by: Yueyang Pan <pyyjason@gmail.com>

Acked-by: Usama Arif <usamaarif642@gmail.com>

> ---
>  mm/show_mem.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/mm/show_mem.c b/mm/show_mem.c
> index 51892ce2efc4..4c876ea2b66f 100644
> --- a/mm/show_mem.c
> +++ b/mm/show_mem.c
> @@ -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
>  }
Re: [PATCH v2 2/2] mm/show_mem: Add trylock while printing alloc info
Posted by Vlastimil Babka 1 month ago
On 9/2/25 17:57, Yueyang Pan wrote:
> 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.
> 
> Signed-off-by: Yueyang Pan <pyyjason@gmail.com>

Acked-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>  mm/show_mem.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/mm/show_mem.c b/mm/show_mem.c
> index 51892ce2efc4..4c876ea2b66f 100644
> --- a/mm/show_mem.c
> +++ b/mm/show_mem.c
> @@ -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
>  }