[PATCH v5 2/3] mm/page_owner: add NUMA node filter with nodelist support

From: Zhen Ni

Date: Thu May 07 2026 - 03:02:54 EST


Add NUMA node filtering functionality to page_owner to allow filtering
pages by specific NUMA node(s). This is useful for NUMA-aware memory
allocation analysis and debugging.

The filter supports flexible nodelist input formats:
- Single node: echo "0" > nid
- Multiple nodes: echo "0,2,3" > nid
- Node range: echo "0-3" > nid
- Mixed format: echo "0,2-4,7" > nid
- Clear filter: echo > nid (empty string)

The implementation uses nodemask_t for efficient multi-node filtering
and nodelist_parse() for flexible input parsing. Empty input clears
the filter.

Note: Access to nid_mask uses plain load/store without locking because
nodemask_t is too large (128 bytes) for READ_ONCE/WRITE_ONCE. This is
safe for debug use: low-frequency changes and torn reads would only
cause temporary inconsistency in debug output.

Signed-off-by: Zhen Ni <zhen.ni@xxxxxxxxxxxx>
---

Changes in v5:
- Optimize nodes_empty() check in page iteration loop
- Add __data_racy qualifier to nid_mask field

Changes in v4:
- Remove "-1" support, use empty string to clear filter
- Use strncpy_from_user() instead of copy_from_user()
- Add concurrency safety documentation for nid_mask access
- Rename fops to page_owner_nid_filter_fops for consistency

Changes in v3:
- Remove READ_ONCE/WRITE_ONCE for nodemask_t
- Add comment explaining input length calculation formula
- Simplify "-1" check using kstrtoint()
- Move nodemask_t mask read outside PFN iteration loop

Changes in v2:
- Use nodemask_t instead of int to support multiple nodes
- Implement nodelist_parse() for flexible input formats
- Use %*pbl format for output
---
mm/page_owner.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 86 insertions(+)

diff --git a/mm/page_owner.c b/mm/page_owner.c
index 28766c854d02..227a377d6bb2 100644
--- a/mm/page_owner.c
+++ b/mm/page_owner.c
@@ -67,10 +67,16 @@ static const char * const page_owner_print_mode_strings[] = {

struct page_owner_filter {
enum page_owner_print_mode print_mode;
+ /*
+ * Lockless access: nodemask_t exceeds READ_ONCE/WRITE_ONCE size limit.
+ * Torn reads acceptable for debug interface with infrequent writes.
+ */
+ nodemask_t __data_racy nid_mask;
};

static struct page_owner_filter owner_filter = {
.print_mode = PAGE_OWNER_PRINT_FULL_STACK,
+ .nid_mask = NODE_MASK_NONE,
};

static bool page_owner_enabled __initdata;
@@ -687,6 +693,7 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
struct page_ext *page_ext;
struct page_owner *page_owner;
depot_stack_handle_t handle;
+ nodemask_t mask;

if (!static_branch_unlikely(&page_owner_inited))
return -EINVAL;
@@ -700,6 +707,9 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
pfn++;

+ mask = owner_filter.nid_mask;
+ bool filter_by_nid = !nodes_empty(mask);
+
/* Find an allocated page */
for (; pfn < max_pfn; pfn++) {
/*
@@ -732,6 +742,14 @@ 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 */
+ if (filter_by_nid) {
+ 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.
@@ -1054,6 +1072,72 @@ static const struct file_operations page_owner_print_mode_fops = {
.llseek = default_llseek,
};

+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).
+ * 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 (strncpy_from_user(kbuf, buf, count) < 0) {
+ ret = -EFAULT;
+ goto out_free;
+ }
+ kbuf[count] = '\0';
+
+ /* Support nodelist format like "0", "0,2", "0-3", or empty to clear */
+ if (nodelist_parse(kbuf, mask)) {
+ ret = -EINVAL;
+ goto out_free;
+ }
+
+ owner_filter.nid_mask = mask;
+ ret = count;
+
+out_free:
+ kfree(kbuf);
+ return ret;
+}
+
+static int nid_filter_show(struct seq_file *m, void *v)
+{
+ nodemask_t mask = owner_filter.nid_mask;
+
+ if (nodes_empty(mask))
+ seq_puts(m, "\n");
+ else
+ seq_printf(m, "%*pbl\n", nodemask_pr_args(&mask));
+
+ return 0;
+}
+
+static int nid_filter_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, nid_filter_show, NULL);
+}
+
+static const struct file_operations page_owner_nid_filter_fops = {
+ .owner = THIS_MODULE,
+ .open = nid_filter_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .write = nid_filter_write,
+ .release = single_release,
+};
+

static int __init pageowner_init(void)
{
@@ -1069,6 +1153,8 @@ static int __init pageowner_init(void)
filter_dir = debugfs_create_dir("page_owner_filter", NULL);
debugfs_create_file("print_mode", 0600, filter_dir, NULL,
&page_owner_print_mode_fops);
+ debugfs_create_file("nid", 0600, filter_dir, NULL,
+ &page_owner_nid_filter_fops);

dir = debugfs_create_dir("page_owner_stacks", NULL);
debugfs_create_file("show_stacks", 0400, dir,
--
2.20.1