From nobody Tue Feb 10 00:22:26 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 243711E1023 for ; Mon, 27 Jan 2025 23:22:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738020168; cv=none; b=m+9kCyCPz76+twQ5iMufsGNECLW29acjVYlzw45Yc/ufEXe/nc8lTeZPJoGzhr7Uy13aXJuz9hYXoPQ3/P+QodqEwh0S6UNkAuSorNO5NQakAFIl6i0pPgC3ulCm6/EQSaLqdaXGkn4amBdx8nD9FFEwx7zq5dVgggSjuK7hXlY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738020168; c=relaxed/simple; bh=i6U8IjDjoqV0e0MA2LEc5tDV94/GyVFkbrdhn+y5TxU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RG2iSl2zWPb9i+PcebsuX5XW3+6ewSI3bKg8WvAmXoryzw9XKOXqjb6IIlQhFN5LHZur4KHdYr04DN2Ezqz+TGYJs9IR9TggF0YnkPxI+gGUGFZRPOevOIuGe47Kj1MBcxU8+NUfSOilmTcKWlx2nWFIar8qUNW+K4Ij2An+Dsk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wyhw3dRg; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wyhw3dRg" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef114d8346so9604515a91.0 for ; Mon, 27 Jan 2025 15:22:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738020166; x=1738624966; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=fPEa5A3QHhx3Kbv5Xhjw+prW9k2+8h7ykdvwjxvCKJY=; b=wyhw3dRgg86rAnz5j+jpg9rm0JHq+DTX5s5Io7UyYz7gtDOGD27TuucU1i2hvcDORx cTLsU45Iq7FKxQ1ZhyYCdM2LzNniMP5tgQfDz0R0eelUTjTB8YuH48/+5bjSsdlL11Rq QxBmoFSKZHyw/rlD6ZUFNo7B6sSglR97qNQVrE4pxj3tDy9LjUaxvkrxp+uee9ZsHsbB RpLG/JhvbGIBlIyPnHBwqS0wGgG0YHxSwxjKVuHzuixPL4Pl09ROFKDrUSMmmZz8+sYw mKOESGqyk9qy1ePvv6zt3wE9UgYvvN7/oi2JERL9zvItw9c5+HDvZQks4KrrwkYmiu2T fFoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738020166; x=1738624966; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fPEa5A3QHhx3Kbv5Xhjw+prW9k2+8h7ykdvwjxvCKJY=; b=E+6g6bjavdZX15LomPIEC1/ZZfWRL1FEGs6EekahnOD96npaswPJwxd8zpDlRSYq7F 733NTBENRdM9S+NEfKh7nDe10KTERX/OEgoCPxqjYdxvraHolKZv285I2Ob2jr2WPzTZ vpbfA6MobnF/Axc4QeOUGr1/7xfvcA1SEy+H7yAGd+EdbVQlV8Yyk7NFqN+gSBILzKoH rhKzr1+anxhSONu3de81JhpiCYm1hM99Cz0tsZQRaex73w2VhvWXrrKg9nghUAXqvI2i 6TXU+hmwEcBYOEQa7gbMrOlpQeqJYQrxYmOtuxNfeVkWhRPmjZb5RcHpTgxV7Dl99raq C4tQ== X-Forwarded-Encrypted: i=1; AJvYcCVWHqNj/EWmbTzdVexYcIQWbbEedCVBowyS0JI1dOV+sDyakj1EToM9p0d2WvGcOM5HoynIIix80qYGelk=@vger.kernel.org X-Gm-Message-State: AOJu0YwASRxPND3X6Z39luZLjbD4Fk7FE4CnaVBl9O+o9PwGBOl+DjHe iYum8HCRbcP/AR8IZ8r3COrjmZIbQ86+gZsAQkkIKiuo5hyE4wumlmoO5b6z2LHHk54+wQ== X-Google-Smtp-Source: AGHT+IGn7jc5Om129Hpx9vXBJ3rH8UAewwJypxwqAHi6sGDmxGCjPTFhR44F/Z++C8sNe0c1dXnl0ue0 X-Received: from pfxa29.prod.google.com ([2002:a05:6a00:1d1d:b0:728:e245:6e93]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:84f:b0:725:df1a:288 with SMTP id d2e1a72fcca58-72dafaf8ab3mr70132923b3a.24.1738020166471; Mon, 27 Jan 2025 15:22:46 -0800 (PST) Date: Mon, 27 Jan 2025 23:21:54 +0000 In-Reply-To: <20250127232207.3888640-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250127232207.3888640-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250127232207.3888640-15-fvdl@google.com> Subject: [PATCH 14/27] mm/hugetlb: check bootmem pages for zone intersections From: Frank van der Linden To: akpm@linux-foundation.org, muchun.song@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: yuzhao@google.com, usama.arif@bytedance.com, joao.m.martins@oracle.com, roman.gushchin@linux.dev, Frank van der Linden Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Bootmem hugetlb pages are allocated using memblock, which isn't (and mostly can't be) aware of zones. So, they may end up crossing zone boundaries. This would create confusion, a hugetlb page that is part of multiple zones is bad. Worse, HVO might then end up stealthily re-assigning pages to a different zone when a hugetlb page is freed, since the tail page structures beyond the first vmemmap page would inherit the zone of the first page structures. While the chance of this happening is low, you can definitely create a configuration where this happens (especially using ZONE_MOVABLE). To avoid this issue, check if bootmem hugetlb pages intersect with multiple zones during the gather phase, and discard them, handing them to the page allocator, if they do. Record the number of invalid bootmem pages per node and subtract them from the number of available pages at the end, making it easier to do these checks in multiple places later on. Signed-off-by: Frank van der Linden --- mm/hugetlb.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++-- mm/internal.h | 2 ++ mm/mm_init.c | 25 +++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9969717b7dd8..a4d29a4f3efe 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -63,6 +63,7 @@ static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODE= S] __initdata; static unsigned long hugetlb_cma_size __initdata; =20 __initdata struct list_head huge_boot_pages[MAX_NUMNODES]; +__initdata unsigned long hstate_boot_nrinvalid[HUGE_MAX_HSTATE]; =20 /* * Due to ordering constraints across the init code for various @@ -3309,6 +3310,44 @@ static void __init prep_and_add_bootmem_folios(struc= t hstate *h, } } =20 +static bool __init hugetlb_bootmem_page_zones_valid(int nid, + struct huge_bootmem_page *m) +{ + unsigned long start_pfn; + bool valid; + + start_pfn =3D virt_to_phys(m) >> PAGE_SHIFT; + + valid =3D !pfn_range_intersects_zones(nid, start_pfn, + pages_per_huge_page(m->hstate)); + if (!valid) + hstate_boot_nrinvalid[hstate_index(m->hstate)]++; + + return valid; +} + +/* + * Free a bootmem page that was found to be invalid (intersecting with + * multiple zones). + * + * Since it intersects with multiple zones, we can't just do a free + * operation on all pages at once, but instead have to walk all + * pages, freeing them one by one. + */ +static void __init hugetlb_bootmem_free_invalid_page(int nid, struct page = *page, + struct hstate *h) +{ + unsigned long npages =3D pages_per_huge_page(h); + unsigned long pfn; + + while (npages--) { + pfn =3D page_to_pfn(page); + __init_reserved_page_zone(pfn, nid); + free_reserved_page(page); + page++; + } +} + /* * Put bootmem huge pages into the standard lists after mem_map is up. * Note: This only applies to gigantic (order > MAX_PAGE_ORDER) pages. @@ -3316,14 +3355,25 @@ static void __init prep_and_add_bootmem_folios(stru= ct hstate *h, static void __init gather_bootmem_prealloc_node(unsigned long nid) { LIST_HEAD(folio_list); - struct huge_bootmem_page *m; + struct huge_bootmem_page *m, *tm; struct hstate *h =3D NULL, *prev_h =3D NULL; =20 - list_for_each_entry(m, &huge_boot_pages[nid], list) { + list_for_each_entry_safe(m, tm, &huge_boot_pages[nid], list) { struct page *page =3D virt_to_page(m); struct folio *folio =3D (void *)page; =20 h =3D m->hstate; + if (!hugetlb_bootmem_page_zones_valid(nid, m)) { + /* + * Can't use this page. Initialize the + * page structures if that hasn't already + * been done, and give them to the page + * allocator. + */ + hugetlb_bootmem_free_invalid_page(nid, page, h); + continue; + } + /* * It is possible to have multiple huge page sizes (hstates) * in this list. If so, process each size separately. @@ -3595,13 +3645,20 @@ static void __init hugetlb_init_hstates(void) static void __init report_hugepages(void) { struct hstate *h; + unsigned long nrinvalid; =20 for_each_hstate(h) { char buf[32]; =20 + nrinvalid =3D hstate_boot_nrinvalid[hstate_index(h)]; + h->max_huge_pages -=3D nrinvalid; + string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n", buf, h->free_huge_pages); + if (nrinvalid) + pr_info("HugeTLB: %s page size: %lu invalid page%s discarded\n", + buf, nrinvalid, nrinvalid > 1 ? "s" : ""); pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n", hugetlb_vmemmap_optimizable_size(h) / SZ_1K, buf); } diff --git a/mm/internal.h b/mm/internal.h index 57662141930e..63fda9bb9426 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -658,6 +658,8 @@ static inline struct page *pageblock_pfn_to_page(unsign= ed long start_pfn, } =20 void set_zone_contiguous(struct zone *zone); +bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, + unsigned long nr_pages); =20 static inline void clear_zone_contiguous(struct zone *zone) { diff --git a/mm/mm_init.c b/mm/mm_init.c index 925ed6564572..f7d5b4fe1ae9 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -2287,6 +2287,31 @@ void set_zone_contiguous(struct zone *zone) zone->contiguous =3D true; } =20 +/* + * Check if a PFN range intersects multiple zones on one or more + * NUMA nodes. Specify the @nid argument if it is known that this + * PFN range is on one node, NUMA_NO_NODE otherwise. + */ +bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, + unsigned long nr_pages) +{ + struct zone *zone, *izone =3D NULL; + + for_each_zone(zone) { + if (nid !=3D NUMA_NO_NODE && zone_to_nid(zone) !=3D nid) + continue; + + if (zone_intersects(zone, start_pfn, nr_pages)) { + if (izone !=3D NULL) + return true; + izone =3D zone; + } + + } + + return false; +} + static void __init mem_init_print_info(void); void __init page_alloc_init_late(void) { --=20 2.48.1.262.g85cc9f2d1e-goog