Re: [PATCH v5 1/3] mm/page_owner: add print_mode filter

From: Andrew Morton

Date: Fri May 08 2026 - 17:13:58 EST


On Thu, 7 May 2026 14:46:41 +0800 Zhen Ni <zhen.ni@xxxxxxxxxxxx> wrote:

> Add a print_mode filter to page_owner that allows users to choose between
> printing full stack traces or only stack handles, significantly reducing
> output size for debugging and analysis.
>
> The filter provides a string-based interface under
> /sys/kernel/debug/page_owner_filter/:
> - Reading shows the current mode with [] brackets around active option
> - Writing accepts "full_stack" or "stack_handle" strings
>
> The default full_stack mode maintains backward compatibility with existing
> usage, displaying complete stack traces for each page allocation.
>
> The stack_handle mode dramatically reduces log size by showing only
> the handle number instead of the full stack trace. The mapping from
> handles to actual stack traces can be obtained via the
> show_stacks_handles interface.
>
> Example usage:
> # echo stack_handle > /sys/kernel/debug/page_owner_filter/print_mode
> # cat /sys/kernel/debug/page_owner_filter/print_mode
> full_stack [stack_handle]
> # cat /sys/kernel/debug/page_owner
> Page allocated via order 0, migratetype Unmovable, gfp_mask 0x1100ca,
> pid 1, tgid 1 (systemd), ts 123456789 ns
> PFN 0x1000 type Unmovable Block 1 type Unmovable
> Flags 0x3fffe800000084(referenced|lru|active|private|node=0|zone=1)
> handle: 17432583
> ...

I like the example. But what is the user to do with `handle'? Perhaps
expand this example to cover looking up that stack in
/sys/kernel/debug/page_owner_stacks/show_stacks_handles?

> --- a/mm/page_owner.c
> +++ b/mm/page_owner.c
>
> ...
>
> +static ssize_t print_mode_write(struct file *file,
> + const char __user *buf,
> + size_t count, loff_t *ppos)
> +{
> + char *kbuf;
> + int mode;
> + int ret = count;
> +
> + /*
> + * Limit input size. Maximum valid input is "stack_handle" (12 chars)
> + * plus newline and null terminator. Use 32 bytes as a reasonable limit.
> + */
> + if (count > 32)
> + return -EINVAL;
> +
> + kbuf = kmalloc(count + 1, GFP_KERNEL);

This is bikeshedding, but...

Why kmalloc this when we know it's <= 32 bytes? A local array...

> + if (!kbuf)
> + return -ENOMEM;
> +
> + if (strncpy_from_user(kbuf, buf, count) < 0) {
> + ret = -EFAULT;
> + goto out_free;
> + }
> + kbuf[count] = '\0';
> +
> + mode = sysfs_match_string(page_owner_print_mode_strings, kbuf);
> + if (mode < 0) {
> + ret = -EINVAL;
> + goto out_free;
> + }
> +
> + WRITE_ONCE(owner_filter.print_mode, mode);
> +
> +out_free:
> + kfree(kbuf);
> + return ret;
> +}

print_mode_write() seems a lot of work to get a simple string from
userspace!

>
> ...
>