Skip to contentSkip to conversation

mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy()

PATCH Applied David Hildenbrand 4 participants 5 messages Dec 10, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy()

In split_large_buddy(), we might call pfn_to_page() on a PFN that might not exist. In corner cases, such as when freeing the highest pageblock in the last memory section, this could result with CONFIG_SPARSEMEM && !CONFIG_SPARSEMEM_EXTREME in __pfn_to_section() returning NULL and and __section_mem_map_addr() dereferencing that NULL pointer.

Let's fix it, and avoid doing a pfn_to_page() call for the first iteration, where we already have the page.

So far this was found by code inspection, but let's just CC stable as the fix is easy.

Fixes fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
Cc Andrew Morton <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Zi Yan <[email protected]>
Cc Yu Zhao <[email protected]>
---
 mm/page_alloc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/page_alloc.c +4 -2
index 48a291c485df4..a52c6022c65cb 100644
@@ -1238,13 +1238,15 @@ static void split_large_buddy(struct zone *zone, struct page *page,
 	if (order > pageblock_order)
 		order = pageblock_order;
 
-	while (pfn != end) {
+	do {
 		int mt = get_pfnblock_migratetype(page, pfn);
 
 		__free_one_page(page, pfn, zone, order, mt, fpi);
 		pfn += 1 << order;
+		if (pfn == end)
+			break;
 		page = pfn_to_page(pfn);
-	}
+	} while (1);
 }
 
 static void free_one_page(struct zone *zone, struct page *page,
Signature · 1 line
2.47.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · David Hildenbrand

In split_large_buddy(), we might call pfn_to_page() on a PFN that might not exist. In corner cases, such as when freeing the highest pageblock in the last memory section, this could result with CONFIG_SPARSEMEM && !CONFIG_SPARSEMEM_EXTREME in __pfn_to_section() returning NULL and and __section_mem_map_addr() dereferencing that NULL pointer.

Let's fix it, and avoid doing a pfn_to_page() call for the first iteration, where we already have the page.

So far this was found by code inspection, but let's just CC stable as the fix is easy.

Quoted 9 lines
Fixes fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
Cc Andrew Morton <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Zi Yan <[email protected]>
Cc Yu Zhao <[email protected]>

Thanks!

Quoted 26 lines
---
 mm/page_alloc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/page_alloc.c +4 -2
index 48a291c485df4..a52c6022c65cb 100644
@@ -1238,13 +1238,15 @@ static void split_large_buddy(struct zone *zone, struct page *page,
 	if (order > pageblock_order)
 		order = pageblock_order;
 
-	while (pfn != end) {
+	do {
 		int mt = get_pfnblock_migratetype(page, pfn);
 
 		__free_one_page(page, pfn, zone, order, mt, fpi);
 		pfn += 1 << order;
+		if (pfn == end)
+			break;
 		page = pfn_to_page(pfn);
-	}
+	} while (1);
 }
 
 static void free_one_page(struct zone *zone, struct page *page,
#2 lore raw
Quoted 23 lines · Vlastimil Babka
Quoted 12 lines · David Hildenbrand

In split_large_buddy(), we might call pfn_to_page() on a PFN that might not exist. In corner cases, such as when freeing the highest pageblock in the last memory section, this could result with CONFIG_SPARSEMEM && !CONFIG_SPARSEMEM_EXTREME in __pfn_to_section() returning NULL and and __section_mem_map_addr() dereferencing that NULL pointer.

Let's fix it, and avoid doing a pfn_to_page() call for the first iteration, where we already have the page.

So far this was found by code inspection, but let's just CC stable as the fix is easy.

Quoted 9 lines
Fixes fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
Cc Andrew Morton <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Zi Yan <[email protected]>
Cc Yu Zhao <[email protected]>
Quoted 1 line

BTW, staring at nr_isolate_pageblock accounting, I just stumbled over prep_move_freepages_block(), and I am not sure about the zone_spans_pfn() checks in there.

With overlapping zones, these are not reliably, which makes me believe that maybe these checks are not required at all. Or that there is a bug. Or that there is some implication that these checks are only required on systems without overlapping zones :)

Signature · 3 lines
Cheers,

David / dhildenb
reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to David Hildenbrand
Quoted 12 lines · David Hildenbrand

In split_large_buddy(), we might call pfn_to_page() on a PFN that might not exist. In corner cases, such as when freeing the highest pageblock in the last memory section, this could result with CONFIG_SPARSEMEM && !CONFIG_SPARSEMEM_EXTREME in __pfn_to_section() returning NULL and and __section_mem_map_addr() dereferencing that NULL pointer.

Let's fix it, and avoid doing a pfn_to_page() call for the first iteration, where we already have the page.

So far this was found by code inspection, but let's just CC stable as the fix is easy.

Quoted 9 lines
Fixes fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
Cc Andrew Morton <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Zi Yan <[email protected]>
Cc Yu Zhao <[email protected]>

Best Regards,
Yan, Zi

reviewer Acked-by on v1 1/1
#4 lore raw
In reply to David Hildenbrand
Quoted 12 lines · David Hildenbrand

In split_large_buddy(), we might call pfn_to_page() on a PFN that might not exist. In corner cases, such as when freeing the highest pageblock in the last memory section, this could result with CONFIG_SPARSEMEM && !CONFIG_SPARSEMEM_EXTREME in __pfn_to_section() returning NULL and and __section_mem_map_addr() dereferencing that NULL pointer.

Let's fix it, and avoid doing a pfn_to_page() call for the first iteration, where we already have the page.

So far this was found by code inspection, but let's just CC stable as the fix is easy.

Quoted 9 lines
Fixes fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
Cc Andrew Morton <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Zi Yan <[email protected]>
Cc Yu Zhao <[email protected]>