diff mbox series

[2/3] mm, page_owner: Add page_owner_stacks file to print out only stacks and their counter

Message ID 20220901044249.4624-3-osalvador@suse.de (mailing list archive)
State New
Headers show
Series page_owner: print stacks and their counter | expand

Commit Message

Oscar Salvador Sept. 1, 2022, 4:42 a.m. UTC
We might be only interested in knowing about stacks <-> count
relationship, so instead of having to fiddle with page_owner
output and screen through pfns, let us add a new file called
'page_owner_stacks' that does just that.
By cating such file, we will get all the stacktraces followed by
its counter (allocated - freed times), so we can have a more specific
overview.

Signed-off-by: Oscar Salvador <osalvador@suse.de>
---
 include/linux/stackdepot.h |  2 ++
 lib/stackdepot.c           | 40 ++++++++++++++++++++++++++++++++++++++
 mm/page_owner.c            | 30 ++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+)

Comments

Ammar Faizi Sept. 1, 2022, 8:16 a.m. UTC | #1
On 9/1/22 11:42 AM, Oscar Salvador wrote:
> +static unsigned long last_stack = 0;

This @last_stack can just be a static local variable in the new
function you wrote, read_page_owner_stacks(), since no other
functions use it.

> +static ssize_t read_page_owner_stacks(struct file *file, char __user *buf,
> +				      size_t count, loff_t *pos)
> +{
> +	char *kbuf;
> +	int ret = 0;
> +
> +	count = min_t(size_t, count, PAGE_SIZE);
> +	kbuf = kmalloc(count, GFP_KERNEL);
> +	if (!kbuf)
> +		return ENOMEM;

Missing a negative sign, return -ENOMEM;

> +	ret += stack_depot_print_stacks_threshold(kbuf, count, pos, &last_stack);
> +	if (copy_to_user(buf, kbuf, ret))
> +		ret = -EFAULT;
> +
> +	if (!ret)
> +		last_stack = 0;
> +
> +	kfree(kbuf);
> +	return ret;
> +}
kernel test robot Sept. 1, 2022, 7:29 p.m. UTC | #2
Hi Oscar,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.0-rc3]
[cannot apply to akpm-mm/mm-everything next-20220901]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Oscar-Salvador/page_owner-print-stacks-and-their-counter/20220901-124408
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git c5e4d5e99162ba8025d58a3af7ad103f155d2df7
config: s390-buildonly-randconfig-r001-20220901
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project c55b41d5199d2394dd6cdb8f52180d8b81d809d4)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install s390 cross compiling tool for clang build
        # apt-get install binutils-s390x-linux-gnu
        # https://github.com/intel-lab-lkp/linux/commit/18d3054fb57a70676be763adab8c8881a1baa504
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Oscar-Salvador/page_owner-print-stacks-and-their-counter/20220901-124408
        git checkout 18d3054fb57a70676be763adab8c8881a1baa504
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=s390 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   In file included from lib/stackdepot.c:34:
   In file included from include/linux/memblock.h:13:
   In file included from arch/s390/include/asm/dma.h:5:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __raw_readb(PCI_IOBASE + addr);
                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:37:59: note: expanded from macro '__le16_to_cpu'
   #define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
                                                             ^
   include/uapi/linux/swab.h:102:54: note: expanded from macro '__swab16'
   #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
                                                        ^
   In file included from lib/stackdepot.c:34:
   In file included from include/linux/memblock.h:13:
   In file included from arch/s390/include/asm/dma.h:5:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:35:59: note: expanded from macro '__le32_to_cpu'
   #define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
                                                             ^
   include/uapi/linux/swab.h:115:54: note: expanded from macro '__swab32'
   #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
                                                        ^
   In file included from lib/stackdepot.c:34:
   In file included from include/linux/memblock.h:13:
   In file included from arch/s390/include/asm/dma.h:5:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writeb(value, PCI_IOBASE + addr);
                               ~~~~~~~~~~ ^
   include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:692:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsb(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:700:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsw(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:708:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsl(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:717:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesb(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
   include/asm-generic/io.h:726:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesw(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
   include/asm-generic/io.h:735:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesl(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
>> lib/stackdepot.c:603:10: error: use of undeclared identifier 'STACK_HASH_SIZE'
           if (i < STACK_HASH_SIZE)
                   ^
   12 warnings and 1 error generated.


vim +/STACK_HASH_SIZE +603 lib/stackdepot.c

   568	
   569	int stack_depot_print_stacks_threshold(char *buf, size_t size, loff_t *pos,
   570					       unsigned long *last_stack)
   571	{
   572		struct stack_record *stack = NULL, *last;
   573		struct stack_record **stacks;
   574		int i = *pos, ret = 0;
   575	
   576		/* Continue from the last week if we have one */
   577		if (*last_stack) {
   578			last = (struct stack_record *)*last_stack;
   579			stack = last->next;
   580		} else {
   581	new_table:
   582			stacks = &stack_table[i];
   583			stack = (struct stack_record *)stacks;
   584		}
   585	
   586		for (; stack; stack = stack->next) {
   587			if (!stack->size || stack->size < 0 ||
   588			    stack->size > size || stack->handle.valid != 1 ||
   589			    refcount_read(&stack->count) < 1)
   590				continue;
   591	
   592			ret += stack_trace_snprint(buf, size, stack->entries, stack->size, 0);
   593			ret += scnprintf(buf + ret, size - ret, "stack count: %d\n\n",
   594					 refcount_read(&stack->count));
   595			*last_stack = (unsigned long)stack;
   596			return ret;
   597		}
   598	
   599		i++;
   600		*pos = i;
   601	
   602		/* Keep looking all tables for valid stacks */
 > 603		if (i < STACK_HASH_SIZE)
kernel test robot Sept. 2, 2022, 12:56 a.m. UTC | #3
Hi Oscar,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.0-rc3]
[cannot apply to akpm-mm/mm-everything next-20220901]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Oscar-Salvador/page_owner-print-stacks-and-their-counter/20220901-124408
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git c5e4d5e99162ba8025d58a3af7ad103f155d2df7
config: arc-randconfig-r043-20220901 (https://download.01.org/0day-ci/archive/20220902/202209020818.s1RIxKyP-lkp@intel.com/config)
compiler: arc-elf-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/18d3054fb57a70676be763adab8c8881a1baa504
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Oscar-Salvador/page_owner-print-stacks-and-their-counter/20220901-124408
        git checkout 18d3054fb57a70676be763adab8c8881a1baa504
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   lib/stackdepot.c: In function 'stack_depot_print_stacks_threshold':
>> lib/stackdepot.c:603:17: error: 'STACK_HASH_SIZE' undeclared (first use in this function); did you mean 'STACK_HASH_SEED'?
     603 |         if (i < STACK_HASH_SIZE)
         |                 ^~~~~~~~~~~~~~~
         |                 STACK_HASH_SEED
   lib/stackdepot.c:603:17: note: each undeclared identifier is reported only once for each function it appears in


vim +603 lib/stackdepot.c

   568	
   569	int stack_depot_print_stacks_threshold(char *buf, size_t size, loff_t *pos,
   570					       unsigned long *last_stack)
   571	{
   572		struct stack_record *stack = NULL, *last;
   573		struct stack_record **stacks;
   574		int i = *pos, ret = 0;
   575	
   576		/* Continue from the last week if we have one */
   577		if (*last_stack) {
   578			last = (struct stack_record *)*last_stack;
   579			stack = last->next;
   580		} else {
   581	new_table:
   582			stacks = &stack_table[i];
   583			stack = (struct stack_record *)stacks;
   584		}
   585	
   586		for (; stack; stack = stack->next) {
   587			if (!stack->size || stack->size < 0 ||
   588			    stack->size > size || stack->handle.valid != 1 ||
   589			    refcount_read(&stack->count) < 1)
   590				continue;
   591	
   592			ret += stack_trace_snprint(buf, size, stack->entries, stack->size, 0);
   593			ret += scnprintf(buf + ret, size - ret, "stack count: %d\n\n",
   594					 refcount_read(&stack->count));
   595			*last_stack = (unsigned long)stack;
   596			return ret;
   597		}
   598	
   599		i++;
   600		*pos = i;
   601	
   602		/* Keep looking all tables for valid stacks */
 > 603		if (i < STACK_HASH_SIZE)
Oscar Salvador Sept. 2, 2022, 3:33 a.m. UTC | #4
On Thu, Sep 01, 2022 at 03:16:44PM +0700, Ammar Faizi wrote:
> On 9/1/22 11:42 AM, Oscar Salvador wrote:
> > +static unsigned long last_stack = 0;
> 
> This @last_stack can just be a static local variable in the new
> function you wrote, read_page_owner_stacks(), since no other
> functions use it.

We could certainly do that.

> 
> > +static ssize_t read_page_owner_stacks(struct file *file, char __user *buf,
> > +				      size_t count, loff_t *pos)
> > +{
> > +	char *kbuf;
> > +	int ret = 0;
> > +
> > +	count = min_t(size_t, count, PAGE_SIZE);
> > +	kbuf = kmalloc(count, GFP_KERNEL);
> > +	if (!kbuf)
> > +		return ENOMEM;
> 
> Missing a negative sign, return -ENOMEM;

Oh yes, I overlooked that.

Thanks!
diff mbox series

Patch

diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h
index 5ee0cf5be88f..20f62039f23a 100644
--- a/include/linux/stackdepot.h
+++ b/include/linux/stackdepot.h
@@ -25,6 +25,8 @@  depot_stack_handle_t __stack_depot_save(unsigned long *entries,
 					gfp_t gfp_flags, bool can_alloc,
 					stack_action_t action);
 void stack_depot_dec_count(depot_stack_handle_t handle);
+int stack_depot_print_stacks_threshold(char *buf, size_t size, loff_t *pos,
+				       unsigned long *last_stack);
 
 /*
  * Every user of stack depot has to call stack_depot_init() during its own init
diff --git a/lib/stackdepot.c b/lib/stackdepot.c
index aeb59d3557e2..3090ae0f3958 100644
--- a/lib/stackdepot.c
+++ b/lib/stackdepot.c
@@ -526,3 +526,43 @@  depot_stack_handle_t stack_depot_save_action(unsigned long *entries,
 	return __stack_depot_save(entries, nr_entries, alloc_flags, true, action);
 }
 EXPORT_SYMBOL_GPL(stack_depot_save_action);
+
+int stack_depot_print_stacks_threshold(char *buf, size_t size, loff_t *pos,
+				       unsigned long *last_stack)
+{
+	struct stack_record *stack = NULL, *last;
+	struct stack_record **stacks;
+	int i = *pos, ret = 0;
+
+	/* Continue from the last week if we have one */
+	if (*last_stack) {
+		last = (struct stack_record *)*last_stack;
+		stack = last->next;
+	} else {
+new_table:
+		stacks = &stack_table[i];
+		stack = (struct stack_record *)stacks;
+	}
+
+	for (; stack; stack = stack->next) {
+		if (!stack->size || stack->size < 0 ||
+		    stack->size > size || stack->handle.valid != 1 ||
+		    refcount_read(&stack->count) < 1)
+			continue;
+
+		ret += stack_trace_snprint(buf, size, stack->entries, stack->size, 0);
+		ret += scnprintf(buf + ret, size - ret, "stack count: %d\n\n",
+				 refcount_read(&stack->count));
+		*last_stack = (unsigned long)stack;
+		return ret;
+	}
+
+	i++;
+	*pos = i;
+
+	/* Keep looking all tables for valid stacks */
+	if (i < STACK_HASH_SIZE)
+		goto new_table;
+
+	return 0;
+}
diff --git a/mm/page_owner.c b/mm/page_owner.c
index 794f346d7520..8c67c7eb2451 100644
--- a/mm/page_owner.c
+++ b/mm/page_owner.c
@@ -43,6 +43,8 @@  static depot_stack_handle_t early_handle;
 
 static void init_early_allocated_pages(void);
 
+static unsigned long last_stack = 0;
+
 static int __init early_page_owner_param(char *buf)
 {
 	int ret = kstrtobool(buf, &page_owner_enabled);
@@ -663,6 +665,32 @@  static void init_early_allocated_pages(void)
 		init_zones_in_node(pgdat);
 }
 
+static ssize_t read_page_owner_stacks(struct file *file, char __user *buf,
+				      size_t count, loff_t *pos)
+{
+	char *kbuf;
+	int ret = 0;
+
+	count = min_t(size_t, count, PAGE_SIZE);
+	kbuf = kmalloc(count, GFP_KERNEL);
+	if (!kbuf)
+		return ENOMEM;
+
+	ret += stack_depot_print_stacks_threshold(kbuf, count, pos, &last_stack);
+	if (copy_to_user(buf, kbuf, ret))
+		ret = -EFAULT;
+
+	if (!ret)
+		last_stack = 0;
+
+	kfree(kbuf);
+	return ret;
+}
+
+static const struct file_operations proc_page_owner_stacks = {
+	.read = read_page_owner_stacks,
+};
+
 static const struct file_operations proc_page_owner_operations = {
 	.read		= read_page_owner,
 };
@@ -676,6 +704,8 @@  static int __init pageowner_init(void)
 
 	debugfs_create_file("page_owner", 0400, NULL, NULL,
 			    &proc_page_owner_operations);
+	debugfs_create_file("page_owner_stacks", 0400, NULL, NULL,
+			    &proc_page_owner_stacks);
 
 	return 0;
 }