Skip to contentSkip to conversation

Helpers for debugging dying cgroups

RFC PATCH In review Yakunin, Dmitry (Nebius) 4 participants 11 messages Sep 11, 2023 – Sep 14, 2023

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

cover Helpers for debugging dying cgroups

This patch series is mostly based on Konstantin's patches which he had sent years ago [1].

This functionality still seems very useful for debugging the difference between entities in cgroupfs and counters in /proc/cgroups, e.g. searching for files that have page cache which prevents destruction of memcg.

I saw the comments in the original thread but didn't understand the Tejun's comment about usage of filehandle instead of ino. Also I saved the original output format in debugfs with extra counters. We can rework this format in the future but now it seems straightforward for just filtering through cmdline utilities.

[1] https://lore.kernel.org/lkml/153414348591.737150.14229960913953276515.stgit@buzz/

Dmitry Yakunin (3):
cgroup: list all subsystem states in debugfs files proc/kpagecgroup: report also inode numbers of offline cgroups tools/mm/page-types: add flag for showing inodes of offline cgroups

 fs/proc/page.c              |  24 ++++++++-
 include/linux/cgroup-defs.h |   1 +
 include/linux/memcontrol.h  |   2 +-
 kernel/cgroup/cgroup.c      | 101 ++++++++++++++++++++++++++++++++++++
 mm/memcontrol.c             |  19 ++++++-
 mm/memory-failure.c         |   2 +-
 tools/mm/page-types.c       |  18 ++++++-
 7 files changed, 159 insertions(+), 8 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 1/3 cgroup: list all subsystem states in debugfs files

After removing cgroup subsystem state could leak or live in background forever because it is pinned by some reference. For example memory cgroup could be pinned by pages in cache or tmpfs.

This patch adds common debugfs interface for listing basic state for each controller. Controller could define callback for dumping own attributes.

In file /sys/kernel/debug/cgroup/<controller> each line shows state in
format: <common_attr>=<value>... [-- <controller_attr>=<value>... ]

Common attributes:

css - css pointer
cgroup - cgroup pointer
id - css id
ino - cgroup inode
flags - css flags
refcnt - css atomic refcount, for online shows huge bias path - cgroup path

This patch adds memcg attributes:

mem_id - 16-bit memory cgroup id
memory - charged pages
memsw - charged memory+swap for v1 and swap for v2 kmem - charged kernel pages
tcpmem - charged tcp pages
shmem - shmem/tmpfs pages

---
 include/linux/cgroup-defs.h |   1 +
 kernel/cgroup/cgroup.c      | 101 ++++++++++++++++++++++++++++++++++++
 mm/memcontrol.c             |  14 +++++
 3 files changed, 116 insertions(+)
include/linux/cgroup-defs.h +1 -0
index 8a0d5466c7be..810bd300cbee 100644
@@ -673,6 +673,7 @@ struct cgroup_subsys {
 	void (*exit)(struct task_struct *task);
 	void (*release)(struct task_struct *task);
 	void (*bind)(struct cgroup_subsys_state *root_css);
+	void (*css_dump)(struct cgroup_subsys_state *css, struct seq_file *m);
 
 	bool early_init:1;
 
kernel/cgroup/cgroup.c +101 -0

110 lines not on this page · raw message

mm/memcontrol.c +14 -0
index 4b27e245a055..7b3d4a10ac63 100644
@@ -5654,6 +5654,20 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu)
 	}
 }
 
+static void mem_cgroup_css_dump(struct cgroup_subsys_state *css,
+				struct seq_file *m)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
+
+	seq_printf(m, "mem_id=%u memory=%lu memsw=%lu kmem=%lu tcpmem=%lu shmem=%lu",
+		   mem_cgroup_id(memcg),
+		   page_counter_read(&memcg->memory),
+		   page_counter_read(&memcg->memsw),
+		   page_counter_read(&memcg->kmem),
+		   page_counter_read(&memcg->tcpmem),
+		   memcg_page_state(memcg, NR_SHMEM));
+}
+
 #ifdef CONFIG_MMU
 /* Handlers for move charge at task migration. */
 static int mem_cgroup_do_precharge(unsigned long count)
Signature · 1 line
2.25.1
#2 lore raw

Re: [RFC PATCH 1/3] cgroup: list all subsystem states in debugfs files

kernel test robot noticed the following build warnings:

Hi Dmitry,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on linus/master v6.6-rc1 next-20230911] [cannot apply to tj-cgroup/for-next]
[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/Yakunin-Dmitry-Nebius/cgroup-list-all-subsystem-states-in-debugfs-files/20230911-155716
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20230911075437.74027-2-zeil%40nebius.com
patch subject: [RFC PATCH 1/3] cgroup: list all subsystem states in debugfs files
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20230911/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230911/[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 >>):

>> mm/memcontrol.c:5669:13: warning: 'mem_cgroup_css_dump' defined but not used [-Wunused-function]
    5669 | static void mem_cgroup_css_dump(struct cgroup_subsys_state *css,
         |             ^~~~~~~~~~~~~~~~~~~
--
>> kernel/cgroup/cgroup.c:7078:7: warning: no previous prototype for 'css_debugfs_seqfile_start' [-Wmissing-prototypes]
    7078 | void *css_debugfs_seqfile_start(struct seq_file *m, loff_t *pos)
         |       ^~~~~~~~~~~~~~~~~~~~~~~~~
>> kernel/cgroup/cgroup.c:7090:7: warning: no previous prototype for 'css_debugfs_seqfile_next' [-Wmissing-prototypes]
    7090 | void *css_debugfs_seqfile_next(struct seq_file *m, void *v, loff_t *pos)
         |       ^~~~~~~~~~~~~~~~~~~~~~~~
>> kernel/cgroup/cgroup.c:7101:6: warning: no previous prototype for 'css_debugfs_seqfile_stop' [-Wmissing-prototypes]
    7101 | void css_debugfs_seqfile_stop(struct seq_file *m, void *v)
         |      ^~~~~~~~~~~~~~~~~~~~~~~~
>> kernel/cgroup/cgroup.c:7106:5: warning: no previous prototype for 'css_debugfs_seqfile_show' [-Wmissing-prototypes]

7106 | int css_debugfs_seqfile_show(struct seq_file *m, void *v)
| ^~~~~~~~~~~~~~~~~~~~~~~~

vim +/mem_cgroup_css_dump +5669 mm/memcontrol.c

5668

> 5669	static void mem_cgroup_css_dump(struct cgroup_subsys_state *css,
  5670					struct seq_file *m)
  5671	{
  5672		struct mem_cgroup *memcg = mem_cgroup_from_css(css);
  5673	
  5674		seq_printf(m, "mem_id=%u memory=%lu memsw=%lu kmem=%lu tcpmem=%lu shmem=%lu",
  5675			   mem_cgroup_id(memcg),
  5676			   page_counter_read(&memcg->memory),
  5677			   page_counter_read(&memcg->memsw),
  5678			   page_counter_read(&memcg->kmem),
  5679			   page_counter_read(&memcg->tcpmem),
  5680			   memcg_page_state(memcg, NR_SHMEM));

5681 }
5682

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw

Re: [RFC PATCH 1/3] cgroup: list all subsystem states in debugfs files

In reply to Yakunin, Dmitry (Nebius)
kernel test robot noticed the following build warnings:

Hi Dmitry,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on linus/master v6.6-rc1 next-20230911] [cannot apply to tj-cgroup/for-next]
[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/Yakunin-Dmitry-Nebius/cgroup-list-all-subsystem-states-in-debugfs-files/20230911-155716
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20230911075437.74027-2-zeil%40nebius.com
patch subject: [RFC PATCH 1/3] cgroup: list all subsystem states in debugfs files
config: hexagon-randconfig-002-20230911 (https://download.01.org/0day-ci/archive/20230911/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230911/[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 kernel/cgroup/cgroup.c:31:
In file included from kernel/cgroup/cgroup-internal.h:5:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:337:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]

     547 |         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]
     560 |         val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
      37 | #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
         |                                                   ^
   In file included from kernel/cgroup/cgroup.c:31:
   In file included from kernel/cgroup/cgroup-internal.h:5:
   In file included from include/linux/cgroup.h:26:
   In file included from include/linux/kernel_stat.h:9:
   In file included from include/linux/interrupt.h:11:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:337:
   include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     573 |         val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
      35 | #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
         |                                                   ^
   In file included from kernel/cgroup/cgroup.c:31:
   In file included from kernel/cgroup/cgroup-internal.h:5:
   In file included from include/linux/cgroup.h:26:
   In file included from include/linux/kernel_stat.h:9:
   In file included from include/linux/interrupt.h:11:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:337:
   include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     584 |         __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]
     594 |         __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]
     604 |         __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
>> kernel/cgroup/cgroup.c:7078:7: warning: no previous prototype for function 'css_debugfs_seqfile_start' [-Wmissing-prototypes]
    7078 | void *css_debugfs_seqfile_start(struct seq_file *m, loff_t *pos)
         |       ^
   kernel/cgroup/cgroup.c:7078:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
    7078 | void *css_debugfs_seqfile_start(struct seq_file *m, loff_t *pos)
         | ^
         | static 
>> kernel/cgroup/cgroup.c:7090:7: warning: no previous prototype for function 'css_debugfs_seqfile_next' [-Wmissing-prototypes]
    7090 | void *css_debugfs_seqfile_next(struct seq_file *m, void *v, loff_t *pos)
         |       ^
   kernel/cgroup/cgroup.c:7090:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
    7090 | void *css_debugfs_seqfile_next(struct seq_file *m, void *v, loff_t *pos)
         | ^
         | static 
>> kernel/cgroup/cgroup.c:7101:6: warning: no previous prototype for function 'css_debugfs_seqfile_stop' [-Wmissing-prototypes]
    7101 | void css_debugfs_seqfile_stop(struct seq_file *m, void *v)
         |      ^
   kernel/cgroup/cgroup.c:7101:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
    7101 | void css_debugfs_seqfile_stop(struct seq_file *m, void *v)
         | ^
         | static 
>> kernel/cgroup/cgroup.c:7106:5: warning: no previous prototype for function 'css_debugfs_seqfile_show' [-Wmissing-prototypes]
    7106 | int css_debugfs_seqfile_show(struct seq_file *m, void *v)
         |     ^
   kernel/cgroup/cgroup.c:7106:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
    7106 | int css_debugfs_seqfile_show(struct seq_file *m, void *v)
         | ^
         | static 

10 warnings generated.

vim +/css_debugfs_seqfile_start +7078 kernel/cgroup/cgroup.c

7076
7077 #ifdef CONFIG_DEBUG_FS

> 7078	void *css_debugfs_seqfile_start(struct seq_file *m, loff_t *pos)
  7079	{
  7080		struct cgroup_subsys *ss = m->private;
  7081		struct cgroup_subsys_state *css;
  7082		int id = *pos;
  7083	
  7084		rcu_read_lock();
  7085		css = idr_get_next(&ss->css_idr, &id);
  7086		*pos = id;
  7087		return css;
  7088	}
  7089	
> 7090	void *css_debugfs_seqfile_next(struct seq_file *m, void *v, loff_t *pos)
  7091	{
  7092		struct cgroup_subsys *ss = m->private;
  7093		struct cgroup_subsys_state *css;
  7094		int id = *pos + 1;
  7095	
  7096		css = idr_get_next(&ss->css_idr, &id);
  7097		*pos = id;
  7098		return css;
  7099	}
  7100	
> 7101	void css_debugfs_seqfile_stop(struct seq_file *m, void *v)
  7102	{
  7103		rcu_read_unlock();
  7104	}
  7105	
> 7106	int css_debugfs_seqfile_show(struct seq_file *m, void *v)

7107 {
7108 struct cgroup_subsys *ss = m->private;
7109 struct cgroup_subsys_state *css = v;
7110 /* data is NULL for root cgroup_subsys_state */
7111 struct percpu_ref_data *data = css->refcnt.data;
7112 size_t buflen;
7113 char *buf;
7114 int len;
7115
7116 seq_printf(m, "css=%pK cgroup=%pK id=%d ino=%lu flags=%#x refcnt=%lu path=",
7117 css, css->cgroup, css->id, cgroup_ino(css->cgroup),
7118 css->flags, data ? atomic_long_read(&data->count) : 0);
7119
7120 buflen = seq_get_buf(m, &buf);
7121 if (buf) {
7122 len = cgroup_path(css->cgroup, buf, buflen);
7123 seq_commit(m, len < buflen ? len : -1);
7124 }
7125
7126 if (ss->css_dump) {
7127 seq_puts(m, " -- ");
7128 ss->css_dump(css, m);
7129 }
7130
7131 seq_putc(m, '\n');
7132 return 0;
7133 }
7134

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

Re: [RFC PATCH 1/3] cgroup: list all subsystem states in debugfs files

In reply to Yakunin, Dmitry (Nebius)

mm/memcontrol.c static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) · line 5669

Quoted 13 lines · Dmitry (Nebius)
mm/memcontrol.c +13 -0
+static void mem_cgroup_css_dump(struct cgroup_subsys_state *css,
+				struct seq_file *m)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
+
+	seq_printf(m, "mem_id=%u memory=%lu memsw=%lu kmem=%lu tcpmem=%lu shmem=%lu",
+		   mem_cgroup_id(memcg),
+		   page_counter_read(&memcg->memory),
+		   page_counter_read(&memcg->memsw),
+		   page_counter_read(&memcg->kmem),
+		   page_counter_read(&memcg->tcpmem),
+		   memcg_page_state(memcg, NR_SHMEM));
+}

Can you please take a look at drgn (https://github.com/osandov/drgn) and see whether that satifies your needs? We can easily add drgn scripts under tools directory too (e.g. iocost already does that).

Thanks.

Signature · 1 line
tejun
#5 lore raw

Re: [RFC PATCH 1/3] cgroup: list all subsystem states in debugfs files

Thank you for pointing out to drgn scripts in kernel source tree, didn't know about them, I will take a look.

Quoted 22 lines · On 11.09.2023 20:55, [email protected] wrote:

mm/memcontrol.c static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) · line 5669

Quoted 13 lines · Dmitry (Nebius)
mm/memcontrol.c +13 -0
+static void mem_cgroup_css_dump(struct cgroup_subsys_state *css,
+                             struct seq_file *m)
+{
+     struct mem_cgroup *memcg = mem_cgroup_from_css(css);
+
+     seq_printf(m, "mem_id=%u memory=%lu memsw=%lu kmem=%lu tcpmem=%lu shmem=%lu",
+                mem_cgroup_id(memcg),
+                page_counter_read(&memcg->memory),
+                page_counter_read(&memcg->memsw),
+                page_counter_read(&memcg->kmem),
+                page_counter_read(&memcg->tcpmem),
+                memcg_page_state(memcg, NR_SHMEM));
+}

Can you please take a look at drgn (https://github.com/osandov/drgn) and see whether that satifies your needs? We can easily add drgn scripts under tools directory too (e.g. iocost already does that).

Thanks.

--
tejun

#6 lore raw

Re: [RFC PATCH 1/3] cgroup: list all subsystem states in debugfs files

In reply to Yakunin, Dmitry (Nebius)
Quoted 30 lines · Dmitry (Nebius)

After removing cgroup subsystem state could leak or live in background forever because it is pinned by some reference. For example memory cgroup could be pinned by pages in cache or tmpfs.

This patch adds common debugfs interface for listing basic state for each controller. Controller could define callback for dumping own attributes.

In file /sys/kernel/debug/cgroup/<controller> each line shows state in
format: <common_attr>=<value>... [-- <controller_attr>=<value>... ]

Common attributes:

css - css pointer
cgroup - cgroup pointer
id - css id
ino - cgroup inode
flags - css flags
refcnt - css atomic refcount, for online shows huge bias path - cgroup path

This patch adds memcg attributes:

mem_id - 16-bit memory cgroup id
memory - charged pages
memsw - charged memory+swap for v1 and swap for v2 kmem - charged kernel pages
tcpmem - charged tcp pages
shmem - shmem/tmpfs pages

Quoted 4 lines

FWIW, I was just recently working on a debugfs directly that exposes a list of all zombie memcgs as well as the "memory.stat" output for all of them.

This entails a file at /sys/kernel/debug/zombie_memcgs/all that contains a list of zombie memcgs (with indentation to reflect the hierarchy) and an id for each of them.

This id can be used to index per-memcg directories at /sys/kernel/debug/zombie_memcgs/<id>/, which include debug files. The only one we have so far is /sys/kernel/debug/zombie_memcgs/<id>/memory.stat.

If there is interest in this, I can share more information.

Quoted 163 lines
---
 include/linux/cgroup-defs.h |   1 +
 kernel/cgroup/cgroup.c      | 101 ++++++++++++++++++++++++++++++++++++
 mm/memcontrol.c             |  14 +++++
 3 files changed, 116 insertions(+)
include/linux/cgroup-defs.h +1 -0
index 8a0d5466c7be..810bd300cbee 100644
@@ -673,6 +673,7 @@ struct cgroup_subsys {
        void (*exit)(struct task_struct *task);
        void (*release)(struct task_struct *task);
        void (*bind)(struct cgroup_subsys_state *root_css);
+       void (*css_dump)(struct cgroup_subsys_state *css, struct seq_file *m);
 
        bool early_init:1;
 
kernel/cgroup/cgroup.c +101 -0
index 625d7483951c..fb9931ff7570 100644
@@ -40,6 +40,7 @@
 #include <linux/mount.h>
 #include <linux/pagemap.h>
 #include <linux/proc_fs.h>
+#include <linux/debugfs.h>
 #include <linux/rcupdate.h>
 #include <linux/sched.h>
 #include <linux/sched/task.h>
@@ -7068,3 +7069,103 @@ static int __init cgroup_sysfs_init(void)
 subsys_initcall(cgroup_sysfs_init);
 
 #endif /* CONFIG_SYSFS */
+
+#ifdef CONFIG_DEBUG_FS
+void *css_debugfs_seqfile_start(struct seq_file *m, loff_t *pos)
+{
+       struct cgroup_subsys *ss = m->private;
+       struct cgroup_subsys_state *css;
+       int id = *pos;
+
+       rcu_read_lock();
+       css = idr_get_next(&ss->css_idr, &id);
+       *pos = id;
+       return css;
+}
+
+void *css_debugfs_seqfile_next(struct seq_file *m, void *v, loff_t *pos)
+{
+       struct cgroup_subsys *ss = m->private;
+       struct cgroup_subsys_state *css;
+       int id = *pos + 1;
+
+       css = idr_get_next(&ss->css_idr, &id);
+       *pos = id;
+       return css;
+}
+
+void css_debugfs_seqfile_stop(struct seq_file *m, void *v)
+{
+       rcu_read_unlock();
+}
+
+int css_debugfs_seqfile_show(struct seq_file *m, void *v)
+{
+       struct cgroup_subsys *ss = m->private;
+       struct cgroup_subsys_state *css = v;
+       /* data is NULL for root cgroup_subsys_state */
+       struct percpu_ref_data *data = css->refcnt.data;
+       size_t buflen;
+       char *buf;
+       int len;
+
+       seq_printf(m, "css=%pK cgroup=%pK id=%d ino=%lu flags=%#x refcnt=%lu path=",
+                  css, css->cgroup, css->id, cgroup_ino(css->cgroup),
+                  css->flags, data ? atomic_long_read(&data->count) : 0);
+
+       buflen = seq_get_buf(m, &buf);
+       if (buf) {
+               len = cgroup_path(css->cgroup, buf, buflen);
+               seq_commit(m, len < buflen ? len : -1);
+       }
+
+       if (ss->css_dump) {
+               seq_puts(m, " -- ");
+               ss->css_dump(css, m);
+       }
+
+       seq_putc(m, '\n');
+       return 0;
+}
+
+static const struct seq_operations css_debug_seq_ops = {
+       .start = css_debugfs_seqfile_start,
+       .next = css_debugfs_seqfile_next,
+       .stop = css_debugfs_seqfile_stop,
+       .show = css_debugfs_seqfile_show,
+};
+
+static int css_debugfs_open(struct inode *inode, struct file *file)
+{
+       int ret = seq_open(file, &css_debug_seq_ops);
+       struct seq_file *m = file->private_data;
+
+       if (!ret)
+               m->private = inode->i_private;
+       return ret;
+}
+
+static const struct file_operations css_debugfs_fops = {
+       .open = css_debugfs_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = seq_release,
+};
+
+static int __init css_debugfs_init(void)
+{
+       struct cgroup_subsys *ss;
+       struct dentry *dir;
+       int ssid;
+
+       dir = debugfs_create_dir("cgroup", NULL);
+       if (dir) {
+               for_each_subsys(ss, ssid)
+                       debugfs_create_file(ss->name, 0644, dir, ss,
+                                           &css_debugfs_fops);
+       }
+
+       return 0;
+}
+late_initcall(css_debugfs_init);
+#endif /* CONFIG_DEBUG_FS */
mm/memcontrol.c +14 -1
index 4b27e245a055..7b3d4a10ac63 100644
@@ -5654,6 +5654,20 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu)
        }
 }
 
+static void mem_cgroup_css_dump(struct cgroup_subsys_state *css,
+                               struct seq_file *m)
+{
+       struct mem_cgroup *memcg = mem_cgroup_from_css(css);
+
+       seq_printf(m, "mem_id=%u memory=%lu memsw=%lu kmem=%lu tcpmem=%lu shmem=%lu",
+                  mem_cgroup_id(memcg),
+                  page_counter_read(&memcg->memory),
+                  page_counter_read(&memcg->memsw),
+                  page_counter_read(&memcg->kmem),
+                  page_counter_read(&memcg->tcpmem),
+                  memcg_page_state(memcg, NR_SHMEM));
+}
+
 #ifdef CONFIG_MMU
 /* Handlers for move charge at task migration. */
 static int mem_cgroup_do_precharge(unsigned long count)
--

2.25.1

#7 lore raw

Re: [RFC PATCH 1/3] cgroup: list all subsystem states in debugfs files

Sure, if you can share you work it would be nice. Thank you.

Quoted 213 lines · Yosry Ahmed
Quoted 29 lines · Dmitry (Nebius)

After removing cgroup subsystem state could leak or live in background forever because it is pinned by some reference. For example memory cgroup could be pinned by pages in cache or tmpfs.

This patch adds common debugfs interface for listing basic state for each controller. Controller could define callback for dumping own attributes.

In file /sys/kernel/debug/cgroup/<controller> each line shows state in
format: <common_attr>=<value>... [-- <controller_attr>=<value>... ]

Common attributes:

css - css pointer
cgroup - cgroup pointer
id - css id
ino - cgroup inode
flags - css flags
refcnt - css atomic refcount, for online shows huge bias path - cgroup path

This patch adds memcg attributes:

mem_id - 16-bit memory cgroup id
memory - charged pages
memsw - charged memory+swap for v1 and swap for v2 kmem - charged kernel pages
tcpmem - charged tcp pages
shmem - shmem/tmpfs pages

Quoted 4 lines

FWIW, I was just recently working on a debugfs directly that exposes a list of all zombie memcgs as well as the "memory.stat" output for all of them.

This entails a file at /sys/kernel/debug/zombie_memcgs/all that contains a list of zombie memcgs (with indentation to reflect the hierarchy) and an id for each of them.

This id can be used to index per-memcg directories at /sys/kernel/debug/zombie_memcgs/<id>/, which include debug files. The only one we have so far is /sys/kernel/debug/zombie_memcgs/<id>/memory.stat.

If there is interest in this, I can share more information.

Quoted 163 lines
---
  include/linux/cgroup-defs.h |   1 +
  kernel/cgroup/cgroup.c      | 101 ++++++++++++++++++++++++++++++++++++
  mm/memcontrol.c             |  14 +++++
  3 files changed, 116 insertions(+)
include/linux/cgroup-defs.h +1 -0
index 8a0d5466c7be..810bd300cbee 100644
@@ -673,6 +673,7 @@ struct cgroup_subsys {
         void (*exit)(struct task_struct *task);
         void (*release)(struct task_struct *task);
         void (*bind)(struct cgroup_subsys_state *root_css);
+       void (*css_dump)(struct cgroup_subsys_state *css, struct seq_file *m);
 
         bool early_init:1;
 
kernel/cgroup/cgroup.c +101 -0
index 625d7483951c..fb9931ff7570 100644
@@ -40,6 +40,7 @@
  #include <linux/mount.h>
  #include <linux/pagemap.h>
  #include <linux/proc_fs.h>
+#include <linux/debugfs.h>
  #include <linux/rcupdate.h>
  #include <linux/sched.h>
  #include <linux/sched/task.h>
@@ -7068,3 +7069,103 @@ static int __init cgroup_sysfs_init(void)
  subsys_initcall(cgroup_sysfs_init);
 
  #endif /* CONFIG_SYSFS */
+
+#ifdef CONFIG_DEBUG_FS
+void *css_debugfs_seqfile_start(struct seq_file *m, loff_t *pos)
+{
+       struct cgroup_subsys *ss = m->private;
+       struct cgroup_subsys_state *css;
+       int id = *pos;
+
+       rcu_read_lock();
+       css = idr_get_next(&ss->css_idr, &id);
+       *pos = id;
+       return css;
+}
+
+void *css_debugfs_seqfile_next(struct seq_file *m, void *v, loff_t *pos)
+{
+       struct cgroup_subsys *ss = m->private;
+       struct cgroup_subsys_state *css;
+       int id = *pos + 1;
+
+       css = idr_get_next(&ss->css_idr, &id);
+       *pos = id;
+       return css;
+}
+
+void css_debugfs_seqfile_stop(struct seq_file *m, void *v)
+{
+       rcu_read_unlock();
+}
+
+int css_debugfs_seqfile_show(struct seq_file *m, void *v)
+{
+       struct cgroup_subsys *ss = m->private;
+       struct cgroup_subsys_state *css = v;
+       /* data is NULL for root cgroup_subsys_state */
+       struct percpu_ref_data *data = css->refcnt.data;
+       size_t buflen;
+       char *buf;
+       int len;
+
+       seq_printf(m, "css=%pK cgroup=%pK id=%d ino=%lu flags=%#x refcnt=%lu path=",
+                  css, css->cgroup, css->id, cgroup_ino(css->cgroup),
+                  css->flags, data ? atomic_long_read(&data->count) : 0);
+
+       buflen = seq_get_buf(m, &buf);
+       if (buf) {
+               len = cgroup_path(css->cgroup, buf, buflen);
+               seq_commit(m, len < buflen ? len : -1);
+       }
+
+       if (ss->css_dump) {
+               seq_puts(m, " -- ");
+               ss->css_dump(css, m);
+       }
+
+       seq_putc(m, '\n');
+       return 0;
+}
+
+static const struct seq_operations css_debug_seq_ops = {
+       .start = css_debugfs_seqfile_start,
+       .next = css_debugfs_seqfile_next,
+       .stop = css_debugfs_seqfile_stop,
+       .show = css_debugfs_seqfile_show,
+};
+
+static int css_debugfs_open(struct inode *inode, struct file *file)
+{
+       int ret = seq_open(file, &css_debug_seq_ops);
+       struct seq_file *m = file->private_data;
+
+       if (!ret)
+               m->private = inode->i_private;
+       return ret;
+}
+
+static const struct file_operations css_debugfs_fops = {
+       .open = css_debugfs_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = seq_release,
+};
+
+static int __init css_debugfs_init(void)
+{
+       struct cgroup_subsys *ss;
+       struct dentry *dir;
+       int ssid;
+
+       dir = debugfs_create_dir("cgroup", NULL);
+       if (dir) {
+               for_each_subsys(ss, ssid)
+                       debugfs_create_file(ss->name, 0644, dir, ss,
+                                           &css_debugfs_fops);
+       }
+
+       return 0;
+}
+late_initcall(css_debugfs_init);
+#endif /* CONFIG_DEBUG_FS */
mm/memcontrol.c +14 -1
index 4b27e245a055..7b3d4a10ac63 100644
@@ -5654,6 +5654,20 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu)
         }
  }
 
+static void mem_cgroup_css_dump(struct cgroup_subsys_state *css,
+                               struct seq_file *m)
+{
+       struct mem_cgroup *memcg = mem_cgroup_from_css(css);
+
+       seq_printf(m, "mem_id=%u memory=%lu memsw=%lu kmem=%lu tcpmem=%lu shmem=%lu",
+                  mem_cgroup_id(memcg),
+                  page_counter_read(&memcg->memory),
+                  page_counter_read(&memcg->memsw),
+                  page_counter_read(&memcg->kmem),
+                  page_counter_read(&memcg->tcpmem),
+                  memcg_page_state(memcg, NR_SHMEM));
+}
+
  #ifdef CONFIG_MMU
  /* Handlers for move charge at task migration. */
  static int mem_cgroup_do_precharge(unsigned long count)
--

2.25.1

#8 lore raw

Re: [RFC PATCH 1/3] cgroup: list all subsystem states in debugfs files

In reply to Yakunin, Dmitry (Nebius)
kernel test robot noticed the following build warnings:

Hi Dmitry,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on linus/master v6.6-rc1 next-20230913] [cannot apply to tj-cgroup/for-next]
[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/Yakunin-Dmitry-Nebius/cgroup-list-all-subsystem-states-in-debugfs-files/20230911-155716
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20230911075437.74027-2-zeil%40nebius.com
patch subject: [RFC PATCH 1/3] cgroup: list all subsystem states in debugfs files
config: i386-randconfig-063-20230914 (https://download.01.org/0day-ci/archive/20230914/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230914/[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]/

sparse warnings: (new ones prefixed by >>)
kernel/cgroup/cgroup.c:6619:41: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/cgroup/cgroup.c:6619:41: sparse: expected struct spinlock [usertype] *lock
kernel/cgroup/cgroup.c:6619:41: sparse: got struct spinlock [noderef] __rcu *
kernel/cgroup/cgroup.c:6622:43: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/cgroup/cgroup.c:6622:43: sparse: expected struct spinlock [usertype] *lock
kernel/cgroup/cgroup.c:6622:43: sparse: got struct spinlock [noderef] __rcu *

>> kernel/cgroup/cgroup.c:7078:6: sparse: sparse: symbol 'css_debugfs_seqfile_start' was not declared. Should it be static?
>> kernel/cgroup/cgroup.c:7090:6: sparse: sparse: symbol 'css_debugfs_seqfile_next' was not declared. Should it be static?
>> kernel/cgroup/cgroup.c:7101:6: sparse: sparse: symbol 'css_debugfs_seqfile_stop' was not declared. Should it be static?
>> kernel/cgroup/cgroup.c:7106:5: sparse: sparse: symbol 'css_debugfs_seqfile_show' was not declared. Should it be static?

kernel/cgroup/cgroup.c:3064:9: sparse: sparse: context imbalance in 'cgroup_lock_and_drain_offline' - wrong count at exit kernel/cgroup/cgroup.c:6386:12: sparse: sparse: context imbalance in 'cgroup_css_set_fork' - wrong count at exit kernel/cgroup/cgroup.c:6502:9: sparse: sparse: context imbalance in 'cgroup_css_set_put_fork' - wrong count at exit kernel/cgroup/cgroup.c:6522:5: sparse: sparse: context imbalance in 'cgroup_can_fork' - wrong count at exit kernel/cgroup/cgroup.c:6571:9: sparse: sparse: context imbalance in 'cgroup_cancel_fork' - unexpected unlock kernel/cgroup/cgroup.c:7084:22: sparse: sparse: context imbalance in 'css_debugfs_seqfile_start' - wrong count at exit kernel/cgroup/cgroup.c: note: in included file (through include/linux/rculist.h, include/linux/pid.h, include/linux/sched.h, ...):
include/linux/rcupdate.h:780:9: sparse: sparse: context imbalance in 'css_debugfs_seqfile_stop' - unexpected unlock

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#9 lore raw

RFC PATCH 2/3 proc/kpagecgroup: report also inode numbers of offline cgroups

In reply to Yakunin, Dmitry (Nebius)

By default this interface reports inode number of closest online ancestor if cgroups is offline (removed). Information about real owner is required for detecting which pages keep removed cgroup.

This patch adds per-file mode which is changed by writing 64-bit flags into opened /proc/kpagecgroup. For now only first bit is used.

---
 fs/proc/page.c             | 24 ++++++++++++++++++++++--
 include/linux/memcontrol.h |  2 +-
 mm/memcontrol.c            |  5 +++--
 mm/memory-failure.c        |  2 +-
 4 files changed, 27 insertions(+), 6 deletions(-)
fs/proc/page.c +22 -2
index 195b077c0fac..ae6feca2bbc7 100644
@@ -278,6 +278,7 @@ static const struct proc_ops kpageflags_proc_ops = {
 static ssize_t kpagecgroup_read(struct file *file, char __user *buf,
 				size_t count, loff_t *ppos)
 {
+	unsigned long flags = (unsigned long)file->private_data;
 	const unsigned long max_dump_pfn = get_max_dump_pfn();
 	u64 __user *out = (u64 __user *)buf;
 	struct page *ppage;
@@ -301,7 +302,7 @@ static ssize_t kpagecgroup_read(struct file *file, char __user *buf,
 		ppage = pfn_to_online_page(pfn);
 
 		if (ppage)
-			ino = page_cgroup_ino(ppage);
+			ino = page_cgroup_ino(ppage, !(flags & 1));
 		else
 			ino = 0;
 
@@ -323,10 +324,29 @@ static ssize_t kpagecgroup_read(struct file *file, char __user *buf,
 	return ret;
 }
 
+static ssize_t kpagecgroup_write(struct file *file, const char __user *buf,
+				 size_t count, loff_t *ppos)
+{
+	u64 flags;
+
+	if (count != 8)
+		return -EINVAL;
+
+	if (get_user(flags, buf))
+		return -EFAULT;
+
+	if (flags > 1)
+		return -EINVAL;
+
+	file->private_data = (void *)(unsigned long)flags;
+	return count;
+}
+
 static const struct proc_ops kpagecgroup_proc_ops = {
 	.proc_flags	= PROC_ENTRY_PERMANENT,
 	.proc_lseek	= mem_lseek,
 	.proc_read	= kpagecgroup_read,
+	.proc_write	= kpagecgroup_write,
 };
 #endif /* CONFIG_MEMCG */
 
@@ -335,7 +355,7 @@ static int __init proc_page_init(void)
 	proc_create("kpagecount", S_IRUSR, NULL, &kpagecount_proc_ops);
 	proc_create("kpageflags", S_IRUSR, NULL, &kpageflags_proc_ops);
 #ifdef CONFIG_MEMCG
-	proc_create("kpagecgroup", S_IRUSR, NULL, &kpagecgroup_proc_ops);
+	proc_create("kpagecgroup", 0600, NULL, &kpagecgroup_proc_ops);
 #endif
 	return 0;
 }
include/linux/memcontrol.h +1 -1
index 222d7370134c..bbbddaa260d3 100644
@@ -892,7 +892,7 @@ static inline bool mm_match_cgroup(struct mm_struct *mm,
 }
 
 struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio);
-ino_t page_cgroup_ino(struct page *page);
+ino_t page_cgroup_ino(struct page *page, bool online);
 
 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
 {
mm/memcontrol.c +3 -2
index 7b3d4a10ac63..48cfe3695e06 100644
@@ -380,6 +380,7 @@ struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio)
 /**
  * page_cgroup_ino - return inode number of the memcg a page is charged to
  * @page: the page
+ * @online: return closest online ancestor
  *
  * Look up the closest online ancestor of the memory cgroup @page is charged to
  * and return its inode number or 0 if @page is not charged to any cgroup. It
@@ -390,7 +391,7 @@ struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio)
  * after page_cgroup_ino() returns, so it only should be used by callers that
  * do not care (such as procfs interfaces).
  */
-ino_t page_cgroup_ino(struct page *page)
+ino_t page_cgroup_ino(struct page *page, bool online)
 {
 	struct mem_cgroup *memcg;
 	unsigned long ino = 0;
@@ -399,7 +400,7 @@ ino_t page_cgroup_ino(struct page *page)
 	/* page_folio() is racy here, but the entire function is racy anyway */
 	memcg = folio_memcg_check(page_folio(page));
 
-	while (memcg && !(memcg->css.flags & CSS_ONLINE))
+	while (memcg && online && !(memcg->css.flags & CSS_ONLINE))
 		memcg = parent_mem_cgroup(memcg);
 	if (memcg)
 		ino = cgroup_ino(memcg->css.cgroup);
mm/memory-failure.c +1 -1
index 5b663eca1f29..6734489b2435 100644
@@ -267,7 +267,7 @@ static int hwpoison_filter_task(struct page *p)
 	if (!hwpoison_filter_memcg)
 		return 0;
 
-	if (page_cgroup_ino(p) != hwpoison_filter_memcg)
+	if (page_cgroup_ino(p, true) != hwpoison_filter_memcg)
 		return -EINVAL;
 
 	return 0;
Signature · 1 line
2.25.1
#10 lore raw

RFC PATCH 3/3 tools/mm/page-types: add flag for showing inodes of offline cgroups

In reply to Yakunin, Dmitry (Nebius)

With flag -R|--real-cgroup page-types will report real owner.

---
 tools/mm/page-types.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
tools/mm/page-types.c +16 -2
index 8d5595b6c59f..f26035b362d2 100644
@@ -161,6 +161,7 @@ static pid_t		opt_pid;	/* process to walk */
 const char		*opt_file;	/* file or directory path */
 static uint64_t		opt_cgroup;	/* cgroup inode */
 static int		opt_list_cgroup;/* list page cgroup */
+static int		opt_real_cgroup;/* list page cgroup */
 static int		opt_list_mapcnt;/* list page map count */
 static const char	*opt_kpageflags;/* kpageflags file to parse */
 
@@ -837,6 +838,7 @@ static void usage(void)
 "            -l|--list                  Show page details in ranges\n"
 "            -L|--list-each             Show page details one by one\n"
 "            -C|--list-cgroup           Show cgroup inode for pages\n"
+"            -R|--real-cgroup           Show real offline cgroups\n"
 "            -M|--list-mapcnt           Show page map count\n"
 "            -N|--no-summary            Don't show summary info\n"
 "            -X|--hwpoison              hwpoison pages\n"
@@ -1257,6 +1259,7 @@ static const struct option opts[] = {
 	{ "list"      , 0, NULL, 'l' },
 	{ "list-each" , 0, NULL, 'L' },
 	{ "list-cgroup", 0, NULL, 'C' },
+	{ "real-cgroup", 0, NULL, 'R' },
 	{ "list-mapcnt", 0, NULL, 'M' },
 	{ "no-summary", 0, NULL, 'N' },
 	{ "hwpoison"  , 0, NULL, 'X' },
@@ -1273,7 +1276,7 @@ int main(int argc, char *argv[])
 	page_size = getpagesize();
 
 	while ((c = getopt_long(argc, argv,
-				"rp:f:a:b:d:c:CilLMNXxF:h",
+				"rp:f:a:b:d:c:CRilLMNXxF:h",
 				opts, NULL)) != -1) {
 		switch (c) {
 		case 'r':
@@ -1297,6 +1300,9 @@ int main(int argc, char *argv[])
 		case 'C':
 			opt_list_cgroup = 1;
 			break;
+		case 'R':
+			opt_real_cgroup = 1;
+			break;
 		case 'd':
 			describe_flags(optarg);
 			exit(0);
@@ -1338,7 +1344,15 @@ int main(int argc, char *argv[])
 	if (!opt_kpageflags)
 		opt_kpageflags = PROC_KPAGEFLAGS;
 
-	if (opt_cgroup || opt_list_cgroup)
+	if (opt_real_cgroup) {
+		uint64_t flags = 1;
+
+		kpagecgroup_fd = checked_open(PROC_KPAGECGROUP, O_RDWR);
+		if (write(kpagecgroup_fd, &flags, sizeof(flags)) < 0) {
+			perror(PROC_KPAGECGROUP);
+			exit(EXIT_FAILURE);
+		}
+	} else if (opt_cgroup || opt_list_cgroup)
 		kpagecgroup_fd = checked_open(PROC_KPAGECGROUP, O_RDONLY);
 
 	if (opt_list && opt_list_mapcnt)
Signature · 1 line
2.25.1