mm: page_ext: add count limit to page_ext_iter_next to prevent invalid PFN access
Subsystems (3)
PATCH v3 1/1 mm: page_ext: add count limit to page_ext_iter_next to prevent invalid PFN access
The page_ext iteration API does not validate if the PFN still belongs to a valid section while advancing the iterator. When dynamically adding memory in the hotplug path, it can lead to a NULL pointer dereference during page_ext_lookup at the boundary of the last valid section when iterator count equals __pgcount.
The for_each_page_ext() macro calls page_ext_iter_next() as its loop increment. for_each_page_ext() does a
"__page_ext = page_ext_iter_next(&__iter)" at the end. This causes page_ext_iter_next() to increment iter->index past __pgcount and call page_ext_lookup(start_pfn + __pgcount). During memory hotplug (online), the PFN at start_pfn + __pgcount may belong to a section that has not yet been initialized, causing page_ext_lookup() to trigger a NULL pointer dereference.
[ 14.555124][ T846] Call trace: [ 14.555125][ T846] lookup_page_ext+0x6c/0x108 (P) [ 14.555127][ T846] page_ext_lookup+0x30/0x3c [ 14.555129][ T846] __reset_page_owner+0x11c/0x260 [ 14.571201][ T846] __free_pages_ok+0x5e8/0x8e0 [ 14.571204][ T846] __free_pages_core+0x78/0xf0 [ 14.571206][ T846] generic_online_page+0x14/0x24 [ 14.597782][ T846] online_pages+0x178/0x30c [ 14.597784][ T846] memory_block_change_state+0x284/0x32c [ 14.597787][ T846] memory_subsys_online+0x4c/0x64 [ 14.597789][ T846] device_online+0x88/0xb0 [ 14.597791][ T846] online_memory_block+0x30/0x40 [ 14.597793][ T846] walk_memory_blocks+0xac/0xe8 [ 14.597794][ T846] add_memory_resource+0x280/0x298 [ 14.656161][ T846] add_memory+0x60/0x98
Move the iteration boundary enforcement inside the iterator functions, so callers cannot inadvertently access beyond the requested range.
---
Changes in v3:
- Fix the iter->index++ increment to pre increment(++iter->index)
- modify the (count == 0) check to (!count)
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- Incorporated comments from David and Matthew to check for invalid PFN
in page_ext iterator rather than checking for NULL section in page_ext_lookup.
- Minor improvement in commit description to include the issue with page_ext_iter_next
- Link to v1: https://patch.msgid.link/[email protected]
To: Andrew Morton <[email protected]>
To: David Hildenbrand <[email protected]>
To: Lorenzo Stoakes <[email protected]>
To: "Liam R. Howlett" <[email protected]>
To: Vlastimil Babka <[email protected]>
To: Mike Rapoport <[email protected]>
To: Suren Baghdasaryan <[email protected]>
To: Michal Hocko <[email protected]>
To: Luiz Capitulino <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--- include/linux/page_ext.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
include/linux/page_ext.h +13 -6
@@ -120,14 +120,18 @@ struct page_ext_iter {* page_ext_iter_begin() - Prepare for iterating through page extensions. * @iter: page extension iterator. * @pfn: PFN of the page we're interested in. +* @count: maximum number of page extensions to return. * * Must be called with RCU read lock taken. * * Return: NULL if no page_ext exists for this page. */ static inline struct page_ext *page_ext_iter_begin (struct page_ext_iter *iter, -unsigned long pfn) +unsigned long pfn,unsigned long count) { +if (!count) +return NULL ; + iter->index =0 ; iter->start_pfn = pfn; iter->page_ext =page_ext_lookup (pfn);
@@ -138,19 +142,22 @@ static inline struct page_ext *page_ext_iter_begin(struct page_ext_iter *iter,/** * page_ext_iter_next() - Get next page extension * @iter: page extension iterator. +* @count: maximum number of page extensions to return. * * Must be called with RCU read lock taken. * * Return: NULL if no next page_ext exists. */ -static inline struct page_ext *page_ext_iter_next (struct page_ext_iter *iter) +static inline struct page_ext *page_ext_iter_next (struct page_ext_iter *iter, +unsigned long count) {unsigned long pfn;if (WARN_ON_ONCE (!iter->page_ext))return NULL ; - iter->index++; +if (++iter->index >= count) +return NULL ; pfn = iter->start_pfn + iter->index;if (page_ext_iter_next_fast_possible (pfn))
@@ -183,9 +190,9 @@ static inline struct page_ext *page_ext_iter_get(const struct page_ext_iter *ite* IMPORTANT: must be called with RCU read lock taken. */ #define for_each_page_ext (__page, __pgcount, __page_ext, __iter) \ - for (__page_ext = page_ext_iter_begin(&__iter, page_to_pfn(__page));\ - __page_ext && __iter.index < __pgcount; \ - __page_ext = page_ext_iter_next(&__iter)) + for (__page_ext = page_ext_iter_begin(&__iter, page_to_pfn(__page), __pgcount); \ + __page_ext; \ + __page_ext = page_ext_iter_next(&__iter, __pgcount))#else /* !CONFIG_PAGE_EXTENSION */ struct page_ext ;
---
base-commit: c425609d6ac4012c8bbf01ec2e10e801b1923a7b
change-id: 20260616-page_ext-31ef555456fc
Best regards,
Signature · 1 line
Ketan Kishore <[email protected]>
Quoted 35 lines · Ketan
The
page_extiteration API does not validate if the PFN still belongs to a valid section while advancing the iterator. When dynamically adding memory in the hotplug path, it can lead to a NULL pointer dereference duringpage_ext_lookupat the boundary of the last valid section when iterator count equals__pgcount.The
for_each_page_ext()macro callspage_ext_iter_next()as its loop increment.for_each_page_ext()does a
"__page_ext=page_ext_iter_next(&__iter)" at the end. This causespage_ext_iter_next()to incrementiter->indexpast__pgcountand callpage_ext_lookup(start_pfn + __pgcount). During memory hotplug (online), the PFN atstart_pfn+__pgcountmay belong to a section that has not yet been initialized, causingpage_ext_lookup()to trigger a NULL pointer dereference.[ 14.555124][ T846] Call trace: [ 14.555125][ T846] lookup_page_ext+0x6c/0x108 (P) [ 14.555127][ T846] page_ext_lookup+0x30/0x3c [ 14.555129][ T846] __reset_page_owner+0x11c/0x260 [ 14.571201][ T846] __free_pages_ok+0x5e8/0x8e0 [ 14.571204][ T846] __free_pages_core+0x78/0xf0 [ 14.571206][ T846] generic_online_page+0x14/0x24 [ 14.597782][ T846] online_pages+0x178/0x30c [ 14.597784][ T846] memory_block_change_state+0x284/0x32c [ 14.597787][ T846] memory_subsys_online+0x4c/0x64 [ 14.597789][ T846] device_online+0x88/0xb0 [ 14.597791][ T846] online_memory_block+0x30/0x40 [ 14.597793][ T846] walk_memory_blocks+0xac/0xe8 [ 14.597794][ T846] add_memory_resource+0x280/0x298 [ 14.656161][ T846] add_memory+0x60/0x98Move the iteration boundary enforcement inside the iterator functions, so callers cannot inadvertently access beyond the requested range.
Quoted 6 lines
Fixes 9039b9096ea2 ("mm: page_owner: use new iteration API")Suggested-by David Hildenbrand <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Ketan Kishore <[email protected]>Tested-by [email protected]
This is probably not needed.
Quoted 26 lines
---
Changes in v3:
- Fix the iter->index++ increment to preincrement(++iter->index)
- modify the (count == 0) check to (!count)
- Link to v2: https://patch.msgid.link/[email protected]Changes in v2:
- Incorporated comments from David and Matthew to check for invalid PFN
inpage_extiterator rather than checking for NULL section inpage_ext_lookup.
- Minor improvement in commit description to include the issue withpage_ext_iter_next
- Link to v1: https://patch.msgid.link/[email protected]To: Andrew Morton <[email protected]>
To: David Hildenbrand <[email protected]>
To: Lorenzo Stoakes <[email protected]>
To: "Liam R. Howlett" <[email protected]>
To: Vlastimil Babka <[email protected]>
To: Mike Rapoport <[email protected]>
To: Suren Baghdasaryan <[email protected]>
To: Michal Hocko <[email protected]>
To: Luiz Capitulino <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Quoted 4 lines
--- include/linux/page_ext.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
LGTM.
Signature · 2 lines
Best Regards, Yan, Zi
Quoted 36 lines · Zi Yan
Quoted 35 lines · Ketan
The
page_extiteration API does not validate if the PFN still belongs to a valid section while advancing the iterator. When dynamically adding memory in the hotplug path, it can lead to a NULL pointer dereference duringpage_ext_lookupat the boundary of the last valid section when iterator count equals__pgcount.The
for_each_page_ext()macro callspage_ext_iter_next()as its loop increment.for_each_page_ext()does a
"__page_ext=page_ext_iter_next(&__iter)" at the end. This causespage_ext_iter_next()to incrementiter->indexpast__pgcountand callpage_ext_lookup(start_pfn + __pgcount). During memory hotplug (online), the PFN atstart_pfn+__pgcountmay belong to a section that has not yet been initialized, causingpage_ext_lookup()to trigger a NULL pointer dereference.[ 14.555124][ T846] Call trace: [ 14.555125][ T846] lookup_page_ext+0x6c/0x108 (P) [ 14.555127][ T846] page_ext_lookup+0x30/0x3c [ 14.555129][ T846] __reset_page_owner+0x11c/0x260 [ 14.571201][ T846] __free_pages_ok+0x5e8/0x8e0 [ 14.571204][ T846] __free_pages_core+0x78/0xf0 [ 14.571206][ T846] generic_online_page+0x14/0x24 [ 14.597782][ T846] online_pages+0x178/0x30c [ 14.597784][ T846] memory_block_change_state+0x284/0x32c [ 14.597787][ T846] memory_subsys_online+0x4c/0x64 [ 14.597789][ T846] device_online+0x88/0xb0 [ 14.597791][ T846] online_memory_block+0x30/0x40 [ 14.597793][ T846] walk_memory_blocks+0xac/0xe8 [ 14.597794][ T846] add_memory_resource+0x280/0x298 [ 14.656161][ T846] add_memory+0x60/0x98Move the iteration boundary enforcement inside the iterator functions, so callers cannot inadvertently access beyond the requested range.
Quoted 7 lines
Quoted 6 lines
Fixes 9039b9096ea2 ("mm: page_owner: use new iteration API")Suggested-by David Hildenbrand <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Ketan Kishore <[email protected]>Tested-by [email protected]This is probably not needed.
Agreed, we usually don't include this.
Signature · 3 lines
Cheers, David