Re: [PATCH v4 1/1] mm: optimize stat output for 11% sys time reduce

From: Johannes Weiner

Date: Mon Mar 02 2026 - 14:43:34 EST


On Fri, Jan 23, 2026 at 11:01:08PM +0800, Jianyue Wu wrote:
> +void memcg_seq_buf_print_stat(struct seq_buf *s, const char *prefix,
> + const char *name, char sep, u64 val)
> +{
> + char num_buf[MEMCG_DEC_U64_MAX_LEN + 2]; /* +2 for separator and newline */
> + int num_len;
> +
> + /* Embed separator at the beginning */
> + num_buf[0] = sep;
> +
> + /* Convert number starting at offset 1 */
> + num_len = num_to_str(num_buf + 1, sizeof(num_buf) - 2, val, 0);
> + if (num_len <= 0)
> + return;
> +
> + /* Embed newline at the end */
> + num_buf[num_len + 1] = '\n';
> +
> + if (prefix && *prefix && seq_buf_puts(s, prefix))
> + return;
> + if (seq_buf_puts(s, name))
> + return;
> + /* Output separator, value, and newline in one call */
> + seq_buf_putmem(s, num_buf, num_len + 2);

You seem to be losing the \0 somewhere. I'm getting garbage at the end
of memory.stat on mm-new:

[...]
thp_swpout_fallback 1212
hp_swpout_fallback 1212
hp_swpout_fallback 1054
907
1278

Dropping this patch fixes the issue.