Re: [PATCH v2 3/3] mm/page_owner: add NUMA node filter with nodelist support

Andrew Morton
2026-04-24T11:26:46Z

On Sun, 19 Apr 2026 23:55:40 +0800 Zhen Ni <zhen.ni@easystack.cn> wrote:

> Add NUMA node filtering functionality to page_owner to allow
> filtering pages by specific NUMA node(s) using nodelist format.
>
> The filter allows users to focus on pages from specific NUMA nodes,
> which is useful for NUMA-aware memory allocation analysis and debugging.
>
> Supported input formats:
> - Single node: echo "2" > nid
> - Multiple nodes: echo "0,2,3" > nid
> - Node range: echo "0-3" > nid
> - Mixed format: echo "0,2-4,7" > nid
> - Disable filter: echo "-1" > nid
>
> ...
>
> --- a/mm/page_owner.c
> +++ b/mm/page_owner.c
> @@ -707,6 +707,7 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
> * user through copy_to_user() or GFP_KERNEL allocations.
> */
> struct page_owner page_owner_tmp;
> + nodemask_t mask;
>
> /*
> * If the new page is in a new MAX_ORDER_NR_PAGES area,
> @@ -730,6 +731,15 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
> if (unlikely(!page_ext))
> continue;
>
> + /* NUMA node filter using bitmask */
> + mask = READ_ONCE(owner_filter.nid_mask);
> + if (!nodes_empty(mask)) {
> + int nid = page_to_nid(page);
> +
> + if (!node_isset(nid, mask))
> + goto ext_put_continue;
> + }
> +
> /*
> * Some pages could be missed by concurrent allocation or free,
> * because we don't hold the zone lock.
> @@ -1009,6 +1019,70 @@ DEFINE_SIMPLE_ATTRIBUTE(page_owner_print_mode_fops,
> &page_owner_print_mode_get,
> &page_owner_print_mode_set, "%lld");
>
> +static ssize_t nid_filter_write(struct file *file,
> + const char __user *buf,
> + size_t count, loff_t *ppos)
> +{
> + char *kbuf;
> + nodemask_t mask;
> + int ret;
> +
> + /* Limit input size to handle worst-case nodelist (all nodes) */
> + if (count > (100 + 6 * MAX_NUMNODES))
> + return -EINVAL;

I'm wondering how that expression was arrived at ;) Perhaps a little
comment explaining? Or can we simply use strnlen_user() here?

> + kbuf = kmalloc(count + 1, GFP_KERNEL);
> + if (!kbuf)
> + return -ENOMEM;
> +
> + if (copy_from_user(kbuf, buf, count)) {
> + ret = -EFAULT;
> + goto out_free;
> + }
> + kbuf[count] = '\0';

Can we use strncpy_from_user() here?

In fact I thought that we had a strdup_from_user() thing which does the
kmalloc also, but maybe I dreamed it.

And memdup_user_nul() could perhaps be used in here.

Nothing really fits. We need a

char *strdup_user(const void __user *src);

(Probably it's OK to assume GFP_KERNEL)
(Maybe needs a size_t max_len arg)

> + /* Support: "-1" to clear, or nodelist format like "0", "0,2", "0-3" */
> + if (strcmp(kbuf, "-1\n") == 0 || strcmp(kbuf, "-1") == 0)

Maybe

if (kstrtoll(kbuf) == -1)

(we have lots of goodies in /lib!)