[PATCH v1 02/12] mm: check page validity when find a buddy page in a non-contiguous zone

From: Zi Yan
Date: Wed Sep 21 2022 - 21:13:06 EST


From: Zi Yan <ziy@xxxxxxxxxx>

When MAX_ORDER > section size, buddy page might not be valid when the zone
is non-contiguous. Check it and return NULL if buddy page is not valid.

For PFNs that not aligned to MAX_ORDER (usually at the beginning and end
of a zone), __free_pages_memory() clamps down the order to make sure
invalid PFN will not show up as a buddy PFN.

Signed-off-by: Zi Yan <ziy@xxxxxxxxxx>
---
mm/internal.h | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/mm/internal.h b/mm/internal.h
index b3002e03c28f..22fb1e6e3541 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -330,12 +330,16 @@ static inline struct page *find_buddy_page_pfn(struct page *page,
unsigned long pfn, unsigned int order, unsigned long *buddy_pfn)
{
unsigned long __buddy_pfn = __find_buddy_pfn(pfn, order);
+ struct zone *zone = page_zone(page);
struct page *buddy;

buddy = page + (__buddy_pfn - pfn);
if (buddy_pfn)
*buddy_pfn = __buddy_pfn;

+ if (unlikely(!zone->contiguous && !pfn_valid(__buddy_pfn)))
+ return NULL;
+
if (page_is_buddy(page, buddy, order))
return buddy;
return NULL;
--
2.35.1