From patchwork Thu Feb 6 18:50:54 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: 13963561 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 13301C02194 for ; Thu, 6 Feb 2025 18:51:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C026728000D; Thu, 6 Feb 2025 13:51:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B89F7280002; Thu, 6 Feb 2025 13:51:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98C8228000D; Thu, 6 Feb 2025 13:51:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 66F4F280002 for ; Thu, 6 Feb 2025 13:51:42 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2389EB255A for ; Thu, 6 Feb 2025 18:51:42 +0000 (UTC) X-FDA: 83090413644.04.4092E5E Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf12.hostedemail.com (Postfix) with ESMTP id 4CB1340007 for ; Thu, 6 Feb 2025 18:51:40 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=H0b8KFIE; spf=pass (imf12.hostedemail.com: domain of 3uwSlZwQKCJwBR9HCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3uwSlZwQKCJwBR9HCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738867900; 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=1XzNWdK53DK5kTMFUNWfsYU5y9JEDM1DtaDFYklZD4s=; b=FRBVWzIjRMq22VYdZKPqMRDiVz48FJeg/3bsrYMB5i7jnn5WZDpfbCLDlalbkZXuCMEH5h /72hmoWF8ygxetTp8HItjY+u5MF8o9d49thWaf/nuts+DOH6UbVPybmqNeFKlcIshpKdU3 wVywYzGt90RWnIpUEnS3lf4QfVHthmg= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=H0b8KFIE; spf=pass (imf12.hostedemail.com: domain of 3uwSlZwQKCJwBR9HCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3uwSlZwQKCJwBR9HCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738867900; a=rsa-sha256; cv=none; b=MkT1MBKpxNgghNuGBvw+EWWS4dyJ/+GQunfU/pEbU3imsIwXUljpg53i8Vp078niLEt2Tl V9uKsC1UBCLvjEdf2D8uXONj5zNUHRqPmJh5Mjn5lx8Yuk5EFlHpx7MtIBU1j7TGNyL1V9 HIJChmF+peirkT/5GCL/eqA3bZHufIU= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21f1e5c641aso43151035ad.2 for ; Thu, 06 Feb 2025 10:51:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738867899; x=1739472699; 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=1XzNWdK53DK5kTMFUNWfsYU5y9JEDM1DtaDFYklZD4s=; b=H0b8KFIEuub4K07VUUyjU3VTQ1xhjWazuZPStyWh0fv9Qo++SwRUqDOZnZYvgj/7NC PZGu4DTxRxKAORdhXNDnUK8h0ShOLMChqtZ3HDol+PpimZMtsDU6YCn3bgF6rbQf9LCH rso5nTsux/hNSzvGfgu2W+eSR5tsEHweERsUak0xLs6m4/dCP7H3umCqAtfnEZZsGxMU 5vVVk28DeexCtE1IMxU1DiQkndWom70qfm96b6sMq0BBkmMDRfEtCBRHGGFMLo7qE7se iHi3YmO6P9Xekl4su/r6cy2qcHXfPZPmZj6bIRNPPwRGUvIJwpQ2jylyq8JftIwgbSnc uwOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738867899; x=1739472699; 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=1XzNWdK53DK5kTMFUNWfsYU5y9JEDM1DtaDFYklZD4s=; b=X835oC4RS8y3O9q+Abau3Raybclf74FXMNcDfIu04pnAuSu4l5maM0XRRAVW0Jimbs X6gngJN3InpljGNTalsNW5RycXbJL9YOtllK/5ym9txOBIMM+/OGOEKObr3Z78M9vicF ZhGAcELq80G2H3grQRR6/hJEqVJUhXFREzhMmsSXAyoeYthj9cXUN1KO1LA3+okix8/1 HXo/295rG/a/zuOm3QqNEPTU8yI40fv/JW3Kz99MsrYnVBRz6APSTQzyE++G4OnT2q/5 1fc/yHXRCxU9gfz6sPscFYgxEc0YxIdd4nTHLQ2U9bX0kQwD2E5foH2oMGVv3Ukoy3Df BHpQ== X-Forwarded-Encrypted: i=1; AJvYcCVdfnzdlCYPKS9xHJNemQovANx/MStHSusmoRehUgr8cg+aXVpiIJTQXTcuB3jvFKe4pYN/bliKkQ==@kvack.org X-Gm-Message-State: AOJu0YywUTRkRn97utCMYrEqHvP8l5eSrCz0lBo7WSoOsIC4Y2JPLsaN 9RnpiPLP+mQbere0HZFoaVSKzMZkoY/7d/DmdwukQ0dCf0ULYafNWURaBazM76uxrYxYjg== X-Google-Smtp-Source: AGHT+IGIkx9zow3SNmjwLwkWX732+NMszmOw5UwG52/2qtVEFt/yi4VJkAhAKbmBsmMF+ToCMr1/WRbP X-Received: from pgbdl12.prod.google.com ([2002:a05:6a02:d0c:b0:ad5:1a11:9dae]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3a87:b0:1ed:a565:f980 with SMTP id adf61e73a8af0-1ee03bb030cmr937467637.27.1738867899243; Thu, 06 Feb 2025 10:51:39 -0800 (PST) Date: Thu, 6 Feb 2025 18:50:54 +0000 In-Reply-To: <20250206185109.1210657-1-fvdl@google.com> Mime-Version: 1.0 References: <20250206185109.1210657-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250206185109.1210657-15-fvdl@google.com> Subject: [PATCH v3 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-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 4CB1340007 X-Stat-Signature: 338oqr545egwhsqic5z3eczbbtk6bsxq X-HE-Tag: 1738867900-668602 X-HE-Meta: U2FsdGVkX1/e7JEKjR4TzpqU2THaU4Wq6kYPAb6xvfT2B6BRD1nLAI0iE/Uk/ApAQgSICmQ+OUoUGR8YiCozCt0WbIKhurtLklN9ZO9n4vpMOIjgVVbtnCglLL+pb7ukXwn3xQdYd+NvsMB/wwHUV0GumXmXm7jDzLWT7s2yoqcSJY/WNH7bKD2+7hDRdQfPhwAFtMGy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mCb2+H9UEkdqu/CvG0hLlCGxobPO0ueDG7obdJPLjGaV4QEvEfMYj0lWlMhDyvb0Pw4Ha+OtDvYRDSU019SObdx8iPh38GOETDUunoxr9FWDJOI7DfhNW9DoKEgYnjiqOLxWZpPXagZME+6K5zNAn2h/eQGkXjkx/wqyArlMjG9qf806vgfcfMqSkQDIG/1arIGR+Yl7HfMCVmibnLXDKxl8Iv7MqTRfSmap1XUYYcHgROfeA== 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 de8adfb487f4..789341158ef6 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -62,6 +62,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]; +static unsigned long hstate_boot_nrinvalid[HUGE_MAX_HSTATE] __initdata; /* * Due to ordering constraints across the init code for various @@ -3308,6 +3309,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. @@ -3315,14 +3354,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. @@ -3594,13 +3644,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) {