From nobody Tue Feb 10 00:22:28 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 8A7B41EEA50 for ; Mon, 27 Jan 2025 23:22:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738020175; cv=none; b=SR3ow7GdU34d2vH9LnKzUF3yilDAqgyHwSJc8Ms3QC3NrS4312+FKZWmBpNHm6DrNm4EocGxRB3LCwRGCgXW4sshwWHznoWABiC5vuYDXOD2sFOpbM1COxSi71EeWHAZasb9X51mt6HPT3GnKlYo3wkSeUE0U+Co2bFgsp1ZsOE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738020175; c=relaxed/simple; bh=nxAzZZDSORA1VV2S2OXdtkpHl2+PzN0NOB+60GwqDN0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pGjBUzwNdTY3BF42Gzsen2PpPonjCo68qPoZJ9mQaOqzAoyqAcNj+aZ0d3P0PDo+CIFMj2BwHAZM3aF9gNPJKwQwJ/wZ7z5B1UusKnBagdm0PPd3mK9kHNNltKk++LTEpJVeS/1ic1zSQvTDhGwvnLWP9a2vG15X03eUmhOI8sQ= 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=CBdGpm+w; arc=none smtp.client-ip=209.85.216.73 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="CBdGpm+w" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f550d28f7dso9574151a91.3 for ; Mon, 27 Jan 2025 15:22:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738020173; x=1738624973; 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=pZnSVaYX+KiYCdXZ4CmyGFw5hmL1VdfR5XYIpStDEuk=; b=CBdGpm+wsCWliZx2bjbkXzJVVpBXhcDUySmGbcKBJQhNXCgproFtBeoBZPNRfnqTUY Y4yYSqdgn7XEcSvWdxNTvvDMoEm3BUNVap2xNTGoHS6sleO363upsq91YkaQ6Tvpy458 4jJbbfBHo8SPm0wWD1bhmPRVV8YvS41KuSCPv2APqWYPYnppvnTvbzgHfE4qIAWYFFYn 3dkHA1vK3W2h1avrLMXEOSq5V6apMdSvS9LfwZLyzeJSJTcpl1TaW8gFnIUl4a1xVdoQ xKO9PlXbbnNBNrHp4tdztdlsJ8WHt6eaBeDhSB+KYDh3WkLBD45ssFLN9ER61JGLJu+s O3Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738020173; x=1738624973; 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=pZnSVaYX+KiYCdXZ4CmyGFw5hmL1VdfR5XYIpStDEuk=; b=hszob8ittmU7cLNcYa9rFfyn7YfI8NugW/Cw1DiR2vmEQo7jTbzpuHeP6SidsOxRRc TihPHygNPgKvvLlZoJjRaV+IPFUTfVhZmp0kkkRXiVxSn+8itehi/j7AUzmAE5Zh5HrU r7p7O54+lS5HicDKfguZEyuIpWN/1D3aI6u8XbE2phMtGNdgCq15CUynELBqMgdIoki1 RbYGpN/ebuaGgbaUsY3oTtqMAERjz1h37LmgkIXv//A8s20cZz1d6ue/wjAkPILWbX56 de+Pf6vrmBpiY36FUt4zpeNVFT0G/ka4GKZWugxSbcsD9hjCuttkF9F51efhzKukoIhB sMRg== X-Forwarded-Encrypted: i=1; AJvYcCUJ6ZFeMzAnWVF91Hetqf/q3E8VAGWxsMZ4AUEGGOxr8765NCVLVTkFswT9i11N3bKk75YfKSi4mlx4T80=@vger.kernel.org X-Gm-Message-State: AOJu0YyPEp2A7d3j7l3iTqOIqYMGpGblWV8g5rTkomyX6Jbb4ky1rhVy 20AEUpZgYp7ZQzaE8ghT0rPsaWE6ACtTaI4vOiTcoACCnhEk1EsSfO6dbVNSFIwuAfq5cw== X-Google-Smtp-Source: AGHT+IFeqEuccYmAhGNha3QlLz2orNvO84nvz+r3SrCrM4Gd+ORaJN3aYgAlJa4q+mgHhPJuRCi37zYw X-Received: from pfbcg7.prod.google.com ([2002:a05:6a00:2907:b0:728:aad0:33a4]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:23c1:b0:726:64a5:5f67 with SMTP id d2e1a72fcca58-72daf99731bmr61371606b3a.12.1738020172884; Mon, 27 Jan 2025 15:22:52 -0800 (PST) Date: Mon, 27 Jan 2025 23:21:58 +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-19-fvdl@google.com> Subject: [PATCH 18/27] 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, 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" 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(struc= t hstate *hstate) gpage_freearray[nr_gpages] =3D 0; list_add(&m->list, &huge_boot_pages[0]); m->hstate =3D hstate; + m->flags =3D 0; return 1; } =20 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; }; =20 +#define HUGE_BOOTMEM_HVO 0x0001 +#define HUGE_BOOTMEM_ZONES_VALID 0x0002 + int isolate_or_dissolve_huge_page(struct page *page, struct list_head *lis= t); int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long en= d_pfn); struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 2aa35c1d112b..05c5a65e605f 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3220,6 +3220,7 @@ int __alloc_bootmem_huge_page(struct hstate *h, int n= id) INIT_LIST_HEAD(&m->list); list_add(&m->list, &huge_boot_pages[node]); m->hstate =3D h; + m->flags =3D 0; return 1; } =20 @@ -3287,7 +3288,7 @@ static void __init prep_and_add_bootmem_folios(struct= hstate *h, struct folio *folio, *tmp_f; =20 /* Send list for bulk vmemmap optimization processing */ - hugetlb_vmemmap_optimize_folios(h, folio_list); + hugetlb_vmemmap_optimize_bootmem_folios(h, folio_list); =20 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; =20 + if (m->flags & HUGE_BOOTMEM_ZONES_VALID) { + /* + * Already validated, skip check. + */ + return true; + } + start_pfn =3D virt_to_phys(m) >> PAGE_SHIFT; =20 valid =3D !pfn_range_intersects_zones(nid, start_pfn, @@ -3348,6 +3356,11 @@ static void __init hugetlb_bootmem_free_invalid_page= (int nid, struct page *page, } } =20 +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(unsi= gned 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); =20 /* diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 326cdf94192e..4eddf3c30d62 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -649,14 +649,39 @@ static int hugetlb_vmemmap_split_folio(const struct h= state *h, struct folio *fol return vmemmap_remap_split(vmemmap_start, vmemmap_end, vmemmap_reuse); } =20 -void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *f= olio_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 =3D VMEMMAP_REMAP_NO_TLB_FLUSH | VMEMMAP_SYNCHRONIZE_= RCU; =20 + nr_to_optimize =3D 0; list_for_each_entry(folio, folio_list, lru) { - int ret =3D 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 =3D (unsigned long)&folio->page; + epfn =3D 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 =3D hugetlb_vmemmap_split_folio(h, folio); =20 /* * 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; } =20 + 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(); =20 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 } } =20 +out: flush_tlb_all(); free_vmemmap_page_list(&vmemmap_pages); } =20 +void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *f= olio_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 struct ctl_table hugetlb_vmemmap_sysctls[] =3D { { .procname =3D "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 *f= olio_list); +void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, struct list= _head *folio_list); + =20 static inline unsigned int hugetlb_vmemmap_size(const struct hstate *h) { @@ -64,6 +66,19 @@ static inline void hugetlb_vmemmap_optimize_folios(struc= t hstate *h, struct list { } =20 +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 h= state *h) { return 0; --=20 2.48.1.262.g85cc9f2d1e-goog