mm/page_owner: add bpf_iter and kfuncs
Subsystems (5)
cover mm/page_owner: add bpf_iter and kfuncs
Hi all,
Introduction
============
page_owner records who allocated every page and is a core memory-debug facility, but it currently reaches userspace as the debugfs flat dump:
slicing allocations by pid, comm, NUMA node, cgroup or stack, etc., means streaming and parsing the entire file in userspace, which is expensive on large hosts and hard to query live.
Exposing the same walk to eBPF lets a program filter and aggregate in the kernel and surface only the records it cares about. This series adds that access as a seq_file bpf_iter target and an open-coded iterator.
Implementation
==============
The page walk is factored into page_owner_next_eligible(), shared by the read path and the iterators so eligibility stays identical. Three kfuncs expose information that a BPF program cannot decode from the snapshot alone: the NUMA node, per-page memcg info including the full cgroup path, and the stack trace behind a stack_depot handle. Each record is snapshotted under the page_ext RCU lock, so consumers read a consistent copy.
Testing
=======
The selftest exercises the seq_file and open-coded iterators, and validates the get_nid, stack_snprint and get_memcg_info kfuncs.
Zhen Ni (6):
mm/page_owner: extract page_owner_next_eligible() from read loop mm/page_owner: add bpf_iter target "page_owner" mm/page_owner: add open-coded page_owner iterator mm/page_owner: add bpf_page_owner_get_nid() kfunc mm/page_owner: add bpf_page_owner_get_memcg_info() kfunc selftests/bpf: add tests for the page_owner bpf_iter target
kernel/bpf/helpers.c | 8 + mm/Makefile | 3 + mm/page_owner.c | 219 +++++++++++------- mm/page_owner.h | 58 +++++ mm/page_owner_iter.c | 180 ++++++++++++++ .../bpf/prog_tests/page_owner_iter.c | 173 ++++++++++++++ .../selftests/bpf/progs/bpf_iter_page_owner.c | 161 +++++++++++++ 7 files changed, 722 insertions(+), 80 deletions(-) create mode 100644 mm/page_owner.h create mode 100644 mm/page_owner_iter.c create mode 100644 tools/testing/selftests/bpf/prog_tests/page_owner_iter.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c
Signature · 1 line
2.20.1
PATCH 1/6 mm/page_owner: extract page_owner_next_eligible() from read loop
Move the page scanning logic out of read_page_owner() into a dedicated helper, in preparation for using it in the page_owner bpf_iter target.
The helper advances a struct page_owner_scan cursor to the next page eligible for page_owner output and returns true on a hit. On a hit it takes a snapshot of the page_owner record inside the page_ext RCU window.
No change to the output is intended. handle is read from the snapshot, taken before the non-zero check, so the validated and reported handle are the same value.
--- mm/page_owner.c | 105 ++++++++++++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 39 deletions(-)
mm/page_owner.c +66 -39
@@ -36,6 +36,16 @@ struct page_owner {pid_t free_tgid; }; +/* +* Cursor and per-page result of page_owner_next_eligible(). +*/ +struct page_owner_scan { +/* resume point */ +unsigned long pfn; +struct page *page; +struct page_owner po_snap; +}; +struct stack {struct stack_record *stack_record;struct stack *next;
@@ -725,37 +735,24 @@ void __dump_page_owner(const struct page *page)page_ext_put (page_ext); } -static ssize_t -read_page_owner (struct file *file,char __user *buf,size_t count,loff_t *ppos) +/* +* Advance @scan to the next page eligible for page_owner output. +* On a hit, fill scan->pfn/page/po_snap and return true; the caller +* must advance scan->pfn past the hit before calling again to resume +* the scan. +*/ +static bool page_owner_next_eligible (struct page_owner_scan *scan) { -unsigned long pfn; -struct page *page; -struct page_ext *page_ext; -struct page_owner *page_owner; -depot_stack_handle_t handle; -struct page_owner_filter_state *state = file->private_data; +unsigned long pfn = scan->pfn; -if (!static_branch_unlikely (&page_owner_inited)) -return -EINVAL ; - - page =NULL ; -if (*ppos ==0 ) - pfn = min_low_pfn; -else - pfn = *ppos;/* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */ while (!pfn_valid (pfn) && (pfn & (MAX_ORDER_NR_PAGES -1 )) !=0 ) pfn++; -/* Find an allocated page */ for (; pfn < max_pfn; pfn++) { -/* -* This temporary page_owner is required so -* that we can avoid the context switches while holding -* the rcu lock and copying the page owner information to -* user through copy_to_user() or GFP_KERNEL allocations. -*/ -struct page_owner page_owner_tmp; +struct page_owner *page_owner; +struct page_ext *page_ext; +struct page *page;/* * If the new page is in a new MAX_ORDER_NR_PAGES area,
@@ -798,37 +795,67 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) goto ext_put_continue;/* -* Access to page_ext->handle isn't synchronous so we should -* be careful to access it. +* Take a snapshot of the page_owner record inside the RCU +* window, then validate it, so that print_page_owner() +* reads the checked handle from the same snapshot. */ - handle =READ_ONCE (page_owner->handle); -if (!handle) - goto ext_put_continue; + scan->pfn = pfn; + scan->page = page; + scan->po_snap = *page_owner; +page_ext_put (page_ext); +if (!scan->po_snap.handle) +continue ; + +return true; + +ext_put_continue: +page_ext_put (page_ext); +cond_resched (); + } + +return false; +} + +static ssize_t +read_page_owner (struct file *file,char __user *buf,size_t count,loff_t *ppos) +{ +struct page_owner_scan scan; +struct page_owner_filter_state *state = file->private_data; + +if (!static_branch_unlikely (&page_owner_inited)) +return -EINVAL ; + +if (*ppos ==0 ) + scan.pfn = min_low_pfn; +else + scan.pfn = *ppos; + +/* Find an allocated page */ +while (page_owner_next_eligible (&scan)) {if (state->nid_filter_enabled) {int nid; -memdesc_flags_t page_flags =READ_ONCE (page->flags); +memdesc_flags_t page_flags = +READ_ONCE (scan.page->flags);/* * Bypass PF_POISONED_CHECK() in page_to_nid() to avoid * VM_BUG_ON when accessing poisoned pages. */ if (page_flags.f ==PAGE_POISON_PATTERN ) - goto ext_put_continue; + goto skip_continue; nid =memdesc_nid (&page_flags);if (!node_isset (nid, state->nid_filter)) - goto ext_put_continue; + goto skip_continue; }/* Record the next PFN to read in the file offset */ - *ppos = pfn +1 ; + *ppos = scan.pfn +1 ; - page_owner_tmp = *page_owner; -page_ext_put (page_ext); -return print_page_owner (buf, count, pfn, page, - &page_owner_tmp, handle, state); -ext_put_continue: -page_ext_put (page_ext); +return print_page_owner (buf, count, scan.pfn, scan.page, + &scan.po_snap, scan.po_snap.handle, state); +skip_continue: + scan.pfn++;cond_resched (); }
Signature · 1 line
2.20.1
[email protected]
AI reviewRe: [PATCH 1/6] mm/page_owner: extract page_owner_next_eligible() from read loop
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] mm: soft lockup in page_owner_next_eligible() due to missing cond_resched() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm: soft lockup in page_owner_next_eligible() due to missing cond_resched() --
commit 303a10c305ed333982228df86334f12bd6d8a3c2
Author: Zhen Ni <[email protected]>
mm/page_owner: extract page_owner_next_eligible() from read loop
This commit extracts the page scanning logic from read_page_owner() into a dedicated helper, page_owner_next_eligible(). It advances a cursor to the next eligible page and takes a snapshot of the page_owner record inside the page_ext RCU window.
Quoted 4 lines
mm/page_owner.c +0
-0
[ ... ]
Quoted 10 lines
mm/page_owner.c +3
-2@@ -798,37 +795,67 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) goto ext_put_continue;/* -* Access to page_ext->handle isn't synchronous so we should -* be careful to access it. +* Take a snapshot of the page_owner record inside the RCU +* window, then validate it, so that print_page_owner() +* reads the checked handle from the same snapshot. */
mm/page_owner.c read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) · line 808
Quoted 10 lines
mm/page_owner.c +6
-3- handle =READ_ONCE (page_owner->handle); -if (!handle) - goto ext_put_continue; + scan->pfn = pfn; + scan->page = page; + scan->po_snap = *page_owner; +page_ext_put (page_ext); +if (!scan->po_snap.handle) +continue ;
[Severity: High]
Could this cause a soft lockup? By using a continue statement here in page_owner_next_eligible(), we jump directly to the next loop iteration (advancing pfn) without executing cond_resched() at the end of the loop block.
If a large contiguous sequence of allocated pages lacks stack handles (e.g., if stack depot is exhausted or during early non-tracked allocations), the kernel could iterate over millions of pages without yielding.
The original implementation used goto ext_put_continue; which correctly yielded. Does this need to be updated to explicitly yield or use the goto instead?
Quoted 7 lines
mm/page_owner.c +7
-0+ +return true; + +ext_put_continue: +page_ext_put (page_ext); +cond_resched (); + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH 1/6] mm/page_owner: extract page_owner_next_eligible() from read loop
Quoted 48 lines · 在 2026/10/9 19:35, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm: soft lockup inpage_owner_next_eligible()due to missingcond_resched()--commit
303a10c305ed333982228df86334f12bd6d8a3c2
Author: Zhen Ni <[email protected]>
mm/page_owner: extractpage_owner_next_eligible()from read loopThis commit extracts the page scanning logic from
read_page_owner()into a dedicated helper,page_owner_next_eligible(). It advances a cursor to the next eligible page and takes a snapshot of thepage_ownerrecord inside thepage_extRCU window.Quoted 4 lines
mm/page_owner.c +0
-0[ ... ]
Quoted 10 lines
mm/page_owner.c +3
-2@@ -798,37 +795,67 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) goto ext_put_continue;/* -* Access to page_ext->handle isn't synchronous so we should -* be careful to access it. +* Take a snapshot of the page_owner record inside the RCU +* window, then validate it, so that print_page_owner() +* reads the checked handle from the same snapshot. */ mm/page_owner.c read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) · line 808
Quoted 10 lines
mm/page_owner.c +6
-3- handle =READ_ONCE (page_owner->handle); -if (!handle) - goto ext_put_continue; + scan->pfn = pfn; + scan->page = page; + scan->po_snap = *page_owner; +page_ext_put (page_ext); +if (!scan->po_snap.handle) +continue ;[Severity: High]
Could this cause a soft lockup? By using a continue statement here inpage_owner_next_eligible(), we jump directly to the next loop iteration (advancing pfn) without executingcond_resched()at the end of the loop block.
Quoted 8 lines
If a large contiguous sequence of allocated pages lacks stack handles (e.g., if stack depot is exhausted or during early non-tracked allocations), the kernel could iterate over millions of pages without yielding.
The original implementation used goto
ext_put_continue; which correctly yielded. Does this need to be updated to explicitly yield or use the goto instead?
Many contiguous allocated pages having a zero handleis an extreme case, so I didn't add a scheduling point there.
It is harmless and keeps the reject path uniform, so I'll switch it to goto ext_put_continue in the next revision.
Quoted 8 lines
mm/page_owner.c read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) · line 815
Quoted 7 lines
mm/page_owner.c +7
-0+ +return true; + +ext_put_continue: +page_ext_put (page_ext); +cond_resched (); + }
Thanks,
Zhen
PATCH 2/6 mm/page_owner: add bpf_iter target "page_owner"
In reply to Zhen NiRegister a bpf_iter target "page_owner" that walks the pages eligible for page_owner output, with the same eligibility rules as the /sys/kernel/debug/page_owner read path.
For each eligible page the attached BPF program receives the pfn, the page and the page_owner record, and can freely filter and format output.
The po_snap record in the scan cursor is taken inside the page_ext RCU window and handed to the BPF program as a copy, so no RCU window is needed at consumption time. Torn reads remain possible if the record is concurrently updated while it is copied, but this is the same best-effort behavior as the read path has always had.
The target is registered from pageowner_init() when both CONFIG_PAGE_OWNER and CONFIG_BPF_SYSCALL are enabled; a registration failure only disables the bpf_iter target.
Userspace tools such as bpftrace can attach to this target like any other bpf_iter. For example, to count pages owned by init (pid 1):
bpftrace -e 'iter:page_owner / ctx->po->pid == 1 / { @ = count(); }'--- mm/Makefile | 3 ++ mm/page_owner.c | 33 +++--------- mm/page_owner.h | 48 ++++++++++++++++++ mm/page_owner_iter.c | 116 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+), 26 deletions(-) create mode 100644 mm/page_owner.h create mode 100644 mm/page_owner_iter.c
mm/Makefile +3 -0
@@ -115,6 +115,9 @@ obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o obj-$(CONFIG_DEBUG_RODATA_TEST) += rodata_test.o obj-$(CONFIG_DEBUG_VM_PGTABLE) += debug_vm_pgtable.o obj-$(CONFIG_PAGE_OWNER) += page_owner.o +ifdef CONFIG_BPF_SYSCALL +obj-$(CONFIG_PAGE_OWNER) += page_owner_iter.o +endif obj-$(CONFIG_MEMORY_ISOLATION) += page_isolation.o obj-$(CONFIG_ZSMALLOC) += zsmalloc.o obj-$(CONFIG_GENERIC_EARLY_IOREMAP) += early_ioremap.o
mm/page_owner.c +7 -26
@@ -14,6 +14,7 @@#include <linux/sched/clock.h> #include "page_alloc.h" +#include "page_owner.h" /* * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
@@ -21,31 +22,6 @@*/ #define PAGE_OWNER_STACK_DEPTH (16) -struct page_owner { -unsigned short order; -short last_migrate_reason; -gfp_t gfp_mask; -depot_stack_handle_t handle; -depot_stack_handle_t free_handle; -u64 ts_nsec; -u64 free_ts_nsec; -char comm[TASK_COMM_LEN ]; -pid_t pid; -pid_t tgid; -pid_t free_pid; -pid_t free_tgid; -}; - -/* -* Cursor and per-page result of page_owner_next_eligible(). -*/ -struct page_owner_scan { -/* resume point */ -unsigned long pfn; -struct page *page; -struct page_owner po_snap; -}; -struct stack {struct stack_record *stack_record;struct stack *next;
@@ -741,7 +717,7 @@ void __dump_page_owner(const struct page *page)* must advance scan->pfn past the hit before calling again to resume * the scan. */ -static bool page_owner_next_eligible (struct page_owner_scan *scan) +bool page_owner_next_eligible (struct page_owner_scan *scan) {unsigned long pfn = scan->pfn;
@@ -1196,6 +1172,11 @@ static int __init pageowner_init(void) &stack_fops);debugfs_create_file ("count_threshold" ,0600 , dir,NULL , &threshold_fops); + +#ifdef CONFIG_BPF_SYSCALL +if (page_owner_iter_register ()) +pr_warn ("page_owner: failed to register bpf_iter target\n" ); +#endif return 0 ; }late_initcall (pageowner_init)
mm/page_owner.h new file +48 -0
48 lines not on this page · raw message
mm/page_owner_iter.c new file +116 -0
116 lines not on this page · raw message
Signature · 1 line
2.20.1
[email protected]
AI reviewRe: [PATCH 2/6] mm/page_owner: add bpf_iter target "page_owner"
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] mm: vmemmap use-after-free in page_owner_next_eligible() during memory hot-remove - [Medium] mm…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] mm: vmemmap use-after-free in page_owner_next_eligible() during memory hot-remove
- [Medium] mm: bpf_iter page_owner skips pages when seq_file buffer fills
- [Medium] bpf: missing EOF notification for page_owner bpf_iter
- [Medium] bpf: page_owner_iter skips records during chunked seq_read
- [Medium] bpf: page_owner_iter lacks EOF notification and yields stale po_snap --
commit fd543af9dd8ff743b641757854c444d98608421d
Author: Zhen Ni <[email protected]>
Subject: mm/page_owner: add bpf_iter target "page_owner"
This commit registers a bpf_iter target page_owner that walks the pages eligible for page_owner output. The attached BPF program receives the pfn, the page, and the page_owner record to filter and format the output.
Quoted 4 lines
mm/page_owner.c +0
-0
[ ... ]
mm/page_owner.c void __dump_page_owner(const struct page *page) · line 720
Quoted 6 lines
mm/page_owner.c +1
-1@@ -741,7 +717,7 @@ void __dump_page_owner(const struct page *page)* must advance scan->pfn past the hit before calling again to resume * the scan. */ -static bool page_owner_next_eligible (struct page_owner_scan *scan) +bool page_owner_next_eligible (struct page_owner_scan *scan)
[Severity: High]
Does exposing this function to the BPF iterator API introduce a use-after-free regression during memory hot-remove?
Inside page_owner_next_eligible(), pfn_valid() is used to verify memory sections. However, since the iteration runs without holding memory hotplug locks or a continuous rcu_read_lock(), the memory section can be concurrently hot-removed by another CPU as soon as pfn_valid() returns.
If the vmemmap pages backing the page struct are unmapped and freed during this race, the subsequent call will dereference the unmapped page struct:
page = pfn_to_page(pfn); if (skip_buddy_pages(&pfn, page)) continue;
Can this cause a kernel page fault and panic?
While this logic already existed in the /sys/kernel/debug/page_owner read path, this patch relies on it and exposes the buggy code path to the new BPF iterator API.
Quoted 5 lines
mm/page_owner_iter.c new file +0
-0
[ ... ]
mm/page_owner_iter.c line 27
Quoted 8 lines
mm/page_owner_iter.c new file +8
-0+static void *page_owner_seq_start (struct seq_file *seq,loff_t *posp) +{ +struct page_owner_scan *scan = seq->private; + +if (*posp ==0 ) + scan->pfn = min_low_pfn; +else + scan->pfn++;
[Severity: Medium]
Can this unconditionally skip elements during chunked reads?
During a seq_read iteration, if a formatted record overflows the output buffer, the iterator discards the overflowed record, returns to userspace, and later resumes by calling start() with *posp > 0.
Since next() already advanced scan->pfn to point to the overflowed record, start() increments it again here. Does this mean userspace tools reading the iterator output in chunks (like cat or bpftool iter) will silently drop one valid page owner record at every buffer chunk boundary?
Quoted 6 lines
mm/page_owner_iter.c new file +6
-0+ +if (!page_owner_next_eligible (scan)) +return NULL ; +return scan->page; +} +
[ ... ]
mm/page_owner_iter.c line 48
Quoted 3 lines
mm/page_owner_iter.c new file +3
-0+static void page_owner_seq_stop (struct seq_file *seq,void *v) +{ +}
[Severity: Medium]
Are we missing an EOF notification here?
Standard bpf_iter implementations invoke the BPF program with v == NULL during seq_ops->stop() to notify the program of EOF, enabling summary aggregate output. Because this function is empty, BPF programs attached to the page_owner iterator cannot detect the end of iteration.
Does this mean any logic relying on the standard EOF signal to output summary statistics or perform cleanup will never execute?
mm/page_owner_iter.c line 59
Quoted 11 lines
mm/page_owner_iter.c new file +11
-0+ +static int page_owner_prog_seq_show (struct bpf_prog *prog, +struct bpf_iter_meta *meta,void *v) +{ +struct page_owner_scan *scan = meta->seq->private; +struct bpf_iter__page_owner ctx; + +/* @po: hit-time snapshot, no RCU window needed. */ + ctx.meta = meta; + ctx.page = (struct page *)v; + ctx.po = &scan->po_snap;
[Severity: Medium]
If EOF were properly signaled with v == NULL, would this unconditional assignment provide stale data to the BPF program?
By unconditionally assigning ctx.po to the snapshot pointer, ctx.po is never NULL, even when v (the page) is NULL. Does this mean BPF programs cannot reliably use if (!ctx->po) to detect EOF, and might operate on stale snapshot data from the last valid page?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/6] mm/page_owner: add bpf_iter target "page_owner"
Quoted 44 lines · 在 2026/10/9 19:50, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] mm: vmemmap use-after-free inpage_owner_next_eligible()during memory hot-remove
- [Medium] mm:bpf_iterpage_ownerskips pages whenseq_filebuffer fills
- [Medium] bpf: missing EOF notification forpage_ownerbpf_iter
- [Medium] bpf:page_owner_iterskips records during chunkedseq_read
- [Medium] bpf:page_owner_iterlacks EOF notification and yields stalepo_snap--commit
fd543af9dd8ff743b641757854c444d98608421d
Author: Zhen Ni <[email protected]>
Subject:mm/page_owner: addbpf_itertarget "page_owner"This commit registers a
bpf_itertargetpage_ownerthat walks the pages eligible forpage_owneroutput. The attached BPF program receives the pfn, the page, and thepage_ownerrecord to filter and format the output.Quoted 4 lines
mm/page_owner.c +0
-0[ ... ]
mm/page_owner.c void __dump_page_owner(const struct page *page) · line 720
Quoted 6 lines
mm/page_owner.c +1
-1@@ -741,7 +717,7 @@ void __dump_page_owner(const struct page *page)* must advance scan->pfn past the hit before calling again to resume * the scan. */ -static bool page_owner_next_eligible (struct page_owner_scan *scan) +bool page_owner_next_eligible (struct page_owner_scan *scan)[Severity: High]
Does exposing this function to the BPF iterator API introduce a use-after-free regression during memory hot-remove?Inside
page_owner_next_eligible(),pfn_valid()is used to verify memory sections. However, since the iteration runs without holding memory hotplug locks or a continuousrcu_read_lock(), the memory section can be concurrently hot-removed by another CPU as soon aspfn_valid()returns.If the vmemmap pages backing the page struct are unmapped and freed during this race, the subsequent call will dereference the unmapped page struct:
page = pfn_to_page(pfn); if (skip_buddy_pages(&pfn, page)) continue;
Quoted 6 lines
Can this cause a kernel page fault and panic?
While this logic already existed in the
/sys/kernel/debug/page_ownerread path, this patch relies on it and exposes the buggy code path to the new BPF iterator API.
This race is pre-existing and not introduced here, so it might be better handled in a separate series rather than as part of this change.
Quoted 18 lines
Quoted 5 lines
mm/page_owner_iter.c new file +0
-0[ ... ]
mm/page_owner_iter.c line 27
Quoted 8 lines
mm/page_owner_iter.c new file +8
-0+static void *page_owner_seq_start (struct seq_file *seq,loff_t *posp) +{ +struct page_owner_scan *scan = seq->private; + +if (*posp ==0 ) + scan->pfn = min_low_pfn; +else + scan->pfn++;[Severity: Medium]
Can this unconditionally skip elements during chunked reads?
Quoted 9 lines
During a
seq_readiteration, if a formatted record overflows the output buffer, the iterator discards the overflowed record, returns to userspace, and later resumes by callingstart()with *posp > 0.Since
next()already advancedscan->pfnto point to the overflowed record,start()increments it again here. Does this mean userspace tools reading the iterator output in chunks (like cat or bpftool iter) will silently drop one valid page owner record at every buffer chunk boundary?
Will remove the else scan->pfn++ in page_owner_seq_start().
Quoted 14 lines
Quoted 6 lines
mm/page_owner_iter.c new file +6
-0+ +if (!page_owner_next_eligible (scan)) +return NULL ; +return scan->page; +} +[ ... ]
mm/page_owner_iter.c line 48
Quoted 3 lines
mm/page_owner_iter.c new file +3
-0+static void page_owner_seq_stop (struct seq_file *seq,void *v) +{ +}[Severity: Medium]
Are we missing an EOF notification here?
Quoted 8 lines
Standard
bpf_iterimplementations invoke the BPF program with v == NULL duringseq_ops->stop()to notify the program of EOF, enabling summary aggregate output. Because this function is empty, BPF programs attached to thepage_owneriterator cannot detect the end of iteration.Does this mean any logic relying on the standard EOF signal to output summary statistics or perform cleanup will never execute?
page_owner_seq_stop() didn't implement the EOF (v == NULL) run; I'll add it.
Quoted 16 lines
mm/page_owner_iter.c line 59
Quoted 11 lines
mm/page_owner_iter.c new file +11
-0+ +static int page_owner_prog_seq_show (struct bpf_prog *prog, +struct bpf_iter_meta *meta,void *v) +{ +struct page_owner_scan *scan = meta->seq->private; +struct bpf_iter__page_owner ctx; + +/* @po: hit-time snapshot, no RCU window needed. */ + ctx.meta = meta; + ctx.page = (struct page *)v; + ctx.po = &scan->po_snap;[Severity: Medium]
If EOF were properly signaled with v == NULL, would this unconditional assignment provide stale data to the BPF program?
Quoted 5 lines
By unconditionally assigning ctx.po to the snapshot pointer, ctx.po is never NULL, even when v (the page) is NULL. Does this mean BPF programs cannot reliably use if (!ctx->po) to detect EOF, and might operate on stale snapshot data from the last valid page?
That ctx.po issue only arises with the EOF run, so I'll fix both together.
Thanks,
Zhen
PATCH 3/6 mm/page_owner: add open-coded page_owner iterator
In reply to Zhen NiAdd an open-coded bpf_iter over the pages eligible for page_owner output, reusing page_owner_next_eligible().
A sleepable BPF program drives it with bpf_iter_page_owner_new()/next()/destroy(); each next() returns the page_owner_scan. po_snap is copied inside the page_ext RCU window, so the program needs no RCU section when it reads it.
Also add bpf_page_owner_stack_snprint(), which renders the stack trace for a stack_depot handle into a caller-provided buffer.
--- kernel/bpf/helpers.c | 6 +++++ mm/page_owner_iter.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+)
kernel/bpf/helpers.c +6 -0
@@ -4946,6 +4946,12 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_new, KF_ITER_NEW | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)#endif +#ifdef CONFIG_PAGE_OWNER +BTF_ID_FLAGS(func, bpf_iter_page_owner_new, KF_ITER_NEW | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_iter_page_owner_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_iter_page_owner_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_page_owner_stack_snprint, KF_SLEEPABLE) +#endif BTF_ID_FLAGS(func, __bpf_trap)BTF_ID_FLAGS (func, bpf_strcmp,KF_PERFMON );BTF_ID_FLAGS (func, bpf_strcasecmp,KF_PERFMON );
mm/page_owner_iter.c +52 -0
61 lines not on this page · raw message
Signature · 1 line
2.20.1
[email protected]
AI reviewRe: [PATCH 3/6] mm/page_owner: add open-coded page_owner iterator
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] bpf: buffer overflow in bpf_page_owner_stack_snprint() due to missing __sz suffix - [High]…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] bpf: buffer overflow in bpf_page_owner_stack_snprint() due to missing __sz suffix
- [High] bpf: Panic DoS and OOB read via untrusted stack depot handle in bpf_page_owner_stack_snprint() --
commit f5344767004d7fb48b115dd782835818e60504bc
Author: Zhen Ni <[email protected]>
mm/page_owner: add open-coded page_owner iterator
This commit adds an open-coded bpf_iter for pages eligible for page_owner output. It also introduces a bpf_page_owner_stack_snprint() kfunc to render the stack trace for a stack_depot handle into a caller-provided buffer.
Quoted 4 lines
mm/page_owner_iter.c +0
-0
[ ... ]
Quoted 1 line
mm/page_owner_iter.c +0
-0@@ -114,3 +115,54 @@ int page_owner_iter_register(void)
[ ... ]
mm/page_owner_iter.c int page_owner_iter_register(void) · line 164
Quoted 3 lines
mm/page_owner_iter.c +3
-0+__bpf_kfunc int bpf_page_owner_stack_snprint(depot_stack_handle_t handle, +char *buf,u32 buf_size) +{
[Severity: Critical]
Does this kfunc bypass the verifier's bounds checking for the buffer?
Because the buffer size argument is named buf_size instead of using the required __sz or __szk suffix (such as buf__sz), the BPF verifier treats it as an independent integer scalar. It only validates that the buffer points to a memory region of at least 1 byte.
If a BPF program passes a 1-byte valid buffer along with a maliciously large buf_size, stack_depot_snprint() will blindly write past the end of the buffer, which can corrupt adjacent stack, heap, or map memory.
[Severity: High]
Is it safe to accept an untrusted depot_stack_handle_t directly from the BPF program here?
Since the BPF verifier does not track scalar bounds for opaque handles, a program could pass a forged handle. If depot_fetch_stack() receives an invalid pool index, it unconditionally triggers a kernel warning:
lib/stackdepot.c:depot_fetch_stack() {
if (pool_index >= pools_num_cached) {
WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n",
pool_index, pools_num_cached, handle);
return NULL;
}
...
}This can panic systems configured with panic_on_warn=1.
Even if the index is valid, a maximized offset could cause the stack pointer to point near the end of the pool allocation. Accessing the stack size and entries would then result in an out-of-bounds read past the allocated pool. This can cause a hard page fault panic, or format arbitrary kernel pointers into the BPF-provided buffer, which the program could then exfiltrate.
Would it be better to change this kfunc to accept a trusted struct page_owner_scan *scan instead of a raw scalar?
Quoted 4 lines
mm/page_owner_iter.c +4
-0+return stack_depot_snprint (handle, buf, buf_size,0 ); +} + +__bpf_kfunc_end_defs ();
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 3/6] mm/page_owner: add open-coded page_owner iterator
Quoted 28 lines · 在 2026/10/9 20:00, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] bpf: buffer overflow inbpf_page_owner_stack_snprint()due to missing__szsuffix
- [High] bpf: Panic DoS and OOB read via untrusted stack depot handle inbpf_page_owner_stack_snprint()--commit
f5344767004d7fb48b115dd782835818e60504bc
Author: Zhen Ni <[email protected]>
mm/page_owner: add open-codedpage_owneriteratorThis commit adds an open-coded
bpf_iterfor pages eligible forpage_owneroutput. It also introduces abpf_page_owner_stack_snprint()kfunc to render the stack trace for astack_depothandle into a caller-provided buffer.Quoted 4 lines
mm/page_owner_iter.c +0
-0[ ... ]
Quoted 1 line
mm/page_owner_iter.c +0
-0@@ -114,3 +115,54 @@ int page_owner_iter_register(void)[ ... ]
mm/page_owner_iter.c int page_owner_iter_register(void) · line 164
Quoted 3 lines
mm/page_owner_iter.c +3
-0+__bpf_kfunc int bpf_page_owner_stack_snprint(depot_stack_handle_t handle, +char *buf,u32 buf_size) +{[Severity: Critical]
Does this kfunc bypass the verifier's bounds checking for the buffer?
Quoted 9 lines
Because the buffer size argument is named
buf_sizeinstead of using the required__szor__szksuffix (such asbuf__sz), the BPF verifier treats it as an independent integer scalar. It only validates that the buffer points to a memory region of at least 1 byte.If a BPF program passes a 1-byte valid buffer along with a maliciously large
buf_size,stack_depot_snprint()will blindly write past the end of the buffer, which can corrupt adjacent stack, heap, or map memory.
The size argument will be renamed to buf__sz.
Quoted 25 lines
[Severity: High]
Is it safe to accept an untrusteddepot_stack_handle_tdirectly from the BPF program here?Since the BPF verifier does not track scalar bounds for opaque handles, a program could pass a forged handle. If
depot_fetch_stack()receives an invalid pool index, it unconditionally triggers a kernel warning:lib/stackdepot.c:depot_fetch_stack() { if (pool_index >= pools_num_cached) { WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", pool_index, pools_num_cached, handle); return NULL; } ... }This can panic systems configured with panic_on_warn=1.
Even if the index is valid, a maximized offset could cause the stack pointer to point near the end of the pool allocation. Accessing the stack size and entries would then result in an out-of-bounds read past the allocated pool. This can cause a hard page fault panic, or format arbitrary kernel pointers into the BPF-provided buffer, which the program could then exfiltrate.
Quoted 8 lines
Would it be better to change this kfunc to accept a trusted struct
page_owner_scan*scan instead of a raw scalar?mm/page_owner_iter.c int page_owner_iter_register(void) · line 168
Quoted 4 lines
mm/page_owner_iter.c +4
-0+return stack_depot_snprint (handle, buf, buf_size,0 ); +} + +__bpf_kfunc_end_defs ();
I will take the trusted struct page_owner_scan * returned by bpf_iter_page_owner_next() and use scan->po_snap.handle.
Thanks,
Zhen
PATCH 4/6 mm/page_owner: add bpf_page_owner_get_nid() kfunc
In reply to Zhen NiExtract the NUMA node lookup from read_page_owner() into a shared page_owner_get_nid() helper, and expose it to BPF programs as bpf_page_owner_get_nid().
The nid bitfield layout is build-time configuration, so BPF programs cannot accurately derive it from page->flags.
--- kernel/bpf/helpers.c | 1 + mm/page_owner.c | 26 +++++++++++++++----------- mm/page_owner.h | 1 + mm/page_owner_iter.c | 5 +++++ 4 files changed, 22 insertions(+), 11 deletions(-)
kernel/bpf/helpers.c +1 -0
@@ -4951,6 +4951,7 @@ BTF_ID_FLAGS(func, bpf_iter_page_owner_new, KF_ITER_NEW | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_iter_page_owner_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_iter_page_owner_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_page_owner_stack_snprint, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_page_owner_get_nid, KF_SLEEPABLE)#endif BTF_ID_FLAGS(func, __bpf_trap)BTF_ID_FLAGS (func, bpf_strcmp,KF_PERFMON );
mm/page_owner.c +15 -11
@@ -711,6 +711,19 @@ void __dump_page_owner(const struct page *page)page_ext_put (page_ext); } +/* +* NUMA node of @page, bypassing PF_POISONED_CHECK() in page_to_nid() to +* avoid VM_BUG_ON on poisoned pages; -1 for poisoned pages. +*/ +int page_owner_get_nid (struct page *page) +{ +memdesc_flags_t page_flags =READ_ONCE (page->flags); + +if (page_flags.f ==PAGE_POISON_PATTERN ) +return -1 ; +return memdesc_nid (&page_flags); +} +/* * Advance @scan to the next page eligible for page_owner output. * On a hit, fill scan->pfn/page/po_snap and return true; the caller
@@ -810,18 +823,9 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)/* Find an allocated page */ while (page_owner_next_eligible (&scan)) {if (state->nid_filter_enabled) { -int nid; -memdesc_flags_t page_flags = -READ_ONCE (scan.page->flags); +int nid =page_owner_get_nid (scan.page); -/* -* Bypass PF_POISONED_CHECK() in page_to_nid() to avoid -* VM_BUG_ON when accessing poisoned pages. -*/ -if (page_flags.f ==PAGE_POISON_PATTERN ) - goto skip_continue; - nid =memdesc_nid (&page_flags); -if (!node_isset (nid, state->nid_filter)) +if (nid <0 || !node_isset (nid, state->nid_filter)) goto skip_continue; }
mm/page_owner.h +1 -0
@@ -39,6 +39,7 @@ struct page_owner_scan { };bool page_owner_next_eligible (struct page_owner_scan *scan); +int page_owner_get_nid (struct page *page);/* * mm/page_owner_iter.c
mm/page_owner_iter.c +5 -0
@@ -165,4 +165,9 @@ __bpf_kfunc int bpf_page_owner_stack_snprint(depot_stack_handle_t handle,return stack_depot_snprint (handle, buf, buf_size,0 ); } +__bpf_kfunc int bpf_page_owner_get_nid(struct page_owner_scan *scan) +{ +return page_owner_get_nid (scan->page); +} +__bpf_kfunc_end_defs ();
Signature · 1 line
2.20.1
[email protected]
AI reviewRe: [PATCH 4/6] mm/page_owner: add bpf_page_owner_get_nid() kfunc
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] bpf: arbitrary kernel memory read via page_owner_scan stack aliasing - [High] bpf…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] bpf: arbitrary kernel memory read via page_owner_scan stack aliasing
- [High] bpf: bpf_page_owner_get_nid() oops on unmapped struct page
--
commit 86f6fa9f642d356a9518f6b22fec24555b1e1568
Author: Zhen Ni <[email protected]>
mm/page_owner: add bpf_page_owner_get_nid() kfunc
This commit extracts the NUMA node lookup from read_page_owner() into a shared page_owner_get_nid() helper. It then exposes this helper to BPF programs via a new bpf_page_owner_get_nid() kfunc.
Quoted 4 lines
mm/page_owner.c +0
-0
[ ... ]
mm/page_owner.c void __dump_page_owner(const struct page *page) · line 720
Quoted 11 lines
mm/page_owner.c +7
-0@@ -711,6 +711,19 @@ void __dump_page_owner(const struct page *page)page_ext_put (page_ext); } +/* +* NUMA node of @page, bypassing PF_POISONED_CHECK() in page_to_nid() to +* avoid VM_BUG_ON on poisoned pages; -1 for poisoned pages. +*/ +int page_owner_get_nid (struct page *page) +{ +memdesc_flags_t page_flags =READ_ONCE (page->flags);
[Severity: High]
Can this trigger a kernel oops on an unmapped struct page?
The KF_SLEEPABLE flag on bpf_iter_page_owner_next() allows the BPF program to be preempted or to explicitly sleep while holding the scan object.
If memory is hot-removed while the program sleeps, the vmemmap region backing the struct page pointer inside the scan object could be unmapped.
When the program wakes up and calls bpf_page_owner_get_nid(), this raw READ_ONCE(page->flags) would dereference the potentially unmapped pointer and crash the kernel.
Should bpf_page_owner_get_nid() use copy_from_kernel_nofault() or BPF probe read semantics to safely probe the memory instead?
Quoted 11 lines
mm/page_owner.c +6
-0+ +if (page_flags.f ==PAGE_POISON_PATTERN ) +return -1 ; +return memdesc_nid (&page_flags); +} +mm/page_owner_iter.c +0
-0
[ ... ]
mm/page_owner_iter.c __bpf_kfunc int bpf_page_owner_stack_snprint(depot_stack_handle_t handle, · line 171
Quoted 8 lines
mm/page_owner_iter.c +4
-0@@ -165,4 +165,9 @@ __bpf_kfunc int bpf_page_owner_stack_snprint(depot_stack_handle_t handle,return stack_depot_snprint (handle, buf, buf_size,0 ); } +__bpf_kfunc int bpf_page_owner_get_nid(struct page_owner_scan *scan) +{ +return page_owner_get_nid (scan->page); +}
[Severity: High]
Is there a risk of an arbitrary kernel memory read here?
Because the scan object physically resides on the BPF program's stack (as returned by bpf_iter_page_owner_next()), a BPF program can overwrite the scan->page pointer with an arbitrary kernel address using normal BPF stack pointers (PTR_TO_STACK).
If the program then passes the aliased PTR_TO_BTF_ID to bpf_page_owner_get_nid(), wouldn't this kfunc blindly dereference the forged pointer, bypassing verifier safety checks?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH 4/6] mm/page_owner: add bpf_page_owner_get_nid() kfunc
Quoted 40 lines · 在 2026/10/9 20:20, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] bpf: arbitrary kernel memory read viapage_owner_scanstack aliasing
- [High] bpf:bpf_page_owner_get_nid()oops on unmapped struct page
--commit
86f6fa9f642d356a9518f6b22fec24555b1e1568
Author: Zhen Ni <[email protected]>
mm/page_owner: addbpf_page_owner_get_nid()kfuncThis commit extracts the NUMA node lookup from
read_page_owner()into a sharedpage_owner_get_nid()helper. It then exposes this helper to BPF programs via a newbpf_page_owner_get_nid()kfunc.Quoted 4 lines
mm/page_owner.c +0
-0[ ... ]
mm/page_owner.c void __dump_page_owner(const struct page *page) · line 720
Quoted 11 lines
mm/page_owner.c +7
-0@@ -711,6 +711,19 @@ void __dump_page_owner(const struct page *page)page_ext_put (page_ext); } +/* +* NUMA node of @page, bypassing PF_POISONED_CHECK() in page_to_nid() to +* avoid VM_BUG_ON on poisoned pages; -1 for poisoned pages. +*/ +int page_owner_get_nid (struct page *page) +{ +memdesc_flags_t page_flags =READ_ONCE (page->flags);[Severity: High]
Can this trigger a kernel oops on an unmapped struct page?The
KF_SLEEPABLEflag onbpf_iter_page_owner_next()allows the BPF program to be preempted or to explicitly sleep while holding the scan object.If memory is hot-removed while the program sleeps, the vmemmap region backing the struct page pointer inside the scan object could be unmapped.
Quoted 7 lines
When the program wakes up and calls
bpf_page_owner_get_nid(), this rawREAD_ONCE(page->flags)would dereference the potentially unmapped pointer and crash the kernel.Should
bpf_page_owner_get_nid()usecopy_from_kernel_nofault()or BPF probe read semantics to safely probe the memory instead?
The pfn->page dereference racing memory hot-remove is pre-existing: the debugfs read path already does READ_ONCE(page->flags) the same way. But a standalone sleepable kfunc does widen the window, since the program can sleep between next() and get_nid() while holding the page pointer.
Rather than harden that kfunc, I'll drop it -- the NUMA node can be computed inside page_owner_next_eligible() at hit time, while the page is still valid and inside the walk, and stored in a new scan->nid field.
Quoted 24 lines
Quoted 11 lines
mm/page_owner.c +6
-0+ +if (page_flags.f ==PAGE_POISON_PATTERN ) +return -1 ; +return memdesc_nid (&page_flags); +} +mm/page_owner_iter.c +0
-0[ ... ]
mm/page_owner_iter.c __bpf_kfunc int bpf_page_owner_stack_snprint(depot_stack_handle_t handle, · line 171
Quoted 8 lines
mm/page_owner_iter.c +4
-0@@ -165,4 +165,9 @@ __bpf_kfunc int bpf_page_owner_stack_snprint(depot_stack_handle_t handle,return stack_depot_snprint (handle, buf, buf_size,0 ); } +__bpf_kfunc int bpf_page_owner_get_nid(struct page_owner_scan *scan) +{ +return page_owner_get_nid (scan->page); +}[Severity: High]
Is there a risk of an arbitrary kernel memory read here?
Quoted 9 lines
Because the scan object physically resides on the BPF program's stack (as returned by
bpf_iter_page_owner_next()), a BPF program can overwrite thescan->pagepointer with an arbitrary kernel address using normal BPF stack pointers (PTR_TO_STACK).If the program then passes the aliased
PTR_TO_BTF_IDtobpf_page_owner_get_nid(), wouldn't this kfunc blindly dereference the forged pointer, bypassing verifier safety checks?
See the message above for the fix.
Thanks,
Zhen
PATCH 5/6 mm/page_owner: add bpf_page_owner_get_memcg_info() kfunc
In reply to Zhen NiExpose per-page memcg information to BPF programs, so that a bpf_iter over page_owner can aggregate and filter allocations by memcg. Attributing an allocation to a specific cgroup needs its full cgroup path, not just the memcg name.
Split memcg info collection out of print_page_owner_memcg() into get_page_memcg_info(), which fills struct memcg_info and, when given a caller-provided buffer, writes the full cgroup path via cgroup_path(). print_page_owner_memcg() now consumes the struct; this introduces no functional change to the debugfs output.
The helper takes a single READ_ONCE snapshot within one RCU read-side critical section, so the flags and cgroup path reflect a consistent view of the page. cgroup_path() returns the path relative to the cgroup hierarchy root, so memcg attribution works on both cgroup v1 and v2.
--- kernel/bpf/helpers.c | 1 + mm/page_owner.c | 85 ++++++++++++++++++++++++++++++++++---------- mm/page_owner.h | 9 +++++ mm/page_owner_iter.c | 7 ++++ 4 files changed, 83 insertions(+), 19 deletions(-)
kernel/bpf/helpers.c +1 -0
@@ -4952,6 +4952,7 @@ BTF_ID_FLAGS(func, bpf_iter_page_owner_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLE BTF_ID_FLAGS(func, bpf_iter_page_owner_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_page_owner_stack_snprint, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_page_owner_get_nid, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_page_owner_get_memcg_info, KF_SLEEPABLE)#endif BTF_ID_FLAGS(func, __bpf_trap)BTF_ID_FLAGS (func, bpf_strcmp,KF_PERFMON );
mm/page_owner.c +66 -19
141 lines not on this page · raw message
mm/page_owner.h +9 -0
@@ -38,8 +38,17 @@ struct page_owner_scan {struct page_owner po_snap; }; +struct memcg_info { +char name[80 ]; +bool is_slab; +bool is_kmem; +bool is_online; +}; +bool page_owner_next_eligible (struct page_owner_scan *scan);int page_owner_get_nid (struct page *page); +int get_page_memcg_info (struct page *page,struct memcg_info *info, +char *buf,int size);/* * mm/page_owner_iter.c
mm/page_owner_iter.c +7 -0
@@ -170,4 +170,11 @@ __bpf_kfunc int bpf_page_owner_get_nid(struct page_owner_scan *scan)return page_owner_get_nid (scan->page); } +__bpf_kfunc int bpf_page_owner_get_memcg_info(struct page_owner_scan *scan, +struct memcg_info *info, +char *buf,u32 size) +{ +return get_page_memcg_info (scan->page, info, buf, size); +} +__bpf_kfunc_end_defs ();
Signature · 1 line
2.20.1
[email protected]
AI reviewRe: [PATCH 5/6] mm/page_owner: add bpf_page_owner_get_memcg_info() kfunc
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] bpf: Missing __sz annotation allows BPF stack overflow in memcg kfunc - [High] bpf: missing…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] bpf: Missing __sz annotation allows BPF stack overflow in memcg kfunc
- [High] bpf: missing __sz annotation on size param causes stack buffer overflow --
commit 98bc7e204a48312a6be1bc67be9ade762ffed9ff
Author: Zhen Ni <[email protected]>
mm/page_owner: add bpf_page_owner_get_memcg_info() kfunc
This commit introduces a new BPF kfunc to expose per-page memcg information, including the cgroup path. It extracts memcg data collection into a new helper function that the kfunc calls directly.
Quoted 4 lines
mm/page_owner_iter.c +0
-0
[ ... ]
mm/page_owner_iter.c __bpf_kfunc int bpf_page_owner_get_nid(struct page_owner_scan *scan) · line 175
Quoted 7 lines
mm/page_owner_iter.c +3
-0@@ -170,4 +170,11 @@ __bpf_kfunc int bpf_page_owner_get_nid(struct page_owner_scan *scan)return page_owner_get_nid (scan->page); } +__bpf_kfunc int bpf_page_owner_get_memcg_info(struct page_owner_scan *scan, +struct memcg_info *info, +char *buf,u32 size)
[Severity: Critical]
Does this size parameter need the __sz suffix for the verifier to track memory bounds correctly?
Because the parameter is named size rather than buf__sz, it appears the verifier might treat buf as a fixed 1-byte allocation and size as an unconstrained scalar.
Could this allow a BPF program to pass a 1-byte stack buffer alongside an arbitrarily large size value? If so, when this calls into get_page_memcg_info() in mm/page_owner.c and executes:
get_page_memcg_info()
...
if (buf && size)
cgroup_path(memcg->css.cgroup, buf, size);can it overwrite the BPF stack and corrupt adjacent kernel memory?
[Severity: High]
Do the buf and info parameters also need the __uninit suffix?
Without this suffix, the verifier might improperly force BPF programs to pre-initialize these output variables just to pass verification.
Quoted 5 lines
mm/page_owner_iter.c +4
-0+{ +return get_page_memcg_info (scan->page, info, buf, size); +} +__bpf_kfunc_end_defs ();
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH 5/6] mm/page_owner: add bpf_page_owner_get_memcg_info() kfunc
Quoted 39 lines · 在 2026/10/9 20:43, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] bpf: Missing__szannotation allows BPF stack overflow in memcg kfunc
- [High] bpf: missing__szannotation on size param causes stack buffer overflow --commit
98bc7e204a48312a6be1bc67be9ade762ffed9ff
Author: Zhen Ni <[email protected]>
mm/page_owner: addbpf_page_owner_get_memcg_info()kfuncThis commit introduces a new BPF kfunc to expose per-page memcg information, including the cgroup path. It extracts memcg data collection into a new helper function that the kfunc calls directly.
Quoted 4 lines
mm/page_owner_iter.c +0
-0[ ... ]
mm/page_owner_iter.c __bpf_kfunc int bpf_page_owner_get_nid(struct page_owner_scan *scan) · line 175
Quoted 7 lines
mm/page_owner_iter.c +3
-0@@ -170,4 +170,11 @@ __bpf_kfunc int bpf_page_owner_get_nid(struct page_owner_scan *scan)return page_owner_get_nid (scan->page); } +__bpf_kfunc int bpf_page_owner_get_memcg_info(struct page_owner_scan *scan, +struct memcg_info *info, +char *buf,u32 size)[Severity: Critical]
Does this size parameter need the__szsuffix for the verifier to track memory bounds correctly?Because the parameter is named size rather than
buf__sz, it appears the verifier might treat buf as a fixed 1-byte allocation and size as an unconstrained scalar.Could this allow a BPF program to pass a 1-byte stack buffer alongside an arbitrarily large size value? If so, when this calls into
get_page_memcg_info()inmm/page_owner.cand executes:
Quoted 7 lines
get_page_memcg_info()... if (buf && size) cgroup_path(memcg->css.cgroup, buf, size);can it overwrite the BPF stack and corrupt adjacent kernel memory?
The size argument will be renamed to buf__sz.
Quoted 12 lines
[Severity: High]
Do the buf and info parameters also need the__uninitsuffix?Without this suffix, the verifier might improperly force BPF programs to pre-initialize these output variables just to pass verification.
mm/page_owner_iter.c __bpf_kfunc int bpf_page_owner_get_nid(struct page_owner_scan *scan) · line 180
Quoted 5 lines
mm/page_owner_iter.c +4
-0+{ +return get_page_memcg_info (scan->page, info, buf, size); +} +__bpf_kfunc_end_defs ();
The __uninit suffix will be added to the buf and info parameters.
Thanks,
Zhen
PATCH 6/6 selftests/bpf: add tests for the page_owner bpf_iter target
In reply to Zhen NiAdd a sleepable BPF program and a userspace runner covering the seq_file and open-coded page_owner iterators and the get_nid, stack_snprint and get_memcg_info kfuncs. The test checks:
- seq_file walk, unfiltered and pid-filtered: records read back from the iterator fd match the lines the program printed.
- open-coded iterator parity with the seq path.
- the kfuncs' contracts: valid nid per page, a non-empty stack trace, and memcg classification.
Skipped when debugfs page_owner is unavailable.
--- .../bpf/prog_tests/page_owner_iter.c | 173 ++++++++++++++++++ .../selftests/bpf/progs/bpf_iter_page_owner.c | 161 ++++++++++++++++ 2 files changed, 334 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/page_owner_iter.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c
tools/testing/selftests/bpf/prog_tests/page_owner_iter.c new file +173 -0
@@ -0,0 +1,173 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 */ + +#include <test_progs.h> +#include "bpf_iter_page_owner.skel.h" + +#define DEBUGFS_PAGE_OWNER "/sys/kernel/debug/page_owner" + +static bool page_owner_available (void ) +{ +return access (DEBUGFS_PAGE_OWNER ,R_OK ) ==0 ; +} + +/* Count link_fd output lines; -1 on error. */ +static long iter_record_count (int link_fd) +{ +char *line =NULL ; +size_t len =0 ; +long count =0 ; +int iter_fd, err; +FILE *f; + + iter_fd =bpf_iter_create (link_fd); +if (!ASSERT_GE (iter_fd,0 ,"bpf_iter_create" )) +return -1 ; + + f =fdopen (iter_fd,"r" ); +if (!f) { +close (iter_fd); +return -1 ; + } +while (getline (&line, &len, f) >0 ) + count++; + err =ferror (f); +free (line); +fclose (f); +return err ?-1 : count; +} + +/* Validate bpf_page_owner_get_memcg_info() results from the open-coded run. */ +static void check_memcg_skel (struct bpf_iter_page_owner *skel) +{ +fprintf (stderr, +"memcg: seen=%u charged=%u online=%u nodata=%u notsup=%u bad=%u first='%s' name='%s' flags=%u\n" , + skel->bss->memcg_seen, skel->bss->memcg_charged, + skel->bss->memcg_online, skel->bss->memcg_nodata, + skel->bss->memcg_notsup, skel->bss->memcg_bad, + skel->bss->memcg_first_path, skel->bss->memcg_first_name, + skel->bss->memcg_first_flags); + +if (!ASSERT_GT (skel->bss->memcg_seen,0 ,"memcg_seen_gt_0" )) +return ; + +/* all -EOPNOTSUPP: CONFIG_MEMCG is off */ +if (skel->bss->memcg_notsup == skel->bss->memcg_seen) { +ASSERT_EQ (skel->bss->memcg_bad,0 ,"memcg_stub_invariants" ); +return ; + } + +if (!ASSERT_EQ (skel->bss->memcg_bad,0 ,"memcg_kfunc_invariants" )) +return ; + +/* Every call is either charged or nodata */ +if (!ASSERT_EQ (skel->bss->memcg_charged + skel->bss->memcg_nodata, + skel->bss->memcg_seen,"memcg_ret_classified" )) +return ; + +if (!skel->bss->memcg_first_saved) +return ; + +if (!ASSERT_EQ (skel->bss->memcg_first_path[0 ],'/' , +"memcg_first_path_abs" )) +return ; + +if (skel->bss->memcg_first_name[0 ]) +ASSERT_OK_PTR (strstr (skel->bss->memcg_first_path, + skel->bss->memcg_first_name), +"memcg_name_in_path" ); +} + +void serial_test_page_owner_iter (void ) +{ +struct bpf_iter_page_owner *skel =NULL ; +LIBBPF_OPTS (bpf_test_run_opts, opts); +long iter_count, unfiltered_count, oc_diff, pid; +unsigned int printed_before; +int link_fd, prog_fd; + +if (!page_owner_available ()) { +test__skip (); +return ; + } + + skel =bpf_iter_page_owner__open_and_load (); +if (!ASSERT_OK_PTR (skel,"bpf_iter_page_owner__open_and_load" )) +return ; + +/* --- seq file iterator: unfiltered run --- */ + skel->bss->filter_en =0 ; + +if (!ASSERT_OK (bpf_iter_page_owner__attach (skel),"skel_attach" )) + goto destroy; + + link_fd =bpf_link__fd (skel->links.dump_page_owner); + iter_count =iter_record_count (link_fd); +if (iter_count <0 ) + goto destroy; + unfiltered_count = iter_count; + +fprintf (stderr,"iter_count=%ld printed_lines=%u total_seen=%u\n" , + iter_count, skel->bss->printed_lines, skel->bss->total_seen); + +ASSERT_GT (iter_count,0 ,"unfiltered_lines_gt_0" ); + +ASSERT_EQ ((long )skel->bss->printed_lines, iter_count, +"lines_match_printed" ); + +/* unfiltered: every visited page is printed, so the two must match */ +ASSERT_EQ (skel->bss->total_seen, skel->bss->printed_lines, +"unfiltered_all_printed" ); + +/* --- seq file iterator: pid-filtered run --- */ + pid =getpid (); + skel->bss->filter_en =1 ; + skel->bss->filter_pid = (pid_t )pid; + +/* printed_lines is cumulative across runs: compare the delta */ + printed_before = skel->bss->printed_lines; + iter_count =iter_record_count (link_fd); +if (iter_count <0 ) + goto destroy; + +fprintf (stderr, +"pid=%ld iter_count=%ld printed_delta=%u\n" , + pid, iter_count, skel->bss->printed_lines - printed_before); + +ASSERT_GT (iter_count,0 ,"filtered_lines_gt_0" ); +ASSERT_EQ (skel->bss->printed_lines - printed_before, iter_count, +"filtered_printed_match" ); + +/* --- open-coded iterator parity --- */ + prog_fd =bpf_program__fd (skel->progs.scan_open_coded); + +ASSERT_OK (bpf_prog_test_run_opts (prog_fd, &opts),"prog_test_run" ); +fprintf (stderr, +"open_coded_seen=%u nid_seen=%u first_nid=%d " +"stack_bytes=%u stack=[\n%s]\n" , + skel->bss->open_coded_seen, skel->bss->nid_seen, + skel->data->first_nid, skel->bss->stack_bytes, + skel->bss->stack_text); + +/* same eligibility as the seq path: counts must be close */ +ASSERT_GT (skel->bss->open_coded_seen,0 ,"oc_seen_gt_0" ); + oc_diff = (long )skel->bss->open_coded_seen - unfiltered_count; +if (oc_diff <0 ) + oc_diff = -oc_diff; +ASSERT_LE (oc_diff, unfiltered_count /100 +64 , +"oc_seen_close_to_iter" ); + +/* the nid kfunc returned a valid node for every page */ +ASSERT_EQ (skel->bss->nid_seen, skel->bss->open_coded_seen, +"nid_valid_all" ); +ASSERT_GE (skel->data->first_nid,0 ,"first_nid_valid" ); + +ASSERT_GT (skel->bss->stack_bytes,0 ,"stack_snprint_ok" ); +ASSERT_OK_PTR (strstr (skel->bss->stack_text,"+0x" ),"stack_frames" ); +ASSERT_OK_PTR (strchr (skel->bss->stack_text,'\n' ),"stack_lines" ); + +check_memcg_skel (skel); + +destroy: +bpf_iter_page_owner__destroy (skel); +}
tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c new file +161 -0
161 lines not on this page · raw message
Signature · 1 line
2.20.1
[email protected]
AI reviewRe: [PATCH 6/6] selftests/bpf: add tests for the page_owner bpf_iter target
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf: missing __sz suffix in kfunc buffer size parameters bypasses verifier bounds check --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: missing __sz suffix in kfunc buffer size parameters bypasses verifier bounds check --
commit 40eb734de063d435912cf0b8932fbb912e64be44
Author: Zhen Ni <[email protected]>
selftests/bpf: add tests for the page_owner bpf_iter target
This commit adds a sleepable BPF program and a userspace runner to test the page_owner BPF iterator. It exercises the seq_file and open-coded iterator paths and verifies the contracts of the get_nid, stack_snprint, and get_memcg_info kfuncs.
Quoted 5 lines
tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c new file +0
-0
[ ... ]
tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c line 48
Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c new file +10
-0+extern int bpf_iter_page_owner_new(struct bpf_iter_page_owner *it) __ksym; +extern struct page_owner_scan * +bpf_iter_page_owner_next (struct bpf_iter_page_owner *it) __ksym; +extern void bpf_iter_page_owner_destroy(struct bpf_iter_page_owner *it) __ksym; +extern int bpf_page_owner_stack_snprint(depot_stack_handle_t handle, + char *buf, u32 buf_size) __ksym; +extern int bpf_page_owner_get_nid(struct page_owner_scan *scan) __ksym; +extern int bpf_page_owner_get_memcg_info(struct page_owner_scan *scan, + struct memcg_info *info, + char *buf, u32 size) __ksym;
[Severity: High]
Does the kfunc ABI exercised here bypass verifier memory bounds checking?
The BPF verifier requires size parameters for memory buffers in kfuncs to be explicitly annotated with a __sz or __szk suffix (e.g., buf__sz). Without this suffix, the verifier treats the size parameter as an independent scalar and enforces no relationship between it and the buffer pointer.
In mm/page_owner_iter.c (which this newly added test relies upon), the kfuncs bpf_page_owner_stack_snprint and bpf_page_owner_get_memcg_info define buffer size arguments as u32 buf_size and u32 size, respectively, omitting the required suffix:
mm/page_owner_iter.c:bpf_page_owner_stack_snprint()
__bpf_kfunc int bpf_page_owner_stack_snprint(depot_stack_handle_t handle,
char *buf, u32 buf_size)
mm/page_owner_iter.c:bpf_page_owner_get_memcg_info()
__bpf_kfunc int bpf_page_owner_get_memcg_info(struct page_owner_scan *scan,
struct memcg_info *info, char *buf, u32 size)
Consequently, the verifier only validates that buf points to a 1-byte valid memory region (based on the char type) while allowing arbitrarily large scalar values to be passed as the size.
A BPF program could exploit this by providing a small buffer alongside a large size argument, causing the underlying kernel functions (stack_depot_snprint and get_page_memcg_info) to write past the bounds of the provided buffer, potentially leading to kernel memory corruption.
Could these kfunc size arguments in the kernel be renamed to include the __sz suffix to ensure proper verifier bounds checking?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH 6/6] selftests/bpf: add tests for the page_owner bpf_iter target
Quoted 59 lines · 在 2026/10/9 20:50, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: missing__szsuffix in kfunc buffer size parameters bypasses verifier bounds check --commit
40eb734de063d435912cf0b8932fbb912e64be44
Author: Zhen Ni <[email protected]>selftests/bpf: add tests for the
page_ownerbpf_itertargetThis commit adds a sleepable BPF program and a userspace runner to test the
page_ownerBPF iterator. It exercises theseq_fileand open-coded iterator paths and verifies the contracts of theget_nid,stack_snprint, andget_memcg_infokfuncs.Quoted 5 lines
tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c new file +0
-0[ ... ]
tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c line 48
Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c new file +10
-0+extern int bpf_iter_page_owner_new(struct bpf_iter_page_owner *it) __ksym; +extern struct page_owner_scan * +bpf_iter_page_owner_next (struct bpf_iter_page_owner *it) __ksym; +extern void bpf_iter_page_owner_destroy(struct bpf_iter_page_owner *it) __ksym; +extern int bpf_page_owner_stack_snprint(depot_stack_handle_t handle, + char *buf, u32 buf_size) __ksym; +extern int bpf_page_owner_get_nid(struct page_owner_scan *scan) __ksym; +extern int bpf_page_owner_get_memcg_info(struct page_owner_scan *scan, + struct memcg_info *info, + char *buf, u32 size) __ksym;[Severity: High]
Does the kfunc ABI exercised here bypass verifier memory bounds checking?The BPF verifier requires size parameters for memory buffers in kfuncs to be explicitly annotated with a
__szor__szksuffix (e.g.,buf__sz). Without this suffix, the verifier treats the size parameter as an independent scalar and enforces no relationship between it and the buffer pointer.In
mm/page_owner_iter.c(which this newly added test relies upon), the kfuncsbpf_page_owner_stack_snprintandbpf_page_owner_get_memcg_infodefine buffer size arguments as u32buf_sizeand u32 size, respectively, omitting the required suffix:mm/page_owner_iter.c:bpf_page_owner_stack_snprint()
__bpf_kfuncint bpf_page_owner_stack_snprint(depot_stack_handle_t handle,
char *buf, u32buf_size)mm/page_owner_iter.c:bpf_page_owner_get_memcg_info()
__bpf_kfuncint bpf_page_owner_get_memcg_info(structpage_owner_scan*scan,
structmemcg_info*info, char *buf, u32 size)Consequently, the verifier only validates that buf points to a 1-byte valid memory region (based on the char type) while allowing arbitrarily large scalar values to be passed as the size.
Quoted 8 lines
A BPF program could exploit this by providing a small buffer alongside a large size argument, causing the underlying kernel functions (
stack_depot_snprintandget_page_memcg_info) to write past the bounds of the provided buffer, potentially leading to kernel memory corruption.Could these kfunc size arguments in the kernel be renamed to include the
__szsuffix to ensure proper verifier bounds checking?
This is the same __sz issue. Will fix it.
Thanks,
Zhen
[email protected]
AI reviewRe: [PATCH 6/6] selftests/bpf: add tests for the page_owner bpf_iter target
In reply to Zhen NiTL;DR: - Selftest BPF program may fail to build, breaking test_progs, if CONFIG_PAGE_OWNER types are missing from vmlinux.h - Iterator read errors (e.g. -EAGAIN) silently end the test as a pass…
Quoted 1 line
selftests/bpf: add tests for the
page_ownerbpf_itertarget
TL;DR:
- Selftest BPF program may fail to build, breaking test_progs, if CONFIG_PAGE_OWNER types are missing from vmlinux.h
- Iterator read errors (e.g. -EAGAIN) silently end the test as a pass
- Skip gives no reason
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/page_owner_iter.c new file +0
-0@@ -0,0 +1,173 @@
[ ... ]
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/page_owner_iter.c new file +3
-0+/* Count link_fd output lines; -1 on error. */ +static long iter_record_count (int link_fd) +{
[ ... ]
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/page_owner_iter.c new file +12
-0+ f =fdopen (iter_fd,"r" ); +if (!f) { +close (iter_fd); +return -1 ; + } +while (getline (&line, &len, f) >0 ) + count++; + err =ferror (f); +free (line); +fclose (f); +return err ?-1 : count; +}
[ ... ]
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/page_owner_iter.c new file +2
-0+void serial_test_page_owner_iter (void ) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/page_owner_iter.c line 92
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/page_owner_iter.c new file +4
-0+if (!page_owner_available ()) { +test__skip (); +return ; + }
A subsystem pattern flags this as potentially concerning: a test that skips silently, with no message.
test__skip() takes no message and nothing is printed before it, so the result is a bare skip. The page_owner file only exists with CONFIG_PAGE_OWNER=y, page_owner=on on the kernel command line, and debugfs mounted, so someone reading the output cannot tell which of these was missing.
Other prog_tests print the reason before skipping, for example in bpf_iter.c:
printf("%s:SKIP:no __builtin_btf_type_id\n", __func__);
test__skip();Could this print why it skipped, for example that page_owner debugfs is unavailable and needs CONFIG_PAGE_OWNER plus page_owner=on?
That said, test_progs still reports the test as skipped in its summary, and many existing prog_tests call test__skip() without a message, so only the reason is missing here.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/page_owner_iter.c new file +5
-0+ + skel =bpf_iter_page_owner__open_and_load (); +if (!ASSERT_OK_PTR (skel,"bpf_iter_page_owner__open_and_load" )) +return ; +
tools/testing/selftests/bpf/prog_tests/page_owner_iter.c line 107
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/page_owner_iter.c new file +10
-0+/* --- seq file iterator: unfiltered run --- */ + skel->bss->filter_en =0 ; + +if (!ASSERT_OK (bpf_iter_page_owner__attach (skel),"skel_attach" )) + goto destroy; + + link_fd =bpf_link__fd (skel->links.dump_page_owner); + iter_count =iter_record_count (link_fd); +if (iter_count <0 ) + goto destroy;
If reading the iterator fails, can this test still report success without running any of its checks?
iter_record_count() only records an assertion failure when bpf_iter_create() fails. When fdopen() fails, or when getline() stops on a read error so that ferror(f) is set, it quietly returns -1.
The two callers in serial_test_page_owner_iter(), the unfiltered pass here and the pid-filtered pass below, then do:
if (iter_count < 0)
goto destroy;Neither calls ASSERT_*() or test__fail(), so test_progs marks the test as OK and skips the unfiltered, filtered, open-coded and memcg checks.
read() on a BPF iterator fd can fail with real errors from bpf_seq_read():
an ERR_PTR returned by the target's start()/next(), -E2BIG, -ENOMEM, or -EAGAIN.
The -EAGAIN case looks like a likely trigger in the pid-filtered pass. There dump_page_owner() returns 0 without printing for every page that does not belong to the test's pid, and bpf_seq_read() returns -EAGAIN once num_objs reaches MAX_ITER_OBJECTS (1000000) while offs == 0:
if (num_objs >= MAX_ITER_OBJECTS) {
if (offs == 0) {
err = -EAGAIN;
...So on a machine where more than about 1M consecutive iterated pages do not belong to the test process, the filtered pass would end in a silent pass.
Should these error paths fail the test, for example with ASSERT_OK_PTR() on the FILE * and an assertion on ferror()? Should -EAGAIN be retried rather than treated as an error?
[ ... ]
Quoted 11 lines
tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c new file +5
-0@@ -0,0 +1,161 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 */ + +#include <vmlinux.h> +#include <bpf/bpf_helpers.h>
[ ... ]
tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c line 121
Quoted 5 lines
tools/testing/selftests/bpf/progs/bpf_iter_page_owner.c new file +5
-0+SEC("syscall") +int scan_open_coded (const void *ctx) +{ +struct bpf_iter_page_owner it; +struct page_owner_scan *scan;
Does this program build against the kernels the BPF selftests are normally built and run on?
The program needs several types from vmlinux.h: struct bpf_iter_page_owner (declared as a stack variable here), struct page_owner_scan, struct memcg_info, struct bpf_iter__page_owner, and struct page_owner (dereferenced as po->pid and po->comm in dump_page_owner()).
struct page_owner is defined only in mm/page_owner.c, which is built only when CONFIG_PAGE_OWNER=y (obj-$(CONFIG_PAGE_OWNER) += page_owner.o in mm/Makefile). The new iterator and kfunc types live in the page_owner code added by the earlier patches in this series, so they are in BTF only when CONFIG_PAGE_OWNER is enabled too.
CONFIG_PAGE_OWNER is default n (mm/Kconfig.debug), and none of tools/testing/selftests/bpf/config, config.vm or config.<arch> enables it. This patch does not touch those files either.
The selftests build compiles every progs/*.c unconditionally:
Makefile.skel:
BPF_SRCS := $(notdir $(wildcard progs/*.c))
BPF_OBJS := $(patsubst %.c,$(RDIR)/%.bpf.o,$(BPF_SRCS))
Makefile:
$(OUTPUT)/test_progs: $(RUNNER_PREREQS) $(BPF_OBJS) $(ALL_SKELS) FORCEvmlinux.h is generated from the VMLINUX_BTF of the kernel under test. On a kernel built from the selftests config, this declaration fails with an incomplete-type error:
struct bpf_iter_page_owner it;
and ctx->po / po->pid fail the same way. That would fail the whole test_progs build, not just this test.
In the tree this patch was checked against, the earlier page_owner patches are not present either, so the file cannot compile there at all.
The runtime check in page_owner_available() cannot help, because the failure happens at build time.
Should CONFIG_PAGE_OWNER=y (plus its DEBUG_KERNEL dependency) be added to tools/testing/selftests/bpf/config? Or should the program use local type definitions with core relocations (___local flavors plus bpf_core_type_exists()) so it still compiles when the types are missing?
Also, pageowner_init() creates /sys/kernel/debug/page_owner only when booted with page_owner=on. Without that parameter the test will always skip on test machines, even after the config is fixed.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/37933988642