From patchwork Wed Jan 29 22:41:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank van der Linden X-Patchwork-Id: 13954211 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80BBCC02190 for ; Wed, 29 Jan 2025 22:43:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFB612800A0; Wed, 29 Jan 2025 17:42:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B0D3528008C; Wed, 29 Jan 2025 17:42:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D5292800A0; Wed, 29 Jan 2025 17:42:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 64EC728008C for ; Wed, 29 Jan 2025 17:42:42 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1D6101A0313 for ; Wed, 29 Jan 2025 22:42:42 +0000 (UTC) X-FDA: 83061965364.18.B9DE73A Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf23.hostedemail.com (Postfix) with ESMTP id 4B166140004 for ; Wed, 29 Jan 2025 22:42:40 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=iXwf8rZi; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3366aZwQKCOoRhPXSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3366aZwQKCOoRhPXSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--fvdl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738190560; a=rsa-sha256; cv=none; b=myCzEwbwhV+bwQmwywoVAkNlDAJT+l2qbEAyLC3t/wqvf+mFUZSNeqVaQT4aP3rQ35casM vdykNE7vRMhayxF7di7IhGtPmyacMKWN7h6jAREpzbcr4ln70W4nC8PNTdQJYw1Q3UdzrZ vvwYzWKMbT+RN4rf064lW7/AZ0egajE= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=iXwf8rZi; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3366aZwQKCOoRhPXSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3366aZwQKCOoRhPXSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--fvdl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738190560; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Bphjz2B5H2n+p6toKCuCjUfnxwYaF6wPJ0biY9P/V2E=; b=Cvcj2RoW9o7DhLzpcNVlNbaC9rCNryZfQYBHtpoIFcAxEq6ZF3iPYM8qOGWm3uSOHhtAFC Cq8GPJBvbx9T19nFrbtr770/tBdhi/o3i+Kf4rczX02KLPZDo8PQAOo1meNnss/IBI4tUx 0YwxWGvE6k21hLbwvCQ5AJ2saJhrNcQ= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-218cf85639eso4517595ad.3 for ; Wed, 29 Jan 2025 14:42:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190559; x=1738795359; darn=kvack.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=Bphjz2B5H2n+p6toKCuCjUfnxwYaF6wPJ0biY9P/V2E=; b=iXwf8rZivoUtlh0TyyO+8+kZhX07AtEyqrBWHmbaCfBuhOMqhFADy+A1/0P4n8Urxl AeAZabMjMIraB+YxeDb+E53Z2oQXprgWbpiojjBoJuhXUTy2opNv4s3eIGu7ta+SQKCu Xby3gUVirQdkXciKDjRPckkHZ6/iQyf1uq2X3fDhgzvCce0/+M2fS28n37bp66SeqiFx MSr7UvkNu88bX6DrvThvV1ffds7mbnLvqBpWUixJeB2C7gl6Ji69I9XuJeJJ5z1OmSEY 31yQ2W9QXShOFpe9LpFzZasNjpJxOGj3h8evS2Qt6/MAflhHMt20Xb2Z+pJHIxtSIDV1 +UNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190559; x=1738795359; 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=Bphjz2B5H2n+p6toKCuCjUfnxwYaF6wPJ0biY9P/V2E=; b=h03dzPcfHrzK35BdlLSHYQaAhs0RwIxF0TIoTINBgA8mY3ihu+RSahM6hEZVLsM+zE v4vuZbBpXGtLB2LJHN26VWcOslkhgPonQJ7r6r9Hhfrb9pBsJKFIFXc/9UHWCHxxxx9H D8xDfD4MeTaxpT0vMx3X+qyFqbn8REUbk7MfABTLeNEmPGeAAW3tvtMQUr73l7BstgZn 59VQ04tK8epIbErZFlrgePDvoZVVS/scyXCS285yGxIYbJqr60o9zhDuFtOSPTOuZ67x slWXh1AUVdG8OAGjxfys4K8/56dFM/a7WfI4TJYwPiQKetEm0VECFDgpzP43fz3UqC5V QBVQ== X-Forwarded-Encrypted: i=1; AJvYcCXYERKVfQr6dmdjmnT27cc4SPJFsGVjfTOrDE/B8cHraI0U3U8yTmY2AMxrLd7RqsBCG6PT0apfiA==@kvack.org X-Gm-Message-State: AOJu0YwqI9qMsj3I1164TDRedoWOumGkFkC51Jy4A3pGpgRrHxlgRH/1 VdqwlBe3kEV5GR3DhECryPDAmeDErEBN4R4X1BDVVfsiuaaqV0nR7yQi7m/8SmyOr7flyg== X-Google-Smtp-Source: AGHT+IEupuZBL681CzwqacE6l9j1whrVwEezyuvYbWICKt+dR8gVuQALet2uv2LXpK1TnJ3eAsBEWAai X-Received: from plxd18.prod.google.com ([2002:a17:902:ef12:b0:21d:dae1:77e8]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ea02:b0:21c:fb6:7c50 with SMTP id d9443c01a7336-21dd7d8aa02mr76684975ad.31.1738190559165; Wed, 29 Jan 2025 14:42:39 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:43 +0000 In-Reply-To: <20250129224157.2046079-1-fvdl@google.com> Mime-Version: 1.0 References: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-15-fvdl@google.com> Subject: [PATCH v2 14/28] 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, usamaarif642@gmail.com, joao.m.martins@oracle.com, roman.gushchin@linux.dev, Frank van der Linden X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 4B166140004 X-Stat-Signature: tqi47buzi38jeb8b6yx6fprkjyfeyuk1 X-Rspam-User: X-HE-Tag: 1738190560-363253 X-HE-Meta: U2FsdGVkX19ylObBzNkhvC1XUwklgsHBK469bFZ7HMlHUrecgtp02ZU6RH7CLAgCv9vXy9TPfRA/w1qPxnNl8G45jcGd4CRtqPE5VYr3c8G/GFH612vcU1RfGQjfIKQW5vhyEtOozOZNXzT7BMOCcOFMEnlAF5YK2dKOUYb4nOgVVCgLGSXUwG+qxJ8ryX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Ippkovnk2oinWDuJfnbpMxQKdejGxmVHA+MlU6pfZgcZYQOW7804qgPJaXYJwGhVVYd5uzH1y8OEtXF/ORpOFFMD0wQ21zAAMdMASyvs+oRYZeQ74iCxrSdDRiqf8r6wvM5Ru4IwVKzWE2HKnhdcUqpSSZSASCXH9lCuGmxIqDd1aW3lj02uVdgRe/2u2ZBadYtU0xWVpZekgF5JITcN15wDJ0LDWAWLncxJPXEpI1TOjznJPiSS4A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 e5ca5cf2c6fd..a0a87d1a8569 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -63,6 +63,7 @@ static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata; static unsigned long hugetlb_cma_size __initdata; __initdata struct list_head huge_boot_pages[MAX_NUMNODES]; +__initdata unsigned long hstate_boot_nrinvalid[HUGE_MAX_HSTATE]; /* * Due to ordering constraints across the init code for various @@ -3309,6 +3310,44 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h, } } +static bool __init hugetlb_bootmem_page_zones_valid(int nid, + struct huge_bootmem_page *m) +{ + unsigned long start_pfn; + bool valid; + + start_pfn = virt_to_phys(m) >> PAGE_SHIFT; + + valid = !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 = pages_per_huge_page(h); + unsigned long pfn; + + while (npages--) { + pfn = 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(struct 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 = NULL, *prev_h = NULL; - 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 = virt_to_page(m); struct folio *folio = (void *)page; h = 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; for_each_hstate(h) { char buf[32]; + nrinvalid = hstate_boot_nrinvalid[hstate_index(h)]; + h->max_huge_pages -= 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(unsigned long start_pfn, } void set_zone_contiguous(struct zone *zone); +bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, + unsigned long nr_pages); 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 = true; } +/* + * 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 = NULL; + + for_each_zone(zone) { + if (nid != NUMA_NO_NODE && zone_to_nid(zone) != nid) + continue; + + if (zone_intersects(zone, start_pfn, nr_pages)) { + if (izone != NULL) + return true; + izone = zone; + } + + } + + return false; +} + static void __init mem_init_print_info(void); void __init page_alloc_init_late(void) {