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

zhen.ni
2026-04-29T07:35:21Z

在 2026/4/28 22:16, Andrew Morton 写道:
> On Tue, 28 Apr 2026 15:11:11 +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
>>
>> ...
>>
>> +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;
>> + int val;
>> +
>> + /*
>> + * Limit input size to handle worst-case nodelist (all nodes).
>> + * Worst case per node: ",NNNNN" (comma + 5-digit node number) = 6 bytes.
>> + * Formula: 100 bytes overhead + 6 * MAX_NUMNODES
>> + */
>> + if (count > (100 + 6 * MAX_NUMNODES))
>> + return -EINVAL;
>> +
>> + 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';
>
> strncpy_from_user() was not useful here?
>

After checking similar usage in mm/ (e.g., mm/kmemleak.c),
I'll switch to strncpy_from_user().

The change is straightforward:

long len;

len = strncpy_from_user(kbuf, buf, count + 1);
if (len < 0) {
ret = -EFAULT;
goto out_free;
}
kbuf[count] = '\0';

I'll make this change in the next version.
>> + /* Support: "-1" to clear, or nodelist format like "0", "0,2", "0-3" */
>> + if (kstrtoint(kbuf, 10, &val) == 0 && val == -1)
>> + nodes_clear(mask);
>> + else if (nodelist_parse(kbuf, mask)) {
>> + ret = -EINVAL;
>> + goto out_free;
>> + }
>> +
>> + owner_filter.nid_mask = mask;
>> + ret = count;
>> +
>> +out_free:
>> + kfree(kbuf);
>> + return ret;
>> +}
>
>
>
Best regards,
Zhen Ni