]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm/hugetlb: cleanup hugetlb_folio_init_tail_vmemmap()
authorDavid Hildenbrand <david@redhat.com>
Mon, 1 Sep 2025 15:03:34 +0000 (17:03 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 12 Sep 2025 00:25:32 +0000 (17:25 -0700)
We can now safely iterate over all pages in a folio, so no need for the
pfn_to_page().

Also, as we already force the refcount in __init_single_page() to 1
through init_page_count(), we can just set the refcount to 0 and avoid
page_ref_freeze() + VM_BUG_ON.  Likely, in the future, we would just want
to tell __init_single_page() to which value to initialize the refcount.

Further, adjust the comments to highlight that we are dealing with an
open-coded prep_compound_page() variant, and add another comment
explaining why we really need the __init_single_page() only on the tail
pages.

Note that the current code was likely problematic, but we never ran into
it: prep_compound_tail() would have been called with an offset that might
exceed a memory section, and prep_compound_tail() would have simply added
that offset to the page pointer -- which would not have done the right
thing on sparsemem without vmemmap.

Link: https://lkml.kernel.org/r/20250901150359.867252-14-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Acked-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/hugetlb.c

index a0464020f6c2d555442ae3216d9198054ec0594c..0a1760aa206ba7a6bb1e9530d0a1a1dc47071010 100644 (file)
@@ -3237,17 +3237,18 @@ static void __init hugetlb_folio_init_tail_vmemmap(struct folio *folio,
 {
        enum zone_type zone = zone_idx(folio_zone(folio));
        int nid = folio_nid(folio);
+       struct page *page = folio_page(folio, start_page_number);
        unsigned long head_pfn = folio_pfn(folio);
        unsigned long pfn, end_pfn = head_pfn + end_page_number;
-       int ret;
-
-       for (pfn = head_pfn + start_page_number; pfn < end_pfn; pfn++) {
-               struct page *page = pfn_to_page(pfn);
 
+       /*
+        * As we marked all tail pages with memblock_reserved_mark_noinit(),
+        * we must initialize them ourselves here.
+        */
+       for (pfn = head_pfn + start_page_number; pfn < end_pfn; page++, pfn++) {
                __init_single_page(page, pfn, zone, nid);
                prep_compound_tail((struct page *)folio, pfn - head_pfn);
-               ret = page_ref_freeze(page, 1);
-               VM_BUG_ON(!ret);
+               set_page_count(page, 0);
        }
 }
 
@@ -3257,12 +3258,15 @@ static void __init hugetlb_folio_init_vmemmap(struct folio *folio,
 {
        int ret;
 
-       /* Prepare folio head */
+       /*
+        * This is an open-coded prep_compound_page() whereby we avoid
+        * walking pages twice by initializing/preparing+freezing them in the
+        * same go.
+        */
        __folio_clear_reserved(folio);
        __folio_set_head(folio);
        ret = folio_ref_freeze(folio, 1);
        VM_BUG_ON(!ret);
-       /* Initialize the necessary tail struct pages */
        hugetlb_folio_init_tail_vmemmap(folio, 1, nr_pages);
        prep_compound_head((struct page *)folio, huge_page_order(h));
 }