]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_b...
authorDavid Hildenbrand <david@redhat.com>
Tue, 10 Dec 2024 09:34:37 +0000 (10:34 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 19 Dec 2024 03:04:45 +0000 (19:04 -0800)
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.

Link: https://lkml.kernel.org/r/20241210093437.174413-1-david@redhat.com
Fixes: fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
Signed-off-by: David Hildenbrand <david@redhat.com>
Reported-by: Vlastimil Babka <vbabka@suse.cz>
Closes: https://lkml.kernel.org/r/e1a898ba-a717-4d20-9144-29df1a6c8813@suse.cz
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Zi Yan <ziy@nvidia.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Yu Zhao <yuzhao@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/page_alloc.c

index 1cb4b8c8886d8abffbf845ca5c8b023c3a427454..cae7b93864c23e50d9c89b9f579ac5bb6143a3a7 100644 (file)
@@ -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,