[PATCH v5 22/32] mm/memcontrol.c: Convert to printbuf

Matthew Wilcox (Oracle) posted 32 patches 3 years, 8 months ago
[PATCH v5 22/32] mm/memcontrol.c: Convert to printbuf
Posted by Matthew Wilcox (Oracle) 3 years, 8 months ago
From: Kent Overstreet <kent.overstreet@gmail.com>

This converts memory_stat_format() from seq_buf to printbuf. Printbuf is
simalar to seq_buf except that it heap allocates the string buffer:
here, we were already heap allocating the buffer with kmalloc() so the
conversion is trivial.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
---
 mm/memcontrol.c | 54 ++++++++++++++++++++++++-------------------------
 1 file changed, 27 insertions(+), 27 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index b69979c9ced5..54897e4ac4ef 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -62,7 +62,7 @@
 #include <linux/file.h>
 #include <linux/resume_user_mode.h>
 #include <linux/psi.h>
-#include <linux/seq_buf.h>
+#include <linux/printbuf.h>
 #include "internal.h"
 #include <net/sock.h>
 #include <net/ip.h>
@@ -1490,13 +1490,10 @@ static const unsigned int memcg_vm_event_stat[] = {
 #endif
 };
 
-static void memory_stat_format(struct mem_cgroup *memcg, char *buf, int bufsize)
+static void memory_stat_format(struct printbuf *out, struct mem_cgroup *memcg)
 {
-	struct seq_buf s;
 	int i;
 
-	seq_buf_init(&s, buf, bufsize);
-
 	/*
 	 * Provide statistics on the state of the memory subsystem as
 	 * well as cumulative event counters that show past behavior.
@@ -1513,30 +1510,30 @@ static void memory_stat_format(struct mem_cgroup *memcg, char *buf, int bufsize)
 		u64 size;
 
 		size = memcg_page_state_output(memcg, memory_stats[i].idx);
-		seq_buf_printf(&s, "%s %llu\n", memory_stats[i].name, size);
+		prt_printf(out, "%s %llu\n", memory_stats[i].name, size);
 
 		if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) {
 			size += memcg_page_state_output(memcg,
 							NR_SLAB_RECLAIMABLE_B);
-			seq_buf_printf(&s, "slab %llu\n", size);
+			prt_printf(out, "slab %llu\n", size);
 		}
 	}
 
 	/* Accumulated memory events */
-	seq_buf_printf(&s, "pgscan %lu\n",
-		       memcg_events(memcg, PGSCAN_KSWAPD) +
-		       memcg_events(memcg, PGSCAN_DIRECT));
-	seq_buf_printf(&s, "pgsteal %lu\n",
-		       memcg_events(memcg, PGSTEAL_KSWAPD) +
-		       memcg_events(memcg, PGSTEAL_DIRECT));
+	prt_printf(out, "pgscan %lu\n",
+		   memcg_events(memcg, PGSCAN_KSWAPD) +
+		   memcg_events(memcg, PGSCAN_DIRECT));
+	prt_printf(out, "pgsteal %lu\n",
+		   memcg_events(memcg, PGSTEAL_KSWAPD) +
+		   memcg_events(memcg, PGSTEAL_DIRECT));
 
 	for (i = 0; i < ARRAY_SIZE(memcg_vm_event_stat); i++)
-		seq_buf_printf(&s, "%s %lu\n",
-			       vm_event_name(memcg_vm_event_stat[i]),
-			       memcg_events(memcg, memcg_vm_event_stat[i]));
+		prt_printf(out, "%s %lu\n",
+			   vm_event_name(memcg_vm_event_stat[i]),
+			   memcg_events(memcg, memcg_vm_event_stat[i]));
 
 	/* The above should easily fit into one page */
-	WARN_ON_ONCE(seq_buf_has_overflowed(&s));
+	WARN_ON_ONCE(!printbuf_remaining(out));
 }
 
 #define K(x) ((x) << (PAGE_SHIFT-10))
@@ -1573,7 +1570,8 @@ void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *
 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
 {
 	/* Use static buffer, for the caller is holding oom_lock. */
-	static char buf[PAGE_SIZE];
+	static char _buf[PAGE_SIZE];
+	struct printbuf buf = PRINTBUF_EXTERN(_buf, sizeof(_buf));
 
 	lockdep_assert_held(&oom_lock);
 
@@ -1596,8 +1594,8 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
 	pr_info("Memory cgroup stats for ");
 	pr_cont_cgroup_path(memcg->css.cgroup);
 	pr_cont(":");
-	memory_stat_format(memcg, buf, sizeof(buf));
-	pr_info("%s", buf);
+	memory_stat_format(&buf, memcg);
+	pr_info("%s", buf.buf);
 }
 
 /*
@@ -6401,14 +6399,16 @@ static int memory_events_local_show(struct seq_file *m, void *v)
 static int memory_stat_show(struct seq_file *m, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
-	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
+	struct printbuf buf = PRINTBUF;
+	int ret;
 
-	if (!buf)
-		return -ENOMEM;
-	memory_stat_format(memcg, buf, PAGE_SIZE);
-	seq_puts(m, buf);
-	kfree(buf);
-	return 0;
+	memory_stat_format(&buf, memcg);
+	ret = buf.allocation_failure ? -ENOMEM : 0;
+	if (!ret)
+		seq_puts(m, buf.buf);
+
+	printbuf_exit(&buf);
+	return ret;
 }
 
 #ifdef CONFIG_NUMA
-- 
2.35.1
Re: [PATCH v5 22/32] mm/memcontrol.c: Convert to printbuf
Posted by Michal Hocko 3 years, 8 months ago
On Mon 08-08-22 03:41:18, Matthew Wilcox wrote:
> From: Kent Overstreet <kent.overstreet@gmail.com>
> 
> This converts memory_stat_format() from seq_buf to printbuf. Printbuf is
> simalar to seq_buf except that it heap allocates the string buffer:
> here, we were already heap allocating the buffer with kmalloc() so the
> conversion is trivial.

The changelog probably needs an update because the oom path doesn't
allocated and for somebody just reading this patch in isolation the
PRINTBUF_EXTERN doesn't really seem very obvious.

> Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Michal Hocko <mhocko@kernel.org>
> Cc: Roman Gushchin <roman.gushchin@linux.dev>

> -static void memory_stat_format(struct mem_cgroup *memcg, char *buf, int bufsize)
> +static void memory_stat_format(struct printbuf *out, struct mem_cgroup *memcg)
>  {
> -	struct seq_buf s;
>  	int i;
>  
> -	seq_buf_init(&s, buf, bufsize);
> -

When is the buffer cleared?

>  	/*
>  	 * Provide statistics on the state of the memory subsystem as
>  	 * well as cumulative event counters that show past behavior.
[...]
>  #define K(x) ((x) << (PAGE_SHIFT-10))
> @@ -1573,7 +1570,8 @@ void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *
>  void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
>  {
>  	/* Use static buffer, for the caller is holding oom_lock. */
> -	static char buf[PAGE_SIZE];
> +	static char _buf[PAGE_SIZE];
> +	struct printbuf buf = PRINTBUF_EXTERN(_buf, sizeof(_buf));
>  
>  	lockdep_assert_held(&oom_lock);

the buffer is static here!

>  
-- 
Michal Hocko
SUSE Labs
Re: [PATCH v5 22/32] mm/memcontrol.c: Convert to printbuf
Posted by Michal Hocko 3 years, 8 months ago
On Mon 08-08-22 11:48:53, Michal Hocko wrote:
> On Mon 08-08-22 03:41:18, Matthew Wilcox wrote:
> > From: Kent Overstreet <kent.overstreet@gmail.com>
> > 
> > This converts memory_stat_format() from seq_buf to printbuf. Printbuf is
> > simalar to seq_buf except that it heap allocates the string buffer:
> > here, we were already heap allocating the buffer with kmalloc() so the
> > conversion is trivial.
> 
> The changelog probably needs an update because the oom path doesn't
> allocated and for somebody just reading this patch in isolation the
> PRINTBUF_EXTERN doesn't really seem very obvious.
> 
> > Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
> > Cc: Johannes Weiner <hannes@cmpxchg.org>
> > Cc: Michal Hocko <mhocko@kernel.org>
> > Cc: Roman Gushchin <roman.gushchin@linux.dev>
> 
> > -static void memory_stat_format(struct mem_cgroup *memcg, char *buf, int bufsize)
> > +static void memory_stat_format(struct printbuf *out, struct mem_cgroup *memcg)
> >  {
> > -	struct seq_buf s;
> >  	int i;
> >  
> > -	seq_buf_init(&s, buf, bufsize);
> > -
> 
> When is the buffer cleared?
> 
> >  	/*
> >  	 * Provide statistics on the state of the memory subsystem as
> >  	 * well as cumulative event counters that show past behavior.
> [...]
> >  #define K(x) ((x) << (PAGE_SHIFT-10))
> > @@ -1573,7 +1570,8 @@ void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *
> >  void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
> >  {
> >  	/* Use static buffer, for the caller is holding oom_lock. */
> > -	static char buf[PAGE_SIZE];
> > +	static char _buf[PAGE_SIZE];
> > +	struct printbuf buf = PRINTBUF_EXTERN(_buf, sizeof(_buf));
> >  
> >  	lockdep_assert_held(&oom_lock);
> 
> the buffer is static here!

And answering to myself. THere is an internal pos field which gets
initialized by PRINTBUF_EXTERN even when not mentioned explicitly
because it gets a default 0 initialization

Acked-by: Michal Hocko <mhocko@suse.com>
-- 
Michal Hocko
SUSE Labs