From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C55C3750B6 for ; Wed, 13 May 2026 13:08:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778677732; cv=none; b=uohAdp8SKI3ntd5T+vxHYboUtmEKzlSXODzr2OQdyIipEFFhba8agZyCD13GQxaNAm++sO1E+qqnvkAHmiSKtloxw2Snt6iTD/RdGitaTkjO+dQ174tpjcqBGaoZOUnj8iUfuWnXMzXqWK/WdlxmUqUShyuyrPxR2P+eEQCW6XQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778677732; c=relaxed/simple; bh=YfH4Q8kJDSdifJvpXS1pm3V9jl0RWNK01uvv/yGc4Uw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D14i7dolt+qfCjuoM9J9W/qH5+FiFNbvOBh+H+DRwTNcQ1SetL7MT7KJxVsQXTkO5hFUXwPG98aFm8ZIU/fPielvB1JsNsJd3MyuTWdwGC0Tvxx/kYr5los/3hdtZHHkdUs7AUR2JVy1uWTuSEyeKd86gCWmA3+xRjMq/USJ4tk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=acjHmEFj; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="acjHmEFj" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2b45cb89f7eso43087035ad.0 for ; Wed, 13 May 2026 06:08:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1778677730; x=1779282530; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hECduHIccySBzXm1iwDhAhigl+PnFtS+sgbTNemvQzE=; b=acjHmEFjm/ZGs3SK8JU+d/4i+MClmltt362KnSu6fc98CESpdX/0fDm7RpmBzKpohD H4NqH9nF002gcerWSbz4homQ4tqDQP3JxXE8Vw/2FFe5IkSjC5DTI2qeFhtNBqtoVmmb B9gNLARXZFAMCTPatpk8dKDV6OmeYU7/Kjn/xt9BDR1lfA3D9q5mhyoJpysJx7397ZpN Fw4ZPfquQ9fjyNdVn7YTkhFPpDId1E5jy+p9nhNGIa8qJJ16btMca5vYqrfneE8dAtv8 Vc7NWBTx7En+rWSEsAOeRXznybliLTOTPq+QZJRhKcaEHYO7P2tSHJ0CEuqgwOOdHEdG 5A2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778677730; x=1779282530; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=hECduHIccySBzXm1iwDhAhigl+PnFtS+sgbTNemvQzE=; b=Rd+tEIcBEupXrTOKDd3yw3/Jwn3aaa6mevwmGlDgoBbwoJOlciHBnHz5AKA3ad23MZ 1dtQqAErnupnPOTbati8oPy4/U7DjUq5Ex1gVdzHjbOVfz5hB8IYlLt+ZmChtOc70T5t gprpk/Ux/cPeddEkBTAkX9H+tNm69kWDGt2pTwUPnB+v/kiNCMCdOM2wGzB1DCqg8vGu YUdA2gKqvcvxAFwGkQqlJFIVfzadZBTaLaEyz3g/NXyuU64qCJucHexsN1H9m/wIxBoh TVD8AtI91Q19cQU1Sir1mV96P5CGuGQXhRkAwZL6FVTg2QoipToS4PANtfpR9lAay/Yr DcYA== X-Forwarded-Encrypted: i=1; AFNElJ+x5IJ2wPA9GvorB2TAN/4mN+y/n1ev3UPLk/I66xvvgK/6SexMKVMOOu/zu+M2WbkI1qpZCB3XAHxNo60=@vger.kernel.org X-Gm-Message-State: AOJu0YxLuwzPykzI5w8Na74vmyrligucCV6gdq4HpZ+iA7Su6WDnsSYQ JAkd0GmF2GjQfGsEo3MaeTFCVEgMU73NCRHWM+JIdCiV4rHrfkAYYdmnEjPd3syHVN4= X-Gm-Gg: Acq92OFfFxcon+tRMgyQvylZbPRFXvzlFsEAqZcNIYUawKmdvX702zcpyDYL6N4XAsy Zy7BJuD+aeODqNtReDttg0L5N5pZgRSMKphzuRqOt5UhRDvKIaVt1ev+NiQdDtHCAy4Lu/85hLn un1xV9+Q1lGhnrFlzZ7/JJroKMMJcRf2bgCEUwH50KHBs54nYqwcb+2RyFk/uEf+DTia0R3clJ5 g018fuL3I5tAnU8sMm2o9umjQXU67DUU5ROlcScX27/H4Yv98LepRYmm8ce4TGdlohb5t18V1NQ Pa4w0jm5IOUQOGfWnadBiSUVZZ0y7YNb127tLtLJnpeFflyhr3KfgSVJojiHWymxtTgRfqehW4+ t3/Hf8FNsNV8pNz3iuFbk37bR2athXQiSQCmHPLTG7WSq3h0u4iGNaXPpqGs7CU3CHhFKNt+0KE 7w04FW9KVri8/u+Ym2XWae3546ShBUC06qGJp4QDRtUfcTS0FxYaYhOGJ39TecrAoFdUJK8Q== X-Received: by 2002:a17:903:3803:b0:2b9:e82f:bfef with SMTP id d9443c01a7336-2bd275b1d3bmr34581465ad.21.1778677730063; Wed, 13 May 2026 06:08:50 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([61.213.176.6]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2baf1e90854sm166641925ad.66.2026.05.13.06.08.45 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 13 May 2026 06:08:49 -0700 (PDT) From: Muchun Song To: Andrew Morton , David Hildenbrand , Muchun Song , Oscar Salvador , Michael Ellerman , Madhavan Srinivasan Cc: Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Nicholas Piggin , Christophe Leroy , Ackerley Tng , Frank van der Linden , aneesh.kumar@linux.ibm.com, joao.m.martins@oracle.com, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH v2 14/69] mm/hugetlb: Free cross-zone bootmem gigantic pages after allocation Date: Wed, 13 May 2026 21:04:42 +0800 Message-ID: <20260513130542.35604-15-songmuchun@bytedance.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260513130542.35604-1-songmuchun@bytedance.com> References: <20260513130542.35604-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Now that hugetlb reservation runs after zone initialization, bootmem gigantic page allocation can detect pages that span multiple zones. Keep those cross-zone pages separate during allocation and free them after allocation completes, so later hugetlb initialization only sees zone-valid gigantic pages. This chooses to free cross-zone gigantic pages directly instead of retrying allocation. In practice, such cross-zone cases are expected to be very rare, so adding retry logic does not seem justified at this point. Keeping the handling simple also preserves the previous behavior. If similar real-world reports show up later, retry support can be reconsidered then. Signed-off-by: Muchun Song --- mm/hugetlb.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 11 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index e9ba0be2eb17..d5d324f69d7a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3077,12 +3077,15 @@ void *__init __alloc_bootmem_huge_page(struct hstate *h, int nid) static bool __init alloc_bootmem_huge_page(struct hstate *h, int nid) { + unsigned long pfn; + unsigned int nid_request = nid; struct huge_bootmem_page *m = arch_alloc_bootmem_huge_page(h, nid); if (!m) return false; - nid = early_pfn_to_nid(PHYS_PFN(__pa(m))); + pfn = PHYS_PFN(__pa(m)); + nid = early_pfn_to_nid(pfn); /* * Use the beginning of the huge page to store the huge_bootmem_page * struct (until gather_bootmem puts them into the mem_map). @@ -3090,22 +3093,38 @@ static bool __init alloc_bootmem_huge_page(struct hstate *h, int nid) * Put them into a private list first because mem_map is not up yet. */ INIT_LIST_HEAD(&m->list); - list_add(&m->list, &huge_boot_pages[nid]); m->hstate = h; if (!hugetlb_early_cma(h)) { m->cma = NULL; m->flags = 0; } - /* - * Only initialize the head struct page in memmap_init_reserved_pages, - * rest of the struct pages will be initialized by the HugeTLB - * subsystem itself. - * The head struct page is used to get folio information by the HugeTLB - * subsystem like zone id and node id. - */ - memblock_reserved_mark_noinit(__pa((void *)m + PAGE_SIZE), - huge_page_size(h) - PAGE_SIZE); + /* CMA pages: zone-crossing is validated in hugetlb_cma_reserve(). */ + if (!hugetlb_early_cma(h) && + pfn_range_intersects_zones(nid, pfn, pages_per_huge_page(h))) { + /* + * If the allocated page is on a different node than requested + * (e.g. on PowerPC LPARs), put it on the requested node's list. + * Otherwise, the cross-zone page will be stranded and never + * freed, as the cleanup code only operates on the requested node. + */ + if (WARN_ON_ONCE(nid_request != NUMA_NO_NODE && nid != nid_request)) + list_add(&m->list, &huge_boot_pages[nid_request]); + else + list_add(&m->list, &huge_boot_pages[nid]); + } else { + list_add_tail(&m->list, &huge_boot_pages[nid]); + m->flags |= HUGE_BOOTMEM_ZONES_VALID; + /* + * Only initialize the head struct page in memmap_init_reserved_pages, + * rest of the struct pages will be initialized by the HugeTLB + * subsystem itself. + * The head struct page is used to get folio information by the HugeTLB + * subsystem like zone id and node id. + */ + memblock_reserved_mark_noinit(__pa((void *)m + PAGE_SIZE), + huge_page_size(h) - PAGE_SIZE); + } return true; } @@ -3384,6 +3403,34 @@ void __init hugetlb_struct_page_init(void) padata_do_multithreaded(&job); } +static unsigned long __init hugetlb_free_cross_zone_pages(struct hstate *h, int nid) +{ + unsigned long freed = 0; + struct huge_bootmem_page *m, *tmp; + + if (!hstate_is_gigantic(h)) + return freed; + + list_for_each_entry_safe(m, tmp, &huge_boot_pages[nid], list) { + if (m->flags & HUGE_BOOTMEM_ZONES_VALID) + break; + + list_del(&m->list); + memblock_free(m, huge_page_size(h)); + freed++; + } + + if (freed) { + char buf[32]; + + string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, sizeof(buf)); + pr_warn("HugeTLB: freed %lu cross-zone hugepages of size %s on node %d.\n", + freed, buf, nid); + } + + return freed; +} + static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid) { unsigned long i; @@ -3414,6 +3461,8 @@ static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid) cond_resched(); } + i -= hugetlb_free_cross_zone_pages(h, nid); + if (!list_empty(&folio_list)) prep_and_add_allocated_folios(h, &folio_list); @@ -3487,6 +3536,7 @@ static void __init hugetlb_pages_alloc_boot_node(unsigned long start, unsigned l static unsigned long __init hugetlb_gigantic_pages_alloc_boot(struct hstate *h) { + int nid; unsigned long i; for (i = 0; i < h->max_huge_pages; ++i) { @@ -3495,6 +3545,9 @@ static unsigned long __init hugetlb_gigantic_pages_alloc_boot(struct hstate *h) cond_resched(); } + for_each_node(nid) + i -= hugetlb_free_cross_zone_pages(h, nid); + return i; } -- 2.54.0