From patchwork Wed Jan 29 22:41:47 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: 13954218 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 A4A78C0218D for ; Wed, 29 Jan 2025 22:43:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E58DA280269; Wed, 29 Jan 2025 17:42:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C490B28008C; Wed, 29 Jan 2025 17:42:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 763E3280270; Wed, 29 Jan 2025 17:42:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id AFFB4280263 for ; Wed, 29 Jan 2025 17:42:48 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6924B1604C0 for ; Wed, 29 Jan 2025 22:42:48 +0000 (UTC) X-FDA: 83061965616.29.A6FEE03 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf16.hostedemail.com (Postfix) with ESMTP id 97383180017 for ; Wed, 29 Jan 2025 22:42:46 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=GZ2kEE9+; spf=pass (imf16.hostedemail.com: domain of 35a6aZwQKCPAXnVdYggYdW.Ugedafmp-eecnSUc.gjY@flex--fvdl.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=35a6aZwQKCPAXnVdYggYdW.Ugedafmp-eecnSUc.gjY@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=1738190566; 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=s6D2Er1AWUGlWuHRy7b4wDChkIROtbX0tri1lKXEI3Q=; b=bRcaDp4I25J6ftYuLZJPD1AIKV3M/ZCDgLHcXupd4oSVdJrl1+Vfuz59MJJi/KxQd9fFvJ BhEjTtKc/RVCYLYV+uoI4bRTKms6npBTRDU+42jlUn1QodzsL5fOaqPpIrgQMHNklA50cB YnLCLSIEueRQchySTEaJfXfxZ9uHqxw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=GZ2kEE9+; spf=pass (imf16.hostedemail.com: domain of 35a6aZwQKCPAXnVdYggYdW.Ugedafmp-eecnSUc.gjY@flex--fvdl.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=35a6aZwQKCPAXnVdYggYdW.Ugedafmp-eecnSUc.gjY@flex--fvdl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738190566; a=rsa-sha256; cv=none; b=YRdHqZnlhvluSFuGe2VO4uVRYmjrl8M4L6+W8SRiYBDZz2yJIVMIY8z8W++Wg+yHKt6wOM udb1d2WGh4MXAQB6S56ILgLx3fbeVSHmR7iDr5Q/PPBLff3N74vaef4Uine/rXq8J39oj6 Hk4ZWc2xIvl6xjWgdV32B63uJQ6HFAM= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef7fbd99a6so231735a91.1 for ; Wed, 29 Jan 2025 14:42:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190565; x=1738795365; 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=s6D2Er1AWUGlWuHRy7b4wDChkIROtbX0tri1lKXEI3Q=; b=GZ2kEE9+aHg6UamRx1LFZCTm4WWgRtmF92xozCNLmRTUKO9DiVQ4FTuWCuJP4ZiZfu MFGFyeHXfIsqmj12n0O+/8wrOMAtFM26VmyLyj+p8n0AJrgH++ovUbxxggEtbuAwwBSw i6SB2aTeTFuDuN8pwguq43Bfsl8qMWADlEjXbSEb5aTAHy62a7C47of4PlJbYOuY1Xfw Oyksky3rsa0WdL+inMJAuwYd53gk3WTQnx2Lvqq2taz7pzoCJGTQuISBYkRZ95jWXje6 364b5gXPqkU16W5uytO1FXa+R46sKaKmimLXLJYrnqoP8BmeAaW3kdIGABqhM0hjmjIx QLaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190565; x=1738795365; 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=s6D2Er1AWUGlWuHRy7b4wDChkIROtbX0tri1lKXEI3Q=; b=UqoMeRe5f2VdLCyTKx05OUQUjNE+ExT2pwYZY9xs80tlvjxRVYKKyj3KW3myVnR/Zo O4RPwFq6+eZ8Szd4huxsk3DKIfZJek122STNEl3gMt8ydlRW8fLlQVCWG3HXjvbVxvbH curcux9alpIWpIPpje5LsE4iO7gMCaS+82MncPpE/NcXpPZzEHpLOGlHCNEax9Glg3yr 48uNYFaepn5gGH749U9sgO4EbGSy+KeI6wQh2ZNqUGtYkG377Zu/JS9G0c6ifXf2NBqM AnSysWmOCrDdoZhjsJGBlerqL0CuP6SkRowF3GTgoP0Vzx1K8WhAZ4xA8gfq2p4n5tL0 DqwA== X-Forwarded-Encrypted: i=1; AJvYcCUeAZTVc6s733EtJwsoXVppzjGosU1VOC8WJBl4lsuDKS69Eu3Kj0k2f0Fmio3qtxaTJvc5A85Q5w==@kvack.org X-Gm-Message-State: AOJu0YxVV2sdnzYZqk3I9UY5AR6tDmbdGWnRPjX/RFbYwbjWUnh9Jmfn XPFlqQPh6F8lApj/q3DwDyT54gdlqvyFZh01q4FzzzTCRfExAEiFcbxiquEU4C8T88Nfdw== X-Google-Smtp-Source: AGHT+IFTdSQExYmjG/I0+G58ZhbdOePk8o9BWwD8WMnaUq4tuY2bhx7wjPi5ZMIwpT+tjSLMeHFg+Bgp X-Received: from pfav7.prod.google.com ([2002:a05:6a00:ab07:b0:728:b8e3:993f]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:acc:b0:725:aa5d:f217 with SMTP id d2e1a72fcca58-72fd0be4e2fmr6604034b3a.7.1738190565418; Wed, 29 Jan 2025 14:42:45 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:47 +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-19-fvdl@google.com> Subject: [PATCH v2 18/28] mm/hugetlb: add pre-HVO framework 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: rspam09 X-Rspamd-Queue-Id: 97383180017 X-Stat-Signature: 6e4fu6f5trq6qwdx1qnzaecbmba3dg6q X-HE-Tag: 1738190566-208902 X-HE-Meta: U2FsdGVkX18FoLJ/SDF5nbrlGTtM3AfxdwcSVQdFl2a1w0agr0UJeglUQgmY3XKTdf/QInD1cwKKCDAQMx/USuE3PWLjpIUytw/kvroDjG9Drcny46HTGFdIe+T7kfO4bEyf8+PbQrDm96L0fvkuFqqu+QOqB88JWUcTriVQF7YlVqpTVOibeli5rmu3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fCdzWl6aa8n3FgaUjKNT9R8MiiZTRPJ7HUdTuKv0teXHP+DmnGU5UZaWzlEE8W7YY7VcGJA4L1x0iqoBYH4+666SEvXQdpZeTxQ3tbCAiDTw9nT+A7ib1PU8zBeKaMUtwgvP9OuClyTYSr96jI8LNFLPWXbECoMU6VwEijxs+X3QDm4pBMeoXowMA0yyHo1DlsjFffzVZVDR0FVJCmaPFXNVYaOoBfH0xAePpbTCL6KL3fJxtiKokpw== 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: Define flags for pre-HVOed bootmem hugetlb pages, and act on them. The most important flag is the HVO flag, signalling that a bootmem allocated gigantic page has already been HVO-ed. If this flag is seen by the hugetlb bootmem gather code, the page is marked as HVO optimized. The HVO code will then not try to optimize it again. Instead, it will just map the tail page mirror pages read-only, completing the HVO steps. No functional change, as nothing sets the flags yet. Signed-off-by: Frank van der Linden --- arch/powerpc/mm/hugetlbpage.c | 1 + include/linux/hugetlb.h | 4 +++ mm/hugetlb.c | 24 ++++++++++++++++- mm/hugetlb_vmemmap.c | 50 +++++++++++++++++++++++++++++++++-- mm/hugetlb_vmemmap.h | 15 +++++++++++ 5 files changed, 91 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/mm/hugetlbpage.c b/arch/powerpc/mm/hugetlbpage.c index 6b043180220a..d3c1b749dcfc 100644 --- a/arch/powerpc/mm/hugetlbpage.c +++ b/arch/powerpc/mm/hugetlbpage.c @@ -113,6 +113,7 @@ static int __init pseries_alloc_bootmem_huge_page(struct hstate *hstate) gpage_freearray[nr_gpages] = 0; list_add(&m->list, &huge_boot_pages[0]); m->hstate = hstate; + m->flags = 0; return 1; } diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 5061279e5f73..10a7ce2b95e1 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -681,8 +681,12 @@ struct hstate { struct huge_bootmem_page { struct list_head list; struct hstate *hstate; + unsigned long flags; }; +#define HUGE_BOOTMEM_HVO 0x0001 +#define HUGE_BOOTMEM_ZONES_VALID 0x0002 + int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list); int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn); struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 7879e772c0d9..b48f8638c9af 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3220,6 +3220,7 @@ int __alloc_bootmem_huge_page(struct hstate *h, int nid) INIT_LIST_HEAD(&m->list); list_add(&m->list, &huge_boot_pages[node]); m->hstate = h; + m->flags = 0; return 1; } @@ -3287,7 +3288,7 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h, struct folio *folio, *tmp_f; /* Send list for bulk vmemmap optimization processing */ - hugetlb_vmemmap_optimize_folios(h, folio_list); + hugetlb_vmemmap_optimize_bootmem_folios(h, folio_list); list_for_each_entry_safe(folio, tmp_f, folio_list, lru) { if (!folio_test_hugetlb_vmemmap_optimized(folio)) { @@ -3316,6 +3317,13 @@ static bool __init hugetlb_bootmem_page_zones_valid(int nid, unsigned long start_pfn; bool valid; + if (m->flags & HUGE_BOOTMEM_ZONES_VALID) { + /* + * Already validated, skip check. + */ + return true; + } + start_pfn = virt_to_phys(m) >> PAGE_SHIFT; valid = !pfn_range_intersects_zones(nid, start_pfn, @@ -3348,6 +3356,11 @@ static void __init hugetlb_bootmem_free_invalid_page(int nid, struct page *page, } } +static bool __init hugetlb_bootmem_page_prehvo(struct huge_bootmem_page *m) +{ + return (m->flags & HUGE_BOOTMEM_HVO); +} + /* * Put bootmem huge pages into the standard lists after mem_map is up. * Note: This only applies to gigantic (order > MAX_PAGE_ORDER) pages. @@ -3388,6 +3401,15 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid) hugetlb_folio_init_vmemmap(folio, h, HUGETLB_VMEMMAP_RESERVE_PAGES); init_new_hugetlb_folio(h, folio); + + if (hugetlb_bootmem_page_prehvo(m)) + /* + * If pre-HVO was done, just set the + * flag, the HVO code will then skip + * this folio. + */ + folio_set_hugetlb_vmemmap_optimized(folio); + list_add(&folio->lru, &folio_list); /* diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 5b484758f813..be6b33ecbc8e 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -649,14 +649,39 @@ static int hugetlb_vmemmap_split_folio(const struct hstate *h, struct folio *fol return vmemmap_remap_split(vmemmap_start, vmemmap_end, vmemmap_reuse); } -void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list) +static void __hugetlb_vmemmap_optimize_folios(struct hstate *h, + struct list_head *folio_list, + bool boot) { struct folio *folio; + int nr_to_optimize; LIST_HEAD(vmemmap_pages); unsigned long flags = VMEMMAP_REMAP_NO_TLB_FLUSH | VMEMMAP_SYNCHRONIZE_RCU; + nr_to_optimize = 0; list_for_each_entry(folio, folio_list, lru) { - int ret = hugetlb_vmemmap_split_folio(h, folio); + int ret; + unsigned long spfn, epfn; + + if (boot && folio_test_hugetlb_vmemmap_optimized(folio)) { + /* + * Already optimized by pre-HVO, just map the + * mirrored tail page structs RO. + */ + spfn = (unsigned long)&folio->page; + epfn = spfn + pages_per_huge_page(h); + vmemmap_wrprotect_hvo(spfn, epfn, folio_nid(folio), + HUGETLB_VMEMMAP_RESERVE_SIZE); + register_page_bootmem_memmap(pfn_to_section_nr(spfn), + &folio->page, + HUGETLB_VMEMMAP_RESERVE_SIZE); + static_branch_inc(&hugetlb_optimize_vmemmap_key); + continue; + } + + nr_to_optimize++; + + ret = hugetlb_vmemmap_split_folio(h, folio); /* * Spliting the PMD requires allocating a page, thus lets fail @@ -668,6 +693,16 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l break; } + if (!nr_to_optimize) + /* + * All pre-HVO folios, nothing left to do. It's ok if + * there is a mix of pre-HVO and not yet HVO-ed folios + * here, as __hugetlb_vmemmap_optimize_folio() will + * skip any folios that already have the optimized flag + * set, see vmemmap_should_optimize_folio(). + */ + goto out; + flush_tlb_all(); list_for_each_entry(folio, folio_list, lru) { @@ -693,10 +728,21 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l } } +out: flush_tlb_all(); free_vmemmap_page_list(&vmemmap_pages); } +void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list) +{ + __hugetlb_vmemmap_optimize_folios(h, folio_list, false); +} + +void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, struct list_head *folio_list) +{ + __hugetlb_vmemmap_optimize_folios(h, folio_list, true); +} + static const struct ctl_table hugetlb_vmemmap_sysctls[] = { { .procname = "hugetlb_optimize_vmemmap", diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 2fcae92d3359..a6354a27e63f 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -24,6 +24,8 @@ long hugetlb_vmemmap_restore_folios(const struct hstate *h, struct list_head *non_hvo_folios); void hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *folio); void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list); +void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, struct list_head *folio_list); + static inline unsigned int hugetlb_vmemmap_size(const struct hstate *h) { @@ -64,6 +66,19 @@ static inline void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list { } +static inline void hugetlb_vmemmap_init_early(int nid) +{ +} + +static inline void hugetlb_vmemmap_init_late(int nid) +{ +} + +static inline void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, + struct list_head *folio_list) +{ +} + static inline unsigned int hugetlb_vmemmap_optimizable_size(const struct hstate *h) { return 0;