From patchwork Tue Feb 18 18:16:41 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: 13980412 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 8F5E9C021AD for ; Tue, 18 Feb 2025 18:17:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D78796B00CE; Tue, 18 Feb 2025 13:17:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D26D6280182; Tue, 18 Feb 2025 13:17:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B7E69280181; Tue, 18 Feb 2025 13:17:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 908CA6B00CE for ; Tue, 18 Feb 2025 13:17:30 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3B51F120420 for ; Tue, 18 Feb 2025 18:17:30 +0000 (UTC) X-FDA: 83133873060.21.0AF46D2 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf19.hostedemail.com (Postfix) with ESMTP id 5EC2C1A001A for ; Tue, 18 Feb 2025 18:17:28 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jHoRLUtX; spf=pass (imf19.hostedemail.com: domain of 3t860ZwQKCGwPfNVQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--fvdl.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3t860ZwQKCGwPfNVQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739902648; a=rsa-sha256; cv=none; b=6f5uQMPdbJgJIEEQ4IsL8qFBYUdOepg9uaeAMFnydu49ESAx2HIbQ0kV3FvTGw/CHwjTLe 0UVvbkz6WF+/9rqGp6CAjP6oH3P6oVL39khmQV59uqFVkdQyqknLkYP0JcnEmF1GNpue/n gtcWA7+Q+eGK4K5VTUWSSvNOzH2iPTE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jHoRLUtX; spf=pass (imf19.hostedemail.com: domain of 3t860ZwQKCGwPfNVQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--fvdl.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3t860ZwQKCGwPfNVQYYQVO.MYWVSXeh-WWUfKMU.YbQ@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=1739902648; 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=165gS3goRLf8zZj6pEhjQb5PxE79JM+3p+KuzMFcveU=; b=xsd3zWqSsscGDfJJXBBEoFktp771f5tqsC2XalKfAMi3DExEck/VmJ2IOYpn9zPII35Yqx Kke5fH2rYxyxv+zhFfEZEmlUGiivd/61MdjXN/HsTRy6iHpbobLHU19HCQKanAwPHyUiHm +Qlql9qiv3kjBuB51QlpFfLb03C/ZTs= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fc1eadf5a8so11121573a91.3 for ; Tue, 18 Feb 2025 10:17:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739902647; x=1740507447; 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=165gS3goRLf8zZj6pEhjQb5PxE79JM+3p+KuzMFcveU=; b=jHoRLUtXzIshEmBTlWMT0Sf/BND+3ECm63dedZNBh9+F+HgSJrxW4e0ntUErvqtWxg QKxuGd/6VrgKmh69/Hms0N/VWKHcEC6aXGxm+SgXXEpSbdZZk+ofFCHVfCgZs+SQ4bIe ppwbNme5CRzmPuzkKboUV+9VQgK13oXuRugsQ8yFllVCqH/XB5PdEH+SYHXq7SfTB5Lf WMfSoHtwsqnZaen2eHVTZVodRRubApqVqwbJIh984vPBbbHCQTpsxbCOY/uuVa/zTDV1 loTrEgjD7VdiKRRq5pAv10vdbzIu5Zb0GvEsF2ta/63AuDw5tbvhJuM4ScSO/rb61Q4C 9/tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739902647; x=1740507447; 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=165gS3goRLf8zZj6pEhjQb5PxE79JM+3p+KuzMFcveU=; b=EnbiP88xBRM4ahfww4KEcfM3ehksXAEangJ22wQXfCgJZKWxFjoxbwLXdLToZ4nd9D o8zhRvcjSZD6N6uBEj14QPvocON1IV3Oc4u4ot060Z71YkvB68bsAHKldX21/ECAecyc rTtsT27fB9WhdQ+DYREPCV01eNtt3XI/C4r8gCGHiGmICqLYbBlI8oz/ZiDSXHfg7nNp n1Vf2a/X7Df7B841n2+9w031QE1Q3eJVfRbGBqI1Q9EfXPb4hnt2sohDte/n4xXlAG+h Lsrs0QEh2K03kBq8R0nBCQ8sE82tUBKcVQNlE1TClum15aAEtZuevS7a/+/24ojP01n1 iQpQ== X-Forwarded-Encrypted: i=1; AJvYcCVhwLPM2GymbM6Hk+tEOVZKUbPHi8onFxTsNjQMiIcwz1SCNF9C+Hbe9XUhUpgjZdbZGchnRI8lIw==@kvack.org X-Gm-Message-State: AOJu0YxjJ79xZE3iyKJVhd7G92FuhkZif/NKARdoYxMHhuTEeMwzjBCF OfbcuObhq3wLnrKeuIcYoEob4BA1v5aqp/wh7Pc8RJNF3KfJX4e1WVCVSseKS3iJpyCBoQ== X-Google-Smtp-Source: AGHT+IEoqMlrNITX7gZxzMB0Y+rjQKU0M6SIkqPiK42wAClKKAVtpeQMhJt67RX0BgBBDoe6CJK14pHN X-Received: from pjf4.prod.google.com ([2002:a17:90b:3f04:b0:2fc:13d6:b4cb]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:134e:b0:2ee:acb4:fecd with SMTP id 98e67ed59e1d1-2fcb5a10379mr642511a91.9.1739902647343; Tue, 18 Feb 2025 10:17:27 -0800 (PST) Date: Tue, 18 Feb 2025 18:16:41 +0000 In-Reply-To: <20250218181656.207178-1-fvdl@google.com> Mime-Version: 1.0 References: <20250218181656.207178-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.601.g30ceb7b040-goog Message-ID: <20250218181656.207178-14-fvdl@google.com> Subject: [PATCH v4 13/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, usamaarif642@gmail.com, joao.m.martins@oracle.com, roman.gushchin@linux.dev, Frank van der Linden X-Rspamd-Queue-Id: 5EC2C1A001A X-Stat-Signature: zbt43pmmb63rznc6ymtzigxepg9ouagj X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1739902648-958242 X-HE-Meta: U2FsdGVkX19Bj6erLJNc0scPGxYf7LIwqztCxOB9yEYaiYxTsCEBLbvD1sqvpiEfvQ0BqASgwiq/QWCc7e4ZGG0CYybgAJLVLOgMteZ7hixlAEc6+MV1bWRFIyHKB/BqUvPOPBbBOUDOAzHhiEzU9cxb+Y5Vhy//t6MCterwG2cOJ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t4MjjqpGfX80HxG0o0E0WSbgnUxkZHVG3L0YEue3oFiCkc9pvhZ50e6gc1XBGye/5PLcXGsQhky0MfO6q/HuuABSAEEV2tl2BK8P1h03B1nx0ZmHCruat+eNMNfkp1zpXGQDEw/1A25QhZUDzHp+s2FBUZfKu6bvu7+ooam52x0HAizPQGdLAvZljIzKITniRCR1XIIj+ni58MpANtUwGzaOVKPZcKSvRCgUCIPNyalkJ/9UM/S4rHv3sAOd7bvKPw1Ul9Q== 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 19a7a795a388..f9704a0e62de 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 @@ -3304,6 +3305,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. @@ -3311,14 +3350,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. @@ -3590,13 +3640,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) {